Skip to content

Commit 51dfe32

Browse files
author
erh
committed
Fix PR#3617 and PR#3205: call realpath to convert mount points and paths to
device nodes into absolute paths before using them.
1 parent 0744314 commit 51dfe32

File tree

35 files changed

+377
-121
lines changed

35 files changed

+377
-121
lines changed

sbin/mount_ados/mount_ados.8

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $NetBSD: mount_ados.8,v 1.17 2003/02/25 10:35:00 wiz Exp $
1+
.\" $NetBSD: mount_ados.8,v 1.18 2005/01/31 05:19:18 erh Exp $
22
.\"
33
.\" Copyright (c) 1993, 1994 Christopher G. Demetriou
44
.\" All rights reserved.
@@ -55,6 +55,11 @@ the device
5555
to the global filesystem namespace at the location
5656
indicated by
5757
.Pa node .
58+
Both
59+
.Ar special
60+
and
61+
.Ar node
62+
are converted to absolute paths before use.
5863
This command is normally executed by
5964
.Xr mount 8
6065
at boot time, but can be used by any user to mount an

sbin/mount_ados/mount_ados.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: mount_ados.c,v 1.17 2003/05/03 15:37:07 christos Exp $ */
1+
/* $NetBSD: mount_ados.c,v 1.18 2005/01/31 05:19:18 erh Exp $ */
22

33
/*
44
* Copyright (c) 1994 Christopher G. Demetriou
@@ -36,7 +36,7 @@
3636

3737
#include <sys/cdefs.h>
3838
#ifndef lint
39-
__RCSID("$NetBSD: mount_ados.c,v 1.17 2003/05/03 15:37:07 christos Exp $");
39+
__RCSID("$NetBSD: mount_ados.c,v 1.18 2005/01/31 05:19:18 erh Exp $");
4040
#endif /* not lint */
4141

4242
#include <sys/cdefs.h>
@@ -84,7 +84,7 @@ mount_ados(argc, argv)
8484
struct adosfs_args args;
8585
struct stat sb;
8686
int c, mntflags, set_gid, set_uid, set_mask;
87-
char *dev, *dir, ndir[MAXPATHLEN+1];
87+
char *dev, *dir, canon_dir[MAXPATHLEN], canon_dev[MAXPATHLEN];
8888

8989
mntflags = set_gid = set_uid = set_mask = 0;
9090
(void)memset(&args, '\0', sizeof(args));
@@ -118,13 +118,20 @@ mount_ados(argc, argv)
118118

119119
dev = argv[optind];
120120
dir = argv[optind + 1];
121-
if (dir[0] != '/') {
121+
122+
if (realpath(dev, canon_dev) == NULL) /* Check device path */
123+
err(1, "realpath %s", dev);
124+
if (strncmp(dev, canon_dev, MAXPATHLEN)) {
125+
warnx("\"%s\" is a relative path.", dev);
126+
dev = canon_dev;
127+
warnx("using \"%s\" instead.", dev);
128+
}
129+
130+
if (realpath(dir, canon_dir) == NULL) /* Check mounton path */
131+
err(1, "realpath %s", dir);
132+
if (strncmp(dir, canon_dir, MAXPATHLEN)) {
122133
warnx("\"%s\" is a relative path.", dir);
123-
if (getcwd(ndir, sizeof(ndir)) == NULL)
124-
err(1, "getcwd");
125-
strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
126-
strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
127-
dir = ndir;
134+
dir = canon_dir;
128135
warnx("using \"%s\" instead.", dir);
129136
}
130137

sbin/mount_cd9660/mount_cd9660.8

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $NetBSD: mount_cd9660.8,v 1.22 2004/11/22 12:22:45 wiz Exp $
1+
.\" $NetBSD: mount_cd9660.8,v 1.23 2005/01/31 05:19:19 erh Exp $
22
.\"
33
.\" Copyright (c) 2004 The NetBSD Foundation, Inc.
44
.\" All rights reserved.
@@ -81,6 +81,11 @@ command attaches the ISO-9660 filesystem residing on the device
8181
.Pa special
8282
to the global filesystem namespace at the location indicated by
8383
.Pa node .
84+
Both
85+
.Ar special
86+
and
87+
.Ar node
88+
are converted to absolute paths before use.
8489
.Pp
8590
The options are as follows:
8691
.Bl -tag -width indent

sbin/mount_cd9660/mount_cd9660.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: mount_cd9660.c,v 1.18 2003/08/07 10:04:26 agc Exp $ */
1+
/* $NetBSD: mount_cd9660.c,v 1.19 2005/01/31 05:19:19 erh Exp $ */
22

33
/*
44
* Copyright (c) 1992, 1993, 1994
@@ -46,7 +46,7 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
4646
#if 0
4747
static char sccsid[] = "@(#)mount_cd9660.c 8.7 (Berkeley) 5/1/95";
4848
#else
49-
__RCSID("$NetBSD: mount_cd9660.c,v 1.18 2003/08/07 10:04:26 agc Exp $");
49+
__RCSID("$NetBSD: mount_cd9660.c,v 1.19 2005/01/31 05:19:19 erh Exp $");
5050
#endif
5151
#endif /* not lint */
5252

@@ -99,7 +99,7 @@ mount_cd9660(argc, argv)
9999
{
100100
struct iso_args args;
101101
int ch, mntflags, opts;
102-
char *dev, *dir;
102+
char *dev, *dir, canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
103103

104104
mntflags = opts = 0;
105105
while ((ch = getopt(argc, argv, "egijo:r")) != -1)
@@ -140,6 +140,22 @@ mount_cd9660(argc, argv)
140140
dev = argv[0];
141141
dir = argv[1];
142142

143+
if (realpath(dev, canon_dev) == NULL) /* Check device path */
144+
err(1, "realpath %s", dev);
145+
if (strncmp(dev, canon_dev, MAXPATHLEN)) {
146+
warnx("\"%s\" is a relative path.", dev);
147+
dev = canon_dev;
148+
warnx("using \"%s\" instead.", dev);
149+
}
150+
151+
if (realpath(dir, canon_dir) == NULL) /* Check mounton path */
152+
err(1, "realpath %s", dir);
153+
if (strncmp(dir, canon_dir, MAXPATHLEN)) {
154+
warnx("\"%s\" is a relative path.", dir);
155+
dir = canon_dir;
156+
warnx("using \"%s\" instead.", dir);
157+
}
158+
143159
#define DEFAULT_ROOTUID -2
144160
/*
145161
* ISO 9660 filesystems are not writable.

sbin/mount_ext2fs/mount_ext2fs.8

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $NetBSD: mount_ext2fs.8,v 1.12 2003/08/07 10:04:27 agc Exp $
1+
.\" $NetBSD: mount_ext2fs.8,v 1.13 2005/01/31 05:19:19 erh Exp $
22
.\"
33
.\" Copyright (c) 1993, 1994
44
.\" The Regents of the University of California. All rights reserved.
@@ -46,6 +46,11 @@ command attaches an EXT2FS file system
4646
.Ar special
4747
device on to the file system tree at the point
4848
.Ar node .
49+
Both
50+
.Ar special
51+
and
52+
.Ar node
53+
are converted to absolute paths before use.
4954
.Pp
5055
This command is normally executed by
5156
.Xr mount 8

sbin/mount_ext2fs/mount_ext2fs.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: mount_ext2fs.c,v 1.11 2003/08/07 10:04:27 agc Exp $ */
1+
/* $NetBSD: mount_ext2fs.c,v 1.12 2005/01/31 05:19:19 erh Exp $ */
22

33
/*-
44
* Copyright (c) 1993, 1994
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993, 1994\n\
3939
#if 0
4040
static char sccsid[] = "@(#)mount_ufs.c 8.4 (Berkeley) 4/26/95";
4141
#else
42-
__RCSID("$NetBSD: mount_ext2fs.c,v 1.11 2003/08/07 10:04:27 agc Exp $");
42+
__RCSID("$NetBSD: mount_ext2fs.c,v 1.12 2005/01/31 05:19:19 erh Exp $");
4343
#endif
4444
#endif /* not lint */
4545

@@ -90,7 +90,7 @@ mount_ext2fs(argc, argv)
9090
{
9191
struct ufs_args args; /* XXX ffs_args */
9292
int ch, mntflags;
93-
char *fs_name;
93+
char fs_name[MAXPATHLEN], canon_dev[MAXPATHLEN];
9494
const char *errcause;
9595

9696
mntflags = 0;
@@ -110,8 +110,20 @@ mount_ext2fs(argc, argv)
110110
if (argc != 2)
111111
ext2fs_usage();
112112

113-
args.fspec = argv[0]; /* The name of the device file. */
114-
fs_name = argv[1]; /* The mount point. */
113+
if (realpath(argv[0], canon_dev) == NULL) /* Check device path */
114+
err(1, "realpath %s", argv[0]);
115+
if (strncmp(argv[0], canon_dev, MAXPATHLEN)) {
116+
warnx("\"%s\" is a relative path.", argv[0]);
117+
warnx("using \"%s\" instead.", canon_dev);
118+
}
119+
args.fspec = canon_dev;
120+
121+
if (realpath(argv[1], fs_name) == NULL) /* Check mounton path */
122+
err(1, "realpath %s", argv[1]);
123+
if (strncmp(argv[1], fs_name, MAXPATHLEN)) {
124+
warnx("\"%s\" is a relative path.", argv[1]);
125+
warnx("using \"%s\" instead.", fs_name);
126+
}
115127

116128
#define DEFAULT_ROOTUID -2
117129
args.export.ex_root = DEFAULT_ROOTUID;

sbin/mount_fdesc/mount_fdesc.8

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $NetBSD: mount_fdesc.8,v 1.14 2003/08/07 10:04:27 agc Exp $
1+
.\" $NetBSD: mount_fdesc.8,v 1.15 2005/01/31 05:19:19 erh Exp $
22
.\"
33
.\" Copyright (c) 1992, 1993, 1994
44
.\" The Regents of the University of California. All rights reserved.
@@ -53,6 +53,10 @@ The conventional mount point is
5353
and the filesystem should be union mounted in order to augment,
5454
rather than replace, the existing entries in
5555
.Pa /dev .
56+
The directory specified by
57+
.Ar mount_point
58+
is converted to an absolute path before use.
59+
.Pp
5660
This command is normally executed by
5761
.Xr mount 8
5862
at boot time.

sbin/mount_fdesc/mount_fdesc.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: mount_fdesc.c,v 1.14 2003/08/07 10:04:27 agc Exp $ */
1+
/* $NetBSD: mount_fdesc.c,v 1.15 2005/01/31 05:19:19 erh Exp $ */
22

33
/*
44
* Copyright (c) 1992, 1993, 1994
@@ -77,7 +77,7 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
7777
#if 0
7878
static char sccsid[] = "@(#)mount_fdesc.c 8.3 (Berkeley) 4/26/95";
7979
#else
80-
__RCSID("$NetBSD: mount_fdesc.c,v 1.14 2003/08/07 10:04:27 agc Exp $");
80+
__RCSID("$NetBSD: mount_fdesc.c,v 1.15 2005/01/31 05:19:19 erh Exp $");
8181
#endif
8282
#endif /* not lint */
8383

@@ -118,6 +118,7 @@ mount_fdesc(argc, argv)
118118
char *argv[];
119119
{
120120
int ch, mntflags;
121+
char canon_dir[MAXPATHLEN];
121122

122123
mntflags = 0;
123124
while ((ch = getopt(argc, argv, "o:")) != -1)
@@ -135,7 +136,14 @@ mount_fdesc(argc, argv)
135136
if (argc != 2)
136137
usage();
137138

138-
if (mount(MOUNT_FDESC, argv[1], mntflags, NULL))
139+
if (realpath(argv[1], canon_dir) == NULL) /* Check mounton path */
140+
err(1, "realpath %s", argv[1]);
141+
if (strncmp(argv[1], canon_dir, MAXPATHLEN)) {
142+
warnx("\"%s\" is a relative path.", argv[1]);
143+
warnx("using \"%s\" instead.", canon_dir);
144+
}
145+
146+
if (mount(MOUNT_FDESC, canon_dir, mntflags, NULL))
139147
err(1, "fdesc on %s", argv[1]);
140148
exit(0);
141149
}

sbin/mount_ffs/mount_ffs.8

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $NetBSD: mount_ffs.8,v 1.12 2003/08/07 10:04:27 agc Exp $
1+
.\" $NetBSD: mount_ffs.8,v 1.13 2005/01/31 05:19:19 erh Exp $
22
.\"
33
.\" Copyright (c) 1980, 1989, 1991, 1993
44
.\" The Regents of the University of California. All rights reserved.
@@ -47,6 +47,11 @@ command attaches the Berkeley Fast File System on the
4747
.Ar special
4848
device on to the file system tree at point
4949
.Ar node .
50+
Both
51+
.Ar special
52+
and
53+
.Ar node
54+
are converted to absolute paths before use.
5055
.Pp
5156
The
5257
.Nm mount_ufs

sbin/mount_ffs/mount_ffs.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: mount_ffs.c,v 1.15 2003/08/07 10:04:27 agc Exp $ */
1+
/* $NetBSD: mount_ffs.c,v 1.16 2005/01/31 05:19:19 erh Exp $ */
22

33
/*-
44
* Copyright (c) 1993, 1994
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993, 1994\n\
3939
#if 0
4040
static char sccsid[] = "@(#)mount_ufs.c 8.4 (Berkeley) 4/26/95";
4141
#else
42-
__RCSID("$NetBSD: mount_ffs.c,v 1.15 2003/08/07 10:04:27 agc Exp $");
42+
__RCSID("$NetBSD: mount_ffs.c,v 1.16 2005/01/31 05:19:19 erh Exp $");
4343
#endif
4444
#endif /* not lint */
4545

@@ -92,7 +92,7 @@ mount_ffs(argc, argv)
9292
{
9393
struct ufs_args args;
9494
int ch, mntflags;
95-
char *fs_name;
95+
char fs_name[MAXPATHLEN], canon_dev[MAXPATHLEN];
9696
const char *errcause;
9797

9898
mntflags = 0;
@@ -112,8 +112,20 @@ mount_ffs(argc, argv)
112112
if (argc != 2)
113113
ffs_usage();
114114

115-
args.fspec = argv[0]; /* The name of the device file. */
116-
fs_name = argv[1]; /* The mount point. */
115+
if (realpath(argv[0], canon_dev) == NULL) /* Check device path */
116+
err(1, "realpath %s", argv[0]);
117+
if (strncmp(argv[0], canon_dev, MAXPATHLEN)) {
118+
warnx("\"%s\" is a relative path.", argv[0]);
119+
warnx("using \"%s\" instead.", canon_dev);
120+
}
121+
args.fspec = canon_dev;
122+
123+
if (realpath(argv[1], fs_name) == NULL) /* Check mounton path */
124+
err(1, "realpath %s", argv[1]);
125+
if (strncmp(argv[1], fs_name, MAXPATHLEN)) {
126+
warnx("\"%s\" is a relative path.", argv[1]);
127+
warnx("using \"%s\" instead.", fs_name);
128+
}
117129

118130
#define DEFAULT_ROOTUID -2
119131
args.export.ex_root = DEFAULT_ROOTUID;

0 commit comments

Comments
 (0)