OLD | NEW |
(Empty) | |
| 1 /* |
| 2 american fuzzy lop - postprocessor for PNG |
| 3 ------------------------------------------ |
| 4 |
| 5 Written and maintained by Michal Zalewski <lcamtuf@google.com> |
| 6 |
| 7 Copyright 2015 Google Inc. All rights reserved. |
| 8 |
| 9 Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 you may not use this file except in compliance with the License. |
| 11 You may obtain a copy of the License at: |
| 12 |
| 13 http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
| 15 See post_library.so.c for a general discussion of how to implement |
| 16 postprocessors. This specific postprocessor attempts to fix up PNG |
| 17 checksums, providing a slightly more complicated example than found |
| 18 in post_library.so.c. |
| 19 |
| 20 Compile with: |
| 21 |
| 22 gcc -shared -Wall -O3 post_library_png.so.c -o post_library_png.so -lz |
| 23 |
| 24 */ |
| 25 |
| 26 #include <stdio.h> |
| 27 #include <stdlib.h> |
| 28 #include <stdint.h> |
| 29 #include <string.h> |
| 30 #include <zlib.h> |
| 31 |
| 32 #include <arpa/inet.h> |
| 33 |
| 34 /* A macro to round an integer up to 4 kB. */ |
| 35 |
| 36 #define UP4K(_i) ((((_i) >> 12) + 1) << 12) |
| 37 |
| 38 const unsigned char* afl_postprocess(const unsigned char* in_buf, |
| 39 unsigned int* len) { |
| 40 |
| 41 static unsigned char* saved_buf; |
| 42 static unsigned int saved_len; |
| 43 |
| 44 unsigned char* new_buf = (unsigned char*)in_buf; |
| 45 unsigned int pos = 8; |
| 46 |
| 47 /* Don't do anything if there's not enough room for the PNG header |
| 48 (8 bytes). */ |
| 49 |
| 50 if (*len < 8) return in_buf; |
| 51 |
| 52 /* Minimum size of a zero-length PNG chunk is 12 bytes; if we |
| 53 don't have that, we can bail out. */ |
| 54 |
| 55 while (pos + 12 <= *len) { |
| 56 |
| 57 unsigned int chunk_len, real_cksum, file_cksum; |
| 58 |
| 59 /* Chunk length is the first big-endian dword in the chunk. */ |
| 60 |
| 61 chunk_len = ntohl(*(uint32_t*)(in_buf + pos)); |
| 62 |
| 63 /* Bail out if chunk size is too big or goes past EOF. */ |
| 64 |
| 65 if (chunk_len > 1024 * 1024 || pos + 12 + chunk_len > *len) break; |
| 66 |
| 67 /* Chunk checksum is calculated for chunk ID (dword) and the actual |
| 68 payload. */ |
| 69 |
| 70 real_cksum = htonl(crc32(0, in_buf + pos + 4, chunk_len + 4)); |
| 71 |
| 72 /* The in-file checksum is the last dword past the chunk data. */ |
| 73 |
| 74 file_cksum = *(uint32_t*)(in_buf + pos + 8 + chunk_len); |
| 75 |
| 76 /* If the checksums do not match, we need to fix the file. */ |
| 77 |
| 78 if (real_cksum != file_cksum) { |
| 79 |
| 80 /* First modification? Make a copy of the input buffer. Round size |
| 81 up to 4 kB to minimize the number of reallocs needed. */ |
| 82 |
| 83 if (new_buf == in_buf) { |
| 84 |
| 85 if (*len <= saved_len) { |
| 86 |
| 87 new_buf = saved_buf; |
| 88 |
| 89 } else { |
| 90 |
| 91 new_buf = realloc(saved_buf, UP4K(*len)); |
| 92 if (!new_buf) return in_buf; |
| 93 saved_buf = new_buf; |
| 94 saved_len = UP4K(*len); |
| 95 memcpy(new_buf, in_buf, *len); |
| 96 |
| 97 } |
| 98 |
| 99 } |
| 100 |
| 101 *(uint32_t*)(new_buf + pos + 8 + chunk_len) = real_cksum; |
| 102 |
| 103 } |
| 104 |
| 105 /* Skip the entire chunk and move to the next one. */ |
| 106 |
| 107 pos += 12 + chunk_len; |
| 108 |
| 109 } |
| 110 |
| 111 return new_buf; |
| 112 |
| 113 } |
OLD | NEW |