Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1343344lqa; Mon, 29 Apr 2024 06:09:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXoHzXu1JhdMOOxOd3pSxVMtKTN1ezU5X4ZvWkCRiiEr1Ga9/DN6dIRIoVN7799iEsFnAAY4bRqXGHcai/XYFgR3jb5KlbDhm58IO2FuA== X-Google-Smtp-Source: AGHT+IGuIGTffDrjU0bStWtKW4HrNQCA19KpNgyQLMF9uQZbVh39kAczQrawr+EsWiBq78HZT+2g X-Received: by 2002:a17:906:1759:b0:a55:b9c6:863 with SMTP id d25-20020a170906175900b00a55b9c60863mr6739991eje.72.1714396198214; Mon, 29 Apr 2024 06:09:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714396198; cv=pass; d=google.com; s=arc-20160816; b=R0Dl+wfOJl82i9elYhaIk/xURB5p6xAc7GMc5ak51pPiVVMpnFGq6F4b+aCQYR4gLv VaAJ6wJhBloPGJTKvfgLb42NbzduIyTLLQcbL8XxpjyffSTcBn5L3T/pvMlAgUH/t251 JOQpcpE9WCXD5jBplPRJcmSavyT6zCNyfu0YagKoLtgTi/Tnk4uYCh2M7y0dpNUVR5PQ GE8h/qA7I/7RsazK/xqsSmhM1SVK+zThfAqTI8J/mZY574uObYGdP0gtVa+rvcqbuLL9 9uqQhv4iO9jMauVjI1ZR+4WTdmDIODPrz9BOlDdfmaiRPJkQqivfCxKG3ahtXePNJADd U75A== 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=urkmiVOMOqNCmekINWle0wq5mHEqJBokwvfQcTI8P9E=; fh=T/ngxZ/rchirObc+ozpAMGxUCKvTk1uHXxRS2gfk1OI=; b=bYP5z9ytAhklFhxAKcTx8U7Mp1PRlC/M8M3QWuCaza+WazVYmCJ134u85lMPgMw64V akhpoMOW2oAYQAXfWCUWwH9IoaWPgEWAbPmYydPhP4d9FmtRQkzvTkpnAyR3DJ8AK3Qo a50DF3HYH/LgZkZkHMvOLxoiLW0/Odx1PdmIGbdDMnKtXlJoUce2f65Mju6CZ8QFGh+y 2rB/yKf86XhQUL5RaqZcYKLoDVny+vQBZDFaWW3NZiK5i6LVE1bp/G40jo4k3faC9sgK D3dWQQnLm6su4Ia2yyXokve/LguZd6L53ANcs38v9A3q7hHmfjtyfuatVOLPiBxZ3jeZ IBRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=0xkNle6Q; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-162328-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162328-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id sh12-20020a1709076e8c00b00a526ae70bcdsi14500344ejc.738.2024.04.29.06.09.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 06:09:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-162328-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=0xkNle6Q; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-162328-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-162328-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 91E231F21C4C for ; Mon, 29 Apr 2024 13:09:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9383164CC0; Mon, 29 Apr 2024 13:09:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="0xkNle6Q" 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 9437AC127 for ; Mon, 29 Apr 2024 13:09:45 +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=1714396188; cv=none; b=eExg86pyQweGSNBKs9oE4r6ntKWAzVYufinqyt59SYReWXduFognt4w96Fe928QFlcgph1WDHdtxhILkLJbFauNkQo+533XL8roeqhubwg0pW9K9sifePe7mFNGFDZ6Mf0Op5CCCRwQB+Dl0ljBqqzdJBraJkprC5SSxBqXL0rY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714396188; c=relaxed/simple; bh=kn14BchHtCQvu/LYPhR9K5I83FUKH0/ZA+8in3/b4Ng=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ogg5+Top9lgn5qB8BUeVAJFdIMmDAoGK5zExrr4u9z5Hq7Dhc8TbsHDE8ZxZ+Ngml5Pit6DgHU5eNpj8GmqrS0wSVuI4FKiDMt//Y8Agph6B4E/JBkvL0ksxMpatMbk3fJGGQMDjUQ74FXyKoa1nqj3IdOfk8JLJKnBV/IHz2t0= 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=0xkNle6Q; 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-4-0001.mail.infomaniak.ch (smtp-4-0001.mail.infomaniak.ch [10.7.10.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4VSkFM49x2zLcl; Mon, 29 Apr 2024 15:09:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1714396183; bh=kn14BchHtCQvu/LYPhR9K5I83FUKH0/ZA+8in3/b4Ng=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0xkNle6QPtDvfqigQD7PPvu5ehz0l54nja4Mr13qD2FzXUckDX4cznG4nXFQZIi0Y C7UI/T0JpKOSZqUXASJgmWhFn9T/pXJsn8kJuK/YgGQ4JAh3nSJ+3Dd6qNLOb/zFvN aN8BjA0oh3wFQCOra0TOgumQvs4tcNwcBjEgrxQ8= Received: from unknown by smtp-4-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4VSkFL70D2zgvx; Mon, 29 Apr 2024 15:09:42 +0200 (CEST) From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Christian Brauner , Jakub Kicinski , Kees Cook , Mark Brown , Shengyu Li , Shuah Khan Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , "David S . Miller" , =?UTF-8?q?G=C3=BCnther=20Noack?= , Will Drewry , kernel test robot , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Shuah Khan Subject: [PATCH v2 5/9] selftests/landlock: Do not allocate memory in fixture data Date: Mon, 29 Apr 2024 15:09:27 +0200 Message-ID: <20240429130931.2394118-6-mic@digikod.net> In-Reply-To: <20240429130931.2394118-1-mic@digikod.net> References: <20240429130931.2394118-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 Do not allocate self->dir_path in the test process because this would not be visible in the FIXTURE_TEARDOWN() process when relying on fork()/clone3() instead of vfork(). This change is required for a following commit removing vfork() call to not break the layout3_fs.* test cases. Cc: Günther Noack Cc: Kees Cook Cc: Shuah Khan Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20240429130931.2394118-6-mic@digikod.net --- Changes since v1: * Extract change from a bigger patch (suggested by Kees). --- tools/testing/selftests/landlock/fs_test.c | 57 +++++++++++++--------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c index 46b9effd53e4..1e2cffde02b5 100644 --- a/tools/testing/selftests/landlock/fs_test.c +++ b/tools/testing/selftests/landlock/fs_test.c @@ -9,6 +9,7 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -4624,7 +4625,6 @@ FIXTURE(layout3_fs) { bool has_created_dir; bool has_created_file; - char *dir_path; bool skip_test; }; @@ -4683,11 +4683,24 @@ FIXTURE_VARIANT_ADD(layout3_fs, hostfs) { .cwd_fs_magic = HOSTFS_SUPER_MAGIC, }; +static char *dirname_alloc(const char *path) +{ + char *dup; + + if (!path) + return NULL; + + dup = strdup(path); + if (!dup) + return NULL; + + return dirname(dup); +} + FIXTURE_SETUP(layout3_fs) { struct stat statbuf; - const char *slash; - size_t dir_len; + char *dir_path = dirname_alloc(variant->file_path); if (!supports_filesystem(variant->mnt.type) || !cwd_matches_fs(variant->cwd_fs_magic)) { @@ -4697,25 +4710,15 @@ FIXTURE_SETUP(layout3_fs) _metadata->teardown_parent = true; - slash = strrchr(variant->file_path, '/'); - ASSERT_NE(slash, NULL); - dir_len = (size_t)slash - (size_t)variant->file_path; - ASSERT_LT(0, dir_len); - self->dir_path = malloc(dir_len + 1); - self->dir_path[dir_len] = '\0'; - strncpy(self->dir_path, variant->file_path, dir_len); - prepare_layout_opt(_metadata, &variant->mnt); /* Creates directory when required. */ - if (stat(self->dir_path, &statbuf)) { + if (stat(dir_path, &statbuf)) { set_cap(_metadata, CAP_DAC_OVERRIDE); - EXPECT_EQ(0, mkdir(self->dir_path, 0700)) + EXPECT_EQ(0, mkdir(dir_path, 0700)) { TH_LOG("Failed to create directory \"%s\": %s", - self->dir_path, strerror(errno)); - free(self->dir_path); - self->dir_path = NULL; + dir_path, strerror(errno)); } self->has_created_dir = true; clear_cap(_metadata, CAP_DAC_OVERRIDE); @@ -4736,6 +4739,8 @@ FIXTURE_SETUP(layout3_fs) self->has_created_file = true; clear_cap(_metadata, CAP_DAC_OVERRIDE); } + + free(dir_path); } FIXTURE_TEARDOWN(layout3_fs) @@ -4754,16 +4759,17 @@ FIXTURE_TEARDOWN(layout3_fs) } if (self->has_created_dir) { + char *dir_path = dirname_alloc(variant->file_path); + set_cap(_metadata, CAP_DAC_OVERRIDE); /* * Don't check for error because the directory might already * have been removed (cf. release_inode test). */ - rmdir(self->dir_path); + rmdir(dir_path); clear_cap(_metadata, CAP_DAC_OVERRIDE); + free(dir_path); } - free(self->dir_path); - self->dir_path = NULL; cleanup_layout(_metadata); } @@ -4830,7 +4836,10 @@ TEST_F_FORK(layout3_fs, tag_inode_dir_mnt) TEST_F_FORK(layout3_fs, tag_inode_dir_child) { - layer3_fs_tag_inode(_metadata, self, variant, self->dir_path); + char *dir_path = dirname_alloc(variant->file_path); + + layer3_fs_tag_inode(_metadata, self, variant, dir_path); + free(dir_path); } TEST_F_FORK(layout3_fs, tag_inode_file) @@ -4857,9 +4866,13 @@ TEST_F_FORK(layout3_fs, release_inodes) if (self->has_created_file) EXPECT_EQ(0, remove_path(variant->file_path)); - if (self->has_created_dir) + if (self->has_created_dir) { + char *dir_path = dirname_alloc(variant->file_path); + /* Don't check for error because of cgroup specificities. */ - remove_path(self->dir_path); + remove_path(dir_path); + free(dir_path); + } ruleset_fd = create_ruleset(_metadata, LANDLOCK_ACCESS_FS_READ_DIR, layer1); -- 2.44.0