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: 9803201 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 D14A56037F for ; Thu, 22 Jun 2017 00:17:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C24C328531 for ; Thu, 22 Jun 2017 00:17:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B51B828554; Thu, 22 Jun 2017 00:17:27 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 84EAA28531 for ; Thu, 22 Jun 2017 00:17:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752002AbdFVARY (ORCPT ); Wed, 21 Jun 2017 20:17:24 -0400 Received: from mail-pg0-f41.google.com ([74.125.83.41]:33856 "EHLO mail-pg0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751689AbdFVARX (ORCPT ); Wed, 21 Jun 2017 20:17:23 -0400 Received: by mail-pg0-f41.google.com with SMTP id e187so607604pgc.1 for ; Wed, 21 Jun 2017 17:17:22 -0700 (PDT) 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=Jxuphpn9nvK17G5VIiiqGAoHaGYk4rHFDs7/BQ1od2iZ6ZiRNXoIBxpZ3H4E3CNudb I6wm4nDkX3zUMWSjUH60+CUkok8TNm3oR18VTYbTM96yPHM4IrRuGZH4a/tAhGrRwqXU U+1zz6RxhD2YB9y90r+hvogk/Jn1wHfOaQLONRLVULakTDOAJIFG2erBqDuO+PE/eAqL /qHoTUVlKb4MaxMo82o6XvRGyVAxXKa3W6JGgSvhYVrKKTghSWxaHngo0C/9V/8M1kCl Sc/9yaE/89OpmVpsEGZZ6F0weA/vjsRc8vTwexIuKqmESSTiw5ErV7xMJ5j2/aKGUQv5 TqmA== X-Gm-Message-State: AKS2vOxSKVEXbE6pMo5QRsJQs7a49ub7qqEPIRy0HuzqOm/FIe55aAsR Lhmez+4e7lB+VwCS X-Received: by 10.84.224.75 with SMTP id a11mr21182407plt.85.1498090642358; Wed, 21 Jun 2017 17:17:22 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id v62sm32082005pfb.124.2017.06.21.17.17.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Jun 2017 17:17:21 -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 Subject: [PATCH] exec: Account for argv/envp pointers Message-ID: <20170622001720.GA32173@beast> MIME-Version: 1.0 Content-Disposition: inline Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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)