From patchwork Wed May 9 00:42:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10387815 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 65B936022E for ; Wed, 9 May 2018 00:45:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3EA6329059 for ; Wed, 9 May 2018 00:45:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 96A872871B; Wed, 9 May 2018 00:44:28 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F99B29090 for ; Wed, 9 May 2018 00:42:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB23D6B02ED; Tue, 8 May 2018 20:42:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B14636B02EF; Tue, 8 May 2018 20:42:42 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B6476B02F0; Tue, 8 May 2018 20:42:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg0-f72.google.com (mail-pg0-f72.google.com [74.125.83.72]) by kanga.kvack.org (Postfix) with ESMTP id 542C46B02ED for ; Tue, 8 May 2018 20:42:42 -0400 (EDT) Received: by mail-pg0-f72.google.com with SMTP id q8-v6so14480213pgv.22 for ; Tue, 08 May 2018 17:42:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=Qx6OMzLBLYLEvMvJFpVdRiCcRmiOLrYdYR+q5x+ILg8=; b=VVexZCWsLZVplw6rKAE1xZalNRzHzRa3KbKoKZUeS4tEIjIuIpyZAIhTVoRM3luMxW vjW6npnzG/irJ0P6U4w2mxXlYXl60wdzjhR+SL5EXm4mTtpSFDDAN6HNasptsaju2PPB cK0iagsdVLumnEuVOF6bdHOsp+JboDP7zl6jeiY+ELQ3RA/0zrQehpgoi1wKXXloJ8tn WcXZV8ZWRsvtW/eB9eYmqq+xsXcevZXlOaNqS+yfQHw6wMyQCdSFL43mzx691akBiIqe +atnUZM3rP9+7JCzMgU0DHeprrSIJmCh1nfv5mTGXznNUdiwItbi/HSljqeGtdSpxBnn BEdQ== X-Gm-Message-State: ALQs6tAqYs80Aa0hPS31zByrMzyXubflIt4825iZ3s3IOas1RokVDwHI Gb3FtZiwRC2RG+nh7MScnkg51v8sX6XT3KtMA1SZUmqEJsBYP5MM4lNwzm9sXTM5RzKf1g1Cpr0 4pwfcfWM00w8iJjH4sU4G/cpVrWTRkLkGGS0TnhZ9Hw2Gwpk/bUxCZajfN4vf/YIGxRqmsHkAOv lExHJkDEn4EiOrtUfQCdL26LXXT/A94P6a4pkiZkSWBno2AUvB/ZBGfr1fOvUmiijQwvtd7HLmh /KzrRoPLk+wtrCi1YppiPk1Ib2xgWYzKOH+6irMdzOYs70KD2w5XChzQsYhrJmYrznHzz35BXaA 7nzspIUKs0librMwaEXAK+V6UTVTG0zCgqOnefotIRpHKFPfNWyt9c6vyuTYum5RNu90DEr1xGb H X-Received: by 2002:a63:755c:: with SMTP id f28-v6mr35206210pgn.30.1525826561907; Tue, 08 May 2018 17:42:41 -0700 (PDT) X-Received: by 2002:a63:755c:: with SMTP id f28-v6mr35206186pgn.30.1525826561024; Tue, 08 May 2018 17:42:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525826560; cv=none; d=google.com; s=arc-20160816; b=EE8u1gwABVh8lUgTblDAItxPu9/kPLt5/KxqlaFtZlhjhHJLMwM5aD4tbEd2j2oo2t cyIEdjSO0xrY9avxlbHcpKasEeExq4EQs4wVmSLJtsiVjkqKPzwU0GVWxWG9EOx8Px9I nF+BZmVYToGu/SOKJmDYEW+Zw5qlICrpy5CVXRZ61HbAPd5DbJhf9EY84bpuPU7R9zCF IZk3fefcCQaKe8Sw8EU3Ag4xyMcPOfT0DhJlFH1p/qxLHsv8PLfvkMYlJlzLcQq9i9P9 LutqYioQu2wgKEu2DWnGJxABoByBzG8RhsDjINC8pF0f6UO56OBTzsZHWpeKVjCsOo/H ESsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=Qx6OMzLBLYLEvMvJFpVdRiCcRmiOLrYdYR+q5x+ILg8=; b=s8fPT/ULiSpvQ/cKSmEvikFNSRC3tsZrw56U+CW5h78gila7Xr6QJ4YN3blnM3lG37 xXrUD7nHUL4GjeWscFXr70oL0XDy7T8FOcW2PMnZUdCEG7brPBKBM/i/5xITuBqRdmHF CWn075wBdgWybhGq96C+Jpi2wYzHeO170zjwJDxdOcVYUkNFeya9tWoSA52cDVpB9AqL Afo99m60aD7drkBoJHBUhXlUaNevDeg2heoEs8nYV43ueWDPCbfBWtahWwYHeEOOivJA lHttkeyh5sJhDD6rhMlTfwMzeQ8LuJJ3geNiK9eDm5lDuLyxNFXvGMIM2HQIuIxiuFTS /XjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EchP3bqB; spf=pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id x15-v6sor5348870pgq.213.2018.05.08.17.42.40 for (Google Transport Security); Tue, 08 May 2018 17:42:40 -0700 (PDT) Received-SPF: pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EchP3bqB; spf=pass (google.com: domain of keescook@chromium.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Qx6OMzLBLYLEvMvJFpVdRiCcRmiOLrYdYR+q5x+ILg8=; b=EchP3bqB/MjDE14k+xBKevFL0oQNq7ckw924bxj0KKXVwxJnicKHnyIUG6XOllhmaU msI0qtl3lu4fBqgaUM8BAP8Z+zTpeCimJvk6wN9o2GYbTTrCocSzp1SzwWzZjkAPG7Jk n7EhcOOpirAZc+CccSDLzI14Us0ZjWdW5qyC0= X-Google-Smtp-Source: AB8JxZq+1cd9h2Iu2geiWXeoy6buXWBqLBJHuUM3jeMZVeE06fXlIoZK+VxlIFki6YyUOW8Al4n8Ig== X-Received: by 2002:a63:69c3:: with SMTP id e186-v6mr34219080pgc.353.1525826560635; Tue, 08 May 2018 17:42:40 -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 c6sm12294278pfj.91.2018.05.08.17.42.38 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 May 2018 17:42:39 -0700 (PDT) From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH 04/13] mm: Use array_size() helpers for kmalloc() Date: Tue, 8 May 2018 17:42:20 -0700 Message-Id: <20180509004229.36341-5-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180509004229.36341-1-keescook@chromium.org> References: <20180509004229.36341-1-keescook@chromium.org> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Instead of open-coded multiplication, use the new array_size() helper to detect overflow in kmalloc()-family functions. Signed-off-by: Kees Cook --- include/linux/slab.h | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 81ebd71f8c03..d03e0726e136 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -13,6 +13,7 @@ #define _LINUX_SLAB_H #include +#include #include #include @@ -499,6 +500,8 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags) */ static __always_inline void *kmalloc(size_t size, gfp_t flags) { + if (size == SIZE_MAX) + return NULL; if (__builtin_constant_p(size)) { if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large(size, flags); @@ -539,6 +542,8 @@ static __always_inline unsigned int kmalloc_size(unsigned int n) static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) { + if (size == SIZE_MAX) + return NULL; #ifndef CONFIG_SLOB if (__builtin_constant_p(size) && size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) { @@ -624,11 +629,13 @@ int memcg_update_all_caches(int num_memcgs); */ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) { - if (size != 0 && n > SIZE_MAX / size) + size_t bytes = array_size(n, size); + + if (bytes == SIZE_MAX) return NULL; if (__builtin_constant_p(n) && __builtin_constant_p(size)) - return kmalloc(n * size, flags); - return __kmalloc(n * size, flags); + return kmalloc(bytes, flags); + return __kmalloc(bytes, flags); } /** @@ -639,7 +646,9 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) */ static inline void *kcalloc(size_t n, size_t size, gfp_t flags) { - return kmalloc_array(n, size, flags | __GFP_ZERO); + size_t bytes = array_size(n, size); + + return kmalloc(bytes, flags | __GFP_ZERO); } /* @@ -657,16 +666,22 @@ extern void *__kmalloc_track_caller(size_t, gfp_t, unsigned long); static inline void *kmalloc_array_node(size_t n, size_t size, gfp_t flags, int node) { - if (size != 0 && n > SIZE_MAX / size) + size_t bytes = array_size(n, size); + + if (bytes == SIZE_MAX) return NULL; if (__builtin_constant_p(n) && __builtin_constant_p(size)) - return kmalloc_node(n * size, flags, node); - return __kmalloc_node(n * size, flags, node); + return kmalloc_node(bytes, flags, node); + return __kmalloc_node(bytes, flags, node); } static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node) { - return kmalloc_array_node(n, size, flags | __GFP_ZERO, node); + size_t bytes = array_size(n, size); + + if (bytes == SIZE_MAX) + return NULL; + return kmalloc_node(bytes, flags | __GFP_ZERO, node); }