|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +use strict; |
| 4 | + |
| 5 | +use Test; |
| 6 | +BEGIN { plan tests => 3 } |
| 7 | + |
| 8 | +use File::Temp qw/ tempdir tempfile /; |
| 9 | + |
| 10 | +### |
| 11 | +# functions |
| 12 | + |
| 13 | +sub key_gen { |
| 14 | + my @chars = ("A".."Z", "a".."z"); |
| 15 | + my $key = "testsuite-" . time . "-"; |
| 16 | + $key .= $chars[rand @chars] for 1..8; |
| 17 | + return $key; |
| 18 | +} |
| 19 | + |
| 20 | +### |
| 21 | +# setup |
| 22 | + |
| 23 | +# reset audit |
| 24 | +system("auditctl -D >& /dev/null"); |
| 25 | + |
| 26 | +# create stdout/stderr sinks |
| 27 | +(my $fh_out, my $stdout) = tempfile(TEMPLATE => '/tmp/audit-testsuite-out-XXXX', |
| 28 | + UNLINK => 1); |
| 29 | +(my $fh_err, my $stderr) = tempfile(TEMPLATE => '/tmp/audit-testsuite-err-XXXX', |
| 30 | + UNLINK => 1); |
| 31 | +(my $fh_ses, my $sesout) = tempfile(TEMPLATE => '/tmp/audit-testsuite-ses-XXXX', |
| 32 | + UNLINK => 1); |
| 33 | +(my $fh_pid, my $pidout) = tempfile(TEMPLATE => '/tmp/audit-testsuite-pid-XXXX', |
| 34 | + UNLINK => 1); |
| 35 | + |
| 36 | +### |
| 37 | +# tests |
| 38 | + |
| 39 | +my $result; |
| 40 | + |
| 41 | +# discover our sesssion ID |
| 42 | +system("cat /proc/self/sessionid > $sesout"); |
| 43 | +my $sessionid = <$fh_ses>; |
| 44 | +chomp($sessionid); |
| 45 | + |
| 46 | +# create a key and rule |
| 47 | +my $key = key_gen(); |
| 48 | +$result = system("auditctl -a always,exit -F arch=b64 -F path=/tmp/$key -F sessionid=$sessionid -k $key"); |
| 49 | +ok($result, 0); |
| 50 | + |
| 51 | +# send the userspace message (NOTE: requires bash) |
| 52 | +system("echo \$\$ > $pidout; exec touch /tmp/$key"); |
| 53 | +my $pid = <$fh_pid>; |
| 54 | +chomp($pid); |
| 55 | + |
| 56 | +# test for the SYSCALL message |
| 57 | +$result = system("ausearch -i -m SYSCALL -sc open -p $pid --session $sessionid -k $key > $stdout 2> $stderr"); |
| 58 | +ok($result, 0); |
| 59 | + |
| 60 | +# test if we generate the SYSCALL record correctly |
| 61 | +my $line; |
| 62 | +my $syscall_msg_match = 0; |
| 63 | +while ($line = <$fh_out>) { |
| 64 | + # test if SYSCALL record matches |
| 65 | + if ($line =~ m?^type=SYSCALL ? and |
| 66 | + $line =~ m? pid=$pid ? and |
| 67 | + $line =~ m? ses=$sessionid ? and |
| 68 | + $line =~ m? key=$key ?) { |
| 69 | + $syscall_msg_match = 1; |
| 70 | + last; |
| 71 | + } |
| 72 | +} |
| 73 | +ok($syscall_msg_match); |
| 74 | + |
| 75 | +### |
| 76 | +# cleanup |
| 77 | + |
| 78 | +system("auditctl -D >& /dev/null"); |
| 79 | + |
0 commit comments