Skip to content

fix(output): open device and pipe targets without O_TRUNC - #209

Merged
TMHSDigital merged 1 commit into
TMHSDigital:mainfrom
bfs2021:fix-nul-output-without-trunc
Nov 10, 2026
Merged

TMHSDigital merged 1 commit into
TMHSDigital:mainfrom
bfs2021:fix-nul-output-without-trunc

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Description

CreateFile opens an existing non-regular target (device, pipe) with os.O_WRONLY|os.O_TRUNC. Windows rejects O_TRUNC on the NUL device with ERROR_INVALID_FUNCTION, so -o NUL — and the same path for -stats NUL / -sarif NUL — failed with Error: creating output file: open NUL: Incorrect function. and exit 1, while -o /dev/null works on Linux and macOS.

This drops O_TRUNC for that branch. Truncating a device or pipe is meaningless, and the branch already writes directly instead of going through the temp-file/rename path, so the flag has no purpose there. Regular files are untouched: they still go through the temporary file and replace the target on Close(true).

Related Issue

Fixes #208

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Root cause

if !fi.Mode().IsRegular() {
	f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0)

os.Stat("NUL") reports a non-regular file on Windows, so this branch runs; CreateFile on the NUL device returns ERROR_INVALID_FUNCTION when O_TRUNC is set. Opening without it succeeds, and writes are discarded as the device intends.

Testing

  • Reproduced first: CreateFile("NUL") on unpatched code returns open NUL: Incorrect function.
  • TestCreateFileWritesNonRegularTargetDirectly no longer skips on Windows and now passes against the NUL device. It still skips when os.DevNull is a regular file.
  • go test ./... — all packages pass on Windows/amd64 (go 1.27.3).
  • gofmt -l internal/output clean, go vet ./internal/output/ clean.

Checklist

  • I have read the CONTRIBUTING document
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes
  • This change maintains or improves the security controls in the tool
  • This change follows the educational/legitimate security testing focus of the project

…l#208)

Windows rejects O_TRUNC on the NUL device with ERROR_INVALID_FUNCTION, so
-o NUL, -stats NUL and -sarif NUL failed with "open NUL: Incorrect
function." and exit 1. Truncating a device or pipe is meaningless, so drop
the flag; the direct-write branch already handles non-regular targets.

Drops the Windows skip in TestCreateFileWritesNonRegularTargetDirectly: it
now exercises the NUL device on Windows too.

Signed-off-by: bfs2021 <[email protected]>
@TMHSDigital
TMHSDigital merged commit 2e325a4 into TMHSDigital:main Nov 10, 2026
16 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.

-o NUL fails on Windows: CreateFile opens devices with O_TRUNC

2 participants