[PATCH 17/28] security: Introduce file_pre_free_security hook

From: Roberto Sassu
Date: Fri Mar 03 2023 - 13:22:53 EST


From: Roberto Sassu <roberto.sassu@xxxxxxxxxx>

In preparation for moving IMA and EVM to the LSM infrastructure, introduce
the file_pre_free_security hook.

Signed-off-by: Roberto Sassu <roberto.sassu@xxxxxxxxxx>
---
fs/file_table.c | 1 +
include/linux/lsm_hook_defs.h | 1 +
include/linux/security.h | 4 ++++
security/security.c | 11 +++++++++++
4 files changed, 17 insertions(+)

diff --git a/fs/file_table.c b/fs/file_table.c
index 372653b9261..3150e613aec 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -312,6 +312,7 @@ static void __fput(struct file *file)
eventpoll_release(file);
locks_remove_file(file);

+ security_file_pre_free(file);
ima_file_free(file);
if (unlikely(file->f_flags & FASYNC)) {
if (file->f_op->fasync)
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index 5d4e256e250..4580912051a 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -171,6 +171,7 @@ LSM_HOOK(int, 0, kernfs_init_security, struct kernfs_node *kn_dir,
struct kernfs_node *kn)
LSM_HOOK(int, 0, file_permission, struct file *file, int mask)
LSM_HOOK(int, 0, file_alloc_security, struct file *file)
+LSM_HOOK(void, LSM_RET_VOID, file_pre_free_security, struct file *file)
LSM_HOOK(void, LSM_RET_VOID, file_free_security, struct file *file)
LSM_HOOK(int, 0, file_ioctl, struct file *file, unsigned int cmd,
unsigned long arg)
diff --git a/include/linux/security.h b/include/linux/security.h
index 4fdc62a1b42..88e88280f7d 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -388,6 +388,7 @@ int security_kernfs_init_security(struct kernfs_node *kn_dir,
struct kernfs_node *kn);
int security_file_permission(struct file *file, int mask);
int security_file_alloc(struct file *file);
+void security_file_pre_free(struct file *file);
void security_file_free(struct file *file);
int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
int security_mmap_file(struct file *file, unsigned long prot,
@@ -984,6 +985,9 @@ static inline int security_file_alloc(struct file *file)
return 0;
}

+static inline void security_file_pre_free(struct file *file)
+{ }
+
static inline void security_file_free(struct file *file)
{ }

diff --git a/security/security.c b/security/security.c
index e252c87df4f..6cbbb4289f7 100644
--- a/security/security.c
+++ b/security/security.c
@@ -2676,6 +2676,17 @@ int security_file_alloc(struct file *file)
return rc;
}

+/**
+ * security_file_pre_free() - Perform actions before freeing a file's LSM blob
+ * @file: the file
+ *
+ * Perform actions before the file descriptor is freed.
+ */
+void security_file_pre_free(struct file *file)
+{
+ call_void_hook(file_pre_free_security, file);
+}
+
/**
* security_file_free() - Free a file's LSM blob
* @file: the file
--
2.25.1