1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2018 MediaTek Inc. All Rights Reserved.
4 * Author: Eddie Horng <eddie.horng@mediatek.com>
5 *
6 * Check if an unlinked executable can run in overlayfs mount.
7 *
8 * The regression is introduced from 8db6c34f1dbc ("Introduce v3
9 * namespaced file capabilities"). in security/commoncap.c,
10 * cap_inode_getsecurity() use d_find_alias() cause unhashed dentry
11 * can't be found. The solution could use d_find_any_alias() instead
12 * of d_find_alias().
13 *
14 * Starting with kernel 4.14, this case fails, execveat shall returns EINVAL.
15 *
16 * This has been fixed by:
17 * 355139a8dba4 ("cap_inode_getsecurity: use d_find_any_alias()
18 * instead of d_find_alias()")
19 */
20
21 #define _GNU_SOURCE
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/syscall.h>
31 #include <sys/mount.h>
32 #include <fcntl.h>
33 #include "tst_test.h"
34 #include "lapi/execveat.h"
35 #include "lapi/fcntl.h"
36 #include "execveat.h"
37
38 #define TEST_APP "execveat_child"
39 #define TEST_FILE_PATH OVL_MNT"/"TEST_APP
40
41 static const char mntpoint[] = OVL_BASE_MNTPOINT;
42
do_child(void)43 static void do_child(void)
44 {
45 char *argv[2] = {TEST_FILE_PATH, NULL};
46 int fd;
47
48 SAFE_CP(TEST_APP, TEST_FILE_PATH);
49
50 fd = SAFE_OPEN(TEST_FILE_PATH, O_PATH);
51 SAFE_UNLINK(TEST_FILE_PATH);
52
53 TEST(execveat(fd, "", argv, environ, AT_EMPTY_PATH));
54 tst_res(TFAIL | TERRNO, "execveat() returned unexpected errno");
55 }
56
verify_execveat(void)57 static void verify_execveat(void)
58 {
59 pid_t pid;
60
61 pid = SAFE_FORK();
62 if (pid == 0)
63 do_child();
64 }
65
setup(void)66 static void setup(void)
67 {
68 check_execveat();
69 }
70
71 static const char *const resource_files[] = {
72 TEST_APP,
73 NULL,
74 };
75
76 static struct tst_test test = {
77 .needs_root = 1,
78 .mount_device = 1,
79 .needs_overlay = 1,
80 .mntpoint = mntpoint,
81 .forks_child = 1,
82 .child_needs_reinit = 1,
83 .setup = setup,
84 .test_all = verify_execveat,
85 .resource_files = resource_files,
86 .tags = (const struct tst_tag[]) {
87 {"linux-git", "8db6c34f1dbc"},
88 {"linux-git", "355139a8dba4"},
89 {}
90 }
91 };
92