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.
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.
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]
22 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include <sys/systm.h>
34 #include <sys/vnode.h>
35 #include <sys/socket.h>
38 #include <sys/tiuser.h>
40 #include <sys/errno.h>
41 #include <sys/debug.h>
43 #include <sys/kstat.h>
44 #include <sys/cmn_err.h>
45 #include <sys/vtrace.h>
46 #include <sys/session.h>
48 #include <sys/bitmap.h>
49 #include <sys/thread.h>
50 #include <sys/policy.h>
52 #include <netinet/in.h>
53 #include <rpc/types.h>
56 #include <rpc/auth_des.h> /* for authdes_create() */
58 #include <rpc/rpcsec_gss.h>
63 * Currently there is no maximum length defined withing the gss
64 * specification. Because of security issues the maximum gss
65 * authentication length is checked to be under the MAXAUTHLEN
66 * defined below. The value was chosen because it will be a safe
67 * maximum value for some time. Currently lengths are generally
68 * under the 16 byte length
70 #define MINAUTHLEN 1 /* minimum gss authentication length */
71 #define MAXAUTHLEN 65535 /* maximum gss authentication length */
72 static int clnt_authdes_cachesz
= 64;
74 static uint_t authdes_win
= 5*60; /* 5 minutes -- should be mount option */
76 struct kmem_cache
*authkern_cache
;
78 struct kmem_cache
*authnone_cache
;
80 struct kmem_cache
*authloopback_cache
;
82 static struct desauthent
{
83 struct sec_data
*da_data
;
89 static int nextdesvictim
;
90 static kmutex_t desauthtab_lock
; /* Lock to protect DES auth cache */
93 kmutex_t authdes_ops_lock
; /* auth_ops initialization in authdes_ops() */
95 static void purge_authtab(struct sec_data
*);
98 zone_key_t auth_zone_key
;
101 * Load RPCSEC_GSS specific data from user space to kernel space.
105 gss_clnt_loadinfo(caddr_t usrdata
, caddr_t
*kdata
, model_t model
)
107 struct gss_clnt_data
*data
;
111 /* map opaque data to gss specific structure */
112 data
= kmem_alloc(sizeof (*data
), KM_SLEEP
);
114 #ifdef _SYSCALL32_IMPL
115 if (model
!= DATAMODEL_NATIVE
) {
116 struct gss_clnt_data32 gd32
;
118 if (copyin(usrdata
, &gd32
, sizeof (gd32
)) == -1) {
121 data
->mechanism
.length
= gd32
.mechanism
.length
;
122 data
->mechanism
.elements
=
123 (caddr_t
)(uintptr_t)gd32
.mechanism
.elements
;
124 data
->service
= gd32
.service
;
125 bcopy(gd32
.uname
, data
->uname
, sizeof (gd32
.uname
));
126 bcopy(gd32
.inst
, data
->inst
, sizeof (gd32
.inst
));
127 bcopy(gd32
.realm
, data
->realm
, sizeof (gd32
.realm
));
128 data
->qop
= gd32
.qop
;
131 #endif /* _SYSCALL32_IMPL */
132 if (copyin(usrdata
, data
, sizeof (*data
)))
136 if (data
->mechanism
.length
>= MINAUTHLEN
&&
137 data
->mechanism
.length
<= MAXAUTHLEN
) {
138 elements
= kmem_alloc(data
->mechanism
.length
, KM_SLEEP
);
139 if (!(copyin(data
->mechanism
.elements
, elements
,
140 data
->mechanism
.length
))) {
141 data
->mechanism
.elements
= elements
;
142 *kdata
= (caddr_t
)data
;
145 kmem_free(elements
, data
->mechanism
.length
);
149 kmem_free(data
, sizeof (*data
));
156 * Load AUTH_DES specific data from user space to kernel space.
160 dh_k4_clnt_loadinfo(caddr_t usrdata
, caddr_t
*kdata
, model_t model
)
165 dh_k4_clntdata_t
*data
;
166 char netname
[MAXNETNAMELEN
+1];
167 struct netbuf
*syncaddr
;
168 struct knetconfig
*knconf
;
170 /* map opaque data to des specific strucutre */
171 data
= kmem_alloc(sizeof (*data
), KM_SLEEP
);
173 #ifdef _SYSCALL32_IMPL
174 if (model
!= DATAMODEL_NATIVE
) {
175 struct des_clnt_data32 data32
;
177 if (copyin(usrdata
, &data32
, sizeof (data32
)) == -1) {
180 data
->syncaddr
.maxlen
= data32
.syncaddr
.maxlen
;
181 data
->syncaddr
.len
= data32
.syncaddr
.len
;
183 (caddr_t
)(uintptr_t)data32
.syncaddr
.buf
;
185 (struct knetconfig
*)(uintptr_t)data32
.knconf
;
186 data
->netname
= (caddr_t
)(uintptr_t)data32
.netname
;
187 data
->netnamelen
= data32
.netnamelen
;
190 #endif /* _SYSCALL32_IMPL */
191 if (copyin(usrdata
, data
, sizeof (*data
)))
195 syncaddr
= &data
->syncaddr
;
196 if (syncaddr
->len
< MINAUTHLEN
|| syncaddr
->len
> MAXAUTHLEN
)
199 userbufptr
= syncaddr
->buf
;
200 syncaddr
->buf
= kmem_alloc(syncaddr
->len
, KM_SLEEP
);
201 syncaddr
->maxlen
= syncaddr
->len
;
202 if (copyin(userbufptr
, syncaddr
->buf
, syncaddr
->len
)) {
203 kmem_free(syncaddr
->buf
, syncaddr
->len
);
204 syncaddr
->buf
= NULL
;
207 (void) copyinstr(data
->netname
, netname
,
208 sizeof (netname
), &nlen
);
211 kmem_alloc(nlen
, KM_SLEEP
);
212 bcopy(netname
, data
->netname
, nlen
);
213 data
->netnamelen
= (int)nlen
;
221 * Allocate space for a knetconfig structure and
222 * its strings and copy in from user-land.
224 knconf
= kmem_alloc(sizeof (*knconf
), KM_SLEEP
);
225 #ifdef _SYSCALL32_IMPL
226 if (model
!= DATAMODEL_NATIVE
) {
227 struct knetconfig32 knconf32
;
229 if (copyin(data
->knconf
, &knconf32
,
230 sizeof (knconf32
)) == -1) {
231 kmem_free(knconf
, sizeof (*knconf
));
232 kmem_free(syncaddr
->buf
, syncaddr
->len
);
233 syncaddr
->buf
= NULL
;
234 kmem_free(data
->netname
, nlen
);
237 knconf
->knc_semantics
= knconf32
.knc_semantics
;
238 knconf
->knc_protofmly
=
239 (caddr_t
)(uintptr_t)knconf32
.knc_protofmly
;
241 (caddr_t
)(uintptr_t)knconf32
.knc_proto
;
242 knconf
->knc_rdev
= expldev(knconf32
.knc_rdev
);
245 #endif /* _SYSCALL32_IMPL */
246 if (copyin(data
->knconf
, knconf
, sizeof (*knconf
))) {
247 kmem_free(knconf
, sizeof (*knconf
));
248 kmem_free(syncaddr
->buf
, syncaddr
->len
);
249 syncaddr
->buf
= NULL
;
250 kmem_free(data
->netname
, nlen
);
259 pf
= kmem_alloc(KNC_STRSIZE
, KM_SLEEP
);
260 p
= kmem_alloc(KNC_STRSIZE
, KM_SLEEP
);
261 error
= copyinstr(knconf
->knc_protofmly
, pf
,
262 KNC_STRSIZE
, &nmoved_tmp
);
264 kmem_free(pf
, KNC_STRSIZE
);
265 kmem_free(p
, KNC_STRSIZE
);
266 kmem_free(knconf
, sizeof (*knconf
));
267 kmem_free(syncaddr
->buf
, syncaddr
->len
);
268 kmem_free(data
->netname
, nlen
);
272 error
= copyinstr(knconf
->knc_proto
,
273 p
, KNC_STRSIZE
, &nmoved_tmp
);
275 kmem_free(pf
, KNC_STRSIZE
);
276 kmem_free(p
, KNC_STRSIZE
);
277 kmem_free(knconf
, sizeof (*knconf
));
278 kmem_free(syncaddr
->buf
, syncaddr
->len
);
279 kmem_free(data
->netname
, nlen
);
284 knconf
->knc_protofmly
= pf
;
285 knconf
->knc_proto
= p
;
291 kmem_free(data
, sizeof (*data
));
295 data
->knconf
= knconf
;
296 *kdata
= (caddr_t
)data
;
301 * Free up AUTH_DES specific data.
304 dh_k4_clnt_freeinfo(caddr_t cdata
)
306 dh_k4_clntdata_t
*data
;
308 data
= (dh_k4_clntdata_t
*)cdata
;
309 if (data
->netnamelen
> 0) {
310 kmem_free(data
->netname
, data
->netnamelen
);
312 if (data
->syncaddr
.buf
!= NULL
) {
313 kmem_free(data
->syncaddr
.buf
, data
->syncaddr
.len
);
315 if (data
->knconf
!= NULL
) {
316 kmem_free(data
->knconf
->knc_protofmly
, KNC_STRSIZE
);
317 kmem_free(data
->knconf
->knc_proto
, KNC_STRSIZE
);
318 kmem_free(data
->knconf
, sizeof (*data
->knconf
));
321 kmem_free(data
, sizeof (*data
));
325 * Load application auth related data from user land to kernel.
326 * Map opaque data field to dh_k4_clntdata_t for AUTH_DES
330 sec_clnt_loadinfo(struct sec_data
*in
, struct sec_data
**out
, model_t model
)
332 struct sec_data
*secdata
;
335 secdata
= kmem_alloc(sizeof (*secdata
), KM_SLEEP
);
337 #ifdef _SYSCALL32_IMPL
338 if (model
!= DATAMODEL_NATIVE
) {
339 struct sec_data32 sd32
;
341 if (copyin(in
, &sd32
, sizeof (sd32
)) == -1) {
344 secdata
->secmod
= sd32
.secmod
;
345 secdata
->rpcflavor
= sd32
.rpcflavor
;
346 secdata
->uid
= sd32
.uid
;
347 secdata
->flags
= sd32
.flags
;
348 secdata
->data
= (caddr_t
)(uintptr_t)sd32
.data
;
351 #endif /* _SYSCALL32_IMPL */
353 if (copyin(in
, secdata
, sizeof (*secdata
)) == -1) {
357 * Copy in opaque data field per flavor.
360 switch (secdata
->rpcflavor
) {
367 error
= dh_k4_clnt_loadinfo(secdata
->data
,
368 &secdata
->data
, model
);
372 error
= gss_clnt_loadinfo(secdata
->data
,
373 &secdata
->data
, model
);
385 kmem_free(secdata
, sizeof (*secdata
));
393 * Null the sec_data index in the cache table, and
394 * free the memory allocated by sec_clnt_loadinfo.
397 sec_clnt_freeinfo(struct sec_data
*secdata
)
399 switch (secdata
->rpcflavor
) {
401 purge_authtab(secdata
);
403 dh_k4_clnt_freeinfo(secdata
->data
);
407 rpc_gss_secpurge((void *)secdata
);
409 gss_clntdata_t
*gss_data
;
411 gss_data
= (gss_clntdata_t
*)secdata
->data
;
412 if (gss_data
->mechanism
.elements
) {
413 kmem_free(gss_data
->mechanism
.elements
,
414 gss_data
->mechanism
.length
);
416 kmem_free(secdata
->data
, sizeof (gss_clntdata_t
));
426 kmem_free(secdata
, sizeof (*secdata
));
430 * Get an AUTH handle for a RPC client based on the given sec_data.
431 * If an AUTH handle exists for the same sec_data, use that AUTH handle,
432 * otherwise create a new one.
435 sec_clnt_geth(CLIENT
*client
, struct sec_data
*secdata
, cred_t
*cr
, AUTH
**ap
)
438 struct desauthent
*da
;
441 int stat
; /* return (errno) status */
442 char gss_svc_name
[MAX_GSS_NAME
];
443 dh_k4_clntdata_t
*desdata
;
445 gss_clntdata_t
*gssdata
;
446 zoneid_t zoneid
= getzoneid();
448 if ((client
== NULL
) || (secdata
== NULL
) || (ap
== NULL
))
452 authflavor
= secdata
->rpcflavor
;
457 switch (authflavor
) {
459 *ap
= (AUTH
*) authnone_create();
460 return ((*ap
!= NULL
) ? 0 : EINTR
);
463 *ap
= (AUTH
*) authkern_create();
464 return ((*ap
!= NULL
) ? 0 : EINTR
);
467 *ap
= (AUTH
*) authloopback_create();
468 return ((*ap
!= NULL
) ? 0 : EINTR
);
471 mutex_enter(&desauthtab_lock
);
472 if (desauthtab
== NULL
) {
473 desauthtab
= kmem_zalloc(clnt_authdes_cachesz
*
474 sizeof (struct desauthent
), KM_SLEEP
);
476 for (da
= desauthtab
;
477 da
< &desauthtab
[clnt_authdes_cachesz
];
479 if (da
->da_data
== secdata
&&
480 da
->da_uid
== crgetuid(cr
) &&
481 da
->da_zoneid
== zoneid
&&
483 da
->da_auth
!= NULL
) {
485 mutex_exit(&desauthtab_lock
);
490 mutex_exit(&desauthtab_lock
);
493 * A better way would be to have a cred paramater to
496 savecred
= curthread
->t_cred
;
497 curthread
->t_cred
= cr
;
500 * Note that authdes_create() expects a
501 * NUL-terminated string for netname, but
502 * dh_k4_clntdata_t gives us netname & netnamelen.
504 * We must create a string for authdes_create();
505 * the latter takes a copy of it, so we may
506 * immediately free it.
508 desdata
= (dh_k4_clntdata_t
*)secdata
->data
;
509 nlen
= desdata
->netnamelen
;
510 /* must be NUL-terminated */
511 netname
= kmem_zalloc(nlen
+ 1, KM_SLEEP
);
512 bcopy(desdata
->netname
, netname
, nlen
);
513 stat
= authdes_create(netname
, authdes_win
,
514 &desdata
->syncaddr
, desdata
->knconf
,
516 (secdata
->flags
& AUTH_F_RPCTIMESYNC
) ? 1 : 0,
518 kmem_free(netname
, nlen
+ 1);
520 curthread
->t_cred
= savecred
;
525 * If AUTH_F_TRYNONE is on, try again
526 * with AUTH_NONE. See bug 1180236.
528 if (secdata
->flags
& AUTH_F_TRYNONE
) {
529 authflavor
= AUTH_NONE
;
535 i
= clnt_authdes_cachesz
;
536 mutex_enter(&desauthtab_lock
);
538 da
= &desauthtab
[nextdesvictim
++];
539 nextdesvictim
%= clnt_authdes_cachesz
;
540 } while (da
->da_inuse
&& --i
> 0);
543 mutex_exit(&desauthtab_lock
);
544 /* overflow of des auths */
548 mutex_exit(&desauthtab_lock
);
550 if (da
->da_auth
!= NULL
)
551 auth_destroy(da
->da_auth
);
554 da
->da_uid
= crgetuid(cr
);
555 da
->da_zoneid
= zoneid
;
556 da
->da_data
= secdata
;
561 * For RPCSEC_GSS, cache is done in rpc_gss_secget().
562 * For every rpc_gss_secget(), it should have
563 * a corresponding rpc_gss_secfree() call.
565 gssdata
= (gss_clntdata_t
*)secdata
->data
;
566 (void) sprintf(gss_svc_name
, "%s@%s", gssdata
->uname
,
569 stat
= rpc_gss_secget(client
, gss_svc_name
,
574 (caddr_t
)secdata
, cr
, &auth
);
582 * let the caller retry if connection timedout
585 if (stat
== ETIMEDOUT
|| stat
== ECONNRESET
)
589 * If AUTH_F_TRYNONE is on, try again
590 * with AUTH_NONE. See bug 1180236.
592 if (secdata
->flags
& AUTH_F_TRYNONE
) {
593 authflavor
= AUTH_NONE
;
597 RPCLOG(1, "sec_clnt_geth: rpc_gss_secget"
598 " failed with %d", stat
);
603 * auth create must have failed, try AUTH_NONE
604 * (this relies on AUTH_NONE never failing)
606 cmn_err(CE_NOTE
, "sec_clnt_geth: unknown "
607 "authflavor %d, trying AUTH_NONE", authflavor
);
608 authflavor
= AUTH_NONE
;
614 sec_clnt_freeh(AUTH
*auth
)
616 struct desauthent
*da
;
618 switch (auth
->ah_cred
.oa_flavor
) {
619 case AUTH_NONE
: /* XXX: do real AUTH_NONE */
622 auth_destroy(auth
); /* was overflow */
626 mutex_enter(&desauthtab_lock
);
627 if (desauthtab
!= NULL
) {
628 for (da
= desauthtab
;
629 da
< &desauthtab
[clnt_authdes_cachesz
]; da
++) {
630 if (da
->da_auth
== auth
) {
632 mutex_exit(&desauthtab_lock
);
637 mutex_exit(&desauthtab_lock
);
638 auth_destroy(auth
); /* was overflow */
642 (void) rpc_gss_secfree(auth
);
646 cmn_err(CE_NOTE
, "sec_clnt_freeh: unknown authflavor %d",
647 auth
->ah_cred
.oa_flavor
);
653 * Revoke the authentication key in the given AUTH handle by setting
654 * it to NULL. If newkey is true, then generate a new key instead of
655 * nulling out the old one. This is necessary for AUTH_DES because
656 * the new key will be used next time the user does a keylogin. If
657 * the zero'd key is used as actual key, then it cannot be revoked
661 revoke_key(AUTH
*auth
, int newkey
)
667 if (key_gendes(&auth
->ah_key
) != RPC_SUCCESS
) {
668 /* failed to get new key, munge the old one */
669 auth
->ah_key
.key
.high
^= auth
->ah_key
.key
.low
;
670 auth
->ah_key
.key
.low
+= auth
->ah_key
.key
.high
;
673 /* null out old key */
674 auth
->ah_key
.key
.high
= 0;
675 auth
->ah_key
.key
.low
= 0;
680 * Revoke all rpc credentials (of the selected auth type) for the given uid
681 * from the auth cache. Must be root to do this if the requested uid is not
682 * the effective uid of the requestor.
684 * Called from nfssys() for backward compatibility, and also
685 * called from krpc_sys().
687 * AUTH_DES does not refer to the "mechanism" information.
688 * RPCSEC_GSS requires the "mechanism" input.
689 * The input argument, mechanism, is a user-space address and needs
690 * to be copied into the kernel address space.
692 * Returns error number.
696 sec_clnt_revoke(int rpcflavor
, uid_t uid
, cred_t
*cr
, void *mechanism
,
699 struct desauthent
*da
;
701 zoneid_t zoneid
= getzoneid();
703 if (uid
!= crgetuid(cr
) && secpolicy_nfs(cr
) != 0)
708 mutex_enter(&desauthtab_lock
);
709 if (desauthtab
!= NULL
) {
710 for (da
= desauthtab
;
711 da
< &desauthtab
[clnt_authdes_cachesz
]; da
++) {
712 if (uid
== da
->da_uid
&&
713 zoneid
== da
->da_zoneid
)
714 revoke_key(da
->da_auth
, 1);
717 mutex_exit(&desauthtab_lock
);
727 /* copyin the gss mechanism type */
728 mech
= kmem_alloc(sizeof (rpc_gss_OID_desc
), KM_SLEEP
);
729 #ifdef _SYSCALL32_IMPL
730 if (model
!= DATAMODEL_NATIVE
) {
731 gss_OID_desc32 mech32
;
733 if (copyin(mechanism
, &mech32
,
734 sizeof (gss_OID_desc32
))) {
735 kmem_free(mech
, sizeof (rpc_gss_OID_desc
));
738 mech
->length
= mech32
.length
;
739 mech
->elements
= (caddr_t
)(uintptr_t)mech32
.elements
;
741 #endif /* _SYSCALL32_IMPL */
742 if (copyin(mechanism
, mech
, sizeof (rpc_gss_OID_desc
))) {
743 kmem_free(mech
, sizeof (rpc_gss_OID_desc
));
747 if (mech
->length
< MINAUTHLEN
||
748 mech
->length
> MAXAUTHLEN
) {
749 kmem_free(mech
, sizeof (rpc_gss_OID_desc
));
753 elements
= kmem_alloc(mech
->length
, KM_SLEEP
);
754 if (copyin(mech
->elements
, elements
, mech
->length
)) {
755 kmem_free(elements
, mech
->length
);
756 kmem_free(mech
, sizeof (rpc_gss_OID_desc
));
759 mech
->elements
= elements
;
761 error
= rpc_gss_revauth(uid
, mech
);
763 kmem_free(elements
, mech
->length
);
764 kmem_free(mech
, sizeof (rpc_gss_OID_desc
));
770 /* not an auth type with cached creds */
776 * Since sec_data is the index for the client auth handles
777 * cache table, whenever the sec_data is freed, the index needs
781 purge_authtab(struct sec_data
*secdata
)
783 struct desauthent
*da
;
785 switch (secdata
->rpcflavor
) {
788 mutex_enter(&desauthtab_lock
);
789 if (desauthtab
!= NULL
) {
790 for (da
= desauthtab
;
791 da
< &desauthtab
[clnt_authdes_cachesz
]; da
++) {
792 if (da
->da_data
== secdata
) {
798 mutex_exit(&desauthtab_lock
);
802 rpc_gss_secpurge((void *)secdata
);
813 authkern_cache
= kmem_cache_create("authkern_cache",
814 sizeof (AUTH
), 0, authkern_init
, NULL
, NULL
, NULL
, NULL
, 0);
815 authnone_cache
= kmem_cache_create("authnone_cache",
816 sizeof (AUTH
), 0, authnone_init
, NULL
, NULL
, NULL
, NULL
, 0);
817 authloopback_cache
= kmem_cache_create("authloopback_cache",
818 sizeof (AUTH
), 0, authloopback_init
, NULL
, NULL
, NULL
, NULL
, 0);
819 mutex_init(&desauthtab_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
822 mutex_init(&authdes_ops_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
823 zone_key_create(&auth_zone_key
, auth_zone_init
, NULL
, auth_zone_fini
);
827 * Destroys the caches and mutexes previously allocated and initialized
829 * This routine is called by _init() if mod_install() failed.
834 mutex_destroy(&desauthtab_lock
);
835 kmem_cache_destroy(authkern_cache
);
836 kmem_cache_destroy(authnone_cache
);
837 kmem_cache_destroy(authloopback_cache
);
840 mutex_destroy(&authdes_ops_lock
);
841 (void) zone_key_delete(auth_zone_key
);