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