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