python / python/cpython

sqlite3: NULL pointer dereference in window aggregate xValue/xInverse callbacks when the window frame is empty

Đang mở
#153,800 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

extension-modules topic-sqlite3 type-crash
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Crash report

What happened?

This issue is a result review security advisories GHSA-g74h-5v93-4q3p.

Summary

Modules/_sqlite/connection.c in the sqlite3 standard library module
dereferences a NULL pointer when a user-defined aggregate window
function's value() (or inverse()) method is invoked by SQLite before
step() has ever been called for that window instance. This happens
whenever the window frame is empty for a given output row (e.g. the
first row of a partition with a frame such as
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING). On a standard (non-debug)
build, this crashes the whole Python process with SIGSEGV.

The problem in the code was found by the Svace static analyzer (ISP RAS) and
was later confirmed by a real REPL crash.

Details

Tested on: CPython 3.12.10 (Fedora 40 system build, standard release
build) and CPython main @ https://github.com/python/cpython/commit/4c79929705eb31959a6f4071cc34583de0737a28
(2026-07-06). No special configuration is needed - a default build of
either version reproduces this.

import sqlite3

class A:
    def value(self):
        return 1

con = sqlite3.connect(":memory:")
con.create_window_function("f", 1, A)
con.execute("CREATE TABLE t(x)")
con.execute("INSERT INTO t VALUES (1)")
con.execute(
    "SELECT f(x) OVER (ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) FROM t"
).fetchall()

Result:

segmentation fault (core dumped)  python

No step, inverse, or finalize method needs to be defined on A -
the single row's window frame is unconditionally empty (there is no
row "1 preceding" the only row in the table), so value() is the first
and only callback SQLite invokes.

For a debug build (Py_DEBUG=1, configure --with-pydebug), the
assert(*cls != NULL) fires instead of segfaulting, printing:
Assertion '*cls != NULL' failed in value_callback.

Package: sqlite3 C API.

Affected versions: >= 3.11

CPython versions tested on:

CPython main branch, 3.11, 3.12, 3.13, 3.14, 3.15, 3.16

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

No response

Linked PRs
  • gh-154576

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với Modules/_sqlite/connection.c, đặc biệt là các đường đi value_callback và inverse_callback, rồi chạy reproducer được cung cấp cho hàm cửa sổ trên một bản build tiêu chuẩn và một bản build debug. Công việc được xem là hoàn tất khi trường hợp frame rỗng hoàn thành mà không xảy ra SIGSEGV hoặc lỗi assertion; PR được liên kết gh-154576 cho thấy công việc có thể đã được tiến hành.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python, sqlite
Lĩnh vực
databases
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.