From 4330741aa5cdb076b3b08f0f4c9ec40377fba56b Mon Sep 17 00:00:00 2001 From: charliemirabile <46761267+charliemirabile@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:05:29 -0400 Subject: [PATCH] smtp: enforce limit on max email size Cause hard error preventing delivery of email that exceeds a preset limit. We can hook into the existing code that runs each time data is appended to the in progress mail item to enforce the limit and raise an error if it is exceeded. The limit defaults to 64k, but a provision to change it could be added to the makefile trivially, similar to the way that we can adjust the server hostname. --- smtp/smtp.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/smtp/smtp.c b/smtp/smtp.c index bce8675a..c9fb6b24 100644 --- a/smtp/smtp.c +++ b/smtp/smtp.c @@ -21,6 +21,11 @@ #define HOSTNAME #endif +//default to 64k limit for incoming mail. Override with `-DEMAIL_SIZE_LIMIT=whatever` if desired +#ifndef EMAIL_SIZE_LIMIT +#define EMAIL_SIZE_LIMIT 65536 +#endif + #define UPPERCASE_LETTERS(MACRO) \ MACRO('A') \ MACRO('B') \ @@ -686,7 +691,7 @@ static void handle_data(enum state *state) #define ERROR_NES(...) { warnx(__VA_ARGS__); error = NOT_ENOUGH_SPACE; last_state = dstate = BODY; break; } #define ERROR_USI(...) { warnx(__VA_ARGS__); error = USER_SYNTAX_ISSUE; last_state = dstate = BODY; break; } #define ERROR_ISE(...) { warnx(__VA_ARGS__); error = INTERNAL_SERVER_ERROR; last_state = dstate = BODY; break; } - #define CHECK_WRITE_FAIL(VARNAME) { if(0 > VARNAME) ERROR_NES("not enough space %d", __LINE__) else curr_size += (off_t)VARNAME; } + #define CHECK_WRITE_FAIL(VARNAME) { if(curr_size > EMAIL_SIZE_LIMIT || 0 > VARNAME) ERROR_NES("not enough space %d", __LINE__) else curr_size += (off_t)VARNAME; } for(enum data_state dstate = HEADERS, last_state = HEADERS; dstate != FINISHED;) { if(dstate == BODY && last_state == HEADERS)