crypto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2024 Meta, Inc */
  3. #include <linux/bpf.h>
  4. #include <linux/bpf_crypto.h>
  5. #include <linux/bpf_mem_alloc.h>
  6. #include <linux/btf.h>
  7. #include <linux/btf_ids.h>
  8. #include <linux/filter.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/skbuff.h>
  11. #include <crypto/skcipher.h>
  12. struct bpf_crypto_type_list {
  13. const struct bpf_crypto_type *type;
  14. struct list_head list;
  15. };
  16. /* BPF crypto initialization parameters struct */
  17. /**
  18. * struct bpf_crypto_params - BPF crypto initialization parameters structure
  19. * @type: The string of crypto operation type.
  20. * @reserved: Reserved member, will be reused for more options in future
  21. * Values:
  22. * 0
  23. * @algo: The string of algorithm to initialize.
  24. * @key: The cipher key used to init crypto algorithm.
  25. * @key_len: The length of cipher key.
  26. * @authsize: The length of authentication tag used by algorithm.
  27. */
  28. struct bpf_crypto_params {
  29. char type[14];
  30. u8 reserved[2];
  31. char algo[128];
  32. u8 key[256];
  33. u32 key_len;
  34. u32 authsize;
  35. };
  36. static LIST_HEAD(bpf_crypto_types);
  37. static DECLARE_RWSEM(bpf_crypto_types_sem);
  38. /**
  39. * struct bpf_crypto_ctx - refcounted BPF crypto context structure
  40. * @type: The pointer to bpf crypto type
  41. * @tfm: The pointer to instance of crypto API struct.
  42. * @siv_len: Size of IV and state storage for cipher
  43. * @rcu: The RCU head used to free the crypto context with RCU safety.
  44. * @usage: Object reference counter. When the refcount goes to 0, the
  45. * memory is released back to the BPF allocator, which provides
  46. * RCU safety.
  47. */
  48. struct bpf_crypto_ctx {
  49. const struct bpf_crypto_type *type;
  50. void *tfm;
  51. u32 siv_len;
  52. struct rcu_head rcu;
  53. refcount_t usage;
  54. };
  55. int bpf_crypto_register_type(const struct bpf_crypto_type *type)
  56. {
  57. struct bpf_crypto_type_list *node;
  58. int err = -EEXIST;
  59. down_write(&bpf_crypto_types_sem);
  60. list_for_each_entry(node, &bpf_crypto_types, list) {
  61. if (!strcmp(node->type->name, type->name))
  62. goto unlock;
  63. }
  64. node = kmalloc(sizeof(*node), GFP_KERNEL);
  65. err = -ENOMEM;
  66. if (!node)
  67. goto unlock;
  68. node->type = type;
  69. list_add(&node->list, &bpf_crypto_types);
  70. err = 0;
  71. unlock:
  72. up_write(&bpf_crypto_types_sem);
  73. return err;
  74. }
  75. EXPORT_SYMBOL_GPL(bpf_crypto_register_type);
  76. int bpf_crypto_unregister_type(const struct bpf_crypto_type *type)
  77. {
  78. struct bpf_crypto_type_list *node;
  79. int err = -ENOENT;
  80. down_write(&bpf_crypto_types_sem);
  81. list_for_each_entry(node, &bpf_crypto_types, list) {
  82. if (strcmp(node->type->name, type->name))
  83. continue;
  84. list_del(&node->list);
  85. kfree(node);
  86. err = 0;
  87. break;
  88. }
  89. up_write(&bpf_crypto_types_sem);
  90. return err;
  91. }
  92. EXPORT_SYMBOL_GPL(bpf_crypto_unregister_type);
  93. static const struct bpf_crypto_type *bpf_crypto_get_type(const char *name)
  94. {
  95. const struct bpf_crypto_type *type = ERR_PTR(-ENOENT);
  96. struct bpf_crypto_type_list *node;
  97. down_read(&bpf_crypto_types_sem);
  98. list_for_each_entry(node, &bpf_crypto_types, list) {
  99. if (strcmp(node->type->name, name))
  100. continue;
  101. if (try_module_get(node->type->owner))
  102. type = node->type;
  103. break;
  104. }
  105. up_read(&bpf_crypto_types_sem);
  106. return type;
  107. }
  108. __bpf_kfunc_start_defs();
  109. /**
  110. * bpf_crypto_ctx_create() - Create a mutable BPF crypto context.
  111. *
  112. * Allocates a crypto context that can be used, acquired, and released by
  113. * a BPF program. The crypto context returned by this function must either
  114. * be embedded in a map as a kptr, or freed with bpf_crypto_ctx_release().
  115. * As crypto API functions use GFP_KERNEL allocations, this function can
  116. * only be used in sleepable BPF programs.
  117. *
  118. * bpf_crypto_ctx_create() allocates memory for crypto context.
  119. * It may return NULL if no memory is available.
  120. * @params: pointer to struct bpf_crypto_params which contains all the
  121. * details needed to initialise crypto context.
  122. * @params__sz: size of steuct bpf_crypto_params usef by bpf program
  123. * @err: integer to store error code when NULL is returned.
  124. */
  125. __bpf_kfunc struct bpf_crypto_ctx *
  126. bpf_crypto_ctx_create(const struct bpf_crypto_params *params, u32 params__sz,
  127. int *err)
  128. {
  129. const struct bpf_crypto_type *type;
  130. struct bpf_crypto_ctx *ctx;
  131. if (!params || params->reserved[0] || params->reserved[1] ||
  132. params__sz != sizeof(struct bpf_crypto_params)) {
  133. *err = -EINVAL;
  134. return NULL;
  135. }
  136. type = bpf_crypto_get_type(params->type);
  137. if (IS_ERR(type)) {
  138. *err = PTR_ERR(type);
  139. return NULL;
  140. }
  141. if (!type->has_algo(params->algo)) {
  142. *err = -EOPNOTSUPP;
  143. goto err_module_put;
  144. }
  145. if (!!params->authsize ^ !!type->setauthsize) {
  146. *err = -EOPNOTSUPP;
  147. goto err_module_put;
  148. }
  149. if (!params->key_len || params->key_len > sizeof(params->key)) {
  150. *err = -EINVAL;
  151. goto err_module_put;
  152. }
  153. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  154. if (!ctx) {
  155. *err = -ENOMEM;
  156. goto err_module_put;
  157. }
  158. ctx->type = type;
  159. ctx->tfm = type->alloc_tfm(params->algo);
  160. if (IS_ERR(ctx->tfm)) {
  161. *err = PTR_ERR(ctx->tfm);
  162. goto err_free_ctx;
  163. }
  164. if (params->authsize) {
  165. *err = type->setauthsize(ctx->tfm, params->authsize);
  166. if (*err)
  167. goto err_free_tfm;
  168. }
  169. *err = type->setkey(ctx->tfm, params->key, params->key_len);
  170. if (*err)
  171. goto err_free_tfm;
  172. if (type->get_flags(ctx->tfm) & CRYPTO_TFM_NEED_KEY) {
  173. *err = -EINVAL;
  174. goto err_free_tfm;
  175. }
  176. ctx->siv_len = type->ivsize(ctx->tfm) + type->statesize(ctx->tfm);
  177. refcount_set(&ctx->usage, 1);
  178. return ctx;
  179. err_free_tfm:
  180. type->free_tfm(ctx->tfm);
  181. err_free_ctx:
  182. kfree(ctx);
  183. err_module_put:
  184. module_put(type->owner);
  185. return NULL;
  186. }
  187. static void crypto_free_cb(struct rcu_head *head)
  188. {
  189. struct bpf_crypto_ctx *ctx;
  190. ctx = container_of(head, struct bpf_crypto_ctx, rcu);
  191. ctx->type->free_tfm(ctx->tfm);
  192. module_put(ctx->type->owner);
  193. kfree(ctx);
  194. }
  195. /**
  196. * bpf_crypto_ctx_acquire() - Acquire a reference to a BPF crypto context.
  197. * @ctx: The BPF crypto context being acquired. The ctx must be a trusted
  198. * pointer.
  199. *
  200. * Acquires a reference to a BPF crypto context. The context returned by this function
  201. * must either be embedded in a map as a kptr, or freed with
  202. * bpf_crypto_ctx_release().
  203. */
  204. __bpf_kfunc struct bpf_crypto_ctx *
  205. bpf_crypto_ctx_acquire(struct bpf_crypto_ctx *ctx)
  206. {
  207. if (!refcount_inc_not_zero(&ctx->usage))
  208. return NULL;
  209. return ctx;
  210. }
  211. /**
  212. * bpf_crypto_ctx_release() - Release a previously acquired BPF crypto context.
  213. * @ctx: The crypto context being released.
  214. *
  215. * Releases a previously acquired reference to a BPF crypto context. When the final
  216. * reference of the BPF crypto context has been released, its memory
  217. * will be released.
  218. */
  219. __bpf_kfunc void bpf_crypto_ctx_release(struct bpf_crypto_ctx *ctx)
  220. {
  221. if (refcount_dec_and_test(&ctx->usage))
  222. call_rcu(&ctx->rcu, crypto_free_cb);
  223. }
  224. static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,
  225. const struct bpf_dynptr_kern *src,
  226. const struct bpf_dynptr_kern *dst,
  227. const struct bpf_dynptr_kern *siv,
  228. bool decrypt)
  229. {
  230. u32 src_len, dst_len, siv_len;
  231. const u8 *psrc;
  232. u8 *pdst, *piv;
  233. int err;
  234. if (__bpf_dynptr_is_rdonly(dst))
  235. return -EINVAL;
  236. siv_len = siv ? __bpf_dynptr_size(siv) : 0;
  237. src_len = __bpf_dynptr_size(src);
  238. dst_len = __bpf_dynptr_size(dst);
  239. if (!src_len || !dst_len || src_len > dst_len)
  240. return -EINVAL;
  241. if (siv_len != ctx->siv_len)
  242. return -EINVAL;
  243. psrc = __bpf_dynptr_data(src, src_len);
  244. if (!psrc)
  245. return -EINVAL;
  246. pdst = __bpf_dynptr_data_rw(dst, dst_len);
  247. if (!pdst)
  248. return -EINVAL;
  249. piv = siv_len ? __bpf_dynptr_data_rw(siv, siv_len) : NULL;
  250. if (siv_len && !piv)
  251. return -EINVAL;
  252. err = decrypt ? ctx->type->decrypt(ctx->tfm, psrc, pdst, src_len, piv)
  253. : ctx->type->encrypt(ctx->tfm, psrc, pdst, src_len, piv);
  254. return err;
  255. }
  256. /**
  257. * bpf_crypto_decrypt() - Decrypt buffer using configured context and IV provided.
  258. * @ctx: The crypto context being used. The ctx must be a trusted pointer.
  259. * @src: bpf_dynptr to the encrypted data. Must be a trusted pointer.
  260. * @dst: bpf_dynptr to the buffer where to store the result. Must be a trusted pointer.
  261. * @siv__nullable: bpf_dynptr to IV data and state data to be used by decryptor. May be NULL.
  262. *
  263. * Decrypts provided buffer using IV data and the crypto context. Crypto context must be configured.
  264. */
  265. __bpf_kfunc int bpf_crypto_decrypt(struct bpf_crypto_ctx *ctx,
  266. const struct bpf_dynptr *src,
  267. const struct bpf_dynptr *dst,
  268. const struct bpf_dynptr *siv__nullable)
  269. {
  270. const struct bpf_dynptr_kern *src_kern = (struct bpf_dynptr_kern *)src;
  271. const struct bpf_dynptr_kern *dst_kern = (struct bpf_dynptr_kern *)dst;
  272. const struct bpf_dynptr_kern *siv_kern = (struct bpf_dynptr_kern *)siv__nullable;
  273. return bpf_crypto_crypt(ctx, src_kern, dst_kern, siv_kern, true);
  274. }
  275. /**
  276. * bpf_crypto_encrypt() - Encrypt buffer using configured context and IV provided.
  277. * @ctx: The crypto context being used. The ctx must be a trusted pointer.
  278. * @src: bpf_dynptr to the plain data. Must be a trusted pointer.
  279. * @dst: bpf_dynptr to the buffer where to store the result. Must be a trusted pointer.
  280. * @siv__nullable: bpf_dynptr to IV data and state data to be used by decryptor. May be NULL.
  281. *
  282. * Encrypts provided buffer using IV data and the crypto context. Crypto context must be configured.
  283. */
  284. __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx,
  285. const struct bpf_dynptr *src,
  286. const struct bpf_dynptr *dst,
  287. const struct bpf_dynptr *siv__nullable)
  288. {
  289. const struct bpf_dynptr_kern *src_kern = (struct bpf_dynptr_kern *)src;
  290. const struct bpf_dynptr_kern *dst_kern = (struct bpf_dynptr_kern *)dst;
  291. const struct bpf_dynptr_kern *siv_kern = (struct bpf_dynptr_kern *)siv__nullable;
  292. return bpf_crypto_crypt(ctx, src_kern, dst_kern, siv_kern, false);
  293. }
  294. __bpf_kfunc_end_defs();
  295. BTF_KFUNCS_START(crypt_init_kfunc_btf_ids)
  296. BTF_ID_FLAGS(func, bpf_crypto_ctx_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
  297. BTF_ID_FLAGS(func, bpf_crypto_ctx_release, KF_RELEASE)
  298. BTF_ID_FLAGS(func, bpf_crypto_ctx_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
  299. BTF_KFUNCS_END(crypt_init_kfunc_btf_ids)
  300. static const struct btf_kfunc_id_set crypt_init_kfunc_set = {
  301. .owner = THIS_MODULE,
  302. .set = &crypt_init_kfunc_btf_ids,
  303. };
  304. BTF_KFUNCS_START(crypt_kfunc_btf_ids)
  305. BTF_ID_FLAGS(func, bpf_crypto_decrypt, KF_RCU)
  306. BTF_ID_FLAGS(func, bpf_crypto_encrypt, KF_RCU)
  307. BTF_KFUNCS_END(crypt_kfunc_btf_ids)
  308. static const struct btf_kfunc_id_set crypt_kfunc_set = {
  309. .owner = THIS_MODULE,
  310. .set = &crypt_kfunc_btf_ids,
  311. };
  312. BTF_ID_LIST(bpf_crypto_dtor_ids)
  313. BTF_ID(struct, bpf_crypto_ctx)
  314. BTF_ID(func, bpf_crypto_ctx_release)
  315. static int __init crypto_kfunc_init(void)
  316. {
  317. int ret;
  318. const struct btf_id_dtor_kfunc bpf_crypto_dtors[] = {
  319. {
  320. .btf_id = bpf_crypto_dtor_ids[0],
  321. .kfunc_btf_id = bpf_crypto_dtor_ids[1]
  322. },
  323. };
  324. ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &crypt_kfunc_set);
  325. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &crypt_kfunc_set);
  326. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &crypt_kfunc_set);
  327. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
  328. &crypt_init_kfunc_set);
  329. return ret ?: register_btf_id_dtor_kfuncs(bpf_crypto_dtors,
  330. ARRAY_SIZE(bpf_crypto_dtors),
  331. THIS_MODULE);
  332. }
  333. late_initcall(crypto_kfunc_init);