3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 // secure-mount <general mount(8) options> [--secure] [--chroot <dir>]
20 // [--mtab <mtabfile>] [--fstab <fstabfile>]
22 // Executes mount-operations in the given chroot-dir: it assumes sources in
23 // the current root-dir while destinations are expected in the chroot
24 // environment. When '--secure' is given, the destination must not contain
33 #include "pathconfig.h"
43 #include <sys/mount.h>
45 #include <sys/types.h>
53 #define MNTPOINT "/etc"
75 #define OPTION_BIND 1024
76 #define OPTION_MOVE 1025
77 #define OPTION_MTAB 1026
78 #define OPTION_FSTAB 1027
79 #define OPTION_CHROOT 1028
80 #define OPTION_SECURE 1029
81 #define OPTION_RBIND 1030
83 #define XFLAG_NOAUTO 0x01
85 static struct option const
87 { "help", no_argument, 0, 'h' },
88 { "version", no_argument, 0, 'v' },
89 { "bind", no_argument, 0, OPTION_BIND },
90 { "move", no_argument, 0, OPTION_MOVE },
91 { "mtab", required_argument, 0, OPTION_MTAB },
92 { "fstab", required_argument, 0, OPTION_FSTAB },
93 { "chroot", required_argument, 0, OPTION_CHROOT },
94 { "secure", no_argument, 0, OPTION_SECURE },
95 { "rbind", no_argument, 0, OPTION_RBIND },
100 # define MS_REC 0x4000
103 static struct FstabOption {
104 char const * const opt;
105 unsigned long const flag;
106 unsigned long const mask;
107 unsigned long const xflag;
109 } const FSTAB_OPTIONS[] = {
110 { "defaults", 0, (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|
111 MS_SYNCHRONOUS), 0, false },
112 { "rbind", MS_BIND|MS_REC, MS_BIND|MS_REC, 0, false },
113 { "bind", MS_BIND, MS_BIND, 0, false },
114 { "move", MS_MOVE, MS_MOVE, 0, false },
115 { "async", 0, MS_SYNCHRONOUS, 0, false },
116 { "sync", MS_SYNCHRONOUS, MS_SYNCHRONOUS, 0, false },
117 { "atime", 0, MS_NOATIME, 0, false },
118 { "noatime", MS_NOATIME, MS_NOATIME, 0, false },
119 { "dev", 0, MS_NODEV, 0, false },
120 { "nodev", MS_NODEV, MS_NODEV, 0, false },
121 { "exec", 0, MS_NOEXEC, 0, false },
122 { "noexec", MS_NOEXEC, MS_NOEXEC, 0, false },
123 { "suid", 0, MS_NOSUID, 0, false },
124 { "nosuid", MS_NOSUID, MS_NOSUID, 0, false },
125 { "ro", MS_RDONLY, MS_RDONLY, 0, false },
126 { "rw", 0, MS_RDONLY, 0, false },
128 { "remount", MS_REMOUNT, MS_REMOUNT, 0, false },
129 { "users", MS_NOEXEC|MS_NOSUID|MS_NODEV,
130 MS_NOEXEC|MS_NOSUID|MS_NODEV, 0, false },
131 { "mandlock", MS_MANDLOCK, MS_MANDLOCK, 0, false },
132 { "nodiratime", MS_NODIRATIME, MS_NODIRATIME, 0, false },
134 { "dirsync", MS_DIRSYNC, MS_DIRSYNC, 0, false },
136 { "_netdev", 0, 0, 0, false },
137 { "auto", 0, 0, 0, false },
138 { "noauto", 0, 0, XFLAG_NOAUTO, false },
139 { "user", 0, 0, 0, false },
140 { "nouser", 0, 0, 0, false },
144 showHelp(int fd, char const *cmd, int res)
146 VSERVER_DECLARE_CMD(cmd);
148 WRITE_MSG(fd, "Usage: ");
151 " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [-n]\n"
152 " [--mtab <filename>] [--fstab <filename>] [--chroot <dirname>] \n"
153 " [--secure] -a|([-o <options>] [--] <src> <dst>)\n\n"
154 "Executes mount-operations in the given chroot-dir: it assumes sources in the\n"
155 "current root-dir while destinations are expected in the chroot environment.\n"
156 "When '--secure' is given, the destination must not contain symlinks.\n\n"
157 "For non-trivial mount-operations it uses the external 'mount' program which\n"
158 "can be overridden by the $MOUNT environment variable.\n\n"
159 "Please report bugs to " PACKAGE_BUGREPORT "\n");
168 "secure-mount " VERSION " -- secure mounting of directories\n"
169 "This program is part of " PACKAGE_STRING "\n\n"
170 "Copyright (C) 2003 Enrico Scholz\n"
171 VERSION_COPYRIGHT_DISCLAIMER);
176 isSameObject(struct stat const *lhs,
177 struct stat const *rhs)
179 return (lhs->st_dev==rhs->st_dev &&
180 lhs->st_ino==rhs->st_ino);
184 chdirSecure(char const *dir)
186 char tmp[strlen(dir)+1], *ptr;
190 cur = strtok_r(tmp, "/", &ptr);
192 struct stat pre_stat, post_stat;
194 if (lstat(cur, &pre_stat)==-1) return -1;
196 if (!S_ISDIR(pre_stat.st_mode)) {
200 if (S_ISLNK(pre_stat.st_mode)) {
205 if (chdir(cur)==-1) return -1;
206 if (stat(".", &post_stat)==-1) return -1;
208 if (!isSameObject(&pre_stat, &post_stat)) {
211 WRITE_MSG(2, "Possible symlink race ATTACK at '");
212 WRITE_STR(2, getcwd(dir, sizeof(dir)));
219 cur = strtok_r(0, "/", &ptr);
226 verifyPosition(char const *mntpoint, char const *dir1, char const *dir2)
228 struct stat pre_stat, post_stat;
230 if (stat(mntpoint, &pre_stat)==-1) return -1;
231 if (chroot(dir1)==-1 || chdir(dir2)==-1) return -1;
232 if (stat(".", &post_stat)==-1) return -1;
234 if (!isSameObject(&pre_stat, &post_stat)) {
237 WRITE_MSG(2, "Possible symlink race ATTACK at '");
238 WRITE_STR(2, getcwd(dir, sizeof(dir)));
239 WRITE_MSG(2, "' within '");
253 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
258 writeX(int fd, void const *buf, size_t len)
260 if ((size_t)(write(fd, buf, len))!=len) return -1;
265 writeStrX(int fd, char const *str)
267 return writeX(fd, str, strlen(str));
271 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
275 assert(opt->mtab!=0);
277 if (opt->rootdir!=0 &&
278 chroot(opt->rootdir)==-1) {
279 perror("secure-mount: chroot()");
283 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
285 if (fd==-1) perror("secure-mount: open(<mtab>)");
287 if (fchroot(opt->cur_rootdir_fd)==-1) {
288 perror("secure-mount: fchroot()");
292 if (fd==-1) goto err0;
294 if (flock(fd, LOCK_EX)==-1) {
295 perror("secure-mount: flock()");
300 if (writeStrX(fd, mnt->src)==-1 ||
301 writeStrX(fd, " ")==-1 ||
302 writeStrX(fd, mnt->dst)==-1 ||
303 writeStrX(fd, " ")==-1 ||
304 writeStrX(fd, mnt->type ? mnt->type : "none")==-1 ||
305 writeStrX(fd, " ")==-1 ||
306 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
307 writeStrX(fd, " 0 0\n")==-1) {
308 perror("secure-mount: write()");
319 callExternalMount(struct MountInfo const *mnt)
321 char const * argv[10];
325 char const * mount_prog = getenv("MOUNT");
327 if (mount_prog==0) mount_prog = MOUNT_PROG;
329 argv[idx++] = mount_prog;
331 if (mnt->flag & MS_BIND) argv[idx++] = "--bind";
332 else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
335 strcmp(mnt->data, "defaults")!=0) {
337 argv[idx++] = mnt->data;
342 argv[idx++] = mnt->type;
345 argv[idx++] = mnt->src;
351 perror("secure-mount: fork()");
356 execv(mount_prog, const_cast(char **)(argv));
357 PERROR_Q("secure-mount: execv", mount_prog);
361 if (wait4(pid, &status, 0, 0)==-1) {
362 perror("secure-mount: wait4()");
366 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
370 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
372 char const *dir = mnt->dst;
376 if (opt->rootdir!=0) {
377 if (chdir(opt->rootdir)==-1) {
378 PERROR_Q("secure-mount: chdir", opt->rootdir);
382 while (*dir=='/') ++dir;
385 if (opt->is_secure) {
386 if (chdirSecure(dir)==-1) {
387 PERROR_Q("secure-mount: chdirSecure", dir);
394 PERROR_Q("secure-mount: chdir", dir);
399 if (mnt->flag & (MS_BIND|MS_MOVE)) {
400 if (mount(mnt->src, ".",
401 mnt->type ? mnt->type : "",
402 mnt->flag, mnt->data)==-1) {
403 perror("secure-mount: mount()");
408 if (!callExternalMount(mnt)) return false;
411 // Check if directories were moved between the chdirSecure() and mount(2)
412 if ((mnt->flag&MS_BIND) && opt->rootdir!=0 &&
413 (verifyPosition(mnt->src, opt->rootdir, mnt->dst)==-1 ||
414 fchroot(opt->cur_rootdir_fd)==-1)) {
415 perror("secure-mount: verifyPosition/fchroot");
416 // TODO: what is with unmounting?
420 if (!opt->ignore_mtab &&
421 updateMtab(mnt, opt)==-1) {
422 WRITE_MSG(2, "Failed to update mtab-file\n");
429 static struct FstabOption const *
430 searchOption(char const *opt, size_t len)
432 struct FstabOption const * i;
433 for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
434 if (strncmp(i->opt, opt, len)==0) return i;
440 transformOptionList(struct MountInfo *info)
442 char const * ptr = info->data;
445 char const * pos = strchr(ptr, ',');
446 struct FstabOption const * opt;
448 if (pos==0) pos = ptr+strlen(ptr);
449 opt = searchOption(ptr, pos-ptr);
452 info->flag &= ~opt->mask;
453 info->flag |= opt->flag;
454 info->xflag |= opt->xflag;
462 } while (*ptr!='\0');
467 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
468 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
469 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
471 while (isspace(*PTR)) ++PTR
473 static enum {prDOIT, prFAIL, prIGNORE}
474 parseFstabLine(struct MountInfo *info, char *buf)
476 while (isspace(*buf)) ++buf;
477 if (*buf=='#' || *buf=='\0') return prIGNORE;
480 MOVE_TO_NEXT_FIELD(buf, false);
482 MOVE_TO_NEXT_FIELD(buf, false);
484 MOVE_TO_NEXT_FIELD(buf, false);
486 MOVE_TO_NEXT_FIELD(buf, true);
488 if (strcmp(info->type, "swap")==0) return prIGNORE;
489 if (strcmp(info->type, "none")==0) info->type = 0;
493 if (!transformOptionList(info)) return prFAIL;
494 if (info->xflag & XFLAG_NOAUTO) return prIGNORE;
499 #undef MOVE_TO_NEXT_FIELD
502 mountFstab(struct Options const *opt)
508 assert(opt->fstab!=0);
509 fd = open(opt->fstab, O_RDONLY);
511 perror("secure-mount: open(<fstab>)");
515 len = lseek(fd, 0, SEEK_END);
517 lseek(fd, 0, SEEK_SET)==-1) {
518 perror("secure-mount: lseek(<fstab>)");
526 if (read(fd, buf, len+1)!=len) {
527 perror("secure-mount: read()");
530 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
534 ptr = strtok_r(buf, "\n", &ptrptr);
536 struct MountInfo mnt;
537 char * new_ptr = strtok_r(0, "\n", &ptrptr);
539 switch (parseFstabLine(&mnt, ptr)) {
541 WRITE_MSG(2, "Failed to parse fstab-line beginning with '");
546 case prIGNORE : break;
549 if (!mountSingle(&mnt, opt)) {
550 WRITE_MSG(2, "Failed to mount fstab-line beginning with '");
569 int main(int argc, char *argv[])
571 struct MountInfo mnt = {
580 struct Options opt = {
582 .fstab = "/etc/fstab",
584 .ignore_mtab = false,
590 opt.cur_rootdir_fd = open("/", O_RDONLY|O_DIRECTORY);
592 if (opt.cur_rootdir_fd==-1) {
593 perror("secure-mount: open(\"/\")");
598 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
602 case 'h' : showHelp(1, argv[0], 0);
603 case 'v' : showVersion();
604 case 't' : mnt.type = optarg; break;
605 case 'n' : opt.ignore_mtab = true; break;
606 case 'a' : opt.mount_all = true; break;
607 case 'o' : mnt.data = optarg; break;
608 case OPTION_RBIND : mnt.flag |= MS_REC; /*@fallthrough@*/
609 case OPTION_BIND : mnt.flag |= MS_BIND; break;
610 case OPTION_MOVE : mnt.flag |= MS_MOVE; break;
611 case OPTION_MTAB : opt.mtab = optarg; break;
612 case OPTION_FSTAB : opt.fstab = optarg; break;
613 case OPTION_CHROOT: opt.rootdir = optarg; break;
614 case OPTION_SECURE: opt.is_secure = true; break;
616 WRITE_MSG(2, "Try '");
617 WRITE_STR(2, argv[0]);
618 WRITE_MSG(2, " --help\" for more information.\n");
624 if (opt.mount_all && optind<argc) {
625 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
630 if (!mountFstab(&opt)) return EXIT_FAILURE;
631 else return EXIT_SUCCESS;
634 if (optind+2!=argc) {
635 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
640 mnt.data = strdup(mnt.data);
641 if (!transformOptionList(&mnt)) {
642 WRITE_MSG(2, "Invalid options specified\n");
647 mnt.src = argv[optind++];
648 mnt.dst = argv[optind++];
650 if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;