Debian Codegraph @

src/OVAL/probes/unix/file_probe.c

raw symbols SCIP
1 /**
2 * @file file_probe.c
3 * @brief file probe
4 * @author "Daniel Kopecek" <[email protected]>
5 *
6 * 2010/06/13 [email protected]
7 * This probe is able to process a file_object as defined in OVAL 5.4 and 5.5.
8 *
9 */
10
11 /*
12 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
13 * All Rights Reserved.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * Authors:
30 * "Daniel Kopecek" <[email protected]>
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include "_seap.h"
38 #include <probe-api.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <pthread.h>
43 #include <errno.h>
44 #include <limits.h>
45
46 #if defined(HAVE_ACL_LIBACL_H)
47 # include <sys/types.h>
48 # include <acl/libacl.h>
49 #endif
50
51 #if defined(HAVE_SYS_ACL_H) && defined(OS_SOLARIS)
52 # include <sys/acl.h>
53 #endif
54
55 #include <probe/probe.h>
56 #include <probe/option.h>
57 #include "oval_fts.h"
58 #include "SEAP/generic/rbt/rbt.h"
59 #include "common/debug_priv.h"
60 #include "file_probe.h"
61
62 #ifndef PATH_MAX
63 #define PATH_MAX 4096
64 #endif
65
66 #define FILE_SEPARATOR '/'
67
68 #define STR_REGULAR "regular"
69 #define STR_DIRECTORY "directory"
70 #define STR_SYMLINK "symbolic link"
71 #define STR_BLKSPEC "block special"
72 #define STR_FIFO "fifo"
73 #define STR_SOCKET "socket"
74 #define STR_CHARSPEC "character special"
75 #define STRLEN_PAIR(str) (str), strlen(str)
76 #if defined(OS_SOLARIS)
77 #define STR_DOOR "door"
78 #define STR_PORT "port"
79 #endif
80
81 struct gr_sexps {
82 SEXP_t *gr_t_reg;
83 SEXP_t *gr_t_dir;
84 SEXP_t *gr_t_lnk;
85 SEXP_t *gr_t_blk;
86 SEXP_t *gr_t_fifo;
87 SEXP_t *gr_t_sock;
88 SEXP_t *gr_t_char;
89 #if defined(OS_SOLARIS)
90 SEXP_t *gr_t_door;
91 SEXP_t *gr_t_port;
92 #endif
93 };
94
95 static struct gr_sexps *gr_sexps_init()
96 {
97 struct gr_sexps *s = malloc(sizeof(struct gr_sexps));
98 s->gr_t_reg = SEXP_string_new(STRLEN_PAIR(STR_REGULAR));
99 s->gr_t_dir = SEXP_string_new(STRLEN_PAIR(STR_DIRECTORY));
100 s->gr_t_lnk = SEXP_string_new(STRLEN_PAIR(STR_SYMLINK));
101 s->gr_t_blk = SEXP_string_new(STRLEN_PAIR(STR_BLKSPEC));
102 s->gr_t_fifo = SEXP_string_new(STRLEN_PAIR(STR_FIFO));
103 s->gr_t_sock = SEXP_string_new(STRLEN_PAIR(STR_SOCKET));
104 s->gr_t_char = SEXP_string_new(STRLEN_PAIR(STR_CHARSPEC));
105 #if defined(OS_SOLARIS)
106 s->gr_t_door = SEXP_string_new(STRLEN_PAIR(STR_DOOR));
107 s->gr_t_port = SEXP_string_new(STRLEN_PAIR(STR_PORT));
108 #endif
109 return s;
110 }
111
112 static void gr_sexps_free(struct gr_sexps *s)
113 {
114 SEXP_free(s->gr_t_reg);
115 SEXP_free(s->gr_t_dir);
116 SEXP_free(s->gr_t_lnk);
117 SEXP_free(s->gr_t_blk);
118 SEXP_free(s->gr_t_fifo);
119 SEXP_free(s->gr_t_sock);
120 SEXP_free(s->gr_t_char);
121 #if defined(OS_SOLARIS)
122 SEXP_free(s->gr_t_door);
123 SEXP_free(s->gr_t_port);
124 #endif
125 free(s);
126 }
127
128 static SEXP_t *se_filetype(struct gr_sexps *s, mode_t mode)
129 {
130 switch (mode & S_IFMT) {
131 case S_IFREG:
132 return s->gr_t_reg;
133 case S_IFDIR:
134 return s->gr_t_dir;
135 case S_IFLNK:
136 return s->gr_t_lnk;
137 case S_IFBLK:
138 return s->gr_t_blk;
139 case S_IFIFO:
140 return s->gr_t_fifo;
141 case S_IFSOCK:
142 return s->gr_t_sock;
143 case S_IFCHR:
144 return s->gr_t_char;
145 #if defined(OS_SOLARIS)
146 case S_IFDOOR:
147 return s->gr_t_door;
148 case S_IFPORT:
149 return s->gr_t_port;
150 #endif
151 default:
152 abort ();
153 }
154 /* NOTREACHED */
155 return (NULL);
156 }
157
158 struct cbargs {
159 probe_ctx *ctx;
160 int error;
161 };
162
163 struct ID_cache {
164 rbt_t *tree;
165 uint32_t max;
166 };
167
168 static SEXP_t *ID_cache_get(struct ID_cache *cache, int32_t id, oval_schema_version_t over)
169 {
170 SEXP_t *s_id = NULL, *s_id2 = NULL;
171
172 if (rbt_i32_get(cache->tree, id, (void *)&s_id) == 0)
173 return SEXP_ref(s_id); /* cache hit (first attempt) */
174
175 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.8)) < 0) {
176 s_id = SEXP_string_newf("%u", id);
177 } else {
178 s_id = SEXP_number_newu_32(id);
179 }
180
181 if (cache->max == 0 || rbt_i32_size(cache->tree) < cache->max) {
182 if (rbt_i32_add(cache->tree, id, (void *)s_id, NULL) == 0)
183 return SEXP_ref(s_id); /* insert succeeded */
184
185 if (rbt_i32_get(cache->tree, id, (void *)&s_id2) == 0) {
186 SEXP_free (s_id); /* cache hit (second attempt) */
187 return SEXP_ref(s_id2);
188 }
189 }
190
191 /* fallback */
192 return (s_id);
193 }
194
195 static struct ID_cache *ID_cache_init(uint32_t max)
196 {
197 struct ID_cache *cache = malloc(sizeof(struct ID_cache));
198 cache->max = max;
199 cache->tree = rbt_i32_new();
200 return cache;
201 }
202
203 static void ID_cache_free_cb(rbt_i32_node_t *n)
204 {
205 SEXP_free(n->data);
206 }
207
208 static void ID_cache_free(struct ID_cache *cache)
209 {
210 rbt_i32_free_cb(cache->tree, ID_cache_free_cb);
211 free(cache);
212 }
213
214 static SEXP_t *get_atime(struct stat *st, SEXP_t *sexp, oval_schema_version_t over)
215 {
216 uint64_t t = (
217 #if defined(OS_FREEBSD)
218 (uint64_t) st->st_atimespec.tv_sec
219 #elif defined(OS_LINUX) || defined(OS_SOLARIS)
220 (uint64_t) st->st_atim.tv_sec
221 #else /* Use the legacy field */
222 (uint64_t) st->st_atime
223 #endif
224 );
225
226 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.8)) < 0) {
227 return SEXP_string_newf_r(sexp, "%llu", (long long unsigned) t);
228 } else {
229 return SEXP_number_newu_64_r(sexp, t);
230 }
231 }
232
233 static SEXP_t *get_ctime(struct stat *st, SEXP_t *sexp, oval_schema_version_t over)
234 {
235 uint64_t t = (
236 #if defined(OS_FREEBSD)
237 (uint64_t) st->st_ctimespec.tv_sec
238 #elif defined(OS_LINUX) || defined(OS_SOLARIS)
239 (uint64_t) st->st_ctim.tv_sec
240 #else /* Use the legacy field */
241 (uint64_t) st->st_ctime
242 #endif
243 );
244
245 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.8)) < 0) {
246 return SEXP_string_newf_r(sexp, "%llu", (long long unsigned) t);
247 } else {
248 return SEXP_number_newu_64_r(sexp, t);
249 }
250 }
251
252 static SEXP_t *get_mtime(struct stat *st, SEXP_t *sexp, oval_schema_version_t over)
253 {
254 uint64_t t = (
255 #if defined(OS_FREEBSD)
256 (uint64_t) st->st_mtimespec.tv_sec
257 #elif defined(OS_LINUX) || defined(OS_SOLARIS)
258 (uint64_t) st->st_mtim.tv_sec
259 #else /* Use the legacy field */
260 (uint64_t) st->st_mtime
261 #endif
262 );
263
264 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.8)) < 0) {
265 return SEXP_string_newf_r(sexp, "%llu", (long long unsigned) t);
266 } else {
267 return SEXP_number_newu_64_r(sexp, t);
268 }
269 }
270
271 static SEXP_t *get_size(struct stat *st, SEXP_t *sexp)
272 {
273 switch (sizeof(st->st_size)) {
274 case 8:
275 return SEXP_number_newu_64_r(sexp, st->st_size);
276 case 4:
277 return SEXP_number_newu_32_r(sexp, st->st_size);
278 default: /* Should not happen. */
279 abort();
280
281 /* NOTREACHED */
282 return NULL;
283 }
284 }
285
286 static bool MODEP(struct stat *statp, unsigned int bit)
287 {
288 return (statp->st_mode & bit);
289 }
290
291 static SEXP_t *has_extended_acl(const char *path)
292 {
293 #if defined(HAVE_ACL_EXTENDED_FILE)
294 int has_acl = acl_extended_file(path);
295 if (has_acl == -1) {
296 dD("Getting extended ACL for file '%s' has failed, %s", path, strerror(errno));
297 return NULL;
298 }
299 return (has_acl == 1) ? SEXP_number_newb(true) : SEXP_number_newb(false);
300 #elif defined(OS_SOLARIS)
301 return acl_trivial(path) ? SEXP_number_newb(true) : SEXP_number_newb(false);
302 #else
303 return NULL;
304 #endif
305 }
306
307 static int file_cb(const char *prefix, const char *p, const char *f, void *ptr, oval_schema_version_t over, struct ID_cache *cache, struct gr_sexps *grs, SEXP_t *gr_lastpath, struct oscap_list *blocked_paths)
308 {
309 char path_buffer[PATH_MAX];
310 SEXP_t *item;
311 struct cbargs *args = (struct cbargs *) ptr;
312 struct stat st;
313 const char *st_path;
314
315 if (f == NULL) {
316 st_path = p;
317 } else {
318 const size_t p_len = strlen(p);
319 /* Avoid 2 slashes */
320 if (p_len >= 1 && p[p_len - 1] == FILE_SEPARATOR) {
321 snprintf(path_buffer, sizeof path_buffer, "%s%s", p, f);
322 } else {
323 snprintf(path_buffer, sizeof path_buffer, "%s%c%s", p, FILE_SEPARATOR, f);
324 }
325 st_path = path_buffer;
326 }
327
328 if (probe_path_is_blocked(st_path, blocked_paths)) {
329 return 0;
330 }
331
332 char *st_path_with_prefix = oscap_path_join(prefix, st_path);
333 if (lstat(st_path_with_prefix, &st) == -1) {
334 dD("lstat failed when processing %s: errno=%u, %s.", st_path, errno, strerror (errno));
335 /*
336 * Whatever the reason of this lstat error (for example the file may
337 * have disappeared) we don't want it to stop the whole file tree walk;
338 * so we just don't report the error.
339 */
340 free(st_path_with_prefix);
341 return 0;
342 } else {
343 SEXP_t *se_usr_id, *se_grp_id;
344 SEXP_t se_atime_mem, se_ctime_mem, se_mtime_mem, se_size_mem;
345 SEXP_t *se_filepath, *se_acl;
346
347 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.6)) < 0
348 || f == NULL) {
349 se_filepath = NULL;
350 } else {
351 se_filepath = SEXP_string_newf("%s", st_path);
352 }
353
354 se_usr_id = ID_cache_get(cache, st.st_uid, over);
355 se_grp_id = st.st_gid != st.st_uid ? ID_cache_get(cache, st.st_gid, over) : SEXP_ref(se_usr_id);
356
357 if (!SEXP_emptyp(gr_lastpath)) {
358 if (SEXP_strcmp(gr_lastpath, p) != 0) {
359 SEXP_free_r(gr_lastpath);
360 SEXP_string_new_r(gr_lastpath, p, strlen(p));
361 }
362 } else
363 SEXP_string_new_r(gr_lastpath, p, strlen(p));
364
365 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.7)) < 0) {
366 se_acl = NULL;
367 } else {
368 se_acl = has_extended_acl(st_path_with_prefix);
369 }
370 free(st_path_with_prefix);
371
372 item = probe_item_create(OVAL_UNIX_FILE, NULL,
373 "filepath", OVAL_DATATYPE_SEXP, se_filepath,
374 "path", OVAL_DATATYPE_SEXP, gr_lastpath,
375 "filename", OVAL_DATATYPE_STRING, f == NULL ? "" : f,
376 "type", OVAL_DATATYPE_SEXP, se_filetype(grs, st.st_mode),
377 "group_id", OVAL_DATATYPE_SEXP, se_grp_id,
378 "user_id", OVAL_DATATYPE_SEXP, se_usr_id,
379 "a_time", OVAL_DATATYPE_SEXP, get_atime(&st, &se_atime_mem, over),
380 "c_time", OVAL_DATATYPE_SEXP, get_ctime(&st, &se_ctime_mem, over),
381 "m_time", OVAL_DATATYPE_SEXP, get_mtime(&st, &se_mtime_mem, over),
382 "size", OVAL_DATATYPE_SEXP, get_size(&st, &se_size_mem),
383 "suid", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_ISUID),
384 "sgid", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_ISGID),
385 "sticky", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_ISVTX),
386 "uread", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IRUSR),
387 "uwrite", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IWUSR),
388 "uexec", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IXUSR),
389 "gread", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IRGRP),
390 "gwrite", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IWGRP),
391 "gexec", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IXGRP),
392 "oread", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IROTH),
393 "owrite", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IWOTH),
394 "oexec", OVAL_DATATYPE_BOOLEAN, MODEP(&st, S_IXOTH),
395 "has_extended_acl", OVAL_DATATYPE_SEXP, se_acl,
396 NULL);
397 if (se_acl == NULL) {
398 SEXP_t *sexp_true = SEXP_number_newb(true);
399 probe_item_ent_add(item, "has_extended_acl", NULL, sexp_true);
400 SEXP_free(sexp_true);
401 probe_itement_setstatus(item, "has_extended_acl", 1, SYSCHAR_STATUS_DOES_NOT_EXIST);
402 } else {
403 SEXP_free(se_acl);
404 }
405
406 SEXP_free(se_grp_id);
407 SEXP_free(se_usr_id);
408 SEXP_free(se_filepath);
409 SEXP_free_r(&se_atime_mem);
410 SEXP_free_r(&se_ctime_mem);
411 SEXP_free_r(&se_mtime_mem);
412 SEXP_free_r(&se_size_mem);
413
414 /*
415 * Stop collecting if we hit the memory usage limit
416 * (return code == 2)
417 */
418 return probe_item_collect(args->ctx, item) == 2 ? 1 : 0;
419 }
420
421 return (0);
422 }
423
424
425 int file_probe_offline_mode_supported()
426 {
427 return PROBE_OFFLINE_OWN;
428 }
429
430 void *file_probe_init(void)
431 {
432 /*
433 * Initialize mutex.
434 */
435 pthread_mutex_t *file_probe_mutex = malloc(sizeof(pthread_mutex_t));
436 switch (pthread_mutex_init (file_probe_mutex, NULL)) {
437 case 0:
438 return ((void *)file_probe_mutex);
439 default:
440 free(file_probe_mutex);
441 dD("Can't initialize mutex: errno=%u, %s.", errno, strerror (errno));
442 }
443 #if 0
444 probe_setoption(PROBEOPT_VARREF_HANDLING, false, "path");
445 probe_setoption(PROBEOPT_VARREF_HANDLING, false, "filename");
446 #endif
447 return (NULL);
448 }
449
450 void file_probe_fini(void *arg)
451 {
452 /*
453 * Destroy mutex.
454 */
455 (void) pthread_mutex_destroy((pthread_mutex_t *)arg);
456 free(arg);
457 }
458
459 int file_probe_main(probe_ctx *ctx, void *mutex)
460 {
461 SEXP_t *path, *filename, *behaviors, *filepath, *probe_in;
462 int err;
463 struct cbargs cbargs;
464 OVAL_FTS *ofts;
465 OVAL_FTSENT *ofts_ent;
466
467 if (mutex == NULL) {
468 return PROBE_EINIT;
469 }
470
471 probe_in = probe_ctx_getobject(ctx);
472
473 oval_schema_version_t over = probe_obj_get_platform_schema_version(probe_in);
474 path = probe_obj_getent (probe_in, "path", 1);
475 filename = probe_obj_getent (probe_in, "filename", 1);
476 behaviors = probe_obj_getent (probe_in, "behaviors", 1);
477 filepath = probe_obj_getent (probe_in, "filepath", 1);
478
479 /* we want either path+filename or filepath */
480 if ( (path == NULL || filename == NULL) && filepath==NULL ) {
481 SEXP_free (behaviors);
482 SEXP_free (path);
483 SEXP_free (filename);
484 SEXP_free (filepath);
485
486 return PROBE_ENOELM;
487 }
488
489 probe_filebehaviors_canonicalize(&behaviors);
490
491 switch (pthread_mutex_lock (mutex)) {
492 case 0:
493 break;
494 default:
495 dD("Can't lock mutex(%p): %u, %s.", mutex, errno, strerror (errno));
496
497 SEXP_free(path);
498 SEXP_free(filename);
499 SEXP_free(filepath);
500 SEXP_free(behaviors);
501
502 return PROBE_EFATAL;
503 }
504
505 cbargs.ctx = ctx;
506 cbargs.error = 0;
507
508 const char *prefix = getenv("OSCAP_PROBE_ROOT");
509 SEXP_t gr_lastpath;
510 SEXP_init(&gr_lastpath);
511 struct ID_cache *cache = ID_cache_init(10000);
512 struct gr_sexps *grs = gr_sexps_init();
513
514 if ((ofts = oval_fts_open_prefixed(prefix, path, filename, filepath, behaviors, probe_ctx_getresult(ctx))) != NULL) {
515 while ((ofts_ent = oval_fts_read(ofts)) != NULL) {
516 if (file_cb(prefix, ofts_ent->path, ofts_ent->file, &cbargs, over, cache, grs, &gr_lastpath, ctx->blocked_paths) != 0) {
517 oval_ftsent_free(ofts_ent);
518 break;
519 }
520 oval_ftsent_free(ofts_ent);
521 }
522 oval_fts_close(ofts);
523 }
524 ID_cache_free(cache);
525 gr_sexps_free(grs);
526
527 if (!SEXP_emptyp(&gr_lastpath))
528 SEXP_free_r(&gr_lastpath);
529
530 err = 0;
531
532 SEXP_free(path);
533 SEXP_free(filename);
534 SEXP_free(filepath);
535 SEXP_free(behaviors);
536
537 switch (pthread_mutex_unlock (mutex)) {
538 case 0:
539 break;
540 default:
541 dD("Can't unlock mutex(%p): %u, %s.", mutex, errno, strerror (errno));
542
543 return PROBE_EFATAL;
544 }
545
546 return err;
547 }