Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | setfiles: based on policycoreutils 2.0.19 |
| 3 | policycoreutils was released under GPL 2. |
Denis Vlasenko | 9fe9c1a | 2007-07-23 14:05:02 +0000 | [diff] [blame] | 4 | Port to BusyBox (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp> |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 5 | */ |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 6 | //config:config SETFILES |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 7 | //config: bool "setfiles (13 kb)" |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 8 | //config: default n |
| 9 | //config: depends on SELINUX |
| 10 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 11 | //config: Enable support to modify to relabel files. |
| 12 | //config: Notice: If you built libselinux with -D_FILE_OFFSET_BITS=64, |
| 13 | //config: (It is default in libselinux's Makefile), you _must_ enable |
| 14 | //config: CONFIG_LFS. |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 15 | //config: |
| 16 | //config:config FEATURE_SETFILES_CHECK_OPTION |
| 17 | //config: bool "Enable check option" |
| 18 | //config: default n |
| 19 | //config: depends on SETFILES |
| 20 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 21 | //config: Support "-c" option (check the validity of the contexts against |
| 22 | //config: the specified binary policy) for setfiles. Requires libsepol. |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 23 | //config: |
| 24 | //config:config RESTORECON |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 25 | //config: bool "restorecon (12 kb)" |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 26 | //config: default n |
| 27 | //config: depends on SELINUX |
| 28 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 29 | //config: Enable support to relabel files. The feature is almost |
| 30 | //config: the same as setfiles, but usage is a little different. |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 31 | |
| 32 | //applet:IF_SETFILES(APPLET(setfiles, BB_DIR_SBIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 33 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | a8e52da | 2016-11-23 18:46:40 +0100 | [diff] [blame] | 34 | //applet:IF_RESTORECON(APPLET_ODDNAME(restorecon, setfiles, BB_DIR_SBIN, BB_SUID_DROP, restorecon)) |
| 35 | |
| 36 | //kbuild:lib-$(CONFIG_SETFILES) += setfiles.o |
| 37 | //kbuild:lib-$(CONFIG_RESTORECON) += setfiles.o |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 38 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 39 | //usage:#define setfiles_trivial_usage |
| 40 | //usage: "[-dnpqsvW] [-e DIR]... [-o FILE] [-r alt_root_path]" |
| 41 | //usage: IF_FEATURE_SETFILES_CHECK_OPTION( |
| 42 | //usage: " [-c policyfile] spec_file" |
| 43 | //usage: ) |
| 44 | //usage: " pathname" |
| 45 | //usage:#define setfiles_full_usage "\n\n" |
| 46 | //usage: "Reset file contexts under pathname according to spec_file\n" |
| 47 | //usage: IF_FEATURE_SETFILES_CHECK_OPTION( |
| 48 | //usage: "\n -c FILE Check the validity of the contexts against the specified binary policy" |
| 49 | //usage: ) |
| 50 | //usage: "\n -d Show which specification matched each file" |
| 51 | //usage: "\n -l Log changes in file labels to syslog" |
Denys Vlasenko | 80f0f1d | 2016-03-30 18:17:35 +0200 | [diff] [blame] | 52 | //TODO: log to syslog is not yet implemented, it goes to stdout only now |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 53 | //usage: "\n -n Don't change any file labels" |
| 54 | //usage: "\n -q Suppress warnings" |
| 55 | //usage: "\n -r DIR Use an alternate root path" |
| 56 | //usage: "\n -e DIR Exclude DIR" |
| 57 | //usage: "\n -F Force reset of context to match file_context for customizable files" |
| 58 | //usage: "\n -o FILE Save list of files with incorrect context" |
| 59 | //usage: "\n -s Take a list of files from stdin (instead of command line)" |
| 60 | //usage: "\n -v Show changes in file labels, if type or role are changing" |
| 61 | //usage: "\n -vv Show changes in file labels, if type, role, or user are changing" |
| 62 | //usage: "\n -W Display warnings about entries that had no matching files" |
| 63 | //usage: |
| 64 | //usage:#define restorecon_trivial_usage |
| 65 | //usage: "[-iFnRv] [-e EXCLUDEDIR]... [-o FILE] [-f FILE]" |
| 66 | //usage:#define restorecon_full_usage "\n\n" |
| 67 | //usage: "Reset security contexts of files in pathname\n" |
| 68 | //usage: "\n -i Ignore files that don't exist" |
| 69 | //usage: "\n -f FILE File with list of files to process" |
| 70 | //usage: "\n -e DIR Directory to exclude" |
| 71 | //usage: "\n -R,-r Recurse" |
| 72 | //usage: "\n -n Don't change any file labels" |
| 73 | //usage: "\n -o FILE Save list of files with incorrect context" |
| 74 | //usage: "\n -v Verbose" |
| 75 | //usage: "\n -vv Show changed labels" |
| 76 | //usage: "\n -F Force reset of context to match file_context" |
| 77 | //usage: "\n for customizable files, or the user section," |
| 78 | //usage: "\n if it has changed" |
| 79 | |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 80 | #include "libbb.h" |
Mike Frysinger | 1cda879 | 2016-12-09 18:14:12 -0500 | [diff] [blame] | 81 | #include "common_bufsiz.h" |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 82 | #if ENABLE_FEATURE_SETFILES_CHECK_OPTION |
| 83 | #include <sepol/sepol.h> |
| 84 | #endif |
| 85 | |
| 86 | #define MAX_EXCLUDES 50 |
| 87 | |
| 88 | struct edir { |
| 89 | char *directory; |
| 90 | size_t size; |
| 91 | }; |
| 92 | |
| 93 | struct globals { |
| 94 | FILE *outfile; |
| 95 | char *policyfile; |
| 96 | char *rootpath; |
| 97 | int rootpathlen; |
| 98 | unsigned count; |
| 99 | int excludeCtr; |
| 100 | int errors; |
| 101 | int verbose; /* getopt32 uses it, has to be int */ |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 102 | smallint recurse; /* Recursive descent */ |
| 103 | smallint follow_mounts; |
| 104 | /* Behavior flags determined based on setfiles vs. restorecon */ |
| 105 | smallint expand_realpath; /* Expand paths via realpath */ |
| 106 | smallint abort_on_error; /* Abort the file tree walk upon an error */ |
| 107 | int add_assoc; /* Track inode associations for conflict detection */ |
| 108 | int matchpathcon_flags; /* Flags to matchpathcon */ |
| 109 | dev_t dev_id; /* Device id where target file exists */ |
| 110 | int nerr; |
| 111 | struct edir excludeArray[MAX_EXCLUDES]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 112 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 113 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 114 | void BUG_setfiles_globals_too_big(void); |
| 115 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 116 | setup_common_bufsiz(); \ |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 117 | if (sizeof(G) > COMMON_BUFSIZE) \ |
| 118 | BUG_setfiles_globals_too_big(); \ |
| 119 | /* memset(&G, 0, sizeof(G)); - already is */ \ |
| 120 | } while (0) |
| 121 | #define outfile (G.outfile ) |
| 122 | #define policyfile (G.policyfile ) |
| 123 | #define rootpath (G.rootpath ) |
| 124 | #define rootpathlen (G.rootpathlen ) |
| 125 | #define count (G.count ) |
| 126 | #define excludeCtr (G.excludeCtr ) |
| 127 | #define errors (G.errors ) |
| 128 | #define verbose (G.verbose ) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 129 | #define recurse (G.recurse ) |
| 130 | #define follow_mounts (G.follow_mounts ) |
| 131 | #define expand_realpath (G.expand_realpath ) |
| 132 | #define abort_on_error (G.abort_on_error ) |
| 133 | #define add_assoc (G.add_assoc ) |
| 134 | #define matchpathcon_flags (G.matchpathcon_flags) |
| 135 | #define dev_id (G.dev_id ) |
| 136 | #define nerr (G.nerr ) |
| 137 | #define excludeArray (G.excludeArray ) |
| 138 | |
| 139 | /* Must match getopt32 string! */ |
| 140 | enum { |
| 141 | OPT_d = (1 << 0), |
| 142 | OPT_e = (1 << 1), |
| 143 | OPT_f = (1 << 2), |
| 144 | OPT_i = (1 << 3), |
| 145 | OPT_l = (1 << 4), |
| 146 | OPT_n = (1 << 5), |
| 147 | OPT_p = (1 << 6), |
| 148 | OPT_q = (1 << 7), |
| 149 | OPT_r = (1 << 8), |
| 150 | OPT_s = (1 << 9), |
| 151 | OPT_v = (1 << 10), |
| 152 | OPT_o = (1 << 11), |
| 153 | OPT_F = (1 << 12), |
| 154 | OPT_W = (1 << 13), |
| 155 | OPT_c = (1 << 14), /* c only for setfiles */ |
| 156 | OPT_R = (1 << 14), /* R only for restorecon */ |
| 157 | }; |
| 158 | #define FLAG_d_debug (option_mask32 & OPT_d) |
| 159 | #define FLAG_e (option_mask32 & OPT_e) |
| 160 | #define FLAG_f (option_mask32 & OPT_f) |
| 161 | #define FLAG_i_ignore_enoent (option_mask32 & OPT_i) |
| 162 | #define FLAG_l_take_log (option_mask32 & OPT_l) |
| 163 | #define FLAG_n_dry_run (option_mask32 & OPT_n) |
| 164 | #define FLAG_p_progress (option_mask32 & OPT_p) |
| 165 | #define FLAG_q_quiet (option_mask32 & OPT_q) |
| 166 | #define FLAG_r (option_mask32 & OPT_r) |
| 167 | #define FLAG_s (option_mask32 & OPT_s) |
| 168 | #define FLAG_v (option_mask32 & OPT_v) |
| 169 | #define FLAG_o (option_mask32 & OPT_o) |
| 170 | #define FLAG_F_force (option_mask32 & OPT_F) |
| 171 | #define FLAG_W_warn_no_match (option_mask32 & OPT_W) |
| 172 | #define FLAG_c (option_mask32 & OPT_c) |
| 173 | #define FLAG_R (option_mask32 & OPT_R) |
| 174 | |
| 175 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 176 | static void qprintf(const char *fmt UNUSED_PARAM, ...) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 177 | { |
| 178 | /* quiet, do nothing */ |
| 179 | } |
| 180 | |
| 181 | static void inc_err(void) |
| 182 | { |
| 183 | nerr++; |
| 184 | if (nerr > 9 && !FLAG_d_debug) { |
| 185 | bb_error_msg_and_die("exiting after 10 errors"); |
| 186 | } |
| 187 | } |
| 188 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 189 | static void add_exclude(const char *directory) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 190 | { |
| 191 | struct stat sb; |
Denis Vlasenko | 1da7772 | 2007-07-24 06:17:43 +0000 | [diff] [blame] | 192 | size_t len; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 193 | |
| 194 | if (directory == NULL || directory[0] != '/') { |
Denis Vlasenko | 972fa09 | 2007-07-25 17:39:46 +0000 | [diff] [blame] | 195 | bb_error_msg_and_die("full path required for exclude: %s", directory); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 196 | } |
| 197 | if (lstat(directory, &sb)) { |
| 198 | bb_error_msg("directory \"%s\" not found, ignoring", directory); |
Denis Vlasenko | 972fa09 | 2007-07-25 17:39:46 +0000 | [diff] [blame] | 199 | return; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 200 | } |
| 201 | if ((sb.st_mode & S_IFDIR) == 0) { |
| 202 | bb_error_msg("\"%s\" is not a directory: mode %o, ignoring", |
| 203 | directory, sb.st_mode); |
Denis Vlasenko | 972fa09 | 2007-07-25 17:39:46 +0000 | [diff] [blame] | 204 | return; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 205 | } |
| 206 | if (excludeCtr == MAX_EXCLUDES) { |
Denis Vlasenko | 972fa09 | 2007-07-25 17:39:46 +0000 | [diff] [blame] | 207 | bb_error_msg_and_die("maximum excludes %d exceeded", MAX_EXCLUDES); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | len = strlen(directory); |
| 211 | while (len > 1 && directory[len - 1] == '/') { |
| 212 | len--; |
| 213 | } |
| 214 | excludeArray[excludeCtr].directory = xstrndup(directory, len); |
| 215 | excludeArray[excludeCtr++].size = len; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Denis Vlasenko | 1da7772 | 2007-07-24 06:17:43 +0000 | [diff] [blame] | 218 | static bool exclude(const char *file) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 219 | { |
| 220 | int i = 0; |
| 221 | for (i = 0; i < excludeCtr; i++) { |
| 222 | if (strncmp(file, excludeArray[i].directory, |
| 223 | excludeArray[i].size) == 0) { |
| 224 | if (file[excludeArray[i].size] == '\0' |
| 225 | || file[excludeArray[i].size] == '/') { |
| 226 | return 1; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int match(const char *name, struct stat *sb, char **con) |
| 234 | { |
| 235 | int ret; |
| 236 | char path[PATH_MAX + 1]; |
| 237 | char *tmp_path = xstrdup(name); |
| 238 | |
Denis Vlasenko | 9fe9c1a | 2007-07-23 14:05:02 +0000 | [diff] [blame] | 239 | if (excludeCtr > 0 && exclude(name)) { |
| 240 | goto err; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 241 | } |
| 242 | ret = lstat(name, sb); |
| 243 | if (ret) { |
| 244 | if (FLAG_i_ignore_enoent && errno == ENOENT) { |
| 245 | free(tmp_path); |
| 246 | return 0; |
| 247 | } |
| 248 | bb_error_msg("stat(%s)", name); |
| 249 | goto err; |
| 250 | } |
| 251 | |
| 252 | if (expand_realpath) { |
| 253 | if (S_ISLNK(sb->st_mode)) { |
| 254 | char *p = NULL; |
| 255 | char *file_sep; |
| 256 | |
| 257 | size_t len = 0; |
| 258 | |
| 259 | if (verbose > 1) |
| 260 | bb_error_msg("warning! %s refers to a symbolic link, not following last component", name); |
| 261 | |
| 262 | file_sep = strrchr(tmp_path, '/'); |
| 263 | if (file_sep == tmp_path) { |
| 264 | file_sep++; |
Denis Vlasenko | a0ad43b | 2007-09-28 11:37:38 +0000 | [diff] [blame] | 265 | path[0] = '\0'; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 266 | p = path; |
| 267 | } else if (file_sep) { |
| 268 | *file_sep++ = '\0'; |
| 269 | p = realpath(tmp_path, path); |
| 270 | } else { |
| 271 | file_sep = tmp_path; |
| 272 | p = realpath("./", path); |
| 273 | } |
| 274 | if (p) |
| 275 | len = strlen(p); |
| 276 | if (!p || len + strlen(file_sep) + 2 > PATH_MAX) { |
| 277 | bb_perror_msg("realpath(%s) failed", name); |
| 278 | goto err; |
| 279 | } |
| 280 | p += len; |
| 281 | /* ensure trailing slash of directory name */ |
| 282 | if (len == 0 || p[-1] != '/') { |
| 283 | *p++ = '/'; |
| 284 | } |
| 285 | strcpy(p, file_sep); |
| 286 | name = path; |
| 287 | if (excludeCtr > 0 && exclude(name)) |
| 288 | goto err; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 289 | } else { |
| 290 | char *p; |
| 291 | p = realpath(name, path); |
| 292 | if (!p) { |
| 293 | bb_perror_msg("realpath(%s)", name); |
| 294 | goto err; |
| 295 | } |
| 296 | name = p; |
| 297 | if (excludeCtr > 0 && exclude(name)) |
| 298 | goto err; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* name will be what is matched in the policy */ |
| 303 | if (NULL != rootpath) { |
| 304 | if (0 != strncmp(rootpath, name, rootpathlen)) { |
| 305 | bb_error_msg("%s is not located in %s", |
| 306 | name, rootpath); |
| 307 | goto err; |
| 308 | } |
| 309 | name += rootpathlen; |
| 310 | } |
| 311 | |
| 312 | free(tmp_path); |
| 313 | if (rootpath != NULL && name[0] == '\0') |
| 314 | /* this is actually the root dir of the alt root */ |
| 315 | return matchpathcon_index("/", sb->st_mode, con); |
| 316 | return matchpathcon_index(name, sb->st_mode, con); |
| 317 | err: |
| 318 | free(tmp_path); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | /* Compare two contexts to see if their differences are "significant", |
| 323 | * or whether the only difference is in the user. */ |
Denis Vlasenko | 1da7772 | 2007-07-24 06:17:43 +0000 | [diff] [blame] | 324 | static bool only_changed_user(const char *a, const char *b) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 325 | { |
| 326 | if (FLAG_F_force) |
| 327 | return 0; |
| 328 | if (!a || !b) |
| 329 | return 0; |
| 330 | a = strchr(a, ':'); /* Rest of the context after the user */ |
| 331 | b = strchr(b, ':'); |
| 332 | if (!a || !b) |
| 333 | return 0; |
| 334 | return (strcmp(a, b) == 0); |
| 335 | } |
| 336 | |
| 337 | static int restore(const char *file) |
| 338 | { |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 339 | char *my_file; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 340 | struct stat my_sb; |
| 341 | int i, j, ret; |
| 342 | char *context = NULL; |
| 343 | char *newcon = NULL; |
Denis Vlasenko | 1da7772 | 2007-07-24 06:17:43 +0000 | [diff] [blame] | 344 | bool user_only_changed = 0; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 345 | int retval = 0; |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 346 | |
| 347 | my_file = bb_simplify_path(file); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 348 | |
| 349 | i = match(my_file, &my_sb, &newcon); |
| 350 | |
| 351 | if (i < 0) /* No matching specification. */ |
| 352 | goto out; |
| 353 | |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 354 | if (FLAG_p_progress) { |
| 355 | count++; |
| 356 | if (count % 0x400 == 0) { /* every 1024 times */ |
| 357 | count = (count % (80*0x400)); |
| 358 | if (count == 0) |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 359 | bb_putchar('\n'); |
| 360 | bb_putchar('*'); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 361 | fflush_all(); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Try to add an association between this inode and |
| 367 | * this specification. If there is already an association |
| 368 | * for this inode and it conflicts with this specification, |
| 369 | * then use the last matching specification. |
| 370 | */ |
| 371 | if (add_assoc) { |
| 372 | j = matchpathcon_filespec_add(my_sb.st_ino, i, my_file); |
| 373 | if (j < 0) |
| 374 | goto err; |
| 375 | |
| 376 | if (j != i) { |
| 377 | /* There was already an association and it took precedence. */ |
| 378 | goto out; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (FLAG_d_debug) |
| 383 | printf("%s: %s matched by %s\n", applet_name, my_file, newcon); |
| 384 | |
| 385 | /* Get the current context of the file. */ |
| 386 | ret = lgetfilecon_raw(my_file, &context); |
| 387 | if (ret < 0) { |
| 388 | if (errno == ENODATA) { |
| 389 | context = NULL; /* paranoia */ |
| 390 | } else { |
| 391 | bb_perror_msg("lgetfilecon_raw on %s", my_file); |
| 392 | goto err; |
| 393 | } |
| 394 | user_only_changed = 0; |
| 395 | } else |
| 396 | user_only_changed = only_changed_user(context, newcon); |
| 397 | |
| 398 | /* |
| 399 | * Do not relabel the file if the matching specification is |
| 400 | * <<none>> or the file is already labeled according to the |
| 401 | * specification. |
| 402 | */ |
| 403 | if ((strcmp(newcon, "<<none>>") == 0) |
| 404 | || (context && (strcmp(context, newcon) == 0) && !FLAG_F_force)) { |
| 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | if (!FLAG_F_force && context && (is_context_customizable(context) > 0)) { |
| 409 | if (verbose > 1) { |
| 410 | bb_error_msg("skipping %s. %s is customizable_types", |
| 411 | my_file, context); |
| 412 | } |
| 413 | goto out; |
| 414 | } |
| 415 | |
| 416 | if (verbose) { |
| 417 | /* If we're just doing "-v", trim out any relabels where |
| 418 | * the user has changed but the role and type are the |
| 419 | * same. For "-vv", emit everything. */ |
| 420 | if (verbose > 1 || !user_only_changed) { |
Denys Vlasenko | 80f0f1d | 2016-03-30 18:17:35 +0200 | [diff] [blame] | 421 | printf("%s: reset %s context %s->%s\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 422 | applet_name, my_file, context ? context : "", newcon); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | if (FLAG_l_take_log && !user_only_changed) { |
| 427 | if (context) |
Denys Vlasenko | 80f0f1d | 2016-03-30 18:17:35 +0200 | [diff] [blame] | 428 | printf("relabeling %s from %s to %s\n", my_file, context, newcon); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 429 | else |
Denys Vlasenko | 80f0f1d | 2016-03-30 18:17:35 +0200 | [diff] [blame] | 430 | printf("labeling %s to %s\n", my_file, newcon); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | if (outfile && !user_only_changed) |
| 434 | fprintf(outfile, "%s\n", my_file); |
| 435 | |
| 436 | /* |
| 437 | * Do not relabel the file if -n was used. |
| 438 | */ |
| 439 | if (FLAG_n_dry_run || user_only_changed) |
| 440 | goto out; |
| 441 | |
| 442 | /* |
| 443 | * Relabel the file to the specified context. |
| 444 | */ |
| 445 | ret = lsetfilecon(my_file, newcon); |
| 446 | if (ret) { |
| 447 | bb_perror_msg("lsetfileconon(%s,%s)", my_file, newcon); |
| 448 | goto err; |
| 449 | } |
| 450 | |
| 451 | out: |
| 452 | freecon(context); |
| 453 | freecon(newcon); |
Denis Vlasenko | 84d71f3 | 2007-08-06 02:36:35 +0000 | [diff] [blame] | 454 | free(my_file); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 455 | return retval; |
| 456 | err: |
| 457 | retval--; /* -1 */ |
| 458 | goto out; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Apply the last matching specification to a file. |
| 463 | * This function is called by recursive_action on each file during |
| 464 | * the directory traversal. |
| 465 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 466 | static int FAST_FUNC apply_spec( |
Denis Vlasenko | 592d4fe | 2008-03-17 09:19:26 +0000 | [diff] [blame] | 467 | const char *file, |
| 468 | struct stat *sb, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 469 | void *userData UNUSED_PARAM, |
| 470 | int depth UNUSED_PARAM) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 471 | { |
| 472 | if (!follow_mounts) { |
| 473 | /* setfiles does not process across different mount points */ |
| 474 | if (sb->st_dev != dev_id) { |
| 475 | return SKIP; |
| 476 | } |
| 477 | } |
| 478 | errors |= restore(file); |
| 479 | if (abort_on_error && errors) |
| 480 | return FALSE; |
| 481 | return TRUE; |
| 482 | } |
| 483 | |
| 484 | |
| 485 | static int canoncon(const char *path, unsigned lineno, char **contextp) |
| 486 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 487 | static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s"; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 488 | |
| 489 | char *tmpcon; |
| 490 | char *context = *contextp; |
| 491 | int invalid = 0; |
| 492 | |
| 493 | #if ENABLE_FEATURE_SETFILES_CHECK_OPTION |
| 494 | if (policyfile) { |
| 495 | if (sepol_check_context(context) >= 0) |
| 496 | return 0; |
| 497 | /* Exit immediately if we're in checking mode. */ |
| 498 | bb_error_msg_and_die(err_msg, path, lineno, context); |
| 499 | } |
| 500 | #endif |
| 501 | |
| 502 | if (security_canonicalize_context_raw(context, &tmpcon) < 0) { |
| 503 | if (errno != ENOENT) { |
| 504 | invalid = 1; |
| 505 | inc_err(); |
| 506 | } |
| 507 | } else { |
| 508 | free(context); |
| 509 | *contextp = tmpcon; |
| 510 | } |
| 511 | |
| 512 | if (invalid) { |
| 513 | bb_error_msg(err_msg, path, lineno, context); |
| 514 | } |
| 515 | |
| 516 | return invalid; |
| 517 | } |
| 518 | |
| 519 | static int process_one(char *name) |
| 520 | { |
| 521 | struct stat sb; |
| 522 | int rc; |
| 523 | |
| 524 | rc = lstat(name, &sb); |
| 525 | if (rc < 0) { |
| 526 | if (FLAG_i_ignore_enoent && errno == ENOENT) |
| 527 | return 0; |
| 528 | bb_perror_msg("stat(%s)", name); |
| 529 | goto err; |
| 530 | } |
| 531 | dev_id = sb.st_dev; |
| 532 | |
| 533 | if (S_ISDIR(sb.st_mode) && recurse) { |
| 534 | if (recursive_action(name, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 535 | ACTION_RECURSE, |
| 536 | apply_spec, |
| 537 | apply_spec, |
| 538 | NULL, 0) != TRUE |
| 539 | ) { |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 540 | bb_error_msg("error while labeling %s", name); |
| 541 | goto err; |
| 542 | } |
| 543 | } else { |
| 544 | rc = restore(name); |
| 545 | if (rc) |
| 546 | goto err; |
| 547 | } |
| 548 | |
| 549 | out: |
| 550 | if (add_assoc) { |
| 551 | if (FLAG_q_quiet) |
| 552 | set_matchpathcon_printf(&qprintf); |
| 553 | matchpathcon_filespec_eval(); |
| 554 | set_matchpathcon_printf(NULL); |
| 555 | matchpathcon_filespec_destroy(); |
| 556 | } |
| 557 | |
| 558 | return rc; |
| 559 | |
| 560 | err: |
| 561 | rc = -1; |
| 562 | goto out; |
| 563 | } |
| 564 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 565 | int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 566 | int setfiles_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 567 | { |
| 568 | struct stat sb; |
| 569 | int rc, i = 0; |
| 570 | const char *input_filename = NULL; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 571 | char *buf = NULL; |
| 572 | size_t buf_len; |
| 573 | int flags; |
| 574 | llist_t *exclude_dir = NULL; |
| 575 | char *out_filename = NULL; |
| 576 | |
| 577 | INIT_G(); |
| 578 | |
| 579 | if (applet_name[0] == 's') { /* "setfiles" */ |
| 580 | /* |
| 581 | * setfiles: |
| 582 | * Recursive descent, |
| 583 | * Does not expand paths via realpath, |
| 584 | * Aborts on errors during the file tree walk, |
| 585 | * Try to track inode associations for conflict detection, |
| 586 | * Does not follow mounts, |
| 587 | * Validates all file contexts at init time. |
| 588 | */ |
| 589 | recurse = 1; |
| 590 | abort_on_error = 1; |
| 591 | add_assoc = 1; |
| 592 | /* follow_mounts = 0; - already is */ |
| 593 | matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS; |
| 594 | } else { |
| 595 | /* |
| 596 | * restorecon: |
| 597 | * No recursive descent unless -r/-R, |
| 598 | * Expands paths via realpath, |
| 599 | * Do not abort on errors during the file tree walk, |
| 600 | * Do not try to track inode associations for conflict detection, |
| 601 | * Follows mounts, |
| 602 | * Does lazy validation of contexts upon use. |
| 603 | */ |
| 604 | expand_realpath = 1; |
| 605 | follow_mounts = 1; |
| 606 | matchpathcon_flags = MATCHPATHCON_NOTRANS; |
| 607 | /* restorecon only */ |
| 608 | selinux_or_die(); |
| 609 | } |
| 610 | |
| 611 | set_matchpathcon_flags(matchpathcon_flags); |
| 612 | |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 613 | opt_complementary = "vv:v--p:p--v:v--q:q--v"; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 614 | /* Option order must match OPT_x definitions! */ |
| 615 | if (applet_name[0] == 'r') { /* restorecon */ |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 616 | flags = getopt32(argv, "de:*f:ilnpqrsvo:FWR", |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 617 | &exclude_dir, &input_filename, &out_filename, &verbose); |
| 618 | } else { /* setfiles */ |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 619 | flags = getopt32(argv, "de:*f:ilnpqr:svo:FW" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 620 | IF_FEATURE_SETFILES_CHECK_OPTION("c:"), |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 621 | &exclude_dir, &input_filename, &rootpath, &out_filename, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 622 | IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 623 | &verbose); |
| 624 | } |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 625 | argv += optind; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 626 | |
| 627 | #if ENABLE_FEATURE_SETFILES_CHECK_OPTION |
| 628 | if ((applet_name[0] == 's') && (flags & OPT_c)) { |
| 629 | FILE *policystream; |
| 630 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 631 | policystream = xfopen_for_read(policyfile); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 632 | if (sepol_set_policydb_from_file(policystream) < 0) { |
| 633 | bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile); |
| 634 | } |
| 635 | fclose(policystream); |
| 636 | |
| 637 | /* Only process the specified file_contexts file, not |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 638 | * any .homedirs or .local files, and do not perform |
| 639 | * context translations. */ |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 640 | set_matchpathcon_flags(MATCHPATHCON_BASEONLY | |
| 641 | MATCHPATHCON_NOTRANS | |
| 642 | MATCHPATHCON_VALIDATE); |
| 643 | } |
| 644 | #endif |
| 645 | |
Denis Vlasenko | 972fa09 | 2007-07-25 17:39:46 +0000 | [diff] [blame] | 646 | while (exclude_dir) |
| 647 | add_exclude(llist_pop(&exclude_dir)); |
| 648 | |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 649 | if (flags & OPT_o) { |
| 650 | outfile = stdout; |
| 651 | if (NOT_LONE_CHAR(out_filename, '-')) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 652 | outfile = xfopen_for_write(out_filename); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 655 | if (applet_name[0] == 'r') { /* restorecon */ |
| 656 | if (flags & (OPT_r | OPT_R)) |
| 657 | recurse = 1; |
| 658 | } else { /* setfiles */ |
| 659 | if (flags & OPT_r) |
| 660 | rootpathlen = strlen(rootpath); |
| 661 | } |
| 662 | if (flags & OPT_s) { |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 663 | input_filename = "-"; |
| 664 | add_assoc = 0; |
| 665 | } |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 666 | |
| 667 | if (applet_name[0] == 's') { /* setfiles */ |
| 668 | /* Use our own invalid context checking function so that |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 669 | * we can support either checking against the active policy or |
| 670 | * checking against a binary policy file. */ |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 671 | set_matchpathcon_canoncon(&canoncon); |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 672 | if (!argv[0]) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 673 | bb_show_usage(); |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 674 | xstat(argv[0], &sb); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 675 | if (!S_ISREG(sb.st_mode)) { |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 676 | bb_error_msg_and_die("spec file %s is not a regular file", argv[0]); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 677 | } |
| 678 | /* Load the file contexts configuration and check it. */ |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 679 | rc = matchpathcon_init(argv[0]); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 680 | if (rc < 0) { |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 681 | bb_simple_perror_msg_and_die(argv[0]); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 682 | } |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 683 | if (nerr) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 684 | exit(EXIT_FAILURE); |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 685 | argv++; |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Denis Vlasenko | 84d71f3 | 2007-08-06 02:36:35 +0000 | [diff] [blame] | 688 | if (input_filename) { |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 689 | ssize_t len; |
| 690 | FILE *f = stdin; |
| 691 | |
| 692 | if (NOT_LONE_CHAR(input_filename, '-')) |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 693 | f = xfopen_for_read(input_filename); |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 694 | while ((len = getline(&buf, &buf_len, f)) > 0) { |
| 695 | buf[len - 1] = '\0'; |
| 696 | errors |= process_one(buf); |
| 697 | } |
| 698 | if (ENABLE_FEATURE_CLEAN_UP) |
| 699 | fclose_if_not_stdin(f); |
| 700 | } else { |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 701 | if (!argv[0]) |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 702 | bb_show_usage(); |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 703 | for (i = 0; argv[i]; i++) { |
Denis Vlasenko | 397137b | 2007-07-23 14:03:30 +0000 | [diff] [blame] | 704 | errors |= process_one(argv[i]); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | if (FLAG_W_warn_no_match) |
| 709 | matchpathcon_checkmatches(argv[0]); |
| 710 | |
| 711 | if (ENABLE_FEATURE_CLEAN_UP && outfile) |
| 712 | fclose(outfile); |
| 713 | |
| 714 | return errors; |
| 715 | } |