-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9bind.c
More file actions
52 lines (45 loc) · 1.05 KB
/
Copy path9bind.c
File metadata and controls
52 lines (45 loc) · 1.05 KB
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
/* © 2008 sqweek <sqweek@gmail.com>
* See COPYING for details.
*/
#define _GNU_SOURCE //for O_PATH
#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
int
main(int argc, char **argv)
{
char *old = NULL, *new = NULL;
int mountfd;
char mountbuf[64];
struct stat stbuf;
while (*++argv) {
if (!old) {
old = *argv;
} else if (!new) {
new = *argv;
} else {
errx(1, "%s: too many arguments", *argv);
}
}
if (!old || !new) {
errx(1, "usage: 9bind old new");
}
/* Make sure mount exists, is writable, and not sticky */
mountfd = open(new, O_PATH | O_DIRECTORY | O_NOFOLLOW);
if (mountfd == -1 || fstat(mountfd, &stbuf) || faccessat(mountfd, ".", W_OK, 0)) {
err(1, "%s", new);
}
if (stbuf.st_mode & S_ISVTX) {
errx(1, "%s: refusing to bind over sticky directory", new);
}
snprintf(mountbuf, sizeof(mountbuf), "/proc/self/fd/%d", mountfd);
if (mount(old, mountbuf, NULL, MS_BIND, NULL)) {
err(1, "mount");
}
return 0;
}