diff options
author | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2018-01-04 10:52:05 +0100 |
---|---|---|
committer | Tomasz Swierczek <t.swierczek@samsung.com> | 2018-05-21 10:21:12 +0200 |
commit | 1701584f7841a79ae4e97699a8c4ac9f0a09c38f (patch) | |
tree | 1dc5c8ca01f36c5392de07468397f6388c456fdd | |
parent | 52e10d8471cd9e6572d85b4bf15e599bc60b3ce5 (diff) | |
download | libtasn1-1701584f7841a79ae4e97699a8c4ac9f0a09c38f.tar.gz libtasn1-1701584f7841a79ae4e97699a8c4ac9f0a09c38f.tar.bz2 libtasn1-1701584f7841a79ae4e97699a8c4ac9f0a09c38f.zip |
BACKPORT: _asn1_decode_simple_ber: restrict the levels of recursion to 3
On indefinite string decoding, setting a maximum level of recursions
protects the BER decoder from a stack exhaustion due to large amounts
of recursion.
Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
(cherry-picked from upstream c593ae84cfcde8fea45787e53950e0ac71e9ca97)
Change-Id: If3eb039487979b9639b42b314c3b0c89f18737fa
Signed-off-by: Tomasz Swierczek <t.swierczek@samsung.com>
-rw-r--r-- | lib/decoding.c | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/lib/decoding.c b/lib/decoding.c index e5513a3..1069e44 100644 --- a/lib/decoding.c +++ b/lib/decoding.c @@ -45,6 +45,13 @@ #define DECODE_FLAG_HAVE_TAG 1 #define DECODE_FLAG_INDEFINITE (1<<1) +/* On indefinite string decoding, allow this maximum levels + * of recursion. Allowing infinite recursion, makes the BER + * decoder susceptible to stack exhaustion due to that recursion. + */ +#define DECODE_FLAG_LEVEL1 (1<<2) +#define DECODE_FLAG_LEVEL2 (1<<3) +#define DECODE_FLAG_LEVEL3 (1<<4) #define DECR_LEN(l, s) do { \ l -= s; \ @@ -2195,7 +2202,8 @@ _asn1_decode_simple_ber (unsigned int etype, const unsigned char *der, } /* indefinite constructed */ - if (((dflags & DECODE_FLAG_INDEFINITE) || class == ASN1_CLASS_STRUCTURED) && ETYPE_IS_STRING(etype)) + if ((((dflags & DECODE_FLAG_INDEFINITE) || class == ASN1_CLASS_STRUCTURED) && ETYPE_IS_STRING(etype)) && + !(dflags & DECODE_FLAG_LEVEL3)) { len_len = 1; @@ -2215,8 +2223,17 @@ _asn1_decode_simple_ber (unsigned int etype, const unsigned char *der, do { unsigned tmp_len; + unsigned flags = DECODE_FLAG_HAVE_TAG; + + if (dflags & DECODE_FLAG_LEVEL1) + flags |= DECODE_FLAG_LEVEL2; + else if (dflags & DECODE_FLAG_LEVEL2) + flags |= DECODE_FLAG_LEVEL3; + else + flags |= DECODE_FLAG_LEVEL1; - result = asn1_decode_simple_ber(etype, p, der_len, &out, &out_len, &tmp_len); + result = _asn1_decode_simple_ber(etype, p, der_len, &out, &out_len, &tmp_len, + flags); if (result != ASN1_SUCCESS) { warn(); |