Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1165175pxb; Fri, 21 Jan 2022 11:14:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJzwslYtHz0j335jXvmK/9RxIQ3niyCKjKpJ93hh5kevdJFvXOvlpX7xuOd4CvFlzaRc7/5d X-Received: by 2002:a17:902:c40a:b0:14a:7fef:981a with SMTP id k10-20020a170902c40a00b0014a7fef981amr5039110plk.156.1642792439847; Fri, 21 Jan 2022 11:13:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642792439; cv=none; d=google.com; s=arc-20160816; b=M9RAzN/XeRCHuIFJ1qfGrjEhCZxlIjwvdw1y5XGNzJp/FjB+U05OyoQnNjfoMdx5R6 ofA6A7GTssSPrpPhUnfPJKCi+ChQsPhVc6nypnhw4xwZ1M3gAD2VYPJQmbkqd3Mr9vcm UXn/EY/NeU47wT9gotIl8dURRq9P8lRTA6pFCpAxNI3S13K2kIOLWIbiHr34DRfjkelF GiMOwn3glZPtkXusGQx6IjNkUZ5cG6PGGX5AkFKSyDioQaC/zjw9MH433ikaqkWI0Ajh vhKUkWrbYWPGzmNSOT2QBjhMyLTnbx5jBeNy0jjzv4Y8dIuk1gt2aGS2i4szWw+Ip2Yd rE/g== 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=9l5XtvzGyjllIeLz5C3AHsPqxjlouor4gcSN+diq9i8=; b=oLkQqDKHfS/e19f9BSW+yadT87KHUKBg2QEeP6a9lfN7msQNgJK1AHW1w/ZvYNLFQW lZzmOD9L7/vheMa26gnvBUchZdnjleVYoUlZO6n8UPduJbRr9NaWPa6m61wgja28/804 Au1ui2mhnmHpsnGZN6Gjhf5WsXm2KWcAjOpmyN4MIZLg6Qf5glRXdODQljhSv+r7G9Sd lMnDGqKEvierduEwrlLDfbMI2/1ahcAFMkzNQW76tBB/uBMlTl+yuZiiBJ4NLYoRiTsZ dEqZWhzp/M83AmBXkJ4TQt8rdQe8YtmGEvP/sBxh1saW0gU5+bCTUn1voCGXxl8dAHpl vEDQ== 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 h4si5746313pls.468.2022.01.21.11.13.47; Fri, 21 Jan 2022 11:13:59 -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 S1354422AbiASMVT (ORCPT + 99 others); Wed, 19 Jan 2022 07:21:19 -0500 Received: from smtp25.cstnet.cn ([159.226.251.25]:43214 "EHLO cstnet.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1354343AbiASMVR (ORCPT ); Wed, 19 Jan 2022 07:21:17 -0500 Received: from localhost.localdomain (unknown [124.16.138.126]) by APP-05 (Coremail) with SMTP id zQCowADHpxQoAuhhdXyFBg--.58592S2; Wed, 19 Jan 2022 20:20:56 +0800 (CST) From: Jiasheng Jiang To: dan.carpenter@oracle.com, keescook@chromium.org, arnd@arndb.de, gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Jiasheng Jiang Subject: [PATCH v2] lkdtm/bugs: Check for the NULL pointer after calling kmalloc Date: Wed, 19 Jan 2022 20:20:55 +0800 Message-Id: <20220119122055.1826561-1-jiasheng@iscas.ac.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: zQCowADHpxQoAuhhdXyF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derInfo: pmld2xxhqjqxpvfd2hldfou0/ Precedence: bulk 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. --- 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; + } pr_info("Array access within bounds ...\n"); /* For both, touch all bytes in the actual member size. */ -- 2.25.1