forked from aristanetworks/bst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpersist.c
283 lines (237 loc) · 6.02 KB
/
unpersist.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
/* Copyright © 2020 Arista Networks, Inc. All rights reserved.
*
* Use of this source code is governed by the MIT license that can be found
* in the LICENSE file.
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include "capable.h"
#include "ns.h"
#ifndef NS_GET_USERNS
# define NSIO 0xb7
# define NS_GET_NSTYPE _IO(NSIO, 0x3)
#endif
static gid_t *get_groups(size_t *ngroups)
{
static gid_t groups[NGROUPS_MAX];
static size_t len;
if (len == 0) {
int rc = getgroups(NGROUPS_MAX, groups);
if (rc == -1) {
return NULL;
}
len = (size_t) rc;
}
*ngroups = len;
return groups;
}
static int checkperm(const struct stat *stat)
{
if (stat->st_mode & 0002) {
return 1;
}
if (stat->st_uid == getuid() && stat->st_mode & 0200) {
return 1;
}
if (!(stat->st_mode & 0020)) {
return 0;
}
if (stat->st_gid == getgid()) {
return 1;
}
size_t ngroups;
gid_t *groups = get_groups(&ngroups);
if (groups == NULL) {
return 0;
}
for (size_t i = 0; i < ngroups; ++i) {
if (stat->st_gid == groups[i]) {
return 1;
}
}
return 0;
}
enum {
UNPERSIST_KEEPFILE = 1,
};
static int unpersistat(int dirfd, const char *pathname, int flags)
{
/* We are a privileged binary, with the ability to unmount arbitrary files.
We only allow ourselves to unmount something that fulfills two conditions:
1. The current uid or gids must have write permissions over the parent
directory.
2. The mount is an nsfs mount.
Everything else results from an access violation on our part. */
int fd = openat(dirfd, pathname, O_RDONLY | O_NOFOLLOW);
if (fd == -1) {
goto error;
}
/* Check if the file is a nsfs mount. */
if (ioctl(fd, NS_GET_NSTYPE) == -1) {
errno = EINVAL;
goto error;
}
char selfpath[PATH_MAX];
snprintf(selfpath, PATH_MAX, "/proc/self/fd/%d", fd);
struct stat stat;
if (dirfd == AT_FDCWD) {
char resolved[PATH_MAX];
if (readlink(selfpath, resolved, sizeof (resolved)) == -1) {
goto error;
}
int dirfd = open(dirname(resolved), O_PATH | O_DIRECTORY);
if (dirfd == -1) {
goto error;
}
int rc = fstat(dirfd, &stat);
close(dirfd);
if (rc == -1) {
goto error;
}
} else if (fstat(dirfd, &stat) == -1) {
goto error;
}
if (!capable(BST_CAP_DAC_OVERRIDE) && !checkperm(&stat)) {
errno = EACCES;
goto error;
}
/* This is subtle -- someone could race against us to swap out the mount
for a symlink to some arbitrary mount between the moment we open the
nsfs file and validate it, and the moment we call umount. Since we can't
trust the path itself, we have to rely on the kernel magic link resolution
to do this for us by unmounting /proc/self/fd/<fd>. */
make_capable(BST_CAP_SYS_ADMIN);
if (umount2(selfpath, MNT_DETACH) == -1) {
goto error;
}
reset_capabilities();
/* The file descriptor is now useless since it refers to our now-defunct
nsfs file. We have to use the original path for removal, but it's fine,
normal access rules apply here. */
if (!(flags & UNPERSIST_KEEPFILE) && unlinkat(dirfd, pathname, 0) == -1) {
goto error;
}
return 0;
error:
if (fd != -1) {
close(fd);
}
return -1;
}
static int usage(int error, char *argv0)
{
FILE *out = error ? stderr : stdout;
fprintf(out, "usage: %s [options] <path> [path...]\n", argv0);
fprintf(out, "\n");
fprintf(out, "Unpersist specified namespace files, or all namespace files\n");
fprintf(out, "in specified directories.\n");
fprintf(out, "\n");
fprintf(out, "Options:\n");
fprintf(out, "\t-h, --help: print this message.\n");
fprintf(out, "\t--no-unlink: do not attempt to remove nsfs mountpoints.\n");
return error ? 2 : 0;
}
enum {
OPTION_NO_UNLINK = 128,
};
/* the argv0 prefix is a bit spammy, so override that */
#define warn(Fmt, ...) fprintf(stderr, Fmt ": %s\n", __VA_ARGS__, strerror(errno))
#define warnx(Fmt, ...) fprintf(stderr, Fmt "\n", __VA_ARGS__)
int main(int argc, char *argv[])
{
static struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "no-unlink", no_argument, NULL, OPTION_NO_UNLINK },
{ NULL, 0, NULL, 0 }
};
static struct {
int unpersistat_flags;
} settings;
init_capabilities();
int error = 0;
int c;
while ((c = getopt_long(argc, argv, "h", options, NULL)) != -1) {
switch (c) {
case 0:
break;
case OPTION_NO_UNLINK:
settings.unpersistat_flags |= UNPERSIST_KEEPFILE;
break;
case '?':
error = 1;
/* fallthrough */
case 'h':
return usage(error, argv[0]);
default:
for (int i = 0; options[i].name != NULL; i++) {
if (options[i].val == c) {
if (options[i].flag != NULL) {
*options[i].flag = c;
}
break;
}
}
}
}
if (argc - optind < 1) {
return usage(true, argv[0]);
}
for (int arg = optind; arg < argc; ++arg) {
char *name = argv[arg];
int dirfd = open(name, O_PATH | O_DIRECTORY);
if (dirfd == -1) {
if (errno == ENOTDIR) {
/* This is a normal filename -- treat it as if we got
passed an nsfs filename. */
if (unpersistat(AT_FDCWD, name, settings.unpersistat_flags) == -1) {
switch (errno) {
case EINVAL:
warnx("%s: not an nsfs file", name);
break;
case EBUSY:
warn("%s: could not unlink mountpoint", name);
break;
default:
warn("%s", name);
break;
}
error = 1;
}
continue;
}
warn("open \"%s\"", name);
}
for (enum nstype ns = 0; ns < MAX_NS; ++ns) {
if (unpersistat(dirfd, ns_name(ns), settings.unpersistat_flags) == -1) {
switch (errno) {
case ENOENT:
continue;
case EINVAL:
warnx("%s/%s: not an nsfs file", name, ns_name(ns));
break;
case EBUSY:
warn("%s/%s: could not unlink mountpoint", name, ns_name(ns));
break;
default:
warn("%s/%s", name, ns_name(ns));
break;
}
error = 1;
}
}
close(dirfd);
}
return error;
}