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;
85 char cur_dir_path[PATH_MAX];
90 #define OPTION_BIND 1024
91 #define OPTION_MOVE 1025
92 #define OPTION_MTAB 1026
93 #define OPTION_FSTAB 1027
94 #define OPTION_CHROOT 1028
95 #define OPTION_SECURE 1029
96 #define OPTION_RBIND 1030
97 #define OPTION_ROOTFS 1031
99 #define XFLAG_NOAUTO 0x01
100 #define XFLAG_FILE 0x02
102 static struct option const
103 CMDLINE_OPTIONS[] = {
104 { "help", no_argument, 0, 'h' },
105 { "version", no_argument, 0, 'v' },
106 { "bind", no_argument, 0, OPTION_BIND },
107 { "move", no_argument, 0, OPTION_MOVE },
108 { "mtab", required_argument, 0, OPTION_MTAB },
109 { "fstab", required_argument, 0, OPTION_FSTAB },
110 { "rootfs", required_argument, 0, OPTION_ROOTFS },
111 { "chroot", no_argument, 0, OPTION_CHROOT },
112 { "secure", no_argument, 0, OPTION_SECURE },
113 { "rbind", no_argument, 0, OPTION_RBIND },
118 #define MS_REC 0x4000
120 #ifndef MS_UNBINDABLE
121 #define MS_UNBINDABLE (1<<17)
124 #define MS_PRIVATE (1<<18)
127 #define MS_SLAVE (1<<19)
130 #define MS_SHARED (1<<20)
133 static struct FstabOption {
134 char const * const opt;
135 unsigned long const flag;
136 unsigned long const mask;
137 unsigned long const xflag;
139 } const FSTAB_OPTIONS[] = {
140 { "defaults", MS_NODEV, (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|
141 MS_SYNCHRONOUS), 0, false },
142 { "rbind", MS_BIND|MS_REC, MS_BIND|MS_REC, 0, false },
143 { "bind", MS_BIND, MS_BIND, 0, false },
144 { "move", MS_MOVE, MS_MOVE, 0, false },
145 { "async", 0, MS_SYNCHRONOUS, 0, false },
146 { "sync", MS_SYNCHRONOUS, MS_SYNCHRONOUS, 0, false },
147 { "atime", 0, MS_NOATIME, 0, false },
148 { "noatime", MS_NOATIME, MS_NOATIME, 0, false },
149 { "dev", 0, MS_NODEV, 0, false },
150 { "nodev", MS_NODEV, MS_NODEV, 0, false },
151 { "exec", 0, MS_NOEXEC, 0, false },
152 { "noexec", MS_NOEXEC, MS_NOEXEC, 0, false },
153 { "suid", 0, MS_NOSUID, 0, false },
154 { "nosuid", MS_NOSUID, MS_NOSUID, 0, false },
155 { "ro", MS_RDONLY, MS_RDONLY, 0, false },
156 { "rw", 0, MS_RDONLY, 0, false },
158 { "remount", MS_REMOUNT, MS_REMOUNT, 0, false },
159 { "users", MS_NOEXEC|MS_NOSUID|MS_NODEV,
160 MS_NOEXEC|MS_NOSUID|MS_NODEV, 0, false },
161 { "mandlock", MS_MANDLOCK, MS_MANDLOCK, 0, false },
162 { "nodiratime", MS_NODIRATIME, MS_NODIRATIME, 0, false },
164 { "dirsync", MS_DIRSYNC, MS_DIRSYNC, 0, false },
166 { "_netdev", 0, 0, 0, false },
167 { "auto", 0, 0, 0, false },
168 { "noauto", 0, 0, XFLAG_NOAUTO, false },
169 { "user", 0, 0, 0, false },
170 { "nouser", 0, 0, 0, false },
171 { "rec", MS_REC, MS_REC, 0, false },
172 { "unbindable", MS_UNBINDABLE, MS_UNBINDABLE, 0, false },
173 { "private", MS_PRIVATE, MS_PRIVATE, 0, false },
174 { "slave", MS_SLAVE, MS_SLAVE, 0, false },
175 { "shared", MS_SHARED, MS_SHARED, 0, false },
178 int wrapper_exit_code = 1;
181 showHelp(int fd, char const *cmd, int res)
183 VSERVER_DECLARE_CMD(cmd);
185 WRITE_MSG(fd, "Usage: ");
188 " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [--chroot]\n"
189 " [--mtab <filename>] [--fstab <filename>] [--rootfs yes|no|only]\n"
190 " [-n] -a|([-o <options>] [--] <src> <dst>)\n\n"
191 "Executes mount-operations under the current directory: it assumes sources in\n"
192 "the current root-dir while destinations are expected in the chroot environment.\n\n"
193 "For non-trivial mount-operations it uses the external 'mount' program which\n"
194 "can be overridden by the $MOUNT environment variable.\n\n"
196 " --bind|move|rbind ... set the correspond flags; with this options\n"
197 " the mount will be executed internally without\n"
198 " calling an external mount program.\n"
199 " -t <type> ... assume the given filesystem type\n"
200 " -o <options> ... set additional options; see mount(2) for details\n"
201 " -n ... do not update the mtab-file\n"
202 " --mtab <filename> ... use <filename> as an alternative mtab file\n"
203 " [default: /etc/mtab]\n"
204 " --chroot ... chroot into the current directory before\n"
205 " mounting the filesystem\n"
206 " --fstab <filename> ... use <filename> as an alternative fstab file;\n"
207 " this option has an effect only with the '-a'\n"
208 " option [default: /etc/fstab]\n"
209 " --rootfs yes|no|only ... specifies how to handle an entry for a rootfs\n"
210 " ('/') when processing an fstab file. 'yes' will\n"
211 " mount it among the other entries, 'only' will\n"
212 " mount only the rootfs entry, and 'no' will ignore\n"
213 " it and mount only the other entries [default: yes]\n"
214 " -a ... mount everything listed in the fstab-file\n\n"
215 " <src> ... the source-filesystem; this path is absolute\n"
216 " to the current root-filesystem. Only valid\n"
217 " without the '-a' option.\n"
218 " <dst> ... the destination mount-point; when used with\n"
219 " '--chroot', this path is relative to the current\n"
220 " directory. Only valid without the '-a' option\n\n"
221 "Please report bugs to " PACKAGE_BUGREPORT "\n");
230 "secure-mount " VERSION " -- secure mounting of directories\n"
231 "This program is part of " PACKAGE_STRING "\n\n"
232 "Copyright (C) 2003 Enrico Scholz\n"
233 VERSION_COPYRIGHT_DISCLAIMER);
238 isSameObject(struct stat const *lhs,
239 struct stat const *rhs)
241 return (lhs->st_dev==rhs->st_dev &&
242 lhs->st_ino==rhs->st_ino);
248 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
253 writeX(int fd, void const *buf, size_t len)
255 if ((size_t)(write(fd, buf, len))!=len) return -1;
260 writeStrX(int fd, char const *str)
262 return writeX(fd, str, strlen(str));
265 static inline char const *
266 getType(struct MountInfo const *mnt)
268 if (mnt->type==0) return "none";
269 else if (strncmp(mnt->type, "ext", 3)==0) return "ufs";
270 else return mnt->type;
274 restoreRoot(struct Options const *opt)
276 if (opt->do_chroot!=0 && fchroot(opt->cur_rootdir_fd)==-1) {
277 perror("secure-mount: fchdir(\"/\")");
278 WRITE_MSG(2, "Failed to restore root-directory; aborting\n");
284 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
288 assert(opt->mtab!=0);
290 if (opt->do_chroot && fchroot(opt->cur_dir_fd)==-1) {
291 perror("secure-mount: fchroot(\".\")");
295 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
298 perror("secure-mount: open(<mtab>)");
302 if (lockf(fd, F_LOCK, 0)==-1) {
303 perror("secure-mount: lockf()");
307 if (writeStrX(fd, mnt->src)==-1 ||
308 writeStrX(fd, " ")==-1 ||
309 writeStrX(fd, mnt->dst)==-1 ||
310 writeStrX(fd, mnt->xflag & XFLAG_FILE ? "/" : "")==-1 ||
311 writeStrX(fd, mnt->xflag & XFLAG_FILE ? mnt->name : "")==-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()");
330 callExternalMount(struct MountInfo const *mnt)
332 char const * argv[10];
336 char const * mount_prog = getenv("MOUNT");
338 if (mount_prog==0) mount_prog = MOUNT_PROG;
340 argv[idx++] = mount_prog;
342 if (mnt->flag & MS_BIND) argv[idx++] = "--bind";
343 else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
346 if (mnt->data && *mnt->data &&
347 strcmp(mnt->data, "defaults")!=0) {
348 if (mnt->mask & MS_NODEV)
349 argv[idx++] = mnt->data;
351 char * tmp = alloca(strlen(mnt->data) + sizeof("nodev,"));
352 strcpy(tmp, "nodev,");
353 strcat(tmp, mnt->data);
358 argv[idx++] = "nodev";
362 argv[idx++] = mnt->type;
365 argv[idx++] = mnt->src;
366 argv[idx++] = mnt->name;
371 perror("secure-mount: fork()");
376 execv(mount_prog, const_cast(char **)(argv));
377 PERROR_Q("secure-mount: execv", mount_prog);
381 if (wait4(pid, &status, 0, 0)==-1) {
382 perror("secure-mount: wait4()");
386 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
390 secureChdir(char const *dir, struct Options const *opt)
395 if (opt->do_chroot!=0 && fchroot(opt->cur_dir_fd)==-1) {
396 perror("secure-mount: fchroot(\".\")");
400 if (chdir(dir)==-1) {
401 PERROR_Q("secure-mount: chdir", dir);
405 dir_fd = open(".", O_RDONLY|O_DIRECTORY);
407 perror("secure-mount: open(\".\")");
412 if (fchdir(dir_fd)==-1)
413 PERROR_Q("secure-mount: fchdir", dir);
426 canHandleInternal(struct MountInfo const *mnt)
428 static char const * FS[] = {
429 "tmpfs", "sysfs", "proc", "sockfs", "pipefs", "futexfs",
430 "inotifyfs", "devpts", "ext3", "ext2", "ramfs",
431 "hugetlbfs", "usbfs", "binfmt_misc",
436 if (!mnt) return false;
437 else if ((mnt->flag & (MS_BIND|MS_MOVE))) return true;
438 else if ((mnt->flag & (MS_SHARED|MS_SLAVE|MS_PRIVATE|
439 MS_UNBINDABLE))) return true;
440 else if (mnt->type==0) return false;
442 for (i=FS+0; *i!=0; ++i)
443 if (strcmp(mnt->type, *i)==0) return true;
449 mountSingle(struct MountInfo const *mnt, struct Options *opt)
453 if (!secureChdir(mnt->dst, opt))
456 if (canHandleInternal(mnt)) {
457 if (mount(mnt->src, mnt->name,
458 mnt->type ? mnt->type : "",
459 mnt->flag, mnt->data_parsed)==-1) {
460 perror("secure-mount: mount()");
463 if (strcmp(mnt->dst, "/") == 0) {
465 if (chdir(opt->cur_dir_path) == -1) {
466 perror("secure-mount: chdir(.)");
469 close(opt->cur_dir_fd);
470 opt->cur_dir_fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
471 Efcntl(opt->cur_dir_fd, F_SETFD, FD_CLOEXEC);
473 if (!vc_isSupported(vcFEATURE_BME) &&
474 (mnt->flag & MS_BIND) &&
475 (mnt->mask & ~(MS_BIND|MS_REC))) {
476 /* This is needed to put us in the new mountpoint */
477 if (!secureChdir(mnt->dst, opt))
479 if (mount(mnt->src, mnt->name,
480 mnt->type ? mnt->type : "",
481 (mnt->flag | MS_REMOUNT), NULL) == -1 &&
482 errno != EBUSY) { /* Returned on older kernels */
483 perror("secure-mount: mount()");
488 else if (!callExternalMount(mnt))
491 if (!opt->ignore_mtab &&
492 updateMtab(mnt, opt)==-1) {
493 WRITE_MSG(2, "Failed to update mtab-file\n");
500 static struct FstabOption const *
501 searchOption(char const *opt, size_t len)
503 struct FstabOption const * i;
504 for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
505 if (strncmp(i->opt, opt, len)==0) return i;
511 transformOptionList(struct MountInfo *info, size_t UNUSED *col)
513 char const * ptr = info->data;
514 char * data = malloc(strlen(info->data));
518 char const * pos = strchr(ptr, ',');
519 struct FstabOption const * opt;
521 if (pos==0) pos = ptr+strlen(ptr);
522 opt = searchOption(ptr, pos-ptr);
525 info->flag &= ~opt->mask;
526 info->flag |= opt->flag;
527 info->mask |= opt->mask;
528 info->xflag |= opt->xflag;
533 strncpy(dst, ptr, pos-ptr);
543 } while (*ptr!='\0');
545 info->data_parsed = data;
549 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
550 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
551 if (col) *col = buf-start_buf+1; \
552 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
554 while (isspace(*PTR)) ++PTR
556 static enum {prDOIT, prFAIL, prIGNORE}
557 parseFstabLine(struct MountInfo *info, char *buf, size_t *col)
559 char const * const start_buf = buf;
562 while (isspace(*buf)) ++buf;
563 if (*buf=='#' || *buf=='\0') return prIGNORE;
566 MOVE_TO_NEXT_FIELD(buf, false);
568 MOVE_TO_NEXT_FIELD(buf, false);
570 MOVE_TO_NEXT_FIELD(buf, false);
571 err_col = buf-start_buf+1;
573 MOVE_TO_NEXT_FIELD(buf, true);
575 info->flag = MS_NODEV;
580 if (strcmp(info->type, "swap") ==0) return prIGNORE;
581 else if (strcmp(info->type, "none") ==0) info->type = 0;
582 else if (strcmp(info->type, "devpts")==0) {
583 info->mask |= MS_NODEV;
584 info->flag &= ~MS_NODEV;
587 if (col) *col = err_col;
588 if (!transformOptionList(info,col)) return prFAIL;
589 if (info->xflag & XFLAG_NOAUTO) return prIGNORE;
594 #undef MOVE_TO_NEXT_FIELD
597 showFstabPosition(int fd, char const *fname, size_t line_nr, size_t col_nr)
599 char buf[3*sizeof(line_nr)*2 + 4];
600 size_t len = utilvserver_fmt_uint(buf+1, line_nr)+1;
604 len += utilvserver_fmt_uint(buf+len, col_nr);
605 WRITE_STR(fd, fname);
606 Vwrite(fd, buf, len);
610 adjustFileMount(struct MountInfo *mnt)
612 char *buf = strrchr(mnt->dst, '/');
618 mnt->xflag |= XFLAG_FILE;
622 mountFstab(struct Options *opt)
628 assert(opt->fstab!=0);
629 fd = open(opt->fstab, O_RDONLY);
631 perror("secure-mount: open(<fstab>)");
635 len = lseek(fd, 0, SEEK_END);
637 lseek(fd, 0, SEEK_SET)==-1) {
638 perror("secure-mount: lseek(<fstab>)");
645 size_t line_nr=0, col_nr;
647 if (read(fd, buf, len+1)!=len) {
648 perror("secure-mount: read()");
651 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
656 while ((ptr=strsep(&ptrptr, "\n")) != 0) {
657 struct MountInfo mnt;
660 switch (parseFstabLine(&mnt, ptr, &col_nr)) {
662 showFstabPosition(2, opt->fstab, line_nr, col_nr);
663 WRITE_MSG(2, ": syntax error\n");
666 case prIGNORE : break;
668 bool is_rootfs = (strcmp(mnt.dst, "/")==0);
670 if (( is_rootfs && opt->rootfs==rfsNO) ||
671 (!is_rootfs && opt->rootfs==rfsONLY)) { /* ignore the entry */ }
673 if (utilvserver_isFile(mnt.dst, 0))
674 adjustFileMount(&mnt);
675 if (!mountSingle(&mnt, opt)) {
676 showFstabPosition(2, opt->fstab, line_nr, 1);
677 WRITE_MSG(2, ": failed to mount fstab-entry\n");
695 initFDs(struct Options *opt)
697 opt->cur_dir_fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
698 opt->cur_rootdir_fd = Eopen("/", O_RDONLY|O_DIRECTORY, 0);
700 Efcntl(opt->cur_dir_fd, F_SETFD, FD_CLOEXEC);
701 Efcntl(opt->cur_rootdir_fd, F_SETFD, FD_CLOEXEC);
703 if (getcwd(opt->cur_dir_path, sizeof(opt->cur_dir_path)) == NULL) {
704 perror("secure-mount: getcwd()");
705 exit(wrapper_exit_code);
710 parseRootFS(char const *str)
712 if (strcasecmp(str, "yes")==0) return rfsYES;
713 else if (strcasecmp(str, "no")==0) return rfsNO;
714 else if (strcasecmp(str, "only")==0) return rfsONLY;
716 WRITE_MSG(2, "secure-mount: invalid option for '--rootfs': '");
719 exit(wrapper_exit_code);
723 int main(int argc, char *argv[])
725 struct MountInfo mnt = {
735 struct Options opt = {
737 .fstab = "/etc/fstab",
739 .ignore_mtab = false,
742 .cur_rootdir_fd = -1,
747 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
751 case 'h' : showHelp(1, argv[0], 0);
752 case 'v' : showVersion();
753 case 't' : mnt.type = optarg; break;
754 case 'n' : opt.ignore_mtab = true; break;
755 case 'a' : opt.mount_all = true; break;
756 case 'o' : mnt.data = optarg; break;
757 case OPTION_RBIND : mnt.flag |= MS_REC; /*@fallthrough@*/
758 case OPTION_BIND : mnt.flag |= MS_BIND; break;
759 case OPTION_MOVE : mnt.flag |= MS_MOVE; break;
760 case OPTION_MTAB : opt.mtab = optarg; break;
761 case OPTION_FSTAB : opt.fstab = optarg; break;
762 case OPTION_CHROOT: opt.do_chroot = true; break;
763 case OPTION_ROOTFS: opt.rootfs = parseRootFS(optarg); break;
765 WRITE_MSG(2, "secure-mount: The '--secure' option is deprecated...\n");
768 WRITE_MSG(2, "Try '");
769 WRITE_STR(2, argv[0]);
770 WRITE_MSG(2, " --help' for more information.\n");
777 if (opt.mount_all && optind<argc) {
778 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
783 signal(SIGCHLD, SIG_DFL);
786 if (!mountFstab(&opt)) return EXIT_FAILURE;
787 else return EXIT_SUCCESS;
790 if (optind+2!=argc) {
791 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
796 mnt.data = strdup(mnt.data);
797 if (!transformOptionList(&mnt, 0)) {
798 WRITE_MSG(2, "Invalid options specified\n");
803 mnt.src = argv[optind++];
804 mnt.dst = argv[optind++];
806 if (utilvserver_isFile(mnt.dst, 0))
807 adjustFileMount(&mnt);
808 if (!mountSingle(&mnt, &opt))