apply realpath to the target directory to avoid loops
authorJan-Simon Pendry <pendry@ucbvax.Berkeley.EDU>
Thu, 17 Feb 1994 23:25:14 +0000 (15:25 -0800)
committerJan-Simon Pendry <pendry@ucbvax.Berkeley.EDU>
Thu, 17 Feb 1994 23:25:14 +0000 (15:25 -0800)
SCCS-vsn: old/mount_lofs/mount_lofs.c 8.2
SCCS-vsn: sbin/mount_null/mount_null.c 8.2
SCCS-vsn: sbin/mount_union/mount_union.c 8.2

usr/src/old/mount_lofs/mount_lofs.c
usr/src/sbin/mount_null/mount_null.c
usr/src/sbin/mount_union/mount_union.c

index 2920912..2259ea2 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdlib.h>
 #include <string.h>
 
+static int subdir __P((char *, char *));
 void usage __P((void));
 
 int
 void usage __P((void));
 
 int
@@ -30,6 +31,7 @@ main(argc, argv)
 {
        struct lofs_args args;
        int ch, mntflags;
 {
        struct lofs_args args;
        int ch, mntflags;
+       char target[MAXPATHLEN];
 
        mntflags = 0;
        while ((ch = getopt(argc, argv, "F:")) != EOF)
 
        mntflags = 0;
        while ((ch = getopt(argc, argv, "F:")) != EOF)
@@ -47,7 +49,20 @@ main(argc, argv)
        if (argc != 2)
                usage();
 
        if (argc != 2)
                usage();
 
-       args.target = argv[0];
+       if (realpath(argv[0], target) == 0) {
+               (void)fprintf(stderr, "mount_lofs: %s: %s\n",
+                               target, strerror(errno));
+               exit(1);
+       }
+
+       if (subdir(target, argv[1])) {
+               (void)fprintf(stderr,
+                       "mount_lofs: %s (%s) is a sub directory of %s\n",
+                               argv[0], target, argv[1]);
+               exit(1);
+       }
+
+       args.target = target;
 
        if (mount(MOUNT_LOFS, argv[1], mntflags, &args)) {
                (void)fprintf(stderr, "mount_lofs: %s\n", strerror(errno));
 
        if (mount(MOUNT_LOFS, argv[1], mntflags, &args)) {
                (void)fprintf(stderr, "mount_lofs: %s\n", strerror(errno));
@@ -56,6 +71,23 @@ main(argc, argv)
        exit(0);
 }
 
        exit(0);
 }
 
+static int
+subdir(p, dir)
+       char *p;
+       char *dir;
+{
+       int l;
+
+       l = strlen(dir);
+       if (l <= 1)
+               return (1);
+
+       if ((strncmp(p, dir, l) == 0) && (p[l] == '/'))
+               return (1);
+
+       return (0);
+}
+
 void
 usage()
 {
 void
 usage()
 {
index ce73af9..4f3a828 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdlib.h>
 #include <string.h>
 
+static int subdir __P((char *, char *));
 void usage __P((void));
 
 int
 void usage __P((void));
 
 int
@@ -30,6 +31,7 @@ main(argc, argv)
 {
        struct null_args args;
        int ch, mntflags;
 {
        struct null_args args;
        int ch, mntflags;
+       char target[MAXPATHLEN];
 
        mntflags = 0;
        while ((ch = getopt(argc, argv, "F:")) != EOF)
 
        mntflags = 0;
        while ((ch = getopt(argc, argv, "F:")) != EOF)
@@ -47,7 +49,20 @@ main(argc, argv)
        if (argc != 2)
                usage();
 
        if (argc != 2)
                usage();
 
-       args.target = argv[0];
+       if (realpath(argv[0], target) == 0) {
+               (void)fprintf(stderr, "mount_lofs: %s: %s\n",
+                               target, strerror(errno));
+               exit(1);
+       }
+
+       if (subdir(target, argv[1])) {
+               (void)fprintf(stderr,
+                       "mount_null: %s (%s) is a sub directory of %s\n",
+                               argv[0], target, argv[1]);
+               exit(1);
+       }
+
+       args.target = target;
 
        if (mount(MOUNT_NULL, argv[1], mntflags, &args)) {
                (void)fprintf(stderr, "mount_null: %s\n", strerror(errno));
 
        if (mount(MOUNT_NULL, argv[1], mntflags, &args)) {
                (void)fprintf(stderr, "mount_null: %s\n", strerror(errno));
@@ -56,6 +71,23 @@ main(argc, argv)
        exit(0);
 }
 
        exit(0);
 }
 
+static int
+subdir(p, dir)
+       char *p;
+       char *dir;
+{
+       int l;
+
+       l = strlen(dir);
+       if (l <= 1)
+               return (1);
+
+       if ((strncmp(p, dir, l) == 0) && (p[l] == '/'))
+               return (1);
+
+       return (0);
+}
+
 void
 usage()
 {
 void
 usage()
 {
index 56dbb42..c9cd373 100644 (file)
@@ -7,7 +7,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)mount_union.c       8.1 (Berkeley) %G%
+ *     @(#)mount_union.c       8.2 (Berkeley) %G%
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdlib.h>
 #include <string.h>
 
+static int subdir __P((char *, char *));
 void usage __P((void));
 
 int
 void usage __P((void));
 
 int
@@ -29,6 +30,7 @@ main(argc, argv)
 {
        struct union_args args;
        int ch, mntflags;
 {
        struct union_args args;
        int ch, mntflags;
+       char target[MAXPATHLEN];
        int error = 0;
 
        mntflags = 0;
        int error = 0;
 
        mntflags = 0;
@@ -81,7 +83,20 @@ main(argc, argv)
        if (error)
                usage();
 
        if (error)
                usage();
 
-       args.target = argv[0];
+       if (realpath(argv[0], target) == 0) {
+               (void)fprintf(stderr, "mount_union: %s: %s\n",
+                               target, strerror(errno));
+               exit(1);
+       }
+
+       if (subdir(target, argv[1])) {
+               (void)fprintf(stderr,
+                       "mount_union: %s is a sub-directory of %s\n",
+                               argv[0], argv[1]);
+               exit(1);
+       }
+
+       args.target = target;
 
        if (mount(MOUNT_UNION, argv[1], mntflags, &args)) {
                (void)fprintf(stderr, "mount_union: %s\n", strerror(errno));
 
        if (mount(MOUNT_UNION, argv[1], mntflags, &args)) {
                (void)fprintf(stderr, "mount_union: %s\n", strerror(errno));
@@ -90,6 +105,23 @@ main(argc, argv)
        exit(0);
 }
 
        exit(0);
 }
 
+static int
+subdir(p, dir)
+       char *p;
+       char *dir;
+{
+       int l;
+
+       l = strlen(dir);
+       if (l <= 1)
+               return (1);
+
+       if ((strncmp(p, dir, l) == 0) && (p[l] == '/'))
+               return (1);
+
+       return (0);
+}
+
 void
 usage()
 {
 void
 usage()
 {