Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3222160lqp; Tue, 26 Mar 2024 03:17:57 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCX/s+NOlOeghkmxD4cUCiTM4ti3QxFc10j4T9uha+SpOyHZhoElPn2mncixdwE7rUyFem9END9+SvaEMCatPvinlEYF2ScH7qAr+4A0CA== X-Google-Smtp-Source: AGHT+IHoRe0KBC9wcutdDiHj7dGzSu1C8/gRNXyL2qnpBCE/FO1htsr5J6z+5ftmC7y5cxTwrKmb X-Received: by 2002:a05:6a21:3293:b0:1a3:ac75:2890 with SMTP id yt19-20020a056a21329300b001a3ac752890mr11460939pzb.46.1711448277377; Tue, 26 Mar 2024 03:17:57 -0700 (PDT) Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id jv12-20020a170903058c00b001dcb3b6e55csi6845981plb.299.2024.03.26.03.17.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 03:17:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-118731-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@digikod.net header.s=20191114 header.b=WMzokmcs; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-118731-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118731-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 8A546B2474A for ; Tue, 26 Mar 2024 10:00:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 75E524F88C; Tue, 26 Mar 2024 10:00:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="WMzokmcs" Received: from smtp-42a9.mail.infomaniak.ch (smtp-42a9.mail.infomaniak.ch [84.16.66.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 699B15D8E1 for ; Tue, 26 Mar 2024 10:00:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711447237; cv=none; b=gu1MHHJUe20NwH1dklrbAFMqD9K3LUczrrLMRsCuW/uTAVRgh46wSt+2WeQMyTBL72EtFX/cW+YbdmFhWtBGs6dEk9bduOYa6eWsLyhjXWfPq+NBiLaALEbeX03C5ZAX/98oC+RKeXWq/5Tcjbl6NxyRsMH/k2+b7EqOPaFZjmo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711447237; c=relaxed/simple; bh=VwZxvUQPVph+NebLcCZQeB0+ANm/1LnxkgU/PHwfhyE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=d98q/Gfj3cPW2HP0z9WUJlCpBiUliP+0KM+VivYGYdInL2cnq09KAYb0GM3uSqylZb0R0A3xibaqPTk3aXn+eFqQQ36OyiRxWhAQ1DwXYQxJ+FJ5IASriFaBDVvi8IMoLXsrLd/hCnLIXaDyL81tGZjPsybBmmMcl+v1qWoBNBw= 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=WMzokmcs; arc=none smtp.client-ip=84.16.66.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 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 4V3lSZ71BPzbYZ; Tue, 26 Mar 2024 10:51:42 +0100 (CET) Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4V3lSZ2K2bzKQ0; Tue, 26 Mar 2024 10:51:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1711446702; bh=VwZxvUQPVph+NebLcCZQeB0+ANm/1LnxkgU/PHwfhyE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WMzokmcsGZ79WBn7AEb+orlII6XtoyVNZGG1dsY3l5PPHCyfs7cglkNI3nQ7/gLOi I4O5Qyp03FyEMp62eEFw3qMfkIxK6zH0ddlgJdYIB+0XY3mdrqhPAETKBNRtNgHcVx MDVtIaxPUX0WwGBf/Gd4HHn/ioXPHROZVrIlpJmw= 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 7/7] kunit: Add tests for fault Date: Tue, 26 Mar 2024 10:51:18 +0100 Message-ID: <20240326095118.126696-8-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 Add a test case to check NULL pointer dereference and make sure it would result as a failed test. The full kunit_fault test suite is marked as skipped when run on UML because it would result to a kernel panic. Tested with: /tools/testing/kunit/kunit.py run --arch x86_64 kunit_fault /tools/testing/kunit/kunit.py run --arch arm64 \ --cross_compile=aarch64-linux-gnu- kunit_fault Cc: Brendan Higgins Cc: Rae Moar Cc: Shuah Khan Reviewed-by: David Gow Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20240326095118.126696-8-mic@digikod.net --- Changes since v2: * Add David's Reviewed-by. Changes since v1: * Remove the rodata and const test cases for now. * Replace CONFIG_X86 check with !CONFIG_UML, and remove the "_x86" references. --- lib/kunit/kunit-test.c | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index f7980ef236a3..0fdca5fffaec 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -109,6 +109,48 @@ static struct kunit_suite kunit_try_catch_test_suite = { .test_cases = kunit_try_catch_test_cases, }; +#ifndef CONFIG_UML + +static void kunit_test_null_dereference(void *data) +{ + struct kunit *test = data; + int *null = NULL; + + *null = 0; + + KUNIT_FAIL(test, "This line should never be reached\n"); +} + +static void kunit_test_fault_null_dereference(struct kunit *test) +{ + struct kunit_try_catch_test_context *ctx = test->priv; + struct kunit_try_catch *try_catch = ctx->try_catch; + + kunit_try_catch_init(try_catch, + test, + kunit_test_null_dereference, + kunit_test_catch); + kunit_try_catch_run(try_catch, test); + + KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR); + KUNIT_EXPECT_TRUE(test, ctx->function_called); +} + +#endif /* !CONFIG_UML */ + +static struct kunit_case kunit_fault_test_cases[] = { +#ifndef CONFIG_UML + KUNIT_CASE(kunit_test_fault_null_dereference), +#endif /* !CONFIG_UML */ + {} +}; + +static struct kunit_suite kunit_fault_test_suite = { + .name = "kunit_fault", + .init = kunit_try_catch_test_init, + .test_cases = kunit_fault_test_cases, +}; + /* * Context for testing test managed resources * is_resource_initialized is used to test arbitrary resources @@ -826,6 +868,7 @@ static struct kunit_suite kunit_current_test_suite = { kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_log_test_suite, &kunit_status_test_suite, - &kunit_current_test_suite, &kunit_device_test_suite); + &kunit_current_test_suite, &kunit_device_test_suite, + &kunit_fault_test_suite); MODULE_LICENSE("GPL v2"); -- 2.44.0