Subject: bin/3617: mount_ changes to check for relative paths
To: None <gnats-bugs@gnats.netbsd.org>
From: None <nimenees@par0163.urh.uiuc.edu>
List: netbsd-bugs
Date: 05/13/1997 21:05:45
>Number:         3617
>Category:       bin
>Synopsis:       mount_ utils don't check for relative paths
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue May 13 19:20:01 1997
>Last-Modified:
>Originator:     Eric Haszlakiewicz
>Organization:
>Release:        1.2E
>Environment:
	Pentium, NetBSD-current
System: NetBSD par0163.urh.uiuc.edu 1.2E NetBSD 1.2E (REALMS) #63: Tue May 13 18:21:30 CDT 1997 root@par0163.urh.uiuc.edu:/usr/src/sys/arch/i386/compile/REALMS i386


>Description:
	mount_ utils don't check for relative paths
>How-To-Repeat:
>Fix:
	here's a few of the patches.  (as far as I got so far)
	(btw, ignore PR3612 about the mount_union stuff for now, I'm changing
	it too)
	Patches for: mount_ados, mount_cd9660, mount_fdesc, mount_ffs, 
			mount_kernfs, mount_msdos


*** mount_ados.c.old    Tue May 13 19:14:44 1997
--- mount_ados.c        Tue May 13 19:27:45 1997
*************** main(argc, argv)
*** 67,73 ****
        struct adosfs_args args;
        struct stat sb;
        int c, mntflags, set_gid, set_uid, set_mask;
!       char *dev, *dir, ndir[MAXPATHLEN+1];
  
        mntflags = set_gid = set_uid = set_mask = 0;
        (void)memset(&args, '\0', sizeof(args));
--- 67,73 ----
        struct adosfs_args args;
        struct stat sb;
        int c, mntflags, set_gid, set_uid, set_mask;
!       char *dev, *dir, ndir[MAXPATHLEN], ndev[MAXPATHLEN];
  
        mntflags = set_gid = set_uid = set_mask = 0;
        (void)memset(&args, '\0', sizeof(args));
*************** main(argc, argv)
*** 101,112 ****
  
        dev = argv[optind];
        dir = argv[optind + 1];
!       if (dir[0] != '/') {
                warnx("\"%s\" is a relative path.", dir);
-               if (getcwd(ndir, sizeof(ndir)) == NULL)
-                       err(1, "getcwd");
-               strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
-               strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
                dir = ndir;
                warnx("using \"%s\" instead.", dir);
        }
--- 101,119 ----
  
        dev = argv[optind];
        dir = argv[optind + 1];
! 
!       if (realpath(dev, ndev) == NULL)                /* Check device path */
!               err(1, "realpath %s", dev);
!       if (strncmp(dev, ndev, MAXPATHLEN)) {
!               warnx("\"%s\" is a relative path.", dev);
!               dev = ndev;
!               warnx("using \"%s\" instead.", dev);
!       }
! 
!       if (realpath(dir, ndir) == NULL)                /* Check mounton path */
!               err(1, "realpath %s", dir);
!       if (strncmp(dir, ndir, MAXPATHLEN)) {
                warnx("\"%s\" is a relative path.", dir);
                dir = ndir;
                warnx("using \"%s\" instead.", dir);
        }


*** mount_cd9660.c.old  Tue May 13 19:26:22 1997
--- mount_cd9660.c      Tue May 13 19:29:51 1997
*************** main(argc, argv)
*** 79,85 ****
  {
        struct iso_args args;
        int ch, mntflags, opts;
!       char *dev, *dir;
  
        mntflags = opts = 0;
        while ((ch = getopt(argc, argv, "ego:r")) != EOF)
--- 79,85 ----
  {
        struct iso_args args;
        int ch, mntflags, opts;
!       char *dev, *dir, ndev[MAXPATHLEN], ndir[MAXPATHLEN];
  
        mntflags = opts = 0;
        while ((ch = getopt(argc, argv, "ego:r")) != EOF)
*************** main(argc, argv)
*** 108,113 ****
--- 108,129 ----
  
        dev = argv[0];
        dir = argv[1];
+ 
+       if (realpath(dev, ndev) == NULL)                /* Check device path */
+               err(1, "realpath %s", dev);
+       if (strncmp(dev, ndev, MAXPATHLEN)) {
+               warnx("\"%s\" is a relative path.", dev);
+               dev = ndev;
+               warnx("using \"%s\" instead.", dev);
+       }
+ 
+       if (realpath(dir, ndir) == NULL)                /* Check mounton path */
+               err(1, "realpath %s", dir);
+       if (strncmp(dir, ndir, MAXPATHLEN)) {
+               warnx("\"%s\" is a relative path.", dir);
+               dir = ndir;
+               warnx("using \"%s\" instead.", dir);
+       }
  
  #define DEFAULT_ROOTUID       -2
        args.fspec = dev;


*** mount_fdesc.c.old   Tue May 13 19:30:03 1997
--- mount_fdesc.c       Tue May 13 19:42:46 1997
*************** main(argc, argv)
*** 75,80 ****
--- 75,81 ----
        char *argv[];
  {
        int ch, mntflags;
+       char ndir[MAXPATHLEN], ndev[MAXPATHLEN];
  
        mntflags = 0;
        while ((ch = getopt(argc, argv, "o:")) != EOF)
*************** main(argc, argv)
*** 92,98 ****
        if (argc != 2)
                usage();
  
!       if (mount(MOUNT_FDESC, argv[1], mntflags, NULL))
                err(1, NULL);
        exit(0);
  }
--- 93,106 ----
        if (argc != 2)
                usage();
  
!       if (realpath(argv[1], ndir) == NULL)            /* Check mounton path */
!               err(1, "realpath %s", argv[1]);
!       if (strncmp(argv[1], ndir, MAXPATHLEN)) {
!               warnx("\"%s\" is a relative path.", argv[1]);
!               warnx("using \"%s\" instead.", ndir);
!       }
! 
!       if (mount(MOUNT_FDESC, ndir, mntflags, NULL))
                err(1, NULL);
        exit(0);
  }


*** mount_ffs.c.old     Tue May 13 19:44:34 1997
--- mount_ffs.c Tue May 13 20:29:13 1997
*************** main(argc, argv)
*** 79,85 ****
        extern int optreset;
        struct ufs_args args;
        int ch, mntflags;
!       char *fs_name, *errcause;
  
        mntflags = 0;
        optind = optreset = 1;          /* Reset for parse of new argv. */
--- 79,85 ----
        extern int optreset;
        struct ufs_args args;
        int ch, mntflags;
!       char fs_name[MAXPATHLEN], ndev[MAXPATHLEN], *errcause;
  
        mntflags = 0;
        optind = optreset = 1;          /* Reset for parse of new argv. */
*************** main(argc, argv)
*** 98,105 ****
        if (argc != 2)
                ffs_usage();
  
!         args.fspec = argv[0];         /* The name of the device file. */
!       fs_name = argv[1];              /* The mount point. */
  
  #define DEFAULT_ROOTUID       -2
        args.export.ex_root = DEFAULT_ROOTUID;
--- 98,117 ----
        if (argc != 2)
                ffs_usage();
  
!       if (realpath(argv[0], ndev) == NULL)    /* Device filename */
!               err(1, "realpath %s", argv[0]);
!       if (strncmp(argv[0], ndev, MAXPATHLEN)) {
!               warnx("\"%s\" is a relative path.", argv[0]);
!               warnx("using \"%s\" instead.", ndev);
!       }
!       args.fspec = ndev;
! 
!       if (realpath(argv[1], fs_name) == NULL) /* Resolve the mount point. */
!               err(1, "realpath %s", argv[1]);
!       if (strncmp(argv[1], fs_name, MAXPATHLEN)) {
!               warnx("\"%s\" is a relative path.", argv[1]);
!               warnx("using \"%s\" instead.", fs_name);
!       }
  
  #define DEFAULT_ROOTUID       -2
        args.export.ex_root = DEFAULT_ROOTUID;
*************** main(argc, argv)
*** 119,124 ****
--- 131,139 ----
                            "specified device does not match mounted device";
                        else 
                                errcause = "incorrect super block";
+ 
+                       /* XXX else errcause = "relative pathname";
+                               but that gets checked earlier */
                        break;
                default:
                        errcause = strerror(errno);


*** mount_kernfs.c.old  Tue May 13 19:44:53 1997
--- mount_kernfs.c      Tue May 13 19:46:39 1997
*************** main(argc, argv)
*** 75,80 ****
--- 75,81 ----
        char *argv[];
  {
        int ch, mntflags;
+       char ndir[MAXPATHLEN];
  
        mntflags = 0;
        while ((ch = getopt(argc, argv, "o:")) != EOF)
*************** main(argc, argv)
*** 92,98 ****
        if (argc != 2)
                usage();
  
!       if (mount(MOUNT_KERNFS, argv[1], mntflags, NULL))
                err(1, NULL);
        exit(0);
  }
--- 93,106 ----
        if (argc != 2)
                usage();
  
!       if (realpath(argv[1], ndir) == NULL)            /* Check mounton path */
!               err(1, "realpath %s", argv[1]);
!       if (strncmp(argv[1], ndir, MAXPATHLEN)) {
!               warnx("\"%s\" is a relative path.", argv[1]);
!               warnx("using \"%s\" instead.", ndir);
!       }
! 
!       if (mount(MOUNT_KERNFS, ndir, mntflags, NULL))
                err(1, NULL);
        exit(0);
  }


*** mount_msdos.c.old   Tue May 13 19:19:15 1997
--- mount_msdos.c       Tue May 13 19:27:31 1997
*************** main(argc, argv)
*** 68,74 ****
        struct msdosfs_args args;
        struct stat sb;
        int c, mntflags, set_gid, set_uid, set_mask;
!       char *dev, *dir, ndir[MAXPATHLEN+1];
  
        mntflags = set_gid = set_uid = set_mask = 0;
        (void)memset(&args, '\0', sizeof(args));
--- 68,74 ----
        struct msdosfs_args args;
        struct stat sb;
        int c, mntflags, set_gid, set_uid, set_mask;
!       char *dev, *dir, ndir[MAXPATHLEN], ndev[MAXPATHLEN];
  
        mntflags = set_gid = set_uid = set_mask = 0;
        (void)memset(&args, '\0', sizeof(args));
*************** main(argc, argv)
*** 114,125 ****
  
        dev = argv[optind];
        dir = argv[optind + 1];
!       if (dir[0] != '/') {
                warnx("\"%s\" is a relative path.", dir);
-               if (getcwd(ndir, sizeof(ndir)) == NULL)
-                       err(1, "getcwd");
-               strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
-               strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
                dir = ndir;
                warnx("using \"%s\" instead.", dir);
        }
--- 114,132 ----
  
        dev = argv[optind];
        dir = argv[optind + 1];
! 
!       if (realpath(dev, ndev) == NULL)                /* Check device path */
!               err(1, "realpath %s", dev);
!       if (strncmp(dev, ndev, MAXPATHLEN)) {
!               warnx("\"%s\" is a relative path.", dev);
!               dev = ndev;
!               warnx("using \"%s\" instead.", dev);
!       }
! 
!       if (realpath(dir, ndir) == NULL)                /* Check mounton path */
!               err(1, "realpath %s", dir);
!       if (strncmp(dir, ndir, MAXPATHLEN)) {
                warnx("\"%s\" is a relative path.", dir);
                dir = ndir;
                warnx("using \"%s\" instead.", dir);
        }

>Audit-Trail:
>Unformatted: