Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/crypto/external/bsd/netpgp/dist/src/lib More checks for the ...



details:   https://anonhg.NetBSD.org/src/rev/13ef198c5ca4
branches:  trunk
changeset: 747960:13ef198c5ca4
user:      agc <agc%NetBSD.org@localhost>
date:      Wed Oct 07 04:56:51 2009 +0000

description:
More checks for the return value from memory allocation.

diffstat:

 crypto/external/bsd/netpgp/dist/src/lib/compress.c |   32 +++-
 crypto/external/bsd/netpgp/dist/src/lib/keyring.c  |   23 ++-
 crypto/external/bsd/netpgp/dist/src/lib/reader.c   |  152 +++++++++++++-------
 3 files changed, 139 insertions(+), 68 deletions(-)

diffs (truncated from 398 to 300 lines):

diff -r 5f6451432b8c -r 13ef198c5ca4 crypto/external/bsd/netpgp/dist/src/lib/compress.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/compress.c        Wed Oct 07 04:18:47 2009 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/compress.c        Wed Oct 07 04:56:51 2009 +0000
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: compress.c,v 1.13 2009/10/04 21:58:25 agc Exp $");
+__RCSID("$NetBSD: compress.c,v 1.14 2009/10/07 04:56:51 agc Exp $");
 #endif
 
 #ifdef HAVE_ZLIB_H
@@ -416,14 +416,20 @@
                     const unsigned int len,
                     __ops_output_t *out)
 {
-       compress_t      *zip = calloc(1, sizeof(compress_t));
+       compress_t      *zip;
        size_t           sz_in;
        size_t           sz_out;
+       int              ret;
        int              r = 0;
 
        /* compress the data */
        const int       level = Z_DEFAULT_COMPRESSION;  /* \todo allow varying
                                                         * levels */
+
+       if ((zip = calloc(1, sizeof(*zip))) == NULL) {
+               (void) fprintf(stderr, "__ops_writez: bad alloc\n");
+               return 0;
+       }
        zip->stream.zalloc = Z_NULL;
        zip->stream.zfree = Z_NULL;
        zip->stream.opaque = NULL;
@@ -443,8 +449,17 @@
 
        sz_in = len * sizeof(unsigned char);
        sz_out = ((101 * sz_in) / 100) + 12;    /* from zlib webpage */
-       zip->src = calloc(1, sz_in);
-       zip->dst = calloc(1, sz_out);
+       if ((zip->src = calloc(1, sz_in)) == NULL) {
+               free(zip);
+               (void) fprintf(stderr, "__ops_writez: bad alloc2\n");
+               return 0;
+       }
+       if ((zip->dst = calloc(1, sz_out)) == NULL) {
+               free(zip->src);
+               free(zip);
+               (void) fprintf(stderr, "__ops_writez: bad alloc3\n");
+               return 0;
+       }
        (void) memcpy(zip->src, data, len);
 
        /* setup stream */
@@ -461,8 +476,13 @@
        } while (r != Z_STREAM_END);
 
        /* write it out */
-       return (__ops_write_ptag(out, OPS_PTAG_CT_COMPRESSED) &&
+       ret = __ops_write_ptag(out, OPS_PTAG_CT_COMPRESSED) &&
                __ops_write_length(out, (unsigned)(zip->stream.total_out + 1))&&
                __ops_write_scalar(out, OPS_C_ZLIB, 1) &&
-               __ops_write(out, zip->dst, (unsigned)zip->stream.total_out));
+               __ops_write(out, zip->dst, (unsigned)zip->stream.total_out);
+
+       free(zip->src);
+       free(zip->dst);
+       free(zip);
+       return ret;
 }
diff -r 5f6451432b8c -r 13ef198c5ca4 crypto/external/bsd/netpgp/dist/src/lib/keyring.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/keyring.c Wed Oct 07 04:18:47 2009 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/keyring.c Wed Oct 07 04:56:51 2009 +0000
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: keyring.c,v 1.21 2009/10/06 02:26:05 agc Exp $");
+__RCSID("$NetBSD: keyring.c,v 1.22 2009/10/07 04:56:51 agc Exp $");
 #endif
 
 #ifdef HAVE_FCNTL_H
@@ -267,7 +267,10 @@
                return OPS_FINISHED;
 
        case OPS_PTAG_CT_SECRET_KEY:
-               decrypt->seckey = calloc(1, sizeof(*decrypt->seckey));
+               if ((decrypt->seckey = calloc(1, sizeof(*decrypt->seckey))) == NULL) {
+                       (void) fprintf(stderr, "decrypt_cb: bad alloc\n");
+                       return OPS_FINISHED;
+               }
                decrypt->seckey->checkhash = calloc(1, OPS_CHECKHASH_SIZE);
                *decrypt->seckey = content->seckey;
                return OPS_KEEP_MEMORY;
@@ -396,8 +399,11 @@
        if (dst->userid) {
                free(dst->userid);
        }
-       dst->userid = calloc(1, len + 1);
-       (void) memcpy(dst->userid, src->userid, len);
+       if ((dst->userid = calloc(1, len + 1)) == NULL) {
+               (void) fprintf(stderr, "__ops_copy_userid: bad alloc\n");
+       } else {
+               (void) memcpy(dst->userid, src->userid, len);
+       }
        return dst;
 }
 
@@ -415,9 +421,12 @@
        if (dst->raw) {
                free(dst->raw);
        }
-       dst->raw = calloc(1, src->length);
-       dst->length = src->length;
-       (void) memcpy(dst->raw, src->raw, src->length);
+       if ((dst->raw = calloc(1, src->length)) == NULL) {
+               (void) fprintf(stderr, "__ops_copy_packet: bad alloc\n");
+       } else {
+               dst->length = src->length;
+               (void) memcpy(dst->raw, src->raw, src->length);
+       }
        return dst;
 }
 
diff -r 5f6451432b8c -r 13ef198c5ca4 crypto/external/bsd/netpgp/dist/src/lib/reader.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/reader.c  Wed Oct 07 04:18:47 2009 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/reader.c  Wed Oct 07 04:56:51 2009 +0000
@@ -54,7 +54,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: reader.c,v 1.23 2009/10/07 04:18:47 agc Exp $");
+__RCSID("$NetBSD: reader.c,v 1.24 2009/10/07 04:56:51 agc Exp $");
 #endif
 
 #include <sys/types.h>
@@ -186,17 +186,21 @@
                __ops_reader_destroyer_t *destroyer,
                void *vp)
 {
-       __ops_reader_t *readinfo = calloc(1, sizeof(*readinfo));
+       __ops_reader_t *readinfo;
 
-       *readinfo = stream->readinfo;
-       (void) memset(&stream->readinfo, 0x0, sizeof(stream->readinfo));
-       stream->readinfo.next = readinfo;
-       stream->readinfo.parent = stream;
+       if ((readinfo = calloc(1, sizeof(*readinfo))) == NULL) {
+               (void) fprintf(stderr, "__ops_reader_push: bad alloc\n");
+       } else {
+               *readinfo = stream->readinfo;
+               (void) memset(&stream->readinfo, 0x0, sizeof(stream->readinfo));
+               stream->readinfo.next = readinfo;
+               stream->readinfo.parent = stream;
 
-       /* should copy accumulate flags from other reader? RW */
-       stream->readinfo.accumulate = readinfo->accumulate;
+               /* should copy accumulate flags from other reader? RW */
+               stream->readinfo.accumulate = readinfo->accumulate;
 
-       __ops_reader_set(stream, reader, destroyer, vp);
+               __ops_reader_set(stream, reader, destroyer, vp);
+       }
 }
 
 /**
@@ -297,8 +301,9 @@
 
        if (dearmour->pushback) {
                (void) fprintf(stderr, "push_back: already pushed back\n");
+       } else if ((dearmour->pushback = calloc(1, length)) == NULL) {
+               (void) fprintf(stderr, "push_back: bad alloc\n");
        } else {
-               dearmour->pushback = calloc(1, length);
                for (n = 0; n < length; ++n) {
                        dearmour->pushback[n] = buf[(length - n) - 1];
                }
@@ -532,11 +537,14 @@
 {
        unsigned        n;
 
-       dest->headers = calloc(src->headerc, sizeof(*dest->headers));
-       dest->headerc = src->headerc;
-       for (n = 0; n < src->headerc; ++n) {
-               dest->headers[n].key = strdup(src->headers[n].key);
-               dest->headers[n].value = strdup(src->headers[n].value);
+       if ((dest->headers = calloc(src->headerc, sizeof(*dest->headers))) == NULL) {
+               (void) fprintf(stderr, "__ops_dup_headers: bad alloc\n");
+       } else {
+               dest->headerc = src->headerc;
+               for (n = 0; n < src->headerc; ++n) {
+                       dest->headers[n].key = strdup(src->headers[n].key);
+                       dest->headers[n].value = strdup(src->headers[n].value);
+               }
        }
 }
 
@@ -558,7 +566,11 @@
        __ops_hash_t     *hash;
        int             total;
 
-       hash = calloc(1, sizeof(*hash));
+       if ((hash = calloc(1, sizeof(*hash))) == NULL) {
+               OPS_ERROR(errors, OPS_E_R_BAD_FORMAT,
+                       "process_dash_escaped: bad alloc");
+               return -1;
+       }
        hashstr = __ops_find_header(&dearmour->headers, "Hash");
        if (hashstr) {
                __ops_hash_alg_t alg;
@@ -1012,13 +1024,15 @@
                     __ops_reader_t *readinfo,
                     __ops_cbdata_t *cbinfo)
 {
-       dearmour_t *dearmour = __ops_reader_get_arg(readinfo);
-       __ops_packet_t content;
-       int             ret;
-       unsigned   first;
-       unsigned char  *dest = dest_;
-       int             saved = length;
+       __ops_packet_t   content;
+       unsigned char   *dest = dest_;
+       dearmour_t      *dearmour;
+       unsigned         first;
+       int              saved;
+       int              ret;
 
+       dearmour = __ops_reader_get_arg(readinfo);
+       saved = length;
        if (dearmour->eof64 && !dearmour->buffered) {
                if (dearmour->state != OUTSIDE_BLOCK &&
                    dearmour->state != AT_TRAILER_NAME) {
@@ -1313,18 +1327,21 @@
 {
        dearmour_t *dearmour;
 
-       dearmour = calloc(1, sizeof(*dearmour));
-       dearmour->seen_nl = 1;
-       /*
-           dearmour->allow_headers_without_gap=without_gap;
-           dearmour->allow_no_gap=no_gap;
-           dearmour->allow_trailing_whitespace=trailing_whitespace;
-       */
-       dearmour->expect_sig = 0;
-       dearmour->got_sig = 0;
+       if ((dearmour = calloc(1, sizeof(*dearmour))) == NULL) {
+               (void) fprintf(stderr, "__ops_reader_push_dearmour: bad alloc\n");
+       } else {
+               dearmour->seen_nl = 1;
+               /*
+                   dearmour->allow_headers_without_gap=without_gap;
+                   dearmour->allow_no_gap=no_gap;
+                   dearmour->allow_trailing_whitespace=trailing_whitespace;
+               */
+               dearmour->expect_sig = 0;
+               dearmour->got_sig = 0;
 
-       __ops_reader_push(parse_info, armoured_data_reader,
+               __ops_reader_push(parse_info, armoured_data_reader,
                        armoured_data_destroyer, dearmour);
+       }
 }
 
 /**
@@ -1497,12 +1514,15 @@
 {
        encrypted_t     *encrypted;
        
-       encrypted = calloc(1, sizeof(*encrypted));
-       encrypted->decrypt = decrypt;
-       encrypted->region = region;
-       __ops_decrypt_init(encrypted->decrypt);
-       __ops_reader_push(stream, encrypted_data_reader,
+       if ((encrypted = calloc(1, sizeof(*encrypted))) == NULL) {
+               (void) fprintf(stderr, "__ops_reader_push_decrypted: bad alloc\n");
+       } else {
+               encrypted->decrypt = decrypt;
+               encrypted->region = region;
+               __ops_decrypt_init(encrypted->decrypt);
+               __ops_reader_push(stream, encrypted_data_reader,
                        encrypted_data_destroyer, encrypted);
+       }
 }
 
 /**
@@ -1572,7 +1592,10 @@
                __ops_init_subregion(&decrypted_region, NULL);
                decrypted_region.length =
                        se_ip->region->length - se_ip->region->readc;
-               buf = calloc(1, decrypted_region.length);
+               if ((buf = calloc(1, decrypted_region.length)) == NULL) {
+                       (void) fprintf(stderr, "se_ip_data_reader: bad alloc\n");
+                       return -1;
+               }
 
                /* read entire SE IP packet */
                if (!__ops_stacked_limited_read(buf, decrypted_region.length,
@@ -1590,8 +1613,7 @@
                                if (!((i + 1) % 8))
                                        fprintf(stderr, "\n");



Home | Main Index | Thread Index | Old Index