HKDF SHA1 stack buffer overflow (write)
A stack buffer overflow write occurs in the SHA1_End function when deriving a key via HKDF with SHA-1 in NSS. An attacker able to supply crafted key-derivation parameters could trigger a four-byte out-of-bounds write on the stack, potentially leading to memory corruption.
Originally disclosed on Mozilla
- Fixed in 32 days
- Paid in 38 days
- Disclosed after 32 days
Created attachment 9089594
poc_hkdf_sha1_oob_write.cpp
=================================================================
==31990==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc26494f80 at pc 0x000000afd696 bp 0x7ffc26494910 sp 0x7ffc26494908
WRITE of size 4 at 0x7ffc26494f80 thread T0
#0 0xafd695 in SHA1_End /home/jhg/nss/nss/out/Debug/../../lib/freebl/sha_fast.c:174:5
#1 0x97e8ce in HMAC_Finish /home/jhg/nss/nss/out/Debug/../../lib/freebl/alghmac.c:133:5
#2 0x68fcca in NSC_DeriveKey /home/jhg/nss/nss/out/Debug/../../lib/softoken/pkcs11c.c:7951:21
#3 0x56b033 in PK11_DeriveWithTemplate /home/jhg/nss/nss/out/Debug/../../lib/pk11wrap/pk11skey.c:1609:15
#4 0x569c3f in PK11_Derive /home/jhg/nss/nss/out/Debug/../../lib/pk11wrap/pk11skey.c:1465:12
#5 0x5192eb in main /home/jhg/nss/cryptofuzz/poc_hkdf_sha1_oob_write.cpp:38:28
#6 0x7f169494eb96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310
#7 0x41c729 in _start (/home/jhg/nss/cryptofuzz/a.out+0x41c729)
Address 0x7ffc26494f80 is located in stack of thread T0 at offset 800 in frame
#0 0x681abf in NSC_DeriveKey /home/jhg/nss/nss/out/Debug/../../lib/softoken/pkcs11c.c:6504
This frame has 34 object(s):
[32, 40) 'crv' (line 6519)
[64, 65) 'cktrue' (line 6520)
[80, 88) 'keyType' (line 6521)
[112, 120) 'classType' (line 6522)
[144, 148) 'outLen' (line 6530)
[160, 180) 'sha_out' (line 6531)
[224, 800) 'key_block' (line 6532) <== Memory access at offset 800 overflows this variable
[928, 992) 'crsrdata' (line 6711)
[1024, 1048) 'crsr' (line 6793)
[1088, 1112) 'master' (line 6794)
[1152, 1176) 'pms' (line 6795)
[1216, 1240) 'pms231' (line 6877)
[1280, 1304) 'seed' (line 6878)
[1344, 1368) 'master232' (line 6879)
[1408, 1472) 'srcrdata' (line 6978)
[1504, 1568) 'crsrdata310' (line 6979)
[1600, 1624) 'srcr' (line 7075)
[1664, 1688) 'keyblk' (line 7076)
[1728, 1752) 'master375' (line 7077)
[1792, 1816) 'des3key' (line 7211)
[1856, 1880) 'derived' (line 7659)
[1920, 1944) 'dhPublic' (line 7659)
[1984, 2008) 'dhPrime' (line 7660)
[2048, 2072) 'dhSubPrime' (line 7660)
[2112, 2136) 'dhValue' (line 7660)
[2176, 2200) 'ecScalar' (line 7707)
[2240, 2264) 'ecPoint' (line 7707)
[2304, 2328) 'tmp' (line 7708)
[2368, 2376) 'secret' (line 7710)
[2400, 2424) 'newPoint' (line 7742)
[2464, 2528) 'hashbuf' (line 7863)
[2560, 2564) 'bufLen' (line 7897)
[2576, 2577) 'bi' (line 7934)
[2592, 2596) 'len1088' (line 7942)
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /home/jhg/nss/nss/out/Debug/../../lib/freebl/sha_fast.c:174:5 in SHA1_End
Shadow bytes around the buggy address:
0x100004c8a9a0: 00 00 04 f2 f2 f2 f2 f2 00 00 00 00 00 00 00 00
0x100004c8a9b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100004c8a9c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100004c8a9d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100004c8a9e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x100004c8a9f0:[f2]f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2
0x100004c8aa00: f8 f8 f8 f8 f8 f8 f8 f8 f2 f2 f2 f2 f8 f8 f8 f2
0x100004c8aa10: f2 f2 f2 f2 f8 f8 f8 f2 f2 f2 f2 f2 f8 f8 f8 f2
0x100004c8aa20: f2 f2 f2 f2 f8 f8 f8 f2 f2 f2 f2 f2 f8 f8 f8 f2
0x100004c8aa30: f2 f2 f2 f2 f8 f8 f8 f2 f2 f2 f2 f2 f8 f8 f8 f8
0x100004c8aa40: f8 f8 f8 f8 f2 f2 f2 f2 f8 f8 f8 f8 f8 f8 f8 f8
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
==31990==ABORTING
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the HKDF key-derivation path in NSC_DeriveKey does not bound the output length against the size of the fixed key_block stack buffer before HMAC/SHA-1 writes into it.
Impact
A crafted HKDF-with-SHA-1 key derivation could cause a four-byte stack out-of-bounds write, which may lead to memory corruption or a crash in the affected process.
What the attacker needed
Ability to invoke PK11_Derive with HKDF/SHA-1; attacker-controlled derivation parameters
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A program keeps some temporary data in a small fixed-size area of memory called the stack. Here, a routine that finishes a SHA-1 hash writes four bytes past the end of a buffer called key_block, which is a memory corruption bug. If an attacker can control the inputs that reach this code, they might be able to corrupt nearby data and possibly influence program behaviour.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.