Add support for booting from cd9660 fs

Support directory traversal and symbolic links for nfs booting
Close device when file opening failed
Plug memory leak in ufs code
This commit is contained in:
ws 1996-09-30 16:01:18 +00:00
parent d67d39b37c
commit fd225e6527
7 changed files with 625 additions and 22 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.11 1996/01/13 22:25:32 leo Exp $
# $NetBSD: Makefile,v 1.12 1996/09/30 16:01:18 ws Exp $
LIB= sa
@ -6,9 +6,12 @@ DIR=${SAREL}${SADIR}
.PATH: ${DIR}
#DEBUGFLAGS= -DNETIF_DEBUG -DETHER_DEBUG -DNFS_DEBUG -DRPC_DEBUG -DRARP_DEBUG -DDEBUG -DPARANOID -Wall
#DEBUGFLAGS= -DBOOTP_DEBUG -DNETIF_DEBUG -DETHER_DEBUG -DNFS_DEBUG -DRPC_DEBUG -DRARP_DEBUG -DDEBUG -DPARANOID -Wall
#DEBUGFLAGS= -ansi -pedantic -Wall
CFLAGS+=-DSTANDALONE -DCOMPAT_UFS $(DEBUGFLAGS) -I${DIR} -I${DIR}/../..
CFLAGS+=-DSTANDALONE -DCOMPAT_UFS $(DEBUGFLAGS) -I. -I${DIR} -I${DIR}/../..
#
# Needed for PowerPC
CFLAGS+=$(EXTRACFLAGS)
# stand routines
SRCS+= alloc.c bcopy.c exit.c exec.c getfile.c gets.c globals.c \
@ -27,7 +30,7 @@ SRCS+= arp.c ether.c in_cksum.c net.c netif.c rpc.c
SRCS+= bootp.c rarp.c bootparam.c
# boot filesystems
SRCS+= ufs.c nfs.c
SRCS+= ufs.c nfs.c cd9660.c
NOPROFILE=
NOPIC=

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile.inc,v 1.4 1995/10/01 06:00:38 phil Exp $
# $NetBSD: Makefile.inc,v 1.5 1996/09/30 16:01:18 ws Exp $
#
# NOTE: $S must correspond to the top of the 'sys' tree
SADIR= $S/lib/libsa
@ -14,14 +14,16 @@ ${SALIB}: .NOTMAIN __always_make_salib
KERNCC="${CC}" \
KERNCFLAGS="${CFLAGS}" \
SAREL="${SAREL}" \
SADIR="${SADIR}" libsa.a)
SADIR="${SADIR}" \
EXTRACFLAGS="${EXTRACFLAGS}" libsa.a)
.else
@(cd ${SADST} && ${MAKE} -f ${SAREL}${SADIR}/Makefile \
KERNCC="${CC}" \
KERNCFLAGS="${CFLAGS}" \
SAREL="${SAREL}" \
SADIR="${SADIR}" \
NO_NET="" libsa.a)
NO_NET="" \
EXTRACFLAGS="${EXTRACFLAGS}" libsa.a)
.endif
clean:: .NOTMAIN __always_make_salib

405
sys/lib/libsa/cd9660.c Normal file
View File

@ -0,0 +1,405 @@
/* $NetBSD: cd9660.c,v 1.1 1996/09/30 16:01:19 ws Exp $ */
/*
* Copyright (C) 1996 Wolfgang Solfrank.
* Copyright (C) 1996 TooLs GmbH.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by TooLs GmbH.
* 4. The name of TooLs GmbH may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Stand-alone ISO9660 file reading package.
*
* Note: This doesn't support Rock Ridge extensions, extended attributes,
* blocksizes other than 2048 bytes, multi-extent files, etc.
*/
#include <sys/param.h>
#include <lib/libkern/libkern.h>
/* THIS IS AN UGLY HACK!!! XXX */
struct fid;
struct mbuf;
struct nameidata;
struct netexport { int x; };
struct proc;
struct statfs;
struct ucred;
#include <isofs/cd9660/iso.h>
/* These once were in iso.h, but got deleted??? */
extern __inline int
isonum_722(p)
unsigned char *p;
{
return ((char)*p << 8)|p[1];
}
extern __inline int
isonum_732(p)
unsigned char *p;
{
return (*p << 24)|(p[1] << 16)|(p[2] << 8)|p[3];
}
#include "stand.h"
#include "cd9660.h"
struct file {
off_t off; /* Current offset within file */
daddr_t bno; /* Starting block number */
off_t size; /* Size of file */
};
struct ptable_ent {
char namlen [ISODCL( 1, 1)]; /* 711 */
char extlen [ISODCL( 2, 2)]; /* 711 */
char block [ISODCL( 3, 6)]; /* 732 */
char parent [ISODCL( 7, 8)]; /* 722 */
char name [1];
};
#define PTFIXSZ 8
#define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
#define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
static int
toupper(c)
int c;
{
return c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c;
}
static int
pnmatch(path, pp)
char *path;
struct ptable_ent *pp;
{
char *cp;
int i;
cp = pp->name;
for (i = isonum_711(pp->namlen); --i >= 0; path++, cp++) {
if (toupper(*path) == *cp)
continue;
return 0;
}
if (*path != '/')
return 0;
return 1;
}
static int
dirmatch(path, dp)
char *path;
struct iso_directory_record *dp;
{
char *cp;
int i;
/* This needs to be a regular file */
if (dp->flags[0] & 6)
return 0;
cp = dp->name;
for (i = isonum_711(dp->name_len); --i >= 0; path++, cp++) {
if (!*path)
break;
if (toupper(*path) == *cp)
continue;
return 0;
}
if (*path)
return 0;
/*
* Allow stripping of trailing dots and the version number.
* Note that this will find the first instead of the last version
* of a file.
*/
if (i >= 0 && (*cp == ';' || *cp == '.')) {
/* This is to prevent matching of numeric extensions */
if (*cp == '.' && cp[1] != ';')
return 0;
while (--i >= 0)
if (*++cp != ';' && (*cp < '0' || *cp > '9'))
return 0;
}
return 1;
}
int
cd9660_open(path, f)
char *path;
struct open_file *f;
{
struct file *fp = 0;
void *buf;
struct iso_primary_descriptor *vd;
size_t buf_size, read, psize, dsize;
daddr_t bno;
int parent, ent;
struct ptable_ent *pp;
struct iso_directory_record *dp;
int rc;
/* First find the volume descriptor */
buf = alloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
vd = buf;
for (bno = 16;; bno++) {
twiddle();
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
ISO_DEFAULT_BLOCK_SIZE, buf, &read);
if (rc)
goto out;
if (read != ISO_DEFAULT_BLOCK_SIZE) {
rc = EIO;
goto out;
}
rc = EINVAL;
if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
goto out;
if (isonum_711(vd->type) == ISO_VD_END)
goto out;
if (isonum_711(vd->type) == ISO_VD_PRIMARY)
break;
}
if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
goto out;
/* Now get the path table and lookup the directory of the file */
bno = isonum_732(vd->type_m_path_table);
psize = isonum_733(vd->path_table_size);
if (psize > ISO_DEFAULT_BLOCK_SIZE) {
free(buf, ISO_DEFAULT_BLOCK_SIZE);
buf = alloc(buf_size = roundup(psize, ISO_DEFAULT_BLOCK_SIZE));
}
twiddle();
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
buf_size, buf, &read);
if (rc)
goto out;
if (read != buf_size) {
rc = EIO;
goto out;
}
parent = 1;
pp = (struct ptable_ent *)buf;
ent = 1;
bno = isonum_732(pp->block) + isonum_711(pp->extlen);
rc = ENOENT;
while (*path) {
if ((void *)pp >= buf + psize)
break;
if (isonum_722(pp->parent) != parent)
break;
if (!pnmatch(path, pp)) {
pp = (struct ptable_ent *)((void *)pp + PTSIZE(pp));
ent++;
continue;
}
path += isonum_711(pp->namlen) + 1;
parent = ent;
bno = isonum_732(pp->block) + isonum_711(pp->extlen);
while ((void *)pp < buf + psize) {
if (isonum_722(pp->parent) == parent)
break;
pp = (struct ptable_ent *)((void *)pp + PTSIZE(pp));
ent++;
}
}
/* Now bno has the start of the directory that supposedly contains the file */
bno--;
dsize = 1; /* Something stupid, but > 0 XXX */
for (psize = 0; psize < dsize;) {
if (!(psize % ISO_DEFAULT_BLOCK_SIZE)) {
bno++;
twiddle();
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
cdb2devb(bno),
ISO_DEFAULT_BLOCK_SIZE,
buf, &read);
if (rc)
goto out;
if (read != ISO_DEFAULT_BLOCK_SIZE) {
rc = EIO;
goto out;
}
dp = (struct iso_directory_record *)buf;
}
if (!isonum_711(dp->length)) {
if ((void *)dp == buf)
psize += ISO_DEFAULT_BLOCK_SIZE;
else
psize = roundup(psize, ISO_DEFAULT_BLOCK_SIZE);
continue;
}
if (dsize == 1)
dsize = isonum_733(dp->size);
if (dirmatch(path, dp))
break;
psize += isonum_711(dp->length);
dp = (struct iso_directory_record *)((void *)dp + isonum_711(dp->length));
}
if (psize >= dsize) {
rc = ENOENT;
goto out;
}
/* allocate file system specific data structure */
fp = alloc(sizeof(struct file));
bzero(fp, sizeof(struct file));
f->f_fsdata = (void *)fp;
fp->off = 0;
fp->bno = isonum_733(dp->extent);
fp->size = isonum_733(dp->size);
free(buf, buf_size);
return 0;
out:
if (fp)
free(fp, sizeof(struct file));
free(buf, buf_size);
return rc;
}
int
cd9660_close(f)
struct open_file *f;
{
struct file *fp = (struct file *)f->f_fsdata;
f->f_fsdata = 0;
free(fp, sizeof *fp);
return 0;
}
int
cd9660_read(f, start, size, resid)
struct open_file *f;
void *start;
size_t size;
size_t *resid;
{
struct file *fp = (struct file *)f->f_fsdata;
int rc = 0;
daddr_t bno;
char buf[ISO_DEFAULT_BLOCK_SIZE];
char *dp;
size_t read, off;
while (size) {
if (fp->off < 0 || fp->off >= fp->size)
break;
bno = fp->off / ISO_DEFAULT_BLOCK_SIZE + fp->bno;
if (fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1)
|| size < ISO_DEFAULT_BLOCK_SIZE)
dp = buf;
else
dp = start;
twiddle();
rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
ISO_DEFAULT_BLOCK_SIZE, dp, &read);
if (rc)
return rc;
if (read != ISO_DEFAULT_BLOCK_SIZE)
return EIO;
if (dp == buf) {
off = fp->off & (ISO_DEFAULT_BLOCK_SIZE - 1);
if (read > off + size)
read = off + size;
read -= off;
bcopy(buf + off, start, read);
start += read;
fp->off += read;
size -= read;
} else {
start += ISO_DEFAULT_BLOCK_SIZE;
fp->off += ISO_DEFAULT_BLOCK_SIZE;
size -= ISO_DEFAULT_BLOCK_SIZE;
}
}
if (resid)
*resid = size;
return rc;
}
int
cd9660_write(f, start, size, resid)
struct open_file *f;
void *start;
size_t size;
size_t *resid;
{
return EROFS;
}
off_t
cd9660_seek(f, offset, where)
struct open_file *f;
off_t offset;
int where;
{
struct file *fp = (struct file *)f->f_fsdata;
switch (where) {
case SEEK_SET:
fp->off = offset;
break;
case SEEK_CUR:
fp->off += offset;
break;
case SEEK_END:
fp->off = fp->size - offset;
break;
default:
return -1;
}
return fp->off;
}
int
cd9660_stat(f, sb)
struct open_file *f;
struct stat *sb;
{
struct file *fp = (struct file *)f->f_fsdata;
/* only importatn stuff */
sb->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
sb->st_uid = sb->st_gid = 0;
sb->st_size = fp->size;
return 0;
}

41
sys/lib/libsa/cd9660.h Normal file
View File

@ -0,0 +1,41 @@
/* $NetBSD: cd9660.h,v 1.1 1996/09/30 16:01:20 ws Exp $ */
/*
* Copyright (C) 1996 Wolfgang Solfrank.
* Copyright (C) 1996 TooLs GmbH.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by TooLs GmbH.
* 4. The name of TooLs GmbH may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
int cd9660_open __P((char *path, struct open_file *f));
int cd9660_close __P((struct open_file *f));
int cd9660_read __P((struct open_file *f, void *buf,
size_t size, size_t *resid));
int cd9660_write __P((struct open_file *f, void *buf,
size_t size, size_t *resid));
off_t cd9660_seek __P((struct open_file *f, off_t offset, int where));
int cd9660_stat __P((struct open_file *f, struct stat *sb));

View File

@ -1,4 +1,4 @@
/* $NetBSD: nfs.c,v 1.15 1996/05/14 10:28:26 leo Exp $ */
/* $NetBSD: nfs.c,v 1.16 1996/09/30 16:01:20 ws Exp $ */
/*-
* Copyright (c) 1993 John Brezak
@ -81,6 +81,12 @@ struct nfs_read_repl {
u_char data[NFSREAD_SIZE];
};
struct nfs_readlnk_repl {
n_long errno;
n_long len;
char path[NFS_MAXPATHLEN];
};
struct nfs_iodesc {
struct iodesc *iodesc;
off_t off;
@ -220,6 +226,50 @@ nfs_lookupfh(d, name, newfd)
return (0);
}
/*
* Get the destination of a symbolic link.
*/
int
nfs_readlink(d, buf)
struct nfs_iodesc *d;
char *buf;
{
struct {
n_long h[RPC_HEADER_WORDS];
u_char fh[NFS_FHSIZE];
} sdata;
struct {
n_long h[RPC_HEADER_WORDS];
struct nfs_readlnk_repl d;
} rdata;
ssize_t cc;
#ifdef NFS_DEBUG
if (debug)
printf("readlink: called\n");
#endif
bcopy(d->fh, sdata.fh, NFS_FHSIZE);
cc = rpc_call(d->iodesc, NFS_PROG, NFS_VER2, NFSPROC_READLINK,
sdata.fh, NFS_FHSIZE,
&rdata.d, sizeof(rdata.d));
if (cc == -1)
return (errno);
if (cc < 4)
return (EIO);
if (rdata.d.errno)
return (ntohl(rdata.d.errno));
if (rdata.d.len > NFS_MAXPATHLEN)
return (ENAMETOOLONG);
bcopy(rdata.d.path, buf, rdata.d.len);
buf[rdata.d.len] = 0;
return (0);
}
/*
* Read data from a file.
* Return transfer count or -1 (and set errno)
@ -329,7 +379,12 @@ nfs_open(path, f)
char *path;
struct open_file *f;
{
struct nfs_iodesc *newfd;
struct nfs_iodesc *newfd, *currfd;
register char *cp, *ncp;
register int c;
char namebuf[NFS_MAXPATHLEN + 1];
char linkbuf[NFS_MAXPATHLEN + 1];
int nlinks = 0;
int error = 0;
#ifdef NFS_DEBUG
@ -341,24 +396,118 @@ nfs_open(path, f)
return (ENXIO);
}
/* allocate file system specific data structure */
newfd = alloc(sizeof(*newfd));
newfd->iodesc = nfs_root_node.iodesc;
newfd->off = 0;
currfd = &nfs_root_node;
newfd = 0;
cp = path;
while (*cp) {
/*
* Remove extra separators
*/
while (*cp == '/')
cp++;
/* lookup a file handle */
error = nfs_lookupfh(&nfs_root_node, path, newfd);
if (!error) {
f->f_fsdata = (void *)newfd;
return (0);
if (*cp == '\0')
break;
/*
* Check that current node is a directory.
*/
if (currfd->fa.fa_type != NFDIR) {
error = ENOTDIR;
goto out;
}
/* allocate file system specific data structure */
newfd = alloc(sizeof(*newfd));
newfd->iodesc = currfd->iodesc;
newfd->off = 0;
/*
* Get next component of path name.
*/
{
register int len = 0;
ncp = cp;
while ((c = *cp) != '\0' && c != '/') {
if (++len > NFS_MAXNAMLEN) {
error = ENOENT;
goto out;
}
cp++;
}
*cp = '\0';
}
/* lookup a file handle */
error = nfs_lookupfh(currfd, ncp, newfd);
*cp = c;
if (error)
goto out;
/*
* Check for symbolic link
*/
if (newfd->fa.fa_type == NFLNK) {
int link_len, len;
error = nfs_readlink(newfd, linkbuf);
if (error)
goto out;
link_len = strlen(linkbuf);
len = strlen(cp);
if (link_len + len > MAXPATHLEN
|| ++nlinks > MAXSYMLINKS) {
error = ENOENT;
goto out;
}
bcopy(cp, &namebuf[link_len], len + 1);
bcopy(linkbuf, namebuf, link_len);
/*
* If absolute pathname, restart at root.
* If relative pathname, restart at parent directory.
*/
cp = namebuf;
if (*cp == '/') {
if (currfd != &nfs_root_node)
free(currfd, sizeof(*currfd));
currfd = &nfs_root_node;
}
free(newfd, sizeof(*newfd));
newfd = 0;
continue;
}
if (currfd != &nfs_root_node)
free(currfd, sizeof(*currfd));
currfd = newfd;
newfd = 0;
}
error = 0;
out:
if (!error) {
f->f_fsdata = (void *)currfd;
return (0);
}
#ifdef NFS_DEBUG
if (debug)
printf("nfs_open: %s lookupfh failed: %s\n",
path, strerror(error));
#endif
free(newfd, sizeof(*newfd));
if (currfd != &nfs_root_node)
free(currfd, sizeof(*currfd));
if (newfd)
free(newfd, sizeof(*newfd));
return (error);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: open.c,v 1.11 1996/06/21 20:51:23 pk Exp $ */
/* $NetBSD: open.c,v 1.12 1996/09/30 16:01:21 ws Exp $ */
/*-
* Copyright (c) 1993
@ -119,6 +119,7 @@ fnd:
if (!error)
error = ENOENT;
f->f_dev->dv_close(f);
err:
f->f_flags = 0;
errno = error;

View File

@ -1,4 +1,4 @@
/* $NetBSD: ufs.c,v 1.15 1996/06/02 13:28:21 ragge Exp $ */
/* $NetBSD: ufs.c,v 1.16 1996/09/30 16:01:22 ws Exp $ */
/*-
* Copyright (c) 1993
@ -554,8 +554,10 @@ ufs_open(path, f)
out:
if (buf)
free(buf, fs->fs_bsize);
if (rc)
if (rc) {
free(fp->f_fs, SBSIZE);
free(fp, sizeof(struct file));
}
return (rc);
}