forked from mirrors/linux
fsnotify: generate pre-content permission event on truncate
Generate FS_PRE_ACCESS event before truncate, without sb_writers held. Move the security hooks also before sb_start_write() to conform with other security hooks (e.g. in write, fallocate). The event will have a range info of the page surrounding the new size to provide an opportunity to fill the conetnt at the end of file before truncating to non-page aligned size. Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz> Link: https://patch.msgid.link/23af8201db6ac2efdea94f09ab067d81ba5de7a7.1731684329.git.josef@toxicpanda.com
This commit is contained in:
parent
9740d17162
commit
4acf3bc76e
2 changed files with 41 additions and 10 deletions
27
fs/open.c
27
fs/open.c
|
|
@ -81,14 +81,18 @@ long vfs_truncate(const struct path *path, loff_t length)
|
||||||
if (!S_ISREG(inode->i_mode))
|
if (!S_ISREG(inode->i_mode))
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
error = mnt_want_write(path->mnt);
|
|
||||||
if (error)
|
|
||||||
goto out;
|
|
||||||
|
|
||||||
idmap = mnt_idmap(path->mnt);
|
idmap = mnt_idmap(path->mnt);
|
||||||
error = inode_permission(idmap, inode, MAY_WRITE);
|
error = inode_permission(idmap, inode, MAY_WRITE);
|
||||||
if (error)
|
if (error)
|
||||||
goto mnt_drop_write_and_out;
|
return error;
|
||||||
|
|
||||||
|
error = fsnotify_truncate_perm(path, length);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
error = mnt_want_write(path->mnt);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
error = -EPERM;
|
error = -EPERM;
|
||||||
if (IS_APPEND(inode))
|
if (IS_APPEND(inode))
|
||||||
|
|
@ -114,7 +118,7 @@ long vfs_truncate(const struct path *path, loff_t length)
|
||||||
put_write_access(inode);
|
put_write_access(inode);
|
||||||
mnt_drop_write_and_out:
|
mnt_drop_write_and_out:
|
||||||
mnt_drop_write(path->mnt);
|
mnt_drop_write(path->mnt);
|
||||||
out:
|
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(vfs_truncate);
|
EXPORT_SYMBOL_GPL(vfs_truncate);
|
||||||
|
|
@ -175,9 +179,16 @@ long do_ftruncate(struct file *file, loff_t length, int small)
|
||||||
/* Check IS_APPEND on real upper inode */
|
/* Check IS_APPEND on real upper inode */
|
||||||
if (IS_APPEND(file_inode(file)))
|
if (IS_APPEND(file_inode(file)))
|
||||||
return -EPERM;
|
return -EPERM;
|
||||||
sb_start_write(inode->i_sb);
|
|
||||||
error = security_file_truncate(file);
|
error = security_file_truncate(file);
|
||||||
if (!error)
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
error = fsnotify_truncate_perm(&file->f_path, length);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
sb_start_write(inode->i_sb);
|
||||||
error = do_truncate(file_mnt_idmap(file), dentry, length,
|
error = do_truncate(file_mnt_idmap(file), dentry, length,
|
||||||
ATTR_MTIME | ATTR_CTIME, file);
|
ATTR_MTIME | ATTR_CTIME, file);
|
||||||
sb_end_write(inode->i_sb);
|
sb_end_write(inode->i_sb);
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,21 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
|
||||||
return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
|
return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* fsnotify_truncate_perm - permission hook before file truncate
|
||||||
|
*/
|
||||||
|
static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
|
||||||
|
{
|
||||||
|
struct inode *inode = d_inode(path->dentry);
|
||||||
|
|
||||||
|
if (!(inode->i_sb->s_iflags & SB_I_ALLOW_HSM) ||
|
||||||
|
!fsnotify_sb_has_priority_watchers(inode->i_sb,
|
||||||
|
FSNOTIFY_PRIO_PRE_CONTENT))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return fsnotify_pre_content(path, &length, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* fsnotify_file_perm - permission hook before file access (unknown range)
|
* fsnotify_file_perm - permission hook before file access (unknown range)
|
||||||
*/
|
*/
|
||||||
|
|
@ -208,6 +223,11 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static inline int fsnotify_file_perm(struct file *file, int perm_mask)
|
static inline int fsnotify_file_perm(struct file *file, int perm_mask)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue