xref: /freebsd/sys/kern/uipc_sem.c (revision 5b20c5e1f8d59f14901f0c0e4b6b577eaa3f9569)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Alfred Perlstein <alfred@FreeBSD.org>
5  * Copyright (c) 2003-2005 SPARTA, Inc.
6  * Copyright (c) 2005, 2016-2017 Robert N. M. Watson
7  * All rights reserved.
8  *
9  * This software was developed for the FreeBSD Project in part by Network
10  * Associates Laboratories, the Security Research Division of Network
11  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
12  * as part of the DARPA CHATS research program.
13  *
14  * Portions of this software were developed by BAE Systems, the University of
15  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
16  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
17  * Computing (TC) research program.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_posix.h"
45 
46 #include <sys/param.h>
47 #include <sys/capsicum.h>
48 #include <sys/condvar.h>
49 #include <sys/fcntl.h>
50 #include <sys/file.h>
51 #include <sys/filedesc.h>
52 #include <sys/fnv_hash.h>
53 #include <sys/jail.h>
54 #include <sys/kernel.h>
55 #include <sys/ksem.h>
56 #include <sys/lock.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
59 #include <sys/mutex.h>
60 #include <sys/priv.h>
61 #include <sys/proc.h>
62 #include <sys/posix4.h>
63 #include <sys/_semaphore.h>
64 #include <sys/stat.h>
65 #include <sys/syscall.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysctl.h>
68 #include <sys/sysent.h>
69 #include <sys/sysproto.h>
70 #include <sys/systm.h>
71 #include <sys/sx.h>
72 #include <sys/user.h>
73 #include <sys/vnode.h>
74 
75 #include <security/audit/audit.h>
76 #include <security/mac/mac_framework.h>
77 
78 FEATURE(p1003_1b_semaphores, "POSIX P1003.1B semaphores support");
79 /*
80  * TODO
81  *
82  * - Resource limits?
83  * - Replace global sem_lock with mtx_pool locks?
84  * - Add a MAC check_create() hook for creating new named semaphores.
85  */
86 
87 #ifndef SEM_MAX
88 #define	SEM_MAX	30
89 #endif
90 
91 #ifdef SEM_DEBUG
92 #define	DP(x)	printf x
93 #else
94 #define	DP(x)
95 #endif
96 
97 struct ksem_mapping {
98 	char		*km_path;
99 	Fnv32_t		km_fnv;
100 	struct ksem	*km_ksem;
101 	LIST_ENTRY(ksem_mapping) km_link;
102 };
103 
104 static MALLOC_DEFINE(M_KSEM, "ksem", "semaphore file descriptor");
105 static LIST_HEAD(, ksem_mapping) *ksem_dictionary;
106 static struct sx ksem_dict_lock;
107 static struct mtx ksem_count_lock;
108 static struct mtx sem_lock;
109 static u_long ksem_hash;
110 static int ksem_dead;
111 
112 #define	KSEM_HASH(fnv)	(&ksem_dictionary[(fnv) & ksem_hash])
113 
114 static int nsems = 0;
115 SYSCTL_DECL(_p1003_1b);
116 SYSCTL_INT(_p1003_1b, OID_AUTO, nsems, CTLFLAG_RD, &nsems, 0,
117     "Number of active kernel POSIX semaphores");
118 
119 static int	kern_sem_wait(struct thread *td, semid_t id, int tryflag,
120 		    struct timespec *abstime);
121 static int	ksem_access(struct ksem *ks, struct ucred *ucred);
122 static struct ksem *ksem_alloc(struct ucred *ucred, mode_t mode,
123 		    unsigned int value);
124 static int	ksem_create(struct thread *td, const char *path,
125 		    semid_t *semidp, mode_t mode, unsigned int value,
126 		    int flags, int compat32);
127 static void	ksem_drop(struct ksem *ks);
128 static int	ksem_get(struct thread *td, semid_t id, cap_rights_t *rightsp,
129     struct file **fpp);
130 static struct ksem *ksem_hold(struct ksem *ks);
131 static void	ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks);
132 static struct ksem *ksem_lookup(char *path, Fnv32_t fnv);
133 static void	ksem_module_destroy(void);
134 static int	ksem_module_init(void);
135 static int	ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
136 static int	sem_modload(struct module *module, int cmd, void *arg);
137 
138 static fo_stat_t	ksem_stat;
139 static fo_close_t	ksem_closef;
140 static fo_chmod_t	ksem_chmod;
141 static fo_chown_t	ksem_chown;
142 static fo_fill_kinfo_t	ksem_fill_kinfo;
143 
144 /* File descriptor operations. */
145 static struct fileops ksem_ops = {
146 	.fo_read = invfo_rdwr,
147 	.fo_write = invfo_rdwr,
148 	.fo_truncate = invfo_truncate,
149 	.fo_ioctl = invfo_ioctl,
150 	.fo_poll = invfo_poll,
151 	.fo_kqfilter = invfo_kqfilter,
152 	.fo_stat = ksem_stat,
153 	.fo_close = ksem_closef,
154 	.fo_chmod = ksem_chmod,
155 	.fo_chown = ksem_chown,
156 	.fo_sendfile = invfo_sendfile,
157 	.fo_fill_kinfo = ksem_fill_kinfo,
158 	.fo_flags = DFLAG_PASSABLE
159 };
160 
161 FEATURE(posix_sem, "POSIX semaphores");
162 
163 static int
164 ksem_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
165     struct thread *td)
166 {
167 	struct ksem *ks;
168 #ifdef MAC
169 	int error;
170 #endif
171 
172 	ks = fp->f_data;
173 
174 #ifdef MAC
175 	error = mac_posixsem_check_stat(active_cred, fp->f_cred, ks);
176 	if (error)
177 		return (error);
178 #endif
179 
180 	/*
181 	 * Attempt to return sanish values for fstat() on a semaphore
182 	 * file descriptor.
183 	 */
184 	bzero(sb, sizeof(*sb));
185 
186 	mtx_lock(&sem_lock);
187 	sb->st_atim = ks->ks_atime;
188 	sb->st_ctim = ks->ks_ctime;
189 	sb->st_mtim = ks->ks_mtime;
190 	sb->st_birthtim = ks->ks_birthtime;
191 	sb->st_uid = ks->ks_uid;
192 	sb->st_gid = ks->ks_gid;
193 	sb->st_mode = S_IFREG | ks->ks_mode;		/* XXX */
194 	mtx_unlock(&sem_lock);
195 
196 	return (0);
197 }
198 
199 static int
200 ksem_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
201     struct thread *td)
202 {
203 	struct ksem *ks;
204 	int error;
205 
206 	error = 0;
207 	ks = fp->f_data;
208 	mtx_lock(&sem_lock);
209 #ifdef MAC
210 	error = mac_posixsem_check_setmode(active_cred, ks, mode);
211 	if (error != 0)
212 		goto out;
213 #endif
214 	error = vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid, VADMIN,
215 	    active_cred);
216 	if (error != 0)
217 		goto out;
218 	ks->ks_mode = mode & ACCESSPERMS;
219 out:
220 	mtx_unlock(&sem_lock);
221 	return (error);
222 }
223 
224 static int
225 ksem_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
226     struct thread *td)
227 {
228 	struct ksem *ks;
229 	int error;
230 
231 	error = 0;
232 	ks = fp->f_data;
233 	mtx_lock(&sem_lock);
234 #ifdef MAC
235 	error = mac_posixsem_check_setowner(active_cred, ks, uid, gid);
236 	if (error != 0)
237 		goto out;
238 #endif
239 	if (uid == (uid_t)-1)
240 		uid = ks->ks_uid;
241 	if (gid == (gid_t)-1)
242                  gid = ks->ks_gid;
243 	if (((uid != ks->ks_uid && uid != active_cred->cr_uid) ||
244 	    (gid != ks->ks_gid && !groupmember(gid, active_cred))) &&
245 	    (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN)))
246 		goto out;
247 	ks->ks_uid = uid;
248 	ks->ks_gid = gid;
249 out:
250 	mtx_unlock(&sem_lock);
251 	return (error);
252 }
253 
254 static int
255 ksem_closef(struct file *fp, struct thread *td)
256 {
257 	struct ksem *ks;
258 
259 	ks = fp->f_data;
260 	fp->f_data = NULL;
261 	ksem_drop(ks);
262 
263 	return (0);
264 }
265 
266 static int
267 ksem_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
268 {
269 	const char *path, *pr_path;
270 	struct ksem *ks;
271 	size_t pr_pathlen;
272 
273 	kif->kf_type = KF_TYPE_SEM;
274 	ks = fp->f_data;
275 	mtx_lock(&sem_lock);
276 	kif->kf_un.kf_sem.kf_sem_value = ks->ks_value;
277 	kif->kf_un.kf_sem.kf_sem_mode = S_IFREG | ks->ks_mode;	/* XXX */
278 	mtx_unlock(&sem_lock);
279 	if (ks->ks_path != NULL) {
280 		sx_slock(&ksem_dict_lock);
281 		if (ks->ks_path != NULL) {
282 			path = ks->ks_path;
283 			pr_path = curthread->td_ucred->cr_prison->pr_path;
284 			if (strcmp(pr_path, "/") != 0) {
285 				/* Return the jail-rooted pathname. */
286 				pr_pathlen = strlen(pr_path);
287 				if (strncmp(path, pr_path, pr_pathlen) == 0 &&
288 				    path[pr_pathlen] == '/')
289 					path += pr_pathlen;
290 			}
291 			strlcpy(kif->kf_path, path, sizeof(kif->kf_path));
292 		}
293 		sx_sunlock(&ksem_dict_lock);
294 	}
295 	return (0);
296 }
297 
298 /*
299  * ksem object management including creation and reference counting
300  * routines.
301  */
302 static struct ksem *
303 ksem_alloc(struct ucred *ucred, mode_t mode, unsigned int value)
304 {
305 	struct ksem *ks;
306 
307 	mtx_lock(&ksem_count_lock);
308 	if (nsems == p31b_getcfg(CTL_P1003_1B_SEM_NSEMS_MAX) || ksem_dead) {
309 		mtx_unlock(&ksem_count_lock);
310 		return (NULL);
311 	}
312 	nsems++;
313 	mtx_unlock(&ksem_count_lock);
314 	ks = malloc(sizeof(*ks), M_KSEM, M_WAITOK | M_ZERO);
315 	ks->ks_uid = ucred->cr_uid;
316 	ks->ks_gid = ucred->cr_gid;
317 	ks->ks_mode = mode;
318 	ks->ks_value = value;
319 	cv_init(&ks->ks_cv, "ksem");
320 	vfs_timestamp(&ks->ks_birthtime);
321 	ks->ks_atime = ks->ks_mtime = ks->ks_ctime = ks->ks_birthtime;
322 	refcount_init(&ks->ks_ref, 1);
323 #ifdef MAC
324 	mac_posixsem_init(ks);
325 	mac_posixsem_create(ucred, ks);
326 #endif
327 
328 	return (ks);
329 }
330 
331 static struct ksem *
332 ksem_hold(struct ksem *ks)
333 {
334 
335 	refcount_acquire(&ks->ks_ref);
336 	return (ks);
337 }
338 
339 static void
340 ksem_drop(struct ksem *ks)
341 {
342 
343 	if (refcount_release(&ks->ks_ref)) {
344 #ifdef MAC
345 		mac_posixsem_destroy(ks);
346 #endif
347 		cv_destroy(&ks->ks_cv);
348 		free(ks, M_KSEM);
349 		mtx_lock(&ksem_count_lock);
350 		nsems--;
351 		mtx_unlock(&ksem_count_lock);
352 	}
353 }
354 
355 /*
356  * Determine if the credentials have sufficient permissions for read
357  * and write access.
358  */
359 static int
360 ksem_access(struct ksem *ks, struct ucred *ucred)
361 {
362 	int error;
363 
364 	error = vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid,
365 	    VREAD | VWRITE, ucred);
366 	if (error)
367 		error = priv_check_cred(ucred, PRIV_SEM_WRITE);
368 	return (error);
369 }
370 
371 /*
372  * Dictionary management.  We maintain an in-kernel dictionary to map
373  * paths to semaphore objects.  We use the FNV hash on the path to
374  * store the mappings in a hash table.
375  */
376 static struct ksem *
377 ksem_lookup(char *path, Fnv32_t fnv)
378 {
379 	struct ksem_mapping *map;
380 
381 	LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
382 		if (map->km_fnv != fnv)
383 			continue;
384 		if (strcmp(map->km_path, path) == 0)
385 			return (map->km_ksem);
386 	}
387 
388 	return (NULL);
389 }
390 
391 static void
392 ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks)
393 {
394 	struct ksem_mapping *map;
395 
396 	map = malloc(sizeof(struct ksem_mapping), M_KSEM, M_WAITOK);
397 	map->km_path = path;
398 	map->km_fnv = fnv;
399 	map->km_ksem = ksem_hold(ks);
400 	ks->ks_path = path;
401 	LIST_INSERT_HEAD(KSEM_HASH(fnv), map, km_link);
402 }
403 
404 static int
405 ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
406 {
407 	struct ksem_mapping *map;
408 	int error;
409 
410 	LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
411 		if (map->km_fnv != fnv)
412 			continue;
413 		if (strcmp(map->km_path, path) == 0) {
414 #ifdef MAC
415 			error = mac_posixsem_check_unlink(ucred, map->km_ksem);
416 			if (error)
417 				return (error);
418 #endif
419 			error = ksem_access(map->km_ksem, ucred);
420 			if (error)
421 				return (error);
422 			map->km_ksem->ks_path = NULL;
423 			LIST_REMOVE(map, km_link);
424 			ksem_drop(map->km_ksem);
425 			free(map->km_path, M_KSEM);
426 			free(map, M_KSEM);
427 			return (0);
428 		}
429 	}
430 
431 	return (ENOENT);
432 }
433 
434 static int
435 ksem_create_copyout_semid(struct thread *td, semid_t *semidp, int fd,
436     int compat32)
437 {
438 	semid_t semid;
439 #ifdef COMPAT_FREEBSD32
440 	int32_t semid32;
441 #endif
442 	void *ptr;
443 	size_t ptrs;
444 
445 #ifdef COMPAT_FREEBSD32
446 	if (compat32) {
447 		semid32 = fd;
448 		ptr = &semid32;
449 		ptrs = sizeof(semid32);
450 	} else {
451 #endif
452 		semid = fd;
453 		ptr = &semid;
454 		ptrs = sizeof(semid);
455 		compat32 = 0; /* silence gcc */
456 #ifdef COMPAT_FREEBSD32
457 	}
458 #endif
459 
460 	return (copyout(ptr, semidp, ptrs));
461 }
462 
463 /* Other helper routines. */
464 static int
465 ksem_create(struct thread *td, const char *name, semid_t *semidp, mode_t mode,
466     unsigned int value, int flags, int compat32)
467 {
468 	struct pwddesc *pdp;
469 	struct ksem *ks;
470 	struct file *fp;
471 	char *path;
472 	const char *pr_path;
473 	size_t pr_pathlen;
474 	Fnv32_t fnv;
475 	int error, fd;
476 
477 	AUDIT_ARG_FFLAGS(flags);
478 	AUDIT_ARG_MODE(mode);
479 	AUDIT_ARG_VALUE(value);
480 
481 	if (value > SEM_VALUE_MAX)
482 		return (EINVAL);
483 
484 	pdp = td->td_proc->p_pd;
485 	mode = (mode & ~pdp->pd_cmask) & ACCESSPERMS;
486 	error = falloc(td, &fp, &fd, O_CLOEXEC);
487 	if (error) {
488 		if (name == NULL)
489 			error = ENOSPC;
490 		return (error);
491 	}
492 
493 	/*
494 	 * Go ahead and copyout the file descriptor now.  This is a bit
495 	 * premature, but it is a lot easier to handle errors as opposed
496 	 * to later when we've possibly created a new semaphore, etc.
497 	 */
498 	error = ksem_create_copyout_semid(td, semidp, fd, compat32);
499 	if (error) {
500 		fdclose(td, fp, fd);
501 		fdrop(fp, td);
502 		return (error);
503 	}
504 
505 	if (name == NULL) {
506 		/* Create an anonymous semaphore. */
507 		ks = ksem_alloc(td->td_ucred, mode, value);
508 		if (ks == NULL)
509 			error = ENOSPC;
510 		else
511 			ks->ks_flags |= KS_ANONYMOUS;
512 	} else {
513 		path = malloc(MAXPATHLEN, M_KSEM, M_WAITOK);
514 		pr_path = td->td_ucred->cr_prison->pr_path;
515 
516 		/* Construct a full pathname for jailed callers. */
517 		pr_pathlen = strcmp(pr_path, "/") == 0 ? 0
518 		    : strlcpy(path, pr_path, MAXPATHLEN);
519 		error = copyinstr(name, path + pr_pathlen,
520 		    MAXPATHLEN - pr_pathlen, NULL);
521 
522 		/* Require paths to start with a '/' character. */
523 		if (error == 0 && path[pr_pathlen] != '/')
524 			error = EINVAL;
525 		if (error) {
526 			fdclose(td, fp, fd);
527 			fdrop(fp, td);
528 			free(path, M_KSEM);
529 			return (error);
530 		}
531 
532 		AUDIT_ARG_UPATH1_CANON(path);
533 		fnv = fnv_32_str(path, FNV1_32_INIT);
534 		sx_xlock(&ksem_dict_lock);
535 		ks = ksem_lookup(path, fnv);
536 		if (ks == NULL) {
537 			/* Object does not exist, create it if requested. */
538 			if (flags & O_CREAT) {
539 				ks = ksem_alloc(td->td_ucred, mode, value);
540 				if (ks == NULL)
541 					error = ENFILE;
542 				else {
543 					ksem_insert(path, fnv, ks);
544 					path = NULL;
545 				}
546 			} else
547 				error = ENOENT;
548 		} else {
549 			/*
550 			 * Object already exists, obtain a new
551 			 * reference if requested and permitted.
552 			 */
553 			if ((flags & (O_CREAT | O_EXCL)) ==
554 			    (O_CREAT | O_EXCL))
555 				error = EEXIST;
556 			else {
557 #ifdef MAC
558 				error = mac_posixsem_check_open(td->td_ucred,
559 				    ks);
560 				if (error == 0)
561 #endif
562 				error = ksem_access(ks, td->td_ucred);
563 			}
564 			if (error == 0)
565 				ksem_hold(ks);
566 #ifdef INVARIANTS
567 			else
568 				ks = NULL;
569 #endif
570 		}
571 		sx_xunlock(&ksem_dict_lock);
572 		if (path)
573 			free(path, M_KSEM);
574 	}
575 
576 	if (error) {
577 		KASSERT(ks == NULL, ("ksem_create error with a ksem"));
578 		fdclose(td, fp, fd);
579 		fdrop(fp, td);
580 		return (error);
581 	}
582 	KASSERT(ks != NULL, ("ksem_create w/o a ksem"));
583 
584 	finit(fp, FREAD | FWRITE, DTYPE_SEM, ks, &ksem_ops);
585 
586 	fdrop(fp, td);
587 
588 	return (0);
589 }
590 
591 static int
592 ksem_get(struct thread *td, semid_t id, cap_rights_t *rightsp,
593     struct file **fpp)
594 {
595 	struct ksem *ks;
596 	struct file *fp;
597 	int error;
598 
599 	error = fget(td, id, rightsp, &fp);
600 	if (error)
601 		return (EINVAL);
602 	if (fp->f_type != DTYPE_SEM) {
603 		fdrop(fp, td);
604 		return (EINVAL);
605 	}
606 	ks = fp->f_data;
607 	if (ks->ks_flags & KS_DEAD) {
608 		fdrop(fp, td);
609 		return (EINVAL);
610 	}
611 	*fpp = fp;
612 	return (0);
613 }
614 
615 /* System calls. */
616 #ifndef _SYS_SYSPROTO_H_
617 struct ksem_init_args {
618 	unsigned int	value;
619 	semid_t		*idp;
620 };
621 #endif
622 int
623 sys_ksem_init(struct thread *td, struct ksem_init_args *uap)
624 {
625 
626 	return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
627 	    0, 0));
628 }
629 
630 #ifndef _SYS_SYSPROTO_H_
631 struct ksem_open_args {
632 	char		*name;
633 	int		oflag;
634 	mode_t		mode;
635 	unsigned int	value;
636 	semid_t		*idp;
637 };
638 #endif
639 int
640 sys_ksem_open(struct thread *td, struct ksem_open_args *uap)
641 {
642 
643 	DP((">>> ksem_open start, pid=%d\n", (int)td->td_proc->p_pid));
644 
645 	if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
646 		return (EINVAL);
647 	return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
648 	    uap->oflag, 0));
649 }
650 
651 #ifndef _SYS_SYSPROTO_H_
652 struct ksem_unlink_args {
653 	char		*name;
654 };
655 #endif
656 int
657 sys_ksem_unlink(struct thread *td, struct ksem_unlink_args *uap)
658 {
659 	char *path;
660 	const char *pr_path;
661 	size_t pr_pathlen;
662 	Fnv32_t fnv;
663 	int error;
664 
665 	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
666 	pr_path = td->td_ucred->cr_prison->pr_path;
667 	pr_pathlen = strcmp(pr_path, "/") == 0 ? 0
668 	    : strlcpy(path, pr_path, MAXPATHLEN);
669 	error = copyinstr(uap->name, path + pr_pathlen, MAXPATHLEN - pr_pathlen,
670 	    NULL);
671 	if (error) {
672 		free(path, M_TEMP);
673 		return (error);
674 	}
675 
676 	AUDIT_ARG_UPATH1_CANON(path);
677 	fnv = fnv_32_str(path, FNV1_32_INIT);
678 	sx_xlock(&ksem_dict_lock);
679 	error = ksem_remove(path, fnv, td->td_ucred);
680 	sx_xunlock(&ksem_dict_lock);
681 	free(path, M_TEMP);
682 
683 	return (error);
684 }
685 
686 #ifndef _SYS_SYSPROTO_H_
687 struct ksem_close_args {
688 	semid_t		id;
689 };
690 #endif
691 int
692 sys_ksem_close(struct thread *td, struct ksem_close_args *uap)
693 {
694 	struct ksem *ks;
695 	struct file *fp;
696 	int error;
697 
698 	/* No capability rights required to close a semaphore. */
699 	AUDIT_ARG_FD(uap->id);
700 	error = ksem_get(td, uap->id, &cap_no_rights, &fp);
701 	if (error)
702 		return (error);
703 	ks = fp->f_data;
704 	if (ks->ks_flags & KS_ANONYMOUS) {
705 		fdrop(fp, td);
706 		return (EINVAL);
707 	}
708 	error = kern_close(td, uap->id);
709 	fdrop(fp, td);
710 	return (error);
711 }
712 
713 #ifndef _SYS_SYSPROTO_H_
714 struct ksem_post_args {
715 	semid_t	id;
716 };
717 #endif
718 int
719 sys_ksem_post(struct thread *td, struct ksem_post_args *uap)
720 {
721 	cap_rights_t rights;
722 	struct file *fp;
723 	struct ksem *ks;
724 	int error;
725 
726 	AUDIT_ARG_FD(uap->id);
727 	error = ksem_get(td, uap->id,
728 	    cap_rights_init_one(&rights, CAP_SEM_POST), &fp);
729 	if (error)
730 		return (error);
731 	ks = fp->f_data;
732 
733 	mtx_lock(&sem_lock);
734 #ifdef MAC
735 	error = mac_posixsem_check_post(td->td_ucred, fp->f_cred, ks);
736 	if (error)
737 		goto err;
738 #endif
739 	if (ks->ks_value == SEM_VALUE_MAX) {
740 		error = EOVERFLOW;
741 		goto err;
742 	}
743 	++ks->ks_value;
744 	if (ks->ks_waiters > 0)
745 		cv_signal(&ks->ks_cv);
746 	error = 0;
747 	vfs_timestamp(&ks->ks_ctime);
748 err:
749 	mtx_unlock(&sem_lock);
750 	fdrop(fp, td);
751 	return (error);
752 }
753 
754 #ifndef _SYS_SYSPROTO_H_
755 struct ksem_wait_args {
756 	semid_t		id;
757 };
758 #endif
759 int
760 sys_ksem_wait(struct thread *td, struct ksem_wait_args *uap)
761 {
762 
763 	return (kern_sem_wait(td, uap->id, 0, NULL));
764 }
765 
766 #ifndef _SYS_SYSPROTO_H_
767 struct ksem_timedwait_args {
768 	semid_t		id;
769 	const struct timespec *abstime;
770 };
771 #endif
772 int
773 sys_ksem_timedwait(struct thread *td, struct ksem_timedwait_args *uap)
774 {
775 	struct timespec abstime;
776 	struct timespec *ts;
777 	int error;
778 
779 	/*
780 	 * We allow a null timespec (wait forever).
781 	 */
782 	if (uap->abstime == NULL)
783 		ts = NULL;
784 	else {
785 		error = copyin(uap->abstime, &abstime, sizeof(abstime));
786 		if (error != 0)
787 			return (error);
788 		if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
789 			return (EINVAL);
790 		ts = &abstime;
791 	}
792 	return (kern_sem_wait(td, uap->id, 0, ts));
793 }
794 
795 #ifndef _SYS_SYSPROTO_H_
796 struct ksem_trywait_args {
797 	semid_t		id;
798 };
799 #endif
800 int
801 sys_ksem_trywait(struct thread *td, struct ksem_trywait_args *uap)
802 {
803 
804 	return (kern_sem_wait(td, uap->id, 1, NULL));
805 }
806 
807 static int
808 kern_sem_wait(struct thread *td, semid_t id, int tryflag,
809     struct timespec *abstime)
810 {
811 	struct timespec ts1, ts2;
812 	struct timeval tv;
813 	cap_rights_t rights;
814 	struct file *fp;
815 	struct ksem *ks;
816 	int error;
817 
818 	DP((">>> kern_sem_wait entered! pid=%d\n", (int)td->td_proc->p_pid));
819 	AUDIT_ARG_FD(id);
820 	error = ksem_get(td, id, cap_rights_init_one(&rights, CAP_SEM_WAIT),
821 	    &fp);
822 	if (error)
823 		return (error);
824 	ks = fp->f_data;
825 	mtx_lock(&sem_lock);
826 	DP((">>> kern_sem_wait critical section entered! pid=%d\n",
827 	    (int)td->td_proc->p_pid));
828 #ifdef MAC
829 	error = mac_posixsem_check_wait(td->td_ucred, fp->f_cred, ks);
830 	if (error) {
831 		DP(("kern_sem_wait mac failed\n"));
832 		goto err;
833 	}
834 #endif
835 	DP(("kern_sem_wait value = %d, tryflag %d\n", ks->ks_value, tryflag));
836 	vfs_timestamp(&ks->ks_atime);
837 	while (ks->ks_value == 0) {
838 		ks->ks_waiters++;
839 		if (tryflag != 0)
840 			error = EAGAIN;
841 		else if (abstime == NULL)
842 			error = cv_wait_sig(&ks->ks_cv, &sem_lock);
843 		else {
844 			for (;;) {
845 				ts1 = *abstime;
846 				getnanotime(&ts2);
847 				timespecsub(&ts1, &ts2, &ts1);
848 				TIMESPEC_TO_TIMEVAL(&tv, &ts1);
849 				if (tv.tv_sec < 0) {
850 					error = ETIMEDOUT;
851 					break;
852 				}
853 				error = cv_timedwait_sig(&ks->ks_cv,
854 				    &sem_lock, tvtohz(&tv));
855 				if (error != EWOULDBLOCK)
856 					break;
857 			}
858 		}
859 		ks->ks_waiters--;
860 		if (error)
861 			goto err;
862 	}
863 	ks->ks_value--;
864 	DP(("kern_sem_wait value post-decrement = %d\n", ks->ks_value));
865 	error = 0;
866 err:
867 	mtx_unlock(&sem_lock);
868 	fdrop(fp, td);
869 	DP(("<<< kern_sem_wait leaving, pid=%d, error = %d\n",
870 	    (int)td->td_proc->p_pid, error));
871 	return (error);
872 }
873 
874 #ifndef _SYS_SYSPROTO_H_
875 struct ksem_getvalue_args {
876 	semid_t		id;
877 	int		*val;
878 };
879 #endif
880 int
881 sys_ksem_getvalue(struct thread *td, struct ksem_getvalue_args *uap)
882 {
883 	cap_rights_t rights;
884 	struct file *fp;
885 	struct ksem *ks;
886 	int error, val;
887 
888 	AUDIT_ARG_FD(uap->id);
889 	error = ksem_get(td, uap->id,
890 	    cap_rights_init_one(&rights, CAP_SEM_GETVALUE), &fp);
891 	if (error)
892 		return (error);
893 	ks = fp->f_data;
894 
895 	mtx_lock(&sem_lock);
896 #ifdef MAC
897 	error = mac_posixsem_check_getvalue(td->td_ucred, fp->f_cred, ks);
898 	if (error) {
899 		mtx_unlock(&sem_lock);
900 		fdrop(fp, td);
901 		return (error);
902 	}
903 #endif
904 	val = ks->ks_value;
905 	vfs_timestamp(&ks->ks_atime);
906 	mtx_unlock(&sem_lock);
907 	fdrop(fp, td);
908 	error = copyout(&val, uap->val, sizeof(val));
909 	return (error);
910 }
911 
912 #ifndef _SYS_SYSPROTO_H_
913 struct ksem_destroy_args {
914 	semid_t		id;
915 };
916 #endif
917 int
918 sys_ksem_destroy(struct thread *td, struct ksem_destroy_args *uap)
919 {
920 	struct file *fp;
921 	struct ksem *ks;
922 	int error;
923 
924 	/* No capability rights required to close a semaphore. */
925 	AUDIT_ARG_FD(uap->id);
926 	error = ksem_get(td, uap->id, &cap_no_rights, &fp);
927 	if (error)
928 		return (error);
929 	ks = fp->f_data;
930 	if (!(ks->ks_flags & KS_ANONYMOUS)) {
931 		fdrop(fp, td);
932 		return (EINVAL);
933 	}
934 	mtx_lock(&sem_lock);
935 	if (ks->ks_waiters != 0) {
936 		mtx_unlock(&sem_lock);
937 		error = EBUSY;
938 		goto err;
939 	}
940 	ks->ks_flags |= KS_DEAD;
941 	mtx_unlock(&sem_lock);
942 
943 	error = kern_close(td, uap->id);
944 err:
945 	fdrop(fp, td);
946 	return (error);
947 }
948 
949 static struct syscall_helper_data ksem_syscalls[] = {
950 	SYSCALL_INIT_HELPER(ksem_init),
951 	SYSCALL_INIT_HELPER(ksem_open),
952 	SYSCALL_INIT_HELPER(ksem_unlink),
953 	SYSCALL_INIT_HELPER(ksem_close),
954 	SYSCALL_INIT_HELPER(ksem_post),
955 	SYSCALL_INIT_HELPER(ksem_wait),
956 	SYSCALL_INIT_HELPER(ksem_timedwait),
957 	SYSCALL_INIT_HELPER(ksem_trywait),
958 	SYSCALL_INIT_HELPER(ksem_getvalue),
959 	SYSCALL_INIT_HELPER(ksem_destroy),
960 	SYSCALL_INIT_LAST
961 };
962 
963 #ifdef COMPAT_FREEBSD32
964 #include <compat/freebsd32/freebsd32.h>
965 #include <compat/freebsd32/freebsd32_proto.h>
966 #include <compat/freebsd32/freebsd32_signal.h>
967 #include <compat/freebsd32/freebsd32_syscall.h>
968 #include <compat/freebsd32/freebsd32_util.h>
969 
970 int
971 freebsd32_ksem_init(struct thread *td, struct freebsd32_ksem_init_args *uap)
972 {
973 
974 	return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
975 	    0, 1));
976 }
977 
978 int
979 freebsd32_ksem_open(struct thread *td, struct freebsd32_ksem_open_args *uap)
980 {
981 
982 	if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
983 		return (EINVAL);
984 	return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
985 	    uap->oflag, 1));
986 }
987 
988 int
989 freebsd32_ksem_timedwait(struct thread *td,
990     struct freebsd32_ksem_timedwait_args *uap)
991 {
992 	struct timespec32 abstime32;
993 	struct timespec *ts, abstime;
994 	int error;
995 
996 	/*
997 	 * We allow a null timespec (wait forever).
998 	 */
999 	if (uap->abstime == NULL)
1000 		ts = NULL;
1001 	else {
1002 		error = copyin(uap->abstime, &abstime32, sizeof(abstime32));
1003 		if (error != 0)
1004 			return (error);
1005 		CP(abstime32, abstime, tv_sec);
1006 		CP(abstime32, abstime, tv_nsec);
1007 		if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
1008 			return (EINVAL);
1009 		ts = &abstime;
1010 	}
1011 	return (kern_sem_wait(td, uap->id, 0, ts));
1012 }
1013 
1014 static struct syscall_helper_data ksem32_syscalls[] = {
1015 	SYSCALL32_INIT_HELPER(freebsd32_ksem_init),
1016 	SYSCALL32_INIT_HELPER(freebsd32_ksem_open),
1017 	SYSCALL32_INIT_HELPER_COMPAT(ksem_unlink),
1018 	SYSCALL32_INIT_HELPER_COMPAT(ksem_close),
1019 	SYSCALL32_INIT_HELPER_COMPAT(ksem_post),
1020 	SYSCALL32_INIT_HELPER_COMPAT(ksem_wait),
1021 	SYSCALL32_INIT_HELPER(freebsd32_ksem_timedwait),
1022 	SYSCALL32_INIT_HELPER_COMPAT(ksem_trywait),
1023 	SYSCALL32_INIT_HELPER_COMPAT(ksem_getvalue),
1024 	SYSCALL32_INIT_HELPER_COMPAT(ksem_destroy),
1025 	SYSCALL_INIT_LAST
1026 };
1027 #endif
1028 
1029 static int
1030 ksem_module_init(void)
1031 {
1032 	int error;
1033 
1034 	mtx_init(&sem_lock, "sem", NULL, MTX_DEF);
1035 	mtx_init(&ksem_count_lock, "ksem count", NULL, MTX_DEF);
1036 	sx_init(&ksem_dict_lock, "ksem dictionary");
1037 	ksem_dictionary = hashinit(1024, M_KSEM, &ksem_hash);
1038 	p31b_setcfg(CTL_P1003_1B_SEMAPHORES, 200112L);
1039 	p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, SEM_MAX);
1040 	p31b_setcfg(CTL_P1003_1B_SEM_VALUE_MAX, SEM_VALUE_MAX);
1041 
1042 	error = syscall_helper_register(ksem_syscalls, SY_THR_STATIC_KLD);
1043 	if (error)
1044 		return (error);
1045 #ifdef COMPAT_FREEBSD32
1046 	error = syscall32_helper_register(ksem32_syscalls, SY_THR_STATIC_KLD);
1047 	if (error)
1048 		return (error);
1049 #endif
1050 	return (0);
1051 }
1052 
1053 static void
1054 ksem_module_destroy(void)
1055 {
1056 
1057 #ifdef COMPAT_FREEBSD32
1058 	syscall32_helper_unregister(ksem32_syscalls);
1059 #endif
1060 	syscall_helper_unregister(ksem_syscalls);
1061 
1062 	p31b_setcfg(CTL_P1003_1B_SEMAPHORES, 0);
1063 	hashdestroy(ksem_dictionary, M_KSEM, ksem_hash);
1064 	sx_destroy(&ksem_dict_lock);
1065 	mtx_destroy(&ksem_count_lock);
1066 	mtx_destroy(&sem_lock);
1067 	p31b_unsetcfg(CTL_P1003_1B_SEM_VALUE_MAX);
1068 	p31b_unsetcfg(CTL_P1003_1B_SEM_NSEMS_MAX);
1069 }
1070 
1071 static int
1072 sem_modload(struct module *module, int cmd, void *arg)
1073 {
1074         int error = 0;
1075 
1076         switch (cmd) {
1077         case MOD_LOAD:
1078 		error = ksem_module_init();
1079 		if (error)
1080 			ksem_module_destroy();
1081                 break;
1082 
1083         case MOD_UNLOAD:
1084 		mtx_lock(&ksem_count_lock);
1085 		if (nsems != 0) {
1086 			error = EOPNOTSUPP;
1087 			mtx_unlock(&ksem_count_lock);
1088 			break;
1089 		}
1090 		ksem_dead = 1;
1091 		mtx_unlock(&ksem_count_lock);
1092 		ksem_module_destroy();
1093                 break;
1094 
1095         case MOD_SHUTDOWN:
1096                 break;
1097         default:
1098                 error = EINVAL;
1099                 break;
1100         }
1101         return (error);
1102 }
1103 
1104 static moduledata_t sem_mod = {
1105         "sem",
1106         &sem_modload,
1107         NULL
1108 };
1109 
1110 DECLARE_MODULE(sem, sem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
1111 MODULE_VERSION(sem, 1);
1112