Received: by 10.192.165.148 with SMTP id m20csp4929994imm; Tue, 8 May 2018 17:50:27 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqKmnMtvd0WO1VoB9tB2BQcZKGzyYO+YLTKBybPOm4cdJqFhTzKqxmxcPuzRCdAIkPBvSh4 X-Received: by 2002:a17:902:108a:: with SMTP id c10-v6mr44701638pla.111.1525827027799; Tue, 08 May 2018 17:50:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525827027; cv=none; d=google.com; s=arc-20160816; b=th7Zs84rBlNZDc9DnT9I2e62Dj8l9LQQElnb1+fm0pBlT8eq50mY0ql+af8ss7WxCj hHQrIXx8rjUQ2KjnxQFQBDQXxO7kj2ok1PFxW7Llf6sTc7lPTqcfM2YDkv4I2nS2PUwf AI+gbrlTB9ho4NsKt8cNB48z+rOYxRcbQ0WA/wrW6naffeWuYnnsIgJHiB6uHahvgvhF Q+zgGlsgVzAs74aCrrFJ0ZracBUjuvNE8mfZsvyVj0Nk72cNmPveSx+/7/0Lr4l/y/mA krCkFQp4TW2aCLQi5aFk82EuT3OSa9Mbl6e9/oUm9n5QcDB8JFdgNdRgTCK8v7ajd1AD lUCg== 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:dkim-signature:arc-authentication-results; bh=Qx6OMzLBLYLEvMvJFpVdRiCcRmiOLrYdYR+q5x+ILg8=; b=AHBs1IFwXqQdvS07Gwh+mAlRBLlqCTO9gMf/2XCV5SGyfWBLncTM2tB0CgdTo/59Bv 6gyfKFwqn1GX/JIM3uzbmQHGEwtUdNl49mZK6s8NE/vW7qxf3btJvPWHPUYYymfT4llF li57B/mtzjLQ6tgN9TwQOVSogPLWHugVkz84S5Zt92PX/OlmtVAGY4dSXd87iBgSm+3m vz963c2SdnhipRVLMInAtTSVv5zoOZJ/1ioxXA0nuK1Gt5hJxGEdg/2SPmNnspK5e7Yc VzH3+2zSC6Z+5yZ3ow1T9YfpZRSo9F4ReyejG3Xdpv6Ldq8ORGO8d+uehlFa0GOBXIuG JJnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=EchP3bqB; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n8-v6si15946598plp.468.2018.05.08.17.50.13; Tue, 08 May 2018 17:50:27 -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; dkim=pass header.i=@chromium.org header.s=google header.b=EchP3bqB; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933303AbeEIAmm (ORCPT + 99 others); Tue, 8 May 2018 20:42:42 -0400 Received: from mail-pg0-f68.google.com ([74.125.83.68]:36434 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932475AbeEIAml (ORCPT ); Tue, 8 May 2018 20:42:41 -0400 Received: by mail-pg0-f68.google.com with SMTP id z70-v6so1681270pgz.3 for ; Tue, 08 May 2018 17:42:40 -0700 (PDT) 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-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Qx6OMzLBLYLEvMvJFpVdRiCcRmiOLrYdYR+q5x+ILg8=; b=hqxfMMu1UPGCzy3NIa9AmOVdAKNKqaQLP7+fQwPhDABJIYpFsefKfX/BSJyl1kKBYJ QbO//eYIbhSqoCsWrnED5NDioy8iUfs42glQquEiMwq2WKTU6FxNdST+pS+BSFrgu4np SBiwJEgJucaeAYKdfNH+m4wr80vqGr6rrnFFejkDMyTppTy9dZRuQn8mRMMt7YFFDbQi 6PkJeG+FxBXJO8/C1s28hQNofoHmeiV7lSBd1U9nkh8oqslNyeJ444Ru/W8hgZPxX5/N SefyPJSbxU77BQUpQiYKH+TJJHCeWyJ+lS7SS7FlpYvRux/tq148Bejp3Ow9pELvOQw7 ze9g== X-Gm-Message-State: ALQs6tCkpO31O+MDkcndEEzeMm3lQqPLJmbYBNU7IMWPaEJtuazTSRRQ dnhz91lOqoCDd0zM4lhRRMRoWg== 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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); } -- 2.17.0