Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1192982pxb; Fri, 21 Jan 2022 11:57:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJxXjUwyQ4E2pEg0TfDCNcmHmMtCSrnGEFN7k0PO5OEJRB/lu+H5yBLQpyav6f6R/7fTL/9+ X-Received: by 2002:a62:ab02:0:b0:4c0:1cd9:2f82 with SMTP id p2-20020a62ab02000000b004c01cd92f82mr4941544pff.8.1642795024255; Fri, 21 Jan 2022 11:57:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642795024; cv=none; d=google.com; s=arc-20160816; b=lTG/RktZ8vUgxgzb2ga74GwudKMPTYy7Hrb66DHt0ddoL9l6upcOpiUvpPr8dkVFTD cGIRaaJt73LwnTXODdI0krIYPNQshPytJ6AZboQlPA3wGsn73u2mGpVeEk60RlFhC6Ds EFJpbbVkaNPr0Gcs09c6QFR6NVDWoIm0Bb+JznDnr+p8hqtHUJAg61XtbmDYkNcPRZLB wvFV44tU9IbMk+RPm7R574QUjYDKHsNIldMsyVUxaLs4YKloEqlJYeD/mvky2IcbSKjA mc4nqYg4fXpq33aRyhh5W/stXvUqha9ohPR5hQhIuRdZIbI9ipm3eNyEaovtp3cvjQj/ SfKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=+1aEqQ/nyIMaBlpDHfQKw2cwOT/MTa3zHEsU16u3B0U=; b=ifKiofxaGp4e0eT5rKfLBGUMoJ9DylPQNKlosSVVIlZPu4pxVmNmg9yWJxARWnboSf Htiix4Ucq0ws0PzSS4Ntt0TiHPWmwheDndxwbkztNUFmWK3WTxsydzJmyNzHFLXfhO1i 6ZkVZNPoO96UtmMMO46yX0PjP5Cc875gM/PkNbDnOX0NQKvfRmYQbw9kvzAmQv6TZKAg 4t4KekCtIH9AB90/7usI88M0aYwNsUFwkvB5fy+d5EwtXHzSBSc41S/hrmOL99TnOQg5 OTDn7flCW5c966NlH6oWuGGYpCZ+e4/qFTu4BFgOU+iirFrkL1qON8ZqxUbPraYTOJuB Bv2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kv+M5ciy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id c7si6977619pgm.587.2022.01.21.11.56.51; Fri, 21 Jan 2022 11:57:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kv+M5ciy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1346920AbiASSpg (ORCPT + 99 others); Wed, 19 Jan 2022 13:45:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241670AbiASSpe (ORCPT ); Wed, 19 Jan 2022 13:45:34 -0500 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FE1DC061574 for ; Wed, 19 Jan 2022 10:45:34 -0800 (PST) Received: by mail-pf1-x42e.google.com with SMTP id w204so3093479pfc.7 for ; Wed, 19 Jan 2022 10:45:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=+1aEqQ/nyIMaBlpDHfQKw2cwOT/MTa3zHEsU16u3B0U=; b=kv+M5ciyFpRf2P3r0za6IWJYO1Ra/ayGXj29Z4QCLl6N3v73IdECniDsIDkNoHmYUr 9s0GnNcyeR3bmle1TPhCVtPv8/hLuGA3Q6QpoSAYlp+dNE+EWUm2hL7CIZaOjMRepKJd UCsvLqRXpJhrTNE3plBjsQVVmGHzAKyCA3n8Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=+1aEqQ/nyIMaBlpDHfQKw2cwOT/MTa3zHEsU16u3B0U=; b=uBLQamWY9OjtkKlJG6Iwzcd4htjN5WIIHTERscf6CluZlrP69mPnbNjsl1/uUzJArE 9AjmqCCnH8oA6wttgPAa7FYDPpqwYg68c3ft/hoPgmPVVhx6HtunMco4bOdAQqNW60C2 YBLx2iFrEtz8a/YAZEHXRJSkLhvFayv+QVEv8+9J513/IGrnX4sI09J0F/GnKbfpPRjQ sZKmhLK7uPvuWvtgBKr1lvuGB7+aVhwsumKVIJp/RqEBvhT8gCVZYYO42TgFotwzn5xi 4iTBgrEEHUwYFcMMOfAI6m1zO5mytfF0dkiCiU4AWc+eFR+aSTm8hzOerNjObVRLninU a2KA== X-Gm-Message-State: AOAM533+ZppW5CH/G/CVwqKqxYEGLEVooYxk9rJZdpTJKdGc+MyFeQwv Fvc8I5wJfagZVwQGmiiN3LlsIQ== X-Received: by 2002:a62:c186:0:b0:4c1:232c:819d with SMTP id i128-20020a62c186000000b004c1232c819dmr32039467pfg.28.1642617933942; Wed, 19 Jan 2022 10:45:33 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id u21sm349560pfi.149.2022.01.19.10.45.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jan 2022 10:45:33 -0800 (PST) Date: Wed, 19 Jan 2022 10:45:33 -0800 From: Kees Cook To: Jiasheng Jiang Cc: dan.carpenter@oracle.com, arnd@arndb.de, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] lkdtm/bugs: Check for the NULL pointer after calling kmalloc Message-ID: <202201191044.69872866B@keescook> References: <20220119122055.1826561-1-jiasheng@iscas.ac.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220119122055.1826561-1-jiasheng@iscas.ac.cn> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 19, 2022 at 08:20:55PM +0800, Jiasheng Jiang wrote: > As the possible failure of the kmalloc(), the not_checked and checked > could be NULL pointer. > Therefore, it should be better to check it in order to avoid the > dereference of the NULL pointer. > Also, we need to kfree the 'not_checked' and 'checked' to avoid > the memory leak if fails. > And since it is just a test, it may directly return without error > number. > > Fixes: ae2e1aad3e48 ("drivers/misc/lkdtm/bugs.c: add arithmetic overflow and array bounds checks") > Signed-off-by: Jiasheng Jiang > --- > Changelog > > v1 -> v2 > > * Change 1. Add the kfree if fails. > --- > drivers/misc/lkdtm/bugs.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/drivers/misc/lkdtm/bugs.c b/drivers/misc/lkdtm/bugs.c > index f4cb94a9aa9c..c35ea54824ac 100644 > --- a/drivers/misc/lkdtm/bugs.c > +++ b/drivers/misc/lkdtm/bugs.c > @@ -325,6 +325,11 @@ void lkdtm_ARRAY_BOUNDS(void) > > not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL); > checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL); > + if (!not_checked || !checked) { > + kfree(not_checked); > + kfree(checked); > + return; > + } This should explicitly yell about the memory failure. See the other error cases for examples. I'd expect something like this before the return: pr_err("FAIL: could not allocate required buffers\n"); -Kees > > pr_info("Array access within bounds ...\n"); > /* For both, touch all bytes in the actual member size. */ > -- > 2.25.1 > -- Kees Cook