Debian Codegraph @

src/OVAL/probes/unix/process58_probe.c

raw symbols SCIP
1 /**
2 * @file process58_probe.c
3 * @brief process58 probe
4 * @author "Steve Grubb" <[email protected]>
5 *
6 * 2010/06/13 [email protected]
7 * This probe is able to process58 an process58_object as defined in OVAL 5.4 and 5.5.
8 *
9 */
10
11 /*
12 * Copyright 2009-2011 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 * Petr Lautrbach <[email protected]>
32 */
33
34 /*
35 * process58 probe:
36 *
37 * command_line
38 * exec_time
39 * pid
40 * ppid
41 * priority
42 * ruid
43 * scheduling_class
44 * start_time
45 * tty
46 * user_id
47 * exec_shield
48 * loginuid
49 * posix_capability
50 * selinux_domain_label
51 * session_id
52 */
53
54 #ifdef HAVE_CONFIG_H
55 #include <config.h>
56 #endif
57
58 #include <stdlib.h>
59 #include <string.h>
60 #include <stdio.h>
61 #include <errno.h>
62 #include <limits.h>
63 #ifdef HAVE_STDIO_EXT_H
64 # include <stdio_ext.h>
65 #endif
66 #include <dirent.h>
67 #include <sys/stat.h>
68 #include <fcntl.h>
69 #include <sched.h>
70 #include <time.h>
71
72 #ifdef HAVE_PROC_DEVNAME_H
73 #include <proc/devname.h>
74 #endif
75
76 #ifdef SELINUX_FOUND
77 #include <selinux/selinux.h>
78 #include <selinux/context.h>
79 #endif
80 #ifdef CAP_FOUND
81 #include <ctype.h>
82 #include <sys/types.h>
83
84 #ifndef HAVE_CAP_GET_PID // we don't have cap_get_pid => assume libcap version == 1
85 #undef _POSIX_SOURCE
86 #define LIBCAP_VERSION 1
87 #include <sys/capability.h>
88 #define CAP_LAST_CAP CAP_AUDIT_CONTROL
89 extern char const *_cap_names[];
90 #else
91 #define LIBCAP_VERSION 2
92 #include <sys/capability.h>
93 #endif
94
95 #include "util.h"
96 #include "process58-capability.h"
97 #endif /* CAP_FOUND */
98
99 #include <probe/probe.h>
100
101 #include "_seap.h"
102 #include "probe-api.h"
103 #include "probe/entcmp.h"
104 #include "common/debug_priv.h"
105 #include <ctype.h>
106 #include "common/oscap_buffer.h"
107 #include "process58_probe.h"
108 #include "oscap_helpers.h"
109
110 #define CHUNK_SIZE 1024
111
112 /* Convenience structure for the results being reported */
113 struct result_info {
114 const char *command_line;
115 const char *exec_time;
116 unsigned pid;
117 unsigned ppid;
118 long priority;
119 int ruid;
120 const char *scheduling_class;
121 const char *start_time;
122 const char *tty;
123 int user_id;
124 int exec_shield;
125 unsigned loginuid;
126 char **posix_capability;
127 const char *selinux_domain_label;
128 int session_id;
129 };
130
131 static void report_finding(struct result_info *res, probe_ctx *ctx)
132 {
133 SEXP_t *item;
134
135 item = probe_item_create(OVAL_UNIX_PROCESS58, NULL,
136 "command_line", OVAL_DATATYPE_STRING, res->command_line,
137 "exec_time", OVAL_DATATYPE_STRING, res->exec_time,
138 "pid", OVAL_DATATYPE_INTEGER, (int64_t)res->pid,
139 "ppid", OVAL_DATATYPE_INTEGER, (int64_t)res->ppid,
140 "priority", OVAL_DATATYPE_INTEGER, (int64_t)res->priority,
141 "ruid", OVAL_DATATYPE_INTEGER, (int64_t) res->ruid,
142 "scheduling_class", OVAL_DATATYPE_STRING, res->scheduling_class,
143 "start_time", OVAL_DATATYPE_STRING, res->start_time,
144 "tty", OVAL_DATATYPE_STRING, res->tty,
145 "user_id", OVAL_DATATYPE_INTEGER, (int64_t)res->user_id,
146 "exec_shield", OVAL_DATATYPE_BOOLEAN, (bool)res->exec_shield,
147 "loginuid", OVAL_DATATYPE_INTEGER, (int64_t)res->loginuid,
148 "posix_capability", OVAL_DATATYPE_STRING_M, res->posix_capability,
149 "selinux_domain_label", OVAL_DATATYPE_STRING, res->selinux_domain_label,
150 "session_id", OVAL_DATATYPE_INTEGER, (int64_t)res->session_id,
151 NULL);
152
153 probe_item_collect(ctx, item);
154 }
155
156 #if defined(OS_LINUX)
157
158 static unsigned long ticks, boot;
159
160 static void get_boot_time(void)
161 {
162 char buf[PATH_MAX];
163 FILE *sf;
164 int line;
165
166 boot = 0;
167 const char *prefix = getenv("OSCAP_PROBE_ROOT");
168 snprintf(buf, sizeof(buf), "%s/proc/stat", prefix ? prefix : "");
169 sf = fopen(buf, "rt");
170 if (sf == NULL)
171 return;
172
173 line = 0;
174 __fsetlocking(sf, FSETLOCKING_BYCALLER);
175 while (fgets(buf, sizeof(buf), sf)) {
176 if (line == 0) {
177 line++;
178 continue;
179 }
180 if (memcmp(buf, "btime", 5) == 0) {
181 sscanf(buf, "btime %lu", &boot);
182 break;
183 }
184 }
185 fclose(sf);
186 }
187
188 static int get_uids(int pid, struct result_info *r)
189 {
190 char buf[PATH_MAX];
191 FILE *sf;
192
193 r->ruid = -1;
194 r->user_id = -1;
195 r->loginuid = -1;
196
197 const char *prefix = getenv("OSCAP_PROBE_ROOT");
198
199 snprintf(buf, sizeof(buf), "%s/proc/%d/status", prefix ? prefix : "", pid);
200 sf = fopen(buf, "rt");
201 if (sf) {
202 int line = 0;
203 __fsetlocking(sf, FSETLOCKING_BYCALLER);
204 while (fgets(buf, sizeof(buf), sf)) {
205 if (line == 0) {
206 line++;
207 continue;
208 }
209 if (memcmp(buf, "Uid:", 4) == 0) {
210 sscanf(buf, "Uid: %d %d", &r->ruid, &r->user_id);
211 break;
212 }
213 }
214 fclose(sf);
215 }
216
217 snprintf(buf, sizeof(buf), "%s/proc/%d/loginuid", prefix ? prefix : "", pid);
218 sf = fopen(buf, "rt");
219 if (sf) {
220 if (fscanf(sf, "%u", &r->loginuid) < 1) {
221 dW("fscanf failed from %s", buf);
222 }
223 fclose(sf);
224 }
225
226 return 0;
227 }
228
229 static char *convert_time(unsigned long long t, char *tbuf, int tb_size)
230 {
231 unsigned d,h,m,s;
232
233 s = t%60;
234 t /= 60;
235 m = t%60;
236 t /=60;
237 h = t%24;
238 t /= 24;
239 d = t;
240 if (d)
241 snprintf(tbuf, tb_size, "%u-%02u:%02u:%02u", d, h, m, s);
242 else
243 snprintf(tbuf, tb_size, "%02u:%02u:%02u", h, m, s);
244 return tbuf;
245 }
246
247 #ifdef SELINUX_FOUND
248 static char *get_selinux_label(int pid) {
249 char *selinux_label;
250 char *pid_context;
251 context_t context;
252
253 if (is_selinux_enabled() == 1) {
254 if (getpidcon(pid, &pid_context) == -1) {
255 /* error getting pid selinux context */
256 dW("Can't get selinux context for process %d", pid);
257 return NULL;
258 }
259 context = context_new(pid_context);
260 if (context == NULL) {
261 // There must be 3 or 4 colon-separated components and no
262 // whitespace in any component other than the MLS
263 // component.
264 freecon(pid_context);
265 return NULL;
266 }
267 selinux_label = strdup(context_type_get(context));
268 context_free(context);
269 freecon(pid_context);
270 return selinux_label;
271 } else {
272 return NULL;
273 }
274 }
275 #else
276 static char *get_selinux_label(int pid) {
277 return NULL;
278 }
279 #endif /* SELINUX_FOUND */
280
281 static char **get_posix_capability(int pid, int max_cap_id) {
282 #ifdef CAP_FOUND
283 cap_t pid_caps;
284 char *cap_name, **ret = NULL;
285 unsigned cap_value, ret_index = 0;
286 cap_flag_value_t cap_flag;
287 int cap_id;
288
289 #if LIBCAP_VERSION == 1
290 pid_caps = cap_init();
291 if (capgetp(pid, pid_caps) == -1) {
292 dW("Can't get capabilities for process %d", pid);
293 cap_free(pid_caps);
294 return NULL;
295 }
296 #elif LIBCAP_VERSION == 2
297 pid_caps = cap_get_pid(pid);
298 #else
299 dW("Can't detect libcap version");
300 return NULL;
301 #endif
302
303 if (pid_caps == NULL) {
304 dW("Can't get capabilities for process %d", pid);
305 return NULL;
306 }
307
308 for (cap_value = 0; cap_value < CAP_LAST_CAP; cap_value++) {
309 if (cap_get_flag(pid_caps, cap_value, CAP_EFFECTIVE, &cap_flag) == -1)
310 continue;
311 if (cap_flag == CAP_SET) {
312 #if LIBCAP_VERSION == 2
313 cap_name = cap_to_name(cap_value);
314 #else
315 cap_name = strdup(_cap_names[cap_value]);
316 #endif
317 if (cap_name != NULL) {
318 char *cap_name_p = cap_name;
319 while (*cap_name_p) {
320 *cap_name_p = toupper(*cap_name_p);
321 cap_name_p++;
322 }
323
324 cap_id = oscap_string_to_enum(CapabilityType, cap_name);
325 if (cap_id > -1 && cap_id <= max_cap_id) {
326 void *new_ret = realloc(ret, (ret_index + 1) * sizeof(char *));
327 if (new_ret == NULL) {
328 dE("Unable to re-allocate memory for ret");
329 cap_free(cap_name);
330 free(ret);
331 ret = NULL;
332 goto exit;
333 }
334 ret = new_ret;
335 ret[ret_index] = strdup(cap_name);
336 ret_index++;
337 }
338 cap_free(cap_name);
339 }
340 }
341 }
342 void *new_ret = realloc(ret, (ret_index + 1) * sizeof(char *));
343 if (new_ret == NULL) {
344 dE("Unable to re-allocate memory for ret");
345 free(ret);
346 ret = NULL;
347 goto exit;
348 }
349 ret = new_ret;
350 ret[ret_index] = NULL;
351
352 exit:
353 cap_free(pid_caps);
354 return ret;
355 #else
356 return NULL;
357 #endif
358 }
359
360 /* get exec shield status according to http://people.redhat.com/sgrubb/files/lsexec
361 * return value: -1 - not detected, 0 - disabled, 1 - enabled */
362 static int get_exec_shield_status(int pid) {
363 char buf[PATH_MAX];
364 FILE *sf;
365 long unsigned low, high, inode;
366 long long unsigned offset;
367 int dev_min, dev_maj;
368 char perm[3], trim;
369 int ret = -1, read_items;
370
371 const char *prefix = getenv("OSCAP_PROBE_ROOT");
372 snprintf(buf, sizeof(buf), "%s/proc/%d/maps", prefix ? prefix : "", pid);
373 sf = fopen(buf, "rt");
374 if (sf) {
375 while (fgets(buf, 500, sf)) {
376 read_items = sscanf(
377 buf, "%lx-%lx rw%s %llx %x:%x %lu %c\n",
378 &low, &high, perm, &offset, &dev_min,
379 &dev_maj, &inode, &trim
380 );
381 if (read_items == 7) {
382 if (perm[0] == 'x' && offset != 0) {
383 ret = 0;
384 }
385 else {
386 ret = 1;
387 }
388 }
389 }
390 fclose(sf);
391 }
392
393 return ret;
394 }
395
396 /**
397 * Parse /proc/%d/cmdline file
398 * @param filepath Path to file ~ use preallocated buffer for the path
399 * @param buffer output buffer with non-zero size
400 * @return ps-like command info or NULL
401 */
402 static inline bool get_process_cmdline(const char* filepath, struct oscap_buffer* const buffer){
403
404 int fd = open(filepath, O_RDONLY, 0);
405
406 if (fd < 0) {
407 return false;
408 }
409
410 oscap_buffer_clear(buffer);
411
412
413
414 for(;;) {
415 char chunk[CHUNK_SIZE];
416 // Read data, store to buffer
417 ssize_t read_size = read(fd, chunk, CHUNK_SIZE);
418 if (read_size < 0) {
419 close(fd);
420 return false;
421 }
422 oscap_buffer_append_binary_data(buffer, chunk, read_size);
423
424 // If reach end of file, then end the loop
425 if (CHUNK_SIZE != read_size) {
426 break;
427 }
428 }
429
430 close(fd);
431
432 int length = oscap_buffer_get_length(buffer);
433 char* buffer_mem = oscap_buffer_get_raw(buffer);
434
435 if ( length == 0 ) { // empty file
436 return false;
437 } else {
438
439 // Skip multiple trailing zeros
440 int i = length - 1;
441 while ( (i > 0) && (buffer_mem[i] == '\0') ) {
442 --i;
443 }
444
445 // Program and args are separated by '\0'
446 // Replace them with spaces ' '
447 while( i >= 0 ){
448 char chr = buffer_mem[i];
449 if ( ( chr == '\0') || ( chr == '\n' ) ) {
450 buffer_mem[i] = ' ';
451 } else if ( !isprint(chr) ) { // "ps" replace non-printable characters with '.' (LC_ALL=C)
452 buffer_mem[i] = '.';
453 }
454 --i;
455 }
456 }
457 return true;
458 }
459
460 /**
461 * Make "[%s] <defunct>" from cmd string - inplace
462 * @param cmd_buffer @see read_process() > cmd_buffer
463 * @return pointer to start of string
464 */
465 static inline char *make_defunc_str(char* const cmd_buffer){
466 static const char DEFUNC_STR[] = "] <defunct>";
467
468 size_t len = strlen(cmd_buffer);
469 memcpy(cmd_buffer + len, DEFUNC_STR, sizeof(DEFUNC_STR));
470 return cmd_buffer;
471 }
472
473 static int read_process(SEXP_t *cmd_ent, SEXP_t *pid_ent, probe_ctx *ctx)
474 {
475 char buf[PATH_MAX];
476 int max_cap_id;
477 DIR *d;
478 struct dirent *ent;
479 oval_schema_version_t oval_version;
480 struct tm result;
481
482 const char *prefix = getenv("OSCAP_PROBE_ROOT");
483 snprintf(buf, PATH_MAX, "%s/proc", prefix ? prefix : "");
484 d = opendir(buf);
485 if (d == NULL) {
486 return prefix ? PROBE_ESUCCESS : PROBE_EACCESS;
487 }
488
489 // Get the time tick hertz
490 ticks = (unsigned long)sysconf(_SC_CLK_TCK);
491 get_boot_time();
492
493 oval_version = probe_obj_get_platform_schema_version(probe_ctx_getobject(ctx));
494 if (oval_schema_version_cmp(oval_version, OVAL_SCHEMA_VERSION(5.11)) < 0) {
495 max_cap_id = OVAL_5_8_MAX_CAP_ID;
496 } else {
497 max_cap_id = OVAL_5_11_MAX_CAP_ID;
498 }
499
500 struct oscap_buffer *cmdline_buffer = oscap_buffer_new();
501
502 char cmd_buffer[1 + 15 + 11 + 1]; // Format:" [ cmd:15 ] <defunc>"
503 cmd_buffer[0] = '[';
504
505 // Scan the directories
506 bool any_pid_dir_found = false;
507 while (( ent = readdir(d) )) {
508 int fd, len;
509 char *tmp, state, tty_dev[128];
510 int pid, ppid, pgrp, session, tty_nr, tpgid;
511 unsigned flags, sched_policy;
512 unsigned long minflt, cminflt, majflt, cmajflt, uutime, ustime;
513 long cutime, cstime, priority, cnice, nthreads, itrealvalue;
514 unsigned long long start;
515 SEXP_t *cmd_sexp = NULL, *pid_sexp = NULL;
516
517 // Skip non-process58 dir entries
518 if(*ent->d_name<'0' || *ent->d_name>'9')
519 continue;
520 errno = 0;
521 pid = strtol(ent->d_name, NULL, 10);
522 if (errno || pid == 2) // skip err & kthreads
523 continue;
524
525 // Parse up the stat file for the proc
526 snprintf(buf, PATH_MAX, "%s/proc/%d/stat", prefix ? prefix : "", pid);
527 fd = open(buf, O_RDONLY, 0);
528 if (fd < 0)
529 continue;
530 len = read(fd, buf, sizeof buf - 1);
531 close(fd);
532 if (len < 40)
533 continue;
534 buf[len] = 0;
535 tmp = strrchr(buf, ')');
536 if (tmp)
537 *tmp = 0;
538 else
539 continue;
540 memset(cmd_buffer + 1, 0, sizeof(cmd_buffer)-1); // clear cmd after starting '['
541 sscanf(buf, "%d (%15c", &ppid, cmd_buffer + 1);
542 sscanf(tmp+2, "%c %d %d %d %d %d "
543 "%u %lu %lu %lu %lu "
544 "%lu %lu %lu %ld %ld "
545 "%ld %ld %ld %llu",
546 &state, &ppid, &pgrp, &session, &tty_nr, &tpgid,
547 &flags, &minflt, &cminflt, &majflt, &cmajflt,
548 &uutime, &ustime, &cutime, &cstime, &priority,
549 &cnice, &nthreads, &itrealvalue, &start
550 );
551
552 // Skip kthreads
553 if (ppid == 2)
554 continue;
555
556 const char* cmd;
557 if (state == 'Z') { // zombie
558 cmd = make_defunc_str(cmd_buffer);
559 } else {
560 snprintf(buf, PATH_MAX, "%s/proc/%d/cmdline", prefix ? prefix : "", pid);
561 if (get_process_cmdline(buf, cmdline_buffer)) {
562 cmd = oscap_buffer_get_raw(cmdline_buffer); // use full cmdline
563 } else {
564 cmd = cmd_buffer + 1;
565 }
566 }
567
568 any_pid_dir_found = true;
569 cmd_sexp = SEXP_string_newf("%s", cmd);
570 pid_sexp = SEXP_number_newu_32(pid);
571 if ((cmd_sexp == NULL || probe_entobj_cmp(cmd_ent, cmd_sexp) == OVAL_RESULT_TRUE) &&
572 (pid_sexp == NULL || probe_entobj_cmp(pid_ent, pid_sexp) == OVAL_RESULT_TRUE)
573 ) {
574 struct result_info r;
575 unsigned long t = uutime/ticks + ustime/ticks;
576 char tbuf[32], sbuf[32], *selinux_domain_label, **posix_capabilities;
577 int tday,tyear;
578 time_t s_time;
579 struct tm *proc, *now;
580 const char *fmt;
581
582 // Now get scheduler policy
583 sched_policy = sched_getscheduler(pid);
584 switch (sched_policy) {
585 case SCHED_OTHER:
586 r.scheduling_class = "TS";
587 break;
588 case SCHED_BATCH:
589 r.scheduling_class = "B";
590 break;
591 #ifdef SCHED_IDLE
592 case SCHED_IDLE:
593 r.scheduling_class = "#5";
594 break;
595 #endif
596 case SCHED_FIFO:
597 r.scheduling_class = "FF";
598 break;
599 case SCHED_RR:
600 r.scheduling_class = "RR";
601 break;
602 default:
603 r.scheduling_class = "?";
604 break;
605 }
606
607 // Calculate the start time
608 s_time = time(NULL);
609 now = localtime_r(&s_time, &result);
610 tyear = now->tm_year;
611 tday = now->tm_yday;
612 s_time = boot + (start / ticks);
613 proc = localtime_r(&s_time, &result);
614
615 // Select format based on how long we've been running
616 //
617 // FROM THE SPEC:
618 // "This is the time of day the process started formatted in HH:MM:SS if
619 // the same day the process started or formatted as MMM_DD (Ex.: Feb_5)
620 // if process started the previous day or further in the past."
621 //
622 if (tday != proc->tm_yday || tyear != proc->tm_year)
623 fmt = "%b_%d";
624 else
625 fmt = "%H:%M:%S";
626 strftime(sbuf, sizeof(sbuf), fmt, proc);
627
628 r.command_line = cmd;
629 r.exec_time = convert_time(t, tbuf, sizeof(tbuf));
630 r.pid = pid;
631 r.ppid = ppid;
632 r.priority = priority;
633 r.start_time = sbuf;
634
635 dev_to_tty(tty_dev, sizeof(tty_dev), (dev_t) tty_nr, pid, ABBREV_DEV);
636 r.tty = tty_dev;
637
638 r.exec_shield = (get_exec_shield_status(pid) > 0);
639
640 selinux_domain_label = get_selinux_label(pid);
641 r.selinux_domain_label = selinux_domain_label;
642
643 posix_capabilities = get_posix_capability(pid, max_cap_id);
644 r.posix_capability = posix_capabilities;
645
646 r.session_id = session;
647
648 get_uids(pid, &r);
649 report_finding(&r, ctx);
650
651 if (selinux_domain_label != NULL)
652 free(selinux_domain_label);
653
654 if (posix_capabilities != NULL) {
655 char **posix_capabilities_p = posix_capabilities;
656 while (*posix_capabilities_p)
657 free(*posix_capabilities_p++);
658 free(posix_capabilities);
659 }
660 }
661 SEXP_free(cmd_sexp);
662 SEXP_free(pid_sexp);
663 }
664 closedir(d);
665 oscap_buffer_free(cmdline_buffer);
666
667 if (!any_pid_dir_found) {
668 dW("No data about processes could be read from '%s'.", buf);
669 }
670 // In offline mode, empty /proc might be a normal situation and doesn't
671 // have to mean permissions problems
672 if (prefix)
673 return PROBE_ESUCCESS;
674 else
675 return any_pid_dir_found ? PROBE_ESUCCESS : PROBE_EACCESS;
676 }
677
678 int process58_probe_offline_mode_supported(void)
679 {
680 return PROBE_OFFLINE_OWN;
681 }
682
683 int process58_probe_main(probe_ctx *ctx, void *arg)
684 {
685 SEXP_t *command_line_ent, *pid_ent;
686
687 command_line_ent = probe_obj_getent(probe_ctx_getobject(ctx), "command_line", 1);
688 pid_ent = probe_obj_getent(probe_ctx_getobject(ctx), "pid", 1);
689 if (command_line_ent == NULL && pid_ent == NULL) {
690 return PROBE_ENOVAL;
691 }
692
693 int err = read_process(command_line_ent, pid_ent, ctx);
694 if (err) {
695 SEXP_free(command_line_ent);
696 SEXP_free(pid_ent);
697 return err;
698 }
699
700 SEXP_free(command_line_ent);
701 SEXP_free(pid_ent);
702
703 return 0;
704 }
705 #elif defined (OS_SOLARIS)
706
707 #include <procfs.h>
708 #include <unistd.h>
709
710 static char *convert_time(time_t t, char *tbuf, int tb_size)
711 {
712 unsigned d,h,m,s;
713
714 s = t%60;
715 t /= 60;
716 m = t%60;
717 t /=60;
718 h = t%24;
719 t /= 24;
720 d = t;
721 if (d)
722 snprintf(tbuf, tb_size, "%u-%02u:%02u:%02u", d, h, m, s);
723 else
724 snprintf(tbuf, tb_size, "%02u:%02u:%02u", h, m, s);
725 return tbuf;
726 }
727
728 static int read_process(SEXP_t *cmd_ent, probe_ctx *ctx)
729 {
730 int err = 1;
731 DIR *d;
732 struct dirent *ent;
733 struct tm result;
734
735 d = opendir("/proc");
736 if (d == NULL)
737 return err;
738
739 psinfo_t *psinfo;
740
741 // Scan the directories
742 while (( ent = readdir(d) )) {
743 int fd, len;
744 char buf[336];
745 int pid;
746 unsigned sched_policy;
747 SEXP_t *cmd_sexp;
748
749
750 // Skip non-process58 dir entries
751 if(*ent->d_name<'0' || *ent->d_name>'9')
752 continue;
753 errno = 0;
754 pid = strtol(ent->d_name, NULL, 10);
755 if (errno || pid == 2) // skip err & kthreads
756 continue;
757
758 // Parse up the stat file for the proc
759 snprintf(buf, 32, "/proc/%d/psinfo", pid);
760 fd = open(buf, O_RDONLY, 0);
761 if (fd < 0)
762 continue;
763 len = read(fd, buf, sizeof buf);
764 close(fd);
765 if (len < 336)
766 continue;
767
768 // The psinfo file contains a psinfo struct; this typecast gets us the struct directly
769 psinfo = (psinfo_t *) buf;
770
771
772 err = 0; // If we get this far, no permission problems
773 dI("Have command: %s", psinfo->pr_fname);
774 cmd_sexp = SEXP_string_newf("%s", psinfo->pr_fname);
775 if (probe_entobj_cmp(cmd_ent, cmd_sexp) == OVAL_RESULT_TRUE) {
776 struct result_info r;
777 char tbuf[32], sbuf[32];
778 int tday,tyear;
779 time_t s_time;
780 struct tm *proc, *now;
781 const char *fmt;
782 int fixfmt_year;
783
784 r.scheduling_class = malloc(PRCLSZ);
785 strncpy(r.scheduling_class, (psinfo->pr_lwp).pr_clname, sizeof(r.scheduling_class));
786
787 // Get the start time
788 s_time = time(NULL);
789 now = localtime_r(&s_time, &result);
790 tyear = now->tm_year;
791 tday = now->tm_yday;
792
793 // Get current time
794 s_time = psinfo->pr_start.tv_sec;
795 proc = localtime_r(&s_time, &result);
796
797 // Select format based on how long we've been running
798 //
799 // FROM THE SPEC:
800 // "This is the time of day the process started formatted in HH:MM:SS if
801 // the same day the process started or formatted as MMM_DD (Ex.: Feb_5)
802 // if process started the previous day or further in the past."
803 //
804 if (tday != proc->tm_yday || tyear != proc->tm_year)
805 fmt = "%b_%d";
806 else
807 fmt = "%H:%M:%S";
808 strftime(sbuf, sizeof(sbuf), fmt, proc);
809
810 r.command_line = psinfo->pr_fname;
811 r.exec_time = convert_time(psinfo->pr_time.tv_sec, tbuf, sizeof(tbuf));
812 r.pid = psinfo->pr_pid;
813 r.ppid = psinfo->pr_ppid;
814 r.priority = (psinfo->pr_lwp).pr_pri;
815 r.ruid = psinfo->pr_uid;
816 r.start_time = sbuf;
817 r.tty = oscap_sprintf("%s", psinfo->pr_ttydev);
818 r.user_id = psinfo->pr_euid;
819 report_finding(&r, ctx);
820 }
821 SEXP_free(cmd_sexp);
822 }
823 closedir(d);
824
825 return err;
826 }
827
828 int process58_probe_offline_mode_supported(void)
829 {
830 return PROBE_OFFLINE_NONE;
831 }
832
833 int process58_probe_main(probe_ctx *ctx, void *arg)
834 {
835 SEXP_t *ent;
836
837 ent = probe_obj_getent(probe_ctx_getobject(ctx), "command", 1);
838 if (ent == NULL) {
839 return PROBE_ENOVAL;
840 }
841
842 if (read_process(ent, ctx)) {
843 SEXP_free(ent);
844 return PROBE_EACCESS;
845 }
846
847 SEXP_free(ent);
848
849 return 0;
850 }
851
852 #else
853 int process58_probe_main(probe_ctx *ctx, void *arg)
854 {
855 SEXP_t *item_sexp;
856
857 item_sexp = probe_item_create(OVAL_UNIX_PROCESS58, NULL, NULL);
858 probe_item_setstatus (item_sexp, SYSCHAR_STATUS_NOT_COLLECTED);
859 probe_item_collect(ctx, item_sexp);
860
861 return 0;
862 }
863 #endif /* __linux */