From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D99FC2BB43 for ; Mon, 15 Aug 2022 21:18:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244498AbiHOVQ3 (ORCPT ); Mon, 15 Aug 2022 17:16:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348014AbiHOVH4 (ORCPT ); Mon, 15 Aug 2022 17:07:56 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4CAC2DABE; Mon, 15 Aug 2022 12:17:47 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A48C7B8107A; Mon, 15 Aug 2022 19:17:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA626C433D6; Mon, 15 Aug 2022 19:17:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660591065; bh=4Ad6WHw+VEo7Yt24zjkg9al8JR/d0E217Ezuc9hpe3s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Amv5AsxWYHSfZ87qjf5Zz6+mUd+BWMF4HikuKyt9IgpFGS3/Mo6h7v9nFFYJgeKJ6 pl7H+WdFpChSQqNL9BDdtcVkH7O81Du93YEWCrQbgHfMSSc8fZlGO28XjhQVhxlspH 6J+294X4cDmLRtf/2wZTYBE2LQbvNl3MLKrXqnDc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, kernel test robot , kernel test robot , Dan Carpenter , Daniel Latypov , Brendan Higgins , David Gow , Shuah Khan , Sasha Levin Subject: [PATCH 5.18 0459/1095] kunit: executor: Fix a memory leak on failure in kunit_filter_tests Date: Mon, 15 Aug 2022 19:57:38 +0200 Message-Id: <20220815180448.591050196@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Gow [ Upstream commit 94681e289bf5d10c9db9db143d1a22d8717205c5 ] It's possible that memory allocation for 'filtered' will fail, but for the copy of the suite to succeed. In this case, the copy could be leaked. Properly free 'copy' in the error case for the allocation of 'filtered' failing. Note that there may also have been a similar issue in kunit_filter_subsuites, before it was removed in "kunit: flatten kunit_suite*** to kunit_suite** in .kunit_test_suites". This was reported by clang-analyzer via the kernel test robot, here: https://lore.kernel.org/all/c8073b8e-7b9e-0830-4177-87c12f16349c@intel.com/ And by smatch via Dan Carpenter and the kernel test robot: https://lore.kernel.org/all/202207101328.ASjx88yj-lkp@intel.com/ Fixes: a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM") Reported-by: kernel test robot Reported-by: kernel test robot Reported-by: Dan Carpenter Reviewed-by: Daniel Latypov Reviewed-by: Brendan Higgins Signed-off-by: David Gow Signed-off-by: Shuah Khan Signed-off-by: Sasha Levin --- lib/kunit/executor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c index 96f96e42ce06..16fb88c0aca3 100644 --- a/lib/kunit/executor.c +++ b/lib/kunit/executor.c @@ -76,8 +76,10 @@ kunit_filter_tests(struct kunit_suite *const suite, const char *test_glob) memcpy(copy, suite, sizeof(*copy)); filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL); - if (!filtered) + if (!filtered) { + kfree(copy); return ERR_PTR(-ENOMEM); + } n = 0; kunit_suite_for_each_test_case(suite, test_case) { -- 2.35.1