git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 06/12] reftable/block: fix OOB write with bogus inflated log size

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 29, 2026, 09:02 UTC
Message-ID
<20260629-pks-reftable-hardening-v2-6-b0228e7d908d@pks.im>
In-Reply-To
<20260629-pks-reftable-hardening-v2-0-b0228e7d908d@pks.im>

The "log" reftable block stores reflog information. This information is compressed using zlib. The inflated size is stored in the block header so that callers can easily learn ahead of time how large of a buffer they have to allocate to inflate the data in a single pass. So to reconstruct the full inflated block we:

  - Copy over the header as-is, as it's not deflated.
  - Append the inflated data to the buffer.

The inflated block size stored in the header also includes the length of the header itself. So to figure out the bytes that should be inflated by zlib we need to subtract the header size, which is trusted data, from the block size, which is untrusted data derived from the block header.

While we do verify that we were able to inflate all data as expected, we don't verify ahead of time that the encoded block length is larger than the header length. This can lead to an underflow, which makes zlib assume that it can write more data into the target buffer than we have allocated. The result is an out-of-bounds write:

  ==1422297==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c1ff6de5231 at pc 0x55555579a628 bp 0x7fffffff4f10 sp 0x7fffffff46d0
  WRITE of size 4 at 0x7c1ff6de5231 thread T0
      #0 0x55555579a627 in __asan_memcpy (./build/t/unit-tests+0x246627)
      #1 0x55555598b093 in reftable_block_init ./build/../reftable/block.c:277:3
      #2 0x555555813701 in test_reftable_block__corrupt_log_block_size ./build/../t/unit-tests/u-reftable-block.c:495:20
      #3 0x5555557f684e in clar_run_test ./build/../t/unit-tests/clar/clar.c:335:3
      #4 0x5555557f2e69 in clar_run_suite ./build/../t/unit-tests/clar/clar.c:431:3
      #5 0x5555557f2882 in clar_test_run ./build/../t/unit-tests/clar/clar.c:636:4
      #6 0x5555557f375f in clar_test ./build/../t/unit-tests/clar/clar.c:687:11
      #7 0x5555557fa49d in cmd_main ./build/../t/unit-tests/unit-test.c:62:8
      #8 0x55555584af4a in main ./build/../common-main.c:9:11
      #9 0x7ffff7a2b284 in __libc_start_call_main (/nix/store/57iz36553175g3178pvxjij8z5rcsd4n-glibc-2.42-61/lib/libc.so.6+0x2b284) (BuildId: 8ae0b698f2d4e727f569f64bb166e08ae30bd077)
      #10 0x7ffff7a2b337 in __libc_start_main@GLIBC_2.2.5 (/nix/store/57iz36553175g3178pvxjij8z5rcsd4n-glibc-2.42-61/lib/libc.so.6+0x2b337) (BuildId: 8ae0b698f2d4e727f569f64bb166e08ae30bd077)
      #11 0x555555694c24 in _start (./build/t/unit-tests+0x140c24)
  0x7c1ff6de5231 is located 0 bytes after 1-byte region [0x7c1ff6de5230,0x7c1ff6de5231)
  allocated by thread T0 here:
      #0 0x55555579db1b in realloc.part.0 asan_malloc_linux.cpp.o
      #1 0x5555559868d7 in reftable_realloc ./build/../reftable/basics.c:36:9
      #2 0x55555598a98f in reftable_alloc_grow ./build/../reftable/basics.h:229:10
      #3 0x55555598ae58 in reftable_block_init ./build/../reftable/block.c:269:3
      #4 0x555555813701 in test_reftable_block__corrupt_log_block_size ./build/../t/unit-tests/u-reftable-block.c:495:20
      #5 0x5555557f684e in clar_run_test ./build/../t/unit-tests/clar/clar.c:335:3
      #6 0x5555557f2e69 in clar_run_suite ./build/../t/unit-tests/clar/clar.c:431:3
      #7 0x5555557f2882 in clar_test_run ./build/../t/unit-tests/clar/clar.c:636:4
      #8 0x5555557f375f in clar_test ./build/../t/unit-tests/clar/clar.c:687:11
      #9 0x5555557fa49d in cmd_main ./build/../t/unit-tests/unit-test.c:62:8
      #10 0x55555584af4a in main ./build/../common-main.c:9:11
      #11 0x7ffff7a2b284 in __libc_start_call_main (/nix/store/57iz36553175g3178pvxjij8z5rcsd4n-glibc-2.42-61/lib/libc.so.6+0x2b284) (BuildId: 8ae0b698f2d4e727f569f64bb166e08ae30bd077)
      #12 0x7ffff7a2b337 in __libc_start_main@GLIBC_2.2.5 (/nix/store/57iz36553175g3178pvxjij8z5rcsd4n-glibc-2.42-61/lib/libc.so.6+0x2b337) (BuildId: 8ae0b698f2d4e727f569f64bb166e08ae30bd077)
      #13 0x555555694c24 in _start (./build/t/unit-tests+0x140c24)
  SUMMARY: AddressSanitizer: heap-buffer-overflow (./build/t/unit-tests+0x246627) in __asan_memcpy
  Shadow bytes around the buggy address:
    0x7c1ff6de4f80: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd
    0x7c1ff6de5000: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd
    0x7c1ff6de5080: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd
    0x7c1ff6de5100: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd
    0x7c1ff6de5180: fa fa fd fd fa fa fd fd fa fa fd fa fa fa fd fd
  =>0x7c1ff6de5200: fa fa 04 fa fa fa[01]fa fa fa fa fa fa fa fa fa
    0x7c1ff6de5280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    0x7c1ff6de5300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    0x7c1ff6de5380: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    0x7c1ff6de5400: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    0x7c1ff6de5480: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  Shadow byte legend (one shadow byte represents 8 application bytes):
    Addressable:           00
    Partially addressable: 01 02 03 04 05 06 07
    Heap left redzone:       fa
    Freed heap region:       fd
    Stack left redzone:      f1
    Stack mid redzone:       f2
    Stack right redzone:     f3
    Stack after return:      f5
    Stack use after scope:   f8
    Global redzone:          f9
    Global init order:       f6
    Poisoned by user:        f7
    Container overflow:      fc
    Array cookie:            ac
    Intra object redzone:    bb
    ASan internal:           fe
    Left alloca redzone:     ca
    Right alloca redzone:    cb
Fix the bug by adding a sanity check and add a unit test.
Reported-by: oxsignal <awo@kakao.com>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/block.c                |  9 +++++++++
 t/unit-tests/u-reftable-block.c | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
diff --git a/reftable/block.c b/reftable/block.c
index 920b3f4486..b86cb9ec5a 100644
--- a/reftable/block.c
+++ b/reftable/block.c
@@ -260,6 +260,15 @@ int reftable_block_init(struct reftable_block *block,
 			goto done;
 	}
 
+	/*
+	 * Verify that the block size covers at least the table header, block
+	 * header and the 2 byte restart counter.
+	 */
+	if (block_size < header_size + 4 + 2) {
+		err = REFTABLE_FORMAT_ERROR;
+		goto done;
+	}
+
 	if (block_type == REFTABLE_BLOCK_TYPE_LOG) {
 		uint32_t block_header_skip = 4 + header_size;
 		uLong dst_len = block_size - block_header_skip;
diff --git a/t/unit-tests/u-reftable-block.c b/t/unit-tests/u-reftable-block.c
index f4e926ce3a..088162483e 100644
--- a/t/unit-tests/u-reftable-block.c
+++ b/t/unit-tests/u-reftable-block.c
@@ -465,3 +465,35 @@ void test_reftable_block__iterator(void)
 	reftable_block_release(&block);
 	reftable_buf_release(&data);
 }
+
+void test_reftable_block__corrupt_log_block_size(void)
+{
+	struct reftable_block_source source = { 0 };
+	struct reftable_record rec = {
+		.type = REFTABLE_BLOCK_TYPE_LOG,
+		.u.log = {
+			.refname = (char *) "refs/heads/main",
+			.update_index = 1,
+			.value_type = REFTABLE_LOG_UPDATE,
+		},
+	};
+	struct reftable_block block = { 0 };
+	struct reftable_buf data = REFTABLE_BUF_INIT;
+
+	cl_reftable_write_block(&data, REFTABLE_BLOCK_TYPE_LOG, &rec, 1);
+
+	/*
+	 * Log blocks store their inflated size as a big-endian 24-bit integer
+	 * right after the one-byte block type. Rewrite it to claim a size that
+	 * is smaller than the block header.
+	 */
+	reftable_put_be24((uint8_t *) data.buf + 1, 1);
+
+	block_source_from_buf(&source, &data);
+	cl_assert_equal_i(reftable_block_init(&block, &source, 0, 0, data.len,
+					      REFTABLE_HASH_SIZE_SHA1, REFTABLE_BLOCK_TYPE_LOG),
+			  REFTABLE_FORMAT_ERROR);
+
+	reftable_block_release(&block);
+	reftable_buf_release(&data);
+}
-- 
2.55.0.rc2.803.g1fd1e6609c.dirty
Previous: Junio C HamanoNext: Toon Claes
Message 24 of 48 in “reftable: harden against corrupted tables”
  1. 00/11 reftable: harden against corrupted tablesPatrick Steinhardt, Jun 24, 2026
  2. 01/11 meson: support building fuzzers with libFuzzerPatrick Steinhardt, Jun 24, 2026
  3. 02/11 oss-fuzz: add fuzzer for parsing reftablesPatrick Steinhardt, Jun 24, 2026
  4. 03/11 reftable/basics: fix OOB read on binary search of empty rangePatrick Steinhardt, Jun 24, 2026
  5. 04/11 reftable/record: don't abort when decoding invalid ref value typePatrick Steinhardt, Jun 24, 2026
  6. 05/11 reftable/block: fix OOB write with bogus inflated log sizePatrick Steinhardt, Jun 24, 2026
  7. Christian CouderJun 26, 2026
  8. Patrick SteinhardtJun 29, 2026
  9. 06/11 reftable/block: fix OOB read with bogus block sizePatrick Steinhardt, Jun 24, 2026
  10. 07/11 reftable/block: fix OOB read with bogus restart countPatrick Steinhardt, Jun 24, 2026
  11. 08/11 reftable/block: fix use of uninitialized memory when binsearch failsPatrick Steinhardt, Jun 24, 2026
  12. 09/11 reftable/block: fix OOB read with bogus restart offsetPatrick Steinhardt, Jun 24, 2026
  13. 10/11 reftable/table: fix NULL pointer access when seeking to bogus offsetsPatrick Steinhardt, Jun 24, 2026
  14. 11/11 reftable/table: fix OOB read on truncated tablePatrick Steinhardt, Jun 24, 2026
  15. 00/12 reftable: harden against corrupted tablesPatrick Steinhardt, Jun 29, 2026
  16. 01/12 meson: support building fuzzers with libFuzzerPatrick Steinhardt, Jun 29, 2026
  17. 02/12 oss-fuzz: add fuzzer for parsing reftablesPatrick Steinhardt, Jun 29, 2026
  18. 03/12 reftable/basics: fix OOB read on binary search of empty rangePatrick Steinhardt, Jun 29, 2026
  19. 04/12 reftable/record: don't abort when decoding invalid ref value typePatrick Steinhardt, Jun 29, 2026
  20. 05/12 t/unit-tests: introduce test helper to write reftable blocksPatrick Steinhardt, Jun 29, 2026
  21. Junio C HamanoJun 30, 2026
  22. Christian CouderJul 2, 2026
  23. Junio C HamanoJul 3, 2026
  24. 06/12 reftable/block: fix OOB write with bogus inflated log sizePatrick Steinhardt, Jun 29, 2026
  25. Toon ClaesJul 3, 2026
  26. Patrick SteinhardtJul 3, 2026
  27. 07/12 reftable/block: fix OOB read with bogus block sizePatrick Steinhardt, Jun 29, 2026
  28. Toon ClaesJul 3, 2026
  29. Patrick SteinhardtJul 3, 2026
  30. 08/12 reftable/block: fix OOB read with bogus restart countPatrick Steinhardt, Jun 29, 2026
  31. 09/12 reftable/block: fix use of uninitialized memory when binsearch failsPatrick Steinhardt, Jun 29, 2026
  32. 10/12 reftable/block: fix OOB read with bogus restart offsetPatrick Steinhardt, Jun 29, 2026
  33. 11/12 reftable/table: fix NULL pointer access when seeking to bogus offsetsPatrick Steinhardt, Jun 29, 2026
  34. 12/12 reftable/table: fix OOB read on truncated tablePatrick Steinhardt, Jun 29, 2026
  35. 00/12 reftable: harden against corrupted tablesPatrick Steinhardt, Jul 3, 2026
  36. 01/12 meson: support building fuzzers with libFuzzerPatrick Steinhardt, Jul 3, 2026
  37. 02/12 oss-fuzz: add fuzzer for parsing reftablesPatrick Steinhardt, Jul 3, 2026
  38. 03/12 reftable/basics: fix OOB read on binary search of empty rangePatrick Steinhardt, Jul 3, 2026
  39. 04/12 reftable/record: don't abort when decoding invalid ref value typePatrick Steinhardt, Jul 3, 2026
  40. 05/12 t/unit-tests: introduce test helper to write reftable blocksPatrick Steinhardt, Jul 3, 2026
  41. 06/12 reftable/block: fix OOB write with bogus inflated log sizePatrick Steinhardt, Jul 3, 2026
  42. 07/12 reftable/block: fix OOB read with bogus block sizePatrick Steinhardt, Jul 3, 2026
  43. 08/12 reftable/block: fix OOB read with bogus restart countPatrick Steinhardt, Jul 3, 2026
  44. 09/12 reftable/block: fix use of uninitialized memory when binsearch failsPatrick Steinhardt, Jul 3, 2026
  45. 10/12 reftable/block: fix OOB read with bogus restart offsetPatrick Steinhardt, Jul 3, 2026
  46. 11/12 reftable/table: fix NULL pointer access when seeking to bogus offsetsPatrick Steinhardt, Jul 3, 2026
  47. 12/12 reftable/table: fix OOB read on truncated tablePatrick Steinhardt, Jul 3, 2026
  48. Toon ClaesJul 8, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.