PR/51216: Instead of trying to open files in the current working

directory first for paths that don't contain "/", first try the
/dev paths to avoid confusion with files in the working directory
that happen to match disk names.
This commit is contained in:
christos 2016-06-06 17:50:19 +00:00
parent 241fae59df
commit 395941f2ce
2 changed files with 81 additions and 48 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: opendisk.3,v 1.11 2008/04/30 13:10:52 martin Exp $
.\" $NetBSD: opendisk.3,v 1.12 2016/06/06 17:50:19 christos Exp $
.\"
.\" Copyright (c) 1997, 2001 The NetBSD Foundation, Inc.
.\" All rights reserved.
@ -27,7 +27,7 @@
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd December 11, 2001
.Dd June 6, 2016
.Dt OPENDISK 3
.Os
.Sh NAME
@ -71,20 +71,6 @@ are tried.
attempts to open the following variations of
.Fa path ,
in order:
.Bl -tag -width "/dev/rpathX"
.It Pa path
The pathname as given.
.It Pa path Ns Em X
.Fa path
with a suffix of
.Sq Em X ,
where
.Sq Em X
represents the raw partition of the device, as determined by
.Xr getrawpartition 3 ,
usually
.Dq c .
.El
.Pp
If
.Fa path
@ -93,13 +79,12 @@ slash
.Pq Dq / ,
the following variations are attempted:
.Pp
.Bl -dash -offset indent
.Bl -dash -compact
.It
If
.Fa iscooked
is zero:
.Pp
.Bl -tag -width "/dev/rpathX"
.Bl -tag -compact -width "/dev/rpathX"
.It Pa /dev/rpath
.Fa path
with a prefix of
@ -116,7 +101,7 @@ and a suffix of
If
.Fa iscooked
is non-zero:
.Bl -tag -width "/dev/rpathX"
.Bl -tag -compact -width "/dev/rpathX"
.It Pa /dev/path
.Fa path
with a prefix of
@ -130,6 +115,31 @@ and a suffix of
(q.v.).
.El
.El
.Pp
If the above fails, than the original
.Fa path
is tried using the following two variations:
.Pp
.Bl -dash -compact
.It
The
.Fa iscooked
value is ignored:
.Bl -tag -compact -width "/dev/rpathX"
.It Pa path
The pathname as given.
.It Pa path Ns Em X
.Fa path
with a suffix of
.Sq Em X ,
where
.Sq Em X
represents the raw partition of the device, as determined by
.Xr getrawpartition 3 ,
usually
.Dq c .
.El
.El
.Sh RETURN VALUES
An open file descriptor, or -1 if the
.Xr open 2
@ -170,3 +180,11 @@ The
.Fn opendisk
function first appeared in
.Nx 1.3 .
.Pp
The lookup order of
.Fn opendisk
was changed in
.Nx 8
to first look in
.Pa /dev
in order to avoid opening random files in the current working directory.

View File

@ -1,4 +1,4 @@
/* $NetBSD: opendisk.c,v 1.13 2014/09/29 21:04:52 christos Exp $ */
/* $NetBSD: opendisk.c,v 1.14 2016/06/06 17:50:19 christos Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
@ -35,13 +35,14 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: opendisk.c,v 1.13 2014/09/29 21:04:52 christos Exp $");
__RCSID("$NetBSD: opendisk.c,v 1.14 2016/06/06 17:50:19 christos Exp $");
#endif
#include <sys/param.h>
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <fcntl.h>
#ifndef HAVE_NBTOOL_CONFIG_H
#include <util.h>
@ -52,48 +53,62 @@ __RCSID("$NetBSD: opendisk.c,v 1.13 2014/09/29 21:04:52 christos Exp $");
#include <stdio.h>
#include <string.h>
static int __printflike(5, 6)
opd(char *buf, size_t len, int (*ofn)(const char *, int, ...),
int flags, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, len, fmt, ap);
va_end(ap);
return (*ofn)(buf, flags, 0);
}
static int
__opendisk(const char *path, int flags, char *buf, size_t buflen, int iscooked,
int (*ofn)(const char *, int, ...))
{
int f, rawpart;
int f, part;
if (buf == NULL) {
errno = EFAULT;
return (-1);
return -1;
}
snprintf(buf, buflen, "%s", path);
if ((flags & O_CREAT) != 0) {
errno = EINVAL;
return (-1);
return -1;
}
rawpart = getrawpartition();
if (rawpart < 0)
return (-1); /* sysctl(3) in getrawpartition sets errno */
part = getrawpartition();
if (part < 0)
return -1; /* sysctl(3) in getrawpartition sets errno */
part += 'a';
f = ofn(buf, flags, 0);
/*
* If we are passed a plain name, first try /dev to avoid accidents
* with files in the same directory that happen to match disk names.
*/
if (strchr(path, '/') == NULL) {
const char *r = iscooked ? "" : "r";
const char *d = _PATH_DEV;
f = opd(buf, buflen, ofn, flags, "%s%s%s", d, r, path);
if (f != -1 || errno != ENOENT)
return (f);
return f;
snprintf(buf, buflen, "%s%c", path, 'a' + rawpart);
f = ofn(buf, flags, 0);
f = opd(buf, buflen, ofn, flags, "%s%s%s%c", d, r, path, part);
if (f != -1 || errno != ENOENT)
return (f);
return f;
}
if (strchr(path, '/') != NULL)
return (-1);
snprintf(buf, buflen, "%s%s%s", _PATH_DEV, iscooked ? "" : "r", path);
f = ofn(buf, flags, 0);
f = opd(buf, buflen, ofn, flags, "%s", path);
if (f != -1 || errno != ENOENT)
return (f);
return f;
snprintf(buf, buflen, "%s%s%s%c", _PATH_DEV, iscooked ? "" : "r", path,
'a' + rawpart);
f = ofn(buf, flags, 0);
return (f);
return opd(buf, buflen, ofn, flags, "%s%c", path, part);
}
int