secmodel.c revision 1.1.4.2 1 1.1.4.2 mrg /* $NetBSD: secmodel.c,v 1.1.4.2 2012/02/18 07:35:46 mrg Exp $ */
2 1.1.4.2 mrg /*-
3 1.1.4.2 mrg * Copyright (c) 2011 Elad Efrat <elad (at) NetBSD.org>
4 1.1.4.2 mrg * All rights reserved.
5 1.1.4.2 mrg *
6 1.1.4.2 mrg * Redistribution and use in source and binary forms, with or without
7 1.1.4.2 mrg * modification, are permitted provided that the following conditions
8 1.1.4.2 mrg * are met:
9 1.1.4.2 mrg * 1. Redistributions of source code must retain the above copyright
10 1.1.4.2 mrg * notice, this list of conditions and the following disclaimer.
11 1.1.4.2 mrg * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.4.2 mrg * notice, this list of conditions and the following disclaimer in the
13 1.1.4.2 mrg * documentation and/or other materials provided with the distribution.
14 1.1.4.2 mrg * 3. The name of the author may not be used to endorse or promote products
15 1.1.4.2 mrg * derived from this software without specific prior written permission.
16 1.1.4.2 mrg *
17 1.1.4.2 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1.4.2 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1.4.2 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1.4.2 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1.4.2 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1.4.2 mrg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1.4.2 mrg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1.4.2 mrg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1.4.2 mrg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.1.4.2 mrg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1.4.2 mrg */
28 1.1.4.2 mrg
29 1.1.4.2 mrg #include <sys/types.h>
30 1.1.4.2 mrg #include <sys/param.h>
31 1.1.4.2 mrg #include <sys/errno.h>
32 1.1.4.2 mrg
33 1.1.4.2 mrg #include <sys/atomic.h>
34 1.1.4.2 mrg #include <sys/kauth.h>
35 1.1.4.2 mrg #include <sys/kmem.h>
36 1.1.4.2 mrg #include <sys/queue.h>
37 1.1.4.2 mrg #include <sys/rwlock.h>
38 1.1.4.2 mrg #include <secmodel/secmodel.h>
39 1.1.4.2 mrg #include <prop/proplib.h>
40 1.1.4.2 mrg
41 1.1.4.2 mrg /* List of secmodels, parameters, and lock. */
42 1.1.4.2 mrg static LIST_HEAD(, secmodel_descr) secmodels =
43 1.1.4.2 mrg LIST_HEAD_INITIALIZER(secmodels);
44 1.1.4.2 mrg static unsigned int secmodel_copy_cred_on_fork = false;
45 1.1.4.2 mrg static krwlock_t secmodels_lock;
46 1.1.4.2 mrg static int nsecmodels = 0; /* number of registered secmodels */
47 1.1.4.2 mrg
48 1.1.4.2 mrg static int secmodel_plug(secmodel_t);
49 1.1.4.2 mrg static int secmodel_unplug(secmodel_t);
50 1.1.4.2 mrg
51 1.1.4.2 mrg int
52 1.1.4.2 mrg secmodel_nsecmodels(void)
53 1.1.4.2 mrg {
54 1.1.4.2 mrg
55 1.1.4.2 mrg return nsecmodels;
56 1.1.4.2 mrg }
57 1.1.4.2 mrg
58 1.1.4.2 mrg void
59 1.1.4.2 mrg secmodel_init(void)
60 1.1.4.2 mrg {
61 1.1.4.2 mrg
62 1.1.4.2 mrg rw_init(&secmodels_lock);
63 1.1.4.2 mrg
64 1.1.4.2 mrg secmodel_copy_cred_on_fork = false;
65 1.1.4.2 mrg }
66 1.1.4.2 mrg
67 1.1.4.2 mrg /*
68 1.1.4.2 mrg * Register a new secmodel.
69 1.1.4.2 mrg */
70 1.1.4.2 mrg int
71 1.1.4.2 mrg secmodel_register(secmodel_t *secmodel, const char *id, const char *name,
72 1.1.4.2 mrg prop_dictionary_t behavior,
73 1.1.4.2 mrg secmodel_eval_t eval, secmodel_setinfo_t setinfo)
74 1.1.4.2 mrg {
75 1.1.4.2 mrg int err;
76 1.1.4.2 mrg secmodel_t sm;
77 1.1.4.2 mrg
78 1.1.4.2 mrg sm = kmem_alloc(sizeof(*sm), KM_SLEEP);
79 1.1.4.2 mrg
80 1.1.4.2 mrg sm->sm_id = id;
81 1.1.4.2 mrg sm->sm_name = name;
82 1.1.4.2 mrg sm->sm_behavior = behavior;
83 1.1.4.2 mrg sm->sm_eval = eval;
84 1.1.4.2 mrg sm->sm_setinfo = setinfo;
85 1.1.4.2 mrg
86 1.1.4.2 mrg err = secmodel_plug(sm);
87 1.1.4.2 mrg if (err == 0) {
88 1.1.4.2 mrg atomic_inc_uint(&nsecmodels);
89 1.1.4.2 mrg } else {
90 1.1.4.2 mrg kmem_free(sm, sizeof(*sm));
91 1.1.4.2 mrg sm = NULL;
92 1.1.4.2 mrg }
93 1.1.4.2 mrg
94 1.1.4.2 mrg *secmodel = sm;
95 1.1.4.2 mrg return err;
96 1.1.4.2 mrg }
97 1.1.4.2 mrg
98 1.1.4.2 mrg /*
99 1.1.4.2 mrg * Deregister a secmodel.
100 1.1.4.2 mrg */
101 1.1.4.2 mrg int
102 1.1.4.2 mrg secmodel_deregister(secmodel_t sm)
103 1.1.4.2 mrg {
104 1.1.4.2 mrg int error;
105 1.1.4.2 mrg
106 1.1.4.2 mrg error = secmodel_unplug(sm);
107 1.1.4.2 mrg if (error == 0) {
108 1.1.4.2 mrg atomic_dec_uint(&nsecmodels);
109 1.1.4.2 mrg kmem_free(sm, sizeof(*sm));
110 1.1.4.2 mrg }
111 1.1.4.2 mrg
112 1.1.4.2 mrg return error;
113 1.1.4.2 mrg }
114 1.1.4.2 mrg
115 1.1.4.2 mrg /*
116 1.1.4.2 mrg * Lookup a secmodel by its id.
117 1.1.4.2 mrg *
118 1.1.4.2 mrg * Requires "secmodels_lock" handling by the caller.
119 1.1.4.2 mrg */
120 1.1.4.2 mrg static secmodel_t
121 1.1.4.2 mrg secmodel_lookup(const char *id)
122 1.1.4.2 mrg {
123 1.1.4.2 mrg secmodel_t tsm;
124 1.1.4.2 mrg
125 1.1.4.2 mrg KASSERT(rw_lock_held(&secmodels_lock));
126 1.1.4.2 mrg
127 1.1.4.2 mrg LIST_FOREACH(tsm, &secmodels, sm_list) {
128 1.1.4.2 mrg if (strcasecmp(tsm->sm_id, id) == 0) {
129 1.1.4.2 mrg return tsm;
130 1.1.4.2 mrg }
131 1.1.4.2 mrg }
132 1.1.4.2 mrg
133 1.1.4.2 mrg return NULL;
134 1.1.4.2 mrg }
135 1.1.4.2 mrg
136 1.1.4.2 mrg /*
137 1.1.4.2 mrg * Adjust system-global secmodel behavior following the addition
138 1.1.4.2 mrg * or removal of a secmodel.
139 1.1.4.2 mrg *
140 1.1.4.2 mrg * Requires "secmodels_lock" to be held by the caller.
141 1.1.4.2 mrg */
142 1.1.4.2 mrg static void
143 1.1.4.2 mrg secmodel_adjust_behavior(secmodel_t sm, bool added)
144 1.1.4.2 mrg {
145 1.1.4.2 mrg bool r, b;
146 1.1.4.2 mrg
147 1.1.4.2 mrg KASSERT(rw_write_held(&secmodels_lock));
148 1.1.4.2 mrg
149 1.1.4.2 mrg #define ADJUST_COUNTER(which, added) \
150 1.1.4.2 mrg do { \
151 1.1.4.2 mrg if (added) { \
152 1.1.4.2 mrg (which)++; \
153 1.1.4.2 mrg } else { \
154 1.1.4.2 mrg if ((which) > 0) \
155 1.1.4.2 mrg (which)--; \
156 1.1.4.2 mrg } \
157 1.1.4.2 mrg } while (/*CONSTCOND*/0)
158 1.1.4.2 mrg
159 1.1.4.2 mrg /* Copy credentials on fork? */
160 1.1.4.2 mrg r = prop_dictionary_get_bool(sm->sm_behavior, "copy-cred-on-fork", &b);
161 1.1.4.2 mrg if (r) {
162 1.1.4.2 mrg ADJUST_COUNTER(secmodel_copy_cred_on_fork, added);
163 1.1.4.2 mrg }
164 1.1.4.2 mrg
165 1.1.4.2 mrg #undef ADJUST_COUNTER
166 1.1.4.2 mrg }
167 1.1.4.2 mrg
168 1.1.4.2 mrg static int
169 1.1.4.2 mrg secmodel_plug(secmodel_t sm)
170 1.1.4.2 mrg {
171 1.1.4.2 mrg secmodel_t tsm;
172 1.1.4.2 mrg int error = 0;
173 1.1.4.2 mrg
174 1.1.4.2 mrg if (sm == NULL) {
175 1.1.4.2 mrg error = EFAULT;
176 1.1.4.2 mrg goto out;
177 1.1.4.2 mrg }
178 1.1.4.2 mrg
179 1.1.4.2 mrg /* Check if the secmodel is already present. */
180 1.1.4.2 mrg rw_enter(&secmodels_lock, RW_WRITER);
181 1.1.4.2 mrg tsm = secmodel_lookup(sm->sm_id);
182 1.1.4.2 mrg if (tsm != NULL) {
183 1.1.4.2 mrg error = EEXIST;
184 1.1.4.2 mrg goto out;
185 1.1.4.2 mrg }
186 1.1.4.2 mrg
187 1.1.4.2 mrg /* Add the secmodel. */
188 1.1.4.2 mrg LIST_INSERT_HEAD(&secmodels, sm, sm_list);
189 1.1.4.2 mrg
190 1.1.4.2 mrg /* Adjust behavior. */
191 1.1.4.2 mrg secmodel_adjust_behavior(sm, true);
192 1.1.4.2 mrg
193 1.1.4.2 mrg out:
194 1.1.4.2 mrg /* Unlock the secmodels list. */
195 1.1.4.2 mrg rw_exit(&secmodels_lock);
196 1.1.4.2 mrg
197 1.1.4.2 mrg return error;
198 1.1.4.2 mrg }
199 1.1.4.2 mrg
200 1.1.4.2 mrg static int
201 1.1.4.2 mrg secmodel_unplug(secmodel_t sm)
202 1.1.4.2 mrg {
203 1.1.4.2 mrg secmodel_t tsm;
204 1.1.4.2 mrg int error = 0;
205 1.1.4.2 mrg
206 1.1.4.2 mrg if (sm == NULL) {
207 1.1.4.2 mrg error = EFAULT;
208 1.1.4.2 mrg goto out;
209 1.1.4.2 mrg }
210 1.1.4.2 mrg
211 1.1.4.2 mrg /* Make sure the secmodel is present. */
212 1.1.4.2 mrg rw_enter(&secmodels_lock, RW_WRITER);
213 1.1.4.2 mrg tsm = secmodel_lookup(sm->sm_id);
214 1.1.4.2 mrg if (tsm == NULL) {
215 1.1.4.2 mrg error = ENOENT;
216 1.1.4.2 mrg goto out;
217 1.1.4.2 mrg }
218 1.1.4.2 mrg
219 1.1.4.2 mrg /* Remove the secmodel. */
220 1.1.4.2 mrg LIST_REMOVE(tsm, sm_list);
221 1.1.4.2 mrg
222 1.1.4.2 mrg /* Adjust behavior. */
223 1.1.4.2 mrg secmodel_adjust_behavior(tsm, false);
224 1.1.4.2 mrg
225 1.1.4.2 mrg out:
226 1.1.4.2 mrg /* Unlock the secmodels list. */
227 1.1.4.2 mrg rw_exit(&secmodels_lock);
228 1.1.4.2 mrg
229 1.1.4.2 mrg return error;
230 1.1.4.2 mrg }
231 1.1.4.2 mrg
232 1.1.4.2 mrg /* XXX TODO */
233 1.1.4.2 mrg int
234 1.1.4.2 mrg secmodel_setinfo(const char *id, void *v, int *err)
235 1.1.4.2 mrg {
236 1.1.4.2 mrg
237 1.1.4.2 mrg return EOPNOTSUPP;
238 1.1.4.2 mrg }
239 1.1.4.2 mrg
240 1.1.4.2 mrg int
241 1.1.4.2 mrg secmodel_eval(const char *id, const char *what, void *arg, void *ret)
242 1.1.4.2 mrg {
243 1.1.4.2 mrg secmodel_t sm;
244 1.1.4.2 mrg int error = 0;
245 1.1.4.2 mrg
246 1.1.4.2 mrg rw_enter(&secmodels_lock, RW_READER);
247 1.1.4.2 mrg sm = secmodel_lookup(id);
248 1.1.4.2 mrg if (sm == NULL) {
249 1.1.4.2 mrg error = EINVAL;
250 1.1.4.2 mrg goto out;
251 1.1.4.2 mrg }
252 1.1.4.2 mrg
253 1.1.4.2 mrg if (sm->sm_eval == NULL) {
254 1.1.4.2 mrg error = ENOENT;
255 1.1.4.2 mrg goto out;
256 1.1.4.2 mrg }
257 1.1.4.2 mrg
258 1.1.4.2 mrg if (ret == NULL) {
259 1.1.4.2 mrg error = EFAULT;
260 1.1.4.2 mrg goto out;
261 1.1.4.2 mrg }
262 1.1.4.2 mrg
263 1.1.4.2 mrg error = sm->sm_eval(what, arg, ret);
264 1.1.4.2 mrg /* pass error from a secmodel(9) callback as a negative value */
265 1.1.4.2 mrg error = -error;
266 1.1.4.2 mrg
267 1.1.4.2 mrg out:
268 1.1.4.2 mrg rw_exit(&secmodels_lock);
269 1.1.4.2 mrg
270 1.1.4.2 mrg return error;
271 1.1.4.2 mrg }
272