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: 308338 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 669582C00B6 for ; Thu, 9 Jan 2014 01:27:08 +1100 (EST) Received: from localhost ([::1]:46993 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0u65-0000rM-RM for incoming@patchwork.ozlabs.org; Wed, 08 Jan 2014 09:27:05 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54595) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0u4Q-0007bd-90 for qemu-devel@nongnu.org; Wed, 08 Jan 2014 09:25:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W0u4G-00050z-HB for qemu-devel@nongnu.org; Wed, 08 Jan 2014 09:25:22 -0500 Received: from static.88-198-71-155.clients.your-server.de ([88.198.71.155]:60014 helo=socrates.bennee.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W0u4G-00050m-AN for qemu-devel@nongnu.org; Wed, 08 Jan 2014 09:25:12 -0500 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 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-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 88.198.71.155 Cc: patches@linaro.org Subject: [Qemu-devel] [PATCH 2/2] scripts/qemu-binfmt-check.py: a binfmt checker X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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)