Debian Codegraph @

src/OVAL/probes/unix/process_probe.c

raw symbols SCIP
1 /**
2 * @file process_probe.c
3 * @brief process probe
4 * @author "Steve Grubb" <[email protected]>
5 *
6 * 2010/06/13 [email protected]
7 * This probe is able to process an process_object as defined in OVAL 5.4 and 5.5.
8 *
9 */
10
11 /*
12 * Copyright 2009-2010 Red Hat Inc., Durham, North Carolina.
13 * All Rights Reserved.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * Authors:
30 * Steve Grubb <[email protected]>
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #ifdef HAVE_STDIO_EXT_H
42 # include <stdio_ext.h>
43 #endif
44 #include <dirent.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <sched.h>
48 #include <time.h>
49
50 #ifdef HAVE_PROC_DEVNAME_H
51 #include <proc/devname.h>
52 #endif
53
54 #include "_seap.h"
55 #include "probe-api.h"
56 #include "probe/entcmp.h"
57 #include "common/debug_priv.h"
58 #include "process_probe.h"
59 #include "oscap_helpers.h"
60
61 #if defined(OS_FREEBSD)
62 #include <kvm.h>
63 #include <sys/param.h>
64 #include <sys/sysctl.h>
65 #include <sys/user.h>
66 #include <limits.h>
67 #include <paths.h>
68
69 #define SEC_PER_YR 31557600
70 #define SEC_PER_DAY 86400
71 #define SEC_PER_HR 3600
72 #define SEC_PER_MIN 60
73 #endif
74
75 static oval_schema_version_t over;
76
77 /* Convenience structure for the results being reported */
78 struct result_info {
79 const char *command;
80 const char *exec_time;
81 unsigned pid;
82 unsigned ppid;
83 long priority;
84 int ruid;
85 const char *scheduling_class;
86 const char *start_time;
87 const char *tty;
88 int user_id;
89 };
90
91 static void report_finding(struct result_info *res, probe_ctx *ctx)
92 {
93 SEXP_t *item;
94 SEXP_t *se_ruid;
95
96 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.8)) < 0) {
97 se_ruid = NULL;
98 } else {
99 se_ruid = SEXP_number_newu_64(res->ruid);
100 }
101
102 item = probe_item_create(OVAL_UNIX_PROCESS, NULL,
103 "command", OVAL_DATATYPE_STRING, res->command,
104 "exec_time", OVAL_DATATYPE_STRING, res->exec_time,
105 "pid", OVAL_DATATYPE_INTEGER, (int64_t)res->pid,
106 "ppid", OVAL_DATATYPE_INTEGER, (int64_t)res->ppid,
107 "priority", OVAL_DATATYPE_INTEGER, (int64_t)res->priority,
108 "ruid", OVAL_DATATYPE_SEXP, se_ruid,
109 "scheduling_class", OVAL_DATATYPE_STRING, res->scheduling_class,
110 "start_time", OVAL_DATATYPE_STRING, res->start_time,
111 "tty", OVAL_DATATYPE_STRING, res->tty,
112 "user_id", OVAL_DATATYPE_INTEGER, (int64_t)res->user_id,
113 NULL);
114 if (se_ruid) {
115 SEXP_free(se_ruid);
116 }
117
118 probe_item_collect(ctx, item);
119 }
120
121 #if defined(OS_LINUX)
122
123 static unsigned long ticks, boot;
124
125 static void get_boot_time(void)
126 {
127 char buf[100];
128 FILE *sf;
129 int line;
130
131 boot = 0;
132 sf = fopen("/proc/stat", "rt");
133 if (sf == NULL)
134 return;
135
136 line = 0;
137 __fsetlocking(sf, FSETLOCKING_BYCALLER);
138 while (fgets(buf, sizeof(buf), sf)) {
139 if (line == 0) {
140 line++;
141 continue;
142 }
143 if (memcmp(buf, "btime", 5) == 0) {
144 sscanf(buf, "btime %lu", &boot);
145 break;
146 }
147 }
148 fclose(sf);
149 }
150
151 static int get_uids(int pid, struct result_info *r)
152 {
153 char buf[100];
154 FILE *sf;
155
156 r->ruid = -1;
157 r->user_id = -1;
158
159 snprintf(buf, sizeof(buf), "/proc/%d/status", pid);
160 sf = fopen(buf, "rt");
161 if (sf) {
162 int line = 0;
163 __fsetlocking(sf, FSETLOCKING_BYCALLER);
164 while (fgets(buf, sizeof(buf), sf)) {
165 if (line == 0) {
166 line++;
167 continue;
168 }
169 if (memcmp(buf, "Uid:", 4) == 0) {
170 sscanf(buf, "Uid: %d %d", &r->ruid, &r->user_id);
171 break;
172 }
173 }
174 fclose(sf);
175 }
176
177 return 0;
178 }
179
180 static char *convert_time(unsigned long long t, char *tbuf, int tb_size)
181 {
182 unsigned d,h,m,s;
183
184 s = t%60;
185 t /= 60;
186 m = t%60;
187 t /=60;
188 h = t%24;
189 t /= 24;
190 d = t;
191 if (d)
192 snprintf(tbuf, tb_size, "%u-%02u:%02u:%02u", d, h, m, s);
193 else
194 snprintf(tbuf, tb_size, "%02u:%02u:%02u", h, m, s);
195 return tbuf;
196 }
197
198 static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
199 {
200 int err = 1;
201 DIR *d;
202 struct dirent *ent;
203 struct tm result;
204
205 d = opendir("/proc");
206 if (d == NULL)
207 return err;
208
209 // Get the time tick hertz
210 ticks = (unsigned long)sysconf(_SC_CLK_TCK);
211 get_boot_time();
212
213 // Scan the directories
214 while (( ent = readdir(d) )) {
215 int fd, len;
216 char buf[256];
217 char *tmp, cmd[16], state, tty_dev[128];
218 int pid, ppid, pgrp, session, tty_nr, tpgid;
219 unsigned flags, sched_policy;
220 unsigned long minflt, cminflt, majflt, cmajflt, uutime, ustime;
221 long cutime, cstime, priority, cnice, nthreads, itrealvalue;
222 unsigned long long start;
223 SEXP_t *cmd_sexp;
224
225 // Skip non-process dir entries
226 if(*ent->d_name<'0' || *ent->d_name>'9')
227 continue;
228 errno = 0;
229 pid = strtol(ent->d_name, NULL, 10);
230 if (errno || pid == 2) // skip err & kthreads
231 continue;
232
233 // Parse up the stat file for the proc
234 snprintf(buf, 32, "/proc/%d/stat", pid);
235 fd = open(buf, O_RDONLY, 0);
236 if (fd < 0)
237 continue;
238 len = read(fd, buf, sizeof buf - 1);
239 close(fd);
240 if (len < 40)
241 continue;
242 buf[len] = 0;
243 tmp = strrchr(buf, ')');
244 if (tmp)
245 *tmp = 0;
246 else
247 continue;
248 memset(cmd, 0, sizeof(cmd));
249 sscanf(buf, "%d (%15c", &ppid, cmd);
250 sscanf(tmp+2, "%c %d %d %d %d %d "
251 "%u %lu %lu %lu %lu "
252 "%lu %lu %lu %ld %ld "
253 "%ld %ld %ld %llu",
254 &state, &ppid, &pgrp, &session, &tty_nr, &tpgid,
255 &flags, &minflt, &cminflt, &majflt, &cmajflt,
256 &uutime, &ustime, &cutime, &cstime, &priority,
257 &cnice, &nthreads, &itrealvalue, &start
258 );
259
260 // Skip kthreads
261 if (ppid == 2)
262 continue;
263
264 err = 0; // If we get this far, no permission problems
265 dI("Have command: %s", cmd);
266 cmd_sexp = SEXP_string_newf("%s", cmd);
267 if (probe_entobj_cmp(cmd_ent, cmd_sexp) == OVAL_RESULT_TRUE) {
268 struct result_info r;
269 unsigned long t = uutime/ticks + ustime/ticks;
270 char tbuf[32], sbuf[32];
271 int tday,tyear;
272 time_t s_time;
273 struct tm *proc, *now;
274 const char *fmt;
275
276 // Now get scheduler policy
277 sched_policy = sched_getscheduler(pid);
278 switch (sched_policy) {
279 case SCHED_OTHER:
280 r.scheduling_class = "TS";
281 break;
282 case SCHED_BATCH:
283 r.scheduling_class = "B";
284 break;
285 #ifdef SCHED_IDLE
286 case SCHED_IDLE:
287 r.scheduling_class = "#5";
288 break;
289 #endif
290 case SCHED_FIFO:
291 r.scheduling_class = "FF";
292 break;
293 case SCHED_RR:
294 r.scheduling_class = "RR";
295 break;
296 default:
297 r.scheduling_class = "?";
298 break;
299 }
300
301 // Calculate the start time
302 s_time = time(NULL);
303 now = localtime_r(&s_time, &result);
304 tyear = now->tm_year;
305 tday = now->tm_yday;
306 s_time = boot + (start / ticks);
307 proc = localtime_r(&s_time, &result);
308
309 // Select format based on how long we've been running
310 //
311 // FROM THE SPEC:
312 // "This is the time of day the process started formatted in HH:MM:SS if
313 // the same day the process started or formatted as MMM_DD (Ex.: Feb_5)
314 // if process started the previous day or further in the past."
315 //
316 if (tday != proc->tm_yday || tyear != proc->tm_year)
317 fmt = "%b_%d";
318 else
319 fmt = "%H:%M:%S";
320 strftime(sbuf, sizeof(sbuf), fmt, proc);
321
322 r.command = cmd;
323 r.exec_time = convert_time(t, tbuf, sizeof(tbuf));
324 r.pid = pid;
325 r.ppid = ppid;
326 r.priority = priority;
327 r.start_time = sbuf;
328
329 dev_to_tty(tty_dev, sizeof(tty_dev), (dev_t) tty_nr, pid, ABBREV_DEV);
330 r.tty = tty_dev;
331
332 get_uids(pid, &r);
333 report_finding(&r, ctx);
334 }
335 SEXP_free(cmd_sexp);
336 }
337 closedir(d);
338
339 return err;
340 }
341
342 int process_probe_main(probe_ctx *ctx, void *arg)
343 {
344 SEXP_t *ent;
345
346 ent = probe_obj_getent(probe_ctx_getobject(ctx), "command", 1);
347 if (ent == NULL) {
348 return PROBE_ENOVAL;
349 }
350
351 if (read_process(ent, ctx)) {
352 SEXP_free(ent);
353 return PROBE_EACCESS;
354 }
355
356 SEXP_free(ent);
357
358 return 0;
359 }
360 #elif defined (OS_SOLARIS)
361
362 #include <procfs.h>
363 #include <unistd.h>
364
365 static char *convert_time(time_t t, char *tbuf, int tb_size)
366 {
367 unsigned d,h,m,s;
368
369 s = t%60;
370 t /= 60;
371 m = t%60;
372 t /=60;
373 h = t%24;
374 t /= 24;
375 d = t;
376 if (d)
377 snprintf(tbuf, tb_size, "%u-%02u:%02u:%02u", d, h, m, s);
378 else
379 snprintf(tbuf, tb_size, "%02u:%02u:%02u", h, m, s);
380 return tbuf;
381 }
382
383 static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
384 {
385 int err = 1;
386 DIR *d;
387 struct dirent *ent;
388
389 d = opendir("/proc");
390 if (d == NULL)
391 return err;
392
393 psinfo_t *psinfo;
394
395 // Scan the directories
396 while (( ent = readdir(d) )) {
397 int fd, len;
398 char buf[336];
399 int pid;
400 unsigned sched_policy;
401 SEXP_t *cmd_sexp;
402
403
404 // Skip non-process dir entries
405 if(*ent->d_name<'0' || *ent->d_name>'9')
406 continue;
407 errno = 0;
408 pid = strtol(ent->d_name, NULL, 10);
409 if (errno || pid == 2) // skip err & kthreads
410 continue;
411
412 // Parse up the stat file for the proc
413 snprintf(buf, 32, "/proc/%d/psinfo", pid);
414 fd = open(buf, O_RDONLY, 0);
415 if (fd < 0)
416 continue;
417 len = read(fd, buf, sizeof buf);
418 close(fd);
419 if (len < 336)
420 continue;
421
422 // The psinfo file contains a psinfo struct; this typecast gets us the struct directly
423 psinfo = (psinfo_t *) buf;
424
425
426 err = 0; // If we get this far, no permission problems
427 dI("Have command: %s", psinfo->pr_fname);
428 cmd_sexp = SEXP_string_newf("%s", psinfo->pr_fname);
429 if (probe_entobj_cmp(cmd_ent, cmd_sexp) == OVAL_RESULT_TRUE) {
430 struct result_info r;
431 char tbuf[32], sbuf[32];
432 int tday,tyear;
433 time_t s_time;
434 struct tm *proc, *now;
435 const char *fmt;
436 int fixfmt_year;
437
438 r.scheduling_class = malloc(PRCLSZ);
439 strncpy(r.scheduling_class, (psinfo->pr_lwp).pr_clname, PRCLSZ);
440
441 // Get the start time
442 s_time = time(NULL);
443 now = localtime_r(&s_time, &result);
444 tyear = now->tm_year;
445 tday = now->tm_yday;
446
447 // Get current time
448 s_time = psinfo->pr_start.tv_sec;
449 proc = localtime_r(&s_time, &result);
450
451 // Select format based on how long we've been running
452 //
453 // FROM THE SPEC:
454 // "This is the time of day the process started formatted in HH:MM:SS if
455 // the same day the process started or formatted as MMM_DD (Ex.: Feb_5)
456 // if process started the previous day or further in the past."
457 //
458 if (tday != proc->tm_yday || tyear != proc->tm_year)
459 fmt = "%b_%d";
460 else
461 fmt = "%H:%M:%S";
462 strftime(sbuf, sizeof(sbuf), fmt, proc);
463
464 r.command = psinfo->pr_fname;
465 r.exec_time = convert_time(psinfo->pr_time.tv_sec, tbuf, sizeof(tbuf));
466 r.pid = psinfo->pr_pid;
467 r.ppid = psinfo->pr_ppid;
468 r.priority = (psinfo->pr_lwp).pr_pri;
469 r.ruid = psinfo->pr_uid;
470 r.start_time = sbuf;
471 r.tty = oscap_sprintf("%s", psinfo->pr_ttydev);
472 r.user_id = psinfo->pr_euid;
473 report_finding(&r, ctx);
474 }
475 SEXP_free(cmd_sexp);
476 }
477 closedir(d);
478
479 return err;
480 }
481
482 int process_probe_main(probe_ctx *ctx, void *arg)
483 {
484 SEXP_t *obj, *ent;
485
486 obj = probe_ctx_getobject(ctx);
487 over = probe_obj_get_platform_schema_version(obj);
488 ent = probe_obj_getent(obj, "command", 1);
489 if (ent == NULL) {
490 return PROBE_ENOVAL;
491 }
492
493 if (read_process(ent, ctx)) {
494 SEXP_free(ent);
495 return PROBE_EACCESS;
496 }
497
498 SEXP_free(ent);
499
500 return 0;
501 }
502
503 #elif defined(OS_FREEBSD)
504 static char *convert_time(time_t t, char *tbuf, int tb_size)
505 {
506 unsigned int days = t / SEC_PER_DAY;
507 unsigned int hrs = (t - days*SEC_PER_DAY) / SEC_PER_HR;
508 unsigned int min = (t - days*SEC_PER_DAY - hrs*SEC_PER_HR) / SEC_PER_MIN;
509 unsigned int sec = (t - days*SEC_PER_DAY - hrs*SEC_PER_HR - min*SEC_PER_MIN);
510
511 if (days) {
512 snprintf(tbuf, tb_size, "%u-%02u:%02u:%02u", days, hrs, min, sec);
513 } else {
514 snprintf(tbuf, tb_size, "%02u:%02u:%02u", hrs, min, sec);
515 }
516
517 return tbuf;
518 }
519
520 static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
521 {
522 struct kinfo_proc *proclist, *proc;
523 struct result_info r;
524 char buf[LINE_MAX];
525 int i, j, count;
526 kvm_t *kd;
527
528 kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, buf);
529
530 if (!kd) {
531 dE("Can't obtain kvm handle");
532 return (PROBE_EFATAL);
533 }
534
535 proclist = kvm_getprocs(kd, KERN_PROC_PROC, 0, &count);
536
537 if (!proclist) {
538 dE("Can't obtain process list");
539 kvm_close(kd);
540 return (PROBE_EFATAL);
541 }
542
543 proc = proclist;
544
545 for (i = 0; i < count; i++, proc++) {
546 // Skip kthreads
547 if (proc->ki_ppid == 0)
548 continue;
549
550 SEXP_t *cmd_sexp;
551 char **argbuf = NULL;
552 char arg_dest[LINE_MAX] = {0};
553 argbuf = kvm_getargv(kd, proc, LINE_MAX);
554
555 if (!argbuf) {
556 r.command = "";
557 } else {
558 for (j = 0; argbuf[j] != NULL; j++) {
559 strncat(arg_dest, argbuf[j], LINE_MAX - strlen(arg_dest) - 1);
560 strncat(arg_dest, " ", LINE_MAX - strlen(arg_dest) - 1);
561 }
562
563 arg_dest[strlen(arg_dest)-1] = '\0';
564 r.command = arg_dest;
565 }
566
567 dD("Have command: %s", r.command);
568 cmd_sexp = SEXP_string_newf("%s", r.command);
569
570 if (probe_entobj_cmp(cmd_ent, cmd_sexp) == OVAL_RESULT_TRUE) {
571 const char *fmt;
572 char tty_buf[64];
573 char exec_buf[64];
574 char start_buf[64];
575
576 time_t curr_year;
577 time_t curr_day;
578 time_t start_year;
579 time_t start_day;
580 time_t exec_time_diff;
581
582 time_t start_time = proc->ki_start.tv_sec;
583 struct timeval current_time;
584
585 gettimeofday(&current_time, NULL);
586 exec_time_diff = current_time.tv_sec - proc->ki_start.tv_sec;
587
588 curr_year = current_time.tv_sec / SEC_PER_YR;
589 curr_day = current_time.tv_sec / SEC_PER_DAY;
590 start_year = start_time / SEC_PER_YR;
591 start_day = start_time / SEC_PER_DAY;
592
593 if (curr_year != start_year || curr_day != start_day) {
594 fmt = "%b_%d";
595 } else {
596 fmt = "%H:%M:%S";
597 }
598
599 struct tm result;
600 struct tm *loc_time = localtime_r(&start_time, &result);
601 strftime(start_buf, sizeof(start_buf), fmt, loc_time);
602
603 switch(proc->ki_tdev) {
604 case NODEV:
605 r.tty = "?";
606 break;
607 default:
608 snprintf(tty_buf, sizeof(tty_buf), "%ld", proc->ki_tdev);
609 r.tty = tty_buf;
610 break;
611 }
612
613 switch (proc->ki_pri.pri_class) {
614 case SCHED_OTHER:
615 r.scheduling_class = "TS";
616 break;
617 case SCHED_FIFO:
618 r.scheduling_class = "FF";
619 break;
620 case SCHED_RR:
621 r.scheduling_class = "RR";
622 break;
623 default:
624 r.scheduling_class = "?";
625 break;
626 }
627
628 r.exec_time = convert_time(exec_time_diff, exec_buf, sizeof(exec_buf));
629 r.pid = proc->ki_pid;
630 r.ppid = proc->ki_ppid;
631 r.priority = proc->ki_pri.pri_level;
632 r.ruid = proc->ki_ruid;
633 r.user_id = proc->ki_uid;
634 r.start_time = start_buf;
635
636 report_finding(&r, ctx);
637 }
638 SEXP_free(cmd_sexp);
639 }
640
641 if (kvm_close(kd)) {
642 dE("Error closing kvm handle");
643 return (PROBE_EFAULT);
644 }
645
646 return 0;
647 }
648
649 int process_probe_main(probe_ctx *ctx, void *arg)
650 {
651 SEXP_t *ent;
652
653 ent = probe_obj_getent(probe_ctx_getobject(ctx), "command", 1);
654
655 if (ent == NULL) {
656 return PROBE_ENOVAL;
657 }
658
659 if (read_process(ent, ctx)) {
660 SEXP_free(ent);
661 return PROBE_EACCESS;
662 }
663
664 SEXP_free(ent);
665
666 return 0;
667 }
668 #else
669 int process_probe_main(probe_ctx *ctx, void *arg)
670 {
671 SEXP_t *item_sexp;
672
673 item_sexp = probe_item_create(OVAL_UNIX_PROCESS, NULL, NULL);
674 probe_item_setstatus (item_sexp, SYSCHAR_STATUS_NOT_COLLECTED);
675 probe_item_collect(ctx, item_sexp);
676
677 return 0;
678 }
679 #endif /* __linux */