From patchwork Wed Jan 8 14:25:34 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 22975 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f200.google.com (mail-qc0-f200.google.com [209.85.216.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 80718202E2 for ; Wed, 8 Jan 2014 14:25:15 +0000 (UTC) Received: by mail-qc0-f200.google.com with SMTP id e9sf2163138qcy.11 for ; Wed, 08 Jan 2014 06:25:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe:content-type :content-transfer-encoding; bh=NIzhwXYbIFDfKf/KcSNMA6D1HO4m/7BpyXcaGweNK5E=; b=XlPQ+9LM/mSgpu9kaMvyKsKOwuJcgIq1/YvODUPeIyw28YbLRUUSyR4WA1KS2uSak2 rHk8H3OZgbtsJiSDC7M3VTGb+NdkJgFRafBi30gY7v+TThUjEJsIHNNZcjnM0ci0myjx KVoP/hID141WzcCuZIuLcZq/dhypVsN19s9jp1ma3lexki6Yii3OtDOX5wa8Gz3+Arjv vsVx7OEkyeHUX5T7WqBVByJJoXhWm7+/Uh12wR9wM6X9biR421m76hGyV46GM66vTjRi 2sDT2T5LAL0cLt1IuabQ3b93w33ZMSqrQrQ+zNw92x8q/UKjPxBMFMIsOTqJh+tw1uXe oh7w== X-Gm-Message-State: ALoCoQkrBvEwwp4HN7cldm5o3vAQQxIUysyXMZNWJgC3Y7jesRVYRML6tf08+K6Tq0oVcHU1pBW4 X-Received: by 10.58.238.199 with SMTP id vm7mr47787182vec.17.1389191114682; Wed, 08 Jan 2014 06:25:14 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.17.163 with SMTP id p3ls603629qed.41.gmail; Wed, 08 Jan 2014 06:25:14 -0800 (PST) X-Received: by 10.58.201.169 with SMTP id kb9mr3495487vec.42.1389191114603; Wed, 08 Jan 2014 06:25:14 -0800 (PST) Received: from mail-ve0-f171.google.com (mail-ve0-f171.google.com [209.85.128.171]) by mx.google.com with ESMTPS id sc7si668379vdc.69.2014.01.08.06.25.14 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 08 Jan 2014 06:25:14 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.171; Received: by mail-ve0-f171.google.com with SMTP id pa12so1291259veb.16 for ; Wed, 08 Jan 2014 06:25:14 -0800 (PST) X-Received: by 10.58.181.71 with SMTP id du7mr9226078vec.25.1389191114414; Wed, 08 Jan 2014 06:25:14 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.59.13.131 with SMTP id ey3csp228909ved; Wed, 8 Jan 2014 06:25:13 -0800 (PST) X-Received: by 10.14.224.197 with SMTP id x45mr13656866eep.103.1389191111971; Wed, 08 Jan 2014 06:25:11 -0800 (PST) Received: from socrates.bennee.com (static.88-198-71-155.clients.your-server.de. [88.198.71.155]) by mx.google.com with ESMTPS id l2si93511708een.125.2014.01.08.06.25.11 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 08 Jan 2014 06:25:11 -0800 (PST) Received-SPF: neutral (google.com: 88.198.71.155 is neither permitted nor denied by best guess record for domain of alex.bennee@linaro.org) client-ip=88.198.71.155; Received: from localhost ([127.0.0.1] helo=zen.linaro.local) by socrates.bennee.com with esmtp (Exim 4.80) (envelope-from ) id 1W0u71-0004At-RQ; Wed, 08 Jan 2014 15:28:03 +0100 From: alex.bennee@linaro.org To: qemu-devel@nongnu.org Cc: patches@linaro.org Subject: [PATCH 2/2] scripts/qemu-binfmt-check.py: a binfmt checker Date: Wed, 8 Jan 2014 14:25:34 +0000 Message-Id: <1389191134-16597-3-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 1.8.5.2 In-Reply-To: <1389191134-16597-1-git-send-email-alex.bennee@linaro.org> References: <1389191134-16597-1-git-send-email-alex.bennee@linaro.org> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: alex.bennee@linaro.org X-SA-Exim-Scanned: No (on socrates.bennee.com); SAEximRunCond expanded to false X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: alex.bennee@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Alex Bennée This script allows you to check if a given binary will match against any of the currently registered binfmts on the system. --- v2 (ajb): - cleaned up whitespace and checkpatch fixes --- scripts/qemu-binfmt-check.py | 109 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100755 scripts/qemu-binfmt-check.py diff --git a/scripts/qemu-binfmt-check.py b/scripts/qemu-binfmt-check.py new file mode 100755 index 0000000..c4309a5 --- /dev/null +++ b/scripts/qemu-binfmt-check.py @@ -0,0 +1,109 @@ +#!/usr/bin/python +# +# binfmt check script +# +# Copyright 2014 Linaro +# +# Authors: +# Alex Bennee +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. + +import os +import re +import binascii + +re_int = re.compile(r"interpreter (.+)$") +re_off = re.compile(r"offset (\d+)$") +re_magic = re.compile(r"magic ([\dabcdef]+)") +re_mask = re.compile(r"mask ([\dabcdef]+)") + +# argparse is only available in Python >= 2.7 +from optparse import OptionParser +parser = OptionParser() + +# list of binfmts +binfmts = [] + + +def read_binfmt_spec(f): + bfmt = {} + with open(f) as fd: + content = fd.readlines() + for l in content: + m = re_int.match(l) + if m: + bfmt["interpreter"] = m.group(1) + m = re_off.match(l) + if m: + bfmt["offset"] = int(m.group(1)) + m = re_magic.match(l) + if m: + bfmt["magic"] = binascii.unhexlify(m.group(1)) + m = re_mask.match(l) + if m: + bfmt["mask"] = binascii.unhexlify(m.group(1)) + print "loaded: %s" % bfmt + binfmts.append(bfmt) + + +def load_binfmt_masks(): + binfmt_dir = "/proc/sys/fs/binfmt_misc" + files = os.listdir(binfmt_dir) + for f in files: + if not f.startswith("status"): + fp = "%s/%s" % (binfmt_dir, f) + if os.access(fp, os.R_OK): + read_binfmt_spec(fp) + + +def check_file_against_binfmt(fmt, f): + """ + Check if a file will match a given binfmt mask + """ + print "checking %s" % (f) + nbytes = len(fmt["magic"]) + + fd = open(f, "rb") + fd.seek(fmt["offset"]) + header = fd.read(nbytes) + magic = fmt["magic"] + try: + mask = fmt["mask"] + except: + # TODO, make full mask + return + + values = zip(mask, magic, header) + failed = False + pos = 0 + for m, g, h in values: + mask = ord(m) + bits_to_check = ord(h) & mask + magic = ord(g) + if not bits_to_check == magic: + print "failed at %d (%x, %x, %x)" % (pos, mask, magic, bits_to_check) + failed = True + break + pos += 1 + return not failed + + +def check_file_against_all_binfmts(f): + """ + Check a file against the binfmt masks + """ + path = os.path.abspath(f) + print "file is %s" % (path) + for b in binfmts: + if check_file_against_binfmt(b, path): + print "%s will use %s" % (path, b["interpreter"]) + break + + +if __name__ == "__main__": + (opts, args) = parser.parse_args() + load_binfmt_masks() + for f in args: + check_file_against_all_binfmts(f)