1 From 785af1ab577f899d2e54172ff120f404709bf172 Mon Sep 17 00:00:00 2001
2 From: Nikos Mavrogiannopoulos <nmav@redhat.com>
3 Date: Wed, 4 Jan 2017 15:22:13 +0100
4 Subject: [PATCH] opencdk: added error checking in the stream reading functions
6 This addresses an out of memory error. Issue found using oss-fuzz:
7 https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=337
9 Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
11 lib/opencdk/read-packet.c | 40 +++++++++++++++++++++++++++++++++++-----
12 1 file changed, 35 insertions(+), 5 deletions(-)
14 Index: gnutls28-3.4.10/lib/opencdk/read-packet.c
15 ===================================================================
16 --- gnutls28-3.4.10.orig/lib/opencdk/read-packet.c 2017-01-26 10:10:49.072776537 -0500
17 +++ gnutls28-3.4.10/lib/opencdk/read-packet.c 2017-01-26 10:10:49.072776537 -0500
19 static u32 read_32(cdk_stream_t s)
27 stream_read(s, buf, 4, &nread);
31 return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
35 static u16 read_16(cdk_stream_t s)
45 read_subpkt(cdk_stream_t inp, cdk_subpkt_t * r_ctx, size_t * r_nbytes)
49 size_t size, nread, n;
54 c = cdk_stream_getc(inp);
59 + if (size == (u32)-1)
60 + return CDK_Inv_Packet;
63 } else if (c >= 192 && c < 255) {
64 c1 = cdk_stream_getc(inp);
66 + return CDK_Inv_Packet;
72 read_old_length(cdk_stream_t inp, int ctb, size_t * r_len, size_t * r_size)
74 int llen = ctb & 0x03;
78 - *r_len = cdk_stream_getc(inp);
79 + c = cdk_stream_getc(inp);
85 } else if (llen == 1) {
86 *r_len = read_16(inp);
87 + if (*r_len == (u16)-1)
90 } else if (llen == 2) {
91 *r_len = read_32(inp);
92 + if (*r_len == (u32)-1) {
102 @@ -856,15 +875,25 @@
105 c = cdk_stream_getc(inp);
112 else if (c >= 192 && c <= 223) {
113 c1 = cdk_stream_getc(inp);
118 *r_len = ((c - 192) << 8) + c1 + 192;
119 } else if (c == 255) {
120 *r_len = read_32(inp);
121 + if (*r_len == (u32)-1) {
127 *r_len = 1 << (c & 0x1f);