From patchwork Tue Aug 20 10:17:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13769834 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mout.perfora.net (mout.perfora.net [74.208.4.197]) (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 E32CF189B99 for ; Tue, 20 Aug 2024 10:18:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.208.4.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724149091; cv=none; b=mLEdC8Uy++88CSnoVvZZoahiO1w7m8FxSwcGx+mc/Bm7WJXJ2DLQqHC/JvAWGt+79haQx+c46OzEkRb7wbPmu2EI2X62xfi6+SWcyGULj0ojXvD+oHpPoGJsZk7Iy7ZtOLlmcL41deLD47uOqfe7cVVLuoPlG78J5xSA3dzTQ9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724149091; c=relaxed/simple; bh=StpzmIy6MTbgeUPsovd3tBDsxc2RZ1DeyJXtAbv11Yc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=E/NoGJsLXu+/XwQPweV7z/oOEI5gVWe+22Z7wj3OKlAcpRJAw6KiD5gtiFNCfwfLDakwv2DLO9o6UPhoEuShCYngDBnkvbbmgJP+o3rvmv7XILLntGqIz6+lelt1WQkZhx69NpdGq+wXYelaNCJ1NVVQhR0VB84IzZe3H0OjOig= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=jordanrome.com; spf=pass smtp.mailfrom=jordanrome.com; dkim=pass (2048-bit key) header.d=jordanrome.com header.i=linux@jordanrome.com header.b=Ba/GczE0; arc=none smtp.client-ip=74.208.4.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=jordanrome.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=jordanrome.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=jordanrome.com header.i=linux@jordanrome.com header.b="Ba/GczE0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1724149062; x=1724753862; i=linux@jordanrome.com; bh=AJMRgGeeMzRnSr8q7dbhe7hdqvLeBI9dUaUJORKMGRM=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID: MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=Ba/GczE0a31vg9J4LZE/GybUQJrfk0S46xVlNacirMxdM84Ef9YzrKS9YF3XVnqg 0TwQ44xmjiicny5yHnSRjx8Mf2u/YRTGJrjfVBtxxjggVfSyDuggEk22JC784Rkag TWW/wGCAmgWfkiLUgcYXt1H8da/TRtWvtPNGuIH3JHdQLQ0y1pVUf28YcT+vfvGM0 ZcQ1hCkmJAtLqS+fhqB9rRmVmVzqW6K+XyZhRmT21AJgrGbz73RWQ2m7PeUNVn7fx euXzMcRwShkhiOdnSe1IMaoplfEHkB+B5h5KrDWEnbWaFEPH1z5xdWtrr4NhBuPpN wIkvRJF683kuwCqJ1w== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([173.252.127.112]) by mrelay.perfora.net (mreueus002 [74.208.5.2]) with ESMTPSA (Nemesis) id 0LxhU5-1s5pBm3wwV-0106F8; Tue, 20 Aug 2024 12:17:42 +0200 From: Jordan Rome To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Kernel Team , sinquersw@gmail.com Subject: [bpf-next v7 1/2] bpf: Add bpf_copy_from_user_str kfunc Date: Tue, 20 Aug 2024 03:17:24 -0700 Message-ID: <20240820101725.1629353-1-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Provags-ID: V03:K1:eb2sZk3hGKhq3SoXHrMuLkbpsORyCWv873eyIfthe5iWHI2zMkR cmkE6BGxrfn3zb52EJvSvIkhZ1sEoEyUA7vU3462Qsmr2JTHExiHe42FGoolAtyK/KjoELU 4DBZViVrNyy/R4mHbu/fyHQnpeJiXYHQ05cGn5bS/OC9VaGAQEuSiva2z5YMbfwj36BFGpH x4KFQSqKA9Dg/OIp+XbWQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:X6Kv4aGorUo=;fELzMtlFdPHnl8FtbR4E8cISqAv 6iodcwkZjKZaE9EK9jcuMez24LNjiifKQDK5IZFc4mjsBq29MLR1AO4zohU+sIY2Of2kkR5nH Lq76c53UrpUYCXNlZSY1NKMxEV3RLwhocv61BBAb8MXBFbdPUZ2TslpNN48DWeGvbhBBEXE99 dd8dn+7uEHX6qgHXubc93yKVfsG+3beNF2FW2ekMM25I5k3jS5Plj2trbJRmjdz5AdDLXQgpn 2TDTS5LO5i9TPsSpf5+ObgtgF/I0WuqtAgDxt0ipMJ8TzO+gfydG0DBXOZJ6WP1vIduUrPK7o NBRWwOXNohDX6iDI5fkP4rSfNp+j899rV+ZpChd3mmTkEWSb6vOFxYPD6ak0nO8amIG0pq2uG GR9l7bwIjs9MDm7F0VvpmLFMG+kdlRDlzGL4HbOAnOsnR3Ikcpwgj7WjgOwIc5uFcdpKhcMTn 0McfmGxgpUKGpGDBvFwCe00Sx0264vN/jlkkt1pReboG+1x52Humlybs8ccBIFJsVYXFmfeQj 8Qa2h3+VxGc9+u4D417FaPAYdDr0XJfXW7ogDVSkbNxGc/yxp7UHecCYAgav3jfNJTOKFJxLr /Bj3+3KKJ4ZgFOsqj+LMUrtdAPqoZToQu8pUwplvBmkDZYGikiHigTcphytFx1ZfIwf+gmCNp 3g4deRFqWYiEQ9oY/8XbUeiePvKOUxiW9F2/8Lr9slTnoJh91yIXKxzgch93EE3RPUmY27VpN fsj5RhGCFRZz2RkQIxzVHrT+ul164HKwQ== X-Patchwork-Delegate: bpf@iogearbox.net This adds a kfunc wrapper around strncpy_from_user, which can be called from sleepable BPF programs. This matches the non-sleepable 'bpf_probe_read_user_str' helper except it includes an additional 'flags' param, which allows consumers to clear the entire destination buffer on success or failure. Signed-off-by: Jordan Rome --- include/uapi/linux/bpf.h | 9 ++++++++ kernel/bpf/helpers.c | 42 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 9 ++++++++ 3 files changed, 60 insertions(+) -- 2.43.5 diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index e05b39e39c3f..d3b69cb055c0 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -7513,4 +7513,13 @@ struct bpf_iter_num { __u64 __opaque[1]; } __attribute__((aligned(8))); +/* + * Flags to control bpf_copy_from_user_str() behaviour. + * - BPF_F_PAD_ZEROS: Pad destination buffer with zeros. (See the respective + * helper documentation for details.) + */ +enum { + BPF_F_PAD_ZEROS = (1ULL << 0) +}; + #endif /* _UAPI__LINUX_BPF_H__ */ diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index d02ae323996b..e5614a980d59 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2939,6 +2939,47 @@ __bpf_kfunc void bpf_iter_bits_destroy(struct bpf_iter_bits *it) bpf_mem_free(&bpf_global_ma, kit->bits); } +/** + * bpf_copy_from_user_str() - Copy a string from an unsafe user address + * @dst: Destination address, in kernel space. This buffer must be at + * least @dst__szk bytes long. + * @dst__szk: Maximum number of bytes to copy, including the trailing NUL. + * @unsafe_ptr__ign: Source address, in user space. + * @flags: The only supported flag is BPF_F_PAD_ZEROS + * + * Copies a NUL-terminated string from userspace to BPF space. If user string is + * too long this will still ensure zero termination in the dst buffer unless + * buffer size is 0. + * + * If BPF_F_PAD_ZEROS flag is set, memset the tail of @dst to 0 on success and + * memset all of @dst on failure. + */ +__bpf_kfunc int bpf_copy_from_user_str(void *dst, u32 dst__szk, const void __user *unsafe_ptr__ign, u64 flags) +{ + int ret; + + if (unlikely(flags & ~BPF_F_PAD_ZEROS)) + return -EINVAL; + + if (unlikely(!dst__szk)) + return 0; + + ret = strncpy_from_user(dst, unsafe_ptr__ign, dst__szk - 1); + if (ret < 0) { + if (flags & BPF_F_PAD_ZEROS) + memset((char *)dst, 0, dst__szk); + + return ret; + } + + if (flags & BPF_F_PAD_ZEROS) + memset((char *)dst + ret, 0, dst__szk - ret); + else + ((char *)dst)[ret] = '\0'; + + return ret + 1; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(generic_btf_ids) @@ -3024,6 +3065,7 @@ BTF_ID_FLAGS(func, bpf_preempt_enable) 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_KFUNCS_END(common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = { diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index e05b39e39c3f..d3b69cb055c0 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -7513,4 +7513,13 @@ struct bpf_iter_num { __u64 __opaque[1]; } __attribute__((aligned(8))); +/* + * Flags to control bpf_copy_from_user_str() behaviour. + * - BPF_F_PAD_ZEROS: Pad destination buffer with zeros. (See the respective + * helper documentation for details.) + */ +enum { + BPF_F_PAD_ZEROS = (1ULL << 0) +}; + #endif /* _UAPI__LINUX_BPF_H__ */ From patchwork Tue Aug 20 10:17:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13769835 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mout.perfora.net (mout.perfora.net [74.208.4.197]) (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 D88C718CC0D for ; Tue, 20 Aug 2024 10:18:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.208.4.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724149097; cv=none; b=cAx+ebEVnd3EW/j1Ot0+DYDBRCW5krCCHobuGv/VM/cmfzoPovz2R4S6E0q92Sn9XmmbTi+h3l64M+ugLswM3oYDZVTK+YXjNQiSAsrGVkhZPdywu95WHIw4uq4SlVzzfxADihs9QAtmDx327cUPIAZdYA2ptv5CwfIFjcLWAiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724149097; c=relaxed/simple; bh=ftiCdK+HuM1djLlIQJCHxpKalyNiSzoY+iDHVh2EdEo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p3GQL93AiNzt83SXYVvn9wTsDTS4cRqT4dhFfwcWzklSU6wsJ941Oc9PYLtspNMrqHfXB7NZ4DnDyEMzf/LTAcqGjwNdQfQLHLetATgGjMOmTpXpwO0eQspJuZDorbnpLJg9m/TTq30HdxwPv4WcqdWyrL9+b5f7zUVwgP+qG40= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=jordanrome.com; spf=pass smtp.mailfrom=jordanrome.com; dkim=pass (2048-bit key) header.d=jordanrome.com header.i=linux@jordanrome.com header.b=ratZBya6; arc=none smtp.client-ip=74.208.4.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=jordanrome.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=jordanrome.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=jordanrome.com header.i=linux@jordanrome.com header.b="ratZBya6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1724149067; x=1724753867; i=linux@jordanrome.com; bh=JAtM5HzXKWoEKkKhlpQuXZ5CfmOI1squcd2mbmSOjBY=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-ID:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=ratZBya6LCMs8Cc4A6s1YFkub0uwmL9qKPNy0thsXZyQJd8kyOm32nkiBfj5bmDM g2axpYtF38yPSfmPQXvJF/zc/7hHXmSH7wRlrnEZFXlx0KLoBcf+ro/xS3NcrN12z 6+uKUdVKs8ifsgQzcAwZVHV7LBygRAtn91wFOYVcYg+K0ocOz5c765dhF2T3+a22G c1GaRZTSags2wCe933v+5ovViRlih639KWD/r2vi6Ws3Ot7sCzU+XDUUcX7TgvFty rgE2mTBIgSjA9Y64o+Sp01910cRM3tA9InnBIKNv7Aryh+N5SdqzX4uVeILv9YL6m fZ+VEbxSkTU8JmLSpg== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([173.252.127.115]) by mrelay.perfora.net (mreueus004 [74.208.5.2]) with ESMTPSA (Nemesis) id 1MrPuF-1sKCHE1uFz-00pDMI; Tue, 20 Aug 2024 12:17:47 +0200 From: Jordan Rome To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Kernel Team , sinquersw@gmail.com Subject: [bpf-next v7 2/2] bpf: Add tests for bpf_copy_from_user_str kfunc Date: Tue, 20 Aug 2024 03:17:25 -0700 Message-ID: <20240820101725.1629353-2-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240820101725.1629353-1-linux@jordanrome.com> References: <20240820101725.1629353-1-linux@jordanrome.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Provags-ID: V03:K1:1deFl2NAkpzcmnCTZ5CI3nU0UYN425yjP0llTeSmHgxFFh1dYnq diVBjuvtbaM+IMPwaOGaSAx7Fwwx4ng1CMA+iB4N995hKqpOkyoiQLROEjCtal12Wm4dKAA FUy+whcDQ0H8HX2IEeL6WvdDoHFG/6FK/C6IqY8UJV26aZXmG+GEhotBwmrTH6tcpuyvm1O yfqhB0Tx7hQUEomgxvbIA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:Ad1WTZ4IJo4=;71QpmsXeWb22dsrWd6iDJRf10PG kkvcFn/KGZwbAW9HSRs+kwSoTjFoCknO7l/jC3acEShHO7En/8vtGhNHdbB+Iw8SWnUebIFBf CHb5L71v601xQmaH46aWOfknYWF3H2FM6/noW/imNPEvO04z8Z0KhMgrc6iWiNPieBZxq9LbS sEzoF4jZoS9LtIr6ygI0TIAv4uJR6DOma2t6Ny618BnRnr4ITHxGgzGnr/r9iLuPkqfiph8hq d+b/zFBQVd9jc/bx8T3edXukNJsYYoGCEwCqj5e01ZlRe+bylH6x1CTs11bgnASHAYR8CAm4e LqXWlGGxamqxd/dbKR2Kswbh2kpbWU63hSXJORvKyzH3vTAOE4YkHfCSP6aQiqc08LwcaoMPP GoYMkymix3bF8mnwoYzgf5yVJzkhBxvFVsMG9Ex2DgzDF3PKVxrRJBfHr7XcJhMD1S1TPvdAz VLR/u5gDwMGhmq/3BvK1IjH0vDoow6gF77cg885UB/6UvLXXDpUfPg/ObK3Lv9ptxg5m2HMdh CmnUko5awfjkvVTzuiT3JY0ILg4l0LHf0abxNuk1s1G1+naB/ZowcfueMhAS0r1+KbjPRFCHY qN46rwJeqP8dDP48WwjwtmRgDLGAFj7K5bYXznc4GOh6tJ3H0+OLWSoyS7gOr48JfGCIk+Lzw 7r2RiF52oAbNz9PU+WdVkDaJplkysIpVUWyPKfV6r1G5Sgar4Op3ZdJTSo0M2NEkaDmhu1G/W gxxL7Jyi/im5tM6yNSQSlkDepYNWfZl3Q== X-Patchwork-Delegate: bpf@iogearbox.net This adds tests for both the happy path and the error path. Signed-off-by: Jordan Rome --- .../selftests/bpf/prog_tests/attach_probe.c | 8 ++- .../selftests/bpf/prog_tests/read_vsyscall.c | 1 + .../selftests/bpf/progs/read_vsyscall.c | 9 ++- .../selftests/bpf/progs/test_attach_probe.c | 57 ++++++++++++++++++- 4 files changed, 68 insertions(+), 7 deletions(-) -- 2.43.5 diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c index 7175af39134f..329c7862b52d 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -283,9 +283,11 @@ static void test_uprobe_sleepable(struct test_attach_probe *skel) trigger_func3(); ASSERT_EQ(skel->bss->uprobe_byname3_sleepable_res, 9, "check_uprobe_byname3_sleepable_res"); - ASSERT_EQ(skel->bss->uprobe_byname3_res, 10, "check_uprobe_byname3_res"); - ASSERT_EQ(skel->bss->uretprobe_byname3_sleepable_res, 11, "check_uretprobe_byname3_sleepable_res"); - ASSERT_EQ(skel->bss->uretprobe_byname3_res, 12, "check_uretprobe_byname3_res"); + ASSERT_EQ(skel->bss->uprobe_byname3_str_sleepable_res, 10, "check_uprobe_byname3_str_sleepable_res"); + ASSERT_EQ(skel->bss->uprobe_byname3_res, 11, "check_uprobe_byname3_res"); + ASSERT_EQ(skel->bss->uretprobe_byname3_sleepable_res, 12, "check_uretprobe_byname3_sleepable_res"); + ASSERT_EQ(skel->bss->uretprobe_byname3_str_sleepable_res, 13, "check_uretprobe_byname3_str_sleepable_res"); + ASSERT_EQ(skel->bss->uretprobe_byname3_res, 14, "check_uretprobe_byname3_res"); } void test_attach_probe(void) diff --git a/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c b/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c index 3405923fe4e6..c7b9ba8b1d06 100644 --- a/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c +++ b/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c @@ -23,6 +23,7 @@ struct read_ret_desc { { .name = "probe_read_user_str", .ret = -EFAULT }, { .name = "copy_from_user", .ret = -EFAULT }, { .name = "copy_from_user_task", .ret = -EFAULT }, + { .name = "copy_from_user_str", .ret = -EFAULT }, }; void test_read_vsyscall(void) diff --git a/tools/testing/selftests/bpf/progs/read_vsyscall.c b/tools/testing/selftests/bpf/progs/read_vsyscall.c index 986f96687ae1..39ebef430059 100644 --- a/tools/testing/selftests/bpf/progs/read_vsyscall.c +++ b/tools/testing/selftests/bpf/progs/read_vsyscall.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (C) 2024. Huawei Technologies Co., Ltd */ +#include "vmlinux.h" #include #include @@ -7,10 +8,15 @@ int target_pid = 0; void *user_ptr = 0; -int read_ret[8]; +int read_ret[9]; char _license[] SEC("license") = "GPL"; +/* + * This is the only kfunc, the others are helpers + */ +int bpf_copy_from_user_str(void *dst, u32, const void *, u64) __weak __ksym; + SEC("fentry/" SYS_PREFIX "sys_nanosleep") int do_probe_read(void *ctx) { @@ -40,6 +46,7 @@ int do_copy_from_user(void *ctx) read_ret[6] = bpf_copy_from_user(buf, sizeof(buf), user_ptr); read_ret[7] = bpf_copy_from_user_task(buf, sizeof(buf), user_ptr, bpf_get_current_task_btf(), 0); + read_ret[8] = bpf_copy_from_user_str((char *)buf, sizeof(buf), user_ptr, 0); return 0; } diff --git a/tools/testing/selftests/bpf/progs/test_attach_probe.c b/tools/testing/selftests/bpf/progs/test_attach_probe.c index 68466a6ad18c..4959eda92479 100644 --- a/tools/testing/selftests/bpf/progs/test_attach_probe.c +++ b/tools/testing/selftests/bpf/progs/test_attach_probe.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "bpf_misc.h" int kprobe2_res = 0; @@ -14,11 +15,15 @@ int uretprobe_byname_res = 0; int uprobe_byname2_res = 0; int uretprobe_byname2_res = 0; int uprobe_byname3_sleepable_res = 0; +int uprobe_byname3_str_sleepable_res = 0; int uprobe_byname3_res = 0; int uretprobe_byname3_sleepable_res = 0; +int uretprobe_byname3_str_sleepable_res = 0; int uretprobe_byname3_res = 0; void *user_ptr = 0; +int bpf_copy_from_user_str(void *dst, u32, const void *, u64) __weak __ksym; + SEC("ksyscall/nanosleep") int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem) { @@ -87,11 +92,55 @@ static __always_inline bool verify_sleepable_user_copy(void) return bpf_strncmp(data, sizeof(data), "test_data") == 0; } +static __always_inline bool verify_sleepable_user_copy_str(void) +{ + int ret; + char data_long[20]; + char data_long_pad[20]; + char data_long_err[20]; + char data_short[4]; + char data_short_pad[4]; + + ret = bpf_copy_from_user_str(data_short, sizeof(data_short), user_ptr, 0); + + if (bpf_strncmp(data_short, 4, "tes\0") != 0 || ret != 4) + return false; + + ret = bpf_copy_from_user_str(data_short_pad, sizeof(data_short_pad), user_ptr, BPF_F_PAD_ZEROS); + + if (bpf_strncmp(data_short, 4, "tes\0") != 0 || ret != 4) + return false; + + ret = bpf_copy_from_user_str(data_long, sizeof(data_long), user_ptr, 0); + + if (bpf_strncmp(data_long, 10, "test_data\0") != 0 || ret != 10) + return false; + + ret = bpf_copy_from_user_str(data_long_pad, sizeof(data_long_pad), user_ptr, BPF_F_PAD_ZEROS); + + if (bpf_strncmp(data_long_pad, 10, "test_data\0") != 0 || ret != 10 || data_long_pad[19] != '\0') + return false; + + ret = bpf_copy_from_user_str(data_long_err, sizeof(data_long_err), (void *)data_long, BPF_F_PAD_ZEROS); + + if (ret > 0 || data_long_err[19] != '\0') + return false; + + ret = bpf_copy_from_user_str(data_long, sizeof(data_long), user_ptr, 2); + + if (ret != -EINVAL) + return false; + + return true; +} + SEC("uprobe.s//proc/self/exe:trigger_func3") int handle_uprobe_byname3_sleepable(struct pt_regs *ctx) { if (verify_sleepable_user_copy()) uprobe_byname3_sleepable_res = 9; + if (verify_sleepable_user_copy_str()) + uprobe_byname3_str_sleepable_res = 10; return 0; } @@ -102,7 +151,7 @@ int handle_uprobe_byname3_sleepable(struct pt_regs *ctx) SEC("uprobe//proc/self/exe:trigger_func3") int handle_uprobe_byname3(struct pt_regs *ctx) { - uprobe_byname3_res = 10; + uprobe_byname3_res = 11; return 0; } @@ -110,14 +159,16 @@ SEC("uretprobe.s//proc/self/exe:trigger_func3") int handle_uretprobe_byname3_sleepable(struct pt_regs *ctx) { if (verify_sleepable_user_copy()) - uretprobe_byname3_sleepable_res = 11; + uretprobe_byname3_sleepable_res = 12; + if (verify_sleepable_user_copy_str()) + uretprobe_byname3_str_sleepable_res = 13; return 0; } SEC("uretprobe//proc/self/exe:trigger_func3") int handle_uretprobe_byname3(struct pt_regs *ctx) { - uretprobe_byname3_res = 12; + uretprobe_byname3_res = 14; return 0; }