Add a SHA1 hashsum to the so_info file 30/246730/5
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Tue, 3 Nov 2020 11:38:41 +0000 (12:38 +0100)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Wed, 4 Nov 2020 08:52:38 +0000 (09:52 +0100)
Change-Id: I2508c06f58a9385973511e86737aa6b146fe2de9

src/crash-manager/CMakeLists.txt
src/crash-manager/so-info.c

index 2fdf2a2..4a2fb3b 100644 (file)
@@ -23,6 +23,7 @@ pkg_check_modules(crash-manager_pkgs REQUIRED
        iniparser
        pkgmgr-info
        rpm
+       libcrypto1.1
        )
 
 pkg_check_modules(helper_pkgs REQUIRED
index b6d9af1..a80cc52 100644 (file)
@@ -37,6 +37,7 @@
 #include "shared/log.h"
 #include "shared/util.h"
 #include <tzplatform_config.h>
+#include <openssl/sha.h>
 
 #define BID_SNAME ".note.gnu.build-id"
 
@@ -47,6 +48,58 @@ struct rpm_info {
        char *rpm_info;
 };
 
+static bool sha1sum(const char *filename, char *output)
+{
+       assert(filename);
+       assert(output);
+
+       bool result = false;
+
+       int fd = open(filename, O_RDONLY);
+       if (fd == -1) {
+               _E("Can not open %s to compute the hash sum", filename);
+               return false;
+       }
+
+       SHA_CTX context;
+       if (SHA1_Init(&context) == 0) {
+               _E("Failed to initialize SHA1 context for: %s", filename);
+               goto exit;
+       }
+
+       ssize_t bytes;
+       do {
+               char buff[4096];
+               bytes = read(fd, buff, sizeof(buff));
+               if (bytes < 0) {
+                       if (errno == EAGAIN || errno == EINTR)
+                               continue;
+
+                       _E("Error while reading file %s for checksum: %m", filename);
+                       goto exit;
+               }
+
+               if (SHA1_Update(&context, buff, bytes) == 0) {
+                       _E("Failed to update SHA1 hash sum for: %s", filename);
+                       goto exit;
+               }
+       } while (bytes > 0);
+
+       unsigned char hash[SHA_DIGEST_LENGTH];
+       if (SHA1_Final(hash, &context) == 0) {
+               _E("Failed to compute the SHA1 hash sum for: %s", filename);
+               goto exit;
+       }
+
+       for (size_t i = 0; i < sizeof(hash); i++)
+               snprintf(&output[i*2], 2 + 1, "%02x", hash[i]);
+
+       result = true;
+exit:
+       close(fd);
+       return result;
+}
+
 void rpm_info_free(struct rpm_info* ri)
 {
        free(ri->file_path);
@@ -537,7 +590,12 @@ void get_and_save_so_info(char *map_path, char *out_path)
                        ri->rpm_info = NULL;
                        pkgs_not_found = g_slist_append(pkgs_not_found, ri);
                } else {
-                       int fret = fprintf(f, "%s %s %s\n", file_path, build_id, rpm_info);
+                       char hash_sum[2*SHA_DIGEST_LENGTH + 1];
+
+                       if (!sha1sum(file_path, hash_sum))
+                               snprintf(hash_sum, sizeof(hash_sum), "hash-error");
+
+                       int fret = fprintf(f, "%s %s %s %s\n", file_path, build_id, rpm_info, hash_sum);
                        free(rpm_info);
                        free(build_id);