Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp21118lqh; Mon, 6 May 2024 10:00:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVWg3p1NeNjTh2GeWb2MQ9MRVah8RLez47KtI9IoPjL12BF8TH3vu2yCXld2Ik5FdUMbPneMhXtIhcSBSiREYGJYBzM8EZXRz3sc/9aDg== X-Google-Smtp-Source: AGHT+IH62KUCyduv5A4qeJKTIJJxrEBkvKdojr1qIw8l9ijReiTrZ6co5MGevujhjhyIfkT3Vflt X-Received: by 2002:a05:6a20:c890:b0:1af:abce:986d with SMTP id hb16-20020a056a20c89000b001afabce986dmr6721607pzb.61.1715014816160; Mon, 06 May 2024 10:00:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715014816; cv=pass; d=google.com; s=arc-20160816; b=wE/opJ5ki/U9I6uC3CCe7XYhorb15yVn4/ozX+u6ryYT2iOiqdwp9bGmXekQ3QCayC P21+UmEE0RSNYTcAo8+sq52UngJ3n5/1w+MmbzEq1SKNtoAvPWlCiQ7mnO1paTWRYMOs cy+OzftiKOofT5ETtpBUQfzoI5/XUdyz//3EIjfVU7v7E5/WdVaINXbyOdCDcbhIQAA4 THngpY3VESwSnhNGBFmAdLeLoq7rh67mY8jGIDeBdNsPZ0AUgle54xLZ2LN1dzSREHDv RN9G1uvJ4cFfbBcpEhy0Wz57sle6bhZfcTCaXLaXdzvYTHSPbTHaADuphOiyFlDDCtCp p8YQ== 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=M6gfpXqtea1KUgODQ8zsxissuDtafmjXXi6XM3RMVXY=; fh=h2xHIZSGiUwuZyladnT9pE6uX086qy42ByZk41PjzSs=; b=npaT8lrKWiIEqYywpXPSE69IAIstWvnurNBcvONj3/v87JeN+afWDKE4QDyWTob31w bXRVck+Cbpa56UvSr2knBZR0XB1zxNzo/k84cQkEy8XhXTq2ZLNHLFM6AKdwXZfRUGB7 zoqsVbgf1bkoYfzWkBW8kuIN1vextAlLhvfqjv482vTV0vZuji0jBzYHqwIFfvOo7q0j laKsfYNwwgwhSJFa4ku0zCPp2g6lkLJYd7W34wEMfvsM8W/7PCz5IiIXfiUfm0omUB2q Bu/ZcSdxhOoo0JYdCB0L08GXkpH042B17uU182jk4kKkpPaAw8kkwYtLhv2mG79OHmhH LA4w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=1hmN6j94; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-170199-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170199-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id n66-20020a632745000000b0061ad115cfeesi8525162pgn.728.2024.05.06.10.00.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 10:00:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170199-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=1hmN6j94; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-170199-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170199-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 38118B20A24 for ; Mon, 6 May 2024 16:56:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9AD081586F5; Mon, 6 May 2024 16:55:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="1hmN6j94" Received: from smtp-8fae.mail.infomaniak.ch (smtp-8fae.mail.infomaniak.ch [83.166.143.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 5BF09157466 for ; Mon, 6 May 2024 16:55:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.166.143.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715014533; cv=none; b=AO5LXlQQgAd3HIYNt/pXyjGLgbF/zMSDUuPcO+tZMtTsr8VGXrM0sQyQudGiOHW/JyP8c8byj5hOjpXoetoUwIefBCJqpV+Sk20Z0rUCOORFRiq4dUs+l+fCqjZbLR41vZ95xyH9eFMszyYXKyVZPhB0OPHwLfrh+mlLBF5vN8o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715014533; c=relaxed/simple; bh=4AgZcttS+cKt9fZPtD0fgjtAHrdg132SQrfeWC6AnYQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hGBfnbmIrV6j2DUTsrOU6CTrPtRfI/lsx7976VHPHuiPiryNAqCvgS0vPtNX7T1OzyiqlmswAMLy04XkMVH4NB2+8Elq4w+on5IDgdU9SvrqUdIHOsstXsPinREowPMUCfMXqDvdwrnmE0as1jmIhjavQCMuibObKGdGn+mLitc= 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=1hmN6j94; arc=none smtp.client-ip=83.166.143.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-0001.mail.infomaniak.ch (smtp-3-0001.mail.infomaniak.ch [10.4.36.108]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4VY6wd424xz6Cp; Mon, 6 May 2024 18:55:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1715014529; bh=4AgZcttS+cKt9fZPtD0fgjtAHrdg132SQrfeWC6AnYQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1hmN6j94zD+InHOVbJ5e4xvcJPJa3TnIm+Pb2jLiHlYS4dDvkxrNeWa/E2HkqEJ/x M8naLm93TBGPWwSvzBI1NKkHMdqhxd2qcEiNJb+svD2lMmTX8QA7dtn6YbL5UpyLBc hEDsmgT6dXrEL9+DYRvlQabL869HI6pJau8Z3yk8= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4VY6wc5GLZzwV9; Mon, 6 May 2024 18:55:28 +0200 (CEST) From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Christian Brauner , Greg Kroah-Hartman , Jakub Kicinski , Kees Cook , Linus Torvalds , Mark Brown , Sasha Levin , Sean Christopherson , Shengyu Li , Shuah Khan , Shuah Khan Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Bagas Sanjaya , Brendan Higgins , David Gow , "David S . Miller" , Florian Fainelli , =?UTF-8?q?G=C3=BCnther=20Noack?= , Jon Hunter , Ron Economos , Ronald Warsow , Stephen Rothwell , Will Drewry , kernel test robot , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v6 05/10] selftests/landlock: Do not allocate memory in fixture data Date: Mon, 6 May 2024 18:55:13 +0200 Message-ID: <20240506165518.474504-6-mic@digikod.net> In-Reply-To: <20240506165518.474504-1-mic@digikod.net> References: <20240506165518.474504-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: Shuah Khan Reviewed-by: Kees Cook Signed-off-by: Mickaël Salaün Link: https://lore.kernel.org/r/20240506165518.474504-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.45.0