Skip to content

[kubectl-plugin] Skip out-of-range file modes and close files during log download - #5054

Open
SrikarChittemsetty wants to merge 1 commit into
ray-project:masterfrom
SrikarChittemsetty:fix-log-download-mode-guard-and-fd-leak
Open

[kubectl-plugin] Skip out-of-range file modes and close files during log download#5054
SrikarChittemsetty wants to merge 1 commit into
ray-project:masterfrom
SrikarChittemsetty:fix-log-download-mode-guard-and-fd-leak

Conversation

@SrikarChittemsetty

Copy link
Copy Markdown

Why are these changes needed?

Two bugs in downloadRayLogFiles (kubectl-plugin/pkg/cmd/log/log.go), reported in #4779.

1. The mode bounds check does nothing. The code checks whether header.Mode fits in an os.FileMode and prints a warning, but has no continue/guard — it falls straight through to os.OpenFile(..., os.FileMode(header.Mode)) and creates the file with a truncated mode. The //nolint:gosec // overflow is guarded by bounds check above comment asserted a guard that was not there, so the linter stayed silent.

Worth noting for reviewers: the fix suggested in the issue (adding a bare continue) would hang. The extraction loop advances the tar reader after the switch:

for !errors.Is(err, io.EOF) {
    switch header.Typeflag { ... }
    header, err = tarReader.Next()   // <- skipped by `continue`
}

so continue would re-read the same header forever. This PR guards the write with an else branch instead, which skips the file without skipping the advance. A regression test asserts the call returns rather than stalling.

2. File descriptors accumulate for the whole download. defer outFile.Close() sat inside the extraction loop, so defer only ran when downloadRayLogFiles returned — every file in the archive stayed open until the entire download finished. Extracting a log archive with many files holds that many descriptors at once.

The per-entry write moves into a writeTarFile helper so the file closes at the end of each entry, and close errors are surfaced rather than discarded.

Related issue number

Closes #4779

Checks

  • go build ./..., go vet, and gofmt are clean.
  • Extended TestDownloadRayLogFiles coverage with TestDownloadRayLogFilesSkipsOutOfRangeMode, which asserts the out-of-range entry is skipped, that the following valid entry still extracts, and that extraction does not stall. Against the previous behaviour it fails with:
Error: "[- file0.txt - file1.txt]" should have 1 item(s), but has 2

Disclosure: this change was prepared with the assistance of an AI coding agent. The analysis, fix, and tests were verified against the source and the test suite before submitting.

…log download

downloadRayLogFiles checked whether a tar entry's mode fits in an os.FileMode
but did not act on the result: the warning was printed and the code fell
through to create the file with a truncated mode anyway. The accompanying
//nolint:gosec comment claimed the cast was guarded when it was not.

Adding a bare continue is not sufficient, because the extraction loop advances
the tar reader after the switch; skipping to the next iteration would re-read
the same header forever. Guard the write with an else branch instead.

Separately, the per-file close was deferred inside the extraction loop, so
every descriptor opened while extracting an archive stayed open until the
whole download finished. Move the write into a helper so the file is closed at
the end of each entry, and surface close errors instead of discarding them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Signed-off-by: Srikar Chittemsetty <srikarchittemsetty@gmail.com>
@SrikarChittemsetty
SrikarChittemsetty force-pushed the fix-log-download-mode-guard-and-fd-leak branch from 2edeb8e to d2bda5c Compare September 3, 2026 20:47
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.

[Bug] kubectl-plugin: Integer overflow (G115) and File Descriptor leak in downloadRayLogFiles

1 participant