xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getxattr/getxattr05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This test verifies that:
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - Without a user namespace, getxattr(2) should get same data when
13*49cdfc7eSAndroid Build Coastguard Worker  *   acquiring the value of system.posix_acl_access twice.
14*49cdfc7eSAndroid Build Coastguard Worker  * - With/Without mapped root UID in a user namespaces, getxattr(2) should
15*49cdfc7eSAndroid Build Coastguard Worker  *   get same data when acquiring the value of system.posix_acl_access twice.
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  * This issue included by getxattr05 has been fixed in kernel:
18*49cdfc7eSAndroid Build Coastguard Worker  * 82c9a927bc5d ("getxattr: use correct xattr length")
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
22*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
29*49cdfc7eSAndroid Build Coastguard Worker # include <sys/xattr.h>
30*49cdfc7eSAndroid Build Coastguard Worker #endif
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_LIBACL
33*49cdfc7eSAndroid Build Coastguard Worker # include <sys/acl.h>
34*49cdfc7eSAndroid Build Coastguard Worker #endif
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_XATTR_H) && defined(HAVE_LIBACL)
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE	"testfile"
42*49cdfc7eSAndroid Build Coastguard Worker #define SELF_USERNS	"/proc/self/ns/user"
43*49cdfc7eSAndroid Build Coastguard Worker #define MAX_USERNS	"/proc/sys/user/max_user_namespaces"
44*49cdfc7eSAndroid Build Coastguard Worker #define UID_MAP	"/proc/self/uid_map"
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker static acl_t acl;
47*49cdfc7eSAndroid Build Coastguard Worker static int orig_max_userns = -1;
48*49cdfc7eSAndroid Build Coastguard Worker static int user_ns_supported = 1;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
51*49cdfc7eSAndroid Build Coastguard Worker 	/* 0: without userns, 1: with userns */
52*49cdfc7eSAndroid Build Coastguard Worker 	int set_userns;
53*49cdfc7eSAndroid Build Coastguard Worker 	/* 0: don't map root UID in userns, 1: map root UID in userns */
54*49cdfc7eSAndroid Build Coastguard Worker 	int map_root;
55*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
56*49cdfc7eSAndroid Build Coastguard Worker 	{0, 0},
57*49cdfc7eSAndroid Build Coastguard Worker 	{1, 0},
58*49cdfc7eSAndroid Build Coastguard Worker 	{1, 1},
59*49cdfc7eSAndroid Build Coastguard Worker };
60*49cdfc7eSAndroid Build Coastguard Worker 
verify_getxattr(void)61*49cdfc7eSAndroid Build Coastguard Worker static void verify_getxattr(void)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	ssize_t i, res1, res2;
64*49cdfc7eSAndroid Build Coastguard Worker 	char buf1[128], buf2[132];
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	res1 = SAFE_GETXATTR(TEST_FILE, "system.posix_acl_access",
67*49cdfc7eSAndroid Build Coastguard Worker 			     buf1, sizeof(buf1));
68*49cdfc7eSAndroid Build Coastguard Worker 	res2 = SAFE_GETXATTR(TEST_FILE, "system.posix_acl_access",
69*49cdfc7eSAndroid Build Coastguard Worker 			     buf2, sizeof(buf2));
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	if (res1 != res2) {
72*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Return different sizes when acquiring "
73*49cdfc7eSAndroid Build Coastguard Worker 			"the value of system.posix_acl_access twice");
74*49cdfc7eSAndroid Build Coastguard Worker 		return;
75*49cdfc7eSAndroid Build Coastguard Worker 	}
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < res1; i++) {
78*49cdfc7eSAndroid Build Coastguard Worker 		if (buf1[i] != buf2[i])
79*49cdfc7eSAndroid Build Coastguard Worker 			break;
80*49cdfc7eSAndroid Build Coastguard Worker 	}
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	if (i < res1) {
83*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Got different data(%02x != %02x) at %ld",
84*49cdfc7eSAndroid Build Coastguard Worker 			buf1[i], buf2[i], i);
85*49cdfc7eSAndroid Build Coastguard Worker 		return;
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Got same data when acquiring the value of "
89*49cdfc7eSAndroid Build Coastguard Worker 		"system.posix_acl_access twice");
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker 
do_unshare(int map_root)92*49cdfc7eSAndroid Build Coastguard Worker static void do_unshare(int map_root)
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker 	int res;
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	/* unshare() should support CLONE_NEWUSER flag since Linux 3.8 */
97*49cdfc7eSAndroid Build Coastguard Worker 	res = unshare(CLONE_NEWUSER);
98*49cdfc7eSAndroid Build Coastguard Worker 	if (res == -1)
99*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TERRNO, "unshare(CLONE_NEWUSER) failed");
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	if (map_root) {
102*49cdfc7eSAndroid Build Coastguard Worker 		/* uid_map file should exist since Linux 3.8 because
103*49cdfc7eSAndroid Build Coastguard Worker 		 * it is available on Linux 3.5
104*49cdfc7eSAndroid Build Coastguard Worker 		 */
105*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_ACCESS(UID_MAP, F_OK);
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(UID_MAP, "%d %d %d", 0, 0, 1);
108*49cdfc7eSAndroid Build Coastguard Worker 	}
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker 
do_getxattr(unsigned int n)111*49cdfc7eSAndroid Build Coastguard Worker static void do_getxattr(unsigned int n)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
114*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->set_userns && !user_ns_supported) {
117*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "user namespace not available");
118*49cdfc7eSAndroid Build Coastguard Worker 		return;
119*49cdfc7eSAndroid Build Coastguard Worker 	}
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
122*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid) {
123*49cdfc7eSAndroid Build Coastguard Worker 		if (tc->set_userns)
124*49cdfc7eSAndroid Build Coastguard Worker 			do_unshare(tc->map_root);
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 		verify_getxattr();
127*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
128*49cdfc7eSAndroid Build Coastguard Worker 	}
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
131*49cdfc7eSAndroid Build Coastguard Worker }
132*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)133*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
134*49cdfc7eSAndroid Build Coastguard Worker {
135*49cdfc7eSAndroid Build Coastguard Worker 	const char *acl_text = "u::rw-,u:root:rwx,g::r--,o::r--,m::rwx";
136*49cdfc7eSAndroid Build Coastguard Worker 	int res;
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEST_FILE, 0644, NULL);
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	acl = acl_from_text(acl_text);
141*49cdfc7eSAndroid Build Coastguard Worker 	if (!acl)
142*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "acl_from_text() failed");
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	res = acl_set_file(TEST_FILE, ACL_TYPE_ACCESS, acl);
145*49cdfc7eSAndroid Build Coastguard Worker 	if (res == -1) {
146*49cdfc7eSAndroid Build Coastguard Worker 		if (errno == EOPNOTSUPP)
147*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF | TERRNO, "acl_set_file()");
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "acl_set_file(%s) failed", TEST_FILE);
150*49cdfc7eSAndroid Build Coastguard Worker 	}
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker 	/* The default value of max_user_namespaces is set to 0 on some distros,
153*49cdfc7eSAndroid Build Coastguard Worker 	 * We need to change the default value to call unshare().
154*49cdfc7eSAndroid Build Coastguard Worker 	 */
155*49cdfc7eSAndroid Build Coastguard Worker 	if (access(SELF_USERNS, F_OK) != 0) {
156*49cdfc7eSAndroid Build Coastguard Worker 		user_ns_supported = 0;
157*49cdfc7eSAndroid Build Coastguard Worker 	} else if (!access(MAX_USERNS, F_OK)) {
158*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_SCANF(MAX_USERNS, "%d", &orig_max_userns);
159*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(MAX_USERNS, "%d", 10);
160*49cdfc7eSAndroid Build Coastguard Worker 	}
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker }
163*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)164*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
165*49cdfc7eSAndroid Build Coastguard Worker {
166*49cdfc7eSAndroid Build Coastguard Worker 	if (orig_max_userns != -1)
167*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(MAX_USERNS, "%d", orig_max_userns);
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	if (acl)
170*49cdfc7eSAndroid Build Coastguard Worker 		acl_free(acl);
171*49cdfc7eSAndroid Build Coastguard Worker }
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
174*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
175*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
176*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
177*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
178*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
179*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
180*49cdfc7eSAndroid Build Coastguard Worker 	.test = do_getxattr,
181*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
182*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "82c9a927bc5d"},
183*49cdfc7eSAndroid Build Coastguard Worker 		{}
184*49cdfc7eSAndroid Build Coastguard Worker },
185*49cdfc7eSAndroid Build Coastguard Worker };
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker #else /* HAVE_SYS_XATTR_H && HAVE_LIBACL*/
188*49cdfc7eSAndroid Build Coastguard Worker 	TST_TEST_TCONF("<sys/xattr.h> or <sys/acl.h> does not exist.");
189*49cdfc7eSAndroid Build Coastguard Worker #endif
190