ld_preload: add MD5 lib checker 36/159636/10
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Fri, 15 Sep 2017 17:19:43 +0000 (20:19 +0300)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Thu, 16 Nov 2017 13:25:45 +0000 (16:25 +0300)
Change-Id: Ideb57ad2db317c8cf6a8f4f64bb45d8d56f3d139
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/da_protocol.c
daemon/da_protocol.h
daemon/da_protocol_inst.c

index b4c53fb3c0f99bcdabc495a37e20c70741a65f1b..6f8ec0292d41c00730b53bed1e5c0847ccf043fc 100644 (file)
@@ -777,9 +777,27 @@ struct binary_ack {
        char *bin_path;
        uint32_t type;
        char *local_bin_path;
-       md5_byte_t digest[16];
+       struct md5sum md5sum;
 };
 
+char *md5sum_to_str(const struct md5sum *md5sum, char *to, size_t buff_len)
+{
+       int i;
+
+       if (buff_len < (sizeof(md5sum->data) * 2 + 1))
+               return NULL;
+
+       for (i = 0; i != sizeof(md5sum->data); ++i) {
+               /* we should use snprintf, snprintf prints data including
+                * terminate '\0' so we need print 3 symbols
+                */
+               snprintf(to, 3, "%02x", md5sum->data[i]);
+               to += 2;
+       }
+
+       return to;
+}
+
 static void binary_ack_free(struct binary_ack *ba)
 {
        if (ba) {
@@ -802,7 +820,6 @@ static size_t binary_ack_pack(char *to, const struct binary_ack *ba)
 {
        char *head;
        size_t len;
-       int i;
 
        head = to;
 
@@ -820,37 +837,40 @@ static size_t binary_ack_pack(char *to, const struct binary_ack *ba)
        memcpy(to, ba->local_bin_path, len);
        to += len;
 
-       for (i = 0; i != 16; ++i) {
-               /* we should use snprintf, snprintf prints data including
-                * terminate '\0' so we need print 3 symbols
-                */
-               snprintf(to, 3, "%02x", ba->digest[i]);
-               to += 2;
-       }
-       *to = '\0';
-       to += 1;
+       /* TODO add bouds check */
+       to = md5sum_to_str(&ba->md5sum, to, sizeof(ba->md5sum) * 2 + 1);
 
 exit:
        return (size_t)(to - head);
 }
 
-static void get_file_md5sum(md5_byte_t digest[16], const char *filename)
+int md5sum_by_file(struct md5sum *md5sum, const char *filename)
 {
        md5_byte_t buffer[1024];
        ssize_t size;
        md5_state_t md5_state;
-       int fd = open(filename, O_RDONLY);
+       int fd;
 
-       md5_init(&md5_state);
-       if (fd >= 0) {
-               while ((size = read(fd, buffer, sizeof(buffer))) > 0)
-                       md5_append(&md5_state, buffer, size);
-               close(fd);
-       } else {
+       fd = open(filename, O_RDONLY);
+
+       if (fd < 0) {
                LOGW("File does not exists <%s>\n", filename);
+               return -EINVAL;
        }
 
-       md5_finish(&md5_state, digest);
+       md5_init(&md5_state);
+       while ((size = read(fd, buffer, sizeof(buffer))) > 0)
+               md5_append(&md5_state, buffer, size);
+       md5_finish(&md5_state, md5sum->data);
+
+       close(fd);
+
+       return 0;
+}
+
+static void md5sum_clean(struct md5sum *md5sum)
+{
+       memset(md5sum->data, 0x00, sizeof(md5sum->data));
 }
 
 static const char* get_basename(const char *filename)
@@ -909,11 +929,11 @@ static struct binary_ack* binary_ack_alloc(const char *filename)
                                 "%s\\%s" : "%s/%s", builddir, get_basename(filename) ?: "");
 
                ba->local_bin_path = strdup(local_bin_path);
-               get_file_md5sum(ba->digest, filename);
+               md5sum_by_file(&ba->md5sum, filename);
        } else {
                ba->type = BINARY_TYPE_FILE_NOT_EXIST;
                ba->local_bin_path = strdup(filename);
-               memset(ba->digest, 0x00, sizeof(ba->digest));
+               md5sum_clean(&ba->md5sum);
        }
 
        return ba;
index aef7b7f6777275066ae2ed7c9daca685000ee8af..7f78195978ccc923d1d46a8e22607c1b6fbcfb65 100644 (file)
@@ -44,6 +44,7 @@ enum {
        MAX_RAW_MESSAGE_SIZE = 8*1024,
 };
 
+#include "md5.h"
 #include "da_msg_ids.h"
 
 enum HostMessageT {
@@ -525,5 +526,12 @@ int parse_string(struct msg_buf_t *msg, char **str);
 int parse_string_no_alloc(struct msg_buf_t *msg, char *str);
 int parse_replay_event_seq(struct msg_buf_t *msg, struct replay_event_seq_t *res);
 
+struct md5sum {
+       md5_byte_t data[16];
+};
+
+int md5sum_by_file(struct md5sum *md5sum, const char *filename);
+char *md5sum_to_str(const struct md5sum *md5sum, char *to, size_t buff_len);
+
 void init_prof_session(struct prof_session_t *prof_session);
 #endif /* _DA_PROTOCOL_ */
index 9cfed14eb146bb0c49c767c10836e1f33e60ddf7..2fa6921f275c4370accbde9c2a9ed440d98ae049 100644 (file)
@@ -31,6 +31,7 @@
 #include "da_protocol.h"
 #include "da_protocol_inst.h"
 #include "da_inst.h"
+#include "md5.h"
 #include "da_protocol_check.h"
 #include "ld_preload_probe_lib.h"
 #include "cpp/features/feature_manager_c.h"
@@ -606,12 +607,46 @@ static int feature_add_inst_lib(struct ld_lib_list_el_t ld_lib,
 
 }
 
+static enum lib_md5_check ld_lib_check_md5(struct ld_lib_list_el_t *lib_el)
+{
+       int res;
+       struct md5sum md5sum;
+       char digest_str[16*2 + 1];
+
+       /* already checked */
+       if (lib_el->md5->check_result != LIB_MD5_NOT_CHECKED)
+               return lib_el->md5->check_result;
+
+       /* try to get md5 */
+       res = md5sum_by_file(&md5sum, lib_el->lib_name);
+       if (res) {
+               LOGW("MD5 file not found \"%s\"\n", lib_el->lib_name);
+               lib_el->md5->check_result = LIB_MD5_CHECK_FAIL;
+               return lib_el->md5->check_result;
+       }
+
+       /* md5 check */
+       md5sum_to_str(&md5sum, digest_str, sizeof(digest_str));
+
+       if (strncmp(digest_str, lib_el->md5->md5_sum,
+                   sizeof(digest_str)) == 0) {
+               lib_el->md5->check_result = LIB_MD5_OK;
+       } else {
+               LOGE("MD5 mismatch \"%s\" target<%s>:buildin<%s>\n",
+                    lib_el->lib_name, digest_str, lib_el->md5->md5_sum);
+               lib_el->md5->check_result = LIB_MD5_CHECK_FAIL;
+       }
+
+       return lib_el->md5->check_result;
+}
+
 int feature_add_lib_inst_list(struct ld_feature_list_el_t *ld_lib_list,
                              struct lib_list_t **lib_list, char *hpath)
 {
 
        uint32_t i = 0, num;
        struct lib_list_t *lib = NULL;
+       struct ld_lib_list_el_t *lib_el = NULL;
 
        num = ld_lib_list->lib_count;
        if (!check_lib_inst_count(num)) {
@@ -620,6 +655,17 @@ int feature_add_lib_inst_list(struct ld_feature_list_el_t *ld_lib_list,
        }
 
        for (i = 0; i < num; i++) {
+               lib_el = &ld_lib_list->libs[i];
+
+               LOGI(">add lib #%d <%s> probes_count=%lu\n", i,
+                    lib_el->lib_name, lib_el->probe_count);
+
+               if (ld_lib_check_md5(lib_el) != LIB_MD5_OK) {
+                       LOGE("WRONG MD5: lib #%d <%s> THIS LIB WILL BE IGNORED\n",
+                            i, lib_el->lib_name);
+                       continue;
+               }
+
                LOGI(">add lib #%d <%s> probes_count=%lu\n", i, ld_lib_list->libs[i].lib_name, ld_lib_list->libs[i].probe_count);
                if (!feature_add_inst_lib(ld_lib_list->libs[i], &lib, hpath)) {
                        // TODO maybe need free allocated memory up there