1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2014 Google, Inc.
*
* Selftests for execveat(2).
*/
#define _GNU_SOURCE /* to get O_PATH, AT_EMPTY_PATH */
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../kselftest.h"
static char longpath[2 * PATH_MAX] = "";
static char *envp[] = { "IN_TEST=yes", NULL, NULL };
static char *argv[] = { "execveat", "99", NULL };
static int execveat_(int fd, const char *path, char **argv, char **envp,
int flags)
{
#ifdef __NR_execveat
return syscall(__NR_execveat, fd, path, argv, envp, flags);
#else
errno = ENOSYS;
return -1;
#endif
}
#define check_execveat_fail(fd, path, flags, errno) \
_check_execveat_fail(fd, path, flags, errno, #errno)
static int _check_execveat_fail(int fd, const char *path, int flags,
int expected_errno, const char *errno_str)
{
int rc;
errno = 0;
printf("Check failure of execveat(%d, '%s', %d) with %s... ",
fd, path?:"(null)", flags, errno_str);
rc = execveat_(fd, path, argv, envp, flags);
if (rc > 0) {
printf("[FAIL] (unexpected success from execveat(2))\n");
return 1;
}
if (errno != expected_errno) {
printf("[FAIL] (expected errno %d (%s) not %d (%s)\n",
expected_errno, strerror(expected_errno),
errno, strerror(errno));
return 1;
}
printf("[OK]\n");
return 0;
}
static int check_execveat_invoked_rc(int fd, const char *path, int flags,
int expected_rc, int expected_rc2)
{
int status;
int rc;
pid_t child;
int pathlen = path ? strlen(path) : 0;
if (pathlen > 40)
printf("Check success of execveat(%d, '%.20s...%s', %d)... ",
fd, path, (path + pathlen - 20), flags);
else
printf("Check success of execveat(%d, '%s', %d)... ",
fd, path?:"(null)", flags);
child = fork();
if (child < 0) {
printf("[FAIL] (fork() failed)\n");
return 1;
}
if (child == 0) {
/* Child: do execveat(). */
rc = execveat_(fd, path, argv, envp, flags);
printf("[FAIL]: execveat() failed, rc=%d errno=%d (%s)\n",
rc, errno, strerror(errno));
exit(1); /* should not reach here */
}
/* Parent: wait for & check child's exit status. */
rc = waitpid(child, &status, 0);
if (rc != child) {
printf("[FAIL] (waitpid(%d,...) returned %d)\n", child, rc);
return 1;
}
if (!WIFEXITED(status)) {
printf("[FAIL] (child %d did not exit cleanly, status=%08x)\n",
child, status);
return 1;
}
if ((WEXITSTATUS(status) != expected_rc) &&
(WEXITSTATUS(status) != expected_rc2)) {
printf("[FAIL] (child %d exited with %d not %d nor %d)\n",
child, WEXITSTATUS(status), expected_rc, expected_rc2);
return 1;
}
printf("[OK]\n");
return 0;
}
static int check_execveat(int fd, const char *path, int flags)
{
return check_execveat_invoked_rc(fd, path, flags, 99, 99);
}
static char *concat(const char *left, const char *right)
{
char *result = malloc(strlen(left) + strlen(right) + 1);
strcpy(result, left);
strcat(result, right);
return result;
}
static int open_or_die(const char *filename, int flags)
{
int fd = open(filename, flags);
if (fd < 0) {
printf("Failed to open '%s'; "
"check prerequisites are available\n", filename);
exit(1);
}
return fd;
}
static void exe_cp(const char *src, const char *dest)
{
int in_fd = open_or_die(src, O_RDONLY);
int out_fd = open(dest, O_RDWR|O_CREAT|O_TRUNC, 0755);
struct stat info;
fstat(in_fd, &info);
sendfile(out_fd, in_fd, NULL, info.st_size);
close(in_fd);
close(out_fd);
}
#define XX_DIR_LEN 200
static int check_execveat_pathmax(int root_dfd, const char *src, int is_script)
{
int fail = 0;
int ii, count, len;
char longname[XX_DIR_LEN + 1];
int fd;
if (*longpath == '\0') {
/* Create a filename close to PATH_MAX in length */
char *cwd = getcwd(NULL, 0);
if (!cwd) {
printf("Failed to getcwd(), errno=%d (%s)\n",
errno, strerror(errno));
return 2;
}
strcpy(longpath, cwd);
strcat(longpath, "/");
memset(longname, 'x', XX_DIR_LEN - 1);
longname[XX_DIR_LEN - 1] = '/';
longname[XX_DIR_LEN] = '\0';
count = (PATH_MAX - 3 - strlen(cwd)) / XX_DIR_LEN;
for (ii = 0; ii < count; ii++) {
strcat(longpath, longname);
mkdir(longpath, 0755);
}
len = (PATH_MAX - 3 - strlen(cwd)) - (count * XX_DIR_LEN);
if (len <= 0)
len = 1;
memset(longname, 'y', len);
longname[len] = '\0';
strcat(longpath, longname);
free(cwd);
}
exe_cp(src, longpath);
/*
* Execute as a pre-opened file descriptor, which works whether this is
* a script or not (because the interpreter sees a filename like
* "/dev/fd/20").
*/
fd = open(longpath, O_RDONLY);
if (fd > 0) {
printf("Invoke copy of '%s' via filename of length %zu:\n",
src, strlen(longpath));
fail += check_execveat(fd, "", AT_EMPTY_PATH);
} else {
printf("Failed to open length %zu filename, errno=%d (%s)\n",
strlen(longpath), errno, strerror(errno));
fail++;
}
/*
* Execute as a long pathname relative to "/". If this is a script,
* the interpreter will launch but fail to open the script because its
* name ("/dev/fd/5/xxx....") is bigger than PATH_MAX.
*
* The failure code is usually 127 (POSIX: "If a command is not found,
* the exit status shall be 127."), but some systems give 126 (POSIX:
* "If the command name is found, but it is not an executable utility,
* the exit status shall be 126."), so allow either.
*/
if (is_script)
fail += check_execveat_invoked_rc(root_dfd, longpath + 1, 0,
127, 126);
else
fail += check_execveat(root_dfd, longpath + 1, 0);
return fail;
}
static int run_tests(void)
{
int fail = 0;
char *fullname = realpath("execveat", NULL);
char *fullname_script =
|