18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * fs/cifs/cifs_spnego.c -- SPNEGO upcall management for CIFS 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (c) 2007 Red Hat, Inc. 58c2ecf20Sopenharmony_ci * Author(s): Jeff Layton (jlayton@redhat.com) 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This library is free software; you can redistribute it and/or modify 88c2ecf20Sopenharmony_ci * it under the terms of the GNU Lesser General Public License as published 98c2ecf20Sopenharmony_ci * by the Free Software Foundation; either version 2.1 of the License, or 108c2ecf20Sopenharmony_ci * (at your option) any later version. 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * This library is distributed in the hope that it will be useful, 138c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 148c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 158c2ecf20Sopenharmony_ci * the GNU Lesser General Public License for more details. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * You should have received a copy of the GNU Lesser General Public License 188c2ecf20Sopenharmony_ci * along with this library; if not, write to the Free Software 198c2ecf20Sopenharmony_ci * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <linux/list.h> 238c2ecf20Sopenharmony_ci#include <linux/slab.h> 248c2ecf20Sopenharmony_ci#include <linux/string.h> 258c2ecf20Sopenharmony_ci#include <keys/user-type.h> 268c2ecf20Sopenharmony_ci#include <linux/key-type.h> 278c2ecf20Sopenharmony_ci#include <linux/keyctl.h> 288c2ecf20Sopenharmony_ci#include <linux/inet.h> 298c2ecf20Sopenharmony_ci#include "cifsglob.h" 308c2ecf20Sopenharmony_ci#include "cifs_spnego.h" 318c2ecf20Sopenharmony_ci#include "cifs_debug.h" 328c2ecf20Sopenharmony_ci#include "cifsproto.h" 338c2ecf20Sopenharmony_cistatic const struct cred *spnego_cred; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* create a new cifs key */ 368c2ecf20Sopenharmony_cistatic int 378c2ecf20Sopenharmony_cicifs_spnego_key_instantiate(struct key *key, struct key_preparsed_payload *prep) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci char *payload; 408c2ecf20Sopenharmony_ci int ret; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci ret = -ENOMEM; 438c2ecf20Sopenharmony_ci payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL); 448c2ecf20Sopenharmony_ci if (!payload) 458c2ecf20Sopenharmony_ci goto error; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci /* attach the data */ 488c2ecf20Sopenharmony_ci key->payload.data[0] = payload; 498c2ecf20Sopenharmony_ci ret = 0; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cierror: 528c2ecf20Sopenharmony_ci return ret; 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic void 568c2ecf20Sopenharmony_cicifs_spnego_key_destroy(struct key *key) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci kfree(key->payload.data[0]); 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* 638c2ecf20Sopenharmony_ci * keytype for CIFS spnego keys 648c2ecf20Sopenharmony_ci */ 658c2ecf20Sopenharmony_cistruct key_type cifs_spnego_key_type = { 668c2ecf20Sopenharmony_ci .name = "cifs.spnego", 678c2ecf20Sopenharmony_ci .instantiate = cifs_spnego_key_instantiate, 688c2ecf20Sopenharmony_ci .destroy = cifs_spnego_key_destroy, 698c2ecf20Sopenharmony_ci .describe = user_describe, 708c2ecf20Sopenharmony_ci}; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/* length of longest version string e.g. strlen("ver=0xFF") */ 738c2ecf20Sopenharmony_ci#define MAX_VER_STR_LEN 8 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci/* length of longest security mechanism name, eg in future could have 768c2ecf20Sopenharmony_ci * strlen(";sec=ntlmsspi") */ 778c2ecf20Sopenharmony_ci#define MAX_MECH_STR_LEN 13 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/* strlen of ";host=" */ 808c2ecf20Sopenharmony_ci#define HOST_KEY_LEN 6 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci/* strlen of ";ip4=" or ";ip6=" */ 838c2ecf20Sopenharmony_ci#define IP_KEY_LEN 5 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/* strlen of ";uid=0x" */ 868c2ecf20Sopenharmony_ci#define UID_KEY_LEN 7 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci/* strlen of ";creduid=0x" */ 898c2ecf20Sopenharmony_ci#define CREDUID_KEY_LEN 11 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* strlen of ";user=" */ 928c2ecf20Sopenharmony_ci#define USER_KEY_LEN 6 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci/* strlen of ";pid=0x" */ 958c2ecf20Sopenharmony_ci#define PID_KEY_LEN 7 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* get a key struct with a SPNEGO security blob, suitable for session setup */ 988c2ecf20Sopenharmony_cistruct key * 998c2ecf20Sopenharmony_cicifs_get_spnego_key(struct cifs_ses *sesInfo) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci struct TCP_Server_Info *server = cifs_ses_server(sesInfo); 1028c2ecf20Sopenharmony_ci struct sockaddr_in *sa = (struct sockaddr_in *) &server->dstaddr; 1038c2ecf20Sopenharmony_ci struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &server->dstaddr; 1048c2ecf20Sopenharmony_ci char *description, *dp; 1058c2ecf20Sopenharmony_ci size_t desc_len; 1068c2ecf20Sopenharmony_ci struct key *spnego_key; 1078c2ecf20Sopenharmony_ci const char *hostname = server->hostname; 1088c2ecf20Sopenharmony_ci const struct cred *saved_cred; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci /* length of fields (with semicolons): ver=0xyz ip4=ipaddress 1118c2ecf20Sopenharmony_ci host=hostname sec=mechanism uid=0xFF user=username */ 1128c2ecf20Sopenharmony_ci desc_len = MAX_VER_STR_LEN + 1138c2ecf20Sopenharmony_ci HOST_KEY_LEN + strlen(hostname) + 1148c2ecf20Sopenharmony_ci IP_KEY_LEN + INET6_ADDRSTRLEN + 1158c2ecf20Sopenharmony_ci MAX_MECH_STR_LEN + 1168c2ecf20Sopenharmony_ci UID_KEY_LEN + (sizeof(uid_t) * 2) + 1178c2ecf20Sopenharmony_ci CREDUID_KEY_LEN + (sizeof(uid_t) * 2) + 1188c2ecf20Sopenharmony_ci PID_KEY_LEN + (sizeof(pid_t) * 2) + 1; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (sesInfo->user_name) 1218c2ecf20Sopenharmony_ci desc_len += USER_KEY_LEN + strlen(sesInfo->user_name); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci spnego_key = ERR_PTR(-ENOMEM); 1248c2ecf20Sopenharmony_ci description = kzalloc(desc_len, GFP_KERNEL); 1258c2ecf20Sopenharmony_ci if (description == NULL) 1268c2ecf20Sopenharmony_ci goto out; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci dp = description; 1298c2ecf20Sopenharmony_ci /* start with version and hostname portion of UNC string */ 1308c2ecf20Sopenharmony_ci spnego_key = ERR_PTR(-EINVAL); 1318c2ecf20Sopenharmony_ci sprintf(dp, "ver=0x%x;host=%s;", CIFS_SPNEGO_UPCALL_VERSION, 1328c2ecf20Sopenharmony_ci hostname); 1338c2ecf20Sopenharmony_ci dp = description + strlen(description); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* add the server address */ 1368c2ecf20Sopenharmony_ci if (server->dstaddr.ss_family == AF_INET) 1378c2ecf20Sopenharmony_ci sprintf(dp, "ip4=%pI4", &sa->sin_addr); 1388c2ecf20Sopenharmony_ci else if (server->dstaddr.ss_family == AF_INET6) 1398c2ecf20Sopenharmony_ci sprintf(dp, "ip6=%pI6", &sa6->sin6_addr); 1408c2ecf20Sopenharmony_ci else 1418c2ecf20Sopenharmony_ci goto out; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci dp = description + strlen(description); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci /* for now, only sec=krb5 and sec=mskrb5 are valid */ 1468c2ecf20Sopenharmony_ci if (server->sec_kerberos) 1478c2ecf20Sopenharmony_ci sprintf(dp, ";sec=krb5"); 1488c2ecf20Sopenharmony_ci else if (server->sec_mskerberos) 1498c2ecf20Sopenharmony_ci sprintf(dp, ";sec=mskrb5"); 1508c2ecf20Sopenharmony_ci else { 1518c2ecf20Sopenharmony_ci cifs_dbg(VFS, "unknown or missing server auth type, use krb5\n"); 1528c2ecf20Sopenharmony_ci sprintf(dp, ";sec=krb5"); 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci dp = description + strlen(description); 1568c2ecf20Sopenharmony_ci sprintf(dp, ";uid=0x%x", 1578c2ecf20Sopenharmony_ci from_kuid_munged(&init_user_ns, sesInfo->linux_uid)); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci dp = description + strlen(description); 1608c2ecf20Sopenharmony_ci sprintf(dp, ";creduid=0x%x", 1618c2ecf20Sopenharmony_ci from_kuid_munged(&init_user_ns, sesInfo->cred_uid)); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci if (sesInfo->user_name) { 1648c2ecf20Sopenharmony_ci dp = description + strlen(description); 1658c2ecf20Sopenharmony_ci sprintf(dp, ";user=%s", sesInfo->user_name); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci dp = description + strlen(description); 1698c2ecf20Sopenharmony_ci sprintf(dp, ";pid=0x%x", current->pid); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci cifs_dbg(FYI, "key description = %s\n", description); 1728c2ecf20Sopenharmony_ci saved_cred = override_creds(spnego_cred); 1738c2ecf20Sopenharmony_ci spnego_key = request_key(&cifs_spnego_key_type, description, ""); 1748c2ecf20Sopenharmony_ci revert_creds(saved_cred); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci#ifdef CONFIG_CIFS_DEBUG2 1778c2ecf20Sopenharmony_ci if (cifsFYI && !IS_ERR(spnego_key)) { 1788c2ecf20Sopenharmony_ci struct cifs_spnego_msg *msg = spnego_key->payload.data[0]; 1798c2ecf20Sopenharmony_ci cifs_dump_mem("SPNEGO reply blob:", msg->data, min(1024U, 1808c2ecf20Sopenharmony_ci msg->secblob_len + msg->sesskey_len)); 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci#endif /* CONFIG_CIFS_DEBUG2 */ 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ciout: 1858c2ecf20Sopenharmony_ci kfree(description); 1868c2ecf20Sopenharmony_ci return spnego_key; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ciint 1908c2ecf20Sopenharmony_ciinit_cifs_spnego(void) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci struct cred *cred; 1938c2ecf20Sopenharmony_ci struct key *keyring; 1948c2ecf20Sopenharmony_ci int ret; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci cifs_dbg(FYI, "Registering the %s key type\n", 1978c2ecf20Sopenharmony_ci cifs_spnego_key_type.name); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* 2008c2ecf20Sopenharmony_ci * Create an override credential set with special thread keyring for 2018c2ecf20Sopenharmony_ci * spnego upcalls. 2028c2ecf20Sopenharmony_ci */ 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci cred = prepare_kernel_cred(NULL); 2058c2ecf20Sopenharmony_ci if (!cred) 2068c2ecf20Sopenharmony_ci return -ENOMEM; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci keyring = keyring_alloc(".cifs_spnego", 2098c2ecf20Sopenharmony_ci GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 2108c2ecf20Sopenharmony_ci (KEY_POS_ALL & ~KEY_POS_SETATTR) | 2118c2ecf20Sopenharmony_ci KEY_USR_VIEW | KEY_USR_READ, 2128c2ecf20Sopenharmony_ci KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); 2138c2ecf20Sopenharmony_ci if (IS_ERR(keyring)) { 2148c2ecf20Sopenharmony_ci ret = PTR_ERR(keyring); 2158c2ecf20Sopenharmony_ci goto failed_put_cred; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci ret = register_key_type(&cifs_spnego_key_type); 2198c2ecf20Sopenharmony_ci if (ret < 0) 2208c2ecf20Sopenharmony_ci goto failed_put_key; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* 2238c2ecf20Sopenharmony_ci * instruct request_key() to use this special keyring as a cache for 2248c2ecf20Sopenharmony_ci * the results it looks up 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_ci set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags); 2278c2ecf20Sopenharmony_ci cred->thread_keyring = keyring; 2288c2ecf20Sopenharmony_ci cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; 2298c2ecf20Sopenharmony_ci spnego_cred = cred; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci cifs_dbg(FYI, "cifs spnego keyring: %d\n", key_serial(keyring)); 2328c2ecf20Sopenharmony_ci return 0; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cifailed_put_key: 2358c2ecf20Sopenharmony_ci key_put(keyring); 2368c2ecf20Sopenharmony_cifailed_put_cred: 2378c2ecf20Sopenharmony_ci put_cred(cred); 2388c2ecf20Sopenharmony_ci return ret; 2398c2ecf20Sopenharmony_ci} 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_civoid 2428c2ecf20Sopenharmony_ciexit_cifs_spnego(void) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci key_revoke(spnego_cred->thread_keyring); 2458c2ecf20Sopenharmony_ci unregister_key_type(&cifs_spnego_key_type); 2468c2ecf20Sopenharmony_ci put_cred(spnego_cred); 2478c2ecf20Sopenharmony_ci cifs_dbg(FYI, "Unregistered %s key type\n", cifs_spnego_key_type.name); 2488c2ecf20Sopenharmony_ci} 249