blob: 353bd0dd702606114b8ced486c0212235cf82018 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Steve Frenchf1d662a2007-11-05 14:38:08 +00002/*
Steve French099dd782021-09-13 14:51:10 -05003 * SPNEGO upcall management for CIFS
Steve Frenchf1d662a2007-11-05 14:38:08 +00004 *
5 * Copyright (c) 2007 Red Hat, Inc.
6 * Author(s): Jeff Layton (jlayton@redhat.com)
7 *
Steve Frenchf1d662a2007-11-05 14:38:08 +00008 */
9
10#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Steve Frenchf1d662a2007-11-05 14:38:08 +000012#include <linux/string.h>
13#include <keys/user-type.h>
14#include <linux/key-type.h>
Sachin Prabhub74cb9a2016-05-17 18:20:13 -050015#include <linux/keyctl.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040016#include <linux/inet.h>
Steve Frenchf1d662a2007-11-05 14:38:08 +000017#include "cifsglob.h"
18#include "cifs_spnego.h"
19#include "cifs_debug.h"
Sachin Prabhub74cb9a2016-05-17 18:20:13 -050020#include "cifsproto.h"
21static const struct cred *spnego_cred;
Steve Frenchf1d662a2007-11-05 14:38:08 +000022
23/* create a new cifs key */
24static int
David Howellscf7f6012012-09-13 13:06:29 +010025cifs_spnego_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
Steve Frenchf1d662a2007-11-05 14:38:08 +000026{
27 char *payload;
28 int ret;
29
30 ret = -ENOMEM;
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +020031 payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
Steve Frenchf1d662a2007-11-05 14:38:08 +000032 if (!payload)
33 goto error;
34
35 /* attach the data */
David Howells146aa8b2015-10-21 14:04:48 +010036 key->payload.data[0] = payload;
Steve Frenchf1d662a2007-11-05 14:38:08 +000037 ret = 0;
38
39error:
40 return ret;
41}
42
43static void
44cifs_spnego_key_destroy(struct key *key)
45{
David Howells146aa8b2015-10-21 14:04:48 +010046 kfree(key->payload.data[0]);
Steve Frenchf1d662a2007-11-05 14:38:08 +000047}
48
49
50/*
51 * keytype for CIFS spnego keys
52 */
53struct key_type cifs_spnego_key_type = {
54 .name = "cifs.spnego",
55 .instantiate = cifs_spnego_key_instantiate,
Steve Frenchf1d662a2007-11-05 14:38:08 +000056 .destroy = cifs_spnego_key_destroy,
57 .describe = user_describe,
58};
59
Steve French7c9c3762008-09-23 17:23:09 +000060/* length of longest version string e.g. strlen("ver=0xFF") */
61#define MAX_VER_STR_LEN 8
62
63/* length of longest security mechanism name, eg in future could have
64 * strlen(";sec=ntlmsspi") */
65#define MAX_MECH_STR_LEN 13
66
Steve French7c9c3762008-09-23 17:23:09 +000067/* strlen of "host=" */
68#define HOST_KEY_LEN 5
69
70/* strlen of ";ip4=" or ";ip6=" */
71#define IP_KEY_LEN 5
72
73/* strlen of ";uid=0x" */
74#define UID_KEY_LEN 7
75
Jeff Laytonba5dadb2010-08-03 10:19:50 -040076/* strlen of ";creduid=0x" */
77#define CREDUID_KEY_LEN 11
78
Steve French7c9c3762008-09-23 17:23:09 +000079/* strlen of ";user=" */
80#define USER_KEY_LEN 6
81
Jeff Laytonc4c1bff2009-07-09 20:02:48 -040082/* strlen of ";pid=0x" */
83#define PID_KEY_LEN 7
84
Steve Frenchf1d662a2007-11-05 14:38:08 +000085/* get a key struct with a SPNEGO security blob, suitable for session setup */
86struct key *
Steve French96daf2b2011-05-27 04:34:02 +000087cifs_get_spnego_key(struct cifs_ses *sesInfo)
Steve Frenchf1d662a2007-11-05 14:38:08 +000088{
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +020089 struct TCP_Server_Info *server = cifs_ses_server(sesInfo);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +030090 struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr;
91 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr;
Steve Frenchf1d662a2007-11-05 14:38:08 +000092 char *description, *dp;
93 size_t desc_len;
94 struct key *spnego_key;
Jeff Laytond6c2e4d2007-11-16 22:23:17 +000095 const char *hostname = server->hostname;
Sachin Prabhub74cb9a2016-05-17 18:20:13 -050096 const struct cred *saved_cred;
Steve Frenchf1d662a2007-11-05 14:38:08 +000097
Jeff Layton66b8bd32008-08-01 17:54:32 +000098 /* length of fields (with semicolons): ver=0xyz ip4=ipaddress
99 host=hostname sec=mechanism uid=0xFF user=username */
100 desc_len = MAX_VER_STR_LEN +
Steve French7c9c3762008-09-23 17:23:09 +0000101 HOST_KEY_LEN + strlen(hostname) +
Jeff Layton50b64e32009-06-02 06:55:20 -0400102 IP_KEY_LEN + INET6_ADDRSTRLEN +
Jeff Layton66b8bd32008-08-01 17:54:32 +0000103 MAX_MECH_STR_LEN +
Steve French7c9c3762008-09-23 17:23:09 +0000104 UID_KEY_LEN + (sizeof(uid_t) * 2) +
Jeff Laytonba5dadb2010-08-03 10:19:50 -0400105 CREDUID_KEY_LEN + (sizeof(uid_t) * 2) +
Jeff Laytonc4c1bff2009-07-09 20:02:48 -0400106 PID_KEY_LEN + (sizeof(pid_t) * 2) + 1;
Jeff Layton66b8bd32008-08-01 17:54:32 +0000107
Jeff Layton04febab2012-01-17 16:09:15 -0500108 if (sesInfo->user_name)
109 desc_len += USER_KEY_LEN + strlen(sesInfo->user_name);
110
Steve Frenchf1d662a2007-11-05 14:38:08 +0000111 spnego_key = ERR_PTR(-ENOMEM);
112 description = kzalloc(desc_len, GFP_KERNEL);
113 if (description == NULL)
114 goto out;
115
116 dp = description;
117 /* start with version and hostname portion of UNC string */
118 spnego_key = ERR_PTR(-EINVAL);
Steve French68bf7282007-11-16 18:32:52 +0000119 sprintf(dp, "ver=0x%x;host=%s;", CIFS_SPNEGO_UPCALL_VERSION,
Steve Frenchf1d662a2007-11-05 14:38:08 +0000120 hostname);
121 dp = description + strlen(description);
122
123 /* add the server address */
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300124 if (server->dstaddr.ss_family == AF_INET)
125 sprintf(dp, "ip4=%pI4", &sa->sin_addr);
126 else if (server->dstaddr.ss_family == AF_INET6)
127 sprintf(dp, "ip6=%pI6", &sa6->sin6_addr);
Steve Frenchf1d662a2007-11-05 14:38:08 +0000128 else
129 goto out;
130
131 dp = description + strlen(description);
132
Steve Frenchc16fefa2008-08-19 19:35:33 +0000133 /* for now, only sec=krb5 and sec=mskrb5 are valid */
Jeff Layton26efa0b2010-04-24 07:57:49 -0400134 if (server->sec_kerberos)
Steve Frenchf1d662a2007-11-05 14:38:08 +0000135 sprintf(dp, ";sec=krb5");
Jeff Layton26efa0b2010-04-24 07:57:49 -0400136 else if (server->sec_mskerberos)
Steve Frenchc16fefa2008-08-19 19:35:33 +0000137 sprintf(dp, ";sec=mskrb5");
Steve French926674d2018-10-28 13:13:23 -0500138 else {
139 cifs_dbg(VFS, "unknown or missing server auth type, use krb5\n");
140 sprintf(dp, ";sec=krb5");
141 }
Steve Frenchf1d662a2007-11-05 14:38:08 +0000142
Igor Mammedov9eae8a82007-11-08 16:13:31 +0000143 dp = description + strlen(description);
Eric W. Biederman64ed39d2013-02-06 02:30:39 -0800144 sprintf(dp, ";uid=0x%x",
145 from_kuid_munged(&init_user_ns, sesInfo->linux_uid));
Igor Mammedov9eae8a82007-11-08 16:13:31 +0000146
Igor Mammedove4058242008-04-02 17:33:47 +0400147 dp = description + strlen(description);
Eric W. Biederman64ed39d2013-02-06 02:30:39 -0800148 sprintf(dp, ";creduid=0x%x",
149 from_kuid_munged(&init_user_ns, sesInfo->cred_uid));
Jeff Layton3e4b3e12010-07-19 18:00:17 -0400150
Jeff Layton04febab2012-01-17 16:09:15 -0500151 if (sesInfo->user_name) {
152 dp = description + strlen(description);
153 sprintf(dp, ";user=%s", sesInfo->user_name);
154 }
Igor Mammedove4058242008-04-02 17:33:47 +0400155
Jeff Laytonc4c1bff2009-07-09 20:02:48 -0400156 dp = description + strlen(description);
157 sprintf(dp, ";pid=0x%x", current->pid);
158
Joe Perchesf96637b2013-05-04 22:12:25 -0500159 cifs_dbg(FYI, "key description = %s\n", description);
Sachin Prabhub74cb9a2016-05-17 18:20:13 -0500160 saved_cred = override_creds(spnego_cred);
Linus Torvalds028db3e2019-07-10 18:43:43 -0700161 spnego_key = request_key(&cifs_spnego_key_type, description, "");
Sachin Prabhub74cb9a2016-05-17 18:20:13 -0500162 revert_creds(saved_cred);
Steve Frenchf1d662a2007-11-05 14:38:08 +0000163
Jeff Layton05b3de62007-12-31 00:51:45 +0000164#ifdef CONFIG_CIFS_DEBUG2
Steve Frenchf1d662a2007-11-05 14:38:08 +0000165 if (cifsFYI && !IS_ERR(spnego_key)) {
David Howells146aa8b2015-10-21 14:04:48 +0100166 struct cifs_spnego_msg *msg = spnego_key->payload.data[0];
Andrew Mortonead03e32008-02-05 15:51:24 +0000167 cifs_dump_mem("SPNEGO reply blob:", msg->data, min(1024U,
Jeff Layton05b3de62007-12-31 00:51:45 +0000168 msg->secblob_len + msg->sesskey_len));
Steve Frenchf1d662a2007-11-05 14:38:08 +0000169 }
Jeff Layton05b3de62007-12-31 00:51:45 +0000170#endif /* CONFIG_CIFS_DEBUG2 */
Steve Frenchf1d662a2007-11-05 14:38:08 +0000171
172out:
173 kfree(description);
174 return spnego_key;
175}
Sachin Prabhub74cb9a2016-05-17 18:20:13 -0500176
177int
178init_cifs_spnego(void)
179{
180 struct cred *cred;
181 struct key *keyring;
182 int ret;
183
184 cifs_dbg(FYI, "Registering the %s key type\n",
185 cifs_spnego_key_type.name);
186
187 /*
188 * Create an override credential set with special thread keyring for
189 * spnego upcalls.
190 */
191
192 cred = prepare_kernel_cred(NULL);
193 if (!cred)
194 return -ENOMEM;
195
196 keyring = keyring_alloc(".cifs_spnego",
197 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700198 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
199 KEY_USR_VIEW | KEY_USR_READ,
Sachin Prabhub74cb9a2016-05-17 18:20:13 -0500200 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
201 if (IS_ERR(keyring)) {
202 ret = PTR_ERR(keyring);
203 goto failed_put_cred;
204 }
205
206 ret = register_key_type(&cifs_spnego_key_type);
207 if (ret < 0)
208 goto failed_put_key;
209
210 /*
211 * instruct request_key() to use this special keyring as a cache for
212 * the results it looks up
213 */
214 set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
215 cred->thread_keyring = keyring;
216 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
217 spnego_cred = cred;
218
219 cifs_dbg(FYI, "cifs spnego keyring: %d\n", key_serial(keyring));
220 return 0;
221
222failed_put_key:
223 key_put(keyring);
224failed_put_cred:
225 put_cred(cred);
226 return ret;
227}
228
229void
230exit_cifs_spnego(void)
231{
232 key_revoke(spnego_cred->thread_keyring);
233 unregister_key_type(&cifs_spnego_key_type);
234 put_cred(spnego_cred);
235 cifs_dbg(FYI, "Unregistered %s key type\n", cifs_spnego_key_type.name);
236}