cockroachdb / cockroachdb/cockroach

File descriptor leak on error path in WritePEMToFile (pkg/security/pem.go)

Open
#164,232 1 comment 0 reactions 1 assignee Claimed by @shriramters View on GitHub
C-bug O-community P-2 T-product-security
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Description:

In pkg/security/pem.go, function WritePEMToFile, a file descriptor opened via:

f, err := os.OpenFile(path, flags, mode)

can be leaked on an error path inside the encoding loop.

Specifically, if pem.Encode fails, the function returns:

return errors.Wrap(err, "could not encode PEM block")

before the file is closed.

Because f.Close() (or a defer f.Close()) is not registered prior to this return path, the descriptor remains open when this error condition is triggered.

This results in a file descriptor leak on that path.

Expected behavior:

The file descriptor should be closed on all return paths.
A minimal fix would be to register defer f.Close() immediately after a successful os.OpenFile, or explicitly call f.Close() before the early return.

Happy to submit a minimal PR if helpful.

Jira issue: CRDB-60702

Epic CC-33751

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.