From patchwork Mon Apr 8 07:46:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 13620704 Received: from smtp-8fa9.mail.infomaniak.ch (smtp-8fa9.mail.infomaniak.ch [83.166.143.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 391F838DEC for ; Mon, 8 Apr 2024 07:55:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712562938; cv=none; b=Un2If4FlOM/YYNgasYweHRAv7amuU9b2VcTncBt5dE5RE7mFm1Z0giQN6fk+NIhDyEUQocNFNlw5mAwMuarZe2YnFQcxIrUwqwRiOrGk57tUobxXG94LGkvUhRxAG4BZ5mOypZaXPJzFFryylWyiJ41gMbsP2tCJQW85qEXrH18= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712562938; c=relaxed/simple; bh=6Iw+PQ6mBt8QbMOSfNH3ppo/54xw6BUauZjHO04urRI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tfTS4eTdQsd5u7o2PwjBlXWm4S69RrvQDD7WqGzzIG/PWo8JdB1CTr5yqgzUk0DAaoLQwIYq55Z2cIuArsUpNxzA6kEJPVOquGXJwNQb1IedRUYr/rRNu2VyIHvjySN4C2hzZfM6S/hWRVwSlmJvpI1Ctj6tdpQd+5alXbzyF2A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net; spf=pass smtp.mailfrom=digikod.net; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b=WKQv+p9G; arc=none smtp.client-ip=83.166.143.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=digikod.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=digikod.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="WKQv+p9G" Received: from smtp-4-0000.mail.infomaniak.ch (smtp-4-0000.mail.infomaniak.ch [10.7.10.107]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4VCh4Y74BQzMSk; Mon, 8 Apr 2024 09:46:53 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1712562413; bh=6Iw+PQ6mBt8QbMOSfNH3ppo/54xw6BUauZjHO04urRI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WKQv+p9Gk0jxfFem0q2/Gur7IS1MQ/zci0LgHJAv+K5Jne10lQtxjdfBNDJSYI0W1 KMtoG6WxjA9B84AXvaOuYxgrN4okbGPiqDMUz34Zylk+Okiu+N1tNn5qgKN2J9zYXy tlZdAqyj7rZY+x2Y5qRp5DxKi9SsCd99f66pypew= Received: from unknown by smtp-4-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4VCh4Y1Dz0zgsN; Mon, 8 Apr 2024 09:46:53 +0200 (CEST) From: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= To: Brendan Higgins , David Gow , Rae Moar , Shuah Khan Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , Alan Maguire , Borislav Petkov , Dave Hansen , "Eric W . Biederman" , "H . Peter Anvin" , Ingo Molnar , James Morris , Kees Cook , Luis Chamberlain , "Madhavan T . Venkataraman" , Marco Pagani , Paolo Bonzini , Sean Christopherson , Stephen Boyd , Thara Gopinath , Thomas Gleixner , Vitaly Kuznetsov , Zahra Tarkhani , kunit-dev@googlegroups.com, kvm@vger.kernel.org, linux-hardening@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-security-module@vger.kernel.org, linux-um@lists.infradead.org, x86@kernel.org Subject: [PATCH v4 RESEND 2/7] kunit: Fix kthread reference Date: Mon, 8 Apr 2024 09:46:20 +0200 Message-ID: <20240408074625.65017-3-mic@digikod.net> In-Reply-To: <20240408074625.65017-1-mic@digikod.net> References: <20240408074625.65017-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Infomaniak-Routing: alpha There is a race condition when a kthread finishes after the deadline and before the call to kthread_stop(), which may lead to use after free. Cc: Brendan Higgins Cc: Shuah Khan Reviewed-by: Kees Cook Fixes: adf505457032 ("kunit: fix UAF when run kfence test case test_gfpzero") Reviewed-by: David Gow Reviewed-by: Rae Moar Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20240408074625.65017-3-mic@digikod.net --- Changes since v2: * Add Fixes tag as suggested by David. * Add David's and Rae's Reviewed-by. Changes since v1: * Add Kees's Reviewed-by. --- lib/kunit/try-catch.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c index a5cb2ef70a25..73f5007f20ea 100644 --- a/lib/kunit/try-catch.c +++ b/lib/kunit/try-catch.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "try-catch-impl.h" @@ -65,14 +66,15 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) try_catch->context = context; try_catch->try_completion = &try_completion; try_catch->try_result = 0; - task_struct = kthread_run(kunit_generic_run_threadfn_adapter, - try_catch, - "kunit_try_catch_thread"); + task_struct = kthread_create(kunit_generic_run_threadfn_adapter, + try_catch, "kunit_try_catch_thread"); if (IS_ERR(task_struct)) { try_catch->try_result = PTR_ERR(task_struct); try_catch->catch(try_catch->context); return; } + get_task_struct(task_struct); + wake_up_process(task_struct); time_remaining = wait_for_completion_timeout(&try_completion, kunit_test_timeout()); @@ -82,6 +84,7 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) kthread_stop(task_struct); } + put_task_struct(task_struct); exit_code = try_catch->try_result; if (!exit_code)