Unverified Commit 3350607d authored by Günther Noack's avatar Günther Noack Committed by Mickaël Salaün
Browse files

security: Create file_truncate hook from path_truncate hook



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: default avatarTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Acked-by: default avatarJohn Johansen <john.johansen@canonical.com>
Acked-by: default avatarPaul Moore <paul@paul-moore.com>
Signed-off-by: default avatarGünther Noack <gnoack3000@gmail.com>
Link: https://lore.kernel.org/r/20221018182216.301684-2-gnoack3000@gmail.com


Signed-off-by: default avatarMickaël Salaün <mic@digikod.net>
parent 9abf2313
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -3211,7 +3211,7 @@ static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
	if (error)
	if (error)
		return error;
		return error;


	error = security_path_truncate(path);
	error = security_file_truncate(filp);
	if (!error) {
	if (!error) {
		error = do_truncate(mnt_userns, path->dentry, 0,
		error = do_truncate(mnt_userns, path->dentry, 0,
				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
+1 −1
Original line number Original line Diff line number Diff line
@@ -188,7 +188,7 @@ long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
	if (IS_APPEND(file_inode(f.file)))
	if (IS_APPEND(file_inode(f.file)))
		goto out_putf;
		goto out_putf;
	sb_start_write(inode->i_sb);
	sb_start_write(inode->i_sb);
	error = security_path_truncate(&f.file->f_path);
	error = security_file_truncate(f.file);
	if (!error)
	if (!error)
		error = do_truncate(file_mnt_user_ns(f.file), dentry, length,
		error = do_truncate(file_mnt_user_ns(f.file), dentry, length,
				    ATTR_MTIME | ATTR_CTIME, f.file);
				    ATTR_MTIME | ATTR_CTIME, f.file);
+1 −0
Original line number Original line Diff line number Diff line
@@ -177,6 +177,7 @@ LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk,
	 struct fown_struct *fown, int sig)
	 struct fown_struct *fown, int sig)
LSM_HOOK(int, 0, file_receive, struct file *file)
LSM_HOOK(int, 0, file_receive, struct file *file)
LSM_HOOK(int, 0, file_open, 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,
LSM_HOOK(int, 0, task_alloc, struct task_struct *task,
	 unsigned long clone_flags)
	 unsigned long clone_flags)
LSM_HOOK(void, LSM_RET_VOID, task_free, struct task_struct *task)
LSM_HOOK(void, LSM_RET_VOID, task_free, struct task_struct *task)
+9 −1
Original line number Original line Diff line number Diff line
@@ -409,7 +409,9 @@
 *	@attr is the iattr structure containing the new file attributes.
 *	@attr is the iattr structure containing the new file attributes.
 *	Return 0 if permission is granted.
 *	Return 0 if permission is granted.
 * @path_truncate:
 * @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.
 *	@path contains the path structure for the file.
 *	Return 0 if permission is granted.
 *	Return 0 if permission is granted.
 * @inode_getattr:
 * @inode_getattr:
@@ -598,6 +600,12 @@
 *	to receive an open file descriptor via socket IPC.
 *	to receive an open file descriptor via socket IPC.
 *	@file contains the file structure being received.
 *	@file contains the file structure being received.
 *	Return 0 if permission is granted.
 *	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:
 * @file_open:
 *	Save open-time permission checking state for later use upon
 *	Save open-time permission checking state for later use upon
 *	file_permission, and recheck access if anything has changed
 *	file_permission, and recheck access if anything has changed
+6 −0
Original line number Original line Diff line number Diff line
@@ -396,6 +396,7 @@ int security_file_send_sigiotask(struct task_struct *tsk,
				 struct fown_struct *fown, int sig);
				 struct fown_struct *fown, int sig);
int security_file_receive(struct file *file);
int security_file_receive(struct file *file);
int security_file_open(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);
int security_task_alloc(struct task_struct *task, unsigned long clone_flags);
void security_task_free(struct task_struct *task);
void security_task_free(struct task_struct *task);
int security_cred_alloc_blank(struct cred *cred, gfp_t gfp);
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;
	return 0;
}
}


static inline int security_file_truncate(struct file *file)
{
	return 0;
}

static inline int security_task_alloc(struct task_struct *task,
static inline int security_task_alloc(struct task_struct *task,
				      unsigned long clone_flags)
				      unsigned long clone_flags)
{
{
Loading