Blame view

fs/cifs/cifs_spnego.c 6.31 KB
f1d662a7d   Steve French   [CIFS] Add upcall...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   *   fs/cifs/cifs_spnego.c -- SPNEGO upcall management for CIFS
   *
   *   Copyright (c) 2007 Red Hat, Inc.
   *   Author(s): Jeff Layton (jlayton@redhat.com)
   *
   *   This library is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU Lesser General Public License as published
   *   by the Free Software Foundation; either version 2.1 of the License, or
   *   (at your option) any later version.
   *
   *   This library is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
   *   the GNU Lesser General Public License for more details.
   *
   *   You should have received a copy of the GNU Lesser General Public License
   *   along with this library; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   */
  
  #include <linux/list.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
23
  #include <linux/slab.h>
f1d662a7d   Steve French   [CIFS] Add upcall...
24
25
26
  #include <linux/string.h>
  #include <keys/user-type.h>
  #include <linux/key-type.h>
b74cb9a80   Sachin Prabhu   cifs: Create dedi...
27
  #include <linux/keyctl.h>
50b64e3b7   Jeff Layton   cifs: fix IPv6 ad...
28
  #include <linux/inet.h>
f1d662a7d   Steve French   [CIFS] Add upcall...
29
30
31
  #include "cifsglob.h"
  #include "cifs_spnego.h"
  #include "cifs_debug.h"
b74cb9a80   Sachin Prabhu   cifs: Create dedi...
32
33
  #include "cifsproto.h"
  static const struct cred *spnego_cred;
f1d662a7d   Steve French   [CIFS] Add upcall...
34
35
36
  
  /* create a new cifs key */
  static int
cf7f601c0   David Howells   KEYS: Add payload...
37
  cifs_spnego_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
f1d662a7d   Steve French   [CIFS] Add upcall...
38
39
40
41
42
  {
  	char *payload;
  	int ret;
  
  	ret = -ENOMEM;
f7f7c1850   Silviu-Mihai Popescu   fs: cifs: use kme...
43
  	payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
f1d662a7d   Steve French   [CIFS] Add upcall...
44
45
46
47
  	if (!payload)
  		goto error;
  
  	/* attach the data */
146aa8b14   David Howells   KEYS: Merge the t...
48
  	key->payload.data[0] = payload;
f1d662a7d   Steve French   [CIFS] Add upcall...
49
50
51
52
53
54
55
56
57
  	ret = 0;
  
  error:
  	return ret;
  }
  
  static void
  cifs_spnego_key_destroy(struct key *key)
  {
146aa8b14   David Howells   KEYS: Merge the t...
58
  	kfree(key->payload.data[0]);
f1d662a7d   Steve French   [CIFS] Add upcall...
59
60
61
62
63
64
65
66
67
  }
  
  
  /*
   * keytype for CIFS spnego keys
   */
  struct key_type cifs_spnego_key_type = {
  	.name		= "cifs.spnego",
  	.instantiate	= cifs_spnego_key_instantiate,
f1d662a7d   Steve French   [CIFS] Add upcall...
68
69
70
  	.destroy	= cifs_spnego_key_destroy,
  	.describe	= user_describe,
  };
7c9c3760b   Steve French   [CIFS] add consta...
71
72
73
74
75
76
  /* length of longest version string e.g.  strlen("ver=0xFF") */
  #define MAX_VER_STR_LEN		8
  
  /* length of longest security mechanism name, eg in future could have
   * strlen(";sec=ntlmsspi") */
  #define MAX_MECH_STR_LEN	13
7c9c3760b   Steve French   [CIFS] add consta...
77
78
79
80
81
82
83
84
  /* strlen of "host=" */
  #define HOST_KEY_LEN		5
  
  /* strlen of ";ip4=" or ";ip6=" */
  #define IP_KEY_LEN		5
  
  /* strlen of ";uid=0x" */
  #define UID_KEY_LEN		7
ba5dadbf4   Jeff Layton   cifs: account for...
85
86
  /* strlen of ";creduid=0x" */
  #define CREDUID_KEY_LEN		11
7c9c3760b   Steve French   [CIFS] add consta...
87
88
  /* strlen of ";user=" */
  #define USER_KEY_LEN		6
c4c1bff64   Jeff Layton   cifs: add pid of ...
89
90
  /* strlen of ";pid=0x" */
  #define PID_KEY_LEN		7
f1d662a7d   Steve French   [CIFS] Add upcall...
91
92
  /* get a key struct with a SPNEGO security blob, suitable for session setup */
  struct key *
96daf2b09   Steve French   [CIFS] Rename thr...
93
  cifs_get_spnego_key(struct cifs_ses *sesInfo)
f1d662a7d   Steve French   [CIFS] Add upcall...
94
95
  {
  	struct TCP_Server_Info *server = sesInfo->server;
a9f1b85e5   Pavel Shilovsky   CIFS: Simplify ip...
96
97
  	struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
  	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
f1d662a7d   Steve French   [CIFS] Add upcall...
98
99
100
  	char *description, *dp;
  	size_t desc_len;
  	struct key *spnego_key;
d6c2e4d02   Jeff Layton   [CIFS] have cifs_...
101
  	const char *hostname = server->hostname;
b74cb9a80   Sachin Prabhu   cifs: Create dedi...
102
  	const struct cred *saved_cred;
f1d662a7d   Steve French   [CIFS] Add upcall...
103

66b8bd3c4   Jeff Layton   [CIFS] properly a...
104
105
106
  	/* length of fields (with semicolons): ver=0xyz ip4=ipaddress
  	   host=hostname sec=mechanism uid=0xFF user=username */
  	desc_len = MAX_VER_STR_LEN +
7c9c3760b   Steve French   [CIFS] add consta...
107
  		   HOST_KEY_LEN + strlen(hostname) +
50b64e3b7   Jeff Layton   cifs: fix IPv6 ad...
108
  		   IP_KEY_LEN + INET6_ADDRSTRLEN +
66b8bd3c4   Jeff Layton   [CIFS] properly a...
109
  		   MAX_MECH_STR_LEN +
7c9c3760b   Steve French   [CIFS] add consta...
110
  		   UID_KEY_LEN + (sizeof(uid_t) * 2) +
ba5dadbf4   Jeff Layton   cifs: account for...
111
  		   CREDUID_KEY_LEN + (sizeof(uid_t) * 2) +
c4c1bff64   Jeff Layton   cifs: add pid of ...
112
  		   PID_KEY_LEN + (sizeof(pid_t) * 2) + 1;
66b8bd3c4   Jeff Layton   [CIFS] properly a...
113

04febabcf   Jeff Layton   cifs: sanitize us...
114
115
  	if (sesInfo->user_name)
  		desc_len += USER_KEY_LEN + strlen(sesInfo->user_name);
f1d662a7d   Steve French   [CIFS] Add upcall...
116
117
118
119
120
121
122
123
  	spnego_key = ERR_PTR(-ENOMEM);
  	description = kzalloc(desc_len, GFP_KERNEL);
  	if (description == NULL)
  		goto out;
  
  	dp = description;
  	/* start with version and hostname portion of UNC string */
  	spnego_key = ERR_PTR(-EINVAL);
68bf728a2   Steve French   [CIFS] add ver= p...
124
  	sprintf(dp, "ver=0x%x;host=%s;", CIFS_SPNEGO_UPCALL_VERSION,
f1d662a7d   Steve French   [CIFS] Add upcall...
125
126
127
128
  		hostname);
  	dp = description + strlen(description);
  
  	/* add the server address */
a9f1b85e5   Pavel Shilovsky   CIFS: Simplify ip...
129
130
131
132
  	if (server->dstaddr.ss_family == AF_INET)
  		sprintf(dp, "ip4=%pI4", &sa->sin_addr);
  	else if (server->dstaddr.ss_family == AF_INET6)
  		sprintf(dp, "ip6=%pI6", &sa6->sin6_addr);
f1d662a7d   Steve French   [CIFS] Add upcall...
133
134
135
136
  	else
  		goto out;
  
  	dp = description + strlen(description);
c16fefa56   Steve French   [CIFS] distinguis...
137
  	/* for now, only sec=krb5 and sec=mskrb5 are valid */
26efa0bac   Jeff Layton   cifs: have decode...
138
  	if (server->sec_kerberos)
f1d662a7d   Steve French   [CIFS] Add upcall...
139
  		sprintf(dp, ";sec=krb5");
26efa0bac   Jeff Layton   cifs: have decode...
140
  	else if (server->sec_mskerberos)
c16fefa56   Steve French   [CIFS] distinguis...
141
  		sprintf(dp, ";sec=mskrb5");
f1d662a7d   Steve French   [CIFS] Add upcall...
142
143
  	else
  		goto out;
9eae8a890   Igor Mammedov   [CIFS] Add uid to...
144
  	dp = description + strlen(description);
64ed39dd1   Eric W. Biederman   cifs: Convert str...
145
146
  	sprintf(dp, ";uid=0x%x",
  		from_kuid_munged(&init_user_ns, sesInfo->linux_uid));
9eae8a890   Igor Mammedov   [CIFS] Add uid to...
147

e4058245a   Igor Mammedov   Adds username in ...
148
  	dp = description + strlen(description);
64ed39dd1   Eric W. Biederman   cifs: Convert str...
149
150
  	sprintf(dp, ";creduid=0x%x",
  		from_kuid_munged(&init_user_ns, sesInfo->cred_uid));
3e4b3e1f6   Jeff Layton   cifs: add separat...
151

04febabcf   Jeff Layton   cifs: sanitize us...
152
153
154
155
  	if (sesInfo->user_name) {
  		dp = description + strlen(description);
  		sprintf(dp, ";user=%s", sesInfo->user_name);
  	}
e4058245a   Igor Mammedov   Adds username in ...
156

c4c1bff64   Jeff Layton   cifs: add pid of ...
157
158
  	dp = description + strlen(description);
  	sprintf(dp, ";pid=0x%x", current->pid);
f96637be0   Joe Perches   [CIFS] cifs: Rena...
159
160
  	cifs_dbg(FYI, "key description = %s
  ", description);
b74cb9a80   Sachin Prabhu   cifs: Create dedi...
161
  	saved_cred = override_creds(spnego_cred);
f1d662a7d   Steve French   [CIFS] Add upcall...
162
  	spnego_key = request_key(&cifs_spnego_key_type, description, "");
b74cb9a80   Sachin Prabhu   cifs: Create dedi...
163
  	revert_creds(saved_cred);
f1d662a7d   Steve French   [CIFS] Add upcall...
164

05b3de63d   Jeff Layton   [CIFS] Only dump ...
165
  #ifdef CONFIG_CIFS_DEBUG2
f1d662a7d   Steve French   [CIFS] Add upcall...
166
  	if (cifsFYI && !IS_ERR(spnego_key)) {
146aa8b14   David Howells   KEYS: Merge the t...
167
  		struct cifs_spnego_msg *msg = spnego_key->payload.data[0];
ead03e30b   Andrew Morton   [CIFS] fix warnin...
168
  		cifs_dump_mem("SPNEGO reply blob:", msg->data, min(1024U,
05b3de63d   Jeff Layton   [CIFS] Only dump ...
169
  				msg->secblob_len + msg->sesskey_len));
f1d662a7d   Steve French   [CIFS] Add upcall...
170
  	}
05b3de63d   Jeff Layton   [CIFS] Only dump ...
171
  #endif /* CONFIG_CIFS_DEBUG2 */
f1d662a7d   Steve French   [CIFS] Add upcall...
172
173
174
175
176
  
  out:
  	kfree(description);
  	return spnego_key;
  }
b74cb9a80   Sachin Prabhu   cifs: Create dedi...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  
  int
  init_cifs_spnego(void)
  {
  	struct cred *cred;
  	struct key *keyring;
  	int ret;
  
  	cifs_dbg(FYI, "Registering the %s key type
  ",
  		 cifs_spnego_key_type.name);
  
  	/*
  	 * Create an override credential set with special thread keyring for
  	 * spnego upcalls.
  	 */
  
  	cred = prepare_kernel_cred(NULL);
  	if (!cred)
  		return -ENOMEM;
  
  	keyring = keyring_alloc(".cifs_spnego",
  				GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  				(KEY_POS_ALL & ~KEY_POS_SETATTR) |
  				KEY_USR_VIEW | KEY_USR_READ,
  				KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  	if (IS_ERR(keyring)) {
  		ret = PTR_ERR(keyring);
  		goto failed_put_cred;
  	}
  
  	ret = register_key_type(&cifs_spnego_key_type);
  	if (ret < 0)
  		goto failed_put_key;
  
  	/*
  	 * instruct request_key() to use this special keyring as a cache for
  	 * the results it looks up
  	 */
  	set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  	cred->thread_keyring = keyring;
  	cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  	spnego_cred = cred;
  
  	cifs_dbg(FYI, "cifs spnego keyring: %d
  ", key_serial(keyring));
  	return 0;
  
  failed_put_key:
  	key_put(keyring);
  failed_put_cred:
  	put_cred(cred);
  	return ret;
  }
  
  void
  exit_cifs_spnego(void)
  {
  	key_revoke(spnego_cred->thread_keyring);
  	unregister_key_type(&cifs_spnego_key_type);
  	put_cred(spnego_cred);
  	cifs_dbg(FYI, "Unregistered %s key type
  ", cifs_spnego_key_type.name);
  }