Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3338573rdh; Thu, 28 Sep 2023 08:51:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFGE4AL/s8okmWVclkdKjydw+XObzAxCrcu1XeyxK1umvK5LS6K+oAD1MQ+Owna4dvsblG4 X-Received: by 2002:a17:90b:1297:b0:279:104d:1473 with SMTP id fw23-20020a17090b129700b00279104d1473mr1454652pjb.45.1695916312175; Thu, 28 Sep 2023 08:51:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695916312; cv=none; d=google.com; s=arc-20160816; b=T3p1BlFj+Hroxa56kTGhBCeYK2MLRre4gv0AGUxCcHUCby4nFtwei12LoO43pYQc0X i6cRTRYx/t3qt9jnILUB/91EovDQ8SQlSSZhooqVzo3xQrokqf3yIJbRQReLJ5teJ8pG T+X02lS/6N6DZMhVZ3IUNUpbOJISceWBCoZYX+M4FugGNCbPsJKvalXKs/gpDzDcTped 6gPxT3l3AGUpPJ2Pv1Il9GMIw3NqqB/xb0f5+0Tv0le1kK8r6kc/1vihhWVOofh9fjta KKZwoxkfx9+fGO5dY0Eh/IrxAfl5sujt04rw5roK0GOkYirLC3bHK1yUmA9DKruf7EXq cj9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=SAsGsjUp2akFOXe3NzL6P0KjpNsT5cDYxfpTwl+LFa4=; fh=frgPBLWOZvp32JiMJOLt6aKy7B3S1iDiMKfksagzb84=; b=pahJYohaYtUwPd7F/2IGmqU30NOAPg5FvQ3nb7s5Jd5rJUvdJw0Whpfw1kf2Z04D8K eygkHxbiMp08U/Ob3v87Ex86GOBfiCgAmMnvFs8zMIYML15zANG64D+WdxLznFGUtTtj 9hfdeJkNOREk3quqxmg+n7uEZ72nmCGA2XNxZaquKJfTM2JzVIHfhBBxEdbUBhEhwTrP mgYJfU6o7KDSLf/YWf+GzzBde+/GyhiJ1eBl6K8Lqt0BMLDPC7cMwjzP1Qk5JRIeNtHl X89L/NmjEeEWnnAg6ZPOqVLycn0Xk4ndSVmtup+J1D4eM0C9u+ApuVgu2X8soo6HrZTX /H2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Fj97zoNG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id gl20-20020a17090b121400b00268a8e0cab3si14197598pjb.178.2023.09.28.08.51.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 08:51:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Fj97zoNG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 71D1B81A6C0B; Thu, 28 Sep 2023 06:03:49 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232633AbjI1NDb (ORCPT + 99 others); Thu, 28 Sep 2023 09:03:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232606AbjI1ND3 (ORCPT ); Thu, 28 Sep 2023 09:03:29 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42D9819C for ; Thu, 28 Sep 2023 06:01:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695906114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=SAsGsjUp2akFOXe3NzL6P0KjpNsT5cDYxfpTwl+LFa4=; b=Fj97zoNGrs8qV8v3FWvCKaJcwK688QB+mmS43YD8YbN+FMv4tZw/WN/2vI1ccetdPd6Q0J 6oJCvN2Xp9rtxSbbhxy4nWWz8gnRPkqR0zHo4YnhS9LlVQwcTMNwy2eogY2XHR2nvT5kxO yk7DoZv3+0+Z6bQbQuKJEdQYJt3VZHA= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-13-LnHq6kcROAWKTWABIKmclA-1; Thu, 28 Sep 2023 09:01:53 -0400 X-MC-Unique: LnHq6kcROAWKTWABIKmclA-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-9a5d86705e4so1131837766b.1 for ; Thu, 28 Sep 2023 06:01:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695906111; x=1696510911; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SAsGsjUp2akFOXe3NzL6P0KjpNsT5cDYxfpTwl+LFa4=; b=Egn5yUYHifkCNdd1kJrW/aRs/6NVbmHI5FEGoruXbY1hToJf0MsyEA9m4r+qFXkHSt vG+P7GhbIMpbHuOgwQMZdYWV+bV4rLAkzAIy3T0MgJ8T8GleZ7ed8TJl6F5UN+jcobT0 DA3XrRmd3OaxgElD6aX7pADaOohrhsJo83L2YjsGI9MAeMgxSfNcZOGkULeCW/ob4TDa 75j3x/U0gKf30O1NT/eAI1J8KtKTSc7cRHhFTJ4FDe1UXua/mLegVxuhYcuuZ1vhl+Ev AHwKqrpqYMRFV/ms6Wb7f8z+X4olmRXVSQbEqRfnX2TvbtmoBaIjWkr5zYzi8OCE35ks MxDA== X-Gm-Message-State: AOJu0YzNyhxAlE+BZGij1dCypGtiZ+7dW/KJSPbtB9s99+rZKk8AZchb 8RGhyU3jnlk7cx7BE33P5Q3vz7BYiOYfKTo0N7Ym5QMlTmMvmtP9YHkSJGMELyTurqmxRUXTXH8 7KQTKaYjV4PRsGFgxhxGVmTrjSZUBtzX7 X-Received: by 2002:a17:906:4d2:b0:9a2:1e03:1572 with SMTP id g18-20020a17090604d200b009a21e031572mr1101934eja.19.1695906110735; Thu, 28 Sep 2023 06:01:50 -0700 (PDT) X-Received: by 2002:a17:906:4d2:b0:9a2:1e03:1572 with SMTP id g18-20020a17090604d200b009a21e031572mr1101898eja.19.1695906110228; Thu, 28 Sep 2023 06:01:50 -0700 (PDT) Received: from maszat.piliscsaba.szeredi.hu (94-21-53-31.pool.digikabel.hu. [94.21.53.31]) by smtp.gmail.com with ESMTPSA id v6-20020a170906380600b0099c53c4407dsm10784863ejc.78.2023.09.28.06.01.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 06:01:49 -0700 (PDT) From: Miklos Szeredi To: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-man@vger.kernel.org, linux-security-module@vger.kernel.org, Karel Zak , Ian Kent , David Howells , Linus Torvalds , Al Viro , Christian Brauner , Amir Goldstein , Matthew House , Florian Weimer , Arnd Bergmann Subject: [PATCH v3 0/4] querying mount attributes Date: Thu, 28 Sep 2023 15:01:42 +0200 Message-ID: <20230928130147.564503-1-mszeredi@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Thu, 28 Sep 2023 06:03:50 -0700 (PDT) Implement mount querying syscalls agreed on at LSF/MM 2023. Features: - statx-like want/got mask - allows returning ascii strings (fs type, root, mount point) - returned buffer is relocatable (no pointers) Still missing: - man pages - kselftest - syscalls on non-x86 archs Please find the test utility at the end of this mail. Usage: statmnt [-l] (mnt_id|path) Git tree: git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git#statmount-v3 Changes v1..v3: - rename statmnt(2) -> statmount(2) - rename listmnt(2) -> listmount(2) - make ABI 32bit compatible by passing 64bit args in a struct (tested on i386 and x32) - only accept new 64bit mount IDs - fix compile on !CONFIG_PROC_FS - call security_sb_statfs() in both syscalls - make lookup_mnt_in_ns() static - add LISTMOUNT_UNREACHABLE flag to listmnt() to explicitly ask for listing unreachable mounts - remove .sb_opts - remove subtype from .fs_type - return the number of bytes used (including strings) in .size - rename .mountpoint -> .mnt_point - point strings by an offset against char[] VLA at the end of the struct. E.g. printf("fs_type: %s\n", st->str + st->fs_type); - don't save string lengths - extend spare space in struct statmnt (complete size is now 512 bytes) --- Miklos Szeredi (4): add unique mount ID namespace: extract show_path() helper add statmount(2) syscall add listmount(2) syscall arch/x86/entry/syscalls/syscall_32.tbl | 2 + arch/x86/entry/syscalls/syscall_64.tbl | 2 + fs/internal.h | 2 + fs/mount.h | 3 +- fs/namespace.c | 365 +++++++++++++++++++++++++ fs/proc_namespace.c | 10 +- fs/stat.c | 9 +- fs/statfs.c | 1 + include/linux/syscalls.h | 8 + include/uapi/asm-generic/unistd.h | 8 +- include/uapi/linux/mount.h | 59 ++++ include/uapi/linux/stat.h | 1 + 12 files changed, 459 insertions(+), 11 deletions(-) -- 2.41.0 === statmnt.c === #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include /* * Structure for getting mount/superblock/filesystem info with statmount(2). * * The interface is similar to statx(2): individual fields or groups can be * selected with the @mask argument of statmount(). Kernel will set the @mask * field according to the supported fields. * * If string fields are selected, then the caller needs to pass a buffer that * has space after the fixed part of the structure. Nul terminated strings are * copied there and offsets relative to @str are stored in the relevant fields. * If the buffer is too small, then EOVERFLOW is returned. The actually used * size is returned in @size. */ struct statmnt { __u32 size; /* Total size, including strings */ __u32 __spare1; __u64 mask; /* What results were written */ __u32 sb_dev_major; /* Device ID */ __u32 sb_dev_minor; __u64 sb_magic; /* ..._SUPER_MAGIC */ __u32 sb_flags; /* MS_{RDONLY,SYNCHRONOUS,DIRSYNC,LAZYTIME} */ __u32 fs_type; /* [str] Filesystem type */ __u64 mnt_id; /* Unique ID of mount */ __u64 mnt_parent_id; /* Unique ID of parent (for root == mnt_id) */ __u32 mnt_id_old; /* Reused IDs used in proc/.../mountinfo */ __u32 mnt_parent_id_old; __u64 mnt_attr; /* MOUNT_ATTR_... */ __u64 mnt_propagation; /* MS_{SHARED,SLAVE,PRIVATE,UNBINDABLE} */ __u64 mnt_peer_group; /* ID of shared peer group */ __u64 mnt_master; /* Mount receives propagation from this ID */ __u64 propagate_from; /* Propagation from in current namespace */ __u32 mnt_root; /* [str] Root of mount relative to root of fs */ __u32 mnt_point; /* [str] Mountpoint relative to current root */ __u64 __spare2[50]; char str[]; /* Variable size part containing strings */ }; /* * To be used on the kernel ABI only for passing 64bit arguments to statmount(2) */ struct __mount_arg { __u64 mnt_id; __u64 request_mask; }; /* * @mask bits for statmount(2) */ #define STMT_SB_BASIC 0x00000001U /* Want/got sb_... */ #define STMT_MNT_BASIC 0x00000002U /* Want/got mnt_... */ #define STMT_PROPAGATE_FROM 0x00000004U /* Want/got propagate_from */ #define STMT_MNT_ROOT 0x00000008U /* Want/got mnt_root */ #define STMT_MNT_POINT 0x00000010U /* Want/got mnt_point */ #define STMT_FS_TYPE 0x00000020U /* Want/got fs_type */ /* listmount(2) flags */ #define LISTMOUNT_UNREACHABLE 0x01 /* List unreachable mounts too */ #define __NR_statmount 454 #define __NR_listmount 455 #define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */ static void free_if_neq(void *p, const void *q) { if (p != q) free(p); } static struct statmnt *statmount(uint64_t mnt_id, uint64_t mask, unsigned int flags) { struct __mount_arg arg = { .mnt_id = mnt_id, .request_mask = mask, }; union { struct statmnt m; char s[4096]; } buf; struct statmnt *ret, *mm = &buf.m; size_t bufsize = sizeof(buf); while (syscall(__NR_statmount, &arg, mm, bufsize, flags) == -1) { free_if_neq(mm, &buf.m); if (errno != EOVERFLOW) return NULL; bufsize = MAX(1 << 15, bufsize << 1); mm = malloc(bufsize); if (!mm) return NULL; } ret = malloc(mm->size); if (ret) memcpy(ret, mm, mm->size); free_if_neq(mm, &buf.m); return ret; } static int listmount(uint64_t mnt_id, uint64_t **listp, unsigned int flags) { struct __mount_arg arg = { .mnt_id = mnt_id, }; uint64_t buf[512]; size_t bufsize = sizeof(buf); uint64_t *ret, *ll = buf; long len; while ((len = syscall(__NR_listmount, &arg, ll, bufsize / sizeof(buf[0]), flags)) == -1) { free_if_neq(ll, buf); if (errno != EOVERFLOW) return -1; bufsize = MAX(1 << 15, bufsize << 1); ll = malloc(bufsize); if (!ll) return -1; } bufsize = len * sizeof(buf[0]); ret = malloc(bufsize); if (!ret) return -1; *listp = ret; memcpy(ret, ll, bufsize); free_if_neq(ll, buf); return len; } int main(int argc, char *argv[]) { struct statmnt *st; char *end; const char *arg = argv[1]; int res; int list = 0; uint64_t mask = STMT_SB_BASIC | STMT_MNT_BASIC | STMT_PROPAGATE_FROM | STMT_MNT_ROOT | STMT_MNT_POINT | STMT_FS_TYPE; uint64_t mnt_id; if (arg && strcmp(arg, "-l") == 0) { list = 1; arg = argv[2]; } if (argc != list + 2) errx(1, "usage: %s [-l] (mnt_id|path)", argv[0]); mnt_id = strtoll(arg, &end, 0); if (!mnt_id || *end != '\0') { struct statx sx; res = statx(AT_FDCWD, arg, 0, STATX_MNT_ID_UNIQUE, &sx); if (res == -1) err(1, "%s", arg); if (!(sx.stx_mask & (STATX_MNT_ID | STATX_MNT_ID_UNIQUE))) errx(1, "Sorry, no mount ID"); mnt_id = sx.stx_mnt_id; } if (list) { uint64_t *list; int num, i; res = listmount(mnt_id, &list, LISTMOUNT_UNREACHABLE); if (res == -1) err(1, "listmnt(%llu)", mnt_id); num = res; for (i = 0; i < num; i++) { printf("0x%llx", list[i]); st = statmount(list[i], STMT_MNT_POINT, 0); if (!st) { printf("\t[%s]\n", strerror(errno)); } else { printf("\t%s\n", (st->mask & STMT_MNT_POINT) ? st->str + st->mnt_point : "???"); } free(st); } free(list); return 0; } st = statmount(mnt_id, mask, 0); if (!st) err(1, "statmnt(%llu)", mnt_id); printf("size: %u\n", st->size); printf("mask: 0x%llx\n", st->mask); if (st->mask & STMT_SB_BASIC) { printf("sb_dev_major: %u\n", st->sb_dev_major); printf("sb_dev_minor: %u\n", st->sb_dev_minor); printf("sb_magic: 0x%llx\n", st->sb_magic); printf("sb_flags: 0x%08x\n", st->sb_flags); } if (st->mask & STMT_MNT_BASIC) { printf("mnt_id: 0x%llx\n", st->mnt_id); printf("mnt_parent_id: 0x%llx\n", st->mnt_parent_id); printf("mnt_id_old: %u\n", st->mnt_id_old); printf("mnt_parent_id_old: %u\n", st->mnt_parent_id_old); printf("mnt_attr: 0x%08llx\n", st->mnt_attr); printf("mnt_propagation: %s%s%s%s\n", st->mnt_propagation & MS_SHARED ? "shared," : "", st->mnt_propagation & MS_SLAVE ? "slave," : "", st->mnt_propagation & MS_UNBINDABLE ? "unbindable," : "", st->mnt_propagation & MS_PRIVATE ? "private" : ""); printf("mnt_peer_group: %llu\n", st->mnt_peer_group); printf("mnt_master: %llu\n", st->mnt_master); } if (st->mask & STMT_PROPAGATE_FROM) printf("propagate_from: %llu\n", st->propagate_from); if (st->mask & STMT_MNT_ROOT) printf("mnt_root: %u <%s>\n", st->mnt_root, st->str + st->mnt_root); if (st->mask & STMT_MNT_POINT) printf("mnt_point: %u <%s>\n", st->mnt_point, st->str + st->mnt_point); if (st->mask & STMT_FS_TYPE) printf("fs_type: %u <%s>\n", st->fs_type, st->str + st->fs_type); free(st); return 0; }