| 1 | /** |
| 2 | * @file selinuxsecuritycontext_probe.c |
| 3 | * @brief selinuxsecuritycontext probe |
| 4 | * @author "Petr Lautrbach" <[email protected]> |
| 5 | * |
| 6 | * 2011/07/01 [email protected] |
| 7 | * This probe is able to process a selinuxsecuritycontext_object as defined in OVAL 5.9 |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Copyright 2011 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 | * "Petr Lautrbach" <[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 <probe/entcmp.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <fcntl.h> |
| 45 | #include <limits.h> |
| 46 | #include <pthread.h> |
| 47 | #include <dirent.h> |
| 48 | #include <errno.h> |
| 49 | |
| 50 | #include <selinux/selinux.h> |
| 51 | #include <selinux/context.h> |
| 52 | |
| 53 | #include "oval_fts.h" |
| 54 | #include "util.h" |
| 55 | #include "common/debug_priv.h" |
| 56 | #include "probe/probe.h" |
| 57 | #include "selinuxsecuritycontext_probe.h" |
| 58 | |
| 59 | #define FILE_SEPARATOR '/' |
| 60 | |
| 61 | static void split_level(const char *level, char **sensitivity, char **category) { |
| 62 | char *level_split; |
| 63 | |
| 64 | level_split = strchr(level, ':'); |
| 65 | if (level_split == NULL) { |
| 66 | *sensitivity = strdup(level); |
| 67 | *category = NULL; |
| 68 | } |
| 69 | else { |
| 70 | *sensitivity = strndup(level, level_split - level); |
| 71 | *category = strdup(level_split + 1); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* parse range according to |
| 76 | * http://selinuxproject.org/page/NB_MLS#MLS_.2F_MCS_Range_Format |
| 77 | */ |
| 78 | static void split_range(const char *range, char **l_s, char **l_c, char **h_s, char **h_c) { |
| 79 | char *level; |
| 80 | const char *range_split; |
| 81 | |
| 82 | if (range == NULL) { |
| 83 | dW("%s: range is NULL", __func__); |
| 84 | *l_s = *l_c = *h_s = *h_c = NULL; |
| 85 | return; |
| 86 | } |
| 87 | range_split = strchr(range, '-'); |
| 88 | if (range_split == NULL) { |
| 89 | /* there is only 1 level */ |
| 90 | split_level(range, l_s, l_c); |
| 91 | *h_s = NULL; |
| 92 | *h_c = NULL; |
| 93 | } |
| 94 | else { |
| 95 | /* low level */ |
| 96 | level = strndup(range, range_split - range); |
| 97 | split_level(level, l_s, l_c); |
| 98 | free(level); |
| 99 | |
| 100 | /* high level */ |
| 101 | level = strdup(range_split + 1); |
| 102 | split_level(level, h_s, h_c); |
| 103 | free(level); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static SEXP_t *create_process_probe_item_with_range( |
| 108 | const char *range, const char *user, const char *role, const char *type, |
| 109 | int pid_number) { |
| 110 | char *l_sensitivity, *l_category, *h_sensitivity, *h_category; |
| 111 | SEXP_t *item; |
| 112 | |
| 113 | split_range(range, &l_sensitivity, &l_category, &h_sensitivity, &h_category); |
| 114 | item = probe_item_create(OVAL_LINUX_SELINUXSECURITYCONTEXT, NULL, |
| 115 | "pid", OVAL_DATATYPE_INTEGER, (int64_t)pid_number, |
| 116 | |
| 117 | "user", OVAL_DATATYPE_STRING, user, |
| 118 | "role", OVAL_DATATYPE_STRING, role, |
| 119 | "type", OVAL_DATATYPE_STRING, type, |
| 120 | "low_sensitivity", OVAL_DATATYPE_STRING, l_sensitivity, |
| 121 | "low_category", OVAL_DATATYPE_STRING, l_category, |
| 122 | "high_sensitivity", OVAL_DATATYPE_STRING, h_sensitivity, |
| 123 | "high_category", OVAL_DATATYPE_STRING, h_category, |
| 124 | NULL); |
| 125 | free(l_sensitivity); |
| 126 | free(l_category); |
| 127 | free(h_sensitivity); |
| 128 | free(h_category); |
| 129 | |
| 130 | return item; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static SEXP_t *create_file_probe_item_with_range( |
| 135 | const char *range, const char *user, const char *role, const char *type, |
| 136 | const char *pbuf, const char *p, const char *f) { |
| 137 | char *l_sensitivity, *l_category, *h_sensitivity, *h_category; |
| 138 | SEXP_t *item; |
| 139 | |
| 140 | split_range(range, &l_sensitivity, &l_category, &h_sensitivity, &h_category); |
| 141 | item = probe_item_create(OVAL_LINUX_SELINUXSECURITYCONTEXT, NULL, |
| 142 | "filepath", OVAL_DATATYPE_STRING, pbuf, |
| 143 | "path", OVAL_DATATYPE_STRING, p, |
| 144 | "filename", OVAL_DATATYPE_STRING, f, |
| 145 | |
| 146 | "user", OVAL_DATATYPE_STRING, user, |
| 147 | "role", OVAL_DATATYPE_STRING, role, |
| 148 | "type", OVAL_DATATYPE_STRING, type, |
| 149 | "low_sensitivity", OVAL_DATATYPE_STRING, l_sensitivity, |
| 150 | "low_category", OVAL_DATATYPE_STRING, l_category, |
| 151 | "high_sensitivity", OVAL_DATATYPE_STRING, h_sensitivity, |
| 152 | "high_category", OVAL_DATATYPE_STRING, h_category, |
| 153 | |
| 154 | "rawlow_sensitivity", OVAL_DATATYPE_STRING, l_sensitivity, |
| 155 | "rawlow_category", OVAL_DATATYPE_STRING, l_category, |
| 156 | "rawhigh_sensitivity", OVAL_DATATYPE_STRING, h_sensitivity, |
| 157 | "rawhigh_category", OVAL_DATATYPE_STRING, h_category, |
| 158 | NULL); |
| 159 | free(l_sensitivity); |
| 160 | free(l_category); |
| 161 | free(h_sensitivity); |
| 162 | free(h_category); |
| 163 | |
| 164 | return item; |
| 165 | } |
| 166 | |
| 167 | static int selinuxsecuritycontext_process_cb (SEXP_t *pid_ent, probe_ctx *ctx) { |
| 168 | char path[PATH_MAX]; |
| 169 | SEXP_t *pid_sexp, *item; |
| 170 | char *pid_context; |
| 171 | context_t context; |
| 172 | int pid_number; |
| 173 | DIR *proc; |
| 174 | struct dirent *dir_entry; |
| 175 | const char *user, *role, *type, *range; |
| 176 | |
| 177 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 178 | snprintf (path, PATH_MAX, "%s/proc", prefix ? prefix : ""); |
| 179 | if ((proc = opendir(path)) == NULL) { |
| 180 | dE("Can't open '%s' dir: %s", path, strerror(errno)); |
| 181 | |
| 182 | probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR); |
| 183 | return errno; |
| 184 | } |
| 185 | |
| 186 | while ((dir_entry = readdir(proc))) { |
| 187 | if (strspn(dir_entry->d_name, "0123456789") != strlen(dir_entry->d_name)) |
| 188 | continue; |
| 189 | |
| 190 | pid_number = atoi(dir_entry->d_name); |
| 191 | pid_sexp = SEXP_number_newi_32(pid_number); |
| 192 | if (probe_entobj_cmp(pid_ent, pid_sexp) == OVAL_RESULT_TRUE) { |
| 193 | |
| 194 | if (getpidcon(pid_number, &pid_context) == -1) { |
| 195 | /* error getting pid selinux context */ |
| 196 | dW("Can't get selinux context for process %d", pid_number); |
| 197 | SEXP_free(pid_sexp); |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | context = context_new(pid_context); |
| 202 | |
| 203 | user = context_user_get(context); |
| 204 | role = context_role_get(context); |
| 205 | type = context_type_get(context); |
| 206 | range = context_range_get(context); |
| 207 | if (range != NULL) { |
| 208 | item = create_process_probe_item_with_range(range, user, role, type, pid_number); |
| 209 | } |
| 210 | else { |
| 211 | item = probe_item_create(OVAL_LINUX_SELINUXSECURITYCONTEXT, NULL, |
| 212 | "pid", OVAL_DATATYPE_INTEGER, (int64_t)pid_number, |
| 213 | "user", OVAL_DATATYPE_STRING, user, |
| 214 | "role", OVAL_DATATYPE_STRING, role, |
| 215 | "type", OVAL_DATATYPE_STRING, type, |
| 216 | NULL); |
| 217 | } |
| 218 | |
| 219 | probe_item_collect(ctx, item); |
| 220 | |
| 221 | context_free(context); |
| 222 | freecon(pid_context); |
| 223 | } |
| 224 | SEXP_free(pid_sexp); |
| 225 | } |
| 226 | closedir(proc); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | static int selinuxsecuritycontext_file_cb(const char *prefix, const char *p, const char *f, probe_ctx *ctx) |
| 233 | { |
| 234 | SEXP_t *item; |
| 235 | |
| 236 | char pbuf[PATH_MAX+1]; |
| 237 | size_t plen, flen; |
| 238 | |
| 239 | char *file_context; |
| 240 | int file_context_size; |
| 241 | context_t context; |
| 242 | const char *user, *role, *type, *range; |
| 243 | int err = 0; |
| 244 | |
| 245 | /* directory */ |
| 246 | if (f == NULL) |
| 247 | flen = 0; |
| 248 | else |
| 249 | flen = strlen (f); |
| 250 | |
| 251 | /* |
| 252 | * Prepare path |
| 253 | */ |
| 254 | plen = strlen (p); |
| 255 | |
| 256 | if (plen + flen + 1 > PATH_MAX) |
| 257 | return (-1); |
| 258 | |
| 259 | memcpy (pbuf, p, sizeof (char) * plen); |
| 260 | |
| 261 | if (p[plen - 1] != FILE_SEPARATOR) { |
| 262 | pbuf[plen] = FILE_SEPARATOR; |
| 263 | ++plen; |
| 264 | } |
| 265 | |
| 266 | if (f != NULL) |
| 267 | memcpy (pbuf + plen, f, sizeof (char) * flen); |
| 268 | |
| 269 | pbuf[plen+flen] = '\0'; |
| 270 | |
| 271 | char *path_with_prefix = oscap_path_join(prefix, pbuf); |
| 272 | if (access(path_with_prefix, F_OK) == -1 ) { |
| 273 | dD("File does not exists anymore (could happen to /dev/fd/X)"); |
| 274 | free(path_with_prefix); |
| 275 | return 0; |
| 276 | } |
| 277 | file_context_size = getfilecon(path_with_prefix, &file_context); |
| 278 | free(path_with_prefix); |
| 279 | if (file_context_size == -1) { |
| 280 | dD("Can't get context for %s: %s", pbuf, strerror(errno)); |
| 281 | |
| 282 | item = probe_item_create(OVAL_LINUX_SELINUXSECURITYCONTEXT, NULL, |
| 283 | "filepath", OVAL_DATATYPE_STRING, pbuf, |
| 284 | "path", OVAL_DATATYPE_STRING, p, |
| 285 | "filename", OVAL_DATATYPE_STRING, f, |
| 286 | NULL); |
| 287 | |
| 288 | probe_item_add_msg(item, OVAL_MESSAGE_LEVEL_ERROR, |
| 289 | "Can't get context for %s: %s\n", pbuf, strerror(errno)); |
| 290 | probe_item_setstatus(item, SYSCHAR_STATUS_ERROR); |
| 291 | err = errno; |
| 292 | |
| 293 | } |
| 294 | else { |
| 295 | context = context_new(file_context); |
| 296 | |
| 297 | user = context_user_get(context); |
| 298 | role = context_role_get(context); |
| 299 | type = context_type_get(context); |
| 300 | range = context_range_get(context); |
| 301 | |
| 302 | if (range != NULL) { |
| 303 | item = create_file_probe_item_with_range(range, user, role, type, pbuf, p, f); |
| 304 | } |
| 305 | else { |
| 306 | item = probe_item_create(OVAL_LINUX_SELINUXSECURITYCONTEXT, NULL, |
| 307 | "filepath", OVAL_DATATYPE_STRING, pbuf, |
| 308 | "path", OVAL_DATATYPE_STRING, p, |
| 309 | "filename", OVAL_DATATYPE_STRING, f, |
| 310 | "user", OVAL_DATATYPE_STRING, user, |
| 311 | "role", OVAL_DATATYPE_STRING, role, |
| 312 | "type", OVAL_DATATYPE_STRING, type, |
| 313 | NULL); |
| 314 | } |
| 315 | |
| 316 | context_free(context); |
| 317 | } |
| 318 | probe_item_collect(ctx, item); |
| 319 | |
| 320 | if (file_context != NULL) |
| 321 | freecon(file_context); |
| 322 | |
| 323 | return (err); |
| 324 | } |
| 325 | |
| 326 | int selinuxsecuritycontext_probe_offline_mode_supported(void) |
| 327 | { |
| 328 | return PROBE_OFFLINE_OWN; |
| 329 | } |
| 330 | |
| 331 | int selinuxsecuritycontext_probe_main(probe_ctx *ctx, void *arg) |
| 332 | { |
| 333 | SEXP_t *probe_in; |
| 334 | SEXP_t *path, *filename, *filepath, *pid, *behaviors = NULL; |
| 335 | int process_pid, err = 0; |
| 336 | |
| 337 | OVAL_FTS *ofts; |
| 338 | OVAL_FTSENT *ofts_ent; |
| 339 | |
| 340 | probe_in = probe_ctx_getobject(ctx); |
| 341 | |
| 342 | behaviors = probe_obj_getent(probe_in, "behaviors", 1); |
| 343 | path = probe_obj_getent(probe_in, "path", 1); |
| 344 | filename = probe_obj_getent(probe_in, "filename", 1); |
| 345 | filepath = probe_obj_getent(probe_in, "filepath", 1); |
| 346 | pid = probe_obj_getent(probe_in, "pid", 1); |
| 347 | |
| 348 | if (((path == NULL || filename == NULL) && filepath==NULL ) && pid == NULL) { |
| 349 | err = PROBE_ENOENT; |
| 350 | goto cleanup; |
| 351 | } |
| 352 | |
| 353 | if (filepath || (path && filename)) { |
| 354 | probe_filebehaviors_canonicalize(&behaviors); |
| 355 | |
| 356 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 357 | if ((ofts = oval_fts_open_prefixed(prefix, path, filename, filepath, behaviors, probe_ctx_getresult(ctx))) != NULL) { |
| 358 | while ((ofts_ent = oval_fts_read(ofts)) != NULL) { |
| 359 | selinuxsecuritycontext_file_cb(prefix, ofts_ent->path, ofts_ent->file, ctx); |
| 360 | oval_ftsent_free(ofts_ent); |
| 361 | } |
| 362 | |
| 363 | oval_fts_close(ofts); |
| 364 | } |
| 365 | SEXP_free(behaviors); |
| 366 | } |
| 367 | |
| 368 | if (pid != NULL) { |
| 369 | PROBE_ENT_I32VAL(pid, process_pid, err = PROBE_ENOVAL;, process_pid = 0;); |
| 370 | if (err != 0) |
| 371 | goto cleanup; |
| 372 | |
| 373 | if (process_pid == 0) { |
| 374 | if (ctx->offline_mode == PROBE_OFFLINE_NONE) { |
| 375 | SEXP_t *nref, *nval, *pid2; |
| 376 | |
| 377 | nref = SEXP_list_first(probe_in); |
| 378 | nval = SEXP_number_newu_32(getpid()); |
| 379 | pid2 = SEXP_list_new(nref, nval, NULL); |
| 380 | SEXP_free(pid); |
| 381 | SEXP_free(nref); |
| 382 | SEXP_free(nval); |
| 383 | pid = pid2; |
| 384 | } else { |
| 385 | err = PROBE_EINVAL; |
| 386 | goto cleanup; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | selinuxsecuritycontext_process_cb(pid, ctx); |
| 391 | } |
| 392 | |
| 393 | cleanup: |
| 394 | SEXP_free (path); |
| 395 | SEXP_free (filename); |
| 396 | SEXP_free (filepath); |
| 397 | SEXP_free (pid); |
| 398 | |
| 399 | return err; |
| 400 | } |