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