Skip to content

Commit edb783e

Browse files
committed
smtp: fix glibcxx assertion failure with zero-length EHLO hostname
Fixes: gromox-2.38-94-g2ff0098a6
1 parent 8829554 commit edb783e

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

include/gromox/flusher_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct GX_EXPORT envelope_info {
3232
void clear();
3333

3434
char parsed_domain[UDOM_SIZE]{"unknown"};
35-
char hello_domain[UDOM_SIZE]{}; /* domain name after helo */
35+
std::string hello_domain; /* domain name after helo */
3636
char from[UADDR_SIZE]{}; /* envelope's from message */
3737
std::vector<std::string> rcpt_to; /* envelope's rcpt to message */
3838
};

mda/message_enqueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ BOOL message_enqueue_try_save_mess(FLUSH_ENTITY *pentity)
235235
auto tmp_len = sprintf(tmp_buff, "X-Lasthop: %s\r\nReceived: from %s "
236236
"(%s [%s%s])\r\n\tby %s with %s%s;\r\n\t%s\r\n",
237237
pentity->pconnection->client_addr,
238-
pentity->penvelope->hello_domain,
238+
pentity->penvelope->hello_domain.c_str(),
239239
pentity->penvelope->parsed_domain,
240240
af_type == AF_INET6 ? "IPv6:" : "",
241241
pentity->pconnection->client_addr,

mda/smtp/cmd.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace gromox;
2222

2323
static BOOL cmdh_check_onlycmd(std::string_view cmd_line, smtp_context &);
2424

25-
int cmdh_helo(std::string_view cmd_line, smtp_context &ctx)
25+
int cmdh_helo(std::string_view cmd_line, smtp_context &ctx) try
2626
{
2727
auto pcontext = &ctx;
2828
int line_length = cmd_line.size();
@@ -33,8 +33,8 @@ int cmdh_helo(std::string_view cmd_line, smtp_context &ctx)
3333
/* 502 Command not implemented */
3434
return 506;
3535
/* copy parameter to hello_domain */
36-
strncpy(pcontext->menv.hello_domain, &cmd_line[5], line_length - 5);
37-
pcontext->menv.hello_domain[line_length-5] = '\0';
36+
cmd_line.remove_prefix(5);
37+
pcontext->menv.hello_domain = cmd_line;
3838
} else if(line_length > 255 + 1 + 4) {
3939
/* domain name too long */
4040
return 502;
@@ -43,9 +43,11 @@ int cmdh_helo(std::string_view cmd_line, smtp_context &ctx)
4343
pcontext->menv.clear();
4444
pcontext->last_cmd = T_HELO_CMD;
4545
return 205;
46-
}
46+
} catch (const std::bad_alloc &) {
47+
return 416; /* ENOMEM */
48+
}
4749

48-
static int cmdh_xhlo(std::string_view cmd_line, smtp_context &ctx)
50+
static int cmdh_xhlo(std::string_view cmd_line, smtp_context &ctx) try
4951
{
5052
auto pcontext = &ctx;
5153
int line_length = cmd_line.size();
@@ -58,8 +60,8 @@ static int cmdh_xhlo(std::string_view cmd_line, smtp_context &ctx)
5860
if (cmd_line[4] != ' ')
5961
return 506;
6062
/* copy parameter to hello_domain */
61-
strncpy(pcontext->menv.hello_domain, &cmd_line[5], line_length - 5);
62-
pcontext->menv.hello_domain[line_length-5] = '\0';
63+
cmd_line.remove_prefix(5);
64+
pcontext->menv.hello_domain = cmd_line;
6365
} else if(line_length > 255 + 1 + 4) {
6466
/* domain name too long */
6567
return 202;
@@ -86,6 +88,8 @@ static int cmdh_xhlo(std::string_view cmd_line, smtp_context &ctx)
8688

8789
pcontext->connection.write(buff, string_length);
8890
return DISPATCH_CONTINUE;
91+
} catch (const std::bad_alloc &) {
92+
return 416; /* ENOMEM */
8993
}
9094

9195
int cmdh_lhlo(std::string_view cmd_line, smtp_context &ctx)
@@ -108,7 +112,7 @@ int cmdh_starttls(std::string_view cmd_line, smtp_context &ctx)
108112
if (!g_param.support_starttls)
109113
return 506;
110114
pcontext->last_cmd = T_STARTTLS_CMD;
111-
memset(pcontext->menv.hello_domain, '\0', std::size(pcontext->menv.hello_domain));
115+
pcontext->menv.hello_domain.clear();
112116
pcontext->menv.clear();
113117
return 210;
114118
}

mda/smtp/parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ static void smtp_parser_reset_context_session(SMTP_CONTEXT *pcontext)
629629
pcontext->pre_rstlen = 0;
630630
pcontext->stream.clear();
631631
pcontext->menv.clear();
632-
*pcontext->menv.hello_domain = '\0';
632+
pcontext->menv.hello_domain.clear();
633633
memset(&pcontext->flusher, 0, sizeof(FLUSH_INFO));
634634
}
635635

0 commit comments

Comments
 (0)