840325
From 363d7fcf703ad3ebf37b45693b2c9e43eb8b4176 Mon Sep 17 00:00:00 2001
840325
From: Christos Zoulas <christos@zoulas.com>
840325
Date: Sat, 22 Aug 2020 18:04:18 +0000
840325
Subject: [PATCH] Improve detection of static-pie binaries, and don't call them
840325
 "dynamically linked", but call them "static-pie" linked.
840325
840325
---
840325
 src/readelf.c | 37 ++++++++++++++++++++++++++-----------
840325
 1 file changed, 26 insertions(+), 11 deletions(-)
840325
840325
diff --git a/src/readelf.c b/src/readelf.c
840325
index cf1dc91b7..d390d5f6a 100644
840325
--- a/src/readelf.c
840325
+++ b/src/readelf.c
840325
@@ -27,7 +27,7 @@
840325
 #include "file.h"
840325
 
840325
 #ifndef lint
840325
-FILE_RCSID("@(#)$File: readelf.c,v 1.173 2020/06/07 22:12:54 christos Exp $")
840325
+FILE_RCSID("@(#)$File: readelf.c,v 1.174 2020/08/22 18:04:18 christos Exp $")
840325
 #endif
840325
 
840325
 #ifdef BUILTIN_ELF
840325
@@ -1099,7 +1099,7 @@ do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
840325
 
840325
 private size_t
840325
 dodynamic(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
840325
-    int clazz, int swap)
840325
+    int clazz, int swap, int *pie, size_t *need)
840325
 {
840325
 	Elf32_Dyn dh32;
840325
 	Elf64_Dyn dh64;
840325
@@ -1117,11 +1117,15 @@ dodynamic(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
840325
 
840325
 	switch (xdh_tag) {
840325
 	case DT_FLAGS_1:
840325
+		*pie = 1;
840325
 		if (xdh_val & DF_1_PIE)
840325
 			ms->mode |= 0111;
840325
 		else
840325
 			ms->mode &= ~0111;
840325
 		break;
840325
+	case DT_NEEDED:
840325
+		(*need)++;
840325
+		break;
840325
 	default:
840325
 		break;
840325
 	}
840325
@@ -1608,9 +1612,10 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
840325
 }
840325
 
840325
 /*
840325
- * Look through the program headers of an executable image, searching
840325
- * for a PT_INTERP section; if one is found, it's dynamically linked,
840325
- * otherwise it's statically linked.
840325
+ * Look through the program headers of an executable image, to determine
840325
+ * if it is statically or dynamically linked. If it has a dynamic section,
840325
+ * it is pie, and does not have an interpreter or needed libraries, we
840325
+ * call it static pie.
840325
  */
840325
 private int
840325
 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
840325
@@ -1619,12 +1624,13 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
840325
 {
840325
 	Elf32_Phdr ph32;
840325
 	Elf64_Phdr ph64;
840325
-	const char *linking_style = "statically";
840325
+	const char *linking_style;
840325
 	unsigned char nbuf[BUFSIZ];
840325
 	char ibuf[BUFSIZ];
840325
 	char interp[BUFSIZ];
840325
 	ssize_t bufsize;
840325
-	size_t offset, align, len;
840325
+	size_t offset, align, len, need = 0;
840325
+	int pie = 0, dynamic = 0;
840325
 
840325
 	if (num == 0) {
840325
 		if (file_printf(ms, ", no program header") == -1)
840325
@@ -1654,7 +1660,6 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
840325
 		switch (xph_type) {
840325
 		case PT_DYNAMIC:
840325
 			doread = 1;
840325
-			linking_style = "dynamically";
840325
 			break;
840325
 		case PT_NOTE:
840325
 			if (sh_num)	/* Did this through section headers */
840325
@@ -1694,6 +1699,7 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
840325
 		/* Things we can determine when we seek */
840325
 		switch (xph_type) {
840325
 		case PT_DYNAMIC:
840325
+			dynamic = 1;
840325
 			offset = 0;
840325
 			// Let DF_1 determine if we are PIE or not.
840325
 			ms->mode &= ~0111;
840325
@@ -1701,7 +1707,8 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
840325
 				if (offset >= CAST(size_t, bufsize))
840325
 					break;
840325
 				offset = dodynamic(ms, nbuf, offset,
840325
-				    CAST(size_t, bufsize), clazz, swap);
840325
+				    CAST(size_t, bufsize), clazz, swap,
840325
+				    &pie, &need);
840325
 				if (offset == 0)
840325
 					break;
840325
 			}
840325
@@ -1710,6 +1717,7 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
840325
 			break;
840325
 
840325
 		case PT_INTERP:
840325
+			need++;
840325
 			if (ms->flags & MAGIC_MIME)
840325
 				continue;
840325
 			if (bufsize && nbuf[0]) {
840325
@@ -1744,8 +1752,15 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
840325
 	}
840325
 	if (ms->flags & MAGIC_MIME)
840325
 		return 0;
840325
-	if (file_printf(ms, ", %s linked", linking_style)
840325
-	    == -1)
840325
+	if (dynamic) {
840325
+		if (pie && need == 0)
840325
+			linking_style = "static-pie";
840325
+		else
840325
+			linking_style = "dynamically";
840325
+	} else {
840325
+		linking_style = "statically";
840325
+	}
840325
+	if (file_printf(ms, ", %s linked", linking_style) == -1)
840325
 		return -1;
840325
 	if (interp[0])
840325
 		if (file_printf(ms, ", interpreter %s",