Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp30859imm; Thu, 31 May 2018 17:49:44 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJjH+iwLPAJIHoXpyBvtfvuen56J7nZX1qRtNvskUDMx8+rb1TSxJDiTbUnr1gfVHuehx+A X-Received: by 2002:a17:902:7d85:: with SMTP id a5-v6mr8900827plm.356.1527814184804; Thu, 31 May 2018 17:49:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527814184; cv=none; d=google.com; s=arc-20160816; b=uSEOGtCcMqyARq63s5aysys/ODlC/iAvRy8LPg6vqFj3VNyaMAvlhvLxHIQTOVWZYZ oHWQjUqd8OEqVxIT7Kih9m3ZevO2B5PAYxBh+86z52yVQnUcGUdN2enJ4bjeBKtP4qKo h91C6oJVv4/yDVcBWdaA336ygJIb5Fd5Eb8PTSoP61xwSKLgTtzqsh86XCaQuBAy0B2m OYvsWRQO+TJ/QAuYet7+bWXR4zTgC/cl+YR6M165Cg7k3zTG7MiP85HKCE9pTL+haX+r 2O/E/DvigXQmP2ngQQhv9KD7zPAc11jsNorPWLhzVXtRP7rxxzfWHPpUvhL4Fkmxt3BS oarg== 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=DwNEonIHIFheAa6fjiUZY2ler4ciSoofQy/do2M29L4=; b=yhl6Z/QO5Lbyc/RqJqrB7FtHcDONpgaPpeFcoRPaqY0N/wLh+NnxlUGpIZFgo1PMb1 wqCTiuNlZ/V0WUrXo2DTU4+SvRrTgj6WrFukxr4awwRYlg/Hm8z4RQ4GMXhCi5DT3X5J S/uGQMFVm7z8Td1mHjnobDk4KMIgunaiCdS4o+2cFj+vfGZeSBJP3Gs0pYcEe2v/XvJt hu6nhreCNK2C/ZxJUQG/mKKDpN1esuwSDFGKBK5UBygCGOzQRmeO6U8aqltTSE5hzA5W 0Z1EeekaNMMZ/t51gqX45oHSaf0fVHDoqPtbWHYN3ehcLmjE8CpedTZGEWpTtLpjkD+k /kLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KdLg8OKY; 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 c17-v6si2257108pfi.102.2018.05.31.17.49.30; Thu, 31 May 2018 17:49:44 -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=KdLg8OKY; 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 S1751554AbeFAArW (ORCPT + 99 others); Thu, 31 May 2018 20:47:22 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:36430 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750724AbeFAAnr (ORCPT ); Thu, 31 May 2018 20:43:47 -0400 Received: by mail-pf0-f196.google.com with SMTP id w129-v6so11618651pfd.3 for ; Thu, 31 May 2018 17:43:47 -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=DwNEonIHIFheAa6fjiUZY2ler4ciSoofQy/do2M29L4=; b=KdLg8OKY5+GoGGKyMoIaTOU1cr3IKoW6xKmTzeEB3wnfilft6/nWwMaw/LhPPeP50Q T+ZwzCzSfrKewfzcfr000yginL9eIbc6vPBW82RVKAlkgYb1SyCs+15fC/0tkvWOoY/E wKezlvLC5qmORsKCHduE9Y3Zl4k0SBWx+3ZWM= 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=DwNEonIHIFheAa6fjiUZY2ler4ciSoofQy/do2M29L4=; b=hzpFXz51fU0HS0I5zh2kGA+S0wA0UFamf9ZUUYklDyGa8lYTRyxJB6VlgJIAcn5Sdl 59fbplgpblUORd5XnPXZuntmlJ+IurLZUvGkmNNaP2WjF12Af/r6eh4EfKyDNnywx5J1 NWos98CZ79BCOxpJH9ea472vk23rvXziV12GVwhBedP2d8VOYjuI9ZxXfkWYuQFh8LVu nKBeYlqcvgDEFk1LGo498c3T7IsQB+UEJPW1QfQz60K0dRMB2m0f5b78yTZMq6c/71HL cnk3tyk41/unHuRvcwODuGlSIKH6E+1nq+JLPT/Sm5vKr8KIpiF59ADO+xA9tAygBeFs X3QA== X-Gm-Message-State: ALKqPwdZoYsLcLmpknwvp6O5YahJEIiosWb/MS+4V3utk08/uAf8Fk5P nRJ0quVPtS2mqYNMyav83TZQwg== X-Received: by 2002:a63:b705:: with SMTP id t5-v6mr10452pgf.343.1527813827534; Thu, 31 May 2018 17:43:47 -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 s17-v6sm84877527pfi.165.2018.05.31.17.43.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 31 May 2018 17:43:45 -0700 (PDT) From: Kees Cook To: Matthew Wilcox Cc: Kees Cook , Linus Torvalds , Rasmus Villemoes , Matthew Wilcox , LKML , Linux-MM , Kernel Hardening Subject: [PATCH v3 06/16] mm: Use overflow helpers in kmalloc_array*() Date: Thu, 31 May 2018 17:42:23 -0700 Message-Id: <20180601004233.37822-7-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180601004233.37822-1-keescook@chromium.org> References: <20180601004233.37822-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 and bounds checking, use the new overflow helper. Signed-off-by: Kees Cook --- include/linux/slab.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 81ebd71f8c03..4d759e1ddc33 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -13,6 +13,7 @@ #define _LINUX_SLAB_H #include +#include #include #include @@ -624,11 +625,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; + + if (unlikely(check_mul_overflow(n, size, &bytes))) 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); } /** @@ -657,11 +660,13 @@ 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; + + if (unlikely(check_mul_overflow(n, size, &bytes))) 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) -- 2.17.0