Skip to content

Commit

Permalink
bolt11: avoid reading uninitialized memory
Browse files Browse the repository at this point in the history
If both databits and *data_len are 0, pull_uint returns unitialized
stack memory in *val.

Detected by valgrind and UBSan.

valgrind:
==225078== Use of uninitialised value of size 8
==225078==    __sanitizer_cov_trace_cmp8
==225078==    decode_c (bolt11.c:294)
==225078==    bolt11_decode_nosig (bolt11.c:881)
==225078==    bolt11_decode (bolt11.c:945)

UBSan:
common/bolt11.c:79:29: runtime error: shift exponent 64 is too large for 64-bit type 'uint64_t'
  • Loading branch information
morehouse authored and rustyrussell committed Oct 15, 2023
1 parent 386d01d commit 440fe8c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion common/bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ static const char *pull_uint(struct hash_u5 *hu5,
err = pull_bits(hu5, data, data_len, &be_val, databits, true);
if (err)
return err;
*val = be64_to_cpu(be_val) >> (sizeof(be_val) * CHAR_BIT - databits);
if (databits == 0)
*val = 0;
else
*val = be64_to_cpu(be_val) >>
(sizeof(be_val) * CHAR_BIT - databits);
return NULL;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lnltc1zzzzzAzcQQQQQZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZXZZZZZZZZZZZZZZZZZJZZZZZZZZZZzzzZZZZZZZZ

0 comments on commit 440fe8c

Please sign in to comment.