summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle Keen <keenerd@gmail.com>2016-07-21 11:54:49 -0400
committerKyle Keen <keenerd@gmail.com>2016-07-21 11:54:49 -0400
commit2746b1ded157f89a50dd1be59932f5b5a044e8f4 (patch)
tree1b091fbe33499b9f570aea9c4707fda9c370be73
parent09a4007391cf78c93f08acc11b45d13e64b920a2 (diff)
downloadnamcap-2746b1ded157f89a50dd1be59932f5b5a044e8f4.tar.gz
namcap-2746b1ded157f89a50dd1be59932f5b5a044e8f4.zip
Consolidate all the adhoc file magic tests
This should also speed up namcap a little, since it replaces full extraction and tempfiles with a small in-place read(). Signed-off-by: Kyle Keen <keenerd@gmail.com>
-rw-r--r--Namcap/depends.py1
-rw-r--r--Namcap/rules/anyelf.py6
-rw-r--r--Namcap/rules/elffiles.py7
-rw-r--r--Namcap/rules/rpath.py4
-rw-r--r--Namcap/rules/shebangdepends.py7
-rw-r--r--Namcap/rules/sodepends.py6
-rw-r--r--Namcap/rules/unusedsodepends.py4
-rw-r--r--Namcap/util.py27
8 files changed, 32 insertions, 30 deletions
diff --git a/Namcap/depends.py b/Namcap/depends.py
index 3c115bf..af11c88 100644
--- a/Namcap/depends.py
+++ b/Namcap/depends.py
@@ -24,7 +24,6 @@
import re, os, os.path
import subprocess
import tempfile
-from Namcap.util import is_elf, script_type
from Namcap.ruleclass import *
import Namcap.tags
from Namcap import package
diff --git a/Namcap/rules/anyelf.py b/Namcap/rules/anyelf.py
index 90172a3..1e497e4 100644
--- a/Namcap/rules/anyelf.py
+++ b/Namcap/rules/anyelf.py
@@ -22,7 +22,7 @@ Check for ELF files to see if a package should be 'any' architecture
"""
import os, re
-from Namcap.util import is_elf, clean_filename
+from Namcap.util import is_elf, is_static, clean_filename
from Namcap.ruleclass import *
class package(TarballRule):
@@ -38,8 +38,8 @@ class package(TarballRule):
if not entry.isfile():
continue
f = tar.extractfile(entry)
- # Archive files are considered as ELF (FS#24854)
- if f.read(4) in (b"\x7fELF", b"!<ar"):
+ # Ar files (static libs) are also architecture specific (FS#24854)
+ if is_elf(f) or is_static(f):
found_elffiles.append(entry.name)
f.close()
diff --git a/Namcap/rules/elffiles.py b/Namcap/rules/elffiles.py
index f8f16ac..68167bd 100644
--- a/Namcap/rules/elffiles.py
+++ b/Namcap/rules/elffiles.py
@@ -51,7 +51,7 @@ class ELFPaths(TarballRule):
continue
# is it an ELF file ?
f = tar.extractfile(entry)
- if f.read(4) == b"\x7fELF":
+ if is_elf(f):
invalid_elffiles.append(entry.name)
self.errors = [("elffile-not-in-allowed-dirs %s", i)
@@ -62,13 +62,12 @@ def _test_elf_and_extract(tar, entry):
if not entry.isfile():
return
f = tar.extractfile(entry)
- magic = f.read(4)
- if magic != b"\x7fELF":
+ if not is_elf(f):
return
# read the rest of file
tmp = tempfile.NamedTemporaryFile(delete=False)
- tmp.write(magic + f.read())
+ tmp.write(f.read())
tmp.close()
return tmp.name
diff --git a/Namcap/rules/rpath.py b/Namcap/rules/rpath.py
index a2d1193..f8e918f 100644
--- a/Namcap/rules/rpath.py
+++ b/Namcap/rules/rpath.py
@@ -58,11 +58,11 @@ class package(TarballRule):
continue
# is it an ELF file ?
+ if not is_elf(tar.extractfile(entry)):
+ continue # not an ELF file
f = tar.extractfile(entry)
elf = f.read()
f.close()
- if elf[:4] != b"\x7fELF":
- continue # not an ELF file
# write it to a temporary file
f = tempfile.NamedTemporaryFile(delete = False)
diff --git a/Namcap/rules/shebangdepends.py b/Namcap/rules/shebangdepends.py
index bac153f..088d888 100644
--- a/Namcap/rules/shebangdepends.py
+++ b/Namcap/rules/shebangdepends.py
@@ -27,7 +27,7 @@ import tempfile
import shutil
import pyalpm
import Namcap.package
-from Namcap.util import script_type
+from Namcap.util import is_script, script_type
from Namcap.ruleclass import *
def scanshebangs(fileobj, filename, scripts):
@@ -39,12 +39,11 @@ def scanshebangs(fileobj, filename, scripts):
"""
# test magic bytes
- magic = fileobj.read(2)
- if magic != b"#!":
+ if not is_script(fileobj):
return
# read the rest of file
tmp = tempfile.NamedTemporaryFile(delete=False)
- tmp.write(magic + fileobj.read())
+ tmp.write(fileobj.read())
tmp.close()
try:
diff --git a/Namcap/rules/sodepends.py b/Namcap/rules/sodepends.py
index 6832ae0..bb17cb1 100644
--- a/Namcap/rules/sodepends.py
+++ b/Namcap/rules/sodepends.py
@@ -27,6 +27,7 @@ import os
import subprocess
import Namcap.package
from Namcap.ruleclass import *
+from Namcap.util import is_elf
from elftools.elf.enums import ENUM_D_TAG
from elftools.elf.elffile import ELFFile
@@ -43,12 +44,9 @@ def scanlibs(fileobj, filename):
returns: a dictionary { library => set(ELF files using that library) }
"""
- # test magic bytes
- magic = fileobj.read(4)
- if magic[:4] != b"\x7fELF":
+ if not is_elf(fileobj):
return {}
- fileobj.seek(0)
elffile = ELFFile(fileobj)
sharedlibs = defaultdict(set)
for section in elffile.iter_sections():
diff --git a/Namcap/rules/unusedsodepends.py b/Namcap/rules/unusedsodepends.py
index 82bfcab..fe2045c 100644
--- a/Namcap/rules/unusedsodepends.py
+++ b/Namcap/rules/unusedsodepends.py
@@ -54,11 +54,11 @@ class package(TarballRule):
continue
# is it an ELF file ?
+ if not is_elf(tar.extractfile(entry)):
+ continue # not an ELF file
f = tar.extractfile(entry)
elf = f.read()
f.close()
- if elf[:4] != b"\x7fELF":
- continue # not an ELF file
# write it to a temporary file
f = tempfile.NamedTemporaryFile(delete = False)
diff --git a/Namcap/util.py b/Namcap/util.py
index 1b47f64..fe7f280 100644
--- a/Namcap/util.py
+++ b/Namcap/util.py
@@ -46,16 +46,23 @@ def _read_carefully(path, readcall):
os.chmod(path, statinfo.st_mode)
return val
-def is_elf(path):
- """
- Given a file path, ensure it exists and peek at the first few bytes
- to determine if it is an ELF file.
- """
- magic = _read_carefully(path, lambda fd: fd.read(4))
- if not magic:
- return False
- # magic elf header, present in binaries and libraries
- return magic == b"\x7FELF"
+def _file_has_magic(fileobj, magic_bytes):
+ length = len(magic_bytes)
+ magic = fileobj.read(length)
+ fileobj.seek(0)
+ return magic == magic_bytes
+
+def is_elf(fileobj):
+ "Take file object, peek at the magic bytes to check if ELF file."
+ return _file_has_magic(fileobj, b"\x7fELF")
+
+def is_static(fileobj):
+ "Take file object, peek at the magic bytes to check if static lib."
+ return _file_has_magic(fileobj, b"!<arch>\n")
+
+def is_script(fileobj):
+ "Take file object, peek at the magic bytes to check if script."
+ return _file_has_magic(fileobj, b"#!")
def script_type(path):
firstline = _read_carefully(path, lambda fd: fd.readline())