]> the.earth.li Git - onak.git/commitdiff
Throw away invalid packet data when parsing packets
authorJonathan McDowell <noodles@earth.li>
Mon, 22 Aug 2016 16:35:02 +0000 (17:35 +0100)
committerJonathan McDowell <noodles@earth.li>
Mon, 22 Aug 2016 16:35:02 +0000 (17:35 +0100)
We would detect that a packet wasn't correctly formed, and handle
requests to try to allocation too much memory that failed, when parsing
keys. However the old partial packet structure was still left around. If
we hit an error when parsing an incoming packet make sure it's fully
cleaned up.

parsekey.c

index 6255ab8bd3aa6dd94643792fb881676d5cdde949..008248e005633f0563d9bdcd409c61130cfc1423 100644 (file)
@@ -356,10 +356,25 @@ onak_status_t read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
                }
        }
 
-       /* Trim the last packet if it doesn't actually exist */
-       if (packetend != NULL && (*packetend)->packet == NULL) {
-               free(*packetend);
-               *packetend = NULL;
+       if (packetend != NULL) {
+               if ((*packetend)->packet != NULL) {
+                       /* If we got an invalid final packet, discard it. */
+                       if ((*packetend)->packet->data != NULL &&
+                                       rc != ONAK_E_OK) {
+                               free((*packetend)->packet->data);
+                               (*packetend)->packet->data = NULL;
+                       }
+                       /* If we didn't get any data, clean it up. */
+                       if ((*packetend)->packet->data == NULL) {
+                               free((*packetend)->packet);
+                               (*packetend)->packet = NULL;
+                       }
+               }
+               /* Trim the last packet if it doesn't actually exist */
+               if ((*packetend)->packet == NULL) {
+                       free(*packetend);
+                       *packetend = NULL;
+               }
        }
 
        return (rc);