| 1 | /** |
| 2 | * @file fileextendedattribute_probe.c |
| 3 | * @brief fileextendedattribute probe |
| 4 | * @author "Daniel Kopecek" <[email protected]> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2011 Red Hat Inc., Durham, North Carolina. |
| 10 | * All Rights Reserved. |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | * |
| 26 | * Authors: |
| 27 | * "Daniel Kopecek" <[email protected]> |
| 28 | */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include "_seap.h" |
| 35 | #include <probe-api.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <pthread.h> |
| 40 | #include <errno.h> |
| 41 | #include <limits.h> |
| 42 | |
| 43 | #include <sys/types.h> |
| 44 | #if defined(HAVE_SYS_XATTR_H) |
| 45 | # include <sys/xattr.h> |
| 46 | #elif defined(HAVE_ATTR_XATTR_H) |
| 47 | # include <attr/xattr.h> |
| 48 | #elif defined(HAVE_SYS_EXTATTR_H) |
| 49 | #include <sys/extattr.h> |
| 50 | #else |
| 51 | # error "This probe requires sys/xattr.h, attr/xattr.h, or sys/extattr.h, and none were found!" |
| 52 | #endif |
| 53 | |
| 54 | #include <probe/probe.h> |
| 55 | #include <probe/option.h> |
| 56 | #include "probe/entcmp.h" |
| 57 | #include "oval_fts.h" |
| 58 | #include "common/debug_priv.h" |
| 59 | #include "fileextendedattribute_probe.h" |
| 60 | |
| 61 | #ifndef PATH_MAX |
| 62 | #define PATH_MAX 4096 |
| 63 | #endif |
| 64 | |
| 65 | #ifdef OS_APPLE |
| 66 | #define llistxattr(path, buf, size) listxattr((path), (buf), (size), XATTR_NOFOLLOW) |
| 67 | #define lgetxattr(path, name, value, size) getxattr((path), (name), (value), (size), 0, XATTR_NOFOLLOW) |
| 68 | #define lsetxattr(path, name, value, size, flags) setxattr((path), (name), (value), (size), 0, (flags) | XATTR_NOFOLLOW) |
| 69 | #endif |
| 70 | |
| 71 | #define FILE_SEPARATOR '/' |
| 72 | |
| 73 | struct cbargs { |
| 74 | probe_ctx *ctx; |
| 75 | int error; |
| 76 | SEXP_t *attr_ent; |
| 77 | }; |
| 78 | |
| 79 | #if defined(OS_FREEBSD) |
| 80 | static int file_cb(const char *prefix, const char *p, const char *f, void *ptr, SEXP_t *gr_lastpath, struct oscap_list *blocked_paths) |
| 81 | { |
| 82 | char path_buffer[PATH_MAX]; |
| 83 | SEXP_t *item; |
| 84 | struct cbargs *args = (struct cbargs *) ptr; |
| 85 | const char *st_path; |
| 86 | |
| 87 | ssize_t byte_count = 0; |
| 88 | ssize_t index; |
| 89 | ssize_t value_len; |
| 90 | ssize_t name_len; |
| 91 | |
| 92 | char *xattr_list = malloc(LINE_MAX); |
| 93 | |
| 94 | if (!xattr_list) |
| 95 | return PROBE_ENOMEM; |
| 96 | |
| 97 | char *name_buf = NULL; |
| 98 | char *value_buf = NULL; |
| 99 | |
| 100 | if (f == NULL) { |
| 101 | st_path = p; |
| 102 | } else { |
| 103 | const size_t p_len = strlen(p); |
| 104 | /* Avoid 2 slashes */ |
| 105 | if (p_len >= 1 && p[p_len - 1] == FILE_SEPARATOR) { |
| 106 | snprintf(path_buffer, sizeof path_buffer, "%s%s", p, f); |
| 107 | } else { |
| 108 | snprintf(path_buffer, sizeof path_buffer, "%s%c%s", p, FILE_SEPARATOR, f); |
| 109 | } |
| 110 | st_path = path_buffer; |
| 111 | } |
| 112 | |
| 113 | if (probe_path_is_blocked(st_path, blocked_paths)) { |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | char *st_path_with_prefix = oscap_path_join(prefix, st_path); |
| 118 | |
| 119 | /* update lastpath if needed */ |
| 120 | if (!SEXP_emptyp(gr_lastpath)) { |
| 121 | if (SEXP_strcmp(gr_lastpath, p) != 0) { |
| 122 | SEXP_free_r(gr_lastpath); |
| 123 | SEXP_string_new_r(gr_lastpath, p, strlen(p)); |
| 124 | } |
| 125 | } else { |
| 126 | SEXP_string_new_r(gr_lastpath, p, strlen(p)); |
| 127 | } |
| 128 | |
| 129 | byte_count = extattr_list_file(st_path_with_prefix, EXTATTR_NAMESPACE_USER, xattr_list, LINE_MAX); |
| 130 | |
| 131 | if (byte_count < 0) { |
| 132 | dD("FAIL: extattr_list_file(%s, EXTATTR_NAMESPACE_USER, %p, %d), errno=%u, %s", st_path_with_prefix, xattr_list, LINE_MAX, errno, strerror(errno)); |
| 133 | free(st_path_with_prefix); |
| 134 | free(xattr_list); |
| 135 | return PROBE_EFATAL; |
| 136 | } |
| 137 | |
| 138 | index = 0; |
| 139 | |
| 140 | while (index < byte_count) { |
| 141 | name_len = xattr_list[index] + 1; |
| 142 | |
| 143 | /* We should be able to trust the output of extattr_list_file(), however |
| 144 | * let's be safe and ensure it doesn't try to read past the end of the array. |
| 145 | */ |
| 146 | if ((name_len < 1) || (name_len > byte_count - index - 1)) { |
| 147 | dD("FAIL: out of bounds name_len value: %d, at index: %d, in array of size: %d", name_len, index, byte_count); |
| 148 | free(st_path_with_prefix); |
| 149 | free(xattr_list); |
| 150 | return PROBE_EFAULT; |
| 151 | } |
| 152 | |
| 153 | name_buf = malloc(name_len); |
| 154 | value_buf = malloc(LINE_MAX); |
| 155 | |
| 156 | if ((name_buf == NULL) || (value_buf == NULL)) { |
| 157 | dD("FAIL: Could not allocate memory for xattr name and value buffers.\n"); |
| 158 | free(name_buf); |
| 159 | free(value_buf); |
| 160 | free(st_path_with_prefix); |
| 161 | free(xattr_list); |
| 162 | return PROBE_ENOMEM; |
| 163 | } |
| 164 | |
| 165 | snprintf(name_buf, name_len, "%s", xattr_list + index + 1); |
| 166 | |
| 167 | value_len = extattr_get_file(st_path_with_prefix, EXTATTR_NAMESPACE_USER, name_buf, value_buf, LINE_MAX); |
| 168 | |
| 169 | if (value_len < 0) { |
| 170 | free(name_buf); |
| 171 | free(value_buf); |
| 172 | free(st_path_with_prefix); |
| 173 | free(xattr_list); |
| 174 | return PROBE_EFATAL; |
| 175 | } |
| 176 | |
| 177 | value_buf[value_len] = '\0'; |
| 178 | |
| 179 | item = probe_item_create(OVAL_UNIX_FILEEXTENDEDATTRIBUTE, NULL, |
| 180 | "filepath", OVAL_DATATYPE_STRING, f == NULL ? NULL : st_path, |
| 181 | "path", OVAL_DATATYPE_SEXP, gr_lastpath, |
| 182 | "filename", OVAL_DATATYPE_STRING, f == NULL ? "" : f, |
| 183 | "attribute_name", OVAL_DATATYPE_STRING, name_buf, |
| 184 | "value", OVAL_DATATYPE_STRING, value_buf, |
| 185 | NULL); |
| 186 | |
| 187 | if (!item) { |
| 188 | dD("FAIL: Could not create new probe item.\n"); |
| 189 | free(name_buf); |
| 190 | free(value_buf); |
| 191 | free(st_path_with_prefix); |
| 192 | free(xattr_list); |
| 193 | return PROBE_EFATAL; |
| 194 | } |
| 195 | |
| 196 | probe_item_collect(args->ctx, item); |
| 197 | |
| 198 | free(name_buf); |
| 199 | free(value_buf); |
| 200 | name_buf = NULL; |
| 201 | value_buf = NULL; |
| 202 | |
| 203 | index += name_len; |
| 204 | } |
| 205 | |
| 206 | free(xattr_list); |
| 207 | free(st_path_with_prefix); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | #else |
| 212 | static int file_cb(const char *prefix, const char *p, const char *f, void *ptr, SEXP_t *gr_lastpath, struct oscap_list *blocked_paths) |
| 213 | { |
| 214 | char path_buffer[PATH_MAX]; |
| 215 | SEXP_t *item, xattr_name; |
| 216 | struct cbargs *args = (struct cbargs *) ptr; |
| 217 | const char *st_path; |
| 218 | |
| 219 | ssize_t xattr_count = -1; |
| 220 | char *xattr_buf = NULL; |
| 221 | size_t xattr_buflen = 0, i; |
| 222 | |
| 223 | if (f == NULL) { |
| 224 | st_path = p; |
| 225 | } else { |
| 226 | const size_t p_len = strlen(p); |
| 227 | /* Avoid 2 slashes */ |
| 228 | if (p_len >= 1 && p[p_len - 1] == FILE_SEPARATOR) { |
| 229 | snprintf(path_buffer, sizeof path_buffer, "%s%s", p, f); |
| 230 | } else { |
| 231 | snprintf(path_buffer, sizeof path_buffer, "%s%c%s", p, FILE_SEPARATOR, f); |
| 232 | } |
| 233 | st_path = path_buffer; |
| 234 | } |
| 235 | |
| 236 | SEXP_init(&xattr_name); |
| 237 | |
| 238 | if (probe_path_is_blocked(st_path, blocked_paths)) { |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | char *st_path_with_prefix = oscap_path_join(prefix, st_path); |
| 243 | do { |
| 244 | /* estimate the size of the buffer */ |
| 245 | xattr_count = llistxattr(st_path_with_prefix, NULL, 0); |
| 246 | |
| 247 | if (xattr_count == 0) { |
| 248 | goto exit; |
| 249 | } |
| 250 | |
| 251 | if (xattr_count < 0) { |
| 252 | dD("FAIL: llistxattr(%s, %p, %zu): errno=%u, %s", st_path_with_prefix, NULL, (size_t)0, errno, strerror(errno)); |
| 253 | goto exit; |
| 254 | } |
| 255 | |
| 256 | /* allocate space for xattr names */ |
| 257 | xattr_buflen = xattr_count; |
| 258 | void *new_xattr_buf = realloc(xattr_buf, sizeof(char) * xattr_buflen); |
| 259 | if (new_xattr_buf == NULL) { |
| 260 | dE("Failed to re-allocate memory for xattr_buf"); |
| 261 | goto exit; |
| 262 | } |
| 263 | xattr_buf = new_xattr_buf; |
| 264 | |
| 265 | /* fill the buffer */ |
| 266 | xattr_count = llistxattr(st_path_with_prefix, xattr_buf, xattr_buflen); |
| 267 | |
| 268 | /* check & retry if needed */ |
| 269 | } while (errno == ERANGE); |
| 270 | |
| 271 | if (xattr_count < 0) { |
| 272 | dD("FAIL: llistxattr(%s, %p, %zu): errno=%u, %s", st_path_with_prefix, xattr_buf, xattr_buflen, errno, strerror(errno)); |
| 273 | free(st_path_with_prefix); |
| 274 | free(xattr_buf); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | /* update lastpath if needed */ |
| 279 | if (!SEXP_emptyp(gr_lastpath)) { |
| 280 | if (SEXP_strcmp(gr_lastpath, p) != 0) { |
| 281 | SEXP_free_r(gr_lastpath); |
| 282 | SEXP_string_new_r(gr_lastpath, p, strlen(p)); |
| 283 | } |
| 284 | } else { |
| 285 | SEXP_string_new_r(gr_lastpath, p, strlen(p)); |
| 286 | } |
| 287 | |
| 288 | i = 0; |
| 289 | /* collect */ |
| 290 | do { |
| 291 | SEXP_string_new_r(&xattr_name, xattr_buf + i, strlen(xattr_buf +i)); |
| 292 | |
| 293 | if (probe_entobj_cmp(args->attr_ent, &xattr_name) == OVAL_RESULT_TRUE) { |
| 294 | ssize_t xattr_vallen = -1; |
| 295 | char *xattr_val = NULL; |
| 296 | |
| 297 | xattr_vallen = lgetxattr(st_path_with_prefix, xattr_buf + i, NULL, 0); |
| 298 | retry_value: |
| 299 | if (xattr_vallen >= 0) { |
| 300 | // Check possible buffer overflow |
| 301 | if (sizeof(char) * (xattr_vallen + 1) <= sizeof(char) * xattr_vallen) { |
| 302 | dE("Attribute is too long."); |
| 303 | free(xattr_val); |
| 304 | goto exit; |
| 305 | } |
| 306 | |
| 307 | // Allocate buffer, '+1' is for trailing '\0' |
| 308 | void *new_xattr_val = realloc(xattr_val, sizeof(char) * (xattr_vallen + 1)); |
| 309 | if (new_xattr_val == NULL) { |
| 310 | dE("Failed to allocate memory for xattr_val"); |
| 311 | free(xattr_val); |
| 312 | goto exit; |
| 313 | } |
| 314 | xattr_val = new_xattr_val; |
| 315 | |
| 316 | // we don't want to override space for '\0' by call of 'lgetxattr' |
| 317 | // we pass only 'xattr_vallen' instead of 'xattr_vallen + 1' |
| 318 | xattr_vallen = lgetxattr(st_path_with_prefix, xattr_buf + i, xattr_val, xattr_vallen); |
| 319 | |
| 320 | if (xattr_vallen < 0 || errno == ERANGE) |
| 321 | goto retry_value; |
| 322 | |
| 323 | xattr_val[xattr_vallen] = '\0'; |
| 324 | |
| 325 | item = probe_item_create(OVAL_UNIX_FILEEXTENDEDATTRIBUTE, NULL, |
| 326 | "filepath", OVAL_DATATYPE_STRING, f == NULL ? NULL : st_path, |
| 327 | "path", OVAL_DATATYPE_SEXP, gr_lastpath, |
| 328 | "filename", OVAL_DATATYPE_STRING, f == NULL ? "" : f, |
| 329 | "attribute_name", OVAL_DATATYPE_SEXP, &xattr_name, |
| 330 | "value", OVAL_DATATYPE_STRING, xattr_val, |
| 331 | NULL); |
| 332 | |
| 333 | free(xattr_val); |
| 334 | } else { |
| 335 | dD("FAIL: lgetxattr(%s, %s, NULL, 0): errno=%u, %s", st_path_with_prefix, xattr_buf + i, errno, strerror(errno)); |
| 336 | |
| 337 | item = probe_item_create(OVAL_UNIX_FILEEXTENDEDATTRIBUTE, NULL, NULL); |
| 338 | probe_item_setstatus(item, SYSCHAR_STATUS_ERROR); |
| 339 | |
| 340 | if (xattr_val != NULL) |
| 341 | free(xattr_val); |
| 342 | } |
| 343 | |
| 344 | probe_item_collect(args->ctx, item); /* XXX: handle ENOMEM */ |
| 345 | } |
| 346 | |
| 347 | SEXP_free_r(&xattr_name); |
| 348 | |
| 349 | /* skip to next name */ |
| 350 | while (i < xattr_buflen && xattr_buf[i] != '\0') |
| 351 | ++i; |
| 352 | ++i; |
| 353 | } while (xattr_buf + i < xattr_buf + xattr_buflen - 1); |
| 354 | |
| 355 | exit: |
| 356 | free(xattr_buf); |
| 357 | free(st_path_with_prefix); |
| 358 | return 0; |
| 359 | } |
| 360 | #endif |
| 361 | |
| 362 | int fileextendedattribute_probe_offline_mode_supported() |
| 363 | { |
| 364 | return PROBE_OFFLINE_OWN; |
| 365 | } |
| 366 | |
| 367 | void *fileextendedattribute_probe_init(void) |
| 368 | { |
| 369 | /* |
| 370 | * Initialize mutex. |
| 371 | */ |
| 372 | pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t)); |
| 373 | switch (pthread_mutex_init(mutex, NULL)) { |
| 374 | case 0: |
| 375 | return (void *)mutex; |
| 376 | default: |
| 377 | dD("Can't initialize mutex: errno=%u, %s.", errno, strerror(errno)); |
| 378 | free(mutex); |
| 379 | } |
| 380 | #if 0 |
| 381 | probe_setoption(PROBEOPT_VARREF_HANDLING, false, "path"); |
| 382 | probe_setoption(PROBEOPT_VARREF_HANDLING, false, "filename"); |
| 383 | #endif |
| 384 | return NULL; |
| 385 | } |
| 386 | |
| 387 | void fileextendedattribute_probe_fini(void *arg) |
| 388 | { |
| 389 | /* |
| 390 | * Destroy mutex. |
| 391 | */ |
| 392 | (void) pthread_mutex_destroy((pthread_mutex_t *)arg); |
| 393 | free(arg); |
| 394 | } |
| 395 | |
| 396 | int fileextendedattribute_probe_main(probe_ctx *ctx, void *mutex) |
| 397 | { |
| 398 | SEXP_t *path, *filename, *behaviors; |
| 399 | SEXP_t *filepath, *attribute_, *probe_in; |
| 400 | int err; |
| 401 | struct cbargs cbargs; |
| 402 | OVAL_FTS *ofts; |
| 403 | OVAL_FTSENT *ofts_ent; |
| 404 | SEXP_t gr_lastpath; |
| 405 | |
| 406 | if (mutex == NULL) |
| 407 | return PROBE_EINIT; |
| 408 | |
| 409 | probe_in = probe_ctx_getobject(ctx); |
| 410 | |
| 411 | path = probe_obj_getent(probe_in, "path", 1); |
| 412 | filename = probe_obj_getent(probe_in, "filename", 1); |
| 413 | behaviors = probe_obj_getent(probe_in, "behaviors", 1); |
| 414 | filepath = probe_obj_getent(probe_in, "filepath", 1); |
| 415 | attribute_ = probe_obj_getent (probe_in, "attribute_name", 1); |
| 416 | |
| 417 | /* we want either path+filename or filepath */ |
| 418 | if (((path == NULL || filename == NULL) && filepath == NULL) || attribute_ == NULL) |
| 419 | { |
| 420 | SEXP_free(behaviors); |
| 421 | SEXP_free(path); |
| 422 | SEXP_free(filename); |
| 423 | SEXP_free(filepath); |
| 424 | SEXP_free(attribute_); |
| 425 | return PROBE_ENOELM; |
| 426 | } |
| 427 | |
| 428 | probe_filebehaviors_canonicalize(&behaviors); |
| 429 | |
| 430 | switch (pthread_mutex_lock(mutex)) { |
| 431 | case 0: |
| 432 | break; |
| 433 | default: |
| 434 | dD("Can't lock mutex(%p): %u, %s.", mutex, errno, strerror(errno)); |
| 435 | SEXP_free(path); |
| 436 | SEXP_free(filename); |
| 437 | SEXP_free(filepath); |
| 438 | SEXP_free(behaviors); |
| 439 | SEXP_free(attribute_); |
| 440 | return PROBE_EFATAL; |
| 441 | } |
| 442 | |
| 443 | cbargs.ctx = ctx; |
| 444 | cbargs.error = 0; |
| 445 | cbargs.attr_ent = attribute_; |
| 446 | |
| 447 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 448 | SEXP_init(&gr_lastpath); |
| 449 | |
| 450 | if ((ofts = oval_fts_open_prefixed(prefix, path, filename, filepath, behaviors, probe_ctx_getresult(ctx))) != NULL) { |
| 451 | while ((ofts_ent = oval_fts_read(ofts)) != NULL) { |
| 452 | file_cb(prefix, ofts_ent->path, ofts_ent->file, &cbargs, &gr_lastpath, ctx->blocked_paths); |
| 453 | oval_ftsent_free(ofts_ent); |
| 454 | } |
| 455 | oval_fts_close(ofts); |
| 456 | } |
| 457 | |
| 458 | if (!SEXP_emptyp(&gr_lastpath)) |
| 459 | SEXP_free_r(&gr_lastpath); |
| 460 | |
| 461 | err = 0; |
| 462 | |
| 463 | SEXP_free(path); |
| 464 | SEXP_free(filename); |
| 465 | SEXP_free(filepath); |
| 466 | SEXP_free(behaviors); |
| 467 | SEXP_free(attribute_); |
| 468 | |
| 469 | switch (pthread_mutex_unlock(mutex)) { |
| 470 | case 0: |
| 471 | break; |
| 472 | default: |
| 473 | dD("Can't unlock mutex(%p): %u, %s.", mutex, errno, strerror(errno)); |
| 474 | return PROBE_EFATAL; |
| 475 | } |
| 476 | |
| 477 | return err; |
| 478 | } |