8000 opencv: link system openjpeg 2.5.4 instead of the bundled copy by bryan-minimal · Pull Request #301 · gominimal/pkgs · GitHub
[go: up one dir, main page]

Skip to content

opencv: link system openjpeg 2.5.4 instead of the bundled copy - #301

Merged
bryan-minimal merged 1 commit into
mainfrom
opencv-system-openjpeg
Jun 27, 2026
Merged

bryan-minimal merged 1 commit into
mainfrom
opencv-system-openjpeg

Conversation

@bryan-minimal
@bryan-minimal bryan-minimal commented Jun 26, 2026 •
Copy link
Copy Markdown
Member

What

Switch opencv (4.13.0) from its bundled 3rdparty/openjpeg to our system openjpeg 2.5.4:

  • build.sh: add -DWITH_OPENJPEG=ON -DBUILD_OPENJPEG=OFF so cmake's find_package(OpenJPEG) detects and links the system libopenjp2 instead of compiling the vendored copy.
  • build.ncl: add openjpeg to build_deps (configure-time headers + OpenJPEGConfig.cmake/libopenjp2.pc, which our openjpeg pkg exports) and to runtime_deps (imgcodecs dynamically links libopenjp2.so).

Why

OpenCV's bundled openjpeg is what OSS-Fuzz flagged as OSV-2023-444 (heap-buffer-overflow in opj_jp2_apply_pclr, found while fuzzing opencv). The fix landed upstream in OpenJPEG PR #1441 (commit c8fef1d, Aug 2022) and shipped in 2.5.1+. We already ship openjpeg 2.5.4, which carries the fix.

Linking the system lib removes the vulnerable code from our opencv binary rather than asserting the vendored copy is patched (today that's an advisory_overrides.toml entry, < 4.13.0, which re-asserts trust on every bump). It also means future openjpeg CVEs surface on the openjpeg package, where we patch once, instead of riding silently inside opencv's vendored copy.

Reviewer notes / to verify on buildbot

  • Confirm the cmake "Media I/O" config summary resolves a real system openjpeg path (e.g. /usr/lib/libopenjp2.so), not build — that's the proof it picked up our 2.5.4 rather than the bundle.
  • opencv requires openjpeg ≥ 2.0 (recommended ≥ 2.3.1); 2.5.4 clears it.
  • Follow-up (separate, after this is green): drop the OSV-2023-444 override in minimal-supply-chain/data/advisory_overrides.toml — the finding no longer applies once the bundle is gone.
  • Unrelated to this PR: opencv's other finding OSV-2022-394 (cv::split) is pure-opencv code fixed in 4.10.0, already covered by its own override; not touched here.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • OpenCV now uses the system OpenJPEG library during build and at runtime, improving compatibility and avoiding the bundled copy.
    • Updated the referenced OpenCV source version information to align with the latest release tag.

OpenCV builds its vendored 3rdparty/openjpeg by default. That bundled
copy is what OSS-Fuzz flagged as OSV-2023-444 (heap-buffer-overflow in
opj_jp2_apply_pclr, found while fuzzing opencv). The fix landed upstream
in OpenJPEG PR #1441 (commit c8fef1d, Aug 2022) and shipped in 2.5.1+.

We already ship openjpeg 2.5.4, which carries the fix and exports a
cmake config + pkgconfig. Point opencv at it:

  - build.sh: -DWITH_OPENJPEG=ON -DBUILD_OPENJPEG=OFF so cmake detects
    and links the system libopenjp2 instead of compiling the bundle.
  - build.ncl: add openjpeg to build_deps (find_package at configure
    time) and runtime_deps (imgcodecs dlinks libopenjp2.so).

This removes the vulnerable code from our opencv binary rather than
asserting the vendored copy is patched, and lets future openjpeg CVEs
surface on the openjpeg package where we patch once. Also corrects the
stale 4.12.0 archive comment (we ship 4.13.0).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai
coderabbitai Bot commented Jun 26, 2026 •
Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

OpenCV’s build spec now imports system openjpeg, adds it to build and runtime dependencies, updates the archive tag comment to 4.13.0, and configures the build to use system OpenJPEG through explicit CMake flags.

Changes

OpenCV OpenJPEG linkage

Layer / File(s) Summary
Build spec dependency wiring
packages/opencv/build.ncl
Imports openjpeg, adds it to build_deps and runtime_deps, and updates the OpenCV archive comment to 4.13.0.
Configure OpenJPEG flags
packages/opencv/build.sh
Adds WITH_OPENJPEG=ON and BUILD_OPENJPEG=OFF to the CMake configure step with an inline comment about system OpenJPEG linking.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A bunny hopped through buildland bright,
with OpenJPEG now set right.
No bundled hops, just system cheer,
and OpenCV boots crystal-clear.
🐇🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: switching OpenCV to the system OpenJPEG instead of the bundled copy.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch opencv-system-openjpeg

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
packages/opencv/build.sh (1)

16-35: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Add a link assertion before removing the advisory override.

The flags should select system OpenJPEG, but a post-install readelf check would make the security contract explicit and catch CMake silently dropping JPEG2000 support or not dynamically linking libopenjp2.so.

Suggested guard
 make DESTDIR="$OUTPUT_DIR" install
+
+# Ensure imgcodecs links the system OpenJPEG shared library.
+readelf -d "$OUTPUT_DIR"/usr/lib/libopencv_imgcodecs.so* \
+  | grep -q 'Shared library: \[libopenjp2\.so'
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/opencv/build.sh` around lines 16 - 35, Add a post-install assertion
to make the OpenJPEG security contract explicit. In build.sh, after the make
install step, verify the built OpenCV binaries with a readelf-based check that
they dynamically link against libopenjp2.so and did not silently drop JPEG2000
support. Use the existing CMake/OpenJPEG setup block as the target area, and
keep the system OpenJPEG selection (WITH_OPENJPEG=ON, BUILD_OPENJPEG=OFF)
unchanged while adding the link validation guard.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/opencv/build.sh`:
- Around line 16-35: Add a post-install assertion to make the OpenJPEG security
contract explicit. In build.sh, after the make install step, verify the built
OpenCV binaries with a readelf-based check that they dynamically link against
libopenjp2.so and did not silently drop JPEG2000 support. Use the existing
CMake/OpenJPEG setup block as the target area, and keep the system OpenJPEG
selection (WITH_OPENJPEG=ON, BUILD_OPENJPEG=OFF) unchanged while adding the link
validation guard.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 06cf33c8-f00c-40dd-af29-5750d96c5b10

📥 Commits

Reviewing files that changed from the base of the PR and between d97df27 and d737c6b.

📒 Files selected for processing (2)
  • packages/opencv/build.ncl
  • packages/opencv/build.sh

@edge-delta edge-delta Bot 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.

Excellent contribution! Linking system openjpeg is a clean and robust way to resolve the security vulnerability (OSS-Fuzz OSV-2023-444 / heap-overflow in opj_jp2_apply_pclr) by carrying the upstream patch. Both the Nix-style declarations in build.ncl and the cmake configure flags in build.sh are perfectly implemented and correctly include openjpeg in both build-time and runtime dependencies. The CI checks and runtime closure checks have also passed successfully.

@bryan-minimal
bryan-minimal added this pull request to the merge queue Jun 27, 2026
Merged via the queue into main with commit a08d4c2 Jun 27, 2026
3 checks passed
@bryan-minimal
bryan-minimal deleted the opencv-system-openjpeg branch June 27, 2026 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0