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>
52 #define MNTPOINT "/etc"
74 #define OPTION_BIND 1024
75 #define OPTION_MOVE 1025
76 #define OPTION_MTAB 1026
77 #define OPTION_FSTAB 1027
78 #define OPTION_CHROOT 1028
79 #define OPTION_SECURE 1029
81 static struct option const
83 { "help", no_argument, 0, 'h' },
84 { "version", no_argument, 0, 'v' },
85 { "bind", no_argument, 0, OPTION_BIND },
86 { "move", no_argument, 0, OPTION_MOVE },
87 { "mtab", required_argument, 0, OPTION_MTAB },
88 { "fstab", required_argument, 0, OPTION_FSTAB },
89 { "chroot", required_argument, 0, OPTION_CHROOT },
90 { "secure", no_argument, 0, OPTION_SECURE },
94 static struct FstabOptions {
95 char const * const opt;
96 unsigned long const or_flag;
97 unsigned long const and_flag;
99 } const FSTAB_OPTIONS[] = {
100 { "bind", MS_BIND, ~0, false },
101 { "move", MS_MOVE, ~0, false },
103 { "noatime", MS_NOATIME, ~0, false },
104 { "mandlock", MS_MANDLOCK, ~0, false },
105 { "nodev", MS_NODEV, ~0, false },
106 { "nodiratime", MS_NODIRATIME, ~0, false },
107 { "noexec", MS_NOEXEC, ~0, false },
108 { "nosuid", MS_NOSUID, ~0, false },
109 { "rdonly", MS_RDONLY, ~0, false },
110 { "remount", MS_REMOUNT, ~0, false },
111 { "sync", MS_SYNCHRONOUS, ~0, false },
113 { "dirsync", MS_DIRSYNC, ~0, false },
120 showHelp(int fd, char const *cmd, int res)
122 WRITE_MSG(fd, "Usage: ");
125 " [--help] [--version] [--bind] [--move] [-t <type>] [-n]\n"
126 " [--mtab <filename>] [--fstab <filename>] [--chroot <dirname>] \n"
127 " [--secure] -a|([-o <options>] [--] <src> <dst>)\n\n"
128 "Executes mount-operations in the given chroot-dir: it assumes sources in the\n"
129 "current root-dir while destinations are expected in the chroot environment.\n"
130 "When '--secure' is given, the destination must not contain symlinks.\n\n"
131 "For non-trivial mount-operations it uses the external 'mount' program which\n"
132 "can be overridden by the $MOUNT environment variable.\n\n"
133 "Please report bugs to " PACKAGE_BUGREPORT "\n");
142 "secure-mount " VERSION " -- secure mounting of directories\n"
143 "This program is part of " PACKAGE_STRING "\n\n"
144 "Copyright (C) 2003 Enrico Scholz\n"
145 VERSION_COPYRIGHT_DISCLAIMER);
150 isSameObject(struct stat const *lhs,
151 struct stat const *rhs)
153 return (lhs->st_dev==rhs->st_dev &&
154 lhs->st_ino==rhs->st_ino);
158 chdirSecure(char const *dir)
160 char tmp[strlen(dir)+1], *ptr;
164 cur = strtok_r(tmp, "/", &ptr);
166 struct stat pre_stat, post_stat;
168 if (lstat(cur, &pre_stat)==-1) return -1;
170 if (!S_ISDIR(pre_stat.st_mode)) {
174 if (S_ISLNK(pre_stat.st_mode)) {
179 if (chdir(cur)==-1) return -1;
180 if (stat(".", &post_stat)==-1) return -1;
182 if (!isSameObject(&pre_stat, &post_stat)) {
185 WRITE_MSG(2, "Possible symlink race ATTACK at '");
186 WRITE_STR(2, getcwd(dir, sizeof(dir)));
193 cur = strtok_r(0, "/", &ptr);
200 verifyPosition(char const *mntpoint, char const *dir1, char const *dir2)
202 struct stat pre_stat, post_stat;
204 if (stat(mntpoint, &pre_stat)==-1) return -1;
205 if (chroot(dir1)==-1 || chdir(dir2)==-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)));
213 WRITE_MSG(2, "' within '");
227 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
232 writeX(int fd, void const *buf, size_t len)
234 if ((size_t)(write(fd, buf, len))!=len) return -1;
239 writeStrX(int fd, char const *str)
241 return writeX(fd, str, strlen(str));
245 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
249 assert(opt->mtab!=0);
251 if (opt->rootdir!=0 &&
252 chroot(opt->rootdir)==-1) {
257 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
259 if (fd==-1) perror("open()");
261 if (fchroot(opt->cur_rootdir_fd)==-1) {
266 if (fd==-1) goto err0;
268 if (flock(fd, LOCK_EX)==-1) {
274 if (writeStrX(fd, mnt->src)==-1 ||
275 writeStrX(fd, " ")==-1 ||
276 writeStrX(fd, mnt->dst)==-1 ||
277 writeStrX(fd, " ")==-1 ||
278 writeStrX(fd, mnt->type ? mnt->type : "none")==-1 ||
279 writeStrX(fd, " ")==-1 ||
280 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
281 writeStrX(fd, " 0 0\n")==-1) {
293 callExternalMount(struct MountInfo const *mnt)
295 char const * argv[10];
299 char const * mount_prog = getenv("MOUNT");
301 if (mount_prog==0) mount_prog = MOUNT_PROG;
303 argv[idx++] = mount_prog;
305 if (mnt->flags & MS_BIND) argv[idx++] = "--bind";
306 else if (mnt->flags & MS_MOVE) argv[idx++] = "--move";
309 strcmp(mnt->data, "defaults")!=0) {
311 argv[idx++] = mnt->data;
316 argv[idx++] = mnt->type;
319 argv[idx++] = mnt->src;
330 execv(mount_prog, const_cast(char **)(argv));
335 if (wait4(pid, &status, 0, 0)==-1) {
340 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
344 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
346 char const *dir = mnt->dst;
350 if (opt->rootdir!=0) {
351 if (chdir(opt->rootdir)==-1) {
356 while (*dir=='/') ++dir;
359 if (opt->is_secure) {
360 if (chdirSecure(dir)==-1) {
361 perror("chdirSecure()");
373 if (mnt->flags&MS_BIND) {
374 if (mount(mnt->src, ".",
375 mnt->type ? mnt->type : "",
376 mnt->flags, mnt->data)==-1) {
382 if (!callExternalMount(mnt)) return false;
385 // Check if directories were moved between the chdirSecure() and mount(2)
386 if ((mnt->flags&MS_BIND) && opt->rootdir!=0 &&
387 (verifyPosition(mnt->src, opt->rootdir, mnt->dst)==-1 ||
388 fchroot(opt->cur_rootdir_fd)==-1)) {
389 perror("verifyPosition/fchroot");
390 // TODO: what is with unmounting?
394 if (!opt->ignore_mtab &&
395 updateMtab(mnt, opt)==-1) {
396 WRITE_MSG(2, "Failed to update mtab-file\n");
404 searchAndRemoveOption(char *buf, char const *needle)
406 char *pos = strstr(buf, needle);
407 size_t len = strlen(needle);
409 if (pos==0) return false;
410 if (pos>buf && pos[-1]!=',') return false;
411 if (pos[len]!=',' && pos[len]!='\0') return false;
413 if (pos>buf || pos[len]!='\0') ++len;
416 memmove(pos, pos+len, strlen(pos+len));
421 transformOptionList(struct MountInfo *info)
423 struct FstabOptions const * flag;
425 for (flag=FSTAB_OPTIONS; flag->opt[0]!='\0'; ++flag) {
426 if (searchAndRemoveOption(info->data, flag->opt) || flag->is_dflt) {
427 info->flags &= flag->and_flag;
428 info->flags |= flag->or_flag;
432 if (searchAndRemoveOption(info->data, "noauto"))
438 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
439 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
440 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
442 while (isspace(*PTR)) ++PTR
444 static enum {prDOIT, prFAIL, prIGNORE}
445 parseFstabLine(struct MountInfo *info, char *buf)
447 while (isspace(*buf)) ++buf;
448 if (*buf=='#' || *buf=='\0') return prIGNORE;
451 MOVE_TO_NEXT_FIELD(buf, false);
453 MOVE_TO_NEXT_FIELD(buf, false);
455 MOVE_TO_NEXT_FIELD(buf, false);
457 MOVE_TO_NEXT_FIELD(buf, true);
459 if (strcmp(info->type, "swap")==0) return prIGNORE;
460 if (strcmp(info->type, "none")==0) info->type = 0;
463 info->noauto = false;
464 if (!transformOptionList(info)) return prFAIL;
465 if (info->noauto) return prIGNORE;
470 #undef MOVE_TO_NEXT_FIELD
473 mountFstab(struct Options const *opt)
479 assert(opt->fstab!=0);
480 fd = open(opt->fstab, O_RDONLY);
482 perror("open(<fstab>)");
486 len = lseek(fd, 0, SEEK_END);
488 lseek(fd, 0, SEEK_SET)==-1) {
489 perror("lseek(<fstab>)");
497 if (read(fd, buf, len+1)!=len) {
501 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
505 ptr = strtok_r(buf, "\n", &ptrptr);
507 struct MountInfo mnt;
508 char * new_ptr = strtok_r(0, "\n", &ptrptr);
510 switch (parseFstabLine(&mnt, ptr)) {
512 WRITE_MSG(2, "Failed to parse fstab-line beginning with '");
517 case prIGNORE : break;
520 if (!mountSingle(&mnt, opt)) {
521 WRITE_MSG(2, "Failed to mount fstab-line beginning with '");
540 int main(int argc, char *argv[])
542 struct MountInfo mnt = {
551 struct Options opt = {
553 .fstab = "/etc/fstab",
555 .ignore_mtab = false,
561 opt.cur_rootdir_fd = open("/", O_RDONLY|O_DIRECTORY);
563 if (opt.cur_rootdir_fd==-1) {
564 perror("open(\"/\")");
569 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
573 case 'h' : showHelp(1, argv[0], 0);
574 case 'v' : showVersion();
575 case 't' : mnt.type = optarg; break;
576 case 'n' : opt.ignore_mtab = true; break;
577 case 'a' : opt.mount_all = true; break;
578 case 'o' : mnt.data = optarg; break;
579 case OPTION_BIND : mnt.flags |= MS_BIND; break;
580 case OPTION_MOVE : mnt.flags |= MS_MOVE; break;
581 case OPTION_MTAB : opt.mtab = optarg; break;
582 case OPTION_FSTAB : opt.fstab = optarg; break;
583 case OPTION_CHROOT: opt.rootdir = optarg; break;
584 case OPTION_SECURE: opt.is_secure = true; break;
586 WRITE_MSG(2, "Try '");
587 WRITE_STR(2, argv[0]);
588 WRITE_MSG(2, " --help\" for more information.\n");
594 if (opt.mount_all && optind<argc) {
595 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
600 if (!mountFstab(&opt)) return EXIT_FAILURE;
601 else return EXIT_SUCCESS;
604 if (optind+2!=argc) {
605 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
610 mnt.data = strdup(mnt.data);
611 if (!transformOptionList(&mnt)) {
612 WRITE_MSG(2, "Invalid options specified\n");
617 mnt.src = argv[optind++];
618 mnt.dst = argv[optind++];
620 if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;