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>
54 #define MNTPOINT "/etc"
76 #define OPTION_BIND 1024
77 #define OPTION_MOVE 1025
78 #define OPTION_MTAB 1026
79 #define OPTION_FSTAB 1027
80 #define OPTION_CHROOT 1028
81 #define OPTION_SECURE 1029
82 #define OPTION_RBIND 1030
84 #define XFLAG_NOAUTO 0x01
86 static struct option const
88 { "help", no_argument, 0, 'h' },
89 { "version", no_argument, 0, 'v' },
90 { "bind", no_argument, 0, OPTION_BIND },
91 { "move", no_argument, 0, OPTION_MOVE },
92 { "mtab", required_argument, 0, OPTION_MTAB },
93 { "fstab", required_argument, 0, OPTION_FSTAB },
94 { "chroot", required_argument, 0, OPTION_CHROOT },
95 { "secure", no_argument, 0, OPTION_SECURE },
96 { "rbind", no_argument, 0, OPTION_RBIND },
101 # define MS_REC 0x4000
104 static struct FstabOption {
105 char const * const opt;
106 unsigned long const flag;
107 unsigned long const mask;
108 unsigned long const xflag;
110 } const FSTAB_OPTIONS[] = {
111 { "defaults", 0, (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|
112 MS_SYNCHRONOUS), 0, false },
113 { "rbind", MS_BIND|MS_REC, MS_BIND|MS_REC, 0, false },
114 { "bind", MS_BIND, MS_BIND, 0, false },
115 { "move", MS_MOVE, MS_MOVE, 0, false },
116 { "async", 0, MS_SYNCHRONOUS, 0, false },
117 { "sync", MS_SYNCHRONOUS, MS_SYNCHRONOUS, 0, false },
118 { "atime", 0, MS_NOATIME, 0, false },
119 { "noatime", MS_NOATIME, MS_NOATIME, 0, false },
120 { "dev", 0, MS_NODEV, 0, false },
121 { "nodev", MS_NODEV, MS_NODEV, 0, false },
122 { "exec", 0, MS_NOEXEC, 0, false },
123 { "noexec", MS_NOEXEC, MS_NOEXEC, 0, false },
124 { "suid", 0, MS_NOSUID, 0, false },
125 { "nosuid", MS_NOSUID, MS_NOSUID, 0, false },
126 { "ro", MS_RDONLY, MS_RDONLY, 0, false },
127 { "rw", 0, MS_RDONLY, 0, false },
129 { "remount", MS_REMOUNT, MS_REMOUNT, 0, false },
130 { "users", MS_NOEXEC|MS_NOSUID|MS_NODEV,
131 MS_NOEXEC|MS_NOSUID|MS_NODEV, 0, false },
132 { "mandlock", MS_MANDLOCK, MS_MANDLOCK, 0, false },
133 { "nodiratime", MS_NODIRATIME, MS_NODIRATIME, 0, false },
135 { "dirsync", MS_DIRSYNC, MS_DIRSYNC, 0, false },
137 { "_netdev", 0, 0, 0, false },
138 { "auto", 0, 0, 0, false },
139 { "noauto", 0, 0, XFLAG_NOAUTO, false },
140 { "user", 0, 0, 0, false },
141 { "nouser", 0, 0, 0, false },
145 showHelp(int fd, char const *cmd, int res)
147 VSERVER_DECLARE_CMD(cmd);
149 WRITE_MSG(fd, "Usage: ");
152 " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [-n]\n"
153 " [--mtab <filename>] [--fstab <filename>] [--chroot <dirname>] \n"
154 " [--secure] -a|([-o <options>] [--] <src> <dst>)\n\n"
155 "Executes mount-operations in the given chroot-dir: it assumes sources in the\n"
156 "current root-dir while destinations are expected in the chroot environment.\n"
157 "When '--secure' is given, the destination must not contain symlinks.\n\n"
158 "For non-trivial mount-operations it uses the external 'mount' program which\n"
159 "can be overridden by the $MOUNT environment variable.\n\n"
160 "Please report bugs to " PACKAGE_BUGREPORT "\n");
169 "secure-mount " VERSION " -- secure mounting of directories\n"
170 "This program is part of " PACKAGE_STRING "\n\n"
171 "Copyright (C) 2003 Enrico Scholz\n"
172 VERSION_COPYRIGHT_DISCLAIMER);
177 isSameObject(struct stat const *lhs,
178 struct stat const *rhs)
180 return (lhs->st_dev==rhs->st_dev &&
181 lhs->st_ino==rhs->st_ino);
185 chdirSecure(char const *dir)
187 char tmp[strlen(dir)+1], *ptr;
191 cur = strtok_r(tmp, "/", &ptr);
193 struct stat pre_stat, post_stat;
195 if (lstat(cur, &pre_stat)==-1) return -1;
197 if (!S_ISDIR(pre_stat.st_mode)) {
201 if (S_ISLNK(pre_stat.st_mode)) {
206 if (chdir(cur)==-1) return -1;
207 if (stat(".", &post_stat)==-1) return -1;
209 if (!isSameObject(&pre_stat, &post_stat)) {
212 WRITE_MSG(2, "Possible symlink race ATTACK at '");
213 WRITE_STR(2, getcwd(dir, sizeof(dir)));
220 cur = strtok_r(0, "/", &ptr);
227 verifyPosition(char const *mntpoint, char const *dir1, char const *dir2)
229 struct stat pre_stat, post_stat;
231 if (stat(mntpoint, &pre_stat)==-1) return -1;
232 if (chroot(dir1)==-1 || chdir(dir2)==-1) return -1;
233 if (stat(".", &post_stat)==-1) return -1;
235 if (!isSameObject(&pre_stat, &post_stat)) {
238 WRITE_MSG(2, "Possible symlink race ATTACK at '");
239 WRITE_STR(2, getcwd(dir, sizeof(dir)));
240 WRITE_MSG(2, "' within '");
254 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
259 writeX(int fd, void const *buf, size_t len)
261 if ((size_t)(write(fd, buf, len))!=len) return -1;
266 writeStrX(int fd, char const *str)
268 return writeX(fd, str, strlen(str));
271 static inline char const *
272 getType(struct MountInfo const *mnt)
274 if (mnt->type==0) return "none";
275 else if (strncmp(mnt->type, "ext", 3)==0) return "ufs";
276 else return mnt->type;
280 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
284 assert(opt->mtab!=0);
286 if (opt->rootdir!=0 &&
287 chroot(opt->rootdir)==-1) {
288 perror("secure-mount: chroot()");
292 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
294 if (fd==-1) perror("secure-mount: open(<mtab>)");
296 if (fchroot(opt->cur_rootdir_fd)==-1) {
297 perror("secure-mount: fchroot()");
301 if (fd==-1) goto err0;
303 if (flock(fd, LOCK_EX)==-1) {
304 perror("secure-mount: flock()");
309 if (writeStrX(fd, mnt->src)==-1 ||
310 writeStrX(fd, " ")==-1 ||
311 writeStrX(fd, mnt->dst)==-1 ||
312 writeStrX(fd, " ")==-1 ||
313 writeStrX(fd, getType(mnt))==-1 ||
314 writeStrX(fd, " ")==-1 ||
315 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
316 writeStrX(fd, " 0 0\n")==-1) {
317 perror("secure-mount: write()");
328 callExternalMount(struct MountInfo const *mnt)
330 char const * argv[10];
334 char const * mount_prog = getenv("MOUNT");
336 if (mount_prog==0) mount_prog = MOUNT_PROG;
338 argv[idx++] = mount_prog;
340 if (mnt->flag & MS_BIND) argv[idx++] = "--bind";
341 else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
344 strcmp(mnt->data, "defaults")!=0) {
346 argv[idx++] = mnt->data;
351 argv[idx++] = mnt->type;
354 argv[idx++] = mnt->src;
360 perror("secure-mount: fork()");
365 execv(mount_prog, const_cast(char **)(argv));
366 PERROR_Q("secure-mount: execv", mount_prog);
370 if (wait4(pid, &status, 0, 0)==-1) {
371 perror("secure-mount: wait4()");
375 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
379 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
381 char const *dir = mnt->dst;
385 if (opt->rootdir!=0) {
386 if (chdir(opt->rootdir)==-1) {
387 PERROR_Q("secure-mount: chdir", opt->rootdir);
391 while (*dir=='/') ++dir;
394 if (opt->is_secure) {
395 if (chdirSecure(dir)==-1) {
396 PERROR_Q("secure-mount: chdirSecure", dir);
403 PERROR_Q("secure-mount: chdir", dir);
408 if (mnt->flag & (MS_BIND|MS_MOVE)) {
409 if (mount(mnt->src, ".",
410 mnt->type ? mnt->type : "",
411 mnt->flag, mnt->data)==-1) {
412 perror("secure-mount: mount()");
417 if (!callExternalMount(mnt)) return false;
420 // Check if directories were moved between the chdirSecure() and mount(2)
421 if ((mnt->flag&MS_BIND) && opt->rootdir!=0 &&
422 (verifyPosition(mnt->src, opt->rootdir, mnt->dst)==-1 ||
423 fchroot(opt->cur_rootdir_fd)==-1)) {
424 perror("secure-mount: verifyPosition/fchroot");
425 // TODO: what is with unmounting?
429 if (!opt->ignore_mtab &&
430 updateMtab(mnt, opt)==-1) {
431 WRITE_MSG(2, "Failed to update mtab-file\n");
438 static struct FstabOption const *
439 searchOption(char const *opt, size_t len)
441 struct FstabOption const * i;
442 for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
443 if (strncmp(i->opt, opt, len)==0) return i;
449 transformOptionList(struct MountInfo *info)
451 char const * ptr = info->data;
454 char const * pos = strchr(ptr, ',');
455 struct FstabOption const * opt;
457 if (pos==0) pos = ptr+strlen(ptr);
458 opt = searchOption(ptr, pos-ptr);
461 info->flag &= ~opt->mask;
462 info->flag |= opt->flag;
463 info->xflag |= opt->xflag;
471 } while (*ptr!='\0');
476 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
477 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
478 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
480 while (isspace(*PTR)) ++PTR
482 static enum {prDOIT, prFAIL, prIGNORE}
483 parseFstabLine(struct MountInfo *info, char *buf)
485 while (isspace(*buf)) ++buf;
486 if (*buf=='#' || *buf=='\0') return prIGNORE;
489 MOVE_TO_NEXT_FIELD(buf, false);
491 MOVE_TO_NEXT_FIELD(buf, false);
493 MOVE_TO_NEXT_FIELD(buf, false);
495 MOVE_TO_NEXT_FIELD(buf, true);
497 if (strcmp(info->type, "swap")==0) return prIGNORE;
498 if (strcmp(info->type, "none")==0) info->type = 0;
502 if (!transformOptionList(info)) return prFAIL;
503 if (info->xflag & XFLAG_NOAUTO) return prIGNORE;
508 #undef MOVE_TO_NEXT_FIELD
511 mountFstab(struct Options const *opt)
517 assert(opt->fstab!=0);
518 fd = open(opt->fstab, O_RDONLY);
520 perror("secure-mount: open(<fstab>)");
524 len = lseek(fd, 0, SEEK_END);
526 lseek(fd, 0, SEEK_SET)==-1) {
527 perror("secure-mount: lseek(<fstab>)");
535 if (read(fd, buf, len+1)!=len) {
536 perror("secure-mount: read()");
539 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
543 ptr = strtok_r(buf, "\n", &ptrptr);
545 struct MountInfo mnt;
546 char * new_ptr = strtok_r(0, "\n", &ptrptr);
548 switch (parseFstabLine(&mnt, ptr)) {
550 WRITE_MSG(2, "Failed to parse fstab-line beginning with '");
555 case prIGNORE : break;
558 if (!mountSingle(&mnt, opt)) {
559 WRITE_MSG(2, "Failed to mount fstab-line beginning with '");
578 int main(int argc, char *argv[])
580 struct MountInfo mnt = {
589 struct Options opt = {
591 .fstab = "/etc/fstab",
593 .ignore_mtab = false,
599 opt.cur_rootdir_fd = open("/", O_RDONLY|O_DIRECTORY);
601 if (opt.cur_rootdir_fd==-1) {
602 perror("secure-mount: open(\"/\")");
606 signal(SIGCHLD, SIG_DFL);
609 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
613 case 'h' : showHelp(1, argv[0], 0);
614 case 'v' : showVersion();
615 case 't' : mnt.type = optarg; break;
616 case 'n' : opt.ignore_mtab = true; break;
617 case 'a' : opt.mount_all = true; break;
618 case 'o' : mnt.data = optarg; break;
619 case OPTION_RBIND : mnt.flag |= MS_REC; /*@fallthrough@*/
620 case OPTION_BIND : mnt.flag |= MS_BIND; break;
621 case OPTION_MOVE : mnt.flag |= MS_MOVE; break;
622 case OPTION_MTAB : opt.mtab = optarg; break;
623 case OPTION_FSTAB : opt.fstab = optarg; break;
624 case OPTION_CHROOT: opt.rootdir = optarg; break;
625 case OPTION_SECURE: opt.is_secure = true; break;
627 WRITE_MSG(2, "Try '");
628 WRITE_STR(2, argv[0]);
629 WRITE_MSG(2, " --help\" for more information.\n");
635 if (opt.mount_all && optind<argc) {
636 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
641 if (!mountFstab(&opt)) return EXIT_FAILURE;
642 else return EXIT_SUCCESS;
645 if (optind+2!=argc) {
646 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
651 mnt.data = strdup(mnt.data);
652 if (!transformOptionList(&mnt)) {
653 WRITE_MSG(2, "Invalid options specified\n");
658 mnt.src = argv[optind++];
659 mnt.dst = argv[optind++];
661 if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;