From 9e5bd8923bff3e4f0cbba05c7dadfe289c66eb6f Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Tue, 6 Dec 2022 14:21:23 +0000 Subject: [PATCH] Fix SMIME_crlf_copy() to properly report an error If the BIO unexpectedly fails to flush then SMIME_crlf_copy() was not correctly reporting the error. We modify it to properly propagate the error condition. Reviewed-by: Hugo Landau Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/19918) --- crypto/asn1/asn_mime.c | 6 +++++- test/bio_memleak_test.c | 10 +++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crypto/asn1/asn_mime.c b/crypto/asn1/asn_mime.c index 014e482e66..a26eac1731 100644 --- a/crypto/asn1/asn_mime.c +++ b/crypto/asn1/asn_mime.c @@ -515,6 +515,7 @@ int SMIME_crlf_copy(BIO *in, BIO *out, int flags) char eol; int len; char linebuf[MAX_SMLEN]; + int ret; /* * Buffer output so we don't write one line at a time. This is useful * when streaming as we don't end up with one OCTET STRING per line. @@ -552,9 +553,12 @@ int SMIME_crlf_copy(BIO *in, BIO *out, int flags) } } } - (void)BIO_flush(out); + ret = BIO_flush(out); BIO_pop(out); BIO_free(bf); + if (ret <= 0) + return 0; + return 1; } diff --git a/test/bio_memleak_test.c b/test/bio_memleak_test.c index e95c21768c..d9c744ff49 100644 --- a/test/bio_memleak_test.c +++ b/test/bio_memleak_test.c @@ -261,13 +261,9 @@ static int test_bio_i2d_ASN1_mime(void) error_callback_fired = 0; - /* - * The call succeeds even if the input stream ends unexpectedly as - * there is no handling for this case in SMIME_crlf_copy(). - */ - if (!TEST_true(i2d_ASN1_bio_stream(out, (ASN1_VALUE*) p7, bio, - SMIME_STREAM | SMIME_BINARY, - ASN1_ITEM_rptr(PKCS7)))) + if (!TEST_false(i2d_ASN1_bio_stream(out, (ASN1_VALUE*) p7, bio, + SMIME_STREAM | SMIME_BINARY, + ASN1_ITEM_rptr(PKCS7)))) goto finish; if (!TEST_int_eq(error_callback_fired, 1))