From 5341b18ecf9caeee2ab5634719f6eb634301cf7e Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Sun, 21 Jan 2018 15:40:58 -0500 Subject: [PATCH] Handle possible pack_fixstr or pack_str32 error Signed-off-by: Mcat12 --- api.c | 72 ++++++++++++++++++++++++++++++------------------- api.h | 4 +-- msgpack.c | 14 +++++++--- socket_client.c | 7 ++--- 4 files changed, 60 insertions(+), 37 deletions(-) diff --git a/api.c b/api.c index fa295c22..dbc6add3 100644 --- a/api.c +++ b/api.c @@ -273,7 +273,10 @@ void getTopDomains(char *client_message, int *sock) else { char *fancyWildcard = calloc(3 + strlen(domains[j].domain), sizeof(char)); sprintf(fancyWildcard, "*.%s", domains[j].domain); - pack_str32(*sock, fancyWildcard); + + if(!pack_str32(*sock, fancyWildcard)) + return; + pack_int32(*sock, domains[j].blockedcount); free(fancyWildcard); } @@ -283,7 +286,9 @@ void getTopDomains(char *client_message, int *sock) if(istelnet[*sock]) ssend(*sock, "%i %i %s\n", n, domains[j].blockedcount, domains[j].domain); else { - pack_str32(*sock, domains[j].domain); + if(!pack_str32(*sock, domains[j].domain)) + return; + pack_int32(*sock, domains[j].blockedcount); } } @@ -295,7 +300,9 @@ void getTopDomains(char *client_message, int *sock) ssend(*sock,"%i %i %s\n",n,(domains[j].count - domains[j].blockedcount),domains[j].domain); else { - pack_str32(*sock, domains[j].domain); + if(!pack_str32(*sock, domains[j].domain)) + return; + pack_int32(*sock, domains[j].count - domains[j].blockedcount); } n++; @@ -391,8 +398,9 @@ void getTopClients(char *client_message, int *sock) ssend(*sock,"%i %i %s %s\n",n,clients[j].count,clients[j].ip,clients[j].name); else { - pack_str32(*sock, clients[j].name); - pack_str32(*sock, clients[j].ip); + if(!pack_str32(*sock, clients[j].name) || !pack_str32(*sock, clients[j].ip)) + return; + pack_int32(*sock, clients[j].count); } n++; @@ -508,8 +516,9 @@ void getForwardDestinations(char *client_message, int *sock) ssend(*sock, "%i %.2f %s %s\n", i, percentage, ip, name); else { - pack_str32(*sock, name); - pack_str32(*sock, ip); + if(!pack_str32(*sock, name) || !pack_str32(*sock, ip)) + return; + pack_float(*sock, (float) percentage); } } @@ -710,11 +719,12 @@ void getAllQueries(char *client_message, int *sock) pack_int32(*sock, queries[i].timestamp); // Use a fixstr because the length of qtype is always 4 (max is 31 for fixstr) - pack_fixstr(*sock, qtype); + if(!pack_fixstr(*sock, qtype)) + return; // Use str32 for domain and client because we have no idea how long they will be (max is 4294967295 for str32) - pack_str32(*sock, domains[queries[i].domainID].domain); - pack_str32(*sock, client); + if(!pack_str32(*sock, domains[queries[i].domainID].domain) || !pack_str32(*sock, client)) + return; pack_uint8(*sock, queries[i].status); pack_uint8(*sock, domains[queries[i].domainID].dnssec); @@ -762,8 +772,8 @@ void getRecentBlocked(char *client_message, int *sock) if(istelnet[*sock]) ssend(*sock,"%s\n", domains[queries[i].domainID].domain); - else - pack_str32(*sock, domains[queries[i].domainID].domain); + else if(!pack_str32(*sock, domains[queries[i].domainID].domain)) + return; } if(found >= num) @@ -840,8 +850,8 @@ void getForwardDestinationsOverTime(int *sock) ip = forwarded[i].ip; } - pack_str32(*sock, name); - pack_str32(*sock, ip); + if(!pack_str32(*sock, name) || !pack_str32(*sock, ip)) + return; } } @@ -994,10 +1004,11 @@ void getVersion(int *sock) if(istelnet[*sock]) ssend(*sock, "version %s\ntag %s\nbranch %s\ndate %s\n", GIT_VERSION, tag, GIT_BRANCH, GIT_DATE); else { - pack_str32(*sock, GIT_VERSION); - pack_str32(*sock, (char *) tag); - pack_str32(*sock, GIT_BRANCH); - pack_str32(*sock, GIT_DATE); + if(!pack_str32(*sock, GIT_VERSION) || + !pack_str32(*sock, (char *) tag) || + !pack_str32(*sock, GIT_BRANCH) || + !pack_str32(*sock, GIT_DATE)) + return; } } else { @@ -1011,10 +1022,11 @@ void getVersion(int *sock) char *hashVersion = calloc(6 + strlen(hash), sizeof(char)); sprintf(hashVersion, "vDev-%s", hash); - pack_str32(*sock, hashVersion); - pack_str32(*sock, (char *) tag); - pack_str32(*sock, GIT_BRANCH); - pack_str32(*sock, GIT_DATE); + if(!pack_str32(*sock, hashVersion) || + !pack_str32(*sock, (char *) tag) || + !pack_str32(*sock, GIT_BRANCH) || + !pack_str32(*sock, GIT_DATE)) + return; free(hashVersion); } @@ -1044,7 +1056,9 @@ void getDBstats(int *sock) else { pack_int32(*sock, get_number_of_queries_in_DB()); pack_int64(*sock, filesize); - pack_str32(*sock, (char *) sqlite3_libversion()); + + if(!pack_str32(*sock, (char *) sqlite3_libversion())) + return; } if(debugclients) @@ -1166,8 +1180,9 @@ void getClientNames(int *sock) if(istelnet[*sock]) ssend(*sock, "%i %i %s %s\n", i, clients[i].count, clients[i].ip, clients[i].name); else { - pack_str32(*sock, clients[i].name); - pack_str32(*sock, clients[i].ip); + if(!pack_str32(*sock, clients[i].name) || !pack_str32(*sock, clients[i].ip)) + return; + pack_int32(*sock, clients[i].count); } } @@ -1213,11 +1228,12 @@ void getUnknownQueries(int *sock) pack_int32(*sock, queries[i].id); // Use a fixstr because the length of qtype is always 4 (max is 31 for fixstr) - pack_fixstr(*sock, type); + if(!pack_fixstr(*sock, type)) + return; // Use str32 for domain and client because we have no idea how long they will be (max is 4294967295 for str32) - pack_str32(*sock, domains[queries[i].domainID].domain); - pack_str32(*sock, client); + if(!pack_str32(*sock, domains[queries[i].domainID].domain) || !pack_str32(*sock, client)) + return; pack_uint8(*sock, queries[i].status); pack_bool(*sock, queries[i].complete); diff --git a/api.h b/api.h index 0ba555b4..c34747f3 100644 --- a/api.h +++ b/api.h @@ -37,6 +37,6 @@ void pack_uint64(int sock, uint64_t value); void pack_int32(int sock, int32_t value); void pack_int64(int sock, int64_t value); void pack_float(int sock, float value); -void pack_fixstr(int sock, char *string); -void pack_str32(int sock, char *string); +bool pack_fixstr(int sock, char *string); +bool pack_str32(int sock, char *string); void pack_map16_start(int sock, uint16_t length); diff --git a/msgpack.c b/msgpack.c index 24eaa362..0fe622de 100644 --- a/msgpack.c +++ b/msgpack.c @@ -74,27 +74,31 @@ void pack_float(int sock, float value) { pack_basic(sock, 0xca, &bigEValue, sizeof(bigEValue)); } -void pack_fixstr(int sock, char *string) { +// Return true if successful +bool pack_fixstr(int sock, char *string) { // Make sure that the length is less than 32 size_t length = strlen(string); if(length >= 32) { logg("Tried to send a fixstr longer than 31 bytes!"); - exit(EXIT_FAILURE); + return false; } uint8_t format = (uint8_t) (0xA0 | length); swrite(sock, &format, sizeof(format)); swrite(sock, string, length); + + return true; } -void pack_str32(int sock, char *string) { +// Return true if successful +bool pack_str32(int sock, char *string) { // Make sure that the length is less than 4294967296 size_t length = strlen(string); if(length >= 2147483648) { logg("Tried to send a str32 longer than 2147483647 bytes!"); - exit(EXIT_FAILURE); + return false; } uint8_t format = 0xdb; @@ -102,6 +106,8 @@ void pack_str32(int sock, char *string) { uint32_t bigELength = htonl((uint32_t) length); swrite(sock, &bigELength, sizeof(bigELength)); swrite(sock, string, length); + + return true; } void pack_map16_start(int sock, uint16_t length) { diff --git a/socket_client.c b/socket_client.c index 37abe99b..5f6c7e17 100644 --- a/socket_client.c +++ b/socket_client.c @@ -16,6 +16,8 @@ #include #include #include +#include + #define BUF 1024 int main (int argc, char **argv) { @@ -38,6 +40,7 @@ int main (int argc, char **argv) { address.sun_family = AF_LOCAL; char *command = ">stats"; + strcpy(address.sun_path,"/var/run/pihole/FTL.sock"); int i; for(i = 1; i < argc; i++) { @@ -50,15 +53,13 @@ int main (int argc, char **argv) { // Set socket file location (respect special location on the CI system Travis) if(strcmp(argv[i], "travis") == 0) strcpy(address.sun_path,"pihole-FTL.sock"); - else - strcpy(address.sun_path,"/var/run/pihole/FTL.sock"); } // Connect to the socket provided by pihole-FTL ret = connect(socketfd, (struct sockaddr *) &address, sizeof (address)); if (ret != 0) { - printf("Error establishing connection!\n"); + printf("Error establishing connection! %s\n", strerror(errno)); exit(EXIT_FAILURE); } printf("Connection established\n");