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