Skip to content

Commit 7d6405c

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 <[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]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 854d0d3 commit 7d6405c

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
@@ -4270,6 +4270,20 @@ static void filemap_cachestat(struct address_space *mapping,
42704270
rcu_read_unlock();
42714271
}
42724272

4273+
/*
4274+
* See mincore: reveal pagecache information only for files
4275+
* that the calling process has write access to, or could (if
4276+
* tried) open for writing.
4277+
*/
4278+
static inline bool can_do_cachestat(struct file *f)
4279+
{
4280+
if (f->f_mode & FMODE_WRITE)
4281+
return true;
4282+
if (inode_owner_or_capable(file_mnt_idmap(f), file_inode(f)))
4283+
return true;
4284+
return file_permission(f, MAY_WRITE) == 0;
4285+
}
4286+
42734287
/*
42744288
* The cachestat(2) system call.
42754289
*
@@ -4329,6 +4343,11 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
43294343
return -EOPNOTSUPP;
43304344
}
43314345

4346+
if (!can_do_cachestat(f.file)) {
4347+
fdput(f);
4348+
return -EPERM;
4349+
}
4350+
43324351
if (flags != 0) {
43334352
fdput(f);
43344353
return -EINVAL;

0 commit comments

Comments
 (0)