From patchwork Sat Jan 25 08:29:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Levi Zim via B4 Relay X-Patchwork-Id: 13950268 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6DC131B412B; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; cv=none; b=bFFisuAqMogvkMa6aq4SdhPAn5kCwJavXVE839gNvmgc9qd+H+mQO34KEd3F9CdsohTfk09jjspPqhjTGU/Awxmy8KNEI/0Ah6OstOC1KCqyLpzCnEvbEgOZ/h70EjKBJa2CRxRQTJXSgsr0Hwy+zAkcKvnWboQWeMv+S0hcufw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; c=relaxed/simple; bh=ZtcDKBYvpGO+GJwMItu+xm+YQJmprDGI/QwBss5wxAA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VPI6hByDefiS+hVQgylRLMy8joYRfMWZ7ixPcUTUvNl4fXmeDlsZMmgPvv4o+I08SoVvjTmz0AfjJkqdU90DQWJDsfSx6GRWbKwZSuv5cnIGAHXT0TJymcB0m196xe7qF3La/yACyctMs7aryIlwryNW5NasXTu0NnLP8LFHm/4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=URANgktB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="URANgktB" Received: by smtp.kernel.org (Postfix) with ESMTPS id EC8B4C4CEE0; Sat, 25 Jan 2025 08:30:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737793828; bh=ZtcDKBYvpGO+GJwMItu+xm+YQJmprDGI/QwBss5wxAA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=URANgktB1oVQ+aNEyysZu0+3FHDV26ajhREpZxBpD9UNGnWUrdOxkvrz+YQAYlLr+ /o++9NeN9jLq/evMvEf0W4kNHE5nQnsC2cRXEUcGMD51Lr8ycveNHXn/Ju4fkUwaJX sOcso6P8bIAm3meHs7Azo/YyGESKcQeZv9RgSxplWWXQmjWQwR5vbJx8ObmgIBcNgY lyejB7WCLQJcAmbPkamqCkH1lgwIUZGt3tFTmcl8qaS0Pi8OF3FlWJ5JJtWZlTm5pt S/hzCTNr3BBSlhprwGzrOkeA6NyNs6HDgZqCOe8HCl65ZiXt0ZYs+Cidl3kaBZBpln PtOmVMJZx/zDA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D824FC0218E; Sat, 25 Jan 2025 08:30:27 +0000 (UTC) From: Levi Zim via B4 Relay Date: Sat, 25 Jan 2025 16:29:48 +0800 Subject: [PATCH bpf-next v2 1/7] bpf: Implement bpf_probe_read_kernel_dynptr helper Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250125-bpf_dynptr_probe-v2-1-c42c87f97afe@outlook.com> References: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> In-Reply-To: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> To: 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 , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrii Nakryiko , Levi Zim X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6356; i=rsworktech@outlook.com; h=from:subject:message-id; bh=A7uHiAoA3YZYbVOxcHt/y8HP3bfI8O7Zg6oKJ3y0uAs=; b=owEBbQKS/ZANAwAIAW87mNQvxsnYAcsmYgBnlKEazmLRxSxN+99+LUcfUAXeS2U/PVRZTjaYH b1xE1uWQeuJAjMEAAEIAB0WIQQolnD5HDY18KF0JEVvO5jUL8bJ2AUCZ5ShGgAKCRBvO5jUL8bJ 2GW1EADFpjQlYxCyffskjdPxEJPfxhVA2uuid/BwoPyzoda7V25wj9O8JWKtaXbNZeE/i/v7nHd KvaU/n/9Li/xuWc5AsHxIZGOEb1QaoOMcn4BpejONG/kEpuy7E/+RRzjtV5LKadnSDt2sZO3qcn jb5yrGCcS3trVG1Hf9IkAC9vsCno8nrMt5o2OvkTszCBwOqWETmlMDwERuaD4g80sQT5oaNpJra NSKdyJUgozDwO1osWYeG183iCKo6Tw3YsGX5Rsjqb/GQ//H6SDyMpzRILDR1HC70w9TPYmtsmt3 WdkIWhbc/zd8Dj8y2AsTF1/nq/N/Aa1Izux8JkuxbPlHeR9OboIOPPxg74Nm9RiJ9pjuiIlgtT/ TeM16kYewqImFrlO8OO1ZhBCKm06S3ArOeZuNQHZcrxIxvctNgvG/sTna4CWAxUE9+/YllCqhZb IRWt/4tkMJXD5d2x5llMSLr4OmJbchDqWgAHWsUUDfcEKNGhOzGI6UrTMyUztV7qRk4rZLvzhkf AY9oQhnH0MQIcNLieFep2rU++GJ1TgvdZ2DGIvAnC+n59VgVMZ0vg/xeF6DW4vufP4jrPyw2v7i WevCUalflNsSDaObLDFDOwvrKLPrUOsF5Wx9KyE+bMiOdkmjgMUASyeTJpI1FQVjyljGui0vO6U ADBAMjwi0ICIt8A== X-Developer-Key: i=rsworktech@outlook.com; a=openpgp; fpr=17AADD6726DDC58B8EE5881757670CCFA42CCF0A X-Endpoint-Received: by B4 Relay for rsworktech@outlook.com/default with auth_id=219 X-Original-From: Levi Zim Reply-To: rsworktech@outlook.com From: Levi Zim This patch add a helper function bpf_probe_read_kernel_dynptr: long bpf_probe_read_kernel_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *unsafe_ptr, u64 flags); It is useful for reading variable-length data from kernel memory into dynptr. Link: https://lore.kernel.org/bpf/MEYP282MB2312CFCE5F7712FDE313215AC64D2@MEYP282MB2312.AUSP282.PROD.OUTLOOK.COM/ Signed-off-by: Levi Zim --- include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 16 ++++++++++++++++ kernel/bpf/helpers.c | 8 ++++++-- kernel/trace/bpf_trace.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f3f50e29d63929acaf12c81f8356173f1f5e154b..9d5ae8b4b7d82c4523bf0ab041d4b76bf134a106 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1323,6 +1323,8 @@ u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr); const void *__bpf_dynptr_data(const struct bpf_dynptr_kern *ptr, u32 len); void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len); bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr); +int bpf_dynptr_check_off_len(const struct bpf_dynptr_kern *ptr, u32 offset, u32 len); +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, diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 2acf9b33637174bd16b1d12ccc6410c5f55a7ea9..2e08a59527ecf56732ea14ac34446b5eb25b5690 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5805,6 +5805,21 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * long bpf_probe_read_kernel_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *unsafe_ptr, u64 flags) + * Description + * Safely attempt to read *size* bytes from kernel space address + * *unsafe_ptr* and store the data in *dst* starting from *offset*. + * *flags* is currently unused. + * Return + * 0 on success. + * + * **-E2BIG** if *offset* + *len* exceeds the length of *src*'s data + * + * **-EINVAL** if *src* is an invalid dynptr or doesn't support this + * support this helper, or if *flags* is not 0. + * + * Or other negative errors on failure reading kernel memory. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -6019,6 +6034,7 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(probe_read_kernel_dynptr, 212, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index f27ce162427ab4040d2e2d2eb84a883fe57de59e..a736dc9e7be98571103ba404420be0da4dac4fbe 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1678,7 +1678,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; } @@ -1714,7 +1714,7 @@ void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr) memset(ptr, 0, sizeof(*ptr)); } -static int bpf_dynptr_check_off_len(const struct bpf_dynptr_kern *ptr, u32 offset, u32 len) +int bpf_dynptr_check_off_len(const struct bpf_dynptr_kern *ptr, u32 offset, u32 len) { u32 size = __bpf_dynptr_size(ptr); @@ -1900,6 +1900,7 @@ const struct bpf_func_proto bpf_probe_read_user_proto __weak; const struct bpf_func_proto bpf_probe_read_user_str_proto __weak; const struct bpf_func_proto bpf_probe_read_kernel_proto __weak; const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak; +const struct bpf_func_proto bpf_probe_read_kernel_dynptr_proto __weak; const struct bpf_func_proto bpf_task_pt_regs_proto __weak; const struct bpf_func_proto * @@ -2031,6 +2032,9 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_probe_read_kernel: return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? NULL : &bpf_probe_read_kernel_proto; + case BPF_FUNC_probe_read_kernel_dynptr: + return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? + NULL : &bpf_probe_read_kernel_dynptr_proto; case BPF_FUNC_probe_read_user_str: return &bpf_probe_read_user_str_proto; case BPF_FUNC_probe_read_kernel_str: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index adc947587eb8132ebbd54778d2db937b3b8861de..75c9d1e8d04c3b8930ae81345f5586756ce8b5ec 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -248,6 +248,48 @@ const struct bpf_func_proto bpf_probe_read_kernel_proto = { .arg3_type = ARG_ANYTHING, }; +BPF_CALL_5(bpf_probe_read_kernel_dynptr, const struct bpf_dynptr_kern *, dst, + u32, offset, u32, size, void *, unsafe_ptr, u64, flags) +{ + enum bpf_dynptr_type type; + int err; + + if (!dst->data || __bpf_dynptr_is_rdonly(dst)) + return -EINVAL; + + err = bpf_dynptr_check_off_len(dst, offset, size); + if (err) + return err; + + type = bpf_dynptr_get_type(dst); + + switch (type) { + case BPF_DYNPTR_TYPE_LOCAL: + case BPF_DYNPTR_TYPE_RINGBUF: + if (flags) + return -EINVAL; + return bpf_probe_read_kernel_common(dst->data + dst->offset + offset, + size, unsafe_ptr); + case BPF_DYNPTR_TYPE_SKB: + case BPF_DYNPTR_TYPE_XDP: + return -EINVAL; + default: + WARN_ONCE(true, "%s: unknown dynptr type %d\n", __func__, type); + return -EFAULT; + } +} + +const struct bpf_func_proto bpf_probe_read_kernel_dynptr_proto = { + .func = bpf_probe_read_kernel_dynptr, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, + .arg2_type = ARG_ANYTHING, + .arg3_type = ARG_ANYTHING, + .arg4_type = ARG_ANYTHING, + .arg5_type = ARG_ANYTHING, +}; + static __always_inline int bpf_probe_read_kernel_str_common(void *dst, u32 size, const void *unsafe_ptr) { From patchwork Sat Jan 25 08:29:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Levi Zim via B4 Relay X-Patchwork-Id: 13950270 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6DCE9207665; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; cv=none; b=unGG2kLZxl3myS0DQQ64m7qz42JZlFpuYKokdRoTYb3Lz1JckCv2loEXwlmc6jV1OvcSHsqItEhN3HZLPm1Llqq+O7sWwqCsqQJOhyxQCHP38HpvCUQKX8Aey9ifHRzRCyIP1P2yoRZAbP8yuYISzREudy86Dp70MSQcTIGFG4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; c=relaxed/simple; bh=uVIhYSX/ojtZXVYzOH86j+THkc3nC0l4nRGJm4xvBxk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Yj0u1Xe4uM4z372ryX95CGKQH8qW3zEoJtxykoUtsWEPWqiVMMrhapkR3SbPh7JVZmM3PLnprf6X3vxUt0Gyny2zzgTHQyas4jbM8DC1IzRTh8HIPpIMb6Hr0h7ZOchFbaIz8oZYNnvgbfY+UsIiohJRFkb79+aKKxxehjb/lgc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qpipAMv8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qpipAMv8" Received: by smtp.kernel.org (Postfix) with ESMTPS id 1096FC4CEE8; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737793828; bh=uVIhYSX/ojtZXVYzOH86j+THkc3nC0l4nRGJm4xvBxk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=qpipAMv8P+TEEuRKrnzeO7oIT84H0CE65/4PGujbKgk5Yk08fVXPKDsnOwNZi7nGM ck5xf8pgLI8D67zObXfwuzngG1oZ3OapEGBg1mrvD4T8gZDC+9cSVnm+RcKmB55AGk av1nRy1lRAgq5S8Vmy3G8Sre3GsoMudNZf8Q4D8410Z/iXmVrrZuc2Y+qxHxg/5hPt XkwQ2jtD2XNX4BsGXqjn/i4dI1CuUcpZyJqE70be78DvVnf4j+A9ZfAjk8Pcse8oom ahbOuwzIvavnMYUI7k8ujrcupU7iYXiUoYhV5zkRV5PlEEt3+ksc/kKX1azucij08z RLypf9otBiFsw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01E66C0218C; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) From: Levi Zim via B4 Relay Date: Sat, 25 Jan 2025 16:29:49 +0800 Subject: [PATCH bpf-next v2 2/7] bpf: Implement bpf_probe_read_user_dynptr helper Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250125-bpf_dynptr_probe-v2-2-c42c87f97afe@outlook.com> References: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> In-Reply-To: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> To: 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 , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrii Nakryiko , Levi Zim X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6307; i=rsworktech@outlook.com; h=from:subject:message-id; bh=FYHnq6c7uuywFLvG8HAAKztCiF4RpinK4T4T/ey1n7A=; b=owEBbQKS/ZANAwAIAW87mNQvxsnYAcsmYgBnlKEbKfdh050oR+U5evUh4HhBuo56XWFUjuxJN ymCJMkFBoqJAjMEAAEIAB0WIQQolnD5HDY18KF0JEVvO5jUL8bJ2AUCZ5ShGwAKCRBvO5jUL8bJ 2A1sEAC1otgG8OUem9SzSh08jElKCXOBpvUV4ESS3PK+TA9yxDsHVte7EhHxeEzmUbtuZOTU/mA h2MEO5aHphgSApxuOzFJinUfHnnvYfU9600M8me9jgYkEcq11N1DeCp8yFTX75v0/2UQpAx8gtB gIIM67NdH2+NdyOPDGyxjYmRxrKu0YdKUaAYDtAVaMoTo+PrlvUnQ0zjl4WUwEeviBXgb7REcI/ BCjdm4tI81hyUmVGA3jeFziqAuDlE8OPfjUyMKvHDU8e/BTj2O8CtD0d5ALsz+dhPmiYILLP/et CQ7Ot/HPzxp9iDMeKK7uW9W5xCaA6qyf0fzAzSF14qzG7QwPGQf+E2IXf5ocIv1u/4KBM52UCKH LBaC2/9lzcXfTrliP7Yv+Phna37Tte6VL3NpcvfFXCfMYZBy8s9ZWWQ+dbkat4k4hWOMoirSPsZ xe851lFe0EzrAqYzJ4/8M8z5qCWglT22ECQ8aTqXKDrJtWxgHeKojFPk1Y+YO90FiAKGw71wZjV AYG8uECQE/wZmcCIIu/cyWJ5zbtGeaiVCWgK/VoxiNt+37w/dlxYp0m0HTVaZ8qnYOsW9oLDusb 9Xl822TPmG3x5yRlCu1KANaA/TIDxBNvRYEV7mo9SV7f3QwO1sX/T8sFAp+2H6lb+W2e8Wy7ERQ mAHbMTCDpb/8+0A== X-Developer-Key: i=rsworktech@outlook.com; a=openpgp; fpr=17AADD6726DDC58B8EE5881757670CCFA42CCF0A X-Endpoint-Received: by B4 Relay for rsworktech@outlook.com/default with auth_id=219 X-Original-From: Levi Zim Reply-To: rsworktech@outlook.com From: Levi Zim This patch add a helper function bpf_probe_read_user_dynptr: long bpf_probe_read_user_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *unsafe_ptr, u64 flags); It is useful for reading variable-length data from user memory into dynptr. Signed-off-by: Levi Zim --- include/uapi/linux/bpf.h | 16 ++++++++++ kernel/bpf/helpers.c | 3 ++ kernel/trace/bpf_trace.c | 76 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 2e08a59527ecf56732ea14ac34446b5eb25b5690..d7d7a9ddd5dca07ba89d81ba77101a704af3163b 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5820,6 +5820,21 @@ union bpf_attr { * support this helper, or if *flags* is not 0. * * Or other negative errors on failure reading kernel memory. + * + * long bpf_probe_read_user_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *unsafe_ptr, u64 flags) + * Description + * Safely attempt to read *size* bytes from user space address + * *unsafe_ptr* and store the data in *dst* starting from *offset*. + * *flags* is currently unused. + * Return + * 0 on success. + * + * **-E2BIG** if *offset* + *len* exceeds the length of *src*'s data + * + * **-EINVAL** if *src* is an invalid dynptr or doesn't support this + * support this helper, or if *flags* is not 0. + * + * Or other negative errors on failure reading user space memory. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -6035,6 +6050,7 @@ union bpf_attr { FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ FN(probe_read_kernel_dynptr, 212, ##ctx) \ + FN(probe_read_user_dynptr, 213, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index a736dc9e7be98571103ba404420be0da4dac4fbe..ac563d09082e7c721999d7de035aabc000206a29 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1898,6 +1898,7 @@ const struct bpf_func_proto bpf_get_current_task_proto __weak; const struct bpf_func_proto bpf_get_current_task_btf_proto __weak; const struct bpf_func_proto bpf_probe_read_user_proto __weak; const struct bpf_func_proto bpf_probe_read_user_str_proto __weak; +const struct bpf_func_proto bpf_probe_read_user_dynptr_proto __weak; const struct bpf_func_proto bpf_probe_read_kernel_proto __weak; const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak; const struct bpf_func_proto bpf_probe_read_kernel_dynptr_proto __weak; @@ -2029,6 +2030,8 @@ bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_get_current_task_btf_proto; case BPF_FUNC_probe_read_user: return &bpf_probe_read_user_proto; + case BPF_FUNC_probe_read_user_dynptr: + return &bpf_probe_read_user_dynptr_proto; case BPF_FUNC_probe_read_kernel: return security_locked_down(LOCKDOWN_BPF_READ_KERNEL) < 0 ? NULL : &bpf_probe_read_kernel_proto; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 75c9d1e8d04c3b8930ae81345f5586756ce8b5ec..d9f704c1342773c74b2414be4adfc8271d6d364d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -181,6 +181,36 @@ bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr) return ret; } +static int bpf_probe_read_check_dynptr(const struct bpf_dynptr_kern *dst, + u32 offset, u32 size, u64 flags) +{ + enum bpf_dynptr_type type; + int err; + + if (!dst->data || __bpf_dynptr_is_rdonly(dst)) + return -EINVAL; + + err = bpf_dynptr_check_off_len(dst, offset, size); + if (err) + return err; + + type = bpf_dynptr_get_type(dst); + + switch (type) { + case BPF_DYNPTR_TYPE_LOCAL: + case BPF_DYNPTR_TYPE_RINGBUF: + if (flags) + return -EINVAL; + return 0; + case BPF_DYNPTR_TYPE_SKB: + case BPF_DYNPTR_TYPE_XDP: + return -EINVAL; + default: + WARN_ONCE(true, "%s: unknown dynptr type %d\n", __func__, type); + return -EFAULT; + } +} + BPF_CALL_3(bpf_probe_read_user, void *, dst, u32, size, const void __user *, unsafe_ptr) { @@ -196,6 +226,26 @@ const struct bpf_func_proto bpf_probe_read_user_proto = { .arg3_type = ARG_ANYTHING, }; +BPF_CALL_5(bpf_probe_read_user_dynptr, const struct bpf_dynptr_kern *, dst, + u32, offset, u32, size, void *, unsafe_ptr, u64, flags) +{ + int ret = bpf_probe_read_check_dynptr(dst, offset, size, flags); + + return ret ?: bpf_probe_read_user_common(dst->data + dst->offset + offset, + size, unsafe_ptr); +} + +const struct bpf_func_proto bpf_probe_read_user_dynptr_proto = { + .func = bpf_probe_read_user_dynptr, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, + .arg2_type = ARG_ANYTHING, + .arg3_type = ARG_ANYTHING, + .arg4_type = ARG_ANYTHING, + .arg5_type = ARG_ANYTHING, +}; + static __always_inline int bpf_probe_read_user_str_common(void *dst, u32 size, const void __user *unsafe_ptr) @@ -251,32 +301,10 @@ const struct bpf_func_proto bpf_probe_read_kernel_proto = { BPF_CALL_5(bpf_probe_read_kernel_dynptr, const struct bpf_dynptr_kern *, dst, u32, offset, u32, size, void *, unsafe_ptr, u64, flags) { - enum bpf_dynptr_type type; - int err; - - if (!dst->data || __bpf_dynptr_is_rdonly(dst)) - return -EINVAL; + int ret = bpf_probe_read_check_dynptr(dst, offset, size, flags); - err = bpf_dynptr_check_off_len(dst, offset, size); - if (err) - return err; - - type = bpf_dynptr_get_type(dst); - - switch (type) { - case BPF_DYNPTR_TYPE_LOCAL: - case BPF_DYNPTR_TYPE_RINGBUF: - if (flags) - return -EINVAL; - return bpf_probe_read_kernel_common(dst->data + dst->offset + offset, + return ret ?: bpf_probe_read_kernel_common(dst->data + dst->offset + offset, size, unsafe_ptr); - case BPF_DYNPTR_TYPE_SKB: - case BPF_DYNPTR_TYPE_XDP: - return -EINVAL; - default: - WARN_ONCE(true, "%s: unknown dynptr type %d\n", __func__, type); - return -EFAULT; - } } const struct bpf_func_proto bpf_probe_read_kernel_dynptr_proto = { From patchwork Sat Jan 25 08:29:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Levi Zim via B4 Relay X-Patchwork-Id: 13950269 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6DC86206F21; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; cv=none; b=C71S1W3oEKWlssVxgjlNd7DRzUiUqhDJO+tKFdkUChpjCUur5SkFVCDOiMgy88cHZfIKjWqm0ZknxKjETv/KWPOQvVRqszEk9qvAruEE9rVunDbGzfiXwzAjtmZKNYgLvsd2hlul334tKtkR+II5r+ohyFCOY5wRbou1sjPmZMI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; c=relaxed/simple; bh=PyRhaQiSnZLjeNlZTzgwUCTGU9Zv+fo1gZ0pY6cwY5A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MQ4AeGC4mt40v0KmGe/Zjlk4R/vxrzLO+s+DoDalgA/GOFMZ2TZ5y0NEH0KRJrIboz0Av3dxm2ZTmU/AA99zT7qEGxd9witxHWl3vs6Lp4xXXDkYJnDKXI6RO4zupa0Ls0H2OkrPT0zpNNMtEuxKf6U+Xo0dap1PjJYMKjHrWGI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E9PlLrUp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="E9PlLrUp" Received: by smtp.kernel.org (Postfix) with ESMTPS id 28110C4CEEB; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737793828; bh=PyRhaQiSnZLjeNlZTzgwUCTGU9Zv+fo1gZ0pY6cwY5A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=E9PlLrUpOgzuaiemYFGgsHCq71K44WdedOABpmUvE0T2oaXJjYZQ7kqs3Hqeoo91Y egXzmza/s0NjuoBNGzwqjtZf8hVTYqz+b0+1K7iCCcU/uG7gIbwN2slNIAqc07oNo0 eJGauI24yOucx8H9hAm1dOhuLRfipKqyWHaHZP5QwmXlJ5yPyX7hz8RxQ3fEdjkz4P 6sR16xicHtVcv1r398AV2o1xpFMx0+deacOxH++tcVCWFSVVJ7N2PG8zVJwUVTUZgU 5Eme/f5GLTY8nvnRbi74Pv9/q+mtRUtBVutviCZn41BxMq6F9YYsO4IxGxl0hib9w8 SUkiDeKbzGxRQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BA73C0218D; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) From: Levi Zim via B4 Relay Date: Sat, 25 Jan 2025 16:29:50 +0800 Subject: [PATCH bpf-next v2 3/7] bpf: Implement bpf_copy_from_user_dynptr helper Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250125-bpf_dynptr_probe-v2-3-c42c87f97afe@outlook.com> References: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> In-Reply-To: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> To: 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 , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrii Nakryiko , Levi Zim X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4919; i=rsworktech@outlook.com; h=from:subject:message-id; bh=PbsS7Vpr6USg1jk+KBsoBiOuzmKNN5IGam682v3+Quo=; b=owEBbQKS/ZANAwAIAW87mNQvxsnYAcsmYgBnlKEcWhxtdmFMeS8e/j2IAS0TMUqjsi0v1GWCl kfWJnx3yfKJAjMEAAEIAB0WIQQolnD5HDY18KF0JEVvO5jUL8bJ2AUCZ5ShHAAKCRBvO5jUL8bJ 2NIOEACF2vYKdtugfPkki7eYCvJ3omVjfA4rb1wl39Hq/qNTHLWNz+/Gvhm2ZrdyAaLaDrMWbFw 1Yhz9cbOPBTTCnaouVLGUHY6rFse1euFPZWduj1RCzNsdvsXHOyfGH0J6BMeXEMUeWNYPx3ttEP VpVe+XlWkhR3VHfLZvafAdgpjs/4NWstbC5AfFrrVxddA3e7zzlYqWX3nSx7Pz0aj5I+FVyYGL7 AH+/k4kScAiW0Zg3ztHF4KT9Zs4+j5kOuEGVh5PbWKMtWAucK7OPy+Ex37P7xZYBRBT3KbOGVsx zJMsn13Hz1DMZRaoZYs058VWLUm03dXkQe1SyrjFmRlURNdmSWiEl8CiYF5iUUK2Q0viniy1+0E AENGF5oWU+MXGtXZrZrJJmqZIOyvxsD7yeuVRoraqzkzNEFTFUVd/YUXBOYha+pNmDnpQQmvTJW WrYfhYiZvMlbb5r4hVCPcpcC/9UDqEg04O1pHgDCqqdgTNwheZ37i8NdNMeo+5fAtX5Zsz5bSN2 kXq49mID7+PqiJNGMGD5JKj3vn/NVEl0oRAwx/DVx+WUD85bbbn0nYBo6bABdRJHjwvDf60/iNY sCxSUOlPs398GNnM+ztvdA8dtpasLLWmiGJXczA1njfLrozvjSNG65Ioi5jexf+mPPggBi60hVj aDVlGDi7FYxYZpw== X-Developer-Key: i=rsworktech@outlook.com; a=openpgp; fpr=17AADD6726DDC58B8EE5881757670CCFA42CCF0A X-Endpoint-Received: by B4 Relay for rsworktech@outlook.com/default with auth_id=219 X-Original-From: Levi Zim Reply-To: rsworktech@outlook.com From: Levi Zim This patch add a helper function bpf_copy_from_user_dynptr: bpf_copy_from_user_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *user_ptr, u64 flags) It is useful for reading variable-length data from kernel memory into dynptr. Signed-off-by: Levi Zim --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 17 +++++++++++++++++ kernel/bpf/helpers.c | 42 ++++++++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 4 files changed, 62 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 9d5ae8b4b7d82c4523bf0ab041d4b76bf134a106..d0412eaf63d69c0e437575c77008548edc692335 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3357,6 +3357,7 @@ extern const struct bpf_func_proto bpf_get_retval_proto; extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto; extern const struct bpf_func_proto bpf_cgrp_storage_get_proto; extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto; +extern const struct bpf_func_proto bpf_copy_from_user_dynptr_proto; const struct bpf_func_proto *tracing_prog_func_proto( enum bpf_func_id func_id, const struct bpf_prog *prog); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index d7d7a9ddd5dca07ba89d81ba77101a704af3163b..f92cf809b50bc393d54eb0e8de2e1ce2a39e95d0 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5835,6 +5835,22 @@ union bpf_attr { * support this helper, or if *flags* is not 0. * * Or other negative errors on failure reading user space memory. + * + * long bpf_copy_from_user_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *user_ptr, u64 flags) + * Description + * Read *size* bytes from user space address *user_ptr* and store + * the data in *dst* starting from *offset*. + * This is a wrapper of **copy_from_user**\ (). + * *flags* is currently unused. + * Return + * 0 on success. + * + * **-E2BIG** if *offset* + *len* exceeds the length of *src*'s data + * + * **-EINVAL** if *src* is an invalid dynptr or doesn't support this + * support this helper, or if *flags* is not 0. + * + * Or other negative errors on failure reading user space memory. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -6051,6 +6067,7 @@ union bpf_attr { FN(cgrp_storage_delete, 211, ##ctx) \ FN(probe_read_kernel_dynptr, 212, ##ctx) \ FN(probe_read_user_dynptr, 213, ##ctx) \ + FN(copy_from_user_dynptr, 214, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index ac563d09082e7c721999d7de035aabc000206a29..d756c80596315bd07fe6e71885b61efc8cb2ef4f 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -676,6 +676,48 @@ const struct bpf_func_proto bpf_copy_from_user_proto = { .arg3_type = ARG_ANYTHING, }; +BPF_CALL_5(bpf_copy_from_user_dynptr, const struct bpf_dynptr_kern *, dst, + u32, offset, u32, size, const void __user *, user_ptr, u32, flags) +{ + enum bpf_dynptr_type type; + int err; + + if (!dst->data || __bpf_dynptr_is_rdonly(dst)) + return -EINVAL; + + err = bpf_dynptr_check_off_len(dst, offset, size); + if (err) + return err; + + type = bpf_dynptr_get_type(dst); + + switch (type) { + case BPF_DYNPTR_TYPE_LOCAL: + case BPF_DYNPTR_TYPE_RINGBUF: + if (flags) + return -EINVAL; + return ____bpf_copy_from_user(dst->data + dst->offset + offset, size, user_ptr); + case BPF_DYNPTR_TYPE_SKB: + case BPF_DYNPTR_TYPE_XDP: + return -EINVAL; + default: + WARN_ONCE(true, "%s: unknown dynptr type %d\n", __func__, type); + return -EFAULT; + } +} + +const struct bpf_func_proto bpf_copy_from_user_dynptr_proto = { + .func = bpf_copy_from_user_dynptr, + .gpl_only = false, + .might_sleep = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_DYNPTR | MEM_RDONLY, + .arg2_type = ARG_ANYTHING, + .arg3_type = ARG_ANYTHING, + .arg4_type = ARG_ANYTHING, + .arg5_type = ARG_ANYTHING, +}; + BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size, const void __user *, user_ptr, struct task_struct *, tsk, u64, flags) { diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index d9f704c1342773c74b2414be4adfc8271d6d364d..424931925fe3b02db083bc19cc64e19918b40c5a 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1598,6 +1598,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_copy_from_user_proto; case BPF_FUNC_copy_from_user_task: return &bpf_copy_from_user_task_proto; + case BPF_FUNC_copy_from_user_dynptr: + return &bpf_copy_from_user_dynptr_proto; case BPF_FUNC_snprintf_btf: return &bpf_snprintf_btf_proto; case BPF_FUNC_per_cpu_ptr: From patchwork Sat Jan 25 08:29:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Levi Zim via B4 Relay X-Patchwork-Id: 13950271 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8159620766B; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; cv=none; b=bu6v94yN9/cNtaB0BzjUC32xakHY6mXZzBlh04jl7+qlY16+WQkrQqzXtmfFXH35VMwqHGCgBXUCohndsQTl4XhAWb0qQ6uydAaBTEcMnbi9Mcjv+Ljme1HDdOqbj1TLH9XDoBwqpBGHpk1BvmdcYkBB1xrMAtDDQrQ6HKgVULo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; c=relaxed/simple; bh=5C36o9IdjmfPRwlDKtDMgiQdJZRcjCq90w+nxcWONik=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VgZofcNMYQCR7GgJbSaKy5GUPnSHvkaEffByPrGFhQdsj5qiLvu1b4OK7fOCOLQ2Bv8LMA5TqO2e1yYHhVfXfU77ASocLrNygaUr1aaboGpZxVlErZ9WTYpzeA/SpNd/GEtjHguh4AenisL7FYFV7nV5o50ABwXeSoUyA4OhbOk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NlWm/2Dm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NlWm/2Dm" Received: by smtp.kernel.org (Postfix) with ESMTPS id 3E924C4CEF2; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737793828; bh=5C36o9IdjmfPRwlDKtDMgiQdJZRcjCq90w+nxcWONik=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=NlWm/2DmuVWdQ18rbIY9mDs4OPM1lo5PWMac5ReTNP7qOEvk7JVSGZGjBQQbeXnfQ y4lGZxk2iiMceVoKW3IozWbJul6dGjCqb9wF9PGSLJg3FNLIFGHPe8eYgCf7e3Gi17 lRyBUFspfj5LG2zzKJ7WjXUlrEDD9FrerQWuHX+ZzdM7rn3R27kidKlo8Vnvpea3v9 HQcuce4wbMXvhSCFLTBeZCnXY3Z/yJoy514JOdAnpRCY0Aal+B6NN8FQVWYgrsKU2B oNA60CxX93XEdftedf76XM9FF1Fzr9RThVNKLLTNDvxrFMO1XIiHyCzQpb1V2mwJWH gX8mIBhjULTCw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33D93C0218C; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) From: Levi Zim via B4 Relay Date: Sat, 25 Jan 2025 16:29:51 +0800 Subject: [PATCH bpf-next v2 4/7] tools headers UAPI: Update tools's copy of bpf.h header Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250125-bpf_dynptr_probe-v2-4-c42c87f97afe@outlook.com> References: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> In-Reply-To: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> To: 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 , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrii Nakryiko , Levi Zim X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3015; i=rsworktech@outlook.com; h=from:subject:message-id; bh=3gnnXYcyukxFoJ6/k3O/B5KiBqxGbCIQ+tpkC1HU5hk=; b=owEBbQKS/ZANAwAIAW87mNQvxsnYAcsmYgBnlKEdTLRZndyUKLBQC5UPoctezuIHnutTGt7GT utgw8pM5luJAjMEAAEIAB0WIQQolnD5HDY18KF0JEVvO5jUL8bJ2AUCZ5ShHQAKCRBvO5jUL8bJ 2A+6EADZl1H4gh/cvrYU14Wbd78OgiD/6umqDHAIrJus+g9gUOiP66bWd91DB27W9DPbhbWlObX hg7aLBoel6BQcWIGWHBpK3urFTFQFtFF7xA0l086PvtYiImRkCx8EYOwp71HP4O/Ggnslbn+V8H 84ba7oDLd8DxMX/+sNS+WFfrxKUMtAwQ+kRfeNMSSRuvzLfSIwgi5Q9sGE7mJJq9Es/ZyyhFI5x NGBsqBGJoNPlz8yTxYtbROq5MqiLaJoEWW0R+mMZMA+XBmg/pg9nvZLa0BgH00BkLQ1DDMs1GL7 VJ6GyH76ToFTez/Z51UYx1wbjMR4hj3lJ98pVfQ17b9VdmIYR1LrnIrZsuvSQyeOW0t1KB/pGex imb/79lOC1bGBY+agLt7eOp6rO08WcQEohNukAx+bRVbmnj4heTfpUEzEXNT3EEYSHJpc8y0shf +Cf7IE9sNI8Mg/xKwEdsxJMSUrUqwIgQ7/XauuSK8CZHKTn5LtrX7o7gRDybF6nmUSl99zKoZYk dmZJzazxCdTCB6no1uAEZw+RxIPv197cchbv0/y3qdkThDS9unoHySIJTYOCretYZnc6ejxdAvn LgJQ7+OZHbk8B6VdntqoRL/ED6CcWIJuIhv4cJXq2FwlSA/tiefZziggN4sYq1qtey0CUnmGmML I/djjzhF7VE5ZQg== X-Developer-Key: i=rsworktech@outlook.com; a=openpgp; fpr=17AADD6726DDC58B8EE5881757670CCFA42CCF0A X-Endpoint-Received: by B4 Relay for rsworktech@outlook.com/default with auth_id=219 X-Original-From: Levi Zim Reply-To: rsworktech@outlook.com From: Levi Zim This update brings the new bpf_probe_read_{kernel,user} helpers Signed-off-by: Levi Zim --- tools/include/uapi/linux/bpf.h | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 2acf9b33637174bd16b1d12ccc6410c5f55a7ea9..f92cf809b50bc393d54eb0e8de2e1ce2a39e95d0 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5805,6 +5805,52 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * long bpf_probe_read_kernel_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *unsafe_ptr, u64 flags) + * Description + * Safely attempt to read *size* bytes from kernel space address + * *unsafe_ptr* and store the data in *dst* starting from *offset*. + * *flags* is currently unused. + * Return + * 0 on success. + * + * **-E2BIG** if *offset* + *len* exceeds the length of *src*'s data + * + * **-EINVAL** if *src* is an invalid dynptr or doesn't support this + * support this helper, or if *flags* is not 0. + * + * Or other negative errors on failure reading kernel memory. + * + * long bpf_probe_read_user_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *unsafe_ptr, u64 flags) + * Description + * Safely attempt to read *size* bytes from user space address + * *unsafe_ptr* and store the data in *dst* starting from *offset*. + * *flags* is currently unused. + * Return + * 0 on success. + * + * **-E2BIG** if *offset* + *len* exceeds the length of *src*'s data + * + * **-EINVAL** if *src* is an invalid dynptr or doesn't support this + * support this helper, or if *flags* is not 0. + * + * Or other negative errors on failure reading user space memory. + * + * long bpf_copy_from_user_dynptr(const struct bpf_dynptr *dst, u32 offset, u32 size, const void *user_ptr, u64 flags) + * Description + * Read *size* bytes from user space address *user_ptr* and store + * the data in *dst* starting from *offset*. + * This is a wrapper of **copy_from_user**\ (). + * *flags* is currently unused. + * Return + * 0 on success. + * + * **-E2BIG** if *offset* + *len* exceeds the length of *src*'s data + * + * **-EINVAL** if *src* is an invalid dynptr or doesn't support this + * support this helper, or if *flags* is not 0. + * + * Or other negative errors on failure reading user space memory. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -6019,6 +6065,9 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(probe_read_kernel_dynptr, 212, ##ctx) \ + FN(probe_read_user_dynptr, 213, ##ctx) \ + FN(copy_from_user_dynptr, 214, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't From patchwork Sat Jan 25 08:29:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Levi Zim via B4 Relay X-Patchwork-Id: 13950272 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A1BFB207E05; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; cv=none; b=S4lXU3Cn9Q5kFue7q7bW0qR8lEaU/SzneLrPYI/sRueh2KRs8HyBTFxATgFiAnGHCNfTRAiuUTVFsBtHE6ulU6F3Q6NAVfMD9b8yOBx+UTAN0l30hAJVqo9ZUJrKd6o7E30ZDKkrtuKYkGAn7TYwRXFdo1Po50B+cE+6dtAH/S0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; c=relaxed/simple; bh=dmQ9GUwd3bRLMUoNF6xVMku1hiH2jSDQcoM3zSlQido=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PoPS8poqyT0rhSFX9RoH8MVnw5Ia2yE2Tpcccj7JRHPKKGNWtvhs9zmAcm0Zq8I1U0UQkrUMhvw4+HlCra6/4eFH+TU3M5f/EXCLtwukxjpzOArHhmPxk4ORs1jMOeNF8qlBoNF7aldkxQAGVrQNqBz4irEL+c6V6WcZKfw2SdQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NRvR0hwN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NRvR0hwN" Received: by smtp.kernel.org (Postfix) with ESMTPS id 6A3F6C4CEE2; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737793828; bh=dmQ9GUwd3bRLMUoNF6xVMku1hiH2jSDQcoM3zSlQido=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=NRvR0hwNTc/uPuzrE6m2SSWT74h1SfaKA0CO0F7OuxbSlgxnKdAws3jvEqahQiZ3D 3wwVUOUVafCUDH6dj/r7c4806ozKuIEOt7K+knhchlSoaXIVrfvY9OJTFk3haD9QL2 GLJoqXPqXAQ8G0tOLCyVovPfLQwPKsT6IyxGcJe4JEyD19uyAfDe9ZGgcW0bXLBRm+ pw/dIBogq0LLnMgudc+FqA/zI6mad/Lvbpd5ObYmLDxoT/79rkoxShe3EGR6CkTg2G TKU7273M68yplx9x41OVltrj6v0dcEiCgF6wt7I7ykfIpvPMbIb8BYkPDIrDO56TAR Az11z1rhU7PmQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F104C0218C; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) From: Levi Zim via B4 Relay Date: Sat, 25 Jan 2025 16:29:52 +0800 Subject: [PATCH bpf-next v2 5/7] selftests/bpf: probe_read_kernel_dynptr test Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250125-bpf_dynptr_probe-v2-5-c42c87f97afe@outlook.com> References: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> In-Reply-To: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> To: 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 , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrii Nakryiko , Levi Zim X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4155; i=rsworktech@outlook.com; h=from:subject:message-id; bh=jMKGNsysp1i//CbcuLXLYuHvxw7b8AHz8NxNTWOsSWc=; b=owEBbQKS/ZANAwAIAW87mNQvxsnYAcsmYgBnlKEebIa/vdUWN/NFRJtaRaVBLp95tqfHpvpwt gRc+8Q7cTiJAjMEAAEIAB0WIQQolnD5HDY18KF0JEVvO5jUL8bJ2AUCZ5ShHgAKCRBvO5jUL8bJ 2LYSD/0Z1DDJMbkTcZqAtRPfq1iGI689fS15jDzjiy16/NUuHxvBVh67b1e+1c1ErgKMZEv0lt2 CFT8y+sPEyBW5aG5QGfOcaYt95UHf9m2JL1hCTIf/b7cy2F/Yxzm0SqZcmoaTmKt4Kn0EYLwJ6z 1lAsiagfVeW1prQXMdpduTJS8UoaRqm9igl++RTk2Tveo5TmYuCmdU/2ZyMvYWss/ivHlM0+S/7 nQSFxe+fFli7HPnwHDA95zh216J/Js8eLKVgAqQUxZQrl+kMfVuA62nErLvuZK4wCXci0yubh7w YhhOVv4B0jq+VzCFfpILH0GdfuwdQb4P90mfHI/kkI0dya1BUta1Vv3u++s0YOCzeUVWFl8hMvd Quzrj0dqHHKRY1M/ZIaulNLG7pmSpOG6bUNb3SU5dXELJShZvVXB24Xu7vNscRi8F8razWZtVxj K0k1n/SbgyDDcnYishDAFyNV6cPwDNsoOwSsj3kkTFTlx4PPeu3K28ASyi1Xrzp4c789iyIw1Qn jF93mrSUAjwlGGmkiW7o88gqje6Yi4HYWqRkDkP8w+F2cdS+Pr+gLuu8po0ydYmdTjvmLItu+Dt plFGp/BViH8lfB4kLbcopVFGCA2I8w6qMhhEoJL+PioyK94iU2aqfI0E6xrtVLmlsitAAslAlXL vfr1wRQqFdtqBxA== X-Developer-Key: i=rsworktech@outlook.com; a=openpgp; fpr=17AADD6726DDC58B8EE5881757670CCFA42CCF0A X-Endpoint-Received: by B4 Relay for rsworktech@outlook.com/default with auth_id=219 X-Original-From: Levi Zim Reply-To: rsworktech@outlook.com From: Levi Zim This patch adds a test for probe_read_kernel_dynptr helper, which tests reading variable length of kernel memory into a ringbuf backed dynptr. Signed-off-by: Levi Zim --- tools/testing/selftests/bpf/prog_tests/dynptr.c | 40 +++++++++++++++++++++- tools/testing/selftests/bpf/progs/dynptr_success.c | 38 ++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c index b614a5272dfd6486e287181270a0bcf63f638344..d9a25c2873b6ed4219e063845b1caf978a7016fd 100644 --- a/tools/testing/selftests/bpf/prog_tests/dynptr.c +++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c @@ -10,6 +10,7 @@ enum test_setup_type { SETUP_SYSCALL_SLEEP, SETUP_SKB_PROG, SETUP_SKB_PROG_TP, + SETUP_RINGBUF, }; static struct { @@ -30,20 +31,37 @@ static struct { {"test_dynptr_skb_no_buff", SETUP_SKB_PROG}, {"test_dynptr_skb_strcmp", SETUP_SKB_PROG}, {"test_dynptr_skb_tp_btf", SETUP_SKB_PROG_TP}, + {"test_probe_read_kernel_dynptr", SETUP_RINGBUF}, }; +static int ringbuf_cb(void *ctx, void *data, size_t len) +{ + struct dynptr_success *skel = ctx; + const char *buf = data; + + if (!ASSERT_EQ(len, skel->data->test_buf.length, "length")) + return -E2BIG; + + if (!ASSERT_MEMEQ(buf, skel->data->test_buf.buf, len, "ringbuf_cb")) + return -EINVAL; + + return 0; +} + static void verify_success(const char *prog_name, enum test_setup_type setup_type) { + struct ring_buffer *rb = NULL; struct dynptr_success *skel; struct bpf_program *prog; struct bpf_link *link; - int err; + int err, ret; skel = dynptr_success__open(); if (!ASSERT_OK_PTR(skel, "dynptr_success__open")) return; skel->bss->pid = getpid(); + skel->data->test_buf.length = 8; prog = bpf_object__find_program_by_name(skel->obj, prog_name); if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) @@ -63,6 +81,24 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ usleep(1); + bpf_link__destroy(link); + break; + case SETUP_RINGBUF: + link = bpf_program__attach(prog); + if (!ASSERT_OK_PTR(link, "bpf_program__attach")) + goto cleanup; + + rb = ring_buffer__new(bpf_map__fd(skel->maps.ringbuf), ringbuf_cb, skel, NULL); + if (!ASSERT_OK_PTR(rb, "ring_buffer__new")) + goto cleanup; + + usleep(1); + + ret = ring_buffer__poll(rb, 5000); + + if (!ASSERT_EQ(ret, 1, "ring_buffer__poll")) + goto cleanup; + bpf_link__destroy(link); break; case SETUP_SKB_PROG: @@ -125,6 +161,8 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ ASSERT_EQ(skel->bss->err, 0, "err"); cleanup: + if (rb != NULL) + ring_buffer__free(rb); dynptr_success__destroy(skel); } diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c index bfcc85686cf046361b451f97f4cd310a6ccdb1ed..64c698f83a37bfe924db93d36982a0a1c8defe62 100644 --- a/tools/testing/selftests/bpf/progs/dynptr_success.c +++ b/tools/testing/selftests/bpf/progs/dynptr_success.c @@ -567,3 +567,41 @@ int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location) return 1; } + +#define MAX_BUFFER_LEN 20 + +struct { + __u32 length; + char buf[MAX_BUFFER_LEN]; +} test_buf = { + .length = 0, + .buf = "0123456789abcdef", +}; + +SEC("?tp/syscalls/sys_enter_nanosleep") +int test_probe_read_kernel_dynptr(void *ctx) +{ + int copy_len = test_buf.length; + struct bpf_dynptr ptr; + + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 0; + + if (test_buf.length > MAX_BUFFER_LEN) + copy_len = MAX_BUFFER_LEN; + + bpf_ringbuf_reserve_dynptr(&ringbuf, copy_len, 0, &ptr); + + if (-E2BIG != bpf_probe_read_kernel_dynptr(&ptr, 0, MAX_BUFFER_LEN + 1, + test_buf.buf, 0)) { + err = 1; + goto cleanup; + } + + err = bpf_probe_read_kernel_dynptr(&ptr, 0, copy_len, + test_buf.buf, 0); + +cleanup: + bpf_ringbuf_submit_dynptr(&ptr, 0); + return 0; +} From patchwork Sat Jan 25 08:29:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Levi Zim via B4 Relay X-Patchwork-Id: 13950274 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 CC8812080CD; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; cv=none; b=WJf73CmCfoXEIY8Qq90ZWxNlN3qnnFpJYJzxhfQQ2mDKdxGnhX8RLBUwGrvAHt8dsCzRUHv25gdf3Brhzfp0z6lqsupyEFCI9yo8m/UGhVsV7dnMc6kdn1Sy1cXAMciT1khlnuaI8z5x5lZSF30JsIo4qT/5Ow4K4L5hjwPWCes= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; c=relaxed/simple; bh=qK9Zcz+XRzuDr8brWcnUUV33yoq3f8IlQjYGMYirx9Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BA8cbX4XsF0yTQpB1ckzc+OmxfmzjvUn+LuYcwWM1VuOG17bZfwAy6T5y9lqX0/v+LyAUqZCwrTBzb89+QQXfqBfgSqqwrpYnStYPK3qkrBqXw2QVWajfbYVH12CkfWFjn51aTSeWKOef02BIquCPh8v8C9aif+xYjmP2bcUkwU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AQyAdmPv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AQyAdmPv" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7AC5FC4CEE3; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737793828; bh=qK9Zcz+XRzuDr8brWcnUUV33yoq3f8IlQjYGMYirx9Q=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=AQyAdmPvLoaFsnnrKFxRTdWOfbrKydHo5QgcU8Pnytbn413UsNIPaH1fAkkUXHVt+ qw0YSIjIL9JkkAxN5XpjIRwrU6p7WZPmsUcWBqNwQMhTUl+gj6tNrvlln5BJoPhLo8 H55qowC3ociwCh4G5rN15RG/Kn4YPMF2c6V4OIYo1sCnHkNnlloxktr9nUuFv0+QMb gMFHIMauZVax1bT2a6pjoMB9tTUJdgycPr6dVp1Ag2zr01KNTrC1c6ipgmgUnbLr06 GzizsFnupKrPeujp0LodA4y+R/dwEvMVaYAHwJmjT1TyWFIUIhQ4EafGKwAMeKQurK 70cI2mIujZzvw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7232CC0218E; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) From: Levi Zim via B4 Relay Date: Sat, 25 Jan 2025 16:29:53 +0800 Subject: [PATCH bpf-next v2 6/7] selftests/bpf: probe_read_user_dynptr test Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250125-bpf_dynptr_probe-v2-6-c42c87f97afe@outlook.com> References: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> In-Reply-To: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> To: 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 , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrii Nakryiko , Levi Zim X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2767; i=rsworktech@outlook.com; h=from:subject:message-id; bh=a7T0+YDmmVckpfX1xozTGLaOH/fP9fHtnj0E/W62eQI=; b=owEBbQKS/ZANAwAIAW87mNQvxsnYAcsmYgBnlKEfAOcRNdu0889uV0dS4IR7bhsKGqAOyiYfH IuRPqRtSruJAjMEAAEIAB0WIQQolnD5HDY18KF0JEVvO5jUL8bJ2AUCZ5ShHwAKCRBvO5jUL8bJ 2OwKEADCDlStXX4FB6YLwyDfvgHougx4GtOI9uKC7Xm0hR+/+jabe4ha6gPfYGCPe+ua6r4Q58K tp59EWI0nq+4mwQmdQiwJVqfRE1K909Lif9ddMqOajq5PPj9+zSxhi9IgIe9DVdyScFdk7QJtj9 venxOJAK7tNVOIHYkxkbk4SZFLF5omg2wwDSuM5L5kQGZqZ3GkeY9z0AO6V7IkcwsEE2dwCTAtB jjkyYYIsyxWZYIOew/N3iSpUUcPmvz8uE9zK7sFbxfbN/4OOMSBKoSoNB+O/X69utwo+FxSpbWx 2juI9ZNbmi6GyxvnaqenZ7EQO/ifiyWLPmseJRxW1fqBBh+6VL+bduT9xmYxmD9ER928IjhaKWT 1ZAdvGdanTxA1W0D4A42FRLabXMthbH2076UoWW/yGYUbBUOZLO6g52Gx+0L6qVh62YrKOgP4Mf VziqS5+hGRpq0WWZs/HDAP9TSVGpncJXcl4dnKVsiZ2bNeTfaHnn+u/Phy6UYCPH4HXhxD5FJPm rI4lMJ/GPwPmcSocLqLqBhP+Yn6NeBE14g9G+4ErIw0si5OoY23TZSyyXl5J6XNHt9Db3b/Yc/d g/Xi06j4u4qvqhyjFKnCbIpY12BiavQR2ve8gAPOdQOPVCXSQ8RBFMfGWaywI4HCEJpCPlIBJJ/ gZd4JQF/LTg81Rg== X-Developer-Key: i=rsworktech@outlook.com; a=openpgp; fpr=17AADD6726DDC58B8EE5881757670CCFA42CCF0A X-Endpoint-Received: by B4 Relay for rsworktech@outlook.com/default with auth_id=219 X-Original-From: Levi Zim Reply-To: rsworktech@outlook.com From: Levi Zim This patch adds a test for probe_read_user_dynptr helper, which tests reading variable length of user memory into a ringbuf backed dynptr. Signed-off-by: Levi Zim --- tools/testing/selftests/bpf/prog_tests/dynptr.c | 2 ++ tools/testing/selftests/bpf/progs/dynptr_success.c | 36 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c index d9a25c2873b6ed4219e063845b1caf978a7016fd..a5a61a4c570d803bfca2af3f1a3b7d5eb0b8197f 100644 --- a/tools/testing/selftests/bpf/prog_tests/dynptr.c +++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c @@ -32,6 +32,7 @@ static struct { {"test_dynptr_skb_strcmp", SETUP_SKB_PROG}, {"test_dynptr_skb_tp_btf", SETUP_SKB_PROG_TP}, {"test_probe_read_kernel_dynptr", SETUP_RINGBUF}, + {"test_probe_read_user_dynptr", SETUP_RINGBUF}, }; static int ringbuf_cb(void *ctx, void *data, size_t len) @@ -61,6 +62,7 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ return; skel->bss->pid = getpid(); + skel->bss->user_ptr = (void *)&skel->data->test_buf; skel->data->test_buf.length = 8; prog = bpf_object__find_program_by_name(skel->obj, prog_name); diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c index 64c698f83a37bfe924db93d36982a0a1c8defe62..5317860290dccb0862b6a0b94bb6f738c8d92835 100644 --- a/tools/testing/selftests/bpf/progs/dynptr_success.c +++ b/tools/testing/selftests/bpf/progs/dynptr_success.c @@ -576,7 +576,7 @@ struct { } test_buf = { .length = 0, .buf = "0123456789abcdef", -}; +}, *user_ptr; SEC("?tp/syscalls/sys_enter_nanosleep") int test_probe_read_kernel_dynptr(void *ctx) @@ -605,3 +605,37 @@ int test_probe_read_kernel_dynptr(void *ctx) bpf_ringbuf_submit_dynptr(&ptr, 0); return 0; } + +SEC("?tp/syscalls/sys_enter_nanosleep") +int test_probe_read_user_dynptr(void *ctx) +{ + struct bpf_dynptr ptr; + int copy_len; + + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 0; + + err = bpf_probe_read_user(©_len, sizeof(copy_len), &user_ptr->length); + if (err < 0 || copy_len < 0) { + err = 1; + return 1; + } + + if (copy_len > MAX_BUFFER_LEN) + copy_len = MAX_BUFFER_LEN; + + bpf_ringbuf_reserve_dynptr(&ringbuf, copy_len, 0, &ptr); + + if (-E2BIG != bpf_probe_read_user_dynptr(&ptr, 0, MAX_BUFFER_LEN + 1, + &user_ptr->buf, 0)) { + err = 2; + goto cleanup; + } + + err = bpf_probe_read_user_dynptr(&ptr, 0, copy_len, + &user_ptr->buf, 0); + +cleanup: + bpf_ringbuf_submit_dynptr(&ptr, 0); + return 0; +} From patchwork Sat Jan 25 08:29:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Levi Zim via B4 Relay X-Patchwork-Id: 13950273 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 BA1DD207E1F; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; cv=none; b=EYlJPFbDHj+iHfFXfNQJJlJCQyy9xwaKsi2zGbuhgNC0R/vRSVN0saZ9SfReIxyvVa1S2JfhuSQxqLBeLtSTVmPepUANUbC4c/fjk+Lz3GelJ0OPusD+tGoUDY49Ggy0BlROvKMJhqHLVRX9uND9vCg2n4+sh8+nqI4SIeFxw4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737793828; c=relaxed/simple; bh=0ISlPQASi8sOGiTZRr8sXYkkw/cj+H2dHR0QbFcOJZg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fE/M456cErgK0gw6qJIgcux8erovBtwTlRXH6p1VidBfGjcJt1sSvHGfQj3mwDvbzre/TwF94SWTkYBDgYuO6zeoidBU8/+IHgvPMQTDHr8532VAilGNQhxe8aNadQxh8OpXvdIkW057N4wjafzIy9qSdlsWL9/HF9KQBIHeKKw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j34qagjp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j34qagjp" Received: by smtp.kernel.org (Postfix) with ESMTPS id 8E522C4CEF9; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737793828; bh=0ISlPQASi8sOGiTZRr8sXYkkw/cj+H2dHR0QbFcOJZg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=j34qagjpglU0mUZTxVx7V705AZZ+TSZG8lr+v1hheI3W3UYXy8PLATiG3L7cdy1xQ ezVDXNirVX6M5mbKRs77VP//QwDg/aBidH57SQTcUjtZK+rVZtQgfR7L72HniZDm7o UOa24rf+87hPpVdaVcrOHRPmGOywvj9N0oTvZ+k+tAQYeZKYR2iVT44wm3IIhWlrzw E04tVMxQpQnTuZtop+Vr7hjhVTEqO6ougPSFRThxqhaXfhp0z31kjPSOle5QmP/q1V FxVu556UuXuMGg7rc7aQPO0v9LT83NnGA9yJjkC/zqEft7+v/nXFW7lArDr6CV2dSy QebgY54JE9HEw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85C81C0218C; Sat, 25 Jan 2025 08:30:28 +0000 (UTC) From: Levi Zim via B4 Relay Date: Sat, 25 Jan 2025 16:29:54 +0800 Subject: [PATCH bpf-next v2 7/7] selftests/bpf: copy_from_user_dynptr test Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250125-bpf_dynptr_probe-v2-7-c42c87f97afe@outlook.com> References: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> In-Reply-To: <20250125-bpf_dynptr_probe-v2-0-c42c87f97afe@outlook.com> To: 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 , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrii Nakryiko , Levi Zim X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2541; i=rsworktech@outlook.com; h=from:subject:message-id; bh=1lAHI+FdFmnes4eRlPuwbThpqaSf+nKUYWM4JR6lmdg=; b=owEBbQKS/ZANAwAIAW87mNQvxsnYAcsmYgBnlKEgaDOvvSTi2myiN4juIhT7XAwb4vipaIBC8 te11MKDd8CJAjMEAAEIAB0WIQQolnD5HDY18KF0JEVvO5jUL8bJ2AUCZ5ShIAAKCRBvO5jUL8bJ 2EHZD/4+UmX7SD5haN6mjGVHWxiNzKbUCDyXVJCQ4glNAR/glcfFXli/rDRJVW1037wSANcxq7G 92qggOGmZCk2/hRHADnnTYYxdWaQhUlOdqfyX7YEhGTM8sS63DNfrryWbP9Gwxz8pTNv65nNrvM /iOJIGYe5oJ4XGoAV3jMqJoZHIeH7qHSw6WxCV03lGmFnw5jZrY1NYwdMhlboj2Gh6Owht3an/0 QpV3nhuZZcGi9Ij7D48n5QFrRaso+HNYxPlR6+H198ITxErAE2jUBmh7cNGZDjdWCPK/PaMiaFy POx4mNrWjG2JyAGpBgdDSwDSe/YzUtpLRhoneOwlehCEm3IkbOCpabINUxII1qNb5R7KIQbDs+X feJXyRtTD4oe/W7cYyMqIQGsXFRG+UTj+PU71D3qH1FXhc7y/kbw+C46dljianT8fq9cjuJJmI4 EYuDFrzvHH+Ysfglgu8hps76uV4I724bI5vC4wtDFjHhqMkQm/tkqCc3e3dqvvdDJ13PYBSZApZ 4sRZPpCw4afkgdeJ9UeYVrclW8QslIV9ZiIgoRF48wjKQ//vqSDtcFYIaww4cO8wGxAsW9r04C8 hRg/y/Q/TFka0xNPijh7FPE1Za5/PjkNptLlXMHqlsiCZiRZEtSuU38dvhK1XHR5fMMhvW4gyAs eLXZu1iOairMXpQ== X-Developer-Key: i=rsworktech@outlook.com; a=openpgp; fpr=17AADD6726DDC58B8EE5881757670CCFA42CCF0A X-Endpoint-Received: by B4 Relay for rsworktech@outlook.com/default with auth_id=219 X-Original-From: Levi Zim Reply-To: rsworktech@outlook.com From: Levi Zim This patch adds a test for copy_from_user_dynptr helper, which tests reading variable length of user memory into a ringbuf backed dynptr. Signed-off-by: Levi Zim --- tools/testing/selftests/bpf/prog_tests/dynptr.c | 3 ++ tools/testing/selftests/bpf/progs/dynptr_success.c | 34 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/dynptr.c b/tools/testing/selftests/bpf/prog_tests/dynptr.c index a5a61a4c570d803bfca2af3f1a3b7d5eb0b8197f..a0095204609c710d49a6105ebf8be44723fb53d2 100644 --- a/tools/testing/selftests/bpf/prog_tests/dynptr.c +++ b/tools/testing/selftests/bpf/prog_tests/dynptr.c @@ -33,6 +33,7 @@ static struct { {"test_dynptr_skb_tp_btf", SETUP_SKB_PROG_TP}, {"test_probe_read_kernel_dynptr", SETUP_RINGBUF}, {"test_probe_read_user_dynptr", SETUP_RINGBUF}, + {"test_copy_from_user_dynptr", SETUP_RINGBUF}, }; static int ringbuf_cb(void *ctx, void *data, size_t len) @@ -43,6 +44,8 @@ static int ringbuf_cb(void *ctx, void *data, size_t len) if (!ASSERT_EQ(len, skel->data->test_buf.length, "length")) return -E2BIG; + ASSERT_EQ(skel->bss->err, 0, "err"); + if (!ASSERT_MEMEQ(buf, skel->data->test_buf.buf, len, "ringbuf_cb")) return -EINVAL; diff --git a/tools/testing/selftests/bpf/progs/dynptr_success.c b/tools/testing/selftests/bpf/progs/dynptr_success.c index 5317860290dccb0862b6a0b94bb6f738c8d92835..10dda58fed66a05ad1b08c8684784fddabe3f4ad 100644 --- a/tools/testing/selftests/bpf/progs/dynptr_success.c +++ b/tools/testing/selftests/bpf/progs/dynptr_success.c @@ -639,3 +639,37 @@ int test_probe_read_user_dynptr(void *ctx) bpf_ringbuf_submit_dynptr(&ptr, 0); return 0; } + +SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_dynptr(void *ctx) +{ + struct bpf_dynptr ptr; + int copy_len; + + if (bpf_get_current_pid_tgid() >> 32 != pid) + return 0; + + err = bpf_probe_read_user(©_len, sizeof(copy_len), &user_ptr->length); + if (err < 0 || copy_len < 0) { + err = 1; + return 0; + } + + if (copy_len > MAX_BUFFER_LEN) + copy_len = MAX_BUFFER_LEN; + + bpf_ringbuf_reserve_dynptr(&ringbuf, copy_len, 0, &ptr); + + if (-E2BIG != bpf_copy_from_user_dynptr(&ptr, 0, MAX_BUFFER_LEN + 1, + &user_ptr->buf, 0)) { + err = 2; + goto cleanup; + } + + err = bpf_copy_from_user_dynptr(&ptr, 0, copy_len, + &user_ptr->buf, 0); + +cleanup: + bpf_ringbuf_submit_dynptr(&ptr, 0); + return 0; +}