David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* Kerberos-based RxRPC security |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/net.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/udp.h> |
| 16 | #include <linux/crypto.h> |
| 17 | #include <linux/scatterlist.h> |
| 18 | #include <linux/ctype.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 20 | #include <net/sock.h> |
| 21 | #include <net/af_rxrpc.h> |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 22 | #include <keys/rxrpc-type.h> |
David Howells | b1bdb69 | 2007-04-27 15:28:45 -0700 | [diff] [blame] | 23 | #define rxrpc_debug rxkad_debug |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 24 | #include "ar-internal.h" |
| 25 | |
| 26 | #define RXKAD_VERSION 2 |
| 27 | #define MAXKRB5TICKETLEN 1024 |
| 28 | #define RXKAD_TKT_TYPE_KERBEROS_V5 256 |
| 29 | #define ANAME_SZ 40 /* size of authentication name */ |
| 30 | #define INST_SZ 40 /* size of principal's instance */ |
| 31 | #define REALM_SZ 40 /* size of principal's auth domain */ |
| 32 | #define SNAME_SZ 40 /* size of service name */ |
| 33 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 34 | unsigned int rxrpc_debug; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 35 | module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO); |
Paul Bolle | 424b00e | 2008-04-16 11:08:22 +0100 | [diff] [blame] | 36 | MODULE_PARM_DESC(debug, "rxkad debugging mask"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 37 | |
| 38 | struct rxkad_level1_hdr { |
| 39 | __be32 data_size; /* true data size (excluding padding) */ |
| 40 | }; |
| 41 | |
| 42 | struct rxkad_level2_hdr { |
| 43 | __be32 data_size; /* true data size (excluding padding) */ |
| 44 | __be32 checksum; /* decrypted data checksum */ |
| 45 | }; |
| 46 | |
David Howells | 8b81547 | 2009-09-14 01:17:30 +0000 | [diff] [blame] | 47 | MODULE_DESCRIPTION("RxRPC network protocol type-2 security (Kerberos 4)"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 48 | MODULE_AUTHOR("Red Hat, Inc."); |
| 49 | MODULE_LICENSE("GPL"); |
| 50 | |
| 51 | /* |
| 52 | * this holds a pinned cipher so that keventd doesn't get called by the cipher |
| 53 | * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE |
| 54 | * packets |
| 55 | */ |
| 56 | static struct crypto_blkcipher *rxkad_ci; |
| 57 | static DEFINE_MUTEX(rxkad_ci_mutex); |
| 58 | |
| 59 | /* |
| 60 | * initialise connection security |
| 61 | */ |
| 62 | static int rxkad_init_connection_security(struct rxrpc_connection *conn) |
| 63 | { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 64 | struct crypto_blkcipher *ci; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 65 | struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 66 | int ret; |
| 67 | |
| 68 | _enter("{%d},{%x}", conn->debug_id, key_serial(conn->key)); |
| 69 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 70 | token = conn->key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 71 | conn->security_ix = token->security_index; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 72 | |
| 73 | ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC); |
| 74 | if (IS_ERR(ci)) { |
| 75 | _debug("no cipher"); |
| 76 | ret = PTR_ERR(ci); |
| 77 | goto error; |
| 78 | } |
| 79 | |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 80 | if (crypto_blkcipher_setkey(ci, token->kad->session_key, |
| 81 | sizeof(token->kad->session_key)) < 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 82 | BUG(); |
| 83 | |
| 84 | switch (conn->security_level) { |
| 85 | case RXRPC_SECURITY_PLAIN: |
| 86 | break; |
| 87 | case RXRPC_SECURITY_AUTH: |
| 88 | conn->size_align = 8; |
| 89 | conn->security_size = sizeof(struct rxkad_level1_hdr); |
| 90 | conn->header_size += sizeof(struct rxkad_level1_hdr); |
| 91 | break; |
| 92 | case RXRPC_SECURITY_ENCRYPT: |
| 93 | conn->size_align = 8; |
| 94 | conn->security_size = sizeof(struct rxkad_level2_hdr); |
| 95 | conn->header_size += sizeof(struct rxkad_level2_hdr); |
| 96 | break; |
| 97 | default: |
| 98 | ret = -EKEYREJECTED; |
| 99 | goto error; |
| 100 | } |
| 101 | |
| 102 | conn->cipher = ci; |
| 103 | ret = 0; |
| 104 | error: |
| 105 | _leave(" = %d", ret); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * prime the encryption state with the invariant parts of a connection's |
| 111 | * description |
| 112 | */ |
| 113 | static void rxkad_prime_packet_security(struct rxrpc_connection *conn) |
| 114 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 115 | struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 116 | struct blkcipher_desc desc; |
| 117 | struct scatterlist sg[2]; |
| 118 | struct rxrpc_crypt iv; |
| 119 | struct { |
| 120 | __be32 x[4]; |
| 121 | } tmpbuf __attribute__((aligned(16))); /* must all be in same page */ |
| 122 | |
| 123 | _enter(""); |
| 124 | |
| 125 | if (!conn->key) |
| 126 | return; |
| 127 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 128 | token = conn->key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 129 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 130 | |
| 131 | desc.tfm = conn->cipher; |
| 132 | desc.info = iv.x; |
| 133 | desc.flags = 0; |
| 134 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 135 | tmpbuf.x[0] = htonl(conn->epoch); |
| 136 | tmpbuf.x[1] = htonl(conn->cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 137 | tmpbuf.x[2] = 0; |
| 138 | tmpbuf.x[3] = htonl(conn->security_ix); |
| 139 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 140 | sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf)); |
| 141 | sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 142 | crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf)); |
| 143 | |
| 144 | memcpy(&conn->csum_iv, &tmpbuf.x[2], sizeof(conn->csum_iv)); |
| 145 | ASSERTCMP(conn->csum_iv.n[0], ==, tmpbuf.x[2]); |
| 146 | |
| 147 | _leave(""); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * partially encrypt a packet (level 1 security) |
| 152 | */ |
| 153 | static int rxkad_secure_packet_auth(const struct rxrpc_call *call, |
| 154 | struct sk_buff *skb, |
| 155 | u32 data_size, |
| 156 | void *sechdr) |
| 157 | { |
| 158 | struct rxrpc_skb_priv *sp; |
| 159 | struct blkcipher_desc desc; |
| 160 | struct rxrpc_crypt iv; |
| 161 | struct scatterlist sg[2]; |
| 162 | struct { |
| 163 | struct rxkad_level1_hdr hdr; |
| 164 | __be32 first; /* first four bytes of data and padding */ |
| 165 | } tmpbuf __attribute__((aligned(8))); /* must all be in same page */ |
| 166 | u16 check; |
| 167 | |
| 168 | sp = rxrpc_skb(skb); |
| 169 | |
| 170 | _enter(""); |
| 171 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 172 | check = sp->hdr.seq ^ sp->hdr.callNumber; |
| 173 | data_size |= (u32)check << 16; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 174 | |
| 175 | tmpbuf.hdr.data_size = htonl(data_size); |
| 176 | memcpy(&tmpbuf.first, sechdr + 4, sizeof(tmpbuf.first)); |
| 177 | |
| 178 | /* start the encryption afresh */ |
| 179 | memset(&iv, 0, sizeof(iv)); |
| 180 | desc.tfm = call->conn->cipher; |
| 181 | desc.info = iv.x; |
| 182 | desc.flags = 0; |
| 183 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 184 | sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf)); |
| 185 | sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 186 | crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf)); |
| 187 | |
| 188 | memcpy(sechdr, &tmpbuf, sizeof(tmpbuf)); |
| 189 | |
| 190 | _leave(" = 0"); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * wholly encrypt a packet (level 2 security) |
| 196 | */ |
| 197 | static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call, |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame^] | 198 | struct sk_buff *skb, |
| 199 | u32 data_size, |
| 200 | void *sechdr) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 201 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 202 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 203 | struct rxkad_level2_hdr rxkhdr |
| 204 | __attribute__((aligned(8))); /* must be all on one page */ |
| 205 | struct rxrpc_skb_priv *sp; |
| 206 | struct blkcipher_desc desc; |
| 207 | struct rxrpc_crypt iv; |
| 208 | struct scatterlist sg[16]; |
| 209 | struct sk_buff *trailer; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 210 | unsigned int len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 211 | u16 check; |
| 212 | int nsg; |
| 213 | |
| 214 | sp = rxrpc_skb(skb); |
| 215 | |
| 216 | _enter(""); |
| 217 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 218 | check = sp->hdr.seq ^ sp->hdr.callNumber; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 219 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 220 | rxkhdr.data_size = htonl(data_size | (u32)check << 16); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 221 | rxkhdr.checksum = 0; |
| 222 | |
| 223 | /* encrypt from the session key */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 224 | token = call->conn->key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 225 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 226 | desc.tfm = call->conn->cipher; |
| 227 | desc.info = iv.x; |
| 228 | desc.flags = 0; |
| 229 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 230 | sg_init_one(&sg[0], sechdr, sizeof(rxkhdr)); |
| 231 | sg_init_one(&sg[1], &rxkhdr, sizeof(rxkhdr)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 232 | crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(rxkhdr)); |
| 233 | |
| 234 | /* we want to encrypt the skbuff in-place */ |
| 235 | nsg = skb_cow_data(skb, 0, &trailer); |
| 236 | if (nsg < 0 || nsg > 16) |
| 237 | return -ENOMEM; |
| 238 | |
| 239 | len = data_size + call->conn->size_align - 1; |
| 240 | len &= ~(call->conn->size_align - 1); |
| 241 | |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 242 | sg_init_table(sg, nsg); |
| 243 | skb_to_sgvec(skb, sg, 0, len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 244 | crypto_blkcipher_encrypt_iv(&desc, sg, sg, len); |
| 245 | |
| 246 | _leave(" = 0"); |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * checksum an RxRPC packet header |
| 252 | */ |
| 253 | static int rxkad_secure_packet(const struct rxrpc_call *call, |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame^] | 254 | struct sk_buff *skb, |
| 255 | size_t data_size, |
| 256 | void *sechdr) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 257 | { |
| 258 | struct rxrpc_skb_priv *sp; |
| 259 | struct blkcipher_desc desc; |
| 260 | struct rxrpc_crypt iv; |
| 261 | struct scatterlist sg[2]; |
| 262 | struct { |
| 263 | __be32 x[2]; |
| 264 | } tmpbuf __attribute__((aligned(8))); /* must all be in same page */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 265 | u32 x, y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 266 | int ret; |
| 267 | |
| 268 | sp = rxrpc_skb(skb); |
| 269 | |
| 270 | _enter("{%d{%x}},{#%u},%zu,", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 271 | call->debug_id, key_serial(call->conn->key), sp->hdr.seq, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 272 | data_size); |
| 273 | |
| 274 | if (!call->conn->cipher) |
| 275 | return 0; |
| 276 | |
| 277 | ret = key_validate(call->conn->key); |
| 278 | if (ret < 0) |
| 279 | return ret; |
| 280 | |
| 281 | /* continue encrypting from where we left off */ |
| 282 | memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); |
| 283 | desc.tfm = call->conn->cipher; |
| 284 | desc.info = iv.x; |
| 285 | desc.flags = 0; |
| 286 | |
| 287 | /* calculate the security checksum */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 288 | x = call->channel << (32 - RXRPC_CIDSHIFT); |
| 289 | x |= sp->hdr.seq & 0x3fffffff; |
| 290 | tmpbuf.x[0] = htonl(sp->hdr.callNumber); |
| 291 | tmpbuf.x[1] = htonl(x); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 292 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 293 | sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf)); |
| 294 | sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 295 | crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf)); |
| 296 | |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 297 | y = ntohl(tmpbuf.x[1]); |
| 298 | y = (y >> 16) & 0xffff; |
| 299 | if (y == 0) |
| 300 | y = 1; /* zero checksums are not permitted */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 301 | sp->hdr.cksum = y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 302 | |
| 303 | switch (call->conn->security_level) { |
| 304 | case RXRPC_SECURITY_PLAIN: |
| 305 | ret = 0; |
| 306 | break; |
| 307 | case RXRPC_SECURITY_AUTH: |
| 308 | ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr); |
| 309 | break; |
| 310 | case RXRPC_SECURITY_ENCRYPT: |
| 311 | ret = rxkad_secure_packet_encrypt(call, skb, data_size, |
| 312 | sechdr); |
| 313 | break; |
| 314 | default: |
| 315 | ret = -EPERM; |
| 316 | break; |
| 317 | } |
| 318 | |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 319 | _leave(" = %d [set %hx]", ret, y); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * decrypt partial encryption on a packet (level 1 security) |
| 325 | */ |
| 326 | static int rxkad_verify_packet_auth(const struct rxrpc_call *call, |
| 327 | struct sk_buff *skb, |
| 328 | u32 *_abort_code) |
| 329 | { |
| 330 | struct rxkad_level1_hdr sechdr; |
| 331 | struct rxrpc_skb_priv *sp; |
| 332 | struct blkcipher_desc desc; |
| 333 | struct rxrpc_crypt iv; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 334 | struct scatterlist sg[16]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 335 | struct sk_buff *trailer; |
| 336 | u32 data_size, buf; |
| 337 | u16 check; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 338 | int nsg; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 339 | |
| 340 | _enter(""); |
| 341 | |
| 342 | sp = rxrpc_skb(skb); |
| 343 | |
| 344 | /* we want to decrypt the skbuff in-place */ |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 345 | nsg = skb_cow_data(skb, 0, &trailer); |
| 346 | if (nsg < 0 || nsg > 16) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 347 | goto nomem; |
| 348 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 349 | sg_init_table(sg, nsg); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 350 | skb_to_sgvec(skb, sg, 0, 8); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 351 | |
| 352 | /* start the decryption afresh */ |
| 353 | memset(&iv, 0, sizeof(iv)); |
| 354 | desc.tfm = call->conn->cipher; |
| 355 | desc.info = iv.x; |
| 356 | desc.flags = 0; |
| 357 | |
| 358 | crypto_blkcipher_decrypt_iv(&desc, sg, sg, 8); |
| 359 | |
| 360 | /* remove the decrypted packet length */ |
| 361 | if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0) |
| 362 | goto datalen_error; |
| 363 | if (!skb_pull(skb, sizeof(sechdr))) |
| 364 | BUG(); |
| 365 | |
| 366 | buf = ntohl(sechdr.data_size); |
| 367 | data_size = buf & 0xffff; |
| 368 | |
| 369 | check = buf >> 16; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 370 | check ^= sp->hdr.seq ^ sp->hdr.callNumber; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 371 | check &= 0xffff; |
| 372 | if (check != 0) { |
| 373 | *_abort_code = RXKADSEALEDINCON; |
| 374 | goto protocol_error; |
| 375 | } |
| 376 | |
| 377 | /* shorten the packet to remove the padding */ |
| 378 | if (data_size > skb->len) |
| 379 | goto datalen_error; |
| 380 | else if (data_size < skb->len) |
| 381 | skb->len = data_size; |
| 382 | |
| 383 | _leave(" = 0 [dlen=%x]", data_size); |
| 384 | return 0; |
| 385 | |
| 386 | datalen_error: |
| 387 | *_abort_code = RXKADDATALEN; |
| 388 | protocol_error: |
| 389 | _leave(" = -EPROTO"); |
| 390 | return -EPROTO; |
| 391 | |
| 392 | nomem: |
| 393 | _leave(" = -ENOMEM"); |
| 394 | return -ENOMEM; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * wholly decrypt a packet (level 2 security) |
| 399 | */ |
| 400 | static int rxkad_verify_packet_encrypt(const struct rxrpc_call *call, |
| 401 | struct sk_buff *skb, |
| 402 | u32 *_abort_code) |
| 403 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 404 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 405 | struct rxkad_level2_hdr sechdr; |
| 406 | struct rxrpc_skb_priv *sp; |
| 407 | struct blkcipher_desc desc; |
| 408 | struct rxrpc_crypt iv; |
| 409 | struct scatterlist _sg[4], *sg; |
| 410 | struct sk_buff *trailer; |
| 411 | u32 data_size, buf; |
| 412 | u16 check; |
| 413 | int nsg; |
| 414 | |
| 415 | _enter(",{%d}", skb->len); |
| 416 | |
| 417 | sp = rxrpc_skb(skb); |
| 418 | |
| 419 | /* we want to decrypt the skbuff in-place */ |
| 420 | nsg = skb_cow_data(skb, 0, &trailer); |
| 421 | if (nsg < 0) |
| 422 | goto nomem; |
| 423 | |
| 424 | sg = _sg; |
| 425 | if (unlikely(nsg > 4)) { |
| 426 | sg = kmalloc(sizeof(*sg) * nsg, GFP_NOIO); |
| 427 | if (!sg) |
| 428 | goto nomem; |
| 429 | } |
| 430 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 431 | sg_init_table(sg, nsg); |
David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 432 | skb_to_sgvec(skb, sg, 0, skb->len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 433 | |
| 434 | /* decrypt from the session key */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 435 | token = call->conn->key->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 436 | memcpy(&iv, token->kad->session_key, sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 437 | desc.tfm = call->conn->cipher; |
| 438 | desc.info = iv.x; |
| 439 | desc.flags = 0; |
| 440 | |
| 441 | crypto_blkcipher_decrypt_iv(&desc, sg, sg, skb->len); |
| 442 | if (sg != _sg) |
| 443 | kfree(sg); |
| 444 | |
| 445 | /* remove the decrypted packet length */ |
| 446 | if (skb_copy_bits(skb, 0, &sechdr, sizeof(sechdr)) < 0) |
| 447 | goto datalen_error; |
| 448 | if (!skb_pull(skb, sizeof(sechdr))) |
| 449 | BUG(); |
| 450 | |
| 451 | buf = ntohl(sechdr.data_size); |
| 452 | data_size = buf & 0xffff; |
| 453 | |
| 454 | check = buf >> 16; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 455 | check ^= sp->hdr.seq ^ sp->hdr.callNumber; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 456 | check &= 0xffff; |
| 457 | if (check != 0) { |
| 458 | *_abort_code = RXKADSEALEDINCON; |
| 459 | goto protocol_error; |
| 460 | } |
| 461 | |
| 462 | /* shorten the packet to remove the padding */ |
| 463 | if (data_size > skb->len) |
| 464 | goto datalen_error; |
| 465 | else if (data_size < skb->len) |
| 466 | skb->len = data_size; |
| 467 | |
| 468 | _leave(" = 0 [dlen=%x]", data_size); |
| 469 | return 0; |
| 470 | |
| 471 | datalen_error: |
| 472 | *_abort_code = RXKADDATALEN; |
| 473 | protocol_error: |
| 474 | _leave(" = -EPROTO"); |
| 475 | return -EPROTO; |
| 476 | |
| 477 | nomem: |
| 478 | _leave(" = -ENOMEM"); |
| 479 | return -ENOMEM; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * verify the security on a received packet |
| 484 | */ |
| 485 | static int rxkad_verify_packet(const struct rxrpc_call *call, |
| 486 | struct sk_buff *skb, |
| 487 | u32 *_abort_code) |
| 488 | { |
| 489 | struct blkcipher_desc desc; |
| 490 | struct rxrpc_skb_priv *sp; |
| 491 | struct rxrpc_crypt iv; |
| 492 | struct scatterlist sg[2]; |
| 493 | struct { |
| 494 | __be32 x[2]; |
| 495 | } tmpbuf __attribute__((aligned(8))); /* must all be in same page */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 496 | u16 cksum; |
| 497 | u32 x, y; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 498 | int ret; |
| 499 | |
| 500 | sp = rxrpc_skb(skb); |
| 501 | |
| 502 | _enter("{%d{%x}},{#%u}", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 503 | call->debug_id, key_serial(call->conn->key), sp->hdr.seq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 504 | |
| 505 | if (!call->conn->cipher) |
| 506 | return 0; |
| 507 | |
David Howells | 8b81547 | 2009-09-14 01:17:30 +0000 | [diff] [blame] | 508 | if (sp->hdr.securityIndex != RXRPC_SECURITY_RXKAD) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 509 | *_abort_code = RXKADINCONSISTENCY; |
| 510 | _leave(" = -EPROTO [not rxkad]"); |
| 511 | return -EPROTO; |
| 512 | } |
| 513 | |
| 514 | /* continue encrypting from where we left off */ |
| 515 | memcpy(&iv, call->conn->csum_iv.x, sizeof(iv)); |
| 516 | desc.tfm = call->conn->cipher; |
| 517 | desc.info = iv.x; |
| 518 | desc.flags = 0; |
| 519 | |
| 520 | /* validate the security checksum */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 521 | x = call->channel << (32 - RXRPC_CIDSHIFT); |
| 522 | x |= sp->hdr.seq & 0x3fffffff; |
| 523 | tmpbuf.x[0] = htonl(call->call_id); |
| 524 | tmpbuf.x[1] = htonl(x); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 525 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 526 | sg_init_one(&sg[0], &tmpbuf, sizeof(tmpbuf)); |
| 527 | sg_init_one(&sg[1], &tmpbuf, sizeof(tmpbuf)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 528 | crypto_blkcipher_encrypt_iv(&desc, &sg[0], &sg[1], sizeof(tmpbuf)); |
| 529 | |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 530 | y = ntohl(tmpbuf.x[1]); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 531 | cksum = (y >> 16) & 0xffff; |
| 532 | if (cksum == 0) |
| 533 | cksum = 1; /* zero checksums are not permitted */ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 534 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 535 | if (sp->hdr.cksum != cksum) { |
| 536 | *_abort_code = RXKADSEALEDINCON; |
| 537 | _leave(" = -EPROTO [csum failed]"); |
| 538 | return -EPROTO; |
| 539 | } |
| 540 | |
| 541 | switch (call->conn->security_level) { |
| 542 | case RXRPC_SECURITY_PLAIN: |
| 543 | ret = 0; |
| 544 | break; |
| 545 | case RXRPC_SECURITY_AUTH: |
| 546 | ret = rxkad_verify_packet_auth(call, skb, _abort_code); |
| 547 | break; |
| 548 | case RXRPC_SECURITY_ENCRYPT: |
| 549 | ret = rxkad_verify_packet_encrypt(call, skb, _abort_code); |
| 550 | break; |
| 551 | default: |
| 552 | ret = -ENOANO; |
| 553 | break; |
| 554 | } |
| 555 | |
| 556 | _leave(" = %d", ret); |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * issue a challenge |
| 562 | */ |
| 563 | static int rxkad_issue_challenge(struct rxrpc_connection *conn) |
| 564 | { |
| 565 | struct rxkad_challenge challenge; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 566 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 567 | struct msghdr msg; |
| 568 | struct kvec iov[2]; |
| 569 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 570 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 571 | int ret; |
| 572 | |
| 573 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->key)); |
| 574 | |
| 575 | ret = key_validate(conn->key); |
| 576 | if (ret < 0) |
| 577 | return ret; |
| 578 | |
| 579 | get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce)); |
| 580 | |
| 581 | challenge.version = htonl(2); |
| 582 | challenge.nonce = htonl(conn->security_nonce); |
| 583 | challenge.min_level = htonl(0); |
| 584 | challenge.__padding = 0; |
| 585 | |
| 586 | msg.msg_name = &conn->trans->peer->srx.transport.sin; |
| 587 | msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin); |
| 588 | msg.msg_control = NULL; |
| 589 | msg.msg_controllen = 0; |
| 590 | msg.msg_flags = 0; |
| 591 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 592 | whdr.epoch = htonl(conn->epoch); |
| 593 | whdr.cid = htonl(conn->cid); |
| 594 | whdr.callNumber = 0; |
| 595 | whdr.seq = 0; |
| 596 | whdr.type = RXRPC_PACKET_TYPE_CHALLENGE; |
| 597 | whdr.flags = conn->out_clientflag; |
| 598 | whdr.userStatus = 0; |
| 599 | whdr.securityIndex = conn->security_ix; |
| 600 | whdr._rsvd = 0; |
| 601 | whdr.serviceId = htons(conn->service_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 602 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 603 | iov[0].iov_base = &whdr; |
| 604 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 605 | iov[1].iov_base = &challenge; |
| 606 | iov[1].iov_len = sizeof(challenge); |
| 607 | |
| 608 | len = iov[0].iov_len + iov[1].iov_len; |
| 609 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 610 | serial = atomic_inc_return(&conn->serial); |
| 611 | whdr.serial = htonl(serial); |
| 612 | _proto("Tx CHALLENGE %%%u", serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 613 | |
| 614 | ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len); |
| 615 | if (ret < 0) { |
| 616 | _debug("sendmsg failed: %d", ret); |
| 617 | return -EAGAIN; |
| 618 | } |
| 619 | |
| 620 | _leave(" = 0"); |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * send a Kerberos security response |
| 626 | */ |
| 627 | static int rxkad_send_response(struct rxrpc_connection *conn, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 628 | struct rxrpc_host_header *hdr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 629 | struct rxkad_response *resp, |
| 630 | const struct rxkad_key *s2) |
| 631 | { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 632 | struct rxrpc_wire_header whdr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 633 | struct msghdr msg; |
| 634 | struct kvec iov[3]; |
| 635 | size_t len; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 636 | u32 serial; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 637 | int ret; |
| 638 | |
| 639 | _enter(""); |
| 640 | |
| 641 | msg.msg_name = &conn->trans->peer->srx.transport.sin; |
| 642 | msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin); |
| 643 | msg.msg_control = NULL; |
| 644 | msg.msg_controllen = 0; |
| 645 | msg.msg_flags = 0; |
| 646 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 647 | memset(&whdr, 0, sizeof(whdr)); |
| 648 | whdr.epoch = htonl(hdr->epoch); |
| 649 | whdr.cid = htonl(hdr->cid); |
| 650 | whdr.type = RXRPC_PACKET_TYPE_RESPONSE; |
| 651 | whdr.flags = conn->out_clientflag; |
| 652 | whdr.securityIndex = hdr->securityIndex; |
| 653 | whdr.serviceId = htons(hdr->serviceId); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 654 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 655 | iov[0].iov_base = &whdr; |
| 656 | iov[0].iov_len = sizeof(whdr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 657 | iov[1].iov_base = resp; |
| 658 | iov[1].iov_len = sizeof(*resp); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 659 | iov[2].iov_base = (void *)s2->ticket; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 660 | iov[2].iov_len = s2->ticket_len; |
| 661 | |
| 662 | len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; |
| 663 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 664 | serial = atomic_inc_return(&conn->serial); |
| 665 | whdr.serial = htonl(serial); |
| 666 | _proto("Tx RESPONSE %%%u", serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 667 | |
| 668 | ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 3, len); |
| 669 | if (ret < 0) { |
| 670 | _debug("sendmsg failed: %d", ret); |
| 671 | return -EAGAIN; |
| 672 | } |
| 673 | |
| 674 | _leave(" = 0"); |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * calculate the response checksum |
| 680 | */ |
| 681 | static void rxkad_calc_response_checksum(struct rxkad_response *response) |
| 682 | { |
| 683 | u32 csum = 1000003; |
| 684 | int loop; |
| 685 | u8 *p = (u8 *) response; |
| 686 | |
| 687 | for (loop = sizeof(*response); loop > 0; loop--) |
| 688 | csum = csum * 0x10204081 + *p++; |
| 689 | |
| 690 | response->encrypted.checksum = htonl(csum); |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * load a scatterlist with a potentially split-page buffer |
| 695 | */ |
| 696 | static void rxkad_sg_set_buf2(struct scatterlist sg[2], |
| 697 | void *buf, size_t buflen) |
| 698 | { |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 699 | int nsg = 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 700 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 701 | sg_init_table(sg, 2); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 702 | |
| 703 | sg_set_buf(&sg[0], buf, buflen); |
| 704 | if (sg[0].offset + buflen > PAGE_SIZE) { |
| 705 | /* the buffer was split over two pages */ |
| 706 | sg[0].length = PAGE_SIZE - sg[0].offset; |
| 707 | sg_set_buf(&sg[1], buf + sg[0].length, buflen - sg[0].length); |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 708 | nsg++; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Jens Axboe | c46f233 | 2007-10-31 12:06:37 +0100 | [diff] [blame] | 711 | sg_mark_end(&sg[nsg - 1]); |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 712 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 713 | ASSERTCMP(sg[0].length + sg[1].length, ==, buflen); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * encrypt the response packet |
| 718 | */ |
| 719 | static void rxkad_encrypt_response(struct rxrpc_connection *conn, |
| 720 | struct rxkad_response *resp, |
| 721 | const struct rxkad_key *s2) |
| 722 | { |
| 723 | struct blkcipher_desc desc; |
| 724 | struct rxrpc_crypt iv; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 725 | struct scatterlist sg[2]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 726 | |
| 727 | /* continue encrypting from where we left off */ |
| 728 | memcpy(&iv, s2->session_key, sizeof(iv)); |
| 729 | desc.tfm = conn->cipher; |
| 730 | desc.info = iv.x; |
| 731 | desc.flags = 0; |
| 732 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 733 | rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted)); |
| 734 | crypto_blkcipher_encrypt_iv(&desc, sg, sg, sizeof(resp->encrypted)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* |
| 738 | * respond to a challenge packet |
| 739 | */ |
| 740 | static int rxkad_respond_to_challenge(struct rxrpc_connection *conn, |
| 741 | struct sk_buff *skb, |
| 742 | u32 *_abort_code) |
| 743 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 744 | const struct rxrpc_key_token *token; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 745 | struct rxkad_challenge challenge; |
| 746 | struct rxkad_response resp |
| 747 | __attribute__((aligned(8))); /* must be aligned for crypto */ |
| 748 | struct rxrpc_skb_priv *sp; |
| 749 | u32 version, nonce, min_level, abort_code; |
| 750 | int ret; |
| 751 | |
| 752 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->key)); |
| 753 | |
| 754 | if (!conn->key) { |
| 755 | _leave(" = -EPROTO [no key]"); |
| 756 | return -EPROTO; |
| 757 | } |
| 758 | |
| 759 | ret = key_validate(conn->key); |
| 760 | if (ret < 0) { |
| 761 | *_abort_code = RXKADEXPIRED; |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | abort_code = RXKADPACKETSHORT; |
| 766 | sp = rxrpc_skb(skb); |
| 767 | if (skb_copy_bits(skb, 0, &challenge, sizeof(challenge)) < 0) |
| 768 | goto protocol_error; |
| 769 | |
| 770 | version = ntohl(challenge.version); |
| 771 | nonce = ntohl(challenge.nonce); |
| 772 | min_level = ntohl(challenge.min_level); |
| 773 | |
| 774 | _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 775 | sp->hdr.serial, version, nonce, min_level); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 776 | |
| 777 | abort_code = RXKADINCONSISTENCY; |
| 778 | if (version != RXKAD_VERSION) |
| 779 | goto protocol_error; |
| 780 | |
| 781 | abort_code = RXKADLEVELFAIL; |
| 782 | if (conn->security_level < min_level) |
| 783 | goto protocol_error; |
| 784 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 785 | token = conn->key->payload.data[0]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 786 | |
| 787 | /* build the response packet */ |
| 788 | memset(&resp, 0, sizeof(resp)); |
| 789 | |
| 790 | resp.version = RXKAD_VERSION; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 791 | resp.encrypted.epoch = htonl(conn->epoch); |
| 792 | resp.encrypted.cid = htonl(conn->cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 793 | resp.encrypted.securityIndex = htonl(conn->security_ix); |
| 794 | resp.encrypted.call_id[0] = |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 795 | htonl(conn->channels[0] ? conn->channels[0]->call_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 796 | resp.encrypted.call_id[1] = |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 797 | htonl(conn->channels[1] ? conn->channels[1]->call_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 798 | resp.encrypted.call_id[2] = |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 799 | htonl(conn->channels[2] ? conn->channels[2]->call_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 800 | resp.encrypted.call_id[3] = |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 801 | htonl(conn->channels[3] ? conn->channels[3]->call_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 802 | resp.encrypted.inc_nonce = htonl(nonce + 1); |
| 803 | resp.encrypted.level = htonl(conn->security_level); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 804 | resp.kvno = htonl(token->kad->kvno); |
| 805 | resp.ticket_len = htonl(token->kad->ticket_len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 806 | |
| 807 | /* calculate the response checksum and then do the encryption */ |
| 808 | rxkad_calc_response_checksum(&resp); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 809 | rxkad_encrypt_response(conn, &resp, token->kad); |
| 810 | return rxkad_send_response(conn, &sp->hdr, &resp, token->kad); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 811 | |
| 812 | protocol_error: |
| 813 | *_abort_code = abort_code; |
| 814 | _leave(" = -EPROTO [%d]", abort_code); |
| 815 | return -EPROTO; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * decrypt the kerberos IV ticket in the response |
| 820 | */ |
| 821 | static int rxkad_decrypt_ticket(struct rxrpc_connection *conn, |
| 822 | void *ticket, size_t ticket_len, |
| 823 | struct rxrpc_crypt *_session_key, |
| 824 | time_t *_expiry, |
| 825 | u32 *_abort_code) |
| 826 | { |
| 827 | struct blkcipher_desc desc; |
| 828 | struct rxrpc_crypt iv, key; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 829 | struct scatterlist sg[1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 830 | struct in_addr addr; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 831 | unsigned int life; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 832 | time_t issue, now; |
| 833 | bool little_endian; |
| 834 | int ret; |
| 835 | u8 *p, *q, *name, *end; |
| 836 | |
| 837 | _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key)); |
| 838 | |
| 839 | *_expiry = 0; |
| 840 | |
| 841 | ret = key_validate(conn->server_key); |
| 842 | if (ret < 0) { |
| 843 | switch (ret) { |
| 844 | case -EKEYEXPIRED: |
| 845 | *_abort_code = RXKADEXPIRED; |
| 846 | goto error; |
| 847 | default: |
| 848 | *_abort_code = RXKADNOAUTH; |
| 849 | goto error; |
| 850 | } |
| 851 | } |
| 852 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 853 | ASSERT(conn->server_key->payload.data[0] != NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 854 | ASSERTCMP((unsigned long) ticket & 7UL, ==, 0); |
| 855 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 856 | memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 857 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 858 | desc.tfm = conn->server_key->payload.data[0]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 859 | desc.info = iv.x; |
| 860 | desc.flags = 0; |
| 861 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 862 | sg_init_one(&sg[0], ticket, ticket_len); |
| 863 | crypto_blkcipher_decrypt_iv(&desc, sg, sg, ticket_len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 864 | |
| 865 | p = ticket; |
| 866 | end = p + ticket_len; |
| 867 | |
| 868 | #define Z(size) \ |
| 869 | ({ \ |
| 870 | u8 *__str = p; \ |
| 871 | q = memchr(p, 0, end - p); \ |
| 872 | if (!q || q - p > (size)) \ |
| 873 | goto bad_ticket; \ |
| 874 | for (; p < q; p++) \ |
| 875 | if (!isprint(*p)) \ |
| 876 | goto bad_ticket; \ |
| 877 | p++; \ |
| 878 | __str; \ |
| 879 | }) |
| 880 | |
| 881 | /* extract the ticket flags */ |
| 882 | _debug("KIV FLAGS: %x", *p); |
| 883 | little_endian = *p & 1; |
| 884 | p++; |
| 885 | |
| 886 | /* extract the authentication name */ |
| 887 | name = Z(ANAME_SZ); |
| 888 | _debug("KIV ANAME: %s", name); |
| 889 | |
| 890 | /* extract the principal's instance */ |
| 891 | name = Z(INST_SZ); |
| 892 | _debug("KIV INST : %s", name); |
| 893 | |
| 894 | /* extract the principal's authentication domain */ |
| 895 | name = Z(REALM_SZ); |
| 896 | _debug("KIV REALM: %s", name); |
| 897 | |
| 898 | if (end - p < 4 + 8 + 4 + 2) |
| 899 | goto bad_ticket; |
| 900 | |
| 901 | /* get the IPv4 address of the entity that requested the ticket */ |
| 902 | memcpy(&addr, p, sizeof(addr)); |
| 903 | p += 4; |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 904 | _debug("KIV ADDR : %pI4", &addr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 905 | |
| 906 | /* get the session key from the ticket */ |
| 907 | memcpy(&key, p, sizeof(key)); |
| 908 | p += 8; |
| 909 | _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1])); |
| 910 | memcpy(_session_key, &key, sizeof(key)); |
| 911 | |
| 912 | /* get the ticket's lifetime */ |
| 913 | life = *p++ * 5 * 60; |
| 914 | _debug("KIV LIFE : %u", life); |
| 915 | |
| 916 | /* get the issue time of the ticket */ |
| 917 | if (little_endian) { |
| 918 | __le32 stamp; |
| 919 | memcpy(&stamp, p, 4); |
| 920 | issue = le32_to_cpu(stamp); |
| 921 | } else { |
| 922 | __be32 stamp; |
| 923 | memcpy(&stamp, p, 4); |
| 924 | issue = be32_to_cpu(stamp); |
| 925 | } |
| 926 | p += 4; |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 927 | now = get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 928 | _debug("KIV ISSUE: %lx [%lx]", issue, now); |
| 929 | |
| 930 | /* check the ticket is in date */ |
| 931 | if (issue > now) { |
| 932 | *_abort_code = RXKADNOAUTH; |
| 933 | ret = -EKEYREJECTED; |
| 934 | goto error; |
| 935 | } |
| 936 | |
| 937 | if (issue < now - life) { |
| 938 | *_abort_code = RXKADEXPIRED; |
| 939 | ret = -EKEYEXPIRED; |
| 940 | goto error; |
| 941 | } |
| 942 | |
| 943 | *_expiry = issue + life; |
| 944 | |
| 945 | /* get the service name */ |
| 946 | name = Z(SNAME_SZ); |
| 947 | _debug("KIV SNAME: %s", name); |
| 948 | |
| 949 | /* get the service instance name */ |
| 950 | name = Z(INST_SZ); |
| 951 | _debug("KIV SINST: %s", name); |
| 952 | |
| 953 | ret = 0; |
| 954 | error: |
| 955 | _leave(" = %d", ret); |
| 956 | return ret; |
| 957 | |
| 958 | bad_ticket: |
| 959 | *_abort_code = RXKADBADTICKET; |
| 960 | ret = -EBADMSG; |
| 961 | goto error; |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * decrypt the response packet |
| 966 | */ |
| 967 | static void rxkad_decrypt_response(struct rxrpc_connection *conn, |
| 968 | struct rxkad_response *resp, |
| 969 | const struct rxrpc_crypt *session_key) |
| 970 | { |
| 971 | struct blkcipher_desc desc; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 972 | struct scatterlist sg[2]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 973 | struct rxrpc_crypt iv; |
| 974 | |
| 975 | _enter(",,%08x%08x", |
| 976 | ntohl(session_key->n[0]), ntohl(session_key->n[1])); |
| 977 | |
| 978 | ASSERT(rxkad_ci != NULL); |
| 979 | |
| 980 | mutex_lock(&rxkad_ci_mutex); |
| 981 | if (crypto_blkcipher_setkey(rxkad_ci, session_key->x, |
| 982 | sizeof(*session_key)) < 0) |
| 983 | BUG(); |
| 984 | |
| 985 | memcpy(&iv, session_key, sizeof(iv)); |
| 986 | desc.tfm = rxkad_ci; |
| 987 | desc.info = iv.x; |
| 988 | desc.flags = 0; |
| 989 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 990 | rxkad_sg_set_buf2(sg, &resp->encrypted, sizeof(resp->encrypted)); |
| 991 | crypto_blkcipher_decrypt_iv(&desc, sg, sg, sizeof(resp->encrypted)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 992 | mutex_unlock(&rxkad_ci_mutex); |
| 993 | |
| 994 | _leave(""); |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * verify a response |
| 999 | */ |
| 1000 | static int rxkad_verify_response(struct rxrpc_connection *conn, |
| 1001 | struct sk_buff *skb, |
| 1002 | u32 *_abort_code) |
| 1003 | { |
| 1004 | struct rxkad_response response |
| 1005 | __attribute__((aligned(8))); /* must be aligned for crypto */ |
| 1006 | struct rxrpc_skb_priv *sp; |
| 1007 | struct rxrpc_crypt session_key; |
| 1008 | time_t expiry; |
| 1009 | void *ticket; |
Al Viro | 91e916c | 2008-03-29 03:08:38 +0000 | [diff] [blame] | 1010 | u32 abort_code, version, kvno, ticket_len, level; |
| 1011 | __be32 csum; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1012 | int ret; |
| 1013 | |
| 1014 | _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key)); |
| 1015 | |
| 1016 | abort_code = RXKADPACKETSHORT; |
| 1017 | if (skb_copy_bits(skb, 0, &response, sizeof(response)) < 0) |
| 1018 | goto protocol_error; |
| 1019 | if (!pskb_pull(skb, sizeof(response))) |
| 1020 | BUG(); |
| 1021 | |
| 1022 | version = ntohl(response.version); |
| 1023 | ticket_len = ntohl(response.ticket_len); |
| 1024 | kvno = ntohl(response.kvno); |
| 1025 | sp = rxrpc_skb(skb); |
| 1026 | _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 1027 | sp->hdr.serial, version, kvno, ticket_len); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1028 | |
| 1029 | abort_code = RXKADINCONSISTENCY; |
| 1030 | if (version != RXKAD_VERSION) |
David Howells | 4aa9cb3 | 2007-12-07 04:31:47 -0800 | [diff] [blame] | 1031 | goto protocol_error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1032 | |
| 1033 | abort_code = RXKADTICKETLEN; |
| 1034 | if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN) |
| 1035 | goto protocol_error; |
| 1036 | |
| 1037 | abort_code = RXKADUNKNOWNKEY; |
| 1038 | if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5) |
| 1039 | goto protocol_error; |
| 1040 | |
| 1041 | /* extract the kerberos ticket and decrypt and decode it */ |
| 1042 | ticket = kmalloc(ticket_len, GFP_NOFS); |
| 1043 | if (!ticket) |
| 1044 | return -ENOMEM; |
| 1045 | |
| 1046 | abort_code = RXKADPACKETSHORT; |
| 1047 | if (skb_copy_bits(skb, 0, ticket, ticket_len) < 0) |
| 1048 | goto protocol_error_free; |
| 1049 | |
| 1050 | ret = rxkad_decrypt_ticket(conn, ticket, ticket_len, &session_key, |
| 1051 | &expiry, &abort_code); |
| 1052 | if (ret < 0) { |
| 1053 | *_abort_code = abort_code; |
| 1054 | kfree(ticket); |
| 1055 | return ret; |
| 1056 | } |
| 1057 | |
| 1058 | /* use the session key from inside the ticket to decrypt the |
| 1059 | * response */ |
| 1060 | rxkad_decrypt_response(conn, &response, &session_key); |
| 1061 | |
| 1062 | abort_code = RXKADSEALEDINCON; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 1063 | if (ntohl(response.encrypted.epoch) != conn->epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1064 | goto protocol_error_free; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 1065 | if (ntohl(response.encrypted.cid) != conn->cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1066 | goto protocol_error_free; |
| 1067 | if (ntohl(response.encrypted.securityIndex) != conn->security_ix) |
| 1068 | goto protocol_error_free; |
| 1069 | csum = response.encrypted.checksum; |
| 1070 | response.encrypted.checksum = 0; |
| 1071 | rxkad_calc_response_checksum(&response); |
| 1072 | if (response.encrypted.checksum != csum) |
| 1073 | goto protocol_error_free; |
| 1074 | |
| 1075 | if (ntohl(response.encrypted.call_id[0]) > INT_MAX || |
| 1076 | ntohl(response.encrypted.call_id[1]) > INT_MAX || |
| 1077 | ntohl(response.encrypted.call_id[2]) > INT_MAX || |
| 1078 | ntohl(response.encrypted.call_id[3]) > INT_MAX) |
| 1079 | goto protocol_error_free; |
| 1080 | |
| 1081 | abort_code = RXKADOUTOFSEQUENCE; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 1082 | if (ntohl(response.encrypted.inc_nonce) != conn->security_nonce + 1) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1083 | goto protocol_error_free; |
| 1084 | |
| 1085 | abort_code = RXKADLEVELFAIL; |
| 1086 | level = ntohl(response.encrypted.level); |
| 1087 | if (level > RXRPC_SECURITY_ENCRYPT) |
| 1088 | goto protocol_error_free; |
| 1089 | conn->security_level = level; |
| 1090 | |
| 1091 | /* create a key to hold the security data and expiration time - after |
| 1092 | * this the connection security can be handled in exactly the same way |
| 1093 | * as for a client connection */ |
| 1094 | ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno); |
| 1095 | if (ret < 0) { |
| 1096 | kfree(ticket); |
| 1097 | return ret; |
| 1098 | } |
| 1099 | |
| 1100 | kfree(ticket); |
| 1101 | _leave(" = 0"); |
| 1102 | return 0; |
| 1103 | |
| 1104 | protocol_error_free: |
| 1105 | kfree(ticket); |
| 1106 | protocol_error: |
| 1107 | *_abort_code = abort_code; |
| 1108 | _leave(" = -EPROTO [%d]", abort_code); |
| 1109 | return -EPROTO; |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * clear the connection security |
| 1114 | */ |
| 1115 | static void rxkad_clear(struct rxrpc_connection *conn) |
| 1116 | { |
| 1117 | _enter(""); |
| 1118 | |
| 1119 | if (conn->cipher) |
| 1120 | crypto_free_blkcipher(conn->cipher); |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * RxRPC Kerberos-based security |
| 1125 | */ |
| 1126 | static struct rxrpc_security rxkad = { |
| 1127 | .owner = THIS_MODULE, |
| 1128 | .name = "rxkad", |
David Howells | 8b81547 | 2009-09-14 01:17:30 +0000 | [diff] [blame] | 1129 | .security_index = RXRPC_SECURITY_RXKAD, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1130 | .init_connection_security = rxkad_init_connection_security, |
| 1131 | .prime_packet_security = rxkad_prime_packet_security, |
| 1132 | .secure_packet = rxkad_secure_packet, |
| 1133 | .verify_packet = rxkad_verify_packet, |
| 1134 | .issue_challenge = rxkad_issue_challenge, |
| 1135 | .respond_to_challenge = rxkad_respond_to_challenge, |
| 1136 | .verify_response = rxkad_verify_response, |
| 1137 | .clear = rxkad_clear, |
| 1138 | }; |
| 1139 | |
| 1140 | static __init int rxkad_init(void) |
| 1141 | { |
| 1142 | _enter(""); |
| 1143 | |
| 1144 | /* pin the cipher we need so that the crypto layer doesn't invoke |
| 1145 | * keventd to go get it */ |
| 1146 | rxkad_ci = crypto_alloc_blkcipher("pcbc(fcrypt)", 0, CRYPTO_ALG_ASYNC); |
| 1147 | if (IS_ERR(rxkad_ci)) |
| 1148 | return PTR_ERR(rxkad_ci); |
| 1149 | |
| 1150 | return rxrpc_register_security(&rxkad); |
| 1151 | } |
| 1152 | |
| 1153 | module_init(rxkad_init); |
| 1154 | |
| 1155 | static __exit void rxkad_exit(void) |
| 1156 | { |
| 1157 | _enter(""); |
| 1158 | |
| 1159 | rxrpc_unregister_security(&rxkad); |
| 1160 | crypto_free_blkcipher(rxkad_ci); |
| 1161 | } |
| 1162 | |
| 1163 | module_exit(rxkad_exit); |