Debian Codegraph @

src/OVAL/probes/unix/linux/probe-chroot.c

raw symbols SCIP
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_Hcxx . . $ `<built-in>:409:9`!
22 #include <config.h>cxx . . $ `<file>/build/config.h`/
23 #endif
24
25 #include "probe-chroot.h"cxx . . $ `<file>/src/OVAL/probes/unix/linux/probe-chroot.h`/
26 #include <errno.h>
27 #include "common/debug_priv.h"cxx . . $ `<file>/src/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_initcxx . . $ probe_chroot_init(a29411824056a).(struct probe_chrootcxx . . $ probe_chroot# *ch, const char *scan_path) {
36 assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(ch != NULL);
37
38 if (scan_path == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
39 ch->scan_pathcxx . . $ probe_chroot#scan_path. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
40 ch->root_fdcxx . . $ probe_chroot#root_fd. = -1;
41 } else {
42 // Copy path
43 ch->scan_pathcxx . . $ probe_chroot#scan_path. = oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(scan_path);
44
45 // Open descriptor to original root
46 // It will be used to unchroot
47 ch->root_fdcxx . . $ probe_chroot#root_fd. = opencxx . . $ open(e156e3aa1e5c4ad1).("/", O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
48 if (ch->root_fdcxx . . $ probe_chroot#root_fd. < 0) {
49 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Unable to open root directory: errno: %d \"%s\"", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
50 abortcxx . . $ abort(8df32a62017fbbf6).();
51 }
52 }
53 }
54
55
56 int probe_chroot_entercxx . . $ probe_chroot_enter(abaf292149bf3691).(const struct probe_chrootcxx . . $ probe_chroot# *ch)
57 {
58 assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(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 (chdircxx . . $ chdir(5c4306d8553b3696).(ch->scan_pathcxx . . $ probe_chroot#scan_path.) < 0 ) {
63 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Unable to chdir to '%s', errno: %d \"%s\"", ch->scan_path, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
64 return -1;
65 }
66
67 if (chrootcxx . . $ chroot(5c4306d8553b3696).(ch->scan_pathcxx . . $ probe_chroot#scan_path.) < 0) {
68 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Unable to chroot to '%s', errno: %d \"%s\"", ch->scan_path, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
69 return -1;
70 }
71
72 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Probe has just entered to new root: '%s'", ch->scan_path);
73 return 0;
74 }
75
76 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! probe_chroot_is_setcxx . . $ probe_chroot_is_set(ff3a78133c2a64ce).(const struct probe_chrootcxx . . $ probe_chroot# *ch)
77 {
78 return (ch->scan_pathcxx . . $ probe_chroot#scan_path. != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!);
79 }
80
81 const char *probe_chroot_get_pathcxx . . $ probe_chroot_get_path(3f6d22cdb48a4015).(const struct probe_chrootcxx . . $ probe_chroot# *ch)
82 {
83 return ch->scan_pathcxx . . $ probe_chroot#scan_path.;
84 }
85
86 int probe_chroot_leavecxx . . $ probe_chroot_leave(abaf292149bf3691).(const struct probe_chrootcxx . . $ probe_chroot# *ch)
87 {
88 assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(ch != NULL);
89
90 if (fchdircxx . . $ fchdir(7864480464b09eea).(ch->root_fdcxx . . $ probe_chroot#root_fd.)) {
91 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Unable to chdir to host root, errno: %d \"%s\"", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
92 return -1;
93 }
94
95 if (chrootcxx . . $ chroot(5c4306d8553b3696).(".") < 0) {
96 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Unable to leave chroot environment, errno: %d \"%s\"", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
97 return -1;
98 }
99
100 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Probe has just escaped chroot environment (previous root: '%s')", ch->scan_path);
101 return 0;
102 }
103
104
105 void probe_chroot_freecxx . . $ probe_chroot_free(139f9897610b6464).(struct probe_chrootcxx . . $ probe_chroot#* ch)
106 {
107 assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(ch != NULL);
108
109 if (ch->scan_pathcxx . . $ probe_chroot#scan_path. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
110 return;
111 }
112 closecxx . . $ close(7864480464b09eea).(ch->root_fdcxx . . $ probe_chroot#root_fd.);
113 ch->root_fdcxx . . $ probe_chroot#root_fd. = -1;
114
115 freecxx . . $ free(5d22bdacc48458e8).(ch->scan_pathcxx . . $ probe_chroot#scan_path.);
116 ch->scan_pathcxx . . $ probe_chroot#scan_path. = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
117 }