Skip to content

Commit

Permalink
criu: Initialize util before service worker starts
Browse files Browse the repository at this point in the history
When restoring dumps in new mount + pid namespaces where multiple dumps
share the same network namespace, CRIU may fail due to conflicting
unix socket names. This happens because the service worker creates
sockets using a pattern that includes criu_run_id, but util_init()
is called after cr_service_work() starts.

The socket naming pattern "crtools-fd-%d-%d" uses the restore PID
and criu_run_id, however criu_run_id is always 0 when not initialized,
leading to conflicts when multiple restores run simultaneously either
in the same CRIU process or because of multiple CRIU processes
doing the same operation in different PID namespaces.

Fix this by:

- Moving util_init() before cr_service_work() starts
- Adding a second util_init() call in the service worker fork
to ensure unique IDs across multiple worker runs
- Making sure that dump and restore operations have util_init() called
early to generate unique socket names

With this fix, socket names always include the namespace ID, preventing
conflicts when multiple processes with the same pid share a network
namespace.

Fixes #2499

[ avagin: minore code changes ]

Signed-off-by: Lorenzo Fontana <[email protected]>
Signed-off-by: Andrei Vagin <[email protected]>
  • Loading branch information
fntlnz authored and avagin committed Oct 31, 2024
1 parent f5dec05 commit dcc3b49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 8 additions & 0 deletions criu/cr-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,14 @@ int cr_service_work(int sk)
int ret = -1;
CriuReq *msg = 0;

/*
* util_init initializes criu_run_id and compel_run_id so that sockets
* are generated with an unique name identifying the specific process
* even in cases where multiple processes with the same pid in
* different pid namespaces are sharing the same network namespace.
*/
util_init();

more:
opts.mode = CR_SWRK;

Expand Down
10 changes: 7 additions & 3 deletions criu/crtools.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ int main(int argc, char *argv[], char *envp[])
pr_err("unknown command: %s\n", argv[optind]);
goto usage;
}

/*
* util_init initializes criu_run_id and compel_run_id so that sockets
* are generated with an unique name identifying the specific process
* even in cases where multiple processes with the same pid in
* different pid namespaces are sharing the same network namespace.
*/
util_init();
if (opts.mode == CR_SWRK) {
if (argc != optind + 2) {
fprintf(stderr, "Usage: criu swrk <fd>\n");
Expand Down Expand Up @@ -254,8 +260,6 @@ int main(int argc, char *argv[], char *envp[])
return 1;
}

util_init();

if (log_init(opts.output))
return 1;

Expand Down

0 comments on commit dcc3b49

Please sign in to comment.