Skip to content

[#689] Reject too long keys and values in conf set - #703

Open
shubhamk0205 wants to merge 1 commit into
pgexporter:mainfrom
shubhamk0205:gsoc/689-conf-set-a-long-key-or-value-overflows-a
Open

shubhamk0205 wants to merge 1 commit into
pgexporter:mainfrom
shubhamk0205:gsoc/689-conf-set-a-long-key-or-value-overflows-a

Conversation

@shubhamk0205

Copy link
Copy Markdown
Contributor

Fixes #689

conf set copies the key and the digits of time/size values into 128 byte stack buffers without checking the length, so a long key or value overflows them and the management worker dies. The CLI prints nothing and exits with 1. The config file path was fixed for the same thing in #617, but conf set takes the text straight from the CLI.

There was no reply on the issue, so I went with the simplest approach. Happy to change it:

  • as_milliseconds() and as_bytes() stop with the normal "invalid value" error once the number would not fit in value[]. This also covers the 128 digit case, where only the terminating '\0' went past the end.
  • pgexporter_conf_set() checks the length before copying the server name and the key. A too long server name gives "unknown server", a too long key gives "unknown configuration key", the same answers you get for names that don't exist.

Added test_configuration_reject_long_key_and_value for both value helpers, the 128 digit boundary, a long key and a long server name.

Tested on Rocky Linux 10 (aarch64) with an ASan build: on main all five cases give stack-buffer-overflow (configuration.c 3139, 3157, 5215, 5270, 5333). With the patch they return the normal error and ASan is quiet; conf set metrics_cache_max_age 30s still works. ./clang-format.sh (21.1.8) makes no changes.

test/check.sh ci (clang Debug): 242 pass, 6 timescaledb skips, no failures, then the run stops with a segfault in test_connection_bind_unix_socket_permissions. That happens on main too, and is the same segfault the Debug jobs on main currently show in CI. The test puts a whole struct configuration (about 26 MB) on the stack, which is more than the 8 MB default. Can open a separate issue for it.

pgagroal and pgmoneta have the same value[index++] loop in their configuration.c (pgagroal also the key copy). I can cross-port once this one looks right.

@shubhamk0205
shubhamk0205 marked this pull request as ready for review November 24, 2026 11:04
@Pranav-error

Copy link
Copy Markdown
Collaborator

#692 is merged now. It fixes the Debug crash in test_connection_bind_unix_socket_permissions (#691), which is what fails both build-linux Debug jobs here. Please rebase on main.

@shubhamk0205
shubhamk0205 force-pushed the gsoc/689-conf-set-a-long-key-or-value-overflows-a branch from cff6f1c to 1286b4f Compare December 2, 2026 13:09
@shubhamk0205

Copy link
Copy Markdown
Contributor Author

#692 is merged now. It fixes the Debug crash in test_connection_bind_unix_socket_permissions (#691), which is what fails both build-linux Debug jobs here. Please rebase on main.

Rebased on main, thanks. The Debug jobs should pass now with #692 in

@Pranav-error Pranav-error left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The same two memcpys still read out of bounds when there is nothing before/after a dot: conf set "" 1, conf set .host x and conf set primary. x leave begin == -1, so end - begin + 1 is 1, the new check passes and memcpy reads config_key[-1]. ASan on 1286b4f: heap-buffer-overflow READ at configuration.c:3145 / 3169 (same on main at 3139 / 3157). Could you reject begin < 0 before both copies (here or as a follow-up) and add these three keys to the test?

The >= checks for key and server name, and the as_bytes one, aren't pinned: the test still passes with > MISC_LENGTH / index >= MISC_LENGTH, while a 128 char key, a 128 char server name and a 128 digit size crash the worker with that change. Please add those three to the test.

Verified (macOS, ASan+UBSan Debug): new test fails on 801d41c (stack-buffer-overflow in as_milliseconds) and passes on 1286b4f, 3/3 each; removing any one of the four checks fails it; 127 digits accepted, 128 rejected. Not run: Linux.

@shubhamk0205
shubhamk0205 force-pushed the gsoc/689-conf-set-a-long-key-or-value-overflows-a branch from 1286b4f to 86f5f35 Compare December 2, 2026 22:26
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.

conf set: a long key or value overflows a stack buffer

2 participants