1 /*
2  * This file is part of execve strace test.
3  *
4  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2015-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 #define FILENAME "test.execve\nfilename"
36 #define Q_FILENAME "test.execve\\nfilename"
37 
38 static const char * const argv[] = {
39 	FILENAME, "first", "second", (const char *) -1L,
40 	(const char *) -2L, (const char *) -3L
41 };
42 static const char * const q_argv[] = {
43 	Q_FILENAME, "first", "second"
44 };
45 
46 static const char * const envp[] = {
47 	"foobar=1", "foo\nbar=2", (const char *) -1L,
48 	(const char *) -2L, (const char *) -3L
49 };
50 static const char * const q_envp[] = {
51 	"foobar=1", "foo\\nbar=2"
52 };
53 
54 int
main(void)55 main(void)
56 {
57 	char ** const tail_argv = tail_memdup(argv, sizeof(argv));
58 	char ** const tail_envp = tail_memdup(envp, sizeof(envp));
59 
60 	execve(FILENAME, tail_argv, tail_envp);
61 	printf("execve(\"%s\""
62 	       ", [\"%s\", \"%s\", \"%s\", %p, %p, %p, ... /* %p */]"
63 #if VERBOSE
64 	       ", [\"%s\", \"%s\", %p, %p, %p, ... /* %p */]"
65 #else
66 	       ", %p /* 5 vars, unterminated */"
67 #endif
68 	       ") = -1 ENOENT (%m)\n",
69 	       Q_FILENAME, q_argv[0], q_argv[1], q_argv[2],
70 	       argv[3], argv[4], argv[5], (char *) tail_argv + sizeof(argv)
71 #if VERBOSE
72 	       , q_envp[0], q_envp[1], envp[2], envp[3], envp[4],
73 	       (char *) tail_envp + sizeof(envp)
74 #else
75 	       , tail_envp
76 #endif
77 	       );
78 
79 	tail_argv[ARRAY_SIZE(q_argv)] = NULL;
80 	tail_envp[ARRAY_SIZE(q_envp)] = NULL;
81 	(void) q_envp;	/* workaround for clang bug #33068 */
82 
83 	execve(FILENAME, tail_argv, tail_envp);
84 	printf("execve(\"%s\", [\"%s\", \"%s\", \"%s\"]"
85 #if VERBOSE
86 	       ", [\"%s\", \"%s\"]"
87 #else
88 	       ", %p /* 2 vars */"
89 #endif
90 	       ") = -1 ENOENT (%m)\n",
91 	       Q_FILENAME, q_argv[0], q_argv[1], q_argv[2]
92 #if VERBOSE
93 	       , q_envp[0], q_envp[1]
94 #else
95 	       , tail_envp
96 #endif
97 	       );
98 
99 	execve(FILENAME, tail_argv + 2, tail_envp + 1);
100 	printf("execve(\"%s\", [\"%s\"]"
101 #if VERBOSE
102 	       ", [\"%s\"]"
103 #else
104 	       ", %p /* 1 var */"
105 #endif
106 	       ") = -1 ENOENT (%m)\n",
107 	       Q_FILENAME, q_argv[2]
108 #if VERBOSE
109 	       , q_envp[1]
110 #else
111 	       , tail_envp + 1
112 #endif
113 	       );
114 
115 	TAIL_ALLOC_OBJECT_CONST_PTR(char *, empty);
116 	char **const efault = empty + 1;
117 	*empty = NULL;
118 
119 	execve(FILENAME, empty, empty);
120 	printf("execve(\"%s\", []"
121 #if VERBOSE
122 	       ", []"
123 #else
124 	       ", %p /* 0 vars */"
125 #endif
126 	       ") = -1 ENOENT (%m)\n", Q_FILENAME
127 #if !VERBOSE
128 	       , empty
129 #endif
130 	       );
131 
132 	char *const str_a = tail_alloc(DEFAULT_STRLEN + 2);
133 	fill_memory_ex(str_a, DEFAULT_STRLEN + 1, '0', 10);
134 	str_a[DEFAULT_STRLEN + 1] = '\0';
135 
136 	char *const str_b = tail_alloc(DEFAULT_STRLEN + 2);
137 	fill_memory_ex(str_b, DEFAULT_STRLEN + 1, '_', 32);
138 	str_b[DEFAULT_STRLEN + 1] = '\0';
139 
140 	char **const a = tail_alloc(sizeof(*a) * (DEFAULT_STRLEN + 2));
141 	char **const b = tail_alloc(sizeof(*b) * (DEFAULT_STRLEN + 2));
142 	unsigned int i;
143 	for (i = 0; i <= DEFAULT_STRLEN; ++i) {
144 		a[i] = &str_a[i];
145 		b[i] = &str_b[i];
146 	}
147 	a[i] = b[i] = NULL;
148 
149 	execve(FILENAME, a, b);
150 	printf("execve(\"%s\", [\"%.*s\"...", Q_FILENAME, DEFAULT_STRLEN, a[0]);
151 	for (i = 1; i < DEFAULT_STRLEN; ++i)
152 		printf(", \"%s\"", a[i]);
153 #if VERBOSE
154 	printf(", \"%s\"", a[i]);
155 #else
156 	printf(", ...");
157 #endif
158 #if VERBOSE
159 	printf("], [\"%.*s\"...", DEFAULT_STRLEN, b[0]);
160 	for (i = 1; i <= DEFAULT_STRLEN; ++i)
161 		printf(", \"%s\"", b[i]);
162 	printf("]");
163 #else
164 	printf("], %p /* %u vars */", b, DEFAULT_STRLEN + 1);
165 #endif
166 	printf(") = -1 ENOENT (%m)\n");
167 
168 	execve(FILENAME, a + 1, b + 1);
169 	printf("execve(\"%s\", [\"%s\"", Q_FILENAME, a[1]);
170 	for (i = 2; i <= DEFAULT_STRLEN; ++i)
171 		printf(", \"%s\"", a[i]);
172 #if VERBOSE
173 	printf("], [\"%s\"", b[1]);
174 	for (i = 2; i <= DEFAULT_STRLEN; ++i)
175 		printf(", \"%s\"", b[i]);
176 	printf("]");
177 #else
178 	printf("], %p /* %d vars */", b + 1, DEFAULT_STRLEN);
179 #endif
180 	printf(") = -1 ENOENT (%m)\n");
181 
182 	execve(FILENAME, (char **) tail_argv[ARRAY_SIZE(q_argv)], efault);
183 	printf("execve(\"%s\", NULL, %p) = -1 ENOENT (%m)\n",
184 	       Q_FILENAME, efault);
185 
186 	execve(FILENAME, efault, NULL);
187 	printf("execve(\"%s\", %p, NULL) = -1 ENOENT (%m)\n",
188 	       Q_FILENAME, efault);
189 
190 	return 0;
191 }
192