| 1 | /* |
| 2 | * Copyright 2016 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | #include "probe-chroot.h" |
| 26 | #include <errno.h> |
| 27 | #include "common/debug_priv.h" |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | |
| 35 | void probe_chroot_init(struct probe_chroot *ch, const char *scan_path) { |
| 36 | assert(ch != NULL); |
| 37 | |
| 38 | if (scan_path == NULL) { |
| 39 | ch->scan_path = NULL; |
| 40 | ch->root_fd = -1; |
| 41 | } else { |
| 42 | // Copy path |
| 43 | ch->scan_path = oscap_strdup(scan_path); |
| 44 | |
| 45 | // Open descriptor to original root |
| 46 | // It will be used to unchroot |
| 47 | ch->root_fd = open("/", O_RDONLY); |
| 48 | if (ch->root_fd < 0) { |
| 49 | dE("Unable to open root directory: errno: %d \"%s\"", errno, strerror(errno)); |
| 50 | abort(); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | int probe_chroot_enter(const struct probe_chroot *ch) |
| 57 | { |
| 58 | assert(ch != NULL); |
| 59 | |
| 60 | // chdir() is not really required for our purposes of chroot() |
| 61 | // but it could help us with debugging of unsuccessfull chroot |
| 62 | if (chdir(ch->scan_path) < 0 ) { |
| 63 | dE("Unable to chdir to '%s', errno: %d \"%s\"", ch->scan_path, errno, strerror(errno)); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | if (chroot(ch->scan_path) < 0) { |
| 68 | dE("Unable to chroot to '%s', errno: %d \"%s\"", ch->scan_path, errno, strerror(errno)); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | dD("Probe has just entered to new root: '%s'", ch->scan_path); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | bool probe_chroot_is_set(const struct probe_chroot *ch) |
| 77 | { |
| 78 | return (ch->scan_path != NULL); |
| 79 | } |
| 80 | |
| 81 | const char *probe_chroot_get_path(const struct probe_chroot *ch) |
| 82 | { |
| 83 | return ch->scan_path; |
| 84 | } |
| 85 | |
| 86 | int probe_chroot_leave(const struct probe_chroot *ch) |
| 87 | { |
| 88 | assert(ch != NULL); |
| 89 | |
| 90 | if (fchdir(ch->root_fd)) { |
| 91 | dE("Unable to chdir to host root, errno: %d \"%s\"", errno, strerror(errno)); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if (chroot(".") < 0) { |
| 96 | dE("Unable to leave chroot environment, errno: %d \"%s\"", errno, strerror(errno)); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | dD("Probe has just escaped chroot environment (previous root: '%s')", ch->scan_path); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void probe_chroot_free(struct probe_chroot* ch) |
| 106 | { |
| 107 | assert(ch != NULL); |
| 108 | |
| 109 | if (ch->scan_path == NULL) { |
| 110 | return; |
| 111 | } |
| 112 | close(ch->root_fd); |
| 113 | ch->root_fd = -1; |
| 114 | |
| 115 | free(ch->scan_path); |
| 116 | ch->scan_path = NULL; |
| 117 | } |