Debian Codegraph @

src/OVAL/probes/independent/environmentvariable58_probe.c

raw symbols SCIP
1 /**
2 * @file environmentvariable58_probe.c
3 * @brief environmentvariable58 probe
4 * @author "Petr Lautrbach" <[email protected]>
5 *
6 * This probe is able to process a environmentvariable58_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 * environmentvariable58 probe:
34 *
35 * pid
36 * name
37 * value
38 */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <limits.h>
47 #include <string.h>
48 #include <stdio.h>
49 #include <errno.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <sys/types.h>
53 #include <dirent.h>
54
55 #if defined(OS_FREEBSD)
56 #include <limits.h>
57 #include <sys/user.h>
58 #include <kvm.h>
59 #include <sys/param.h>
60 #include <paths.h>
61 #include <libprocstat.h>
62 #include <sys/sysctl.h>
63 #endif
64
65 #include <probe/probe.h>
66 #include "_seap.h"
67 #include "probe-api.h"
68 #include "probe/entcmp.h"
69 #include "common/debug_priv.h"
70 #include "environmentvariable58_probe.h"
71
72 #if defined(OS_FREEBSD)
73 static int read_environment(SEXP_t *pid_ent, SEXP_t *name_ent, probe_ctx *ctx)
74 {
75 struct kinfo_proc *proclist, *proc;
76 int i, j, count, pid;
77 char *name, *value;
78 const char *SEP = "=";
79 struct procstat *stat = NULL;
80 char buf[LINE_MAX];
81 SEXP_t *pid_sexp;
82 char **env = NULL;
83 kvm_t *kd;
84
85 const char *extra_vars = getenv("OSCAP_CONTAINER_VARS");
86 if (extra_vars && *extra_vars) {
87 char *vars = strdup(extra_vars);
88 char *tok, *eq_chr, *str, *strp;
89
90 for (str = vars; ; str = NULL) {
91 tok = strtok_r(str, "\n", &strp);
92 if (tok == NULL)
93 break;
94 eq_chr = strchr(tok, '=');
95 if (eq_chr == NULL)
96 continue;
97 PROBE_ENT_I32VAL(pid_ent, pid, pid = -1;, pid = 0;);
98
99 size_t extra_env_name_size = eq_chr - tok;
100 SEXP_t *extra_env_name = SEXP_string_new(tok, extra_env_name_size);
101 SEXP_t *extra_env_value = SEXP_string_newf("%s", tok + extra_env_name_size + 1);
102
103 if (probe_entobj_cmp(name_ent, extra_env_name) == OVAL_RESULT_TRUE) {
104 SEXP_t *extra_item = probe_item_create(
105 OVAL_INDEPENDENT_ENVIRONMENT_VARIABLE58, NULL,
106 "pid", OVAL_DATATYPE_INTEGER, (int64_t)pid,
107 "name", OVAL_DATATYPE_SEXP, extra_env_name,
108 "value", OVAL_DATATYPE_SEXP, extra_env_value,
109 NULL);
110
111 if (!extra_item) {
112 dE("Failed to create new probem item");
113 SEXP_free(extra_env_name);
114 SEXP_free(extra_env_value);
115 free(vars);
116 return (PROBE_EFAULT);
117 }
118
119 probe_item_collect(ctx, extra_item);
120 } else {
121 SEXP_free(extra_env_name);
122 SEXP_free(extra_env_value);
123 }
124 }
125
126 free(vars);
127 return 0;
128 }
129
130 kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, buf);
131
132 if (!kd)
133 return (PROBE_EFAULT);
134
135 proclist = kvm_getprocs(kd, KERN_PROC_PROC, 0, &count);
136
137 if (!proclist) {
138 dE("Failed to obtain process list handle");
139 kvm_close(kd);
140 return (PROBE_EFAULT);
141 }
142
143 proc = proclist;
144
145 /* Loop until we find a matching PID */
146 for (i = 0; i < count; i++, proc++) {
147 pid = proc->ki_pid;
148 pid_sexp = SEXP_number_newi_32(pid);
149
150 if (!pid_sexp) {
151 dE("Failed to allocate pid_sexp");
152 kvm_close(kd);
153 return (PROBE_EFATAL);
154 }
155
156 /* PID doesn't match */
157 if (probe_entobj_cmp(pid_ent, pid_sexp) != OVAL_RESULT_TRUE) {
158 SEXP_free(pid_sexp);
159 continue;
160 }
161
162 /* PID matches */
163 stat = procstat_open_sysctl();
164
165 if (!stat) {
166 dE("NULL procstat handle returned");
167 SEXP_free(pid_sexp);
168 kvm_close(kd);
169 return (PROBE_EFAULT);
170 }
171
172 env = procstat_getenvv(stat, proc, ARG_MAX);
173
174 /* Found matching process, so stop searching */
175 SEXP_free(pid_sexp);
176 break;
177 }
178
179 if (!env) {
180 dE("NULL env returned");
181 procstat_freeenvv(stat);
182 kvm_close(kd);
183 return (PROBE_EFATAL);
184 }
185
186 for (j = 0; env[j] != NULL; j++) {
187 char *strp;
188 name = strtok_r(env[j], SEP, &strp);
189
190 if (!name) {
191 dE("Error parsing environment string");
192 procstat_freeenvv(stat);
193 kvm_close(kd);
194 return (PROBE_EFAULT);
195 }
196
197 value = strtok_r(NULL, SEP, &strp);
198
199 if (value != NULL) {
200 SEXP_t *env_name = SEXP_string_new(name, strlen(name) + 1);
201 SEXP_t *env_value = SEXP_string_new(value, strlen(value) + 1);
202
203 if (probe_entobj_cmp(name_ent, env_name) == OVAL_RESULT_TRUE) {
204
205 SEXP_t *item = probe_item_create(
206 OVAL_INDEPENDENT_ENVIRONMENT_VARIABLE58, NULL,
207 "pid", OVAL_DATATYPE_INTEGER, (int64_t)pid,
208 "name", OVAL_DATATYPE_SEXP, env_name,
209 "value", OVAL_DATATYPE_SEXP, env_value,
210 NULL);
211
212 if (!item) {
213 dE("Failed to create new probe item");
214 SEXP_free(env_name);
215 SEXP_free(env_value);
216 procstat_freeenvv(stat);
217 kvm_close(kd);
218 return (PROBE_EFAULT);
219 }
220
221 probe_item_collect(ctx, item);
222 } else {
223 SEXP_free(env_name);
224 SEXP_free(env_value);
225 }
226 }
227 }
228
229 procstat_freeenvv(stat);
230 kvm_close(kd);
231 return 0;
232 }
233
234 #else
235 #define BUFFER_SIZE 256
236
237 extern char **environ;
238
239 static int collect_variable(char *buffer, size_t env_name_size, int pid, SEXP_t *name_ent, probe_ctx *ctx)
240 {
241 int res = 1;
242
243 SEXP_t *env_name = SEXP_string_new(buffer, env_name_size);
244 SEXP_t *env_value = SEXP_string_newf("%s", buffer + env_name_size + 1);
245
246 if (probe_entobj_cmp(name_ent, env_name) == OVAL_RESULT_TRUE) {
247 SEXP_t *item = probe_item_create(
248 OVAL_INDEPENDENT_ENVIRONMENT_VARIABLE58, NULL,
249 "pid", OVAL_DATATYPE_INTEGER, (int64_t)pid,
250 "name", OVAL_DATATYPE_SEXP, env_name,
251 "value", OVAL_DATATYPE_SEXP, env_value,
252 NULL);
253 probe_item_collect(ctx, item);
254 res = 0;
255 }
256 SEXP_free(env_name);
257 SEXP_free(env_value);
258
259 return res;
260 }
261
262 static int read_environment(SEXP_t *pid_ent, SEXP_t *name_ent, probe_ctx *ctx)
263 {
264 int err = 1, pid, fd;
265 bool empty;
266 SEXP_t *item, *pid_sexp;
267 DIR *d;
268 struct dirent *d_entry;
269 char *buffer, *null_char, path[PATH_MAX] = {0};
270 ssize_t buffer_used;
271 size_t buffer_size;
272
273 const char *extra_vars = getenv("OSCAP_CONTAINER_VARS");
274 if (extra_vars && *extra_vars) {
275 char *vars = strdup(extra_vars);
276 char *tok, *eq_chr, *str, *strp;
277
278 for (str = vars; ; str = NULL) {
279 tok = strtok_r(str, "\n", &strp);
280 if (tok == NULL)
281 break;
282 eq_chr = strchr(tok, '=');
283 if (eq_chr == NULL)
284 continue;
285 PROBE_ENT_I32VAL(pid_ent, pid, pid = -1;, pid = 0;);
286 collect_variable(tok, eq_chr - tok, pid, name_ent, ctx);
287 }
288
289 free(vars);
290 return 0;
291 }
292
293 const char *prefix = getenv("OSCAP_PROBE_ROOT");
294 snprintf(path, PATH_MAX, "%s/proc", prefix ? prefix : "");
295 d = opendir(path);
296 if (d == NULL) {
297 dE("Can't read %s/proc: errno=%d, %s.", prefix ? prefix : "", errno, strerror(errno));
298 return PROBE_EACCESS;
299 }
300
301 if ((buffer = realloc(NULL, BUFFER_SIZE)) == NULL) {
302 dE("Can't re-allocate memory");
303 closedir(d);
304 return PROBE_EFAULT;
305 }
306 buffer_size = BUFFER_SIZE;
307
308 while ((d_entry = readdir(d))) {
309 if (strspn(d_entry->d_name, "0123456789") != strlen(d_entry->d_name))
310 continue;
311
312 pid = atoi(d_entry->d_name);
313 pid_sexp = SEXP_number_newi_32(pid);
314
315 if (probe_entobj_cmp(pid_ent, pid_sexp) != OVAL_RESULT_TRUE) {
316 SEXP_free(pid_sexp);
317 continue;
318 }
319 SEXP_free(pid_sexp);
320
321 snprintf(path, PATH_MAX, "%s/proc/%d/environ", prefix ? prefix : "", pid);
322 if ((fd = open(path, O_RDONLY)) == -1) {
323 dE("Can't open \"%s\": errno=%d, %s.", path, errno, strerror (errno));
324 item = probe_item_create(
325 OVAL_INDEPENDENT_ENVIRONMENT_VARIABLE58, NULL,
326 "pid", OVAL_DATATYPE_INTEGER, (int64_t)pid,
327 NULL
328 );
329
330 probe_item_setstatus(item, SYSCHAR_STATUS_ERROR);
331 probe_item_add_msg(item, OVAL_MESSAGE_LEVEL_ERROR,
332 "Can't open \"%s\": errno=%d, %s.", path, errno, strerror (errno));
333 probe_item_collect(ctx, item);
334 continue;
335 }
336
337 empty = true;
338
339 if ((buffer_used = read(fd, buffer, buffer_size - 1)) > 0) {
340 empty = false;
341 }
342
343 while (! empty) {
344 while (! (null_char = memchr(buffer, 0, buffer_used))) {
345 ssize_t s;
346 if ((size_t)buffer_used >= buffer_size) {
347 buffer_size += BUFFER_SIZE;
348 void *new_buffer = realloc(buffer, buffer_size);
349 if (new_buffer == NULL) {
350 dE("Can't re-allocate memory");
351 free(buffer);
352 closedir(d);
353 close(fd);
354 return PROBE_ENOMEM;
355 }
356 buffer = new_buffer;
357 }
358 s = read(fd, buffer + buffer_used, buffer_size - buffer_used);
359 if (s <= 0) {
360 empty = true;
361 buffer[buffer_used++] = 0;
362 }
363 else {
364 buffer_used += s;
365 }
366 }
367
368 do {
369 char *eq_char = strchr(buffer, '=');
370 if (eq_char == NULL) {
371 /* strange but possible:
372 * $ strings /proc/1218/environ
373 /dev/input/event0 /dev/input/event1 /dev/input/event4 /dev/input/event3
374 */
375 buffer_used -= null_char + 1 - buffer;
376 memmove(buffer, null_char + 1, buffer_used);
377 continue;
378 }
379
380 collect_variable(buffer, eq_char - buffer, pid, name_ent, ctx);
381
382 buffer_used -= null_char + 1 - buffer;
383 memmove(buffer, null_char + 1, buffer_used);
384 } while ((null_char = memchr(buffer, 0, buffer_used)));
385 }
386
387 close(fd);
388 }
389 closedir(d);
390 free(buffer);
391 if (err) {
392 SEXP_t *msg = probe_msg_creatf(OVAL_MESSAGE_LEVEL_ERROR,
393 "Can't find process with requested PID.");
394 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
395 SEXP_free(msg);
396 err = 0;
397 }
398
399 return err;
400 }
401 #endif
402
403 int environmentvariable58_probe_offline_mode_supported(void)
404 {
405 return PROBE_OFFLINE_OWN;
406 }
407
408 int environmentvariable58_probe_main(probe_ctx *ctx, void *arg)
409 {
410 SEXP_t *probe_in, *name_ent, *pid_ent;
411 int pid, err;
412
413 probe_in = probe_ctx_getobject(ctx);
414 name_ent = probe_obj_getent(probe_in, "name", 1);
415
416 if (name_ent == NULL) {
417 return PROBE_ENOENT;
418 }
419
420 pid_ent = probe_obj_getent(probe_in, "pid", 1);
421 if (pid_ent == NULL) {
422 SEXP_free(name_ent);
423 return PROBE_ENOENT;
424 }
425
426 PROBE_ENT_I32VAL(pid_ent, pid, pid = -1;, pid = 0;);
427
428 if (pid == -1) {
429 SEXP_free(name_ent);
430 SEXP_free(pid_ent);
431 return PROBE_ERANGE;
432 }
433
434 if (pid == 0) {
435 /* overwrite pid value with actual pid */
436 SEXP_t *nref, *nval, *new_pid_ent;
437
438 nref = SEXP_list_first(probe_in);
439 nval = SEXP_number_newu_32(getpid());
440 new_pid_ent = SEXP_list_new(nref, nval, NULL);
441 SEXP_free(pid_ent);
442 SEXP_free(nref);
443 SEXP_free(nval);
444 pid_ent = new_pid_ent;
445 }
446
447 err = read_environment(pid_ent, name_ent, ctx);
448
449 SEXP_free(name_ent);
450 SEXP_free(pid_ent);
451
452 return err;
453 }