Tristan reports: pdb_smbpasswd strlen Underflow Causes Out-of-Bounds Array Access ================================================================ CVSS 3.1: 5.5 (AV:L/AC:L/PR:L/UI:N/S:U/C:N/I:N/A:H) CWE: CWE-191 (Integer Underflow) leading to CWE-787 (Out-of-Bounds Write) Auth: Local (access to smbpasswd file) Version: Samba 4.24.3, master HEAD File: source3/passdb/pdb_smbpasswd.c, mod_smbfilepwd_entry(), line 826 Root Cause: When fgets() returns an empty string (first character is null terminator, possible when the file contains a NUL byte at line start), strlen() returns 0. The subsequent access linebuf[linebuf_len - 1] becomes linebuf[SIZE_MAX] due to size_t underflow, reading far past the stack buffer. linebuf_len = strlen(linebuf); // returns 0 if (linebuf[linebuf_len - 1] != '\n') { // linebuf[SIZE_MAX] - OOB ... } else { linebuf[linebuf_len - 1] = '\0'; // linebuf[SIZE_MAX] = '\0' - OOB write } Data Flow: smbpasswd file contains NUL byte at start of a line -> fgets returns string starting with '\0' -> strlen returns 0, linebuf_len = 0 -> linebuf[(size_t)0 - 1] = linebuf[SIZE_MAX] -> reads stack memory at ~18 exabytes past buffer -> SIGSEGV, or if comparison matches '\n': OOB write of '\0' Impact: A maliciously crafted smbpasswd file crashes smbd during password modification. The most likely outcome is a crash (DoS), but the potential OOB write of a null byte at a massive offset makes this a memory corruption primitive if the read lands in mapped memory. Lab verified: code trace confirmed underflow with size_t arithmetic.
I think in practice linebuf[SIZE_MAX] is the same as linebuf[-1], since size_t is the size as a pointer and the overflow arithmetic works that way. The accessed byte is thus likely to be the most significant byte of the `char *status` pointer. If that byte contains 0x0a (i.e. the pointer is 0x0axxxxxxxxxxxxxx), it will be changed, which would be bad, but otherwise there will be no crash. The pointer will not be 0xaxxxxxxxxxxxxxx on x86 as the address spaces is only 48 bit on all existing hardware. Probably this is just a bug, not exploitable as described.
"A maliciously crafted smbpasswd file" could only be created by root and would much more simply just contain known passwords for the account root. Moving to our embargoed non-security bug tracker for a normal fix in due time.
Created attachment 19197 [details] WIP patch Proposed cleanup. Passed private autobuild. Shall I create a normal MR for this?
(In reply to Volker Lendecke from comment #3) Yes please. I've removed the embargo.
https://gitlab.com/samba-team/samba/-/merge_requests/4704