From: wufan@kernel.org
To: corbet@lwn.net, jmorris@namei.org, paul@paul-moore.com,
serge@hallyn.com, akpm@linux-foundation.org
Cc: linux-doc@vger.kernel.org, linux-security-module@vger.kernel.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Fan Wu <wufan@kernel.org>
Subject: [RFC PATCH v2 1/2] memfd,lsm: add a security hook to memfd_create()
Date: Wed, 29 Jan 2025 20:39:31 +0000 [thread overview]
Message-ID: <20250129203932.22165-2-wufan@kernel.org> (raw)
In-Reply-To: <20250129203932.22165-1-wufan@kernel.org>
From: Fan Wu <wufan@kernel.org>
This patch adds a new LSM hook that notifies the security subsystem
whenever a new memfd is created by memfd_create(). The hook is invoked
before fd_install() inside memfd_create(), allowing the LSM to
differentiate memfd files from regular shmemfs or hugetlbfs files that
share the same superblock.
Upon receiving this notification, the security system can label
the memfd files thereafter the lsms can make security decision
specifically for them.
Signed-off-by: Fan Wu <wufan@kernel.org>
---
include/linux/lsm_hook_defs.h | 3 +++
include/linux/security.h | 8 ++++++++
mm/memfd.c | 2 ++
security/security.c | 11 +++++++++++
4 files changed, 24 insertions(+)
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index e2f1ce37c41e..1c0a9953c924 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -463,3 +463,6 @@ LSM_HOOK(int, 0, bdev_alloc_security, struct block_device *bdev)
LSM_HOOK(void, LSM_RET_VOID, bdev_free_security, struct block_device *bdev)
LSM_HOOK(int, 0, bdev_setintegrity, struct block_device *bdev,
enum lsm_integrity_type type, const void *value, size_t size)
+
+LSM_HOOK(void, 0, memfd_created, struct file *file)
+
diff --git a/include/linux/security.h b/include/linux/security.h
index 980b6c207cad..40ae79270eaf 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -2386,4 +2386,12 @@ static inline void security_initramfs_populated(void)
}
#endif /* CONFIG_SECURITY */
+#ifdef CONFIG_SECURITY
+extern void security_memfd_created(struct file *file);
+#else
+static inline void security_memfd_created(struct file *file)
+{
+}
+#endif /* CONFIG_SECURITY */
+
#endif /* ! __LINUX_SECURITY_H */
diff --git a/mm/memfd.c b/mm/memfd.c
index 37f7be57c2f5..597d27ccb0b6 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -19,6 +19,7 @@
#include <linux/shmem_fs.h>
#include <linux/memfd.h>
#include <linux/pid_namespace.h>
+#include <linux/security.h>
#include <uapi/linux/memfd.h>
/*
@@ -483,6 +484,7 @@ SYSCALL_DEFINE2(memfd_create,
goto err_fd;
}
+ security_memfd_created(file);
fd_install(fd, file);
kfree(name);
return fd;
diff --git a/security/security.c b/security/security.c
index 143561ebc3e8..daa9e0e0e879 100644
--- a/security/security.c
+++ b/security/security.c
@@ -6010,3 +6010,14 @@ void security_initramfs_populated(void)
{
call_void_hook(initramfs_populated);
}
+
+/**
+ * security_memfd_created() - Notify LSMs that a memfd has been created
+ *
+ * Tells the LSMs that a memfd has been created.
+ */
+void security_memfd_created(struct file *file)
+{
+ call_void_hook(memfd_created, file);
+}
+
--
2.47.1
next prev parent reply other threads:[~2025-01-29 20:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-29 20:39 [RFC PATCH v2 0/2] ipe support for anonymous memory and memfd wufan
2025-01-29 20:39 ` wufan [this message]
2025-01-29 20:39 ` [RFC PATCH v2 2/2] ipe: add 'anonymous_memory' property for policy decisions wufan
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=20250129203932.22165-2-wufan@kernel.org \
--to=wufan@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=corbet@lwn.net \
--cc=jmorris@namei.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-security-module@vger.kernel.org \
--cc=paul@paul-moore.com \
--cc=serge@hallyn.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
Powered by JetHome