Skip to content

Commit d9e5d31

Browse files
amir73ilbrauner
authored andcommitted
fsnotify: optionally pass access range in file permission hooks
In preparation for pre-content permission events with file access range, move fsnotify_file_perm() hook out of security_file_permission() and into the callers. Callers that have the access range information call the new hook fsnotify_file_area_perm() with the access range. Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Amir Goldstein <amir73il@gmail.com> Link: https://lore.kernel.org/r/20231212094440.250945-6-amir73il@gmail.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent cb383f0 commit d9e5d31

6 files changed

Lines changed: 35 additions & 12 deletions

File tree

fs/open.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
304304
if (ret)
305305
return ret;
306306

307+
ret = fsnotify_file_area_perm(file, MAY_WRITE, &offset, len);
308+
if (ret)
309+
return ret;
310+
307311
if (S_ISFIFO(inode->i_mode))
308312
return -ESPIPE;
309313

fs/read_write.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
354354

355355
int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
356356
{
357+
int mask = read_write == READ ? MAY_READ : MAY_WRITE;
358+
int ret;
359+
357360
if (unlikely((ssize_t) count < 0))
358361
return -EINVAL;
359362

@@ -371,8 +374,11 @@ int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t
371374
}
372375
}
373376

374-
return security_file_permission(file,
375-
read_write == READ ? MAY_READ : MAY_WRITE);
377+
ret = security_file_permission(file, mask);
378+
if (ret)
379+
return ret;
380+
381+
return fsnotify_file_area_perm(file, mask, ppos, count);
376382
}
377383
EXPORT_SYMBOL(rw_verify_area);
378384

fs/readdir.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ int iterate_dir(struct file *file, struct dir_context *ctx)
9696
if (res)
9797
goto out;
9898

99+
res = fsnotify_file_perm(file, MAY_READ);
100+
if (res)
101+
goto out;
102+
99103
res = down_read_killable(&inode->i_rwsem);
100104
if (res)
101105
goto out;

fs/remap_range.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,21 @@ static int generic_remap_checks(struct file *file_in, loff_t pos_in,
102102
static int remap_verify_area(struct file *file, loff_t pos, loff_t len,
103103
bool write)
104104
{
105+
int mask = write ? MAY_WRITE : MAY_READ;
105106
loff_t tmp;
107+
int ret;
106108

107109
if (unlikely(pos < 0 || len < 0))
108110
return -EINVAL;
109111

110112
if (unlikely(check_add_overflow(pos, len, &tmp)))
111113
return -EINVAL;
112114

113-
return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
115+
ret = security_file_permission(file, mask);
116+
if (ret)
117+
return ret;
118+
119+
return fsnotify_file_area_perm(file, mask, &pos, len);
114120
}
115121

116122
/*

include/linux/fsnotify.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ static inline int fsnotify_file(struct file *file, __u32 mask)
101101
}
102102

103103
/*
104-
* fsnotify_file_perm - permission hook before file access
104+
* fsnotify_file_area_perm - permission hook before access to file range
105105
*/
106-
static inline int fsnotify_file_perm(struct file *file, int perm_mask)
106+
static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
107+
const loff_t *ppos, size_t count)
107108
{
108109
__u32 fsnotify_mask = FS_ACCESS_PERM;
109110

@@ -120,6 +121,14 @@ static inline int fsnotify_file_perm(struct file *file, int perm_mask)
120121
return fsnotify_file(file, fsnotify_mask);
121122
}
122123

124+
/*
125+
* fsnotify_file_perm - permission hook before file access
126+
*/
127+
static inline int fsnotify_file_perm(struct file *file, int perm_mask)
128+
{
129+
return fsnotify_file_area_perm(file, perm_mask, NULL, 0);
130+
}
131+
123132
/*
124133
* fsnotify_open_perm - permission hook before file open
125134
*/

security/security.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,13 +2580,7 @@ int security_kernfs_init_security(struct kernfs_node *kn_dir,
25802580
*/
25812581
int security_file_permission(struct file *file, int mask)
25822582
{
2583-
int ret;
2584-
2585-
ret = call_int_hook(file_permission, 0, file, mask);
2586-
if (ret)
2587-
return ret;
2588-
2589-
return fsnotify_file_perm(file, mask);
2583+
return call_int_hook(file_permission, 0, file, mask);
25902584
}
25912585

25922586
/**

0 commit comments

Comments
 (0)