[arch-dev-public] [PATCH 2/4] Make is_elf() more resilient to weird perms

Dan McGee dan at archlinux.org
Tue Jun 15 01:29:24 EDT 2010


Some files in packages do not have the owner read bit set, which means
open() blows up when trying to peek into the file. Add a workaround that
temporarily sets the extracted file to be readable so we can peek at it and
determine if it is an ELF file. For those files that are either links or
something else funky, just assume it is not an ELF file.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 Namcap/util.py |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/Namcap/util.py b/Namcap/util.py
index 8f61c30..3d09212 100644
--- a/Namcap/util.py
+++ b/Namcap/util.py
@@ -19,6 +19,7 @@
 
 import os
 import re
+import stat
 
 def is_elf(path):
 	"""
@@ -27,9 +28,26 @@ def is_elf(path):
 	"""
 	if not os.path.isfile(path):
 		return False
+	reset_perms = False
+	if not os.access(path, os.R_OK):
+		# don't mess with links we can't read
+		if os.path.islink(path):
+			return False
+		reset_perms = True
+		# attempt to make it readable if possible
+		statinfo = os.stat(path)
+		newmode = statinfo.st_mode | stat.S_IRUSR
+		try:
+			os.chmod(path, newmode)
+		except IOError:
+			return False
 	fd = open(path)
 	bytes = fd.read(4)
 	fd.close()
+	# reset permissions if necessary
+	if reset_perms:
+		# set file back to original permissions
+		os.chmod(path, statinfo.st_mode)
 	# magic elf header, present in binaries and libraries
 	if bytes == "\x7FELF":
 		return True
-- 
1.7.1



More information about the arch-dev-public mailing list