Skip to contentSkip to conversation

security: add LSM hook at the memfd_create point

PATCH In review Li Qiang 2 participants 2 messages Jun 13, 2021

Subsystems (3)
lore
Share & tools
lore

Li Qiang

author
#0 lore raw

PATCH 1/1 security: add LSM hook at the memfd_create point

memfd_create is often used in the fileless attack. Let's create a LSM hook so that we can detect and prevent anonymous file creation.

---
 include/linux/lsm_hook_defs.h |  4 ++++
 include/linux/lsm_hooks.h     |  5 +++++
 include/linux/security.h      | 15 +++++++++++++++
 mm/memfd.c                    |  6 ++++++
 security/security.c           |  7 +++++++
 5 files changed, 37 insertions(+)
include/linux/lsm_hook_defs.h +4 -0
index 04c01794de83..955556d0d084 100644
@@ -403,3 +403,7 @@ LSM_HOOK(void, LSM_RET_VOID, perf_event_free, struct perf_event *event)
 LSM_HOOK(int, 0, perf_event_read, struct perf_event *event)
 LSM_HOOK(int, 0, perf_event_write, struct perf_event *event)
 #endif /* CONFIG_PERF_EVENTS */
+
+#ifdef CONFIG_MEMFD_CREATE
+LSM_HOOK(int, 0, memfd_create, const char *name, unsigned int flags)
+#endif /* CONFIG_MEMFD_CREATE */
include/linux/lsm_hooks.h +5 -0
index 5c4c5c0602cb..e9c31dbb2783 100644
@@ -1557,6 +1557,11 @@
  * 	Read perf_event security info if allowed.
  * @perf_event_write:
  * 	Write perf_event security info if allowed.
+ *
+ * Security hooks for anonymous file
+ *
+ * @memfd_create:
+ *	Check whether anonymous file creation is allowed
  */
 union security_list_options {
 	#define LSM_HOOK(RET, DEFAULT, NAME, ...) RET (*NAME)(__VA_ARGS__);
include/linux/security.h +15 -0
index 06f7c50ce77f..44b43a7569b5 100644
@@ -2037,4 +2037,19 @@ static inline int security_perf_event_write(struct perf_event *event)
 #endif /* CONFIG_SECURITY */
 #endif /* CONFIG_PERF_EVENTS */
 
+#ifdef CONFIG_MEMFD_CREATE
+#ifdef CONFIG_SECURITY
+
+extern int security_memfd_create(const char *name, unsigned int flags);
+
+#else
+
+static inline int security_memfd_create(const char *name, unsigned int flags)
+{
+	return 0;
+}
+
+#endif /* CONFIG_SECURITY */
+#endif /* CONFIG_MEMFD_CREATE */
+
 #endif /* ! __LINUX_SECURITY_H */
mm/memfd.c +6 -0
index 2647c898990c..dbd309e455d2 100644
@@ -18,6 +18,7 @@
 #include <linux/hugetlb.h>
 #include <linux/shmem_fs.h>
 #include <linux/memfd.h>
+#include <linux/security.h>
 #include <uapi/linux/memfd.h>
 
 /*
@@ -290,6 +291,11 @@ SYSCALL_DEFINE2(memfd_create,
 		goto err_name;
 	}
 
+	if (security_memfd_create(name, flags)) {
+		error = -EPERM;
+		goto err_name;
+	}
+
 	fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
 	if (fd < 0) {
 		error = fd;
security/security.c +7 -0
index b38155b2de83..5723408c5d0b 100644
@@ -2624,3 +2624,10 @@ int security_perf_event_write(struct perf_event *event)
 	return call_int_hook(perf_event_write, 0, event);
 }
 #endif /* CONFIG_PERF_EVENTS */
+
+#ifdef CONFIG_MEMFD_CREATE
+int security_memfd_create(const char *name, unsigned int flags)
+{
+	return call_int_hook(memfd_create, 0, name, flags);
+}
+#endif /* CONFIG_MEMFD_CREATE */
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · Li Qiang

memfd_create is often used in the fileless attack. Let's create a LSM hook so that we can detect and prevent anonymous file creation.

We don't add LSM hooks on speculation. Resubmit when you have an LSM that needs the hook.

Quoted 99 lines
---
 include/linux/lsm_hook_defs.h |  4 ++++
 include/linux/lsm_hooks.h     |  5 +++++
 include/linux/security.h      | 15 +++++++++++++++
 mm/memfd.c                    |  6 ++++++
 security/security.c           |  7 +++++++
 5 files changed, 37 insertions(+)
include/linux/lsm_hook_defs.h +4 -0
index 04c01794de83..955556d0d084 100644
@@ -403,3 +403,7 @@ LSM_HOOK(void, LSM_RET_VOID, perf_event_free, struct perf_event *event)
 LSM_HOOK(int, 0, perf_event_read, struct perf_event *event)
 LSM_HOOK(int, 0, perf_event_write, struct perf_event *event)
 #endif /* CONFIG_PERF_EVENTS */
+
+#ifdef CONFIG_MEMFD_CREATE
+LSM_HOOK(int, 0, memfd_create, const char *name, unsigned int flags)
+#endif /* CONFIG_MEMFD_CREATE */
include/linux/lsm_hooks.h +5 -0
index 5c4c5c0602cb..e9c31dbb2783 100644
@@ -1557,6 +1557,11 @@
  * 	Read perf_event security info if allowed.
  * @perf_event_write:
  * 	Write perf_event security info if allowed.
+ *
+ * Security hooks for anonymous file
+ *
+ * @memfd_create:
+ *	Check whether anonymous file creation is allowed
  */
 union security_list_options {
 	#define LSM_HOOK(RET, DEFAULT, NAME, ...) RET (*NAME)(__VA_ARGS__);
include/linux/security.h +15 -0
index 06f7c50ce77f..44b43a7569b5 100644
@@ -2037,4 +2037,19 @@ static inline int security_perf_event_write(struct perf_event *event)
 #endif /* CONFIG_SECURITY */
 #endif /* CONFIG_PERF_EVENTS */
 
+#ifdef CONFIG_MEMFD_CREATE
+#ifdef CONFIG_SECURITY
+
+extern int security_memfd_create(const char *name, unsigned int flags);
+
+#else
+
+static inline int security_memfd_create(const char *name, unsigned int flags)
+{
+	return 0;
+}
+
+#endif /* CONFIG_SECURITY */
+#endif /* CONFIG_MEMFD_CREATE */
+
 #endif /* ! __LINUX_SECURITY_H */
mm/memfd.c +6 -0
index 2647c898990c..dbd309e455d2 100644
@@ -18,6 +18,7 @@
 #include <linux/hugetlb.h>
 #include <linux/shmem_fs.h>
 #include <linux/memfd.h>
+#include <linux/security.h>
 #include <uapi/linux/memfd.h>
 
 /*
@@ -290,6 +291,11 @@ SYSCALL_DEFINE2(memfd_create,
 		goto err_name;
 	}
 
+	if (security_memfd_create(name, flags)) {
+		error = -EPERM;
+		goto err_name;
+	}
+
 	fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
 	if (fd < 0) {
 		error = fd;
security/security.c +7 -0
index b38155b2de83..5723408c5d0b 100644
@@ -2624,3 +2624,10 @@ int security_perf_event_write(struct perf_event *event)
 	return call_int_hook(perf_event_write, 0, event);
 }
 #endif /* CONFIG_PERF_EVENTS */
+
+#ifdef CONFIG_MEMFD_CREATE
+int security_memfd_create(const char *name, unsigned int flags)
+{
+	return call_int_hook(memfd_create, 0, name, flags);
+}
+#endif /* CONFIG_MEMFD_CREATE */