Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp652836lqs; Tue, 5 Mar 2024 12:11:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUoPDd6PACsQVoiWnwZR1a7So07nLGHMilw6/KnE/qUSLz+YVaUOSIrYue/h9HPdm70K6T5xR13NvPUH3z5Oey2UACHGietpaD2RocYQA== X-Google-Smtp-Source: AGHT+IFCrE1L0mgjlGSRe8jZYgzpCAcMPnaVqXoZ1kBQy+g44BNUbraI78u325TUTUd/ds5iXIwH X-Received: by 2002:a17:907:1709:b0:a45:7f4:666d with SMTP id le9-20020a170907170900b00a4507f4666dmr3033654ejc.38.1709669468329; Tue, 05 Mar 2024 12:11:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709669468; cv=pass; d=google.com; s=arc-20160816; b=nM5PH84V0Pmximt24W+EaJY2uN9pZs7OShdsb8COGAegXCuFPGEenVzsDfIVgbNIE4 oeFvDNpnBk3F3qezCFiiaNQ1bnKk0SkCT8NeCs8al/fK+RpzfCCo3PkKmnK0Cnc2ZrXk EHOynqKSeoEzKw/x24YUFCzdXLanF5vokfi7sDHgs7j7iHJXk6PJjb6j/mWAHXuPTQyg gIDBeqhCl0S7ahM0DRPcow6C9iOI52GZHRvuihYw0ko9eAy2KynsPz425EDO7nW3I+N2 B+nnWcqm2A+XgnjeK46W4h+/v3AXEuU6NUXsKUSx5j4o0fm33wumNNhj7OfjJDCdLAO4 +F8w== 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=wd16TVgn2lu9FwLlGVDUakTxiNy+3J+pw+rk5tJ667o=; fh=b6/Jr5KWzbVMQcZHY9xxepz/HH7Yi8eyKI3p6Rm8/KQ=; b=nFWtVVzDnYifV5R0wyluubaekjG7TeH3czbIGrsY5TWZkAobgKvSyqiwDfKSsT0ULA Eh4CwwktRYrDQ75n49gUQEx0ZJZrl5gMI6ygbu4MfAMAMW/ZqXi6x5S1FkhoB2UlVFbB NlvVM9DxJI4L7xpcbZ75Kg7clUeaaLXjIST3Cv7rwE8YNt3hOjpg1+BvN8bstwqFsN4f ze9aGVHrSTR/g+7UM9xowsNcqgHMe8fTmmXYOEdkaDSPgY4arMQWVVTApWSo91Q0Ovt8 YVwPJS4jSJnWfPcg8NJYfs+qZY91iVkFQVruBi6tlDPjlZp886IsxnN4gcXi/FplbLTZ KZEQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=iqRjI1Dy; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-92938-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92938-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 o21-20020a17090611d500b00a4470550550si4594453eja.601.2024.03.05.12.11.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 12:11:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-92938-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=iqRjI1Dy; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-92938-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92938-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 E14B51F2ABBE for ; Tue, 5 Mar 2024 20:11:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0AFD12838E; Tue, 5 Mar 2024 20:11:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="iqRjI1Dy" Received: from smtp-42ae.mail.infomaniak.ch (smtp-42ae.mail.infomaniak.ch [84.16.66.174]) (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 ACF1C17BDC for ; Tue, 5 Mar 2024 20:10:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.16.66.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709669459; cv=none; b=FooYLf1Na/Al/955Wx0znzZ0RHrKPkw322lHONdt8OjyXnsckUTxeGxjW4IKsN9NMsmIB2F9Tf+NTHjIkcnFl7ptRcvoljIxFEPAkz6UTvpzaNzwN5UHK0fJOngCPNs6l4aRReV+x5VZcrP3zF8ZhzwlSaHkux7wCjpeeSujW+M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709669459; c=relaxed/simple; bh=YagDFFA6rbmtQARJcRghj4PIMIy8y9znN8rxiNy6FWo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=n8MR2zq30FhsXnZIleYovW/a7dttQq0UDBGHS4txKDNJynf85kBrk+/AMBplEP8SqhaBXhTu4tzflhYM5ANGLSL+CHs7echovjukGfl0vv10CKuVek0oWxWUzXyJr2eXnj5kjHWu7RRXRUI1EBgvji8neM6eFKCzzDm/YgF3+bo= 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=iqRjI1Dy; arc=none smtp.client-ip=84.16.66.174 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-3-0000.mail.infomaniak.ch (unknown [10.4.36.107]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4Tq6Bh3hBzzMqFcJ; Tue, 5 Mar 2024 21:10:52 +0100 (CET) Received: from unknown by smtp-3-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4Tq6Bg4Rt6z3W; Tue, 5 Mar 2024 21:10:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1709669452; bh=YagDFFA6rbmtQARJcRghj4PIMIy8y9znN8rxiNy6FWo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iqRjI1Dy24DR8Hdurm3d3isbc56yHWvmpEKMfRgZ0QEGyvKHer+cdIGDKvDQeYKas 11oLvI7VsJYOxwcNZUAiNi8sfJJyapxPmMqHGWJCPMgb11x7Wk2vDMQCoG++3Tdt3x wtP9I51i9h8vpR3tiipkGYG2oxtCb/Z4Pq8eEIls= From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: "David S . Miller" , Jakub Kicinski , Kees Cook , Mark Brown , Shuah Khan Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , =?UTF-8?q?G=C3=BCnther=20Noack?= , Will Drewry , edumazet@google.com, jakub@cloudflare.com, pabeni@redhat.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-security-module@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH] selftests/harness: Fix TEST_F()'s vfork handling Date: Tue, 5 Mar 2024 21:10:29 +0100 Message-ID: <20240305201029.1331333-1-mic@digikod.net> In-Reply-To: <20240305.sheeF9yain1O@digikod.net> References: <20240305.sheeF9yain1O@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 Always run fixture setup in the grandchild process, and by default also run the teardown in the same process. However, this change makes it possible to run the teardown in a parent process when _metadata->teardown_parent is set to true (e.g. in fixture setup). Fix TEST_SIGNAL() by forwarding grandchild's signal to its parent. Fix seccomp tests by running the test setup in the parent of the test thread, as expected by the related test code. Fix Landlock tests by waiting for the grandchild before processing _metadata. Use of exit(3) in tests should be OK because the environment in which the vfork(2) call happen is already dedicated to the running test (with flushed stdio, setpgrp() call), see __run_test() and the call to fork(2) just before running the setup/test/teardown. Even if the test configures its own exit handlers, they will not be run by the parent because it never calls exit(3), and the test function either ends with a call to _exit(2) or a signal. Cc: David S. Miller Cc: Günther Noack Cc: Jakub Kicinski Cc: Kees Cook Cc: Mark Brown Cc: Shuah Khan Cc: Will Drewry Fixes: 0710a1a73fb4 ("selftests/harness: Merge TEST_F_FORK() into TEST_F()") Link: https://lore.kernel.org/r/20240305201029.1331333-1-mic@digikod.net --- tools/testing/selftests/kselftest_harness.h | 28 +++++++++++++-------- tools/testing/selftests/landlock/fs_test.c | 22 ++++++++-------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h index 634be793ad58..4fd735e48ee7 100644 --- a/tools/testing/selftests/kselftest_harness.h +++ b/tools/testing/selftests/kselftest_harness.h @@ -382,29 +382,33 @@ /* fixture data is alloced, setup, and torn down per call. */ \ FIXTURE_DATA(fixture_name) self; \ pid_t child = 1; \ + int status = 0; \ memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \ if (setjmp(_metadata->env) == 0) { \ - fixture_name##_setup(_metadata, &self, variant->data); \ - /* Let setup failure terminate early. */ \ - if (_metadata->exit_code) \ - return; \ - _metadata->setup_completed = true; \ /* Use the same _metadata. */ \ child = vfork(); \ if (child == 0) { \ + fixture_name##_setup(_metadata, &self, variant->data); \ + /* Let setup failure terminate early. */ \ + if (_metadata->exit_code) \ + _exit(0); \ + _metadata->setup_completed = true; \ fixture_name##_##test_name(_metadata, &self, variant->data); \ - _exit(0); \ - } \ - if (child < 0) { \ + } else if (child < 0 || child != waitpid(child, &status, 0)) { \ ksft_print_msg("ERROR SPAWNING TEST GRANDCHILD\n"); \ _metadata->exit_code = KSFT_FAIL; \ } \ } \ - if (child == 0) \ - /* Child failed and updated the shared _metadata. */ \ + if (child == 0) { \ + if (_metadata->setup_completed && !_metadata->teardown_parent) \ + fixture_name##_teardown(_metadata, &self, variant->data); \ _exit(0); \ - if (_metadata->setup_completed) \ + } \ + if (_metadata->setup_completed && _metadata->teardown_parent) \ fixture_name##_teardown(_metadata, &self, variant->data); \ + if (!WIFEXITED(status) && WIFSIGNALED(status)) \ + /* Forward signal to __wait_for_test(). */ \ + kill(getpid(), WTERMSIG(status)); \ __test_check_assert(_metadata); \ } \ static struct __test_metadata \ @@ -414,6 +418,7 @@ .fixture = &_##fixture_name##_fixture_object, \ .termsig = signal, \ .timeout = tmout, \ + .teardown_parent = false, \ }; \ static void __attribute__((constructor)) \ _register_##fixture_name##_##test_name(void) \ @@ -873,6 +878,7 @@ struct __test_metadata { bool timed_out; /* did this test timeout instead of exiting? */ bool aborted; /* stopped test due to failed ASSERT */ bool setup_completed; /* did setup finish? */ + bool teardown_parent; /* run teardown in a parent process */ jmp_buf env; /* for exiting out of test early */ struct __test_results *results; struct __test_metadata *prev, *next; diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c index 98817a14c91b..9a6036fbf289 100644 --- a/tools/testing/selftests/landlock/fs_test.c +++ b/tools/testing/selftests/landlock/fs_test.c @@ -285,6 +285,8 @@ static void prepare_layout_opt(struct __test_metadata *const _metadata, static void prepare_layout(struct __test_metadata *const _metadata) { + _metadata->teardown_parent = true; + prepare_layout_opt(_metadata, &mnt_tmp); } @@ -3861,9 +3863,7 @@ FIXTURE_SETUP(layout1_bind) FIXTURE_TEARDOWN(layout1_bind) { - set_cap(_metadata, CAP_SYS_ADMIN); - EXPECT_EQ(0, umount(dir_s2d2)); - clear_cap(_metadata, CAP_SYS_ADMIN); + /* umount(dir_s2d2)) is handled by namespace lifetime. */ remove_layout1(_metadata); @@ -4276,9 +4276,8 @@ FIXTURE_TEARDOWN(layout2_overlay) EXPECT_EQ(0, remove_path(lower_fl1)); EXPECT_EQ(0, remove_path(lower_do1_fo2)); EXPECT_EQ(0, remove_path(lower_fo1)); - set_cap(_metadata, CAP_SYS_ADMIN); - EXPECT_EQ(0, umount(LOWER_BASE)); - clear_cap(_metadata, CAP_SYS_ADMIN); + + /* umount(LOWER_BASE)) is handled by namespace lifetime. */ EXPECT_EQ(0, remove_path(LOWER_BASE)); EXPECT_EQ(0, remove_path(upper_do1_fu3)); @@ -4287,14 +4286,11 @@ FIXTURE_TEARDOWN(layout2_overlay) EXPECT_EQ(0, remove_path(upper_do1_fo2)); EXPECT_EQ(0, remove_path(upper_fo1)); EXPECT_EQ(0, remove_path(UPPER_WORK "/work")); - set_cap(_metadata, CAP_SYS_ADMIN); - EXPECT_EQ(0, umount(UPPER_BASE)); - clear_cap(_metadata, CAP_SYS_ADMIN); + + /* umount(UPPER_BASE)) is handled by namespace lifetime. */ EXPECT_EQ(0, remove_path(UPPER_BASE)); - set_cap(_metadata, CAP_SYS_ADMIN); - EXPECT_EQ(0, umount(MERGE_DATA)); - clear_cap(_metadata, CAP_SYS_ADMIN); + /* umount(MERGE_DATA)) is handled by namespace lifetime. */ EXPECT_EQ(0, remove_path(MERGE_DATA)); cleanup_layout(_metadata); @@ -4691,6 +4687,8 @@ FIXTURE_SETUP(layout3_fs) SKIP(return, "this filesystem is not supported (setup)"); } + _metadata->teardown_parent = true; + slash = strrchr(variant->file_path, '/'); ASSERT_NE(slash, NULL); dir_len = (size_t)slash - (size_t)variant->file_path; -- 2.44.0