static int eval_data(const char *type, const char *local_address, unsigned int local_port) { SEXP_t *r0; r0 = SEXP_string_newf("%s", type); if (probe_entobj_cmp(req.protocol_ent, r0) != OVAL_RESULT_TRUE) { SEXP_free(r0); return 0; } SEXP_free(r0); r0 = SEXP_string_newf("%s", local_address); if (probe_entobj_cmp(req.local_address_ent, r0) != OVAL_RESULT_TRUE) { SEXP_free(r0); return 0; } SEXP_free(r0); r0 = SEXP_number_newu_32(local_port); if (probe_entobj_cmp(req.local_port_ent, r0) != OVAL_RESULT_TRUE) { SEXP_free(r0); return 0; } SEXP_free(r0); return 1; }
static int collect_item(struct route_info *rt, probe_ctx *ctx) { SEXP_t *item, *rt_dst; oval_datatype_t addr_type; rt_dst = SEXP_string_new(rt->ip_dst, strlen(rt->ip_dst)); if (probe_entobj_cmp(rt->ip_dst_ent, rt_dst) != OVAL_RESULT_TRUE) { SEXP_free(rt_dst); return 0; } addr_type = rt->ip_version == 4 ? OVAL_DATATYPE_IPV4ADDR : OVAL_DATATYPE_IPV6ADDR; /* create the item */ item = probe_item_create(OVAL_UNIX_ROUTINGTABLE, NULL, "destination", addr_type, rt->ip_dst, "gateway", addr_type, rt->ip_gw, "flags", OVAL_DATATYPE_STRING_M, rt->rt_flags, "interface_name", OVAL_DATATYPE_STRING, rt->if_name, NULL); SEXP_free(rt_dst); return probe_item_collect(ctx, item) == 2 ? 1 : 0; }
static int unit_callback(const char *unit, void *cbarg) { struct unit_callback_vars *vars = (struct unit_callback_vars *)cbarg; SEXP_t *se_unit = SEXP_string_new(unit, strlen(unit)); if (probe_entobj_cmp(vars->unit_entity, se_unit) != OVAL_RESULT_TRUE) { /* Do nothing, continue with the next unit */ SEXP_free(se_unit); return 0; } vars->se_unit = se_unit; vars->se_property = NULL; vars->item = NULL; char *unit_path = get_path_by_unit(vars->dbus_conn, unit); if (unit_path == NULL) { return 1; } get_all_properties_by_unit_path(vars->dbus_conn, unit_path, property_callback, vars); if (vars->item != NULL) { probe_item_collect(vars->ctx, vars->item); vars->item = NULL; SEXP_free(vars->se_property); vars->se_property = NULL; } SEXP_free(se_unit); return 0; }
static int read_packet(llist *l, probe_ctx *ctx, oval_schema_version_t over) { int line = 0; FILE *f; char buf[256]; void *s; int refcnt, sk_type, ifindex, running; unsigned long inode; unsigned rmem, uid, proto_num; struct interface_t interface; f = fopen("/proc/net/packet", "rt"); if (f == NULL) { if (errno != ENOENT) return 1; else return 0; } __fsetlocking(f, FSETLOCKING_BYCALLER); while (fgets(buf, sizeof(buf), f)) { if (line == 0) { line++; continue; } /* follow structure from net/packet/af_packet.c */ sscanf(buf, "%p %d %d %04x %d %d %u %u %lu\n", &s, &refcnt, &sk_type, &proto_num, &ifindex, &running, &rmem, &uid, &inode ); if (list_find_inode(l, inode) && get_interface(ifindex, &interface)) { struct result_info r; SEXP_t *r0; dI("Have interface_name: %s, hw_address: %s\n", interface.interface_name, interface.hw_address); r0 = SEXP_string_newf("%s", interface.interface_name); if (probe_entobj_cmp(interface_name_ent, r0) != OVAL_RESULT_TRUE) { SEXP_free(r0); continue; } SEXP_free(r0); r.interface_name = interface.interface_name; r.protocol = oscap_enum_to_string(ProtocolType, proto_num); r.hw_address = interface.hw_address; report_finding(&r, l, ctx, over); } } fclose(f); return 0; }
static int read_password(SEXP_t *un_ent, probe_ctx *ctx, oval_schema_version_t over) { struct passwd *pw; while ((pw = getpwent())) { SEXP_t *un; dI("Have user: %s", pw->pw_name); un = SEXP_string_newf("%s", pw->pw_name); if (probe_entobj_cmp(un_ent, un) == OVAL_RESULT_TRUE) { struct result_info r; r.username = pw->pw_name; r.password = pw->pw_passwd; r.user_id = pw->pw_uid; r.group_id = pw->pw_gid; r.gcos = pw->pw_gecos; r.home_dir = pw->pw_dir; r.login_shell = pw->pw_shell; r.last_login = -1; if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.10)) >= 0) { FILE *ll_fp = fopen(_PATH_LASTLOG, "r"); if (ll_fp != NULL) { struct lastlog ll; if (fseeko(ll_fp, (off_t)pw->pw_uid * sizeof(ll), SEEK_SET) == 0) if (fread((char *)&ll, sizeof(ll), 1, ll_fp) == 1) r.last_login = (int64_t)ll.ll_time; fclose(ll_fp); } } report_finding(&r, ctx, over); } SEXP_free(un); } endpwent(); return 0; }
int accesstoken_probe_main(probe_ctx *ctx, void *arg) { SEXP_t *probe_in = probe_ctx_getobject(ctx); SEXP_t *behaviors_ent = probe_obj_getent(probe_in, "behaviors", 1); SEXP_t *security_principle_ent = probe_obj_getent(probe_in, "security_principle", 1); SEXP_t *security_principle_val = probe_ent_getval(security_principle_ent); bool include_group = accesstoken_behaviors_get_include_group(behaviors_ent); bool resolve_group = accesstoken_behaviors_get_resolve_group(behaviors_ent); oval_operation_t operation = probe_ent_getoperation(security_principle_ent, OVAL_OPERATION_EQUALS); if (operation == OVAL_OPERATION_EQUALS) { char *security_principle_str = SEXP_string_cstr(security_principle_val); WCHAR *security_principle_wstr = oscap_windows_str_to_wstr(security_principle_str); collect_access_rights(ctx, security_principle_wstr, include_group, resolve_group); free(security_principle_str); free(security_principle_wstr); } else { struct oscap_list *trustees_list = oscap_list_new(); get_all_trustee_names(trustees_list); struct oscap_iterator *it = oscap_iterator_new(trustees_list); while (oscap_iterator_has_more(it)) { WCHAR *trustee_wstr = oscap_iterator_next(it); char *trustee_str = oscap_windows_wstr_to_str(trustee_wstr); SEXP_t *tmp = SEXP_string_new(trustee_str, strlen(trustee_str)); if (probe_entobj_cmp(security_principle_ent, tmp) == OVAL_RESULT_TRUE) { collect_access_rights(ctx, trustee_wstr, include_group, resolve_group); } free(trustee_str); SEXP_free(tmp); } oscap_iterator_free(it); oscap_list_free(trustees_list, free); } SEXP_free(behaviors_ent); SEXP_free(security_principle_ent); SEXP_free(security_principle_val); return 0; }
static int property_callback(const char *property, const char *value, void *cbarg) { struct unit_callback_vars *vars = (struct unit_callback_vars *)cbarg; if (vars->se_property != NULL) { // // Compare the previously matched entity to the current one // If they are the same, continue to fill the current item // with property values. If not, collect the item and create // a new one for the current property. // if (SEXP_strcmp(vars->se_property, property) == 0) { SEXP_t *se_value = SEXP_string_new(value, strlen(value)); probe_item_ent_add(vars->item, "value", NULL, se_value); SEXP_free(se_value); return 0; } else { probe_item_collect(vars->ctx, vars->item); vars->item = NULL; SEXP_free(vars->se_property); vars->se_property = NULL; } } SEXP_t *se_property = SEXP_string_new(property, strlen(property)); if (probe_entobj_cmp(vars->property_entity, se_property) != OVAL_RESULT_TRUE) { SEXP_free(se_property); return 0; } vars->se_property = se_property; vars->item = probe_item_create(OVAL_LINUX_SYSTEMDUNITPROPERTY, NULL, "unit", OVAL_DATATYPE_SEXP, vars->se_unit, "property", OVAL_DATATYPE_SEXP, vars->se_property, "value", OVAL_DATATYPE_STRING, value, NULL); return 0; }
static int get_selinuxboolean(SEXP_t *ut_ent, probe_ctx *ctx) { int err = 1, active, pending, len, i; SEXP_t *boolean, *item; char **booleans; if ( ! is_selinux_enabled()) { probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_NOT_APPLICABLE); return 0; } if (security_get_boolean_names(&booleans, &len) == -1) { probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR); return err; } for (i = 0; i < len; i++) { boolean = SEXP_string_new(booleans[i], strlen(booleans[i])); if (probe_entobj_cmp(ut_ent, boolean) == OVAL_RESULT_TRUE) { active = security_get_boolean_active(booleans[i]); pending = security_get_boolean_pending(booleans[i]); item = probe_item_create( OVAL_LINUX_SELINUXBOOLEAN, NULL, "name", OVAL_DATATYPE_SEXP, boolean, "current_status", OVAL_DATATYPE_BOOLEAN, active, "pending_status", OVAL_DATATYPE_BOOLEAN, pending, NULL); probe_item_collect(ctx, item); } SEXP_free(boolean); } for (i = 0; i < len; i++) free(booleans[i]); free(booleans); return 0; }
static int process_file(const char *path, const char *file, void *arg) { struct pfdata *pfd = (struct pfdata *) arg; int ret = 0, path_len, file_len, cur_inst = 0, fd = -1, substr_cnt, buf_size = 0, buf_used = 0, ofs = 0, buf_inc = 4096; char *whole_path = NULL, *buf = NULL; SEXP_t *next_inst = NULL; struct stat st; if (file == NULL) goto cleanup; path_len = strlen(path); file_len = strlen(file); whole_path = oscap_alloc(path_len + file_len + 2); memcpy(whole_path, path, path_len); if (whole_path[path_len - 1] != FILE_SEPARATOR) { whole_path[path_len] = FILE_SEPARATOR; ++path_len; } memcpy(whole_path + path_len, file, file_len + 1); /* * If stat() fails, don't report an error and just skip the file. * This is an expected situation, because the fts_*() functions * are called with the 'FTS_PHYSICAL' option. Normally, stumbling * upon a symlink without a target would cause fts_read() to return * the 'FTS_SLNONE' flag, but the 'FTS_PHYSICAL' option causes it * to return 'FTS_SL' and the presence of a valid target has to * be determined with stat(). */ if (stat(whole_path, &st) == -1) goto cleanup; if (!S_ISREG(st.st_mode)) goto cleanup; fd = open(whole_path, O_RDONLY); if (fd == -1) { SEXP_t *msg; msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "open(): '%s' %s.", whole_path, strerror(errno)); probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg); SEXP_free(msg); probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR); ret = -1; goto cleanup; } do { buf_size += buf_inc; buf = oscap_realloc(buf, buf_size); ret = read(fd, buf + buf_used, buf_inc); if (ret == -1) { SEXP_t *msg; msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "read(): '%s' %s.", whole_path, strerror(errno)); probe_cobj_add_msg(probe_ctx_getresult(pfd->ctx), msg); SEXP_free(msg); probe_cobj_set_flag(probe_ctx_getresult(pfd->ctx), SYSCHAR_FLAG_ERROR); ret = -2; goto cleanup; } buf_used += ret; } while (ret == buf_inc); if (buf_used == buf_size) buf = realloc(buf, ++buf_size); buf[buf_used++] = '\0'; do { char **substrs; int want_instance; next_inst = SEXP_number_newi_32(cur_inst + 1); if (probe_entobj_cmp(pfd->instance_ent, next_inst) == OVAL_RESULT_TRUE) want_instance = 1; else want_instance = 0; SEXP_free(next_inst); substr_cnt = get_substrings(buf, &ofs, pfd->compiled_regex, want_instance, &substrs); if (substr_cnt > 0) { ++cur_inst; if (want_instance) { int k; SEXP_t *item; item = create_item(path, file, pfd->pattern, cur_inst, substrs, substr_cnt); probe_item_collect(pfd->ctx, item); for (k = 0; k < substr_cnt; ++k) oscap_free(substrs[k]); oscap_free(substrs); } } } while (substr_cnt > 0 && ofs < buf_used); cleanup: if (fd != -1) close(fd); oscap_free(buf); if (whole_path != NULL) oscap_free(whole_path); return ret; }
static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx) { int err = 1; DIR *d; struct dirent *ent; d = opendir("/proc"); if (d == NULL) return err; psinfo_t *psinfo; // Scan the directories while (( ent = readdir(d) )) { int fd, len; char buf[336]; int pid; unsigned sched_policy; SEXP_t *cmd_sexp; // Skip non-process dir entries if(*ent->d_name<'0' || *ent->d_name>'9') continue; errno = 0; pid = strtol(ent->d_name, NULL, 10); if (errno || pid == 2) // skip err & kthreads continue; // Parse up the stat file for the proc snprintf(buf, 32, "/proc/%d/psinfo", pid); fd = open(buf, O_RDONLY, 0); if (fd < 0) continue; len = read(fd, buf, sizeof buf); close(fd); if (len < 336) continue; // The psinfo file contains a psinfo struct; this typecast gets us the struct directly psinfo = (psinfo_t *) buf; err = 0; // If we get this far, no permission problems dI("Have command: %s\n", psinfo->pr_fname); cmd_sexp = SEXP_string_newf("%s", psinfo->pr_fname); if (probe_entobj_cmp(cmd_ent, cmd_sexp) == OVAL_RESULT_TRUE) { struct result_info r; char tbuf[32], sbuf[32]; int tday,tyear; time_t s_time; struct tm *proc, *now; const char *fmt; int fixfmt_year; r.scheduling_class = malloc(PRCLSZ); strncpy(r.scheduling_class, (psinfo->pr_lwp).pr_clname, sizeof(r.scheduling_class)); // Get the start time s_time = time(NULL); now = localtime(&s_time); tyear = now->tm_year; tday = now->tm_yday; // Get current time s_time = psinfo->pr_start.tv_sec; proc = localtime(&s_time); // Select format based on how long we've been running // // FROM THE SPEC: // "This is the time of day the process started formatted in HH:MM:SS if // the same day the process started or formatted as MMM_DD (Ex.: Feb_5) // if process started the previous day or further in the past." // if (tday != proc->tm_yday || tyear != proc->tm_year) fmt = "%b_%d"; else fmt = "%H:%M:%S"; strftime(sbuf, sizeof(sbuf), fmt, proc); r.command = psinfo->pr_fname; r.exec_time = convert_time(psinfo->pr_time.tv_sec, tbuf, sizeof(tbuf)); r.pid = psinfo->pr_pid; r.ppid = psinfo->pr_ppid; r.priority = (psinfo->pr_lwp).pr_pri; r.ruid = psinfo->pr_uid; r.start_time = sbuf; r.tty = oscap_sprintf("%s", psinfo->pr_ttydev); r.user_id = psinfo->pr_euid; report_finding(&r, ctx); } SEXP_free(cmd_sexp); } closedir(d); return err; }
static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx) { int err = 1; DIR *d; struct dirent *ent; d = opendir("/proc"); if (d == NULL) return err; // Get the time tick hertz ticks = (unsigned long)sysconf(_SC_CLK_TCK); get_boot_time(); // Scan the directories while (( ent = readdir(d) )) { int fd, len; char buf[256]; char *tmp, cmd[16], state, tty_dev[128]; int pid, ppid, pgrp, session, tty_nr, tpgid; unsigned flags, sched_policy; unsigned long minflt, cminflt, majflt, cmajflt, uutime, ustime; long cutime, cstime, priority, cnice, nthreads, itrealvalue; unsigned long long start; SEXP_t *cmd_sexp; // Skip non-process dir entries if(*ent->d_name<'0' || *ent->d_name>'9') continue; errno = 0; pid = strtol(ent->d_name, NULL, 10); if (errno || pid == 2) // skip err & kthreads continue; // Parse up the stat file for the proc snprintf(buf, 32, "/proc/%d/stat", pid); fd = open(buf, O_RDONLY, 0); if (fd < 0) continue; len = read(fd, buf, sizeof buf - 1); close(fd); if (len < 40) continue; buf[len] = 0; tmp = strrchr(buf, ')'); if (tmp) *tmp = 0; else continue; memset(cmd, 0, sizeof(cmd)); sscanf(buf, "%d (%15c", &ppid, cmd); sscanf(tmp+2, "%c %d %d %d %d %d " "%u %lu %lu %lu %lu " "%lu %lu %lu %ld %ld " "%ld %ld %ld %llu", &state, &ppid, &pgrp, &session, &tty_nr, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &uutime, &ustime, &cutime, &cstime, &priority, &cnice, &nthreads, &itrealvalue, &start ); // Skip kthreads if (ppid == 2) continue; err = 0; // If we get this far, no permission problems dI("Have command: %s\n", cmd); cmd_sexp = SEXP_string_newf("%s", cmd); if (probe_entobj_cmp(cmd_ent, cmd_sexp) == OVAL_RESULT_TRUE) { struct result_info r; unsigned long t = uutime/ticks + ustime/ticks; char tbuf[32], sbuf[32]; int tday,tyear; time_t s_time; struct tm *proc, *now; const char *fmt; // Now get scheduler policy sched_policy = sched_getscheduler(pid); switch (sched_policy) { case SCHED_OTHER: r.scheduling_class = "TS"; break; case SCHED_BATCH: r.scheduling_class = "B"; break; #ifdef SCHED_IDLE case SCHED_IDLE: r.scheduling_class = "#5"; break; #endif case SCHED_FIFO: r.scheduling_class = "FF"; break; case SCHED_RR: r.scheduling_class = "RR"; break; default: r.scheduling_class = "?"; break; } // Calculate the start time s_time = time(NULL); now = localtime(&s_time); tyear = now->tm_year; tday = now->tm_yday; s_time = boot + (start / ticks); proc = localtime(&s_time); // Select format based on how long we've been running // // FROM THE SPEC: // "This is the time of day the process started formatted in HH:MM:SS if // the same day the process started or formatted as MMM_DD (Ex.: Feb_5) // if process started the previous day or further in the past." // if (tday != proc->tm_yday || tyear != proc->tm_year) fmt = "%b_%d"; else fmt = "%H:%M:%S"; strftime(sbuf, sizeof(sbuf), fmt, proc); r.command = cmd; r.exec_time = convert_time(t, tbuf, sizeof(tbuf)); r.pid = pid; r.ppid = ppid; r.priority = priority; r.start_time = sbuf; dev_to_tty(tty_dev, sizeof(tty_dev), (dev_t) tty_nr, pid, ABBREV_DEV); r.tty = tty_dev; get_uids(pid, &r); report_finding(&r, ctx); } SEXP_free(cmd_sexp); } closedir(d); return err; }
static int read_environment(SEXP_t *pid_ent, SEXP_t *name_ent, probe_ctx *ctx) { int err = 1, pid, fd; bool empty; size_t env_name_size; SEXP_t *env_name, *env_value, *item, *pid_sexp; DIR *d; struct dirent *d_entry; char *buffer, env_file[256], *null_char; ssize_t buffer_used; size_t buffer_size; d = opendir("/proc"); if (d == NULL) { dE("Can't read /proc: errno=%d, %s.\n", errno, strerror (errno)); return PROBE_EACCESS; } if ((buffer = oscap_realloc(NULL, BUFFER_SIZE)) == NULL) { dE("Can't allocate memory"); closedir(d); return PROBE_EFAULT; } buffer_size = BUFFER_SIZE; while ((d_entry = readdir(d))) { if (strspn(d_entry->d_name, "0123456789") != strlen(d_entry->d_name)) continue; pid = atoi(d_entry->d_name); pid_sexp = SEXP_number_newi_32(pid); if (probe_entobj_cmp(pid_ent, pid_sexp) != OVAL_RESULT_TRUE) { SEXP_free(pid_sexp); continue; } SEXP_free(pid_sexp); sprintf(env_file, "/proc/%d/environ", pid); if ((fd = open(env_file, O_RDONLY)) == -1) { dE("Can't open \"%s\": errno=%d, %s.\n", env_file, errno, strerror (errno)); item = probe_item_create( OVAL_INDEPENDENT_ENVIRONMENT_VARIABLE58, NULL, "pid", OVAL_DATATYPE_INTEGER, (int64_t)pid, NULL ); probe_item_setstatus(item, SYSCHAR_STATUS_ERROR); probe_item_add_msg(item, OVAL_MESSAGE_LEVEL_ERROR, "Can't open \"%s\": errno=%d, %s.", env_file, errno, strerror (errno)); probe_item_collect(ctx, item); continue; } empty = true; if ((buffer_used = read(fd, buffer, buffer_size - 1)) > 0) { empty = false; } while (! empty) { while (! (null_char = memchr(buffer, 0, buffer_used))) { ssize_t s; if ((size_t)buffer_used >= buffer_size) { buffer_size += BUFFER_SIZE; buffer = oscap_realloc(buffer, buffer_size); if (buffer == NULL) { dE("Can't allocate memory"); exit(ENOMEM); } } s = read(fd, buffer + buffer_used, buffer_size - buffer_used); if (s <= 0) { empty = true; buffer[buffer_used++] = 0; } else { buffer_used += s; } } do { char *eq_char = strchr(buffer, '='); if (eq_char == NULL) { /* strange but possible: * $ strings /proc/1218/environ /dev/input/event0 /dev/input/event1 /dev/input/event4 /dev/input/event3 */ buffer_used -= null_char + 1 - buffer; memmove(buffer, null_char + 1, buffer_used); continue; } env_name_size = eq_char - buffer; env_name = SEXP_string_new(buffer, env_name_size); env_value = SEXP_string_newf("%s", buffer + env_name_size + 1); if (probe_entobj_cmp(name_ent, env_name) == OVAL_RESULT_TRUE) { item = probe_item_create( OVAL_INDEPENDENT_ENVIRONMENT_VARIABLE58, NULL, "pid", OVAL_DATATYPE_INTEGER, (int64_t)pid, "name", OVAL_DATATYPE_SEXP, env_name, "value", OVAL_DATATYPE_SEXP, env_value, NULL); probe_item_collect(ctx, item); err = 0; } SEXP_free(env_name); SEXP_free(env_value); buffer_used -= null_char + 1 - buffer; memmove(buffer, null_char + 1, buffer_used); } while ((null_char = memchr(buffer, 0, buffer_used))); } close(fd); } closedir(d); oscap_free(buffer); if (err) { SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR, "Can't find process with requested PID."); probe_cobj_add_msg(probe_ctx_getresult(ctx), msg); SEXP_free(msg); err = 0; } return err; }
static int file_cb (const char *p, const char *f, void *ptr) { char path_buffer[PATH_MAX]; SEXP_t *item, xattr_name; struct cbargs *args = (struct cbargs *) ptr; const char *st_path; ssize_t xattr_count = -1; char *xattr_buf = NULL; size_t xattr_buflen = 0, i; if (f == NULL) { st_path = p; } else { snprintf (path_buffer, sizeof path_buffer, "%s/%s", p, f); st_path = path_buffer; } SEXP_init(&xattr_name); do { /* estimate the size of the buffer */ xattr_count = llistxattr(st_path, NULL, 0); if (xattr_count == 0) return (0); if (xattr_count < 0) { dI("FAIL: llistxattr(%s, %p, %zu): errno=%u, %s.", errno, strerror(errno)); return 0; } /* allocate space for xattr names */ xattr_buflen = xattr_count; xattr_buf = oscap_realloc(xattr_buf, sizeof(char) * xattr_buflen); /* fill the buffer */ xattr_count = llistxattr(st_path, xattr_buf, xattr_buflen); /* check & retry if needed */ } while (errno == ERANGE); if (xattr_count < 0) { dI("FAIL: llistxattr(%s, %p, %zu): errno=%u, %s.", errno, strerror(errno)); oscap_free(xattr_buf); } /* update lastpath if needed */ if (!SEXP_emptyp(&gr_lastpath)) { if (SEXP_strcmp(&gr_lastpath, p) != 0) { SEXP_free_r(&gr_lastpath); SEXP_string_new_r(&gr_lastpath, p, strlen(p)); } } else SEXP_string_new_r(&gr_lastpath, p, strlen(p)); i = 0; /* collect */ do { SEXP_string_new_r(&xattr_name, xattr_buf + i, strlen(xattr_buf +i)); if (probe_entobj_cmp(args->attr_ent, &xattr_name) == OVAL_RESULT_TRUE) { ssize_t xattr_vallen = -1; char *xattr_val = NULL; xattr_vallen = lgetxattr(st_path, xattr_buf + i, NULL, 0); retry_value: if (xattr_vallen >= 0) { // Check possible buffer overflow if (sizeof(char) * (xattr_vallen + 1) <= sizeof(char) * xattr_vallen) { dE("Attribute is too long."); abort(); } // Allocate buffer, '+1' is for trailing '\0' xattr_val = oscap_realloc(xattr_val, sizeof(char) * (xattr_vallen + 1)); // we don't want to override space for '\0' by call of 'lgetxattr' // we pass only 'xattr_vallen' instead of 'xattr_vallen + 1' xattr_vallen = lgetxattr(st_path, xattr_buf + i, xattr_val, xattr_vallen); if (xattr_vallen < 0 || errno == ERANGE) goto retry_value; xattr_val[xattr_vallen] = '\0'; item = probe_item_create(OVAL_UNIX_FILEEXTENDEDATTRIBUTE, NULL, "filepath", OVAL_DATATYPE_STRING, f == NULL ? NULL : st_path, "path", OVAL_DATATYPE_SEXP, &gr_lastpath, "filename", OVAL_DATATYPE_STRING, f == NULL ? "" : f, "attribute_name", OVAL_DATATYPE_SEXP, &xattr_name, "value", OVAL_DATATYPE_STRING, xattr_val, NULL); oscap_free(xattr_val); } else { dI("FAIL: lgetxattr(%s, %s, NULL, 0): errno=%u, %s.", errno, strerror(errno)); item = probe_item_create(OVAL_UNIX_FILEEXTENDEDATTRIBUTE, NULL, NULL); probe_item_setstatus(item, SYSCHAR_STATUS_ERROR); if (xattr_val != NULL) oscap_free(xattr_val); } probe_item_collect(args->ctx, item); /* XXX: handle ENOMEM */ } SEXP_free_r(&xattr_name); /* skip to next name */ while (i < xattr_buflen && xattr_buf[i] != '\0') ++i; ++i; } while (xattr_buf + i < xattr_buf + xattr_buflen - 1); oscap_free(xattr_buf); return (0); }
static int rpmverify_collect(probe_ctx *ctx, const char *name, oval_operation_t name_op, const char *file, oval_operation_t file_op, SEXP_t *name_ent, SEXP_t *filepath_ent, uint64_t flags, void (*callback)(probe_ctx *, struct rpmverify_res *)) { rpmdbMatchIterator match; rpmVerifyAttrs omit = (rpmVerifyAttrs)(flags & RPMVERIFY_RPMATTRMASK); Header pkgh; pcre *re = NULL; int ret = -1; /* pre-compile regex if needed */ if (file_op == OVAL_OPERATION_PATTERN_MATCH) { const char *errmsg; int erroff; re = pcre_compile(file, PCRE_UTF8, &errmsg, &erroff, NULL); if (re == NULL) { /* TODO */ return (-1); } } RPMVERIFY_LOCK; switch (name_op) { case OVAL_OPERATION_EQUALS: match = rpmtsInitIterator (g_rpm.rpmts, RPMTAG_NAME, (const void *)name, 0); if (match == NULL) { ret = 0; goto ret; } ret = rpmdbGetIteratorCount (match); break; case OVAL_OPERATION_NOT_EQUAL: match = rpmtsInitIterator (g_rpm.rpmts, RPMDBI_PACKAGES, NULL, 0); if (match == NULL) { ret = 0; goto ret; } if (rpmdbSetIteratorRE (match, RPMTAG_NAME, RPMMIRE_GLOB, "*") != 0) { ret = -1; goto ret; } break; case OVAL_OPERATION_PATTERN_MATCH: match = rpmtsInitIterator (g_rpm.rpmts, RPMDBI_PACKAGES, NULL, 0); if (match == NULL) { ret = 0; goto ret; } if (rpmdbSetIteratorRE (match, RPMTAG_NAME, RPMMIRE_REGEX, (const char *)name) != 0) { ret = -1; goto ret; } break; default: /* not supported */ dE("package name: operation not supported"); ret = -1; goto ret; } assume_d(RPMTAG_BASENAMES != 0, -1); assume_d(RPMTAG_DIRNAMES != 0, -1); while ((pkgh = rpmdbNextIterator (match)) != NULL) { rpmfi fi; rpmTag tag[2] = { RPMTAG_BASENAMES, RPMTAG_DIRNAMES }; struct rpmverify_res res; errmsg_t rpmerr; int i; SEXP_t *name_sexp; res.name = headerFormat(pkgh, "%{NAME}", &rpmerr); name_sexp = SEXP_string_newf("%s", res.name); if (probe_entobj_cmp(name_ent, name_sexp) != OVAL_RESULT_TRUE) { SEXP_free(name_sexp); continue; } SEXP_free(name_sexp); /* * Inspect package files & directories */ for (i = 0; i < 2; ++i) { fi = rpmfiNew(g_rpm.rpmts, pkgh, tag[i], 1); while (rpmfiNext(fi) != -1) { SEXP_t *filepath_sexp; res.fflags = rpmfiFFlags(fi); res.oflags = omit; if (((res.fflags & RPMFILE_CONFIG) && (flags & RPMVERIFY_SKIP_CONFIG)) || ((res.fflags & RPMFILE_GHOST) && (flags & RPMVERIFY_SKIP_GHOST))) continue; res.file = strdup(rpmfiFN(fi)); filepath_sexp = SEXP_string_newf("%s", res.file); if (probe_entobj_cmp(filepath_ent, filepath_sexp) != OVAL_RESULT_TRUE) { SEXP_free(filepath_sexp); free(res.file); continue; } SEXP_free(filepath_sexp); if (rpmVerifyFile(g_rpm.rpmts, fi, &res.vflags, omit) != 0) res.vflags = RPMVERIFY_FAILURES; callback(ctx, &res); free(res.file); } rpmfiFree(fi); } } match = rpmdbFreeIterator (match); ret = 0; ret: if (re != NULL) pcre_free(re); RPMVERIFY_UNLOCK; return (ret); }
static int read_process(SEXP_t *cmd_ent, SEXP_t *pid_ent, probe_ctx *ctx) { int err = 1, max_cap_id; DIR *d; struct dirent *ent; oval_version_t oval_version; d = opendir("/proc"); if (d == NULL) return err; // Get the time tick hertz ticks = (unsigned long)sysconf(_SC_CLK_TCK); get_boot_time(); oval_version = probe_obj_get_schema_version(probe_ctx_getobject(ctx)); if (oval_version_cmp(oval_version, OVAL_VERSION(5.11)) < 0) { max_cap_id = OVAL_5_8_MAX_CAP_ID; } else { max_cap_id = OVAL_5_11_MAX_CAP_ID; } struct oscap_buffer *cmdline_buffer = oscap_buffer_new(); char cmd_buffer[1 + 15 + 11 + 1]; // Format:" [ cmd:15 ] <defunc>" cmd_buffer[0] = '['; // Scan the directories while (( ent = readdir(d) )) { int fd, len; char buf[256]; char *tmp, state, tty_dev[128]; int pid, ppid, pgrp, session, tty_nr, tpgid; unsigned flags, sched_policy; unsigned long minflt, cminflt, majflt, cmajflt, uutime, ustime; long cutime, cstime, priority, cnice, nthreads, itrealvalue; unsigned long long start; SEXP_t *cmd_sexp = NULL, *pid_sexp = NULL; // Skip non-process58 dir entries if(*ent->d_name<'0' || *ent->d_name>'9') continue; errno = 0; pid = strtol(ent->d_name, NULL, 10); if (errno || pid == 2) // skip err & kthreads continue; // Parse up the stat file for the proc snprintf(buf, 32, "/proc/%d/stat", pid); fd = open(buf, O_RDONLY, 0); if (fd < 0) continue; len = read(fd, buf, sizeof buf - 1); close(fd); if (len < 40) continue; buf[len] = 0; tmp = strrchr(buf, ')'); if (tmp) *tmp = 0; else continue; memset(cmd_buffer + 1, 0, sizeof(cmd_buffer)-1); // clear cmd after starting '[' sscanf(buf, "%d (%15c", &ppid, cmd_buffer + 1); sscanf(tmp+2, "%c %d %d %d %d %d " "%u %lu %lu %lu %lu " "%lu %lu %lu %ld %ld " "%ld %ld %ld %llu", &state, &ppid, &pgrp, &session, &tty_nr, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &uutime, &ustime, &cutime, &cstime, &priority, &cnice, &nthreads, &itrealvalue, &start ); // Skip kthreads if (ppid == 2) continue; const char* cmd; if (state == 'Z') { // zombie cmd = make_defunc_str(cmd_buffer); } else { snprintf(buf, 32, "/proc/%d/cmdline", pid); if (get_process_cmdline(buf, cmdline_buffer)) { cmd = oscap_buffer_get_raw(cmdline_buffer); // use full cmdline } else { cmd = cmd_buffer + 1; } } err = 0; // If we get this far, no permission problems dI("Have command: %s\n", cmd); cmd_sexp = SEXP_string_newf("%s", cmd); pid_sexp = SEXP_number_newu_32(pid); if ((cmd_sexp == NULL || probe_entobj_cmp(cmd_ent, cmd_sexp) == OVAL_RESULT_TRUE) && (pid_sexp == NULL || probe_entobj_cmp(pid_ent, pid_sexp) == OVAL_RESULT_TRUE) ) { struct result_info r; unsigned long t = uutime/ticks + ustime/ticks; char tbuf[32], sbuf[32], *selinux_domain_label, **posix_capabilities; int tday,tyear; time_t s_time; struct tm *proc, *now; const char *fmt; // Now get scheduler policy sched_policy = sched_getscheduler(pid); switch (sched_policy) { case SCHED_OTHER: r.scheduling_class = "TS"; break; case SCHED_BATCH: r.scheduling_class = "B"; break; #ifdef SCHED_IDLE case SCHED_IDLE: r.scheduling_class = "#5"; break; #endif case SCHED_FIFO: r.scheduling_class = "FF"; break; case SCHED_RR: r.scheduling_class = "RR"; break; default: r.scheduling_class = "?"; break; } // Calculate the start time s_time = time(NULL); now = localtime(&s_time); tyear = now->tm_year; tday = now->tm_yday; s_time = boot + (start / ticks); proc = localtime(&s_time); // Select format based on how long we've been running // // FROM THE SPEC: // "This is the time of day the process started formatted in HH:MM:SS if // the same day the process started or formatted as MMM_DD (Ex.: Feb_5) // if process started the previous day or further in the past." // if (tday != proc->tm_yday || tyear != proc->tm_year) fmt = "%b_%d"; else fmt = "%H:%M:%S"; strftime(sbuf, sizeof(sbuf), fmt, proc); r.command_line = cmd; r.exec_time = convert_time(t, tbuf, sizeof(tbuf)); r.pid = pid; r.ppid = ppid; r.priority = priority; r.start_time = sbuf; dev_to_tty(tty_dev, sizeof(tty_dev), (dev_t) tty_nr, pid, ABBREV_DEV); r.tty = tty_dev; r.exec_shield = (get_exec_shield_status(pid) > 0); selinux_domain_label = get_selinux_label(pid); r.selinux_domain_label = selinux_domain_label; posix_capabilities = get_posix_capability(pid, max_cap_id); r.posix_capability = posix_capabilities; r.session_id = session; get_uids(pid, &r); report_finding(&r, ctx); if (selinux_domain_label != NULL) free(selinux_domain_label); if (posix_capabilities != NULL) { char **posix_capabilities_p = posix_capabilities; while (*posix_capabilities_p) free(*posix_capabilities_p++); free(posix_capabilities); } } SEXP_free(cmd_sexp); SEXP_free(pid_sexp); } closedir(d); oscap_buffer_free(cmdline_buffer); return err; }
static int get_runlevel_sysv (struct runlevel_req *req, struct runlevel_rep **rep, bool suse, const char *init_path, const char *rc_path) { const char runlevel_list[] = {'0', '1', '2', '3', '4', '5', '6'}; char pathbuf[PATH_MAX]; DIR *init_dir, *rc_dir; struct dirent *init_dp, *rc_dp; struct stat init_st, rc_st; struct runlevel_rep *rep_lst = NULL; _A(req != NULL); _A(rep != NULL); init_dir = opendir(init_path); if (init_dir == NULL) { dI("Can't open directory \"%s\": errno=%d, %s.", init_path, errno, strerror (errno)); return (-1); } while ((init_dp = readdir(init_dir)) != NULL) { char *service_name; unsigned int i; SEXP_t *r0; // Ensure that we are in the expected directory before // touching relative paths if (fchdir(dirfd(init_dir)) != 0) { dI("Can't fchdir to \"%s\": errno=%d, %s.", init_path, errno, strerror (errno)); closedir(init_dir); return -1; } if (stat(init_dp->d_name, &init_st) != 0) { dI("Can't stat file %s/%s: errno=%d, %s.", init_path, init_dp->d_name, errno, strerror(errno)); continue; } r0 = SEXP_string_newf("%s", init_dp->d_name); if (probe_entobj_cmp(req->service_name_ent, r0) != OVAL_RESULT_TRUE) { SEXP_free(r0); continue; } SEXP_free(r0); service_name = init_dp->d_name; for (i = 0; i < (sizeof (runlevel_list) / sizeof (runlevel_list[0])); ++i) { char runlevel[2] = {'\0', '\0'}; bool start, kill; r0 = SEXP_string_newf("%c", runlevel_list[i]); if (probe_entobj_cmp(req->runlevel_ent, r0) != OVAL_RESULT_TRUE) { SEXP_free(r0); continue; } SEXP_free(r0); runlevel[0] = runlevel_list[i]; snprintf(pathbuf, sizeof (pathbuf), rc_path, runlevel_list[i]); rc_dir = opendir(pathbuf); if (rc_dir == NULL) { dI("Can't open directory \"%s\": errno=%d, %s.", rc_path, errno, strerror (errno)); continue; } if (chdir(pathbuf) != 0) { dI("Can't fchdir to \"%s\": errno=%d, %s.", rc_path, errno, strerror (errno)); closedir(rc_dir); continue; } // On SUSE, the presence of a symbolic link to the init.d/<service> in // a runlevel directory rcx.d implies that the sevice is started on x. if (suse) { start = false; kill = true; } else start = kill = false; while ((rc_dp = readdir(rc_dir)) != NULL) { if (stat(rc_dp->d_name, &rc_st) != 0) { dI("Can't stat file %s/%s: errno=%d, %s.", rc_path, rc_dp->d_name, errno, strerror(errno)); continue; } if (init_st.st_ino == rc_st.st_ino) { if (suse) { if (rc_dp->d_name[0] == 'S') { start = true; kill = false; break; } } else { if (rc_dp->d_name[0] == 'S') { start = true; break; } else if (rc_dp->d_name[0] == 'K') { kill = true; break; } else { dI("Unexpected character in filename: %c, %s/%s.", rc_dp->d_name[0], pathbuf, rc_dp->d_name); } } } } closedir(rc_dir); if (rep_lst == NULL) { rep_lst = *rep = oscap_alloc(sizeof (struct runlevel_rep)); } else { rep_lst->next = oscap_alloc(sizeof (struct runlevel_rep)); rep_lst = rep_lst->next; } rep_lst->service_name = strdup(service_name); rep_lst->runlevel = strdup(runlevel); rep_lst->start = start; rep_lst->kill = kill; rep_lst->next = NULL; } } closedir(init_dir); return (1); }