[PATCH] Fix incomplete memory clearing in OAuth authentication

Started by Taras Klobaover 1 year ago2 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

awaiting CIrunningCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t51742
psql -h localhost -U postgres

Built from patchset v1 (message #1), September 20, 2026 at 03:26 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t51742_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t51742_1 && git checkout t51742_1

Patchset v1 (message #1) is on t51742_1

Jump to latest
#1Taras Kloba
sql.ua.tech@gmail.com

Hi hackers,

I discovered a minor security issue in the OAuth authentication code where
sensitive bearer tokens are not completely cleared from memory.

## The Issue

In src/backend/libpq/auth-oauth.c, the oauth_exchange() function attempts
to
clear the bearer token from memory using explicit_bzero(), but it only
clears
inputlen bytes. Since the buffer is allocated with pstrdup(), which
allocates
strlen(input) + 1 bytes, the null terminator byte remains uncleared.

## The Fix

The attached patch changes line 296 from:
explicit_bzero(input_copy, inputlen);
to:
explicit_bzero(input_copy, inputlen + 1);

This ensures the entire allocated buffer, including the null terminator, is
properly cleared from memory.

## Testing

The fix has been tested by:
- Verifying the code compiles without warnings
- Confirming inputlen equals strlen(input) per the validation at line 171
- Ensuring pstrdup() allocates inputlen + 1 bytes

## Impact

This is a minor security issue as only the null terminator byte remains in
memory, but it's worth fixing to ensure complete removal of sensitive
authentication data as intended by the comment "Don't let extra copies of
the bearer token hang around."

The patch applies cleanly to the master branch.

Best regards,
Taras Kloba

Attachments:

t51742_1
0001-Fix-incomplete-memory-clearing-in-OAuth-authenticati.patchapplication/octet-stream; name=0001-Fix-incomplete-memory-clearing-in-OAuth-authenticati.patchDownload+1-2
#2Daniel Gustafsson
daniel@yesql.se
In reply to: Taras Kloba (#1)
Re: [PATCH] Fix incomplete memory clearing in OAuth authentication

On 13 Jun 2025, at 18:41, Taras Kloba <sql.ua.tech@gmail.com> wrote:

Hi hackers,

I discovered a minor security issue in the OAuth authentication code where
sensitive bearer tokens are not completely cleared from memory.

## The Issue

In src/backend/libpq/auth-oauth.c, the oauth_exchange() function attempts to
clear the bearer token from memory using explicit_bzero(), but it only clears
inputlen bytes. Since the buffer is allocated with pstrdup(), which allocates
strlen(input) + 1 bytes, the null terminator byte remains uncleared.

Maybe I'm lacking imagination, but I fail to see how it's a security issue to
not set a byte to \0 when it is known to be \0?

--
Daniel Gustafsson