在 2022/3/18 15:44, Arnd Bergmann 写道:
On Fri, Mar 18, 2022 at 8:11 AM Chen Jiahao <chenjiahao16@xxxxxxxxxx> wrote:
In __access_ok, TASK_SIZE_MAX is used to check if a memory access
is in user address space, but some cases may get omitted in compat
mode.
For example, a 32-bit testcase calling pread64(fd, buf, -1, 1)
and running in x86-64 kernel, the obviously illegal size "-1" will
get ignored by __access_ok. Since from the kernel point of view,
32-bit userspace 0xffffffff is within the limit of 64-bit
TASK_SIZE_MAX.
Replacing the limit TASK_SIZE_MAX with TASK_SIZE in __access_ok
will fix the problem above.
I don't see what problem this fixes, the choice of TASK_SIZE_MAX in
__access_ok() is intentional, as this means we can use a compile-time
constant as the limit, which produces better code.
Any user pointer between COMPAT_TASK_SIZE and TASK_SIZE_MAX is
not accessible by a user process but will not let user space access
any kernel data either, which is the point of the check.
In your example of using '-1' as the pointer, access_ok() returns true,
so the kernel can go on to perform an unchecked __get_user() on
__put_user() on 0xffffffffull, which causes page fault that is intercepted
by the ex_table fixup.
This should not result in any user visible difference, in both cases
user process will see a -EFAULT return code from its system call.
Are you able to come up with a test case that shows an observable
difference in behavior?
Arnd
.
Actually, this patch do comes from a testcase failure, the code is
pasted below:
#define TMPFILE "__1234567890"
#define BUF_SIZE 1024
int main()
{
char buf[BUF_SIZE] = {0};
int fd;
int ret;
int err;
fd = open(TMPFILE, O_CREAT | O_RDWR);
if(-1 == fd)
{
perror("open");
return 1;
}
ret = pread64(fd, buf, -1, 1);
if((-1 == ret) && (EFAULT == errno))
{
close(fd);
unlink(TMPFILE);
printf("PASS\n");
return 0;
}
err = errno;
perror("pread64");
printf("err = %d\n", err);
close(fd);
unlink(TMPFILE);
printf("FAIL\n");
return 1;
}
The expected result is:
PASS
but the result of 32-bit testcase running in x86-64 kernel with compat
mode is:
pread64: Success
err = 0
FAIL
In my explanation, pread64 is called with count '0xffffffffull' and
offset '1', which might still not trigger
page fault in 64-bit kernel.
This patch uses TASK_SIZE as the addr_limit to performance a stricter
address check and intercepts
the illegal pointer address from 32-bit userspace at a very early time.
Which is roughly the same
address limit check as __access_ok in arch/ia64.
This is why this fixes my testcase failure above, or have I missed
anything else?
Jiahao