From 8aa3af65b2b2a57bd40ecc47467d58d27cc9d3a7 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 27 May 2026 12:21:02 +0200
Subject: [PATCH 10/12] libmount: use fd_target in hook_idmap for move_mount()

Use the pinned fd_target with MOVE_MOUNT_T_EMPTY_PATH for restricted
users instead of string-based move_mount().  Re-open the target fd
after mount to point to the mounted filesystem root.

Signed-off-by: Karel Zak <kzak@redhat.com>
---
 libmount/src/hook_idmap.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Index: util-linux-2.40.4/libmount/src/hook_idmap.c
===================================================================
--- util-linux-2.40.4.orig/libmount/src/hook_idmap.c
+++ util-linux-2.40.4/libmount/src/hook_idmap.c
@@ -349,10 +349,25 @@ static int hook_mount_post(
 
 	/* Attach the idmapped mount. */
 	if (is_private) {
+		unsigned int mmflags = MOVE_MOUNT_F_EMPTY_PATH;
+
 		/* Unmount the old, non-idmapped mount we just cloned and idmapped. */
 		umount2(target, MNT_DETACH);
 
-		rc = move_mount(fd_tree, "", -1, target, MOVE_MOUNT_F_EMPTY_PATH);
+		if (mnt_context_target_fd_required(cxt)) {
+			int fd_tgt = mnt_context_get_target_fd(cxt);
+
+			if (fd_tgt < 0) {
+				rc = -errno;
+				goto done;
+			}
+			mmflags |= MOVE_MOUNT_T_EMPTY_PATH;
+			rc = move_mount(fd_tree, "", fd_tgt, "", mmflags);
+		} else
+			rc = move_mount(fd_tree, "", AT_FDCWD, target, mmflags);
+
+		if (rc == 0)
+			rc = mnt_context_reopen_target_fd(cxt);
 		if (rc)
 			DBG(HOOK, ul_debugobj(hs, " failed to set move mount"));
 	}
