Skip to content

Commit 780ab83

Browse files
torvaldsgregkh
authored andcommitted
cachestat: fix page cache statistics permission checking
commit 5f53766 upstream. When the 'cachestat()' system call was added in commit cf264e1 ("cachestat: implement cachestat syscall"), it was meant to be a much more convenient (and performant) version of mincore() that didn't need mapping things into the user virtual address space in order to work. But it ended up missing the "check for writability or ownership" fix for mincore(), done in commit 134fca9 ("mm/mincore.c: make mincore() more conservative"). This just adds equivalent logic to 'cachestat()', modified for the file context (rather than vma). Reported-by: Sudheendra Raghav Neela <sneela@tugraz.at> Fixes: cf264e1 ("cachestat: implement cachestat syscall") Tested-by: Johannes Weiner <hannes@cmpxchg.org> Acked-by: Johannes Weiner <hannes@cmpxchg.org> Acked-by: Nhat Pham <nphamcs@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 276df5d commit 780ab83

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

mm/filemap.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4383,6 +4383,20 @@ static void filemap_cachestat(struct address_space *mapping,
43834383
rcu_read_unlock();
43844384
}
43854385

4386+
/*
4387+
* See mincore: reveal pagecache information only for files
4388+
* that the calling process has write access to, or could (if
4389+
* tried) open for writing.
4390+
*/
4391+
static inline bool can_do_cachestat(struct file *f)
4392+
{
4393+
if (f->f_mode & FMODE_WRITE)
4394+
return true;
4395+
if (inode_owner_or_capable(file_mnt_idmap(f), file_inode(f)))
4396+
return true;
4397+
return file_permission(f, MAY_WRITE) == 0;
4398+
}
4399+
43864400
/*
43874401
* The cachestat(2) system call.
43884402
*
@@ -4442,6 +4456,11 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
44424456
return -EOPNOTSUPP;
44434457
}
44444458

4459+
if (!can_do_cachestat(fd_file(f))) {
4460+
fdput(f);
4461+
return -EPERM;
4462+
}
4463+
44454464
if (flags != 0) {
44464465
fdput(f);
44474466
return -EINVAL;

0 commit comments

Comments
 (0)