git: d352fa262709 - stable/13 - tests: add a basic test for argc == 0

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Thu, 10 Feb 2022 20:22:16 UTC
The branch stable/13 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=d352fa2627096c0cdb1238e06c047f91a3ef0527

commit d352fa2627096c0cdb1238e06c047f91a3ef0527
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2022-01-26 01:22:03 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2022-02-10 20:21:59 +0000

    tests: add a basic test for argc == 0
    
    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.
    
    (cherry picked from commit e5b431fc0c20771a2a18cb5169d75cf337d0f1dd)
---
 tests/sys/kern/execve/Makefile             |  1 +
 tests/sys/kern/execve/execve_argc_helper.c | 15 +++++++++++++++
 tests/sys/kern/execve/execve_helper.c      | 13 ++++++++++---
 tests/sys/kern/execve/execve_test.sh       | 18 ++++++++++++++++++
 4 files changed, 44 insertions(+), 3 deletions(-)

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
@@ -10,6 +10,7 @@ ATF_TESTS_SH+=	execve_test
 
 PROGS+=		good_aout
 PROGS+=		execve_helper
+PROGS+=		execve_argc_helper
 
 LDFLAGS.goodaout+=	-static
 
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 <sys/cdefs.h>
+
+#include <stdio.h>
+
+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
@@ -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");
 }
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
@@ -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
@@ -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
 
 }