aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/memfd_create/memfd_create01.c
blob: bdc0c8512eaa59414e03c52865b6f2b0ee495311 (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
264
265
266
267
268
269
270
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) Linux Test Project, 2017-2020
 * Copyright (C) 2017  Red Hat, Inc.
 */

/*
 *  Based on Linux/tools/testing/selftests/memfd/memfd_test.c
 *  by David Herrmann <dh.herrmann@gmail.com>
 *
 *  24/02/2017   Port to LTP    <jracek@redhat.com>
 */

#define _GNU_SOURCE

#include <errno.h>
#include "tst_test.h"
#include "memfd_create_common.h"

/*
 * Do few basic sealing tests to see whether setting/retrieving seals works.
 */
static void test_basic(int fd)
{
	/* add basic seals */
	CHECK_MFD_HAS_SEALS(fd, 0);
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_SHRINK | F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_SHRINK | F_SEAL_WRITE);

	/* add them again */
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_SHRINK | F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_SHRINK | F_SEAL_WRITE);

	/* add more seals and seal against sealing */
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_GROW | F_SEAL_SEAL);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_SHRINK | F_SEAL_GROW |
			F_SEAL_WRITE | F_SEAL_SEAL);

	/* verify that sealing no longer works */
	CHECK_MFD_FAIL_ADD_SEALS(fd, F_SEAL_GROW);
	CHECK_MFD_FAIL_ADD_SEALS(fd, 0);
}

/*
 * Verify that no sealing is possible when memfd is created without
 * MFD_ALLOW_SEALING flag.
 */
static void test_no_sealing_without_flag(int fd)
{
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_SEAL);
	CHECK_MFD_FAIL_ADD_SEALS(fd,
		F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_SEAL);
}

/*
 * Test SEAL_WRITE
 * Test whether SEAL_WRITE actually prevents modifications.
 */
static void test_seal_write(int fd)
{
	CHECK_MFD_HAS_SEALS(fd, 0);
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_WRITE);

	CHECK_MFD_READABLE(fd);
	CHECK_MFD_NON_WRITEABLE(fd);
	CHECK_MFD_SHRINKABLE(fd);
	CHECK_MFD_GROWABLE(fd);
	CHECK_MFD_NON_GROWABLE_BY_WRITE(fd);
}

/*
 * Test SEAL_SHRINK
 * Test whether SEAL_SHRINK actually prevents shrinking
 */
static void test_seal_shrink(int fd)
{
	CHECK_MFD_HAS_SEALS(fd, 0);
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_SHRINK);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_SHRINK);

	CHECK_MFD_READABLE(fd);
	CHECK_MFD_WRITEABLE(fd);
	CHECK_MFD_NON_SHRINKABLE(fd);
	CHECK_MFD_GROWABLE(fd);
	CHECK_MFD_GROWABLE_BY_WRITE(fd);
}

/*
 * Test SEAL_GROW
 * Test whether SEAL_GROW actually prevents growing
 */
static void test_seal_grow(int fd)
{
	CHECK_MFD_HAS_SEALS(fd, 0);
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_GROW);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_GROW);

	CHECK_MFD_READABLE(fd);
	CHECK_MFD_WRITEABLE(fd);
	CHECK_MFD_SHRINKABLE(fd);
	CHECK_MFD_NON_GROWABLE(fd);
	CHECK_MFD_NON_GROWABLE_BY_WRITE(fd);
}

/*
 * Test SEAL_SHRINK | SEAL_GROW
 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
 */
static void test_seal_resize(int fd)
{
	CHECK_MFD_HAS_SEALS(fd, 0);
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_SHRINK | F_SEAL_GROW);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_SHRINK | F_SEAL_GROW);

	CHECK_MFD_READABLE(fd);
	CHECK_MFD_WRITEABLE(fd);
	CHECK_MFD_NON_SHRINKABLE(fd);
	CHECK_MFD_NON_GROWABLE(fd);
	CHECK_MFD_NON_GROWABLE_BY_WRITE(fd);
}

/*
 * Test sharing via dup()
 * Test that seals are shared between dupped FDs and they're all equal.
 */
static void test_share_dup(int fd)
{
	int fd2;

	CHECK_MFD_HAS_SEALS(fd, 0);

	fd2 = SAFE_DUP(fd);
	CHECK_MFD_HAS_SEALS(fd2, 0);

	CHECK_MFD_ADD_SEALS(fd, F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd2, F_SEAL_WRITE);

	CHECK_MFD_ADD_SEALS(fd2, F_SEAL_SHRINK);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
	CHECK_MFD_HAS_SEALS(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);

	CHECK_MFD_ADD_SEALS(fd, F_SEAL_SEAL);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
	CHECK_MFD_HAS_SEALS(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);

	CHECK_MFD_FAIL_ADD_SEALS(fd, F_SEAL_GROW);
	CHECK_MFD_FAIL_ADD_SEALS(fd2, F_SEAL_GROW);
	CHECK_MFD_FAIL_ADD_SEALS(fd, F_SEAL_SEAL);
	CHECK_MFD_FAIL_ADD_SEALS(fd2, F_SEAL_SEAL);

	SAFE_CLOSE(fd2);

	CHECK_MFD_FAIL_ADD_SEALS(fd, F_SEAL_GROW);
}

/*
 * Test sealing with active mmap()s
 * Modifying seals is only allowed if no other mmap() refs exist.
 */
static void test_share_mmap(int fd)
{
	void *p;

	CHECK_MFD_HAS_SEALS(fd, 0);

	/* shared/writable ref prevents sealing WRITE, but allows others */
	p = SAFE_MMAP(NULL, MFD_DEF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
		fd, 0);

	CHECK_MFD_FAIL_ADD_SEALS(fd, F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd, 0);
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_SHRINK);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_SHRINK);
	SAFE_MUNMAP(p, MFD_DEF_SIZE);

	/* readable ref allows sealing */
	p = SAFE_MMAP(NULL, MFD_DEF_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
	SAFE_MUNMAP(p, MFD_DEF_SIZE);
}

/*
 * Test sealing with open(/proc/self/fd/%d)
 * Via /proc we can get access to a separate file-context for the same memfd.
 * This is *not* like dup(), but like a real separate open(). Make sure the
 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
 */
static void test_share_open(int fd)
{
	int fd2;

	CHECK_MFD_HAS_SEALS(fd, 0);

	fd2 = CHECK_MFD_OPEN(fd, O_RDWR, 0);
	CHECK_MFD_ADD_SEALS(fd, F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_WRITE);
	CHECK_MFD_HAS_SEALS(fd2, F_SEAL_WRITE);

	CHECK_MFD_ADD_SEALS(fd2, F_SEAL_SHRINK);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
	CHECK_MFD_HAS_SEALS(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);

	SAFE_CLOSE(fd);
	fd = CHECK_MFD_OPEN(fd2, O_RDONLY, 0);

	CHECK_MFD_FAIL_ADD_SEALS(fd, F_SEAL_SEAL);
	CHECK_MFD_HAS_SEALS(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
	CHECK_MFD_HAS_SEALS(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);

	SAFE_CLOSE(fd2);
}


static const struct tcase {
	int flags;
	void (*func)(int fd);
	const char *desc;
} tcases[] = {
	{MFD_ALLOW_SEALING, &test_basic, "Basic tests + set/get seals"},
	{0,                 &test_no_sealing_without_flag, "Disabled sealing"},

	{MFD_ALLOW_SEALING, &test_seal_write, "Write seal"},
	{MFD_ALLOW_SEALING, &test_seal_shrink, "Shrink seal"},
	{MFD_ALLOW_SEALING, &test_seal_grow, "Grow seal"},
	{MFD_ALLOW_SEALING, &test_seal_resize, "Resize seal"},

	{MFD_ALLOW_SEALING, &test_share_dup, "Seals shared for dup"},
	{MFD_ALLOW_SEALING, &test_share_mmap, "Seals shared for mmap"},
	{MFD_ALLOW_SEALING, &test_share_open, "Seals shared for open"},
};

static void verify_memfd_create(unsigned int n)
{
	int fd;
	const struct tcase *tc;

	tc = &tcases[n];

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

	fd = CHECK_MFD_NEW("ltp_memfd_create01", MFD_DEF_SIZE, tc->flags);

	tc->func(fd);

	SAFE_CLOSE(fd);
}

static void setup(void)
{
	/*
	 * For now, all tests in this file require MFD_ALLOW_SEALING flag
	 * to be implemented, even though that flag isn't always set when
	 * memfd is created. So don't check anything else and TCONF right away
	 * is this flag is missing.
	 */
	if (!MFD_FLAGS_AVAILABLE(MFD_ALLOW_SEALING)) {
		tst_brk(TCONF | TERRNO,
			"memfd_create(%u) not implemented", MFD_ALLOW_SEALING);
	}
}

static struct tst_test test = {
	.test = verify_memfd_create,
	.tcnt = ARRAY_SIZE(tcases),
	.setup = setup,
};