teknoraver / rpms / rpm

Forked from rpms/rpm 3 months ago
Clone

Blame SOURCES/0001-Detect-kernel-modules-by-.modinfo-section-presence-f.patch

f6faf3
From 68d383c39cef8d58b80940b13dd132d3f41a03f0 Mon Sep 17 00:00:00 2001
f6faf3
Message-Id: <68d383c39cef8d58b80940b13dd132d3f41a03f0.1571917458.git.pmatilai@redhat.com>
f6faf3
From: Panu Matilainen <pmatilai@redhat.com>
f6faf3
Date: Tue, 2 Apr 2019 15:22:07 +0300
f6faf3
Subject: [PATCH 1/2] Detect kernel modules by .modinfo section presence for
f6faf3
 build-id generation
f6faf3
f6faf3
File extension based heuristics only work so far at best, and break
f6faf3
completely on compressed files with arbitrary .gz/.xz etc extension.
f6faf3
This isn't supposed to change any behavior as such, only provide more
f6faf3
reliable detection of kernel modules.
f6faf3
---
f6faf3
 build/files.c | 27 ++++++++++++++++++++++++---
f6faf3
 1 file changed, 24 insertions(+), 3 deletions(-)
f6faf3
f6faf3
diff --git a/build/files.c b/build/files.c
f6faf3
index dbad9a7f3..3822be3d3 100644
f6faf3
--- a/build/files.c
f6faf3
+++ b/build/files.c
f6faf3
@@ -1739,6 +1739,28 @@ static int addNewIDSymlink(ARGV_t *files,
f6faf3
     return rc;
f6faf3
 }
f6faf3
 
f6faf3
+static int haveModinfo(Elf *elf)
f6faf3
+{
f6faf3
+    Elf_Scn * scn = NULL;
f6faf3
+    size_t shstrndx;
f6faf3
+    int have_modinfo = 0;
f6faf3
+    const char *sname;
f6faf3
+
f6faf3
+    if (elf_getshdrstrndx(elf, &shstrndx) == 0) {
f6faf3
+	while ((scn = elf_nextscn(elf, scn)) != NULL) {
f6faf3
+	    GElf_Shdr shdr_mem, *shdr = gelf_getshdr(scn, &shdr_mem);
f6faf3
+	    if (shdr == NULL)
f6faf3
+		continue;
f6faf3
+	    sname = elf_strptr(elf, shstrndx, shdr->sh_name);
f6faf3
+	    if (sname && rstreq(sname, ".modinfo")) {
f6faf3
+		have_modinfo = 1;
f6faf3
+		break;
f6faf3
+	    }
f6faf3
+	}
f6faf3
+    }
f6faf3
+    return have_modinfo;
f6faf3
+}
f6faf3
+
f6faf3
 static int generateBuildIDs(FileList fl, ARGV_t *files)
f6faf3
 {
f6faf3
     int rc = 0;
f6faf3
@@ -1803,15 +1825,14 @@ static int generateBuildIDs(FileList fl, ARGV_t *files)
f6faf3
 	    int fd = open (flp->diskPath, O_RDONLY);
f6faf3
 	    if (fd >= 0) {
f6faf3
 		/* Only real ELF files, that are ET_EXEC, ET_DYN or
f6faf3
-		   kernel modules (ET_REL files with names ending in .ko)
f6faf3
+		   kernel modules (ET_REL files with .modinfo section)
f6faf3
 		   should have build-ids. */
f6faf3
 		GElf_Ehdr ehdr;
f6faf3
 		Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
f6faf3
 		if (elf != NULL && elf_kind(elf) == ELF_K_ELF
f6faf3
 		    && gelf_getehdr(elf, &ehdr) != NULL
f6faf3
 		    && (ehdr.e_type == ET_EXEC || ehdr.e_type == ET_DYN
f6faf3
-			|| (ehdr.e_type == ET_REL
f6faf3
-			    && rpmFileHasSuffix (flp->diskPath, ".ko")))) {
f6faf3
+			|| (ehdr.e_type == ET_REL && haveModinfo(elf)))) {
f6faf3
 		    const void *build_id;
f6faf3
 		    ssize_t len = dwelf_elf_gnu_build_id (elf, &build_id);
f6faf3
 		    /* len == -1 means error. Zero means no
f6faf3
-- 
f6faf3
2.21.0
f6faf3