|
|
a800e3 |
From 948d0e24f33c3b411b5ec1e320acec889e6781b8 Mon Sep 17 00:00:00 2001
|
|
|
a800e3 |
From: Vincent Mihalkovic <vmihalko@redhat.com>
|
|
|
a800e3 |
Date: Mon, 6 Feb 2023 15:04:33 +0100
|
|
|
a800e3 |
Subject: [PATCH] Improve detection of static-pie binaries, and don't call them
|
|
|
a800e3 |
"dynamically linked", but call them "static-pie" linked.
|
|
|
a800e3 |
|
|
|
a800e3 |
363d7fcf703ad3ebf37b45693b2c9e43eb8b4176
|
|
|
a800e3 |
---
|
|
|
a800e3 |
src/readelf.c | 35 +++++++++++++++++++++++++----------
|
|
|
a800e3 |
1 file changed, 25 insertions(+), 10 deletions(-)
|
|
|
a800e3 |
|
|
|
a800e3 |
diff --git a/src/readelf.c b/src/readelf.c
|
|
|
a800e3 |
index 9c75c0a..0011659 100644
|
|
|
a800e3 |
--- a/src/readelf.c
|
|
|
a800e3 |
+++ b/src/readelf.c
|
|
|
a800e3 |
@@ -1040,7 +1040,7 @@ do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
|
|
|
a800e3 |
|
|
|
a800e3 |
private size_t
|
|
|
a800e3 |
dodynamic(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
|
|
|
a800e3 |
- int clazz, int swap)
|
|
|
a800e3 |
+ int clazz, int swap, int *pie, size_t *need)
|
|
|
a800e3 |
{
|
|
|
a800e3 |
Elf32_Dyn dh32;
|
|
|
a800e3 |
Elf64_Dyn dh64;
|
|
|
a800e3 |
@@ -1058,11 +1058,15 @@ dodynamic(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
|
|
|
a800e3 |
|
|
|
a800e3 |
switch (xdh_tag) {
|
|
|
a800e3 |
case DT_FLAGS_1:
|
|
|
a800e3 |
+ *pie = 1;
|
|
|
a800e3 |
if (xdh_val & DF_1_PIE)
|
|
|
a800e3 |
ms->mode |= 0111;
|
|
|
a800e3 |
else
|
|
|
a800e3 |
ms->mode &= ~0111;
|
|
|
a800e3 |
break;
|
|
|
a800e3 |
+ case DT_NEEDED:
|
|
|
a800e3 |
+ (*need)++;
|
|
|
a800e3 |
+ break;
|
|
|
a800e3 |
default:
|
|
|
a800e3 |
break;
|
|
|
a800e3 |
}
|
|
|
a800e3 |
@@ -1529,9 +1533,10 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
|
|
|
a800e3 |
}
|
|
|
a800e3 |
|
|
|
a800e3 |
/*
|
|
|
a800e3 |
- * Look through the program headers of an executable image, searching
|
|
|
a800e3 |
- * for a PT_INTERP section; if one is found, it's dynamically linked,
|
|
|
a800e3 |
- * otherwise it's statically linked.
|
|
|
a800e3 |
+ * Look through the program headers of an executable image, to determine
|
|
|
a800e3 |
+ * if it is statically or dynamically linked. If it has a dynamic section,
|
|
|
a800e3 |
+ * it is pie, and does not have an interpreter or needed libraries, we
|
|
|
a800e3 |
+ * call it static pie.
|
|
|
a800e3 |
*/
|
|
|
a800e3 |
private int
|
|
|
a800e3 |
dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
a800e3 |
@@ -1540,12 +1545,13 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
a800e3 |
{
|
|
|
a800e3 |
Elf32_Phdr ph32;
|
|
|
a800e3 |
Elf64_Phdr ph64;
|
|
|
a800e3 |
- const char *linking_style = "statically";
|
|
|
a800e3 |
+ const char *linking_style;
|
|
|
a800e3 |
unsigned char nbuf[BUFSIZ];
|
|
|
a800e3 |
char ibuf[BUFSIZ];
|
|
|
a800e3 |
char interp[BUFSIZ];
|
|
|
a800e3 |
ssize_t bufsize;
|
|
|
a800e3 |
- size_t offset, align, len;
|
|
|
a800e3 |
+ size_t offset, align, len, need = 0;
|
|
|
a800e3 |
+ int pie = 0, dynamic = 0;
|
|
|
a800e3 |
|
|
|
a800e3 |
if (size != xph_sizeof) {
|
|
|
a800e3 |
if (file_printf(ms, ", corrupted program header size") == -1)
|
|
|
a800e3 |
@@ -1569,7 +1575,6 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
a800e3 |
/* Things we can determine before we seek */
|
|
|
a800e3 |
switch (xph_type) {
|
|
|
a800e3 |
case PT_DYNAMIC:
|
|
|
a800e3 |
- linking_style = "dynamically";
|
|
|
a800e3 |
doread = 1;
|
|
|
a800e3 |
break;
|
|
|
a800e3 |
case PT_NOTE:
|
|
|
a800e3 |
@@ -1610,6 +1615,7 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
a800e3 |
/* Things we can determine when we seek */
|
|
|
a800e3 |
switch (xph_type) {
|
|
|
a800e3 |
case PT_DYNAMIC:
|
|
|
a800e3 |
+ dynamic = 1;
|
|
|
a800e3 |
offset = 0;
|
|
|
a800e3 |
// Let DF_1 determine if we are PIE or not.
|
|
|
a800e3 |
ms->mode &= ~0111;
|
|
|
a800e3 |
@@ -1617,7 +1623,8 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
a800e3 |
if (offset >= (size_t)bufsize)
|
|
|
a800e3 |
break;
|
|
|
a800e3 |
offset = dodynamic(ms, nbuf, offset,
|
|
|
a800e3 |
- CAST(size_t, bufsize), clazz, swap);
|
|
|
a800e3 |
+ CAST(size_t, bufsize), clazz, swap,
|
|
|
a800e3 |
+ &pie, &need);
|
|
|
a800e3 |
if (offset == 0)
|
|
|
a800e3 |
break;
|
|
|
a800e3 |
}
|
|
|
a800e3 |
@@ -1626,6 +1633,7 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
a800e3 |
break;
|
|
|
a800e3 |
|
|
|
a800e3 |
case PT_INTERP:
|
|
|
a800e3 |
+ need++;
|
|
|
a800e3 |
if (ms->flags & MAGIC_MIME)
|
|
|
a800e3 |
continue;
|
|
|
a800e3 |
if (bufsize && nbuf[0]) {
|
|
|
a800e3 |
@@ -1660,8 +1668,15 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
|
|
|
a800e3 |
}
|
|
|
a800e3 |
if (ms->flags & MAGIC_MIME)
|
|
|
a800e3 |
return 0;
|
|
|
a800e3 |
- if (file_printf(ms, ", %s linked", linking_style)
|
|
|
a800e3 |
- == -1)
|
|
|
a800e3 |
+ if (dynamic) {
|
|
|
a800e3 |
+ if (pie && need == 0)
|
|
|
a800e3 |
+ linking_style = "static-pie";
|
|
|
a800e3 |
+ else
|
|
|
a800e3 |
+ linking_style = "dynamically";
|
|
|
a800e3 |
+ } else {
|
|
|
a800e3 |
+ linking_style = "statically";
|
|
|
a800e3 |
+ }
|
|
|
a800e3 |
+ if (file_printf(ms, ", %s linked", linking_style) == -1)
|
|
|
a800e3 |
return -1;
|
|
|
a800e3 |
if (interp[0])
|
|
|
a800e3 |
if (file_printf(ms, ", interpreter %s",
|
|
|
a800e3 |
--
|
|
|
a800e3 |
2.39.1
|
|
|
a800e3 |
|