From patchwork Thu Jun 22 00:17:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9803205 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2ADD66037F for ; Thu, 22 Jun 2017 00:17:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1F6E228531 for ; Thu, 22 Jun 2017 00:17:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 13EBF28548; Thu, 22 Jun 2017 00:17:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 3518D28531 for ; Thu, 22 Jun 2017 00:17:37 +0000 (UTC) Received: (qmail 32349 invoked by uid 550); 22 Jun 2017 00:17:36 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 32317 invoked from network); 22 Jun 2017 00:17:34 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=YyqoouKA99VDGohZCc7XNzU28gq4dwT2CDypUGqbDV0=; b=W1jAqkEG4Oc2Od/3yOy5qt+EvdNAOu9JwNd6AmxyYxWMzq6eBtrQuKcHt9qBXTQgiw 06Ffm/s56VBnEwtvvCmXAlkPkSZ8VPH5TguyNARu0apyJQRURJF51qaYxvsRfHjhuTqI 40GjapTZLW9Jm8REeF7k81yX0bXI6WeKusMew= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=YyqoouKA99VDGohZCc7XNzU28gq4dwT2CDypUGqbDV0=; b=q0i5lEhojHVPKKvTChcNQ7uHqu3Nr2Sk82odslBD1o8R+JkYn3Cp5XWz64L5Fm2ahj gk/hpeSBGRw9xwkChKR6/m/hYWEWaXBdQWDeudHHI9hM8WnwiqlRXJx9QbiS+QrjAoxr 9Vb7jO5RFtFGln9uWLyohfcneO285ah6aTHf8cik55RzX+nfF0Hho38EPEUyF0sha+6T V005jvLRRObcRQwiCfCAlhiq89Y3sE77PAfMt7VYU8kCEbIxPNJW4nBKUQnvGEx+B4XY WIAOnuQ1BeLpIsaYvQEDN3Yfyug//W6Kfv5RQuNrrQibc9MXAxFVzg51QMH8TM5Z0Age u3GQ== X-Gm-Message-State: AKS2vOzfuW6aAbjVwF/CCDZuHMNN18gw5J0jaG30CybRRvEJXmESo45C N//XJmezqQK0yJiC1ICSdQ== X-Received: by 10.84.224.75 with SMTP id a11mr21182407plt.85.1498090642358; Wed, 21 Jun 2017 17:17:22 -0700 (PDT) Date: Wed, 21 Jun 2017 17:17:20 -0700 From: Kees Cook To: Andrew Morton Cc: Alexander Viro , Qualys Security Advisory , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Message-ID: <20170622001720.GA32173@beast> MIME-Version: 1.0 Content-Disposition: inline Subject: [kernel-hardening] [PATCH] exec: Account for argv/envp pointers X-Virus-Scanned: ClamAV using ClamSMTP When limiting the argv/envp strings during exec to 1/4 of the stack limit, the storage of the pointers to the strings was not included. This means that an exec with huge numbers of tiny strings could eat 1/4 of the stack limit in strings and then additional space would be later used by the pointers to the strings. For example, on 32-bit with a 8MB stack rlimit, an exec with 1677721 single-byte strings would consume less than 2MB of stack, the max (8MB / 4) amount allowed, but the pointers to the strings would consume the remaining additional stack space (1677721 * 4 == 6710884). The result (1677721 + 6710884 == 8388605) would exhaust stack space entirely. Controlling this stack exhaustion could result in pathological behavior in setuid binaries (CVE-2017-1000365). Fixes: b6a2fea39318 ("mm: variable length argument support") Cc: stable@vger.kernel.org Signed-off-by: Kees Cook Acked-by: Rik van Riel Acked-by: Michal Hocko --- fs/exec.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 72934df68471..8079ca70cfda 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -220,8 +220,18 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, if (write) { unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start; + unsigned long ptr_size; struct rlimit *rlim; + /* + * Since the stack will hold pointers to the strings, we + * must account for them as well. + */ + ptr_size = (bprm->argc + bprm->envc) * sizeof(void *); + if (ptr_size > ULONG_MAX - size) + goto fail; + size += ptr_size; + acct_arg_size(bprm, size / PAGE_SIZE); /* @@ -239,13 +249,15 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos, * to work from. */ rlim = current->signal->rlim; - if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) { - put_page(page); - return NULL; - } + if (size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) + goto fail; } return page; + +fail: + put_page(page); + return NULL; } static void put_arg_page(struct page *page)