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