| 1 | |
| 2 | * algif_rng: User-space interface for random number generators
|
| 3 | *
|
| 4 | * This file provides the user-space API for random number generators.
|
| 5 | *
|
| 6 | * Copyright (C) 2014, Stephan Mueller <[email protected]>
|
| 7 | *
|
| 8 | * Redistribution and use in source and binary forms, with or without
|
| 9 | * modification, are permitted provided that the following conditions
|
| 10 | * are met:
|
| 11 | * 1. Redistributions of source code must retain the above copyright
|
| 12 | * notice, and the entire permission notice in its entirety,
|
| 13 | * including the disclaimer of warranties.
|
| 14 | * 2. Redistributions in binary form must reproduce the above copyright
|
| 15 | * notice, this list of conditions and the following disclaimer in the
|
| 16 | * documentation and/or other materials provided with the distribution.
|
| 17 | * 3. The name of the author may not be used to endorse or promote
|
| 18 | * products derived from this software without specific prior
|
| 19 | * written permission.
|
| 20 | *
|
| 21 | * ALTERNATIVELY, this product may be distributed under the terms of
|
| 22 | * the GNU General Public License, in which case the provisions of the GPL2
|
| 23 | * are required INSTEAD OF the above restrictions. (This clause is
|
| 24 | * necessary due to a potential bad interaction between the GPL and
|
| 25 | * the restrictions contained in a BSD-style copyright.)
|
| 26 | *
|
| 27 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
| 28 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
| 29 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
|
| 30 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
| 31 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 32 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
| 33 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
| 34 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
| 35 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
| 37 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
|
| 38 | * DAMAGE.
|
| 39 |
|
| 40 |
|
| 41 | #include <linux/capability.h>
|
| 42 | #include <linux/module.h>
|
| 43 | #include <crypto/rng.h>
|
| 44 | #include <linux/random.h>
|
| 45 | #include <crypto/if_alg.h>
|
| 46 | #include <linux/net.h>
|
| 47 | #include <net/sock.h>
|
| 48 |
|
| 49 | MODULE_LICENSE("GPL");
|
| 50 | MODULE_AUTHOR("Stephan Mueller <[email protected]>");
|
| 51 | MODULE_DESCRIPTION("User-space interface for random number generators");
|
| 52 |
|
| 53 | static const struct af_alg_allowlist_entry rng_allowlist[] = {
|
| 54 | {},
|
| 55 | };
|
| 56 |
|
| 57 | struct rng_ctx {
|
| 58 | #define MAXSIZE 128
|
| 59 | unsigned int len;
|
| 60 | struct crypto_rng *drng;
|
| 61 | u8 *addtl;
|
| 62 | size_t addtl_len;
|
| 63 | };
|
| 64 |
|
| 65 | struct rng_parent_ctx {
|
| 66 | struct crypto_rng *drng;
|
| 67 | u8 *entropy;
|
| 68 | };
|
| 69 |
|
| 70 | static void rng_reset_addtl(struct rng_ctx *ctx)
|
| 71 | {
|
| 72 | kfree_sensitive(ctx->addtl);
|
| 73 | ctx->addtl = NULL;
|
| 74 | ctx->addtl_len = 0;
|
| 75 | }
|
| 76 |
|
| 77 | static int _rng_recvmsg(struct crypto_rng *drng, struct msghdr *msg, size_t len,
|
| 78 | u8 *addtl, size_t addtl_len)
|
| 79 | {
|
| 80 | int err = 0;
|
| 81 | int genlen = 0;
|
| 82 | u8 result[MAXSIZE];
|
| 83 |
|
| 84 | if (len == 0)
|
| 85 | return 0;
|
| 86 | if (len > MAXSIZE)
|
| 87 | len = MAXSIZE;
|
| 88 |
|
| 89 | |
| 90 | * although not strictly needed, this is a precaution against coding
|
| 91 | * errors
|
| 92 |
|
| 93 | memset(result, 0, len);
|
| 94 |
|
| 95 | |
| 96 | * The enforcement of a proper seeding of an RNG is done within an
|
| 97 | * RNG implementation. Some RNGs (DRBG, krng) do not need specific
|
| 98 | * seeding as they automatically seed. The X9.31 DRNG will return
|
| 99 | * an error if it was not seeded properly.
|
| 100 |
|
| 101 | genlen = crypto_rng_generate(drng, addtl, addtl_len, result, len);
|
| 102 | if (genlen < 0)
|
| 103 | return genlen;
|
| 104 |
|
| 105 | err = memcpy_to_msg(msg, result, len);
|
| 106 | memzero_explicit(result, len);
|
| 107 |
|
| 108 | return err ? err : len;
|
| 109 | }
|
| 110 |
|
| 111 | static int rng_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
|
| 112 | int flags)
|
| 113 | {
|
| 114 | struct sock *sk = sock->sk;
|
| 115 | struct alg_sock *ask = alg_sk(sk);
|
| 116 | struct rng_ctx *ctx = ask->private;
|
| 117 |
|
| 118 | return _rng_recvmsg(ctx->drng, msg, len, NULL, 0);
|
| 119 | }
|
| 120 |
|
| 121 | static int rng_test_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
|
| 122 | int flags)
|
| 123 | {
|
| 124 | struct sock *sk = sock->sk;
|
| 125 | struct alg_sock *ask = alg_sk(sk);
|
| 126 | struct rng_ctx *ctx = ask->private;
|
| 127 | int ret;
|
| 128 |
|
| 129 | lock_sock(sock->sk);
|
| 130 | ret = _rng_recvmsg(ctx->drng, msg, len, ctx->addtl, ctx->addtl_len);
|
| 131 | rng_reset_addtl(ctx);
|
| 132 | release_sock(sock->sk);
|
| 133 |
|
| 134 | return ret;
|
| 135 | }
|
| 136 |
|
| 137 | static int rng_test_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
|
| 138 | {
|
| 139 | int err;
|
| 140 | struct alg_sock *ask = alg_sk(sock->sk);
|
| 141 | struct rng_ctx *ctx = ask->private;
|
| 142 |
|
| 143 | lock_sock(sock->sk);
|
| 144 | if (len > MAXSIZE) {
|
| 145 | err = -EMSGSIZE;
|
| 146 | goto unlock;
|
| 147 | }
|
| 148 |
|
| 149 | rng_reset_addtl(ctx);
|
| 150 | ctx->addtl = kmalloc(len, GFP_KERNEL);
|
| 151 | if (!ctx->addtl) {
|
| 152 | err = -ENOMEM;
|
| 153 | goto unlock;
|
| 154 | }
|
| 155 |
|
| 156 | err = memcpy_from_msg(ctx->addtl, msg, len);
|
| 157 | if (err) {
|
| 158 | rng_reset_addtl(ctx);
|
| 159 | goto unlock;
|
| 160 | }
|
| 161 | ctx->addtl_len = len;
|
| 162 |
|
| 163 | unlock:
|
| 164 | release_sock(sock->sk);
|
| 165 | return err ? err : len;
|
| 166 | }
|
| 167 |
|
| 168 | static struct proto_ops algif_rng_ops = {
|
| 169 | .family = PF_ALG,
|
| 170 |
|
| 171 | .connect = sock_no_connect,
|
| 172 | .socketpair = sock_no_socketpair,
|
| 173 | .getname = sock_no_getname,
|
| 174 | .ioctl = sock_no_ioctl,
|
| 175 | .listen = sock_no_listen,
|
| 176 | .shutdown = sock_no_shutdown,
|
| 177 | .mmap = sock_no_mmap,
|
| 178 | .bind = sock_no_bind,
|
| 179 | .accept = sock_no_accept,
|
| 180 | .sendmsg = sock_no_sendmsg,
|
| 181 |
|
| 182 | .release = af_alg_release,
|
| 183 | .recvmsg = rng_recvmsg,
|
| 184 | };
|
| 185 |
|
| 186 | static struct proto_ops __maybe_unused algif_rng_test_ops = {
|
| 187 | .family = PF_ALG,
|
| 188 |
|
| 189 | .connect = sock_no_connect,
|
| 190 | .socketpair = sock_no_socketpair,
|
| 191 | .getname = sock_no_getname,
|
| 192 | .ioctl = sock_no_ioctl,
|
| 193 | .listen = sock_no_listen,
|
| 194 | .shutdown = sock_no_shutdown,
|
| 195 | .mmap = sock_no_mmap,
|
| 196 | .bind = sock_no_bind,
|
| 197 | .accept = sock_no_accept,
|
| 198 |
|
| 199 | .release = af_alg_release,
|
| 200 | .recvmsg = rng_test_recvmsg,
|
| 201 | .sendmsg = rng_test_sendmsg,
|
| 202 | };
|
| 203 |
|
| 204 | static void *rng_bind(const char *name)
|
| 205 | {
|
| 206 | struct rng_parent_ctx *pctx;
|
| 207 | struct crypto_rng *rng;
|
| 208 | int err;
|
| 209 |
|
| 210 | err = af_alg_check_restriction(name, rng_allowlist);
|
| 211 | if (err)
|
| 212 | return ERR_PTR(err);
|
| 213 |
|
| 214 | pctx = kzalloc_obj(*pctx);
|
| 215 | if (!pctx)
|
| 216 | return ERR_PTR(-ENOMEM);
|
| 217 |
|
| 218 | rng = crypto_alloc_rng(name, 0, AF_ALG_CRYPTOAPI_MASK);
|
| 219 | if (IS_ERR(rng)) {
|
| 220 | kfree(pctx);
|
| 221 | return ERR_CAST(rng);
|
| 222 | }
|
| 223 |
|
| 224 | pctx->drng = rng;
|
| 225 | return pctx;
|
| 226 | }
|
| 227 |
|
| 228 | static void rng_release(void *private)
|
| 229 | {
|
| 230 | struct rng_parent_ctx *pctx = private;
|
| 231 |
|
| 232 | if (unlikely(!pctx))
|
| 233 | return;
|
| 234 | crypto_free_rng(pctx->drng);
|
| 235 | kfree_sensitive(pctx->entropy);
|
| 236 | kfree_sensitive(pctx);
|
| 237 | }
|
| 238 |
|
| 239 | static void rng_sock_destruct(struct sock *sk)
|
| 240 | {
|
| 241 | struct alg_sock *ask = alg_sk(sk);
|
| 242 | struct rng_ctx *ctx = ask->private;
|
| 243 |
|
| 244 | rng_reset_addtl(ctx);
|
| 245 | sock_kfree_s(sk, ctx, ctx->len);
|
| 246 | af_alg_release_parent(sk);
|
| 247 | }
|
| 248 |
|
| 249 | static int rng_accept_parent(void *private, struct sock *sk)
|
| 250 | {
|
| 251 | struct rng_ctx *ctx;
|
| 252 | struct rng_parent_ctx *pctx = private;
|
| 253 | struct alg_sock *ask = alg_sk(sk);
|
| 254 | unsigned int len = sizeof(*ctx);
|
| 255 |
|
| 256 | ctx = sock_kmalloc(sk, len, GFP_KERNEL);
|
| 257 | if (!ctx)
|
| 258 | return -ENOMEM;
|
| 259 |
|
| 260 | memset(ctx, 0, len);
|
| 261 | ctx->len = len;
|
| 262 |
|
| 263 | |
| 264 | * No seeding done at that point -- if multiple accepts are
|
| 265 | * done on one RNG instance, each resulting FD points to the same
|
| 266 | * state of the RNG.
|
| 267 |
|
| 268 |
|
| 269 | ctx->drng = pctx->drng;
|
| 270 | ask->private = ctx;
|
| 271 | sk->sk_destruct = rng_sock_destruct;
|
| 272 |
|
| 273 | |
| 274 | * Non NULL pctx->entropy means that CAVP test has been initiated on
|
| 275 | * this socket, replace proto_ops algif_rng_ops with algif_rng_test_ops.
|
| 276 |
|
| 277 | if (IS_ENABLED(CONFIG_CRYPTO_USER_API_RNG_CAVP) && pctx->entropy)
|
| 278 | sk->sk_socket->ops = &algif_rng_test_ops;
|
| 279 |
|
| 280 | return 0;
|
| 281 | }
|
| 282 |
|
| 283 | static int rng_setkey(void *private, const u8 *seed, unsigned int seedlen)
|
| 284 | {
|
| 285 | struct rng_parent_ctx *pctx = private;
|
| 286 | |
| 287 | * Check whether seedlen is of sufficient size is done in RNG
|
| 288 | * implementations.
|
| 289 |
|
| 290 | return crypto_rng_reset(pctx->drng, seed, seedlen);
|
| 291 | }
|
| 292 |
|
| 293 | static int __maybe_unused rng_setentropy(void *private, sockptr_t entropy,
|
| 294 | unsigned int len)
|
| 295 | {
|
| 296 | struct rng_parent_ctx *pctx = private;
|
| 297 | u8 *kentropy = NULL;
|
| 298 |
|
| 299 | if (!capable(CAP_SYS_ADMIN))
|
| 300 | return -EACCES;
|
| 301 |
|
| 302 | if (pctx->entropy)
|
| 303 | return -EINVAL;
|
| 304 |
|
| 305 | if (len > MAXSIZE)
|
| 306 | return -EMSGSIZE;
|
| 307 |
|
| 308 | if (len) {
|
| 309 | kentropy = memdup_sockptr(entropy, len);
|
| 310 | if (IS_ERR(kentropy))
|
| 311 | return PTR_ERR(kentropy);
|
| 312 | }
|
| 313 |
|
| 314 | crypto_rng_alg(pctx->drng)->set_ent(pctx->drng, kentropy, len);
|
| 315 | |
| 316 | * Since rng doesn't perform any memory management for the entropy
|
| 317 | * buffer, save kentropy pointer to pctx now to free it after use.
|
| 318 |
|
| 319 | pctx->entropy = kentropy;
|
| 320 | return 0;
|
| 321 | }
|
| 322 |
|
| 323 | static const struct af_alg_type algif_type_rng = {
|
| 324 | .bind = rng_bind,
|
| 325 | .release = rng_release,
|
| 326 | .accept = rng_accept_parent,
|
| 327 | .setkey = rng_setkey,
|
| 328 | #ifdef CONFIG_CRYPTO_USER_API_RNG_CAVP
|
| 329 | .setentropy = rng_setentropy,
|
| 330 | #endif
|
| 331 | .ops = &algif_rng_ops,
|
| 332 | .name = "rng",
|
| 333 | .owner = THIS_MODULE
|
| 334 | };
|
| 335 |
|
| 336 | static int __init rng_init(void)
|
| 337 | {
|
| 338 | return af_alg_register_type(&algif_type_rng);
|
| 339 | }
|
| 340 |
|
| 341 | static void __exit rng_exit(void)
|
| 342 | {
|
| 343 | int err = af_alg_unregister_type(&algif_type_rng);
|
| 344 | BUG_ON(err);
|
| 345 | }
|
| 346 |
|
| 347 | module_init(rng_init);
|
| 348 | module_exit(rng_exit);
|