]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
build: Perform BUILDROOT check in C
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 17 Mar 2023 13:05:21 +0000 (13:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 17 Mar 2023 13:05:21 +0000 (13:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/build.c
src/libpakfire/file.c
src/libpakfire/include/pakfire/file.h

index ffc7599fdbef2583bef9d045fb835830dc267d82..efd9a297e2cf58c1743eab06a28c1a9f3a7d55fa 100644 (file)
@@ -720,7 +720,6 @@ tests_parser_test_LDADD = \
 # ------------------------------------------------------------------------------
 
 dist_scripts_SCRIPTS = \
-       src/scripts/check-buildroot \
        src/scripts/check-hardening \
        src/scripts/check-interpreters \
        src/scripts/check-rpaths \
index db54fdc5d7af0c1753cdf5131997f0469a42ab57..0bf09345e79fbbe1d43e9ab8f2cae7eb8971722f 100644 (file)
@@ -1219,6 +1219,33 @@ static int pakfire_build_post_check_filesystem(
                PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY);
 }
 
+/*
+       BUILDROOT Check
+*/
+static int pakfire_build_post_check_buildroot(
+               struct pakfire_build* build, struct pakfire_filelist* filelist) {
+       const char* buildroot = pakfire_relpath(build->pakfire, build->buildroot);
+
+       // Nested function to keep a reference to buildroot
+       int __pakfire_build_post_check_buildroot(
+                       struct pakfire* pakfire, struct pakfire_file* file, void* data) {
+               struct pakfire_filelist* matches = (struct pakfire_filelist*)data;
+               int r;
+
+               if (pakfire_file_payload_matches(file, buildroot, strlen(buildroot))) {
+                       r = pakfire_filelist_add(matches, file);
+                       if (r)
+                               return r;
+               }
+
+               return 0;
+       }
+
+       return pakfire_build_post_process_files(
+               build, filelist, "Files containing BUILDROOT:",
+               __pakfire_build_post_check_buildroot, PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY);
+}
+
 /*
        Hardening
 */
@@ -1309,6 +1336,11 @@ static int pakfire_build_run_post_build_checks(struct pakfire_build* build) {
        if (r)
                goto ERROR;
 
+       // Check for BUILDROOT
+       r = pakfire_build_post_check_buildroot(build, filelist);
+       if (r)
+               goto ERROR;
+
        // Check hardening
        r = pakfire_build_post_check_hardening(build, filelist);
        if (r)
@@ -1324,7 +1356,6 @@ ERROR:
 static const char* post_build_scripts[] = {
        "check-unsafe-files",
        "check-rpaths",
-       "check-buildroot",
        "check-hardening",
        "check-interpreters",
        "compress-man-pages",
index a5110880ff665e34608ffcf88a00aa24932ef77c..1da33fbfd786da25bac6bf4388685f0b25b04a55 100644 (file)
@@ -987,6 +987,52 @@ FILE* pakfire_file_open(struct pakfire_file* file) {
        return f;
 }
 
+int pakfire_file_payload_matches(struct pakfire_file* file,
+               const void* needle, const size_t length) {
+       char buffer[1024 * 1024];
+       FILE* f = NULL;
+       void* p = NULL;
+       int r;
+
+       // Only run for regular files
+       if (!S_ISREG(file->st.st_mode))
+               return 0;
+
+       // Open the file
+       f = pakfire_file_open(file);
+       if (!f)
+               goto ERROR;
+
+       printf("needle = %.*s\n", length, (const char*)needle);
+
+       while (!feof(f)) {
+               size_t bytes_read = fread(buffer, 1, sizeof(buffer), f);
+
+               // Raise any reading errors
+               if (ferror(f)) {
+                       r = 1;
+                       goto ERROR;
+               }
+
+               // Search for the needle
+               p = memmem(buffer, bytes_read, needle, length);
+               printf("p = %p\n", p);
+               if (p) {
+                       r = 1;
+                       goto ERROR;
+               }
+       }
+
+       // No match
+       r = 0;
+
+ERROR:
+       if (f)
+               fclose(f);
+
+       return r;
+}
+
 static int __pakfire_file_compute_digests(struct pakfire_file* file,
                struct pakfire_digests* digests, const int types) {
        FILE* f = NULL;
index e2fdecff03c525558f9dc66cc070a0f26c7bef11..e9d190cca530a950a4f29135d604b5df2adc0236 100644 (file)
@@ -154,6 +154,9 @@ int pakfire_file_set_abspath(struct pakfire_file* file, const char* path);
 
 FILE* pakfire_file_open(struct pakfire_file* file);
 
+int pakfire_file_payload_matches(struct pakfire_file* file,
+       const void* needle, const size_t length);
+
 int pakfire_file_compute_digests(struct pakfire_file* file, const int types);
 
 int pakfire_file_remove(struct pakfire_file* file);