Skip to content

Commit

Permalink
do_print_ex(): Avoid possible integer overflow
Browse files Browse the repository at this point in the history
Fixes Coverity 1604657
Fixes openssl/project#780

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Tom Cosgrove <[email protected]>
Reviewed-by: Paul Dale <[email protected]>
(Merged from openssl/openssl#25084)

(cherry picked from commit e3e15e7)
  • Loading branch information
t8m committed Aug 7, 2024
1 parent 3c1efe4 commit e032985
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions crypto/asn1/a_strex.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdio.h>
#include <string.h>
#include "internal/cryptlib.h"
#include "internal/sizes.h"
#include "crypto/asn1.h"
#include <openssl/crypto.h>
#include <openssl/x509.h>
Expand Down Expand Up @@ -345,8 +346,10 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,

if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
const char *tagname;

tagname = ASN1_tag2str(type);
outlen += strlen(tagname);
/* We can directly cast here as tagname will never be too large. */
outlen += (int)strlen(tagname);
if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
return -1;
outlen++;
Expand All @@ -372,7 +375,7 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,

if (type == -1) {
len = do_dump(lflags, io_ch, arg, str);
if (len < 0)
if (len < 0 || len > INT_MAX - outlen)
return -1;
outlen += len;
return outlen;
Expand All @@ -391,7 +394,7 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
}

len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
if (len < 0)
if (len < 0 || len > INT_MAX - 2 - outlen)
return -1;
outlen += len;
if (quotes)
Expand Down

0 comments on commit e032985

Please sign in to comment.