Skip to contentSkip to conversation

sign-file: Fix incorrect return values check

PATCH Superseded Yusong Gao 3 participants 5 messages Nov 21, 2023 – Nov 27, 2023

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Yusong Gao

author
#0 lore raw

PATCH v3 1/1 sign-file: Fix incorrect return values check

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.

Fixes e5a2e3c84782 ("scripts/sign-file.c: Add support for signing with a raw signature")

---
V1, V2: Clarify the description of git message.

---
 scripts/sign-file.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/sign-file.c +6 -6
index 598ef5465f82..dcebbcd6bebd 100644
@@ -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)
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 10 lines · Yusong Gao

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.

Quoted 4 lines
Fixes e5a2e3c84782 ("scripts/sign-file.c: Add support for signing

with a raw signature")

No empty line here.

Quoted 58 lines

---
V1, V2: Clarify the description of git message.
---
 scripts/sign-file.c | 12 ++++++------

 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/sign-file.c +0 -0
index 598ef5465f82..dcebbcd6bebd 100644
@@ -322,7 +322,7 @@ int main(int argc, char **argv)

       CMS_NOSMIMECAP | use_keyid | use_signed_attrs), "CMS_add1_signer");

scripts/sign-file.c +0 -1
-		ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS |

CMS_BINARY) < 0,

scripts/sign-file.c +1 -0
+		ERR(CMS_final(cms, bm, NULL, CMS_NOCERTS |

CMS_BINARY) <= 0,
      "CMS_final");

 #else

scripts/sign-file.c +2 -2
@@ -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);

scripts/sign-file.c +0 -0
@@ -374,9 +374,9 @@ int main(int argc, char **argv)


  if (!raw_sig) {
 #ifndef USE_PKCS7

scripts/sign-file.c +0 -1
-		ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) < 0, "%s",

dest_name);

scripts/sign-file.c +1 -0
+		ERR(i2d_CMS_bio_stream(bd, cms, NULL, 0) <= 0, "%s",

dest_name);
 #else

scripts/sign-file.c +1 -1
-		ERR(i2d_PKCS7_bio(bd, pkcs7) < 0, "%s", dest_name);
+		ERR(i2d_PKCS7_bio(bd, pkcs7) <= 0, "%s", dest_name);

 #endif
  } else {
  BIO *b;

scripts/sign-file.c +0 -0
@@ -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);

scripts/sign-file.c +1 -1
-	ERR(BIO_free(bd) < 0, "%s", dest_name);
+	ERR(BIO_free(bd) <= 0, "%s", dest_name);
Quoted 4 lines


  /* Finally, if we're signing in place, replace the original. */
  if (replace_orig)

BR, Jarkko

author
#2 lore raw

PATCH v3 1/1 sign-file: Fix incorrect return values check

Quoted 78 lines · Jarkko Sakkinen
Quoted 10 lines · Yusong Gao

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.

Quoted 4 lines
Fixes e5a2e3c84782 ("scripts/sign-file.c: Add support for signing

with a raw signature")

No empty line here.

Quoted 59 lines

---
V1, V2: Clarify the description of git message.

---
 scripts/sign-file.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/sign-file.c +1 -1
index 598ef5465f82..dcebbcd6bebd 100644
@@ -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

scripts/sign-file.c +2 -2
@@ -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);
scripts/sign-file.c +2 -2
@@ -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;
scripts/sign-file.c +1 -1
@@ -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);
 
Quoted 6 lines
Quoted 3 lines
      /* Finally, if we're signing in place, replace the original.

*/
if (replace_orig)

BR, Jarkko

Thanks.

BR, Yusong Gao

#3 lore raw
In reply to Yusong Gao
Quoted 7 lines · On Tue, 21 Nov 2023 03:40:44 +0000 "Yusong Gao" <[email protected]> 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

Quoted 15 lines
Fixes e5a2e3c84782 ("scripts/sign-file.c: Add support for signing with a raw signature")

---
V1, V2: Clarify the description of git message.

---
 scripts/sign-file.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/sign-file.c +0 -0
index 598ef5465f82..dcebbcd6bebd 100644

scripts/sign-file.c int main(int argc, char **argv) · line 325

Quoted 5 lines
scripts/sign-file.c +1 -1
@@ -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,

scripts/sign-file.c int main(int argc, char **argv) · line 344

Quoted 9 lines
scripts/sign-file.c +1 -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,

scripts/sign-file.c int main(int argc, char **argv) · line 347

Quoted 4 lines
scripts/sign-file.c +1 -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,

scripts/sign-file.c int main(int argc, char **argv) · line 377

Quoted 9 lines
scripts/sign-file.c +1 -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);

scripts/sign-file.c int main(int argc, char **argv) · line 379

Quoted 3 lines
scripts/sign-file.c +1 -1
 #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);

scripts/sign-file.c int main(int argc, char **argv) · line 399

Quoted 9 lines
scripts/sign-file.c +1 -1
 #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);

Quoted 6 lines
scripts/sign-file.c +0 -1
 	/* Finally, if we're signing in place, replace the original. */
 	if (replace_orig)
--

2.34.1

author
#4 lore raw

PATCH v3 1/1 sign-file: Fix incorrect return values check

Quoted 101 lines · Juerg Haefliger
Quoted 9 lines · On Tue, 21 Nov 2023 03:40:44 +0000 "Yusong Gao" <[email protected]> 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

Quoted 16 lines
Fixes e5a2e3c84782 ("scripts/sign-file.c: Add support for signing

with a raw signature")

---
V1, V2: Clarify the description of git message.
---
 scripts/sign-file.c | 12 ++++++------

 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/sign-file.c +0 -0
index 598ef5465f82..dcebbcd6bebd 100644
@@ -322,7 +322,7 @@ int main(int argc, char **argv)
Quoted 7 lines

                                   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,

scripts/sign-file.c int main(int argc, char **argv) · line 344

Quoted 9 lines

                  "CMS_final");

 #else

scripts/sign-file.c +1 -1
@@ -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,

scripts/sign-file.c int main(int argc, char **argv) · line 347

Quoted 4 lines
scripts/sign-file.c +1 -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,

scripts/sign-file.c int main(int argc, char **argv) · line 379

Quoted 11 lines
scripts/sign-file.c +1 -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);

scripts/sign-file.c int main(int argc, char **argv) · line 379

Quoted 3 lines
scripts/sign-file.c +1 -1
  #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);

Quoted 9 lines
scripts/sign-file.c +0 -0
  #endif

      } else {
              BIO *b;

scripts/sign-file.c +0 -0
@@ -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);
 
Quoted 14 lines

scripts/sign-file.c int main(int argc, char **argv) · line 399

Quoted 2 lines
scripts/sign-file.c +1 -1
-     ERR(BIO_free(bd) < 0, "%s", dest_name);
+     ERR(BIO_free(bd) <= 0, "%s", dest_name);

ERR(BIO_free(bd) != 1, "%s", dest_name);

Quoted 7 lines
scripts/sign-file.c +0 -0
       /* 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