Skip to content

Commit

Permalink
tests: add a basic test for argc == 0
Browse files Browse the repository at this point in the history
The kernel should reject such exec()s now, early on. Instead of adding
the needed boilerplate to write a test in C, just add an -n argument for
"(n)ull argv" to the execve helper and exec this other helper that just
exits silently with argv count.

Reviewed by:	emaste, kib, markj (all previous version)
Differential Revision:	https://reviews.freebsd.org/D34045
  • Loading branch information
kevans91 committed Jan 26, 2022
1 parent 773fa8c commit e5b431f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions tests/sys/kern/execve/Makefile
Expand Up @@ -10,6 +10,7 @@ ATF_TESTS_SH+= execve_test

PROGS+= good_aout
PROGS+= execve_helper
PROGS+= execve_argc_helper

LDFLAGS.goodaout+= -static

Expand Down
15 changes: 15 additions & 0 deletions tests/sys/kern/execve/execve_argc_helper.c
@@ -0,0 +1,15 @@
/*
* This file is in the public domain.
*/

#include <sys/cdefs.h>

#include <stdio.h>

int
main(int argc, char **argv __unused)
{

printf("%d\n", argc);
return (0);
}
13 changes: 10 additions & 3 deletions tests/sys/kern/execve/execve_helper.c
Expand Up @@ -38,17 +38,24 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* Passing -n == null_argv */
static char * const null_argv[] = { NULL };

int
main(int argc, char **argv)
{

if (argc != 2) {
fprintf(stderr, "usage: %s <progname>\n", argv[0]);
if (argc == 2) {
execve(argv[1], &argv[1], NULL);
} else if (argc == 3 && strcmp(argv[1], "-n") == 0) {
execve(argv[2], null_argv, NULL);
} else {
fprintf(stderr, "usage: %s [-n] <progname>\n", argv[0]);
exit(2);
}

execve(argv[1], &argv[1], NULL);
err(1, "execve failed");
}
18 changes: 18 additions & 0 deletions tests/sys/kern/execve/execve_test.sh
Expand Up @@ -99,6 +99,23 @@ trunc_aout_body()
-x "cd $(atf_get_srcdir) && ./execve_helper trunc_aout"
}

empty_args_head()
{
atf_set "descr" "Empty argv behavior"
}
empty_args_body()
{
atf_check -o inline:"1\n" \
-x "cd $(atf_get_srcdir) && ./execve_helper execve_argc_helper"

# Historically we allowed argc == 0, while execve(2) claimed we didn't.
# execve() should kick back an EINVAL now. We verified the helper was
# there/working in the check just above.
atf_check -s exit:1 \
-e match:".+Invalid argument$" \
-x "cd $(atf_get_srcdir) && ./execve_helper -n execve_argc_helper"
}

atf_init_test_cases()
{
atf_add_test_case bad_interp_len
Expand All @@ -111,5 +128,6 @@ atf_init_test_cases()
atf_add_test_case script_arg_nospace
atf_add_test_case sparse_aout
atf_add_test_case trunc_aout
atf_add_test_case empty_args

}

0 comments on commit e5b431f

Please sign in to comment.