CVE-2022-35260: .netrc parser out-of-bounds access
The .netrc parser in curl reads past the end of its buffer when a line contains no whitespace characters, and then writes a single null byte out of bounds. An attacker who can supply a crafted .netrc file could crash the application, with other consequences not yet determined.
- Triaged in 38 hours
- Fixed in 23 days
- Disclosed after 24 days
Summary:
Curl expects the .netrc file to have space characters. So if there is no space character, it will do an out-of-bounds read and a 1-byte out-of-bounds write. This can happen multiple times depending on the state of the memory.
Steps To Reproduce:
curl --netrc-file .netrc test.local
".netrc" is attached.
The content is 'a' for 4095 bytes.
Depending on memory conditions, even single-byte files can cause problems.
It's not exactly just spaces and newlines. The condition is that the .netrc file does not contain characters for which ISSPACE() returns true (so it is also a condition that there is no line feed code). There is a problem with parsenetrc() in lib/netrc.c. parsenetrc() has the following loop.
while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
char *tok;
char *tok_end;
bool quoted;
if(state == MACDEF) {
if((netrcbuffer[0] == '\n') || (netrcbuffer[0] == '\r'))
state = NOTHING;
else
continue;
}
tok = netrcbuffer;
while(tok) {
while(ISSPACE(*tok))
tok++;
/* tok is first non-space letter */
if(!*tok || (*tok == '#'))
/* end of line or the rest is a comment */
break;
/* leading double-quote means quoted string */
quoted = (*tok == '\"');
tok_end = tok;
if(!quoted) {
while(!ISSPACE(*tok_end))
tok_end++;
*tok_end = 0;
}
The 'a' and the terminating character '\0' in the .netrc file are characters for which ISSPACE() returns false, so while on line 25 is true(!false). This causes an out-of-bounds read. Also, line 27 is an out-of-bounds write. (1 byte for '\0).
Remediation ideas:
I think it would be better to include the condition that *tok is not NULL in the while statement.
Impact
Application crash plus other as yet undetermined consequences.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The inner loop that scans for the end of a token only stops at a whitespace character, so a line with no whitespace makes it run past the buffer and then write a terminator out of bounds.
Impact
The application could crash, and the reporter says other consequences are as yet undetermined.
What the attacker needed
Attacker controls the .netrc file; curl run with --netrc-file
The fix
The loop condition should also check that the current character is not the string terminator, so scanning stops at the end of the buffer.
How it was found
- Create a .netrc file whose content is a single repeated non-whitespace character filling the buffer.
- Run curl with the --netrc-file option pointing at that file.
- Observe the out-of-bounds read and write, which may crash the application.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A .netrc file stores login details for servers, and curl reads it when asked. The code that reads it assumes every line has a space or newline somewhere, so it keeps scanning past the end of the text it loaded. That can crash the program and, in some cases, corrupt memory.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.