BUG #19670: Silent Integer Overflow in time_pl_interval() Returns Wrong Time Value

Поиск
Список
Период
Сортировка
Искать

BUG #19670: Silent Integer Overflow in time_pl_interval() Returns Wrong Time Value

От:
PG Bug reporting form <noreply@postgresql.org>
Дата:
Вложения:
The following bug has been logged on the website:

Bug reference:      19670
Logged by:          Tianyu Shi
Email address:      1950233439@qq.com
PostgreSQL version: 19beta3
Operating system:   Ubuntu22.04
Description:        

### Summary

`time_pl_interval()` in `src/backend/utils/adt/date.c` (lines 2174–2195)
silently produces incorrect results when adding a near-maximal interval to a
time value. The infinity guard (`INTERVAL_NOT_FINITE`) requires all three
interval fields to simultaneously hold their extreme values, so an interval
such as `'9223372036854 seconds'` (where `span->time = 9223372036854000000`,
slightly below `INT64_MAX`) bypasses the check entirely. The subsequent
unchecked addition `result = time + span->time` overflows signed 64-bit
integer arithmetic (C undefined behavior), returning a garbage `TimeADT`
with no error raised. Applications relying on correct time arithmetic for
security decisions — session expiry, scheduling windows, access-time
enforcement — may silently receive a corrupted value and act on it.

### PoC

Any authenticated database user can trigger the overflow with a single SQL
statement; no special privileges are required.

```sql
SELECT '23:59:59.999999'::time + interval '9223372036854 seconds';
```

To run against the local build:

```sql
-- Connect: ./build/bin/psql -h ./build/run -p 5432 postgres
SELECT
  '23:59:59.999999'::time + interval '9223372036854 seconds' AS
actual_result,
  make_time(0, 0, 0) + 24053999999::bigint * interval '1 microsecond' AS
expected_result,
  CASE
    WHEN ('23:59:59.999999'::time + interval '9223372036854 seconds') !=
         (make_time(0, 0, 0) + 24053999999::bigint * interval '1
microsecond')
    THEN 'MISMATCH: Integer overflow confirmed - result is WRONG'
    ELSE 'MATCH: No overflow detected'
  END AS verdict;
```

### Result

Expected output (correct modular arithmetic): `06:40:53.999999`.
Actual output observed: `19:59:04.448383` — an overflow-corrupted value
returned without any error or warning.

```
 actual_result  | expected_result |                        verdict
-----------------+-----------------+--------------------------------------------------------
 19:59:04.448383 | 06:40:53.999999 | MISMATCH: Integer overflow confirmed -
result is WRONG
```

The semantic invariant `(time + interval) mod USECS_PER_DAY` is violated. No
exception is raised, so callers cannot distinguish a correct result from a
corrupted one.




Re: BUG #19670: Silent Integer Overflow in time_pl_interval() Returns Wrong Time Value

От:
Rahul Yadav <rahul@rhyadav.com>
Дата:
Вложения:
Hi,

I can reproduce this on master. time_pl_interval() adds the
interval's time field to the time value before reducing the result
modulo one day, so a large enough interval overflows int64.
time_mi_interval() and the two timetz variants have the same problem.

Since the result wraps around at midnight anyway, only the interval's
time field modulo one day matters. The attached patch reduces it
first, so the intermediate result always fits in an int64; results
for intervals that didn't overflow are unchanged. It also adds
regression tests for the largest and smallest interval time values,
which fail without the fix.

The same arithmetic exists in all supported branches, so I think this
should be back-patched.

Regards,
Rahul Yadav

Re: BUG #19670: Silent Integer Overflow in time_pl_interval() Returns Wrong Time Value

От:
Andrew Krylosov <krylosov.andrew@gmail.com>
Дата:
Rahul Yadav  wrote:
> Since the result wraps around at midnight anyway, only the interval's
> time field modulo one day matters. The attached patch reduces it
> first, so the intermediate result always fits in an int64; results
> for intervals that didn't overflow are unchanged. It also adds
> regression tests for the largest and smallest interval time values,
> which fail without the fix.

I applied v1 on top of 3c5d9d914f, built it on macos clang 17 with
assertions enabled and ran the regression tests; they pass, and the new
ones fail without the date.c change.  The example from the report now
gives 04:00:53.999999.

The fix looks right to me.  Since 0 <= time <= USECS_PER_DAY and the
reduced offset is in (-USECS_PER_DAY, USECS_PER_DAY), the sum can't
overflow, and the existing normalization maps it to the same result as
before.  I also compared time +/- interval against an exact numeric
computation for a couple of thousand random interval values plus the
boundaries: the results match HEAD wherever HEAD doesn't overflow, and
are correct where it does. The timetz zone is never touched.

Best regards,
Andrew Krylosov


Re: BUG #19670: Silent Integer Overflow in time_pl_interval() Returns Wrong Time Value

От:
solai v <solai.cdac@gmail.com>
Дата:
Hi Rahul
I tested the v1 patch for Bug #19670 on PostgreSQL 19beta1.
Before applying the patch, I was able to reproduce the issue. The
query returned 19:59:04.448383 instead of the expected
04:00:53.999999.
After applying the patch, rebuilding, and restarting the server, the
same query returned the expected result.
I also tested the four boundary cases covering time and timetz
addition and subtraction with extreme interval values. All four
produced the expected results. The regression test suite also passed.
The patch looks good to me based on these results. Reducing the
interval's time component modulo one day before performing the
arithmetic appears to address the overflow.

Thanks for working on this fix.

Regards,
solai


FAQ