NetBSD Problem Report #1043
From gnats Thu May 11 16:06:53 1995
Received: from pain.lcs.mit.edu (pain.lcs.mit.edu [128.52.46.239]) by sun-lamp.cs.berkeley.edu (8.6.10/8.6.10) with ESMTP id QAA02552 for <gnats-bugs@sun-lamp.cs.berkeley.edu>; Thu, 11 May 1995 16:06:52 -0700
Message-Id: <199505112248.SAA19380@kolvir.blrc.ma.us>
Date: Thu, 11 May 1995 18:48:00 -0400
From: John Kohl <jtk@kolvir.blrc.ma.us>
Reply-To: jtk@kolvir.blrc.ma.us
To: gnats-bugs@NetBSD.ORG
Subject: unlink(2) should not let superuser remove directories
X-Send-Pr-Version: 3.2
>Number: 1043
>Category: kern
>Synopsis: root can unlink directories, causing FS corruption
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: kern-bug-people
>State: closed
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Thu May 11 16:20:04 +0000 1995
>Closed-Date: Fri Feb 09 15:01:27 +0000 1996
>Last-Modified: Fri Feb 09 15:04:21 +0000 1996
>Originator: John Kohl
>Release: 1.0A (-current)
>Organization:
NetBSD Kernel Hackers `R` Us
>Environment:
System: NetBSD kolvir 1.0A NetBSD 1.0A (KOLVIR) #318: Mon May 8 23:09:33 EDT 1995 jtk@kolvir:/u1/NetBSD-current/src/sys/arch/i386/compile/KOLVIR i386
>Description:
The superuser can remove directories with the unlink(2) system call,
even if the directory is not empty. This can lead to unreferenced files
on a filesystem which will never be detected if fsck(8) doesn't run on
the filesystems (it won't, at least not until its timeout expires, if
things are unmounted cleanly at each reboot).
>How-To-Repeat:
main(int argc, char *argv[])
{
unlink(argv[1]);
}
>Fix:
===================================================================
RCS file: RCS/vfs_syscalls.c,v
retrieving revision 1.1.1.5
diff -u18 -r1.1.1.5 vfs_syscalls.c
--- 1.1.1.5 1995/05/11 22:41:59
+++ sys/kern/vfs_syscalls.c 1995/05/11 22:42:13
@@ -1044,45 +1044,48 @@
unlink(p, uap, retval)
struct proc *p;
struct unlink_args /* {
syscallarg(char *) path;
} */ *uap;
register_t *retval;
{
register struct vnode *vp;
int error;
struct nameidata nd;
NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
if (error = namei(&nd))
return (error);
vp = nd.ni_vp;
VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
VOP_LOCK(vp);
- if (vp->v_type != VDIR ||
- (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
+ if (vp->v_type == VDIR) {
+ error = EISDIR;
+ } else {
/*
* The root of a mounted filesystem cannot be deleted.
*/
if (vp->v_flag & VROOT)
error = EBUSY;
- else
+ else {
(void)vnode_pager_uncache(vp);
+ error = 0;
+ }
}
if (!error) {
VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
} else {
VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
if (nd.ni_dvp == vp)
vrele(nd.ni_dvp);
else
vput(nd.ni_dvp);
if (vp != NULLVP)
vput(vp);
}
return (error);
}
/*
>Release-Note:
>Audit-Trail:
From: John Kohl <jtk@kolvir.blrc.ma.us>
To: gnats-bugs@netbsd.org
Subject: Re: kern/1043: unlink(2) should not let superuser remove directories
Date: Sat, 13 May 1995 00:43:17 -0400
I missed the corresponding change to link(2). Here it is:
===================================================================
RCS file: RCS/vfs_syscalls.c,v
retrieving revision 1.7
diff -u16 -r1.7 vfs_syscalls.c
--- 1.7 1995/05/11 22:42:17
+++ vfs_syscalls.c 1995/05/13 04:41:51
@@ -916,34 +916,35 @@
link(p, uap, retval)
struct proc *p;
register struct link_args /* {
syscallarg(char *) path;
syscallarg(char *) link;
} */ *uap;
register_t *retval;
{
register struct vnode *vp;
struct nameidata nd;
int error;
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
if (error = namei(&nd))
return (error);
vp = nd.ni_vp;
- if (vp->v_type != VDIR ||
- (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
+ if (vp->v_type == VDIR) {
+ error = EISDIR;
+ } else {
nd.ni_cnd.cn_nameiop = CREATE;
nd.ni_cnd.cn_flags = LOCKPARENT;
nd.ni_dirp = SCARG(uap, link);
if ((error = namei(&nd)) == 0) {
if (nd.ni_vp != NULL)
error = EEXIST;
if (!error) {
VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
LEASE_WRITE);
VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
} else {
VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
if (nd.ni_dvp == nd.ni_vp)
vrele(nd.ni_dvp);
else
From: John Kohl <jtk@kolvir.arlington.ma.us>
To: gnats-bugs@NetBSD.ORG
Cc: core@NetBSD.ORG
Subject: Re: kern/1043: root can unlink directories, causing FS corruption
Date: Thu, 14 Sep 1995 23:33:33 -0400
I have put an updated version of fsdb (it now uses libedit) onto
pain.lcs.mit.edu:~jtk/fsdb.tar.gz, and also onto
ftp://sipb.mit.edu/pub/netbsd/fsdb.tar.gz
Is this something you'd like to add to the source tree?
It can go either in /usr/sbin or /sbin; the former will take less space
but the latter might be more useful in a pinch. Since it sucks in FSCK
code, it's not tiny :(
Given this plus fixes for mount -o reload, can we apply the patches I
sent in kern/1043?
==John
State-Changed-From-To: open->closed
State-Changed-By: mycroft
State-Changed-When: Fri Feb 9 10:01:27 EST 1996
State-Changed-Why:
The proposed changes are really not correct; the ability to link and unlink
directories is a property of the file system. I've implemented a more
appropriate change to prevent trashing FFS file systems.
>Unformatted:
(Contact us)
$NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.