Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion common/utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "config.h"
#include <assert.h>
#include <bitcoin/chainparams.h>
#include <ccan/err/err.h>
#include <ccan/list/list.h>
#include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h>
Expand All @@ -11,6 +12,7 @@
#include <errno.h>
#include <locale.h>
#include <sodium.h>
#include <string.h>

const tal_t *wally_tal_ctx = NULL;
secp256k1_context *secp256k1_ctx;
Expand Down Expand Up @@ -100,8 +102,21 @@ static void destroy_munlock(const tal_t *ptr)

void mlock_tal_memory(const tal_t *ptr)
{
if (sodium_mlock((void *)ptr, tal_bytelen(ptr)) != 0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

daemon_setup() already calls err_set_progname(argv0) (via common_setup) before mlock_tal_memory() can run. Did you consider using ccan warn()/warnx() instead of a hand-rolled fprintf + strerror, so the message gets the daemon-name prefix for free and matches the warnx() call in crashdump() that immediately follows it on the same failure path?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — no, I'd missed that common_setup() calls err_set_progname(argv0)
(common/setup.c:43) before any of these call sites. Switched to warnx().

One wrinkle in case it comes up: warn() appends : strerror(errno) to the end of
the whole string, which would leave the errno dangling after the Docker/Podman line,
so I've kept warnx() with strerror(errno) inline on the first line where it
belongs. Dropping fprintf also lets the added #include <stdio.h> go again;
<string.h> stays for strerror.

Verified end-to-end on FreeBSD 15.1 / clang 19, in a jail with allow.mlock=0
(which is the case that prompted the patch):

lightning_hsmd: FATAL: could not lock 64 bytes of sensitive memory into RAM: Operation not permitted
Memory locking is required to keep secrets out of swap.
If you are running in a container or jail, the privilege must be granted:
  FreeBSD jail: set allow.mlock=1 for the jail
  Linux: raise RLIMIT_MEMLOCK (ulimit -l), or grant the CAP_IPC_LOCK capability
  Docker/Podman: --ulimit memlock=-1:-1 or --cap-add=IPC_LOCK
lightning_hsmd: FATAL SIGNAL 6 (version 1f86c30-modded)

Which I think makes your point better than I could: with warnx() the diagnostic and
the crashdump() line beneath it now carry the same lightning_hsmd: prefix and read
as one block. Full tree builds clean, no new warnings on common/utils.c (also checked
with -Werror -Wformat=2 added).

if (sodium_mlock((void *)ptr, tal_bytelen(ptr)) != 0) {
warnx("FATAL: could not lock %zu bytes of sensitive memory"
" into RAM: %s\n"
"Memory locking is required to keep secrets out of"
" swap.\n"
"If you are running in a container or jail, the"
" privilege must be granted:\n"
" FreeBSD jail: set allow.mlock=1 for the jail\n"
" Linux: raise RLIMIT_MEMLOCK (ulimit -l), or grant"
" the CAP_IPC_LOCK capability\n"
" Docker/Podman: --ulimit memlock=-1:-1 or"
" --cap-add=IPC_LOCK",
tal_bytelen(ptr), strerror(errno));
abort();
}
tal_add_destructor(ptr, destroy_munlock);
}

Expand Down