From bd0f209c2bdaa24c86544f1e71fe4ff41599b70a Mon Sep 17 00:00:00 2001 From: Pavel Tikhomirov Date: Thu, 16 Mar 2023 10:34:54 +0800 Subject: [PATCH] pstree: improve id intersection detection in prepare_pstree_for_shell_job First, let's move lookup_create_item-s to the end so that on pgid replacement we don't have false positive pstree_pid_by_virt check founding item created by sid replacement. (note: we need those lookup_create_item-s for the sake of free pid selection mechanism) Second, let's add checks for sid/pgid in images intersecting with current_sid/pgid, as this would also bring problems on restore. Signed-off-by: Pavel Tikhomirov --- criu/pstree.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/criu/pstree.c b/criu/pstree.c index 72c4a3502..8c44e7134 100644 --- a/criu/pstree.c +++ b/criu/pstree.c @@ -381,20 +381,26 @@ static int prepare_pstree_for_shell_job(pid_t pid) } for_each_pstree_item(pi) { + if (pi->sid == current_sid) { + pr_err("Current sid %d intersects with sid of (%d) in images\n", current_sid, vpid(pi)); + return -1; + } if (pi->sid == old_sid) pi->sid = current_sid; + if (pi->pgid == current_sid) { + pr_err("Current sid %d intersects with pgid of (%d) in images\n", current_sid, + vpid(pi)); + return -1; + } if (pi->pgid == old_sid) pi->pgid = current_sid; } - - if (lookup_create_item(current_sid) == NULL) - return -1; } /* root_item is a group leader */ if (root_item->pgid == vpid(root_item)) - return 0; + goto add_fake_session_leader; old_gid = root_item->pgid; if (old_gid != current_gid) { @@ -407,14 +413,21 @@ static int prepare_pstree_for_shell_job(pid_t pid) } for_each_pstree_item(pi) { + if (current_gid != current_sid && pi->pgid == current_gid) { + pr_err("Current gid %d intersects with pgid of (%d) in images\n", current_gid, + vpid(pi)); + return -1; + } if (pi->pgid == old_gid) pi->pgid = current_gid; } - - if (lookup_create_item(current_gid) == NULL) - return -1; } + if (old_gid != current_gid && !lookup_create_item(current_gid)) + return -1; +add_fake_session_leader: + if (old_sid != current_sid && !lookup_create_item(current_sid)) + return -1; return 0; }