1 /*
2  * Copyright (C) 2012 Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it
13  * is free of the rightful claim of any third person regarding
14  * infringement or the like.  Any license provided herein, whether
15  * implied or otherwise, applies only to this software file.  Patent
16  * licenses, if any, provided herein do not apply to combinations of
17  * this program with other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24 
25 /*
26  * setxattr(2) to immutable and append-only files should get EPERM
27  *
28  * There are 2 test cases:
29  * 1. Set attribute to a immutable file, setxattr(2) should return -1
30  *    and set errno to EPERM
31  * 2. Set attribute to a append-only file, setxattr(2) should return
32  *    -1 and set errno to EPERM
33  */
34 
35 #include "config.h"
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/wait.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #ifdef HAVE_ATTR_XATTR_H
48 #include <attr/xattr.h>
49 #endif
50 #include <linux/fs.h>
51 
52 #include "test.h"
53 
54 char *TCID = "setxattr03";
55 
56 #if defined HAVE_ATTR_XATTR_H && defined HAVE_FS_IOC_FLAGS
57 #define XATTR_TEST_KEY "user.testkey"
58 #define XATTR_TEST_VALUE "this is a test value"
59 #define XATTR_TEST_VALUE_SIZE (sizeof(XATTR_TEST_VALUE) - 1)
60 
61 #define IMMU_FILE "setxattr03immutable"
62 #define APPEND_FILE  "setxattr03appendonly"
63 
64 #define set_immutable_on(fd) fsetflag(fd, 1, 1)
65 #define set_immutable_off(fd) fsetflag(fd, 0, 1)
66 #define set_append_on(fd) fsetflag(fd, 1, 0)
67 #define set_append_off(fd) fsetflag(fd, 0, 0)
68 
69 struct test_case {
70 	char *desc;
71 	char *fname;
72 	char *key;
73 	char *value;
74 	size_t size;
75 	int flags;
76 	int exp_err;
77 };
78 static struct test_case tc[] = {
79 	{			/* case 00, set attr to immutable file */
80 	 .desc = "Set attr to immutable file",
81 	 .fname = IMMU_FILE,
82 	 .key = XATTR_TEST_KEY,
83 	 .value = XATTR_TEST_VALUE,
84 	 .size = XATTR_TEST_VALUE_SIZE,
85 	 .flags = XATTR_CREATE,
86 	 .exp_err = EPERM,
87 	 },
88 	{			/* case 01, set attr to append-only file */
89 	 .desc = "Set attr to append-only file",
90 	 .fname = APPEND_FILE,
91 	 .key = XATTR_TEST_KEY,
92 	 .value = XATTR_TEST_VALUE,
93 	 .size = XATTR_TEST_VALUE_SIZE,
94 	 .flags = XATTR_CREATE,
95 	 .exp_err = EPERM,
96 	 },
97 };
98 
99 static void setup(void);
100 static void cleanup(void);
101 
102 static int immu_fd;
103 static int append_fd;
104 
105 int TST_TOTAL = sizeof(tc) / sizeof(tc[0]);
106 
main(int argc,char * argv[])107 int main(int argc, char *argv[])
108 {
109 	int lc;
110 	int i;
111 
112 	tst_parse_opts(argc, argv, NULL, NULL);
113 
114 	setup();
115 
116 	for (lc = 0; TEST_LOOPING(lc); lc++) {
117 		tst_count = 0;
118 
119 		for (i = 0; i < TST_TOTAL; i++) {
120 			TEST(setxattr(tc[i].fname, tc[i].key, tc[i].value,
121 				      tc[i].size, tc[i].flags));
122 
123 			if (TEST_ERRNO == tc[i].exp_err) {
124 				tst_resm(TPASS | TTERRNO, "%s", tc[i].desc);
125 			} else {
126 				tst_resm(TFAIL | TTERRNO, "%s - expected errno"
127 					 " %d - Got", tc[i].desc,
128 					 tc[i].exp_err);
129 			}
130 		}
131 	}
132 
133 	cleanup();
134 	tst_exit();
135 }
136 
fsetflag(int fd,int on,int immutable)137 static int fsetflag(int fd, int on, int immutable)
138 {
139 	int fsflags = 0;
140 	int fsfl;
141 
142 	if (ioctl(fd, FS_IOC_GETFLAGS, &fsflags) < 0)
143 		return 1;
144 
145 	if (immutable)
146 		fsfl = FS_IMMUTABLE_FL;
147 	else
148 		fsfl = FS_APPEND_FL;
149 
150 	if (on)
151 		fsflags |= fsfl;
152 	else
153 		fsflags &= ~fsfl;
154 
155 	if (ioctl(fd, FS_IOC_SETFLAGS, &fsflags) < 0)
156 		return 1;
157 
158 	return 0;
159 }
160 
setup(void)161 static void setup(void)
162 {
163 	int fd;
164 
165 	tst_require_root();
166 
167 	tst_tmpdir();
168 
169 	/* Test for xattr support */
170 	fd = creat("testfile", 0644);
171 	if (fd == -1)
172 		tst_brkm(TBROK | TERRNO, cleanup, "Create testfile failed");
173 	close(fd);
174 	if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
175 		if (errno == ENOTSUP)
176 			tst_brkm(TCONF, cleanup, "No xattr support in fs or "
177 				 "fs mounted without user_xattr option");
178 	unlink("testfile");
179 
180 	/* Create test files and set file immutable or append-only */
181 	immu_fd = creat(IMMU_FILE, 0644);
182 	if (immu_fd == -1)
183 		tst_brkm(TBROK | TERRNO, cleanup, "Create test file(%s) failed",
184 			 IMMU_FILE);
185 	if (set_immutable_on(immu_fd))
186 		tst_brkm(TBROK | TERRNO, cleanup, "Set %s immutable failed",
187 			 IMMU_FILE);
188 
189 	append_fd = creat(APPEND_FILE, 0644);
190 	if (append_fd == -1)
191 		tst_brkm(TBROK | TERRNO, cleanup, "Create test file(%s) failed",
192 			 APPEND_FILE);
193 	if (set_append_on(append_fd))
194 		tst_brkm(TBROK | TERRNO, cleanup, "Set %s append-only failed",
195 			 APPEND_FILE);
196 
197 	TEST_PAUSE;
198 }
199 
cleanup(void)200 static void cleanup(void)
201 {
202 	if ((immu_fd > 0) && set_immutable_off(immu_fd))
203 		tst_resm(TWARN | TERRNO, "Unset %s immutable failed",
204 			 IMMU_FILE);
205 	if ((append_fd > 0) && set_append_off(append_fd))
206 		tst_resm(TWARN | TERRNO, "Unset %s append-only failed",
207 			 APPEND_FILE);
208 	close(immu_fd);
209 	close(append_fd);
210 
211 	tst_rmdir();
212 }
213 #else
main(void)214 int main(void)
215 {
216 	tst_brkm(TCONF, NULL, "<attr/xattr.h> not present or FS_IOC_FLAGS "
217 		 "missing in <linux/fs.h>");
218 }
219 #endif /* defined HAVE_ATTR_XATTR_H && defined HAVE_FS_IOC_FLAGS */
220