Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3210101lqp; Tue, 26 Mar 2024 02:53:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU9ZXK8h52yd9HYoNs4A72Xq4tazzAhms/35Qg/GRdyvCKM1bDdAiCvFLrdVmwvPeN6S65PGl9UlD78ubdQIk2GY0ZpOLQj3m2lTThOfA== X-Google-Smtp-Source: AGHT+IFKyyW8NVC0mjhj3dugfRwJBl6oz/uMu2yMBXjfvlETUthvGhMThIm8sYrSDWdq39/6cxoS X-Received: by 2002:a17:906:7d51:b0:a45:f352:73b0 with SMTP id l17-20020a1709067d5100b00a45f35273b0mr6979217ejp.65.1711446784882; Tue, 26 Mar 2024 02:53:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711446784; cv=pass; d=google.com; s=arc-20160816; b=GTtouATuGVaLV69inw2E++/wg5DP1Pjq3sCJqSvFtLo0sApgV8HUP6QBo2dSgPsP40 Wb8HUhq8WVZEtYnnP8BT8bOJZeak74O1Py9BHH7twhiOd61JbOuQSBCMsw8hfphCFPt7 5hG+AhELzewNRylaXuko6MEQ+VtDBLrjS3I9NErhx1xx4PaKtE3oq9z8NvuzWmKR6s5k GgBuiHlpq3ljtga29unFhp0SwMsH7O1BRTsmZdguHoBN73A5em460FnopzOS/8Jeratv Fb8H3VmAXLrZv2VD63927E3PLLeqwUObWOQjGzpTQ/Y4GV+Lab+uObRb81xPIxHUz5fD vjUA== 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=e6awg/8K1Xi+mqR//7IBqBOHFuznw79ZK+S7TPwk0wM=; fh=onDSeV07aUbQUxh6ZRJssLrvo0jc0O2SovuD7/EFOxc=; b=o9z8hc2skG2LK3vDz1ukdTBiTwRZOrmCzSRAmqLbhQCaMmb2gC6kWgViFGRe/Vq48Z L58pDbcE0gKpo8jm+h+yy1yGjrgrMHWEuix/TTxTOnnT+NxrDk8ecX2FNEKR/oPythln BDJGy7hZl27evO0FqVx46OoWhUzk2dXqCBgikG8unL2GptIrRiyCZaAb48O341j3UMKu DPTt1Z0lWCjxl7iI/Iof2jj+nyB+ZQgH2ineYgkugMVn2Ro/js9j82TbjnJd3M9UDBhs iK9xwKSPW+iqwoZ7E3SUUoOHsyv6a0egh3yQgRaYEYD/ukb8hYnBoL91htO+gqRgOKQK T2Ow==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=lcTiXieD; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-118718-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118718-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u1-20020a17090663c100b00a46aaf6be5asi3303799ejk.589.2024.03.26.02.53.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 02:53:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-118718-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=lcTiXieD; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-118718-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118718-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 6CDE21F28672 for ; Tue, 26 Mar 2024 09:53:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B5BEC7175A; Tue, 26 Mar 2024 09:51:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="lcTiXieD" Received: from smtp-1909.mail.infomaniak.ch (smtp-1909.mail.infomaniak.ch [185.125.25.9]) (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 D117F5A0F9; Tue, 26 Mar 2024 09:51:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.25.9 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711446707; cv=none; b=fA6qtmz+bnwAT+aRB3GZDn6QuPMrvqfFhXU9D54K109+/OY78XXaOxuq4aH9PNUuhsNTcL83/4AgIzkFMyxXNoH40fPxu0O1lnCIyISAvI/W0E+SepxiVoLJwOa/8M+Mul6EqvGz2bWhvecEvCnJfonpUalo8+ZTHSpbiTdPrE4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711446707; c=relaxed/simple; bh=pBMis6waGtJRvazqv7DmqZ/uoc/dd8l3LTTdLpiJnSE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PYRQRtm+hXBTQd1dIBqzaQ+38mqlKBkZ6mZ4klYv09yux954i0eQXBYoM+XMMg3ptQFLrG+2VuLK6HQgYahtrls1PLqXIHNosg6m9iO3wkue+S00KbMJSzhtiWW/WDGS9GWyMuI3YIejTCLPn0DGKVh0YVEgK0RV7Ph5ufu07A8= 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=lcTiXieD; arc=none smtp.client-ip=185.125.25.9 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 4V3lST28FvzNkr; Tue, 26 Mar 2024 10:51:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1711446697; bh=pBMis6waGtJRvazqv7DmqZ/uoc/dd8l3LTTdLpiJnSE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lcTiXieDNEC8hrQOmhiQwYuzDC/abUeUVkm+eGtlEKIbs1DqKddPBomjStsLx5Pof +GBBH894Fathn8a8uU0APDC9BeGyyIuewqaT5TjDoEJ8faSPu6yb5HQvClE1zGYZwF rSYthP6yna4iQmxh7yqy/TZhYY1Oi2PuTwNzpp1U= Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4V3lSS4QkWzJRS; Tue, 26 Mar 2024 10:51:36 +0100 (CET) From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Brendan Higgins , David Gow , Rae Moar , Shuah Khan Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=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 , 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 v4 2/7] kunit: Fix kthread reference Date: Tue, 26 Mar 2024 10:51:13 +0100 Message-ID: <20240326095118.126696-3-mic@digikod.net> In-Reply-To: <20240326095118.126696-1-mic@digikod.net> References: <20240326095118.126696-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: 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/20240326095118.126696-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) -- 2.44.0