| 1 | /** |
| 2 | * @file selinuxboolean_probe.c |
| 3 | * @brief selinuxboolean probe |
| 4 | * @author "Petr Lautrbach" <[email protected]> |
| 5 | * |
| 6 | * This probe is able to process a selinuxboolean_object as defined in OVAL 5.8. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Copyright 2009-2011 Red Hat Inc., Durham, North Carolina. |
| 12 | * All Rights Reserved. |
| 13 | * |
| 14 | * This library is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU Lesser General Public |
| 16 | * License as published by the Free Software Foundation; either |
| 17 | * version 2.1 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This library is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | * Lesser General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Lesser General Public |
| 25 | * License along with this library; if not, write to the Free Software |
| 26 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 | * |
| 28 | * Authors: |
| 29 | * Petr Lautrbach <[email protected]> |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * selinuxboolean probe: |
| 34 | * |
| 35 | * name |
| 36 | * current_status |
| 37 | * pending_status |
| 38 | */ |
| 39 | |
| 40 | #ifdef HAVE_CONFIG_H |
| 41 | #include <config.h> |
| 42 | #endif |
| 43 | |
| 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
| 46 | #include <stdio.h> |
| 47 | #include <stdio_ext.h> |
| 48 | #include <errno.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <sys/vfs.h> |
| 51 | #include <sys/statvfs.h> |
| 52 | #include <fcntl.h> |
| 53 | #include <limits.h> |
| 54 | |
| 55 | #include "_seap.h" |
| 56 | #include "probe-api.h" |
| 57 | #include "probe/entcmp.h" |
| 58 | #include <probe/probe.h> |
| 59 | |
| 60 | #include <selinux/selinux.h> |
| 61 | #include "selinuxboolean_probe.h" |
| 62 | |
| 63 | |
| 64 | /* These definitions and three functions were commandeered from SELinux internals |
| 65 | * and creatively improved to take the global prefix into account */ |
| 66 | |
| 67 | #define OLDSELINUXMNT "/selinux" |
| 68 | #define SELINUXMNT "/sys/fs/selinux" |
| 69 | #define SELINUXFS "selinuxfs" |
| 70 | #define SELINUX_MAGIC 0xf97cff8c |
| 71 | |
| 72 | static int verify_selinuxmnt_prefixed(const char *prefix, const char *mnt) |
| 73 | { |
| 74 | char path[PATH_MAX] = {0}; |
| 75 | struct statfs sfbuf; |
| 76 | int rc; |
| 77 | |
| 78 | if (!prefix) |
| 79 | return -1; |
| 80 | |
| 81 | snprintf(path, PATH_MAX, "%s%s", prefix, mnt); |
| 82 | |
| 83 | do { |
| 84 | rc = statfs(path, &sfbuf); |
| 85 | } while (rc < 0 && errno == EINTR); |
| 86 | if (rc == 0) { |
| 87 | if ((uint32_t)sfbuf.f_type == (uint32_t)SELINUX_MAGIC) { |
| 88 | struct statvfs vfsbuf; |
| 89 | rc = statvfs(path, &vfsbuf); |
| 90 | if (rc == 0) { |
| 91 | if (!(vfsbuf.f_flag & ST_RDONLY)) { |
| 92 | set_selinuxmnt(path); |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | static int selinuxfs_exists_prefixed(const char *prefix) |
| 103 | { |
| 104 | char path[PATH_MAX] = {0}; |
| 105 | int exists = 0; |
| 106 | FILE *fp = NULL; |
| 107 | char *buf = NULL; |
| 108 | size_t len; |
| 109 | ssize_t num; |
| 110 | |
| 111 | if (!prefix) |
| 112 | return 0; |
| 113 | |
| 114 | snprintf(path, PATH_MAX, "%s/proc/filesystems", prefix); |
| 115 | fp = fopen(path, "re"); |
| 116 | if (!fp) |
| 117 | return 1; /* Fail as if it exists */ |
| 118 | __fsetlocking(fp, FSETLOCKING_BYCALLER); |
| 119 | |
| 120 | num = getline(&buf, &len, fp); |
| 121 | while (num != -1) { |
| 122 | if (strstr(buf, SELINUXFS)) { |
| 123 | exists = 1; |
| 124 | break; |
| 125 | } |
| 126 | num = getline(&buf, &len, fp); |
| 127 | } |
| 128 | |
| 129 | free(buf); |
| 130 | fclose(fp); |
| 131 | return exists; |
| 132 | } |
| 133 | |
| 134 | static int init_selinuxmnt_prefixed(const char *prefix) |
| 135 | { |
| 136 | char path[PATH_MAX] = {0}; |
| 137 | char *buf = NULL, *p; |
| 138 | FILE *fp = NULL; |
| 139 | size_t len; |
| 140 | ssize_t num; |
| 141 | int res = -1; |
| 142 | |
| 143 | if (!prefix) |
| 144 | return res; |
| 145 | |
| 146 | if (verify_selinuxmnt_prefixed(prefix, SELINUXMNT) == 0) |
| 147 | return 0; |
| 148 | |
| 149 | if (verify_selinuxmnt_prefixed(prefix, OLDSELINUXMNT) == 0) |
| 150 | return 0; |
| 151 | |
| 152 | /* Drop back to detecting it the long way. */ |
| 153 | if (!selinuxfs_exists_prefixed(prefix)) |
| 154 | goto out; |
| 155 | |
| 156 | /* At this point, the usual spot doesn't have an selinuxfs so |
| 157 | * we look around for it */ |
| 158 | snprintf(path, PATH_MAX, "%s/proc/mounts", prefix); |
| 159 | fp = fopen(path, "re"); |
| 160 | if (!fp) |
| 161 | goto out; |
| 162 | |
| 163 | __fsetlocking(fp, FSETLOCKING_BYCALLER); |
| 164 | while ((num = getline(&buf, &len, fp)) != -1) { |
| 165 | char *tmp; |
| 166 | p = strchr(buf, ' '); |
| 167 | if (!p) |
| 168 | goto out; |
| 169 | p++; |
| 170 | tmp = strchr(p, ' '); |
| 171 | if (!tmp) |
| 172 | goto out; |
| 173 | if (!strncmp(tmp + 1, SELINUXFS" ", strlen(SELINUXFS)+1)) { |
| 174 | *tmp = '\0'; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* If we found something, dup it */ |
| 180 | if (num > 0) |
| 181 | res = verify_selinuxmnt_prefixed(prefix, p); |
| 182 | |
| 183 | out: |
| 184 | free(buf); |
| 185 | if (fp) |
| 186 | fclose(fp); |
| 187 | return res; |
| 188 | } |
| 189 | |
| 190 | /* End of borrowing from SELinux */ |
| 191 | |
| 192 | static int get_selinuxboolean(SEXP_t *ut_ent, probe_ctx *ctx) |
| 193 | { |
| 194 | int err = 1, active, pending, len, i; |
| 195 | SEXP_t *boolean, *item; |
| 196 | char **booleans; |
| 197 | |
| 198 | if (security_get_boolean_names(&booleans, &len) == -1) { |
| 199 | probe_cobj_set_flag(probe_ctx_getresult(ctx), SYSCHAR_FLAG_ERROR); |
| 200 | return err; |
| 201 | } |
| 202 | |
| 203 | for (i = 0; i < len; i++) { |
| 204 | boolean = SEXP_string_new(booleans[i], strlen(booleans[i])); |
| 205 | if (probe_entobj_cmp(ut_ent, boolean) == OVAL_RESULT_TRUE) { |
| 206 | active = security_get_boolean_active(booleans[i]); |
| 207 | pending = security_get_boolean_pending(booleans[i]); |
| 208 | item = probe_item_create( |
| 209 | OVAL_LINUX_SELINUXBOOLEAN, NULL, |
| 210 | "name", OVAL_DATATYPE_SEXP, boolean, |
| 211 | "current_status", OVAL_DATATYPE_BOOLEAN, active, |
| 212 | "pending_status", OVAL_DATATYPE_BOOLEAN, pending, |
| 213 | NULL); |
| 214 | probe_item_collect(ctx, item); |
| 215 | } |
| 216 | SEXP_free(boolean); |
| 217 | } |
| 218 | |
| 219 | for (i = 0; i < len; i++) |
| 220 | free(booleans[i]); |
| 221 | free(booleans); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | int selinuxboolean_probe_offline_mode_supported(void) |
| 227 | { |
| 228 | return PROBE_OFFLINE_OWN; |
| 229 | } |
| 230 | |
| 231 | int selinuxboolean_probe_main(probe_ctx *ctx, void *arg) |
| 232 | { |
| 233 | SEXP_t *probe_in, *name; |
| 234 | int err; |
| 235 | |
| 236 | probe_in = probe_ctx_getobject(ctx); |
| 237 | name = probe_obj_getent(probe_in, "name", 1); |
| 238 | |
| 239 | if (name == NULL) { |
| 240 | return PROBE_ENOVAL; |
| 241 | } |
| 242 | |
| 243 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 244 | if (prefix != NULL) { |
| 245 | if (init_selinuxmnt_prefixed(prefix)) { |
| 246 | SEXP_free(name); |
| 247 | return PROBE_ESUCCESS; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | err = get_selinuxboolean(name, ctx); |
| 252 | SEXP_free(name); |
| 253 | |
| 254 | return err; |
| 255 | } |