Skip to content

smbserver: set file size with ftruncate on set end-of-file info - #2381

Open
cielavenir wants to merge 1 commit into
fortra:masterfrom
cielavenir:setFileSizeFtruncate
Open

cielavenir wants to merge 1 commit into
fortra:masterfrom
cielavenir:setFileSizeFtruncate

Conversation

@cielavenir

@cielavenir cielavenir commented Nov 17, 2026 •

Copy link
Copy Markdown

Problem

The set end-of-file handlers in smbserver do not set the file size:

  • SMB_SET_FILE_END_OF_FILE_INFO (SMB1, setFileInformation)
  • SMB2_FILE_END_OF_FILE_INFO (SMB2, smb2SetInfo)

Both seek to EndOfFile - 1 and write a single NUL byte. As a result:

  • an existing byte at that offset is overwritten with \x00;
  • the file can never shrink;
  • EndOfFile = 0 is ignored.

So a client that writes a file and then sets its size (e.g. libsmb2 smb2_ftruncate, which is a
common "write, then truncate to length" pattern) ends up with its last byte corrupted.

Fix

Use os.ftruncate(fileHandle, EndOfFile) in both handlers. It truncates or zero-extends to exactly
EndOfFile and keeps the existing contents, which matches the semantics of FileEndOfFileInformation.

Testing

Tested with the patch on top of impacket 0.13.1 (the patched handlers are unchanged on master):

  • tests/SMB_RPC/test_smbserver.py: 45 passed, 3 skipped.

  • A libsmb2 6.x client against SimpleSMBServer with SMB2 enabled:

    Operation Before After
    write "short\n" over a longer file, then ftruncate(6) short\0 short\n
    ftruncate of abcdef to 3 ab\0def abc
    ftruncate of abcdef to 9 abcdef\0\0\0 abcdef\0\0\0
    ftruncate to 0 unchanged empty

The SMB1 handler gets the identical change; it was not exercised separately since libsmb2 only speaks
SMB2/3.

🤖 Generated with Claude Code

SMB_SET_FILE_END_OF_FILE_INFO (SMB1) and SMB2_FILE_END_OF_FILE_INFO
(SMB2) wrote a NUL byte at EndOfFile - 1 instead of setting the file
size. That overwrote the last byte of the file, could not shrink a
file, and ignored EndOfFile = 0. Clients that truncate after writing
(e.g. libsmb2 smb2_ftruncate) ended up with a corrupted last byte.

Use os.ftruncate, which truncates or zero-extends to EndOfFile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cielavenir
cielavenir marked this pull request as ready for review November 17, 2026 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant