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