2 * Copyright (c) 2002 Alfred Perlstein <alfred@FreeBSD.org>
3 * Copyright (c) 2003-2005 SPARTA, Inc.
4 * Copyright (c) 2005 Robert N. M. Watson
7 * This software was developed for the FreeBSD Project in part by Network
8 * Associates Laboratories, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
10 * as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_compat.h"
38 #include "opt_posix.h"
40 #include <sys/param.h>
41 #include <sys/condvar.h>
42 #include <sys/fcntl.h>
44 #include <sys/filedesc.h>
45 #include <sys/fnv_hash.h>
46 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/mutex.h>
54 #include <sys/posix4.h>
55 #include <sys/_semaphore.h>
57 #include <sys/syscall.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
61 #include <sys/sysproto.h>
62 #include <sys/systm.h>
64 #include <sys/vnode.h>
66 #include <security/mac/mac_framework.h>
73 * - Replace global sem_lock with mtx_pool locks?
74 * - Add a MAC check_create() hook for creating new named semaphores.
82 #define DP(x) printf x
91 LIST_ENTRY(ksem_mapping) km_link;
94 static MALLOC_DEFINE(M_KSEM, "ksem", "semaphore file descriptor");
95 static LIST_HEAD(, ksem_mapping) *ksem_dictionary;
96 static struct sx ksem_dict_lock;
97 static struct mtx ksem_count_lock;
98 static struct mtx sem_lock;
99 static u_long ksem_hash;
100 static int ksem_dead;
102 #define KSEM_HASH(fnv) (&ksem_dictionary[(fnv) & ksem_hash])
104 static int nsems = 0;
105 SYSCTL_DECL(_p1003_1b);
106 SYSCTL_INT(_p1003_1b, OID_AUTO, nsems, CTLFLAG_RD, &nsems, 0,
107 "Number of active kernel POSIX semaphores");
109 static int kern_sem_wait(struct thread *td, semid_t id, int tryflag,
110 struct timespec *abstime);
111 static int ksem_access(struct ksem *ks, struct ucred *ucred);
112 static struct ksem *ksem_alloc(struct ucred *ucred, mode_t mode,
114 static int ksem_create(struct thread *td, const char *path,
115 semid_t *semidp, mode_t mode, unsigned int value,
116 int flags, int compat32);
117 static void ksem_drop(struct ksem *ks);
118 static int ksem_get(struct thread *td, semid_t id, struct file **fpp);
119 static struct ksem *ksem_hold(struct ksem *ks);
120 static void ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks);
121 static struct ksem *ksem_lookup(char *path, Fnv32_t fnv);
122 static void ksem_module_destroy(void);
123 static int ksem_module_init(void);
124 static int ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
125 static int sem_modload(struct module *module, int cmd, void *arg);
127 static fo_rdwr_t ksem_read;
128 static fo_rdwr_t ksem_write;
129 static fo_truncate_t ksem_truncate;
130 static fo_ioctl_t ksem_ioctl;
131 static fo_poll_t ksem_poll;
132 static fo_kqfilter_t ksem_kqfilter;
133 static fo_stat_t ksem_stat;
134 static fo_close_t ksem_closef;
136 /* File descriptor operations. */
137 static struct fileops ksem_ops = {
138 .fo_read = ksem_read,
139 .fo_write = ksem_write,
140 .fo_truncate = ksem_truncate,
141 .fo_ioctl = ksem_ioctl,
142 .fo_poll = ksem_poll,
143 .fo_kqfilter = ksem_kqfilter,
144 .fo_stat = ksem_stat,
145 .fo_close = ksem_closef,
146 .fo_flags = DFLAG_PASSABLE
149 FEATURE(posix_sem, "POSIX semaphores");
152 ksem_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
153 int flags, struct thread *td)
160 ksem_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
161 int flags, struct thread *td)
168 ksem_truncate(struct file *fp, off_t length, struct ucred *active_cred,
176 ksem_ioctl(struct file *fp, u_long com, void *data,
177 struct ucred *active_cred, struct thread *td)
184 ksem_poll(struct file *fp, int events, struct ucred *active_cred,
192 ksem_kqfilter(struct file *fp, struct knote *kn)
199 ksem_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
210 error = mac_posixsem_check_stat(active_cred, fp->f_cred, ks);
216 * Attempt to return sanish values for fstat() on a semaphore
219 bzero(sb, sizeof(*sb));
220 sb->st_mode = S_IFREG | ks->ks_mode; /* XXX */
222 sb->st_atim = ks->ks_atime;
223 sb->st_ctim = ks->ks_ctime;
224 sb->st_mtim = ks->ks_mtime;
225 sb->st_birthtim = ks->ks_birthtime;
226 sb->st_uid = ks->ks_uid;
227 sb->st_gid = ks->ks_gid;
233 ksem_closef(struct file *fp, struct thread *td)
245 * ksem object management including creation and reference counting
249 ksem_alloc(struct ucred *ucred, mode_t mode, unsigned int value)
253 mtx_lock(&ksem_count_lock);
254 if (nsems == p31b_getcfg(CTL_P1003_1B_SEM_NSEMS_MAX) || ksem_dead) {
255 mtx_unlock(&ksem_count_lock);
259 mtx_unlock(&ksem_count_lock);
260 ks = malloc(sizeof(*ks), M_KSEM, M_WAITOK | M_ZERO);
261 ks->ks_uid = ucred->cr_uid;
262 ks->ks_gid = ucred->cr_gid;
264 ks->ks_value = value;
265 cv_init(&ks->ks_cv, "ksem");
266 vfs_timestamp(&ks->ks_birthtime);
267 ks->ks_atime = ks->ks_mtime = ks->ks_ctime = ks->ks_birthtime;
268 refcount_init(&ks->ks_ref, 1);
270 mac_posixsem_init(ks);
271 mac_posixsem_create(ucred, ks);
278 ksem_hold(struct ksem *ks)
281 refcount_acquire(&ks->ks_ref);
286 ksem_drop(struct ksem *ks)
289 if (refcount_release(&ks->ks_ref)) {
291 mac_posixsem_destroy(ks);
293 cv_destroy(&ks->ks_cv);
295 mtx_lock(&ksem_count_lock);
297 mtx_unlock(&ksem_count_lock);
302 * Determine if the credentials have sufficient permissions for read
306 ksem_access(struct ksem *ks, struct ucred *ucred)
310 error = vaccess(VREG, ks->ks_mode, ks->ks_uid, ks->ks_gid,
311 VREAD | VWRITE, ucred, NULL);
313 error = priv_check_cred(ucred, PRIV_SEM_WRITE, 0);
318 * Dictionary management. We maintain an in-kernel dictionary to map
319 * paths to semaphore objects. We use the FNV hash on the path to
320 * store the mappings in a hash table.
323 ksem_lookup(char *path, Fnv32_t fnv)
325 struct ksem_mapping *map;
327 LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
328 if (map->km_fnv != fnv)
330 if (strcmp(map->km_path, path) == 0)
331 return (map->km_ksem);
338 ksem_insert(char *path, Fnv32_t fnv, struct ksem *ks)
340 struct ksem_mapping *map;
342 map = malloc(sizeof(struct ksem_mapping), M_KSEM, M_WAITOK);
345 map->km_ksem = ksem_hold(ks);
346 LIST_INSERT_HEAD(KSEM_HASH(fnv), map, km_link);
350 ksem_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
352 struct ksem_mapping *map;
355 LIST_FOREACH(map, KSEM_HASH(fnv), km_link) {
356 if (map->km_fnv != fnv)
358 if (strcmp(map->km_path, path) == 0) {
360 error = mac_posixsem_check_unlink(ucred, map->km_ksem);
364 error = ksem_access(map->km_ksem, ucred);
367 LIST_REMOVE(map, km_link);
368 ksem_drop(map->km_ksem);
369 free(map->km_path, M_KSEM);
379 ksem_create_copyout_semid(struct thread *td, semid_t *semidp, int fd,
383 #ifdef COMPAT_FREEBSD32
389 #ifdef COMPAT_FREEBSD32
393 ptrs = sizeof(semid32);
398 ptrs = sizeof(semid);
399 compat32 = 0; /* silence gcc */
400 #ifdef COMPAT_FREEBSD32
404 return (copyout(ptr, semidp, ptrs));
407 /* Other helper routines. */
409 ksem_create(struct thread *td, const char *name, semid_t *semidp, mode_t mode,
410 unsigned int value, int flags, int compat32)
412 struct filedesc *fdp;
419 if (value > SEM_VALUE_MAX)
422 fdp = td->td_proc->p_fd;
423 mode = (mode & ~fdp->fd_cmask) & ACCESSPERMS;
424 error = falloc(td, &fp, &fd);
432 * Go ahead and copyout the file descriptor now. This is a bit
433 * premature, but it is a lot easier to handle errors as opposed
434 * to later when we've possibly created a new semaphore, etc.
436 error = ksem_create_copyout_semid(td, semidp, fd, compat32);
438 fdclose(fdp, fp, fd, td);
444 /* Create an anonymous semaphore. */
445 ks = ksem_alloc(td->td_ucred, mode, value);
449 ks->ks_flags |= KS_ANONYMOUS;
451 path = malloc(MAXPATHLEN, M_KSEM, M_WAITOK);
452 error = copyinstr(name, path, MAXPATHLEN, NULL);
454 /* Require paths to start with a '/' character. */
455 if (error == 0 && path[0] != '/')
458 fdclose(fdp, fp, fd, td);
464 fnv = fnv_32_str(path, FNV1_32_INIT);
465 sx_xlock(&ksem_dict_lock);
466 ks = ksem_lookup(path, fnv);
468 /* Object does not exist, create it if requested. */
469 if (flags & O_CREAT) {
470 ks = ksem_alloc(td->td_ucred, mode, value);
474 ksem_insert(path, fnv, ks);
481 * Object already exists, obtain a new
482 * reference if requested and permitted.
484 if ((flags & (O_CREAT | O_EXCL)) ==
489 error = mac_posixsem_check_open(td->td_ucred,
493 error = ksem_access(ks, td->td_ucred);
502 sx_xunlock(&ksem_dict_lock);
508 KASSERT(ks == NULL, ("ksem_create error with a ksem"));
509 fdclose(fdp, fp, fd, td);
513 KASSERT(ks != NULL, ("ksem_create w/o a ksem"));
515 finit(fp, FREAD | FWRITE, DTYPE_SEM, ks, &ksem_ops);
518 if (fdp->fd_ofiles[fd] == fp)
519 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
520 FILEDESC_XUNLOCK(fdp);
527 ksem_get(struct thread *td, semid_t id, struct file **fpp)
533 error = fget(td, id, &fp);
536 if (fp->f_type != DTYPE_SEM) {
541 if (ks->ks_flags & KS_DEAD) {
550 #ifndef _SYS_SYSPROTO_H_
551 struct ksem_init_args {
557 ksem_init(struct thread *td, struct ksem_init_args *uap)
560 return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
564 #ifndef _SYS_SYSPROTO_H_
565 struct ksem_open_args {
574 ksem_open(struct thread *td, struct ksem_open_args *uap)
577 DP((">>> ksem_open start, pid=%d\n", (int)td->td_proc->p_pid));
579 if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
581 return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
585 #ifndef _SYS_SYSPROTO_H_
586 struct ksem_unlink_args {
591 ksem_unlink(struct thread *td, struct ksem_unlink_args *uap)
597 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
598 error = copyinstr(uap->name, path, MAXPATHLEN, NULL);
604 fnv = fnv_32_str(path, FNV1_32_INIT);
605 sx_xlock(&ksem_dict_lock);
606 error = ksem_remove(path, fnv, td->td_ucred);
607 sx_xunlock(&ksem_dict_lock);
613 #ifndef _SYS_SYSPROTO_H_
614 struct ksem_close_args {
619 ksem_close(struct thread *td, struct ksem_close_args *uap)
625 error = ksem_get(td, uap->id, &fp);
629 if (ks->ks_flags & KS_ANONYMOUS) {
633 error = kern_close(td, uap->id);
638 #ifndef _SYS_SYSPROTO_H_
639 struct ksem_post_args {
644 ksem_post(struct thread *td, struct ksem_post_args *uap)
650 error = ksem_get(td, uap->id, &fp);
657 error = mac_posixsem_check_post(td->td_ucred, fp->f_cred, ks);
661 if (ks->ks_value == SEM_VALUE_MAX) {
666 if (ks->ks_waiters > 0)
667 cv_signal(&ks->ks_cv);
669 vfs_timestamp(&ks->ks_ctime);
671 mtx_unlock(&sem_lock);
676 #ifndef _SYS_SYSPROTO_H_
677 struct ksem_wait_args {
682 ksem_wait(struct thread *td, struct ksem_wait_args *uap)
685 return (kern_sem_wait(td, uap->id, 0, NULL));
688 #ifndef _SYS_SYSPROTO_H_
689 struct ksem_timedwait_args {
691 const struct timespec *abstime;
695 ksem_timedwait(struct thread *td, struct ksem_timedwait_args *uap)
697 struct timespec abstime;
702 * We allow a null timespec (wait forever).
704 if (uap->abstime == NULL)
707 error = copyin(uap->abstime, &abstime, sizeof(abstime));
710 if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
714 return (kern_sem_wait(td, uap->id, 0, ts));
717 #ifndef _SYS_SYSPROTO_H_
718 struct ksem_trywait_args {
723 ksem_trywait(struct thread *td, struct ksem_trywait_args *uap)
726 return (kern_sem_wait(td, uap->id, 1, NULL));
730 kern_sem_wait(struct thread *td, semid_t id, int tryflag,
731 struct timespec *abstime)
733 struct timespec ts1, ts2;
739 DP((">>> kern_sem_wait entered! pid=%d\n", (int)td->td_proc->p_pid));
740 error = ksem_get(td, id, &fp);
745 DP((">>> kern_sem_wait critical section entered! pid=%d\n",
746 (int)td->td_proc->p_pid));
748 error = mac_posixsem_check_wait(td->td_ucred, fp->f_cred, ks);
750 DP(("kern_sem_wait mac failed\n"));
754 DP(("kern_sem_wait value = %d, tryflag %d\n", ks->ks_value, tryflag));
755 vfs_timestamp(&ks->ks_atime);
756 while (ks->ks_value == 0) {
760 else if (abstime == NULL)
761 error = cv_wait_sig(&ks->ks_cv, &sem_lock);
766 timespecsub(&ts1, &ts2);
767 TIMESPEC_TO_TIMEVAL(&tv, &ts1);
772 error = cv_timedwait_sig(&ks->ks_cv,
773 &sem_lock, tvtohz(&tv));
774 if (error != EWOULDBLOCK)
783 DP(("kern_sem_wait value post-decrement = %d\n", ks->ks_value));
786 mtx_unlock(&sem_lock);
788 DP(("<<< kern_sem_wait leaving, pid=%d, error = %d\n",
789 (int)td->td_proc->p_pid, error));
793 #ifndef _SYS_SYSPROTO_H_
794 struct ksem_getvalue_args {
800 ksem_getvalue(struct thread *td, struct ksem_getvalue_args *uap)
806 error = ksem_get(td, uap->id, &fp);
813 error = mac_posixsem_check_getvalue(td->td_ucred, fp->f_cred, ks);
815 mtx_unlock(&sem_lock);
821 vfs_timestamp(&ks->ks_atime);
822 mtx_unlock(&sem_lock);
824 error = copyout(&val, uap->val, sizeof(val));
828 #ifndef _SYS_SYSPROTO_H_
829 struct ksem_destroy_args {
834 ksem_destroy(struct thread *td, struct ksem_destroy_args *uap)
840 error = ksem_get(td, uap->id, &fp);
844 if (!(ks->ks_flags & KS_ANONYMOUS)) {
849 if (ks->ks_waiters != 0) {
850 mtx_unlock(&sem_lock);
854 ks->ks_flags |= KS_DEAD;
855 mtx_unlock(&sem_lock);
857 error = kern_close(td, uap->id);
863 static struct syscall_helper_data ksem_syscalls[] = {
864 SYSCALL_INIT_HELPER(ksem_init),
865 SYSCALL_INIT_HELPER(ksem_open),
866 SYSCALL_INIT_HELPER(ksem_unlink),
867 SYSCALL_INIT_HELPER(ksem_close),
868 SYSCALL_INIT_HELPER(ksem_post),
869 SYSCALL_INIT_HELPER(ksem_wait),
870 SYSCALL_INIT_HELPER(ksem_timedwait),
871 SYSCALL_INIT_HELPER(ksem_trywait),
872 SYSCALL_INIT_HELPER(ksem_getvalue),
873 SYSCALL_INIT_HELPER(ksem_destroy),
877 #ifdef COMPAT_FREEBSD32
878 #include <compat/freebsd32/freebsd32.h>
879 #include <compat/freebsd32/freebsd32_proto.h>
880 #include <compat/freebsd32/freebsd32_signal.h>
881 #include <compat/freebsd32/freebsd32_syscall.h>
882 #include <compat/freebsd32/freebsd32_util.h>
885 freebsd32_ksem_init(struct thread *td, struct freebsd32_ksem_init_args *uap)
888 return (ksem_create(td, NULL, uap->idp, S_IRWXU | S_IRWXG, uap->value,
893 freebsd32_ksem_open(struct thread *td, struct freebsd32_ksem_open_args *uap)
896 if ((uap->oflag & ~(O_CREAT | O_EXCL)) != 0)
898 return (ksem_create(td, uap->name, uap->idp, uap->mode, uap->value,
903 freebsd32_ksem_timedwait(struct thread *td,
904 struct freebsd32_ksem_timedwait_args *uap)
906 struct timespec32 abstime32;
907 struct timespec *ts, abstime;
911 * We allow a null timespec (wait forever).
913 if (uap->abstime == NULL)
916 error = copyin(uap->abstime, &abstime32, sizeof(abstime32));
919 CP(abstime32, abstime, tv_sec);
920 CP(abstime32, abstime, tv_nsec);
921 if (abstime.tv_nsec >= 1000000000 || abstime.tv_nsec < 0)
925 return (kern_sem_wait(td, uap->id, 0, ts));
928 static struct syscall_helper_data ksem32_syscalls[] = {
929 SYSCALL32_INIT_HELPER(freebsd32_ksem_init),
930 SYSCALL32_INIT_HELPER(freebsd32_ksem_open),
931 SYSCALL32_INIT_HELPER(ksem_unlink),
932 SYSCALL32_INIT_HELPER(ksem_close),
933 SYSCALL32_INIT_HELPER(ksem_post),
934 SYSCALL32_INIT_HELPER(ksem_wait),
935 SYSCALL32_INIT_HELPER(freebsd32_ksem_timedwait),
936 SYSCALL32_INIT_HELPER(ksem_trywait),
937 SYSCALL32_INIT_HELPER(ksem_getvalue),
938 SYSCALL32_INIT_HELPER(ksem_destroy),
944 ksem_module_init(void)
948 mtx_init(&sem_lock, "sem", NULL, MTX_DEF);
949 mtx_init(&ksem_count_lock, "ksem count", NULL, MTX_DEF);
950 sx_init(&ksem_dict_lock, "ksem dictionary");
951 ksem_dictionary = hashinit(1024, M_KSEM, &ksem_hash);
952 p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, SEM_MAX);
953 p31b_setcfg(CTL_P1003_1B_SEM_VALUE_MAX, SEM_VALUE_MAX);
955 error = syscall_helper_register(ksem_syscalls);
958 #ifdef COMPAT_FREEBSD32
959 error = syscall32_helper_register(ksem32_syscalls);
967 ksem_module_destroy(void)
970 #ifdef COMPAT_FREEBSD32
971 syscall32_helper_unregister(ksem32_syscalls);
973 syscall_helper_unregister(ksem_syscalls);
975 hashdestroy(ksem_dictionary, M_KSEM, ksem_hash);
976 sx_destroy(&ksem_dict_lock);
977 mtx_destroy(&ksem_count_lock);
978 mtx_destroy(&sem_lock);
982 sem_modload(struct module *module, int cmd, void *arg)
988 error = ksem_module_init();
990 ksem_module_destroy();
994 mtx_lock(&ksem_count_lock);
997 mtx_unlock(&ksem_count_lock);
1001 mtx_unlock(&ksem_count_lock);
1002 ksem_module_destroy();
1014 static moduledata_t sem_mod = {
1020 DECLARE_MODULE(sem, sem_mod, SI_SUB_SYSV_SEM, SI_ORDER_FIRST);
1021 MODULE_VERSION(sem, 1);