tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

factoring out the change_root() and making exporting change_{root,dir}() as interface



I'd like to factor out the common code of the *chroot syscalls like in
the following patch.  I'd also like to export change_dir() and the new
change_root() as an interface from vfs_syscalls.h

I want to use change_dir() and change_root() in a different kernel module.

I've been the code in 5.99.4 kernel for a while.  But this is the diff ported
to 5.99.15 for discussion purposes.

Comments? Objections?
--chris

Index: kern/vfs_syscalls.c
===================================================================
RCS file: /cvsroot/src/sys/kern/vfs_syscalls.c,v
retrieving revision 1.396
diff -u -r1.396 vfs_syscalls.c
--- kern/vfs_syscalls.c 2 Jul 2009 12:53:47 -0000       1.396
+++ kern/vfs_syscalls.c 28 Jul 2009 22:23:32 -0000
@@ -110,7 +110,6 @@
 
 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
 
-static int change_dir(struct nameidata *, struct lwp *);
 static int change_flags(struct vnode *, u_long, struct lwp *);
 static int change_mode(struct vnode *, int, struct lwp *l);
 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
@@ -1126,7 +1125,6 @@
 sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t 
*retval)
 {
        struct proc *p = l->l_proc;
-       struct cwdinfo *cwdi;
        struct vnode    *vp;
        file_t  *fp;
        int              error, fd = SCARG(uap, fd);
@@ -1135,7 +1133,7 @@
            KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
                return error;
        /* fd_getvnode() will use the descriptor for us */
-       if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
+       if ((error = fd_getvnode(fd, &fp)) != 0)
                return error;
        vp = fp->f_data;
        vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
@@ -1148,27 +1146,7 @@
                goto out;
        VREF(vp);
 
-       /*
-        * Prevent escaping from chroot by putting the root under
-        * the working directory.  Silently chdir to / if we aren't
-        * already there.
-        */
-       cwdi = p->p_cwdi;
-       rw_enter(&cwdi->cwdi_lock, RW_WRITER);
-       if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
-               /*
-                * XXX would be more failsafe to change directory to a
-                * deadfs node here instead
-                */
-               vrele(cwdi->cwdi_cdir);
-               VREF(vp);
-               cwdi->cwdi_cdir = vp;
-       }
-
-       if (cwdi->cwdi_rdir != NULL)
-               vrele(cwdi->cwdi_rdir);
-       cwdi->cwdi_rdir = vp;
-       rw_exit(&cwdi->cwdi_lock);
+       change_root(p->p_cwdi, vp, l);
 
  out:
        fd_putfile(fd);
@@ -1226,11 +1204,21 @@
        if ((error = change_dir(&nd, l)) != 0)
                return (error);
 
-       cwdi = p->p_cwdi;
+       change_root(p->p_cwdi, nd.ni_vp, l);
+
+       return (0);
+}
+
+/*
+ * Common routine for chroot and fchroot.
+ */
+void
+change_root(struct cwdinfo *cwdi, struct vnode *vp, struct lwp *l)
+{
+
        rw_enter(&cwdi->cwdi_lock, RW_WRITER);
        if (cwdi->cwdi_rdir != NULL)
                vrele(cwdi->cwdi_rdir);
-       vp = nd.ni_vp;
        cwdi->cwdi_rdir = vp;
 
        /*
@@ -1248,14 +1236,12 @@
                cwdi->cwdi_cdir = vp;
        }
        rw_exit(&cwdi->cwdi_lock);
-
-       return (0);
 }
 
 /*
  * Common routine for chroot and chdir.
  */
-static int
+int
 change_dir(struct nameidata *ndp, struct lwp *l)
 {
        struct vnode *vp;
Index: sys/vfs_syscalls.h
===================================================================
RCS file: /cvsroot/src/sys/sys/vfs_syscalls.h,v
retrieving revision 1.11
diff -u -r1.11 vfs_syscalls.h
--- sys/vfs_syscalls.h  2 Jul 2009 12:56:40 -0000       1.11
+++ sys/vfs_syscalls.h  28 Jul 2009 22:23:33 -0000
@@ -65,4 +65,7 @@
 int    do_sys_mknod(struct lwp *l, const char *, mode_t, dev_t, register_t *);
 int    do_sys_mkdir(const char *, mode_t);
 
+int change_dir(struct nameidata *, struct lwp *);
+void change_root(struct cwdinfo *, struct vnode *, struct lwp *);
+
 #endif /* _SYS_VFS_SYSCALLS_H_ */


Home | Main Index | Thread Index | Old Index