Skip to content

fix(bigquery-jdbc): harden log file creation with 0600/0700 permissions and symlink checks - #15112

Open
Neenu1995 wants to merge 2 commits into
mainfrom
fix/bq-jdbc-log-file-hardening
Open

Neenu1995 wants to merge 2 commits into
mainfrom
fix/bq-jdbc-log-file-hardening

Conversation

@Neenu1995

@Neenu1995 Neenu1995 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Stack: PR 1 of 6 (base: main)
Bug: b/584840147

Summary

Replaces java.util.logging.FileHandler in PerConnectionFileHandler with a Java New I/O (NIO) FileChannel writer (ChannelLogWriter) enforcing owner-only POSIX (0700 directory / 0600 file) permissions, component-by-component symbolic-link and hard-link checks, and graceful error handling.

Design Decisions

  • NIO FileChannel (ChannelLogWriter) over java.util.logging.FileHandler: Applies 0600 (rw-------) atomically via StandardOpenOption.CREATE_NEW, avoids .lck sidecar files and % pattern expansion, and keeps flush() as a no-op over unbuffered OS channel writes.
  • Path traversal, symlink, UID & hard-link (nlink == 1) checks: Walks every path segment from root with LinkOption.NOFOLLOW_LINKS, rejecting target symlinks, non-regular files, hard-linked files (unix:nlink > 1), and foreign-owned paths (while allowing root-owned OS ancestor symlinks), and re-verifies attributes post-open.
  • Graceful JDBC degradation: Inaccessible LogPath targets report via java.util.logging ErrorManager without throwing SQLException from DriverManager.getConnection(...).

@Neenu1995
Neenu1995 requested review from a team as code owners November 20, 2026 21:25
@Neenu1995
Neenu1995 added this pull request to stack #15115 November 20, 2026 21:26

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the JDBC driver's logging infrastructure to support secure, per-connection file logging using NIO SeekableByteChannel directly, preventing lock file creation and enforcing strict POSIX file permissions. It also introduces a typed ConnectionLogContext to avoid parameter collisions during message formatting and ensures proper cleanup of executors and OpenTelemetry resources upon connection failure. The code review feedback identifies critical resource leaks in PerConnectionFileHandler where opened channels could be leaked if setting POSIX permissions or positioning the channel throws an exception. Additionally, a potential NullPointerException was flagged in BigQueryConnection's cleanup block if openTelemetry is null during a failed initialization.

@Neenu1995 Neenu1995 changed the title fix(bigquery-jdbc): harden log file permissions and prevent post-close handler leaks [WIP] fix(bigquery-jdbc): harden log file permissions and prevent post-close handler leaks Nov 23, 2026
@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-log-file-hardening branch from 47aaf40 to 578cc0b Compare November 23, 2026 19:57
@Neenu1995 Neenu1995 changed the title [WIP] fix(bigquery-jdbc): harden log file permissions and prevent post-close handler leaks [WIP] fix(bigquery-jdbc): use NIO channel with 0700/0600 permissions for log files Nov 23, 2026
@Neenu1995

Copy link
Copy Markdown
Contributor Author

/gemini review

@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-log-file-hardening branch from 578cc0b to 8c47507 Compare November 23, 2026 20:23

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors PerConnectionFileHandler to use custom NIO SeekableByteChannel writers instead of standard FileHandler, enforcing secure owner-only POSIX permissions and rejecting symbolic links to improve security. Feedback on these changes includes removing the expensive FileChannel.force(false) call during flushes, improving the robustness of the POSIX owner verification in containerized environments using UserPrincipalLookupService, and adding a defensive null check in closeQuietlyWithSuppressed to prevent a potential NullPointerException from masking primary exceptions.

@Neenu1995
Neenu1995 removed this pull request from stack #15115 November 23, 2026 20:27
@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-log-file-hardening branch from 8c47507 to 5b55b06 Compare November 23, 2026 20:58
@Neenu1995 Neenu1995 changed the title [WIP] fix(bigquery-jdbc): use NIO channel with 0700/0600 permissions for log files fix(bigquery-jdbc): use NIO channel with 0700/0600 permissions for log files Nov 24, 2026

@keshavdandeva keshavdandeva left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The permission hardening works: new directories and files come out 0700 / 0600. But replacing FileHandler with a raw FileChannel introduced one blocker and three majors that main does not have. Details inline.

Question. On Windows, tryLock() on the log file is a mandatory lock. Can another process still tail BQ-JDBC-GLOBAL.log while the JVM runs? FileHandler locked a .lck sidecar instead.

@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-log-file-hardening branch from 77ae50e to d2e8aba Compare November 26, 2026 20:34
@Neenu1995 Neenu1995 changed the title fix(bigquery-jdbc): use NIO channel with 0700/0600 permissions for log files fix(bigquery-jdbc): harden log file creation with 0600/0700 permissions and symlink checks Nov 26, 2026
@Neenu1995
Neenu1995 added this pull request to stack #15256 November 26, 2026 20:52
@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-log-file-hardening branch from d2e8aba to 483b66a Compare November 26, 2026 21:01
Files.readAttributes(current, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
if (attrs.isSymbolicLink()) {
if (isTarget || !isPosix) {
throw new IOException("Refusing to use symbolic link in log path: " + current);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Question. A LogPath that is itself a symlink is still refused, while links higher in the path are followed when root or the user owns them. Should the last component follow the same rule, e.g. /var/log/app -> /data/logs/app?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Keeping the leaf component (isTarget) rejected as a symlink is intentional for two reasons:

  1. Destination directory ownership validation: In ensureSecureDirectory, verifyPosixOwner(current, true) inspects the symlink inode itself (NOFOLLOW_LINKS), not its target. For intermediate ancestors (/tmp -> /private/tmp, /var/run -> /run), subsequent loop iterations still validate the descendant directory components; if we allowed continue on isTarget (i == nameCount - 1), the loop would finish without verifying ownership of the resolved destination directory unless we added recursive readSymbolicLink chain-walking with cycle bounds.
  2. Security contract (b/584840147): Rejecting a symlinked leaf LogPath directly enforces the symlink-redirection protection on the log directory while still supporting OS-level ancestor symlinks. Deployments mapping /var/log/app to another volume can either bind-mount /data/logs/app or point LogPath to /var/log/app/jdbc (making /var/log/app an allowed ancestor symlink).

@Neenu1995

Copy link
Copy Markdown
Contributor Author

/gcbrun

This branch has not been deployed

No deployments
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.

2 participants