xref: /titanic_52/usr/src/uts/common/syscall/chdir.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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*da6c28aaSamw  * Copyright 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/cred.h>
417c478bd9Sstevel@tonic-gate #include <sys/user.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/errno.h>
447c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
457c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
467c478bd9Sstevel@tonic-gate #include <sys/var.h>
477c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
487c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
497c478bd9Sstevel@tonic-gate #include <sys/file.h>
507c478bd9Sstevel@tonic-gate #include <sys/mode.h>
517c478bd9Sstevel@tonic-gate #include <sys/proc.h>
527c478bd9Sstevel@tonic-gate #include <sys/uio.h>
537c478bd9Sstevel@tonic-gate #include <sys/poll.h>
547c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
557c478bd9Sstevel@tonic-gate #include <sys/filio.h>
567c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
577c478bd9Sstevel@tonic-gate #include <sys/policy.h>
587c478bd9Sstevel@tonic-gate #include <sys/zone.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <sys/debug.h>
617c478bd9Sstevel@tonic-gate #include <c2/audit.h>
62dd29fa4aSprabahar #include <fs/fs_subr.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Change current working directory (".").
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate static int	chdirec(vnode_t *, int ischroot, int do_traverse);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate int
707c478bd9Sstevel@tonic-gate chdir(char *fname)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	vnode_t *vp;
737c478bd9Sstevel@tonic-gate 	int error;
74dd29fa4aSprabahar 	int estale_retry = 0;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate lookup:
777c478bd9Sstevel@tonic-gate 	if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
78dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
797c478bd9Sstevel@tonic-gate 			goto lookup;
807c478bd9Sstevel@tonic-gate 		return (set_errno(error));
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	error = chdirec(vp, 0, 1);
847c478bd9Sstevel@tonic-gate 	if (error) {
85dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
867c478bd9Sstevel@tonic-gate 			goto lookup;
877c478bd9Sstevel@tonic-gate 		return (set_errno(error));
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	return (0);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * File-descriptor based version of 'chdir'.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate int
967c478bd9Sstevel@tonic-gate fchdir(int fd)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	vnode_t *vp;
997c478bd9Sstevel@tonic-gate 	file_t *fp;
1007c478bd9Sstevel@tonic-gate 	int error;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if ((fp = getf(fd)) == NULL)
1037c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
1047c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
1057c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
1067c478bd9Sstevel@tonic-gate 	releasef(fd);
1077c478bd9Sstevel@tonic-gate 	error = chdirec(vp, 0, 0);
1087c478bd9Sstevel@tonic-gate 	if (error)
1097c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1107c478bd9Sstevel@tonic-gate 	return (0);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Change notion of root ("/") directory.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate int
1177c478bd9Sstevel@tonic-gate chroot(char *fname)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1207c478bd9Sstevel@tonic-gate 	int error;
121dd29fa4aSprabahar 	int estale_retry = 0;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate lookup:
1247c478bd9Sstevel@tonic-gate 	if (error = lookupname(fname, UIO_USERSPACE, FOLLOW, NULLVPP, &vp)) {
125dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1267c478bd9Sstevel@tonic-gate 			goto lookup;
1277c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	error = chdirec(vp, 1, 1);
1317c478bd9Sstevel@tonic-gate 	if (error) {
132dd29fa4aSprabahar 		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
1337c478bd9Sstevel@tonic-gate 			goto lookup;
1347c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	return (0);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  *	++++++++++++++++++++++++
1417c478bd9Sstevel@tonic-gate  *	++  SunOS4.1 Buyback  ++
1427c478bd9Sstevel@tonic-gate  *	++++++++++++++++++++++++
1437c478bd9Sstevel@tonic-gate  * Change root directory with a user given fd
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate int
1467c478bd9Sstevel@tonic-gate fchroot(int fd)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1497c478bd9Sstevel@tonic-gate 	file_t *fp;
1507c478bd9Sstevel@tonic-gate 	int error;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if ((fp = getf(fd)) == NULL)
1537c478bd9Sstevel@tonic-gate 		return (set_errno(EBADF));
1547c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
1557c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
1567c478bd9Sstevel@tonic-gate 	releasef(fd);
1577c478bd9Sstevel@tonic-gate 	error = chdirec(vp, 1, 0);
1587c478bd9Sstevel@tonic-gate 	if (error)
1597c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1607c478bd9Sstevel@tonic-gate 	return (0);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static int
1647c478bd9Sstevel@tonic-gate chdirec(vnode_t *vp, int ischroot, int do_traverse)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	int error;
1677c478bd9Sstevel@tonic-gate 	vnode_t *oldvp;
1687c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
1697c478bd9Sstevel@tonic-gate 	vnode_t **vpp;
1707c478bd9Sstevel@tonic-gate 	refstr_t *cwd;
1717c478bd9Sstevel@tonic-gate 	int newcwd = 1;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
1747c478bd9Sstevel@tonic-gate 		error = ENOTDIR;
1757c478bd9Sstevel@tonic-gate 		goto bad;
1767c478bd9Sstevel@tonic-gate 	}
177*da6c28aaSamw 	if (error = VOP_ACCESS(vp, VEXEC, 0, CRED(), NULL))
1787c478bd9Sstevel@tonic-gate 		goto bad;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * The VOP_ACCESS() may have covered 'vp' with a new filesystem,
1827c478bd9Sstevel@tonic-gate 	 * if 'vp' is an autoFS vnode. Traverse the mountpoint so
1837c478bd9Sstevel@tonic-gate 	 * that we don't end up with a covered current directory.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	if (vn_mountedvfs(vp) != NULL && do_traverse) {
1867c478bd9Sstevel@tonic-gate 		if (error = traverse(&vp))
1877c478bd9Sstevel@tonic-gate 			goto bad;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/*
1917c478bd9Sstevel@tonic-gate 	 * Special chroot semantics: chroot is allowed if privileged
1927c478bd9Sstevel@tonic-gate 	 * or if the target is really a loopback mount of the root (or
1937c478bd9Sstevel@tonic-gate 	 * root of the zone) as determined by comparing dev and inode
1947c478bd9Sstevel@tonic-gate 	 * numbers
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	if (ischroot) {
1977c478bd9Sstevel@tonic-gate 		struct vattr tattr;
1987c478bd9Sstevel@tonic-gate 		struct vattr rattr;
1997c478bd9Sstevel@tonic-gate 		vnode_t *zonevp = curproc->p_zone->zone_rootvp;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		tattr.va_mask = AT_FSID|AT_NODEID;
202*da6c28aaSamw 		if (error = VOP_GETATTR(vp, &tattr, 0, CRED(), NULL))
2037c478bd9Sstevel@tonic-gate 			goto bad;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		rattr.va_mask = AT_FSID|AT_NODEID;
206*da6c28aaSamw 		if (error = VOP_GETATTR(zonevp, &rattr, 0, CRED(), NULL))
2077c478bd9Sstevel@tonic-gate 			goto bad;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		if ((tattr.va_fsid != rattr.va_fsid ||
2107c478bd9Sstevel@tonic-gate 		    tattr.va_nodeid != rattr.va_nodeid) &&
2117c478bd9Sstevel@tonic-gate 		    (error = secpolicy_chroot(CRED())) != 0)
2127c478bd9Sstevel@tonic-gate 			goto bad;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		vpp = &PTOU(pp)->u_rdir;
2157c478bd9Sstevel@tonic-gate 	} else {
2167c478bd9Sstevel@tonic-gate 		vpp = &PTOU(pp)->u_cdir;
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
2207c478bd9Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
2217c478bd9Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
2227c478bd9Sstevel@tonic-gate #endif
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * This bit of logic prevents us from overwriting u_cwd if we are
2277c478bd9Sstevel@tonic-gate 	 * changing to the same directory.  We set the cwd to NULL so that we
2287c478bd9Sstevel@tonic-gate 	 * don't try to do the lookup on the next call to getcwd().
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	if (!ischroot && *vpp != NULL && vp != NULL && VN_CMP(*vpp, vp))
2317c478bd9Sstevel@tonic-gate 		newcwd = 0;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	oldvp = *vpp;
2347c478bd9Sstevel@tonic-gate 	*vpp = vp;
2357c478bd9Sstevel@tonic-gate 	if ((cwd = PTOU(pp)->u_cwd) != NULL && newcwd)
2367c478bd9Sstevel@tonic-gate 		PTOU(pp)->u_cwd = NULL;
2377c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (cwd && newcwd)
2407c478bd9Sstevel@tonic-gate 		refstr_rele(cwd);
2417c478bd9Sstevel@tonic-gate 	if (oldvp)
2427c478bd9Sstevel@tonic-gate 		VN_RELE(oldvp);
2437c478bd9Sstevel@tonic-gate 	return (0);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate bad:
2467c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
2477c478bd9Sstevel@tonic-gate 	return (error);
2487c478bd9Sstevel@tonic-gate }
249