]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1990, 1996-1998 Apple Computer, Inc. | |
30 | * All Rights Reserved. | |
31 | */ | |
32 | /* | |
9bccf70c | 33 | * posix_shm.c : Support for POSIX shared memory APIs |
1c79356b A |
34 | * |
35 | * File: posix_shm.c | |
36 | * Author: Ananthakrishna Ramesh | |
37 | * | |
38 | * HISTORY | |
39 | * 2-Sep-1999 A.Ramesh | |
40 | * Created for MacOSX | |
41 | * | |
42 | */ | |
2d21ac55 A |
43 | /* |
44 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
45 | * support for mandatory and extensible security protections. This notice | |
46 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
47 | * Version 2.0. | |
48 | */ | |
1c79356b A |
49 | |
50 | #include <sys/cdefs.h> | |
51 | #include <sys/param.h> | |
52 | #include <sys/systm.h> | |
53 | #include <sys/kernel.h> | |
91447636 | 54 | #include <sys/file_internal.h> |
1c79356b A |
55 | #include <sys/filedesc.h> |
56 | #include <sys/stat.h> | |
91447636 A |
57 | #include <sys/proc_internal.h> |
58 | #include <sys/kauth.h> | |
1c79356b A |
59 | #include <sys/mount.h> |
60 | #include <sys/namei.h> | |
61 | #include <sys/vnode.h> | |
2d21ac55 | 62 | #include <sys/vnode_internal.h> |
1c79356b A |
63 | #include <sys/ioctl.h> |
64 | #include <sys/tty.h> | |
65 | #include <sys/malloc.h> | |
66 | #include <sys/mman.h> | |
91447636 A |
67 | #include <sys/stat.h> |
68 | #include <sys/sysproto.h> | |
0c530ab8 | 69 | #include <sys/proc_info.h> |
b0d623f7 | 70 | #include <security/audit/audit.h> |
e5568f75 | 71 | |
2d21ac55 A |
72 | #if CONFIG_MACF |
73 | #include <security/mac_framework.h> | |
74 | #endif | |
75 | ||
1c79356b | 76 | #include <mach/mach_types.h> |
91447636 A |
77 | #include <mach/mach_vm.h> |
78 | #include <mach/vm_map.h> | |
1c79356b A |
79 | #include <mach/vm_prot.h> |
80 | #include <mach/vm_inherit.h> | |
81 | #include <mach/kern_return.h> | |
82 | #include <mach/memory_object_control.h> | |
83 | ||
91447636 A |
84 | #include <vm/vm_map.h> |
85 | #include <vm/vm_protos.h> | |
1c79356b | 86 | |
91447636 | 87 | #define f_flag f_fglob->fg_flag |
39236c6e | 88 | #define f_type f_fglob->fg_ops->fo_type |
91447636 A |
89 | #define f_msgcount f_fglob->fg_msgcount |
90 | #define f_cred f_fglob->fg_cred | |
91 | #define f_ops f_fglob->fg_ops | |
92 | #define f_offset f_fglob->fg_offset | |
93 | #define f_data f_fglob->fg_data | |
1c79356b A |
94 | #define PSHMNAMLEN 31 /* maximum name segment length we bother with */ |
95 | ||
b0d623f7 A |
96 | struct pshmobj { |
97 | void * pshmo_memobject; | |
98 | memory_object_size_t pshmo_size; | |
99 | struct pshmobj * pshmo_next; | |
100 | }; | |
101 | ||
1c79356b A |
102 | struct pshminfo { |
103 | unsigned int pshm_flags; | |
104 | unsigned int pshm_usecount; | |
105 | off_t pshm_length; | |
106 | mode_t pshm_mode; | |
107 | uid_t pshm_uid; | |
108 | gid_t pshm_gid; | |
109 | char pshm_name[PSHMNAMLEN + 1]; /* segment name */ | |
b0d623f7 | 110 | struct pshmobj *pshm_memobjects; |
1c79356b A |
111 | #if DIAGNOSTIC |
112 | unsigned int pshm_readcount; | |
113 | unsigned int pshm_writecount; | |
2d21ac55 | 114 | proc_t pshm_proc; |
1c79356b | 115 | #endif /* DIAGNOSTIC */ |
2d21ac55 | 116 | struct label* pshm_label; |
1c79356b A |
117 | }; |
118 | #define PSHMINFO_NULL (struct pshminfo *)0 | |
119 | ||
b0d623f7 A |
120 | #define PSHM_NONE 0x001 |
121 | #define PSHM_DEFINED 0x002 | |
122 | #define PSHM_ALLOCATED 0x004 | |
123 | #define PSHM_MAPPED 0x008 | |
124 | #define PSHM_INUSE 0x010 | |
125 | #define PSHM_REMOVED 0x020 | |
126 | #define PSHM_INCREATE 0x040 | |
127 | #define PSHM_INDELETE 0x080 | |
128 | #define PSHM_ALLOCATING 0x100 | |
1c79356b A |
129 | |
130 | struct pshmcache { | |
131 | LIST_ENTRY(pshmcache) pshm_hash; /* hash chain */ | |
132 | struct pshminfo *pshminfo; /* vnode the name refers to */ | |
133 | int pshm_nlen; /* length of name */ | |
134 | char pshm_name[PSHMNAMLEN + 1]; /* segment name */ | |
135 | }; | |
136 | #define PSHMCACHE_NULL (struct pshmcache *)0 | |
137 | ||
490019cf A |
138 | #define PSHMCACHE_NOTFOUND (0) |
139 | #define PSHMCACHE_FOUND (-1) | |
140 | #define PSHMCACHE_NEGATIVE (ENOENT) | |
141 | ||
1c79356b A |
142 | struct pshmstats { |
143 | long goodhits; /* hits that we can really use */ | |
144 | long neghits; /* negative hits that we can use */ | |
145 | long badhits; /* hits we must drop */ | |
146 | long falsehits; /* hits with id mismatch */ | |
147 | long miss; /* misses */ | |
148 | long longnames; /* long names that ignore cache */ | |
149 | }; | |
150 | ||
151 | struct pshmname { | |
152 | char *pshm_nameptr; /* pointer to looked up name */ | |
153 | long pshm_namelen; /* length of looked up component */ | |
154 | u_long pshm_hash; /* hash value of looked up name */ | |
155 | }; | |
156 | ||
157 | struct pshmnode { | |
91447636 | 158 | off_t mapp_addr; |
b0d623f7 | 159 | user_size_t map_size; /* XXX unused ? */ |
1c79356b A |
160 | struct pshminfo *pinfo; |
161 | unsigned int pshm_usecount; | |
162 | #if DIAGNOSTIC | |
163 | unsigned int readcnt; | |
164 | unsigned int writecnt; | |
165 | #endif | |
166 | }; | |
167 | #define PSHMNODE_NULL (struct pshmnode *)0 | |
168 | ||
169 | ||
170 | #define PSHMHASH(pnp) \ | |
171 | (&pshmhashtbl[(pnp)->pshm_hash & pshmhash]) | |
91447636 | 172 | |
1c79356b A |
173 | LIST_HEAD(pshmhashhead, pshmcache) *pshmhashtbl; /* Hash Table */ |
174 | u_long pshmhash; /* size of hash table - 1 */ | |
175 | long pshmnument; /* number of cache entries allocated */ | |
176 | struct pshmstats pshmstats; /* cache effectiveness statistics */ | |
177 | ||
91447636 | 178 | static int pshm_read (struct fileproc *fp, struct uio *uio, |
5ba3f43e | 179 | int flags, vfs_context_t ctx); |
91447636 | 180 | static int pshm_write (struct fileproc *fp, struct uio *uio, |
5ba3f43e | 181 | int flags, vfs_context_t ctx); |
91447636 | 182 | static int pshm_ioctl (struct fileproc *fp, u_long com, |
5ba3f43e | 183 | caddr_t data, vfs_context_t ctx); |
2d21ac55 | 184 | static int pshm_select (struct fileproc *fp, int which, void *wql, vfs_context_t ctx); |
6d2010ae | 185 | static int pshm_close(struct pshminfo *pinfo, int dropref); |
2d21ac55 | 186 | static int pshm_closefile (struct fileglob *fg, vfs_context_t ctx); |
91447636 | 187 | |
5ba3f43e A |
188 | static int pshm_kqfilter(struct fileproc *fp, struct knote *kn, |
189 | struct kevent_internal_s *kev, vfs_context_t ctx); | |
91447636 | 190 | |
2d21ac55 | 191 | int pshm_access(struct pshminfo *pinfo, int mode, kauth_cred_t cred, proc_t p); |
490019cf A |
192 | int pshm_cache_purge_all(proc_t p); |
193 | ||
91447636 A |
194 | static int pshm_cache_add(struct pshminfo *pshmp, struct pshmname *pnp, struct pshmcache *pcp); |
195 | static void pshm_cache_delete(struct pshmcache *pcp); | |
91447636 | 196 | static int pshm_cache_search(struct pshminfo **pshmp, struct pshmname *pnp, |
6d2010ae | 197 | struct pshmcache **pcache, int addref); |
490019cf | 198 | static int pshm_unlink_internal(struct pshminfo *pinfo, struct pshmcache *pcache); |
55e303ae | 199 | |
39236c6e | 200 | static const struct fileops pshmops = { |
39037602 A |
201 | .fo_type = DTYPE_PSXSHM, |
202 | .fo_read = pshm_read, | |
203 | .fo_write = pshm_write, | |
204 | .fo_ioctl = pshm_ioctl, | |
205 | .fo_select = pshm_select, | |
206 | .fo_close = pshm_closefile, | |
207 | .fo_kqfilter = pshm_kqfilter, | |
208 | .fo_drain = NULL, | |
39236c6e | 209 | }; |
91447636 A |
210 | |
211 | static lck_grp_t *psx_shm_subsys_lck_grp; | |
212 | static lck_grp_attr_t *psx_shm_subsys_lck_grp_attr; | |
213 | static lck_attr_t *psx_shm_subsys_lck_attr; | |
214 | static lck_mtx_t psx_shm_subsys_mutex; | |
215 | ||
216 | #define PSHM_SUBSYS_LOCK() lck_mtx_lock(& psx_shm_subsys_mutex) | |
217 | #define PSHM_SUBSYS_UNLOCK() lck_mtx_unlock(& psx_shm_subsys_mutex) | |
39037602 | 218 | #define PSHM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_shm_subsys_mutex, LCK_MTX_ASSERT_OWNED) |
91447636 A |
219 | |
220 | ||
221 | /* Initialize the mutex governing access to the posix shm subsystem */ | |
222 | __private_extern__ void | |
223 | pshm_lock_init( void ) | |
224 | { | |
225 | ||
226 | psx_shm_subsys_lck_grp_attr = lck_grp_attr_alloc_init(); | |
91447636 A |
227 | |
228 | psx_shm_subsys_lck_grp = lck_grp_alloc_init("posix shared memory", psx_shm_subsys_lck_grp_attr); | |
229 | ||
230 | psx_shm_subsys_lck_attr = lck_attr_alloc_init(); | |
91447636 A |
231 | lck_mtx_init(& psx_shm_subsys_mutex, psx_shm_subsys_lck_grp, psx_shm_subsys_lck_attr); |
232 | } | |
1c79356b | 233 | |
1c79356b A |
234 | /* |
235 | * Lookup an entry in the cache | |
236 | * | |
237 | * | |
238 | * status of -1 is returned if matches | |
239 | * If the lookup determines that the name does not exist | |
240 | * (negative cacheing), a status of ENOENT is returned. If the lookup | |
241 | * fails, a status of zero is returned. | |
242 | */ | |
243 | ||
91447636 A |
244 | static int |
245 | pshm_cache_search(struct pshminfo **pshmp, struct pshmname *pnp, | |
6d2010ae | 246 | struct pshmcache **pcache, int addref) |
1c79356b | 247 | { |
91447636 A |
248 | struct pshmcache *pcp, *nnp; |
249 | struct pshmhashhead *pcpp; | |
1c79356b A |
250 | |
251 | if (pnp->pshm_namelen > PSHMNAMLEN) { | |
252 | pshmstats.longnames++; | |
490019cf | 253 | return PSHMCACHE_NOTFOUND; |
1c79356b A |
254 | } |
255 | ||
256 | pcpp = PSHMHASH(pnp); | |
257 | for (pcp = pcpp->lh_first; pcp != 0; pcp = nnp) { | |
258 | nnp = pcp->pshm_hash.le_next; | |
259 | if (pcp->pshm_nlen == pnp->pshm_namelen && | |
260 | !bcmp(pcp->pshm_name, pnp->pshm_nameptr, (u_int)pcp-> pshm_nlen)) | |
261 | break; | |
262 | } | |
263 | ||
264 | if (pcp == 0) { | |
265 | pshmstats.miss++; | |
490019cf | 266 | return PSHMCACHE_NOTFOUND; |
1c79356b A |
267 | } |
268 | ||
269 | /* We found a "positive" match, return the vnode */ | |
270 | if (pcp->pshminfo) { | |
271 | pshmstats.goodhits++; | |
272 | /* TOUCH(ncp); */ | |
273 | *pshmp = pcp->pshminfo; | |
274 | *pcache = pcp; | |
6d2010ae A |
275 | if (addref) |
276 | pcp->pshminfo->pshm_usecount++; | |
490019cf | 277 | return PSHMCACHE_FOUND; |
1c79356b A |
278 | } |
279 | ||
280 | /* | |
281 | * We found a "negative" match, ENOENT notifies client of this match. | |
1c79356b A |
282 | */ |
283 | pshmstats.neghits++; | |
490019cf | 284 | return PSHMCACHE_NEGATIVE; |
1c79356b A |
285 | } |
286 | ||
287 | /* | |
288 | * Add an entry to the cache. | |
91447636 | 289 | * XXX should be static? |
1c79356b | 290 | */ |
91447636 A |
291 | static int |
292 | pshm_cache_add(struct pshminfo *pshmp, struct pshmname *pnp, struct pshmcache *pcp) | |
1c79356b | 293 | { |
91447636 | 294 | struct pshmhashhead *pcpp; |
55e303ae A |
295 | struct pshminfo *dpinfo; |
296 | struct pshmcache *dpcp; | |
1c79356b A |
297 | |
298 | #if DIAGNOSTIC | |
2d21ac55 | 299 | if (pnp->pshm_namelen > PSHMNAMLEN) |
1c79356b A |
300 | panic("cache_enter: name too long"); |
301 | #endif | |
302 | ||
91447636 | 303 | |
1c79356b | 304 | /* if the entry has already been added by some one else return */ |
490019cf A |
305 | if (pshm_cache_search(&dpinfo, pnp, &dpcp, 0) == PSHMCACHE_FOUND) { |
306 | return EEXIST; | |
1c79356b A |
307 | } |
308 | pshmnument++; | |
309 | ||
1c79356b A |
310 | /* |
311 | * Fill in cache info, if vp is NULL this is a "negative" cache entry. | |
1c79356b A |
312 | */ |
313 | pcp->pshminfo = pshmp; | |
314 | pcp->pshm_nlen = pnp->pshm_namelen; | |
315 | bcopy(pnp->pshm_nameptr, pcp->pshm_name, (unsigned)pcp->pshm_nlen); | |
316 | pcpp = PSHMHASH(pnp); | |
317 | #if DIAGNOSTIC | |
318 | { | |
91447636 | 319 | struct pshmcache *p; |
1c79356b A |
320 | |
321 | for (p = pcpp->lh_first; p != 0; p = p->pshm_hash.le_next) | |
322 | if (p == pcp) | |
323 | panic("cache_enter: duplicate"); | |
324 | } | |
325 | #endif | |
326 | LIST_INSERT_HEAD(pcpp, pcp, pshm_hash); | |
490019cf | 327 | return 0; |
1c79356b A |
328 | } |
329 | ||
330 | /* | |
331 | * Name cache initialization, from vfs_init() when we are booting | |
332 | */ | |
333 | void | |
91447636 | 334 | pshm_cache_init(void) |
1c79356b | 335 | { |
2d21ac55 | 336 | pshmhashtbl = hashinit(desiredvnodes / 8, M_SHM, &pshmhash); |
1c79356b A |
337 | } |
338 | ||
339 | /* | |
490019cf A |
340 | * Invalidate all entries and delete all objects associated with it. Entire |
341 | * non Kernel entries are going away. Just dump'em all | |
342 | * | |
1c79356b A |
343 | * We actually just increment the v_id, that will do it. The entries will |
344 | * be purged by lookup as they get found. If the v_id wraps around, we | |
345 | * need to ditch the entire cache, to avoid confusion. No valid vnode will | |
346 | * ever have (v_id == 0). | |
347 | */ | |
490019cf A |
348 | int |
349 | pshm_cache_purge_all(__unused proc_t p) | |
1c79356b | 350 | { |
490019cf | 351 | struct pshmcache *pcp, *tmppcp; |
1c79356b | 352 | struct pshmhashhead *pcpp; |
490019cf A |
353 | int error = 0; |
354 | ||
355 | if (kauth_cred_issuser(kauth_cred_get()) == 0) | |
356 | return EPERM; | |
1c79356b | 357 | |
490019cf | 358 | PSHM_SUBSYS_LOCK(); |
1c79356b | 359 | for (pcpp = &pshmhashtbl[pshmhash]; pcpp >= pshmhashtbl; pcpp--) { |
490019cf A |
360 | LIST_FOREACH_SAFE(pcp, pcpp, pshm_hash, tmppcp) { |
361 | assert(pcp->pshm_nlen); | |
362 | error = pshm_unlink_internal(pcp->pshminfo, pcp); | |
363 | if (error) | |
364 | goto out; | |
365 | } | |
1c79356b | 366 | } |
490019cf A |
367 | assert(pshmnument == 0); |
368 | ||
369 | out: | |
370 | PSHM_SUBSYS_UNLOCK(); | |
371 | ||
372 | if (error) | |
373 | printf("%s: Error %d removing shm cache: %ld remain!\n", | |
374 | __func__, error, pshmnument); | |
375 | return error; | |
1c79356b A |
376 | } |
377 | ||
91447636 A |
378 | static void |
379 | pshm_cache_delete(struct pshmcache *pcp) | |
1c79356b A |
380 | { |
381 | #if DIAGNOSTIC | |
382 | if (pcp->pshm_hash.le_prev == 0) | |
383 | panic("namecache purge le_prev"); | |
384 | if (pcp->pshm_hash.le_next == pcp) | |
385 | panic("namecache purge le_next"); | |
386 | #endif /* DIAGNOSTIC */ | |
387 | LIST_REMOVE(pcp, pshm_hash); | |
388 | pcp->pshm_hash.le_prev = 0; | |
389 | pshmnument--; | |
390 | } | |
391 | ||
392 | ||
1c79356b | 393 | int |
b0d623f7 | 394 | shm_open(proc_t p, struct shm_open_args *uap, int32_t *retval) |
1c79356b | 395 | { |
91447636 | 396 | size_t i; |
91447636 | 397 | int indx, error; |
1c79356b A |
398 | struct pshmname nd; |
399 | struct pshminfo *pinfo; | |
b0d623f7 A |
400 | struct fileproc *fp = NULL; |
401 | char *pnbuf = NULL; | |
402 | struct pshminfo *new_pinfo = PSHMINFO_NULL; | |
403 | struct pshmnode *new_pnode = PSHMNODE_NULL; | |
404 | struct pshmcache *pcache = PSHMCACHE_NULL; /* ignored on return */ | |
1c79356b A |
405 | char * nameptr; |
406 | char * cp; | |
407 | size_t pathlen, plen; | |
408 | int fmode ; | |
409 | int cmode = uap->mode; | |
410 | int incache = 0; | |
b0d623f7 | 411 | struct pshmcache *pcp = NULL; |
1c79356b | 412 | |
e5568f75 A |
413 | AUDIT_ARG(fflags, uap->oflag); |
414 | AUDIT_ARG(mode, uap->mode); | |
91447636 | 415 | |
1c79356b A |
416 | pinfo = PSHMINFO_NULL; |
417 | ||
b0d623f7 A |
418 | /* |
419 | * Preallocate everything we might need up front to avoid taking | |
420 | * and dropping the lock, opening us up to race conditions. | |
421 | */ | |
91447636 A |
422 | MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); |
423 | if (pnbuf == NULL) { | |
b0d623f7 A |
424 | error = ENOSPC; |
425 | goto bad; | |
91447636 A |
426 | } |
427 | ||
1c79356b | 428 | pathlen = MAXPATHLEN; |
91447636 | 429 | error = copyinstr(uap->name, (void *)pnbuf, MAXPATHLEN, &pathlen); |
1c79356b A |
430 | if (error) { |
431 | goto bad; | |
432 | } | |
e5568f75 | 433 | AUDIT_ARG(text, pnbuf); |
1c79356b A |
434 | if (pathlen > PSHMNAMLEN) { |
435 | error = ENAMETOOLONG; | |
436 | goto bad; | |
437 | } | |
1c79356b A |
438 | #ifdef PSXSHM_NAME_RESTRICT |
439 | nameptr = pnbuf; | |
440 | if (*nameptr == '/') { | |
441 | while (*(nameptr++) == '/') { | |
442 | plen--; | |
443 | error = EINVAL; | |
444 | goto bad; | |
445 | } | |
2d21ac55 | 446 | } else { |
1c79356b A |
447 | error = EINVAL; |
448 | goto bad; | |
449 | } | |
450 | #endif /* PSXSHM_NAME_RESTRICT */ | |
451 | ||
452 | plen = pathlen; | |
453 | nameptr = pnbuf; | |
454 | nd.pshm_nameptr = nameptr; | |
455 | nd.pshm_namelen = plen; | |
456 | nd. pshm_hash =0; | |
457 | ||
2d21ac55 A |
458 | for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) { |
459 | nd.pshm_hash += (unsigned char)*cp * i; | |
1c79356b A |
460 | } |
461 | ||
b0d623f7 A |
462 | /* |
463 | * attempt to allocate a new fp; if unsuccessful, the fp will be | |
464 | * left unmodified (NULL). | |
465 | */ | |
466 | error = falloc(p, &fp, &indx, vfs_context_current()); | |
467 | if (error) | |
468 | goto bad; | |
469 | ||
6d2010ae A |
470 | cmode &= ALLPERMS; |
471 | ||
472 | fmode = FFLAGS(uap->oflag); | |
473 | if ((fmode & (FREAD | FWRITE)) == 0) { | |
474 | error = EINVAL; | |
475 | goto bad; | |
476 | } | |
477 | ||
b0d623f7 A |
478 | /* |
479 | * We allocate a new entry if we are less than the maximum | |
480 | * allowed and the one at the front of the LRU list is in use. | |
481 | * Otherwise we use the one at the front of the LRU list. | |
482 | */ | |
483 | MALLOC(pcp, struct pshmcache *, sizeof(struct pshmcache), M_SHM, M_WAITOK|M_ZERO); | |
484 | if (pcp == NULL) { | |
485 | error = ENOSPC; | |
486 | goto bad; | |
487 | } | |
488 | ||
489 | MALLOC(new_pinfo, struct pshminfo *, sizeof(struct pshminfo), M_SHM, M_WAITOK|M_ZERO); | |
490 | if (new_pinfo == PSHMINFO_NULL) { | |
491 | error = ENOSPC; | |
492 | goto bad; | |
493 | } | |
494 | #if CONFIG_MACF | |
495 | mac_posixshm_label_init(new_pinfo); | |
496 | #endif | |
497 | ||
498 | MALLOC(new_pnode, struct pshmnode *, sizeof(struct pshmnode), M_SHM, M_WAITOK|M_ZERO); | |
499 | if (new_pnode == PSHMNODE_NULL) { | |
500 | error = ENOSPC; | |
501 | goto bad; | |
502 | } | |
503 | ||
91447636 | 504 | PSHM_SUBSYS_LOCK(); |
b0d623f7 | 505 | |
6d2010ae A |
506 | /* |
507 | * If we find the entry in the cache, this will take a reference, | |
508 | * allowing us to unlock it for the permissions check. | |
509 | */ | |
510 | error = pshm_cache_search(&pinfo, &nd, &pcache, 1); | |
511 | ||
512 | PSHM_SUBSYS_UNLOCK(); | |
1c79356b | 513 | |
490019cf | 514 | if (error == PSHMCACHE_NEGATIVE) { |
1c79356b | 515 | error = EINVAL; |
6d2010ae | 516 | goto bad; |
1c79356b | 517 | } |
6d2010ae | 518 | |
490019cf | 519 | if (error == PSHMCACHE_NOTFOUND) { |
1c79356b | 520 | incache = 0; |
6d2010ae A |
521 | if (fmode & O_CREAT) { |
522 | /* create a new one (commit the allocation) */ | |
523 | pinfo = new_pinfo; | |
524 | pinfo->pshm_flags = PSHM_DEFINED | PSHM_INCREATE; | |
525 | pinfo->pshm_usecount = 1; /* existence reference */ | |
526 | pinfo->pshm_mode = cmode; | |
527 | pinfo->pshm_uid = kauth_getuid(); | |
528 | pinfo->pshm_gid = kauth_getgid(); | |
316670eb A |
529 | bcopy(pnbuf, &pinfo->pshm_name[0], pathlen); |
530 | pinfo->pshm_name[pathlen]=0; | |
6d2010ae A |
531 | #if CONFIG_MACF |
532 | error = mac_posixshm_check_create(kauth_cred_get(), nameptr); | |
533 | if (error) { | |
534 | goto bad; | |
535 | } | |
536 | mac_posixshm_label_associate(kauth_cred_get(), pinfo, nameptr); | |
537 | #endif | |
538 | } | |
539 | } else { | |
1c79356b | 540 | incache = 1; |
6d2010ae | 541 | if (fmode & O_CREAT) { |
b0d623f7 A |
542 | /* already exists */ |
543 | if ((fmode & O_EXCL)) { | |
544 | AUDIT_ARG(posix_ipc_perm, pinfo->pshm_uid, | |
545 | pinfo->pshm_gid, | |
546 | pinfo->pshm_mode); | |
547 | ||
548 | /* shm obj exists and opened O_EXCL */ | |
549 | error = EEXIST; | |
6d2010ae | 550 | goto bad; |
b0d623f7 A |
551 | } |
552 | ||
553 | if( pinfo->pshm_flags & PSHM_INDELETE) { | |
554 | error = ENOENT; | |
6d2010ae | 555 | goto bad; |
b0d623f7 | 556 | } |
e5568f75 | 557 | AUDIT_ARG(posix_ipc_perm, pinfo->pshm_uid, |
2d21ac55 | 558 | pinfo->pshm_gid, pinfo->pshm_mode); |
b0d623f7 | 559 | #if CONFIG_MACF |
316670eb | 560 | if ((error = mac_posixshm_check_open(kauth_cred_get(), pinfo, fmode))) { |
6d2010ae | 561 | goto bad; |
2d21ac55 | 562 | } |
b0d623f7 A |
563 | #endif |
564 | if ( (error = pshm_access(pinfo, fmode, kauth_cred_get(), p)) ) { | |
6d2010ae | 565 | goto bad; |
2d21ac55 | 566 | } |
2d21ac55 | 567 | } |
6d2010ae A |
568 | } |
569 | if (!(fmode & O_CREAT)) { | |
1c79356b | 570 | if (!incache) { |
b0d623f7 | 571 | /* O_CREAT is not set and the object does not exist */ |
1c79356b | 572 | error = ENOENT; |
6d2010ae | 573 | goto bad; |
1c79356b A |
574 | } |
575 | if( pinfo->pshm_flags & PSHM_INDELETE) { | |
576 | error = ENOENT; | |
6d2010ae | 577 | goto bad; |
1c79356b | 578 | } |
2d21ac55 | 579 | #if CONFIG_MACF |
316670eb | 580 | if ((error = mac_posixshm_check_open(kauth_cred_get(), pinfo, fmode))) { |
6d2010ae | 581 | goto bad; |
2d21ac55 A |
582 | } |
583 | #endif | |
584 | ||
b0d623f7 | 585 | if ((error = pshm_access(pinfo, fmode, kauth_cred_get(), p))) { |
6d2010ae | 586 | goto bad; |
91447636 | 587 | } |
1c79356b A |
588 | } |
589 | if (fmode & O_TRUNC) { | |
590 | error = EINVAL; | |
6d2010ae | 591 | goto bad; |
1c79356b | 592 | } |
6d2010ae A |
593 | |
594 | ||
595 | PSHM_SUBSYS_LOCK(); | |
596 | ||
1c79356b A |
597 | #if DIAGNOSTIC |
598 | if (fmode & FWRITE) | |
599 | pinfo->pshm_writecount++; | |
600 | if (fmode & FREAD) | |
601 | pinfo->pshm_readcount++; | |
602 | #endif | |
1c79356b | 603 | if (!incache) { |
b0d623f7 | 604 | /* if successful, this will consume the pcp */ |
91447636 | 605 | if ( (error = pshm_cache_add(pinfo, &nd, pcp)) ) { |
b0d623f7 | 606 | goto bad_locked; |
1c79356b | 607 | } |
6d2010ae A |
608 | /* |
609 | * add reference for the new entry; otherwise, we obtained | |
610 | * one from the cache hit earlier. | |
611 | */ | |
612 | pinfo->pshm_usecount++; | |
1c79356b A |
613 | } |
614 | pinfo->pshm_flags &= ~PSHM_INCREATE; | |
b0d623f7 | 615 | new_pnode->pinfo = pinfo; |
91447636 A |
616 | |
617 | PSHM_SUBSYS_UNLOCK(); | |
b0d623f7 A |
618 | |
619 | /* | |
620 | * if incache, we did not use the new pcp or new_pinfo and must | |
621 | * free them | |
622 | */ | |
623 | if (incache) { | |
624 | FREE(pcp, M_SHM); | |
625 | ||
626 | if (new_pinfo != PSHMINFO_NULL) { | |
627 | #if CONFIG_MACF | |
628 | mac_posixshm_label_destroy(new_pinfo); | |
629 | #endif | |
630 | FREE(new_pinfo, M_SHM); | |
631 | } | |
632 | } | |
633 | ||
91447636 | 634 | proc_fdlock(p); |
1c79356b | 635 | fp->f_flag = fmode & FMASK; |
1c79356b | 636 | fp->f_ops = &pshmops; |
b0d623f7 | 637 | fp->f_data = (caddr_t)new_pnode; |
2d21ac55 | 638 | *fdflags(p, indx) |= UF_EXCLOSE; |
6601e61a | 639 | procfdtbl_releasefd(p, indx, NULL); |
91447636 A |
640 | fp_drop(p, indx, fp, 1); |
641 | proc_fdunlock(p); | |
642 | ||
1c79356b | 643 | *retval = indx; |
55e303ae | 644 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); |
1c79356b | 645 | return (0); |
2d21ac55 | 646 | |
b0d623f7 A |
647 | bad_locked: |
648 | PSHM_SUBSYS_UNLOCK(); | |
649 | bad: | |
6d2010ae A |
650 | /* |
651 | * If we obtained the entry from the cache, we need to drop the | |
652 | * reference; holding the reference may have prevented unlinking, | |
653 | * so we need to call pshm_close() to get the full effect. | |
654 | */ | |
655 | if (incache) { | |
656 | PSHM_SUBSYS_LOCK(); | |
657 | pshm_close(pinfo, 1); | |
658 | PSHM_SUBSYS_UNLOCK(); | |
659 | } | |
660 | ||
b0d623f7 A |
661 | if (pcp != NULL) |
662 | FREE(pcp, M_SHM); | |
663 | ||
664 | if (new_pnode != PSHMNODE_NULL) | |
665 | FREE(new_pnode, M_SHM); | |
666 | ||
667 | if (fp != NULL) | |
668 | fp_free(p, indx, fp); | |
669 | ||
670 | if (new_pinfo != PSHMINFO_NULL) { | |
2d21ac55 | 671 | #if CONFIG_MACF |
b0d623f7 | 672 | mac_posixshm_label_destroy(new_pinfo); |
2d21ac55 | 673 | #endif |
b0d623f7 | 674 | FREE(new_pinfo, M_SHM); |
2d21ac55 | 675 | } |
b0d623f7 A |
676 | if (pnbuf != NULL) |
677 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); | |
1c79356b A |
678 | return (error); |
679 | } | |
680 | ||
681 | ||
1c79356b | 682 | int |
2d21ac55 | 683 | pshm_truncate(__unused proc_t p, struct fileproc *fp, __unused int fd, |
b0d623f7 | 684 | off_t length, __unused int32_t *retval) |
1c79356b A |
685 | { |
686 | struct pshminfo * pinfo; | |
687 | struct pshmnode * pnode ; | |
688 | kern_return_t kret; | |
91447636 | 689 | mem_entry_name_port_t mem_object; |
6d2010ae A |
690 | mach_vm_size_t total_size, alloc_size; |
691 | memory_object_size_t mosize; | |
b0d623f7 | 692 | struct pshmobj *pshmobj, *pshmobj_next, **pshmobj_next_p; |
39236c6e | 693 | vm_map_t user_map; |
2d21ac55 A |
694 | #if CONFIG_MACF |
695 | int error; | |
696 | #endif | |
1c79356b | 697 | |
39236c6e A |
698 | user_map = current_map(); |
699 | ||
1c79356b A |
700 | if (fp->f_type != DTYPE_PSXSHM) { |
701 | return(EINVAL); | |
702 | } | |
703 | ||
704 | ||
705 | if (((pnode = (struct pshmnode *)fp->f_data)) == PSHMNODE_NULL ) | |
706 | return(EINVAL); | |
707 | ||
91447636 A |
708 | PSHM_SUBSYS_LOCK(); |
709 | if ((pinfo = pnode->pinfo) == PSHMINFO_NULL) { | |
710 | PSHM_SUBSYS_UNLOCK(); | |
1c79356b | 711 | return(EINVAL); |
91447636 | 712 | } |
b0d623f7 | 713 | if ((pinfo->pshm_flags & (PSHM_DEFINED|PSHM_ALLOCATING|PSHM_ALLOCATED)) |
1c79356b | 714 | != PSHM_DEFINED) { |
91447636 | 715 | PSHM_SUBSYS_UNLOCK(); |
1c79356b A |
716 | return(EINVAL); |
717 | } | |
2d21ac55 | 718 | #if CONFIG_MACF |
6d2010ae | 719 | error = mac_posixshm_check_truncate(kauth_cred_get(), pinfo, length); |
2d21ac55 A |
720 | if (error) { |
721 | PSHM_SUBSYS_UNLOCK(); | |
722 | return(error); | |
723 | } | |
724 | #endif | |
1c79356b | 725 | |
b0d623f7 | 726 | pinfo->pshm_flags |= PSHM_ALLOCATING; |
39236c6e A |
727 | total_size = vm_map_round_page(length, |
728 | vm_map_page_mask(user_map)); | |
b0d623f7 | 729 | pshmobj_next_p = &pinfo->pshm_memobjects; |
1c79356b | 730 | |
b0d623f7 A |
731 | for (alloc_size = 0; |
732 | alloc_size < total_size; | |
6d2010ae | 733 | alloc_size += mosize) { |
1c79356b | 734 | |
b0d623f7 A |
735 | PSHM_SUBSYS_UNLOCK(); |
736 | ||
6d2010ae | 737 | mosize = MIN(total_size - alloc_size, ANON_MAX_SIZE); |
b0d623f7 A |
738 | kret = mach_make_memory_entry_64( |
739 | VM_MAP_NULL, | |
6d2010ae | 740 | &mosize, |
b0d623f7 A |
741 | 0, |
742 | MAP_MEM_NAMED_CREATE | VM_PROT_DEFAULT, | |
743 | &mem_object, | |
744 | 0); | |
745 | ||
746 | if (kret != KERN_SUCCESS) | |
747 | goto out; | |
748 | ||
749 | MALLOC(pshmobj, struct pshmobj *, sizeof (struct pshmobj), | |
750 | M_SHM, M_WAITOK); | |
751 | if (pshmobj == NULL) { | |
752 | kret = KERN_NO_SPACE; | |
753 | mach_memory_entry_port_release(mem_object); | |
754 | mem_object = NULL; | |
755 | goto out; | |
756 | } | |
757 | ||
758 | PSHM_SUBSYS_LOCK(); | |
759 | ||
760 | pshmobj->pshmo_memobject = (void *) mem_object; | |
6d2010ae | 761 | pshmobj->pshmo_size = mosize; |
b0d623f7 A |
762 | pshmobj->pshmo_next = NULL; |
763 | ||
764 | *pshmobj_next_p = pshmobj; | |
765 | pshmobj_next_p = &pshmobj->pshmo_next; | |
766 | } | |
767 | ||
3e170ce0 A |
768 | pinfo->pshm_flags |= PSHM_ALLOCATED; |
769 | pinfo->pshm_flags &= ~(PSHM_ALLOCATING); | |
b0d623f7 | 770 | pinfo->pshm_length = total_size; |
91447636 | 771 | PSHM_SUBSYS_UNLOCK(); |
1c79356b A |
772 | return(0); |
773 | ||
774 | out: | |
b0d623f7 A |
775 | PSHM_SUBSYS_LOCK(); |
776 | for (pshmobj = pinfo->pshm_memobjects; | |
777 | pshmobj != NULL; | |
778 | pshmobj = pshmobj_next) { | |
779 | pshmobj_next = pshmobj->pshmo_next; | |
780 | mach_memory_entry_port_release(pshmobj->pshmo_memobject); | |
781 | FREE(pshmobj, M_SHM); | |
782 | } | |
783 | pinfo->pshm_memobjects = NULL; | |
784 | pinfo->pshm_flags &= ~PSHM_ALLOCATING; | |
785 | PSHM_SUBSYS_UNLOCK(); | |
786 | ||
1c79356b A |
787 | switch (kret) { |
788 | case KERN_INVALID_ADDRESS: | |
789 | case KERN_NO_SPACE: | |
790 | return (ENOMEM); | |
791 | case KERN_PROTECTION_FAILURE: | |
792 | return (EACCES); | |
793 | default: | |
794 | return (EINVAL); | |
795 | ||
796 | } | |
797 | } | |
798 | ||
799 | int | |
2d21ac55 | 800 | pshm_stat(struct pshmnode *pnode, void *ub, int isstat64) |
1c79356b | 801 | { |
2d21ac55 A |
802 | struct stat *sb = (struct stat *)0; /* warning avoidance ; protected by isstat64 */ |
803 | struct stat64 * sb64 = (struct stat64 *)0; /* warning avoidance ; protected by isstat64 */ | |
1c79356b | 804 | struct pshminfo *pinfo; |
2d21ac55 A |
805 | #if CONFIG_MACF |
806 | int error; | |
807 | #endif | |
1c79356b | 808 | |
91447636 A |
809 | PSHM_SUBSYS_LOCK(); |
810 | if ((pinfo = pnode->pinfo) == PSHMINFO_NULL){ | |
811 | PSHM_SUBSYS_UNLOCK(); | |
1c79356b | 812 | return(EINVAL); |
91447636 | 813 | } |
1c79356b | 814 | |
2d21ac55 A |
815 | #if CONFIG_MACF |
816 | error = mac_posixshm_check_stat(kauth_cred_get(), pinfo); | |
817 | if (error) { | |
818 | PSHM_SUBSYS_UNLOCK(); | |
819 | return(error); | |
820 | } | |
821 | #endif | |
822 | ||
823 | if (isstat64 != 0) { | |
824 | sb64 = (struct stat64 *)ub; | |
825 | bzero(sb64, sizeof(struct stat64)); | |
826 | sb64->st_mode = pinfo->pshm_mode; | |
827 | sb64->st_uid = pinfo->pshm_uid; | |
828 | sb64->st_gid = pinfo->pshm_gid; | |
829 | sb64->st_size = pinfo->pshm_length; | |
830 | } else { | |
831 | sb = (struct stat *)ub; | |
832 | bzero(sb, sizeof(struct stat)); | |
833 | sb->st_mode = pinfo->pshm_mode; | |
834 | sb->st_uid = pinfo->pshm_uid; | |
835 | sb->st_gid = pinfo->pshm_gid; | |
836 | sb->st_size = pinfo->pshm_length; | |
837 | } | |
91447636 | 838 | PSHM_SUBSYS_UNLOCK(); |
1c79356b A |
839 | |
840 | return(0); | |
841 | } | |
842 | ||
91447636 A |
843 | /* |
844 | * This is called only from shm_open which holds pshm_lock(); | |
845 | * XXX This code is repeated many times | |
846 | */ | |
1c79356b | 847 | int |
2d21ac55 | 848 | pshm_access(struct pshminfo *pinfo, int mode, kauth_cred_t cred, __unused proc_t p) |
1c79356b | 849 | { |
6d2010ae A |
850 | int mode_req = ((mode & FREAD) ? S_IRUSR : 0) | |
851 | ((mode & FWRITE) ? S_IWUSR : 0); | |
1c79356b A |
852 | |
853 | /* Otherwise, user id 0 always gets access. */ | |
91447636 | 854 | if (!suser(cred, NULL)) |
1c79356b A |
855 | return (0); |
856 | ||
6d2010ae | 857 | return(posix_cred_access(cred, pinfo->pshm_uid, pinfo->pshm_gid, pinfo->pshm_mode, mode_req)); |
1c79356b | 858 | } |
9bccf70c | 859 | |
1c79356b | 860 | int |
2d21ac55 | 861 | pshm_mmap(__unused proc_t p, struct mmap_args *uap, user_addr_t *retval, struct fileproc *fp, off_t pageoff) |
1c79356b | 862 | { |
316670eb A |
863 | vm_map_offset_t user_addr = (vm_map_offset_t)uap->addr; |
864 | vm_map_size_t user_size = (vm_map_size_t)uap->len ; | |
865 | vm_map_offset_t user_start_addr; | |
866 | vm_map_size_t map_size, mapped_size; | |
1c79356b A |
867 | int prot = uap->prot; |
868 | int flags = uap->flags; | |
869 | vm_object_offset_t file_pos = (vm_object_offset_t)uap->pos; | |
b0d623f7 | 870 | vm_object_offset_t map_pos; |
1c79356b | 871 | vm_map_t user_map; |
91447636 | 872 | int alloc_flags; |
5ba3f43e | 873 | vm_map_kernel_flags_t vmk_flags; |
91447636 | 874 | boolean_t docow; |
1c79356b A |
875 | kern_return_t kret; |
876 | struct pshminfo * pinfo; | |
877 | struct pshmnode * pnode; | |
b0d623f7 | 878 | struct pshmobj * pshmobj; |
2d21ac55 A |
879 | #if CONFIG_MACF |
880 | int error; | |
881 | #endif | |
1c79356b A |
882 | |
883 | if (user_size == 0) | |
884 | return(0); | |
885 | ||
886 | if ((flags & MAP_SHARED) == 0) | |
887 | return(EINVAL); | |
888 | ||
889 | ||
890 | if ((prot & PROT_WRITE) && ((fp->f_flag & FWRITE) == 0)) { | |
891 | return(EPERM); | |
892 | } | |
893 | ||
894 | if (((pnode = (struct pshmnode *)fp->f_data)) == PSHMNODE_NULL ) | |
895 | return(EINVAL); | |
896 | ||
91447636 A |
897 | PSHM_SUBSYS_LOCK(); |
898 | if ((pinfo = pnode->pinfo) == PSHMINFO_NULL) { | |
899 | PSHM_SUBSYS_UNLOCK(); | |
1c79356b | 900 | return(EINVAL); |
91447636 | 901 | } |
1c79356b A |
902 | |
903 | if ((pinfo->pshm_flags & PSHM_ALLOCATED) != PSHM_ALLOCATED) { | |
91447636 | 904 | PSHM_SUBSYS_UNLOCK(); |
1c79356b A |
905 | return(EINVAL); |
906 | } | |
5c9f4661 | 907 | if (user_size > (vm_map_size_t)pinfo->pshm_length) { |
91447636 | 908 | PSHM_SUBSYS_UNLOCK(); |
1c79356b A |
909 | return(EINVAL); |
910 | } | |
5c9f4661 A |
911 | vm_map_size_t end_pos = 0; |
912 | if (os_add_overflow(user_size, file_pos, &end_pos)) { | |
913 | PSHM_SUBSYS_UNLOCK(); | |
914 | return(EINVAL); | |
915 | } | |
916 | if (end_pos > (vm_map_size_t)pinfo->pshm_length) { | |
91447636 | 917 | PSHM_SUBSYS_UNLOCK(); |
1c79356b A |
918 | return(EINVAL); |
919 | } | |
b0d623f7 | 920 | if ((pshmobj = pinfo->pshm_memobjects) == NULL) { |
91447636 | 921 | PSHM_SUBSYS_UNLOCK(); |
1c79356b A |
922 | return(EINVAL); |
923 | } | |
924 | ||
2d21ac55 A |
925 | #if CONFIG_MACF |
926 | error = mac_posixshm_check_mmap(kauth_cred_get(), pinfo, prot, flags); | |
927 | if (error) { | |
928 | PSHM_SUBSYS_UNLOCK(); | |
929 | return(error); | |
930 | } | |
931 | #endif | |
91447636 A |
932 | |
933 | PSHM_SUBSYS_UNLOCK(); | |
1c79356b A |
934 | user_map = current_map(); |
935 | ||
936 | if ((flags & MAP_FIXED) == 0) { | |
91447636 | 937 | alloc_flags = VM_FLAGS_ANYWHERE; |
39236c6e A |
938 | user_addr = vm_map_round_page(user_addr, |
939 | vm_map_page_mask(user_map)); | |
1c79356b | 940 | } else { |
39236c6e A |
941 | if (user_addr != vm_map_round_page(user_addr, |
942 | vm_map_page_mask(user_map))) | |
1c79356b | 943 | return (EINVAL); |
91447636 A |
944 | /* |
945 | * We do not get rid of the existing mappings here because | |
946 | * it wouldn't be atomic (see comment in mmap()). We let | |
947 | * Mach VM know that we want it to replace any existing | |
948 | * mapping with the new one. | |
949 | */ | |
950 | alloc_flags = VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE; | |
1c79356b A |
951 | } |
952 | docow = FALSE; | |
953 | ||
b0d623f7 | 954 | mapped_size = 0; |
5ba3f43e A |
955 | vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; |
956 | /* reserve the entire space first... */ | |
b0d623f7 A |
957 | kret = vm_map_enter_mem_object(user_map, |
958 | &user_addr, | |
959 | user_size, | |
960 | 0, | |
961 | alloc_flags, | |
5ba3f43e A |
962 | vmk_flags, |
963 | VM_KERN_MEMORY_NONE, | |
b0d623f7 A |
964 | IPC_PORT_NULL, |
965 | 0, | |
966 | FALSE, | |
967 | VM_PROT_NONE, | |
968 | VM_PROT_NONE, | |
969 | VM_INHERIT_NONE); | |
970 | user_start_addr = user_addr; | |
1c79356b | 971 | if (kret != KERN_SUCCESS) { |
1c79356b A |
972 | goto out; |
973 | } | |
b0d623f7 A |
974 | |
975 | /* ... and overwrite with the real mappings */ | |
976 | for (map_pos = 0, pshmobj = pinfo->pshm_memobjects; | |
977 | user_size != 0; | |
978 | map_pos += pshmobj->pshmo_size, pshmobj = pshmobj->pshmo_next) { | |
979 | if (pshmobj == NULL) { | |
980 | /* nothing there to map !? */ | |
981 | goto out; | |
982 | } | |
983 | if (file_pos >= map_pos + pshmobj->pshmo_size) { | |
984 | continue; | |
985 | } | |
986 | map_size = pshmobj->pshmo_size - (file_pos - map_pos); | |
987 | if (map_size > user_size) { | |
988 | map_size = user_size; | |
989 | } | |
5ba3f43e | 990 | vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; |
b0d623f7 A |
991 | kret = vm_map_enter_mem_object( |
992 | user_map, | |
993 | &user_addr, | |
994 | map_size, | |
995 | 0, | |
996 | VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, | |
5ba3f43e A |
997 | vmk_flags, |
998 | VM_KERN_MEMORY_NONE, | |
b0d623f7 A |
999 | pshmobj->pshmo_memobject, |
1000 | file_pos - map_pos, | |
1001 | docow, | |
1002 | prot, | |
1003 | VM_PROT_DEFAULT, | |
1004 | VM_INHERIT_SHARE); | |
1005 | if (kret != KERN_SUCCESS) | |
1006 | goto out; | |
1007 | ||
1008 | user_addr += map_size; | |
1009 | user_size -= map_size; | |
1010 | mapped_size += map_size; | |
1011 | file_pos += map_size; | |
1012 | } | |
1013 | ||
91447636 | 1014 | PSHM_SUBSYS_LOCK(); |
b0d623f7 A |
1015 | pnode->mapp_addr = user_start_addr; |
1016 | pnode->map_size = mapped_size; | |
1c79356b | 1017 | pinfo->pshm_flags |= (PSHM_MAPPED | PSHM_INUSE); |
91447636 | 1018 | PSHM_SUBSYS_UNLOCK(); |
1c79356b | 1019 | out: |
b0d623f7 A |
1020 | if (kret != KERN_SUCCESS) { |
1021 | if (mapped_size != 0) { | |
1022 | (void) mach_vm_deallocate(current_map(), | |
1023 | user_start_addr, | |
1024 | mapped_size); | |
1025 | } | |
1026 | } | |
1027 | ||
1c79356b A |
1028 | switch (kret) { |
1029 | case KERN_SUCCESS: | |
b0d623f7 | 1030 | *retval = (user_start_addr + pageoff); |
1c79356b A |
1031 | return (0); |
1032 | case KERN_INVALID_ADDRESS: | |
1033 | case KERN_NO_SPACE: | |
1034 | return (ENOMEM); | |
1035 | case KERN_PROTECTION_FAILURE: | |
1036 | return (EACCES); | |
1037 | default: | |
1038 | return (EINVAL); | |
1039 | } | |
1040 | ||
1041 | } | |
1042 | ||
490019cf A |
1043 | static int |
1044 | pshm_unlink_internal(struct pshminfo *pinfo, struct pshmcache *pcache) | |
1045 | { | |
1046 | struct pshmobj *pshmobj, *pshmobj_next; | |
1047 | ||
1048 | PSHM_SUBSYS_ASSERT_HELD(); | |
1049 | ||
1050 | if (!pinfo || !pcache) | |
1051 | return EINVAL; | |
1052 | ||
1053 | if ((pinfo->pshm_flags & (PSHM_DEFINED | PSHM_ALLOCATED)) == 0) | |
1054 | return EINVAL; | |
1055 | ||
1056 | if (pinfo->pshm_flags & PSHM_INDELETE) | |
1057 | return 0; | |
1058 | ||
1059 | pinfo->pshm_flags |= PSHM_INDELETE; | |
1060 | pinfo->pshm_usecount--; | |
1061 | ||
1062 | pshm_cache_delete(pcache); | |
1063 | pinfo->pshm_flags |= PSHM_REMOVED; | |
1064 | ||
1065 | /* release the existence reference */ | |
1066 | if (!pinfo->pshm_usecount) { | |
1067 | #if CONFIG_MACF | |
1068 | mac_posixshm_label_destroy(pinfo); | |
1069 | #endif | |
1070 | /* | |
1071 | * If this is the last reference going away on the object, | |
1072 | * then we need to destroy the backing object. The name | |
1073 | * has an implied but uncounted reference on the object, | |
1074 | * once it's created, since it's used as a rendezvous, and | |
1075 | * therefore may be subsequently reopened. | |
1076 | */ | |
1077 | for (pshmobj = pinfo->pshm_memobjects; | |
1078 | pshmobj != NULL; | |
1079 | pshmobj = pshmobj_next) { | |
1080 | mach_memory_entry_port_release(pshmobj->pshmo_memobject); | |
1081 | pshmobj_next = pshmobj->pshmo_next; | |
1082 | FREE(pshmobj, M_SHM); | |
1083 | } | |
1084 | FREE(pinfo,M_SHM); | |
1085 | } | |
1086 | ||
1087 | FREE(pcache, M_SHM); | |
1088 | ||
1089 | return 0; | |
1090 | } | |
1091 | ||
1c79356b | 1092 | int |
490019cf | 1093 | shm_unlink(proc_t p, struct shm_unlink_args *uap, __unused int32_t *retval) |
1c79356b | 1094 | { |
91447636 | 1095 | size_t i; |
490019cf A |
1096 | char * pnbuf; |
1097 | size_t pathlen; | |
1098 | int error = 0; | |
1099 | ||
1c79356b A |
1100 | struct pshmname nd; |
1101 | struct pshminfo *pinfo; | |
1c79356b A |
1102 | char * nameptr; |
1103 | char * cp; | |
1c79356b | 1104 | struct pshmcache *pcache = PSHMCACHE_NULL; |
1c79356b A |
1105 | |
1106 | pinfo = PSHMINFO_NULL; | |
1107 | ||
490019cf | 1108 | |
91447636 A |
1109 | MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); |
1110 | if (pnbuf == NULL) { | |
1111 | return(ENOSPC); /* XXX non-standard */ | |
1112 | } | |
1c79356b | 1113 | pathlen = MAXPATHLEN; |
91447636 | 1114 | error = copyinstr(uap->name, (void *)pnbuf, MAXPATHLEN, &pathlen); |
1c79356b A |
1115 | if (error) { |
1116 | goto bad; | |
1117 | } | |
e5568f75 | 1118 | AUDIT_ARG(text, pnbuf); |
1c79356b A |
1119 | if (pathlen > PSHMNAMLEN) { |
1120 | error = ENAMETOOLONG; | |
1121 | goto bad; | |
1122 | } | |
1123 | ||
490019cf | 1124 | nameptr = pnbuf; |
1c79356b A |
1125 | |
1126 | #ifdef PSXSHM_NAME_RESTRICT | |
1c79356b A |
1127 | if (*nameptr == '/') { |
1128 | while (*(nameptr++) == '/') { | |
490019cf | 1129 | pathlen--; |
1c79356b A |
1130 | error = EINVAL; |
1131 | goto bad; | |
1132 | } | |
1133 | } else { | |
1134 | error = EINVAL; | |
1135 | goto bad; | |
1136 | } | |
1137 | #endif /* PSXSHM_NAME_RESTRICT */ | |
1138 | ||
1c79356b | 1139 | nd.pshm_nameptr = nameptr; |
490019cf A |
1140 | nd.pshm_namelen = pathlen; |
1141 | nd.pshm_hash = 0; | |
1c79356b | 1142 | |
490019cf | 1143 | for (cp = nameptr, i=1; *cp != 0 && i <= pathlen; i++, cp++) { |
1c79356b A |
1144 | nd.pshm_hash += (unsigned char)*cp * i; |
1145 | } | |
1146 | ||
91447636 | 1147 | PSHM_SUBSYS_LOCK(); |
6d2010ae | 1148 | error = pshm_cache_search(&pinfo, &nd, &pcache, 0); |
1c79356b | 1149 | |
39236c6e | 1150 | /* During unlink lookup failure also implies ENOENT */ |
490019cf | 1151 | if (error != PSHMCACHE_FOUND) { |
91447636 | 1152 | PSHM_SUBSYS_UNLOCK(); |
39236c6e | 1153 | error = ENOENT; |
1c79356b | 1154 | goto bad; |
490019cf A |
1155 | |
1156 | } | |
1c79356b A |
1157 | |
1158 | if ((pinfo->pshm_flags & (PSHM_DEFINED | PSHM_ALLOCATED))==0) { | |
91447636 | 1159 | PSHM_SUBSYS_UNLOCK(); |
2d21ac55 A |
1160 | error = EINVAL; |
1161 | goto bad; | |
1c79356b A |
1162 | } |
1163 | ||
b0d623f7 A |
1164 | if (pinfo->pshm_flags & PSHM_ALLOCATING) { |
1165 | /* XXX should we wait for flag to clear and then proceed ? */ | |
1166 | PSHM_SUBSYS_UNLOCK(); | |
1167 | error = EAGAIN; | |
1168 | goto bad; | |
1169 | } | |
1170 | ||
1c79356b | 1171 | if (pinfo->pshm_flags & PSHM_INDELETE) { |
91447636 | 1172 | PSHM_SUBSYS_UNLOCK(); |
1c79356b A |
1173 | error = 0; |
1174 | goto bad; | |
1175 | } | |
490019cf | 1176 | |
2d21ac55 A |
1177 | #if CONFIG_MACF |
1178 | error = mac_posixshm_check_unlink(kauth_cred_get(), pinfo, nameptr); | |
1179 | if (error) { | |
1180 | PSHM_SUBSYS_UNLOCK(); | |
1181 | goto bad; | |
1182 | } | |
1183 | #endif | |
1c79356b | 1184 | |
e5568f75 A |
1185 | AUDIT_ARG(posix_ipc_perm, pinfo->pshm_uid, pinfo->pshm_gid, |
1186 | pinfo->pshm_mode); | |
91447636 | 1187 | |
316670eb | 1188 | /* |
490019cf A |
1189 | * following file semantics, unlink should be allowed |
1190 | * for users with write permission only. | |
91447636 | 1191 | */ |
316670eb A |
1192 | if ( (error = pshm_access(pinfo, FWRITE, kauth_cred_get(), p)) ) { |
1193 | PSHM_SUBSYS_UNLOCK(); | |
1194 | goto bad; | |
1195 | } | |
91447636 | 1196 | |
490019cf A |
1197 | error = pshm_unlink_internal(pinfo, pcache); |
1198 | PSHM_SUBSYS_UNLOCK(); | |
1199 | ||
1c79356b | 1200 | bad: |
55e303ae | 1201 | FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI); |
490019cf | 1202 | return error; |
1c79356b | 1203 | } |
1c79356b | 1204 | |
91447636 A |
1205 | /* already called locked */ |
1206 | static int | |
6d2010ae | 1207 | pshm_close(struct pshminfo *pinfo, int dropref) |
1c79356b | 1208 | { |
6d2010ae | 1209 | int error = 0; |
b0d623f7 | 1210 | struct pshmobj *pshmobj, *pshmobj_next; |
1c79356b | 1211 | |
6d2010ae A |
1212 | /* |
1213 | * If we are dropping the reference we took on the cache object, don't | |
1214 | * enforce the allocation requirement. | |
1215 | */ | |
1216 | if ( !dropref && ((pinfo->pshm_flags & PSHM_ALLOCATED) != PSHM_ALLOCATED)) { | |
1c79356b A |
1217 | return(EINVAL); |
1218 | } | |
1219 | #if DIAGNOSTIC | |
1220 | if(!pinfo->pshm_usecount) { | |
1221 | kprintf("negative usecount in pshm_close\n"); | |
1222 | } | |
1223 | #endif /* DIAGNOSTIC */ | |
91447636 | 1224 | pinfo->pshm_usecount--; /* release this fd's reference */ |
1c79356b A |
1225 | |
1226 | if ((pinfo->pshm_flags & PSHM_REMOVED) && !pinfo->pshm_usecount) { | |
b0d623f7 A |
1227 | #if CONFIG_MACF |
1228 | mac_posixshm_label_destroy(pinfo); | |
1229 | #endif | |
91447636 A |
1230 | PSHM_SUBSYS_UNLOCK(); |
1231 | /* | |
1232 | * If this is the last reference going away on the object, | |
1233 | * then we need to destroy the backing object. | |
1234 | */ | |
b0d623f7 A |
1235 | for (pshmobj = pinfo->pshm_memobjects; |
1236 | pshmobj != NULL; | |
1237 | pshmobj = pshmobj_next) { | |
1238 | mach_memory_entry_port_release(pshmobj->pshmo_memobject); | |
1239 | pshmobj_next = pshmobj->pshmo_next; | |
1240 | FREE(pshmobj, M_SHM); | |
1241 | } | |
91447636 A |
1242 | PSHM_SUBSYS_LOCK(); |
1243 | FREE(pinfo,M_SHM); | |
1244 | } | |
1c79356b A |
1245 | return (error); |
1246 | } | |
9bccf70c | 1247 | |
2d21ac55 | 1248 | /* vfs_context_t passed to match prototype for struct fileops */ |
9bccf70c | 1249 | static int |
2d21ac55 | 1250 | pshm_closefile(struct fileglob *fg, __unused vfs_context_t ctx) |
9bccf70c | 1251 | { |
6d2010ae A |
1252 | int error = EINVAL; |
1253 | struct pshmnode *pnode; | |
91447636 A |
1254 | |
1255 | PSHM_SUBSYS_LOCK(); | |
6d2010ae A |
1256 | |
1257 | if ((pnode = (struct pshmnode *)fg->fg_data) != NULL) { | |
1258 | if (pnode->pinfo != PSHMINFO_NULL) { | |
1259 | error = pshm_close(pnode->pinfo, 0); | |
1260 | } | |
1261 | FREE(pnode, M_SHM); | |
1262 | } | |
1263 | ||
91447636 | 1264 | PSHM_SUBSYS_UNLOCK(); |
6d2010ae | 1265 | |
91447636 | 1266 | return(error); |
9bccf70c A |
1267 | } |
1268 | ||
1269 | static int | |
5ba3f43e A |
1270 | pshm_read(__unused struct fileproc *fp, __unused struct uio *uio, |
1271 | __unused int flags, __unused vfs_context_t ctx) | |
1c79356b | 1272 | { |
91447636 | 1273 | return(ENOTSUP); |
1c79356b | 1274 | } |
9bccf70c A |
1275 | |
1276 | static int | |
5ba3f43e A |
1277 | pshm_write(__unused struct fileproc *fp, __unused struct uio *uio, |
1278 | __unused int flags, __unused vfs_context_t ctx) | |
1c79356b | 1279 | { |
91447636 | 1280 | return(ENOTSUP); |
1c79356b | 1281 | } |
9bccf70c A |
1282 | |
1283 | static int | |
5ba3f43e A |
1284 | pshm_ioctl(__unused struct fileproc *fp, __unused u_long com, |
1285 | __unused caddr_t data, __unused vfs_context_t ctx) | |
1c79356b | 1286 | { |
91447636 | 1287 | return(ENOTSUP); |
1c79356b | 1288 | } |
9bccf70c A |
1289 | |
1290 | static int | |
5ba3f43e A |
1291 | pshm_select(__unused struct fileproc *fp, __unused int which, __unused void *wql, |
1292 | __unused vfs_context_t ctx) | |
1c79356b | 1293 | { |
91447636 | 1294 | return(ENOTSUP); |
1c79356b | 1295 | } |
55e303ae A |
1296 | |
1297 | static int | |
39037602 | 1298 | pshm_kqfilter(__unused struct fileproc *fp, struct knote *kn, |
5ba3f43e | 1299 | __unused struct kevent_internal_s *kev, __unused vfs_context_t ctx) |
55e303ae | 1300 | { |
39037602 A |
1301 | kn->kn_flags = EV_ERROR; |
1302 | kn->kn_data = ENOTSUP; | |
1303 | return 0; | |
55e303ae | 1304 | } |
0c530ab8 A |
1305 | |
1306 | int | |
1307 | fill_pshminfo(struct pshmnode * pshm, struct pshm_info * info) | |
1308 | { | |
1309 | struct pshminfo *pinfo; | |
2d21ac55 | 1310 | struct vinfo_stat *sb; |
0c530ab8 A |
1311 | |
1312 | PSHM_SUBSYS_LOCK(); | |
1313 | if ((pinfo = pshm->pinfo) == PSHMINFO_NULL){ | |
1314 | PSHM_SUBSYS_UNLOCK(); | |
1315 | return(EINVAL); | |
1316 | } | |
1317 | ||
1318 | sb = &info->pshm_stat; | |
1319 | ||
2d21ac55 A |
1320 | bzero(sb, sizeof(struct vinfo_stat)); |
1321 | sb->vst_mode = pinfo->pshm_mode; | |
1322 | sb->vst_uid = pinfo->pshm_uid; | |
1323 | sb->vst_gid = pinfo->pshm_gid; | |
1324 | sb->vst_size = pinfo->pshm_length; | |
0c530ab8 A |
1325 | |
1326 | info->pshm_mappaddr = pshm->mapp_addr; | |
1327 | bcopy(&pinfo->pshm_name[0], &info->pshm_name[0], PSHMNAMLEN+1); | |
1328 | ||
1329 | PSHM_SUBSYS_UNLOCK(); | |
1330 | return(0); | |
1331 | } | |
1332 | ||
2d21ac55 A |
1333 | #if CONFIG_MACF |
1334 | void | |
1335 | pshm_label_associate(struct fileproc *fp, struct vnode *vp, vfs_context_t ctx) | |
1336 | { | |
1337 | struct pshmnode *pnode; | |
1338 | struct pshminfo *pshm; | |
0c530ab8 | 1339 | |
2d21ac55 A |
1340 | PSHM_SUBSYS_LOCK(); |
1341 | pnode = (struct pshmnode *)fp->f_fglob->fg_data; | |
1342 | if (pnode != NULL) { | |
1343 | pshm = pnode->pinfo; | |
1344 | if (pshm != NULL) | |
1345 | mac_posixshm_vnode_label_associate( | |
1346 | vfs_context_ucred(ctx), pshm, pshm->pshm_label, | |
1347 | vp, vp->v_label); | |
1348 | } | |
1349 | PSHM_SUBSYS_UNLOCK(); | |
1350 | } | |
1351 | #endif |