Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x4ddY-007IBn-1e for pgsql-bugs@arkaria.postgresql.org; Thu, 10 Sep 2026 12:12:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x4ddX-005XZY-1d for pgsql-bugs@arkaria.postgresql.org; Thu, 10 Sep 2026 12:12:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x3a5r-001t5m-05 for pgsql-bugs@lists.postgresql.org; Mon, 07 Sep 2026 14:13:07 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x3a5p-00000004ROD-0APY for pgsql-bugs@lists.postgresql.org; Mon, 07 Sep 2026 14:13:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=vhg5ixKBw4u3N4bQ5d1QTNbe+u7CXrkow+7L2slPWos=; b=VxJeZDCuY3rDue9otDAANv6IxZ 37XgMf6e8SNXlrlxfHMeRIyKZ/uwyVjbD8YpRYdbmU+4UFV0q0Lg66FTceAF+WBnsr5151CsNZxVx hL6nyKpIm78YnCZRObUQ3RMVP1nEUTo0s0OoSbjBBj87KRwfQ6LsMWbRbk5W1jThgh0THtmNHber1 0DQGjZiFk2J9HrAlZKd6DjaamQOhR4f9i5jZoCczekUcNjrVd9QN1PATrPT94WZS7Aq9Uet3b4sWP onwKEa62lSf0Ex6eSFA3svGRXC/fbyqZ98R45Pq6Bzw0P4/IBqJHEF5aDiy8K74iu3YUkF1GAX4m+ 7i61QQCw==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x3a5o-00DTUR-2c for pgsql-bugs@lists.postgresql.org; Mon, 07 Sep 2026 14:13:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x3a5n-00000000g5s-35ys for pgsql-bugs@lists.postgresql.org; Mon, 07 Sep 2026 14:13:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19673: inetmi_int8 Signed Integer Overflow Returns Wrong IPv6 Address To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 1950233439@qq.com Reply-To: 1950233439@qq.com, pgsql-bugs@lists.postgresql.org Date: Mon, 07 Sep 2026 14:12:15 +0000 Message-ID: <19673-3af11c9dfc3e0824@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19673 Logged by: Tianyu Shi Email address: 1950233439@qq.com PostgreSQL version: 19beta3 Operating system: Ubuntu22.04 Description: =20 ### Summary In `inetmi_int8()` (`src/backend/utils/adt/network.c`, line 1946), the expression `-addend` triggers signed integer overflow (C11 undefined behavior) when `addend =3D=3D INT64_MIN`. On x86-64, the CPU wraps the nega= tion back to `INT64_MIN`, causing the function to compute `ip - 2^63` instead of the correct `ip + 2^63`. For IPv6 addresses, the overflow-detection path in `internal_inetpl()` does not catch this case, so the function silently returns a completely wrong address rather than raising an error. Applications that use `inet - int8` to compute IPv6 subnet boundaries may therefore derive incorrect range endpoints, potentially causing access-control decisions to accept or reject the wrong addresses. ### PoC Any unprivileged user can trigger the bug with a single SQL statement using the `inet - int8` operator and `INT64_MIN` as the subtrahend. ```sql -- Connect as any normal (non-superuser) role. -- INT64_MIN must be passed as a string cast to avoid parse-time overflow. -- Trigger: should compute 8000::1 + 2^63 =3D 8000::8000:0:0:1, but silently returns a lower address. SELECT '8000::1'::inet - '-9223372036854775808'::int8 AS trigger_result; -- Roundtrip invariant: (X - INT64_MIN) + INT64_MIN must equal X. SELECT '8000::1'::inet AS original, '8000::1'::inet - '-9223372036854775808'::int8 AS minus_int64min, ('8000::1'::inet - '-9223372036854775808'::int8) + '-9223372036854775808'::int8 AS roundtrip, (('8000::1'::inet - '-9223372036854775808'::int8) + '-9223372036854775808'::int8) =3D '8000::1'::inet AS roundtrip_correct; -- Direction invariant: subtracting a negative must increase the address. SELECT '8000::1'::inet AS original, '8000::1'::inet - '-9223372036854775808'::int8 AS result, ('8000::1'::inet - '-9223372036854775808'::int8) > '8000::1'::inet AS direction_increased; ``` ### Result Expected: `'8000::1'::inet - INT64_MIN` =3D `ip + 2^63` =3D `8000::8000:0:0= :1` (address increases). Actual: the function returns `7fff:ffff:ffff:ffff:8000::1`, which is *less* than the original address =E2=80=94 the arithmetic went in the wrong direct= ion with no error raised. All three semantic invariants are violated: ``` original | minus_int64min | roundtrip | roundtrip_correct ----------+-----------------------------+------------------------+---------= ---------- 8000::1 | 7fff:ffff:ffff:ffff:8000::1 | 7fff:ffff:ffff:ffff::1 | f original | result | direction_increased ----------+-----------------------------+--------------------- 8000::1 | 7fff:ffff:ffff:ffff:8000::1 | f ``` `roundtrip_correct =3D f` and `direction_increased =3D f` confirm that `inetmi_int8()` computed `ip - 2^63` instead of `ip + 2^63` due to the signed integer overflow of `-INT64_MIN` on x86-64.