Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1244605pxb; Fri, 21 Jan 2022 13:13:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJzLCUv4hkhb6VftuRzwdFmnnRxn19SigiZQ7CFRzhQcpFepZXkcEXZGjqBuY81yR38Za4aF X-Received: by 2002:a17:90b:4c46:: with SMTP id np6mr2496327pjb.83.1642799599561; Fri, 21 Jan 2022 13:13:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642799599; cv=none; d=google.com; s=arc-20160816; b=su6Tx0PQhQA1w1bUlEpZhtANH8uT/3a9qPn5ioWHkla3Juhbw0juUi/jLE7JagSuHD on++IVW0WJZBXv7lqXcK8Er2jO0E+fc4trkOxgqCx1hliYyyEiOBvTptfN3s99F68/KI HZo4L3VLK7D/MacxDj/RTSg2gZaIE32KbH3FVNhYc/fUx4dI7F5/lpXA7gUn1z4y1kU3 xL6vXyvD8YzuaqWutib6vO2Za6LSgdLNJLqGr4sIP6VQIaa6wTzpgtLgx+3Y1r+TpR0P SjCg+X5LcrbHGCwsg33NcyZ/u4e3bmN21HwptmTewnJJe1hjiJRukO06RZIcuiwTZI/T 6Qow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=9YtSx5BiThs6aU+7fCREIxWgUbRGjAZu2PRwXo/rLeg=; b=BfPgQezsbCgIGv4oNT8oTtTzMttAL88UhLBwK1/7FSm3pEWZ2zFqo/rZTkJz7fSOeK MqysY600RuBMoNUMSOTfHgdSs9KAP20yio2DuliskuLvmhLFqPXkWjO5T6bFS5KBCWNO Bf2uRXu7V3eW9X0iBSdgulwRnf+a7BAiXM14HC8A9Oiy3qfJtB0NAre39B1mR9dPwSSK aEHDPjEYmVzniVBr5sA3iFAJDRmuGDexmjdUd+lzgN1Hltx3i4i6MPYK93EC6xPIlHkk Klp+4DO420wl19Y+w54TtmS44mZmEYWV/KNa96PwR56O2WLqlmKb9jCfpQYJ2EHSrXp4 eD+w== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u8si8445778ple.560.2022.01.21.13.13.07; Fri, 21 Jan 2022 13:13:19 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359574AbiATJaB (ORCPT + 99 others); Thu, 20 Jan 2022 04:30:01 -0500 Received: from smtp25.cstnet.cn ([159.226.251.25]:48602 "EHLO cstnet.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S239847AbiATJaA (ORCPT ); Thu, 20 Jan 2022 04:30:00 -0500 Received: from localhost.localdomain (unknown [124.16.138.126]) by APP-05 (Coremail) with SMTP id zQCowACni0GBK+lhs7SRBg--.8229S2; Thu, 20 Jan 2022 17:29:37 +0800 (CST) From: Jiasheng Jiang To: gregkh@linuxfoundation.org Cc: keescook@chromium.org, dan.carpenter@oracle.com, arnd@arndb.de, linux-kernel@vger.kernel.org, Jiasheng Jiang Subject: [PATCH v4] lkdtm/bugs: Check for the NULL pointer after calling kmalloc Date: Thu, 20 Jan 2022 17:29:36 +0800 Message-Id: <20220120092936.1874264-1-jiasheng@iscas.ac.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: zQCowACni0GBK+lhs7SRBg--.8229S2 X-Coremail-Antispam: 1UD129KBjvJXoW7tFy3Wr1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ulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. v2 -> v3 * Change 1. Add pr_err if fails. v3 -> v4 * Change 1. Revert to v2 that remove the pr_err. --- 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 4282b625200f..072e3b742edf 100644 --- a/drivers/misc/lkdtm/bugs.c +++ b/drivers/misc/lkdtm/bugs.c @@ -248,6 +248,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; + } pr_info("Array access within bounds ...\n"); /* For both, touch all bytes in the actual member size. */ -- 2.25.1