Skip to content

Commit

Permalink
cachestat: fix page cache statistics permission checking
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Linus Torvalds committed Jan 22, 2025
1 parent c4b9570 commit 5f53766
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mm/filemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4375,6 +4375,20 @@ static void filemap_cachestat(struct address_space *mapping,
rcu_read_unlock();
}

/*
* See mincore: reveal pagecache information only for files
* that the calling process has write access to, or could (if
* tried) open for writing.
*/
static inline bool can_do_cachestat(struct file *f)
{
if (f->f_mode & FMODE_WRITE)
return true;
if (inode_owner_or_capable(file_mnt_idmap(f), file_inode(f)))
return true;
return file_permission(f, MAY_WRITE) == 0;
}

/*
* The cachestat(2) system call.
*
Expand Down Expand Up @@ -4430,6 +4444,9 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
if (is_file_hugepages(fd_file(f)))
return -EOPNOTSUPP;

if (!can_do_cachestat(fd_file(f)))
return -EPERM;

if (flags != 0)
return -EINVAL;

Expand Down

0 comments on commit 5f53766

Please sign in to comment.