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