Bug 16167 - pdb_smbpasswd strlen Underflow Causes Out-of-Bounds Array Access
Summary: pdb_smbpasswd strlen Underflow Causes Out-of-Bounds Array Access
Status: NEW
Alias: None
Product: Samba 4.1 and newer
Classification: Unclassified
Component: Other (show other bugs)
Version: 4.24.3
Hardware: All All
: P5 normal (vote)
Target Milestone: ---
Assignee: Samba QA Contact
QA Contact: Samba QA Contact
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-07-09 02:52 UTC by Douglas Bagnall
Modified: 2026-09-11 13:07 UTC (History)
1 user (show)

See Also:


Attachments
WIP patch (14.75 KB, patch)
2026-09-09 10:25 UTC, Volker Lendecke
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Douglas Bagnall 2026-07-09 02:52:47 UTC
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.
Comment 1 Douglas Bagnall 2026-07-09 03:12:18 UTC
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.
Comment 2 Andrew Bartlett 2026-08-08 09:44:48 UTC
"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.
Comment 3 Volker Lendecke 2026-09-09 10:25:55 UTC
Created attachment 19197 [details]
WIP patch

Proposed cleanup. Passed private autobuild. Shall I create a normal MR for this?
Comment 4 Andrew Bartlett 2026-09-10 08:37:10 UTC
(In reply to Volker Lendecke from comment #3)
Yes please.

I've removed the embargo.