Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5828480rdb; Thu, 14 Dec 2023 00:04:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IE8EeTDULZIMhm6z+0irtBs2XXryrj6Cv0yqH7+hsuHAFzh/HZQEy8+kvK0fNE+hT3m6VxA X-Received: by 2002:a05:6358:281e:b0:16f:f32c:1d96 with SMTP id k30-20020a056358281e00b0016ff32c1d96mr3342079rwb.29.1702541042294; Thu, 14 Dec 2023 00:04:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702541042; cv=none; d=google.com; s=arc-20160816; b=nmPQ7qkQ/yy7Zi4q4oR2MzDrWnoiaC8oLagDNmQoHlZTH8ezI9gYrITcUXP7F4Viy/ ceTTtYCtnJW+C8LFpe17c8/idIrYZSOMc+396VbhhgzR/VNz/csVPIqfVKFbAJg/wAoI 6h3qDSGpIhYQIuX/cWQ0h5F93Pez9L4ypJO+Eqq5TIAJk38Kscv16oFrpPflHAm2oX+R BqYxnAmlYHgOgixXRbxYdCAj6biOxTZ1hxOYpSChYuUxmjzI9R182l2e5QWWRUSF1MnW i89zUqf81wOy0fY10xviIp2smtF3+jg6pkZkl/ilnX1XMiyQ68tNpVUsYUKG4Dgis7t5 eLJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=bKrbI1xAKWbEcqudHvTK+osAC0pIMnz5+OHOQinRQHA=; fh=/4Y1fd/TtJ0SN9Ahj9OSvdxtfUVXtYBhthPT1qVN+jk=; b=ADsrWCNEXgyZZO2C12dyCrPIAl9k9rh14DLr0MA+9keCh4zpEK4uvMCjtr9RBV2EPq AKIyN+j8HoPbJIvmhUWR5u9gv05Tq1pa4X0PaMySKnIGz1ITeDaLKW5s2gGmDfGFxN9N AcxS//3nJcjCmY/o11+ldVMPwn7MXX38WIZWhYz2uIBcV+JrdSQLgcTlSRi0W+fe+GhF L0TzQpt93VmScrnPvVDTHlp+ShgEWFaDGjJJc400YPNM3zOw4pLpn965ehqEAlRtgdf9 l2vHOvAVQye6yEz5Tu6ezbWAmgqAuXZiH1+cSBp151Z1Wsu6eqduqY85/J1lz66aOdzn VtKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jHJ+y3PU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id f1-20020a654001000000b005c6ec7ede98si10334438pgp.875.2023.12.14.00.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 00:04:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jHJ+y3PU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 548BE8027F5A; Thu, 14 Dec 2023 00:03:59 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443384AbjLNIDj (ORCPT + 99 others); Thu, 14 Dec 2023 03:03:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234342AbjLNIDi (ORCPT ); Thu, 14 Dec 2023 03:03:38 -0500 Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF31EE0; Thu, 14 Dec 2023 00:03:43 -0800 (PST) Received: by mail-qv1-xf2a.google.com with SMTP id 6a1803df08f44-67f01b911c5so6406526d6.0; Thu, 14 Dec 2023 00:03:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702541023; x=1703145823; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=bKrbI1xAKWbEcqudHvTK+osAC0pIMnz5+OHOQinRQHA=; b=jHJ+y3PUlVrZapEItmupJJvgHSa4NOirQJ4450Ofh4u9fuQk9OP/njU6PrhpwlcogJ 1PyoS4Rif9o0pn/y8dIYR6qV3MJI6uXXImm4+BZo/FaCdg8jF7DQJ3Dkn2oqVOEIYFmL 1e2MURTPowV8tARl6Zisc7PDpXHzbph/QnavRHPqeCW03jUjqosgIapyR3lcYCLR2IQ/ jsS7S27Q5A/2MjUpi774uFFEXp4TtAEP23AgIqv2UTebURcDyY61l0+rBr1dygHRRcqQ 5WE1P+08Z6SPBVlh5logEU7fLwpM68ZdfAbUwoux7wYcsoiXVBmQqAQkiDLnw7LtLHeY //Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702541023; x=1703145823; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bKrbI1xAKWbEcqudHvTK+osAC0pIMnz5+OHOQinRQHA=; b=pmUaWdBM0eyke5eZjvV6bl11+30r8gAjZq+X7PuoW3Cajp5EpcfO0v7q0eXf5NdjTb 5iRrZpfB8YyJ0GfLFSK155R3SxXHbNpNsEIlkp461ePkLeGdqb3zAhDuPhT3eNpwLnzB 0uHNLJb+jldOYP/dRj7NXx8iCLglGcmwCW3ldE0vP8rrWbJBrbYAVfGt/cL4tJluebTh ubUF1aw9qTCQZAI8+YC8up2wzfifx1RM9AAvgVJJ0Q0rKH4v12Ekm00WAT1hHpO4iaK+ atgwbFxllyGtEZXPn42THFEXnnKMnNXgCpxnW0jP5NXcUvXYBf/hvns/7LMmkemn4EgL 76ZA== X-Gm-Message-State: AOJu0YxHAY43sBqQn7BzDAO8/9/zGNkjsQhKfV88GlkYw2YoikPl38S/ atk/p0D0xvgsIDBp3K4WWteHK9o9IIEA1y5DGHU= X-Received: by 2002:a05:6214:246d:b0:67f:e1d:af38 with SMTP id im13-20020a056214246d00b0067f0e1daf38mr183836qvb.50.1702541022813; Thu, 14 Dec 2023 00:03:42 -0800 (PST) MIME-Version: 1.0 References: <20231214064439.1023011-1-avagin@google.com> <20231214064439.1023011-2-avagin@google.com> In-Reply-To: <20231214064439.1023011-2-avagin@google.com> From: Amir Goldstein Date: Thu, 14 Dec 2023 10:03:31 +0200 Message-ID: Subject: Re: [PATCH 2/2] selftests/overlayfs: verify device and inode numbers in /proc/pid/maps To: Andrei Vagin Cc: Andrew Morton , linux-kernel@vger.kernel.org, Alexander Mikhalitsyn , Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, overlayfs , Miklos Szeredi Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 14 Dec 2023 00:03:59 -0800 (PST) On Thu, Dec 14, 2023 at 8:44=E2=80=AFAM Andrei Vagin wr= ote: > > When mapping a file on overlayfs, the file stored in ->vm_file is a > backing file whose f_inode is on the underlying filesystem. We need to > verify that /proc/pid/maps contains numbers of the overlayfs file, but > not its backing file. > > Cc: Amir Goldstein > Cc: Alexander Mikhalitsyn > Signed-off-by: Andrei Vagin Reviewed-by: Amir Goldstein > --- > tools/testing/selftests/Makefile | 1 + > .../filesystems/overlayfs/.gitignore | 2 + > .../selftests/filesystems/overlayfs/Makefile | 7 + > .../filesystems/overlayfs/dev_in_maps.c | 182 ++++++++++++++++++ > .../selftests/filesystems/overlayfs/log.h | 26 +++ > 5 files changed, 218 insertions(+) > create mode 100644 tools/testing/selftests/filesystems/overlayfs/.gitign= ore > create mode 100644 tools/testing/selftests/filesystems/overlayfs/Makefil= e > create mode 100644 tools/testing/selftests/filesystems/overlayfs/dev_in_= maps.c > create mode 100644 tools/testing/selftests/filesystems/overlayfs/log.h > > diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/M= akefile > index 3b2061d1c1a5..0939a40abb28 100644 > --- a/tools/testing/selftests/Makefile > +++ b/tools/testing/selftests/Makefile > @@ -26,6 +26,7 @@ TARGETS +=3D filesystems > TARGETS +=3D filesystems/binderfs > TARGETS +=3D filesystems/epoll > TARGETS +=3D filesystems/fat > +TARGETS +=3D filesystems/overlayfs > TARGETS +=3D firmware > TARGETS +=3D fpu > TARGETS +=3D ftrace > diff --git a/tools/testing/selftests/filesystems/overlayfs/.gitignore b/t= ools/testing/selftests/filesystems/overlayfs/.gitignore > new file mode 100644 > index 000000000000..52ae618fdd98 > --- /dev/null > +++ b/tools/testing/selftests/filesystems/overlayfs/.gitignore > @@ -0,0 +1,2 @@ > +# SPDX-License-Identifier: GPL-2.0-only > +dev_in_maps > diff --git a/tools/testing/selftests/filesystems/overlayfs/Makefile b/too= ls/testing/selftests/filesystems/overlayfs/Makefile > new file mode 100644 > index 000000000000..56b2b48a765b > --- /dev/null > +++ b/tools/testing/selftests/filesystems/overlayfs/Makefile > @@ -0,0 +1,7 @@ > +# SPDX-License-Identifier: GPL-2.0 > + > +TEST_GEN_PROGS :=3D dev_in_maps > + > +CFLAGS :=3D -Wall -Werror > + > +include ../../lib.mk > diff --git a/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c = b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c > new file mode 100644 > index 000000000000..e19ab0e85709 > --- /dev/null > +++ b/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c > @@ -0,0 +1,182 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#define _GNU_SOURCE > + > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "../../kselftest.h" > +#include "log.h" > + > +static int sys_fsopen(const char *fsname, unsigned int flags) > +{ > + return syscall(__NR_fsopen, fsname, flags); > +} > + > +static int sys_fsconfig(int fd, unsigned int cmd, const char *key, const= char *value, int aux) > +{ > + return syscall(__NR_fsconfig, fd, cmd, key, value, aux); > +} > + > +static int sys_fsmount(int fd, unsigned int flags, unsigned int attr_fla= gs) > +{ > + return syscall(__NR_fsmount, fd, flags, attr_flags); > +} > + > +static int sys_move_mount(int from_dfd, const char *from_pathname, > + int to_dfd, const char *to_pathname, > + unsigned int flags) > +{ > + return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd, = to_pathname, flags); > +} > + > +static long get_file_dev_and_inode(void *addr, struct statx *stx) > +{ > + char buf[4096]; > + FILE *mapf; > + > + mapf =3D fopen("/proc/self/maps", "r"); > + if (mapf =3D=3D NULL) > + return pr_perror("fopen(/proc/self/maps)"); > + > + while (fgets(buf, sizeof(buf), mapf)) { > + unsigned long start, end; > + uint32_t maj, min; > + __u64 ino; > + > + if (sscanf(buf, "%lx-%lx %*s %*s %x:%x %llu", > + &start, &end, &maj, &min, &ino) !=3D 5) > + return pr_perror("unable to parse: %s", buf); > + if (start =3D=3D (unsigned long)addr) { > + stx->stx_dev_major =3D maj; > + stx->stx_dev_minor =3D min; > + stx->stx_ino =3D ino; > + return 0; > + } > + } > + > + return pr_err("unable to find the mapping"); > +} > + > +static int ovl_mount(void) > +{ > + int tmpfs, fsfd, ovl; > + > + fsfd =3D sys_fsopen("tmpfs", 0); > + if (fsfd =3D=3D -1) > + return pr_perror("fsopen(tmpfs)"); > + > + if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) =3D=3D= -1) > + return pr_perror("FSCONFIG_CMD_CREATE"); > + > + tmpfs =3D sys_fsmount(fsfd, 0, 0); > + if (tmpfs =3D=3D -1) > + return pr_perror("fsmount"); > + > + close(fsfd); > + > + /* overlayfs can't be constructed on top of a detached mount. */ > + if (sys_move_mount(tmpfs, "", AT_FDCWD, "/tmp", MOVE_MOUNT_F_EMPT= Y_PATH)) > + return pr_perror("move_mount"); > + close(tmpfs); > + > + if (mkdir("/tmp/w", 0755) =3D=3D -1 || > + mkdir("/tmp/u", 0755) =3D=3D -1 || > + mkdir("/tmp/l", 0755) =3D=3D -1) > + return pr_perror("mkdir"); > + > + fsfd =3D sys_fsopen("overlay", 0); > + if (fsfd =3D=3D -1) > + return pr_perror("fsopen(overlay)"); > + if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) = =3D=3D -1 || > + sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", "/tmp/l",= 0) =3D=3D -1 || > + sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", "/tmp/u",= 0) =3D=3D -1 || > + sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", "/tmp/w", = 0) =3D=3D -1) > + return pr_perror("fsconfig"); > + if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) =3D=3D= -1) > + return pr_perror("fsconfig"); > + ovl =3D sys_fsmount(fsfd, 0, 0); > + if (ovl =3D=3D -1) > + return pr_perror("fsmount"); > + > + return ovl; > +} > + > +/* > + * Check that the file device and inode shown in /proc/pid/maps match va= lues > + * returned by stat(2). > + */ > +static int test(void) > +{ > + struct statx stx, mstx; > + int ovl, fd; > + void *addr; > + > + ovl =3D ovl_mount(); > + if (ovl =3D=3D -1) > + return -1; > + > + fd =3D openat(ovl, "test", O_RDWR | O_CREAT, 0644); > + if (fd =3D=3D -1) > + return pr_perror("openat"); > + > + addr =3D mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_FILE | MAP_= SHARED, fd, 0); > + if (addr =3D=3D MAP_FAILED) > + return pr_perror("mmap"); > + > + if (get_file_dev_and_inode(addr, &mstx)) > + return -1; > + if (statx(fd, "", AT_EMPTY_PATH | AT_STATX_SYNC_AS_STAT, STATX_IN= O, &stx)) > + return pr_perror("statx"); > + > + if (stx.stx_dev_major !=3D mstx.stx_dev_major || > + stx.stx_dev_minor !=3D mstx.stx_dev_minor || > + stx.stx_ino !=3D mstx.stx_ino) > + return pr_fail("unmatched dev:ino %x:%x:%llx (expected %x= :%x:%llx)\n", > + mstx.stx_dev_major, mstx.stx_dev_minor, mstx.stx_= ino, > + stx.stx_dev_major, stx.stx_dev_minor, stx.stx_ino= ); > + > + ksft_test_result_pass("devices are matched\n"); > + return 0; > +} > + > +int main(int argc, char **argv) > +{ > + int fsfd; > + > + fsfd =3D sys_fsopen("overlay", 0); > + if (fsfd =3D=3D -1) { > + ksft_test_result_skip("unable to create overlay mount\n")= ; > + return 1; > + } > + close(fsfd); > + > + /* Create a new mount namespace to not care about cleaning test m= ounts. */ > + if (unshare(CLONE_NEWNS) =3D=3D -1) { > + ksft_test_result_skip("unable to create a new mount names= pace\n"); > + return 1; > + } > + > + if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) =3D=3D -1) { > + pr_perror("mount"); > + return 1; > + } > + > + ksft_set_plan(1); > + > + if (test()) > + return 1; > + > + ksft_exit_pass(); > + return 0; > +} > diff --git a/tools/testing/selftests/filesystems/overlayfs/log.h b/tools/= testing/selftests/filesystems/overlayfs/log.h > new file mode 100644 > index 000000000000..db64df2a8483 > --- /dev/null > +++ b/tools/testing/selftests/filesystems/overlayfs/log.h > @@ -0,0 +1,26 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +#ifndef __SELFTEST_TIMENS_LOG_H__ > +#define __SELFTEST_TIMENS_LOG_H__ > + > +#define pr_msg(fmt, lvl, ...) \ > + ksft_print_msg("[%s] (%s:%d)\t" fmt "\n", \ > + lvl, __FILE__, __LINE__, ##__VA_ARGS__) > + > +#define pr_p(func, fmt, ...) func(fmt ": %m", ##__VA_ARGS__) > + > +#define pr_err(fmt, ...) \ > + ({ \ > + ksft_test_result_error(fmt "\n", ##__VA_ARGS__); = \ > + -1; \ > + }) > + > +#define pr_fail(fmt, ...) \ > + ({ \ > + ksft_test_result_fail(fmt, ##__VA_ARGS__); \ > + -1; \ > + }) > + > +#define pr_perror(fmt, ...) pr_p(pr_err, fmt, ##__VA_ARGS__) > + > +#endif > -- > 2.43.0.472.g3155946c3a-goog >