]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/posix_sem.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / kern / posix_sem.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
30 * All Rights Reserved.
31 */
32/*
6d2010ae 33 * posix_sem.c : Support for POSIX semaphore APIs
1c79356b
A
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 */
2d21ac55
A
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 */
1c79356b
A
49
50#include <sys/cdefs.h>
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/kernel.h>
91447636 54#include <sys/file_internal.h>
1c79356b
A
55#include <sys/filedesc.h>
56#include <sys/stat.h>
91447636
A
57#include <sys/proc_internal.h>
58#include <sys/kauth.h>
1c79356b
A
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>
91447636 66#include <sys/sysproto.h>
0c530ab8 67#include <sys/proc_info.h>
e5568f75 68
2d21ac55
A
69#if CONFIG_MACF
70#include <sys/vnode_internal.h>
71#include <security/mac_framework.h>
72#endif
73
b0d623f7 74#include <security/audit/audit.h>
e5568f75 75
1c79356b
A
76#include <mach/mach_types.h>
77#include <mach/vm_prot.h>
78#include <mach/semaphore.h>
79#include <mach/sync_policy.h>
91447636
A
80#include <mach/task.h>
81#include <kern/kern_types.h>
1c79356b
A
82#include <kern/task.h>
83#include <kern/clock.h>
84#include <mach/kern_return.h>
85
f427ee49
A
86#define f_flag fp_glob->fg_flag
87#define f_ops fp_glob->fg_ops
88#define f_data fp_glob->fg_data
91447636 89
0a7de745 90#define PSEMNAMLEN 31 /* maximum name segment length we bother with */
1c79356b
A
91
92struct pseminfo {
0a7de745
A
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;
1c79356b
A
103};
104#define PSEMINFO_NULL (struct pseminfo *)0
105
0a7de745
A
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 */
f427ee49 118 size_t psem_nlen; /* length of name */
0a7de745 119 char psem_name[PSEMNAMLEN + 1]; /* segment name */
1c79356b
A
120};
121#define PSEMCACHE_NULL (struct psemcache *)0
122
490019cf
A
123#define PSEMCACHE_NOTFOUND (0)
124#define PSEMCACHE_FOUND (-1)
125#define PSEMCACHE_NEGATIVE (ENOENT)
126
0a7de745
A
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 */
1c79356b
A
134};
135
136struct psemname {
0a7de745 137 char *psem_nameptr; /* pointer to looked up name */
f427ee49 138 size_t psem_namelen; /* length of looked up component */
0a7de745 139 u_int32_t psem_hash; /* hash value of looked up name */
1c79356b
A
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])
0a7de745
A
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 */
6d2010ae
A
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");
0a7de745 161SYSCTL_LONG(_kern_posix_sem, OID_AUTO, max, CTLFLAG_RW | CTLFLAG_LOCKED, &posix_sem_max, "max");
91447636 162
0a7de745 163struct psemstats psemstats; /* cache effectiveness statistics */
1c79356b 164
f427ee49 165static int psem_access(struct pseminfo *pinfo, mode_t mode, kauth_cred_t cred);
91447636 166static int psem_cache_search(struct pseminfo **,
0a7de745 167 struct psemname *, struct psemcache **);
91447636 168static int psem_delete(struct pseminfo * pinfo);
1c79356b 169
0a7de745 170static int psem_closefile(struct fileglob *fp, vfs_context_t ctx);
490019cf 171static int psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache);
1c79356b 172
39236c6e 173static const struct fileops psemops = {
cb323159
A
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,
39236c6e 182};
91447636
A
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)
39037602 191#define PSEM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_sem_subsys_mutex, LCK_MTX_ASSERT_OWNED)
91447636
A
192
193
194static int psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp);
490019cf
A
195static void psem_cache_delete(struct psemcache *pcp);
196int psem_cache_purge_all(proc_t);
197
198
91447636
A
199/* Initialize the mutex governing access to the posix sem subsystem */
200__private_extern__ void
201psem_lock_init( void )
202{
0a7de745 203 psx_sem_subsys_lck_grp_attr = lck_grp_attr_alloc_init();
91447636 204
0a7de745 205 psx_sem_subsys_lck_grp = lck_grp_alloc_init("posix shared memory", psx_sem_subsys_lck_grp_attr);
91447636 206
0a7de745
A
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);
91447636 209}
1c79356b
A
210
211/*
0a7de745
A
212 * Lookup an entry in the cache
213 *
214 *
1c79356b
A
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
9bccf70c 221static int
2d21ac55 222psem_cache_search(struct pseminfo **psemp, struct psemname *pnp,
0a7de745 223 struct psemcache **pcache)
1c79356b 224{
91447636
A
225 struct psemcache *pcp, *nnp;
226 struct psemhashhead *pcpp;
1c79356b
A
227
228 if (pnp->psem_namelen > PSEMNAMLEN) {
229 psemstats.longnames++;
490019cf 230 return PSEMCACHE_NOTFOUND;
1c79356b
A
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 &&
f427ee49 237 !bcmp(pcp->psem_name, pnp->psem_nameptr, pcp->psem_nlen)) {
1c79356b 238 break;
0a7de745 239 }
1c79356b
A
240 }
241
242 if (pcp == 0) {
243 psemstats.miss++;
490019cf 244 return PSEMCACHE_NOTFOUND;
1c79356b
A
245 }
246
247 /* We found a "positive" match, return the vnode */
0a7de745 248 if (pcp->pseminfo) {
1c79356b
A
249 psemstats.goodhits++;
250 /* TOUCH(ncp); */
251 *psemp = pcp->pseminfo;
252 *pcache = pcp;
490019cf 253 return PSEMCACHE_FOUND;
1c79356b
A
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++;
490019cf 261 return PSEMCACHE_NEGATIVE;
1c79356b
A
262}
263
264/*
265 * Add an entry to the cache.
266 */
9bccf70c 267static int
91447636 268psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp)
1c79356b 269{
91447636 270 struct psemhashhead *pcpp;
1c79356b
A
271 struct pseminfo *dpinfo;
272 struct psemcache *dpcp;
273
274#if DIAGNOSTIC
0a7de745 275 if (pnp->psem_namelen > PSEMNAMLEN) {
1c79356b 276 panic("cache_enter: name too long");
0a7de745 277 }
1c79356b
A
278#endif
279
91447636 280
1c79356b 281 /* if the entry has already been added by some one else return */
490019cf
A
282 if (psem_cache_search(&dpinfo, pnp, &dpcp) == PSEMCACHE_FOUND) {
283 return EEXIST;
1c79356b 284 }
0a7de745 285 if (psemnument >= posix_sem_max) {
490019cf 286 return ENOSPC;
0a7de745 287 }
1c79356b 288 psemnument++;
1c79356b
A
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;
f427ee49 297 bcopy(pnp->psem_nameptr, pcp->psem_name, pcp->psem_nlen);
1c79356b
A
298 pcpp = PSEMHASH(pnp);
299#if DIAGNOSTIC
300 {
91447636 301 struct psemcache *p;
1c79356b 302
0a7de745
A
303 for (p = pcpp->lh_first; p != 0; p = p->psem_hash.le_next) {
304 if (p == pcp) {
1c79356b 305 panic("psem:cache_enter duplicate");
0a7de745
A
306 }
307 }
1c79356b
A
308 }
309#endif
310 LIST_INSERT_HEAD(pcpp, pcp, psem_hash);
490019cf 311 return 0;
1c79356b
A
312}
313
314/*
315 * Name cache initialization, from vfs_init() when we are booting
316 */
317void
91447636 318psem_cache_init(void)
1c79356b 319{
f427ee49 320 psemhashtbl = hashinit((int)(posix_sem_max / 2), M_SHM, &psemhash);
1c79356b
A
321}
322
9bccf70c 323static void
91447636 324psem_cache_delete(struct psemcache *pcp)
9bccf70c
A
325{
326#if DIAGNOSTIC
0a7de745 327 if (pcp->psem_hash.le_prev == 0) {
9bccf70c 328 panic("psem namecache purge le_prev");
0a7de745
A
329 }
330 if (pcp->psem_hash.le_next == pcp) {
9bccf70c 331 panic("namecache purge le_next");
0a7de745 332 }
9bccf70c
A
333#endif /* DIAGNOSTIC */
334 LIST_REMOVE(pcp, psem_hash);
0a7de745 335 pcp->psem_hash.le_prev = NULL;
9bccf70c
A
336 psemnument--;
337}
338
1c79356b 339/*
490019cf
A
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.
1c79356b 343 */
490019cf
A
344int
345psem_cache_purge_all(__unused proc_t p)
1c79356b 346{
490019cf 347 struct psemcache *pcp, *tmppcp;
1c79356b 348 struct psemhashhead *pcpp;
490019cf 349 int error = 0;
1c79356b 350
0a7de745 351 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
490019cf 352 return EPERM;
0a7de745 353 }
490019cf
A
354
355 PSEM_SUBSYS_LOCK();
1c79356b 356 for (pcpp = &psemhashtbl[psemhash]; pcpp >= psemhashtbl; pcpp--) {
490019cf
A
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);
0a7de745 363 if (error) {
490019cf 364 goto out;
0a7de745 365 }
490019cf 366 }
1c79356b 367 }
490019cf
A
368 assert(psemnument == 0);
369
370out:
371 PSEM_SUBSYS_UNLOCK();
372
0a7de745 373 if (error) {
490019cf 374 printf("%s: Error %d removing all semaphores: %ld remain!\n",
0a7de745
A
375 __func__, error, psemnument);
376 }
490019cf 377 return error;
1c79356b 378}
1c79356b 379
f427ee49
A
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 */
1c79356b 390int
2d21ac55 391sem_open(proc_t p, struct sem_open_args *uap, user_addr_t *retval)
1c79356b 392{
91447636 393 size_t i;
91447636 394 int indx, error;
1c79356b
A
395 struct psemname nd;
396 struct pseminfo *pinfo;
b0d623f7
A
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;
1c79356b
A
402 char * nameptr;
403 char * cp;
404 size_t pathlen, plen;
f427ee49
A
405 mode_t fmode;
406 mode_t cmode = (mode_t)uap->mode;
1c79356b
A
407 int value = uap->value;
408 int incache = 0;
b0d623f7 409 struct psemcache *pcp = PSEMCACHE_NULL;
0a7de745
A
410 kern_return_t kret = KERN_INVALID_ADDRESS; /* default fail */
411
e5568f75 412 AUDIT_ARG(fflags, uap->oflag);
f427ee49 413 AUDIT_ARG(mode, (mode_t)uap->mode);
b0d623f7 414 AUDIT_ARG(value32, uap->value);
91447636 415
1c79356b
A
416 pinfo = PSEMINFO_NULL;
417
b0d623f7
A
418 /*
419 * Preallocate everything we might need up front to avoid taking
420 * and dropping the lock, opening us up to race conditions.
421 */
f427ee49 422 pnbuf = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO);
91447636 423
1c79356b 424 pathlen = MAXPATHLEN;
91447636 425 error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen);
1c79356b
A
426 if (error) {
427 goto bad;
428 }
e5568f75 429 AUDIT_ARG(text, pnbuf);
0a7de745 430 if ((pathlen > PSEMNAMLEN)) {
1c79356b
A
431 error = ENAMETOOLONG;
432 goto bad;
433 }
434
1c79356b
A
435#ifdef PSXSEM_NAME_RESTRICT
436 nameptr = pnbuf;
437 if (*nameptr == '/') {
438 while (*(nameptr++) == '/') {
439 plen--;
440 error = EINVAL;
441 goto bad;
442 }
0a7de745 443 } else {
1c79356b
A
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;
2d21ac55 453 nd.psem_hash = 0;
1c79356b 454
0a7de745
A
455 for (cp = nameptr, i = 1; *cp != 0 && i <= plen; i++, cp++) {
456 nd.psem_hash += (unsigned char)*cp * i;
1c79356b
A
457 }
458
b0d623f7
A
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());
0a7de745 464 if (error) {
b0d623f7 465 goto bad;
0a7de745 466 }
b0d623f7
A
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 */
0a7de745 473 MALLOC(pcp, struct psemcache *, sizeof(struct psemcache), M_SHM, M_WAITOK | M_ZERO);
b0d623f7
A
474 if (pcp == PSEMCACHE_NULL) {
475 error = ENOMEM;
476 goto bad;
477 }
478
0a7de745 479 MALLOC(new_pinfo, struct pseminfo *, sizeof(struct pseminfo), M_SHM, M_WAITOK | M_ZERO);
b0d623f7
A
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 */
0a7de745 494
f427ee49 495 fmode = (mode_t)FFLAGS(uap->oflag);
0a7de745
A
496
497 if ((fmode & O_CREAT)) {
498 if ((value < 0) || (value > SEM_VALUE_MAX)) {
b0d623f7
A
499 error = EINVAL;
500 goto bad;
501 }
0a7de745 502
b0d623f7 503 kret = semaphore_create(kernel_task, &new_pinfo->psem_semobject, SYNC_POLICY_FIFO, value);
0a7de745 504
b0d623f7
A
505 if (kret != KERN_SUCCESS) {
506 switch (kret) {
0a7de745
A
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;
b0d623f7
A
515 }
516 goto bad;
517 }
518 }
0a7de745
A
519
520 MALLOC(new_pnode, struct psemnode *, sizeof(struct psemnode), M_SHM, M_WAITOK | M_ZERO);
b0d623f7
A
521 if (new_pnode == NULL) {
522 error = ENOSPC;
523 goto bad;
524 }
525
91447636 526 PSEM_SUBSYS_LOCK();
1c79356b
A
527 error = psem_cache_search(&pinfo, &nd, &pcache);
528
490019cf 529 if (error == PSEMCACHE_NEGATIVE) {
1c79356b 530 error = EINVAL;
b0d623f7 531 goto bad_locked;
1c79356b 532 }
490019cf 533
0a7de745 534 if (error == PSEMCACHE_FOUND) {
1c79356b 535 incache = 1;
0a7de745 536 } else {
490019cf 537 incache = 0;
0a7de745 538 }
1c79356b 539
1c79356b
A
540 cmode &= ALLPERMS;
541
0a7de745 542 if (((fmode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) && incache) {
1c79356b
A
543 /* sem exists and opened O_EXCL */
544#if notyet
545 if (pinfo->psem_flags & PSEM_INDELETE) {
546 }
0a7de745 547#endif
e5568f75 548 AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid,
0a7de745 549 pinfo->psem_gid, pinfo->psem_mode);
1c79356b 550 error = EEXIST;
b0d623f7 551 goto bad_locked;
1c79356b 552 }
0a7de745 553 if (((fmode & (O_CREAT | O_EXCL)) == O_CREAT) && incache) {
1c79356b
A
554 /* As per POSIX, O_CREAT has no effect */
555 fmode &= ~O_CREAT;
556 }
557
0a7de745 558 if ((fmode & O_CREAT)) {
b0d623f7
A
559 /* create a new one (commit the allocation) */
560 pinfo = new_pinfo;
1c79356b
A
561 pinfo->psem_flags = PSEM_DEFINED | PSEM_INCREATE;
562 pinfo->psem_usecount = 1;
563 pinfo->psem_mode = cmode;
6d2010ae
A
564 pinfo->psem_uid = kauth_getuid();
565 pinfo->psem_gid = kauth_getgid();
0c530ab8 566 bcopy(pnbuf, &pinfo->psem_name[0], PSEMNAMLEN);
0a7de745 567 pinfo->psem_name[PSEMNAMLEN] = 0;
1c79356b
A
568 pinfo->psem_flags &= ~PSEM_DEFINED;
569 pinfo->psem_flags |= PSEM_ALLOCATED;
39236c6e
A
570 pinfo->psem_creator_pid = p->p_pid;
571 pinfo->psem_creator_uniqueid = p->p_uniqueid;
0a7de745 572
2d21ac55
A
573#if CONFIG_MACF
574 error = mac_posixsem_check_create(kauth_cred_get(), nameptr);
575 if (error) {
b0d623f7 576 goto bad_locked;
2d21ac55
A
577 }
578 mac_posixsem_label_associate(kauth_cred_get(), pinfo, nameptr);
579#endif
1c79356b
A
580 } else {
581 /* semaphore should exist as it is without O_CREAT */
582 if (!incache) {
583 error = ENOENT;
b0d623f7 584 goto bad_locked;
1c79356b 585 }
0a7de745 586 if (pinfo->psem_flags & PSEM_INDELETE) {
1c79356b 587 error = ENOENT;
b0d623f7 588 goto bad_locked;
0a7de745 589 }
e5568f75 590 AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid,
0a7de745 591 pinfo->psem_gid, pinfo->psem_mode);
2d21ac55
A
592#if CONFIG_MACF
593 error = mac_posixsem_check_open(kauth_cred_get(), pinfo);
594 if (error) {
b0d623f7 595 goto bad_locked;
2d21ac55
A
596 }
597#endif
0a7de745 598 if ((error = psem_access(pinfo, fmode, kauth_cred_get()))) {
b0d623f7 599 goto bad_locked;
91447636 600 }
1c79356b 601 }
1c79356b
A
602
603 if (!incache) {
b0d623f7 604 /* if successful, this will consume the pcp */
0a7de745 605 if ((error = psem_cache_add(pinfo, &nd, pcp))) {
b0d623f7 606 goto bad_locked;
1c79356b
A
607 }
608 }
609 pinfo->psem_flags &= ~PSEM_INCREATE;
610 pinfo->psem_usecount++;
b0d623f7 611 new_pnode->pinfo = pinfo;
91447636
A
612 PSEM_SUBSYS_UNLOCK();
613
b0d623f7
A
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
91447636 632 proc_fdlock(p);
55e303ae 633 fp->f_flag = fmode & FMASK;
1c79356b 634 fp->f_ops = &psemops;
b0d623f7 635 fp->f_data = (caddr_t)new_pnode;
6601e61a 636 procfdtbl_releasefd(p, indx, NULL);
91447636
A
637 fp_drop(p, indx, fp, 1);
638 proc_fdunlock(p);
639
640 *retval = CAST_USER_ADDR_T(indx);
f427ee49 641 zfree(ZV_NAMEI, pnbuf);
0a7de745 642 return 0;
1c79356b 643
b0d623f7
A
644bad_locked:
645 PSEM_SUBSYS_UNLOCK();
646bad:
0a7de745 647 if (pcp != PSEMCACHE_NULL) {
b0d623f7 648 FREE(pcp, M_SHM);
0a7de745 649 }
b0d623f7 650
0a7de745 651 if (new_pnode != PSEMNODE_NULL) {
b0d623f7 652 FREE(new_pnode, M_SHM);
0a7de745 653 }
b0d623f7 654
0a7de745 655 if (fp != NULL) {
b0d623f7 656 fp_free(p, indx, fp);
0a7de745 657 }
b0d623f7
A
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 }
2d21ac55 669#if CONFIG_MACF
b0d623f7 670 mac_posixsem_label_destroy(new_pinfo);
2d21ac55 671#endif
b0d623f7 672 FREE(new_pinfo, M_SHM);
2d21ac55 673 }
b0d623f7 674
0a7de745 675 if (pnbuf != NULL) {
f427ee49 676 zfree(ZV_NAMEI, pnbuf);
0a7de745
A
677 }
678 return error;
1c79356b
A
679}
680
91447636
A
681/*
682 * XXX This code is repeated in several places
683 */
684static int
f427ee49 685psem_access(struct pseminfo *pinfo, mode_t mode, kauth_cred_t cred)
1c79356b 686{
f427ee49 687 mode_t mode_req = ((mode & FREAD) ? S_IRUSR : 0) |
0a7de745 688 ((mode & FWRITE) ? S_IWUSR : 0);
1c79356b
A
689
690 /* Otherwise, user id 0 always gets access. */
0a7de745
A
691 if (!suser(cred, NULL)) {
692 return 0;
693 }
1c79356b 694
0a7de745 695 return posix_cred_access(cred, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode, mode_req);
1c79356b
A
696}
697
490019cf
A
698static int
699psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache)
700{
701 PSEM_SUBSYS_ASSERT_HELD();
702
0a7de745 703 if (!pinfo || !pcache) {
490019cf 704 return EINVAL;
0a7de745 705 }
490019cf 706
0a7de745 707 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) == 0) {
490019cf 708 return EINVAL;
0a7de745 709 }
490019cf 710
0a7de745 711 if (pinfo->psem_flags & PSEM_INDELETE) {
490019cf 712 return 0;
0a7de745 713 }
490019cf
A
714
715 AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid,
0a7de745 716 pinfo->psem_mode);
490019cf
A
717
718 pinfo->psem_flags |= PSEM_INDELETE;
719 pinfo->psem_usecount--;
720
721 if (!pinfo->psem_usecount) {
722 psem_delete(pinfo);
0a7de745 723 FREE(pinfo, M_SHM);
490019cf
A
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
1c79356b 734int
b0d623f7 735sem_unlink(__unused proc_t p, struct sem_unlink_args *uap, __unused int32_t *retval)
1c79356b 736{
91447636 737 size_t i;
0a7de745 738 int error = 0;
1c79356b
A
739 struct psemname nd;
740 struct pseminfo *pinfo;
1c79356b
A
741 char * nameptr;
742 char * cp;
490019cf
A
743 char * pnbuf;
744 size_t pathlen;
1c79356b 745 struct psemcache *pcache = PSEMCACHE_NULL;
1c79356b
A
746
747 pinfo = PSEMINFO_NULL;
748
f427ee49
A
749 pnbuf = zalloc(ZV_NAMEI);
750
1c79356b 751 pathlen = MAXPATHLEN;
91447636 752 error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen);
1c79356b
A
753 if (error) {
754 goto bad;
755 }
e5568f75 756 AUDIT_ARG(text, pnbuf);
1c79356b
A
757 if (pathlen > PSEMNAMLEN) {
758 error = ENAMETOOLONG;
759 goto bad;
760 }
761
490019cf 762 nameptr = pnbuf;
1c79356b
A
763
764#ifdef PSXSEM_NAME_RESTRICT
1c79356b
A
765 if (*nameptr == '/') {
766 while (*(nameptr++) == '/') {
490019cf 767 pathlen--;
1c79356b
A
768 error = EINVAL;
769 goto bad;
770 }
0a7de745 771 } else {
1c79356b
A
772 error = EINVAL;
773 goto bad;
774 }
775#endif /* PSXSEM_NAME_RESTRICT */
776
1c79356b 777 nd.psem_nameptr = nameptr;
490019cf 778 nd.psem_namelen = pathlen;
0a7de745 779 nd.psem_hash = 0;
1c79356b 780
0a7de745
A
781 for (cp = nameptr, i = 1; *cp != 0 && i <= pathlen; i++, cp++) {
782 nd.psem_hash += (unsigned char)*cp * i;
1c79356b
A
783 }
784
91447636 785 PSEM_SUBSYS_LOCK();
1c79356b
A
786 error = psem_cache_search(&pinfo, &nd, &pcache);
787
490019cf 788 if (error != PSEMCACHE_FOUND) {
91447636 789 PSEM_SUBSYS_UNLOCK();
cb323159 790 error = ENOENT;
1c79356b 791 goto bad;
1c79356b 792 }
490019cf 793
2d21ac55
A
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
0a7de745 801 if ((error = psem_access(pinfo, pinfo->psem_mode, kauth_cred_get()))) {
91447636 802 PSEM_SUBSYS_UNLOCK();
1c79356b 803 goto bad;
91447636 804 }
1c79356b 805
490019cf 806 error = psem_unlink_internal(pinfo, pcache);
91447636 807 PSEM_SUBSYS_UNLOCK();
490019cf 808
1c79356b 809bad:
f427ee49 810 zfree(ZV_NAMEI, pnbuf);
490019cf 811 return error;
1c79356b
A
812}
813
1c79356b 814int
b0d623f7 815sem_close(proc_t p, struct sem_close_args *uap, __unused int32_t *retval)
1c79356b 816{
0a7de745 817 int fd = CAST_DOWN_EXPLICIT(int, uap->sem);
91447636 818 struct fileproc *fp;
1c79356b 819
e5568f75 820 AUDIT_ARG(fd, fd); /* XXX This seems wrong; uap->sem is a pointer */
91447636
A
821
822 proc_fdlock(p);
f427ee49 823 if ((fp = fp_get_noref_locked(p, fd)) == NULL) {
91447636 824 proc_fdunlock(p);
f427ee49 825 return EBADF;
91447636 826 }
f427ee49 827 if (FILEGLOB_DTYPE(fp->fp_glob) != DTYPE_PSXSEM) {
e8c3f781 828 proc_fdunlock(p);
0a7de745 829 return EBADF;
e8c3f781 830 }
f427ee49 831 return fp_close_and_unlock(p, fd, fp, 0);
1c79356b
A
832}
833
1c79356b 834int
b0d623f7 835sem_wait(proc_t p, struct sem_wait_args *uap, int32_t *retval)
2d21ac55
A
836{
837 __pthread_testcancel(1);
0a7de745 838 return sem_wait_nocancel(p, (struct sem_wait_nocancel_args *)uap, retval);
2d21ac55
A
839}
840
841int
b0d623f7 842sem_wait_nocancel(proc_t p, struct sem_wait_nocancel_args *uap, __unused int32_t *retval)
1c79356b 843{
0a7de745 844 int fd = CAST_DOWN_EXPLICIT(int, uap->sem);
91447636 845 struct fileproc *fp;
1c79356b 846 struct pseminfo * pinfo;
0a7de745 847 struct psemnode * pnode;
1c79356b
A
848 kern_return_t kret;
849 int error;
850
f427ee49 851 error = fp_get_ftype(p, fd, DTYPE_PSXSEM, EBADF, &fp);
0a7de745
A
852 if (error) {
853 return error;
854 }
f427ee49
A
855 pnode = (struct psemnode *)fp->f_data;
856
91447636
A
857 PSEM_SUBSYS_LOCK();
858 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
859 PSEM_SUBSYS_UNLOCK();
860 error = EINVAL;
861 goto out;
862 }
0a7de745
A
863 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
864 != PSEM_ALLOCATED) {
91447636
A
865 PSEM_SUBSYS_UNLOCK();
866 error = EINVAL;
867 goto out;
1c79356b 868 }
2d21ac55
A
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
91447636 876 PSEM_SUBSYS_UNLOCK();
1c79356b
A
877 kret = semaphore_wait(pinfo->psem_semobject);
878 switch (kret) {
879 case KERN_INVALID_ADDRESS:
880 case KERN_PROTECTION_FAILURE:
91447636
A
881 error = EACCES;
882 break;
1c79356b
A
883 case KERN_ABORTED:
884 case KERN_OPERATION_TIMED_OUT:
91447636
A
885 error = EINTR;
886 break;
1c79356b 887 case KERN_SUCCESS:
91447636
A
888 error = 0;
889 break;
1c79356b 890 default:
91447636
A
891 error = EINVAL;
892 break;
1c79356b 893 }
91447636
A
894out:
895 fp_drop(p, fd, fp, 0);
0a7de745 896 return error;
91447636 897}
1c79356b
A
898
899int
b0d623f7 900sem_trywait(proc_t p, struct sem_trywait_args *uap, __unused int32_t *retval)
1c79356b 901{
0a7de745 902 int fd = CAST_DOWN_EXPLICIT(int, uap->sem);
91447636 903 struct fileproc *fp;
1c79356b 904 struct pseminfo * pinfo;
0a7de745 905 struct psemnode * pnode;
1c79356b
A
906 kern_return_t kret;
907 mach_timespec_t wait_time;
908 int error;
0a7de745 909
f427ee49 910 error = fp_get_ftype(p, fd, DTYPE_PSXSEM, EBADF, &fp);
0a7de745
A
911 if (error) {
912 return error;
913 }
f427ee49
A
914 pnode = (struct psemnode *)fp->f_data;
915
91447636
A
916 PSEM_SUBSYS_LOCK();
917 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
918 PSEM_SUBSYS_UNLOCK();
919 error = EINVAL;
920 goto out;
921 }
0a7de745
A
922 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
923 != PSEM_ALLOCATED) {
91447636
A
924 PSEM_SUBSYS_UNLOCK();
925 error = EINVAL;
926 goto out;
1c79356b 927 }
2d21ac55
A
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
91447636 935 PSEM_SUBSYS_UNLOCK();
1c79356b
A
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:
91447636
A
943 error = EINVAL;
944 break;
1c79356b 945 case KERN_ABORTED:
91447636
A
946 error = EINTR;
947 break;
1c79356b 948 case KERN_OPERATION_TIMED_OUT:
91447636
A
949 error = EAGAIN;
950 break;
1c79356b 951 case KERN_SUCCESS:
91447636
A
952 error = 0;
953 break;
1c79356b 954 default:
91447636
A
955 error = EINVAL;
956 break;
1c79356b 957 }
91447636
A
958out:
959 fp_drop(p, fd, fp, 0);
0a7de745 960 return error;
1c79356b
A
961}
962
1c79356b 963int
b0d623f7 964sem_post(proc_t p, struct sem_post_args *uap, __unused int32_t *retval)
1c79356b 965{
0a7de745 966 int fd = CAST_DOWN_EXPLICIT(int, uap->sem);
91447636 967 struct fileproc *fp;
1c79356b 968 struct pseminfo * pinfo;
0a7de745 969 struct psemnode * pnode;
1c79356b
A
970 kern_return_t kret;
971 int error;
972
f427ee49 973 error = fp_get_ftype(p, fd, DTYPE_PSXSEM, EBADF, &fp);
0a7de745
A
974 if (error) {
975 return error;
976 }
f427ee49
A
977 pnode = (struct psemnode *)fp->f_data;
978
91447636
A
979 PSEM_SUBSYS_LOCK();
980 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
981 PSEM_SUBSYS_UNLOCK();
982 error = EINVAL;
983 goto out;
984 }
0a7de745
A
985 if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED))
986 != PSEM_ALLOCATED) {
91447636
A
987 PSEM_SUBSYS_UNLOCK();
988 error = EINVAL;
989 goto out;
1c79356b 990 }
2d21ac55
A
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
91447636 998 PSEM_SUBSYS_UNLOCK();
1c79356b
A
999 kret = semaphore_signal(pinfo->psem_semobject);
1000 switch (kret) {
1001 case KERN_INVALID_ADDRESS:
1002 case KERN_PROTECTION_FAILURE:
91447636
A
1003 error = EINVAL;
1004 break;
1c79356b
A
1005 case KERN_ABORTED:
1006 case KERN_OPERATION_TIMED_OUT:
91447636
A
1007 error = EINTR;
1008 break;
1c79356b 1009 case KERN_SUCCESS:
91447636
A
1010 error = 0;
1011 break;
1c79356b 1012 default:
91447636
A
1013 error = EINVAL;
1014 break;
1c79356b 1015 }
91447636
A
1016out:
1017 fp_drop(p, fd, fp, 0);
0a7de745 1018 return error;
1c79356b
A
1019}
1020
9bccf70c 1021static int
f427ee49 1022psem_close(struct psemnode *pnode)
1c79356b 1023{
0a7de745 1024 int error = 0;
2d21ac55 1025 struct pseminfo *pinfo;
1c79356b 1026
91447636 1027 PSEM_SUBSYS_LOCK();
0a7de745 1028 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
91447636 1029 PSEM_SUBSYS_UNLOCK();
0a7de745 1030 return EINVAL;
91447636 1031 }
1c79356b
A
1032
1033 if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) {
91447636 1034 PSEM_SUBSYS_UNLOCK();
0a7de745 1035 return EINVAL;
1c79356b
A
1036 }
1037#if DIAGNOSTIC
0a7de745 1038 if (!pinfo->psem_usecount) {
1c79356b
A
1039 kprintf("negative usecount in psem_close\n");
1040 }
1041#endif /* DIAGNOSTIC */
1042 pinfo->psem_usecount--;
1043
0a7de745 1044 if ((pinfo->psem_flags & PSEM_REMOVED) && !pinfo->psem_usecount) {
91447636
A
1045 PSEM_SUBSYS_UNLOCK();
1046 /* lock dropped as only semaphore is destroyed here */
1c79356b 1047 error = psem_delete(pinfo);
0a7de745 1048 FREE(pinfo, M_SHM);
91447636
A
1049 } else {
1050 PSEM_SUBSYS_UNLOCK();
1c79356b 1051 }
91447636
A
1052 /* subsystem lock is dropped when we get here */
1053 FREE(pnode, M_SHM);
0a7de745 1054 return error;
1c79356b
A
1055}
1056
9bccf70c 1057static int
2d21ac55 1058psem_closefile(struct fileglob *fg, __unused vfs_context_t ctx)
9bccf70c 1059{
2d21ac55
A
1060 /*
1061 * Not locked as psem_close is called only from here and is locked
1062 * properly
1063 */
f427ee49 1064 return psem_close((struct psemnode *)fg->fg_data);
9bccf70c
A
1065}
1066
0a7de745 1067static int
1c79356b
A
1068psem_delete(struct pseminfo * pinfo)
1069{
1070 kern_return_t kret;
1071
1072 kret = semaphore_destroy(kernel_task, pinfo->psem_semobject);
2d21ac55
A
1073#if CONFIG_MACF
1074 mac_posixsem_label_destroy(pinfo);
1075#endif
1c79356b
A
1076
1077 switch (kret) {
1078 case KERN_INVALID_ADDRESS:
1079 case KERN_PROTECTION_FAILURE:
0a7de745 1080 return EINVAL;
1c79356b
A
1081 case KERN_ABORTED:
1082 case KERN_OPERATION_TIMED_OUT:
0a7de745 1083 return EINTR;
1c79356b 1084 case KERN_SUCCESS:
0a7de745 1085 return 0;
1c79356b 1086 default:
0a7de745 1087 return EINVAL;
1c79356b 1088 }
1c79356b
A
1089}
1090
0c530ab8
A
1091int
1092fill_pseminfo(struct psemnode *pnode, struct psem_info * info)
1093{
2d21ac55
A
1094 struct pseminfo *pinfo;
1095 struct vinfo_stat *sb;
0c530ab8
A
1096
1097 PSEM_SUBSYS_LOCK();
0a7de745 1098 if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) {
0c530ab8 1099 PSEM_SUBSYS_UNLOCK();
0a7de745 1100 return EINVAL;
0c530ab8
A
1101 }
1102
1103#if 0
1104 if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) {
1105 PSEM_SUBSYS_UNLOCK();
0a7de745 1106 return EINVAL;
0c530ab8
A
1107 }
1108#endif
1109
1110 sb = &info->psem_stat;
2d21ac55 1111 bzero(sb, sizeof(struct vinfo_stat));
0c530ab8 1112
0a7de745
A
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);
0c530ab8
A
1118
1119 PSEM_SUBSYS_UNLOCK();
0a7de745 1120 return 0;
0c530ab8
A
1121}
1122
2d21ac55
A
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();
f427ee49 1131 pnode = (struct psemnode *)fp->fp_glob->fg_data;
2d21ac55
A
1132 if (pnode != NULL) {
1133 psem = pnode->pinfo;
0a7de745 1134 if (psem != NULL) {
2d21ac55
A
1135 mac_posixsem_vnode_label_associate(
1136 vfs_context_ucred(ctx), psem, psem->psem_label,
1137 vp, vp->v_label);
0a7de745 1138 }
2d21ac55
A
1139 }
1140 PSEM_SUBSYS_UNLOCK();
1141}
1142#endif