fix(libSceLibreSsl3): reject oversized lengths in AES_wrap_key and AES_unwrap_key #23208

Merged
gurtgg merged 1 commit from upstream-pr/23208 into main 2026-11-18 20:59:21 +00:00
gurtgg commented 2026-11-18 20:19:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gurtgg on 2026-11-18T20:19:18Z.


What

  • Reject oversized inlen > INT_MAX - 8 in AES_wrap_key_nid_postfix (core/libs/prx/libSceLibreSsl3/src/AesWrap.cpp) and length > INT_MAX in AES_unwrap_key_nid_postfix (core/libs/prx/libSceLibreSsl3/src/Aes.cpp) with -1, matching LibreSSL 4.0.0 aes.c and preventing multi-gigabyte guest reads/allocations and signed int return overflow.
  • Keep the existing null-pointer std::invalid_argument check first in AES_wrap_key_nid_postfix so existing null-argument tests remain unchanged.

Tested

  • CheckKeyWrapBounds (core/libs/prx/libSceLibreSsl3/tests/AesCcm.cpp): verifies a 16-byte RFC 3394 wrap/unwrap round-trip returns 24 and 16, and negatively controls oversized aligned lengths (0x7ffffff8u, 0x80000000u, 0xfffffff8u for AES_wrap_key_nid_postfix; 0x80000000u, 0x80000008u, 0xfffffff8u for AES_unwrap_key_nid_postfix) by asserting -1 without modifying pre-filled sentinel output bytes.

Checklist

  • tools/check_conventions.py passes (UTF-8 no-BOM LF, zero disallowed comments, Conventional Commits)
  • Negative-control regression test fails against unmodified main and passes after fix
  • AI-assisted: yes
  • Depends on: none
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5383416385 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23208). Originally posted by [gurtgg](https://github.com/gurtgg) on 2026-11-18T20:19:18Z. --- ### What - Reject oversized `inlen > INT_MAX - 8` in `AES_wrap_key_nid_postfix` (`core/libs/prx/libSceLibreSsl3/src/AesWrap.cpp`) and `length > INT_MAX` in `AES_unwrap_key_nid_postfix` (`core/libs/prx/libSceLibreSsl3/src/Aes.cpp`) with `-1`, matching LibreSSL 4.0.0 `aes.c` and preventing multi-gigabyte guest reads/allocations and signed `int` return overflow. - Keep the existing null-pointer `std::invalid_argument` check first in `AES_wrap_key_nid_postfix` so existing null-argument tests remain unchanged. ### Tested - `CheckKeyWrapBounds` (`core/libs/prx/libSceLibreSsl3/tests/AesCcm.cpp`): verifies a 16-byte RFC 3394 wrap/unwrap round-trip returns `24` and `16`, and negatively controls oversized aligned lengths (`0x7ffffff8u`, `0x80000000u`, `0xfffffff8u` for `AES_wrap_key_nid_postfix`; `0x80000000u`, `0x80000008u`, `0xfffffff8u` for `AES_unwrap_key_nid_postfix`) by asserting `-1` without modifying pre-filled sentinel output bytes. ### Checklist - [x] `tools/check_conventions.py` passes (UTF-8 no-BOM LF, zero disallowed comments, Conventional Commits) - [x] Negative-control regression test fails against unmodified `main` and passes after fix - AI-assisted: yes - Depends on: none
PrivacyOnion commented 2026-11-18 20:37:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-18T20:37:05Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 3c255b2ff3:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_aes_ccm_tests built cleanly.
  • CTest (guest_libressl3_aes_ccm):
    1/1 Test #4424: guest_libressl3_aes_ccm ..........   Passed    3.96 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/src/Aes.cpp, core/libs/prx/libSceLibreSsl3/src/AesWrap.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4424: guest_libressl3_aes_ccm ..........Subprocess aborted***Exception:   1.84 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • AES_unwrap_key_nid_postfix bounds check (Aes.cpp:108): verified that length > static_cast(INT_MAX) accurately guards against signed int return overflow (length - 8 returning as int) and aligns with LibreSSL upstream aes.c bounds checking
    • AES_wrap_key_nid_postfix bounds check (AesWrap.cpp:59): verified inlen > static_caststd::uint32_t(INT_MAX - 8) prevents integer overflow on inlen + 8 and guarantees return value fits cleanly in signed int without truncation or wrapping
    • Null pointer validation order: checked that AES_wrap_key_nid_postfix preserves existing null pointer checks throwing std::invalid_argument before inlen validation, maintaining compatibility with existing tests
    • CopyIn / CopyOut buffer chunking safety: verified that all guest memory access in CopyInChunks / CopyOutChunks utilizes size_t safely and cannot receive an unvalidated oversized length
    • AGENTS.md conventions & test completeness: verified zero comments added in Aes.cpp, AesWrap.cpp, and AesCcm.cpp, UTF-8 clean, and CheckKeyWrapBounds explicitly asserts boundary return values and ensures output buffers remain unmodified on rejection

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5964642475 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23208#pullrequestreview-5964642475). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-18T20:37:05Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `3c255b2ff3`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libressl3_aes_ccm_tests` built cleanly. - **CTest** (`guest_libressl3_aes_ccm`): ``` 1/1 Test #4424: guest_libressl3_aes_ccm .......... Passed 3.96 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibreSsl3/src/Aes.cpp`, `core/libs/prx/libSceLibreSsl3/src/AesWrap.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4424: guest_libressl3_aes_ccm ..........Subprocess aborted***Exception: 1.84 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - AES_unwrap_key_nid_postfix bounds check (Aes.cpp:108): verified that length > static_cast<unsigned int>(INT_MAX) accurately guards against signed int return overflow (length - 8 returning as int) and aligns with LibreSSL upstream aes.c bounds checking - AES_wrap_key_nid_postfix bounds check (AesWrap.cpp:59): verified inlen > static_cast<std::uint32_t>(INT_MAX - 8) prevents integer overflow on inlen + 8 and guarantees return value fits cleanly in signed int without truncation or wrapping - Null pointer validation order: checked that AES_wrap_key_nid_postfix preserves existing null pointer checks throwing std::invalid_argument before inlen validation, maintaining compatibility with existing tests - CopyIn / CopyOut buffer chunking safety: verified that all guest memory access in CopyInChunks / CopyOutChunks utilizes size_t safely and cannot receive an unvalidated oversized length - AGENTS.md conventions & test completeness: verified zero comments added in Aes.cpp, AesWrap.cpp, and AesCcm.cpp, UTF-8 clean, and CheckKeyWrapBounds explicitly asserts boundary return values and ensures output buffers remain unmodified on rejection Looks Good To Me
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!23208
No description provided.