]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: use uniq_id for target mount verification
authorKarel Zak <kzak@redhat.com>
Thu, 25 Jun 2026 10:32:46 +0000 (12:32 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 29 Jun 2026 09:08:58 +0000 (11:08 +0200)
Prefer the unique mount ID (STATX_MNT_ID_UNIQUE) over the old mount ID
when verifying that a mount landed on the expected target in
mnt_context_reopen_target_fd(). Fall back to the old ID when uniq_id is
not available.

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/context.c

index d5d9151b38d8f5b95a4fb452c89212ded5a94587..9ae857eb40ff8fdad1ec2c0cb19737611ff7dbaf 100644 (file)
@@ -420,23 +420,33 @@ int mnt_context_reopen_target_fd(struct libmnt_context *cxt)
                return -errno;
 
        /* verify the mount landed on the expected target;
-        * cxt->fs->id is set from fd_tree in hook_create_mount() */
-       if (cxt->fs && cxt->fs->id > 0) {
-               int id = 0;
+        * IDs are set from fd_tree in hook_create_mount() */
+       if (cxt->fs && (cxt->fs->uniq_id || cxt->fs->id > 0)) {
+               int mismatch = 0;
 
-               if (mnt_id_from_fd(cxt->fd_target, NULL, &id) == 0
-                   && id != cxt->fs->id) {
+               if (cxt->fs->uniq_id) {
+                       uint64_t uniq_id = 0;
+
+                       if (mnt_id_from_fd(cxt->fd_target, &uniq_id, NULL) == 0
+                           && uniq_id != cxt->fs->uniq_id)
+                               mismatch = 1;
+               } else {
+                       int id = 0;
+
+                       if (mnt_id_from_fd(cxt->fd_target, NULL, &id) == 0
+                           && id != cxt->fs->id)
+                               mismatch = 1;
+               }
+               if (mismatch) {
                        const char *tgt = mnt_fs_get_target(cxt->fs);
 
-                       DBG_OBJ(CXT, cxt, ul_debug(
-                               "target mount ID mismatch (expected %d, got %d), umounting",
-                               cxt->fs->id, id));
+                       DBG_OBJ(CXT, cxt, ul_debug("target mount ID mismatch, umounting"));
                        if (tgt)
                                umount2(tgt, MNT_DETACH);
                        mnt_context_close_target_fd(cxt);
                        return -EPERM;
                }
-               DBG_OBJ(CXT, cxt, ul_debug("target mount ID verified (%d)", id));
+               DBG_OBJ(CXT, cxt, ul_debug("target mount ID verified"));
        }
 
        return 0;