Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 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" |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 53 | |
| 54 | char *TCID = "setxattr03"; |
| 55 | |
Cyril Hrubis | c57112e | 2012-02-10 14:06:43 +0100 | [diff] [blame] | 56 | #if defined HAVE_ATTR_XATTR_H && defined HAVE_FS_IOC_FLAGS |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 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[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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 | }, |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 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 | |
| 107 | int main(int argc, char *argv[]) |
| 108 | { |
| 109 | int lc; |
| 110 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 111 | const char *msg; |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 112 | |
| 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 Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 120 | tst_count = 0; |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 121 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | for (i = 0; i < TST_TOTAL; i++) { |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 123 | TEST(setxattr(tc[i].fname, tc[i].key, tc[i].value, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | tc[i].size, tc[i].flags)); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 125 | |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | " %d - Got", tc[i].desc, |
| 131 | tc[i].exp_err); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | cleanup(); |
| 137 | tst_exit(); |
| 138 | } |
| 139 | |
| 140 | static 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 | |
| 164 | static 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | "fs mounted without user_xattr option"); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 181 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 187 | IMMU_FILE); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 188 | if (set_immutable_on(immu_fd)) |
| 189 | tst_brkm(TBROK | TERRNO, cleanup, "Set %s immutable failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 190 | IMMU_FILE); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 191 | |
| 192 | append_fd = creat(APPEND_FILE, 0644); |
| 193 | if (append_fd == -1) |
| 194 | tst_brkm(TBROK | TERRNO, cleanup, "Create test file(%s) failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 195 | APPEND_FILE); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 196 | if (set_append_on(append_fd)) |
| 197 | tst_brkm(TBROK | TERRNO, cleanup, "Set %s append-only failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 198 | APPEND_FILE); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 199 | |
| 200 | TEST_PAUSE; |
| 201 | } |
| 202 | |
| 203 | static void cleanup(void) |
| 204 | { |
Eryu Guan | 15be7af | 2012-07-02 18:53:10 +0800 | [diff] [blame] | 205 | if ((immu_fd > 0) && set_immutable_off(immu_fd)) |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 206 | tst_resm(TWARN | TERRNO, "Unset %s immutable failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 207 | IMMU_FILE); |
Eryu Guan | 15be7af | 2012-07-02 18:53:10 +0800 | [diff] [blame] | 208 | if ((append_fd > 0) && set_append_off(append_fd)) |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 209 | tst_resm(TWARN | TERRNO, "Unset %s append-only failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 210 | APPEND_FILE); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 211 | close(immu_fd); |
| 212 | close(append_fd); |
| 213 | |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 214 | tst_rmdir(); |
| 215 | } |
Cyril Hrubis | c57112e | 2012-02-10 14:06:43 +0100 | [diff] [blame] | 216 | #else |
Cyril Hrubis | bd8ae72 | 2012-02-10 15:01:19 +0100 | [diff] [blame] | 217 | int main(void) |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 218 | { |
Cyril Hrubis | c57112e | 2012-02-10 14:06:43 +0100 | [diff] [blame] | 219 | tst_brkm(TCONF, NULL, "<attr/xattr.h> not present or FS_IOC_FLAGS " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 220 | "missing in <linux/fs.h>"); |
Eryu Guan | 7bb21e4 | 2012-01-20 17:54:08 +0800 | [diff] [blame] | 221 | } |
Cyril Hrubis | c57112e | 2012-02-10 14:06:43 +0100 | [diff] [blame] | 222 | #endif /* defined HAVE_ATTR_XATTR_H && defined HAVE_FS_IOC_FLAGS */ |