fsnotify: Check mntns_get_root_by_mnt_id() return value in get_mark_path()

Add checks for mntns_get_root_by_mnt_id() return value before using it
in openat() calls. This prevents using an invalid file descriptor if
the mount namespace root lookup fails.

Add error logging when mntns_get_root_by_mnt_id() fails to provide
diagnostic context about which mount namespace and path were being
accessed.

Also rename local variable 'path' to 'fpath' to avoid shadowing the
outer scope variable of the same name.

Signed-off-by: 3idey <elaidya225@gmail.com>
This commit is contained in:
3idey 2026-02-10 13:27:50 +02:00 committed by Alexander Mikhalitsyn
parent 8bfd6af996
commit 12792bfca7
No known key found for this signature in database
GPG key ID: B1F47F5CB05B4FA3

View file

@ -518,23 +518,33 @@ static char *get_mark_path(const char *who, struct file_remap *remap, FhEntry *f
int mntns_root;
mntns_root = mntns_get_root_by_mnt_id(remap->rmnt_id);
if (mntns_root < 0) {
pr_err("Failed to get mount namespace root for remap (mnt_id %#x path %s)\n",
remap->rmnt_id, remap->rpath);
goto err;
}
pr_debug("\t\tRestore %s watch for %#08x:%#016lx (via %s)\n", who, s_dev, i_ino, remap->rpath);
*target = openat(mntns_root, remap->rpath, O_PATH);
} else if (f_handle->path) {
int mntns_root;
char *path = ".";
char *fpath = ".";
uint32_t mnt_id = f_handle->has_mnt_id ? f_handle->mnt_id : -1;
/* irmap cache is collected in the root namespaces. */
mntns_root = mntns_get_root_by_mnt_id(mnt_id);
if (mntns_root < 0) {
pr_err("Failed to get mount namespace root for path hint (mnt_id %#x path %s)\n",
mnt_id, f_handle->path);
goto err;
}
/* change "/foo" into "foo" and "/" into "." */
if (f_handle->path[1] != '\0')
path = f_handle->path + 1;
fpath = f_handle->path + 1;
pr_debug("\t\tRestore with path hint %d:%s\n", mnt_id, path);
*target = openat(mntns_root, path, O_PATH);
pr_debug("\t\tRestore with path hint %d:%s\n", mnt_id, fpath);
*target = openat(mntns_root, fpath, O_PATH);
} else
*target = open_handle(s_dev, i_ino, f_handle);