aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adler <git@madler.net>2026-01-10 21:34:20 -0800
committerMark Adler <git@madler.net>2026-01-10 21:40:07 -0800
commit84045903ee415efbfaf6d3d443224c2f3a1daea0 (patch)
tree7890e52dd46347088c3b1f45bbd9dfa9754733ff
parentedebc8c488a48ad556178286f6cea2672e477277 (diff)
downloadzlib-84045903ee415efbfaf6d3d443224c2f3a1daea0.tar.gz
zlib-84045903ee415efbfaf6d3d443224c2f3a1daea0.tar.bz2
zlib-84045903ee415efbfaf6d3d443224c2f3a1daea0.zip
Copy only the initialized deflate state in deflateCopy.
To avoid the propagation and possible disclosure of uninitialized memory contents.
-rw-r--r--deflate.c19
-rw-r--r--deflate.h3
2 files changed, 17 insertions, 5 deletions
diff --git a/deflate.c b/deflate.c
index 6f88630..6ec1e45 100644
--- a/deflate.c
+++ b/deflate.c
@@ -172,6 +172,7 @@ local const config configuration_table[10] = {
172 s->head[s->hash_size - 1] = NIL; \ 172 s->head[s->hash_size - 1] = NIL; \
173 zmemzero((Bytef *)s->head, \ 173 zmemzero((Bytef *)s->head, \
174 (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \ 174 (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
175 s->slid = 0; \
175 } while (0) 176 } while (0)
176 177
177/* =========================================================================== 178/* ===========================================================================
@@ -195,8 +196,8 @@ local void slide_hash(deflate_state *s) {
195 m = *--p; 196 m = *--p;
196 *p = (Pos)(m >= wsize ? m - wsize : NIL); 197 *p = (Pos)(m >= wsize ? m - wsize : NIL);
197 } while (--n); 198 } while (--n);
198 n = wsize;
199#ifndef FASTEST 199#ifndef FASTEST
200 n = wsize;
200 p = &s->prev[n]; 201 p = &s->prev[n];
201 do { 202 do {
202 m = *--p; 203 m = *--p;
@@ -206,6 +207,7 @@ local void slide_hash(deflate_state *s) {
206 */ 207 */
207 } while (--n); 208 } while (--n);
208#endif 209#endif
210 s->slid = 1;
209} 211}
210 212
211/* =========================================================================== 213/* ===========================================================================
@@ -431,6 +433,7 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
431 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ 433 if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
432 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); 434 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
433 if (s == Z_NULL) return Z_MEM_ERROR; 435 if (s == Z_NULL) return Z_MEM_ERROR;
436 zmemzero(s, sizeof(deflate_state));
434 strm->state = (struct internal_state FAR *)s; 437 strm->state = (struct internal_state FAR *)s;
435 s->strm = strm; 438 s->strm = strm;
436 s->status = INIT_STATE; /* to pass state test in deflateReset() */ 439 s->status = INIT_STATE; /* to pass state test in deflateReset() */
@@ -1320,6 +1323,7 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1320 1323
1321 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); 1324 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
1322 if (ds == Z_NULL) return Z_MEM_ERROR; 1325 if (ds == Z_NULL) return Z_MEM_ERROR;
1326 zmemzero(ds, sizeof(deflate_state));
1323 dest->state = (struct internal_state FAR *) ds; 1327 dest->state = (struct internal_state FAR *) ds;
1324 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); 1328 zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
1325 ds->strm = dest; 1329 ds->strm = dest;
@@ -1334,18 +1338,23 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
1334 deflateEnd (dest); 1338 deflateEnd (dest);
1335 return Z_MEM_ERROR; 1339 return Z_MEM_ERROR;
1336 } 1340 }
1337 /* following zmemcpy do not work for 16-bit MSDOS */ 1341 /* following zmemcpy's do not work for 16-bit MSDOS */
1338 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); 1342 zmemcpy(ds->window, ss->window, ss->high_water);
1339 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); 1343 zmemcpy((voidpf)ds->prev, (voidpf)ss->prev,
1344 (ss->slid || ss->strstart - ss->insert > ds->w_size ? ds->w_size :
1345 ss->strstart - ss->insert) * sizeof(Pos));
1340 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); 1346 zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
1341 zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS);
1342 1347
1343 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); 1348 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1349 zmemcpy(ds->pending_out, ss->pending_out, ss->pending);
1344#ifdef LIT_MEM 1350#ifdef LIT_MEM
1345 ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1)); 1351 ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1346 ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2); 1352 ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1353 zmemcpy(ds->d_buf, ss->d_buf, ss->sym_next * sizeof(ush));
1354 zmemcpy(ds->l_buf, ss->l_buf, ss->sym_next);
1347#else 1355#else
1348 ds->sym_buf = ds->pending_buf + ds->lit_bufsize; 1356 ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1357 zmemcpy(ds->sym_buf, ss->sym_buf, ss->sym_next);
1349#endif 1358#endif
1350 1359
1351 ds->l_desc.dyn_tree = ds->dyn_ltree; 1360 ds->l_desc.dyn_tree = ds->dyn_ltree;
diff --git a/deflate.h b/deflate.h
index 4884a4b..15c015e 100644
--- a/deflate.h
+++ b/deflate.h
@@ -282,6 +282,9 @@ typedef struct internal_state {
282 * updated to the new high water mark. 282 * updated to the new high water mark.
283 */ 283 */
284 284
285 int slid;
286 /* True if the hash table has been slid since it was cleared. */
287
285} FAR deflate_state; 288} FAR deflate_state;
286 289
287/* Output a byte on the stream. 290/* Output a byte on the stream.