From: Michal Clapinski <mclapinski@google.com>
To: Jonathan Corbet <corbet@lwn.net>,
Mike Kravetz <mike.kravetz@oracle.com>,
Muchun Song <muchun.song@linux.dev>,
Andrew Morton <akpm@linux-foundation.org>,
Hugh Dickins <hughd@google.com>, Shuah Khan <shuah@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Arnd Bergmann <arnd@arndb.de>, Yi Liu <yi.l.liu@intel.com>,
Dominik Brodowski <linux@dominikbrodowski.net>,
Hans Verkuil <hverkuil-cisco@xs4all.nl>,
Steve French <stfrench@microsoft.com>,
Simon Ser <contact@emersion.fr>, Jason Gunthorpe <jgg@ziepe.ca>,
Marc Dionne <marc.dionne@auristor.com>,
Jiri Slaby <jirislaby@kernel.org>,
David Howells <dhowells@redhat.com>,
Luca Vizzarro <Luca.Vizzarro@arm.com>,
Jeff Xu <jeffxu@google.com>, Aleksa Sarai <cyphar@cyphar.com>,
Kees Cook <keescook@chromium.org>,
Daniel Verkamp <dverkamp@chromium.org>,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, linux-kselftest@vger.kernel.org
Cc: Michal Clapinski <mclapinski@google.com>
Subject: [PATCH v2 1/2] mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL)
Date: Fri, 8 Sep 2023 19:57:37 +0200 [thread overview]
Message-ID: <20230908175738.41895-2-mclapinski@google.com> (raw)
In-Reply-To: <20230908175738.41895-1-mclapinski@google.com>
Add a way to check if an fd points to the memfd's original open fd
(the one created by memfd_create).
Useful because only the original open fd can be both writable and
executable.
Signed-off-by: Michal Clapinski <mclapinski@google.com>
---
Documentation/userspace-api/ioctl/ioctl-number.rst | 1 +
fs/hugetlbfs/inode.c | 9 +++++++++
include/linux/memfd.h | 12 ++++++++++++
mm/memfd.c | 9 +++++++++
mm/shmem.c | 9 +++++++++
5 files changed, 40 insertions(+)
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
index 4ea5b837399a..9a0782116ac2 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -355,6 +355,7 @@ Code Seq# Include File Comments
0xB6 all linux/fpga-dfl.h
0xB7 all uapi/linux/remoteproc_cdev.h <mailto:linux-remoteproc@vger.kernel.org>
0xB7 all uapi/linux/nsfs.h <mailto:Andrei Vagin <avagin@openvz.org>>
+0xB8 00 linux/memfd.h
0xC0 00-0F linux/usb/iowarrior.h
0xCA 00-0F uapi/misc/cxl.h
0xCA 10-2F uapi/misc/ocxl.h
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 316c4cebd3f3..89ff46f7ac54 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -35,6 +35,7 @@
#include <linux/magic.h>
#include <linux/migrate.h>
#include <linux/uio.h>
+#include <linux/memfd.h>
#include <linux/uaccess.h>
#include <linux/sched/mm.h>
@@ -1324,6 +1325,12 @@ static void init_once(void *foo)
inode_init_once(&ei->vfs_inode);
}
+static long hugetlbfs_file_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ return memfd_ioctl(file, cmd, arg);
+}
+
const struct file_operations hugetlbfs_file_operations = {
.read_iter = hugetlbfs_read_iter,
.mmap = hugetlbfs_file_mmap,
@@ -1331,6 +1338,8 @@ const struct file_operations hugetlbfs_file_operations = {
.get_unmapped_area = hugetlb_get_unmapped_area,
.llseek = default_llseek,
.fallocate = hugetlbfs_fallocate,
+ .unlocked_ioctl = hugetlbfs_file_ioctl,
+ .compat_ioctl = hugetlbfs_file_ioctl,
};
static const struct inode_operations hugetlbfs_dir_inode_operations = {
diff --git a/include/linux/memfd.h b/include/linux/memfd.h
index e7abf6fa4c52..50f512624c92 100644
--- a/include/linux/memfd.h
+++ b/include/linux/memfd.h
@@ -3,14 +3,26 @@
#define __LINUX_MEMFD_H
#include <linux/file.h>
+#include <linux/ioctl.h>
#ifdef CONFIG_MEMFD_CREATE
extern long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg);
+extern long memfd_ioctl(struct file *file, unsigned int cmd, unsigned int arg);
#else
static inline long memfd_fcntl(struct file *f, unsigned int c, unsigned int a)
{
return -EINVAL;
}
+static inline long memfd_ioctl(struct file *f, unsigned int c, unsigned int a)
+{
+ return -EINVAL;
+}
#endif
+/*
+ * Return 1 if the memfd is original (i.e. was created by memfd_create,
+ * not reopened), 0 otherwise.
+ */
+#define MEMFD_CHECK_IF_ORIGINAL _IOR(0xB8, 0, int)
+
#endif /* __LINUX_MEMFD_H */
diff --git a/mm/memfd.c b/mm/memfd.c
index 1cad1904fc26..06bcb970c387 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -262,6 +262,15 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned int arg)
return error;
}
+long memfd_ioctl(struct file *file, unsigned int cmd, unsigned int arg)
+{
+ if (cmd == MEMFD_CHECK_IF_ORIGINAL)
+ return (file->f_mode & FMODE_WRITE) &&
+ !(file->f_mode & FMODE_WRITER);
+
+ return -EINVAL;
+}
+
#define MFD_NAME_PREFIX "memfd:"
#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
diff --git a/mm/shmem.c b/mm/shmem.c
index 02e62fccc80d..347fcba15fb7 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -79,6 +79,7 @@ static struct vfsmount *shm_mnt;
#include <linux/rmap.h>
#include <linux/uuid.h>
#include <linux/quotaops.h>
+#include <linux/memfd.h>
#include <linux/uaccess.h>
@@ -4459,6 +4460,12 @@ const struct address_space_operations shmem_aops = {
};
EXPORT_SYMBOL(shmem_aops);
+static long shmem_file_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ return memfd_ioctl(file, cmd, arg);
+}
+
static const struct file_operations shmem_file_operations = {
.mmap = shmem_mmap,
.open = shmem_file_open,
@@ -4471,6 +4478,8 @@ static const struct file_operations shmem_file_operations = {
.splice_read = shmem_file_splice_read,
.splice_write = iter_file_splice_write,
.fallocate = shmem_fallocate,
+ .unlocked_ioctl = shmem_file_ioctl,
+ .compat_ioctl = shmem_file_ioctl,
#endif
};
--
2.42.0.283.g2d96d420d3-goog
next prev parent reply other threads:[~2023-09-08 17:58 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-08 17:57 [PATCH v2 0/2] " Michal Clapinski
2023-09-08 17:57 ` Michal Clapinski [this message]
2023-09-08 20:02 ` [PATCH v2 1/2] " kernel test robot
2023-09-08 17:57 ` [PATCH v2 2/2] selftests: test ioctl(MEMFD_CHECK_IF_ORIGINAL) Michal Clapinski
2023-09-08 20:34 ` [PATCH v2 0/2] mm/memfd: add ioctl(MEMFD_CHECK_IF_ORIGINAL) Jonathan Corbet
2023-09-08 21:55 ` Michał Cłapiński
2023-09-08 22:07 ` Jonathan Corbet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230908175738.41895-2-mclapinski@google.com \
--to=mclapinski@google.com \
--cc=Luca.Vizzarro@arm.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=contact@emersion.fr \
--cc=corbet@lwn.net \
--cc=cyphar@cyphar.com \
--cc=dhowells@redhat.com \
--cc=dverkamp@chromium.org \
--cc=gregkh@linuxfoundation.org \
--cc=hughd@google.com \
--cc=hverkuil-cisco@xs4all.nl \
--cc=jeffxu@google.com \
--cc=jgg@ziepe.ca \
--cc=jirislaby@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux@dominikbrodowski.net \
--cc=marc.dionne@auristor.com \
--cc=mike.kravetz@oracle.com \
--cc=muchun.song@linux.dev \
--cc=shuah@kernel.org \
--cc=stfrench@microsoft.com \
--cc=yi.l.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox