Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 2 | /* RxRPC security handling |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/net.h> |
| 10 | #include <linux/skbuff.h> |
| 11 | #include <linux/udp.h> |
| 12 | #include <linux/crypto.h> |
| 13 | #include <net/sock.h> |
| 14 | #include <net/af_rxrpc.h> |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 15 | #include <keys/rxrpc-type.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 16 | #include "ar-internal.h" |
| 17 | |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 18 | static const struct rxrpc_security *rxrpc_security_types[] = { |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 19 | [RXRPC_SECURITY_NONE] = &rxrpc_no_security, |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 20 | #ifdef CONFIG_RXKAD |
| 21 | [RXRPC_SECURITY_RXKAD] = &rxkad, |
| 22 | #endif |
| 23 | }; |
| 24 | |
| 25 | int __init rxrpc_init_security(void) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 26 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 27 | int i, ret; |
| 28 | |
| 29 | for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) { |
| 30 | if (rxrpc_security_types[i]) { |
| 31 | ret = rxrpc_security_types[i]->init(); |
| 32 | if (ret < 0) |
| 33 | goto failed; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | return 0; |
| 38 | |
| 39 | failed: |
| 40 | for (i--; i >= 0; i--) |
| 41 | if (rxrpc_security_types[i]) |
| 42 | rxrpc_security_types[i]->exit(); |
| 43 | return ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 44 | } |
| 45 | |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 46 | void rxrpc_exit_security(void) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 47 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 48 | int i; |
| 49 | |
| 50 | for (i = 0; i < ARRAY_SIZE(rxrpc_security_types); i++) |
| 51 | if (rxrpc_security_types[i]) |
| 52 | rxrpc_security_types[i]->exit(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
| 56 | * look up an rxrpc security module |
| 57 | */ |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 58 | static const struct rxrpc_security *rxrpc_security_lookup(u8 security_index) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 59 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 60 | if (security_index >= ARRAY_SIZE(rxrpc_security_types)) |
| 61 | return NULL; |
| 62 | return rxrpc_security_types[security_index]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 63 | } |
| 64 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 65 | /* |
| 66 | * initialise the security on a client connection |
| 67 | */ |
| 68 | int rxrpc_init_client_conn_security(struct rxrpc_connection *conn) |
| 69 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 70 | const struct rxrpc_security *sec; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 71 | struct rxrpc_key_token *token; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 72 | struct key *key = conn->params.key; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 73 | int ret; |
| 74 | |
| 75 | _enter("{%d},{%x}", conn->debug_id, key_serial(key)); |
| 76 | |
| 77 | if (!key) |
| 78 | return 0; |
| 79 | |
| 80 | ret = key_validate(key); |
| 81 | if (ret < 0) |
| 82 | return ret; |
| 83 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 84 | token = key->payload.data[0]; |
| 85 | if (!token) |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 86 | return -EKEYREJECTED; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 87 | |
| 88 | sec = rxrpc_security_lookup(token->security_index); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 89 | if (!sec) |
| 90 | return -EKEYREJECTED; |
| 91 | conn->security = sec; |
| 92 | |
| 93 | ret = conn->security->init_connection_security(conn); |
| 94 | if (ret < 0) { |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 95 | conn->security = &rxrpc_no_security; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | _leave(" = 0"); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 104 | * Find the security key for a server connection. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 105 | */ |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 106 | bool rxrpc_look_up_server_security(struct rxrpc_local *local, struct rxrpc_sock *rx, |
| 107 | const struct rxrpc_security **_sec, |
| 108 | struct key **_key, |
| 109 | struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 110 | { |
David Howells | 648af7f | 2016-04-07 17:23:51 +0100 | [diff] [blame] | 111 | const struct rxrpc_security *sec; |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 112 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 113 | key_ref_t kref = NULL; |
David Howells | b4f1342 | 2016-03-04 15:56:19 +0000 | [diff] [blame] | 114 | char kdesc[5 + 1 + 3 + 1]; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 115 | |
| 116 | _enter(""); |
| 117 | |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 118 | sprintf(kdesc, "%u:%u", sp->hdr.serviceId, sp->hdr.securityIndex); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 119 | |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 120 | sec = rxrpc_security_lookup(sp->hdr.securityIndex); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 121 | if (!sec) { |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 122 | trace_rxrpc_abort(0, "SVS", |
| 123 | sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq, |
| 124 | RX_INVALID_OPERATION, EKEYREJECTED); |
| 125 | skb->mark = RXRPC_SKB_MARK_REJECT_ABORT; |
| 126 | skb->priority = RX_INVALID_OPERATION; |
| 127 | return false; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 128 | } |
| 129 | |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 130 | if (sp->hdr.securityIndex == RXRPC_SECURITY_NONE) |
| 131 | goto out; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 132 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 133 | if (!rx->securities) { |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 134 | trace_rxrpc_abort(0, "SVR", |
| 135 | sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq, |
| 136 | RX_INVALID_OPERATION, EKEYREJECTED); |
| 137 | skb->mark = RXRPC_SKB_MARK_REJECT_ABORT; |
| 138 | skb->priority = RX_INVALID_OPERATION; |
| 139 | return false; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* look through the service's keyring */ |
| 143 | kref = keyring_search(make_key_ref(rx->securities, 1UL), |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 144 | &key_type_rxrpc_s, kdesc, true); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 145 | if (IS_ERR(kref)) { |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 146 | trace_rxrpc_abort(0, "SVK", |
| 147 | sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq, |
| 148 | sec->no_key_abort, EKEYREJECTED); |
| 149 | skb->mark = RXRPC_SKB_MARK_REJECT_ABORT; |
| 150 | skb->priority = sec->no_key_abort; |
| 151 | return false; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 152 | } |
| 153 | |
David Howells | 063c60d | 2019-12-20 16:17:16 +0000 | [diff] [blame] | 154 | out: |
| 155 | *_sec = sec; |
| 156 | *_key = key_ref_to_ptr(kref); |
| 157 | return true; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 158 | } |