blob: 2e0f6c569f630794ab5bd97c84d3623361652194 [file] [log] [blame]
Eryu Guan7bb21e42012-01-20 17:54:08 +08001/*
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"
Eryu Guan7bb21e42012-01-20 17:54:08 +080053
54char *TCID = "setxattr03";
55
Cyril Hrubisc57112e2012-02-10 14:06:43 +010056#if defined HAVE_ATTR_XATTR_H && defined HAVE_FS_IOC_FLAGS
Eryu Guan7bb21e42012-01-20 17:54:08 +080057#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
69struct 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};
78static struct test_case tc[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 { /* 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 },
Eryu Guan7bb21e42012-01-20 17:54:08 +080097};
98
99static void setup(void);
100static void cleanup(void);
101
102static int immu_fd;
103static int append_fd;
104
105int TST_TOTAL = sizeof(tc) / sizeof(tc[0]);
106
107int main(int argc, char *argv[])
108{
109 int lc;
110 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200111 const char *msg;
Eryu Guan7bb21e42012-01-20 17:54:08 +0800112
113 msg = parse_opts(argc, argv, NULL, NULL);
114 if (msg != NULL)
115 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
116
117 setup();
118
119 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800120 tst_count = 0;
Eryu Guan7bb21e42012-01-20 17:54:08 +0800121
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 for (i = 0; i < TST_TOTAL; i++) {
Eryu Guan7bb21e42012-01-20 17:54:08 +0800123 TEST(setxattr(tc[i].fname, tc[i].key, tc[i].value,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 tc[i].size, tc[i].flags));
Eryu Guan7bb21e42012-01-20 17:54:08 +0800125
126 if (TEST_ERRNO == tc[i].exp_err) {
127 tst_resm(TPASS | TTERRNO, "%s", tc[i].desc);
128 } else {
129 tst_resm(TFAIL | TTERRNO, "%s - expected errno"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 " %d - Got", tc[i].desc,
131 tc[i].exp_err);
Eryu Guan7bb21e42012-01-20 17:54:08 +0800132 }
133 }
134 }
135
136 cleanup();
137 tst_exit();
138}
139
140static int fsetflag(int fd, int on, int immutable)
141{
142 int fsflags = 0;
143 int fsfl;
144
145 if (ioctl(fd, FS_IOC_GETFLAGS, &fsflags) < 0)
146 return 1;
147
148 if (immutable)
149 fsfl = FS_IMMUTABLE_FL;
150 else
151 fsfl = FS_APPEND_FL;
152
153 if (on)
154 fsflags |= fsfl;
155 else
156 fsflags &= ~fsfl;
157
158 if (ioctl(fd, FS_IOC_SETFLAGS, &fsflags) < 0)
159 return 1;
160
161 return 0;
162}
163
164static void setup(void)
165{
166 int fd;
167
168 tst_require_root(NULL);
169
170 tst_tmpdir();
171
172 /* Test for xattr support */
173 fd = creat("testfile", 0644);
174 if (fd == -1)
175 tst_brkm(TBROK | TERRNO, cleanup, "Create testfile failed");
176 close(fd);
177 if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
178 if (errno == ENOTSUP)
179 tst_brkm(TCONF, cleanup, "No xattr support in fs or "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 "fs mounted without user_xattr option");
Eryu Guan7bb21e42012-01-20 17:54:08 +0800181 unlink("testfile");
182
183 /* Create test files and set file immutable or append-only */
184 immu_fd = creat(IMMU_FILE, 0644);
185 if (immu_fd == -1)
186 tst_brkm(TBROK | TERRNO, cleanup, "Create test file(%s) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 IMMU_FILE);
Eryu Guan7bb21e42012-01-20 17:54:08 +0800188 if (set_immutable_on(immu_fd))
189 tst_brkm(TBROK | TERRNO, cleanup, "Set %s immutable failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 IMMU_FILE);
Eryu Guan7bb21e42012-01-20 17:54:08 +0800191
192 append_fd = creat(APPEND_FILE, 0644);
193 if (append_fd == -1)
194 tst_brkm(TBROK | TERRNO, cleanup, "Create test file(%s) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800195 APPEND_FILE);
Eryu Guan7bb21e42012-01-20 17:54:08 +0800196 if (set_append_on(append_fd))
197 tst_brkm(TBROK | TERRNO, cleanup, "Set %s append-only failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 APPEND_FILE);
Eryu Guan7bb21e42012-01-20 17:54:08 +0800199
200 TEST_PAUSE;
201}
202
203static void cleanup(void)
204{
Eryu Guan15be7af2012-07-02 18:53:10 +0800205 if ((immu_fd > 0) && set_immutable_off(immu_fd))
Eryu Guan7bb21e42012-01-20 17:54:08 +0800206 tst_resm(TWARN | TERRNO, "Unset %s immutable failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800207 IMMU_FILE);
Eryu Guan15be7af2012-07-02 18:53:10 +0800208 if ((append_fd > 0) && set_append_off(append_fd))
Eryu Guan7bb21e42012-01-20 17:54:08 +0800209 tst_resm(TWARN | TERRNO, "Unset %s append-only failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800210 APPEND_FILE);
Eryu Guan7bb21e42012-01-20 17:54:08 +0800211 close(immu_fd);
212 close(append_fd);
213
Eryu Guan7bb21e42012-01-20 17:54:08 +0800214 tst_rmdir();
215}
Cyril Hrubisc57112e2012-02-10 14:06:43 +0100216#else
Cyril Hrubisbd8ae722012-02-10 15:01:19 +0100217int main(void)
Eryu Guan7bb21e42012-01-20 17:54:08 +0800218{
Cyril Hrubisc57112e2012-02-10 14:06:43 +0100219 tst_brkm(TCONF, NULL, "<attr/xattr.h> not present or FS_IOC_FLAGS "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220 "missing in <linux/fs.h>");
Eryu Guan7bb21e42012-01-20 17:54:08 +0800221}
Cyril Hrubisc57112e2012-02-10 14:06:43 +0100222#endif /* defined HAVE_ATTR_XATTR_H && defined HAVE_FS_IOC_FLAGS */