wallet: bound-check shachain_known pos on load
What changed, and why it matters
This update fixes a bug in Core Lightning's wallet code where it reads a 'position' value from its database and uses it directly as an array index without first checking whether it is valid. If the database contained an out-of-range value, the program could read or write memory outside the intended array, which can cause crashes or unpredictable behavior. The fix adds a simple bounds check and makes the program stop with a clear error if the value is invalid.
Treat as a low-to-moderate reliability/security fix. Backport to maintained branches if the affected code is present. Review other channel/wallet loaders for similar missing bounds checks on database-derived array indices.
Security signals we found
Out-of-bounds array index from database value
Defensive bounds check added in wallet loading path
db_fatal() used to fail loudly on malformed row
Changelog labels this as a fixed wallet issue
Evidence from the diff
In wallet_shachain_load(), a ‘pos’ integer is read from the shachain_known table and used to index chain->chain.known[pos] before validating that pos is within [0, ARRAY_SIZE(chain->chain.known)). The patch adds a bounds check and calls db_fatal() on violation, turning a potential out-of-bounds read/write into a loud failure. This is consistent with other defensive checks in the channel loader.
Changed components
wallet/wallet.cwallet_shachain_load()shachain_known database rowsInspect captured patch +3 / −0
### wallet/wallet.c
@@ -1255,6 +1255,9 @@ static bool wallet_shachain_load(struct wallet *wallet, u64 id,
while (db_step(stmt)) {
int pos = db_col_int(stmt, "pos");
+ if (pos < 0 || pos >= ARRAY_SIZE(chain->chain.known))
+ db_fatal(wallet->db,
+ "shachain_known pos %i out of range", pos);
chain->chain.known[pos].index = db_col_u64(stmt, "idx");
db_col_sha256(stmt, "hash", &chain->chain.known[pos].hash);
}Why this scored 57/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.