summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/fchmodat2/fchmodat2_test.c
blob: e0319417124d5a1e8cc82f555085d7aaaf340bee (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later

#define _GNU_SOURCE
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syscall.h>
#include <unistd.h>

#include "../kselftest.h"

int sys_fchmodat2(int dfd, const char *filename, mode_t mode, int flags)
{
	int ret = syscall(__NR_fchmodat2, dfd, filename, mode, flags);

	return ret >= 0 ? ret : -errno;
}

int setup_testdir(void)
{
	int dfd, ret;
	char dirname[] = "/tmp/ksft-fchmodat2.XXXXXX";

	/* Make the top-level directory. */
	if (!mkdtemp(dirname))
		ksft_exit_fail_msg("%s: failed to create tmpdir\n", __func__);

	dfd = open(dirname, O_PATH | O_DIRECTORY);
	if (dfd < 0)
		ksft_exit_fail_msg("%s: failed to open tmpdir\n", __func__);

	ret = openat(dfd, "regfile", O_CREAT | O_WRONLY | O_TRUNC, 0644);
	if (ret < 0)
		ksft_exit_fail_msg("%s: failed to create file in tmpdir\n",
				__func__);
	close(ret);

	ret = symlinkat("regfile", dfd, "symlink");
	if (ret < 0)
		ksft_exit_fail_msg("%s: failed to create symlink in tmpdir\n",
				__func__);

	return dfd;
}

int expect_mode(int dfd, const char *filename, mode_t expect_mode)
{
	struct stat st;
	int ret = fstatat(dfd, filename, &st, AT_SYMLINK_NOFOLLOW);

	if (ret)
		ksft_exit_fail_msg("%s: %s: fstatat failed\n",
				__func__, filename);

	return (st.st_mode == expect_mode);
}

void test_regfile(void)
{
	int dfd, ret;

	dfd = setup_testdir();

	ret = sys_fchmodat2(dfd, "regfile", 0640, 0);

	if (ret < 0)
		ksft_exit_fail_msg("%s: fchmodat2(noflag) failed\n", __func__);

	if (!expect_mode(dfd, "regfile", 0100640))
		ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2\n",
				__func__);

	ret = sys_fchmodat2(dfd, "regfile", 0600, AT_SYMLINK_NOFOLLOW);

	if (ret < 0)
		ksft_exit_fail_msg("%s: fchmodat2(AT_SYMLINK_NOFOLLOW) failed\n",
				__func__);

	if (!expect_mode(dfd, "regfile", 0100600))
		ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
				__func__);

	ksft_test_result_pass("fchmodat2(regfile)\n");
}

void test_symlink(void)
{
	int dfd, ret;

	dfd = setup_testdir();

	ret = sys_fchmodat2(dfd, "symlink", 0640, 0);

	if (ret < 0)
		ksft_exit_fail_msg("%s: fchmodat2(noflag) failed\n", __func__);

	if (!expect_mode(dfd, "regfile", 0100640))
		ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2\n",
				__func__);

	if (!expect_mode(dfd, "symlink", 0120777))
		ksft_exit_fail_msg("%s: wrong symlink mode bits after fchmodat2\n",
				__func__);

	ret = sys_fchmodat2(dfd, "symlink", 0600, AT_SYMLINK_NOFOLLOW);

	/*
	 * On certain filesystems (xfs or btrfs), chmod operation fails. So we
	 * first check the symlink target but if the operation fails we mark the
	 * test as skipped.
	 *
	 * https://sourceware.org/legacy-ml/libc-alpha/2020-02/msg00467.html
	 */
	if (ret == 0 && !expect_mode(dfd, "symlink", 0120600))
		ksft_exit_fail_msg("%s: wrong symlink mode bits after fchmodat2 with nofollow\n",
				__func__);

	if (!expect_mode(dfd, "regfile", 0100640))
		ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
				__func__);

	if (ret != 0)
		ksft_test_result_skip("fchmodat2(symlink)\n");
	else
		ksft_test_result_pass("fchmodat2(symlink)\n");
}

#define NUM_TESTS 2

int main(int argc, char **argv)
{
	ksft_print_header();
	ksft_set_plan(NUM_TESTS);

	test_regfile();
	test_symlink();

	if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)
		ksft_exit_fail();
	else
		ksft_exit_pass();
}