xref: /freebsd/sys/security/mac/mac_posix_sem.c (revision 83825b7109e5dd034c48dfba10503ecfd2002feb)
1 /*-
2  * Copyright (c) 2003-2006 SPARTA, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project in part by Network
6  * Associates Laboratories, the Security Research Division of Network
7  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
8  * as part of the DARPA CHATS research program.
9  *
10  * This software was enhanced by SPARTA ISSO under SPAWAR contract
11  * N66001-04-C-6019 ("SEFOS").
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_mac.h"
39 #include "opt_posix.h"
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/ksem.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/systm.h>
47 #include <sys/sysctl.h>
48 
49 #include <security/mac/mac_framework.h>
50 #include <security/mac/mac_internal.h>
51 #include <security/mac/mac_policy.h>
52 
53 static struct label *
54 mac_posixsem_label_alloc(void)
55 {
56 	struct label *label;
57 
58 	label = mac_labelzone_alloc(M_WAITOK);
59 	MAC_PERFORM(posixsem_init_label, label);
60 	return (label);
61 }
62 
63 void
64 mac_posixsem_init(struct ksem *ks)
65 {
66 
67 	ks->ks_label = mac_posixsem_label_alloc();
68 }
69 
70 static void
71 mac_posixsem_label_free(struct label *label)
72 {
73 
74 	MAC_PERFORM(posixsem_destroy_label, label);
75 	mac_labelzone_free(label);
76 }
77 
78 void
79 mac_posixsem_destroy(struct ksem *ks)
80 {
81 
82 	mac_posixsem_label_free(ks->ks_label);
83 	ks->ks_label = NULL;
84 }
85 
86 void
87 mac_posixsem_create(struct ucred *cred, struct ksem *ks)
88 {
89 
90 	MAC_PERFORM(posixsem_create, cred, ks, ks->ks_label);
91 }
92 
93 int
94 mac_posixsem_check_open(struct ucred *cred, struct ksem *ks)
95 {
96 	int error;
97 
98 	MAC_CHECK(posixsem_check_open, cred, ks, ks->ks_label);
99 
100 	return (error);
101 }
102 
103 int
104 mac_posixsem_check_getvalue(struct ucred *active_cred, struct ucred *file_cred,
105     struct ksem *ks)
106 {
107 	int error;
108 
109 	MAC_CHECK(posixsem_check_getvalue, active_cred, file_cred, ks,
110 	    ks->ks_label);
111 
112 	return (error);
113 }
114 
115 int
116 mac_posixsem_check_post(struct ucred *active_cred, struct ucred *file_cred,
117     struct ksem *ks)
118 {
119 	int error;
120 
121 	MAC_CHECK(posixsem_check_post, active_cred, file_cred, ks,
122 	    ks->ks_label);
123 
124 	return (error);
125 }
126 
127 int
128 mac_posixsem_check_stat(struct ucred *active_cred, struct ucred *file_cred,
129     struct ksem *ks)
130 {
131 	int error;
132 
133 	MAC_CHECK(posixsem_check_stat, active_cred, file_cred, ks,
134 	    ks->ks_label);
135 
136 	return (error);
137 }
138 
139 int
140 mac_posixsem_check_unlink(struct ucred *cred, struct ksem *ks)
141 {
142 	int error;
143 
144 	MAC_CHECK(posixsem_check_unlink, cred, ks, ks->ks_label);
145 
146 	return (error);
147 }
148 
149 int
150 mac_posixsem_check_wait(struct ucred *active_cred, struct ucred *file_cred,
151     struct ksem *ks)
152 {
153 	int error;
154 
155 	MAC_CHECK(posixsem_check_wait, active_cred, file_cred, ks,
156 	    ks->ks_label);
157 
158 	return (error);
159 }
160