Repository navigation
feat(libSceLibreSsl3): implement BN_dup - #15627
Conversation
XairRrR
left a comment
There was a problem hiding this comment.
Reviewed — approved
BN_dup matches LibreSSL 4.0.0 BN_new + bn_wexpand + BN_copy, including the queued error codes and the BN_FLG_CONSTTIME copy. It builds, the new test passes, and the test fails both when the export is put back to a throwing stub and when the constant-time copy is removed.
What I ran
Head a87c18213efb31970a888f7dbf02bd7997429226, main 1c8c0efc8c41314402e6966da5bef6ea3d639d10, merged tree d481db5aaf5d72a9b6dee0b1ea8604ba7864bce0 (--no-ff), on Debian 13, GCC 14.2.0, Release, Ninja.
git diff --check 1c8c0efc8c41314402e6966da5bef6ea3d639d10...a87c18213efb31970a888f7dbf02bd7997429226
python3 tools/check_conventions.py --base 1c8c0efc8c41314402e6966da5bef6ea3d639d10
ctest --test-dir build --output-on-failure -R "^guest_libressl3_"
ctest --test-dir build --output-on-failure -R "public_headers"
# negative control 1: src/BignumDup.cpp replaced by main's throwing stub (NotImplemented_nid_no_patch) with the new signature
ninja -C build -j32 -l32 guest_libressl3_bn_dup_tests
ctest --test-dir build --output-on-failure -R "^guest_libressl3_bn_dup$"
# negative control 2: merged src/BignumDup.cpp without the `copy->flags |= source->flags & FlagConstantTime;` line
ninja -C build -j32 -l32 guest_libressl3_bn_dup_tests
ctest --test-dir build --output-on-failure -R "^guest_libressl3_bn_dup$"
# restore merged file, rebuild, rerun, confirm clean tree
git show d481db5aaf5d72a9b6dee0b1ea8604ba7864bce0:core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp > core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp
git status --porcelain
# reference check against the LibreSSL commit cited in the PR (present in the 3rdparty/libressl-openbsd object store)
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/bn/bn_lib.c
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/bn/bn.h
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/err/err.h
Results
| Check | Result |
|---|---|
git diff --check |
clean |
check_conventions.py |
All conventions checks passed |
| Build (script, merged tree) | OK |
| Selected tests (script): 835 ran | 831 passed, 3 skipped (all expected), 0 new failures, 0 timeouts. One known failure (unwrap_self_..._inherits_the_destination_directory_group) also fails on main and is unrelated |
ctest -R ^guest_libressl3_ (11 tests) |
11/11 passed; guest_libressl3_bn_dup takes 0.01 s |
ctest -R public_headers (65 tests) |
65/65 passed |
| Negative control 1 (export back to a stub) | fails: BN_dup_nid_postfix not implemented |
| Negative control 2 (no constant-time flag copy) | fails at test line 90 |
| Worktree after restore | git status --porcelain empty; test passes again |
| Vulkan validation | not applicable (no AGC/shader/VideoOut files) |
Negative control 1 output
1/1 Test #2874: guest_libressl3_bn_dup ...........Subprocess aborted***Exception: 0.01 sec
[libc] unhandled exception 'St13runtime_error' thrown from 0x7f01a74cbb79
terminate called after throwing an instance of 'std::runtime_error'
what(): BN_dup_nid_postfix not implemented
0% tests passed, 1 tests failed out of 1
Negative control 2 output
LibreSSL3 BN_dup check failed at line 90
0% tests passed, 1 tests failed out of 1
Reference check
I checked the implementation line by line against LibreSSL at the commit the PR cites (ce488b78e0bd):
BN_newusescallocand setsBN_FLG_MALLOCED(0x01). On failure it queuesERR_R_MALLOC_FAILURE(1|ERR_R_FATAL= 65).bn_wexpandreturns 0 for a negative word count without queuing anything. That matches the silent null for a negativetop.bn_expand_internalqueuesBN_R_BIGNUM_TOO_LONG(114) aboveINT_MAX / (4 * BN_BITS2), which isINT_MAX / 256with 64-bit words. It allocates a zeroed array of exactlywords, setsdmax = words, and queuesERR_R_MALLOC_FAILUREon failure.BN_copycopies the words,top,neg, andb->flags & BN_FLG_CONSTTIME(0x04).BNerroruses function code 0xfff andERR_LIB_BN= 3. WithERR_PACK, the test's expected codes are 0x03fff041 and 0x03fff072.- The 24-byte
bignum_stlayout matches thestatic_asserts inlibSceLibreSsl/src/Bignum.cpp.
The copy's d comes from calloc_nid_postfix, and the struct has FlagMalloced without FlagStaticData. So the existing BN_free_nid_postfix frees both, and the test's liveBlocks == 0 check confirms it. A source with top > 0 and null d throws where LibreSSL would dereference null, as the project rules require.
Negative control
The PR adds src/BignumDup.cpp. Moving it aside would break the CMake source list, so I replaced it with main's production behaviour: a NotImplemented_nid_no_patch stub, with the new signature so the test still compiles. The test aborted as expected. As a second check, I removed only the BN_FLG_CONSTTIME copy, and the test failed at line 90. I then restored the merged file, rebuilt and reran the test (it passed), and confirmed the worktree is back to the merged tree.
Findings
Blocking
None.
Non-blocking
docs/dev/debt/libSceLibreSsl3.md:28citesbn_lib.c#L71-L276, which runs fromBN_newtoBN_dup. The word, sign andBN_FLG_CONSTTIMEcopy thatcore/libs/prx/libSceLibreSsl3/src/BignumDup.cpp:48reproduces is inBN_copy, at L289-L307, outside the cited range. Extending the range to L307 (in the doc and the PR description) would make the source of the flag behaviour explicit.
Not verified
- Behaviour on a console (the debt entry says so).
- Windows/MinGW build and the new
ENVIRONMENT_MODIFICATIONblock. I only built on Linux with GCC. - The description's claim of 3829/3829 tests passing. I ran only the selected tests, not the full suite.
|
@XairRrR thanks. The debt entry now cites |
✅ AMD GPU test: passedPR head
Unstable on main (1, these also failed in recent main runs on this machine; not counted against this PR)Failing on main too (3, not counted against this PR)Validation IDs already present on main (8 IDs in 467 tests)Skipped (7)ReproduceAutomated build and test on a contributor's AMD machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs. |
47a3a24 to
74f9c46
Compare
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
…onstant-time flag into a new BIGNUM
…ailure and over-long sources
74f9c46 to
578f0d9
Compare
|
Hi @alvaroelpob, main had moved under this PR again (the LibreSsl3 exports, |
|
Hi @alvaroelpob, main moved again under this PR (it implemented |
What
Implements
BN_dupin libSceLibreSsl3, following LibreSSL 4.0.0bn_lib.c: a null source returns null; otherwise a new libSceLibreSslBIGNUMgets a guest-heap copy of the words, the sign and the constant-time flag. A failed allocation or a source overINT_MAX / 256words returns null and queuesERR_R_MALLOC_FAILUREorBN_R_BIGNUM_TOO_LONGon the shared libSceLibreSsl error queue.Tested
Linux x86-64 (WSL2), Release: the full build passes and
ctest --test-dir build -j16passes 3829 of 3829 tests, including the newguest_libressl3_bn_dup.python3 tools/check_conventions.py --base upstream/mainpasses. Not run on a console or in a title.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)