diff --git a/tests/sys/kern/execve/Makefile b/tests/sys/kern/execve/Makefile index 82c5d4b85b10..d1dcae623df4 100644 --- a/tests/sys/kern/execve/Makefile +++ b/tests/sys/kern/execve/Makefile @@ -1,39 +1,40 @@ # $FreeBSD$ TESTSDIR= ${TESTSBASE}/sys/kern/execve BINDIR= ${TESTSDIR} MAN= ATF_TESTS_SH+= execve_test PROGS+= good_aout PROGS+= execve_helper +PROGS+= execve_argc_helper LDFLAGS.goodaout+= -static CLEANFILES+= empty CLEANFILES+= sparse_aout CLEANFILES+= trunc_aout SCRIPTS+= bad_interp_len SCRIPTS+= dev_null_script SCRIPTS+= empty SCRIPTS+= good_script SCRIPTS+= non_exist_shell SCRIPTS+= script_arg SCRIPTS+= script_arg_nospace SCRIPTS+= sparse_aout SCRIPTS+= trunc_aout empty: @touch $@ sparse_aout: @truncate -s 20480 $@ trunc_aout: @truncate -s 16 $@ .include diff --git a/tests/sys/kern/execve/execve_argc_helper.c b/tests/sys/kern/execve/execve_argc_helper.c new file mode 100644 index 000000000000..519c96902fdf --- /dev/null +++ b/tests/sys/kern/execve/execve_argc_helper.c @@ -0,0 +1,15 @@ +/* + * This file is in the public domain. + */ + +#include + +#include + +int +main(int argc, char **argv __unused) +{ + + printf("%d\n", argc); + return (0); +} diff --git a/tests/sys/kern/execve/execve_helper.c b/tests/sys/kern/execve/execve_helper.c index 989b3e4b9761..c14ca2d29554 100644 --- a/tests/sys/kern/execve/execve_helper.c +++ b/tests/sys/kern/execve/execve_helper.c @@ -1,54 +1,61 @@ /* $NetBSD: doexec.c,v 1.8 2003/07/26 19:38:48 salo Exp $ */ /* * Copyright (c) 1993 Christopher G. Demetriou * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the * NetBSD Project. See http://www.NetBSD.org/ for * information about NetBSD. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include +#include #include +/* Passing -n == null_argv */ +static char * const null_argv[] = { NULL }; + int main(int argc, char **argv) { - if (argc != 2) { - fprintf(stderr, "usage: %s \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] \n", argv[0]); exit(2); } - execve(argv[1], &argv[1], NULL); err(1, "execve failed"); } diff --git a/tests/sys/kern/execve/execve_test.sh b/tests/sys/kern/execve/execve_test.sh index ef803a18d442..d3650f3472cc 100644 --- a/tests/sys/kern/execve/execve_test.sh +++ b/tests/sys/kern/execve/execve_test.sh @@ -1,115 +1,133 @@ bad_interp_len_head() { atf_set "descr" "Bad interpreter length" } bad_interp_len_body() { atf_check -s exit:1 -e 'match:No such file or directory' -o empty \ -x "cd $(atf_get_srcdir) && ./execve_helper bad_interp_len" } empty_head() { atf_set "descr" "Empty file" } empty_body() { atf_check -s exit:1 -e 'match:Exec format error' -o empty \ -x "cd $(atf_get_srcdir) && ./execve_helper empty" } good_aout_head() { atf_set "descr" "Good a.out" } good_aout_body() { atf_check -s exit:0 -e empty -o 'match:succeeded' \ -x "cd $(atf_get_srcdir) && ./execve_helper ./good_aout" } good_script_head() { atf_set "descr" "Good script" } good_script_body() { atf_check -s exit:0 -e empty -o 'match:succeeded' \ -x "cd $(atf_get_srcdir) && ./execve_helper good_script" } non_exist_head() { atf_set "descr" "Non-existent file" } non_exist_body() { atf_check -s exit:1 -e 'match:No such file or directory' -o empty \ -x "cd $(atf_get_srcdir) && ./execve_helper non_exist" } non_exist_shell_head() { atf_set "descr" "Non-existent shell" } non_exist_shell_body() { atf_check -s exit:1 -e 'match:No such file or directory' -o empty \ -x "cd $(atf_get_srcdir) && ./execve_helper non_exist_shell" } script_arg_head() { atf_set "descr" "-x in the shebang" } script_arg_body() { atf_check -s exit:0 -e 'match:\+ echo succeeded' -o 'match:succeeded' \ -x "cd $(atf_get_srcdir) && ./execve_helper script_arg" } script_arg_nospace_head() { atf_set "descr" '-x in the shebang; no space between #! and /bin/sh' } script_arg_nospace_body() { atf_check -s exit:0 -e 'match:\+ echo succeeded' -o 'match:succeeded' \ -x "cd $(atf_get_srcdir) && ./execve_helper script_arg_nospace" } sparse_aout_head() { atf_set "descr" 'Sparse file' } sparse_aout_body() { atf_check -s exit:1 -e 'match:Exec format error' -o empty \ -x "cd $(atf_get_srcdir) && ./execve_helper sparse_aout" } trunc_aout_head() { atf_set "descr" 'Truncated file' } trunc_aout_body() { atf_check -s exit:1 -e 'match:Exec format error' -o empty \ -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 atf_add_test_case empty atf_add_test_case good_aout atf_add_test_case good_script atf_add_test_case non_exist atf_add_test_case non_exist_shell atf_add_test_case script_arg 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 }