17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5dd29fa4aSprabahar * Common Development and Distribution License (the "License").
6dd29fa4aSprabahar * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*005d3febSMarek Pospisil * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/cred.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
437c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
447c478bd9Sstevel@tonic-gate #include <sys/var.h>
457c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/file.h>
487c478bd9Sstevel@tonic-gate #include <sys/mode.h>
497c478bd9Sstevel@tonic-gate #include <sys/proc.h>
507c478bd9Sstevel@tonic-gate #include <sys/uio.h>
517c478bd9Sstevel@tonic-gate #include <sys/poll.h>
527c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/filio.h>
547c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
557c478bd9Sstevel@tonic-gate #include <sys/policy.h>
567c478bd9Sstevel@tonic-gate #include <sys/zone.h>
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #include <sys/debug.h>
597c478bd9Sstevel@tonic-gate #include <c2/audit.h>
60dd29fa4aSprabahar #include <fs/fs_subr.h>
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate * Change current working directory (".").
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate static int chdirec(vnode_t *, int ischroot, int do_traverse);
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate int
chdir(char * fname)687c478bd9Sstevel@tonic-gate chdir(char *fname)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate vnode_t *vp;
717c478bd9Sstevel@tonic-gate int error;
72dd29fa4aSprabahar int estale_retry = 0;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate lookup:
757c478bd9Sstevel@tonic-gate if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
76dd29fa4aSprabahar if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
777c478bd9Sstevel@tonic-gate goto lookup;
787c478bd9Sstevel@tonic-gate return (set_errno(error));
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate error = chdirec(vp, 0, 1);
827c478bd9Sstevel@tonic-gate if (error) {
83dd29fa4aSprabahar if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
847c478bd9Sstevel@tonic-gate goto lookup;
857c478bd9Sstevel@tonic-gate return (set_errno(error));
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate return (0);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate * File-descriptor based version of 'chdir'.
927c478bd9Sstevel@tonic-gate */
937c478bd9Sstevel@tonic-gate int
fchdir(int fd)947c478bd9Sstevel@tonic-gate fchdir(int fd)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate vnode_t *vp;
977c478bd9Sstevel@tonic-gate file_t *fp;
987c478bd9Sstevel@tonic-gate int error;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate if ((fp = getf(fd)) == NULL)
1017c478bd9Sstevel@tonic-gate return (set_errno(EBADF));
1027c478bd9Sstevel@tonic-gate vp = fp->f_vnode;
1037c478bd9Sstevel@tonic-gate VN_HOLD(vp);
1047c478bd9Sstevel@tonic-gate releasef(fd);
1057c478bd9Sstevel@tonic-gate error = chdirec(vp, 0, 0);
1067c478bd9Sstevel@tonic-gate if (error)
1077c478bd9Sstevel@tonic-gate return (set_errno(error));
1087c478bd9Sstevel@tonic-gate return (0);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * Change notion of root ("/") directory.
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate int
chroot(char * fname)1157c478bd9Sstevel@tonic-gate chroot(char *fname)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate vnode_t *vp;
1187c478bd9Sstevel@tonic-gate int error;
119dd29fa4aSprabahar int estale_retry = 0;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate lookup:
1227c478bd9Sstevel@tonic-gate if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
123dd29fa4aSprabahar if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1247c478bd9Sstevel@tonic-gate goto lookup;
1257c478bd9Sstevel@tonic-gate return (set_errno(error));
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate error = chdirec(vp, 1, 1);
1297c478bd9Sstevel@tonic-gate if (error) {
130dd29fa4aSprabahar if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1317c478bd9Sstevel@tonic-gate goto lookup;
1327c478bd9Sstevel@tonic-gate return (set_errno(error));
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate return (0);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate * ++++++++++++++++++++++++
1397c478bd9Sstevel@tonic-gate * ++ SunOS4.1 Buyback ++
1407c478bd9Sstevel@tonic-gate * ++++++++++++++++++++++++
1417c478bd9Sstevel@tonic-gate * Change root directory with a user given fd
1427c478bd9Sstevel@tonic-gate */
1437c478bd9Sstevel@tonic-gate int
fchroot(int fd)1447c478bd9Sstevel@tonic-gate fchroot(int fd)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate vnode_t *vp;
1477c478bd9Sstevel@tonic-gate file_t *fp;
1487c478bd9Sstevel@tonic-gate int error;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate if ((fp = getf(fd)) == NULL)
1517c478bd9Sstevel@tonic-gate return (set_errno(EBADF));
1527c478bd9Sstevel@tonic-gate vp = fp->f_vnode;
1537c478bd9Sstevel@tonic-gate VN_HOLD(vp);
1547c478bd9Sstevel@tonic-gate releasef(fd);
1557c478bd9Sstevel@tonic-gate error = chdirec(vp, 1, 0);
1567c478bd9Sstevel@tonic-gate if (error)
1577c478bd9Sstevel@tonic-gate return (set_errno(error));
1587c478bd9Sstevel@tonic-gate return (0);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate static int
chdirec(vnode_t * vp,int ischroot,int do_traverse)1627c478bd9Sstevel@tonic-gate chdirec(vnode_t *vp, int ischroot, int do_traverse)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate int error;
1657c478bd9Sstevel@tonic-gate vnode_t *oldvp;
1667c478bd9Sstevel@tonic-gate proc_t *pp = curproc;
1677c478bd9Sstevel@tonic-gate vnode_t **vpp;
1687c478bd9Sstevel@tonic-gate refstr_t *cwd;
1697c478bd9Sstevel@tonic-gate int newcwd = 1;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate if (vp->v_type != VDIR) {
1727c478bd9Sstevel@tonic-gate error = ENOTDIR;
1737c478bd9Sstevel@tonic-gate goto bad;
1747c478bd9Sstevel@tonic-gate }
175da6c28aaSamw if (error = VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL))
1767c478bd9Sstevel@tonic-gate goto bad;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * The VOP_ACCESS() may have covered 'vp' with a new filesystem,
1807c478bd9Sstevel@tonic-gate * if 'vp' is an autoFS vnode. Traverse the mountpoint so
1817c478bd9Sstevel@tonic-gate * that we don't end up with a covered current directory.
1827c478bd9Sstevel@tonic-gate */
1837c478bd9Sstevel@tonic-gate if (vn_mountedvfs(vp) != NULL && do_traverse) {
1847c478bd9Sstevel@tonic-gate if (error = traverse(&vp))
1857c478bd9Sstevel@tonic-gate goto bad;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate * Special chroot semantics: chroot is allowed if privileged
1907c478bd9Sstevel@tonic-gate * or if the target is really a loopback mount of the root (or
1917c478bd9Sstevel@tonic-gate * root of the zone) as determined by comparing dev and inode
1927c478bd9Sstevel@tonic-gate * numbers
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate if (ischroot) {
1957c478bd9Sstevel@tonic-gate struct vattr tattr;
1967c478bd9Sstevel@tonic-gate struct vattr rattr;
1977c478bd9Sstevel@tonic-gate vnode_t *zonevp = curproc->p_zone->zone_rootvp;
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate tattr.va_mask = AT_FSID|AT_NODEID;
200da6c28aaSamw if (error = VOP_GETATTR(vp, &tattr, 0, CRED(), NULL))
2017c478bd9Sstevel@tonic-gate goto bad;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate rattr.va_mask = AT_FSID|AT_NODEID;
204da6c28aaSamw if (error = VOP_GETATTR(zonevp, &rattr, 0, CRED(), NULL))
2057c478bd9Sstevel@tonic-gate goto bad;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate if ((tattr.va_fsid != rattr.va_fsid ||
2087c478bd9Sstevel@tonic-gate tattr.va_nodeid != rattr.va_nodeid) &&
2097c478bd9Sstevel@tonic-gate (error = secpolicy_chroot(CRED())) != 0)
2107c478bd9Sstevel@tonic-gate goto bad;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate vpp = &PTOU(pp)->u_rdir;
2137c478bd9Sstevel@tonic-gate } else {
2147c478bd9Sstevel@tonic-gate vpp = &PTOU(pp)->u_cdir;
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
217*005d3febSMarek Pospisil /* update abs cwd/root path see c2/audit.c */
218*005d3febSMarek Pospisil if (AU_AUDITING())
2197c478bd9Sstevel@tonic-gate audit_chdirec(vp, vpp);
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock);
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate * This bit of logic prevents us from overwriting u_cwd if we are
2247c478bd9Sstevel@tonic-gate * changing to the same directory. We set the cwd to NULL so that we
2257c478bd9Sstevel@tonic-gate * don't try to do the lookup on the next call to getcwd().
2267c478bd9Sstevel@tonic-gate */
2277c478bd9Sstevel@tonic-gate if (!ischroot && *vpp != NULL && vp != NULL && VN_CMP(*vpp, vp))
2287c478bd9Sstevel@tonic-gate newcwd = 0;
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate oldvp = *vpp;
2317c478bd9Sstevel@tonic-gate *vpp = vp;
2327c478bd9Sstevel@tonic-gate if ((cwd = PTOU(pp)->u_cwd) != NULL && newcwd)
2337c478bd9Sstevel@tonic-gate PTOU(pp)->u_cwd = NULL;
2347c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock);
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate if (cwd && newcwd)
2377c478bd9Sstevel@tonic-gate refstr_rele(cwd);
2387c478bd9Sstevel@tonic-gate if (oldvp)
2397c478bd9Sstevel@tonic-gate VN_RELE(oldvp);
2407c478bd9Sstevel@tonic-gate return (0);
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate bad:
2437c478bd9Sstevel@tonic-gate VN_RELE(vp);
2447c478bd9Sstevel@tonic-gate return (error);
2457c478bd9Sstevel@tonic-gate }
246