security: Create file_truncate hook from path_truncate hook
authorGünther Noack <gnoack3000@gmail.com>
Tue, 18 Oct 2022 18:22:06 +0000 (20:22 +0200)
committerMickaël Salaün <mic@digikod.net>
Wed, 19 Oct 2022 07:01:40 +0000 (09:01 +0200)
Like path_truncate, the file_truncate hook also restricts file
truncation, but is called in the cases where truncation is attempted
on an already-opened file.

This is required in a subsequent commit to handle ftruncate()
operations differently to truncate() operations.

Acked-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Acked-by: John Johansen <john.johansen@canonical.com>
Acked-by: Paul Moore <paul@paul-moore.com>
Signed-off-by: Günther Noack <gnoack3000@gmail.com>
Link: https://lore.kernel.org/r/20221018182216.301684-2-gnoack3000@gmail.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
fs/namei.c
fs/open.c
include/linux/lsm_hook_defs.h
include/linux/lsm_hooks.h
include/linux/security.h
security/apparmor/lsm.c
security/security.c
security/tomoyo/tomoyo.c

index 578c211..85e9b9c 100644 (file)
@@ -3211,7 +3211,7 @@ static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
        if (error)
                return error;
 
-       error = security_path_truncate(path);
+       error = security_file_truncate(filp);
        if (!error) {
                error = do_truncate(mnt_userns, path->dentry, 0,
                                    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
index a81319b..c92f76a 100644 (file)
--- a/fs/open.c
+++ b/fs/open.c
@@ -188,7 +188,7 @@ long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
        if (IS_APPEND(file_inode(f.file)))
                goto out_putf;
        sb_start_write(inode->i_sb);
-       error = security_path_truncate(&f.file->f_path);
+       error = security_file_truncate(f.file);
        if (!error)
                error = do_truncate(file_mnt_user_ns(f.file), dentry, length,
                                    ATTR_MTIME | ATTR_CTIME, f.file);
index ec119da..f670258 100644 (file)
@@ -177,6 +177,7 @@ LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk,
         struct fown_struct *fown, int sig)
 LSM_HOOK(int, 0, file_receive, struct file *file)
 LSM_HOOK(int, 0, file_open, struct file *file)
+LSM_HOOK(int, 0, file_truncate, struct file *file)
 LSM_HOOK(int, 0, task_alloc, struct task_struct *task,
         unsigned long clone_flags)
 LSM_HOOK(void, LSM_RET_VOID, task_free, struct task_struct *task)
index 4ec80b9..fad93a6 100644 (file)
  *     @attr is the iattr structure containing the new file attributes.
  *     Return 0 if permission is granted.
  * @path_truncate:
- *     Check permission before truncating a file.
+ *     Check permission before truncating the file indicated by path.
+ *     Note that truncation permissions may also be checked based on
+ *     already opened files, using the @file_truncate hook.
  *     @path contains the path structure for the file.
  *     Return 0 if permission is granted.
  * @inode_getattr:
  *     to receive an open file descriptor via socket IPC.
  *     @file contains the file structure being received.
  *     Return 0 if permission is granted.
+ * @file_truncate:
+ *     Check permission before truncating a file, i.e. using ftruncate.
+ *     Note that truncation permission may also be checked based on the path,
+ *     using the @path_truncate hook.
+ *     @file contains the file structure for the file.
+ *     Return 0 if permission is granted.
  * @file_open:
  *     Save open-time permission checking state for later use upon
  *     file_permission, and recheck access if anything has changed
index ca1b710..1b5de26 100644 (file)
@@ -396,6 +396,7 @@ int security_file_send_sigiotask(struct task_struct *tsk,
                                 struct fown_struct *fown, int sig);
 int security_file_receive(struct file *file);
 int security_file_open(struct file *file);
+int security_file_truncate(struct file *file);
 int security_task_alloc(struct task_struct *task, unsigned long clone_flags);
 void security_task_free(struct task_struct *task);
 int security_cred_alloc_blank(struct cred *cred, gfp_t gfp);
@@ -1014,6 +1015,11 @@ static inline int security_file_open(struct file *file)
        return 0;
 }
 
+static inline int security_file_truncate(struct file *file)
+{
+       return 0;
+}
+
 static inline int security_task_alloc(struct task_struct *task,
                                      unsigned long clone_flags)
 {
index f560702..be31549 100644 (file)
@@ -329,6 +329,11 @@ static int apparmor_path_truncate(const struct path *path)
        return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
 }
 
+static int apparmor_file_truncate(struct file *file)
+{
+       return apparmor_path_truncate(&file->f_path);
+}
+
 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
                                 const char *old_name)
 {
@@ -1232,6 +1237,7 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
        LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
        LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
        LSM_HOOK_INIT(file_lock, apparmor_file_lock),
+       LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
 
        LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
        LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
index 79d82cb..b555969 100644 (file)
@@ -1652,6 +1652,11 @@ int security_file_open(struct file *file)
        return fsnotify_perm(file, MAY_OPEN);
 }
 
+int security_file_truncate(struct file *file)
+{
+       return call_int_hook(file_truncate, 0, file);
+}
+
 int security_task_alloc(struct task_struct *task, unsigned long clone_flags)
 {
        int rc = lsm_task_alloc(task);
index 71e82d8..af04a7b 100644 (file)
@@ -135,6 +135,18 @@ static int tomoyo_path_truncate(const struct path *path)
 }
 
 /**
+ * tomoyo_file_truncate - Target for security_file_truncate().
+ *
+ * @file: Pointer to "struct file".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int tomoyo_file_truncate(struct file *file)
+{
+       return tomoyo_path_truncate(&file->f_path);
+}
+
+/**
  * tomoyo_path_unlink - Target for security_path_unlink().
  *
  * @parent: Pointer to "struct path".
@@ -545,6 +557,7 @@ static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = {
        LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security),
        LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl),
        LSM_HOOK_INIT(file_open, tomoyo_file_open),
+       LSM_HOOK_INIT(file_truncate, tomoyo_file_truncate),
        LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate),
        LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink),
        LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir),