Repository navigation
smbserver: set file size with ftruncate on set end-of-file info - #2381
Open
cielavenir wants to merge 1 commit into
Open
cielavenir wants to merge 1 commit into
cielavenir wants to merge 1 commit into
Conversation
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
marked this pull request as ready for review
November 17, 2026 04:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The set end-of-file handlers in
smbserverdo 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 - 1and write a single NUL byte. As a result:\x00;EndOfFile = 0is ignored.So a client that writes a file and then sets its size (e.g. libsmb2
smb2_ftruncate, which is acommon "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 exactlyEndOfFileand 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
SimpleSMBServerwith SMB2 enabled:"short\n"over a longer file, thenftruncate(6)short\0short\nftruncateofabcdefto 3ab\0defabcftruncateofabcdefto 9abcdef\0\0\0abcdef\0\0\0ftruncateto 0The SMB1 handler gets the identical change; it was not exercised separately since libsmb2 only speaks
SMB2/3.
🤖 Generated with Claude Code