Re: [PATCH v3 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2)

From: Jann Horn
Date: Fri May 03 2019 - 19:16:57 EST


On Fri, May 3, 2019 at 2:12 PM Joel Savitz <jsavitz@xxxxxxxxxx> wrote:
> When PR_GET_TASK_SIZE is passed to prctl, the kernel will attempt to
> copy the value of TASK_SIZE to the userspace address in arg2.

A commit message shouldn't just describe what you're doing, but also
why you're doing it. Is this intended for processes that are running
on X86-64 and want to determine whether the system supports 5-level
paging, or something like that?

> +static int prctl_get_tasksize(void __user *uaddr)
> +{
> + unsigned long current_task_size, current_word_size;
> +
> + current_task_size = TASK_SIZE;
> + current_word_size = sizeof(unsigned long);
> +
> +#ifdef CONFIG_64BIT
> + /* On 64-bit architecture, we must check whether the current thread
> + * is running in 32-bit compat mode. If it is, we can simply cut
> + * the size in half. This avoids corruption of the userspace stack.
> + */
> + if (test_thread_flag(TIF_ADDR32))
> + current_word_size >>= 1;
> +#endif
> +
> + return copy_to_user(uaddr, &current_task_size, current_word_size) ? -EFAULT : 0;
> +}

This function looks completely wrong; in particular, you're assuming
that the architecture is little-endian.
Make the value a u64, and you won't have these problems:

static int prctl_get_tasksize(u64 __user *uaddr)
{
return put_user(TASK_SIZE, uaddr) ? -EFAULT : 0;
}

A bunch of other new pieces of userspace API already use "u64" to
store userspace pointers and lengths to avoid compat issues.

> @@ -2486,6 +2506,9 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
> return -EINVAL;
> error = PAC_RESET_KEYS(me, arg2);
> break;
> + case PR_GET_TASK_SIZE:
> + error = prctl_get_tasksize((void *)arg2);

s/void */void __user */