aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/mount/mount03.c
blob: 98d5933b796e4a7c8daa143287a4cf721fd1461f (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
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) Linux Test Project, 2022
 * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
 */

/*\
 * [Description]
 *
 * Check mount(2) system call with various flags.
 *
 * Verify that mount(2) syscall passes for each flag setting and validate
 * the flags:
 *
 * - MS_RDONLY - mount read-only
 * - MS_NODEV - disallow access to device special files
 * - MS_NOEXEC - disallow program execution
 * - MS_REMOUNT - alter flags of a mounted FS
 * - MS_NOSUID - ignore suid and sgid bits
 * - MS_NOATIME - do not update access times
 * - MS_NODIRATIME - only update access_time for directory instead of all types
 * - MS_STRICTATIME - always update access times
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/vfs.h>
#include <pwd.h>
#include "tst_test.h"
#include "lapi/mount.h"

#define MNTPOINT	"mntpoint"
#define TESTBIN	"mount03_suid_child"
#define BIN_PATH	MNTPOINT "/" TESTBIN
#define TEST_STR "abcdefghijklmnopqrstuvwxyz"
#define FILE_MODE	0644
#define SUID_MODE	(0511 | S_ISUID)

#define CHECK_ENOENT(x) ((x) == -1 && errno == ENOENT)

static int otfd;
static char file[PATH_MAX];
static char dir[PATH_MAX];
static uid_t nobody_uid;
static gid_t nobody_gid;

static void test_rdonly(void)
{
	snprintf(file, PATH_MAX, "%s/rdonly", MNTPOINT);
	TST_EXP_FAIL(otfd = open(file, O_CREAT | O_RDWR, 0700), EROFS);
}

static void test_nodev(void)
{
	snprintf(file, PATH_MAX, "%s/nodev", MNTPOINT);
	SAFE_MKNOD(file, S_IFBLK | 0777, 0);
	TST_EXP_FAIL(otfd = open(file, O_RDWR, 0700), EACCES);
	SAFE_UNLINK(file);
}

static void test_noexec(void)
{
	snprintf(file, PATH_MAX, "%s/noexec", MNTPOINT);
	otfd = SAFE_OPEN(file, O_CREAT | O_RDWR, 0700);
	TST_EXP_FAIL(execlp(file, basename(file), NULL), EACCES);
}

static void test_remount(void)
{
	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, MS_REMOUNT, NULL);
	snprintf(file, PATH_MAX, "%s/remount", MNTPOINT);
	TST_EXP_FD(otfd = open(file, O_CREAT | O_RDWR, 0700));
}

static void test_nosuid(void)
{
	int ret;
	struct stat st;

	if (!SAFE_FORK()) {
		SAFE_CP(TESTBIN, BIN_PATH);

		ret = TST_RETRY_FN_EXP_BACKOFF(access(BIN_PATH, F_OK), !CHECK_ENOENT, 15);
		if (CHECK_ENOENT(ret))
			tst_brk(TBROK, "Timeout, %s does not exist", BIN_PATH);

		SAFE_STAT(BIN_PATH, &st);
		if (st.st_mode != SUID_MODE)
			SAFE_CHMOD(BIN_PATH, SUID_MODE);

		SAFE_SETREUID(nobody_uid, nobody_uid);
		SAFE_EXECL(BIN_PATH, BIN_PATH, NULL);
		tst_brk(TFAIL | TTERRNO, "Failed to execute %s", BIN_PATH);
	}

	tst_reap_children();
}

static void test_file_dir_noatime(int update_fatime, int update_datime)
{
	time_t atime, dir_atime;
	struct stat st, dir_st;
	char readbuf[20];
	DIR *test_dir;

	snprintf(file, PATH_MAX, "%s/noatime", MNTPOINT);
	TST_EXP_FD_SILENT(otfd = open(file, O_CREAT | O_RDWR, 0700));

	snprintf(dir, PATH_MAX, "%s/nodiratime", MNTPOINT);
	if (access(dir, F_OK) == -1 && errno == ENOENT)
		SAFE_MKDIR(dir, 0700);

	SAFE_WRITE(1, otfd, TEST_STR, strlen(TEST_STR));
	SAFE_FSTAT(otfd, &st);
	atime = st.st_atime;

	test_dir = SAFE_OPENDIR(dir);
	SAFE_STAT(dir, &dir_st);
	SAFE_READDIR(test_dir);
	SAFE_CLOSEDIR(test_dir);
	dir_atime = dir_st.st_atime;

	usleep(1001000);

	SAFE_READ(0, otfd, readbuf, sizeof(readbuf));
	SAFE_FSTAT(otfd, &st);

	test_dir = SAFE_OPENDIR(dir);
	SAFE_READDIR(test_dir);
	SAFE_CLOSEDIR(test_dir);
	SAFE_STAT(dir, &dir_st);

	if (update_fatime) {
		if (st.st_atime > atime)
			tst_res(TPASS, "st.st_atime(%ld) > atime(%ld)",
					st.st_atime, atime);
		else
			tst_res(TFAIL, "st.st_atime(%ld) < atime(%ld)",
					st.st_atime, atime);
	} else {
		TST_EXP_EQ_LI(st.st_atime, atime);
	}

	if (update_datime) {
		if (dir_st.st_atime > dir_atime)
			tst_res(TPASS, "dir_st.st_atime(%ld) > dir_atime(%ld)",
					dir_st.st_atime, dir_atime);
		else
			tst_res(TFAIL, "dir_st.st_atime(%ld) < dir_atime(%ld)",
					dir_st.st_atime, dir_atime);
	} else {
		TST_EXP_EQ_LI(dir_st.st_atime, dir_atime);
	}
}

static void test_noatime(void)
{
	test_file_dir_noatime(0, 0);
}

static void test_nodiratime(void)
{
	test_file_dir_noatime(1, 0);
}

static void test_strictatime(void)
{
	test_file_dir_noatime(1, 1);
}

#define FLAG_DESC(x) .flag = x, .flag2 = x, .desc = #x
#define FLAG_DESC2(x) .flag2 = x, .desc = #x
static struct tcase {
	unsigned int flag;
	unsigned int flag2;
	char *desc;
	void (*test)(void);
} tcases[] = {
	{FLAG_DESC(MS_RDONLY), test_rdonly},
	{FLAG_DESC(MS_NODEV), test_nodev},
	{FLAG_DESC(MS_NOEXEC), test_noexec},
	{MS_RDONLY, FLAG_DESC2(MS_REMOUNT), test_remount},
	{FLAG_DESC(MS_NOSUID), test_nosuid},
	{FLAG_DESC(MS_NOATIME), test_noatime},
	{FLAG_DESC(MS_NODIRATIME), test_nodiratime},
	{FLAG_DESC(MS_STRICTATIME), test_strictatime}
};

static void setup(void)
{
	struct passwd *ltpuser = SAFE_GETPWNAM("nobody");

	nobody_uid = ltpuser->pw_uid;
	nobody_gid = ltpuser->pw_gid;
}

static void cleanup(void)
{
	if (otfd >= 0)
		SAFE_CLOSE(otfd);

	if (tst_is_mounted(MNTPOINT))
		SAFE_UMOUNT(MNTPOINT);
}


static void run(unsigned int n)
{
	struct tcase *tc = &tcases[n];
	struct statfs stfs;

	tst_res(TINFO, "Testing flag %s", tc->desc);

	TST_EXP_PASS_SILENT(mount(tst_device->dev, MNTPOINT, tst_device->fs_type,
		   tc->flag, NULL));
	if (!TST_PASS)
		return;

	if (tc->test)
		tc->test();

	SAFE_STATFS(MNTPOINT, &stfs);
	if (tc->flag == MS_STRICTATIME) {
		if (stfs.f_flags & (MS_NOATIME | MS_RELATIME))
			tst_res(TFAIL, "statfs() gets the incorrect mount flag");
		else
			tst_res(TPASS, "statfs() gets the correct mount flag");
		cleanup();
		return;
	}

	if (stfs.f_flags & tc->flag2)
		tst_res(TPASS, "statfs() gets the correct mount flag");
	else
		tst_res(TFAIL, "statfs() gets the incorrect mount flag");

	cleanup();
}

static struct tst_test test = {
	.tcnt = ARRAY_SIZE(tcases),
	.test = run,
	.setup = setup,
	.cleanup = cleanup,
	.needs_root = 1,
	.format_device = 1,
	.resource_files = (const char *const[]) {
		TESTBIN,
		NULL,
	},
	.forks_child = 1,
	.child_needs_reinit = 1,
	.mntpoint = MNTPOINT,
	.all_filesystems = 1,
	.skip_filesystems = (const char *const []){
		"exfat",
		"vfat",
		"ntfs",
		NULL
	},
};