1 | /* |
2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | * |
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. |
14 | * |
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 |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
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. |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ |
28 | /* |
29 | * Copyright (c) 1990, 1996-1998 Apple Computer, Inc. |
30 | * All Rights Reserved. |
31 | */ |
32 | /* |
33 | * posix_sem.c : Support for POSIX semaphore APIs |
34 | * |
35 | * File: posix_sem.c |
36 | * Author: Ananthakrishna Ramesh |
37 | * |
38 | * HISTORY |
39 | * 2-Sep-1999 A.Ramesh |
40 | * Created for MacOSX |
41 | * |
42 | */ |
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 | */ |
49 | |
50 | #include <sys/cdefs.h> |
51 | #include <sys/param.h> |
52 | #include <sys/systm.h> |
53 | #include <sys/kernel.h> |
54 | #include <sys/file_internal.h> |
55 | #include <sys/filedesc.h> |
56 | #include <sys/stat.h> |
57 | #include <sys/proc_internal.h> |
58 | #include <sys/kauth.h> |
59 | #include <sys/mount.h> |
60 | #include <sys/namei.h> |
61 | #include <sys/vnode.h> |
62 | #include <sys/ioctl.h> |
63 | #include <sys/tty.h> |
64 | #include <sys/malloc.h> |
65 | #include <sys/semaphore.h> |
66 | #include <sys/sysproto.h> |
67 | #include <sys/proc_info.h> |
68 | |
69 | #if CONFIG_MACF |
70 | #include <sys/vnode_internal.h> |
71 | #include <security/mac_framework.h> |
72 | #endif |
73 | |
74 | #include <security/audit/audit.h> |
75 | |
76 | #include <mach/mach_types.h> |
77 | #include <mach/vm_prot.h> |
78 | #include <mach/semaphore.h> |
79 | #include <mach/sync_policy.h> |
80 | #include <mach/task.h> |
81 | #include <kern/kern_types.h> |
82 | #include <kern/task.h> |
83 | #include <kern/clock.h> |
84 | #include <mach/kern_return.h> |
85 | |
86 | #define f_flag fp_glob->fg_flag |
87 | #define f_ops fp_glob->fg_ops |
88 | |
89 | #define PSEMNAMLEN 31 /* maximum name segment length we bother with */ |
90 | |
91 | struct pseminfo { |
92 | unsigned int psem_flags; |
93 | unsigned int psem_usecount; |
94 | mode_t psem_mode; |
95 | uid_t psem_uid; |
96 | gid_t psem_gid; |
97 | char psem_name[PSEMNAMLEN + 1]; /* segment name */ |
98 | semaphore_t psem_semobject; |
99 | struct label * psem_label; |
100 | pid_t psem_creator_pid; |
101 | uint64_t psem_creator_uniqueid; |
102 | }; |
103 | #define PSEMINFO_NULL (struct pseminfo *)0 |
104 | |
105 | #define PSEM_NONE 1 |
106 | #define PSEM_DEFINED 2 |
107 | #define PSEM_ALLOCATED 4 |
108 | #define PSEM_MAPPED 8 |
109 | #define PSEM_INUSE 0x10 |
110 | #define PSEM_REMOVED 0x20 |
111 | #define PSEM_INCREATE 0x40 |
112 | #define PSEM_INDELETE 0x80 |
113 | |
114 | struct psemcache { |
115 | LIST_ENTRY(psemcache) psem_hash; /* hash chain */ |
116 | struct pseminfo *pseminfo; /* vnode the name refers to */ |
117 | size_t psem_nlen; /* length of name */ |
118 | char psem_name[PSEMNAMLEN + 1]; /* segment name */ |
119 | }; |
120 | #define PSEMCACHE_NULL (struct psemcache *)0 |
121 | |
122 | #define PSEMCACHE_NOTFOUND (0) |
123 | #define PSEMCACHE_FOUND (-1) |
124 | #define PSEMCACHE_NEGATIVE (ENOENT) |
125 | |
126 | struct psemstats { |
127 | long goodhits; /* hits that we can really use */ |
128 | long neghits; /* negative hits that we can use */ |
129 | long badhits; /* hits we must drop */ |
130 | long falsehits; /* hits with id mismatch */ |
131 | long miss; /* misses */ |
132 | long longnames; /* long names that ignore cache */ |
133 | }; |
134 | |
135 | struct psemname { |
136 | char *psem_nameptr; /* pointer to looked up name */ |
137 | size_t psem_namelen; /* length of looked up component */ |
138 | u_int32_t psem_hash; /* hash value of looked up name */ |
139 | }; |
140 | |
141 | struct psemnode { |
142 | struct pseminfo *pinfo; |
143 | #if DIAGNOSTIC |
144 | unsigned int readcnt; |
145 | unsigned int writecnt; |
146 | #endif |
147 | }; |
148 | #define PSEMNODE_NULL (struct psemnode *)0 |
149 | |
150 | |
151 | #define PSEMHASH(pnp) \ |
152 | (&psemhashtbl[(pnp)->psem_hash & psemhash]) |
153 | LIST_HEAD(psemhashhead, psemcache) * psemhashtbl; /* Hash Table */ |
154 | u_long psemhash; /* size of hash table - 1 */ |
155 | long psemnument; /* number of cache entries allocated */ |
156 | long posix_sem_max = 10000; /* tunable for max POSIX semaphores */ |
157 | /* 10000 limits to ~1M of memory */ |
158 | SYSCTL_NODE(_kern, KERN_POSIX, posix, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Posix" ); |
159 | SYSCTL_NODE(_kern_posix, OID_AUTO, sem, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Semaphores" ); |
160 | SYSCTL_LONG(_kern_posix_sem, OID_AUTO, max, CTLFLAG_RW | CTLFLAG_LOCKED, &posix_sem_max, "max" ); |
161 | |
162 | struct psemstats psemstats; /* cache effectiveness statistics */ |
163 | |
164 | static int psem_access(struct pseminfo *pinfo, mode_t mode, kauth_cred_t cred); |
165 | static int psem_cache_search(struct pseminfo **, |
166 | struct psemname *, struct psemcache **); |
167 | static int psem_delete(struct pseminfo * pinfo); |
168 | |
169 | static int psem_closefile(struct fileglob *fp, vfs_context_t ctx); |
170 | static int psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache); |
171 | |
172 | static const struct fileops psemops = { |
173 | .fo_type = DTYPE_PSXSEM, |
174 | .fo_read = fo_no_read, |
175 | .fo_write = fo_no_write, |
176 | .fo_ioctl = fo_no_ioctl, |
177 | .fo_select = fo_no_select, |
178 | .fo_close = psem_closefile, |
179 | .fo_drain = fo_no_drain, |
180 | .fo_kqfilter = fo_no_kqfilter, |
181 | }; |
182 | |
183 | static LCK_GRP_DECLARE(psx_sem_subsys_lck_grp, "posix semaphores" ); |
184 | static LCK_MTX_DECLARE(psx_sem_subsys_mutex, &psx_sem_subsys_lck_grp); |
185 | |
186 | #define PSEM_SUBSYS_LOCK() lck_mtx_lock(&psx_sem_subsys_mutex) |
187 | #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(&psx_sem_subsys_mutex) |
188 | #define PSEM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_sem_subsys_mutex, LCK_MTX_ASSERT_OWNED) |
189 | |
190 | |
191 | static int psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp); |
192 | static void psem_cache_delete(struct psemcache *pcp); |
193 | int psem_cache_purge_all(void); |
194 | |
195 | /* |
196 | * Lookup an entry in the cache |
197 | * |
198 | * |
199 | * status of -1 is returned if matches |
200 | * If the lookup determines that the name does not exist |
201 | * (negative cacheing), a status of ENOENT is returned. If the lookup |
202 | * fails, a status of zero is returned. |
203 | */ |
204 | |
205 | static int |
206 | psem_cache_search(struct pseminfo **psemp, struct psemname *pnp, |
207 | struct psemcache **pcache) |
208 | { |
209 | struct psemcache *pcp, *nnp; |
210 | struct psemhashhead *pcpp; |
211 | |
212 | if (pnp->psem_namelen > PSEMNAMLEN) { |
213 | psemstats.longnames++; |
214 | return PSEMCACHE_NOTFOUND; |
215 | } |
216 | |
217 | pcpp = PSEMHASH(pnp); |
218 | for (pcp = pcpp->lh_first; pcp != 0; pcp = nnp) { |
219 | nnp = pcp->psem_hash.le_next; |
220 | if (pcp->psem_nlen == pnp->psem_namelen && |
221 | !bcmp(s1: pcp->psem_name, s2: pnp->psem_nameptr, n: pcp->psem_nlen)) { |
222 | break; |
223 | } |
224 | } |
225 | |
226 | if (pcp == 0) { |
227 | psemstats.miss++; |
228 | return PSEMCACHE_NOTFOUND; |
229 | } |
230 | |
231 | /* We found a "positive" match, return the vnode */ |
232 | if (pcp->pseminfo) { |
233 | psemstats.goodhits++; |
234 | /* TOUCH(ncp); */ |
235 | *psemp = pcp->pseminfo; |
236 | *pcache = pcp; |
237 | return PSEMCACHE_FOUND; |
238 | } |
239 | |
240 | /* |
241 | * We found a "negative" match, ENOENT notifies client of this match. |
242 | * The nc_vpid field records whether this is a whiteout. |
243 | */ |
244 | psemstats.neghits++; |
245 | return PSEMCACHE_NEGATIVE; |
246 | } |
247 | |
248 | /* |
249 | * Add an entry to the cache. |
250 | */ |
251 | static int |
252 | psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp) |
253 | { |
254 | struct psemhashhead *pcpp; |
255 | struct pseminfo *dpinfo; |
256 | struct psemcache *dpcp; |
257 | |
258 | #if DIAGNOSTIC |
259 | if (pnp->psem_namelen > PSEMNAMLEN) { |
260 | panic("cache_enter: name too long" ); |
261 | } |
262 | #endif |
263 | |
264 | |
265 | /* if the entry has already been added by some one else return */ |
266 | if (psem_cache_search(psemp: &dpinfo, pnp, pcache: &dpcp) == PSEMCACHE_FOUND) { |
267 | return EEXIST; |
268 | } |
269 | if (psemnument >= posix_sem_max) { |
270 | return ENOSPC; |
271 | } |
272 | psemnument++; |
273 | /* |
274 | * Fill in cache info, if vp is NULL this is a "negative" cache entry. |
275 | * For negative entries, we have to record whether it is a whiteout. |
276 | * the whiteout flag is stored in the nc_vpid field which is |
277 | * otherwise unused. |
278 | */ |
279 | pcp->pseminfo = psemp; |
280 | pcp->psem_nlen = pnp->psem_namelen; |
281 | bcopy(src: pnp->psem_nameptr, dst: pcp->psem_name, n: pcp->psem_nlen); |
282 | pcpp = PSEMHASH(pnp); |
283 | #if DIAGNOSTIC |
284 | { |
285 | struct psemcache *p; |
286 | |
287 | for (p = pcpp->lh_first; p != 0; p = p->psem_hash.le_next) { |
288 | if (p == pcp) { |
289 | panic("psem:cache_enter duplicate" ); |
290 | } |
291 | } |
292 | } |
293 | #endif |
294 | LIST_INSERT_HEAD(pcpp, pcp, psem_hash); |
295 | return 0; |
296 | } |
297 | |
298 | /* |
299 | * Name cache initialization, from vfs_init() when we are booting |
300 | */ |
301 | void |
302 | psem_cache_init(void) |
303 | { |
304 | psemhashtbl = hashinit(count: (int)(posix_sem_max / 2), M_SHM, hashmask: &psemhash); |
305 | } |
306 | |
307 | static void |
308 | psem_cache_delete(struct psemcache *pcp) |
309 | { |
310 | #if DIAGNOSTIC |
311 | if (pcp->psem_hash.le_prev == 0) { |
312 | panic("psem namecache purge le_prev" ); |
313 | } |
314 | if (pcp->psem_hash.le_next == pcp) { |
315 | panic("namecache purge le_next" ); |
316 | } |
317 | #endif /* DIAGNOSTIC */ |
318 | LIST_REMOVE(pcp, psem_hash); |
319 | pcp->psem_hash.le_prev = NULL; |
320 | psemnument--; |
321 | } |
322 | |
323 | /* |
324 | * Remove all cached psem entries. Open semaphores (with a positive refcount) |
325 | * will continue to exist, but their cache entries tying them to a particular |
326 | * name/path will be removed making all future lookups on the name fail. |
327 | */ |
328 | int |
329 | psem_cache_purge_all(void) |
330 | { |
331 | struct psemcache *pcp, *tmppcp; |
332 | struct psemhashhead *pcpp; |
333 | int error = 0; |
334 | |
335 | if (kauth_cred_issuser(cred: kauth_cred_get()) == 0) { |
336 | return EPERM; |
337 | } |
338 | |
339 | PSEM_SUBSYS_LOCK(); |
340 | for (pcpp = &psemhashtbl[psemhash]; pcpp >= psemhashtbl; pcpp--) { |
341 | LIST_FOREACH_SAFE(pcp, pcpp, psem_hash, tmppcp) { |
342 | assert(pcp->psem_nlen); |
343 | /* |
344 | * unconditionally unlink the cache entry |
345 | */ |
346 | error = psem_unlink_internal(pinfo: pcp->pseminfo, pcache: pcp); |
347 | if (error) { |
348 | goto out; |
349 | } |
350 | } |
351 | } |
352 | assert(psemnument == 0); |
353 | |
354 | out: |
355 | PSEM_SUBSYS_UNLOCK(); |
356 | |
357 | if (error) { |
358 | printf("%s: Error %d removing all semaphores: %ld remain!\n" , |
359 | __func__, error, psemnument); |
360 | } |
361 | return error; |
362 | } |
363 | |
364 | /* |
365 | * In order to support unnamed POSIX semaphores, the named |
366 | * POSIX semaphores will have to move out of the per-process |
367 | * open filetable, and into a global table that is shared with |
368 | * unnamed POSIX semaphores, since unnamed POSIX semaphores |
369 | * are typically used by declaring instances in shared memory, |
370 | * and there's no other way to do this without changing the |
371 | * underlying type, which would introduce binary compatibility |
372 | * issues. |
373 | */ |
374 | int |
375 | sem_open(proc_t p, struct sem_open_args *uap, user_addr_t *retval) |
376 | { |
377 | size_t i; |
378 | int indx, error; |
379 | struct psemname nd; |
380 | struct pseminfo *pinfo; |
381 | struct fileproc *fp = NULL; |
382 | char *pnbuf = NULL; |
383 | struct pseminfo *new_pinfo = PSEMINFO_NULL; |
384 | struct psemnode *new_pnode = PSEMNODE_NULL; |
385 | struct psemcache *pcache = PSEMCACHE_NULL; |
386 | char * nameptr; |
387 | char * cp; |
388 | size_t pathlen, plen; |
389 | mode_t fmode; |
390 | mode_t cmode = (mode_t)uap->mode; |
391 | int value = uap->value; |
392 | int incache = 0; |
393 | struct psemcache *pcp = PSEMCACHE_NULL; |
394 | kern_return_t kret = KERN_INVALID_ADDRESS; /* default fail */ |
395 | |
396 | AUDIT_ARG(fflags, uap->oflag); |
397 | AUDIT_ARG(mode, (mode_t)uap->mode); |
398 | AUDIT_ARG(value32, uap->value); |
399 | |
400 | pinfo = PSEMINFO_NULL; |
401 | |
402 | /* |
403 | * Preallocate everything we might need up front to avoid taking |
404 | * and dropping the lock, opening us up to race conditions. |
405 | */ |
406 | pnbuf = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO); |
407 | |
408 | pathlen = MAXPATHLEN; |
409 | error = copyinstr(uaddr: uap->name, kaddr: pnbuf, MAXPATHLEN, done: &pathlen); |
410 | if (error) { |
411 | goto bad; |
412 | } |
413 | AUDIT_ARG(text, pnbuf); |
414 | if ((pathlen > PSEMNAMLEN)) { |
415 | error = ENAMETOOLONG; |
416 | goto bad; |
417 | } |
418 | |
419 | #ifdef PSXSEM_NAME_RESTRICT |
420 | nameptr = pnbuf; |
421 | if (*nameptr == '/') { |
422 | while (*(nameptr++) == '/') { |
423 | plen--; |
424 | error = EINVAL; |
425 | goto bad; |
426 | } |
427 | } else { |
428 | error = EINVAL; |
429 | goto bad; |
430 | } |
431 | #endif /* PSXSEM_NAME_RESTRICT */ |
432 | |
433 | plen = pathlen; |
434 | nameptr = pnbuf; |
435 | nd.psem_nameptr = nameptr; |
436 | nd.psem_namelen = plen; |
437 | nd.psem_hash = 0; |
438 | |
439 | for (cp = nameptr, i = 1; *cp != 0 && i <= plen; i++, cp++) { |
440 | nd.psem_hash += (unsigned char)*cp * i; |
441 | } |
442 | |
443 | /* |
444 | * attempt to allocate a new fp; if unsuccessful, the fp will be |
445 | * left unmodified (NULL). |
446 | */ |
447 | error = falloc(p, &fp, &indx); |
448 | if (error) { |
449 | goto bad; |
450 | } |
451 | |
452 | /* |
453 | * We allocate a new entry if we are less than the maximum |
454 | * allowed and the one at the front of the LRU list is in use. |
455 | * Otherwise we use the one at the front of the LRU list. |
456 | */ |
457 | pcp = kalloc_type(struct psemcache, Z_WAITOK | Z_ZERO | Z_NOFAIL); |
458 | new_pinfo = kalloc_type(struct pseminfo, Z_WAITOK | Z_ZERO | Z_NOFAIL); |
459 | #if CONFIG_MACF |
460 | mac_posixsem_label_init(psem: new_pinfo); |
461 | #endif |
462 | |
463 | /* |
464 | * Provisionally create the semaphore in the new_pinfo; we have to do |
465 | * this here to prevent locking later. We use the value of kret to |
466 | * signal success or failure, which is why we set its default value |
467 | * to KERN_INVALID_ADDRESS, above. |
468 | */ |
469 | |
470 | fmode = (mode_t)FFLAGS(uap->oflag); |
471 | |
472 | if ((fmode & O_CREAT)) { |
473 | if ((value < 0) || (value > SEM_VALUE_MAX)) { |
474 | error = EINVAL; |
475 | goto bad; |
476 | } |
477 | |
478 | kret = semaphore_create(task: kernel_task, semaphore: &new_pinfo->psem_semobject, SYNC_POLICY_FIFO, value); |
479 | |
480 | if (kret != KERN_SUCCESS) { |
481 | switch (kret) { |
482 | case KERN_RESOURCE_SHORTAGE: |
483 | error = ENOMEM; |
484 | break; |
485 | case KERN_PROTECTION_FAILURE: |
486 | error = EACCES; |
487 | break; |
488 | default: |
489 | error = EINVAL; |
490 | } |
491 | goto bad; |
492 | } |
493 | } |
494 | |
495 | new_pnode = kalloc_type(struct psemnode, Z_WAITOK | Z_ZERO | Z_NOFAIL); |
496 | |
497 | PSEM_SUBSYS_LOCK(); |
498 | error = psem_cache_search(psemp: &pinfo, pnp: &nd, pcache: &pcache); |
499 | |
500 | if (error == PSEMCACHE_NEGATIVE) { |
501 | error = EINVAL; |
502 | goto bad_locked; |
503 | } |
504 | |
505 | if (error == PSEMCACHE_FOUND) { |
506 | incache = 1; |
507 | } else { |
508 | incache = 0; |
509 | } |
510 | |
511 | cmode &= ALLPERMS; |
512 | |
513 | if (((fmode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) && incache) { |
514 | /* sem exists and opened O_EXCL */ |
515 | #if notyet |
516 | if (pinfo->psem_flags & PSEM_INDELETE) { |
517 | } |
518 | #endif |
519 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, |
520 | pinfo->psem_gid, pinfo->psem_mode); |
521 | error = EEXIST; |
522 | goto bad_locked; |
523 | } |
524 | if (((fmode & (O_CREAT | O_EXCL)) == O_CREAT) && incache) { |
525 | /* As per POSIX, O_CREAT has no effect */ |
526 | fmode &= ~O_CREAT; |
527 | } |
528 | |
529 | if ((fmode & O_CREAT)) { |
530 | /* create a new one (commit the allocation) */ |
531 | pinfo = new_pinfo; |
532 | pinfo->psem_flags = PSEM_DEFINED | PSEM_INCREATE; |
533 | pinfo->psem_usecount = 1; |
534 | pinfo->psem_mode = cmode; |
535 | pinfo->psem_uid = kauth_getuid(); |
536 | pinfo->psem_gid = kauth_getgid(); |
537 | bcopy(src: pnbuf, dst: &pinfo->psem_name[0], PSEMNAMLEN); |
538 | pinfo->psem_name[PSEMNAMLEN] = 0; |
539 | pinfo->psem_flags &= ~PSEM_DEFINED; |
540 | pinfo->psem_flags |= PSEM_ALLOCATED; |
541 | pinfo->psem_creator_pid = proc_getpid(p); |
542 | pinfo->psem_creator_uniqueid = proc_uniqueid(p); |
543 | |
544 | #if CONFIG_MACF |
545 | error = mac_posixsem_check_create(cred: kauth_cred_get(), name: nameptr); |
546 | if (error) { |
547 | goto bad_locked; |
548 | } |
549 | mac_posixsem_label_associate(cred: kauth_cred_get(), psem: pinfo, name: nameptr); |
550 | #endif |
551 | } else { |
552 | /* semaphore should exist as it is without O_CREAT */ |
553 | if (!incache) { |
554 | error = ENOENT; |
555 | goto bad_locked; |
556 | } |
557 | if (pinfo->psem_flags & PSEM_INDELETE) { |
558 | error = ENOENT; |
559 | goto bad_locked; |
560 | } |
561 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, |
562 | pinfo->psem_gid, pinfo->psem_mode); |
563 | #if CONFIG_MACF |
564 | error = mac_posixsem_check_open(cred: kauth_cred_get(), psem: pinfo); |
565 | if (error) { |
566 | goto bad_locked; |
567 | } |
568 | #endif |
569 | if ((error = psem_access(pinfo, mode: fmode, cred: kauth_cred_get()))) { |
570 | goto bad_locked; |
571 | } |
572 | } |
573 | |
574 | if (!incache) { |
575 | /* if successful, this will consume the pcp */ |
576 | if ((error = psem_cache_add(psemp: pinfo, pnp: &nd, pcp))) { |
577 | goto bad_locked; |
578 | } |
579 | } |
580 | pinfo->psem_flags &= ~PSEM_INCREATE; |
581 | pinfo->psem_usecount++; |
582 | new_pnode->pinfo = pinfo; |
583 | PSEM_SUBSYS_UNLOCK(); |
584 | |
585 | /* |
586 | * if incache, we did not use the new pcp or the new pcp or the |
587 | * new . and we must free them. |
588 | */ |
589 | if (incache) { |
590 | kfree_type(struct psemcache, pcp); |
591 | pcp = PSEMCACHE_NULL; |
592 | if (new_pinfo != PSEMINFO_NULL) { |
593 | /* return value ignored - we can't _not_ do this */ |
594 | (void)semaphore_destroy(task: kernel_task, semaphore: new_pinfo->psem_semobject); |
595 | #if CONFIG_MACF |
596 | mac_posixsem_label_destroy(psem: new_pinfo); |
597 | #endif |
598 | kfree_type(struct pseminfo, new_pinfo); |
599 | new_pinfo = PSEMINFO_NULL; |
600 | } |
601 | } |
602 | |
603 | proc_fdlock(p); |
604 | fp->f_flag = fmode & FMASK; |
605 | fp->f_ops = &psemops; |
606 | fp_set_data(fp, fg_data: new_pnode); |
607 | procfdtbl_releasefd(p, fd: indx, NULL); |
608 | fp_drop(p, fd: indx, fp, locked: 1); |
609 | proc_fdunlock(p); |
610 | |
611 | *retval = CAST_USER_ADDR_T(indx); |
612 | zfree(ZV_NAMEI, pnbuf); |
613 | return 0; |
614 | |
615 | bad_locked: |
616 | PSEM_SUBSYS_UNLOCK(); |
617 | bad: |
618 | kfree_type(struct psemcache, pcp); |
619 | |
620 | kfree_type(struct psemnode, new_pnode); |
621 | |
622 | if (fp != NULL) { |
623 | fp_free(p, fd: indx, fp); |
624 | } |
625 | |
626 | if (new_pinfo != PSEMINFO_NULL) { |
627 | /* |
628 | * kret signals whether or not we successfully created a |
629 | * Mach semaphore for this semaphore; if so, we need to |
630 | * destroy it here. |
631 | */ |
632 | if (kret == KERN_SUCCESS) { |
633 | /* return value ignored - we can't _not_ do this */ |
634 | (void)semaphore_destroy(task: kernel_task, semaphore: new_pinfo->psem_semobject); |
635 | } |
636 | #if CONFIG_MACF |
637 | mac_posixsem_label_destroy(psem: new_pinfo); |
638 | #endif |
639 | kfree_type(struct pseminfo, new_pinfo); |
640 | } |
641 | |
642 | if (pnbuf != NULL) { |
643 | zfree(ZV_NAMEI, pnbuf); |
644 | } |
645 | return error; |
646 | } |
647 | |
648 | /* |
649 | * XXX This code is repeated in several places |
650 | */ |
651 | static int |
652 | psem_access(struct pseminfo *pinfo, mode_t mode, kauth_cred_t cred) |
653 | { |
654 | mode_t mode_req = ((mode & FREAD) ? S_IRUSR : 0) | |
655 | ((mode & FWRITE) ? S_IWUSR : 0); |
656 | |
657 | /* Otherwise, user id 0 always gets access. */ |
658 | if (!suser(cred, NULL)) { |
659 | return 0; |
660 | } |
661 | |
662 | return posix_cred_access(cred, object_uid: pinfo->psem_uid, object_gid: pinfo->psem_gid, object_mode: pinfo->psem_mode, mode_req); |
663 | } |
664 | |
665 | static int |
666 | psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache) |
667 | { |
668 | PSEM_SUBSYS_ASSERT_HELD(); |
669 | |
670 | if (!pinfo || !pcache) { |
671 | return EINVAL; |
672 | } |
673 | |
674 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) == 0) { |
675 | return EINVAL; |
676 | } |
677 | |
678 | if (pinfo->psem_flags & PSEM_INDELETE) { |
679 | return 0; |
680 | } |
681 | |
682 | AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid, |
683 | pinfo->psem_mode); |
684 | |
685 | pinfo->psem_flags |= PSEM_INDELETE; |
686 | pinfo->psem_usecount--; |
687 | |
688 | if (!pinfo->psem_usecount) { |
689 | psem_delete(pinfo); |
690 | kfree_type(struct pseminfo, pinfo); |
691 | } else { |
692 | pinfo->psem_flags |= PSEM_REMOVED; |
693 | } |
694 | |
695 | psem_cache_delete(pcp: pcache); |
696 | kfree_type(struct psemcache, pcache); |
697 | return 0; |
698 | } |
699 | |
700 | |
701 | int |
702 | sem_unlink(__unused proc_t p, struct sem_unlink_args *uap, __unused int32_t *retval) |
703 | { |
704 | size_t i; |
705 | int error = 0; |
706 | struct psemname nd; |
707 | struct pseminfo *pinfo; |
708 | char * nameptr; |
709 | char * cp; |
710 | char * pnbuf; |
711 | size_t pathlen; |
712 | struct psemcache *pcache = PSEMCACHE_NULL; |
713 | |
714 | pinfo = PSEMINFO_NULL; |
715 | |
716 | pnbuf = zalloc(view: ZV_NAMEI); |
717 | |
718 | pathlen = MAXPATHLEN; |
719 | error = copyinstr(uaddr: uap->name, kaddr: pnbuf, MAXPATHLEN, done: &pathlen); |
720 | if (error) { |
721 | goto bad; |
722 | } |
723 | AUDIT_ARG(text, pnbuf); |
724 | if (pathlen > PSEMNAMLEN) { |
725 | error = ENAMETOOLONG; |
726 | goto bad; |
727 | } |
728 | |
729 | nameptr = pnbuf; |
730 | |
731 | #ifdef PSXSEM_NAME_RESTRICT |
732 | if (*nameptr == '/') { |
733 | while (*(nameptr++) == '/') { |
734 | pathlen--; |
735 | error = EINVAL; |
736 | goto bad; |
737 | } |
738 | } else { |
739 | error = EINVAL; |
740 | goto bad; |
741 | } |
742 | #endif /* PSXSEM_NAME_RESTRICT */ |
743 | |
744 | nd.psem_nameptr = nameptr; |
745 | nd.psem_namelen = pathlen; |
746 | nd.psem_hash = 0; |
747 | |
748 | for (cp = nameptr, i = 1; *cp != 0 && i <= pathlen; i++, cp++) { |
749 | nd.psem_hash += (unsigned char)*cp * i; |
750 | } |
751 | |
752 | PSEM_SUBSYS_LOCK(); |
753 | error = psem_cache_search(psemp: &pinfo, pnp: &nd, pcache: &pcache); |
754 | |
755 | if (error != PSEMCACHE_FOUND) { |
756 | PSEM_SUBSYS_UNLOCK(); |
757 | error = ENOENT; |
758 | goto bad; |
759 | } |
760 | |
761 | #if CONFIG_MACF |
762 | error = mac_posixsem_check_unlink(cred: kauth_cred_get(), psem: pinfo, name: nameptr); |
763 | if (error) { |
764 | PSEM_SUBSYS_UNLOCK(); |
765 | goto bad; |
766 | } |
767 | #endif |
768 | if ((error = psem_access(pinfo, mode: pinfo->psem_mode, cred: kauth_cred_get()))) { |
769 | PSEM_SUBSYS_UNLOCK(); |
770 | goto bad; |
771 | } |
772 | |
773 | error = psem_unlink_internal(pinfo, pcache); |
774 | PSEM_SUBSYS_UNLOCK(); |
775 | |
776 | bad: |
777 | zfree(ZV_NAMEI, pnbuf); |
778 | return error; |
779 | } |
780 | |
781 | int |
782 | sem_close(proc_t p, struct sem_close_args *uap, __unused int32_t *retval) |
783 | { |
784 | int fd = CAST_DOWN_EXPLICIT(int, uap->sem); |
785 | kauth_cred_t p_cred; |
786 | struct fileproc *fp; |
787 | |
788 | AUDIT_ARG(fd, fd); /* XXX This seems wrong; uap->sem is a pointer */ |
789 | |
790 | proc_fdlock(p); |
791 | if ((fp = fp_get_noref_locked(p, fd)) == NULL) { |
792 | proc_fdunlock(p); |
793 | return EBADF; |
794 | } |
795 | if (FILEGLOB_DTYPE(fp->fp_glob) != DTYPE_PSXSEM) { |
796 | proc_fdunlock(p); |
797 | return EBADF; |
798 | } |
799 | |
800 | p_cred = current_cached_proc_cred(p); |
801 | return fp_close_and_unlock(p, p_cred, fd, fp, flags: 0); |
802 | } |
803 | |
804 | int |
805 | sem_wait(proc_t p, struct sem_wait_args *uap, int32_t *retval) |
806 | { |
807 | __pthread_testcancel(presyscall: 1); |
808 | return sem_wait_nocancel(p, (struct sem_wait_nocancel_args *)uap, retval); |
809 | } |
810 | |
811 | int |
812 | sem_wait_nocancel(proc_t p, struct sem_wait_nocancel_args *uap, __unused int32_t *retval) |
813 | { |
814 | int fd = CAST_DOWN_EXPLICIT(int, uap->sem); |
815 | struct fileproc *fp; |
816 | struct pseminfo * pinfo; |
817 | struct psemnode * pnode; |
818 | kern_return_t kret; |
819 | int error; |
820 | |
821 | error = fp_get_ftype(p, fd, ftype: DTYPE_PSXSEM, EBADF, fpp: &fp); |
822 | if (error) { |
823 | return error; |
824 | } |
825 | pnode = (struct psemnode *)fp_get_data(fp); |
826 | |
827 | PSEM_SUBSYS_LOCK(); |
828 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { |
829 | PSEM_SUBSYS_UNLOCK(); |
830 | error = EINVAL; |
831 | goto out; |
832 | } |
833 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
834 | != PSEM_ALLOCATED) { |
835 | PSEM_SUBSYS_UNLOCK(); |
836 | error = EINVAL; |
837 | goto out; |
838 | } |
839 | #if CONFIG_MACF |
840 | error = mac_posixsem_check_wait(cred: kauth_cred_get(), psem: pinfo); |
841 | if (error) { |
842 | PSEM_SUBSYS_UNLOCK(); |
843 | goto out; |
844 | } |
845 | #endif |
846 | PSEM_SUBSYS_UNLOCK(); |
847 | kret = semaphore_wait(semaphore: pinfo->psem_semobject); |
848 | switch (kret) { |
849 | case KERN_INVALID_ADDRESS: |
850 | case KERN_PROTECTION_FAILURE: |
851 | error = EACCES; |
852 | break; |
853 | case KERN_ABORTED: |
854 | case KERN_OPERATION_TIMED_OUT: |
855 | error = EINTR; |
856 | break; |
857 | case KERN_SUCCESS: |
858 | error = 0; |
859 | break; |
860 | default: |
861 | error = EINVAL; |
862 | break; |
863 | } |
864 | out: |
865 | fp_drop(p, fd, fp, locked: 0); |
866 | return error; |
867 | } |
868 | |
869 | int |
870 | sem_trywait(proc_t p, struct sem_trywait_args *uap, __unused int32_t *retval) |
871 | { |
872 | int fd = CAST_DOWN_EXPLICIT(int, uap->sem); |
873 | struct fileproc *fp; |
874 | struct pseminfo * pinfo; |
875 | struct psemnode * pnode; |
876 | kern_return_t kret; |
877 | mach_timespec_t wait_time; |
878 | int error; |
879 | |
880 | error = fp_get_ftype(p, fd, ftype: DTYPE_PSXSEM, EBADF, fpp: &fp); |
881 | if (error) { |
882 | return error; |
883 | } |
884 | pnode = (struct psemnode *)fp_get_data(fp); |
885 | |
886 | PSEM_SUBSYS_LOCK(); |
887 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { |
888 | PSEM_SUBSYS_UNLOCK(); |
889 | error = EINVAL; |
890 | goto out; |
891 | } |
892 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
893 | != PSEM_ALLOCATED) { |
894 | PSEM_SUBSYS_UNLOCK(); |
895 | error = EINVAL; |
896 | goto out; |
897 | } |
898 | #if CONFIG_MACF |
899 | error = mac_posixsem_check_wait(cred: kauth_cred_get(), psem: pinfo); |
900 | if (error) { |
901 | PSEM_SUBSYS_UNLOCK(); |
902 | goto out; |
903 | } |
904 | #endif |
905 | PSEM_SUBSYS_UNLOCK(); |
906 | wait_time.tv_sec = 0; |
907 | wait_time.tv_nsec = 0; |
908 | |
909 | kret = semaphore_timedwait(semaphore: pinfo->psem_semobject, MACH_TIMESPEC_ZERO); |
910 | switch (kret) { |
911 | case KERN_INVALID_ADDRESS: |
912 | case KERN_PROTECTION_FAILURE: |
913 | error = EINVAL; |
914 | break; |
915 | case KERN_ABORTED: |
916 | error = EINTR; |
917 | break; |
918 | case KERN_OPERATION_TIMED_OUT: |
919 | error = EAGAIN; |
920 | break; |
921 | case KERN_SUCCESS: |
922 | error = 0; |
923 | break; |
924 | default: |
925 | error = EINVAL; |
926 | break; |
927 | } |
928 | out: |
929 | fp_drop(p, fd, fp, locked: 0); |
930 | return error; |
931 | } |
932 | |
933 | int |
934 | sem_post(proc_t p, struct sem_post_args *uap, __unused int32_t *retval) |
935 | { |
936 | int fd = CAST_DOWN_EXPLICIT(int, uap->sem); |
937 | struct fileproc *fp; |
938 | struct pseminfo * pinfo; |
939 | struct psemnode * pnode; |
940 | kern_return_t kret; |
941 | int error; |
942 | |
943 | error = fp_get_ftype(p, fd, ftype: DTYPE_PSXSEM, EBADF, fpp: &fp); |
944 | if (error) { |
945 | return error; |
946 | } |
947 | pnode = (struct psemnode *)fp_get_data(fp); |
948 | |
949 | PSEM_SUBSYS_LOCK(); |
950 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { |
951 | PSEM_SUBSYS_UNLOCK(); |
952 | error = EINVAL; |
953 | goto out; |
954 | } |
955 | if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) |
956 | != PSEM_ALLOCATED) { |
957 | PSEM_SUBSYS_UNLOCK(); |
958 | error = EINVAL; |
959 | goto out; |
960 | } |
961 | #if CONFIG_MACF |
962 | error = mac_posixsem_check_post(cred: kauth_cred_get(), psem: pinfo); |
963 | if (error) { |
964 | PSEM_SUBSYS_UNLOCK(); |
965 | goto out; |
966 | } |
967 | #endif |
968 | PSEM_SUBSYS_UNLOCK(); |
969 | kret = semaphore_signal(semaphore: pinfo->psem_semobject); |
970 | switch (kret) { |
971 | case KERN_INVALID_ADDRESS: |
972 | case KERN_PROTECTION_FAILURE: |
973 | error = EINVAL; |
974 | break; |
975 | case KERN_ABORTED: |
976 | case KERN_OPERATION_TIMED_OUT: |
977 | error = EINTR; |
978 | break; |
979 | case KERN_SUCCESS: |
980 | error = 0; |
981 | break; |
982 | default: |
983 | error = EINVAL; |
984 | break; |
985 | } |
986 | out: |
987 | fp_drop(p, fd, fp, locked: 0); |
988 | return error; |
989 | } |
990 | |
991 | static int |
992 | psem_close(struct psemnode *pnode) |
993 | { |
994 | int error = 0; |
995 | struct pseminfo *pinfo; |
996 | |
997 | PSEM_SUBSYS_LOCK(); |
998 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { |
999 | PSEM_SUBSYS_UNLOCK(); |
1000 | return EINVAL; |
1001 | } |
1002 | |
1003 | if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { |
1004 | PSEM_SUBSYS_UNLOCK(); |
1005 | return EINVAL; |
1006 | } |
1007 | #if DIAGNOSTIC |
1008 | if (!pinfo->psem_usecount) { |
1009 | kprintf("negative usecount in psem_close\n" ); |
1010 | } |
1011 | #endif /* DIAGNOSTIC */ |
1012 | pinfo->psem_usecount--; |
1013 | |
1014 | if ((pinfo->psem_flags & PSEM_REMOVED) && !pinfo->psem_usecount) { |
1015 | PSEM_SUBSYS_UNLOCK(); |
1016 | /* lock dropped as only semaphore is destroyed here */ |
1017 | error = psem_delete(pinfo); |
1018 | kfree_type(struct pseminfo, pinfo); |
1019 | } else { |
1020 | PSEM_SUBSYS_UNLOCK(); |
1021 | } |
1022 | /* subsystem lock is dropped when we get here */ |
1023 | kfree_type(struct psemnode, pnode); |
1024 | return error; |
1025 | } |
1026 | |
1027 | static int |
1028 | psem_closefile(struct fileglob *fg, __unused vfs_context_t ctx) |
1029 | { |
1030 | /* |
1031 | * Not locked as psem_close is called only from here and is locked |
1032 | * properly |
1033 | */ |
1034 | return psem_close(pnode: (struct psemnode *)fg_get_data(fg)); |
1035 | } |
1036 | |
1037 | static int |
1038 | psem_delete(struct pseminfo * pinfo) |
1039 | { |
1040 | kern_return_t kret; |
1041 | |
1042 | kret = semaphore_destroy(task: kernel_task, semaphore: pinfo->psem_semobject); |
1043 | #if CONFIG_MACF |
1044 | mac_posixsem_label_destroy(psem: pinfo); |
1045 | #endif |
1046 | |
1047 | switch (kret) { |
1048 | case KERN_INVALID_ADDRESS: |
1049 | case KERN_PROTECTION_FAILURE: |
1050 | return EINVAL; |
1051 | case KERN_ABORTED: |
1052 | case KERN_OPERATION_TIMED_OUT: |
1053 | return EINTR; |
1054 | case KERN_SUCCESS: |
1055 | return 0; |
1056 | default: |
1057 | return EINVAL; |
1058 | } |
1059 | } |
1060 | |
1061 | int |
1062 | fill_pseminfo(struct psemnode *pnode, struct psem_info * info) |
1063 | { |
1064 | struct pseminfo *pinfo; |
1065 | struct vinfo_stat *sb; |
1066 | |
1067 | PSEM_SUBSYS_LOCK(); |
1068 | if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { |
1069 | PSEM_SUBSYS_UNLOCK(); |
1070 | return EINVAL; |
1071 | } |
1072 | |
1073 | #if 0 |
1074 | if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { |
1075 | PSEM_SUBSYS_UNLOCK(); |
1076 | return EINVAL; |
1077 | } |
1078 | #endif |
1079 | |
1080 | sb = &info->psem_stat; |
1081 | bzero(s: sb, n: sizeof(struct vinfo_stat)); |
1082 | |
1083 | sb->vst_mode = pinfo->psem_mode; |
1084 | sb->vst_uid = pinfo->psem_uid; |
1085 | sb->vst_gid = pinfo->psem_gid; |
1086 | sb->vst_size = pinfo->psem_usecount; |
1087 | bcopy(src: &pinfo->psem_name[0], dst: &info->psem_name[0], PSEMNAMLEN + 1); |
1088 | |
1089 | PSEM_SUBSYS_UNLOCK(); |
1090 | return 0; |
1091 | } |
1092 | |
1093 | #if CONFIG_MACF |
1094 | void |
1095 | psem_label_associate(struct fileproc *fp, struct vnode *vp, vfs_context_t ctx) |
1096 | { |
1097 | struct psemnode *pnode; |
1098 | struct pseminfo *psem; |
1099 | |
1100 | PSEM_SUBSYS_LOCK(); |
1101 | pnode = (struct psemnode *)fp_get_data(fp); |
1102 | if (pnode != NULL) { |
1103 | psem = pnode->pinfo; |
1104 | if (psem != NULL) { |
1105 | mac_posixsem_vnode_label_associate( |
1106 | cred: vfs_context_ucred(ctx), psem, |
1107 | plabel: mac_posixsem_label(psem), |
1108 | vp, vlabel: mac_vnode_label(vp)); |
1109 | } |
1110 | } |
1111 | PSEM_SUBSYS_UNLOCK(); |
1112 | } |
1113 | #endif |
1114 | |