Skip to content

main, pipewire-frame: check the output frame geometry before writing it - #22

Merged
RyanTheTide merged 1 commit into
mainfrom
fix/output-geometry-bounds
Dec 5, 2026
Merged

RyanTheTide merged 1 commit into
mainfrom
fix/output-geometry-bounds

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: The output frame's size was computed in 32-bit arithmetic that wraps. A huge NV12 format, 65522x43692, needs 2^32 + 131072 bytes but passed the per-frame check with a 128 KiB buffer, and the row copies then wrote past it. The output geometry is now computed in 64 bits. Formats above the backends' largest frame or PipeWire's 32-bit sizes are refused at negotiation, and the output mapping must hold the whole frame before anything is written.

Tested: tests/test_buffers.c and tests/test_formats.c pass under ASan and UBSan. The daemon compiles warning-free with PipeWire 1.6.11. J700 (Neo) at e75dea33fc00, the real user service with the camera: 1920x1080 (170 frames) and 1080x1920 (88 frames, stride rounded to 1088) negotiate the same size, stride and buffer size as base, with no service errors.

Risk: two-way · Neo and M5 Pro, where the camera service runs. Real camera formats are far below the new 8192 limit, so their negotiation and buffers are unchanged.

Not yet: a camera row on the M5 Pro.

Details
  • cdn_output_layout() computes the stride (width rounded up to 16) and the NV12 size in uint64_t. It refuses zero or odd sizes, sizes above CDN_MAX_FRAME_DIM, and a stride or size above INT32_MAX, since SPA_PARAM_BUFFERS_size and stride are Ints. CDN_MAX_FRAME_DIM (8192) is the GPU backend's existing limit, now named in backend.h and used there too.
  • on_out_param_changed() refuses such a format with pw_stream_set_error() before buffers or a backend are set up, and takes the buffer size and stride from the layout.
  • cdn_output_frame() maps the output frame onto the first data plane only if maxsize holds stride * height * 3 / 2, computed in 64 bits. on_in_process() calls it before any write, and the chunk size is the negotiated size.
  • The input side already checks its planes in 64 bits (cdn_frame_from_buffer()).
  • Not addressed here: the filter accepts sources by their node's driver property. That's a session-policy question, separate from the arithmetic.
Evidence
  • cc -fsanitize=address,undefined tests/test_buffers.c src/pipewire-frame.c $(pkg-config --cflags --libs libpipewire-0.3): exit 0. It covers layout edges (18x18, 1920x1080, 8192x8192); 8194 refused; zero and odd sizes; INT_MAX - 1; UINT32_MAX - 1 with its stride wrapping; 65522x43692; an output mapping one byte short or with no data; a stride below the width; and the 128 KiB mapping for the wrapping frame.
  • At the previous code the 65522x43692 frame's check computed (uint32_t)65536 * 43692 * 3 / 2 = 131072.
  • tests/test_formats.c: exit 0.

The output format took any even NV12 size. Its stride and the requested
buffer size were then computed in int, and every frame was checked with
`od->maxsize < (uint32_t)c->out_stride * h * 3 / 2`, which wraps modulo
2^32. A 65522x43692 format needs 2^32 + 131072 output bytes, but the
check saw 131072, so a 128 KiB buffer passed and the row copies wrote
far past it.

Add cdn_output_layout(), which computes the stride (the width rounded
up to 16) and the NV12 size in 64 bits. It refuses zero or odd sizes,
sizes above CDN_MAX_FRAME_DIM (8192, the GPU backend's existing limit,
now named in backend.h) and anything that does not fit PipeWire's
32-bit Int. Format negotiation uses it, so such a format is refused
before any buffer or backend is set up, and the buffer size comes from
it.

Add cdn_output_frame(), which maps the output frame onto the buffer only
if its mapping holds the whole frame, computed in 64 bits. The process
callback uses it before anything is written, and chunk sizes come from
the negotiated layout.

tests/test_buffers.c checks the layout at its edges and with INT_MAX,
UINT32_MAX, a stride that wraps when rounded and the 65522x43692 case,
and the output mapping one byte short, missing or too small for the
wrapping frame.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested a review from a team as a code owner November 26, 2026 10:22
@DjDeveloperr DjDeveloperr changed the title Check the output frame geometry before advertising and writing it main, pipewire-frame: check the output frame geometry before writing it Nov 26, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at e75dea33fc00: ready (code). CI (pr-text, readiness) was still pending when I checked, and the Neo and M5 Pro camera rows listed under "Not yet" are still owed.

Blockers: none

Checked

  • Every write to the output buffer goes through the checked layout. on_in_process() maps fo only through cdn_output_frame() (src/main.c:392). That function checks maxsize against stride*height + stride*(height/2) in 64 bits before anything is written, and refuses a missing mapping, a zero stride or a stride below the width. After that, every write lands in fo:
    • the temporal backend, through temporal_process(), both the dmabuf ext->process and ops->process paths, plus the CPU fallback it creates on failure;
    • the low-light stage (ml_ops->process(&sf, &fo));
    • the cdn_frame_copy() fallbacks;
    • the effects pipeline (cdn_effects_take()).
      fi, the stage buffer and fo all take their size from c->fmt, so cdn_frame_copy(), which copies the source's width and height, never writes past fo. The GPU backend refuses an output that isn't its own size (valid_output()), and the CPU backend checks both frames (denoise_cpu.c:822).
  • A renegotiation can't reuse a stale layout.
    • on_out_param_changed() validates the new format with cdn_output_layout() (780) before it touches c->fmt. A refused format leaves the previous size, stride and frame size together, as one consistent set.
    • A new size updates c->fmt, out_stride and out_size together, then recreates the backends (setup_backend()), and recreates the effects through cam_stop_input()/cam_start_input() → setup_effects(). The effects size comes from c->fmt, so an old-size result can't be copied into a new fo.
    • Buffers allocated for the old size are still checked against the new frame on every process call. A smaller one is refused until the new buffers arrive.
    • Both streams run on the one pw_main_loop, with no PW_STREAM_FLAG_RT_PROCESS, so param_changed and process can't interleave.
  • cdn_output_layout() (src/pipewire-frame.c:76) rounds the stride and computes the NV12 size in uint64_t. It refuses zero, odd and over-8192 sizes, and any stride or size above INT32_MAX, before the Buffers param is built. CDN_MAX_FRAME_DIM names the GPU backend's existing 8192 limit. The chunk size is the negotiated out_size, which the mapping is checked to hold.
  • No runtime switches, and no SoC or board gates.

Not a blocker: the ANE and AVE plugins write out with the input frame's width and height, and check only the input against their own size. That's safe today, because the caller always passes fo the same size as the input. An output check like the CPU and GPU backends' would make them safe whoever calls them.

Evidence
  • cc -D_GNU_SOURCE -fsanitize=address,undefined -fno-sanitize-recover=undefined tests/test_buffers.c src/pipewire-frame.c against libpipewire 1.6.11: exit 0, covering 65522x43692, INT_MAX - 1, UINT32_MAX - 1, the short and NULL mappings, and a stride below the width. tests/test_formats.c under the same flags: exit 0.
  • cc -fsyntax-only -Wall -Wextra src/main.c src/pipewire-frame.c: no warnings. src/gpu/denoise_gpu.c couldn't be compiled here (no Vulkan headers); its change only swaps the literal 8192 for CDN_MAX_FRAME_DIM, which comes from ../backend.h.

@github-actions github-actions Bot added blast:m5 Macs affected, from Merge risk (bot) blast:neo Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at e75dea33: not ready yet

Check Detail
CI at head ok 3 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok in the PR body at e75dea33
Conflicts ok none
PR text ok Risk: two-way · Neo and M5 Pro, where the camera service runs. Real camera formats are far below the new 8192 limit, so their negotiation and buffers are unchanged.

Areas: none matched in CODEOWNERS

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@github-actions github-actions Bot removed the needs-hw-row No hardware row at the PR head (bot) label Nov 26, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at e75dea3 after the remaining hardware validation.

Checked: negotiation uses a bounded 64-bit layout before narrowing to PipeWire fields, the frame path checks the full NV12 mapping before writing, and chunk size uses the negotiated value. The author and independent sanitizer results cover the layout helpers. The J700 receipt covers landscape and portrait stride. The M5 Pro camera-denoise row remains due.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Nov 30, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Hardware row at exact head e75dea33fc00d7977d7d56f2eaf08aa833b6a1e7 on K114s (MacBook Pro 14" M5 Pro, T6050, Fedora 44 ARM64, 7.3.0-f5f6446c0265-47515862135.1-aurora-edge, boot_id=0b68fbed, SELinux=Enforcing, failed=0, zero-reboot) — PASS:

  • Built exact head e75dea33fc00 for Fedora 44 ARM64 (sha256: 03a45ad768a963da...): meson test 10/10 PASS (buffers, formats, ane, trigger, loader, config, effects, session-config, user-unit, cpu).
  • Stock baseline (/usr/bin/aurora-camera-denoise, sha256: 1f36983a...): 1920x1080@30 NV12 negotiated 30/30 frames (3110400 bytes/frame).
  • Trial (e75dea33fc00) on live v4l2_input.platform-580000000.isp (sensor:imx958-uw, /dev/video0):
    • 1920x1080@30/1 NV12: 60/60 frames (3110400 bytes/frame, 4.26s, temporal cpu), matching baseline caps/buffer size, 0 errors.
    • 1920x1080@15/1 NV12 re-negotiation: 30/30 frames (3110400 bytes/frame, 2.80s, 62/62 frames denoised at 10.42 ms/frame), 0 errors.
  • Stock aurora-camera-denoise.service cleanly restored (active), SELinux=Enforcing, failed=0.

Trace: traces/camera/20261206-k114s-camera-denoise-22/run-k114s.log.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict at e75dea33fc00: APPROVE (Core non-author review).

Blockers: none.

Checked:

  • cdn_output_layout() / cdn_output_frame() compute stride and NV12 output size in uint64_t, enforce CDN_MAX_FRAME_DIM (8192) and INT32_MAX bounds at negotiation, and verify maxsize holds the full frame before any write.
  • 10/10 ARM64 F44 Meson tests pass (buffers, formats, etc.) alongside aurora-silicon-review approval and CI green.
  • Verified across both affected platforms: author's J700 (Neo) 1920x1080 + 1080x1920 row and our K114s (M5 Pro) 1920x1080@30 + 1920x1080@15 live ISP/PipeWire row.

@RyanTheTide
RyanTheTide merged commit d9191e2 into main Dec 5, 2026
5 of 7 checks passed
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merged at e75dea33fc00 (d9191e207c376c449a141251c0260e375acec01f): double-approved (aurora-silicon-review + Core), CI green, 10/10 ARM64 Meson tests PASS, and verified on both J700 (Neo) and K114s (M5 Pro). Revert: git revert -m 1 d9191e207c376c449a141251c0260e375acec01f.

@RyanTheTide
RyanTheTide deleted the fix/output-geometry-bounds branch December 5, 2026 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blast:m5 Macs affected, from Merge risk (bot) blast:neo Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants