diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2018-04-17 22:09:22 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2018-04-19 19:47:11 -0700 |
commit | 5c44459c3b28a9bd3283aaceab7c615f8020c531 (patch) | |
tree | 7a5d9e7a3fd8c0438f592de2f3ee89a91d2cd06a /deflate.h | |
parent | e99813dbfe9a09e33d42e8da9e550a0c4b7ff734 (diff) | |
download | zlib-5c44459c3b28a9bd3283aaceab7c615f8020c531.tar.gz zlib-5c44459c3b28a9bd3283aaceab7c615f8020c531.tar.bz2 zlib-5c44459c3b28a9bd3283aaceab7c615f8020c531.zip |
Fix a bug that can crash deflate on some input when using Z_FIXED.
This bug was reported by Danilo Ramos of Eideticom, Inc. It has
lain in wait 13 years before being found! The bug was introduced
in zlib 1.2.2.2, with the addition of the Z_FIXED option. That
option forces the use of fixed Huffman codes. For rare inputs with
a large number of distant matches, the pending buffer into which
the compressed data is written can overwrite the distance symbol
table which it overlays. That results in corrupted output due to
invalid distances, and can result in out-of-bound accesses,
crashing the application.
The fix here combines the distance buffer and literal/length
buffers into a single symbol buffer. Now three bytes of pending
buffer space are opened up for each literal or length/distance
pair consumed, instead of the previous two bytes. This assures
that the pending buffer cannot overwrite the symbol table, since
the maximum fixed code compressed length/distance is 31 bits, and
since there are four bytes of pending space for every three bytes
of symbol space.
Diffstat (limited to 'deflate.h')
-rw-r--r-- | deflate.h | 25 |
1 files changed, 11 insertions, 14 deletions
@@ -217,7 +217,7 @@ typedef struct internal_state { | |||
217 | /* Depth of each subtree used as tie breaker for trees of equal frequency | 217 | /* Depth of each subtree used as tie breaker for trees of equal frequency |
218 | */ | 218 | */ |
219 | 219 | ||
220 | uchf *l_buf; /* buffer for literals or lengths */ | 220 | uchf *sym_buf; /* buffer for distances and literals/lengths */ |
221 | 221 | ||
222 | uInt lit_bufsize; | 222 | uInt lit_bufsize; |
223 | /* Size of match buffer for literals/lengths. There are 4 reasons for | 223 | /* Size of match buffer for literals/lengths. There are 4 reasons for |
@@ -239,13 +239,8 @@ typedef struct internal_state { | |||
239 | * - I can't count above 4 | 239 | * - I can't count above 4 |
240 | */ | 240 | */ |
241 | 241 | ||
242 | uInt last_lit; /* running index in l_buf */ | 242 | uInt sym_next; /* running index in sym_buf */ |
243 | 243 | uInt sym_end; /* symbol table full when sym_next reaches this */ | |
244 | ushf *d_buf; | ||
245 | /* Buffer for distances. To simplify the code, d_buf and l_buf have | ||
246 | * the same number of elements. To use different lengths, an extra flag | ||
247 | * array would be necessary. | ||
248 | */ | ||
249 | 244 | ||
250 | ulg opt_len; /* bit length of current block with optimal trees */ | 245 | ulg opt_len; /* bit length of current block with optimal trees */ |
251 | ulg static_len; /* bit length of current block with static trees */ | 246 | ulg static_len; /* bit length of current block with static trees */ |
@@ -325,20 +320,22 @@ void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, | |||
325 | 320 | ||
326 | # define _tr_tally_lit(s, c, flush) \ | 321 | # define _tr_tally_lit(s, c, flush) \ |
327 | { uch cc = (c); \ | 322 | { uch cc = (c); \ |
328 | s->d_buf[s->last_lit] = 0; \ | 323 | s->sym_buf[s->sym_next++] = 0; \ |
329 | s->l_buf[s->last_lit++] = cc; \ | 324 | s->sym_buf[s->sym_next++] = 0; \ |
325 | s->sym_buf[s->sym_next++] = cc; \ | ||
330 | s->dyn_ltree[cc].Freq++; \ | 326 | s->dyn_ltree[cc].Freq++; \ |
331 | flush = (s->last_lit == s->lit_bufsize-1); \ | 327 | flush = (s->sym_next == s->sym_end); \ |
332 | } | 328 | } |
333 | # define _tr_tally_dist(s, distance, length, flush) \ | 329 | # define _tr_tally_dist(s, distance, length, flush) \ |
334 | { uch len = (uch)(length); \ | 330 | { uch len = (uch)(length); \ |
335 | ush dist = (ush)(distance); \ | 331 | ush dist = (ush)(distance); \ |
336 | s->d_buf[s->last_lit] = dist; \ | 332 | s->sym_buf[s->sym_next++] = dist; \ |
337 | s->l_buf[s->last_lit++] = len; \ | 333 | s->sym_buf[s->sym_next++] = dist >> 8; \ |
334 | s->sym_buf[s->sym_next++] = len; \ | ||
338 | dist--; \ | 335 | dist--; \ |
339 | s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ | 336 | s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ |
340 | s->dyn_dtree[d_code(dist)].Freq++; \ | 337 | s->dyn_dtree[d_code(dist)].Freq++; \ |
341 | flush = (s->last_lit == s->lit_bufsize-1); \ | 338 | flush = (s->sym_next == s->sym_end); \ |
342 | } | 339 | } |
343 | #else | 340 | #else |
344 | # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) | 341 | # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) |