xref: /illumos-gate/usr/src/uts/common/crypto/core/kcf.c (revision 8a8d276f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Core KCF (Kernel Cryptographic Framework). This file implements
30  * the loadable module entry points and module verification routines.
31  */
32 
33 #include <sys/systm.h>
34 #include <sys/cmn_err.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/modctl.h>
38 #include <sys/errno.h>
39 #include <sys/rwlock.h>
40 #include <sys/kmem.h>
41 #include <sys/door.h>
42 #include <sys/kobj.h>
43 
44 #include <sys/crypto/common.h>
45 #include <sys/crypto/api.h>
46 #include <sys/crypto/spi.h>
47 #include <sys/crypto/impl.h>
48 #include <sys/crypto/sched_impl.h>
49 #include <sys/crypto/elfsign.h>
50 
51 #ifdef DEBUG
52 int kcf_frmwrk_debug = 0;
53 
54 #define	KCF_FRMWRK_DEBUG(l, x)	if (kcf_frmwrk_debug >= l) printf x
55 #else	/* DEBUG */
56 #define	KCF_FRMWRK_DEBUG(l, x)
57 #endif	/* DEBUG */
58 
59 /*
60  * Door to make upcalls to kcfd. kcfd will send us this
61  * handle when it is coming up.
62  */
63 kmutex_t kcf_dh_lock;
64 door_handle_t kcf_dh = NULL;
65 
66 
67 static struct modlmisc modlmisc = {
68 	&mod_miscops, "Kernel Crypto Framework %I%"
69 };
70 
71 static struct modlinkage modlinkage = {
72 	MODREV_1, (void *)&modlmisc, NULL
73 };
74 
75 static int rngtimer_started;
76 
77 
78 int
79 _init()
80 {
81 	/* initialize the mechanisms tables supported out-of-the-box */
82 	kcf_init_mech_tabs();
83 
84 	/* initialize the providers tables */
85 	kcf_prov_tab_init();
86 
87 	/* initialize the policy table */
88 	kcf_policy_tab_init();
89 
90 	/* initialize soft_config_list */
91 	kcf_soft_config_init();
92 
93 	/*
94 	 * Initialize scheduling structures. Note that this does NOT
95 	 * start any threads since it might not be safe to do so.
96 	 */
97 	kcf_sched_init();
98 
99 	/* initialize the RNG support structures */
100 	rngtimer_started = 0;
101 	kcf_rnd_init();
102 
103 	return (mod_install(&modlinkage));
104 }
105 
106 int
107 _info(struct modinfo *modinfop)
108 {
109 	return (mod_info(&modlinkage, modinfop));
110 }
111 
112 /*
113  * We do not allow kcf to unload.
114  */
115 int
116 _fini(void)
117 {
118 	return (EBUSY);
119 }
120 
121 /*
122  * Return a pointer to the modctl structure of the
123  * provider's module.
124  */
125 struct modctl *
126 kcf_get_modctl(crypto_provider_info_t *pinfo)
127 {
128 	struct modctl *mctlp;
129 
130 	/* Get the modctl struct for this module */
131 	if (pinfo->pi_provider_type == CRYPTO_SW_PROVIDER)
132 		mctlp = mod_getctl(pinfo->pi_provider_dev.pd_sw);
133 	else {
134 		major_t major;
135 		char *drvmod;
136 
137 		if ((major =
138 		    ddi_driver_major(pinfo->pi_provider_dev.pd_hw)) != -1) {
139 			drvmod = ddi_major_to_name(major);
140 			mctlp = mod_find_by_filename("drv", drvmod);
141 		} else
142 			return (NULL);
143 	}
144 
145 	return (mctlp);
146 }
147 
148 /*
149  * Check if signature verification is needed for a provider.
150  *
151  * Returns 0, if no verification is needed. Returns 1, if
152  * verification is needed. Returns -1, if there is an
153  * error.
154  */
155 int
156 kcf_need_signature_verification(kcf_provider_desc_t *pd)
157 {
158 	struct module *mp;
159 	struct modctl *mctlp = pd->pd_mctlp;
160 	crypto_ops_t *prov_ops = pd->pd_ops_vector;
161 
162 	if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
163 		return (0);
164 
165 	if (mctlp == NULL || mctlp->mod_mp == NULL)
166 		return (-1);
167 
168 	mp = (struct module *)mctlp->mod_mp;
169 
170 	/*
171 	 * Check if this provider needs to be verified. We always verify
172 	 * the module if it carries a signature. Any operation set which has
173 	 * a encryption/decryption component is a candidate for verification.
174 	 */
175 	if (prov_ops->co_cipher_ops == NULL && prov_ops->co_dual_ops == NULL &&
176 	    prov_ops->co_dual_cipher_mac_ops == NULL &&
177 	    prov_ops->co_key_ops == NULL && prov_ops->co_sign_ops == NULL &&
178 	    prov_ops->co_verify_ops == NULL && mp->sigdata == NULL) {
179 		return (0);
180 	}
181 
182 	/*
183 	 * See if this module has a proper signature section.
184 	 */
185 	if (mp->sigdata == NULL) {
186 		return (-1);
187 	}
188 
189 	mutex_enter(&pd->pd_lock);
190 	pd->pd_state = KCF_PROV_UNVERIFIED;
191 	mutex_exit(&pd->pd_lock);
192 
193 	return (1);
194 }
195 
196 /*
197  * Do the signature verification on the given module. This function can
198  * be called from user context or kernel context.
199  *
200  * We call kcfd with the full pathname of the module to be
201  * verified. kcfd will return success/restricted/fail, signature length
202  * and the actual signature in the ELF section of the module. If kcfd
203  * returns success or restricted, we compare the signature and the length
204  * with the values that krtld stored in the module structure. We log an
205  * error message in case of a failure.
206  *
207  * The provider state is changed to KCF_PROV_READY on success.
208  */
209 void
210 kcf_verify_signature(void *arg)
211 {
212 	int rv;
213 	int error = CRYPTO_MODVERIFICATION_FAILED;
214 	door_arg_t darg;
215 	door_handle_t ldh;
216 	kcf_door_arg_t *kda;
217 	char *filename;
218 	kcf_provider_desc_t *pd = arg;
219 	struct module *mp;
220 	boolean_t do_notify = B_FALSE;
221 	boolean_t modhold_done = B_FALSE;
222 	struct modctl *mctlp = pd->pd_mctlp;
223 
224 	ASSERT(pd->pd_prov_type != CRYPTO_LOGICAL_PROVIDER);
225 	ASSERT(mctlp != NULL);
226 
227 	for (;;) {
228 		mutex_enter(&pd->pd_lock);
229 		/* No need to do verification */
230 		if (pd->pd_state != KCF_PROV_UNVERIFIED) {
231 			mutex_exit(&pd->pd_lock);
232 			goto out;
233 		}
234 		mutex_exit(&pd->pd_lock);
235 
236 		mutex_enter(&mod_lock);
237 		if (mctlp->mod_mp == NULL) {
238 			mutex_exit(&mod_lock);
239 			goto out;
240 		}
241 
242 		/*
243 		 * This check is needed since a software provider can call
244 		 * us directly from the _init->crypto_register_provider path.
245 		 */
246 		if (pd->pd_prov_type == CRYPTO_SW_PROVIDER &&
247 		    mctlp->mod_inprogress_thread == curthread) {
248 			mutex_exit(&mod_lock);
249 			modhold_done = B_FALSE;
250 			break;
251 		}
252 
253 		/*
254 		 * We could be in a race with the register thread or
255 		 * the unregister thread. So, retry if register or
256 		 * unregister is in progress. Note that we can't do
257 		 * mod_hold_by_modctl without this check since that
258 		 * could result in a deadlock with the other threads.
259 		 */
260 		if (mctlp->mod_busy) {
261 			mutex_exit(&mod_lock);
262 			/* delay for 10ms and try again */
263 			delay(drv_usectohz(10000));
264 			continue;
265 		}
266 
267 		(void) mod_hold_by_modctl(mctlp,
268 		    MOD_WAIT_FOREVER | MOD_LOCK_HELD);
269 		mutex_exit(&mod_lock);
270 		modhold_done = B_TRUE;
271 		break;
272 	}
273 
274 	/*
275 	 * Check if the door is set up yet. This will be set when kcfd
276 	 * comes up. If not, we return and leave the provider state unchanged
277 	 * at KCF_PROV_UNVERIFIED. This will trigger the verification of
278 	 * the module later when kcfd is up. This is safe as we NEVER use
279 	 * a provider that has not been verified yet.
280 	 */
281 	mutex_enter(&kcf_dh_lock);
282 	if (kcf_dh == NULL) {
283 		mutex_exit(&kcf_dh_lock);
284 		goto out;
285 	}
286 
287 	ldh = kcf_dh;
288 	door_ki_hold(ldh);
289 	mutex_exit(&kcf_dh_lock);
290 
291 	mp = (struct module *)mctlp->mod_mp;
292 	filename = mp->filename;
293 	KCF_FRMWRK_DEBUG(2, ("Verifying module: %s\n", filename));
294 
295 	kda = kmem_alloc(sizeof (kcf_door_arg_t) + mp->sigsize, KM_SLEEP);
296 	kda->da_version = KCF_KCFD_VERSION1;
297 	kda->da_iskernel = B_TRUE;
298 	bcopy(filename, kda->da_u.filename, strlen(filename) + 1);
299 
300 	darg.data_ptr = (char *)kda;
301 	darg.data_size = sizeof (kcf_door_arg_t) + mp->sigsize;
302 	darg.desc_ptr = NULL;
303 	darg.desc_num = 0;
304 	darg.rbuf = (char *)kda;
305 	darg.rsize = sizeof (kcf_door_arg_t);
306 
307 	/*
308 	 * Make door upcall. door_ki_upcall() checks for validity of the handle.
309 	 */
310 	rv = door_ki_upcall(ldh, &darg);
311 
312 	if (rv == 0) {
313 		kcf_door_arg_t *rkda =  (kcf_door_arg_t *)darg.rbuf;
314 
315 		KCF_FRMWRK_DEBUG(2,
316 		    ("passed: %d\n", rkda->da_u.result.status));
317 		KCF_FRMWRK_DEBUG(2,
318 		    ("signature length: %d\n", rkda->da_u.result.siglen));
319 		KCF_FRMWRK_DEBUG(2,
320 		    ("signature: %p\n", (void*)rkda->da_u.result.signature));
321 
322 
323 		/* Check kcfd result and compare against module struct fields */
324 		if (((rkda->da_u.result.status != ELFSIGN_SUCCESS) &&
325 		    (rkda->da_u.result.status != ELFSIGN_RESTRICTED)) ||
326 		    !(rkda->da_u.result.siglen == mp->sigsize) ||
327 		    (bcmp(rkda->da_u.result.signature, mp->sigdata,
328 		    mp->sigsize))) {
329 			cmn_err(CE_WARN, "Module verification failed for %s.",
330 			    filename);
331 		} else {
332 			error = 0;
333 		}
334 
335 		if (rkda->da_u.result.status == ELFSIGN_RESTRICTED) {
336 			pd->pd_flags |= KCF_PROV_RESTRICTED;
337 			KCF_FRMWRK_DEBUG(2, ("provider is restricted\n"));
338 		}
339 
340 		if (rkda != kda)
341 			kmem_free(rkda, darg.rsize);
342 
343 	} else {
344 		cmn_err(CE_WARN, "Module verification door upcall failed "
345 		    "for %s. errno = %d", filename, rv);
346 	}
347 
348 	kmem_free(kda, sizeof (kcf_door_arg_t) + mp->sigsize);
349 	door_ki_rele(ldh);
350 
351 	mutex_enter(&pd->pd_lock);
352 	/* change state only if the original state is unchanged */
353 	if (pd->pd_state == KCF_PROV_UNVERIFIED) {
354 		if (error == 0) {
355 			pd->pd_state = KCF_PROV_READY;
356 			do_notify = B_TRUE;
357 		} else {
358 			pd->pd_state = KCF_PROV_VERIFICATION_FAILED;
359 		}
360 	}
361 	mutex_exit(&pd->pd_lock);
362 
363 	if (do_notify) {
364 		/* Dispatch events for this new provider */
365 		kcf_do_notify(pd, B_TRUE);
366 	}
367 
368 out:
369 	if (modhold_done)
370 		mod_release_mod(mctlp);
371 	KCF_PROV_IREFRELE(pd);
372 	KCF_PROV_REFRELE(pd);
373 }
374 
375 /* called from the CRYPTO_LOAD_DOOR ioctl */
376 int
377 crypto_load_door(uint_t did)
378 {
379 	mutex_enter(&kcf_dh_lock);
380 	kcf_dh = door_ki_lookup(did);
381 	mutex_exit(&kcf_dh_lock);
382 
383 	verify_unverified_providers();
384 
385 	/* Start the timeout handler to get random numbers */
386 	if (rngtimer_started == 0) {
387 		kcf_rnd_schedule_timeout(B_TRUE);
388 		rngtimer_started = 1;
389 	}
390 
391 	return (0);
392 }
393