Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 2 | /* Kerberos-based RxRPC security |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 10 | #include <crypto/skcipher.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/net.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/udp.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
| 16 | #include <linux/ctype.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David Howells | 12da59f | 2020-09-16 08:37:29 +0100 | [diff] [blame] | 18 | #include <linux/key-type.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 19 | #include <net/sock.h> |
| 20 | #include <net/af_rxrpc.h> |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 21 | #include <keys/rxrpc-type.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 22 | #include "ar-internal.h" |
| 23 | |
| 24 | #define RXKAD_VERSION 2 |
| 25 | #define MAXKRB5TICKETLEN 1024 |
| 26 | #define RXKAD_TKT_TYPE_KERBEROS_V5 256 |
| 27 | #define ANAME_SZ 40 /* size of authentication name */ |
| 28 | #define INST_SZ 40 /* size of principal's instance */ |
| 29 | #define REALM_SZ 40 /* size of principal's auth domain */ |
| 30 | #define SNAME_SZ 40 /* size of service name */ |
| 31 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 32 | struct rxkad_level1_hdr { |
| 33 | __be32 data_size; /* true data size (excluding padding) */ |
| 34 | }; |
| 35 | |
| 36 | struct rxkad_level2_hdr { |
| 37 | __be32 data_size; /* true data size (excluding padding) */ |
| 38 | __be32 checksum; /* decrypted data checksum */ |
| 39 | }; |
| 40 | |
David Howells | 8d47a43 | 2020-09-16 01:38:15 +0100 | [diff] [blame^] | 41 | static int rxkad_prime_packet_security(struct rxrpc_connection *conn, |
| 42 | struct crypto_sync_skcipher *ci); |
| 43 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 44 | /* |
| 45 | * this holds a pinned cipher so that keventd doesn't get called by the cipher |
| 46 | * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE |
| 47 | * packets |
| 48 | */ |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 49 | static struct crypto_sync_skcipher *rxkad_ci; |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 50 | static struct skcipher_request *rxkad_ci_req; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 51 | static DEFINE_MUTEX(rxkad_ci_mutex); |
| 52 | |
| 53 | /* |
David Howells | 12da59f | 2020-09-16 08:37:29 +0100 | [diff] [blame] | 54 | * Parse the information from a server key |
| 55 | * |
| 56 | * The data should be the 8-byte secret key. |
| 57 | */ |
| 58 | static int rxkad_preparse_server_key(struct key_preparsed_payload *prep) |
| 59 | { |
| 60 | struct crypto_skcipher *ci; |
| 61 | |
| 62 | if (prep->datalen != 8) |
| 63 | return -EINVAL; |
| 64 | |
| 65 | memcpy(&prep->payload.data[2], prep->data, 8); |
| 66 | |
| 67 | ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC); |
| 68 | if (IS_ERR(ci)) { |
| 69 | _leave(" = %ld", PTR_ERR(ci)); |
| 70 | return PTR_ERR(ci); |
| 71 | } |
| 72 | |
| 73 | if (crypto_skcipher_setkey(ci, prep->data, 8) < 0) |
| 74 | BUG(); |
| 75 | |
| 76 | prep->payload.data[0] = ci; |
| 77 | _leave(" = 0"); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static void rxkad_free_preparse_server_key(struct key_preparsed_payload *prep) |
| 82 | { |
| 83 | |
| 84 | if (prep->payload.data[0]) |
| 85 | crypto_free_skcipher(prep->payload.data[0]); |
| 86 | } |
| 87 | |
| 88 | static void rxkad_destroy_server_key(struct key *key) |
| 89 | { |
| 90 | if (key->payload.data[0]) { |
| 91 | crypto_free_skcipher(key->payload.data[0]); |
| 92 | key->payload.data[0] = NULL; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 97 | * initialise connection security |
| 98 | */ |
David Howells | 41057eb | 2020-09-16 08:19:12 +0100 | [diff] [blame] | 99 | static int rxkad_init_connection_security(struct rxrpc_connection *conn, |
| 100 | struct rxrpc_key_token *token) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 101 | { |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 102 | struct crypto_sync_skcipher *ci; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 103 | int ret; |
| 104 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 105 | _enter("{%d},{%x}", conn->debug_id, key_serial(conn->params.key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 106 | |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 107 | conn->security_ix = token->security_index; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 108 | |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 109 | ci = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 110 | if (IS_ERR(ci)) { |
| 111 | _debug("no cipher"); |
| 112 | ret = PTR_ERR(ci); |
| 113 | goto error; |
| 114 | } |
| 115 | |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 116 | if (crypto_sync_skcipher_setkey(ci, token->kad->session_key, |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 117 | sizeof(token->kad->session_key)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 118 | BUG(); |
| 119 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 120 | switch (conn->params.security_level) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 121 | case RXRPC_SECURITY_PLAIN: |
| 122 | break; |
| 123 | case RXRPC_SECURITY_AUTH: |
| 124 | conn->size_align = 8; |
| 125 | conn->security_size = sizeof(struct rxkad_level1_hdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 126 | break; |
| 127 | case RXRPC_SECURITY_ENCRYPT: |
| 128 | conn->size_align = 8; |
| 129 | conn->security_size = sizeof(struct rxkad_level2_hdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 130 | break; |
| 131 | default: |
| 132 | ret = -EKEYREJECTED; |
| 133 | goto error; |
| 134 | } |
| 135 | |
David Howells | 8d47a43 | 2020-09-16 01:38:15 +0100 | [diff] [blame^] | 136 | ret = rxkad_prime_packet_security(conn, ci); |
| 137 | if (ret < 0) |
| 138 | goto error_ci; |
| 139 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 140 | conn->cipher = ci; |
David Howells | 8d47a43 | 2020-09-16 01:38:15 +0100 | [diff] [blame^] | 141 | return 0; |
| 142 | |
| 143 | error_ci: |
| 144 | crypto_free_sync_skcipher(ci); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 145 | error: |
| 146 | _leave(" = %d", ret); |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * prime the encryption state with the invariant parts of a connection's |
| 152 | * description |
| 153 | */ |
David Howells | 8d47a43 | 2020-09-16 01:38:15 +0100 | [diff] [blame^] | 154 | static int rxkad_prime_packet_security(struct rxrpc_connection *conn, |
| 155 | struct crypto_sync_skcipher *ci) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 156 | { |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 157 | struct skcipher_request *req; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 158 | struct rxrpc_key_token *token; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 159 | struct scatterlist sg; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 160 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 161 | __be32 *tmpbuf; |
| 162 | size_t tmpsize = 4 * sizeof(__be32); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 163 | |
| 164 | _enter(""); |
| 165 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 166 | if (!conn->params.key) |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 167 | return 0; |
| 168 | |
| 169 | tmpbuf = kmalloc(tmpsize, GFP_KERNEL); |
| 170 | if (!tmpbuf) |
| 171 | return -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 172 | |
David Howells | 8d47a43 | 2020-09-16 01:38:15 +0100 | [diff] [blame^] | 173 | req = skcipher_request_alloc(&ci->base, GFP_NOFS); |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 174 | if (!req) { |
| 175 | kfree(tmpbuf); |
| 176 | return -ENOMEM; |
| 177 | } |
| 178 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 179 | token = conn->params.key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 180 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 181 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 182 | tmpbuf[0] = htonl(conn->proto.epoch); |
| 183 | tmpbuf[1] = htonl(conn->proto.cid); |
| 184 | tmpbuf[2] = 0; |
| 185 | tmpbuf[3] = htonl(conn->security_ix); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 186 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 187 | sg_init_one(&sg, tmpbuf, tmpsize); |
David Howells | 8d47a43 | 2020-09-16 01:38:15 +0100 | [diff] [blame^] | 188 | skcipher_request_set_sync_tfm(req, ci); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 189 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 190 | skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 191 | crypto_skcipher_encrypt(req); |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 192 | skcipher_request_free(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 193 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 194 | memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv)); |
| 195 | kfree(tmpbuf); |
| 196 | _leave(" = 0"); |
| 197 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 201 | * Allocate and prepare the crypto request on a call. For any particular call, |
| 202 | * this is called serially for the packets, so no lock should be necessary. |
| 203 | */ |
| 204 | static struct skcipher_request *rxkad_get_call_crypto(struct rxrpc_call *call) |
| 205 | { |
| 206 | struct crypto_skcipher *tfm = &call->conn->cipher->base; |
| 207 | struct skcipher_request *cipher_req = call->cipher_req; |
| 208 | |
| 209 | if (!cipher_req) { |
| 210 | cipher_req = skcipher_request_alloc(tfm, GFP_NOFS); |
| 211 | if (!cipher_req) |
| 212 | return NULL; |
| 213 | call->cipher_req = cipher_req; |
| 214 | } |
| 215 | |
| 216 | return cipher_req; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Clean up the crypto on a call. |
| 221 | */ |
| 222 | static void rxkad_free_call_crypto(struct rxrpc_call *call) |
| 223 | { |
| 224 | if (call->cipher_req) |
| 225 | skcipher_request_free(call->cipher_req); |
| 226 | call->cipher_req = NULL; |
| 227 | } |
| 228 | |
| 229 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 230 | * partially encrypt a packet (level 1 security) |
| 231 | */ |
| 232 | static int rxkad_secure_packet_auth(const struct rxrpc_call *call, |
| 233 | struct sk_buff *skb, |
| 234 | u32 data_size, |
Kees Cook | 54424d3 | 2018-08-03 10:15:25 +0100 | [diff] [blame] | 235 | void *sechdr, |
| 236 | struct skcipher_request *req) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 237 | { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 238 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 239 | struct rxkad_level1_hdr hdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 240 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 241 | struct scatterlist sg; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 242 | u16 check; |
| 243 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 244 | _enter(""); |
| 245 | |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 246 | check = sp->hdr.seq ^ call->call_id; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 247 | data_size |= (u32)check << 16; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 248 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 249 | hdr.data_size = htonl(data_size); |
| 250 | memcpy(sechdr, &hdr, sizeof(hdr)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 251 | |
| 252 | /* start the encryption afresh */ |
| 253 | memset(&iv, 0, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 254 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 255 | sg_init_one(&sg, sechdr, 8); |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 256 | skcipher_request_set_sync_tfm(req, call->conn->cipher); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 257 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 258 | skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 259 | crypto_skcipher_encrypt(req); |
| 260 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 261 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 262 | _leave(" = 0"); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * wholly encrypt a packet (level 2 security) |
| 268 | */ |
| 269 | static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call, |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame] | 270 | struct sk_buff *skb, |
| 271 | u32 data_size, |
Kees Cook | 54424d3 | 2018-08-03 10:15:25 +0100 | [diff] [blame] | 272 | void *sechdr, |
| 273 | struct skcipher_request *req) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 274 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 275 | const struct rxrpc_key_token *token; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 276 | struct rxkad_level2_hdr rxkhdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 277 | struct rxrpc_skb_priv *sp; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 278 | struct rxrpc_crypt iv; |
| 279 | struct scatterlist sg[16]; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 280 | unsigned int len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 281 | u16 check; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 282 | int err; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 283 | |
| 284 | sp = rxrpc_skb(skb); |
| 285 | |
| 286 | _enter(""); |
| 287 | |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 288 | check = sp->hdr.seq ^ call->call_id; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 289 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 290 | rxkhdr.data_size = htonl(data_size | (u32)check << 16); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 291 | rxkhdr.checksum = 0; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 292 | memcpy(sechdr, &rxkhdr, sizeof(rxkhdr)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 293 | |
| 294 | /* encrypt from the session key */ |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 295 | token = call->conn->params.key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 296 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 297 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 298 | sg_init_one(&sg[0], sechdr, sizeof(rxkhdr)); |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 299 | skcipher_request_set_sync_tfm(req, call->conn->cipher); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 300 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 301 | skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 302 | crypto_skcipher_encrypt(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 303 | |
| 304 | /* we want to encrypt the skbuff in-place */ |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 305 | err = -EMSGSIZE; |
| 306 | if (skb_shinfo(skb)->nr_frags > 16) |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 307 | goto out; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 308 | |
| 309 | len = data_size + call->conn->size_align - 1; |
| 310 | len &= ~(call->conn->size_align - 1); |
| 311 | |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 312 | sg_init_table(sg, ARRAY_SIZE(sg)); |
Jason A. Donenfeld | 89a5ea9 | 2017-06-04 04:16:24 +0200 | [diff] [blame] | 313 | err = skb_to_sgvec(skb, sg, 0, len); |
| 314 | if (unlikely(err < 0)) |
| 315 | goto out; |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 316 | skcipher_request_set_crypt(req, sg, sg, len, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 317 | crypto_skcipher_encrypt(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 318 | |
| 319 | _leave(" = 0"); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 320 | err = 0; |
| 321 | |
| 322 | out: |
| 323 | skcipher_request_zero(req); |
| 324 | return err; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
| 328 | * checksum an RxRPC packet header |
| 329 | */ |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 330 | static int rxkad_secure_packet(struct rxrpc_call *call, |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame] | 331 | struct sk_buff *skb, |
| 332 | size_t data_size, |
| 333 | void *sechdr) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 334 | { |
| 335 | struct rxrpc_skb_priv *sp; |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 336 | struct skcipher_request *req; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 337 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 338 | struct scatterlist sg; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 339 | u32 x, y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 340 | int ret; |
| 341 | |
| 342 | sp = rxrpc_skb(skb); |
| 343 | |
| 344 | _enter("{%d{%x}},{#%u},%zu,", |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 345 | call->debug_id, key_serial(call->conn->params.key), |
| 346 | sp->hdr.seq, data_size); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 347 | |
| 348 | if (!call->conn->cipher) |
| 349 | return 0; |
| 350 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 351 | ret = key_validate(call->conn->params.key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 352 | if (ret < 0) |
| 353 | return ret; |
| 354 | |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 355 | req = rxkad_get_call_crypto(call); |
| 356 | if (!req) |
| 357 | return -ENOMEM; |
| 358 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 359 | /* continue encrypting from where we left off */ |
| 360 | memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 361 | |
| 362 | /* calculate the security checksum */ |
David Howells | 01a90a4 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 363 | x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 364 | x |= sp->hdr.seq & 0x3fffffff; |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 365 | call->crypto_buf[0] = htonl(call->call_id); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 366 | call->crypto_buf[1] = htonl(x); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 367 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 368 | sg_init_one(&sg, call->crypto_buf, 8); |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 369 | skcipher_request_set_sync_tfm(req, call->conn->cipher); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 370 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 371 | skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 372 | crypto_skcipher_encrypt(req); |
| 373 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 374 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 375 | y = ntohl(call->crypto_buf[1]); |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 376 | y = (y >> 16) & 0xffff; |
| 377 | if (y == 0) |
| 378 | y = 1; /* zero checksums are not permitted */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 379 | sp->hdr.cksum = y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 380 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 381 | switch (call->conn->params.security_level) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 382 | case RXRPC_SECURITY_PLAIN: |
| 383 | ret = 0; |
| 384 | break; |
| 385 | case RXRPC_SECURITY_AUTH: |
Kees Cook | 54424d3 | 2018-08-03 10:15:25 +0100 | [diff] [blame] | 386 | ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr, |
| 387 | req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 388 | break; |
| 389 | case RXRPC_SECURITY_ENCRYPT: |
| 390 | ret = rxkad_secure_packet_encrypt(call, skb, data_size, |
Kees Cook | 54424d3 | 2018-08-03 10:15:25 +0100 | [diff] [blame] | 391 | sechdr, req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 392 | break; |
| 393 | default: |
| 394 | ret = -EPERM; |
| 395 | break; |
| 396 | } |
| 397 | |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 398 | _leave(" = %d [set %hx]", ret, y); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * decrypt partial encryption on a packet (level 1 security) |
| 404 | */ |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 405 | static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb, |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 406 | unsigned int offset, unsigned int len, |
Kees Cook | 54424d3 | 2018-08-03 10:15:25 +0100 | [diff] [blame] | 407 | rxrpc_seq_t seq, |
| 408 | struct skcipher_request *req) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 409 | { |
| 410 | struct rxkad_level1_hdr sechdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 411 | struct rxrpc_crypt iv; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 412 | struct scatterlist sg[16]; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 413 | bool aborted; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 414 | u32 data_size, buf; |
| 415 | u16 check; |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 416 | int ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 417 | |
| 418 | _enter(""); |
| 419 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 420 | if (len < 8) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 421 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_hdr", "V1H", |
| 422 | RXKADSEALEDINCON); |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 423 | goto protocol_error; |
| 424 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 425 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 426 | /* Decrypt the skbuff in-place. TODO: We really want to decrypt |
| 427 | * directly into the target buffer. |
| 428 | */ |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 429 | sg_init_table(sg, ARRAY_SIZE(sg)); |
Jason A. Donenfeld | 89a5ea9 | 2017-06-04 04:16:24 +0200 | [diff] [blame] | 430 | ret = skb_to_sgvec(skb, sg, offset, 8); |
| 431 | if (unlikely(ret < 0)) |
| 432 | return ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 433 | |
| 434 | /* start the decryption afresh */ |
| 435 | memset(&iv, 0, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 436 | |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 437 | skcipher_request_set_sync_tfm(req, call->conn->cipher); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 438 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 439 | skcipher_request_set_crypt(req, sg, sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 440 | crypto_skcipher_decrypt(req); |
| 441 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 442 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 443 | /* Extract the decrypted packet length */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 444 | if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 445 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_len", "XV1", |
| 446 | RXKADDATALEN); |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 447 | goto protocol_error; |
| 448 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 449 | offset += sizeof(sechdr); |
| 450 | len -= sizeof(sechdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 451 | |
| 452 | buf = ntohl(sechdr.data_size); |
| 453 | data_size = buf & 0xffff; |
| 454 | |
| 455 | check = buf >> 16; |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 456 | check ^= seq ^ call->call_id; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 457 | check &= 0xffff; |
| 458 | if (check != 0) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 459 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_check", "V1C", |
| 460 | RXKADSEALEDINCON); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 461 | goto protocol_error; |
| 462 | } |
| 463 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 464 | if (data_size > len) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 465 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_datalen", "V1L", |
| 466 | RXKADDATALEN); |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 467 | goto protocol_error; |
| 468 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 469 | |
| 470 | _leave(" = 0 [dlen=%x]", data_size); |
| 471 | return 0; |
| 472 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 473 | protocol_error: |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 474 | if (aborted) |
| 475 | rxrpc_send_abort_packet(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 476 | return -EPROTO; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* |
| 480 | * wholly decrypt a packet (level 2 security) |
| 481 | */ |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 482 | static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb, |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 483 | unsigned int offset, unsigned int len, |
Kees Cook | 54424d3 | 2018-08-03 10:15:25 +0100 | [diff] [blame] | 484 | rxrpc_seq_t seq, |
| 485 | struct skcipher_request *req) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 486 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 487 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 488 | struct rxkad_level2_hdr sechdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 489 | struct rxrpc_crypt iv; |
| 490 | struct scatterlist _sg[4], *sg; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 491 | bool aborted; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 492 | u32 data_size, buf; |
| 493 | u16 check; |
Jason A. Donenfeld | 89a5ea9 | 2017-06-04 04:16:24 +0200 | [diff] [blame] | 494 | int nsg, ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 495 | |
| 496 | _enter(",{%d}", skb->len); |
| 497 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 498 | if (len < 8) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 499 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_hdr", "V2H", |
| 500 | RXKADSEALEDINCON); |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 501 | goto protocol_error; |
| 502 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 503 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 504 | /* Decrypt the skbuff in-place. TODO: We really want to decrypt |
| 505 | * directly into the target buffer. |
| 506 | */ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 507 | sg = _sg; |
David Howells | d0d5c0c | 2019-08-27 10:13:46 +0100 | [diff] [blame] | 508 | nsg = skb_shinfo(skb)->nr_frags; |
| 509 | if (nsg <= 4) { |
| 510 | nsg = 4; |
| 511 | } else { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 512 | sg = kmalloc_array(nsg, sizeof(*sg), GFP_NOIO); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 513 | if (!sg) |
| 514 | goto nomem; |
| 515 | } |
| 516 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 517 | sg_init_table(sg, nsg); |
Jason A. Donenfeld | 89a5ea9 | 2017-06-04 04:16:24 +0200 | [diff] [blame] | 518 | ret = skb_to_sgvec(skb, sg, offset, len); |
| 519 | if (unlikely(ret < 0)) { |
| 520 | if (sg != _sg) |
| 521 | kfree(sg); |
| 522 | return ret; |
| 523 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 524 | |
| 525 | /* decrypt from the session key */ |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 526 | token = call->conn->params.key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 527 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 528 | |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 529 | skcipher_request_set_sync_tfm(req, call->conn->cipher); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 530 | skcipher_request_set_callback(req, 0, NULL, NULL); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 531 | skcipher_request_set_crypt(req, sg, sg, len, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 532 | crypto_skcipher_decrypt(req); |
| 533 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 534 | if (sg != _sg) |
| 535 | kfree(sg); |
| 536 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 537 | /* Extract the decrypted packet length */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 538 | if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 539 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_len", "XV2", |
| 540 | RXKADDATALEN); |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 541 | goto protocol_error; |
| 542 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 543 | offset += sizeof(sechdr); |
| 544 | len -= sizeof(sechdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 545 | |
| 546 | buf = ntohl(sechdr.data_size); |
| 547 | data_size = buf & 0xffff; |
| 548 | |
| 549 | check = buf >> 16; |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 550 | check ^= seq ^ call->call_id; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 551 | check &= 0xffff; |
| 552 | if (check != 0) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 553 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_check", "V2C", |
| 554 | RXKADSEALEDINCON); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 555 | goto protocol_error; |
| 556 | } |
| 557 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 558 | if (data_size > len) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 559 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_datalen", "V2L", |
| 560 | RXKADDATALEN); |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 561 | goto protocol_error; |
| 562 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 563 | |
| 564 | _leave(" = 0 [dlen=%x]", data_size); |
| 565 | return 0; |
| 566 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 567 | protocol_error: |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 568 | if (aborted) |
| 569 | rxrpc_send_abort_packet(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 570 | return -EPROTO; |
| 571 | |
| 572 | nomem: |
| 573 | _leave(" = -ENOMEM"); |
| 574 | return -ENOMEM; |
| 575 | } |
| 576 | |
| 577 | /* |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 578 | * Verify the security on a received packet or subpacket (if part of a |
| 579 | * jumbo packet). |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 580 | */ |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 581 | static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb, |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 582 | unsigned int offset, unsigned int len, |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 583 | rxrpc_seq_t seq, u16 expected_cksum) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 584 | { |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 585 | struct skcipher_request *req; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 586 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 587 | struct scatterlist sg; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 588 | bool aborted; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 589 | u16 cksum; |
| 590 | u32 x, y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 591 | |
| 592 | _enter("{%d{%x}},{#%u}", |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 593 | call->debug_id, key_serial(call->conn->params.key), seq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 594 | |
| 595 | if (!call->conn->cipher) |
| 596 | return 0; |
| 597 | |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 598 | req = rxkad_get_call_crypto(call); |
| 599 | if (!req) |
| 600 | return -ENOMEM; |
| 601 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 602 | /* continue encrypting from where we left off */ |
| 603 | memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 604 | |
| 605 | /* validate the security checksum */ |
David Howells | 01a90a4 | 2016-08-23 15:27:24 +0100 | [diff] [blame] | 606 | x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT); |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 607 | x |= seq & 0x3fffffff; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 608 | call->crypto_buf[0] = htonl(call->call_id); |
| 609 | call->crypto_buf[1] = htonl(x); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 610 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 611 | sg_init_one(&sg, call->crypto_buf, 8); |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 612 | skcipher_request_set_sync_tfm(req, call->conn->cipher); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 613 | skcipher_request_set_callback(req, 0, NULL, NULL); |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 614 | skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 615 | crypto_skcipher_encrypt(req); |
| 616 | skcipher_request_zero(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 617 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 618 | y = ntohl(call->crypto_buf[1]); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 619 | cksum = (y >> 16) & 0xffff; |
| 620 | if (cksum == 0) |
| 621 | cksum = 1; /* zero checksums are not permitted */ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 622 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 623 | if (cksum != expected_cksum) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 624 | aborted = rxrpc_abort_eproto(call, skb, "rxkad_csum", "VCK", |
| 625 | RXKADSEALEDINCON); |
| 626 | goto protocol_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 627 | } |
| 628 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 629 | switch (call->conn->params.security_level) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 630 | case RXRPC_SECURITY_PLAIN: |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 631 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 632 | case RXRPC_SECURITY_AUTH: |
Kees Cook | 54424d3 | 2018-08-03 10:15:25 +0100 | [diff] [blame] | 633 | return rxkad_verify_packet_1(call, skb, offset, len, seq, req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 634 | case RXRPC_SECURITY_ENCRYPT: |
Kees Cook | 54424d3 | 2018-08-03 10:15:25 +0100 | [diff] [blame] | 635 | return rxkad_verify_packet_2(call, skb, offset, len, seq, req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 636 | default: |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 637 | return -ENOANO; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 638 | } |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 639 | |
| 640 | protocol_error: |
| 641 | if (aborted) |
| 642 | rxrpc_send_abort_packet(call); |
| 643 | return -EPROTO; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 647 | * Locate the data contained in a packet that was partially encrypted. |
| 648 | */ |
| 649 | static void rxkad_locate_data_1(struct rxrpc_call *call, struct sk_buff *skb, |
| 650 | unsigned int *_offset, unsigned int *_len) |
| 651 | { |
| 652 | struct rxkad_level1_hdr sechdr; |
| 653 | |
| 654 | if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0) |
| 655 | BUG(); |
| 656 | *_offset += sizeof(sechdr); |
| 657 | *_len = ntohl(sechdr.data_size) & 0xffff; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Locate the data contained in a packet that was completely encrypted. |
| 662 | */ |
| 663 | static void rxkad_locate_data_2(struct rxrpc_call *call, struct sk_buff *skb, |
| 664 | unsigned int *_offset, unsigned int *_len) |
| 665 | { |
| 666 | struct rxkad_level2_hdr sechdr; |
| 667 | |
| 668 | if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0) |
| 669 | BUG(); |
| 670 | *_offset += sizeof(sechdr); |
| 671 | *_len = ntohl(sechdr.data_size) & 0xffff; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Locate the data contained in an already decrypted packet. |
| 676 | */ |
| 677 | static void rxkad_locate_data(struct rxrpc_call *call, struct sk_buff *skb, |
| 678 | unsigned int *_offset, unsigned int *_len) |
| 679 | { |
| 680 | switch (call->conn->params.security_level) { |
| 681 | case RXRPC_SECURITY_AUTH: |
| 682 | rxkad_locate_data_1(call, skb, _offset, _len); |
| 683 | return; |
| 684 | case RXRPC_SECURITY_ENCRYPT: |
| 685 | rxkad_locate_data_2(call, skb, _offset, _len); |
| 686 | return; |
| 687 | default: |
| 688 | return; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 693 | * issue a challenge |
| 694 | */ |
| 695 | static int rxkad_issue_challenge(struct rxrpc_connection *conn) |
| 696 | { |
| 697 | struct rxkad_challenge challenge; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 698 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 699 | struct msghdr msg; |
| 700 | struct kvec iov[2]; |
| 701 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 702 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 703 | int ret; |
| 704 | |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 705 | _enter("{%d}", conn->debug_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 706 | |
| 707 | get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce)); |
| 708 | |
| 709 | challenge.version = htonl(2); |
| 710 | challenge.nonce = htonl(conn->security_nonce); |
| 711 | challenge.min_level = htonl(0); |
| 712 | challenge.__padding = 0; |
| 713 | |
David Howells | 7b674e3 | 2017-08-29 10:18:37 +0100 | [diff] [blame] | 714 | msg.msg_name = &conn->params.peer->srx.transport; |
| 715 | msg.msg_namelen = conn->params.peer->srx.transport_len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 716 | msg.msg_control = NULL; |
| 717 | msg.msg_controllen = 0; |
| 718 | msg.msg_flags = 0; |
| 719 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 720 | whdr.epoch = htonl(conn->proto.epoch); |
| 721 | whdr.cid = htonl(conn->proto.cid); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 722 | whdr.callNumber = 0; |
| 723 | whdr.seq = 0; |
| 724 | whdr.type = RXRPC_PACKET_TYPE_CHALLENGE; |
| 725 | whdr.flags = conn->out_clientflag; |
| 726 | whdr.userStatus = 0; |
| 727 | whdr.securityIndex = conn->security_ix; |
| 728 | whdr._rsvd = 0; |
David Howells | 68d6d1a | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 729 | whdr.serviceId = htons(conn->service_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 730 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 731 | iov[0].iov_base = &whdr; |
| 732 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 733 | iov[1].iov_base = &challenge; |
| 734 | iov[1].iov_len = sizeof(challenge); |
| 735 | |
| 736 | len = iov[0].iov_len + iov[1].iov_len; |
| 737 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 738 | serial = atomic_inc_return(&conn->serial); |
| 739 | whdr.serial = htonl(serial); |
| 740 | _proto("Tx CHALLENGE %%%u", serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 741 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 742 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 743 | if (ret < 0) { |
David Howells | 6b47fe1 | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 744 | trace_rxrpc_tx_fail(conn->debug_id, serial, ret, |
David Howells | 4764c0d | 2018-07-23 17:18:37 +0100 | [diff] [blame] | 745 | rxrpc_tx_point_rxkad_challenge); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 746 | return -EAGAIN; |
| 747 | } |
| 748 | |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 749 | conn->params.peer->last_tx_at = ktime_get_seconds(); |
David Howells | 4764c0d | 2018-07-23 17:18:37 +0100 | [diff] [blame] | 750 | trace_rxrpc_tx_packet(conn->debug_id, &whdr, |
| 751 | rxrpc_tx_point_rxkad_challenge); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 752 | _leave(" = 0"); |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | /* |
| 757 | * send a Kerberos security response |
| 758 | */ |
| 759 | static int rxkad_send_response(struct rxrpc_connection *conn, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 760 | struct rxrpc_host_header *hdr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 761 | struct rxkad_response *resp, |
| 762 | const struct rxkad_key *s2) |
| 763 | { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 764 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 765 | struct msghdr msg; |
| 766 | struct kvec iov[3]; |
| 767 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 768 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 769 | int ret; |
| 770 | |
| 771 | _enter(""); |
| 772 | |
David Howells | 7b674e3 | 2017-08-29 10:18:37 +0100 | [diff] [blame] | 773 | msg.msg_name = &conn->params.peer->srx.transport; |
| 774 | msg.msg_namelen = conn->params.peer->srx.transport_len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 775 | msg.msg_control = NULL; |
| 776 | msg.msg_controllen = 0; |
| 777 | msg.msg_flags = 0; |
| 778 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 779 | memset(&whdr, 0, sizeof(whdr)); |
| 780 | whdr.epoch = htonl(hdr->epoch); |
| 781 | whdr.cid = htonl(hdr->cid); |
| 782 | whdr.type = RXRPC_PACKET_TYPE_RESPONSE; |
| 783 | whdr.flags = conn->out_clientflag; |
| 784 | whdr.securityIndex = hdr->securityIndex; |
| 785 | whdr.serviceId = htons(hdr->serviceId); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 786 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 787 | iov[0].iov_base = &whdr; |
| 788 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 789 | iov[1].iov_base = resp; |
| 790 | iov[1].iov_len = sizeof(*resp); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 791 | iov[2].iov_base = (void *)s2->ticket; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 792 | iov[2].iov_len = s2->ticket_len; |
| 793 | |
| 794 | len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; |
| 795 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 796 | serial = atomic_inc_return(&conn->serial); |
| 797 | whdr.serial = htonl(serial); |
| 798 | _proto("Tx RESPONSE %%%u", serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 799 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 800 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 801 | if (ret < 0) { |
David Howells | 6b47fe1 | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 802 | trace_rxrpc_tx_fail(conn->debug_id, serial, ret, |
David Howells | 4764c0d | 2018-07-23 17:18:37 +0100 | [diff] [blame] | 803 | rxrpc_tx_point_rxkad_response); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 804 | return -EAGAIN; |
| 805 | } |
| 806 | |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 807 | conn->params.peer->last_tx_at = ktime_get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 808 | _leave(" = 0"); |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * calculate the response checksum |
| 814 | */ |
| 815 | static void rxkad_calc_response_checksum(struct rxkad_response *response) |
| 816 | { |
| 817 | u32 csum = 1000003; |
| 818 | int loop; |
| 819 | u8 *p = (u8 *) response; |
| 820 | |
| 821 | for (loop = sizeof(*response); loop > 0; loop--) |
| 822 | csum = csum * 0x10204081 + *p++; |
| 823 | |
| 824 | response->encrypted.checksum = htonl(csum); |
| 825 | } |
| 826 | |
| 827 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 828 | * encrypt the response packet |
| 829 | */ |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 830 | static int rxkad_encrypt_response(struct rxrpc_connection *conn, |
| 831 | struct rxkad_response *resp, |
| 832 | const struct rxkad_key *s2) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 833 | { |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 834 | struct skcipher_request *req; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 835 | struct rxrpc_crypt iv; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 836 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 837 | |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 838 | req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS); |
| 839 | if (!req) |
| 840 | return -ENOMEM; |
| 841 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 842 | /* continue encrypting from where we left off */ |
| 843 | memcpy(&iv, s2->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 844 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 845 | sg_init_table(sg, 1); |
| 846 | sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 847 | skcipher_request_set_sync_tfm(req, conn->cipher); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 848 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 849 | skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 850 | crypto_skcipher_encrypt(req); |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 851 | skcipher_request_free(req); |
| 852 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* |
| 856 | * respond to a challenge packet |
| 857 | */ |
| 858 | static int rxkad_respond_to_challenge(struct rxrpc_connection *conn, |
| 859 | struct sk_buff *skb, |
| 860 | u32 *_abort_code) |
| 861 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 862 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 863 | struct rxkad_challenge challenge; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 864 | struct rxkad_response *resp; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 865 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 866 | const char *eproto; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 867 | u32 version, nonce, min_level, abort_code; |
| 868 | int ret; |
| 869 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 870 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 871 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 872 | eproto = tracepoint_string("chall_no_key"); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 873 | abort_code = RX_PROTOCOL_ERROR; |
| 874 | if (!conn->params.key) |
| 875 | goto protocol_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 876 | |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 877 | abort_code = RXKADEXPIRED; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 878 | ret = key_validate(conn->params.key); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 879 | if (ret < 0) |
| 880 | goto other_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 881 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 882 | eproto = tracepoint_string("chall_short"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 883 | abort_code = RXKADPACKETSHORT; |
David Howells | 775e5b7 | 2016-09-30 13:26:03 +0100 | [diff] [blame] | 884 | if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), |
| 885 | &challenge, sizeof(challenge)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 886 | goto protocol_error; |
| 887 | |
| 888 | version = ntohl(challenge.version); |
| 889 | nonce = ntohl(challenge.nonce); |
| 890 | min_level = ntohl(challenge.min_level); |
| 891 | |
| 892 | _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 893 | sp->hdr.serial, version, nonce, min_level); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 894 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 895 | eproto = tracepoint_string("chall_ver"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 896 | abort_code = RXKADINCONSISTENCY; |
| 897 | if (version != RXKAD_VERSION) |
| 898 | goto protocol_error; |
| 899 | |
| 900 | abort_code = RXKADLEVELFAIL; |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 901 | ret = -EACCES; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 902 | if (conn->params.security_level < min_level) |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 903 | goto other_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 904 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 905 | token = conn->params.key->payload.data[0]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 906 | |
| 907 | /* build the response packet */ |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 908 | resp = kzalloc(sizeof(struct rxkad_response), GFP_NOFS); |
| 909 | if (!resp) |
| 910 | return -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 911 | |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 912 | resp->version = htonl(RXKAD_VERSION); |
| 913 | resp->encrypted.epoch = htonl(conn->proto.epoch); |
| 914 | resp->encrypted.cid = htonl(conn->proto.cid); |
| 915 | resp->encrypted.securityIndex = htonl(conn->security_ix); |
| 916 | resp->encrypted.inc_nonce = htonl(nonce + 1); |
| 917 | resp->encrypted.level = htonl(conn->params.security_level); |
| 918 | resp->kvno = htonl(token->kad->kvno); |
| 919 | resp->ticket_len = htonl(token->kad->ticket_len); |
| 920 | resp->encrypted.call_id[0] = htonl(conn->channels[0].call_counter); |
| 921 | resp->encrypted.call_id[1] = htonl(conn->channels[1].call_counter); |
| 922 | resp->encrypted.call_id[2] = htonl(conn->channels[2].call_counter); |
| 923 | resp->encrypted.call_id[3] = htonl(conn->channels[3].call_counter); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 924 | |
| 925 | /* calculate the response checksum and then do the encryption */ |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 926 | rxkad_calc_response_checksum(resp); |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 927 | ret = rxkad_encrypt_response(conn, resp, token->kad); |
| 928 | if (ret == 0) |
| 929 | ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad); |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 930 | kfree(resp); |
| 931 | return ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 932 | |
| 933 | protocol_error: |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 934 | trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 935 | ret = -EPROTO; |
| 936 | other_error: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 937 | *_abort_code = abort_code; |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 938 | return ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* |
| 942 | * decrypt the kerberos IV ticket in the response |
| 943 | */ |
| 944 | static int rxkad_decrypt_ticket(struct rxrpc_connection *conn, |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 945 | struct key *server_key, |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 946 | struct sk_buff *skb, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 947 | void *ticket, size_t ticket_len, |
| 948 | struct rxrpc_crypt *_session_key, |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 949 | time64_t *_expiry, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 950 | u32 *_abort_code) |
| 951 | { |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 952 | struct skcipher_request *req; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 953 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 954 | struct rxrpc_crypt iv, key; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 955 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 956 | struct in_addr addr; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 957 | unsigned int life; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 958 | const char *eproto; |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 959 | time64_t issue, now; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 960 | bool little_endian; |
| 961 | int ret; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 962 | u32 abort_code; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 963 | u8 *p, *q, *name, *end; |
| 964 | |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 965 | _enter("{%d},{%x}", conn->debug_id, key_serial(server_key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 966 | |
| 967 | *_expiry = 0; |
| 968 | |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 969 | ASSERT(server_key->payload.data[0] != NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 970 | ASSERTCMP((unsigned long) ticket & 7UL, ==, 0); |
| 971 | |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 972 | memcpy(&iv, &server_key->payload.data[2], sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 973 | |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 974 | ret = -ENOMEM; |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 975 | req = skcipher_request_alloc(server_key->payload.data[0], GFP_NOFS); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 976 | if (!req) |
| 977 | goto temporary_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 978 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 979 | sg_init_one(&sg[0], ticket, ticket_len); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 980 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 981 | skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 982 | crypto_skcipher_decrypt(req); |
| 983 | skcipher_request_free(req); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 984 | |
| 985 | p = ticket; |
| 986 | end = p + ticket_len; |
| 987 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 988 | #define Z(field) \ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 989 | ({ \ |
| 990 | u8 *__str = p; \ |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 991 | eproto = tracepoint_string("rxkad_bad_"#field); \ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 992 | q = memchr(p, 0, end - p); \ |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 993 | if (!q || q - p > (field##_SZ)) \ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 994 | goto bad_ticket; \ |
| 995 | for (; p < q; p++) \ |
| 996 | if (!isprint(*p)) \ |
| 997 | goto bad_ticket; \ |
| 998 | p++; \ |
| 999 | __str; \ |
| 1000 | }) |
| 1001 | |
| 1002 | /* extract the ticket flags */ |
| 1003 | _debug("KIV FLAGS: %x", *p); |
| 1004 | little_endian = *p & 1; |
| 1005 | p++; |
| 1006 | |
| 1007 | /* extract the authentication name */ |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1008 | name = Z(ANAME); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1009 | _debug("KIV ANAME: %s", name); |
| 1010 | |
| 1011 | /* extract the principal's instance */ |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1012 | name = Z(INST); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1013 | _debug("KIV INST : %s", name); |
| 1014 | |
| 1015 | /* extract the principal's authentication domain */ |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1016 | name = Z(REALM); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1017 | _debug("KIV REALM: %s", name); |
| 1018 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1019 | eproto = tracepoint_string("rxkad_bad_len"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1020 | if (end - p < 4 + 8 + 4 + 2) |
| 1021 | goto bad_ticket; |
| 1022 | |
| 1023 | /* get the IPv4 address of the entity that requested the ticket */ |
| 1024 | memcpy(&addr, p, sizeof(addr)); |
| 1025 | p += 4; |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 1026 | _debug("KIV ADDR : %pI4", &addr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1027 | |
| 1028 | /* get the session key from the ticket */ |
| 1029 | memcpy(&key, p, sizeof(key)); |
| 1030 | p += 8; |
| 1031 | _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1])); |
| 1032 | memcpy(_session_key, &key, sizeof(key)); |
| 1033 | |
| 1034 | /* get the ticket's lifetime */ |
| 1035 | life = *p++ * 5 * 60; |
| 1036 | _debug("KIV LIFE : %u", life); |
| 1037 | |
| 1038 | /* get the issue time of the ticket */ |
| 1039 | if (little_endian) { |
| 1040 | __le32 stamp; |
| 1041 | memcpy(&stamp, p, 4); |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 1042 | issue = rxrpc_u32_to_time64(le32_to_cpu(stamp)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1043 | } else { |
| 1044 | __be32 stamp; |
| 1045 | memcpy(&stamp, p, 4); |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 1046 | issue = rxrpc_u32_to_time64(be32_to_cpu(stamp)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1047 | } |
| 1048 | p += 4; |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 1049 | now = ktime_get_real_seconds(); |
| 1050 | _debug("KIV ISSUE: %llx [%llx]", issue, now); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1051 | |
| 1052 | /* check the ticket is in date */ |
| 1053 | if (issue > now) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1054 | abort_code = RXKADNOAUTH; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1055 | ret = -EKEYREJECTED; |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1056 | goto other_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | if (issue < now - life) { |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1060 | abort_code = RXKADEXPIRED; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1061 | ret = -EKEYEXPIRED; |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1062 | goto other_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | *_expiry = issue + life; |
| 1066 | |
| 1067 | /* get the service name */ |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1068 | name = Z(SNAME); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1069 | _debug("KIV SNAME: %s", name); |
| 1070 | |
| 1071 | /* get the service instance name */ |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1072 | name = Z(INST); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1073 | _debug("KIV SINST: %s", name); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1074 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1075 | |
| 1076 | bad_ticket: |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1077 | trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto); |
| 1078 | abort_code = RXKADBADTICKET; |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1079 | ret = -EPROTO; |
| 1080 | other_error: |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1081 | *_abort_code = abort_code; |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1082 | return ret; |
| 1083 | temporary_error: |
| 1084 | return ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * decrypt the response packet |
| 1089 | */ |
| 1090 | static void rxkad_decrypt_response(struct rxrpc_connection *conn, |
| 1091 | struct rxkad_response *resp, |
| 1092 | const struct rxrpc_crypt *session_key) |
| 1093 | { |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 1094 | struct skcipher_request *req = rxkad_ci_req; |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 1095 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1096 | struct rxrpc_crypt iv; |
| 1097 | |
| 1098 | _enter(",,%08x%08x", |
| 1099 | ntohl(session_key->n[0]), ntohl(session_key->n[1])); |
| 1100 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1101 | mutex_lock(&rxkad_ci_mutex); |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 1102 | if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x, |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 1103 | sizeof(*session_key)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1104 | BUG(); |
| 1105 | |
| 1106 | memcpy(&iv, session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1107 | |
Herbert Xu | a263629 | 2016-06-26 14:55:24 -0700 | [diff] [blame] | 1108 | sg_init_table(sg, 1); |
| 1109 | sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted)); |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 1110 | skcipher_request_set_sync_tfm(req, rxkad_ci); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 1111 | skcipher_request_set_callback(req, 0, NULL, NULL); |
| 1112 | skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x); |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 1113 | crypto_skcipher_decrypt(req); |
| 1114 | skcipher_request_zero(req); |
| 1115 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1116 | mutex_unlock(&rxkad_ci_mutex); |
| 1117 | |
| 1118 | _leave(""); |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * verify a response |
| 1123 | */ |
| 1124 | static int rxkad_verify_response(struct rxrpc_connection *conn, |
| 1125 | struct sk_buff *skb, |
| 1126 | u32 *_abort_code) |
| 1127 | { |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1128 | struct rxkad_response *response; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 1129 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1130 | struct rxrpc_crypt session_key; |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 1131 | struct key *server_key; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1132 | const char *eproto; |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 1133 | time64_t expiry; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1134 | void *ticket; |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 1135 | u32 abort_code, version, kvno, ticket_len, level; |
| 1136 | __be32 csum; |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1137 | int ret, i; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1138 | |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 1139 | _enter("{%d}", conn->debug_id); |
| 1140 | |
| 1141 | server_key = rxrpc_look_up_server_security(conn, skb, 0, 0); |
| 1142 | if (IS_ERR(server_key)) { |
| 1143 | switch (PTR_ERR(server_key)) { |
| 1144 | case -ENOKEY: |
| 1145 | abort_code = RXKADUNKNOWNKEY; |
| 1146 | break; |
| 1147 | case -EKEYEXPIRED: |
| 1148 | abort_code = RXKADEXPIRED; |
| 1149 | break; |
| 1150 | default: |
| 1151 | abort_code = RXKADNOAUTH; |
| 1152 | break; |
| 1153 | } |
| 1154 | trace_rxrpc_abort(0, "SVK", |
| 1155 | sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq, |
| 1156 | abort_code, PTR_ERR(server_key)); |
| 1157 | *_abort_code = abort_code; |
| 1158 | return -EPROTO; |
| 1159 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1160 | |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1161 | ret = -ENOMEM; |
| 1162 | response = kzalloc(sizeof(struct rxkad_response), GFP_NOFS); |
| 1163 | if (!response) |
| 1164 | goto temporary_error; |
| 1165 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1166 | eproto = tracepoint_string("rxkad_rsp_short"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1167 | abort_code = RXKADPACKETSHORT; |
David Howells | 775e5b7 | 2016-09-30 13:26:03 +0100 | [diff] [blame] | 1168 | if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1169 | response, sizeof(*response)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1170 | goto protocol_error; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1171 | if (!pskb_pull(skb, sizeof(*response))) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1172 | BUG(); |
| 1173 | |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1174 | version = ntohl(response->version); |
| 1175 | ticket_len = ntohl(response->ticket_len); |
| 1176 | kvno = ntohl(response->kvno); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1177 | _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 1178 | sp->hdr.serial, version, kvno, ticket_len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1179 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1180 | eproto = tracepoint_string("rxkad_rsp_ver"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1181 | abort_code = RXKADINCONSISTENCY; |
| 1182 | if (version != RXKAD_VERSION) |
David Howells | 4aa9cb3 | 2007-12-07 04:31:47 -0800 | [diff] [blame] | 1183 | goto protocol_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1184 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1185 | eproto = tracepoint_string("rxkad_rsp_tktlen"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1186 | abort_code = RXKADTICKETLEN; |
| 1187 | if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN) |
| 1188 | goto protocol_error; |
| 1189 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1190 | eproto = tracepoint_string("rxkad_rsp_unkkey"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1191 | abort_code = RXKADUNKNOWNKEY; |
| 1192 | if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5) |
| 1193 | goto protocol_error; |
| 1194 | |
| 1195 | /* extract the kerberos ticket and decrypt and decode it */ |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1196 | ret = -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1197 | ticket = kmalloc(ticket_len, GFP_NOFS); |
| 1198 | if (!ticket) |
Dinghao Liu | b43c75a | 2020-08-27 16:55:46 +0100 | [diff] [blame] | 1199 | goto temporary_error_free_resp; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1200 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1201 | eproto = tracepoint_string("rxkad_tkt_short"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1202 | abort_code = RXKADPACKETSHORT; |
David Howells | 775e5b7 | 2016-09-30 13:26:03 +0100 | [diff] [blame] | 1203 | if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), |
| 1204 | ticket, ticket_len) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1205 | goto protocol_error_free; |
| 1206 | |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 1207 | ret = rxkad_decrypt_ticket(conn, server_key, skb, ticket, ticket_len, |
| 1208 | &session_key, &expiry, _abort_code); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1209 | if (ret < 0) |
Qiushi Wu | f45d01f | 2020-05-22 13:45:18 -0500 | [diff] [blame] | 1210 | goto temporary_error_free_ticket; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1211 | |
| 1212 | /* use the session key from inside the ticket to decrypt the |
| 1213 | * response */ |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1214 | rxkad_decrypt_response(conn, response, &session_key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1215 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1216 | eproto = tracepoint_string("rxkad_rsp_param"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1217 | abort_code = RXKADSEALEDINCON; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1218 | if (ntohl(response->encrypted.epoch) != conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1219 | goto protocol_error_free; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1220 | if (ntohl(response->encrypted.cid) != conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1221 | goto protocol_error_free; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1222 | if (ntohl(response->encrypted.securityIndex) != conn->security_ix) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1223 | goto protocol_error_free; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1224 | csum = response->encrypted.checksum; |
| 1225 | response->encrypted.checksum = 0; |
| 1226 | rxkad_calc_response_checksum(response); |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1227 | eproto = tracepoint_string("rxkad_rsp_csum"); |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1228 | if (response->encrypted.checksum != csum) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1229 | goto protocol_error_free; |
| 1230 | |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 1231 | spin_lock(&conn->bundle->channel_lock); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1232 | for (i = 0; i < RXRPC_MAXCALLS; i++) { |
| 1233 | struct rxrpc_call *call; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1234 | u32 call_id = ntohl(response->encrypted.call_id[i]); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1235 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1236 | eproto = tracepoint_string("rxkad_rsp_callid"); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1237 | if (call_id > INT_MAX) |
| 1238 | goto protocol_error_unlock; |
| 1239 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1240 | eproto = tracepoint_string("rxkad_rsp_callctr"); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1241 | if (call_id < conn->channels[i].call_counter) |
| 1242 | goto protocol_error_unlock; |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1243 | |
| 1244 | eproto = tracepoint_string("rxkad_rsp_callst"); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1245 | if (call_id > conn->channels[i].call_counter) { |
| 1246 | call = rcu_dereference_protected( |
| 1247 | conn->channels[i].call, |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 1248 | lockdep_is_held(&conn->bundle->channel_lock)); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1249 | if (call && call->state < RXRPC_CALL_COMPLETE) |
| 1250 | goto protocol_error_unlock; |
| 1251 | conn->channels[i].call_counter = call_id; |
| 1252 | } |
| 1253 | } |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 1254 | spin_unlock(&conn->bundle->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1255 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1256 | eproto = tracepoint_string("rxkad_rsp_seq"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1257 | abort_code = RXKADOUTOFSEQUENCE; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1258 | if (ntohl(response->encrypted.inc_nonce) != conn->security_nonce + 1) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1259 | goto protocol_error_free; |
| 1260 | |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1261 | eproto = tracepoint_string("rxkad_rsp_level"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1262 | abort_code = RXKADLEVELFAIL; |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1263 | level = ntohl(response->encrypted.level); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1264 | if (level > RXRPC_SECURITY_ENCRYPT) |
| 1265 | goto protocol_error_free; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 1266 | conn->params.security_level = level; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1267 | |
| 1268 | /* create a key to hold the security data and expiration time - after |
| 1269 | * this the connection security can be handled in exactly the same way |
| 1270 | * as for a client connection */ |
| 1271 | ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1272 | if (ret < 0) |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1273 | goto temporary_error_free_ticket; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1274 | |
| 1275 | kfree(ticket); |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1276 | kfree(response); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1277 | _leave(" = 0"); |
| 1278 | return 0; |
| 1279 | |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 1280 | protocol_error_unlock: |
David Howells | 245500d | 2020-07-01 11:15:32 +0100 | [diff] [blame] | 1281 | spin_unlock(&conn->bundle->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1282 | protocol_error_free: |
| 1283 | kfree(ticket); |
| 1284 | protocol_error: |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1285 | kfree(response); |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 1286 | trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto); |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 1287 | key_put(server_key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1288 | *_abort_code = abort_code; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1289 | return -EPROTO; |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1290 | |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1291 | temporary_error_free_ticket: |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1292 | kfree(ticket); |
Dinghao Liu | b43c75a | 2020-08-27 16:55:46 +0100 | [diff] [blame] | 1293 | temporary_error_free_resp: |
David Howells | 8c2f826 | 2018-02-08 15:59:07 +0000 | [diff] [blame] | 1294 | kfree(response); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1295 | temporary_error: |
| 1296 | /* Ignore the response packet if we got a temporary error such as |
| 1297 | * ENOMEM. We just want to send the challenge again. Note that we |
| 1298 | * also come out this way if the ticket decryption fails. |
| 1299 | */ |
David Howells | ec832bd | 2020-09-16 08:00:44 +0100 | [diff] [blame] | 1300 | key_put(server_key); |
David Howells | ef68622 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 1301 | return ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * clear the connection security |
| 1306 | */ |
| 1307 | static void rxkad_clear(struct rxrpc_connection *conn) |
| 1308 | { |
| 1309 | _enter(""); |
| 1310 | |
| 1311 | if (conn->cipher) |
Kees Cook | 69d826f | 2018-09-18 19:10:47 -0700 | [diff] [blame] | 1312 | crypto_free_sync_skcipher(conn->cipher); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1316 | * Initialise the rxkad security service. |
| 1317 | */ |
| 1318 | static int rxkad_init(void) |
| 1319 | { |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 1320 | struct crypto_sync_skcipher *tfm; |
| 1321 | struct skcipher_request *req; |
| 1322 | |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1323 | /* pin the cipher we need so that the crypto layer doesn't invoke |
| 1324 | * keventd to go get it */ |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 1325 | tfm = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0); |
| 1326 | if (IS_ERR(tfm)) |
| 1327 | return PTR_ERR(tfm); |
| 1328 | |
| 1329 | req = skcipher_request_alloc(&tfm->base, GFP_KERNEL); |
| 1330 | if (!req) |
| 1331 | goto nomem_tfm; |
| 1332 | |
| 1333 | rxkad_ci_req = req; |
| 1334 | rxkad_ci = tfm; |
| 1335 | return 0; |
| 1336 | |
| 1337 | nomem_tfm: |
| 1338 | crypto_free_sync_skcipher(tfm); |
| 1339 | return -ENOMEM; |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * Clean up the rxkad security service. |
| 1344 | */ |
| 1345 | static void rxkad_exit(void) |
| 1346 | { |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 1347 | crypto_free_sync_skcipher(rxkad_ci); |
| 1348 | skcipher_request_free(rxkad_ci_req); |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1352 | * RxRPC Kerberos-based security |
| 1353 | */ |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1354 | const struct rxrpc_security rxkad = { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1355 | .name = "rxkad", |
David Howells | 8b81547 | 2009-09-14 01:17:30 +0000 | [diff] [blame] | 1356 | .security_index = RXRPC_SECURITY_RXKAD, |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 1357 | .no_key_abort = RXKADUNKNOWNKEY, |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 1358 | .init = rxkad_init, |
| 1359 | .exit = rxkad_exit, |
David Howells | 12da59f | 2020-09-16 08:37:29 +0100 | [diff] [blame] | 1360 | .preparse_server_key = rxkad_preparse_server_key, |
| 1361 | .free_preparse_server_key = rxkad_free_preparse_server_key, |
| 1362 | .destroy_server_key = rxkad_destroy_server_key, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1363 | .init_connection_security = rxkad_init_connection_security, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1364 | .secure_packet = rxkad_secure_packet, |
| 1365 | .verify_packet = rxkad_verify_packet, |
David Howells | 1db88c5 | 2019-07-30 15:56:57 +0100 | [diff] [blame] | 1366 | .free_call_crypto = rxkad_free_call_crypto, |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 1367 | .locate_data = rxkad_locate_data, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1368 | .issue_challenge = rxkad_issue_challenge, |
| 1369 | .respond_to_challenge = rxkad_respond_to_challenge, |
| 1370 | .verify_response = rxkad_verify_response, |
| 1371 | .clear = rxkad_clear, |
| 1372 | }; |