]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
fix memory alloc bug for the testcase and c89 style
authorTCY16 <tom@nlnetlabs.nl>
Fri, 17 Jun 2022 13:52:45 +0000 (15:52 +0200)
committerTCY16 <tom@nlnetlabs.nl>
Fri, 17 Jun 2022 13:52:45 +0000 (15:52 +0200)
services/cache/infra.c
testcode/testpkts.c

index 02ac7228fcaca72f4949cf2c16a90ef819a285ed..beabca6a71f605c2ca915637587ce7edb7948811 100644 (file)
@@ -389,6 +389,7 @@ data_entry_init(struct infra_cache* infra, struct lruhash_entry* e,
        time_t timenow)
 {
        int i;
+       struct infra_data* data;
        uint8_t cookie[8] = {0,0,0,0,0,0,0,0};
 
        for (i = 0; i < 8; i++) {
@@ -396,8 +397,7 @@ data_entry_init(struct infra_cache* infra, struct lruhash_entry* e,
                        255); // @TODO is 255 correct? macro-ify it?
        }
 
-
-       struct infra_data* data = (struct infra_data*)e->data;
+       data = (struct infra_data*)e->data;
        data->ttl = timenow + infra->host_ttl;
        rtt_init(&data->rtt);
        data->edns_version = 0;
index 5b233e83f1fc8b44bccae351ea231b8915a1b121..a50bf3aee0dbcaaaa8be2c8a572e586e92a7c94e 100644 (file)
@@ -1778,22 +1778,32 @@ adjust_packet(struct entry* match, uint8_t** answer_pkt, size_t *answer_len,
        if(match->server_cookie) {
                /** Find the cookie option and add the server cookie if 
                 * the client cookie is present and not already there */
+               res = realloc(res, reslen + 28);
                uint8_t* walk_query = query_pkt;
                size_t walk_query_len = query_len;
                uint8_t* walk_response = res;
-               size_t walk_response_len = reslen;
+               size_t walk_response_len = reslen+28;
 
                uint8_t* rdlen_ptr_query;
                uint8_t* rdlen_ptr_response;
 
+               reslen += 28;
+
+               if (!(walk_response)) {
+                       log_err("testbound: out of memory; send without cookie");
+                       return;
+               }
+
                /* verify that we have a EDNS record */
                if(!pkt_find_edns_opt(&walk_query, &walk_query_len)) {
                        walk_query_len = 0;
-                       log_err("testbound: no EDNS in the query packet when trying to attach a EDNS cookie");
+                       log_err("testbound: no EDNS in the query packet when "
+                               "trying to attach a EDNS cookie");
                }
                if(!pkt_find_edns_opt(&walk_response, &walk_response_len)) {
                        walk_response_len = 0;
-                       log_err("testbound: no EDNS in the response packet when trying to attach a EDNS cookie");
+                       log_err("testbound: no EDNS in the response packet when"
+                                "trying to attach a EDNS cookie");
                }
 
                /* verify that we have a EDNS option */
@@ -1832,11 +1842,15 @@ adjust_packet(struct entry* match, uint8_t** answer_pkt, size_t *answer_len,
                        log_err("testbound: invalid EDNS cookie in the query packet");
                }
 
-               if (walk_query_len > 0 && walk_response_len == 0) {
+               if (walk_query_len > 0 && walk_response_len > 0) {
+                       /* create space for the cookie, as the length of the
+                        * rest of the response is 0 */
+
                        /* depending on the incoming cookie, add the server cookie
                         * or copy the complete cookie to the response */
                        if (sldns_read_uint16(walk_query+2) == 8) {
-                               /* copy the EDNS client cookie from the query packet to the response */
+                               /* copy the EDNS client cookie from the query
+                                * packet to the response */
                                memcpy(walk_response, walk_query, 12);
 
                                /* add the server cookie to the client cookie to make it
@@ -1859,15 +1873,12 @@ adjust_packet(struct entry* match, uint8_t** answer_pkt, size_t *answer_len,
                                        memcpy(rdlen_ptr_response, rdlen_ptr_query, 12);
                                        memcpy(walk_response+12, rdlen_ptr_query+12+8, 8);
                                        memcpy(walk_response+12+8, rdlen_ptr_query+12, 8);
-
-                                       reslen = origlen + 28;
                                } else {
                                        memcpy(rdlen_ptr_response, rdlen_ptr_query, 28);
-
-                                       reslen = origlen + 28;
                                }
                        } else {
                                log_err("testbound: the incoming EDNS cookie has the wrong length");
+
                        }
                } else {
                        log_err("testbound: an error has occured while parsing the EDNS cookie");