fix: self-deadlock in subaddress lookahead on mmap resize (#316)
What changed, and why it matters
This patch fixes a self-deadlock bug in Monero LWS, a lightweight wallet server. When the server tried to expand the number of subaddresses it watches ahead of time, it could lock itself because one part of the code held a shared lock while another part needed an exclusive lock on the same resource. The fix releases the shared lock before calling the function that may need the exclusive lock, then reacquires it afterward. A deadlock would freeze server operations rather than let an attacker steal funds directly, but it could be triggered by normal wallet activity.
Apply the patch. After deployment, monitor for any remaining hangs during subaddress lookahead or mmap resize. Consider adding regression tests that exercise the resize path while updating lookahead. No immediate incident response beyond patching is indicated unless the service has already experienced unexplained freezes.
Security signals we found
Self-deadlock in synchronization primitive (reader shared vs resize exclusive)
Denial-of-service vector: wallet/server thread can hang on subaddress lookahead
Fix pattern: release shared lock before operation that may acquire exclusive lock
Mmap resize path implicated in deadlock
Evidence from the diff
In src/util/ownership_test.cpp, the subaddress lookahead update path previously held a db::storage_reader (reader) while calling disk.update_lookahead(). If update_lookahead() triggered an mmap resize, resize() attempts to take ctx.sync exclusively, but the current thread already holds it shared via the reader, causing a self-deadlock. The patch explicitly resets reader before update_lookahead() and unconditionally calls update_reader() afterward, removing the prior conditional reacquisition only when upserted > 0. This ensures the shared lock is never held while an exclusive lock on the same synchronization primitive may be required.
Changed components
src/util/ownership_test.cppSubaddress lookahead logicdb::storage_reader lifecyclemmap resize pathInspect captured patch +7 / −3
### src/util/ownership_test.cpp
@@ -349,12 +349,16 @@ namespace lws
if (height == db::block_id::txpool)
height = user.scan_height();
+ // release reader first - update_lookahead() below can trigger resize(),
+ // which takes ctx.sync exclusive while this reader holds it shared
+ reader = expect<db::storage_reader>{common_error::kInvalidArgument};
+
auto upserted = disk.update_lookahead(user.db_address(), height, match, max_subaddresses);
+ update_reader(); // reacquire - released above regardless of outcome
+
if (!upserted)
return upserted.error();
- if (0 < *upserted)
- update_reader(); // update reader after upsert added new addresses
- else if (*upserted < 0)
+ if (*upserted < 0)
return {error::max_subaddresses};
return success();
}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.