Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1232116pxb; Fri, 21 Jan 2022 12:56:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJxSnj8odqtkhe9+1qn/t5JRFmx4E/vGKTTnTE0811O8RlIt0vp/vTqVookxbGq90Wl8QGn4 X-Received: by 2002:a17:902:6948:b0:149:f187:e601 with SMTP id k8-20020a170902694800b00149f187e601mr5469427plt.81.1642798561553; Fri, 21 Jan 2022 12:56:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642798561; cv=none; d=google.com; s=arc-20160816; b=Mjo5utE1I0qxM9LNjUntn6UAj3P8Phg7xaptkaGFCHYg3OvPV919NCocHI3NfCQ4hM VWfooCXm/DN1Um/dwaPOkQGDU/0ML0aCerHqqK/iIZLPi7mqKZEd6dp3q+yOg+oIUhmP FqYTUdDld2ggTw52tVNyMTn62oO0nLa6dyjxnBKk590FylJ8XFv/sYfy1WufU0ptA4UH BM3ySCefwSWn0Tz3y8CIt+HxbOusUK3267ZRX/P7i8pNlfrhvKkv2ss0ThvxCz9rMfEa J4D6ElMX46uJ6w1D6yke4srXtR0jqnB3yCmLjs8w/Qq+zu7VyY3DPpaf76FDjmEX7Vsn elqw== 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=Xbkq+294tbMc/G1Suazc0rN4C+OkIJV1YnSlmRsC4kc=; b=Atwd92sR2zyos82nnO14EI6wdU9QpiebITVAzweQfREeAkh/4500SR9EOfOvyXtDfH yZR26QwuuJpIIu2yLu6Gg6Uu82vRe2OVqLGoQuyheP8a6CwfigyBb/GAFrUK3HDNr8hM cBLbQJHmuHsYM71T6sdNLYg/HRAN2Q4DSkJoTyJslUGVF+2u6UIRWtxe2oyGfVbxIAJh XgRvIryZ+On+35Rbztft2/ZD70kKFn05yUrIiPZhtlCSDu6YRJl34scADL1p3YBHEwhq DJUKWK5HqGLj7X8f4/B7X5W5iXWrNIJq5Bvw7S17/BTYjYh+nqdOLKVzaT/PHQ5Asfms V82A== 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 x14si7638862plg.176.2022.01.21.12.55.49; Fri, 21 Jan 2022 12:56:01 -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 S1358093AbiATB0R (ORCPT + 99 others); Wed, 19 Jan 2022 20:26:17 -0500 Received: from smtp23.cstnet.cn ([159.226.251.23]:55102 "EHLO cstnet.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1358092AbiATB0P (ORCPT ); Wed, 19 Jan 2022 20:26:15 -0500 Received: from localhost.localdomain (unknown [124.16.138.126]) by APP-03 (Coremail) with SMTP id rQCowAB3fS0iuuhhb47FBQ--.44891S2; Thu, 20 Jan 2022 09:25:54 +0800 (CST) From: Jiasheng Jiang To: keescook@chromium.org Cc: dan.carpenter@oracle.com, arnd@arndb.de, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, Jiasheng Jiang Subject: [PATCH v3] lkdtm/bugs: Check for the NULL pointer after calling kmalloc Date: Thu, 20 Jan 2022 09:25:51 +0800 Message-Id: <20220120012552.1851621-1-jiasheng@iscas.ac.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: rQCowAB3fS0iuuhhb47FBQ--.44891S2 X-Coremail-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: 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 explicitly yell about the memory failure and then 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. --- drivers/misc/lkdtm/bugs.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/misc/lkdtm/bugs.c b/drivers/misc/lkdtm/bugs.c index f4cb94a9aa9c..c64ab6f387eb 100644 --- a/drivers/misc/lkdtm/bugs.c +++ b/drivers/misc/lkdtm/bugs.c @@ -325,6 +325,12 @@ 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) { + pr_err("FAIL: could not allocate required buffers!\n"); + 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