public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Yury Norov <yury.norov@gmail.com>
To: Joel Savitz <jsavitz@redhat.com>
Cc: linux-kernel@vger.kernel.org,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@kernel.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Waiman Long <longman@redhat.com>,
	Mauro Carvalho Chehab <mchehab+samsung@kernel.org>,
	Kristina Martsenko <kristina.martsenko@arm.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Cyrill Gorcunov <gorcunov@gmail.com>,
	Kees Cook <keescook@chromium.org>,
	"Gustavo A. R. Silva" <gustavo@embeddedor.com>,
	YueHaibing <yuehaibing@huawei.com>,
	Micah Morton <mortonm@chromium.org>,
	Yang Shi <yang.shi@linux.alibaba.com>,
	Jann Horn <jannh@google.com>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Rafael Aquini <aquini@redhat.com>,
	Michael Kerrisk <mtk.manpages@gmail.com>,
	David Laight <David.Laight@aculab.com>
Subject: Re: [PATCH v3 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2)
Date: Fri, 3 May 2019 14:08:31 -0700	[thread overview]
Message-ID: <20190503210831.GB5887@yury-thinkpad> (raw)
In-Reply-To: <1556907021-29730-2-git-send-email-jsavitz@redhat.com>

On Fri, May 03, 2019 at 02:10:20PM -0400, Joel Savitz 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.
> 
> It is important that we account for the case of the userspace task
> running in 32-bit compat mode on a 64-bit kernel. As such, we must be
> careful to copy the correct number of bytes to userspace to avoid stack
> corruption.
> 
> Suggested-by: Yuri Norov <yury.norov@gmail.com>

I actually didn't suggest that. If you _really_ need TASK_SIZE to
be exposed, I would suggest to expose it in kernel headers. TASK_SIZE
is a compile-time information, and it may available for userspace at
compile time as well.

> Suggested-by: Alexey Dobriyan <adobriyan@gmail.com>
> Signed-off-by: Joel Savitz <jsavitz@redhat.com>
> ---
>  include/uapi/linux/prctl.h |  3 +++
>  kernel/sys.c               | 23 +++++++++++++++++++++++
>  2 files changed, 26 insertions(+)
> 
> diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
> index 094bb03b9cc2..2c261c461952 100644
> --- a/include/uapi/linux/prctl.h
> +++ b/include/uapi/linux/prctl.h
> @@ -229,4 +229,7 @@ struct prctl_mm_map {
>  # define PR_PAC_APDBKEY                        (1UL << 3)
>  # define PR_PAC_APGAKEY                        (1UL << 4)
> 
> +/* Get the process virtual memory size (i.e. the highest usable VM address) */
> +#define PR_GET_TASK_SIZE               55
> +
>  #endif /* _LINUX_PRCTL_H */
> diff --git a/kernel/sys.c b/kernel/sys.c
> index 12df0e5434b8..709584400070 100644
> --- a/kernel/sys.c
> +++ b/kernel/sys.c
> @@ -2252,6 +2252,26 @@ static int propagate_has_child_subreaper(struct task_struct *p, void *data)
>         return 1;
>  }
> 
> +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))

It breaks build for all architectures except x86 since TIF_ADDR32 is
defined for x86 only.

In comment to v2 I suggested you to stick to fixed-size data type to
avoid exactly this problem.

NACK

Yury

> +		current_word_size >>= 1;
> +#endif
> +
> +	return copy_to_user(uaddr, &current_task_size, current_word_size) ? -EFAULT : 0;
> +}
> +
>  int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
>  {
>         return -EINVAL;
> @@ -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);
> +		break;
>         default:
>                 error = -EINVAL;
>                 break;
> --
> 2.18.1

  reply	other threads:[~2019-05-03 21:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-03 18:10 [PATCH v3 0/2] sys/prctl: expose TASK_SIZE value to userspace Joel Savitz
2019-05-03 18:10 ` [PATCH v3 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2) Joel Savitz
2019-05-03 21:08   ` Yury Norov [this message]
2019-05-03 21:51     ` Rafael Aquini
2019-05-03 22:14     ` Rafael Aquini
2019-05-03 23:15   ` Jann Horn
2019-05-04  6:56   ` Alexey Dobriyan
2019-05-03 18:10 ` [PATCH v3 2/2] prctl.2: Document the new PR_GET_TASK_SIZE option Joel Savitz
2019-05-03 20:49 ` [PATCH v3 0/2] sys/prctl: expose TASK_SIZE value to userspace Yury Norov
2019-05-03 21:57   ` Rafael Aquini
2019-05-04  4:21     ` Yury Norov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190503210831.GB5887@yury-thinkpad \
    --to=yury.norov@gmail.com \
    --cc=David.Laight@aculab.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=aquini@redhat.com \
    --cc=gorcunov@gmail.com \
    --cc=gustavo@embeddedor.com \
    --cc=jannh@google.com \
    --cc=jsavitz@redhat.com \
    --cc=keescook@chromium.org \
    --cc=kristina.martsenko@arm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mchehab+samsung@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=mortonm@chromium.org \
    --cc=mtk.manpages@gmail.com \
    --cc=tglx@linutronix.de \
    --cc=yang.shi@linux.alibaba.com \
    --cc=yuehaibing@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox