Skip to content

Commit 5f53766

Browse files
committed
cachestat: fix page cache statistics permission checking
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 <[email protected]> Fixes: cf264e1 ("cachestat: implement cachestat syscall") Tested-by: Johannes Weiner <[email protected]> Acked-by: Johannes Weiner <[email protected]> Acked-by: Nhat Pham <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c4b9570 commit 5f53766

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

mm/filemap.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4375,6 +4375,20 @@ static void filemap_cachestat(struct address_space *mapping,
43754375
rcu_read_unlock();
43764376
}
43774377

4378+
/*
4379+
* See mincore: reveal pagecache information only for files
4380+
* that the calling process has write access to, or could (if
4381+
* tried) open for writing.
4382+
*/
4383+
static inline bool can_do_cachestat(struct file *f)
4384+
{
4385+
if (f->f_mode & FMODE_WRITE)
4386+
return true;
4387+
if (inode_owner_or_capable(file_mnt_idmap(f), file_inode(f)))
4388+
return true;
4389+
return file_permission(f, MAY_WRITE) == 0;
4390+
}
4391+
43784392
/*
43794393
* The cachestat(2) system call.
43804394
*
@@ -4430,6 +4444,9 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
44304444
if (is_file_hugepages(fd_file(f)))
44314445
return -EOPNOTSUPP;
44324446

4447+
if (!can_do_cachestat(fd_file(f)))
4448+
return -EPERM;
4449+
44334450
if (flags != 0)
44344451
return -EINVAL;
44354452

0 commit comments

Comments
 (0)