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