xref: /linux/tools/testing/selftests/landlock/scoped_common.h (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1*fefcf0f7STahera Fahimi /* SPDX-License-Identifier: GPL-2.0 */
2*fefcf0f7STahera Fahimi /*
3*fefcf0f7STahera Fahimi  * Landlock scope test helpers
4*fefcf0f7STahera Fahimi  *
5*fefcf0f7STahera Fahimi  * Copyright © 2024 Tahera Fahimi <fahimitahera@gmail.com>
6*fefcf0f7STahera Fahimi  */
7*fefcf0f7STahera Fahimi 
8*fefcf0f7STahera Fahimi #define _GNU_SOURCE
9*fefcf0f7STahera Fahimi 
10*fefcf0f7STahera Fahimi #include <sys/types.h>
11*fefcf0f7STahera Fahimi 
create_scoped_domain(struct __test_metadata * const _metadata,const __u16 scope)12*fefcf0f7STahera Fahimi static void create_scoped_domain(struct __test_metadata *const _metadata,
13*fefcf0f7STahera Fahimi 				 const __u16 scope)
14*fefcf0f7STahera Fahimi {
15*fefcf0f7STahera Fahimi 	int ruleset_fd;
16*fefcf0f7STahera Fahimi 	const struct landlock_ruleset_attr ruleset_attr = {
17*fefcf0f7STahera Fahimi 		.scoped = scope,
18*fefcf0f7STahera Fahimi 	};
19*fefcf0f7STahera Fahimi 
20*fefcf0f7STahera Fahimi 	ruleset_fd =
21*fefcf0f7STahera Fahimi 		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
22*fefcf0f7STahera Fahimi 	ASSERT_LE(0, ruleset_fd)
23*fefcf0f7STahera Fahimi 	{
24*fefcf0f7STahera Fahimi 		TH_LOG("Failed to create a ruleset: %s", strerror(errno));
25*fefcf0f7STahera Fahimi 	}
26*fefcf0f7STahera Fahimi 	enforce_ruleset(_metadata, ruleset_fd);
27*fefcf0f7STahera Fahimi 	EXPECT_EQ(0, close(ruleset_fd));
28*fefcf0f7STahera Fahimi }
29