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 key management |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
| 6 | * |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 7 | * RxRPC keys should have a description of describing their purpose: |
David Howells | 177b898 | 2020-10-15 15:47:45 +0100 | [diff] [blame] | 8 | * "afs@example.com" |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
Herbert Xu | 1afe593 | 2016-01-24 21:19:01 +0800 | [diff] [blame] | 13 | #include <crypto/skcipher.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/net.h> |
| 16 | #include <linux/skbuff.h> |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 17 | #include <linux/key-type.h> |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 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> |
| 22 | #include <keys/rxrpc-type.h> |
| 23 | #include <keys/user-type.h> |
| 24 | #include "ar-internal.h" |
| 25 | |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 26 | static int rxrpc_preparse(struct key_preparsed_payload *); |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 27 | static void rxrpc_free_preparse(struct key_preparsed_payload *); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 28 | static void rxrpc_destroy(struct key *); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 29 | static void rxrpc_describe(const struct key *, struct seq_file *); |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 30 | static long rxrpc_read(const struct key *, char *, size_t); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * rxrpc defined keys take an arbitrary string as the description and an |
| 34 | * arbitrary blob of data as the payload |
| 35 | */ |
| 36 | struct key_type key_type_rxrpc = { |
| 37 | .name = "rxrpc", |
David Howells | 9b24261 | 2019-06-26 21:02:33 +0100 | [diff] [blame] | 38 | .flags = KEY_TYPE_NET_DOMAIN, |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 39 | .preparse = rxrpc_preparse, |
| 40 | .free_preparse = rxrpc_free_preparse, |
| 41 | .instantiate = generic_key_instantiate, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 42 | .destroy = rxrpc_destroy, |
| 43 | .describe = rxrpc_describe, |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 44 | .read = rxrpc_read, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 45 | }; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 46 | EXPORT_SYMBOL(key_type_rxrpc); |
| 47 | |
| 48 | /* |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 49 | * parse an RxKAD type XDR format token |
| 50 | * - the caller guarantees we have at least 4 words |
| 51 | */ |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 52 | static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep, |
| 53 | size_t datalen, |
| 54 | const __be32 *xdr, unsigned int toklen) |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 55 | { |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 56 | struct rxrpc_key_token *token, **pptoken; |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 57 | time64_t expiry; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 58 | size_t plen; |
| 59 | u32 tktlen; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 60 | |
| 61 | _enter(",{%x,%x,%x,%x},%u", |
| 62 | ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]), |
| 63 | toklen); |
| 64 | |
| 65 | if (toklen <= 8 * 4) |
| 66 | return -EKEYREJECTED; |
| 67 | tktlen = ntohl(xdr[7]); |
| 68 | _debug("tktlen: %x", tktlen); |
| 69 | if (tktlen > AFSTOKEN_RK_TIX_MAX) |
| 70 | return -EKEYREJECTED; |
Nathaniel W Filardo | fde0133 | 2014-05-15 15:51:22 +0100 | [diff] [blame] | 71 | if (toklen < 8 * 4 + tktlen) |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 72 | return -EKEYREJECTED; |
| 73 | |
| 74 | plen = sizeof(*token) + sizeof(*token->kad) + tktlen; |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 75 | prep->quotalen = datalen + plen; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 76 | |
| 77 | plen -= sizeof(*token); |
Anton Blanchard | 0a93ea2 | 2011-02-25 15:33:17 +0000 | [diff] [blame] | 78 | token = kzalloc(sizeof(*token), GFP_KERNEL); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 79 | if (!token) |
| 80 | return -ENOMEM; |
| 81 | |
Anton Blanchard | 0a93ea2 | 2011-02-25 15:33:17 +0000 | [diff] [blame] | 82 | token->kad = kzalloc(plen, GFP_KERNEL); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 83 | if (!token->kad) { |
| 84 | kfree(token); |
| 85 | return -ENOMEM; |
| 86 | } |
| 87 | |
| 88 | token->security_index = RXRPC_SECURITY_RXKAD; |
| 89 | token->kad->ticket_len = tktlen; |
| 90 | token->kad->vice_id = ntohl(xdr[0]); |
| 91 | token->kad->kvno = ntohl(xdr[1]); |
| 92 | token->kad->start = ntohl(xdr[4]); |
| 93 | token->kad->expiry = ntohl(xdr[5]); |
| 94 | token->kad->primary_flag = ntohl(xdr[6]); |
| 95 | memcpy(&token->kad->session_key, &xdr[2], 8); |
| 96 | memcpy(&token->kad->ticket, &xdr[8], tktlen); |
| 97 | |
| 98 | _debug("SCIX: %u", token->security_index); |
| 99 | _debug("TLEN: %u", token->kad->ticket_len); |
| 100 | _debug("EXPY: %x", token->kad->expiry); |
| 101 | _debug("KVNO: %u", token->kad->kvno); |
| 102 | _debug("PRIM: %u", token->kad->primary_flag); |
| 103 | _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x", |
| 104 | token->kad->session_key[0], token->kad->session_key[1], |
| 105 | token->kad->session_key[2], token->kad->session_key[3], |
| 106 | token->kad->session_key[4], token->kad->session_key[5], |
| 107 | token->kad->session_key[6], token->kad->session_key[7]); |
| 108 | if (token->kad->ticket_len >= 8) |
| 109 | _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x", |
| 110 | token->kad->ticket[0], token->kad->ticket[1], |
| 111 | token->kad->ticket[2], token->kad->ticket[3], |
| 112 | token->kad->ticket[4], token->kad->ticket[5], |
| 113 | token->kad->ticket[6], token->kad->ticket[7]); |
| 114 | |
| 115 | /* count the number of tokens attached */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 116 | prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 117 | |
| 118 | /* attach the data */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 119 | for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0]; |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 120 | *pptoken; |
| 121 | pptoken = &(*pptoken)->next) |
| 122 | continue; |
| 123 | *pptoken = token; |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 124 | expiry = rxrpc_u32_to_time64(token->kad->expiry); |
| 125 | if (expiry < prep->expiry) |
| 126 | prep->expiry = expiry; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 127 | |
| 128 | _leave(" = 0"); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * attempt to parse the data as the XDR format |
| 134 | * - the caller guarantees we have more than 7 words |
| 135 | */ |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 136 | static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep) |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 137 | { |
David Howells | 4c20c33 | 2020-09-27 11:17:03 +0100 | [diff] [blame] | 138 | const __be32 *xdr = prep->data, *token, *p; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 139 | const char *cp; |
David Howells | 5f2f976 | 2017-06-15 00:12:24 +0100 | [diff] [blame] | 140 | unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix; |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 141 | size_t datalen = prep->datalen; |
David Howells | 9a0e646 | 2020-10-15 15:59:36 +0100 | [diff] [blame] | 142 | int ret, ret2; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 143 | |
| 144 | _enter(",{%x,%x,%x,%x},%zu", |
| 145 | ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]), |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 146 | prep->datalen); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 147 | |
| 148 | if (datalen > AFSTOKEN_LENGTH_MAX) |
| 149 | goto not_xdr; |
| 150 | |
| 151 | /* XDR is an array of __be32's */ |
| 152 | if (datalen & 3) |
| 153 | goto not_xdr; |
| 154 | |
| 155 | /* the flags should be 0 (the setpag bit must be handled by |
| 156 | * userspace) */ |
| 157 | if (ntohl(*xdr++) != 0) |
| 158 | goto not_xdr; |
| 159 | datalen -= 4; |
| 160 | |
| 161 | /* check the cell name */ |
| 162 | len = ntohl(*xdr++); |
| 163 | if (len < 1 || len > AFSTOKEN_CELL_MAX) |
| 164 | goto not_xdr; |
| 165 | datalen -= 4; |
David Howells | 5f2f976 | 2017-06-15 00:12:24 +0100 | [diff] [blame] | 166 | paddedlen = (len + 3) & ~3; |
| 167 | if (paddedlen > datalen) |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 168 | goto not_xdr; |
| 169 | |
| 170 | cp = (const char *) xdr; |
| 171 | for (loop = 0; loop < len; loop++) |
| 172 | if (!isprint(cp[loop])) |
| 173 | goto not_xdr; |
David Howells | 5f2f976 | 2017-06-15 00:12:24 +0100 | [diff] [blame] | 174 | for (; loop < paddedlen; loop++) |
| 175 | if (cp[loop]) |
| 176 | goto not_xdr; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 177 | _debug("cellname: [%u/%u] '%*.*s'", |
David Howells | 5f2f976 | 2017-06-15 00:12:24 +0100 | [diff] [blame] | 178 | len, paddedlen, len, len, (const char *) xdr); |
| 179 | datalen -= paddedlen; |
| 180 | xdr += paddedlen >> 2; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 181 | |
| 182 | /* get the token count */ |
| 183 | if (datalen < 12) |
| 184 | goto not_xdr; |
| 185 | ntoken = ntohl(*xdr++); |
| 186 | datalen -= 4; |
| 187 | _debug("ntoken: %x", ntoken); |
| 188 | if (ntoken < 1 || ntoken > AFSTOKEN_MAX) |
| 189 | goto not_xdr; |
| 190 | |
| 191 | /* check each token wrapper */ |
David Howells | 4c20c33 | 2020-09-27 11:17:03 +0100 | [diff] [blame] | 192 | p = xdr; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 193 | loop = ntoken; |
| 194 | do { |
| 195 | if (datalen < 8) |
| 196 | goto not_xdr; |
David Howells | 4c20c33 | 2020-09-27 11:17:03 +0100 | [diff] [blame] | 197 | toklen = ntohl(*p++); |
| 198 | sec_ix = ntohl(*p); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 199 | datalen -= 4; |
| 200 | _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix); |
David Howells | 5f2f976 | 2017-06-15 00:12:24 +0100 | [diff] [blame] | 201 | paddedlen = (toklen + 3) & ~3; |
| 202 | if (toklen < 20 || toklen > datalen || paddedlen > datalen) |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 203 | goto not_xdr; |
David Howells | 5f2f976 | 2017-06-15 00:12:24 +0100 | [diff] [blame] | 204 | datalen -= paddedlen; |
David Howells | 4c20c33 | 2020-09-27 11:17:03 +0100 | [diff] [blame] | 205 | p += paddedlen >> 2; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 206 | |
| 207 | } while (--loop > 0); |
| 208 | |
| 209 | _debug("remainder: %zu", datalen); |
| 210 | if (datalen != 0) |
| 211 | goto not_xdr; |
| 212 | |
| 213 | /* okay: we're going to assume it's valid XDR format |
| 214 | * - we ignore the cellname, relying on the key to be correctly named |
| 215 | */ |
David Howells | 9a0e646 | 2020-10-15 15:59:36 +0100 | [diff] [blame] | 216 | ret = -EPROTONOSUPPORT; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 217 | do { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 218 | toklen = ntohl(*xdr++); |
David Howells | 4c20c33 | 2020-09-27 11:17:03 +0100 | [diff] [blame] | 219 | token = xdr; |
| 220 | xdr += (toklen + 3) / 4; |
| 221 | |
| 222 | sec_ix = ntohl(*token++); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 223 | toklen -= 4; |
| 224 | |
David Howells | 4c20c33 | 2020-09-27 11:17:03 +0100 | [diff] [blame] | 225 | _debug("TOKEN type=%x len=%x", sec_ix, toklen); |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 226 | |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 227 | switch (sec_ix) { |
| 228 | case RXRPC_SECURITY_RXKAD: |
David Howells | 9a0e646 | 2020-10-15 15:59:36 +0100 | [diff] [blame] | 229 | ret2 = rxrpc_preparse_xdr_rxkad(prep, datalen, token, toklen); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 230 | break; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 231 | default: |
David Howells | 9a0e646 | 2020-10-15 15:59:36 +0100 | [diff] [blame] | 232 | ret2 = -EPROTONOSUPPORT; |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | switch (ret2) { |
| 237 | case 0: |
| 238 | ret = 0; |
| 239 | break; |
| 240 | case -EPROTONOSUPPORT: |
| 241 | break; |
| 242 | case -ENOPKG: |
| 243 | if (ret != 0) |
| 244 | ret = -ENOPKG; |
| 245 | break; |
| 246 | default: |
| 247 | ret = ret2; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 248 | goto error; |
| 249 | } |
| 250 | |
| 251 | } while (--ntoken > 0); |
| 252 | |
David Howells | 9a0e646 | 2020-10-15 15:59:36 +0100 | [diff] [blame] | 253 | error: |
| 254 | _leave(" = %d", ret); |
| 255 | return ret; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 256 | |
| 257 | not_xdr: |
| 258 | _leave(" = -EPROTO"); |
| 259 | return -EPROTO; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 263 | * Preparse an rxrpc defined key. |
| 264 | * |
| 265 | * Data should be of the form: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 266 | * OFFSET LEN CONTENT |
| 267 | * 0 4 key interface version number |
| 268 | * 4 2 security index (type) |
| 269 | * 6 2 ticket length |
| 270 | * 8 4 key expiry time (time_t) |
| 271 | * 12 4 kvno |
| 272 | * 16 8 session key |
| 273 | * 24 [len] ticket |
| 274 | * |
| 275 | * if no data is provided, then a no-security key is made |
| 276 | */ |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 277 | static int rxrpc_preparse(struct key_preparsed_payload *prep) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 278 | { |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 279 | const struct rxrpc_key_data_v1 *v1; |
| 280 | struct rxrpc_key_token *token, **pp; |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 281 | time64_t expiry; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 282 | size_t plen; |
| 283 | u32 kver; |
| 284 | int ret; |
| 285 | |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 286 | _enter("%zu", prep->datalen); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 287 | |
| 288 | /* handle a no-security key */ |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 289 | if (!prep->data && prep->datalen == 0) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 290 | return 0; |
| 291 | |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 292 | /* determine if the XDR payload format is being used */ |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 293 | if (prep->datalen > 7 * 4) { |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 294 | ret = rxrpc_preparse_xdr(prep); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 295 | if (ret != -EPROTO) |
| 296 | return ret; |
| 297 | } |
| 298 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 299 | /* get the key interface version number */ |
| 300 | ret = -EINVAL; |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 301 | if (prep->datalen <= 4 || !prep->data) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 302 | goto error; |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 303 | memcpy(&kver, prep->data, sizeof(kver)); |
| 304 | prep->data += sizeof(kver); |
| 305 | prep->datalen -= sizeof(kver); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 306 | |
| 307 | _debug("KEY I/F VERSION: %u", kver); |
| 308 | |
| 309 | ret = -EKEYREJECTED; |
| 310 | if (kver != 1) |
| 311 | goto error; |
| 312 | |
| 313 | /* deal with a version 1 key */ |
| 314 | ret = -EINVAL; |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 315 | if (prep->datalen < sizeof(*v1)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 316 | goto error; |
| 317 | |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 318 | v1 = prep->data; |
| 319 | if (prep->datalen != sizeof(*v1) + v1->ticket_length) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 320 | goto error; |
| 321 | |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 322 | _debug("SCIX: %u", v1->security_index); |
| 323 | _debug("TLEN: %u", v1->ticket_length); |
| 324 | _debug("EXPY: %x", v1->expiry); |
| 325 | _debug("KVNO: %u", v1->kvno); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 326 | _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x", |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 327 | v1->session_key[0], v1->session_key[1], |
| 328 | v1->session_key[2], v1->session_key[3], |
| 329 | v1->session_key[4], v1->session_key[5], |
| 330 | v1->session_key[6], v1->session_key[7]); |
| 331 | if (v1->ticket_length >= 8) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 332 | _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x", |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 333 | v1->ticket[0], v1->ticket[1], |
| 334 | v1->ticket[2], v1->ticket[3], |
| 335 | v1->ticket[4], v1->ticket[5], |
| 336 | v1->ticket[6], v1->ticket[7]); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 337 | |
| 338 | ret = -EPROTONOSUPPORT; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 339 | if (v1->security_index != RXRPC_SECURITY_RXKAD) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 340 | goto error; |
| 341 | |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 342 | plen = sizeof(*token->kad) + v1->ticket_length; |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 343 | prep->quotalen = plen + sizeof(*token); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 344 | |
| 345 | ret = -ENOMEM; |
Anton Blanchard | 0a93ea2 | 2011-02-25 15:33:17 +0000 | [diff] [blame] | 346 | token = kzalloc(sizeof(*token), GFP_KERNEL); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 347 | if (!token) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 348 | goto error; |
Anton Blanchard | 0a93ea2 | 2011-02-25 15:33:17 +0000 | [diff] [blame] | 349 | token->kad = kzalloc(plen, GFP_KERNEL); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 350 | if (!token->kad) |
| 351 | goto error_free; |
| 352 | |
| 353 | token->security_index = RXRPC_SECURITY_RXKAD; |
| 354 | token->kad->ticket_len = v1->ticket_length; |
| 355 | token->kad->expiry = v1->expiry; |
| 356 | token->kad->kvno = v1->kvno; |
| 357 | memcpy(&token->kad->session_key, &v1->session_key, 8); |
| 358 | memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 359 | |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 360 | /* count the number of tokens attached */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 361 | prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 362 | |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 363 | /* attach the data */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 364 | pp = (struct rxrpc_key_token **)&prep->payload.data[0]; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 365 | while (*pp) |
| 366 | pp = &(*pp)->next; |
| 367 | *pp = token; |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 368 | expiry = rxrpc_u32_to_time64(token->kad->expiry); |
| 369 | if (expiry < prep->expiry) |
| 370 | prep->expiry = expiry; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 371 | token = NULL; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 372 | ret = 0; |
| 373 | |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 374 | error_free: |
| 375 | kfree(token); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 376 | error: |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | /* |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 381 | * Free token list. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 382 | */ |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 383 | static void rxrpc_free_token_list(struct rxrpc_key_token *token) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 384 | { |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 385 | struct rxrpc_key_token *next; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 386 | |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 387 | for (; token; token = next) { |
| 388 | next = token->next; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 389 | switch (token->security_index) { |
| 390 | case RXRPC_SECURITY_RXKAD: |
| 391 | kfree(token->kad); |
| 392 | break; |
| 393 | default: |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 394 | pr_err("Unknown token type %x on rxrpc key\n", |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 395 | token->security_index); |
| 396 | BUG(); |
| 397 | } |
| 398 | |
| 399 | kfree(token); |
| 400 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 404 | * Clean up preparse data. |
| 405 | */ |
| 406 | static void rxrpc_free_preparse(struct key_preparsed_payload *prep) |
| 407 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 408 | rxrpc_free_token_list(prep->payload.data[0]); |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 412 | * dispose of the data dangling from the corpse of a rxrpc key |
| 413 | */ |
| 414 | static void rxrpc_destroy(struct key *key) |
| 415 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 416 | rxrpc_free_token_list(key->payload.data[0]); |
David Howells | 8a7a3eb | 2014-07-18 18:56:36 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 420 | * describe the rxrpc key |
| 421 | */ |
| 422 | static void rxrpc_describe(const struct key *key, struct seq_file *m) |
| 423 | { |
David Howells | 0727d3e | 2020-09-08 22:30:52 +0100 | [diff] [blame] | 424 | const struct rxrpc_key_token *token; |
| 425 | const char *sep = ": "; |
| 426 | |
| 427 | seq_puts(m, key->description); |
| 428 | |
| 429 | for (token = key->payload.data[0]; token; token = token->next) { |
| 430 | seq_puts(m, sep); |
| 431 | |
| 432 | switch (token->security_index) { |
| 433 | case RXRPC_SECURITY_RXKAD: |
| 434 | seq_puts(m, "ka"); |
| 435 | break; |
| 436 | default: /* we have a ticket we can't encode */ |
| 437 | seq_printf(m, "%u", token->security_index); |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | sep = " "; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 446 | * grab the security key for a socket |
| 447 | */ |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 448 | int rxrpc_request_key(struct rxrpc_sock *rx, sockptr_t optval, int optlen) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 449 | { |
| 450 | struct key *key; |
| 451 | char *description; |
| 452 | |
| 453 | _enter(""); |
| 454 | |
David Howells | 38b1dc4 | 2020-10-02 14:04:51 +0100 | [diff] [blame] | 455 | if (optlen <= 0 || optlen > PAGE_SIZE - 1 || rx->securities) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 456 | return -EINVAL; |
| 457 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 458 | description = memdup_sockptr_nul(optval, optlen); |
Al Viro | 16e5c1f | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 459 | if (IS_ERR(description)) |
| 460 | return PTR_ERR(description); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 461 | |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 462 | key = request_key_net(&key_type_rxrpc, description, sock_net(&rx->sk), NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 463 | if (IS_ERR(key)) { |
| 464 | kfree(description); |
| 465 | _leave(" = %ld", PTR_ERR(key)); |
| 466 | return PTR_ERR(key); |
| 467 | } |
| 468 | |
| 469 | rx->key = key; |
| 470 | kfree(description); |
| 471 | _leave(" = 0 [key %x]", key->serial); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 476 | * generate a server data key |
| 477 | */ |
| 478 | int rxrpc_get_server_data_key(struct rxrpc_connection *conn, |
| 479 | const void *session_key, |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 480 | time64_t expiry, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 481 | u32 kvno) |
| 482 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 483 | const struct cred *cred = current_cred(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 484 | struct key *key; |
| 485 | int ret; |
| 486 | |
| 487 | struct { |
| 488 | u32 kver; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 489 | struct rxrpc_key_data_v1 v1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 490 | } data; |
| 491 | |
| 492 | _enter(""); |
| 493 | |
Eric W. Biederman | c608973 | 2012-05-25 16:37:54 -0600 | [diff] [blame] | 494 | key = key_alloc(&key_type_rxrpc, "x", |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 495 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0, |
David Howells | 5ac7eac | 2016-04-06 16:14:24 +0100 | [diff] [blame] | 496 | KEY_ALLOC_NOT_IN_QUOTA, NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 497 | if (IS_ERR(key)) { |
| 498 | _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key)); |
| 499 | return -ENOMEM; |
| 500 | } |
| 501 | |
| 502 | _debug("key %d", key_serial(key)); |
| 503 | |
| 504 | data.kver = 1; |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 505 | data.v1.security_index = RXRPC_SECURITY_RXKAD; |
| 506 | data.v1.ticket_length = 0; |
Baolin Wang | 10674a0 | 2017-08-29 10:15:40 +0100 | [diff] [blame] | 507 | data.v1.expiry = rxrpc_time64_to_u32(expiry); |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 508 | data.v1.kvno = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 509 | |
David Howells | 3394128 | 2009-09-14 01:17:35 +0000 | [diff] [blame] | 510 | memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 511 | |
| 512 | ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL); |
| 513 | if (ret < 0) |
| 514 | goto error; |
| 515 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 516 | conn->params.key = key; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 517 | _leave(" = 0 [%d]", key_serial(key)); |
| 518 | return 0; |
| 519 | |
| 520 | error: |
| 521 | key_revoke(key); |
| 522 | key_put(key); |
| 523 | _leave(" = -ENOMEM [ins %d]", ret); |
| 524 | return -ENOMEM; |
| 525 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 526 | EXPORT_SYMBOL(rxrpc_get_server_data_key); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 527 | |
| 528 | /** |
| 529 | * rxrpc_get_null_key - Generate a null RxRPC key |
| 530 | * @keyname: The name to give the key. |
| 531 | * |
| 532 | * Generate a null RxRPC key that can be used to indicate anonymous security is |
| 533 | * required for a particular domain. |
| 534 | */ |
| 535 | struct key *rxrpc_get_null_key(const char *keyname) |
| 536 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 537 | const struct cred *cred = current_cred(); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 538 | struct key *key; |
| 539 | int ret; |
| 540 | |
Eric W. Biederman | c608973 | 2012-05-25 16:37:54 -0600 | [diff] [blame] | 541 | key = key_alloc(&key_type_rxrpc, keyname, |
| 542 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 543 | KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 544 | if (IS_ERR(key)) |
| 545 | return key; |
| 546 | |
| 547 | ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL); |
| 548 | if (ret < 0) { |
| 549 | key_revoke(key); |
| 550 | key_put(key); |
| 551 | return ERR_PTR(ret); |
| 552 | } |
| 553 | |
| 554 | return key; |
| 555 | } |
| 556 | EXPORT_SYMBOL(rxrpc_get_null_key); |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 557 | |
| 558 | /* |
| 559 | * read the contents of an rxrpc key |
| 560 | * - this returns the result in XDR form |
| 561 | */ |
| 562 | static long rxrpc_read(const struct key *key, |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 563 | char *buffer, size_t buflen) |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 564 | { |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 565 | const struct rxrpc_key_token *token; |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 566 | size_t size; |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 567 | __be32 *xdr, *oldxdr; |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 568 | u32 cnlen, toksize, ntoks, tok, zero; |
| 569 | u16 toksizes[AFSTOKEN_MAX]; |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 570 | |
| 571 | _enter(""); |
| 572 | |
| 573 | /* we don't know what form we should return non-AFS keys in */ |
| 574 | if (memcmp(key->description, "afs@", 4) != 0) |
| 575 | return -EOPNOTSUPP; |
| 576 | cnlen = strlen(key->description + 4); |
| 577 | |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 578 | #define RND(X) (((X) + 3) & ~3) |
| 579 | |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 580 | /* AFS keys we return in XDR form, so we need to work out the size of |
| 581 | * the XDR */ |
| 582 | size = 2 * 4; /* flags, cellname len */ |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 583 | size += RND(cnlen); /* cellname */ |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 584 | size += 1 * 4; /* token count */ |
| 585 | |
| 586 | ntoks = 0; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 587 | for (token = key->payload.data[0]; token; token = token->next) { |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 588 | toksize = 4; /* sec index */ |
| 589 | |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 590 | switch (token->security_index) { |
| 591 | case RXRPC_SECURITY_RXKAD: |
Marc Dionne | 5630511 | 2020-09-04 14:01:24 -0300 | [diff] [blame] | 592 | toksize += 8 * 4; /* viceid, kvno, key*2, begin, |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 593 | * end, primary, tktlen */ |
David Howells | d2ae4e9 | 2020-09-27 11:07:21 +0100 | [diff] [blame] | 594 | if (!token->no_leak_key) |
| 595 | toksize += RND(token->kad->ticket_len); |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 596 | break; |
| 597 | |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 598 | default: /* we have a ticket we can't encode */ |
David Howells | 9a059cd | 2020-09-08 22:09:04 +0100 | [diff] [blame] | 599 | pr_err("Unsupported key token type (%u)\n", |
| 600 | token->security_index); |
David Howells | d52e419 | 2021-01-12 15:23:51 +0000 | [diff] [blame] | 601 | return -ENOPKG; |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 602 | } |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 603 | |
| 604 | _debug("token[%u]: toksize=%u", ntoks, toksize); |
| 605 | ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX); |
| 606 | |
| 607 | toksizes[ntoks++] = toksize; |
| 608 | size += toksize + 4; /* each token has a length word */ |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 609 | } |
| 610 | |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 611 | #undef RND |
| 612 | |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 613 | if (!buffer || buflen < size) |
| 614 | return size; |
| 615 | |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 616 | xdr = (__be32 *)buffer; |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 617 | zero = 0; |
| 618 | #define ENCODE(x) \ |
| 619 | do { \ |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 620 | *xdr++ = htonl(x); \ |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 621 | } while(0) |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 622 | #define ENCODE_DATA(l, s) \ |
| 623 | do { \ |
| 624 | u32 _l = (l); \ |
| 625 | ENCODE(l); \ |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 626 | memcpy(xdr, (s), _l); \ |
| 627 | if (_l & 3) \ |
| 628 | memcpy((u8 *)xdr + _l, &zero, 4 - (_l & 3)); \ |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 629 | xdr += (_l + 3) >> 2; \ |
| 630 | } while(0) |
Marc Dionne | 5630511 | 2020-09-04 14:01:24 -0300 | [diff] [blame] | 631 | #define ENCODE_BYTES(l, s) \ |
| 632 | do { \ |
| 633 | u32 _l = (l); \ |
| 634 | memcpy(xdr, (s), _l); \ |
| 635 | if (_l & 3) \ |
| 636 | memcpy((u8 *)xdr + _l, &zero, 4 - (_l & 3)); \ |
| 637 | xdr += (_l + 3) >> 2; \ |
| 638 | } while(0) |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 639 | #define ENCODE64(x) \ |
| 640 | do { \ |
| 641 | __be64 y = cpu_to_be64(x); \ |
Waiman Long | d3ec10a | 2020-03-21 21:11:24 -0400 | [diff] [blame] | 642 | memcpy(xdr, &y, 8); \ |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 643 | xdr += 8 >> 2; \ |
| 644 | } while(0) |
| 645 | #define ENCODE_STR(s) \ |
| 646 | do { \ |
| 647 | const char *_s = (s); \ |
| 648 | ENCODE_DATA(strlen(_s), _s); \ |
| 649 | } while(0) |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 650 | |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 651 | ENCODE(0); /* flags */ |
| 652 | ENCODE_DATA(cnlen, key->description + 4); /* cellname */ |
| 653 | ENCODE(ntoks); |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 654 | |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 655 | tok = 0; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 656 | for (token = key->payload.data[0]; token; token = token->next) { |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 657 | toksize = toksizes[tok++]; |
| 658 | ENCODE(toksize); |
| 659 | oldxdr = xdr; |
| 660 | ENCODE(token->security_index); |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 661 | |
| 662 | switch (token->security_index) { |
| 663 | case RXRPC_SECURITY_RXKAD: |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 664 | ENCODE(token->kad->vice_id); |
| 665 | ENCODE(token->kad->kvno); |
Marc Dionne | 5630511 | 2020-09-04 14:01:24 -0300 | [diff] [blame] | 666 | ENCODE_BYTES(8, token->kad->session_key); |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 667 | ENCODE(token->kad->start); |
| 668 | ENCODE(token->kad->expiry); |
| 669 | ENCODE(token->kad->primary_flag); |
David Howells | d2ae4e9 | 2020-09-27 11:07:21 +0100 | [diff] [blame] | 670 | if (token->no_leak_key) |
| 671 | ENCODE(0); |
| 672 | else |
| 673 | ENCODE_DATA(token->kad->ticket_len, token->kad->ticket); |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 674 | break; |
| 675 | |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 676 | default: |
David Howells | d52e419 | 2021-01-12 15:23:51 +0000 | [diff] [blame] | 677 | pr_err("Unsupported key token type (%u)\n", |
| 678 | token->security_index); |
| 679 | return -ENOPKG; |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 680 | } |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 681 | |
| 682 | ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==, |
| 683 | toksize); |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 684 | } |
| 685 | |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 686 | #undef ENCODE_STR |
| 687 | #undef ENCODE_DATA |
| 688 | #undef ENCODE64 |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 689 | #undef ENCODE |
| 690 | |
David Howells | 99455153 | 2009-09-14 01:17:46 +0000 | [diff] [blame] | 691 | ASSERTCMP(tok, ==, ntoks); |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 692 | ASSERTCMP((char __user *) xdr - buffer, ==, size); |
| 693 | _leave(" = %zu", size); |
| 694 | return size; |
David Howells | ed6dd18 | 2009-09-14 01:17:40 +0000 | [diff] [blame] | 695 | } |