Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp508211lqg; Fri, 1 Mar 2024 11:42:22 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWVvszV4Zq6CCBZsmLuC0dWpxoYyE26HH4hFNxpkk0HnQG8R2G6rpsPFoEY/Nj5fqqEljCTUYGV0tDrN8HgRZGeEAwKAAt3nB27vRLzzg== X-Google-Smtp-Source: AGHT+IHz0p/cZQZWiwlacXDHZoHjyAbHNUTyUiD5RChPuAHbGqGQLJhhtCXsXZskUdaTisVPsNn4 X-Received: by 2002:a0c:e302:0:b0:690:4f40:86a2 with SMTP id s2-20020a0ce302000000b006904f4086a2mr2545198qvl.7.1709322141954; Fri, 01 Mar 2024 11:42:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709322141; cv=pass; d=google.com; s=arc-20160816; b=vsdusXuhuAgTpBGR5zsMP/uO34GZZ289p1z+Oyup0I9APkJPl+ETzKyVcUymTZ1b75 mzBHCbVlddXSVV8BdkVau20ut4uwBnTI96u87En5McyRZzF7IKo+FyV11+W7wxgid5Pw ZLRuwvzqCFfUMzSD0/jaghufo2wMO6kYl6NEaCmnzL8JcXrae3fFlJ1XdJIb7Le26W5k rbtys7jIdIwXN8/dO7V9FHlkj6bbltut+cSDKcWdmNMoJRMsvE5QNXR0JPldeyPjYv1E r4vGDPFBmjxdVpGzkbf1wUSxNX4HTQRXFokPeIgycEAlwiILrlEyE55JMdt7uA+mnF5L heLQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=g/YzloGagxw07ZqEzYYosPxBme8ECoh1NNB7drVcOys=; fh=MR/wbEn34JHmT+rtzWNq5RVU/RKi8DhjSvb3ywiuLkE=; b=D/xkjVjk2Im//+46aNKpF1LpFc9KI958bY5jCte0ZUNyNSq9PFC1AMIllJCJCbaGOG 4GvDoxgRakwGm9I3lYChJkSAO/hCMHNVSdt9gR1ISuy1lK8Yhb2ghWBD277IAdTOo20x vG0SpmDxO2YKRT5TT+7T+0bWcAmswiiUQ/+CuLDi3vypa77e/v4LMFH4NQqTAy6AyqSl 7tzZttxhe0oDWj6d8jz8LMyW2XDbGnEw61v+NnytSuYcXONpOwEyLwG3lmQek3nPBdLI P+lTfNE75/U0hCYz5UOK8A0TCMPJ6522KvVCLnG7O3EV42Nyasb0yPT0rE6d34r7wsPi 2ijQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=UNYZ0xJK; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-89040-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89040-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ow19-20020a0562143f9300b0068efd7caaabsi4089175qvb.105.2024.03.01.11.42.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 11:42:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-89040-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=UNYZ0xJK; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-89040-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89040-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DB23C1C25C32 for ; Fri, 1 Mar 2024 19:41:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB38F3C48E; Fri, 1 Mar 2024 19:40:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="UNYZ0xJK" Received: from smtp-bc0f.mail.infomaniak.ch (smtp-bc0f.mail.infomaniak.ch [45.157.188.15]) (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 A7C493B2A1 for ; Fri, 1 Mar 2024 19:40:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709322055; cv=none; b=cPHEd96+5EI2VoxC+D+KBNc3kh1DvroOIz+g9BoVZVTixKTQSsI3+Q6Jb5cUabXtR2FNhEEGn99oJblllarfvueRkga0E1g9t3r2y70vHgh+G8Fvs+Opl+7aE3I7DjDmakNS7NsMCWeX1LNcr5nNewhQg4qdMA7kxjy8QOmKxUM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709322055; c=relaxed/simple; bh=aBipZnzNTUCokDElM7UYa9F0lGpvUK219Za2n6GQq4Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ARlJXRfv1+1qxlOSmFpG4yVX+QnOMW1cAq8SUM8FxDsFscXoMHoEFjs1OhRApimK9y8D9g1diXIdZ6kIvW20gmWHB45tQAdjTPKnIl03V6z4VDL39PASLQfolWJopsNxUvGcwDWp73LchCejtQ4hKGLJhHIMJccm9LXQpFeF1vg= 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=UNYZ0xJK; arc=none smtp.client-ip=45.157.188.15 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 Received: from smtp-4-0001.mail.infomaniak.ch (smtp-4-0001.mail.infomaniak.ch [10.7.10.108]) by smtp-4-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4Tmdjw0F1Nznjb; Fri, 1 Mar 2024 20:40:52 +0100 (CET) Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4Tmdjv1xJGznyy; Fri, 1 Mar 2024 20:40:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1709322051; bh=aBipZnzNTUCokDElM7UYa9F0lGpvUK219Za2n6GQq4Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UNYZ0xJKl83Weh0HZXkX+75DtgbucsYUYb3GwJDzqtVbcYnTvcggNkhy0ioW4MMSZ fnerYo+ZLufmIhR0vKm5k5WdS3vITUXQbsgjc2ISrEzQNh0TIdOvkp4cGNI+MRf3Ur LJCtGilHG9y8nSgLg8D8vSJX5kJXlQ0ztX1gQWyU= From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Brendan Higgins , David Gow , Kees Cook , Rae Moar , Shuah Khan Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Alan Maguire , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Ingo Molnar , James Morris , Luis Chamberlain , "Madhavan T . Venkataraman" , Marco Pagani , Paolo Bonzini , Sean Christopherson , Stephen Boyd , Thara Gopinath , Thomas Gleixner , Vitaly Kuznetsov , Wanpeng Li , Zahra Tarkhani , kvm@vger.kernel.org, linux-hardening@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, linux-um@lists.infradead.org, x86@kernel.org Subject: [PATCH v2 2/7] kunit: Fix kthread reference Date: Fri, 1 Mar 2024 20:40:32 +0100 Message-ID: <20240301194037.532117-3-mic@digikod.net> In-Reply-To: <20240301194037.532117-1-mic@digikod.net> References: <20240301194037.532117-1-mic@digikod.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: David Gow Cc: Rae Moar Cc: Shuah Khan Reviewed-by: Kees Cook Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20240301194037.532117-3-mic@digikod.net --- Changes since v1: * Added 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) -- 2.44.0