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 static struct option const
85 { "help", no_argument, 0, 'h' },
86 { "version", no_argument, 0, 'v' },
87 { "bind", no_argument, 0, OPTION_BIND },
88 { "move", no_argument, 0, OPTION_MOVE },
89 { "mtab", required_argument, 0, OPTION_MTAB },
90 { "fstab", required_argument, 0, OPTION_FSTAB },
91 { "chroot", required_argument, 0, OPTION_CHROOT },
92 { "secure", no_argument, 0, OPTION_SECURE },
93 { "rbind", no_argument, 0, OPTION_RBIND },
97 static struct FstabOptions {
98 char const * const opt;
99 unsigned long const or_flag;
100 unsigned long const and_flag;
102 } const FSTAB_OPTIONS[] = {
103 { "rbind", MS_BIND|MS_REC, ~0, false },
104 { "bind", MS_BIND, ~0, false },
105 { "move", MS_MOVE, ~0, false },
107 { "noatime", MS_NOATIME, ~0, false },
108 { "mandlock", MS_MANDLOCK, ~0, false },
109 { "nodev", MS_NODEV, ~0, false },
110 { "nodiratime", MS_NODIRATIME, ~0, false },
111 { "noexec", MS_NOEXEC, ~0, false },
112 { "nosuid", MS_NOSUID, ~0, false },
113 { "rdonly", MS_RDONLY, ~0, false },
114 { "remount", MS_REMOUNT, ~0, false },
115 { "sync", MS_SYNCHRONOUS, ~0, false },
117 { "dirsync", MS_DIRSYNC, ~0, false },
124 showHelp(int fd, char const *cmd, int res)
126 VSERVER_DECLARE_CMD(cmd);
128 WRITE_MSG(fd, "Usage: ");
131 " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [-n]\n"
132 " [--mtab <filename>] [--fstab <filename>] [--chroot <dirname>] \n"
133 " [--secure] -a|([-o <options>] [--] <src> <dst>)\n\n"
134 "Executes mount-operations in the given chroot-dir: it assumes sources in the\n"
135 "current root-dir while destinations are expected in the chroot environment.\n"
136 "When '--secure' is given, the destination must not contain symlinks.\n\n"
137 "For non-trivial mount-operations it uses the external 'mount' program which\n"
138 "can be overridden by the $MOUNT environment variable.\n\n"
139 "Please report bugs to " PACKAGE_BUGREPORT "\n");
148 "secure-mount " VERSION " -- secure mounting of directories\n"
149 "This program is part of " PACKAGE_STRING "\n\n"
150 "Copyright (C) 2003 Enrico Scholz\n"
151 VERSION_COPYRIGHT_DISCLAIMER);
156 isSameObject(struct stat const *lhs,
157 struct stat const *rhs)
159 return (lhs->st_dev==rhs->st_dev &&
160 lhs->st_ino==rhs->st_ino);
164 chdirSecure(char const *dir)
166 char tmp[strlen(dir)+1], *ptr;
170 cur = strtok_r(tmp, "/", &ptr);
172 struct stat pre_stat, post_stat;
174 if (lstat(cur, &pre_stat)==-1) return -1;
176 if (!S_ISDIR(pre_stat.st_mode)) {
180 if (S_ISLNK(pre_stat.st_mode)) {
185 if (chdir(cur)==-1) return -1;
186 if (stat(".", &post_stat)==-1) return -1;
188 if (!isSameObject(&pre_stat, &post_stat)) {
191 WRITE_MSG(2, "Possible symlink race ATTACK at '");
192 WRITE_STR(2, getcwd(dir, sizeof(dir)));
199 cur = strtok_r(0, "/", &ptr);
206 verifyPosition(char const *mntpoint, char const *dir1, char const *dir2)
208 struct stat pre_stat, post_stat;
210 if (stat(mntpoint, &pre_stat)==-1) return -1;
211 if (chroot(dir1)==-1 || chdir(dir2)==-1) return -1;
212 if (stat(".", &post_stat)==-1) return -1;
214 if (!isSameObject(&pre_stat, &post_stat)) {
217 WRITE_MSG(2, "Possible symlink race ATTACK at '");
218 WRITE_STR(2, getcwd(dir, sizeof(dir)));
219 WRITE_MSG(2, "' within '");
233 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
238 writeX(int fd, void const *buf, size_t len)
240 if ((size_t)(write(fd, buf, len))!=len) return -1;
245 writeStrX(int fd, char const *str)
247 return writeX(fd, str, strlen(str));
251 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
255 assert(opt->mtab!=0);
257 if (opt->rootdir!=0 &&
258 chroot(opt->rootdir)==-1) {
263 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
265 if (fd==-1) perror("open()");
267 if (fchroot(opt->cur_rootdir_fd)==-1) {
272 if (fd==-1) goto err0;
274 if (flock(fd, LOCK_EX)==-1) {
280 if (writeStrX(fd, mnt->src)==-1 ||
281 writeStrX(fd, " ")==-1 ||
282 writeStrX(fd, mnt->dst)==-1 ||
283 writeStrX(fd, " ")==-1 ||
284 writeStrX(fd, mnt->type ? mnt->type : "none")==-1 ||
285 writeStrX(fd, " ")==-1 ||
286 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
287 writeStrX(fd, " 0 0\n")==-1) {
299 callExternalMount(struct MountInfo const *mnt)
301 char const * argv[10];
305 char const * mount_prog = getenv("MOUNT");
307 if (mount_prog==0) mount_prog = MOUNT_PROG;
309 argv[idx++] = mount_prog;
311 if (mnt->flags & MS_BIND) argv[idx++] = "--bind";
312 else if (mnt->flags & MS_MOVE) argv[idx++] = "--move";
315 strcmp(mnt->data, "defaults")!=0) {
317 argv[idx++] = mnt->data;
322 argv[idx++] = mnt->type;
325 argv[idx++] = mnt->src;
336 execv(mount_prog, const_cast(char **)(argv));
341 if (wait4(pid, &status, 0, 0)==-1) {
346 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
350 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
352 char const *dir = mnt->dst;
356 if (opt->rootdir!=0) {
357 if (chdir(opt->rootdir)==-1) {
362 while (*dir=='/') ++dir;
365 if (opt->is_secure) {
366 if (chdirSecure(dir)==-1) {
367 perror("chdirSecure()");
379 if (mnt->flags & (MS_BIND|MS_MOVE)) {
380 if (mount(mnt->src, ".",
381 mnt->type ? mnt->type : "",
382 mnt->flags, mnt->data)==-1) {
388 if (!callExternalMount(mnt)) return false;
391 // Check if directories were moved between the chdirSecure() and mount(2)
392 if ((mnt->flags&MS_BIND) && opt->rootdir!=0 &&
393 (verifyPosition(mnt->src, opt->rootdir, mnt->dst)==-1 ||
394 fchroot(opt->cur_rootdir_fd)==-1)) {
395 perror("verifyPosition/fchroot");
396 // TODO: what is with unmounting?
400 if (!opt->ignore_mtab &&
401 updateMtab(mnt, opt)==-1) {
402 WRITE_MSG(2, "Failed to update mtab-file\n");
410 searchAndRemoveOption(char *buf, char const *needle)
412 char *pos = strstr(buf, needle);
413 size_t len = strlen(needle);
415 if (pos==0) return false;
416 if (pos>buf && pos[-1]!=',') return false;
417 if (pos[len]!=',' && pos[len]!='\0') return false;
419 if (pos>buf || pos[len]!='\0') ++len;
422 memmove(pos, pos+len, strlen(pos+len));
427 transformOptionList(struct MountInfo *info)
429 struct FstabOptions const * flag;
431 for (flag=FSTAB_OPTIONS; flag->opt[0]!='\0'; ++flag) {
432 if (searchAndRemoveOption(info->data, flag->opt) || flag->is_dflt) {
433 info->flags &= flag->and_flag;
434 info->flags |= flag->or_flag;
438 if (searchAndRemoveOption(info->data, "noauto"))
444 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
445 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
446 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
448 while (isspace(*PTR)) ++PTR
450 static enum {prDOIT, prFAIL, prIGNORE}
451 parseFstabLine(struct MountInfo *info, char *buf)
453 while (isspace(*buf)) ++buf;
454 if (*buf=='#' || *buf=='\0') return prIGNORE;
457 MOVE_TO_NEXT_FIELD(buf, false);
459 MOVE_TO_NEXT_FIELD(buf, false);
461 MOVE_TO_NEXT_FIELD(buf, false);
463 MOVE_TO_NEXT_FIELD(buf, true);
465 if (strcmp(info->type, "swap")==0) return prIGNORE;
466 if (strcmp(info->type, "none")==0) info->type = 0;
469 info->noauto = false;
470 if (!transformOptionList(info)) return prFAIL;
471 if (info->noauto) return prIGNORE;
476 #undef MOVE_TO_NEXT_FIELD
479 mountFstab(struct Options const *opt)
485 assert(opt->fstab!=0);
486 fd = open(opt->fstab, O_RDONLY);
488 perror("open(<fstab>)");
492 len = lseek(fd, 0, SEEK_END);
494 lseek(fd, 0, SEEK_SET)==-1) {
495 perror("lseek(<fstab>)");
503 if (read(fd, buf, len+1)!=len) {
507 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
511 ptr = strtok_r(buf, "\n", &ptrptr);
513 struct MountInfo mnt;
514 char * new_ptr = strtok_r(0, "\n", &ptrptr);
516 switch (parseFstabLine(&mnt, ptr)) {
518 WRITE_MSG(2, "Failed to parse fstab-line beginning with '");
523 case prIGNORE : break;
526 if (!mountSingle(&mnt, opt)) {
527 WRITE_MSG(2, "Failed to mount fstab-line beginning with '");
546 int main(int argc, char *argv[])
548 struct MountInfo mnt = {
557 struct Options opt = {
559 .fstab = "/etc/fstab",
561 .ignore_mtab = false,
567 opt.cur_rootdir_fd = open("/", O_RDONLY|O_DIRECTORY);
569 if (opt.cur_rootdir_fd==-1) {
570 perror("open(\"/\")");
575 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
579 case 'h' : showHelp(1, argv[0], 0);
580 case 'v' : showVersion();
581 case 't' : mnt.type = optarg; break;
582 case 'n' : opt.ignore_mtab = true; break;
583 case 'a' : opt.mount_all = true; break;
584 case 'o' : mnt.data = optarg; break;
585 case OPTION_RBIND : mnt.flags |= MS_REC; /*@fallthrough@*/
586 case OPTION_BIND : mnt.flags |= MS_BIND; break;
587 case OPTION_MOVE : mnt.flags |= MS_MOVE; break;
588 case OPTION_MTAB : opt.mtab = optarg; break;
589 case OPTION_FSTAB : opt.fstab = optarg; break;
590 case OPTION_CHROOT: opt.rootdir = optarg; break;
591 case OPTION_SECURE: opt.is_secure = true; break;
593 WRITE_MSG(2, "Try '");
594 WRITE_STR(2, argv[0]);
595 WRITE_MSG(2, " --help\" for more information.\n");
601 if (opt.mount_all && optind<argc) {
602 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
607 if (!mountFstab(&opt)) return EXIT_FAILURE;
608 else return EXIT_SUCCESS;
611 if (optind+2!=argc) {
612 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
617 mnt.data = strdup(mnt.data);
618 if (!transformOptionList(&mnt)) {
619 WRITE_MSG(2, "Invalid options specified\n");
624 mnt.src = argv[optind++];
625 mnt.dst = argv[optind++];
627 if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;