This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnfs-mount-in-userns.c
317 lines (255 loc) · 6.94 KB
/
nfs-mount-in-userns.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Code picked from samples/vfs/test-fsmount.c */
// SPDX-License-Identifier: GPL-2.0-or-later
/* fd-based mount test.
*
* Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sched.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <linux/mount.h>
#include <sys/wait.h>
#define FSOPEN_CLOEXEC 0x00000001
#ifndef __NR_fsopen
#define __NR_fsopen -1
#endif
#ifndef __NR_fsmount
#define __NR_fsmount -1
#endif
#ifndef __NR_fsconfig
#define __NR_fsconfig -1
#endif
#ifndef __NR_move_mount
#define __NR_move_mount -1
#endif
static inline int fsopen(const char *fs_name, unsigned int flags)
{
return syscall(__NR_fsopen, fs_name, flags);
}
static inline int fsmount(int fsfd, unsigned int flags, unsigned int ms_flags)
{
return syscall(__NR_fsmount, fsfd, flags, ms_flags);
}
static inline int fsconfig(int fsfd, unsigned int cmd,
const char *key, const void *val, int aux)
{
return syscall(__NR_fsconfig, fsfd, cmd, key, val, aux);
}
static inline int move_mount(int from_dfd, const char *from_pathname,
int to_dfd, const char *to_pathname,
unsigned int flags)
{
return syscall(__NR_move_mount,
from_dfd, from_pathname,
to_dfd, to_pathname, flags);
}
static void check_messages(int fd)
{
char buf[4096];
int err, n;
err = errno;
for (;;) {
n = read(fd, buf, sizeof(buf));
if (n < 0)
break;
n -= 2;
switch (buf[0]) {
case 'e':
fprintf(stderr, "Error: %*.*s\n", n, n, buf + 2);
break;
case 'w':
fprintf(stderr, "Warning: %*.*s\n", n, n, buf + 2);
break;
case 'i':
fprintf(stderr, "Info: %*.*s\n", n, n, buf + 2);
break;
}
}
errno = err;
}
static __attribute__((noreturn))
void mount_error(int fd, const char *s) {
check_messages(fd);
fprintf(stderr, "%s: %m\n", s);
exit(1);
}
static int
sendfd(int sockfd, int baggagefd) {
struct msghdr msg;
struct iovec iov[1];
char c = 0;
union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(int))];
} control_un;
struct cmsghdr *cmptr;
int ret;
msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control);
cmptr = CMSG_FIRSTHDR(&msg);
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
*((int *) CMSG_DATA(cmptr)) = baggagefd;
msg.msg_name = NULL;
msg.msg_namelen = 0;
iov[0].iov_base = &c;
iov[0].iov_len = 1;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
ret = sendmsg(sockfd, &msg, 0);
if (ret == 1)
return 0;
else
return -1;
}
static int
receivefd(int sockfd, int *baggagefd) {
struct msghdr msg;
struct iovec iov[1];
ssize_t n;
char c;
union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof (int))];
} control_un;
struct cmsghdr *cmptr;
msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control);
msg.msg_name = NULL;
msg.msg_namelen = 0;
iov[0].iov_base = &c;
iov[0].iov_len = 1;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
n = recvmsg(sockfd, &msg, 0);
if (n <= 0)
return n;
if ( (cmptr = CMSG_FIRSTHDR(&msg)) != NULL && cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
assert(cmptr->cmsg_level == SOL_SOCKET);
assert(cmptr->cmsg_type == SCM_RIGHTS);
*baggagefd = *((int *) CMSG_DATA(cmptr));
} else {
*baggagefd = -1;
}
return 0;
}
int
get_sfd(char *netns_path, char *userns_path, char *mntns_path) {
int netns_fd = open(netns_path, O_RDONLY);
assert(netns_fd != -1);
int userns_fd = open(userns_path, O_RDONLY);
assert(userns_fd != -1);
int mntns_fd = open(mntns_path, O_RDONLY);
assert(mntns_fd != -1);
int sockfd[2];
int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd);
int childpid;
if ((childpid = fork()) == 0) {
// child
close(sockfd[0]);
ret = setns(netns_fd, 0);
if (ret == -1) fprintf(stderr, "Error: %s\n", strerror(errno));
assert(ret == 0);
ret = setns(mntns_fd, 0);
if (ret == -1) fprintf(stderr, "Error: %s\n", strerror(errno));
assert(ret == 0);
ret = setns(userns_fd, 0);
if (ret == -1) fprintf(stderr, "Error: %s\n", strerror(errno));
assert(ret == 0);
int sfd;
sfd = fsopen("nfs4", FSOPEN_CLOEXEC);
if (sfd == -1) fprintf(stderr, "Error: %s\n", strerror(errno));
assert(sfd != -1);
ret = sendfd(sockfd[1], sfd);
assert(ret == 0);
exit(0);
}
// parent
close(sockfd[1]);
int status;
waitpid(childpid, &status, 0);
if (WIFEXITED(status) == 0) {
fprintf(stderr, "Error: child did not terminate\n");
exit(1);
}
int fd_from_child;
if ( (status = WEXITSTATUS(status)) == 0) {
ret = receivefd(sockfd[0], &fd_from_child);
assert(ret == 0);
} else {
fprintf(stderr, "Error: child returned %d\n", status);
exit(1);
}
close(sockfd[0]);
return fd_from_child;
}
void
finish_mount(int sfd) {
int ret;
int mfd;
// mount("127.0.0.1:/tmp/nfsserver", "/mnt/nfs", "nfs", 0, "hard,vers=4.2,addr=127.0.0.1,clientaddr=127.0.0.1")
//ret = fsconfig(sfd, FSCONFIG_SET_FLAG, "ro", NULL, 0);
//if (ret == -1) mount_error(sfd, "ro");
//assert(ret == 0);
ret = fsconfig(sfd, FSCONFIG_SET_STRING, "source", "127.0.0.1:/server", 0);
if (ret == -1) mount_error(sfd, "source");
assert(ret == 0);
//ret = fsconfig(sfd, FSCONFIG_SET_FLAG, "hard", NULL, 0);
//if (ret == -1) mount_error(sfd, "hard");
//assert(ret == 0);
ret = fsconfig(sfd, FSCONFIG_SET_STRING, "vers", "4.2", 0);
if (ret == -1) mount_error(sfd, "vers");
assert(ret == 0);
//ret = fsconfig(sfd, FSCONFIG_SET_STRING, "proto", "tcp", 0);
//if (ret == -1) mount_error(sfd, "proto");
//assert(ret == 0);
//ret = fsconfig(sfd, FSCONFIG_SET_STRING, "mountvers", "3", 0);
//if (ret == -1) mount_error(sfd, "mountvers");
//assert(ret == 0);
//ret = fsconfig(sfd, FSCONFIG_SET_STRING, "mountproto", "udp", 0);
//if (ret == -1) mount_error(sfd, "mountproto");
//assert(ret == 0);
//ret = fsconfig(sfd, FSCONFIG_SET_STRING, "mountport", "56859", 0);
//if (ret == -1) mount_error(sfd, "mountport");
//assert(ret == 0);
ret = fsconfig(sfd, FSCONFIG_SET_STRING, "addr", "127.0.0.1", 0);
if (ret == -1) mount_error(sfd, "addr");
assert(ret == 0);
ret = fsconfig(sfd, FSCONFIG_SET_STRING, "clientaddr", "127.0.0.1", 0);
if (ret == -1) mount_error(sfd, "clientaddr");
assert(ret == 0);
ret = fsconfig(sfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
if (ret == -1) mount_error(sfd, "create");
assert(ret == 0);
mfd = fsmount(sfd, FSMOUNT_CLOEXEC, 0);
if (mfd == -1) fprintf(stderr, "Error: %s\n", strerror(errno));
assert(mfd >= 0);
ret = close (sfd);
assert(ret == 0);
ret = move_mount(mfd, "", AT_FDCWD, "/mnt/nfs", MOVE_MOUNT_F_EMPTY_PATH);
assert(ret == 0);
}
int
main(int argc, char **argv) {
if (argc < 4) {
fprintf(stderr, "%s /proc/PID/ns/net /proc/PID/ns/user /proc/PID/ns/mnt\n", argv[0]);
exit(EXIT_FAILURE);
}
int sfd;
sfd = get_sfd(argv[1], argv[2], argv[3]);
assert(sfd >= 0);
finish_mount(sfd);
return 0;
}