server_key.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* RxRPC key management
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. *
  7. * RxRPC keys should have a description of describing their purpose:
  8. * "[email protected]>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <crypto/skcipher.h>
  12. #include <linux/module.h>
  13. #include <linux/net.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/key-type.h>
  16. #include <linux/ctype.h>
  17. #include <linux/slab.h>
  18. #include <net/sock.h>
  19. #include <net/af_rxrpc.h>
  20. #include <keys/rxrpc-type.h>
  21. #include <keys/user-type.h>
  22. #include "ar-internal.h"
  23. static int rxrpc_vet_description_s(const char *);
  24. static int rxrpc_preparse_s(struct key_preparsed_payload *);
  25. static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
  26. static void rxrpc_destroy_s(struct key *);
  27. static void rxrpc_describe_s(const struct key *, struct seq_file *);
  28. /*
  29. * rxrpc server keys take "<serviceId>:<securityIndex>[:<sec-specific>]" as the
  30. * description and the key material as the payload.
  31. */
  32. struct key_type key_type_rxrpc_s = {
  33. .name = "rxrpc_s",
  34. .flags = KEY_TYPE_NET_DOMAIN,
  35. .vet_description = rxrpc_vet_description_s,
  36. .preparse = rxrpc_preparse_s,
  37. .free_preparse = rxrpc_free_preparse_s,
  38. .instantiate = generic_key_instantiate,
  39. .destroy = rxrpc_destroy_s,
  40. .describe = rxrpc_describe_s,
  41. };
  42. /*
  43. * Vet the description for an RxRPC server key.
  44. */
  45. static int rxrpc_vet_description_s(const char *desc)
  46. {
  47. unsigned long service, sec_class;
  48. char *p;
  49. service = simple_strtoul(desc, &p, 10);
  50. if (*p != ':' || service > 65535)
  51. return -EINVAL;
  52. sec_class = simple_strtoul(p + 1, &p, 10);
  53. if ((*p && *p != ':') || sec_class < 1 || sec_class > 255)
  54. return -EINVAL;
  55. return 0;
  56. }
  57. /*
  58. * Preparse a server secret key.
  59. */
  60. static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
  61. {
  62. const struct rxrpc_security *sec;
  63. unsigned int service, sec_class;
  64. int n;
  65. _enter("%zu", prep->datalen);
  66. if (!prep->orig_description)
  67. return -EINVAL;
  68. if (sscanf(prep->orig_description, "%u:%u%n", &service, &sec_class, &n) != 2)
  69. return -EINVAL;
  70. sec = rxrpc_security_lookup(sec_class);
  71. if (!sec)
  72. return -ENOPKG;
  73. prep->payload.data[1] = (struct rxrpc_security *)sec;
  74. if (!sec->preparse_server_key)
  75. return -EINVAL;
  76. return sec->preparse_server_key(prep);
  77. }
  78. static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
  79. {
  80. const struct rxrpc_security *sec = prep->payload.data[1];
  81. if (sec && sec->free_preparse_server_key)
  82. sec->free_preparse_server_key(prep);
  83. }
  84. static void rxrpc_destroy_s(struct key *key)
  85. {
  86. const struct rxrpc_security *sec = key->payload.data[1];
  87. if (sec && sec->destroy_server_key)
  88. sec->destroy_server_key(key);
  89. }
  90. static void rxrpc_describe_s(const struct key *key, struct seq_file *m)
  91. {
  92. const struct rxrpc_security *sec = key->payload.data[1];
  93. seq_puts(m, key->description);
  94. if (sec && sec->describe_server_key)
  95. sec->describe_server_key(key, m);
  96. }
  97. /*
  98. * grab the security keyring for a server socket
  99. */
  100. int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
  101. {
  102. struct key *key;
  103. char *description;
  104. _enter("");
  105. if (optlen <= 0 || optlen > PAGE_SIZE - 1)
  106. return -EINVAL;
  107. description = memdup_sockptr_nul(optval, optlen);
  108. if (IS_ERR(description))
  109. return PTR_ERR(description);
  110. key = request_key(&key_type_keyring, description, NULL);
  111. if (IS_ERR(key)) {
  112. kfree(description);
  113. _leave(" = %ld", PTR_ERR(key));
  114. return PTR_ERR(key);
  115. }
  116. rx->securities = key;
  117. kfree(description);
  118. _leave(" = 0 [key %x]", key->serial);
  119. return 0;
  120. }