Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp1171799lqo; Sat, 11 May 2024 10:17:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUVEvnzBDE0YfT7bep3Ks31qoy5iooc+BaJesKxWTK4oZK0qjWTkyL5cU5AnqbLJLdG4LeCoSx2Til+SQYIxgzSRn9skv+zmpg/YxzEMQ== X-Google-Smtp-Source: AGHT+IHirYfoyXIDt68FAAGHgIoFA+j4rC1lq2ES5D4efKue5tJfPUCM1L3SQyIrzYrFFYM6WX6H X-Received: by 2002:a05:620a:390b:b0:790:9666:5c8a with SMTP id af79cd13be357-792bbdfd00bmr1553886985a.19.1715447869597; Sat, 11 May 2024 10:17:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715447869; cv=pass; d=google.com; s=arc-20160816; b=Vvwp+3TLuIRfCrcRTC3SM/wqp4tXr4oeV8k2qAKxxQaW3COCgrv6YFjh3ptjZGoX9q DZQhLJX7VCxY9a+OsA+vE3TnKHDm7ehMPwKH4+sNVJvqrI68tpWC/L55xps8INzYcTPq 4anGUmpDnoECt1v6Cn4g1gLBB04vG9fy2NJT8p7XEA8IanwTZHh0DdxhgMrU9KMgWDu3 24eYHFgx90nxBr5AVj7MwR9WwH5G/EUiI5uj010OC1v6J+jS8dzYdX9cbHLLkXxLFezy foIYV59bFa5snuSgy0Q5tcaZix2uJGu0QFOesvscsoHbRfwgyT7e+ES/CGkEaVl+qki5 eG0Q== 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=bQmT9mCo/KDkKXa+gN2WERC8VD+y33I3bpVzTIyCpiU=; fh=h2xHIZSGiUwuZyladnT9pE6uX086qy42ByZk41PjzSs=; b=O02H0hOowuYQuxeIGn6yRWlNoH6g23Wogy45hhg1hoY5e7EYH2t11iWuCFL04HH2ZK AY57RQA6eKd+bPxhsUlZ5cNmPI8q+9IRs4ZbZAO2E2Bw18o24PXPBauoFiQb1es9rwO8 quMyundYxEt5BheW547C53URiG5B8EsuQaR81SEIIoOC8lkake8I3cjD9kzjOfnoePSb /jpPDergAx6BhuXCrt+gX87o+7TUZ5V2oMIzSF7l06SCHzpsR3mJB7vjbVTsn2Mp7aDz tOZHgnvzdwlygd1kQtZHI7oVp6/y5CAwYkUdpfhpBAjBya9wt/b/JCUeWrdvy6TuDg81 a/dA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=d8watQiw; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-176660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176660-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-792bf33b24esi596727985a.417.2024.05.11.10.17.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 May 2024 10:17:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-176660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=d8watQiw; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-176660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176660-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 3B0721C20CD9 for ; Sat, 11 May 2024 17:17:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8ECDF5FDA9; Sat, 11 May 2024 17:15:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="d8watQiw" 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 B47DA1E863 for ; Sat, 11 May 2024 17:15:04 +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=1715447707; cv=none; b=Q93/4XH78gOOfRcvzT5J/qAMfSNtVw1ypkXWHIDdgJOWLzY6w9uUvTh6NYhVwLRwUUdpfOk6Y5+MRzagwjZboD7zFYoLvo0nlv1ALyzfo0uMHoy5DLm30aHrTmpykljZ9SrJZwLctB9heEU3Arg+he6/ojVjRTYz2UYJgr174CA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715447707; c=relaxed/simple; bh=vI+1BGX2Xyx65Q+T6XA0m9R8xr24obS+EMq8zTGL4bw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bT9IRY5WrymsiKtPHA+g4o0VclhjW3lVkOPyKz1hc9nmhndFzy45nmwU910/4wGr1oCjto1uadr7E/eQN0dSUPXQKlYY0UGEXbREqVcmYnr53hWyDf2skPHN9Avy0AWRDw8TponUPllOu+wm0F+T8VoB0PP2zWshgkMlIm4EZ04= 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=d8watQiw; 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-4-0000.mail.infomaniak.ch (smtp-4-0000.mail.infomaniak.ch [10.7.10.107]) by smtp-3-3000.mail.infomaniak.ch (Postfix) with ESMTPS id 4VcC6n4w5ZzdHC; Sat, 11 May 2024 19:14:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digikod.net; s=20191114; t=1715447697; bh=bQmT9mCo/KDkKXa+gN2WERC8VD+y33I3bpVzTIyCpiU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d8watQiw2LCiqGZZQLPeIQhNgIV936flZ5KixdlEgmy81sDKZ8uzRqrRTxNwo5Xfb wO7mS4j7LljJi5JMQea+y8fknW3AruhlF0HBXfJGPDl849eyzEUDMrlM7n0cZKONcy xrUtgnreZu4bbFOgsy04gJtBVndvGwxNikLNWa5U= Received: from unknown by smtp-4-0000.mail.infomaniak.ch (Postfix) with ESMTPA id 4VcC6m6W7czSl5; Sat, 11 May 2024 19:14:56 +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 v7 05/10] selftests/landlock: Do not allocate memory in fixture data Date: Sat, 11 May 2024 19:14:40 +0200 Message-ID: <20240511171445.904356-6-mic@digikod.net> In-Reply-To: <20240511171445.904356-1-mic@digikod.net> References: <20240511171445.904356-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/20240511171445.904356-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