Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* RxRPC key management
0003  *
0004  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  *
0007  * RxRPC keys should have a description of describing their purpose:
0008  *  "afs@CAMBRIDGE.REDHAT.COM>
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <crypto/skcipher.h>
0014 #include <linux/module.h>
0015 #include <linux/net.h>
0016 #include <linux/skbuff.h>
0017 #include <linux/key-type.h>
0018 #include <linux/ctype.h>
0019 #include <linux/slab.h>
0020 #include <net/sock.h>
0021 #include <net/af_rxrpc.h>
0022 #include <keys/rxrpc-type.h>
0023 #include <keys/user-type.h>
0024 #include "ar-internal.h"
0025 
0026 static int rxrpc_vet_description_s(const char *);
0027 static int rxrpc_preparse_s(struct key_preparsed_payload *);
0028 static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
0029 static void rxrpc_destroy_s(struct key *);
0030 static void rxrpc_describe_s(const struct key *, struct seq_file *);
0031 
0032 /*
0033  * rxrpc server keys take "<serviceId>:<securityIndex>[:<sec-specific>]" as the
0034  * description and the key material as the payload.
0035  */
0036 struct key_type key_type_rxrpc_s = {
0037     .name       = "rxrpc_s",
0038     .flags      = KEY_TYPE_NET_DOMAIN,
0039     .vet_description = rxrpc_vet_description_s,
0040     .preparse   = rxrpc_preparse_s,
0041     .free_preparse  = rxrpc_free_preparse_s,
0042     .instantiate    = generic_key_instantiate,
0043     .destroy    = rxrpc_destroy_s,
0044     .describe   = rxrpc_describe_s,
0045 };
0046 
0047 /*
0048  * Vet the description for an RxRPC server key.
0049  */
0050 static int rxrpc_vet_description_s(const char *desc)
0051 {
0052     unsigned long service, sec_class;
0053     char *p;
0054 
0055     service = simple_strtoul(desc, &p, 10);
0056     if (*p != ':' || service > 65535)
0057         return -EINVAL;
0058     sec_class = simple_strtoul(p + 1, &p, 10);
0059     if ((*p && *p != ':') || sec_class < 1 || sec_class > 255)
0060         return -EINVAL;
0061     return 0;
0062 }
0063 
0064 /*
0065  * Preparse a server secret key.
0066  */
0067 static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
0068 {
0069     const struct rxrpc_security *sec;
0070     unsigned int service, sec_class;
0071     int n;
0072 
0073     _enter("%zu", prep->datalen);
0074 
0075     if (!prep->orig_description)
0076         return -EINVAL;
0077 
0078     if (sscanf(prep->orig_description, "%u:%u%n", &service, &sec_class, &n) != 2)
0079         return -EINVAL;
0080 
0081     sec = rxrpc_security_lookup(sec_class);
0082     if (!sec)
0083         return -ENOPKG;
0084 
0085     prep->payload.data[1] = (struct rxrpc_security *)sec;
0086 
0087     if (!sec->preparse_server_key)
0088         return -EINVAL;
0089 
0090     return sec->preparse_server_key(prep);
0091 }
0092 
0093 static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
0094 {
0095     const struct rxrpc_security *sec = prep->payload.data[1];
0096 
0097     if (sec && sec->free_preparse_server_key)
0098         sec->free_preparse_server_key(prep);
0099 }
0100 
0101 static void rxrpc_destroy_s(struct key *key)
0102 {
0103     const struct rxrpc_security *sec = key->payload.data[1];
0104 
0105     if (sec && sec->destroy_server_key)
0106         sec->destroy_server_key(key);
0107 }
0108 
0109 static void rxrpc_describe_s(const struct key *key, struct seq_file *m)
0110 {
0111     const struct rxrpc_security *sec = key->payload.data[1];
0112 
0113     seq_puts(m, key->description);
0114     if (sec && sec->describe_server_key)
0115         sec->describe_server_key(key, m);
0116 }
0117 
0118 /*
0119  * grab the security keyring for a server socket
0120  */
0121 int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
0122 {
0123     struct key *key;
0124     char *description;
0125 
0126     _enter("");
0127 
0128     if (optlen <= 0 || optlen > PAGE_SIZE - 1)
0129         return -EINVAL;
0130 
0131     description = memdup_sockptr_nul(optval, optlen);
0132     if (IS_ERR(description))
0133         return PTR_ERR(description);
0134 
0135     key = request_key(&key_type_keyring, description, NULL);
0136     if (IS_ERR(key)) {
0137         kfree(description);
0138         _leave(" = %ld", PTR_ERR(key));
0139         return PTR_ERR(key);
0140     }
0141 
0142     rx->securities = key;
0143     kfree(description);
0144     _leave(" = 0 [key %x]", key->serial);
0145     return 0;
0146 }