Debian Codegraph @

src/OVAL/probes/unix/sysctl_probe.c

raw symbols SCIP
1 /**
2 * @file sysctl_probe.c
3 * @brief implementation of the sysctl_object
4 * @author "Daniel Kopecek" <[email protected]>
5 */
6
7 /*
8 * Copyright 2011 Red Hat Inc., Durham, North Carolina.
9 * All Rights Reserved.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Authors:
26 * "Daniel Kopecek" <[email protected]>
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <probe/probe.h>
34 #include "probe-api.h"
35 #include "probe/entcmp.h"
36 #include "sysctl_probe.h"
37
38 #if defined(OS_FREEBSD)
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <limits.h>
42 #include <string.h>
43
44 #include "common/debug_priv.h"
45
46 #define SYSCTL_CMD "/sbin/sysctl -ae"
47 #endif
48
49 #if defined(OS_LINUX)
50
51 #include <stdio.h>
52 #include <string.h>
53 #include <ctype.h>
54 #include <errno.h>
55 #include <limits.h>
56 #include <dirent.h>
57 #include "oval_fts.h"
58 #include "common/debug_priv.h"
59
60 #define PROC_SYS_DIR "/proc/sys"
61 #define PROC_SYS_MAXDEPTH 7
62
63 int sysctl_probe_offline_mode_supported(void)
64 {
65 return PROBE_OFFLINE_OWN;
66 }
67
68 int sysctl_probe_main(probe_ctx *ctx, void *probe_arg)
69 {
70 OVAL_FTS *ofts;
71 OVAL_FTSENT *ofts_ent;
72
73 SEXP_t *name_entity, *probe_in;
74 SEXP_t *r0, *r1, *r2, *r3;
75 SEXP_t *ent_attrs, *bh_entity, *path_entity, *filename_entity;
76 oval_schema_version_t over;
77 int over_cmp;
78
79 const char *ipv6_conf_path = "/proc/sys/net/ipv6/conf/";
80 size_t ipv6_conf_path_len = strlen(ipv6_conf_path);
81
82 probe_in = probe_ctx_getobject(ctx);
83 name_entity = probe_obj_getent(probe_in, "name", 1);
84 over = probe_obj_get_platform_schema_version(probe_in);
85 over_cmp = oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.10));
86
87 if (name_entity == NULL) {
88 dE("Missing \"name\" entity in the input object");
89 return (PROBE_ENOENT);
90 }
91
92 /*
93 * prepare behaviors
94 */
95 ent_attrs = probe_attr_creat("max_depth", r0 = SEXP_string_newf("%d", PROC_SYS_MAXDEPTH),
96 "recurse_direction", r1 = SEXP_string_new("down", 4),
97 "recurse_file_system", r2 = SEXP_string_new("all", 3),
98 "recurse", r3 = SEXP_string_new("symlinks and directories", 24),
99 NULL);
100 bh_entity = probe_ent_creat1("behaviors", ent_attrs, NULL);
101 SEXP_free(r0);
102 SEXP_free(r1);
103 SEXP_free(r2);
104 SEXP_free(r3);
105 SEXP_free(ent_attrs);
106
107 /*
108 * prepare path, filename
109 */
110 ent_attrs = probe_attr_creat("operation", r0 = SEXP_number_newi(OVAL_OPERATION_EQUALS),
111 NULL);
112 path_entity = probe_ent_creat1("path", ent_attrs, r1 = SEXP_string_new(PROC_SYS_DIR, strlen(PROC_SYS_DIR)));
113 SEXP_free(r0);
114 SEXP_free(r1);
115 SEXP_free(ent_attrs);
116
117 ent_attrs = probe_attr_creat("operation", r0 = SEXP_number_newi(OVAL_OPERATION_PATTERN_MATCH),
118 NULL);
119 filename_entity = probe_ent_creat1("filename", ent_attrs, r1 = SEXP_string_new(".*", 2));
120 SEXP_free(r0);
121 SEXP_free(r1);
122 SEXP_free(ent_attrs);
123
124 /*
125 * collect sysctls
126 * XXX: use direct access for the "equals" op
127 */
128 const char *prefix = getenv("OSCAP_PROBE_ROOT");
129 ofts = oval_fts_open_prefixed(prefix, path_entity, filename_entity, NULL, bh_entity, probe_ctx_getresult(ctx));
130
131 if (ofts == NULL) {
132 SEXP_free(path_entity);
133 SEXP_free(filename_entity);
134 SEXP_free(bh_entity);
135 SEXP_free(name_entity);
136
137 if (prefix != NULL) {
138 DIR *d = opendir(prefix);
139 if (d != NULL) {
140 closedir(d);
141 return PROBE_ESUCCESS;
142 }
143 dW("Can't open prefix directory '%s': %s", prefix, strerror(errno));
144 }
145
146 dE("oval_fts_open_prefixed(%s, %s, %s, ...) failed", prefix, PROC_SYS_DIR, ".*");
147 return PROBE_EFATAL;
148 }
149
150 while ((ofts_ent = oval_fts_read(ofts)) != NULL) {
151 SEXP_t *se_mib;
152 char mibpath[PATH_MAX], *mib;
153 size_t miblen, mibstart;
154 struct stat file_stat;
155
156 if (prefix != NULL) {
157 snprintf(mibpath, sizeof mibpath, "%s/%s/%s", prefix, ofts_ent->path, ofts_ent->file);
158 } else {
159 snprintf(mibpath, sizeof mibpath, "%s/%s", ofts_ent->path, ofts_ent->file);
160 }
161
162 /* Skip write-only files, eg. /proc/sys/net/ipv4/route/flush */
163 if (stat(mibpath, &file_stat) == -1) {
164 dE("Stat failed on %s: %u, %s", mibpath, errno, strerror(errno));
165 oval_ftsent_free(ofts_ent);
166 continue;
167 }
168 /* the sysctl utility uses same condition in sysctl.c in ReadSetting() */
169 if ((file_stat.st_mode & S_IRUSR) == 0) {
170 dD("Skipping write-only file %s", mibpath);
171 oval_ftsent_free(ofts_ent);
172 continue;
173 }
174
175 mibstart = 0;
176 mibstart += prefix != NULL ? strlen(prefix)+1 : 0;
177 mibstart += strlen(PROC_SYS_DIR)+1;
178 mib = strdup(mibpath + mibstart);
179 miblen = strlen(mib);
180
181 while (miblen > 0) {
182 if(mib[miblen - 1] == '/')
183 mib[miblen - 1] = '.';
184 --miblen;
185 }
186
187 dD("MIB: %s", mib);
188 se_mib = SEXP_string_new(mib, strlen(mib));
189 free(mib);
190
191 if (probe_entobj_cmp(name_entity, se_mib) == OVAL_RESULT_TRUE) {
192 FILE *fp;
193 SEXP_t *item;
194 char sysval[8192];
195 char *sysvals[512];
196 long i, l;
197 size_t s;
198
199 dD("MIB match");
200
201 /*
202 * read sysctl value
203 */
204 fp = fopen(mibpath, "r");
205
206 if (fp == NULL) {
207 dE("Can't read sysctl value from \"%s\": %u, %s",
208 mibpath, errno, strerror(errno));
209 goto fail_item;
210 }
211
212 l = fread(sysval, 1, sizeof sysval - 1, fp);
213
214 if (ferror(fp)) {
215 /* Linux 4.1.0 introduced a per-NIC IPv6 stable_secret file.
216 * The stable_secret file cannot be read until it is set,
217 * so we skip it when it is not readable. Otherwise we collect it.
218 */
219 if (strncmp(ofts_ent->path, ipv6_conf_path, ipv6_conf_path_len) == 0 &&
220 strcmp(ofts_ent->file, "stable_secret") == 0) {
221 dD("Skipping file %s", mibpath);
222 oval_ftsent_free(ofts_ent);
223 SEXP_free(se_mib);
224 fclose(fp);
225 continue;
226 } else {
227 dE("An error occurred when reading from \"%s\" (fp=%p): l=%ld, %u, %s",
228 mibpath, fp, l, errno, strerror(errno));
229 goto fail_item;
230 }
231 }
232
233 fclose(fp);
234
235 /* Skip empty values as sysctl tool does.
236 * See https://bugzilla.redhat.com/show_bug.cgi?id=1473207
237 */
238 if (l == 0) {
239 dD("Skipping file '%s' because it has no value.", mibpath);
240 oval_ftsent_free(ofts_ent);
241 SEXP_free(se_mib);
242 continue;
243 }
244
245 /*
246 * sanitize the value
247 * - only printable and whitespace chars allowed
248 * - remove the last '\n'
249 */
250 sysvals[0] = sysval;
251
252 for(s = 0, i = 0; i < l && s < sizeof sysvals/sizeof(char *) - 1; ++i) {
253 if ((!isprint(sysval[i]) && !isspace(sysval[i]))
254 || (over_cmp >= 0 && sysval[i] == '\n' /* OVAL 5.10 and above */))
255 {
256 sysval[i] = '\0';
257 sysvals[++s] = sysval + i + 1;
258 }
259 }
260
261 if (sysval[l - 1] == '\n')
262 sysval[l - 1] = '\0';
263 else
264 sysval[l] = '\0';
265
266 if (strlen(sysvals[s]) == 0)
267 sysvals[s] = NULL;
268 else
269 sysvals[++s] = NULL;
270
271 if (over_cmp >= 0) {
272 /* Only in OVAL 5.10 and above */
273 item = probe_item_create(OVAL_UNIX_SYSCTL, NULL,
274 "name", OVAL_DATATYPE_SEXP, se_mib,
275 "value", OVAL_DATATYPE_STRING_M, sysvals,
276 NULL);
277 } else {
278 item = probe_item_create(OVAL_UNIX_SYSCTL, NULL,
279 "name", OVAL_DATATYPE_SEXP, se_mib,
280 "value", OVAL_DATATYPE_STRING, sysval,
281 NULL);
282 }
283
284 goto add_item;
285 fail_item:
286 if (fp != NULL)
287 fclose(fp);
288
289 item = probe_item_create(OVAL_UNIX_SYSCTL, NULL, NULL);
290 probe_item_setstatus(item, SYSCHAR_STATUS_ERROR);
291 add_item:
292 probe_item_collect(ctx, item);
293 }
294
295 oval_ftsent_free(ofts_ent);
296 SEXP_free(se_mib);
297 }
298
299 oval_fts_close(ofts);
300 SEXP_free(path_entity);
301 SEXP_free(filename_entity);
302 SEXP_free(bh_entity);
303 SEXP_free(name_entity);
304
305 return (0);
306 }
307
308 #elif defined(OS_FREEBSD)
309
310 int sysctl_probe_offline_mode_supported(void)
311 {
312 return PROBE_OFFLINE_NONE;
313 }
314
315 int sysctl_probe_main(probe_ctx *ctx, void *probe_arg)
316 {
317 FILE *fp;
318 char output[LINE_MAX];
319 const char* SEP = "=";
320 char* mib;
321 char* sysval;
322 SEXP_t *se_mib;
323 SEXP_t *name_entity, *probe_in;
324
325 probe_in = probe_ctx_getobject(ctx);
326 name_entity = probe_obj_getent(probe_in, "name", 1);
327
328 if (name_entity == NULL) {
329 dE("Missing \"name\" entity in the input object");
330 return (PROBE_ENOENT);
331 }
332
333 /* FreeBSD's sysctl(8) uses undocumented, and potentially unstable,
334 * kernel interfaces to obtain the list of system properties and values.
335 * Hence we call the executable and parse its output rather than
336 * implement the functionality ourselves which risks breakage if/when
337 * the interfaces change.
338 */
339 fp = popen(SYSCTL_CMD, "r");
340
341 if (!fp) {
342 dE("Failed to open output of %s", SYSCTL_CMD);
343 return (PROBE_EFATAL);
344 }
345
346 while (fgets(output, sizeof(output), fp)) {
347 char *strp;
348 mib = strtok_r(output, SEP, &strp);
349 sysval = strtok_r(NULL, SEP, &strp);
350
351 if (!mib)
352 continue;
353
354 if (!sysval)
355 continue;
356
357 se_mib = SEXP_string_new(mib, strlen(mib));
358
359 if (!se_mib) {
360 dE("Failed to allocate new SEXP_string for se_mib");
361 pclose(fp);
362 return (PROBE_ENOENT);
363 }
364
365 /* Remove newline */
366 sysval[strlen(sysval)-1] = '\0';
367
368 if (probe_entobj_cmp(name_entity, se_mib) == OVAL_RESULT_TRUE) {
369 SEXP_t *item;
370
371 item = probe_item_create(OVAL_UNIX_SYSCTL, NULL,
372 "name", OVAL_DATATYPE_SEXP, se_mib,
373 "value", OVAL_DATATYPE_STRING, sysval,
374 NULL);
375
376 if (!item) {
377 dE("probe_item_create() returned a null item");
378 pclose(fp);
379 SEXP_free(se_mib);
380 return (PROBE_ENOENT);
381 }
382
383 probe_item_collect(ctx, item);
384 }
385
386 SEXP_free(se_mib);
387 }
388
389 pclose(fp);
390 return (0);
391 }
392
393 #else
394
395 int sysctl_probe_offline_mode_supported(void)
396 {
397 return PROBE_OFFLINE_NONE;
398 }
399
400 int sysctl_probe_main(probe_ctx *ctx, void *probe_arg)
401 {
402 return(PROBE_EOPNOTSUPP);
403 }
404
405 #endif