* [PATCH v3] sign-file: Fix incorrect return values check
@ 2023-11-21 3:40 Yusong Gao
2023-11-21 20:54 ` Jarkko Sakkinen
2023-11-22 7:20 ` Juerg Haefliger
0 siblings, 2 replies; 5+ messages in thread
From: Yusong Gao @ 2023-11-21 3:40 UTC (permalink / raw)
To: jarkko, davem, dhowells, dwmw2, juergh, zohar, herbert, lists,
dimitri.ledkov
Cc: keyrings, linux-kernel, linux-crypto
There are some wrong return values check in sign-file when call OpenSSL
API. The ERR() check cond is wrong because of the program only check the
return value is < 0 instead of <= 0. For example:
1. CMS_final() return 1 for success or 0 for failure.
2. i2d_CMS_bio_stream() returns 1 for success or 0 for failure.
3. i2d_TYPEbio() return 1 for success and 0 for failure.
4. BIO_free() return 1 for success and 0 for failure.
Link: https://www.openssl.org/docs/manmaster/man3/
Fixes: e5a2e3c84782 ("scripts/sign-file.c: Add support for signing with a raw signature")
Signed-off-by: Yusong Gao <a869920004@gmail.com>
---
V1, V2: Clarify the description of git message.
---
scripts/sign-file.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/scripts/sign-file.c b/scripts/sign-file.c
index 598ef5465f82..dcebbcd6bebd 100644
--- a/scripts/sign-file.c
+++ b/scripts/sign-file.c
@@ -322,7 +322,7 @@ int main(int argc, char **argv)
CMS_NOSMIMECAP | use_keyid |
use_signed_attrs),
"CMS_add1_signer");
- ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0,
+ ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) <= 0,
"CMS_final");
#else
@@ -341,10 +341,10 @@ int main(int argc, char **argv)
b = BIO_new_file(sig_file_name, "wb");
ERR(!b, "%s", sig_file_name);
#ifndef USE_PKCS7
- ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0,
+ ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) <= 0,
"%s", sig_file_name);
#else
- ERR(i2d_PKCS7_bio(b, pkcs7) < 0,
+ ERR(i2d_PKCS7_bio(b, pkcs7) <= 0,
"%s", sig_file_name);
#endif
BIO_free(b);
@@ -374,9 +374,9 @@ int main(int argc, char **argv)
if (!raw_sig) {
#ifndef USE_PKCS7
- ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name);
+ ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) <= 0, "%s", dest_name);
#else
- ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name);
+ ERR(i2d_PKCS7_bio(bd, pkcs7) <= 0, "%s", dest_name);
#endif
} else {
BIO *b;
@@ -396,7 +396,7 @@ int main(int argc, char **argv)
ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name);
ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name);
- ERR(BIO_free(bd) < 0, "%s", dest_name);
+ ERR(BIO_free(bd) <= 0, "%s", dest_name);
/* Finally, if we're signing in place, replace the original. */
if (replace_orig)
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3] sign-file: Fix incorrect return values check 2023-11-21 3:40 [PATCH v3] sign-file: Fix incorrect return values check Yusong Gao @ 2023-11-21 20:54 ` Jarkko Sakkinen 2023-11-22 3:19 ` Yusong Gao 2023-11-22 7:20 ` Juerg Haefliger 1 sibling, 1 reply; 5+ messages in thread From: Jarkko Sakkinen @ 2023-11-21 20:54 UTC (permalink / raw) To: Yusong Gao, davem, dhowells, dwmw2, juergh, zohar, herbert, lists, dimitri.ledkov Cc: keyrings, linux-kernel, linux-crypto On Tue, 2023-11-21 at 03:40 +0000, Yusong Gao wrote: > There are some wrong return values check in sign-file when call > OpenSSL > API. The ERR() check cond is wrong because of the program only check > the > return value is < 0 instead of <= 0. For example: > 1. CMS_final() return 1 for success or 0 for failure. > 2. i2d_CMS_bio_stream() returns 1 for success or 0 for failure. > 3. i2d_TYPEbio() return 1 for success and 0 for failure. > 4. BIO_free() return 1 for success and 0 for failure. > > Link: https://www.openssl.org/docs/manmaster/man3/ > Fixes: e5a2e3c84782 ("scripts/sign-file.c: Add support for signing > with a raw signature") > No empty line here. > Signed-off-by: Yusong Gao <a869920004@gmail.com> > --- > V1, V2: Clarify the description of git message. > --- > scripts/sign-file.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/scripts/sign-file.c b/scripts/sign-file.c > index 598ef5465f82..dcebbcd6bebd 100644 > --- a/scripts/sign-file.c > +++ b/scripts/sign-file.c > @@ -322,7 +322,7 @@ int main(int argc, char **argv) > CMS_NOSMIMECAP | use_keyid | > use_signed_attrs), > "CMS_add1_signer"); > - ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | > CMS_BINARY) < 0, > + ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | > CMS_BINARY) <= 0, > "CMS_final"); > > #else > @@ -341,10 +341,10 @@ int main(int argc, char **argv) > b = BIO_new_file(sig_file_name, "wb"); > ERR(!b, "%s", sig_file_name); > #ifndef USE_PKCS7 > - ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, > + ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) <= > 0, > "%s", sig_file_name); > #else > - ERR(i2d_PKCS7_bio(b, pkcs7) < 0, > + ERR(i2d_PKCS7_bio(b, pkcs7) <= 0, > "%s", sig_file_name); > #endif > BIO_free(b); > @@ -374,9 +374,9 @@ int main(int argc, char **argv) > > if (!raw_sig) { > #ifndef USE_PKCS7 > - ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", > dest_name); > + ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) <= 0, "%s", > dest_name); > #else > - ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name); > + ERR(i2d_PKCS7_bio(bd, pkcs7) <= 0, "%s", dest_name); > #endif > } else { > BIO *b; > @@ -396,7 +396,7 @@ int main(int argc, char **argv) > ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", > dest_name); > ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < > 0, "%s", dest_name); > > - ERR(BIO_free(bd) < 0, "%s", dest_name); > + ERR(BIO_free(bd) <= 0, "%s", dest_name); > > /* Finally, if we're signing in place, replace the original. > */ > if (replace_orig) BR, Jarkko ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3] sign-file: Fix incorrect return values check 2023-11-21 20:54 ` Jarkko Sakkinen @ 2023-11-22 3:19 ` Yusong Gao 0 siblings, 0 replies; 5+ messages in thread From: Yusong Gao @ 2023-11-22 3:19 UTC (permalink / raw) To: jarkko Cc: a869920004, davem, dhowells, dimitri.ledkov, dwmw2, herbert, juergh, keyrings, linux-crypto, linux-kernel, lists, zohar On Wed, Nov 22, 2023 at 4:54 AM Jarkko Sakkinen <jarkko@kernel.org> wrote: > > On Tue, 2023-11-21 at 03:40 +0000, Yusong Gao wrote: > > There are some wrong return values check in sign-file when call > > OpenSSL > > API. The ERR() check cond is wrong because of the program only check > > the > > return value is < 0 instead of <= 0. For example: > > 1. CMS_final() return 1 for success or 0 for failure. > > 2. i2d_CMS_bio_stream() returns 1 for success or 0 for failure. > > 3. i2d_TYPEbio() return 1 for success and 0 for failure. > > 4. BIO_free() return 1 for success and 0 for failure. > > > > Link: https://www.openssl.org/docs/manmaster/man3/ > > Fixes: e5a2e3c84782 ("scripts/sign-file.c: Add support for signing > > with a raw signature") > > > > No empty line here. > > > Signed-off-by: Yusong Gao <a869920004@gmail.com> > > --- > > V1, V2: Clarify the description of git message. > > --- > > scripts/sign-file.c | 12 ++++++------ > > 1 file changed, 6 insertions(+), 6 deletions(-) > > > > diff --git a/scripts/sign-file.c b/scripts/sign-file.c > > index 598ef5465f82..dcebbcd6bebd 100644 > > --- a/scripts/sign-file.c > > +++ b/scripts/sign-file.c > > @@ -322,7 +322,7 @@ int main(int argc, char **argv) > > CMS_NOSMIMECAP | use_keyid | > > use_signed_attrs), > > "CMS_add1_signer"); > > - ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | > > CMS_BINARY) < 0, > > + ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | > > CMS_BINARY) <= 0, > > "CMS_final"); > > > > #else > > @@ -341,10 +341,10 @@ int main(int argc, char **argv) > > b = BIO_new_file(sig_file_name, "wb"); > > ERR(!b, "%s", sig_file_name); > > #ifndef USE_PKCS7 > > - ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, > > + ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) <= > > 0, > > "%s", sig_file_name); > > #else > > - ERR(i2d_PKCS7_bio(b, pkcs7) < 0, > > + ERR(i2d_PKCS7_bio(b, pkcs7) <= 0, > > "%s", sig_file_name); > > #endif > > BIO_free(b); > > @@ -374,9 +374,9 @@ int main(int argc, char **argv) > > > > if (!raw_sig) { > > #ifndef USE_PKCS7 > > - ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", > > dest_name); > > + ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) <= 0, "%s", > > dest_name); > > #else > > - ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name); > > + ERR(i2d_PKCS7_bio(bd, pkcs7) <= 0, "%s", dest_name); > > #endif > > } else { > > BIO *b; > > @@ -396,7 +396,7 @@ int main(int argc, char **argv) > > ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", > > dest_name); > > ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < > > 0, "%s", dest_name); > > > > - ERR(BIO_free(bd) < 0, "%s", dest_name); > > + ERR(BIO_free(bd) <= 0, "%s", dest_name); > > > > /* Finally, if we're signing in place, replace the original. > > */ > > if (replace_orig) > > > BR, Jarkko Thanks. BR, Yusong Gao ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3] sign-file: Fix incorrect return values check 2023-11-21 3:40 [PATCH v3] sign-file: Fix incorrect return values check Yusong Gao 2023-11-21 20:54 ` Jarkko Sakkinen @ 2023-11-22 7:20 ` Juerg Haefliger 2023-11-27 2:57 ` Yusong Gao 1 sibling, 1 reply; 5+ messages in thread From: Juerg Haefliger @ 2023-11-22 7:20 UTC (permalink / raw) To: Yusong Gao Cc: jarkko, davem, dhowells, dwmw2, zohar, herbert, lists, dimitri.ledkov, keyrings, linux-kernel, linux-crypto [-- Attachment #1: Type: text/plain, Size: 3125 bytes --] On Tue, 21 Nov 2023 03:40:44 +0000 "Yusong Gao" <a869920004@gmail.com> wrote: > There are some wrong return values check in sign-file when call OpenSSL > API. The ERR() check cond is wrong because of the program only check the > return value is < 0 instead of <= 0. For example: > 1. CMS_final() return 1 for success or 0 for failure. > 2. i2d_CMS_bio_stream() returns 1 for success or 0 for failure. > 3. i2d_TYPEbio() return 1 for success and 0 for failure. > 4. BIO_free() return 1 for success and 0 for failure. Good catch! In this case I'd probably be more strict and check for '!= 1'. See below. ...Juerg > Link: https://www.openssl.org/docs/manmaster/man3/ > Fixes: e5a2e3c84782 ("scripts/sign-file.c: Add support for signing with a raw signature") > > Signed-off-by: Yusong Gao <a869920004@gmail.com> > --- > V1, V2: Clarify the description of git message. > --- > scripts/sign-file.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/scripts/sign-file.c b/scripts/sign-file.c > index 598ef5465f82..dcebbcd6bebd 100644 > --- a/scripts/sign-file.c > +++ b/scripts/sign-file.c > @@ -322,7 +322,7 @@ int main(int argc, char **argv) > CMS_NOSMIMECAP | use_keyid | > use_signed_attrs), > "CMS_add1_signer"); > - ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) < 0, > + ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) <= 0, ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) != 1, > "CMS_final"); > > #else > @@ -341,10 +341,10 @@ int main(int argc, char **argv) > b = BIO_new_file(sig_file_name, "wb"); > ERR(!b, "%s", sig_file_name); > #ifndef USE_PKCS7 > - ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, > + ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) <= 0, ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) != 1, > "%s", sig_file_name); > #else > - ERR(i2d_PKCS7_bio(b, pkcs7) < 0, > + ERR(i2d_PKCS7_bio(b, pkcs7) <= 0, ERR(i2d_PKCS7_bio(b, pkcs7) != 1, > "%s", sig_file_name); > #endif > BIO_free(b); > @@ -374,9 +374,9 @@ int main(int argc, char **argv) > > if (!raw_sig) { > #ifndef USE_PKCS7 > - ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", dest_name); > + ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) <= 0, "%s", dest_name); ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) != 1, "%s", dest_name); > #else > - ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name); > + ERR(i2d_PKCS7_bio(bd, pkcs7) <= 0, "%s", dest_name); ERR(i2d_PKCS7_bio(bd, pkcs7) != 1, "%s", dest_name); > #endif > } else { > BIO *b; > @@ -396,7 +396,7 @@ int main(int argc, char **argv) > ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", dest_name); > ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, "%s", dest_name); > > - ERR(BIO_free(bd) < 0, "%s", dest_name); > + ERR(BIO_free(bd) <= 0, "%s", dest_name); ERR(BIO_free(bd) != 1, "%s", dest_name); > > /* Finally, if we're signing in place, replace the original. */ > if (replace_orig) > -- > 2.34.1 > [-- Attachment #2: attachment.sig --] [-- Type: application/pgp-signature, Size: 849 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3] sign-file: Fix incorrect return values check 2023-11-22 7:20 ` Juerg Haefliger @ 2023-11-27 2:57 ` Yusong Gao 0 siblings, 0 replies; 5+ messages in thread From: Yusong Gao @ 2023-11-27 2:57 UTC (permalink / raw) To: juergh, jarkko Cc: a869920004, davem, dhowells, dimitri.ledkov, dwmw2, herbert, keyrings, linux-crypto, linux-kernel, lists, zohar [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #1: Type: text/plain, Size: 4189 bytes --] On Wed, Nov 22, 2023 at 3:21 PM Juerg Haefliger <juergh@proton.me> wrote: > > On Tue, 21 Nov 2023 03:40:44 +0000 > "Yusong Gao" <a869920004@gmail.com> wrote: > > > There are some wrong return values check in sign-file when call > > OpenSSL > > API. The ERR() check cond is wrong because of the program only check > > the > > return value is < 0 instead of <= 0. For example: > > 1. CMS_final() return 1 for success or 0 for failure. > > 2. i2d_CMS_bio_stream() returns 1 for success or 0 for failure. > > 3. i2d_TYPEbio() return 1 for success and 0 for failure. > > 4. BIO_free() return 1 for success and 0 for failure. > > Good catch! In this case I'd probably be more strict and check for '!= > 1'. > See below. > > ...Juerg > > > > Link: https://www.openssl.org/docs/manmaster/man3/ > > Fixes: e5a2e3c84782 ("scripts/sign-file.c: Add support for signing > > with a raw signature") > > > > Signed-off-by: Yusong Gao <a869920004@gmail.com> > > --- > > V1, V2: Clarify the description of git message. > > --- > > scripts/sign-file.c | 12 ++++++------ > > 1 file changed, 6 insertions(+), 6 deletions(-) > > > > diff --git a/scripts/sign-file.c b/scripts/sign-file.c > > index 598ef5465f82..dcebbcd6bebd 100644 > > --- a/scripts/sign-file.c > > +++ b/scripts/sign-file.c > > @@ -322,7 +322,7 @@ int main(int argc, char **argv) > > CMS_NOSMIMECAP | use_keyid | > > use_signed_attrs), > > "CMS_add1_signer"); > > - ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) > > < 0, > > + ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) > > <= 0, > > ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS | CMS_BINARY) != 1, > > > > "CMS_final"); > > > > #else > > @@ -341,10 +341,10 @@ int main(int argc, char **argv) > > b = BIO_new_file(sig_file_name, "wb"); > > ERR(!b, "%s", sig_file_name); > > #ifndef USE_PKCS7 > > - ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) < 0, > > + ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) <= 0, > > ERR(i2d_CMS_bio_stream(b, cms, NULL, 0) != 1, > > > > "%s", sig_file_name); > > #else > > - ERR(i2d_PKCS7_bio(b, pkcs7) < 0, > > + ERR(i2d_PKCS7_bio(b, pkcs7) <= 0, > > ERR(i2d_PKCS7_bio(b, pkcs7) != 1, > > > > "%s", sig_file_name); > > #endif > > BIO_free(b); > > @@ -374,9 +374,9 @@ int main(int argc, char **argv) > > > > if (!raw_sig) { > > #ifndef USE_PKCS7 > > - ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s", > > dest_name); > > + ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) <= 0, "%s", > > dest_name); > > > ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) != 1, "%s", dest_name); > > > > #else > > - ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name); > > + ERR(i2d_PKCS7_bio(bd, pkcs7) <= 0, "%s", dest_name); > > ERR(i2d_PKCS7_bio(bd, pkcs7) != 1, "%s", dest_name); > > > > #endif > > } else { > > BIO *b; > > @@ -396,7 +396,7 @@ int main(int argc, char **argv) > > ERR(BIO_write(bd, &sig_info, sizeof(sig_info)) < 0, "%s", > > dest_name); > > ERR(BIO_write(bd, magic_number, sizeof(magic_number) - 1) < 0, > > "%s", dest_name); > > > > - ERR(BIO_free(bd) < 0, "%s", dest_name); > > + ERR(BIO_free(bd) <= 0, "%s", dest_name); > > ERR(BIO_free(bd) != 1, "%s", dest_name); > > > > > > /* Finally, if we're signing in place, replace the original. > > */ > > if (replace_orig) > > -- > > 2.34.1 > > > Agreed. Will do. Thanks for your review. BR, Yusong Gao ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-11-27 2:57 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2023-11-21 3:40 [PATCH v3] sign-file: Fix incorrect return values check Yusong Gao 2023-11-21 20:54 ` Jarkko Sakkinen 2023-11-22 3:19 ` Yusong Gao 2023-11-22 7:20 ` Juerg Haefliger 2023-11-27 2:57 ` Yusong Gao
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox