xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/execve/execve06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2022 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Test that kernel adds dummy argv[0] if empty argument list was passed to
10*49cdfc7eSAndroid Build Coastguard Worker  * execve(). This fixes at least one CVE where userspace programs start to
11*49cdfc7eSAndroid Build Coastguard Worker  * process argument list blindly from argv[1] such as polkit pkexec
12*49cdfc7eSAndroid Build Coastguard Worker  * CVE-2021-4034.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * See also https://lwn.net/Articles/883547/
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
verify_execve(void)21*49cdfc7eSAndroid Build Coastguard Worker static void verify_execve(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
24*49cdfc7eSAndroid Build Coastguard Worker 	char path[512];
25*49cdfc7eSAndroid Build Coastguard Worker 	char ipc_env_var[1024];
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(ipc_env_var, IPC_ENV_VAR "=%s", getenv(IPC_ENV_VAR));
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	char *const envp[] = {ipc_env_var, NULL};
30*49cdfc7eSAndroid Build Coastguard Worker 	char *const argv[] = {NULL};
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_get_path("execve06_child", path, sizeof(path)))
33*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Couldn't find execve06_child in $PATH");
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
36*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0) {
37*49cdfc7eSAndroid Build Coastguard Worker 		execve(path, argv, envp);
38*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TERRNO, "Failed to execute execve06_child");
39*49cdfc7eSAndroid Build Coastguard Worker 	}
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
43*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
44*49cdfc7eSAndroid Build Coastguard Worker 	.child_needs_reinit = 1,
45*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_execve,
46*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
47*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "dcd46d897adb"},
48*49cdfc7eSAndroid Build Coastguard Worker 		{"CVE", "2021-4034"},
49*49cdfc7eSAndroid Build Coastguard Worker 		{}
50*49cdfc7eSAndroid Build Coastguard Worker 	}
51*49cdfc7eSAndroid Build Coastguard Worker };
52