Commit 2465b398 authored by Linus Torvalds's avatar Linus Torvalds Committed by Wen Zhiwei
Browse files

cachestat: fix page cache statistics permission checking

stable inclusion
from stable-v6.6.75
commit 7d6405c13b0d8a8367cd8df63f118b619a3f0dd2
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IBSPQB

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=7d6405c13b0d8a8367cd8df63f118b619a3f0dd2



--------------------------------

commit 5f537664e705b0bf8b7e329861f20128534f6a83 upstream.

When the 'cachestat()' system call was added in commit cf264e13
("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 134fca90 ("mm/mincore.c: make mincore()
more conservative").

This just adds equivalent logic to 'cachestat()', modified for the file
context (rather than vma).

Reported-by: default avatarSudheendra Raghav Neela <sneela@tugraz.at>
Fixes: cf264e13 ("cachestat: implement cachestat syscall")
Tested-by: default avatarJohannes Weiner <hannes@cmpxchg.org>
Acked-by: default avatarJohannes Weiner <hannes@cmpxchg.org>
Acked-by: default avatarNhat Pham <nphamcs@gmail.com>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarWen Zhiwei <wenzhiwei@kylinos.cn>
parent 2b954417
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -4373,6 +4373,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.
 *
@@ -4432,6 +4446,11 @@ SYSCALL_DEFINE4(cachestat, unsigned int, fd,
		return -EOPNOTSUPP;
	}

	if (!can_do_cachestat(f.file)) {
		fdput(f);
		return -EPERM;
	}

	if (flags != 0) {
		fdput(f);
		return -EINVAL;