]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
file: Check if files are in ELF format
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Mar 2023 15:41:13 +0000 (15:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Mar 2023 15:41:13 +0000 (15:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
configure.ac
src/libpakfire/file.c
src/libpakfire/include/pakfire/file.h

index b7396e3df34604e1742e982f50903339844288c1..6d2a0b7b0324c077813a999a8fa52b4c3b1d2b3e 100644 (file)
@@ -301,6 +301,7 @@ libpakfire_la_CFLAGS = \
        -fvisibility=hidden \
        $(CAP_LIBS) \
        $(CURL_CFLAGS) \
+       $(ELF_CFLAGS) \
        $(JSON_C_CFLAGS) \
        $(MAGIC_CFLAGS) \
        $(MOUNT_CFLAGS) \
@@ -329,6 +330,7 @@ libpakfire_la_LIBADD = \
        $(ARCHIVE_LIBS) \
        $(CAP_LIBS) \
        $(CURL_LIBS) \
+       $(ELF_LIBS) \
        $(GPGME_LIBS) \
        $(JSON_C_LIBS) \
        $(LZMA_LIBS) \
index cf84c5e38ed60971d5a7dc80c9076075ee409731..91029206362264a6b26eda1705df5dfcef786d22 100644 (file)
@@ -255,6 +255,7 @@ AX_PYTHON_MODULE([tornado], [fatal])
 PKG_CHECK_MODULES([ARCHIVE], [libarchive >= 3.4.0])
 PKG_CHECK_MODULES([CAP], [libcap])
 PKG_CHECK_MODULES([CURL], [libcurl])
+PKG_CHECK_MODULES([ELF], [libelf])
 PKG_CHECK_MODULES([GPGME], [gpgme >= 1.6.0])
 PKG_CHECK_MODULES([PYTHON_DEVEL], [python-${PYTHON_VERSION}-embed],
        [], [PKG_CHECK_MODULES([PYTHON_DEVEL], [python-${PYTHON_VERSION}])])
index 1ea290428abd614ed7d4043771f194f4e9ae269b..d1cedb9065e00d67540355bd49b240cdbeb34e6b 100644 (file)
@@ -31,6 +31,8 @@
 
 #include <archive_entry.h>
 
+#include <gelf.h>
+
 #include <pakfire/constants.h>
 #include <pakfire/digest.h>
 #include <pakfire/file.h>
@@ -934,6 +936,17 @@ int pakfire_file_symlink_target_exists(struct pakfire_file* file) {
        Classification
 */
 
+static int setup_libelf(struct pakfire* pakfire) {
+       // Initialize libelf
+       if (elf_version(EV_CURRENT) == EV_NONE) {
+               ERROR(pakfire, "Could not initialize libelf: %s\n", elf_errmsg(-1));
+
+               return 1;
+       }
+
+       return 0;
+}
+
 static int pakfire_file_classify_mode(struct pakfire_file* file) {
        // Check for regular files
        if (S_ISREG(file->st.st_mode))
@@ -1026,6 +1039,54 @@ static int pakfire_file_classify_magic(struct pakfire_file* file) {
        return 0;
 }
 
+static int pakfire_file_classify_elf(struct pakfire_file* file) {
+       FILE* f = NULL;
+       Elf* elf = NULL;
+       int r;
+
+       // Don't run this if we already know that file is an ELF file
+       if (file->class & PAKFIRE_FILE_ELF)
+               return 0;
+
+       // Setup libelf
+       r = setup_libelf(file->pakfire);
+       if (r)
+               return r;
+
+       // Open the file
+       f = fopen(file->abspath, "r");
+       if (!f) {
+               ERROR(file->pakfire, "Could not open %s: %m\n", file->path);
+               return 1;
+       }
+
+       // Try to open the ELF file
+       elf = elf_begin(fileno(f), ELF_C_READ, NULL);
+       if (!elf) {
+               // We fail silently here, because this file might be in a different format
+               goto ERROR;
+       }
+
+       switch (elf_kind(elf)) {
+               // Mark this file as an ELF file
+               case ELF_K_ELF:
+                       file->class |= PAKFIRE_FILE_ELF;
+                       break;
+
+               // Ignore everything else
+               default:
+                       break;
+       }
+
+ERROR:
+       if (elf)
+               elf_end(elf);
+       if (f)
+               fclose(f);
+
+       return 0;
+}
+
 int pakfire_file_classify(struct pakfire_file* file) {
        int r;
 
@@ -1046,6 +1107,11 @@ int pakfire_file_classify(struct pakfire_file* file) {
                        r = pakfire_file_classify_magic(file);
                        if (r)
                                goto ERROR;
+
+                       // Check if the file is an ELF file
+                       r = pakfire_file_classify_elf(file);
+                       if (r)
+                               goto ERROR;
                }
        }
 
index 18366180722d7c5a911c04c1432d6f7fc91f0849..dc26fa84f1032d25ce56201e05d02a99cd8380e3 100644 (file)
@@ -104,11 +104,12 @@ enum pakfire_file_classes {
        PAKFIRE_FILE_SOCKET          = (1 << 6),
 
        // The rest
-       PAKFIRE_FILE_EXECUTABLE      = (1 << 7),
-       PAKFIRE_FILE_PKGCONFIG       = (1 << 8),
-       PAKFIRE_FILE_PERL            = (1 << 9),
-       PAKFIRE_FILE_STATIC_LIBRARY  = (1 << 10),
-       PAKFIRE_FILE_LIBTOOL_ARCHIVE = (1 << 11),
+       PAKFIRE_FILE_ELF             = (1 << 7),
+       PAKFIRE_FILE_EXECUTABLE      = (1 << 8),
+       PAKFIRE_FILE_PKGCONFIG       = (1 << 9),
+       PAKFIRE_FILE_PERL            = (1 << 10),
+       PAKFIRE_FILE_STATIC_LIBRARY  = (1 << 11),
+       PAKFIRE_FILE_LIBTOOL_ARCHIVE = (1 << 12),
 };
 
 int pakfire_file_create_from_path(struct pakfire_file** file,