From patchwork Tue Apr 11 01:29:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13206945 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A303C77B77 for ; Tue, 11 Apr 2023 01:29:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A70C280041; Mon, 10 Apr 2023 21:29:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9576528003C; Mon, 10 Apr 2023 21:29:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78345280041; Mon, 10 Apr 2023 21:29:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6A72B28003C for ; Mon, 10 Apr 2023 21:29:41 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3A98CABDF8 for ; Tue, 11 Apr 2023 01:29:41 +0000 (UTC) X-FDA: 80667378162.07.8AF7EE4 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) by imf16.hostedemail.com (Postfix) with ESMTP id 75280180010 for ; Tue, 11 Apr 2023 01:29:39 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XXnjZgXz; spf=pass (imf16.hostedemail.com: domain of 3Arg0ZAsKCG0LNVPcWPjeYRRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--ackerleytng.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3Arg0ZAsKCG0LNVPcWPjeYRRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681176579; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jBCdWjTPDV7u7tywdkOEhqA30FFTo51cVFS76tLVdIk=; b=zD5HYHi/9luyGsgaLRm1xY2misZkdFBsHUYeI4Jh8HIslOmAYuRfjOCADY62RX8nNZvgkq h3ZKZDv1+H4PgyrxPL+l6a5N+8YrmbqLHl5qOExex3aRmOsTQTsfd6bOiW5yuSuRXKYu4q hXmC0em+q0dRJfv9XEUaP+PJX0rJ0Jc= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XXnjZgXz; spf=pass (imf16.hostedemail.com: domain of 3Arg0ZAsKCG0LNVPcWPjeYRRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--ackerleytng.bounces.google.com designates 209.85.210.202 as permitted sender) smtp.mailfrom=3Arg0ZAsKCG0LNVPcWPjeYRRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681176579; a=rsa-sha256; cv=none; b=H6orwosAhQ2iv9YZBND1oqwH3S5Pxv4OiWzwXE9QY8AXrroiBkNGCSyZo8XGJcKLLBpqRi hms4Epm7QKodgBOxCxIoOuivUfxcjHMy9oK6jKY45xQ2kX+eXwzlkZW2FV0tkgEG/1RiE8 ccxrFRkfxn4PxBh+nHetoZ1PiOU4CXY= Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-63243c02cceso602405b3a.2 for ; Mon, 10 Apr 2023 18:29:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1681176578; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jBCdWjTPDV7u7tywdkOEhqA30FFTo51cVFS76tLVdIk=; b=XXnjZgXzwtwzC9wPP7936MOH1Bh6PiD2hqfXsgLbzkeGH7jGOqDvWazEmiyAzxPEXN HOXn2bdQRnFFlnJUMCqgw4px9COwSYMyn3YVtfmzMWvKtYBIfUSMShHDLtLIFs/2YTUR ZIlb/4bJS9MEL7swsNJzi4YAZKTAsbO10KC0Ds2cNLC22Pg9HmCMPpjEwtsK0gp8xNc3 mZO89Ov6XW97TbXx3Qad8uCoGve6ai+6KRHCABfhvY1sO8p+sNfAEeAPFhpBXQq5q71+ Ao9iCqyyDHst/xqAqkZjIbwaUW4f94Yk0iTSASX0LtcD2fSwJLyo8HMt+YM3EG3m5HrO XxOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681176578; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jBCdWjTPDV7u7tywdkOEhqA30FFTo51cVFS76tLVdIk=; b=ys4Ijd9ASMxP8HhvBv+67IlXLjAEJwQ+swgTfL49wZYxhetyaG/bDHpNrUKTgN8OiQ KQ4Uqwe9MDjfUHIzprkeX0C8T466ModUh1cPM+DP8YijVwtLBvplQGkHfql1Y6up32cE y8aR+51K7OOkHH4tNqn4xYWE19gs2yh0Ste7Vx2qepJ+qD0teJxbrJusbrKnqBomHg+r WI5A4i3QdBw1xMynMtKgLU+1bMm3wiO5KImTINuuHodOh1KfjYXGUcfh2fg/L5plT1xm LwF8WBR8BCvi5q4i8R1Oc95Wih056kqWEKiRPj8cH1VYEe4bmNIivUgYStfEAbXdrdjg BkyQ== X-Gm-Message-State: AAQBX9cnz290pbHN1JeigiCjHH1bm52/m6QS93tmgTfB/r+4stPOc4Q+ s18lTz0YD1csl1EYP9+f9xBBqTZIOgvnkvlIug== X-Google-Smtp-Source: AKy350aUc53Va1zkms/tZeMPTaKwPOXFpbnBKZFuGedztjHPQD03PotiB50/sPjZNhzfTmekJxf45Wmqylq6+Y7Cgw== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a05:6a00:a1b:b0:62d:dade:825 with SMTP id p27-20020a056a000a1b00b0062ddade0825mr6152408pfh.3.1681176578151; Mon, 10 Apr 2023 18:29:38 -0700 (PDT) Date: Tue, 11 Apr 2023 01:29:32 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.577.gac1e443424-goog Message-ID: Subject: [RFC PATCH v4 1/2] mm: restrictedmem: Allow userspace to specify mount for memfd_restricted From: Ackerley Tng To: kvm@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, qemu-devel@nongnu.org Cc: aarcange@redhat.com, ak@linux.intel.com, akpm@linux-foundation.org, arnd@arndb.de, bfields@fieldses.org, bp@alien8.de, chao.p.peng@linux.intel.com, corbet@lwn.net, dave.hansen@intel.com, david@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, hpa@zytor.com, hughd@google.com, jlayton@kernel.org, jmattson@google.com, joro@8bytes.org, jun.nakajima@intel.com, kirill.shutemov@linux.intel.com, linmiaohe@huawei.com, luto@kernel.org, mail@maciej.szmigiero.name, mhocko@suse.com, michael.roth@amd.com, mingo@redhat.com, naoya.horiguchi@nec.com, pbonzini@redhat.com, qperret@google.com, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, tabba@google.com, tglx@linutronix.de, vannapurve@google.com, vbabka@suse.cz, vkuznets@redhat.com, wanpengli@tencent.com, wei.w.wang@intel.com, x86@kernel.org, yu.c.zhang@linux.intel.com, Ackerley Tng X-Stat-Signature: 9entxgwtb83168dsykkpwpxtyy19qu1b X-Rspam-User: X-Rspamd-Queue-Id: 75280180010 X-Rspamd-Server: rspam06 X-HE-Tag: 1681176579-626222 X-HE-Meta: U2FsdGVkX18gy+1KnEFctfRM7hUkoSoRAABe/rF2ko7FFxfXmMp5wuL0AYDSARK9kwbfXzU80siERYBTf8RBOsM88z7CedPvK1HMJT8I2D1mr+AYj7myohZT3kObDgS/6ydv+echIkeQgr8trVkgmBC1YxR3bJgS4BHV22a65ObVZFXc+rtTkDcd6MBU9bBvDMUH7ajwnLBSWLWknHkzSvDNa4eaaeW5YGtBG3J7FpKN13XWrIyrgE9p+rJjCW6/3UPmTPReqSqgBEClykMlhH04jDsZQhk56OnyimNMqIAb7Tu5jeKE8pQkQHYSITpnuSNEC4+W1XJ1Npg2MzdWxqMLLXlV5xjXfFTaHK5sLB/q9NjSYR93NBxYaCk75Crc0FXeVcU7N5sGS62YIvjhXUbFa2Llg1IMRkSbV8JHqQtd+2iUvdsTqpRizmPl8W9mVVGnlvJ/B0UNEGKXtkbCrpJDOG7GtwPBbmf4B4wmWxSR99CaF9Snb1nXuMqtzvA3saDsGs4FnJVTdr+7zcMi4jgJTk/AZryx6xECOV5xA3mL8HnCN6ubDlXXGOXmEA53ivygL0Kn/rjTgFeLyquZbQlZiyfHI/o4M738vMb4xrRhBWm400OQhFwsVK8xsGbEtOHyynk5w0vJ3MUsxPzp+0vxFnQgehQ04nyEt/5RtUe+jjDBwuqQraQkm40sHe+B54MtTvm9IoH6JIZUw7ebSZ8QdzsV6oMLGbBRk0+67PEa10+waE7XoEHQ0h3tw45yA5xuLBr/KSnW9KkVWzUVRKHhqJloKsEvM8vxKyyr8FRvRAHY5fzTKgqKraqy94MMXLiYp+faocC8AQyHqxVUeOe46gHXFY1Y91066/VibFOBzjhkh0v/ZoSIy4b0D/NvXOjDG62lirxSk5c7Sxlz71EQ4rVURbB6E8yjuIeRK//pgaK535hrvXwEI77ortVNA3ZqXaVlU4cNPUqw0dp KkRJLUtb ZyOxR6PU9EQmtxm9nG9DqXry5gJJ1zjtzzfRBTSaMmO1fv+mlL+CUyDjPiYwzBeUFBVbYYAB48ogFPFCeTUyoumKDr0ywOUdaOg2m2SEirDqXLPosm+sLj5v2mrAp1Y3p6SJcBeDlXE5iVtws1+LrLUphptIhsAZa6Ajg9pmnMjLToFbDL/I4UUkq00QlDuON3HGm2uVdQz1mrnQ86AbEh5RRcfhh7Q6L9Nw9rr1pme3AZ3Py/z0EXI6qGYnxuakdjYS4dJaRqnDFymiYeAPUBSVaZA6srhkbet+ZalzVN7jnhlxyRfMw5GHfvHzaM3x+VDadVezslyfJeVACg2wnck5IDd9uHPC688iqCx8GLhOeo67i3rIJ+9roFF4RAk40ZW/O9ieydP5SLREY+F9MFvgnQNX8x1n7sH3nxQnGeHMTh+APFiLgEsncrgH6Hx9pQSRxzJJgsaXRmHKCegGYgRJiJEpD+hvlzyP3nYkgZbOvQ5wJh+n2kNCvECgBPMhgaY4UNfxPSmJq1DesK5ZewhPuHk5eQSekzzfMEiclxDHZcM2VMVoZ71h8cNcL9iZ+mrsxLC3e9rrzeGDsUnPp3VBAVijNvVzhX1JdUEqJYvpGrVPTOo96KE5NTp1YoEVYz0eBexRRQC9mXnpHwxKTmYF4Ih4L0YldnYYn6IbWyyuf6q7s+H5sfCSv9/8Vv3nMDy6V8amDQXabnSp4cV7o3O6kjw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: By default, the backing shmem file for a restrictedmem fd is created on shmem's kernel space mount. With this patch, an optional tmpfs mount can be specified via an fd, which will be used as the mountpoint for backing the shmem file associated with a restrictedmem fd. This will help restrictedmem fds inherit the properties of the provided tmpfs mounts, for example, hugepage (THP) allocation hints, NUMA binding hints, etc. Permissions for the fd passed to memfd_restricted() is modeled after the openat() syscall, since both of these allow creation of a file upon a mount/directory. Permission to reference the mount the fd represents is checked upon fd creation by other syscalls (e.g. fsmount(), open(), or open_tree(), etc) and any process that can present memfd_restricted() with a valid fd is expected to have obtained permission to use the mount represented by the fd. This behavior is intended to parallel that of the openat() syscall. memfd_restricted() will check that the tmpfs superblock is writable, and that the mount is also writable, before attempting to create a restrictedmem file on the mount. Signed-off-by: Ackerley Tng --- include/linux/syscalls.h | 2 +- include/uapi/linux/restrictedmem.h | 8 ++++ mm/restrictedmem.c | 73 ++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 include/uapi/linux/restrictedmem.h diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 660be0bf89d5..90c73b9e14e5 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1058,7 +1058,7 @@ asmlinkage long sys_memfd_secret(unsigned int flags); asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned long len, unsigned long home_node, unsigned long flags); -asmlinkage long sys_memfd_restricted(unsigned int flags); +asmlinkage long sys_memfd_restricted(unsigned int flags, int mount_fd); /* * Architecture-specific system calls diff --git a/include/uapi/linux/restrictedmem.h b/include/uapi/linux/restrictedmem.h new file mode 100644 index 000000000000..73e31bce73dc --- /dev/null +++ b/include/uapi/linux/restrictedmem.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_LINUX_RESTRICTEDMEM_H +#define _UAPI_LINUX_RESTRICTEDMEM_H + +/* flags for memfd_restricted */ +#define MEMFD_RSTD_USERMNT 0x0001U + +#endif /* _UAPI_LINUX_RESTRICTEDMEM_H */ diff --git a/mm/restrictedmem.c b/mm/restrictedmem.c index 55e99e6c09a1..032ad1f15138 100644 --- a/mm/restrictedmem.c +++ b/mm/restrictedmem.c @@ -6,6 +6,7 @@ #include #include #include +#include #include struct restrictedmem { @@ -250,19 +251,20 @@ static struct address_space_operations restricted_aops = { #endif }; -SYSCALL_DEFINE1(memfd_restricted, unsigned int, flags) +static int restrictedmem_create(struct vfsmount *mount) { struct file *file, *restricted_file; int fd, err; - if (flags) - return -EINVAL; - fd = get_unused_fd_flags(0); if (fd < 0) return fd; - file = shmem_file_setup("memfd:restrictedmem", 0, VM_NORESERVE); + if (mount) + file = shmem_file_setup_with_mnt(mount, "memfd:restrictedmem", 0, VM_NORESERVE); + else + file = shmem_file_setup("memfd:restrictedmem", 0, VM_NORESERVE); + if (IS_ERR(file)) { err = PTR_ERR(file); goto err_fd; @@ -286,6 +288,67 @@ SYSCALL_DEFINE1(memfd_restricted, unsigned int, flags) return err; } +static struct vfsmount *restrictedmem_get_user_mount(struct file *file) +{ + int ret; + struct vfsmount *mnt; + struct path *path; + + path = &file->f_path; + if (path->dentry != path->mnt->mnt_root) + return ERR_PTR(-EINVAL); + + /* + * Disallow bind-mounts that aren't bind-mounts of the whole + * filesystem + */ + mnt = path->mnt; + if (mnt->mnt_root != mnt->mnt_sb->s_root) + return ERR_PTR(-EINVAL); + + if (mnt->mnt_sb->s_magic != TMPFS_MAGIC) + return ERR_PTR(-EINVAL); + + ret = mnt_want_write(mnt); + if (ret) + return ERR_PTR(ret); + + return mnt; +} + +SYSCALL_DEFINE2(memfd_restricted, unsigned int, flags, int, mount_fd) +{ + int ret; + struct fd f = {}; + struct vfsmount *mnt = NULL; + + if (flags & ~MEMFD_RSTD_USERMNT) + return -EINVAL; + + if (flags & MEMFD_RSTD_USERMNT) { + f = fdget_raw(mount_fd); + if (!f.file) + return -EBADF; + + mnt = restrictedmem_get_user_mount(f.file); + if (IS_ERR(mnt)) { + ret = PTR_ERR(mnt); + goto out; + } + } + + ret = restrictedmem_create(mnt); + + if (mnt) + mnt_drop_write(mnt); + +out: + if (f.file) + fdput(f); + + return ret; +} + int restrictedmem_bind(struct file *file, pgoff_t start, pgoff_t end, struct restrictedmem_notifier *notifier, bool exclusive) {