1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/vnode.h>
30 #include <sys/door.h>
31 #include <sys/proc.h>
32 #include <sys/kmem.h>
33 #include <sys/debug.h>
34 #include <sys/cmn_err.h>
35 #include <fs/fs_subr.h>
36 #include <sys/zone.h>
37 #include <sys/tsol/label.h>
38 
39 kmutex_t	door_knob;
40 static int	door_open(struct vnode **vpp, int flag, struct cred *cr);
41 static int	door_close(struct vnode *vp, int flag, int count,
42 			offset_t offset, struct cred *cr);
43 static int	door_getattr(struct vnode *vp, struct vattr *vap,
44 			int flags, struct cred *cr);
45 static void	door_inactive(struct vnode *vp, struct cred *cr);
46 static int	door_access(struct vnode *vp, int mode, int flags,
47 			struct cred *cr);
48 static int	door_realvp(vnode_t *vp, vnode_t **vpp);
49 
50 struct vfs door_vfs;
51 
52 struct vnodeops *door_vnodeops;
53 
54 const fs_operation_def_t door_vnodeops_template[] = {
55 	VOPNAME_OPEN, door_open,
56 	VOPNAME_CLOSE, door_close,
57 	VOPNAME_GETATTR, door_getattr,
58 	VOPNAME_ACCESS, door_access,
59 	VOPNAME_INACTIVE, (fs_generic_func_p) door_inactive,
60 	VOPNAME_FRLOCK, fs_error,
61 	VOPNAME_REALVP, door_realvp,
62 	VOPNAME_POLL, fs_error,
63 	VOPNAME_PATHCONF, fs_error,
64 	VOPNAME_DISPOSE, fs_error,
65 	VOPNAME_GETSECATTR, fs_error,
66 	VOPNAME_SHRLOCK, fs_error,
67 	NULL, NULL
68 };
69 
70 /* ARGSUSED */
71 static int
72 door_open(struct vnode **vpp, int flag, struct cred *cr)
73 {
74 	/*
75 	 * MAC policy for doors.  Restrict cross-zone open()s so that only
76 	 * door servers in the global zone can have clients from other zones.
77 	 * For other zones, client must be within the same zone as server.
78 	 */
79 	if (is_system_labeled()) {
80 		zone_t		*server_zone, *client_zone;
81 		door_node_t	*dp = VTOD((*vpp));
82 
83 		mutex_enter(&door_knob);
84 		if (DOOR_INVALID(dp)) {
85 			mutex_exit(&door_knob);
86 			return (0);
87 		}
88 		client_zone = curproc->p_zone;
89 		server_zone = dp->door_target->p_zone;
90 		mutex_exit(&door_knob);
91 		if (server_zone != global_zone &&
92 		    server_zone != client_zone)
93 			return (EACCES);
94 	}
95 	return (0);
96 }
97 
98 /* ARGSUSED */
99 static int
100 door_close(
101 	struct vnode *vp,
102 	int flag,
103 	int count,
104 	offset_t offset,
105 	struct cred *cr
106 )
107 {
108 	door_node_t	*dp = VTOD(vp);
109 
110 	/*
111 	 * If this is being called from closeall on exit, any doors created
112 	 * by this process should have been revoked already in door_exit.
113 	 */
114 	ASSERT(dp->door_target != curproc ||
115 	    ((curthread->t_proc_flag & TP_LWPEXIT) == 0));
116 
117 	/*
118 	 * Deliver an unref if needed.
119 	 *
120 	 * If the count is equal to 2, it means that I'm doing a VOP_CLOSE
121 	 * on the next to last reference for *this* file struct. There may
122 	 * be multiple files pointing to this vnode in which case the v_count
123 	 * will be > 1.
124 	 *
125 	 * The door_active count is bumped during each invocation.
126 	 */
127 	if (count == 2 && vp->v_count == 1 &&
128 	    (dp->door_flags & (DOOR_UNREF | DOOR_UNREF_MULTI))) {
129 		mutex_enter(&door_knob);
130 		if (dp->door_active == 0) {
131 			/* o.k. to deliver unref now */
132 			door_deliver_unref(dp);
133 		} else {
134 			/* do the unref later */
135 			dp->door_flags |= DOOR_DELAY;
136 		}
137 		mutex_exit(&door_knob);
138 	}
139 	return (0);
140 }
141 
142 /* ARGSUSED */
143 static int
144 door_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr)
145 {
146 	static timestruc_t tzero = {0, 0};
147 	extern dev_t doordev;
148 
149 	vap->va_mask = 0;		/* bit-mask of attributes */
150 	vap->va_type = vp->v_type;	/* vnode type (for create) */
151 	vap->va_mode = 0777;		/* file access mode */
152 	vap->va_uid = 0;		/* owner user id */
153 	vap->va_gid = 0;		/* owner group id */
154 	vap->va_fsid = doordev;		/* file system id (dev for now) */
155 	vap->va_nodeid = (ino64_t)0;		/* node id */
156 	vap->va_nlink = vp->v_count;	/* number of references to file */
157 	vap->va_size = (u_offset_t)0;		/* file size in bytes */
158 	vap->va_atime = tzero;		/* time of last access */
159 	vap->va_mtime = tzero;		/* time of last modification */
160 	vap->va_ctime = tzero;		/* time file ``created'' */
161 	vap->va_rdev = doordev;		/* device the file represents */
162 	vap->va_blksize = 0;		/* fundamental block size */
163 	vap->va_nblocks = (fsblkcnt64_t)0;	/* # of blocks allocated */
164 	vap->va_seq = 0;		/* sequence number */
165 
166 	return (0);
167 }
168 
169 /* ARGSUSED */
170 static void
171 door_inactive(struct vnode *vp, struct cred *cr)
172 {
173 	door_node_t *dp = VTOD(vp);
174 
175 	mutex_enter(&vp->v_lock);
176 	/*
177 	 * Once the door_node is unreferenced, it stays unreferenced,
178 	 * so we can simply return if there are active thread bindings;
179 	 * the final door_unbind_thread() will re-invoke us.
180 	 */
181 	ASSERT(vp->v_count == 1);
182 	if (dp->door_bound_threads > 0) {
183 		vp->v_count--;
184 		mutex_exit(&vp->v_lock);
185 		return;
186 	}
187 	mutex_exit(&vp->v_lock);
188 
189 	/* if not revoked, remove door from per-process list */
190 	if (dp->door_target) {
191 		mutex_enter(&door_knob);
192 		if (dp->door_target)	/* recheck door_target under lock */
193 			door_list_delete(dp);
194 		mutex_exit(&door_knob);
195 	}
196 	vn_invalid(vp);
197 	vn_free(vp);
198 	kmem_free(dp, sizeof (door_node_t));
199 }
200 
201 /*
202  * To avoid having bound threads interfere with unref processing, we
203  * don't use VN_HOLD/VN_RELE to track threads bound to our private
204  * pool.  Instead, we keep a separate counter, also under v_lock.
205  */
206 void
207 door_bind_thread(door_node_t *dp)
208 {
209 	vnode_t *vp = DTOV(dp);
210 
211 	mutex_enter(&vp->v_lock);
212 	dp->door_bound_threads++;
213 	ASSERT(dp->door_bound_threads > 0 && vp->v_count > 0);
214 	mutex_exit(&vp->v_lock);
215 }
216 
217 void
218 door_unbind_thread(door_node_t *dp)
219 {
220 	vnode_t *vp = DTOV(dp);
221 	int do_inactive = 0;
222 
223 	mutex_enter(&vp->v_lock);
224 	ASSERT(dp->door_bound_threads > 0);
225 	if (--dp->door_bound_threads == 0 && vp->v_count == 0) {
226 		/* set up for inactive handling */
227 		vp->v_count++;
228 		do_inactive = 1;
229 	}
230 	mutex_exit(&vp->v_lock);
231 
232 	if (do_inactive)
233 		door_inactive(vp, NULL);
234 }
235 
236 /* ARGSUSED */
237 static int
238 door_access(struct vnode *vp, int mode, int flags, struct cred *cr)
239 {
240 	return (0);
241 }
242 
243 /* ARGSUSED */
244 static int
245 door_realvp(vnode_t *vp, vnode_t **vpp)
246 {
247 	*vpp = vp;
248 	return (0);
249 }
250