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) {
{
char *head;
size_t len;
- int i;
head = to;
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)
"%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;
MAX_RAW_MESSAGE_SIZE = 8*1024,
};
+#include "md5.h"
#include "da_msg_ids.h"
enum HostMessageT {
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_ */
#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"
}
+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)) {
}
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