Skip to content

Commit

Permalink
tests: cleanup and improve the syscalls_file test
Browse files Browse the repository at this point in the history
Lots of small changes, but the most significant from a testing
perspective is that we now test for the existence of the OPENAT2
record when possible.

Signed-off-by: Paul Moore <paul@paul-moore.com>
  • Loading branch information
pcmoore committed Feb 9, 2022
1 parent 6197bc4 commit 7b73dc8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
39 changes: 24 additions & 15 deletions tests/syscalls_file/openat2.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,36 @@
#include <sys/stat.h>
#include <fcntl.h>

/* not available yet on travis-ci.org
* #include <linux/openat2.h>
*/
/* linux/openat2.h not available yet on travis-ci.org */
#if 0
#include <linux/openat2.h>
#else
#include <linux/types.h>
struct open_how {
long long unsigned flags;
long long unsigned mode;
long long unsigned resolve;
__u64 flags;
__u64 mode;
__u64 resolve;
};
#define RESOLVE_NO_MAGICLINKS 0x02
#define RESOLVE_BENEATH 0x08
#endif

#include <sys/syscall.h>

#ifndef __NR_openat2
/* on all current Linux ABIs openat2() is 437 */
#define __NR_openat2 437
#endif

int main(int argc, char **argv)
{
char *test_dir = argv[1] ? : ".";
int dir_fd, file_fd;
char *test_dir = argv[1] ? : ".";
char *file_name = argv[2] ? : "file-openat2";
struct open_how how = { .flags = O_CREAT | O_RDWR | O_EXCL,
.mode = 0600,
.resolve = RESOLVE_BENEATH | RESOLVE_NO_MAGICLINKS,
struct open_how how = {
.flags = O_CREAT | O_RDWR | O_EXCL,
.mode = 0600,
.resolve = RESOLVE_BENEATH | RESOLVE_NO_MAGICLINKS,
};

dir_fd = open(test_dir, O_RDONLY | O_DIRECTORY);
Expand All @@ -58,13 +67,13 @@ int main(int argc, char **argv)
return 1;
}

/* Try to create a file. */
printf("create file=%s in directory=%s with flags=0%llo, mode=0%llo, resolve=0x%0llx\n",
file_name, test_dir, how.flags, how.mode, how.resolve);
file_fd = syscall(437, dir_fd, file_name, &how, sizeof(how)); //__NR_openat2
printf("create file=%s/%s with"
" flags=0%llo, mode=0%llo, resolve=0x%0llx\n",
test_dir, file_name, how.flags, how.mode, how.resolve);
file_fd = syscall(__NR_openat2, dir_fd, file_name, &how, sizeof(how));
if (file_fd == -1) {
if (errno == ENOSYS) {
printf("openat2 function not supported\n");
printf("openat2 syscall not supported\n");
return 2;
}
printf("file creation failed\n");
Expand Down
12 changes: 10 additions & 2 deletions tests/syscalls_file/test
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ my $dir = tempdir( TEMPLATE => '/tmp/audit-testsuite-XXXX', CLEANUP => 1 );
###
# tests

my $nr_openat2 = 437;

# audit all open() syscalls on 64-bit systems
my $key = key_gen();
my $result;
Expand All @@ -52,7 +54,7 @@ my $result_open = system(
my $result_openat =
system("auditctl -a always,exit -F arch=b$abi_bits -S openat -k $key");
my $result_openat2 =
system("auditctl -a always,exit -F arch=b$abi_bits -S 437 -k $key"); #openat2
system("auditctl -a always,exit -F arch=b$abi_bits -S $nr_openat2 -k $key");
ok( $result_open == 0 or $result_openat == 0 or $result_openat2 == 0 );

# create a new file
Expand Down Expand Up @@ -82,14 +84,20 @@ my $line;
my $found_syscall = 0;
my $found_parent = 0;
my $found_create = 0;
my $found_openat2 = 0;
while ( $line = <$fh_out> ) {
if ( $line =~ /^type=SYSCALL /
and ( $line =~ / syscall=open(at|at2)? / ) )
{
$found_syscall = 1;
}
if ( $line =~ /^type=OPENAT2 / )
{
$found_openat2 = 1;
}
}
ok($found_syscall);
ok($found_syscall &&
(($result_openat2 != 0) || ($result_openat2 == 0 && $found_openat2 == 1)));

###
# cleanup
Expand Down

0 comments on commit 7b73dc8

Please sign in to comment.