Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp29390imm; Thu, 31 May 2018 17:47:25 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJZ8dlkDqBb69dP5PIKplYRnl78kUvp/Pt3NJ15tmrlsBVXVlXqAuFdX7jlMo4HHLLsnKVg X-Received: by 2002:a17:902:9a98:: with SMTP id w24-v6mr9199315plp.9.1527814045316; Thu, 31 May 2018 17:47:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527814045; cv=none; d=google.com; s=arc-20160816; b=FPAjNFkKeebZ/KvnWgiijQjkN7AB7DYJQggchkwkCHMEA8W7gjA0EkZsLxTlaDroLO h4eQ5XwjCnotz+QO57q++2HrPi60JJgRgLJ+T8wR31bXapwMtYfuODQJBRFeT6RnULmz nxSoEePSYYh0Cnzj5Zolg4zjEifBq0oK47my+WIqYadCvPrw3TbFzfHfvy1fdKRjOJp4 OlhaMLI6lGX28f1vtP6efjTvzrt4gUxd2+AjTACrBuhKv83rWOItXuNO0wGfDpoz8YJZ 0wUNLd8wOOQ30D439/p0dlIZo3/xkttG8YovDFJtdhWYKydnBPdjky/vLU+kvSYqIYq0 a8sQ== 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=/JUjYk2nCovIupNyFgtiO+nuZfsAJgLxrIErm5uq/jE=; b=P3gUeOtfh6eBgW+yPQf16PGyHQdKbgeJYIeSyaptxblw0Wpth3++cBd08WrfkJF0ZH zO7zWwJ8WyWaeTy3jg3TdG2m4RHx8tYgd/78FLlxCt3DmTe6/KLGz9JmfKqNHPvagak8 A7fo+dK8YGm1YeJ9/tS6S8xp8CMRedAw6836nOjCrzrvZqEkZF9VFifsMybpTHOv3Vd7 9sbhoU50v/BFdASS6CNvEVN9dPNYwMRuWKb0q5zN0EhLrPCli3P79WSFMxkb4rIRvsZO 0aEEzbWcJFYR4d76rquYJkXcW3ESY8jo8tR71AT5Vqo1lovAD4Z5qSvzq3vvOzs46+XE Qv9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UZ05RovQ; 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 k21-v6si8733056pgn.504.2018.05.31.17.47.11; Thu, 31 May 2018 17:47:25 -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=UZ05RovQ; 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 S1751782AbeFAAqL (ORCPT + 99 others); Thu, 31 May 2018 20:46:11 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:36409 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751388AbeFAAnt (ORCPT ); Thu, 31 May 2018 20:43:49 -0400 Received: by mail-pg0-f65.google.com with SMTP id m5-v6so6084297pgd.3 for ; Thu, 31 May 2018 17:43:48 -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=/JUjYk2nCovIupNyFgtiO+nuZfsAJgLxrIErm5uq/jE=; b=UZ05RovQAWKIcizxOrVseQ9neuK9Ghr4QUCaj25icNPXKuEwLeVnuM07PwN677HMLg cO3W96aCDo6xpmIexSx1+PBiQcxkxhMOnA/k8GgYs7OztxTxn2b2Hxjz9NgfpiTVUYIO M/gCjO3t3CStaVomRtzIQtu+pQRvyeA0qwxsw= 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=/JUjYk2nCovIupNyFgtiO+nuZfsAJgLxrIErm5uq/jE=; b=kZMFW2ZLSXXQhHQZcMurws0LZUrCsPUejEoVJv7/bBArBNfyJ5uMwZDVg5iu+N05g+ yYW2diaX27v9f6LrBSHvuUVM4QsHftrKEofsMYFcO+wWXr1lwFNReCYsPhhXCuKDaRaP xB6LPVAZa0SwymeXUgk/mUSmt3AMW0jOoZj9Uo4+xcEwur9WAFZ+6/lQ139pB/j8rZ3B 5SfDv3Qf4qgM7h5VscXdQPfiYn6r5FH7JfC95zTgrhWtF1HzPmclzf+1idHr1W5wvRkY eHlNyxvo5KfwrllgkJbmhKom8DeC2FfkWFKrlsanVlopT0P57d1h1Q9c/xHL7VyEU+EW w4JA== X-Gm-Message-State: ALKqPwdsDeq7HRCAi7Pu8TSafZ7he658mfMODDESZlAgijwGHziFIf3t zT0Q9FdR6ckEH4/AAYFnl/jQcg== X-Received: by 2002:a62:ee14:: with SMTP id e20-v6mr8855355pfi.130.1527813828539; Thu, 31 May 2018 17:43:48 -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 j186-v6sm37776387pgc.10.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 07/16] mm: Use overflow helpers in kvmalloc() Date: Thu, 31 May 2018 17:42:24 -0700 Message-Id: <20180601004233.37822-8-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. Additionally prepare for vmalloc() users to add array_size()-family helpers in the future. Signed-off-by: Kees Cook --- include/linux/mm.h | 7 +++++-- include/linux/vmalloc.h | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 1ac1f06a4be6..7cb1c6a6bf82 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -25,6 +25,7 @@ #include #include #include +#include struct mempolicy; struct anon_vma; @@ -560,10 +561,12 @@ static inline void *kvzalloc(size_t size, gfp_t flags) static inline void *kvmalloc_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; - return kvmalloc(n * size, flags); + return kvmalloc(bytes, flags); } extern void kvfree(const void *addr); diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 1e5d8c392f15..398e9c95cd61 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -8,6 +8,7 @@ #include #include /* pgprot_t */ #include +#include struct vm_area_struct; /* vma defining user mapping in mm_types.h */ struct notifier_block; /* in notifier.h */ -- 2.17.0