Skip to content

Rework memory BIOs and implement BIO_seek (2nd try) #2433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2025

Conversation

nhatnghiho
Copy link
Contributor

@nhatnghiho nhatnghiho commented May 22, 2025

Issues:

Resolves #CryptoAlg-3111

Description of changes:

Same as #2380.

Fixed the state bug that was causing Ruby failures. The failures came from BIO_reset, which tries to "rewind" the R/O buffer to its original state. The old implementation only works when the buffer pointer and read pointer are aligned. But in the new memory BIO, this is not always the case. The buffer pointer (b->data) does not need to align with the read pointer (since we can have a positive bbm->read_off). Our tests didn't cover this because the buffer pointer was synced with read pointer after every test case.

Testing:

Added tests for BIO_reset and BIO_seek functions that attempt to rewind the buffer internally. The tests cover both cases -- when buffer pointer and read pointer are synced and when they are not.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

nhatnghiho and others added 2 commits May 21, 2025 22:57
### Issues:
Resolves #CryptoAlg-3111

### Description of changes: 
This PR creates a wrapper around `BUF_MEM` and adds a new field `off` to
indicate the current offset of the read pointer. This enables us to
implement `BIO_seek`, which is also in this PR.

### Testing:
Unit tests are modified and added to cover more test cases.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license and the ISC license.
@nhatnghiho nhatnghiho requested a review from a team as a code owner May 22, 2025 02:45
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions


BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
BIO *ret;
BUF_MEM *b;
const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
BIO_BUF_MEM *bbm;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: variable 'bbm' is not initialized [cppcoreguidelines-init-variables]

Suggested change
BIO_BUF_MEM *bbm;
BIO_BUF_MEM *bbm = NULL;

@codecov-commenter
Copy link

codecov-commenter commented May 22, 2025

Codecov Report

Attention: Patch coverage is 93.81443% with 6 lines in your changes missing coverage. Please review.

Project coverage is 78.89%. Comparing base (f0b4afe) to head (4c2b05d).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crypto/bio/bio_mem.c 90.32% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2433      +/-   ##
==========================================
+ Coverage   78.88%   78.89%   +0.01%     
==========================================
  Files         621      621              
  Lines      108547   108671     +124     
  Branches    15406    15420      +14     
==========================================
+ Hits        85623    85738     +115     
- Misses      22256    22261       +5     
- Partials      668      672       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

justsmth
justsmth previously approved these changes May 22, 2025
@justsmth justsmth merged commit d93dde3 into aws:main May 23, 2025
113 of 116 checks passed
justsmth added a commit to justsmth/aws-lc that referenced this pull request Jun 5, 2025
justsmth added a commit that referenced this pull request Jun 5, 2025
@justsmth justsmth mentioned this pull request Jun 6, 2025
nhatnghiho added a commit to nhatnghiho/aws-lc that referenced this pull request Jun 6, 2025
Resolves #CryptoAlg-3111

Same as aws#2380.

Fixed the state bug that was causing Ruby failures. The failures came
from `BIO_reset`, which tries to "rewind" the R/O buffer to its original
state. The old implementation only works when the buffer pointer and
read pointer are aligned. But in the new memory BIO, this is not always
the case. The buffer pointer (`b->data`) does not need to align with the
read pointer (since we can have a positive `bbm->read_off`). Our tests
didn't cover this because the buffer pointer was synced with read
pointer after every test case.

Added tests for `BIO_reset` and `BIO_seek` functions that attempt to
rewind the buffer internally. The tests cover both cases -- when buffer
pointer and read pointer are synced and when they are not.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license and the ISC license.
nhatnghiho added a commit to nhatnghiho/aws-lc that referenced this pull request Jun 10, 2025
Resolves #CryptoAlg-3111

Same as aws#2380.

Fixed the state bug that was causing Ruby failures. The failures came
from `BIO_reset`, which tries to "rewind" the R/O buffer to its original
state. The old implementation only works when the buffer pointer and
read pointer are aligned. But in the new memory BIO, this is not always
the case. The buffer pointer (`b->data`) does not need to align with the
read pointer (since we can have a positive `bbm->read_off`). Our tests
didn't cover this because the buffer pointer was synced with read
pointer after every test case.

Added tests for `BIO_reset` and `BIO_seek` functions that attempt to
rewind the buffer internally. The tests cover both cases -- when buffer
pointer and read pointer are synced and when they are not.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license and the ISC license.
justsmth added a commit that referenced this pull request Jun 13, 2025
## What's Changed
* Add build with hardened flag by @m271828 in
#2396
* Openssl tool output ordered by options provided by @justsmth in
#2452
* [SCRUTINICE] Remove redundant condition check by @nhatnghiho in
#2450
* Support relro in delocator by @torben-hansen in
#2455
* Explicitly don't allow buffers aliasing in ctr-drbg implementation by
@torben-hansen in #2458
* Remove unused Windows afunix.h by @justsmth in
#2461
* Revert "Rework memory BIOs and implement BIO_seek (2nd try) (#2433)"
by @justsmth in #2466
* Use max_cert_list for TLSv1.3 NewSessionTicket by @skmcgrail in
#2453
* ML-KEM memory safety by @m271828 in
#2263
* Simplify Compiler CI jobs by @justsmth in
#2430
* Improve support for multilib-style distros in our test scripts by
@justsmth in #2467
* Fix Ruby mainline and nginx CI by @samuel40791765 in
#2460
* Add hardened build back in by @m271828 in
#2474
* Fix OCSP integration test failures by @samuel40791765 in
#2480
* Fix some theoretical missing earlyclobber markers in inline assembly
by @torben-hansen in #2477
* Simplify sshkdf and kbkdf by @torben-hansen in
#2478
* Run 3p module tests on python 3.13, add patch for 3.14 by
@WillChilds-Klein in #2476
* [UPSTREAM] Fix BIO_eof for BIO pairs by @justsmth in
#2440
* Fix service indicator in HKDF, more paranoid zeroization, and simplify
logic by @torben-hansen in #2482


By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license and the ISC license.
nhatnghiho added a commit that referenced this pull request Jul 1, 2025
### Issues:
Resolves #CryptoAlg-3111

### Description of changes: 
Same as #2433. 

Fixed the bug that was causing HAProxy failures. The previous tries
attempted to disallow passing empty memory BIOs into BIO functions (such
as `BIO_reset`) by treating that as errors. HAProxy didn't expect this
error, hence the failures. This PR reverted that behavior.

PQ-TLS integration test also started failing consistently after my first
BIO changes but investigation showed no memory BIO functions were used
in PQ integ test. Concluded that this change is now safe to merge.

### Testing:
No additional testing needed

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license and the ISC license.
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.

4 participants