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> [--chroot]
20 // [--mtab <mtabfile>] [--fstab <fstabfile>]
22 // Executes mount-operations under the current directory: it assumes sources
23 // in the current root-dir while destinations are expected in the chroot
32 #include "pathconfig.h"
34 #include <lib/internal.h>
45 #include <sys/mount.h>
47 #include <sys/types.h>
57 #define ENSC_WRAPPERS_FCNTL 1
58 #define ENSC_WRAPPERS_UNISTD 1
61 #define MNTPOINT "/etc"
63 typedef enum { rfsYES, rfsNO, rfsONLY } RootFsOption;
89 #define OPTION_BIND 1024
90 #define OPTION_MOVE 1025
91 #define OPTION_MTAB 1026
92 #define OPTION_FSTAB 1027
93 #define OPTION_CHROOT 1028
94 #define OPTION_SECURE 1029
95 #define OPTION_RBIND 1030
96 #define OPTION_ROOTFS 1031
98 #define XFLAG_NOAUTO 0x01
99 #define XFLAG_FILE 0x02
101 static struct option const
102 CMDLINE_OPTIONS[] = {
103 { "help", no_argument, 0, 'h' },
104 { "version", no_argument, 0, 'v' },
105 { "bind", no_argument, 0, OPTION_BIND },
106 { "move", no_argument, 0, OPTION_MOVE },
107 { "mtab", required_argument, 0, OPTION_MTAB },
108 { "fstab", required_argument, 0, OPTION_FSTAB },
109 { "rootfs", required_argument, 0, OPTION_ROOTFS },
110 { "chroot", no_argument, 0, OPTION_CHROOT },
111 { "secure", no_argument, 0, OPTION_SECURE },
112 { "rbind", no_argument, 0, OPTION_RBIND },
117 #define MS_REC 0x4000
119 #ifndef MS_UNBINDABLE
120 #define MS_UNBINDABLE (1<<17)
123 #define MS_PRIVATE (1<<18)
126 #define MS_SLAVE (1<<19)
129 #define MS_SHARED (1<<20)
132 static struct FstabOption {
133 char const * const opt;
134 unsigned long const flag;
135 unsigned long const mask;
136 unsigned long const xflag;
138 } const FSTAB_OPTIONS[] = {
139 { "defaults", MS_NODEV, (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|
140 MS_SYNCHRONOUS), 0, false },
141 { "rbind", MS_BIND|MS_REC, MS_BIND|MS_REC, 0, false },
142 { "bind", MS_BIND, MS_BIND, 0, false },
143 { "move", MS_MOVE, MS_MOVE, 0, false },
144 { "async", 0, MS_SYNCHRONOUS, 0, false },
145 { "sync", MS_SYNCHRONOUS, MS_SYNCHRONOUS, 0, false },
146 { "atime", 0, MS_NOATIME, 0, false },
147 { "noatime", MS_NOATIME, MS_NOATIME, 0, false },
148 { "dev", 0, MS_NODEV, 0, false },
149 { "nodev", MS_NODEV, MS_NODEV, 0, false },
150 { "exec", 0, MS_NOEXEC, 0, false },
151 { "noexec", MS_NOEXEC, MS_NOEXEC, 0, false },
152 { "suid", 0, MS_NOSUID, 0, false },
153 { "nosuid", MS_NOSUID, MS_NOSUID, 0, false },
154 { "ro", MS_RDONLY, MS_RDONLY, 0, false },
155 { "rw", 0, MS_RDONLY, 0, false },
157 { "remount", MS_REMOUNT, MS_REMOUNT, 0, false },
158 { "users", MS_NOEXEC|MS_NOSUID|MS_NODEV,
159 MS_NOEXEC|MS_NOSUID|MS_NODEV, 0, false },
160 { "mandlock", MS_MANDLOCK, MS_MANDLOCK, 0, false },
161 { "nodiratime", MS_NODIRATIME, MS_NODIRATIME, 0, false },
163 { "dirsync", MS_DIRSYNC, MS_DIRSYNC, 0, false },
165 { "_netdev", 0, 0, 0, false },
166 { "auto", 0, 0, 0, false },
167 { "noauto", 0, 0, XFLAG_NOAUTO, false },
168 { "user", 0, 0, 0, false },
169 { "nouser", 0, 0, 0, false },
170 { "rec", MS_REC, MS_REC, 0, false },
171 { "unbindable", MS_UNBINDABLE, MS_UNBINDABLE, 0, false },
172 { "private", MS_PRIVATE, MS_PRIVATE, 0, false },
173 { "slave", MS_SLAVE, MS_SLAVE, 0, false },
174 { "shared", MS_SHARED, MS_SHARED, 0, false },
177 int wrapper_exit_code = 1;
180 showHelp(int fd, char const *cmd, int res)
182 VSERVER_DECLARE_CMD(cmd);
184 WRITE_MSG(fd, "Usage: ");
187 " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [--chroot]\n"
188 " [--mtab <filename>] [--fstab <filename>] [--rootfs yes|no|only]\n"
189 " [-n] -a|([-o <options>] [--] <src> <dst>)\n\n"
190 "Executes mount-operations under the current directory: it assumes sources in\n"
191 "the current root-dir while destinations are expected in the chroot environment.\n\n"
192 "For non-trivial mount-operations it uses the external 'mount' program which\n"
193 "can be overridden by the $MOUNT environment variable.\n\n"
195 " --bind|move|rbind ... set the correspond flags; with this options\n"
196 " the mount will be executed internally without\n"
197 " calling an external mount program.\n"
198 " -t <type> ... assume the given filesystem type\n"
199 " -o <options> ... set additional options; see mount(2) for details\n"
200 " -n ... do not update the mtab-file\n"
201 " --mtab <filename> ... use <filename> as an alternative mtab file\n"
202 " [default: /etc/mtab]\n"
203 " --chroot ... chroot into the current directory before\n"
204 " mounting the filesystem\n"
205 " --fstab <filename> ... use <filename> as an alternative fstab file;\n"
206 " this option has an effect only with the '-a'\n"
207 " option [default: /etc/fstab]\n"
208 " --rootfs yes|no|only ... specifies how to handle an entry for a rootfs\n"
209 " ('/') when processing an fstab file. 'yes' will\n"
210 " mount it among the other entries, 'only' will\n"
211 " mount only the rootfs entry, and 'no' will ignore\n"
212 " it and mount only the other entries [default: yes]\n"
213 " -a ... mount everything listed in the fstab-file\n\n"
214 " <src> ... the source-filesystem; this path is absolute\n"
215 " to the current root-filesystem. Only valid\n"
216 " without the '-a' option.\n"
217 " <dst> ... the destination mount-point; when used with\n"
218 " '--chroot', this path is relative to the current\n"
219 " directory. Only valid without the '-a' option\n\n"
220 "Please report bugs to " PACKAGE_BUGREPORT "\n");
229 "secure-mount " VERSION " -- secure mounting of directories\n"
230 "This program is part of " PACKAGE_STRING "\n\n"
231 "Copyright (C) 2003 Enrico Scholz\n"
232 VERSION_COPYRIGHT_DISCLAIMER);
237 isSameObject(struct stat const *lhs,
238 struct stat const *rhs)
240 return (lhs->st_dev==rhs->st_dev &&
241 lhs->st_ino==rhs->st_ino);
247 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
252 writeX(int fd, void const *buf, size_t len)
254 if ((size_t)(write(fd, buf, len))!=len) return -1;
259 writeStrX(int fd, char const *str)
261 return writeX(fd, str, strlen(str));
264 static inline char const *
265 getType(struct MountInfo const *mnt)
267 if (mnt->type==0) return "none";
268 else if (strncmp(mnt->type, "ext", 3)==0) return "ufs";
269 else return mnt->type;
273 restoreRoot(struct Options const *opt)
275 if (opt->do_chroot!=0 && fchroot(opt->cur_rootdir_fd)==-1) {
276 perror("secure-mount: fchdir(\"/\")");
277 WRITE_MSG(2, "Failed to restore root-directory; aborting\n");
283 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
287 assert(opt->mtab!=0);
289 if (opt->do_chroot && fchroot(opt->cur_dir_fd)==-1) {
290 perror("secure-mount: fchroot(\".\")");
294 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
297 perror("secure-mount: open(<mtab>)");
301 if (lockf(fd, F_LOCK, 0)==-1) {
302 perror("secure-mount: lockf()");
306 if (writeStrX(fd, mnt->src)==-1 ||
307 writeStrX(fd, " ")==-1 ||
308 writeStrX(fd, mnt->dst)==-1 ||
309 writeStrX(fd, mnt->xflag & XFLAG_FILE ? "/" : "")==-1 ||
310 writeStrX(fd, mnt->xflag & XFLAG_FILE ? mnt->name : "")==-1 ||
311 writeStrX(fd, " ")==-1 ||
312 writeStrX(fd, getType(mnt))==-1 ||
313 writeStrX(fd, " ")==-1 ||
314 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
315 writeStrX(fd, " 0 0\n")==-1) {
316 perror("secure-mount: write()");
329 callExternalMount(struct MountInfo const *mnt)
331 char const * argv[10];
335 char const * mount_prog = getenv("MOUNT");
337 if (mount_prog==0) mount_prog = MOUNT_PROG;
339 argv[idx++] = mount_prog;
341 if (mnt->flag & MS_BIND) argv[idx++] = "--bind";
342 else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
345 if (mnt->data && *mnt->data &&
346 strcmp(mnt->data, "defaults")!=0) {
347 if (mnt->mask & MS_NODEV)
348 argv[idx++] = mnt->data;
350 char * tmp = alloca(strlen(mnt->data) + sizeof("nodev,"));
351 strcpy(tmp, "nodev,");
352 strcat(tmp, mnt->data);
357 argv[idx++] = "nodev";
361 argv[idx++] = mnt->type;
364 argv[idx++] = mnt->src;
365 argv[idx++] = mnt->name;
370 perror("secure-mount: fork()");
375 execv(mount_prog, const_cast(char **)(argv));
376 PERROR_Q("secure-mount: execv", mount_prog);
380 if (wait4(pid, &status, 0, 0)==-1) {
381 perror("secure-mount: wait4()");
385 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
389 secureChdir(char const *dir, struct Options const *opt)
394 if (opt->do_chroot!=0 && fchroot(opt->cur_dir_fd)==-1) {
395 perror("secure-mount: fchroot(\".\")");
399 if (chdir(dir)==-1) {
400 PERROR_Q("secure-mount: chdir", dir);
404 dir_fd = open(".", O_RDONLY|O_DIRECTORY);
406 perror("secure-mount: open(\".\")");
411 if (fchdir(dir_fd)==-1)
412 PERROR_Q("secure-mount: fchdir", dir);
425 canHandleInternal(struct MountInfo const *mnt)
427 static char const * FS[] = {
428 "tmpfs", "sysfs", "proc", "sockfs", "pipefs", "futexfs",
429 "inotifyfs", "devpts", "ext3", "ext2", "ramfs",
430 "hugetlbfs", "usbfs", "binfmt_misc",
435 if (!mnt) return false;
436 else if ((mnt->flag & (MS_BIND|MS_MOVE))) return true;
437 else if ((mnt->flag & (MS_SHARED|MS_SLAVE|MS_PRIVATE|
438 MS_UNBINDABLE))) return true;
439 else if (mnt->type==0) return false;
441 for (i=FS+0; *i!=0; ++i)
442 if (strcmp(mnt->type, *i)==0) return true;
448 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
452 if (!secureChdir(mnt->dst, opt))
455 if (canHandleInternal(mnt)) {
456 if (mount(mnt->src, mnt->name,
457 mnt->type ? mnt->type : "",
458 mnt->flag, mnt->data_parsed)==-1) {
459 perror("secure-mount: mount()");
462 if ((mnt->flag & MS_BIND) &&
463 (mnt->flag & ~(MS_BIND|MS_REC))) {
464 /* This is needed to put us in the new mountpoint */
465 if (!secureChdir(mnt->dst, opt))
467 if (mount(mnt->src, mnt->name,
468 mnt->type ? mnt->type : "",
469 (mnt->flag | MS_REMOUNT), NULL) == -1 &&
470 errno != EBUSY) { /* Returned on older kernels */
471 perror("secure-mount: mount()");
476 else if (!callExternalMount(mnt))
479 if (!opt->ignore_mtab &&
480 updateMtab(mnt, opt)==-1) {
481 WRITE_MSG(2, "Failed to update mtab-file\n");
488 static struct FstabOption const *
489 searchOption(char const *opt, size_t len)
491 struct FstabOption const * i;
492 for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
493 if (strncmp(i->opt, opt, len)==0) return i;
499 transformOptionList(struct MountInfo *info, size_t UNUSED *col)
501 char const * ptr = info->data;
502 char * data = malloc(strlen(info->data));
506 char const * pos = strchr(ptr, ',');
507 struct FstabOption const * opt;
509 if (pos==0) pos = ptr+strlen(ptr);
510 opt = searchOption(ptr, pos-ptr);
513 info->flag &= ~opt->mask;
514 info->flag |= opt->flag;
515 info->mask |= opt->mask;
516 info->xflag |= opt->xflag;
521 strncpy(dst, ptr, pos-ptr);
531 } while (*ptr!='\0');
533 info->data_parsed = data;
537 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
538 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
539 if (col) *col = buf-start_buf+1; \
540 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
542 while (isspace(*PTR)) ++PTR
544 static enum {prDOIT, prFAIL, prIGNORE}
545 parseFstabLine(struct MountInfo *info, char *buf, size_t *col)
547 char const * const start_buf = buf;
550 while (isspace(*buf)) ++buf;
551 if (*buf=='#' || *buf=='\0') return prIGNORE;
554 MOVE_TO_NEXT_FIELD(buf, false);
556 MOVE_TO_NEXT_FIELD(buf, false);
558 MOVE_TO_NEXT_FIELD(buf, false);
559 err_col = buf-start_buf+1;
561 MOVE_TO_NEXT_FIELD(buf, true);
563 info->flag = MS_NODEV;
568 if (strcmp(info->type, "swap") ==0) return prIGNORE;
569 else if (strcmp(info->type, "none") ==0) info->type = 0;
570 else if (strcmp(info->type, "devpts")==0) {
571 info->mask |= MS_NODEV;
572 info->flag &= ~MS_NODEV;
575 if (col) *col = err_col;
576 if (!transformOptionList(info,col)) return prFAIL;
577 if (info->xflag & XFLAG_NOAUTO) return prIGNORE;
582 #undef MOVE_TO_NEXT_FIELD
585 showFstabPosition(int fd, char const *fname, size_t line_nr, size_t col_nr)
587 char buf[3*sizeof(line_nr)*2 + 4];
588 size_t len = utilvserver_fmt_uint(buf+1, line_nr)+1;
592 len += utilvserver_fmt_uint(buf+len, col_nr);
593 WRITE_STR(fd, fname);
594 Vwrite(fd, buf, len);
598 adjustFileMount(struct MountInfo *mnt)
600 char *buf = strrchr(mnt->dst, '/');
606 mnt->xflag |= XFLAG_FILE;
610 mountFstab(struct Options const *opt)
616 assert(opt->fstab!=0);
617 fd = open(opt->fstab, O_RDONLY);
619 perror("secure-mount: open(<fstab>)");
623 len = lseek(fd, 0, SEEK_END);
625 lseek(fd, 0, SEEK_SET)==-1) {
626 perror("secure-mount: lseek(<fstab>)");
633 size_t line_nr=0, col_nr;
635 if (read(fd, buf, len+1)!=len) {
636 perror("secure-mount: read()");
639 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
644 while ((ptr=strsep(&ptrptr, "\n")) != 0) {
645 struct MountInfo mnt;
648 switch (parseFstabLine(&mnt, ptr, &col_nr)) {
650 showFstabPosition(2, opt->fstab, line_nr, col_nr);
651 WRITE_MSG(2, ": syntax error\n");
654 case prIGNORE : break;
656 bool is_rootfs = (strcmp(mnt.dst, "/")==0);
658 if (( is_rootfs && opt->rootfs==rfsNO) ||
659 (!is_rootfs && opt->rootfs==rfsONLY)) { /* ignore the entry */ }
661 if (utilvserver_isFile(mnt.dst, 0))
662 adjustFileMount(&mnt);
663 if (!mountSingle(&mnt, opt)) {
664 showFstabPosition(2, opt->fstab, line_nr, 1);
665 WRITE_MSG(2, ": failed to mount fstab-entry\n");
683 initFDs(struct Options *opt)
685 opt->cur_dir_fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
686 opt->cur_rootdir_fd = Eopen("/", O_RDONLY|O_DIRECTORY, 0);
688 Efcntl(opt->cur_dir_fd, F_SETFD, FD_CLOEXEC);
689 Efcntl(opt->cur_rootdir_fd, F_SETFD, FD_CLOEXEC);
693 parseRootFS(char const *str)
695 if (strcasecmp(str, "yes")==0) return rfsYES;
696 else if (strcasecmp(str, "no")==0) return rfsNO;
697 else if (strcasecmp(str, "only")==0) return rfsONLY;
699 WRITE_MSG(2, "secure-mount: invalid option for '--rootfs': '");
702 exit(wrapper_exit_code);
706 int main(int argc, char *argv[])
708 struct MountInfo mnt = {
718 struct Options opt = {
720 .fstab = "/etc/fstab",
722 .ignore_mtab = false,
725 .cur_rootdir_fd = -1,
730 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
734 case 'h' : showHelp(1, argv[0], 0);
735 case 'v' : showVersion();
736 case 't' : mnt.type = optarg; break;
737 case 'n' : opt.ignore_mtab = true; break;
738 case 'a' : opt.mount_all = true; break;
739 case 'o' : mnt.data = optarg; break;
740 case OPTION_RBIND : mnt.flag |= MS_REC; /*@fallthrough@*/
741 case OPTION_BIND : mnt.flag |= MS_BIND; break;
742 case OPTION_MOVE : mnt.flag |= MS_MOVE; break;
743 case OPTION_MTAB : opt.mtab = optarg; break;
744 case OPTION_FSTAB : opt.fstab = optarg; break;
745 case OPTION_CHROOT: opt.do_chroot = true; break;
746 case OPTION_ROOTFS: opt.rootfs = parseRootFS(optarg); break;
748 WRITE_MSG(2, "secure-mount: The '--secure' option is deprecated...\n");
751 WRITE_MSG(2, "Try '");
752 WRITE_STR(2, argv[0]);
753 WRITE_MSG(2, " --help' for more information.\n");
760 if (opt.mount_all && optind<argc) {
761 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
766 signal(SIGCHLD, SIG_DFL);
769 if (!mountFstab(&opt)) return EXIT_FAILURE;
770 else return EXIT_SUCCESS;
773 if (optind+2!=argc) {
774 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
779 mnt.data = strdup(mnt.data);
780 if (!transformOptionList(&mnt, 0)) {
781 WRITE_MSG(2, "Invalid options specified\n");
786 mnt.src = argv[optind++];
787 mnt.dst = argv[optind++];
789 if (utilvserver_isFile(mnt.dst, 0))
790 adjustFileMount(&mnt);
791 if (!mountSingle(&mnt, &opt))