Repository navigation
opencv: link system openjpeg 2.5.4 instead of the bundled copy - #301
Conversation
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>
📝 WalkthroughWalkthroughOpenCV’s build spec now imports system ChangesOpenCV OpenJPEG linkage
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/opencv/build.sh (1)
16-35: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick winAdd a link assertion before removing the advisory override.
The flags should select system OpenJPEG, but a post-install
readelfcheck would make the security contract explicit and catch CMake silently dropping JPEG2000 support or not dynamically linkinglibopenjp2.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
📒 Files selected for processing (2)
packages/opencv/build.nclpackages/opencv/build.sh
There was a problem hiding this comment.
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.
What
Switch opencv (4.13.0) from its bundled
3rdparty/openjpegto our system openjpeg 2.5.4:build.sh: add-DWITH_OPENJPEG=ON -DBUILD_OPENJPEG=OFFso cmake'sfind_package(OpenJPEG)detects and links the systemlibopenjp2instead of compiling the vendored copy.build.ncl: addopenjpegtobuild_deps(configure-time headers +OpenJPEGConfig.cmake/libopenjp2.pc, which our openjpeg pkg exports) and toruntime_deps(imgcodecs dynamically linkslibopenjp2.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 (commitc8fef1d, 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.tomlentry,< 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
/usr/lib/libopenjp2.so), notbuild— that's the proof it picked up our 2.5.4 rather than the bundle.OSV-2023-444override inminimal-supply-chain/data/advisory_overrides.toml— the finding no longer applies once the bundle is gone.🤖 Generated with Claude Code
Summary by CodeRabbit