Skip to contentSkip to conversation

bpf: add skcipher API support to TC/XDP programs

PATCH Superseded Vadim Fedorenko 5 participants 11 messages Oct 26, 2023 – Oct 27, 2023

Subsystems (7)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bpf: add skcipher API support to TC/XDP programs

Add crypto API support to BPF to be able to decrypt or encrypt packets in TC/XDP BPF programs. Only symmetric key ciphers are supported for now. Special care should be taken for initialization part of crypto algo because crypto_alloc_sync_skcipher() doesn't work with preemtion disabled, it can be run only in sleepable BPF program. Also async crypto is not supported because of the very same issue - TC/XDP BPF programs are not sleepable.

---
 include/linux/bpf.h   |   1 +
 kernel/bpf/Makefile   |   3 +
 kernel/bpf/crypto.c   | 278 ++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/helpers.c  |   2 +-
 kernel/bpf/verifier.c |   1 +
 5 files changed, 284 insertions(+), 1 deletion(-)
 create mode 100644 kernel/bpf/crypto.c
include/linux/bpf.h +1 -0
index d3c51a507508..17145738f176 100644
@@ -1222,6 +1222,7 @@ enum bpf_dynptr_type {
 
 int bpf_dynptr_check_size(u32 size);
 u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr);
+enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *ptr);
 
 #ifdef CONFIG_BPF_JIT
 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
kernel/bpf/Makefile +3 -0
index f526b7573e97..e14b5834c477 100644
@@ -41,6 +41,9 @@ obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o
 obj-$(CONFIG_BPF_SYSCALL) += cpumask.o
 obj-${CONFIG_BPF_LSM} += bpf_lsm.o
 endif
+ifeq ($(CONFIG_CRYPTO_SKCIPHER),y)
+obj-$(CONFIG_BPF_SYSCALL) += crypto.o
+endif
 obj-$(CONFIG_BPF_PRELOAD) += preload/
 
 obj-$(CONFIG_BPF_SYSCALL) += relo_core.o
kernel/bpf/crypto.c new file +278 -0

278 lines not on this page · raw message

kernel/bpf/helpers.c +1 -1
index 62a53ebfedf9..0c2a10ff5dfd 100644
@@ -1444,7 +1444,7 @@ static void bpf_dynptr_set_type(struct bpf_dynptr_kern *ptr, enum bpf_dynptr_typ
 	ptr->size |= type << DYNPTR_TYPE_SHIFT;
 }
 
-static enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *ptr)
+enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *ptr)
 {
 	return (ptr->size & ~(DYNPTR_RDONLY_BIT)) >> DYNPTR_TYPE_SHIFT;
 }
kernel/bpf/verifier.c +1 -0
index bb58987e4844..75d2f47ca3cb 100644
@@ -5184,6 +5184,7 @@ BTF_ID(struct, prog_test_ref_kfunc)
 BTF_ID(struct, cgroup)
 BTF_ID(struct, bpf_cpumask)
 BTF_ID(struct, task_struct)
+BTF_ID(struct, bpf_crypto_skcipher_ctx)
 BTF_SET_END(rcu_protected_types)
 
 static bool rcu_protected_object(const struct btf *btf, u32 btf_id)
Signature · 1 line
2.39.3
#1 lore raw

PATCH 2/2 selftests: bpf: crypto skcipher algo selftests

Add simple tc hook selftests to show the way to work with new crypto BPF API. Some weird structre and map are added to setup program to make verifier happy about dynptr initialization from memory. Simple AES-ECB algo is used to demonstrate encryption and decryption of fixed size buffers.

---
 tools/testing/selftests/bpf/config            |   1 +
 .../selftests/bpf/prog_tests/crypto_sanity.c  | 129 +++++++++++++++
 .../selftests/bpf/progs/crypto_common.h       |  98 +++++++++++
 .../selftests/bpf/progs/crypto_sanity.c       | 154 ++++++++++++++++++
 4 files changed, 382 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/crypto_sanity.c
 create mode 100644 tools/testing/selftests/bpf/progs/crypto_common.h
 create mode 100644 tools/testing/selftests/bpf/progs/crypto_sanity.c
tools/testing/selftests/bpf/config +1 -0
index 02dd4409200e..2a5d6339831b 100644
@@ -14,6 +14,7 @@ CONFIG_CGROUP_BPF=y
 CONFIG_CRYPTO_HMAC=y
 CONFIG_CRYPTO_SHA256=y
 CONFIG_CRYPTO_USER_API_HASH=y
+CONFIG_CRYPTO_SKCIPHER=y
 CONFIG_DEBUG_INFO=y
 CONFIG_DEBUG_INFO_BTF=y
 CONFIG_DEBUG_INFO_DWARF4=y
tools/testing/selftests/bpf/prog_tests/crypto_sanity.c new file +129 -0

129 lines not on this page · raw message

tools/testing/selftests/bpf/progs/crypto_common.h new file +98 -0

98 lines not on this page · raw message

tools/testing/selftests/bpf/progs/crypto_sanity.c new file +154 -0

154 lines not on this page · raw message

Signature · 1 line
2.39.3
maintainer
#2 lore raw

Re: [PATCH bpf-next 2/2] selftests: bpf: crypto skcipher algo selftests

Quoted 22 lines · Vadim Fedorenko

Add simple tc hook selftests to show the way to work with new crypto BPF API. Some weird structre and map are added to setup program to make verifier happy about dynptr initialization from memory. Simple AES-ECB algo is used to demonstrate encryption and decryption of fixed size buffers.

---
  tools/testing/selftests/bpf/config            |   1 +
  .../selftests/bpf/prog_tests/crypto_sanity.c  | 129 +++++++++++++++
  .../selftests/bpf/progs/crypto_common.h       |  98 +++++++++++
  .../selftests/bpf/progs/crypto_sanity.c       | 154 ++++++++++++++++++
  4 files changed, 382 insertions(+)

create mode 100644 tools/testing/selftests/bpf/prog_tests/crypto_sanity.c create mode 100644 tools/testing/selftests/bpf/progs/crypto_common.h create mode 100644 tools/testing/selftests/bpf/progs/crypto_sanity.c

tools/testing/selftests/bpf/config +0 -0
index 02dd4409200e..2a5d6339831b 100644

tools/testing/selftests/bpf/config CONFIG_CGROUP_BPF=y · line 20

Quoted 7 lines
tools/testing/selftests/bpf/config +1 -0
@@ -14,6 +14,7 @@ CONFIG_CGROUP_BPF=y
  CONFIG_CRYPTO_HMAC=y
  CONFIG_CRYPTO_SHA256=y
  CONFIG_CRYPTO_USER_API_HASH=y
+CONFIG_CRYPTO_SKCIPHER=y
  CONFIG_DEBUG_INFO=y
  CONFIG_DEBUG_INFO_BTF=y
  CONFIG_DEBUG_INFO_DWARF4=y

Quick note: for upstream CI side, more config seems missing, see the GHA failure:

https://github.com/kernel-patches/bpf/actions/runs/6654055344/job/18081734522

Notice: Success: 435/3403, Skipped: 32, Failed: 1
Error: #64 crypto_sanity
Error: #64 crypto_sanity
test_crypto_sanity:PASS:skel open 0 nsec
test_crypto_sanity:PASS:ip netns add crypto_sanity_ns 0 nsec test_crypto_sanity:PASS:ip -net crypto_sanity_ns -6 addr add face::1/128 dev lo nodad 0 nsec test_crypto_sanity:PASS:ip -net crypto_sanity_ns link set dev lo up 0 nsec test_crypto_sanity:PASS:crypto_sanity__load 0 nsec
open_netns:PASS:malloc token 0 nsec
open_netns:PASS:open /proc/self/ns/net 0 nsec
open_netns:PASS:open netns fd 0 nsec
open_netns:PASS:setns 0 nsec
test_crypto_sanity:PASS:open_netns 0 nsec test_crypto_sanity:PASS:if_nametoindex lo 0 nsec test_crypto_sanity:PASS:crypto_sanity__attach 0 nsec test_crypto_sanity:PASS:skb_crypto_setup fd 0 nsec test_crypto_sanity:PASS:skb_crypto_setup 0 nsec test_crypto_sanity:PASS:skb_crypto_setup retval 0 nsec test_crypto_sanity:FAIL:skb_crypto_setup status unexpected error: -95 (errno 2)
libbpf: Kernel error message: Parent Qdisc doesn't exists close_netns:PASS:setns 0 nsec
Test Results:
bpftool: PASS
test_progs-no_alu32: FAIL (returned 1)
shutdown: CLEAN
Error: Process completed with exit code 1.

maintainer
#3 lore raw

Re: [PATCH bpf-next 2/2] selftests: bpf: crypto skcipher algo selftests

Quoted 18 lines · Daniel Borkmann
Quoted 17 lines · Vadim Fedorenko

Add simple tc hook selftests to show the way to work with new crypto BPF API. Some weird structre and map are added to setup program to make verifier happy about dynptr initialization from memory. Simple AES-ECB algo is used to demonstrate encryption and decryption of fixed size buffers.

---
  tools/testing/selftests/bpf/config            |   1 + .../selftests/bpf/prog_tests/crypto_sanity.c  | 129 +++++++++++++++ .../selftests/bpf/progs/crypto_common.h       |  98 +++++++++++ .../selftests/bpf/progs/crypto_sanity.c       | 154 ++++++++++++++++++

  4 files changed, 382 insertions(+)

  create mode 100644 tools/testing/selftests/bpf/prog_tests/crypto_sanity.c create mode 100644 tools/testing/selftests/bpf/progs/crypto_common.h create mode 100644 tools/testing/selftests/bpf/progs/crypto_sanity.c

Quoted 14 lines
Quoted 5 lines
tools/testing/selftests/bpf/config +0 -0
index 02dd4409200e..2a5d6339831b 100644
@@ -14,6 +14,7 @@ CONFIG_CGROUP_BPF=y
Quoted 7 lines

  CONFIG_CRYPTO_HMAC=y
  CONFIG_CRYPTO_SHA256=y
  CONFIG_CRYPTO_USER_API_HASH=y
+CONFIG_CRYPTO_SKCIPHER=y
  CONFIG_DEBUG_INFO=y
  CONFIG_DEBUG_INFO_BTF=y
  CONFIG_DEBUG_INFO_DWARF4=y

Quick note: for upstream CI side, more config seems missing, see the GHA failure:

Thanks for the signal, Daniel. Looks like CONFIG_CRYPTO_ECB is missing. I'll adjust config for v2, but I'll wait a bit longer to get more feedback

Quoted 31 lines

https://github.com/kernel-patches/bpf/actions/runs/6654055344/job/18081734522

Notice: Success: 435/3403, Skipped: 32, Failed: 1
Error: #64 crypto_sanity
  Error: #64 crypto_sanity
  test_crypto_sanity:PASS:skel open 0 nsec test_crypto_sanity:PASS:ip netns add crypto_sanity_ns 0 nsec test_crypto_sanity:PASS:ip -net crypto_sanity_ns -6 addr add
face::1/128 dev lo nodad 0 nsec
  test_crypto_sanity:PASS:ip -net crypto_sanity_ns link set dev lo up 0
nsec
  test_crypto_sanity:PASS:crypto_sanity__load 0 nsec open_netns:PASS:malloc token 0 nsec
  open_netns:PASS:open /proc/self/ns/net 0 nsec open_netns:PASS:open netns fd 0 nsec open_netns:PASS:setns 0 nsec test_crypto_sanity:PASS:open_netns 0 nsec test_crypto_sanity:PASS:if_nametoindex lo 0 nsec test_crypto_sanity:PASS:crypto_sanity__attach 0 nsec test_crypto_sanity:PASS:skb_crypto_setup fd 0 nsec test_crypto_sanity:PASS:skb_crypto_setup 0 nsec test_crypto_sanity:PASS:skb_crypto_setup retval 0 nsec test_crypto_sanity:FAIL:skb_crypto_setup status unexpected error: -95
(errno 2)
  libbpf: Kernel error message: Parent Qdisc doesn't exists close_netns:PASS:setns 0 nsec
Test Results:
             bpftool: PASS
 test_progs-no_alu32: FAIL (returned 1)
            shutdown: CLEAN
Error: Process completed with exit code 1.

maintainer
#4 lore raw
In reply to Vadim Fedorenko
Quoted 7 lines · On Wed, 25 Oct 2023 18:59:37 -0700 Vadim Fedorenko wrote:

Add crypto API support to BPF to be able to decrypt or encrypt packets in TC/XDP BPF programs. Only symmetric key ciphers are supported for now. Special care should be taken for initialization part of crypto algo because crypto_alloc_sync_skcipher() doesn't work with preemtion disabled, it can be run only in sleepable BPF program. Also async crypto is not supported because of the very same issue - TC/XDP BPF programs are not sleepable.

Do CC crypto@ for the next version, please.

kernel/bpf/crypto.c line 20

Quoted 9 lines
kernel/bpf/crypto.c new file +9 -0
+/**
+ * struct bpf_crypto_skcipher_ctx - refcounted BPF sync skcipher context structure
+ * @tfm:	The pointer to crypto_sync_skcipher struct.
+ * @rcu:	The RCU head used to free the crypto context with RCU safety.
+ * @usage:	Object reference counter. When the refcount goes to 0, the
+ *		memory is released back to the BPF allocator, which provides
+ *		RCU safety.
+ */
+

spurious newline?

kernel/bpf/crypto.c

Quoted 4 lines
kernel/bpf/crypto.c new file +3 -0
+struct bpf_crypto_skcipher_ctx {
 
+/**
+ * bpf_crypto_skcipher_ctx_acquire() - Acquire a reference to a BPF crypto context.

The contexts are refcounted and can be placed in maps? Does anything prevent them from being used simultaneously by difference CPUs?

kernel/bpf/crypto.c line 146

Quoted 2 lines
kernel/bpf/crypto.c new file +2 -0
+	case BPF_DYNPTR_TYPE_SKB:
+		return skb_pointer_if_linear(ptr->data, ptr->offset, __bpf_dynptr_size(ptr));

dynptr takes care of checking if skb can be written to?

maintainer
#5 lore raw
Quoted 10 lines · Jakub Kicinski
Quoted 7 lines · On Wed, 25 Oct 2023 18:59:37 -0700 Vadim Fedorenko wrote:

Add crypto API support to BPF to be able to decrypt or encrypt packets in TC/XDP BPF programs. Only symmetric key ciphers are supported for now. Special care should be taken for initialization part of crypto algo because crypto_alloc_sync_skcipher() doesn't work with preemtion disabled, it can be run only in sleepable BPF program. Also async crypto is not supported because of the very same issue - TC/XDP BPF programs are not sleepable.

Do CC crypto@ for the next version, please.

Sure

Quoted 11 lines

kernel/bpf/crypto.c line 20

Quoted 9 lines
kernel/bpf/crypto.c new file +9 -0
+/**
+ * struct bpf_crypto_skcipher_ctx - refcounted BPF sync skcipher context structure
+ * @tfm:	The pointer to crypto_sync_skcipher struct.
+ * @rcu:	The RCU head used to free the crypto context with RCU safety.
+ * @usage:	Object reference counter. When the refcount goes to 0, the
+ *		memory is released back to the BPF allocator, which provides
+ *		RCU safety.
+ */
+

spurious newline?

yeah, will fix it

Quoted 6 lines

kernel/bpf/crypto.c

Quoted 4 lines
kernel/bpf/crypto.c new file +3 -0
+struct bpf_crypto_skcipher_ctx {
 
+/**
+ * bpf_crypto_skcipher_ctx_acquire() - Acquire a reference to a BPF crypto context.

The contexts are refcounted and can be placed in maps?

Yes, the idea was to avoid allocation of algo object and setting the key on hot path. And for now there is no way to allocate crypto cipher object in TC/XDP hook because it uses GFP_KERNEL and delayed module load.

Quoted 2 lines

Does anything prevent them from being used simultaneously by difference CPUs?

The algorithm configuration and the key can be used by different CPUs simultaneously

Quoted 4 lines

kernel/bpf/crypto.c line 146

Quoted 2 lines
kernel/bpf/crypto.c new file +2 -0
+	case BPF_DYNPTR_TYPE_SKB:
+		return skb_pointer_if_linear(ptr->data, ptr->offset, __bpf_dynptr_size(ptr));

dynptr takes care of checking if skb can be written to?

dynptr is used to take care of size checking, but this particular part is used to provide plain buffer from skb. I'm really sure if we can (or should) encrypt or decrypt in-place, so API now assumes that src and dst are different buffers.

maintainer
#6 lore raw
Quoted 5 lines · On Fri, 27 Oct 2023 00:29:29 +0100 Vadim Fedorenko wrote:
Quoted 2 lines

Does anything prevent them from being used simultaneously by difference CPUs?

The algorithm configuration and the key can be used by different CPUs simultaneously

Makes sense, got confused ctx vs req. You allocate req on the fly.

Quoted 8 lines
Quoted 4 lines

kernel/bpf/crypto.c line 146

Quoted 2 lines
kernel/bpf/crypto.c new file +2 -0
+	case BPF_DYNPTR_TYPE_SKB:
+		return skb_pointer_if_linear(ptr->data, ptr->offset, __bpf_dynptr_size(ptr));  

dynptr takes care of checking if skb can be written to?

dynptr is used to take care of size checking, but this particular part is used to provide plain buffer from skb. I'm really sure if we can (or should) encrypt or decrypt in-place, so API now assumes that src and dst are different buffers.

Not sure this answers my question. What I'm asking is basically whether for destination we need to call __bpf_dynptr_is_rdonly() or something already checks that.

maintainer
#7 lore raw
Quoted 14 lines · Jakub Kicinski
Quoted 5 lines · On Fri, 27 Oct 2023 00:29:29 +0100 Vadim Fedorenko wrote:
Quoted 2 lines

Does anything prevent them from being used simultaneously by difference CPUs?

The algorithm configuration and the key can be used by different CPUs simultaneously

Makes sense, got confused ctx vs req. You allocate req on the fly.

Quoted 5 lines
Quoted 4 lines

kernel/bpf/crypto.c line 146

Quoted 2 lines
kernel/bpf/crypto.c new file +2 -0
+	case BPF_DYNPTR_TYPE_SKB:
+		return skb_pointer_if_linear(ptr->data, ptr->offset, __bpf_dynptr_size(ptr));

dynptr takes care of checking if skb can be written to?

Quoted 7 lines
Quoted 3 lines

dynptr is used to take care of size checking, but this particular part is used to provide plain buffer from skb. I'm really sure if we can (or should) encrypt or decrypt in-place, so API now assumes that src and dst are different buffers.

Not sure this answers my question. What I'm asking is basically whether for destination we need to call __bpf_dynptr_is_rdonly() or something already checks that.

ah, good point. I'm not sure how to make it better. the __bpf_dynptr_data_ptr() code is based on bpf_dynptr_slice() which has bpf_dynptr_slice_rdwr() variant. I don't think it's good idea to add local rdwr variant. I can either add 2 parameter to force checking if dynptr isn't read-only, or I can convert bpf_dynptr_slice* functions to be wrappers over __bpf_dynptr_slice and reuse it in this code.

maintainer
#8 lore raw
In reply to Jakub Kicinski
Quoted 14 lines · Jakub Kicinski
Quoted 5 lines · On Fri, 27 Oct 2023 00:29:29 +0100 Vadim Fedorenko wrote:
Quoted 2 lines

Does anything prevent them from being used simultaneously by difference CPUs?

The algorithm configuration and the key can be used by different CPUs simultaneously

Makes sense, got confused ctx vs req. You allocate req on the fly.

Quoted 5 lines
Quoted 4 lines

kernel/bpf/crypto.c line 146

Quoted 2 lines
kernel/bpf/crypto.c new file +2 -0
+	case BPF_DYNPTR_TYPE_SKB:
+		return skb_pointer_if_linear(ptr->data, ptr->offset, __bpf_dynptr_size(ptr));

dynptr takes care of checking if skb can be written to?

Quoted 7 lines
Quoted 3 lines

dynptr is used to take care of size checking, but this particular part is used to provide plain buffer from skb. I'm really sure if we can (or should) encrypt or decrypt in-place, so API now assumes that src and dst are different buffers.

Not sure this answers my question. What I'm asking is basically whether for destination we need to call __bpf_dynptr_is_rdonly() or something already checks that.

Well, I actually went to simpler implementation. As it's only needed for dst buffer, move __bpf_dynptr_is_rdonly to helpers and use it to check dst only and break earlier in case of error. If there will be other customers of __bpf_dynptr_data_ptr helper, I'll implement it other way.

#9 lore raw
In reply to Vadim Fedorenko
Quoted 8 lines · Vadim Fedorenko
kernel/bpf/crypto.c new file +7 -0
+__bpf_kfunc struct bpf_crypto_skcipher_ctx *
+bpf_crypto_skcipher_ctx_create(const struct bpf_dynptr_kern *algo, const struct bpf_dynptr_kern *key,
+                              int *err)
+{
+       struct bpf_crypto_skcipher_ctx *ctx;
+
+       if (__bpf_dynptr_size(algo) > CRYPTO_MAX_ALG_NAME) {

kernel/bpf/crypto.c line 61

Quoted 10 lines
kernel/bpf/crypto.c new file +10 -0
+               *err = -EINVAL;
+               return NULL;
+       }
+
+       if (!crypto_has_skcipher(algo->data, CRYPTO_ALG_TYPE_SKCIPHER, CRYPTO_ALG_TYPE_MASK)) {
+               *err = -EOPNOTSUPP;
+               return NULL;
+       }
+
+       ctx = bpf_mem_cache_alloc(&bpf_crypto_ctx_ma);

Since this kfunc is sleepable, just kmalloc(GFP_KERNEL) here. No need to use bpf_mem_alloc.

maintainer
#10 lore raw
Quoted 14 lines · Alexei Starovoitov
Quoted 13 lines · Vadim Fedorenko
kernel/bpf/crypto.c new file +12 -0
+__bpf_kfunc struct bpf_crypto_skcipher_ctx *
+bpf_crypto_skcipher_ctx_create(const struct bpf_dynptr_kern *algo, const struct bpf_dynptr_kern *key,
+                              int *err)
+{
+       struct bpf_crypto_skcipher_ctx *ctx;
+
+       if (__bpf_dynptr_size(algo) > CRYPTO_MAX_ALG_NAME) {
+               *err = -EINVAL;
+               return NULL;
+       }
+
+       if (!crypto_has_skcipher(algo->data, CRYPTO_ALG_TYPE_SKCIPHER, CRYPTO_ALG_TYPE_MASK)) {
Quoted 8 lines

kernel/bpf/crypto.c line 61

Quoted 5 lines
kernel/bpf/crypto.c new file +5 -0
+               *err = -EOPNOTSUPP;
+               return NULL;
+       }
+
+       ctx = bpf_mem_cache_alloc(&bpf_crypto_ctx_ma);

Since this kfunc is sleepable, just kmalloc(GFP_KERNEL) here. No need to use bpf_mem_alloc.

I was thinking about adding GFP_ATOMIC allocation option to crypto_alloc_sync_skcipher, it's already implemented for cloning skcipher object. Then the code can be reused for both sleepable (expect module loading) and non-sleepable (fail if there is no crypto module loaded) variants without any changes. But I can implement different allocators for different options.