/src/selinux/libselinux/src/matchpathcon.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include <assert.h> |
2 | | #include <sys/stat.h> |
3 | | #include <string.h> |
4 | | #include <errno.h> |
5 | | #include <stdio.h> |
6 | | #include "selinux_internal.h" |
7 | | #include "label_internal.h" |
8 | | #include "callbacks.h" |
9 | | #include <limits.h> |
10 | | |
11 | | static int (*myinvalidcon) (const char *p, unsigned l, char *c) = NULL; |
12 | | static int (*mycanoncon) (const char *p, unsigned l, char **c) = NULL; |
13 | | |
14 | | static void |
15 | | #ifdef __GNUC__ |
16 | | __attribute__ ((format(printf, 1, 2))) |
17 | | #endif |
18 | | default_printf(const char *fmt, ...) |
19 | 0 | { |
20 | 0 | va_list ap; |
21 | 0 | va_start(ap, fmt); |
22 | 0 | vfprintf(stderr, fmt, ap); |
23 | 0 | va_end(ap); |
24 | 0 | } |
25 | | |
26 | | void |
27 | | #ifdef __GNUC__ |
28 | | __attribute__ ((format(printf, 1, 2))) |
29 | | #endif |
30 | | (*myprintf) (const char *fmt,...) = &default_printf; |
31 | | int myprintf_compat = 0; |
32 | | |
33 | | void set_matchpathcon_printf(void (*f) (const char *fmt, ...)) |
34 | 0 | { |
35 | 0 | myprintf = f ? f : &default_printf; |
36 | 0 | myprintf_compat = 1; |
37 | 0 | } |
38 | | |
39 | | int compat_validate(const struct selabel_handle *rec, |
40 | | struct selabel_lookup_rec *contexts, |
41 | | const char *path, unsigned lineno) |
42 | 0 | { |
43 | 0 | int rc; |
44 | 0 | char **ctx = &contexts->ctx_raw; |
45 | |
|
46 | 0 | if (myinvalidcon) |
47 | 0 | rc = myinvalidcon(path, lineno, *ctx); |
48 | 0 | else if (mycanoncon) |
49 | 0 | rc = mycanoncon(path, lineno, ctx); |
50 | 0 | else if (rec->validating) { |
51 | 0 | rc = selabel_validate(contexts); |
52 | 0 | if (rc < 0) { |
53 | 0 | if (lineno) { |
54 | 0 | COMPAT_LOG(SELINUX_WARNING, |
55 | 0 | "%s: line %u has invalid context %s\n", |
56 | 0 | path, lineno, *ctx); |
57 | 0 | } else { |
58 | 0 | COMPAT_LOG(SELINUX_WARNING, |
59 | 0 | "%s: has invalid context %s\n", path, *ctx); |
60 | 0 | } |
61 | 0 | } |
62 | 0 | } else |
63 | 0 | rc = 0; |
64 | |
|
65 | 0 | return rc ? -1 : 0; |
66 | 0 | } |
67 | | |
68 | | #ifndef BUILD_HOST |
69 | | |
70 | | static __thread struct selabel_handle *hnd; |
71 | | |
72 | | /* |
73 | | * An array for mapping integers to contexts |
74 | | */ |
75 | | static __thread char **con_array; |
76 | | static __thread int con_array_size; |
77 | | static __thread int con_array_used; |
78 | | |
79 | | static pthread_once_t once = PTHREAD_ONCE_INIT; |
80 | | static pthread_key_t destructor_key; |
81 | | static int destructor_key_initialized = 0; |
82 | | |
83 | | static void free_array_elts(void) |
84 | 0 | { |
85 | 0 | int i; |
86 | 0 | for (i = 0; i < con_array_used; i++) |
87 | 0 | free(con_array[i]); |
88 | 0 | free(con_array); |
89 | |
|
90 | 0 | con_array_size = con_array_used = 0; |
91 | 0 | con_array = NULL; |
92 | 0 | } |
93 | | |
94 | | static int add_array_elt(char *con) |
95 | 0 | { |
96 | 0 | char **tmp; |
97 | 0 | if (con_array_size) { |
98 | 0 | while (con_array_used >= con_array_size) { |
99 | 0 | con_array_size *= 2; |
100 | 0 | tmp = (char **)reallocarray(con_array, con_array_size, |
101 | 0 | sizeof(char*)); |
102 | 0 | if (!tmp) { |
103 | 0 | free_array_elts(); |
104 | 0 | return -1; |
105 | 0 | } |
106 | 0 | con_array = tmp; |
107 | 0 | } |
108 | 0 | } else { |
109 | 0 | con_array_size = 1000; |
110 | 0 | con_array = (char **)malloc(sizeof(char*) * con_array_size); |
111 | 0 | if (!con_array) { |
112 | 0 | con_array_size = con_array_used = 0; |
113 | 0 | return -1; |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | con_array[con_array_used] = strdup(con); |
118 | 0 | if (!con_array[con_array_used]) |
119 | 0 | return -1; |
120 | 0 | return con_array_used++; |
121 | 0 | } |
122 | | |
123 | | void set_matchpathcon_invalidcon(int (*f) (const char *p, unsigned l, char *c)) |
124 | 0 | { |
125 | 0 | myinvalidcon = f; |
126 | 0 | } |
127 | | |
128 | | static int default_canoncon(const char *path, unsigned lineno, char **context) |
129 | 0 | { |
130 | 0 | char *tmpcon; |
131 | 0 | if (security_canonicalize_context_raw(*context, &tmpcon) < 0) { |
132 | 0 | if (errno == ENOENT) |
133 | 0 | return 0; |
134 | 0 | if (lineno) |
135 | 0 | myprintf("%s: line %u has invalid context %s\n", path, |
136 | 0 | lineno, *context); |
137 | 0 | else |
138 | 0 | myprintf("%s: invalid context %s\n", path, *context); |
139 | 0 | return 1; |
140 | 0 | } |
141 | 0 | free(*context); |
142 | 0 | *context = tmpcon; |
143 | 0 | return 0; |
144 | 0 | } |
145 | | |
146 | | void set_matchpathcon_canoncon(int (*f) (const char *p, unsigned l, char **c)) |
147 | 0 | { |
148 | 0 | if (f) |
149 | 0 | mycanoncon = f; |
150 | 0 | else |
151 | 0 | mycanoncon = &default_canoncon; |
152 | 0 | } |
153 | | |
154 | | static __thread struct selinux_opt options[SELABEL_NOPT]; |
155 | | static __thread int notrans; |
156 | | |
157 | | void set_matchpathcon_flags(unsigned int flags) |
158 | 0 | { |
159 | 0 | int i; |
160 | 0 | memset(options, 0, sizeof(options)); |
161 | 0 | i = SELABEL_OPT_BASEONLY; |
162 | 0 | options[i].type = i; |
163 | 0 | options[i].value = (flags & MATCHPATHCON_BASEONLY) ? (char*)1 : NULL; |
164 | 0 | i = SELABEL_OPT_VALIDATE; |
165 | 0 | options[i].type = i; |
166 | 0 | options[i].value = (flags & MATCHPATHCON_VALIDATE) ? (char*)1 : NULL; |
167 | 0 | notrans = flags & MATCHPATHCON_NOTRANS; |
168 | 0 | } |
169 | | |
170 | | /* |
171 | | * An association between an inode and a |
172 | | * specification. |
173 | | */ |
174 | | typedef struct file_spec { |
175 | | ino_t ino; /* inode number */ |
176 | | int specind; /* index of specification in spec */ |
177 | | char *file; /* full pathname for diagnostic messages about conflicts */ |
178 | | struct file_spec *next; /* next association in hash bucket chain */ |
179 | | } file_spec_t; |
180 | | |
181 | | /* |
182 | | * The hash table of associations, hashed by inode number. |
183 | | * Chaining is used for collisions, with elements ordered |
184 | | * by inode number in each bucket. Each hash bucket has a dummy |
185 | | * header. |
186 | | */ |
187 | 0 | #define HASH_BITS 16 |
188 | 0 | #define HASH_BUCKETS (1 << HASH_BITS) |
189 | 0 | #define HASH_MASK (HASH_BUCKETS-1) |
190 | | static file_spec_t *fl_head; |
191 | | |
192 | | /* |
193 | | * Try to add an association between an inode and |
194 | | * a specification. If there is already an association |
195 | | * for the inode and it conflicts with this specification, |
196 | | * then use the specification that occurs later in the |
197 | | * specification array. |
198 | | */ |
199 | | int matchpathcon_filespec_add(ino_t ino, int specind, const char *file) |
200 | 0 | { |
201 | 0 | file_spec_t *prevfl, *fl; |
202 | 0 | int h, ret; |
203 | 0 | struct stat sb; |
204 | |
|
205 | 0 | if (!fl_head) { |
206 | 0 | fl_head = calloc(HASH_BUCKETS, sizeof(file_spec_t)); |
207 | 0 | if (!fl_head) |
208 | 0 | goto oom; |
209 | 0 | } |
210 | | |
211 | 0 | h = (ino + (ino >> HASH_BITS)) & HASH_MASK; |
212 | 0 | for (prevfl = &fl_head[h], fl = fl_head[h].next; fl; |
213 | 0 | prevfl = fl, fl = fl->next) { |
214 | 0 | if (ino == fl->ino) { |
215 | 0 | ret = lstat(fl->file, &sb); |
216 | 0 | if (ret < 0 || sb.st_ino != ino) { |
217 | 0 | fl->specind = specind; |
218 | 0 | free(fl->file); |
219 | 0 | fl->file = strdup(file); |
220 | 0 | if (!fl->file) |
221 | 0 | goto oom; |
222 | 0 | return fl->specind; |
223 | |
|
224 | 0 | } |
225 | | |
226 | 0 | if (!strcmp(con_array[fl->specind], |
227 | 0 | con_array[specind])) |
228 | 0 | return fl->specind; |
229 | | |
230 | 0 | myprintf |
231 | 0 | ("%s: conflicting specifications for %s and %s, using %s.\n", |
232 | 0 | __FUNCTION__, file, fl->file, |
233 | 0 | con_array[fl->specind]); |
234 | 0 | free(fl->file); |
235 | 0 | fl->file = strdup(file); |
236 | 0 | if (!fl->file) |
237 | 0 | goto oom; |
238 | 0 | return fl->specind; |
239 | 0 | } |
240 | | |
241 | 0 | if (ino > fl->ino) |
242 | 0 | break; |
243 | 0 | } |
244 | | |
245 | 0 | fl = malloc(sizeof(file_spec_t)); |
246 | 0 | if (!fl) |
247 | 0 | goto oom; |
248 | 0 | fl->ino = ino; |
249 | 0 | fl->specind = specind; |
250 | 0 | fl->file = strdup(file); |
251 | 0 | if (!fl->file) |
252 | 0 | goto oom_freefl; |
253 | 0 | fl->next = prevfl->next; |
254 | 0 | prevfl->next = fl; |
255 | 0 | return fl->specind; |
256 | 0 | oom_freefl: |
257 | 0 | free(fl); |
258 | 0 | oom: |
259 | 0 | myprintf("%s: insufficient memory for file label entry for %s\n", |
260 | 0 | __FUNCTION__, file); |
261 | 0 | return -1; |
262 | 0 | } |
263 | | |
264 | | #if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) && defined(__INO64_T_TYPE) && !defined(__INO_T_MATCHES_INO64_T) |
265 | | /* alias defined in the public header but we undefine it here */ |
266 | | #undef matchpathcon_filespec_add |
267 | | |
268 | | /* ABI backwards-compatible shim for non-LFS 32-bit systems */ |
269 | | |
270 | | static_assert(sizeof(unsigned long) == sizeof(__ino_t), "inode size mismatch"); |
271 | | static_assert(sizeof(unsigned long) == sizeof(uint32_t), "inode size mismatch"); |
272 | | static_assert(sizeof(ino_t) == sizeof(ino64_t), "inode size mismatch"); |
273 | | static_assert(sizeof(ino64_t) == sizeof(uint64_t), "inode size mismatch"); |
274 | | |
275 | | extern int matchpathcon_filespec_add(unsigned long ino, int specind, |
276 | | const char *file); |
277 | | |
278 | | int matchpathcon_filespec_add(unsigned long ino, int specind, |
279 | | const char *file) |
280 | | { |
281 | | return matchpathcon_filespec_add64(ino, specind, file); |
282 | | } |
283 | | #elif (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || defined(__INO_T_MATCHES_INO64_T) |
284 | | |
285 | | static_assert(sizeof(uint64_t) == sizeof(ino_t), "inode size mismatch"); |
286 | | |
287 | | #else |
288 | | |
289 | | static_assert(sizeof(uint32_t) == sizeof(ino_t), "inode size mismatch"); |
290 | | |
291 | | #endif |
292 | | |
293 | | /* |
294 | | * Evaluate the association hash table distribution. |
295 | | */ |
296 | | void matchpathcon_filespec_eval(void) |
297 | 0 | { |
298 | 0 | file_spec_t *fl; |
299 | 0 | int h, used, nel, len, longest; |
300 | |
|
301 | 0 | if (!fl_head) |
302 | 0 | return; |
303 | | |
304 | 0 | used = 0; |
305 | 0 | longest = 0; |
306 | 0 | nel = 0; |
307 | 0 | for (h = 0; h < HASH_BUCKETS; h++) { |
308 | 0 | len = 0; |
309 | 0 | for (fl = fl_head[h].next; fl; fl = fl->next) { |
310 | 0 | len++; |
311 | 0 | } |
312 | 0 | if (len) |
313 | 0 | used++; |
314 | 0 | if (len > longest) |
315 | 0 | longest = len; |
316 | 0 | nel += len; |
317 | 0 | } |
318 | |
|
319 | 0 | myprintf |
320 | 0 | ("%s: hash table stats: %d elements, %d/%d buckets used, longest chain length %d\n", |
321 | 0 | __FUNCTION__, nel, used, HASH_BUCKETS, longest); |
322 | 0 | } |
323 | | |
324 | | /* |
325 | | * Destroy the association hash table. |
326 | | */ |
327 | | void matchpathcon_filespec_destroy(void) |
328 | 0 | { |
329 | 0 | file_spec_t *fl, *tmp; |
330 | 0 | int h; |
331 | |
|
332 | 0 | free_array_elts(); |
333 | |
|
334 | 0 | if (!fl_head) |
335 | 0 | return; |
336 | | |
337 | 0 | for (h = 0; h < HASH_BUCKETS; h++) { |
338 | 0 | fl = fl_head[h].next; |
339 | 0 | while (fl) { |
340 | 0 | tmp = fl; |
341 | 0 | fl = fl->next; |
342 | 0 | free(tmp->file); |
343 | 0 | free(tmp); |
344 | 0 | } |
345 | 0 | fl_head[h].next = NULL; |
346 | 0 | } |
347 | 0 | free(fl_head); |
348 | 0 | fl_head = NULL; |
349 | 0 | } |
350 | | |
351 | | static void matchpathcon_fini_internal(void) |
352 | 0 | { |
353 | 0 | free_array_elts(); |
354 | |
|
355 | 0 | if (hnd) { |
356 | 0 | selabel_close(hnd); |
357 | 0 | hnd = NULL; |
358 | 0 | } |
359 | 0 | } |
360 | | |
361 | | static void matchpathcon_thread_destructor(void __attribute__((unused)) *ptr) |
362 | 0 | { |
363 | 0 | matchpathcon_fini_internal(); |
364 | 0 | } |
365 | | |
366 | | void __attribute__((destructor)) matchpathcon_lib_destructor(void); |
367 | | |
368 | | void __attribute__((destructor)) matchpathcon_lib_destructor(void) |
369 | 0 | { |
370 | 0 | if (destructor_key_initialized) |
371 | 0 | __selinux_key_delete(destructor_key); |
372 | 0 | } |
373 | | |
374 | | static void matchpathcon_init_once(void) |
375 | 0 | { |
376 | 0 | if (__selinux_key_create(&destructor_key, matchpathcon_thread_destructor) == 0) |
377 | 0 | destructor_key_initialized = 1; |
378 | 0 | } |
379 | | |
380 | | int matchpathcon_init_prefix(const char *path, const char *subset) |
381 | 0 | { |
382 | 0 | if (!mycanoncon) |
383 | 0 | mycanoncon = default_canoncon; |
384 | |
|
385 | 0 | __selinux_once(once, matchpathcon_init_once); |
386 | 0 | __selinux_setspecific(destructor_key, /* some valid address to please GCC */ &selinux_page_size); |
387 | |
|
388 | 0 | options[SELABEL_OPT_SUBSET].type = SELABEL_OPT_SUBSET; |
389 | 0 | options[SELABEL_OPT_SUBSET].value = subset; |
390 | 0 | options[SELABEL_OPT_PATH].type = SELABEL_OPT_PATH; |
391 | 0 | options[SELABEL_OPT_PATH].value = path; |
392 | |
|
393 | 0 | hnd = selabel_open(SELABEL_CTX_FILE, options, SELABEL_NOPT); |
394 | 0 | return hnd ? 0 : -1; |
395 | 0 | } |
396 | | |
397 | | |
398 | | int matchpathcon_init(const char *path) |
399 | 0 | { |
400 | 0 | return matchpathcon_init_prefix(path, NULL); |
401 | 0 | } |
402 | | |
403 | | void matchpathcon_fini(void) |
404 | 0 | { |
405 | 0 | matchpathcon_fini_internal(); |
406 | 0 | } |
407 | | |
408 | | /* |
409 | | * We do not want to resolve a symlink to a real path if it is the final |
410 | | * component of the name. Thus we split the pathname on the last "/" and |
411 | | * determine a real path component of the first portion. We then have to |
412 | | * copy the last part back on to get the final real path. Wheww. |
413 | | */ |
414 | | int realpath_not_final(const char *name, char *resolved_path) |
415 | 0 | { |
416 | 0 | char *last_component; |
417 | 0 | char *tmp_path, *p; |
418 | 0 | size_t len = 0; |
419 | 0 | int rc = 0; |
420 | |
|
421 | 0 | tmp_path = strdup(name); |
422 | 0 | if (!tmp_path) { |
423 | 0 | myprintf("symlink_realpath(%s) strdup() failed: %m\n", |
424 | 0 | name); |
425 | 0 | rc = -1; |
426 | 0 | goto out; |
427 | 0 | } |
428 | | |
429 | 0 | last_component = strrchr(tmp_path, '/'); |
430 | |
|
431 | 0 | if (last_component == tmp_path) { |
432 | 0 | last_component++; |
433 | 0 | p = strcpy(resolved_path, ""); |
434 | 0 | } else if (last_component) { |
435 | 0 | *last_component = '\0'; |
436 | 0 | last_component++; |
437 | 0 | p = realpath(tmp_path, resolved_path); |
438 | 0 | } else { |
439 | 0 | last_component = tmp_path; |
440 | 0 | p = realpath("./", resolved_path); |
441 | 0 | } |
442 | |
|
443 | 0 | if (!p) { |
444 | 0 | myprintf("symlink_realpath(%s) realpath() failed: %m\n", |
445 | 0 | name); |
446 | 0 | rc = -1; |
447 | 0 | goto out; |
448 | 0 | } |
449 | | |
450 | 0 | len = strlen(p); |
451 | 0 | if (len + strlen(last_component) + 2 > PATH_MAX) { |
452 | 0 | myprintf("symlink_realpath(%s) failed: Filename too long \n", |
453 | 0 | name); |
454 | 0 | errno = ENAMETOOLONG; |
455 | 0 | rc = -1; |
456 | 0 | goto out; |
457 | 0 | } |
458 | | |
459 | 0 | resolved_path += len; |
460 | 0 | strcpy(resolved_path, "/"); |
461 | 0 | resolved_path += 1; |
462 | 0 | strcpy(resolved_path, last_component); |
463 | 0 | out: |
464 | 0 | free(tmp_path); |
465 | 0 | return rc; |
466 | 0 | } |
467 | | |
468 | | static int matchpathcon_internal(const char *path, mode_t mode, char ** con) |
469 | 0 | { |
470 | 0 | char stackpath[PATH_MAX + 1]; |
471 | 0 | char *p = NULL; |
472 | 0 | if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0)) |
473 | 0 | return -1; |
474 | | |
475 | 0 | if (S_ISLNK(mode)) { |
476 | 0 | if (!realpath_not_final(path, stackpath)) |
477 | 0 | path = stackpath; |
478 | 0 | } else { |
479 | 0 | p = realpath(path, stackpath); |
480 | 0 | if (p) |
481 | 0 | path = p; |
482 | 0 | } |
483 | |
|
484 | 0 | return notrans ? |
485 | 0 | selabel_lookup_raw(hnd, con, path, mode) : |
486 | 0 | selabel_lookup(hnd, con, path, mode); |
487 | 0 | } |
488 | | |
489 | 0 | int matchpathcon(const char *path, mode_t mode, char ** con) { |
490 | 0 | return matchpathcon_internal(path, mode, con); |
491 | 0 | } |
492 | | |
493 | | int matchpathcon_index(const char *name, mode_t mode, char ** con) |
494 | 0 | { |
495 | 0 | int i = matchpathcon_internal(name, mode, con); |
496 | |
|
497 | 0 | if (i < 0) |
498 | 0 | return -1; |
499 | | |
500 | 0 | return add_array_elt(*con); |
501 | 0 | } |
502 | | |
503 | | void matchpathcon_checkmatches(char *str __attribute__((unused))) |
504 | 0 | { |
505 | 0 | selabel_stats(hnd); |
506 | 0 | } |
507 | | |
508 | | /* Compare two contexts to see if their differences are "significant", |
509 | | * or whether the only difference is in the user. */ |
510 | | int selinux_file_context_cmp(const char * a, |
511 | | const char * b) |
512 | 0 | { |
513 | 0 | const char *rest_a, *rest_b; /* Rest of the context after the user */ |
514 | 0 | if (!a && !b) |
515 | 0 | return 0; |
516 | 0 | if (!a) |
517 | 0 | return -1; |
518 | 0 | if (!b) |
519 | 0 | return 1; |
520 | 0 | rest_a = strchr(a, ':'); |
521 | 0 | rest_b = strchr(b, ':'); |
522 | 0 | if (!rest_a && !rest_b) |
523 | 0 | return 0; |
524 | 0 | if (!rest_a) |
525 | 0 | return -1; |
526 | 0 | if (!rest_b) |
527 | 0 | return 1; |
528 | 0 | return strcmp(rest_a, rest_b); |
529 | 0 | } |
530 | | |
531 | | int selinux_file_context_verify(const char *path, mode_t mode) |
532 | 0 | { |
533 | 0 | char * con = NULL; |
534 | 0 | char * fcontext = NULL; |
535 | 0 | int rc = 0; |
536 | 0 | char stackpath[PATH_MAX + 1]; |
537 | 0 | char *p = NULL; |
538 | |
|
539 | 0 | if (S_ISLNK(mode)) { |
540 | 0 | if (!realpath_not_final(path, stackpath)) |
541 | 0 | path = stackpath; |
542 | 0 | } else { |
543 | 0 | p = realpath(path, stackpath); |
544 | 0 | if (p) |
545 | 0 | path = p; |
546 | 0 | } |
547 | |
|
548 | 0 | rc = lgetfilecon_raw(path, &con); |
549 | 0 | if (rc == -1) { |
550 | 0 | if (errno != ENOTSUP) |
551 | 0 | return -1; |
552 | 0 | else |
553 | 0 | return 0; |
554 | 0 | } |
555 | | |
556 | 0 | if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0)){ |
557 | 0 | freecon(con); |
558 | 0 | return -1; |
559 | 0 | } |
560 | | |
561 | 0 | if (selabel_lookup_raw(hnd, &fcontext, path, mode) != 0) { |
562 | 0 | if (errno != ENOENT) |
563 | 0 | rc = -1; |
564 | 0 | else |
565 | 0 | rc = 0; |
566 | 0 | } else { |
567 | | /* |
568 | | * Need to set errno to 0 as it can be set to ENOENT if the |
569 | | * file_contexts.subs file does not exist (see selabel_open in |
570 | | * label.c), thus causing confusion if errno is checked on return. |
571 | | */ |
572 | 0 | errno = 0; |
573 | 0 | rc = (selinux_file_context_cmp(fcontext, con) == 0); |
574 | 0 | } |
575 | |
|
576 | 0 | freecon(con); |
577 | 0 | freecon(fcontext); |
578 | 0 | return rc; |
579 | 0 | } |
580 | | |
581 | | int selinux_lsetfilecon_default(const char *path) |
582 | 0 | { |
583 | 0 | struct stat st; |
584 | 0 | int rc = -1; |
585 | 0 | char * scontext = NULL; |
586 | 0 | if (lstat(path, &st) != 0) |
587 | 0 | return rc; |
588 | | |
589 | 0 | if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0)) |
590 | 0 | return -1; |
591 | | |
592 | | /* If there's an error determining the context, or it has none, |
593 | | return to allow default context */ |
594 | 0 | if (selabel_lookup_raw(hnd, &scontext, path, st.st_mode)) { |
595 | 0 | if (errno == ENOENT) |
596 | 0 | rc = 0; |
597 | 0 | } else { |
598 | 0 | rc = lsetfilecon_raw(path, scontext); |
599 | 0 | freecon(scontext); |
600 | 0 | } |
601 | 0 | return rc; |
602 | 0 | } |
603 | | |
604 | | #endif |