Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1413745lqm; Thu, 2 May 2024 14:11:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWqWgPLHozVtXSBShqxS5tuXppFaxc4XZoD+hWB2Se+cQQmqlgZLQPuXpWj49OVvJsLH5+UdjD9Xb8wYlvlmmYSp0tIyh98mkeulFDI0A== X-Google-Smtp-Source: AGHT+IEK/Hn8sJbEs5g1EWlifs6Hn9/1SBX4PIXKf75otyMAzE238Vs7BRktEIc4mlslhLpBfa8J X-Received: by 2002:a05:622a:1922:b0:43a:c243:8669 with SMTP id w34-20020a05622a192200b0043ac2438669mr934382qtc.56.1714684302771; Thu, 02 May 2024 14:11:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714684302; cv=pass; d=google.com; s=arc-20160816; b=o/vqcSG+DC8W+gZ24nCwAfkDTdS2vXy6TN98URrjR4fPkFT4bCQL4kGrW0BXVy+eij yjXqdH61Ys+qJtzbkgyiCpiOXGIa1KTW5cTkcGU5M2Pm/GoIwIxbDgoS+2K4UgCW/CKH t16BfUcfUHU8mgZ5OnYoOQDaiVYn7DKB0Yr+TYz3tP9oSVm7oshPPszLxrmkk9spuE7x PAgzziBXs28JEGHF501cO3aJ3RTEct60UnLyHcDzAuvMdnMxD+D2wwfSiLSkpN0F+PJB vQMDSrroDCl9K4jc93OBqGapmt77CfrQ3OP13rRQvGDatkaGB2lenmUOGh8EmXXS/qwe n3EA== 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=XTqF7r0/6Y57JdOLO2t2OcHKJJXS7vFZRmf/PBgP1XY=; fh=DyJwEPHty4QZ77xuwgmyonrTSVvlIRI2bkR/2kTqG9Q=; b=ZRsJTtxp0N8A8fmyKtwb1lKTWHfm6attxShOI411bT8fbNsPBZBd3S+/ZeCn26YAPn L7haYAKz+Q5kGG8c7/r62EPbu3JDNFJSB6RhWu5Hd9oHjIDQ16nZ7B1JzBW/trDE8N2C y1DY6cTl14x4Zj07E1IxCA8PBVwa/pLOPvXyJKM2HsynrfQDRnPHBtilUMU2IFyrjrta U4hzLx3juQv8bZp8D/EqC8KhDLl6k4fsHHjU5p76A6HAGYjyKdju+KZRf/R0s55Gv44C fhGdytXOXtppl4ji7ziYIpY1sQoFVIGTFr3wlRAKt/oMxEfWnWopWIo01IWGvfxzbdrX 2mgA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@digikod.net header.s=20191114 header.b=aT7uiL4o; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-166919-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166919-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 z5-20020a05622a028500b0043a3d81ac5esi1900123qtw.133.2024.05.02.14.11.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 14:11:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-166919-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=aT7uiL4o; arc=pass (i=1 spf=pass spfdomain=digikod.net dkim=pass dkdomain=digikod.net); spf=pass (google.com: domain of linux-kernel+bounces-166919-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166919-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 6CB9F1C20CD9 for ; Thu, 2 May 2024 21:11:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 50B1D1C6605; Thu, 2 May 2024 21:09:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=digikod.net header.i=@digikod.net header.b="aT7uiL4o" Received: from smtp-bc08.mail.infomaniak.ch (smtp-bc08.mail.infomaniak.ch [45.157.188.8]) (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 9BE631C0DF0 for ; Thu, 2 May 2024 21:09:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.157.188.8 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714684196; cv=none; b=oY5J+fcqGYa+313kHj7qdqZjip1pySsW2OGjM7DqqDIWG7bnqCrgau4l+vHuApgTVWun2oFF/EduLPLi5l/YuXSn6BiPcDoRPVmdSH7cqQcl236TXC/UUyhEDpPopfQaLyB7yrN3lDwsfMbJXj+XKEeK7z1OIvEX6ZawY2zg/Cc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714684196; c=relaxed/simple; bh=jw4R3rrY+OC2XdTr3iRaQSj/Q6GJ0Ni0bEq4asle6Y4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rz71KnHlcVtXDHgwxjdvu1HmhN4j/ukSeFesqBwme1hiZvh+OXXsJbtmgfZabMFhQXNXJn/QDrN8HZc6HcHp52ub08h5I4cWAUC3qIEh8aG/B1wudqdFxcQ9Ra6AHdI+CYf4DeBAKkPJ2G8xqE5d10nOb1Fh55omWZHQ4cbr13A= 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=aT7uiL4o; arc=none smtp.client-ip=45.157.188.8 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 4VVmls6z3kzNgQ; Thu, 2 May 2024 23:09:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=digikod.net; s=20191114; t=1714684185; bh=jw4R3rrY+OC2XdTr3iRaQSj/Q6GJ0Ni0bEq4asle6Y4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aT7uiL4oBzfeHAqB7ipNeHdwZrzsl6OSKadvxK/dR5Xs3HJLgiJex09UaspZf+E/h /VQRLz1oCr7N1porjxHlg1AJBsDSaZ8rROdV5NsZSskW+BWEGRydFDoQkmQ7GESMQj NEG/GcLbhdbrzGBgSfMeWY3u6IW5QiBKo/kKhNqg= Received: from unknown by smtp-3-0001.mail.infomaniak.ch (Postfix) with ESMTPA id 4VVmls00bGztFV; Thu, 2 May 2024 23:09:44 +0200 (CEST) From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: Christian Brauner , Jakub Kicinski , Kees Cook , Mark Brown , Sean Christopherson , 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 , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Shuah Khan Subject: [PATCH v4 05/10] selftests/landlock: Do not allocate memory in fixture data Date: Thu, 2 May 2024 23:09:21 +0200 Message-ID: <20240502210926.145539-6-mic@digikod.net> In-Reply-To: <20240502210926.145539-1-mic@digikod.net> References: <20240502210926.145539-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/20240502210926.145539-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