| 1 | /** |
| 2 | * @file partition_probe.c |
| 3 | * @brief partition probe implementation |
| 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 | #if defined(OS_LINUX) |
| 35 | #undef _XOPEN_SOURCE |
| 36 | #define _XOPEN_SOURCE /* for fdopen */ |
| 37 | #include <sys/vfs.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <stdio.h> |
| 42 | #include <linux/fs.h> |
| 43 | |
| 44 | #ifdef HAVE_PROC_MAGIC |
| 45 | #include <linux/magic.h> |
| 46 | #else /* RHEL5 work-around */ |
| 47 | #define PROC_SUPER_MAGIC 0x9fa0 |
| 48 | #endif |
| 49 | |
| 50 | #endif |
| 51 | |
| 52 | |
| 53 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 54 | #include <blkid/blkid.h> |
| 55 | #endif |
| 56 | |
| 57 | #include <stdlib.h> |
| 58 | #include <string.h> |
| 59 | #include <stdint.h> |
| 60 | #include <limits.h> |
| 61 | #include <sys/statvfs.h> |
| 62 | #include <probe-api.h> |
| 63 | #include <probe/probe.h> |
| 64 | #include <probe/option.h> |
| 65 | #include <mntent.h> |
| 66 | |
| 67 | #include "common/debug_priv.h" |
| 68 | #include "common/oscap_pcre.h" |
| 69 | #include "partition_probe.h" |
| 70 | |
| 71 | #ifndef MTAB_PATH |
| 72 | # define MTAB_PATH "/proc/mounts" |
| 73 | #endif |
| 74 | |
| 75 | #ifndef MTAB_LINE_MAX |
| 76 | # define MTAB_LINE_MAX 4096 |
| 77 | #endif |
| 78 | |
| 79 | const char *__OVAL_fs_types[][2] = { |
| 80 | { "adfs", "ADFS_SUPER_MAGIC" }, |
| 81 | { "affs", "AFFS_SUPER_MAGIC" }, |
| 82 | { "afs", "AFS_SUPER_MAGIC" }, |
| 83 | { "autofs", "AUTOFS_SUPER_MAGIC" }, |
| 84 | { "coda", "CODA_SUPER_MAGIC" }, |
| 85 | { "cramfs", "CRAMFS_MAGIC" }, |
| 86 | { "cramfs", "CRAMFS_MAGIC_WEND" }, |
| 87 | { "debugfs", "DEBUGFS_MAGIC" }, |
| 88 | { "sysfs", "SYSFS_MAGIC" }, |
| 89 | { "securityfs", "SECURITYFS_MAGIC" }, |
| 90 | { "selinuxfs", "SELINUX_MAGIC" }, |
| 91 | { "ramfs", "RAMFS_MAGIC" }, |
| 92 | { "tmpfs", "TMPFS_MAGIC" }, |
| 93 | { "hugetlbfs", "HUGETLBFS_MAGIC" }, |
| 94 | { "squashfs", "SQUASHFS_MAGIC" }, |
| 95 | { "efs", "EFS_SUPER_MAGIC" }, |
| 96 | { "ext2", "EXT2_SUPER_MAGIC" }, |
| 97 | { "ext3", "EXT3_SUPER_MAGIC" }, |
| 98 | { "xenfs", "XENFS_SUPER_MAGIC" }, |
| 99 | { "ext4", "EXT4_SUPER_MAGIC" }, |
| 100 | { "btrfs", "BTRFS_SUPER_MAGIC" }, |
| 101 | { "hpfs", "HPFS_SUPER_MAGIC" }, |
| 102 | { "iso9660", "ISOFS_SUPER_MAGIC" }, |
| 103 | { "jffs2", "JFFS2_SUPER_MAGIC" }, |
| 104 | { "anon", "ANON_INODE_FS_MAGIC" }, |
| 105 | { "minix", "MINIX_SUPER_MAGIC" }, |
| 106 | { "minix", "MINIX_SUPER_MAGIC2" }, |
| 107 | { "minix2", "MINIX2_SUPER_MAGIC" }, |
| 108 | { "minix2", "MINIX2_SUPER_MAGIC2" }, |
| 109 | { "minix3", "MINIX3_SUPER_MAGIC" }, |
| 110 | { "msdos", "MSDOS_SUPER_MAGIC" }, |
| 111 | { "ncpfs", "NCP_SUPER_MAGIC" }, |
| 112 | { "nfs", "NFS_SUPER_MAGIC" }, |
| 113 | { "openprom", "OPENPROM_SUPER_MAGIC" }, |
| 114 | { "proc", "PROC_SUPER_MAGIC" }, |
| 115 | { "qnx4", "QNX4_SUPER_MAGIC" }, |
| 116 | { "reiserfs", "REISERFS_SUPER_MAGIC" }, |
| 117 | { "reiserfs", "REISERFS_SUPER_MAGIC_STRING" }, |
| 118 | { "reiser2fs", "REISER2FS_SUPER_MAGIC_STRING" }, |
| 119 | { "reiser2fs", "REISER2FS_JR_SUPER_MAGIC_STRING" }, |
| 120 | { "smbfs", "SMB_SUPER_MAGIC" }, |
| 121 | { "usbdevfs", "USBDEVICE_SUPER_MAGIC" }, |
| 122 | { "usbfs", "USBDEVICE_SUPER_MAGIC" }, |
| 123 | { "cgroup", "CGROUP_SUPER_MAGIC" }, |
| 124 | { "futexfs", "FUTEXFS_SUPER_MAGIC" }, |
| 125 | { "stack", "STACK_END_MAGIC" }, |
| 126 | { "devpts", "DEVPTS_SUPER_MAGIC" }, |
| 127 | { "sockfs", "SOCKFS_MAGIC" } |
| 128 | }; |
| 129 | |
| 130 | static const char *correct_fstype(char *type) |
| 131 | { |
| 132 | register size_t i; |
| 133 | |
| 134 | for (i = 0; i < sizeof __OVAL_fs_types/(sizeof(char *) * 2); ++i) { |
| 135 | if (strcmp(__OVAL_fs_types[i][0], type) == 0) |
| 136 | return __OVAL_fs_types[i][1]; |
| 137 | } |
| 138 | |
| 139 | return ""; |
| 140 | } |
| 141 | |
| 142 | static uint8_t add_mnt_opt(char ***mnt_opts, uint8_t mnt_ocnt, char *opt) |
| 143 | { |
| 144 | void *new_mnt_opts = realloc(*mnt_opts, sizeof(char *) * (mnt_ocnt + 2)); |
| 145 | if (new_mnt_opts == NULL) |
| 146 | return mnt_ocnt; |
| 147 | *mnt_opts = new_mnt_opts; |
| 148 | (*mnt_opts)[mnt_ocnt] = opt; |
| 149 | (*mnt_opts)[mnt_ocnt + 1] = NULL; |
| 150 | return mnt_ocnt + 1; |
| 151 | } |
| 152 | |
| 153 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 154 | static int collect_item(probe_ctx *ctx, oval_schema_version_t over, struct mntent *mnt_ent, blkid_cache blkcache) |
| 155 | #else |
| 156 | static int collect_item(probe_ctx *ctx, oval_schema_version_t over, struct mntent *mnt_ent) |
| 157 | #endif |
| 158 | { |
| 159 | SEXP_t *item; |
| 160 | char *uuid = "", *tok, *save = NULL, **mnt_opts = NULL; |
| 161 | char path[PATH_MAX]; |
| 162 | uint8_t mnt_ocnt; |
| 163 | struct statvfs stvfs; |
| 164 | |
| 165 | /* |
| 166 | * Get FS stats |
| 167 | */ |
| 168 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 169 | snprintf(path, PATH_MAX, "%s%s", prefix ? prefix : "", mnt_ent->mnt_dir); |
| 170 | if (statvfs(path, &stvfs) != 0) { |
| 171 | dE("Can't statvfs %s: errno=%d, %s.", path, errno, strerror(errno)); |
| 172 | return (-1); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Get UUID |
| 177 | */ |
| 178 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 179 | uuid = blkid_get_tag_value(blkcache, "UUID", mnt_ent->mnt_fsname); |
| 180 | if (uuid == NULL) { |
| 181 | uuid = strdup(""); |
| 182 | } |
| 183 | #endif |
| 184 | /* |
| 185 | * Create a NULL-terminated array from the mount options |
| 186 | */ |
| 187 | mnt_ocnt = 0; |
| 188 | |
| 189 | tok = strtok_r(mnt_ent->mnt_opts, ",", &save); |
| 190 | |
| 191 | do { |
| 192 | mnt_ocnt = add_mnt_opt(&mnt_opts, mnt_ocnt, tok); |
| 193 | } while ((tok = strtok_r(NULL, ",", &save)) != NULL); |
| 194 | |
| 195 | /* |
| 196 | * Check for "remount", "bind" and "move" mount options |
| 197 | * These options can't be found in /proc/mounts, |
| 198 | * we must use flags got by statvfs(). |
| 199 | */ |
| 200 | if (stvfs.f_flag & MS_REMOUNT) { |
| 201 | mnt_ocnt = add_mnt_opt(&mnt_opts, mnt_ocnt, "remount"); |
| 202 | } |
| 203 | if (stvfs.f_flag & MS_BIND) { |
| 204 | mnt_ocnt = add_mnt_opt(&mnt_opts, mnt_ocnt, "bind"); |
| 205 | } |
| 206 | if (stvfs.f_flag & MS_MOVE) { |
| 207 | mnt_ocnt = add_mnt_opt(&mnt_opts, mnt_ocnt, "move"); |
| 208 | } |
| 209 | |
| 210 | dD("mnt_ocnt = %d, mnt_opts[mnt_ocnt]=%p", mnt_ocnt, mnt_opts == NULL ? NULL : mnt_opts[mnt_ocnt]); |
| 211 | |
| 212 | /* |
| 213 | * "Correct" the type (this won't be (hopefully) needed in a later version |
| 214 | * of OVAL) |
| 215 | */ |
| 216 | if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.10)) < 0) |
| 217 | mnt_ent->mnt_type = (char *)correct_fstype(mnt_ent->mnt_type); |
| 218 | |
| 219 | /* |
| 220 | * Create the item |
| 221 | */ |
| 222 | item = probe_item_create(OVAL_LINUX_PARTITION, NULL, |
| 223 | "mount_point", OVAL_DATATYPE_STRING, mnt_ent->mnt_dir, |
| 224 | "device", OVAL_DATATYPE_STRING, mnt_ent->mnt_fsname, |
| 225 | "uuid", OVAL_DATATYPE_STRING, uuid, |
| 226 | "fs_type", OVAL_DATATYPE_STRING, mnt_ent->mnt_type, |
| 227 | "mount_options", OVAL_DATATYPE_STRING_M, mnt_opts, |
| 228 | "total_space", OVAL_DATATYPE_INTEGER, (int64_t)stvfs.f_blocks, |
| 229 | "space_used", OVAL_DATATYPE_INTEGER, (int64_t)(stvfs.f_blocks - stvfs.f_bfree), |
| 230 | "space_left", OVAL_DATATYPE_INTEGER, (int64_t)stvfs.f_bfree, |
| 231 | NULL); |
| 232 | |
| 233 | if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.11.2)) >= 0) { |
| 234 | SEXP_t *block_size = SEXP_number_newi_64(stvfs.f_frsize); |
| 235 | probe_item_ent_add(item, "block_size", NULL, block_size); |
| 236 | SEXP_free(block_size); |
| 237 | } |
| 238 | |
| 239 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 240 | /* |
| 241 | * If the partition doesn't have an UUID assigned, set the uuid entity status to |
| 242 | * "does not exist" which means that the value was collected but does not exist |
| 243 | * on the system. |
| 244 | */ |
| 245 | if (strcmp(uuid, "") == 0) { |
| 246 | probe_itement_setstatus(item, "uuid", 1, SYSCHAR_STATUS_DOES_NOT_EXIST); |
| 247 | } |
| 248 | free(uuid); |
| 249 | #else |
| 250 | /* Compiled without blkid library, we don't collect UUID */ |
| 251 | probe_itement_setstatus(item, "uuid", 1, SYSCHAR_STATUS_NOT_COLLECTED); |
| 252 | #endif /* HAVE_BLKID_GET_TAG_VALUE */ |
| 253 | |
| 254 | probe_item_collect(ctx, item); |
| 255 | free(mnt_opts); |
| 256 | |
| 257 | return (0); |
| 258 | } |
| 259 | |
| 260 | int patition_probe_offline_mode_supported(void) |
| 261 | { |
| 262 | return PROBE_OFFLINE_OWN; |
| 263 | } |
| 264 | |
| 265 | int partition_probe_main(probe_ctx *ctx, void *probe_arg) |
| 266 | { |
| 267 | int probe_ret = 0; |
| 268 | SEXP_t *mnt_entity, *mnt_opval, *mnt_entval, *probe_in; |
| 269 | char mnt_path[PATH_MAX]; |
| 270 | oval_operation_t mnt_op; |
| 271 | FILE *mnt_fp; |
| 272 | oval_schema_version_t obj_over; |
| 273 | |
| 274 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 275 | snprintf(mnt_path, PATH_MAX, "%s"MTAB_PATH, prefix ? prefix : ""); |
| 276 | |
| 277 | #if defined(OS_LINUX) |
| 278 | int mnt_fd; |
| 279 | struct statfs stfs; |
| 280 | |
| 281 | mnt_fd = open(mnt_path, O_RDONLY); |
| 282 | |
| 283 | if (mnt_fd < 0) { |
| 284 | if (!prefix) |
| 285 | dE("Can't open %s: errno=%d, %s.", mnt_path, errno, strerror(errno)); |
| 286 | return (prefix ? PROBE_ESUCCESS : PROBE_ESYSTEM); |
| 287 | } |
| 288 | |
| 289 | if (fstatfs(mnt_fd, &stfs) != 0) { |
| 290 | close(mnt_fd); |
| 291 | return (prefix ? PROBE_ESUCCESS : PROBE_ESYSTEM); |
| 292 | } |
| 293 | |
| 294 | if (stfs.f_type != PROC_SUPER_MAGIC) { |
| 295 | close(mnt_fd); |
| 296 | return (prefix ? PROBE_ESUCCESS : PROBE_EFATAL); |
| 297 | } |
| 298 | |
| 299 | mnt_fp = fdopen(mnt_fd, "r"); |
| 300 | |
| 301 | if (mnt_fp == NULL) { |
| 302 | close(mnt_fd); |
| 303 | return (prefix ? PROBE_ESUCCESS : PROBE_ESYSTEM); |
| 304 | } |
| 305 | #else |
| 306 | mnt_fp = fopen(mnt_path, "r"); |
| 307 | |
| 308 | if (mnt_fp == NULL) { |
| 309 | if (!prefix) |
| 310 | dE("Can't open %s: errno=%d, %s.", mnt_path, errno, strerror(errno)); |
| 311 | return (prefix ? PROBE_ESUCCESS : PROBE_ESYSTEM); |
| 312 | } |
| 313 | #endif |
| 314 | |
| 315 | probe_in = probe_ctx_getobject(ctx); |
| 316 | obj_over = probe_obj_get_platform_schema_version(probe_in); |
| 317 | mnt_entity = probe_obj_getent(probe_in, "mount_point", 1); |
| 318 | |
| 319 | if (mnt_entity == NULL) { |
| 320 | fclose(mnt_fp); |
| 321 | return (PROBE_ENOENT); |
| 322 | } |
| 323 | |
| 324 | mnt_opval = probe_ent_getattrval(mnt_entity, "operation"); |
| 325 | |
| 326 | if (mnt_opval != NULL) { |
| 327 | mnt_op = (oval_operation_t)SEXP_number_geti(mnt_opval); |
| 328 | SEXP_free(mnt_opval); |
| 329 | } else |
| 330 | mnt_op = OVAL_OPERATION_EQUALS; |
| 331 | |
| 332 | mnt_entval = probe_ent_getval(mnt_entity); |
| 333 | |
| 334 | if (!SEXP_stringp(mnt_entval)) { |
| 335 | SEXP_free(mnt_entval); |
| 336 | SEXP_free(mnt_entity); |
| 337 | fclose(mnt_fp); |
| 338 | return (PROBE_EINVAL); |
| 339 | } |
| 340 | |
| 341 | SEXP_string_cstr_r(mnt_entval, mnt_path, sizeof mnt_path); |
| 342 | SEXP_free(mnt_entval); |
| 343 | SEXP_free(mnt_entity); |
| 344 | |
| 345 | if (mnt_fp != NULL) { |
| 346 | char buffer[MTAB_LINE_MAX]; |
| 347 | struct mntent mnt_ent, *mnt_entp; |
| 348 | |
| 349 | oscap_pcre_t *re = NULL; |
| 350 | char *estr = NULL; |
| 351 | int eoff = -1; |
| 352 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 353 | blkid_cache blkcache; |
| 354 | |
| 355 | if (blkid_get_cache(&blkcache, NULL) != 0) { |
| 356 | endmntent(mnt_fp); |
| 357 | return (PROBE_EUNKNOWN); |
| 358 | } |
| 359 | #endif |
| 360 | if (mnt_op == OVAL_OPERATION_PATTERN_MATCH) { |
| 361 | re = oscap_pcre_compile(mnt_path, OSCAP_PCRE_OPTS_UTF8, &estr, &eoff); |
| 362 | |
| 363 | if (re == NULL) { |
| 364 | endmntent(mnt_fp); |
| 365 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 366 | blkid_put_cache(blkcache); |
| 367 | #endif |
| 368 | oscap_pcre_err_free(estr); |
| 369 | return (PROBE_EINVAL); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | while ((mnt_entp = getmntent_r(mnt_fp, &mnt_ent, |
| 374 | buffer, sizeof buffer)) != NULL) |
| 375 | { |
| 376 | if (strcmp(mnt_entp->mnt_type, "rootfs") == 0) |
| 377 | continue; |
| 378 | |
| 379 | if (mnt_op == OVAL_OPERATION_EQUALS) { |
| 380 | if (strcmp(mnt_entp->mnt_dir, mnt_path) == 0) { |
| 381 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 382 | collect_item(ctx, obj_over, mnt_entp, blkcache); |
| 383 | #else |
| 384 | collect_item(ctx, obj_over, mnt_entp); |
| 385 | #endif |
| 386 | break; |
| 387 | } |
| 388 | } else if (mnt_op == OVAL_OPERATION_NOT_EQUAL) { |
| 389 | if (strcmp(mnt_entp->mnt_dir, mnt_path) != 0) { |
| 390 | if ( |
| 391 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 392 | collect_item(ctx, obj_over, mnt_entp, blkcache) |
| 393 | #else |
| 394 | collect_item(ctx, obj_over, mnt_entp) |
| 395 | #endif |
| 396 | != 0) |
| 397 | break; |
| 398 | } |
| 399 | } else if (mnt_op == OVAL_OPERATION_PATTERN_MATCH) { |
| 400 | int rc; |
| 401 | |
| 402 | rc = oscap_pcre_exec(re, mnt_entp->mnt_dir, |
| 403 | strlen(mnt_entp->mnt_dir), 0, 0, NULL, 0); |
| 404 | |
| 405 | if (rc > OSCAP_PCRE_ERR_NOMATCH) { |
| 406 | if ( |
| 407 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 408 | collect_item(ctx, obj_over, mnt_entp, blkcache) |
| 409 | #else |
| 410 | collect_item(ctx, obj_over, mnt_entp) |
| 411 | #endif |
| 412 | != 0) |
| 413 | break; |
| 414 | } |
| 415 | /* XXX: check for pcre_exec error */ |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | endmntent(mnt_fp); |
| 420 | |
| 421 | if (mnt_op == OVAL_OPERATION_PATTERN_MATCH) |
| 422 | oscap_pcre_free(re); |
| 423 | #if defined(HAVE_BLKID_GET_TAG_VALUE) |
| 424 | blkid_put_cache(blkcache); |
| 425 | #endif |
| 426 | } |
| 427 | |
| 428 | return (probe_ret); |
| 429 | } |