Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

message_parse_received_date() avoid calling message_parse_string(hdr="") #4771

Conversation

dilyanpalauzov
Copy link
Contributor

as the latter does hdr = strchr(hdr+1, '\n') and hdr+1 is not allocated.

Without the change in message.c, calling

$ valgrind --trace-children=yes ./cunit/unit cunit/message:parse_received_semicolon

prints

==176956== Invalid read of size 1                                   
==176956==    at 0x7F25960: __strchr_avx2 (strchr-avx2.S:53)        
==176956==    by 0x490AA62: message_parse_string (message.c:1180)                                                                       
==176956==    by 0x490CCDE: message_parse_received_date (message.c:2019)         
==176956==    by 0x490A3F1: message_parse_headers (message.c:981)                                                                       
==176956==    by 0x4909BAD: message_parse_body (message.c:779)                                                                          
==176956==    by 0x4908FEF: message_parse_mapped (message.c:525)    
==176956==    by 0x53800A: test_parse_received_semicolon (message.testc:1403)
==176956==    by 0x4112F8: __cunit_wrap_test (unit.c:159)
==176956==    by 0x538C5E: __cunit_test_parse_received_semicolon (message.testc-cunit.c:143)
==176956==    by 0x789266A: run_single_test.constprop.0 (TestRun.c:991)
==176956==    by 0x7894DE7: CU_run_test (TestRun.c:473)
==176956==    by 0x411C25: run_tests (unit.c:389)
==176956==  Address 0x8563245 is 0 bytes after a block of size 5 alloc'd
==176956==    at 0x484278B: malloc (vg_replace_malloc.c:431)
==176956==    by 0x4C4E328: xmalloc (xmalloc.c:56)
==176956==    by 0x4C4E553: xstrndup (xmalloc.c:115)
==176956==    by 0x490AAE3: message_parse_string (message.c:1190)
==176956==    by 0x490CC78: message_parse_received_date (message.c:1999)
==176956==    by 0x490A3F1: message_parse_headers (message.c:981)
==176956==    by 0x4909BAD: message_parse_body (message.c:779)
==176956==    by 0x4908FEF: message_parse_mapped (message.c:525)
==176956==    by 0x53800A: test_parse_received_semicolon (message.testc:1403)
==176956==    by 0x4112F8: __cunit_wrap_test (unit.c:159)
==176956==    by 0x538C5E: __cunit_test_parse_received_semicolon (message.testc-cunit.c:143)
==176956==    by 0x789266A: run_single_test.constprop.0 (TestRun.c:991)

as the latter does hdr = strchr(hdr+1, '\n') and hdr+1 is not allocated.
@dilyanpalauzov dilyanpalauzov force-pushed the message_parse_received_date_invalid_read branch from aa11be7 to 5f5503a Compare December 17, 2023 18:29
Copy link
Contributor

@elliefm elliefm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good, and the test proves that it fixes the invalid read. But the test itself fails... looks like a simple fix though, see suggestion inline.

struct body body;
memset(&body, 0x45, sizeof(body));
CU_ASSERT_EQUAL(message_parse_mapped(msg, sizeof(msg)-1, &body, NULL), 0);
CU_ASSERT_STRING_EQUAL(body.received_date, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test fails here with the fix, because when a semicolon with nothing after it is found, the original header value is used as the date string, which in this case is "abc;". But this line expects the empty string. It should expect "abc;", like this:

Suggested change
CU_ASSERT_STRING_EQUAL(body.received_date, "");
CU_ASSERT_STRING_EQUAL(body.received_date, "abc;");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither "abc" nor "" looks right to me. A non-existent date-time part in the header value should result in a NULL received_date value. The code building on body->received_date in lmtpd and jmap_mail is already prepared to handle NULL received_dates.

@rsto
Copy link
Member

rsto commented Dec 18, 2023

Thanks for submitting this. I will accept this as-is but will rewrite setting received_date to NULL on top of it. I'll then merge the updated PR so that CI succeeds.

@rsto rsto closed this Dec 18, 2023
@dilyanpalauzov dilyanpalauzov deleted the message_parse_received_date_invalid_read branch December 18, 2023 18:20
@elliefm elliefm added backport-to-3.6 for PRs that are to be backported to 3.6 backport-to-3.8 for PRs that are to be backported to 3.8 labels Jan 16, 2024
@elliefm
Copy link
Contributor

elliefm commented Jan 16, 2024

@elliefm When backporting this, see if you can integrate the Received header test changes from #4776. That test should work just as well with this version of the implementation with probably the same tweaks as my suggestion in this PR.

@elliefm elliefm added backport-to-3.4 for PRs that are to be backported to 3.4 backport-to-3.2 for PRs that are to be backported to 3.2 and removed backport-to-3.8 for PRs that are to be backported to 3.8 backport-to-3.6 for PRs that are to be backported to 3.6 labels Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-to-3.2 for PRs that are to be backported to 3.2 backport-to-3.4 for PRs that are to be backported to 3.4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants