From patchwork Thu Sep 26 07:29:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Malik X-Patchwork-Id: 13812949 X-Patchwork-Delegate: bpf@iogearbox.net Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9A5218D656 for ; Thu, 26 Sep 2024 07:29:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727335792; cv=none; b=dPjG46HEgf75HmOyprWHzjhY6CxGotaqI1D9HsUS/vv81Gl8/Ks48/SmlPeHx0IFbaeqKI/h9bzlwbpIBprzrtTKEYd7R5JGlW4w4ZMKuGS67fyis/gZId5nieM1kHK7Vb7zYRuiOS99+p9aNMq5bvfC4bzwNZSmQaGdIDT7e1M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727335792; c=relaxed/simple; bh=AtILwdso8NzOLxtEFs34RJP8DsC7sUWq5Kp+xEIxX+8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iy07CM0GrjtHI4gAO0UEjJhcZ1zKqccWm1qrhzotYQ0UKj66fnttvr9Uber3fFmnGlWz/goR0iGeK25ROtxjLWQQzdIM7S7/B0VAaUugjs39qMMd2HslnsfelKUWXWnKJqarq2Gfz3H/6bhOSCxm3v96Z0+mLzyjlFupXcbzrAg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=NB27zixT; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NB27zixT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727335789; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=egHzXd90JXX2Z14ZT3IazKwsXKSJa5C8GINnQaoOXYs=; b=NB27zixTWMvzQq1uZs1c38A6C77MDB/b64gOJ5wDxD13YoZMVw/qN8umEQdByupPhc1ZbD ilu+ZyXxzCvOBEUvwB8g/cVS9cA36M5omYKsFzuJiMGDGHTsJ49YjKDvtGSuNcd9y8jpTw b1LHLC10Hn2V0tz2tiOdTUq42VqJdSU= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-553-dZK2uiVYOOuz77JL8etxTg-1; Thu, 26 Sep 2024 03:29:46 -0400 X-MC-Unique: dZK2uiVYOOuz77JL8etxTg-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1FD2119792F7; Thu, 26 Sep 2024 07:29:44 +0000 (UTC) Received: from vmalik-fedora.fit.vutbr.cz (unknown [10.45.225.122]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 15A881954B0F; Thu, 26 Sep 2024 07:29:39 +0000 (UTC) From: Viktor Malik To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Viktor Malik Subject: [PATCH bpf-next v2 1/2] bpf: Add kfuncs for read-only string operations Date: Thu, 26 Sep 2024 09:29:29 +0200 Message-ID: In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Patchwork-Delegate: bpf@iogearbox.net Kernel contains highly optimised implementation of traditional string operations. Expose them as kfuncs to allow BPF programs leverage the kernel implementation instead of needing to reimplement the operations. For now, add kfuncs for all string operations which do not copy memory around: strcmp, strchr, strrchr, strnchr, strstr, strnstr, strlen, strnlen, strpbrk, strspn, strcspn. Do not add strncmp as it is already exposed as a helper. Signed-off-by: Viktor Malik --- kernel/bpf/helpers.c | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 1a43d06eab28..daa19760d8c8 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3004,6 +3004,61 @@ __bpf_kfunc int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void __user return ret + 1; } +__bpf_kfunc int bpf_strcmp(const char *cs, const char *ct) +{ + return strcmp(cs, ct); +} + +__bpf_kfunc char *bpf_strchr(const char *s, int c) +{ + return strchr(s, c); +} + +__bpf_kfunc char *bpf_strrchr(const char *s, int c) +{ + return strrchr(s, c); +} + +__bpf_kfunc char *bpf_strnchr(const char *s, size_t count, int c) +{ + return strnchr(s, count, c); +} + +__bpf_kfunc char *bpf_strstr(const char *s1, const char *s2) +{ + return strstr(s1, s2); +} + +__bpf_kfunc char *bpf_strnstr(const char *s1, const char *s2, size_t len) +{ + return strnstr(s1, s2, len); +} + +__bpf_kfunc size_t bpf_strlen(const char *s) +{ + return strlen(s); +} + +__bpf_kfunc size_t bpf_strnlen(const char *s, size_t count) +{ + return strnlen(s, count); +} + +__bpf_kfunc char *bpf_strpbrk(const char *cs, const char *ct) +{ + return strpbrk(cs, ct); +} + +__bpf_kfunc size_t bpf_strspn(const char *s, const char *accept) +{ + return strspn(s, accept); +} + +__bpf_kfunc size_t bpf_strcspn(const char *s, const char *reject) +{ + return strcspn(s, reject); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(generic_btf_ids) @@ -3090,6 +3145,17 @@ BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_strcmp) +BTF_ID_FLAGS(func, bpf_strchr) +BTF_ID_FLAGS(func, bpf_strrchr) +BTF_ID_FLAGS(func, bpf_strnchr) +BTF_ID_FLAGS(func, bpf_strstr) +BTF_ID_FLAGS(func, bpf_strnstr) +BTF_ID_FLAGS(func, bpf_strlen) +BTF_ID_FLAGS(func, bpf_strnlen) +BTF_ID_FLAGS(func, bpf_strpbrk) +BTF_ID_FLAGS(func, bpf_strspn) +BTF_ID_FLAGS(func, bpf_strcspn) BTF_KFUNCS_END(common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = { From patchwork Thu Sep 26 07:29:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Malik X-Patchwork-Id: 13812950 X-Patchwork-Delegate: bpf@iogearbox.net Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DEFD8158535 for ; Thu, 26 Sep 2024 07:29:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727335798; cv=none; b=gYwgdGqpnfnAnbKWK2PSqc4dzbUoq7y8Rwr20uB3VNtB2Jos8Qd84ygkgfD9ayQlDTnfvfbwypyN3bwVfjgBLkIvN2SWnI9O1OiGDinkfBEHxSj8BVWcsm96E6S8/ELhT5IpQTWjnQUNMEOxclm7lQxbLoFOkN7xOCjRe7V1Qo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727335798; c=relaxed/simple; bh=CRzDTSNhGSOhcYm3bbHTZ1VAMbEs3i8UeGqZP7XwJI8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ePigS1l+ivBvNctyDtC71cM5GHc1g2wkqTmqfzPGAkKgyP88NidP8lasOwXs6oATLpvlmeY5f7mF5yzv6RVI+Zu0gGZ9JEmy+IekY+i4bD7eS6fPsMV7+5vH39PtSxDfgYc4zPAop+xnPkcthHFIUhfhaTA7YjYTJiCuNloIdoU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XHY8i6ja; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XHY8i6ja" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727335795; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0HbVazn4VlbfB6HhIIQQoS+IwBqztLctyeGKpa+ejHE=; b=XHY8i6jaupLhNokMPQ5MJrCw9vNTYIwzHuxMy2l8I66sVJV5HjHgQJO6Xd1sF7j3UKZbnT zkr80c6mjLd+tlqEu97WhLGg7XSIs+z+vSIIBS5c5usJA5prLUNW3fg/5p8KQtBPhs6oBI g2nCE0RSbQEGJZ99VWBWYDRVWTkQD+0= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-183-A2PIjycxO5-pq3DZnP3q4A-1; Thu, 26 Sep 2024 03:29:50 -0400 X-MC-Unique: A2PIjycxO5-pq3DZnP3q4A-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5A5491955DCF; Thu, 26 Sep 2024 07:29:48 +0000 (UTC) Received: from vmalik-fedora.fit.vutbr.cz (unknown [10.45.225.122]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 90A611954B0E; Thu, 26 Sep 2024 07:29:44 +0000 (UTC) From: Viktor Malik To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Viktor Malik Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Add tests for string kfuncs Date: Thu, 26 Sep 2024 09:29:30 +0200 Message-ID: <34e1a69f9e45fc8e4373d04f5543a1ffa32981fd.1727335530.git.vmalik@redhat.com> In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Patchwork-Delegate: bpf@iogearbox.net The tests attach to `raw_tp/bpf_testmod_test_write_bare` triggerred by `trigger_module_test_write` which writes the string "aaa..." of the given size. Signed-off-by: Viktor Malik --- .../selftests/bpf/prog_tests/string_kfuncs.c | 37 +++ .../selftests/bpf/progs/test_string_kfuncs.c | 215 ++++++++++++++++++ 2 files changed, 252 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/string_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/test_string_kfuncs.c diff --git a/tools/testing/selftests/bpf/prog_tests/string_kfuncs.c b/tools/testing/selftests/bpf/prog_tests/string_kfuncs.c new file mode 100644 index 000000000000..4fe28a4ee6ad --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/string_kfuncs.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "test_string_kfuncs.skel.h" + +void test_string_kfuncs(void) +{ + const int WRITE_SZ = 10; + struct test_string_kfuncs *skel; + struct test_string_kfuncs__bss *bss; + + skel = test_string_kfuncs__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_string_kfuncs__open_end_load")) + return; + + bss = skel->bss; + + if (!ASSERT_OK(test_string_kfuncs__attach(skel), "test_string_kfuncs__attach")) + goto end; + + ASSERT_OK(trigger_module_test_write(WRITE_SZ), "trigger_write"); + + ASSERT_EQ(bss->strcmp_check, 1, "test_strcmp"); + ASSERT_EQ(bss->strchr_check, 1, "test_strchr"); + ASSERT_EQ(bss->strrchr_check, 1, "test_strrchr"); + ASSERT_EQ(bss->strnchr_check, 1, "test_strnchr"); + ASSERT_EQ(bss->strstr_check, 1, "test_strstr"); + ASSERT_EQ(bss->strnstr_check, 1, "test_strstr"); + ASSERT_EQ(bss->strlen_check, 1, "test_strlen"); + ASSERT_EQ(bss->strnlen_check, 1, "test_strnlen"); + ASSERT_EQ(bss->strpbrk_check, 1, "test_strpbrk"); + ASSERT_EQ(bss->strspn_check, 1, "test_strspn"); + ASSERT_EQ(bss->strcspn_check, 1, "test_strspn"); + +end: + test_string_kfuncs__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_string_kfuncs.c b/tools/testing/selftests/bpf/progs/test_string_kfuncs.c new file mode 100644 index 000000000000..3cfe80b1941b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_string_kfuncs.c @@ -0,0 +1,215 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "../bpf_testmod/bpf_testmod.h" + +#define BUFSZ 10 + +int bpf_strcmp(const char *cs, const char *ct) __ksym; +char *bpf_strchr(const char *s, int c) __ksym; +char *bpf_strrchr(const char *s, int c) __ksym; +char *bpf_strnchr(const char *s, size_t count, int c) __ksym; +char *bpf_strstr(const char *s1, const char *s2) __ksym; +char *bpf_strnstr(const char *s1, const char *s2, size_t len) __ksym; +size_t bpf_strlen(const char *) __ksym; +size_t bpf_strnlen(const char *s, size_t count) __ksym; +char *bpf_strpbrk(const char *cs, const char *ct) __ksym; +size_t bpf_strspn(const char *s, const char *accept) __ksym; +size_t bpf_strcspn(const char *s, const char *reject) __ksym; + +__u32 strcmp_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strcmp, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + char expected[] = "aaaaaaaaa"; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strcmp(buf, expected) == 0) + strcmp_check = 1; + + return 0; +} + +__u32 strchr_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strchr, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strchr(buf, 'a') == buf) + strchr_check = 1; + + return 0; +} + +__u32 strrchr_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strrchr, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strrchr(buf, 'a') == &buf[8]) + strrchr_check = 1; + + return 0; +} + +__u32 strnchr_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strnchr, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strnchr(buf, 1, 'a') == buf) + strnchr_check = 1; + + return 0; +} + +__u32 strstr_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strstr, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + char substr[] = "aaa"; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strstr(buf, substr) == buf) + strstr_check = 1; + + return 0; +} + +__u32 strnstr_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strnstr, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + char substr[] = "aaa"; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strnstr(buf, substr, 3) == buf) + strnstr_check = 1; + + return 0; +} + +__u32 strlen_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strlen, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strlen(buf) == 9) + strlen_check = 1; + + return 0; +} + +__u32 strnlen_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strnlen, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strnlen(buf, 5) == 5) + strnlen_check = 1; + + return 0; +} + +__u32 strpbrk_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strpbrk, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + char accept[] = "abc"; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strpbrk(buf, accept) == buf) + strpbrk_check = 1; + + return 0; +} + +__u32 strspn_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strspn, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + char accept[] = "abc"; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strspn(buf, accept) == 9) + strspn_check = 1; + + return 0; +} + +__u32 strcspn_check = 0; + +SEC("raw_tp/bpf_testmod_test_write_bare") +int BPF_PROG(test_strcspn, + struct task_struct *task, struct bpf_testmod_test_write_ctx *write_ctx) +{ + char buf[BUFSZ], *buf_ptr; + char reject[] = "abc"; + + buf_ptr = BPF_PROBE_READ(write_ctx, buf); + bpf_probe_read_kernel_str(buf, sizeof(buf), buf_ptr); + + if (bpf_strcspn(buf, reject) == 0) + strcspn_check = 1; + + return 0; +} + +char LICENSE[] SEC("license") = "GPL";