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