1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

libpq-oauth: Avoid overflow for very large intervals

The slow_down interval parsing code checks explicitly for overflow, but
since it does that after the signed overflow has already occurred, we
end up inviting undefined behavior from the compiler anyway.

Use checked arithmetic instead. set_timer() takes a long int in order to
interface nicely with libcurl, so use an int32 as the interval counter
and clamp to LONG_MAX during conversion to milliseconds.

Backpatch to 18, where libpq-oauth was introduced.

Reported-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: https://postgr.es/m/qtclihmrkq67ach3xjxyi4qcksstin5qxwsnkqefkmotxwh4g6%40ae2bj6jvcmry
Backpatch-through: 18
This commit is contained in:
Jacob Champion
2026-07-31 11:19:44 -07:00
parent fff8c86759
commit 74169d3a1d

View File

@@ -28,6 +28,7 @@
#error libpq-oauth is not supported on this platform
#endif
#include "common/int.h"
#include "common/jsonapi.h"
#include "fe-auth-oauth.h"
#include "mb/pg_wchar.h"
@@ -144,7 +145,7 @@ struct device_authz
/* Fields below are parsed from the corresponding string above. */
int expires_in;
int interval;
int32 interval;
};
static void
@@ -976,7 +977,7 @@ parse_json_number(const char *s)
* expensive network polling loop.) Tests may remove the lower bound with
* PGOAUTHDEBUG, for improved performance.
*/
static int
static int32
parse_interval(struct async_ctx *actx, const char *interval_str)
{
double parsed;
@@ -987,8 +988,8 @@ parse_interval(struct async_ctx *actx, const char *interval_str)
if (parsed < 1)
return actx->debugging ? 0 : 1;
else if (parsed >= INT_MAX)
return INT_MAX;
else if (parsed >= INT32_MAX)
return INT32_MAX;
return parsed;
}
@@ -2590,10 +2591,7 @@ handle_token_response(struct async_ctx *actx, char **token)
*/
if (strcmp(err->error, "slow_down") == 0)
{
int prev_interval = actx->authz.interval;
actx->authz.interval += 5;
if (actx->authz.interval < prev_interval)
if (pg_add_s32_overflow(actx->authz.interval, 5, &actx->authz.interval))
{
actx_error(actx, "slow_down interval overflow");
goto token_cleanup;
@@ -2974,8 +2972,25 @@ pg_fe_run_oauth_flow_impl(PGconn *conn)
* Wait for the required interval before issuing the next
* request.
*/
if (!set_timer(actx, actx->authz.interval * 1000))
goto error_return;
{
/*
* Avoid overflow of long int. (By the time we reach
* LONG_MAX milliseconds -- 24 days on 32-bit platforms --
* continuing to honor slow_down requests seems pretty
* pointless anyway.)
*/
int64 interval_ms;
if (pg_mul_s64_overflow(actx->authz.interval, 1000,
&interval_ms)
|| (interval_ms > LONG_MAX))
{
interval_ms = LONG_MAX;
}
if (!set_timer(actx, (long) interval_ms))
goto error_return;
}
/*
* No Curl requests are running, so we can simplify by having