xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/lremovexattr/lremovexattr01.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 Linaro Limited. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Rafael David Tinoco <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Test Name: lremovexattr01
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Description:
11*49cdfc7eSAndroid Build Coastguard Worker  * lremovexattr(2) removes the extended attribute identified by a name and
12*49cdfc7eSAndroid Build Coastguard Worker  * associated with a given path in the filesystem. Unlike removexattr(2),
13*49cdfc7eSAndroid Build Coastguard Worker  * lremovexattr(2) removes the attribute from the symbolic link only, and not
14*49cdfc7eSAndroid Build Coastguard Worker  * the file. This test verifies that a simple call to lremovexattr(2) removes,
15*49cdfc7eSAndroid Build Coastguard Worker  * indeed, a previously set attribute key/value from a symbolic link, and the
16*49cdfc7eSAndroid Build Coastguard Worker  * symbolic link _only_.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * Note:
19*49cdfc7eSAndroid Build Coastguard Worker  * According to attr(5), extended attributes are interpreted differently from
20*49cdfc7eSAndroid Build Coastguard Worker  * regular files, directories and symbolic links. User attributes are only
21*49cdfc7eSAndroid Build Coastguard Worker  * allowed for regular files and directories, thus the need of using trusted.*
22*49cdfc7eSAndroid Build Coastguard Worker  * attributes for this test.
23*49cdfc7eSAndroid Build Coastguard Worker  */
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
33*49cdfc7eSAndroid Build Coastguard Worker # include <sys/xattr.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 
38*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_SYS_XATTR_H
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #define ENOATTR ENODATA
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker #define XATTR_KEY		"trusted.key1"
43*49cdfc7eSAndroid Build Coastguard Worker #define XATTR_VALUE		"file and link"
44*49cdfc7eSAndroid Build Coastguard Worker #define XATTR_VALUE_SIZE	13
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
47*49cdfc7eSAndroid Build Coastguard Worker #define FILENAME MNTPOINT"/lremovexattr01testfile"
48*49cdfc7eSAndroid Build Coastguard Worker #define SYMLINK  MNTPOINT"/lremovexattr01symlink"
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static char got_value[XATTR_VALUE_SIZE];
51*49cdfc7eSAndroid Build Coastguard Worker 
verify_lremovexattr(void)52*49cdfc7eSAndroid Build Coastguard Worker static void verify_lremovexattr(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	/* set attribute on both: file and symlink */
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETXATTR(FILENAME, XATTR_KEY, XATTR_VALUE, XATTR_VALUE_SIZE,
57*49cdfc7eSAndroid Build Coastguard Worker 			XATTR_CREATE);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSETXATTR(SYMLINK, XATTR_KEY, XATTR_VALUE, XATTR_VALUE_SIZE,
60*49cdfc7eSAndroid Build Coastguard Worker 			XATTR_CREATE);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	/* remove attribute from symlink only */
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	TEST(lremovexattr(SYMLINK, XATTR_KEY));
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0) {
67*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "lremovexattr(2) failed");
68*49cdfc7eSAndroid Build Coastguard Worker 		return;
69*49cdfc7eSAndroid Build Coastguard Worker 	}
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	/* make sure attribute is gone from symlink */
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	memset(&got_value, 0, XATTR_VALUE_SIZE);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	TEST(lgetxattr(SYMLINK, XATTR_KEY, &got_value, XATTR_VALUE_SIZE));
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET >= 0) {
78*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "lremovexattr(2) did not work");
79*49cdfc7eSAndroid Build Coastguard Worker 		return;
80*49cdfc7eSAndroid Build Coastguard Worker 	}
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR != ENOATTR) {
83*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "lgetxattr(2) failed unexpectedly");
84*49cdfc7eSAndroid Build Coastguard Worker 		return;
85*49cdfc7eSAndroid Build Coastguard Worker 	}
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	/* check if file is unchanged, like it should be */
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	memset(&got_value, 0, XATTR_VALUE_SIZE);
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	TEST(getxattr(FILENAME, XATTR_KEY, &got_value, XATTR_VALUE_SIZE));
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET <= 0) {
94*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "lremovexattr(2) deleted file attribute");
95*49cdfc7eSAndroid Build Coastguard Worker 		return;
96*49cdfc7eSAndroid Build Coastguard Worker 	}
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	if (strncmp(got_value, XATTR_VALUE, XATTR_VALUE_SIZE)) {
99*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "lremovexattr(2) changed file attribute");
100*49cdfc7eSAndroid Build Coastguard Worker 		return;
101*49cdfc7eSAndroid Build Coastguard Worker 	}
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	/* cleanup file attribute for iteration */
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_REMOVEXATTR(FILENAME, XATTR_KEY);
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "lremovexattr(2) removed attribute as expected");
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)110*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
111*49cdfc7eSAndroid Build Coastguard Worker {
112*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(FILENAME, 0644, NULL);
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	if (symlink(FILENAME, SYMLINK) < 0)
115*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "symlink() not supported");
116*49cdfc7eSAndroid Build Coastguard Worker }
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
119*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
120*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_lremovexattr,
121*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
122*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
123*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
124*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
125*49cdfc7eSAndroid Build Coastguard Worker };
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker #else /* HAVE_SYS_XATTR_H */
128*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("<sys/xattr.h> does not exist");
129*49cdfc7eSAndroid Build Coastguard Worker #endif
130