Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp964031yba; Fri, 3 May 2019 13:29:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqyhNJx3tr1qOsdTrmR89i7ISldp4cS7wnlFqBBAShCX57IX940y53spPrnEncCCi2FXAhkz X-Received: by 2002:a17:902:7d90:: with SMTP id a16mr12857938plm.122.1556915376879; Fri, 03 May 2019 13:29:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556915376; cv=none; d=google.com; s=arc-20160816; b=V3odPmxR3y5E2nN6W+krlemClHzyLMFlMweZvaUOOjD37f7ehV+s2v3lPZ6S946C1T zQNGdykZ0Zg4wkXKWH0YyZR6AjIFAMF+slpkE0juqJGtjINCGA1VCH3rxXNZaUM47TII AMlX52U4Y4ZnzpDSSDCTXYaZ8xNllQHtApvj04NsyK83w+aXcZXAxKXw9ouhflWwcOb7 rrptLuQ+3rhr3zAJXwmfTy06VHBdXcOJp+Xkw5JT2GFwdV9kmXnD45HWKsC9mVvFwLEb bEtMlAOIpNSNapHVId2i1+YWWPhs4IWBL5zYU5E1YUItbdY5aPSU86zJZyfCI5Ik5H1k 9WzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=vwwugaUBc5SOulw+gOc9TNQE702WvKaW/UYqdHNJ7b8=; b=WqkV7zU6up6t/D12g/6L2LigD/6pomb5vuWM9Mscmwsq4eJ69GI1x5+vV/3w9TEsPW qCZrSWSJWKfdorpxKl1ItG0btxTAlaRea7pywYoMi4CCn9kpoq0v6mh81m3g9jc18Hfc TqrbUJ6o/RcxuCiERarZoj0qU5VewRGk1bSXsmRq9BVrGjTvenAZ59iIL5AaPPObw09p C3PBdHvet47NZRuCqKtj8P3FYU3sDFzOh3AZa6goc+Mz7bLsqoufvCdOMJqxHH64iE2z CXn6N0gxzTyZZ7j/WF/zf7cij8ZTBO0kMJ5Hkkxl0pFcs7NKPJBNNftNUuGQMgK5jzNe 2VcA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t7si3563518pgu.342.2019.05.03.13.29.03; Fri, 03 May 2019 13:29:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728605AbfECSMS (ORCPT + 99 others); Fri, 3 May 2019 14:12:18 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47294 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726992AbfECSMR (ORCPT ); Fri, 3 May 2019 14:12:17 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DFCDC58E3E; Fri, 3 May 2019 18:12:16 +0000 (UTC) Received: from jsavitz.bos.com (dhcp-17-237.bos.redhat.com [10.18.17.237]) by smtp.corp.redhat.com (Postfix) with ESMTP id EA7AB60BFB; Fri, 3 May 2019 18:12:14 +0000 (UTC) From: Joel Savitz To: linux-kernel@vger.kernel.org Cc: Joel Savitz , Thomas Gleixner , Ingo Molnar , Masami Hiramatsu , Waiman Long , Mauro Carvalho Chehab , Kristina Martsenko , Andrew Morton , Cyrill Gorcunov , Kees Cook , "Gustavo A. R. Silva" , YueHaibing , Micah Morton , Yang Shi , Jann Horn , Alexey Dobriyan , Rafael Aquini , Michael Kerrisk , Yury Norov , David Laight Subject: [PATCH v3 1/2] kernel/sys: add PR_GET_TASK_SIZE option to prctl(2) Date: Fri, 3 May 2019 14:10:20 -0400 Message-Id: <1556907021-29730-2-git-send-email-jsavitz@redhat.com> In-Reply-To: <1556907021-29730-1-git-send-email-jsavitz@redhat.com> References: <1556907021-29730-1-git-send-email-jsavitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 03 May 2019 18:12:17 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Suggested-by: Alexey Dobriyan Signed-off-by: Joel Savitz --- 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)) + current_word_size >>= 1; +#endif + + return copy_to_user(uaddr, ¤t_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