]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/posix_sem.c
c312d1b84b43781da019ebc06caedd01559fa3ae
[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
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 | CTLFLAG_LOCKED, &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_getuid();
528 pinfo->psem_gid = kauth_getgid();
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 int mode_req = ((mode & FREAD) ? S_IRUSR : 0) |
647 ((mode & FWRITE) ? S_IWUSR : 0);
648
649 /* Otherwise, user id 0 always gets access. */
650 if (!suser(cred, NULL))
651 return (0);
652
653 return(posix_cred_access(cred, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode, mode_req));
654 }
655
656 int
657 sem_unlink(__unused proc_t p, struct sem_unlink_args *uap, __unused int32_t *retval)
658 {
659 size_t i;
660 int error=0;
661 struct psemname nd;
662 struct pseminfo *pinfo;
663 char * pnbuf;
664 char * nameptr;
665 char * cp;
666 size_t pathlen, plen;
667 int incache = 0;
668 struct psemcache *pcache = PSEMCACHE_NULL;
669
670 pinfo = PSEMINFO_NULL;
671
672 MALLOC_ZONE(pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
673 if (pnbuf == NULL) {
674 return(ENOSPC); /* XXX non-standard */
675 }
676 pathlen = MAXPATHLEN;
677 error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen);
678 if (error) {
679 goto bad;
680 }
681 AUDIT_ARG(text, pnbuf);
682 if (pathlen > PSEMNAMLEN) {
683 error = ENAMETOOLONG;
684 goto bad;
685 }
686
687
688 #ifdef PSXSEM_NAME_RESTRICT
689 nameptr = pnbuf;
690 if (*nameptr == '/') {
691 while (*(nameptr++) == '/') {
692 plen--;
693 error = EINVAL;
694 goto bad;
695 }
696 } else {
697 error = EINVAL;
698 goto bad;
699 }
700 #endif /* PSXSEM_NAME_RESTRICT */
701
702 plen = pathlen;
703 nameptr = pnbuf;
704 nd.psem_nameptr = nameptr;
705 nd.psem_namelen = plen;
706 nd. psem_hash =0;
707
708 for (cp = nameptr, i=1; *cp != 0 && i <= plen; i++, cp++) {
709 nd.psem_hash += (unsigned char)*cp * i;
710 }
711
712 PSEM_SUBSYS_LOCK();
713 error = psem_cache_search(&pinfo, &nd, &pcache);
714
715 if (error == ENOENT) {
716 PSEM_SUBSYS_UNLOCK();
717 error = EINVAL;
718 goto bad;
719
720 }
721 if (!error) {
722 PSEM_SUBSYS_UNLOCK();
723 error = EINVAL;
724 goto bad;
725 } else
726 incache = 1;
727 #if CONFIG_MACF
728 error = mac_posixsem_check_unlink(kauth_cred_get(), pinfo, nameptr);
729 if (error) {
730 PSEM_SUBSYS_UNLOCK();
731 goto bad;
732 }
733 #endif
734 if ( (error = psem_access(pinfo, pinfo->psem_mode, kauth_cred_get())) ) {
735 PSEM_SUBSYS_UNLOCK();
736 goto bad;
737 }
738
739 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))==0) {
740 PSEM_SUBSYS_UNLOCK();
741 error = EINVAL;
742 goto bad;
743 }
744
745 if ( (pinfo->psem_flags & PSEM_INDELETE) ) {
746 PSEM_SUBSYS_UNLOCK();
747 error = 0;
748 goto bad;
749 }
750
751 AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid,
752 pinfo->psem_mode);
753
754 pinfo->psem_flags |= PSEM_INDELETE;
755 pinfo->psem_usecount--;
756
757 if (!pinfo->psem_usecount) {
758 psem_delete(pinfo);
759 FREE(pinfo,M_SHM);
760 } else
761 pinfo->psem_flags |= PSEM_REMOVED;
762
763 psem_cache_delete(pcache);
764 PSEM_SUBSYS_UNLOCK();
765 FREE(pcache, M_SHM);
766 error = 0;
767 bad:
768 FREE_ZONE(pnbuf, MAXPATHLEN, M_NAMEI);
769 return (error);
770 }
771
772 int
773 sem_close(proc_t p, struct sem_close_args *uap, __unused int32_t *retval)
774 {
775 int fd = CAST_DOWN_EXPLICIT(int,uap->sem);
776 struct fileproc *fp;
777 int error = 0;
778
779 AUDIT_ARG(fd, fd); /* XXX This seems wrong; uap->sem is a pointer */
780
781 proc_fdlock(p);
782 error = fp_lookup(p,fd, &fp, 1);
783 if (error) {
784 proc_fdunlock(p);
785 return(error);
786 }
787 procfdtbl_markclosefd(p, fd);
788 fileproc_drain(p, fp);
789 fdrelse(p, fd);
790 error = closef_locked(fp, fp->f_fglob, p);
791 FREE_ZONE(fp, sizeof *fp, M_FILEPROC);
792 proc_fdunlock(p);
793 return(error);
794 }
795
796 int
797 sem_wait(proc_t p, struct sem_wait_args *uap, int32_t *retval)
798 {
799 __pthread_testcancel(1);
800 return(sem_wait_nocancel(p, (struct sem_wait_nocancel_args *)uap, retval));
801 }
802
803 int
804 sem_wait_nocancel(proc_t p, struct sem_wait_nocancel_args *uap, __unused int32_t *retval)
805 {
806 int fd = CAST_DOWN_EXPLICIT(int,uap->sem);
807 struct fileproc *fp;
808 struct pseminfo * pinfo;
809 struct psemnode * pnode ;
810 kern_return_t kret;
811 int error;
812
813 error = fp_getfpsem(p, fd, &fp, &pnode);
814 if (error)
815 return (error);
816 if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) {
817 error = EINVAL;
818 goto out;
819 }
820 PSEM_SUBSYS_LOCK();
821 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
822 PSEM_SUBSYS_UNLOCK();
823 error = EINVAL;
824 goto out;
825 }
826 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
827 != PSEM_ALLOCATED) {
828 PSEM_SUBSYS_UNLOCK();
829 error = EINVAL;
830 goto out;
831 }
832 #if CONFIG_MACF
833 error = mac_posixsem_check_wait(kauth_cred_get(), pinfo);
834 if (error) {
835 PSEM_SUBSYS_UNLOCK();
836 goto out;
837 }
838 #endif
839 PSEM_SUBSYS_UNLOCK();
840 kret = semaphore_wait(pinfo->psem_semobject);
841 switch (kret) {
842 case KERN_INVALID_ADDRESS:
843 case KERN_PROTECTION_FAILURE:
844 error = EACCES;
845 break;
846 case KERN_ABORTED:
847 case KERN_OPERATION_TIMED_OUT:
848 error = EINTR;
849 break;
850 case KERN_SUCCESS:
851 error = 0;
852 break;
853 default:
854 error = EINVAL;
855 break;
856 }
857 out:
858 fp_drop(p, fd, fp, 0);
859 return(error);
860
861 }
862
863 int
864 sem_trywait(proc_t p, struct sem_trywait_args *uap, __unused int32_t *retval)
865 {
866 int fd = CAST_DOWN_EXPLICIT(int,uap->sem);
867 struct fileproc *fp;
868 struct pseminfo * pinfo;
869 struct psemnode * pnode ;
870 kern_return_t kret;
871 mach_timespec_t wait_time;
872 int error;
873
874 error = fp_getfpsem(p, fd, &fp, &pnode);
875 if (error)
876 return (error);
877 if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) {
878 error = EINVAL;
879 goto out;
880 }
881 PSEM_SUBSYS_LOCK();
882 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
883 PSEM_SUBSYS_UNLOCK();
884 error = EINVAL;
885 goto out;
886 }
887 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
888 != PSEM_ALLOCATED) {
889 PSEM_SUBSYS_UNLOCK();
890 error = EINVAL;
891 goto out;
892 }
893 #if CONFIG_MACF
894 error = mac_posixsem_check_wait(kauth_cred_get(), pinfo);
895 if (error) {
896 PSEM_SUBSYS_UNLOCK();
897 goto out;
898 }
899 #endif
900 PSEM_SUBSYS_UNLOCK();
901 wait_time.tv_sec = 0;
902 wait_time.tv_nsec = 0;
903
904 kret = semaphore_timedwait(pinfo->psem_semobject, MACH_TIMESPEC_ZERO);
905 switch (kret) {
906 case KERN_INVALID_ADDRESS:
907 case KERN_PROTECTION_FAILURE:
908 error = EINVAL;
909 break;
910 case KERN_ABORTED:
911 error = EINTR;
912 break;
913 case KERN_OPERATION_TIMED_OUT:
914 error = EAGAIN;
915 break;
916 case KERN_SUCCESS:
917 error = 0;
918 break;
919 default:
920 error = EINVAL;
921 break;
922 }
923 out:
924 fp_drop(p, fd, fp, 0);
925 return(error);
926 }
927
928 int
929 sem_post(proc_t p, struct sem_post_args *uap, __unused int32_t *retval)
930 {
931 int fd = CAST_DOWN_EXPLICIT(int,uap->sem);
932 struct fileproc *fp;
933 struct pseminfo * pinfo;
934 struct psemnode * pnode ;
935 kern_return_t kret;
936 int error;
937
938 error = fp_getfpsem(p, fd, &fp, &pnode);
939 if (error)
940 return (error);
941 if (((pnode = (struct psemnode *)fp->f_data)) == PSEMNODE_NULL ) {
942 error = EINVAL;
943 goto out;
944 }
945 PSEM_SUBSYS_LOCK();
946 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
947 PSEM_SUBSYS_UNLOCK();
948 error = EINVAL;
949 goto out;
950 }
951 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
952 != PSEM_ALLOCATED) {
953 PSEM_SUBSYS_UNLOCK();
954 error = EINVAL;
955 goto out;
956 }
957 #if CONFIG_MACF
958 error = mac_posixsem_check_post(kauth_cred_get(), pinfo);
959 if (error) {
960 PSEM_SUBSYS_UNLOCK();
961 goto out;
962 }
963 #endif
964 PSEM_SUBSYS_UNLOCK();
965 kret = semaphore_signal(pinfo->psem_semobject);
966 switch (kret) {
967 case KERN_INVALID_ADDRESS:
968 case KERN_PROTECTION_FAILURE:
969 error = EINVAL;
970 break;
971 case KERN_ABORTED:
972 case KERN_OPERATION_TIMED_OUT:
973 error = EINTR;
974 break;
975 case KERN_SUCCESS:
976 error = 0;
977 break;
978 default:
979 error = EINVAL;
980 break;
981 }
982 out:
983 fp_drop(p, fd, fp, 0);
984 return(error);
985 }
986
987 int
988 sem_init(__unused proc_t p, __unused struct sem_init_args *uap, __unused int32_t *retval)
989 {
990 return(ENOSYS);
991 }
992
993 int
994 sem_destroy(__unused proc_t p, __unused struct sem_destroy_args *uap, __unused int32_t *retval)
995 {
996 return(ENOSYS);
997 }
998
999 int
1000 sem_getvalue(__unused proc_t p, __unused struct sem_getvalue_args *uap, __unused int32_t *retval)
1001 {
1002 return(ENOSYS);
1003 }
1004
1005 static int
1006 psem_close(struct psemnode *pnode, __unused int flags)
1007 {
1008 int error=0;
1009 struct pseminfo *pinfo;
1010
1011 PSEM_SUBSYS_LOCK();
1012 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL){
1013 PSEM_SUBSYS_UNLOCK();
1014 return(EINVAL);
1015 }
1016
1017 if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) {
1018 PSEM_SUBSYS_UNLOCK();
1019 return(EINVAL);
1020 }
1021 #if DIAGNOSTIC
1022 if(!pinfo->psem_usecount) {
1023 kprintf("negative usecount in psem_close\n");
1024 }
1025 #endif /* DIAGNOSTIC */
1026 pinfo->psem_usecount--;
1027
1028 if ((pinfo->psem_flags & PSEM_REMOVED) && !pinfo->psem_usecount) {
1029 PSEM_SUBSYS_UNLOCK();
1030 /* lock dropped as only semaphore is destroyed here */
1031 error = psem_delete(pinfo);
1032 FREE(pinfo,M_SHM);
1033 } else {
1034 PSEM_SUBSYS_UNLOCK();
1035 }
1036 /* subsystem lock is dropped when we get here */
1037 FREE(pnode, M_SHM);
1038 return (error);
1039 }
1040
1041 static int
1042 psem_closefile(struct fileglob *fg, __unused vfs_context_t ctx)
1043 {
1044 int error;
1045
1046 /*
1047 * Not locked as psem_close is called only from here and is locked
1048 * properly
1049 */
1050 error = psem_close(((struct psemnode *)fg->fg_data), fg->fg_flag);
1051
1052 return(error);
1053 }
1054
1055 static int
1056 psem_delete(struct pseminfo * pinfo)
1057 {
1058 kern_return_t kret;
1059
1060 kret = semaphore_destroy(kernel_task, pinfo->psem_semobject);
1061 #if CONFIG_MACF
1062 mac_posixsem_label_destroy(pinfo);
1063 #endif
1064
1065 switch (kret) {
1066 case KERN_INVALID_ADDRESS:
1067 case KERN_PROTECTION_FAILURE:
1068 return (EINVAL);
1069 case KERN_ABORTED:
1070 case KERN_OPERATION_TIMED_OUT:
1071 return (EINTR);
1072 case KERN_SUCCESS:
1073 return(0);
1074 default:
1075 return (EINVAL);
1076 }
1077 }
1078
1079 static int
1080 psem_read(__unused struct fileproc *fp, __unused struct uio *uio,
1081 __unused int flags, __unused vfs_context_t ctx)
1082 {
1083 return(ENOTSUP);
1084 }
1085
1086 static int
1087 psem_write(__unused struct fileproc *fp, __unused struct uio *uio,
1088 __unused int flags, __unused vfs_context_t ctx)
1089 {
1090 return(ENOTSUP);
1091 }
1092
1093 static int
1094 psem_ioctl(__unused struct fileproc *fp, __unused u_long com,
1095 __unused caddr_t data, __unused vfs_context_t ctx)
1096 {
1097 return(ENOTSUP);
1098 }
1099
1100 static int
1101 psem_select(__unused struct fileproc *fp, __unused int which,
1102 __unused void *wql, __unused vfs_context_t ctx)
1103 {
1104 return(ENOTSUP);
1105 }
1106
1107 static int
1108 psem_kqfilter(__unused struct fileproc *fp, __unused struct knote *kn,
1109 __unused vfs_context_t ctx)
1110 {
1111 return (ENOTSUP);
1112 }
1113
1114 int
1115 fill_pseminfo(struct psemnode *pnode, struct psem_info * info)
1116 {
1117 struct pseminfo *pinfo;
1118 struct vinfo_stat *sb;
1119
1120 PSEM_SUBSYS_LOCK();
1121 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL){
1122 PSEM_SUBSYS_UNLOCK();
1123 return(EINVAL);
1124 }
1125
1126 #if 0
1127 if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) {
1128 PSEM_SUBSYS_UNLOCK();
1129 return(EINVAL);
1130 }
1131 #endif
1132
1133 sb = &info->psem_stat;
1134 bzero(sb, sizeof(struct vinfo_stat));
1135
1136 sb->vst_mode = pinfo->psem_mode;
1137 sb->vst_uid = pinfo->psem_uid;
1138 sb->vst_gid = pinfo->psem_gid;
1139 sb->vst_size = pinfo->psem_usecount;
1140 bcopy(&pinfo->psem_name[0], &info->psem_name[0], PSEMNAMLEN+1);
1141
1142 PSEM_SUBSYS_UNLOCK();
1143 return(0);
1144 }
1145
1146 #if CONFIG_MACF
1147 void
1148 psem_label_associate(struct fileproc *fp, struct vnode *vp, vfs_context_t ctx)
1149 {
1150 struct psemnode *pnode;
1151 struct pseminfo *psem;
1152
1153 PSEM_SUBSYS_LOCK();
1154 pnode = (struct psemnode *)fp->f_fglob->fg_data;
1155 if (pnode != NULL) {
1156 psem = pnode->pinfo;
1157 if (psem != NULL)
1158 mac_posixsem_vnode_label_associate(
1159 vfs_context_ucred(ctx), psem, psem->psem_label,
1160 vp, vp->v_label);
1161 }
1162 PSEM_SUBSYS_UNLOCK();
1163 }
1164 #endif
1165