xref: /OK3568_Linux_fs/kernel/fs/cifs/cifs_spnego.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *   fs/cifs/cifs_spnego.c -- SPNEGO upcall management for CIFS
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *   Copyright (c) 2007 Red Hat, Inc.
5*4882a593Smuzhiyun  *   Author(s): Jeff Layton (jlayton@redhat.com)
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *   This library is free software; you can redistribute it and/or modify
8*4882a593Smuzhiyun  *   it under the terms of the GNU Lesser General Public License as published
9*4882a593Smuzhiyun  *   by the Free Software Foundation; either version 2.1 of the License, or
10*4882a593Smuzhiyun  *   (at your option) any later version.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *   This library is distributed in the hope that it will be useful,
13*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15*4882a593Smuzhiyun  *   the GNU Lesser General Public License for more details.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *   You should have received a copy of the GNU Lesser General Public License
18*4882a593Smuzhiyun  *   along with this library; if not, write to the Free Software
19*4882a593Smuzhiyun  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <linux/list.h>
23*4882a593Smuzhiyun #include <linux/slab.h>
24*4882a593Smuzhiyun #include <linux/string.h>
25*4882a593Smuzhiyun #include <keys/user-type.h>
26*4882a593Smuzhiyun #include <linux/key-type.h>
27*4882a593Smuzhiyun #include <linux/keyctl.h>
28*4882a593Smuzhiyun #include <linux/inet.h>
29*4882a593Smuzhiyun #include "cifsglob.h"
30*4882a593Smuzhiyun #include "cifs_spnego.h"
31*4882a593Smuzhiyun #include "cifs_debug.h"
32*4882a593Smuzhiyun #include "cifsproto.h"
33*4882a593Smuzhiyun static const struct cred *spnego_cred;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* create a new cifs key */
36*4882a593Smuzhiyun static int
cifs_spnego_key_instantiate(struct key * key,struct key_preparsed_payload * prep)37*4882a593Smuzhiyun cifs_spnego_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	char *payload;
40*4882a593Smuzhiyun 	int ret;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	ret = -ENOMEM;
43*4882a593Smuzhiyun 	payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
44*4882a593Smuzhiyun 	if (!payload)
45*4882a593Smuzhiyun 		goto error;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	/* attach the data */
48*4882a593Smuzhiyun 	key->payload.data[0] = payload;
49*4882a593Smuzhiyun 	ret = 0;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun error:
52*4882a593Smuzhiyun 	return ret;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun static void
cifs_spnego_key_destroy(struct key * key)56*4882a593Smuzhiyun cifs_spnego_key_destroy(struct key *key)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	kfree(key->payload.data[0]);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun  * keytype for CIFS spnego keys
64*4882a593Smuzhiyun  */
65*4882a593Smuzhiyun struct key_type cifs_spnego_key_type = {
66*4882a593Smuzhiyun 	.name		= "cifs.spnego",
67*4882a593Smuzhiyun 	.instantiate	= cifs_spnego_key_instantiate,
68*4882a593Smuzhiyun 	.destroy	= cifs_spnego_key_destroy,
69*4882a593Smuzhiyun 	.describe	= user_describe,
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /* length of longest version string e.g.  strlen("ver=0xFF") */
73*4882a593Smuzhiyun #define MAX_VER_STR_LEN		8
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /* length of longest security mechanism name, eg in future could have
76*4882a593Smuzhiyun  * strlen(";sec=ntlmsspi") */
77*4882a593Smuzhiyun #define MAX_MECH_STR_LEN	13
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /* strlen of "host=" */
80*4882a593Smuzhiyun #define HOST_KEY_LEN		5
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* strlen of ";ip4=" or ";ip6=" */
83*4882a593Smuzhiyun #define IP_KEY_LEN		5
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* strlen of ";uid=0x" */
86*4882a593Smuzhiyun #define UID_KEY_LEN		7
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /* strlen of ";creduid=0x" */
89*4882a593Smuzhiyun #define CREDUID_KEY_LEN		11
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /* strlen of ";user=" */
92*4882a593Smuzhiyun #define USER_KEY_LEN		6
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* strlen of ";pid=0x" */
95*4882a593Smuzhiyun #define PID_KEY_LEN		7
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /* get a key struct with a SPNEGO security blob, suitable for session setup */
98*4882a593Smuzhiyun struct key *
cifs_get_spnego_key(struct cifs_ses * sesInfo)99*4882a593Smuzhiyun cifs_get_spnego_key(struct cifs_ses *sesInfo)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	struct TCP_Server_Info *server = cifs_ses_server(sesInfo);
102*4882a593Smuzhiyun 	struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
103*4882a593Smuzhiyun 	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
104*4882a593Smuzhiyun 	char *description, *dp;
105*4882a593Smuzhiyun 	size_t desc_len;
106*4882a593Smuzhiyun 	struct key *spnego_key;
107*4882a593Smuzhiyun 	const char *hostname = server->hostname;
108*4882a593Smuzhiyun 	const struct cred *saved_cred;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/* length of fields (with semicolons): ver=0xyz ip4=ipaddress
111*4882a593Smuzhiyun 	   host=hostname sec=mechanism uid=0xFF user=username */
112*4882a593Smuzhiyun 	desc_len = MAX_VER_STR_LEN +
113*4882a593Smuzhiyun 		   HOST_KEY_LEN + strlen(hostname) +
114*4882a593Smuzhiyun 		   IP_KEY_LEN + INET6_ADDRSTRLEN +
115*4882a593Smuzhiyun 		   MAX_MECH_STR_LEN +
116*4882a593Smuzhiyun 		   UID_KEY_LEN + (sizeof(uid_t) * 2) +
117*4882a593Smuzhiyun 		   CREDUID_KEY_LEN + (sizeof(uid_t) * 2) +
118*4882a593Smuzhiyun 		   PID_KEY_LEN + (sizeof(pid_t) * 2) + 1;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	if (sesInfo->user_name)
121*4882a593Smuzhiyun 		desc_len += USER_KEY_LEN + strlen(sesInfo->user_name);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	spnego_key = ERR_PTR(-ENOMEM);
124*4882a593Smuzhiyun 	description = kzalloc(desc_len, GFP_KERNEL);
125*4882a593Smuzhiyun 	if (description == NULL)
126*4882a593Smuzhiyun 		goto out;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	dp = description;
129*4882a593Smuzhiyun 	/* start with version and hostname portion of UNC string */
130*4882a593Smuzhiyun 	spnego_key = ERR_PTR(-EINVAL);
131*4882a593Smuzhiyun 	sprintf(dp, "ver=0x%x;host=%s;", CIFS_SPNEGO_UPCALL_VERSION,
132*4882a593Smuzhiyun 		hostname);
133*4882a593Smuzhiyun 	dp = description + strlen(description);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* add the server address */
136*4882a593Smuzhiyun 	if (server->dstaddr.ss_family == AF_INET)
137*4882a593Smuzhiyun 		sprintf(dp, "ip4=%pI4", &sa->sin_addr);
138*4882a593Smuzhiyun 	else if (server->dstaddr.ss_family == AF_INET6)
139*4882a593Smuzhiyun 		sprintf(dp, "ip6=%pI6", &sa6->sin6_addr);
140*4882a593Smuzhiyun 	else
141*4882a593Smuzhiyun 		goto out;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	dp = description + strlen(description);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	/* for now, only sec=krb5 and sec=mskrb5 are valid */
146*4882a593Smuzhiyun 	if (server->sec_kerberos)
147*4882a593Smuzhiyun 		sprintf(dp, ";sec=krb5");
148*4882a593Smuzhiyun 	else if (server->sec_mskerberos)
149*4882a593Smuzhiyun 		sprintf(dp, ";sec=mskrb5");
150*4882a593Smuzhiyun 	else {
151*4882a593Smuzhiyun 		cifs_dbg(VFS, "unknown or missing server auth type, use krb5\n");
152*4882a593Smuzhiyun 		sprintf(dp, ";sec=krb5");
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	dp = description + strlen(description);
156*4882a593Smuzhiyun 	sprintf(dp, ";uid=0x%x",
157*4882a593Smuzhiyun 		from_kuid_munged(&init_user_ns, sesInfo->linux_uid));
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	dp = description + strlen(description);
160*4882a593Smuzhiyun 	sprintf(dp, ";creduid=0x%x",
161*4882a593Smuzhiyun 		from_kuid_munged(&init_user_ns, sesInfo->cred_uid));
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	if (sesInfo->user_name) {
164*4882a593Smuzhiyun 		dp = description + strlen(description);
165*4882a593Smuzhiyun 		sprintf(dp, ";user=%s", sesInfo->user_name);
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	dp = description + strlen(description);
169*4882a593Smuzhiyun 	sprintf(dp, ";pid=0x%x", current->pid);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	cifs_dbg(FYI, "key description = %s\n", description);
172*4882a593Smuzhiyun 	saved_cred = override_creds(spnego_cred);
173*4882a593Smuzhiyun 	spnego_key = request_key(&cifs_spnego_key_type, description, "");
174*4882a593Smuzhiyun 	revert_creds(saved_cred);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun #ifdef CONFIG_CIFS_DEBUG2
177*4882a593Smuzhiyun 	if (cifsFYI && !IS_ERR(spnego_key)) {
178*4882a593Smuzhiyun 		struct cifs_spnego_msg *msg = spnego_key->payload.data[0];
179*4882a593Smuzhiyun 		cifs_dump_mem("SPNEGO reply blob:", msg->data, min(1024U,
180*4882a593Smuzhiyun 				msg->secblob_len + msg->sesskey_len));
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun #endif /* CONFIG_CIFS_DEBUG2 */
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun out:
185*4882a593Smuzhiyun 	kfree(description);
186*4882a593Smuzhiyun 	return spnego_key;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun int
init_cifs_spnego(void)190*4882a593Smuzhiyun init_cifs_spnego(void)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	struct cred *cred;
193*4882a593Smuzhiyun 	struct key *keyring;
194*4882a593Smuzhiyun 	int ret;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	cifs_dbg(FYI, "Registering the %s key type\n",
197*4882a593Smuzhiyun 		 cifs_spnego_key_type.name);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	/*
200*4882a593Smuzhiyun 	 * Create an override credential set with special thread keyring for
201*4882a593Smuzhiyun 	 * spnego upcalls.
202*4882a593Smuzhiyun 	 */
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	cred = prepare_kernel_cred(NULL);
205*4882a593Smuzhiyun 	if (!cred)
206*4882a593Smuzhiyun 		return -ENOMEM;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	keyring = keyring_alloc(".cifs_spnego",
209*4882a593Smuzhiyun 				GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
210*4882a593Smuzhiyun 				(KEY_POS_ALL & ~KEY_POS_SETATTR) |
211*4882a593Smuzhiyun 				KEY_USR_VIEW | KEY_USR_READ,
212*4882a593Smuzhiyun 				KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
213*4882a593Smuzhiyun 	if (IS_ERR(keyring)) {
214*4882a593Smuzhiyun 		ret = PTR_ERR(keyring);
215*4882a593Smuzhiyun 		goto failed_put_cred;
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	ret = register_key_type(&cifs_spnego_key_type);
219*4882a593Smuzhiyun 	if (ret < 0)
220*4882a593Smuzhiyun 		goto failed_put_key;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	/*
223*4882a593Smuzhiyun 	 * instruct request_key() to use this special keyring as a cache for
224*4882a593Smuzhiyun 	 * the results it looks up
225*4882a593Smuzhiyun 	 */
226*4882a593Smuzhiyun 	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
227*4882a593Smuzhiyun 	cred->thread_keyring = keyring;
228*4882a593Smuzhiyun 	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
229*4882a593Smuzhiyun 	spnego_cred = cred;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	cifs_dbg(FYI, "cifs spnego keyring: %d\n", key_serial(keyring));
232*4882a593Smuzhiyun 	return 0;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun failed_put_key:
235*4882a593Smuzhiyun 	key_put(keyring);
236*4882a593Smuzhiyun failed_put_cred:
237*4882a593Smuzhiyun 	put_cred(cred);
238*4882a593Smuzhiyun 	return ret;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun void
exit_cifs_spnego(void)242*4882a593Smuzhiyun exit_cifs_spnego(void)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	key_revoke(spnego_cred->thread_keyring);
245*4882a593Smuzhiyun 	unregister_key_type(&cifs_spnego_key_type);
246*4882a593Smuzhiyun 	put_cred(spnego_cred);
247*4882a593Smuzhiyun 	cifs_dbg(FYI, "Unregistered %s key type\n", cifs_spnego_key_type.name);
248*4882a593Smuzhiyun }
249