From c9dae9237803b4ae3517f9599b33c6d4b6b9c0a4 Mon Sep 17 00:00:00 2001 From: Alexander Mikhalitsyn Date: Fri, 7 May 2021 14:11:00 +0200 Subject: [PATCH] UBUNTU: SAUCE: overlayfs: fix incorrect mnt_id of files opened from map_files BugLink: https://bugs.launchpad.net/bugs/1857257 The hack was introduced in ("UBUNTU: SAUCE: overlayfs: allow with shiftfs as underlay") and it broke checkpoint/restore of docker contains: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1857257 The following script can be used to trigger the issue: #!/bin/bash cat > test.py << EOF import sys f = open("/proc/self/maps") for l in f.readlines(): if "python" not in l: continue print(l) s = l.split() start, end = s[0].split("-") fname = s[-1] print(start, end, fname) break else: sys.exit(1) test_file1 = open(fname) test_file2 = open("/proc/self/map_files/%s-%s" % (start, end)) fdinfo1 = open("/proc/self/fdinfo/%d" % test_file1.fileno()).read() fdinfo2 = open("/proc/self/fdinfo/%d" % test_file2.fileno()).read() if fdinfo1 != fdinfo2: print("FAIL") print(test_file1) print(fdinfo1) print(test_file2) print(fdinfo2) sys.exit(1) print("PASS") EOF sudo docker run -it --privileged --rm -v `pwd`:/mnt python python /mnt/test.py Thanks to Andrei Vagin for the reproducer and investigation of this problem. Cc: Andrei Vagin Cc: Adrian Reber Cc: Christian Brauner Cc: Stefan Bader Cc: Kleber Sacilotto de Souza Cc: Krzysztof Kozlowski Fixes: d24b8a5 ("UBUNTU: SAUCE: overlayfs: allow with shiftfs as underlay") Signed-off-by: Alexander Mikhalitsyn [ saf: resolve conflicts with "ovl: fix reference counting in ovl_mmap error path" ] Signed-off-by: Seth Forshee Acked-by: Stefan Bader Acked-by: Krzysztof Kozlowski Signed-off-by: Stefan Bader --- fs/overlayfs/file.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index bad38d34f8b2..9655befdb9fc 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -420,6 +420,18 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) return ret; } +/* handle vma->vm_prfile */ +static void ovl_vm_prfile_set(struct vm_area_struct *vma, + struct file *file) +{ + get_file(file); + vma->vm_prfile = file; +#ifndef CONFIG_MMU + get_file(file); + vma->vm_region->vm_prfile = file; +#endif +} + static int ovl_mmap(struct file *file, struct vm_area_struct *vma) { struct file *realfile = file->private_data; @@ -439,6 +451,24 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) revert_creds(old_cred); ovl_file_accessed(file); + /* + * In map_files_get_link() (fs/proc/base.c) + * we need to determine correct path from overlayfs. + * But real_mount(realfile->f_path.mnt) may be not + * equal to real_mount(file->f_path.mnt). In such case + * fdinfo of the same file which was opened from + * /proc//map_files/... and "usual" path + * will show different mnt_id. + * + * We solve issue like in aufs by using additional + * field on struct vm_area_struct called "vm_prfile" + * which is used only for fdinfo/"printing" needs. + * + * See also mm/prfile.c + */ + if (!ret) + ovl_vm_prfile_set(vma, file); + return ret; } -- 2.39.2