Skip to content

Fix 4 code bugs: substr off-by-one, HANDLE* cast, TOCTOU GetLastError, sun_path overflow#14297

Merged
benhillis merged 1 commit intomasterfrom
user/benhill/bug_fix_batch
Feb 27, 2026
Merged

Fix 4 code bugs: substr off-by-one, HANDLE* cast, TOCTOU GetLastError, sun_path overflow#14297
benhillis merged 1 commit intomasterfrom
user/benhill/bug_fix_batch

Conversation

@benhillis
Copy link
Copy Markdown
Member

  • LxssHttpProxy.cpp: IPv6 substr extraction used wrong length calculation. substr(openBracket+1, closeBracket-1) is incorrect when openBracket > 0; fixed to substr(openBracket+1, closeBracket-openBracket-1). Also fixed empty-address guard to check closeBracket (not closeBracket-1).

  • LxssUserSession.cpp: Two instances of reinterpret_cast<HANDLE*> in ScopedMultiRelay construction should be reinterpret_cast (without the pointer). Other identical callsites in the same file already use the correct cast.

  • LxssUserSession.cpp: GetLastError() was called unconditionally after CreateFileW, even on success. A stale ERROR_SHARING_VIOLATION from a prior API call could cause a false throw. Fixed to only check GetLastError() when CreateFileW fails (!vhd).

  • plan9.cpp: sun_path bounds check used > instead of >= leaving no room for null terminator. Also added a post-split check to ensure the child name fits after splitting parent/child for long paths.

…, sun_path overflow

Bug 1 - LxssHttpProxy.cpp: IPv6 substr extraction used wrong length
  calculation. substr(openBracket+1, closeBracket-1) is incorrect when
  openBracket > 0; fixed to substr(openBracket+1, closeBracket-openBracket-1).
  Also fixed empty-address guard to check closeBracket (not closeBracket-1).

Bug 2 - LxssUserSession.cpp: Two instances of reinterpret_cast<HANDLE*> in
  ScopedMultiRelay construction should be reinterpret_cast<HANDLE> (without
  the pointer). Other identical callsites in the same file already use the
  correct cast.

Bug 3 - LxssUserSession.cpp: GetLastError() was called unconditionally after
  CreateFileW, even on success. A stale ERROR_SHARING_VIOLATION from a prior
  API call could cause a false throw. Fixed to only check GetLastError() when
  CreateFileW fails (!vhd).

Bug 4 - plan9.cpp: sun_path bounds check used > instead of >= leaving no room
  for null terminator. Also added a post-split check to ensure the child name
  fits after splitting parent/child for long paths.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes four distinct bugs across three files related to incorrect string operations, type casting, error handling, and bounds checking.

Changes:

  • Corrected IPv6 address extraction logic with proper substring bounds
  • Fixed HANDLE type casting in relay construction
  • Fixed premature GetLastError() call that could report stale errors
  • Corrected bounds checking for Unix socket paths to ensure null terminator space

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/windows/service/exe/LxssHttpProxy.cpp Fixed IPv6 address extraction substring calculation and empty address validation
src/windows/service/exe/LxssUserSession.cpp Fixed HANDLE type casts and moved GetLastError() check to only execute on CreateFileW failure
src/linux/init/plan9.cpp Fixed sun_path bounds check to account for null terminator and added validation after path splitting

return UnsupportedProxyReason::Supported;
}
portRemoved = portRemoved.substr(openBracket + 1, closeBracket - 1);
portRemoved = portRemoved.substr(openBracket + 1, closeBracket - openBracket - 1);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish there was an easy way to add test coverage for this.

If we ever factor IsUnsupportedProxy() in common I guess we could add coverage for this

@benhillis benhillis merged commit e01a672 into master Feb 27, 2026
10 checks passed
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.

3 participants