From 4523c0c2ae946e1e5c982a01c961d3aaacd7858a Mon Sep 17 00:00:00 2001 From: ame Date: Sun, 8 Sep 2024 01:51:33 -0500 Subject: net hardening --- .gitignore | 4 ++++ src/net.c | 13 ++++++++----- src/net/common.h | 1 + src/net/util.c | 20 ++++++++++++++++---- src/types/map.c | 4 ++-- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index c3d01b5..d1b3bd9 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ vgcore.* src/*.o src/*/*.o + +CMakeCache.txt +Makefile +*.cmake diff --git a/src/net.c b/src/net.c index 7db887b..2243117 100644 --- a/src/net.c +++ b/src/net.c @@ -13,7 +13,7 @@ void* handle_client(void *_arg){ int client_fd = args->fd; char* buffer; char dummy[2] = {0, 0}; - int header_eof; + int header_eof = -1; lua_State* L = args->L; //sleep(1); //create state for this thread @@ -43,7 +43,12 @@ void* handle_client(void *_arg){ //read full request //time_start(recv) int64_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof, &read_state); - + /* + for(int i = 0; i != header_eof; i++) + putchar(buffer[i]); + putchar('\n'); + printf("hi %li:%i\n", bytes_received,header_eof); + */ //ignore if header is just fucked if(bytes_received >= -1){ parray_t* table; @@ -67,7 +72,6 @@ void* handle_client(void *_arg){ sprintf(portc, "%i", args->port); str* aa = str_init(portc); - str_push(aa, sk->c); void* v = parray_find(paths, aa->c); @@ -81,7 +85,7 @@ void* handle_client(void *_arg){ int req_idx = lua_gettop(L); lua_newtable(L); int res_idx = lua_gettop(L); - + //handle cookies //TODO: enable and test with valgrind if(0 && sC != NULL){ @@ -205,7 +209,6 @@ void* handle_client(void *_arg){ } parray_clear(table, STR); } - shutdown(client_fd, 2); close(client_fd); free(args); diff --git a/src/net/common.h b/src/net/common.h index 04447f1..4120734 100644 --- a/src/net/common.h +++ b/src/net/common.h @@ -22,6 +22,7 @@ #define max_con 200 //2^42 +#define MAX_HEADER_SIZE (1<<20) #define BUFFER_SIZE 20000 #define HTTP_BUFFER_SIZE 4098 #define max_content_length 200000 diff --git a/src/net/util.c b/src/net/util.c index 213be9f..36f6d39 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -47,6 +47,10 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st } len += n; + if(len >= MAX_HEADER_SIZE){ + *_buffer = buffer; + return -2;//p_fatal("too large"); + } if(*header_eof == -1){ buffer = realloc(buffer, len + BUFFER_SIZE + 1); memset(buffer + len, 0, n + 1); @@ -80,7 +84,14 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){ str_clear(current); item++; if(buffer[oi] == '\n') break; - } else str_pushl(current, buffer + oi, 1); + } else { + str_pushl(current, buffer + oi, 1); + } + } + + if(item != 3){ + *_table = table; + return -1; } int key = 1; @@ -103,10 +114,12 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){ continue; } else str_pushl(current, buffer + i, 1); } - parray_set(table, sw->c, (void*)str_init(current->c)); + if(sw != NULL){ + parray_set(table, sw->c, (void*)str_init(current->c)); + str_free(sw); + } str_free(current); - if(sw != NULL) str_free(sw); *_table = table; return 0; } @@ -135,7 +148,6 @@ void http_build(str** _dest, int code, char* code_det, char* header_vs, char* co * */ void http_code(int code, char* code_det){ - //this was done with a script btw switch(code){ case 100: sprintf(code_det,"Continue"); break; case 101: sprintf(code_det,"Switching Protocols"); break; diff --git a/src/types/map.c b/src/types/map.c index a15762a..31e79a9 100644 --- a/src/types/map.c +++ b/src/types/map.c @@ -115,7 +115,7 @@ void map_clear(map_t* M, enum free_type free){ map_lclear(M); } -int main(){ +int __main(){ int i = 5; int b = 24; int c = 9; @@ -132,4 +132,4 @@ int main(){ map_clear(m, NONE); return 0; -} \ No newline at end of file +} -- cgit v1.2.3