• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.
4  */
5 
6 /*
7  * setxattr(2) to immutable and append-only files should get EPERM
8  *
9  * There are 2 test cases:
10  * 1. Set attribute to a immutable file, setxattr(2) should return -1
11  *    and set errno to EPERM
12  * 2. Set attribute to a append-only file, setxattr(2) should return
13  *    -1 and set errno to EPERM
14  */
15 
16 #define _GNU_SOURCE
17 #include "config.h"
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/wait.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_SYS_XATTR_H
30 # include <sys/xattr.h>
31 #endif
32 #include "lapi/fs.h"
33 
34 #include "tst_test.h"
35 
36 #if defined HAVE_SYS_XATTR_H
37 #define XATTR_TEST_KEY "user.testkey"
38 #define XATTR_TEST_VALUE "this is a test value"
39 #define XATTR_TEST_VALUE_SIZE (sizeof(XATTR_TEST_VALUE) - 1)
40 
41 #define IMMU_FILE "setxattr03immutable"
42 #define APPEND_FILE  "setxattr03appendonly"
43 
44 #define set_immutable_on(fd) fsetflag(fd, 1, 1)
45 #define set_immutable_off(fd) fsetflag(fd, 0, 1)
46 #define set_append_on(fd) fsetflag(fd, 1, 0)
47 #define set_append_off(fd) fsetflag(fd, 0, 0)
48 
49 struct test_case {
50 	char *desc;
51 	char *fname;
52 	char *key;
53 	char *value;
54 	size_t size;
55 	int flags;
56 	int exp_err;
57 };
58 static struct test_case tc[] = {
59 	{			/* case 00, set attr to immutable file */
60 	 .desc = "Set attr to immutable file",
61 	 .fname = IMMU_FILE,
62 	 .key = XATTR_TEST_KEY,
63 	 .value = XATTR_TEST_VALUE,
64 	 .size = XATTR_TEST_VALUE_SIZE,
65 	 .flags = XATTR_CREATE,
66 	 .exp_err = EPERM,
67 	},
68 	{			/* case 01, set attr to append-only file */
69 	 .desc = "Set attr to append-only file",
70 	 .fname = APPEND_FILE,
71 	 .key = XATTR_TEST_KEY,
72 	 .value = XATTR_TEST_VALUE,
73 	 .size = XATTR_TEST_VALUE_SIZE,
74 	 .flags = XATTR_CREATE,
75 	 .exp_err = EPERM,
76 	},
77 };
78 
79 static int immu_fd;
80 static int append_fd;
81 
verify_setxattr(unsigned int i)82 static void verify_setxattr(unsigned int i)
83 {
84 	TEST(setxattr(tc[i].fname, tc[i].key, tc[i].value, tc[i].size,
85 			tc[i].flags));
86 
87 	if (!TST_RET) {
88 		tst_res(TFAIL, "%s succeeded unexpectedly", tc[i].desc);
89 		return;
90 	}
91 
92 	if (TST_ERR != tc[i].exp_err) {
93 		tst_res(TFAIL | TTERRNO, "%s - expected %s, got", tc[i].desc,
94 			tst_strerrno(tc[i].exp_err));
95 		return;
96 	}
97 
98 	tst_res(TPASS | TTERRNO, "%s", tc[i].desc);
99 }
100 
fsetflag(int fd,int on,int immutable)101 static int fsetflag(int fd, int on, int immutable)
102 {
103 	int fsflags = 0;
104 	int fsfl;
105 
106 	if (ioctl(fd, FS_IOC_GETFLAGS, &fsflags) < 0)
107 		return 1;
108 
109 	if (immutable)
110 		fsfl = FS_IMMUTABLE_FL;
111 	else
112 		fsfl = FS_APPEND_FL;
113 
114 	if (on)
115 		fsflags |= fsfl;
116 	else
117 		fsflags &= ~fsfl;
118 
119 	if (ioctl(fd, FS_IOC_SETFLAGS, &fsflags) < 0)
120 		return 1;
121 
122 	return 0;
123 }
124 
setup(void)125 static void setup(void)
126 {
127 	int fd;
128 
129 	/* Test for xattr support */
130 	fd = SAFE_CREAT("testfile", 0644);
131 	SAFE_CLOSE(fd);
132 	if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
133 		if (errno == ENOTSUP)
134 			tst_brk(TCONF, "No xattr support in fs or "
135 				 "fs mounted without user_xattr option");
136 	SAFE_UNLINK("testfile");
137 
138 	/* Create test files and set file immutable or append-only */
139 	immu_fd = SAFE_CREAT(IMMU_FILE, 0644);
140 	if (set_immutable_on(immu_fd))
141 		tst_brk(TBROK | TERRNO, "Set %s immutable failed",
142 			 IMMU_FILE);
143 
144 	append_fd = SAFE_CREAT(APPEND_FILE, 0644);
145 	if (set_append_on(append_fd))
146 		tst_brk(TBROK | TERRNO, "Set %s append-only failed",
147 			APPEND_FILE);
148 }
149 
cleanup(void)150 static void cleanup(void)
151 {
152 	if ((immu_fd > 0) && set_immutable_off(immu_fd))
153 		tst_res(TWARN | TERRNO, "Unset %s immutable failed",
154 			 IMMU_FILE);
155 
156 	if ((append_fd > 0) && set_append_off(append_fd))
157 		tst_res(TWARN | TERRNO, "Unset %s append-only failed",
158 			 APPEND_FILE);
159 
160 	if (immu_fd > 0)
161 		SAFE_CLOSE(immu_fd);
162 
163 	if (append_fd > 0)
164 		SAFE_CLOSE(append_fd);
165 }
166 
167 static struct tst_test test = {
168 	.setup = setup,
169 	.cleanup = cleanup,
170 	.test = verify_setxattr,
171 	.tcnt = ARRAY_SIZE(tc),
172 	.needs_tmpdir = 1,
173 	.needs_root = 1,
174 };
175 
176 #else
177 TST_TEST_TCONF("<sys/xattr.h> does not exist");
178 #endif /* defined HAVE_SYS_XATTR_H */
179