Skip to content

fix(bigquery-jdbc): harden log file permissions and prevent post-close handler leaks - #15112

Open
Neenu1995 wants to merge 1 commit into
mainfrom
fix/bq-jdbc-log-file-hardening
Open

Neenu1995 wants to merge 1 commit into
mainfrom
fix/bq-jdbc-log-file-hardening

Conversation

@Neenu1995

Copy link
Copy Markdown
Contributor

What & Why

PR 1 of 3 in the diagnostic logging hardening stack.

When diagnostic file logging is enabled (LogLevel > 0), PerConnectionFileHandler previously had three issues:

  1. World-readable log files and directories: Log directories (Files.createDirectories) and .log files (new FileHandler(filePath, 0, 1, true)) inherited the OS default umask (typically 0755 for directories and 0644 for files) on shared POSIX hosts.
  2. Unbounded file growth & % pattern expansion: FileHandler was configured with limit = 0 (unlimited file size), expanded %t/%h/%g/%u pattern tokens in paths, and fell back to .1 files using the default umask whenever .lck files collided.
  3. Post-close & third-party params[0] file-descriptor leaks: When BigQueryJdbcMdc was empty, PerConnectionFileHandler.publish() treated any String in LogRecord.getParameters()[0] as an active connection ID via computeIfAbsent. Calls on closed ResultSets, detached Struct/Array instances, or google-cloud-bigquery retry loggers (BigQueryRetryHelper / BigQueryRetryAlgorithm at FINEST) opened unclosed file writers permanently.

Changes in this PR

  • Direct NIO SeekableByteChannel writer with owner-only POSIX permissions (0700 / 0600): Replaces java.util.logging.FileHandler inside PerConnectionFileHandler with a bounded NIO SeekableByteChannel writer (BoundedLogFileWriter):
    • Newly created log directories use rwx------ (0700) without modifying permissions on pre-existing shared directories (such as /tmp), while rejecting symbolic links beneath top-level OS root links (supporting macOS /tmp and /var while blocking nested symlink traversal).
    • Per-connection log files are created exclusively via CREATE_NEW with rw------- (0600) and collision-safe naming (BQ-JDBC-<ts>-<attempt>-<shortUuid>.log), and global log files verify regular file type, NOFOLLOW_LINKS, and OS owner match (isolating by PROCESS_ID if a shared directory like /tmp already contains another user's BQ-JDBC-GLOBAL.log), eliminating .lck files, .1 0644 fallbacks, and % pattern expansion by construction.
  • Bounded file size (100 MiB default): Caps each log file at 100 MiB (100 * 1024 * 1024 bytes, overridable via -Dcom.google.cloud.bigquery.jdbc.maxLogFileBytes), truncating the open 0600 channel in place when exceeded.
    • Design decision (revisable): The 100 MiB default single-file cap was chosen so a connection retains substantial FINEST/FINE diagnostic history for debugging teams while preventing unbounded disk growth and preserving 0600 permissions on the open inode. This 100 MiB default is a decision that can be revised (or exposed as a connection URL property) based on team feedback.
  • Typed ConnectionLogContext & synchronized active-slot gate: Replaces raw String stamping in BigQueryJdbcResultSetLogger params[0] with package-private ConnectionLogContext, strips params[0] (checking {0..{9 positional placeholders) in OpenTelemetryJulHandler.formatMessage so SQL single quotes and positional arguments are preserved, synchronizes BigQueryJdbcRootLogger.setLevel/setPath, and gates per-connection log writers on synchronized active ConnectionFileSlot entries with BigQueryConnection constructor failure cleanup.

How It Was Tested

  • JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 make lint (fmt-maven-plugin:format).
  • JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 make unittest (Checkstyle, Java 8 animal-sniffer, and the full JUnit 5 unit test suite) and integration test compilation.
  • Added JUnit 5 regression tests in PerConnectionFileHandlerTest, OpenTelemetryJulHandlerTest, and BigQueryJdbcCustomLoggerTest covering POSIX 0700/0600 creation and tightening, shared directory & /tmp preservation, file and directory symlink rejection, literal % paths, maxLogFileBytes system property overrides, bounded size truncation, same-second prefix collisions, concurrent close() vs publish() races, post-close ResultSet/detached Struct/retry-string non-resurrection, failed BigQueryConnection constructor cleanup, and OTel message formatting.
  • Live GCP integration tests (IT*) were not executed against production BigQuery in this step.

…e handler leaks

Resolves b/584840147 (PR 1 of 3).

Replaces java.util.logging.FileHandler inside PerConnectionFileHandler with a bounded NIO SeekableByteChannel writer enforcing 0700 on newly created directories (preserving existing shared directories and macOS root symlinks while rejecting nested symlinks), 0600 on log files, and a revisable 100 MiB default single-file cap (configurable via -Dcom.google.cloud.bigquery.jdbc.maxLogFileBytes). Stamps a typed ConnectionLogContext in ResultSet log parameters and gates per-connection log writers on synchronized active ConnectionFileSlot entries.
@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.

Comment on lines +200 to +204
SeekableByteChannel channel = Files.newByteChannel(file, createNewOpts, attrs);
if (isPosix) {
Files.setPosixFilePermissions(file, OWNER_FILE_PERMS);
}
return channel;

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.

high

If Files.setPosixFilePermissions throws an IOException or RuntimeException (e.g., due to filesystem restrictions or security managers), the newly opened SeekableByteChannel is leaked because it is never closed. Wrapping the permission setting in a try-catch block that closes the channel on failure prevents resource leaks.

      SeekableByteChannel channel = Files.newByteChannel(file, createNewOpts, attrs);
      if (isPosix) {
        try {
          Files.setPosixFilePermissions(file, OWNER_FILE_PERMS);
        } catch (IOException | RuntimeException e) {
          try {
            channel.close();
          } catch (IOException closeEx) {
            e.addSuppressed(closeEx);
          }
          throw e;
        }
      }
      return channel;

Comment on lines +221 to +223
SeekableByteChannel channel = Files.newByteChannel(file, writeExistingOpts);
channel.position(channel.size());
return channel;

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.

high

If channel.position(channel.size()) throws an IOException (e.g., if the channel is concurrently closed or an I/O error occurs), the opened SeekableByteChannel is leaked. Wrapping the positioning call in a try-catch block that closes the channel on failure prevents resource leaks.

      SeekableByteChannel channel = Files.newByteChannel(file, writeExistingOpts);
      try {
        channel.position(channel.size());
      } catch (IOException | RuntimeException e) {
        try {
          channel.close();
        } catch (IOException closeEx) {
          e.addSuppressed(closeEx);
        }
        throw e;
      }
      return channel;

Comment on lines +236 to +240
SeekableByteChannel channel = Files.newByteChannel(file, opts, attrs);
if (isPosix) {
Files.setPosixFilePermissions(file, OWNER_FILE_PERMS);
}
return channel;

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.

high

If Files.setPosixFilePermissions throws an IOException or RuntimeException, the newly opened SeekableByteChannel is leaked. Wrapping the permission setting in a try-catch block that closes the channel on failure prevents resource leaks.

    SeekableByteChannel channel = Files.newByteChannel(file, opts, attrs);
    if (isPosix) {
      try {
        Files.setPosixFilePermissions(file, OWNER_FILE_PERMS);
      } catch (IOException | RuntimeException e) {
        try {
          channel.close();
        } catch (IOException closeEx) {
          e.addSuppressed(closeEx);
        }
        throw e;
      }
    }
    return channel;

}
BigQueryJdbcRootLogger.closeConnectionHandler(this.connectionId);
BigQueryJdbcOpenTelemetry.unregisterConnection(this.connectionId);
BigQueryJdbcOpenTelemetry.releaseSdk(this.openTelemetry);

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.

medium

In the finally block, this.openTelemetry can be null if the constructor fails before openTelemetry is initialized (or during its initialization). Calling releaseSdk with a null argument without a guard can cause a NullPointerException, which would mask the original exception that triggered the finally block. Adding a null check prevents this.

        if (this.openTelemetry != null) {
          BigQueryJdbcOpenTelemetry.releaseSdk(this.openTelemetry);
        }
References
  1. When using lazily initialized resources, ensure that teardown or close methods perform explicit null checks before invoking methods on them to prevent NullPointerException.

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.

1 participant