From patchwork Tue Jan 28 22:43:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13953327 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D077C0218D for ; Tue, 28 Jan 2025 22:44:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C4E2C28000F; Tue, 28 Jan 2025 17:44:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BFDB428000D; Tue, 28 Jan 2025 17:44:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A9EAC28000F; Tue, 28 Jan 2025 17:44:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8632128000D for ; Tue, 28 Jan 2025 17:44:34 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2A028805DA for ; Tue, 28 Jan 2025 22:44:34 +0000 (UTC) X-FDA: 83058341268.11.C4FC5E8 Received: from mout.perfora.net (mout.perfora.net [74.208.4.196]) by imf09.hostedemail.com (Postfix) with ESMTP id 5850E14000C for ; Tue, 28 Jan 2025 22:44:32 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=AGuQb1aq; spf=pass (imf09.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.196 as permitted sender) smtp.mailfrom=linux@jordanrome.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738104272; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QaM92VVJ8sVRDEhsj6U41D+q2njMPqo34UDqJs8UQDM=; b=ltJbT8bqIvdbywJjfWjIcThzIyaZyMRKM4xYYj7qn2X2sZAUPuOyegMO8+2H+5otvJ2wbw 8gftTFM8A2Afa2GIEdRMl5oGgNPkZ0i4i0HfxlRq8Qcj349Hdt/8MCrgDc2PBmHqzu6t1i Obwf8lh+qc/IXTDdCkSADgzylt18H58= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=AGuQb1aq; spf=pass (imf09.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.196 as permitted sender) smtp.mailfrom=linux@jordanrome.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738104272; a=rsa-sha256; cv=none; b=MLztF1NCVDUd3bngcyyraGXCwjwBoU5Uy7+vzEYFNw+4lF4rEwqxZrzszAQqErg7OZ3pJn kqFncrPnXq2dBiyfb0umEs6KFgMHFwuOlsKnlp1lloTdEqmjcuxsf4MB+uwhmiFz5nbsES e8ZUULEPizamQpXWrLhmClYWW1UXFmo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1738104252; x=1738709052; i=linux@jordanrome.com; bh=QaM92VVJ8sVRDEhsj6U41D+q2njMPqo34UDqJs8UQDM=; 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=AGuQb1aqIhkM+t4+H+aKV9x5h0pPsY81c0aAaxeguJlpZOirIYLpZfGQ46W7MO0+ z1sb28CiiDEFcQWUygQmz51ckV3HC/D8iCml46wSBYfMUftUAf0+IE6nfTrWJsUeP lcMWh1UOJNhztcQmiTMpHHA/OS4KqZDTyprfEvqbfO/RukoP/ENVZJ1plolEt1II/ F+tIZAkNbwjmq0W9t8gMcgvEjNlTRT1vpocQmKZMwZPxXKboicW074XcGaowS5ccn rNFR21spsnBOSN47SVqQRihYZ5X1cljtGzAEFBtYdEDLWZvApOr3GbfmKW8RjurKQ 3hytXralyJcjqUJG+Q== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.5]) by mrelay.perfora.net (mreueus003 [74.208.5.2]) with ESMTPSA (Nemesis) id 0MLvf0-1tXNSq09jI-004vGQ; Tue, 28 Jan 2025 23:44:12 +0100 From: Jordan Rome To: bpf@vger.kernel.org Cc: linux-mm@kvack.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Kernel Team , Andrew Morton , Shakeel Butt , Alexander Potapenko Subject: [bpf-next v6 2/3] bpf: Add bpf_copy_from_user_task_str kfunc Date: Tue, 28 Jan 2025 14:43:51 -0800 Message-ID: <20250128224352.3808460-2-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250128224352.3808460-1-linux@jordanrome.com> References: <20250128224352.3808460-1-linux@jordanrome.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:4xRNZXuuNESdqb2Tos29jq2ltKwzIJIj/MAmdqpTx6w0jR8Lksm UjLxdEFq7BpU5ZSVRgbHpG4UDMg1WATsegYcoM0SZ1bE5JYeiQzO/oy7tGbTSloEh1Yq/Ug yUCQxc98CuwpQohOGSqe1alLhLnZ4D1qhH+b6wTS8OIrm4fPeww5udt4QDV4F2hOu9fSFyL DGnrJOZ8BP5xI3IepRMdQ== UI-OutboundReport: notjunk:1;M01:P0:29i+6/+81s8=;DL5HqVC7ikGGjloURnjlmdkEFAI BKIy8ftc/phvEFeIiW5aSLRhpQ6rxdAtIp9L9+MgKxlUwkxVsxiW+TZ/dCqHrWgPXV6SC9Lni 8MLkTuacgufXQVRdAZT0KS34H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0a78Tgbbthiynfc3yE7mfmqd9O3VIcXQp+4uyCS5SiLfVVjJ0QvSTkwSCp42YnVluPZYELLPDEV8IGLDN9W7IAUAUkHy4mKjbNw62oXlCLN2wZueSD9VGBpzzB49+uiYvwy168mDTa 9yrj1hpd Mdxbqn0Ky/IbuAR2FsiKyqXoVa0VW+AvvdNYtPx/1HjKohMxZNABmHYnOY2cUFa0AFWktqOopub5/xdo8YMaycc6QLhyWtCkNSdss2P9GnzLJDlfCA3oTOhDFIXSQExH7jJ617pivPhjZdxi3WP0RF0fFJCXwsfLBQyGkZR6QRzHL66UoyFzivh8q52FgNWgAnmeYlbda0VBmjS2cZfWbIiAJuJsP4Dk2/U17loBXkOnVzu4P3aJqsV1Urx8Day29oug4A7HZ5AuMrEeeV3RpnkzJTe4rFD8hFs9nqhmufVURlPYbjD8q41CNrHfAq66XSrK/CB0ZHiGghUNfjBBEii5oYxJnF1lAjnVyci05EZjyjdo3VXXGgFJhoG+5BET/ReFJFoxasrmV8MEOSvO6czGy3g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This new kfunc will be able to copy a string from another process's/task's address space. This is similar to `bpf_copy_from_user_str` but accepts a `struct task_struct*` argument. Acked-by: Andrii Nakryiko Signed-off-by: Jordan Rome --- kernel/bpf/helpers.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) -- 2.43.5 diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index f27ce162427a..a33f72a4c31f 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3082,6 +3082,53 @@ __bpf_kfunc void bpf_local_irq_restore(unsigned long *flags__irq_flag) local_irq_restore(*flags__irq_flag); } +/** + * bpf_copy_from_user_task_str() - Copy a string from an task's address space + * @dst: Destination address, in kernel space. This buffer must be + * at least @dst__sz bytes long. + * @dst__sz: Maximum number of bytes to copy, includes the trailing NUL. + * @unsafe_ptr__ign: Source address in the task's address space. + * @tsk: The task whose address space will be used + * @flags: The only supported flag is BPF_F_PAD_ZEROS + * + * Copies a NUL terminated string from a task's address space to @dst__sz + * buffer. If user string is too long this will still ensure zero termination + * in the @dst__sz buffer unless buffer size is 0. + * + * If BPF_F_PAD_ZEROS flag is set, memset the tail of @dst__sz to 0 on success + * and memset all of @dst__sz on failure. + * + * Return: The number of copied bytes on success including the NUL terminator. + * A negative error code on failure. + */ +__bpf_kfunc int bpf_copy_from_user_task_str(void *dst, + u32 dst__sz, + const void __user *unsafe_ptr__ign, + struct task_struct *tsk, + u64 flags) +{ + int ret; + + if (unlikely(flags & ~BPF_F_PAD_ZEROS)) + return -EINVAL; + + if (unlikely(!dst__sz)) + return 0; + + ret = copy_remote_vm_str(tsk, (unsigned long)unsafe_ptr__ign, dst, dst__sz, 0); + + if (ret < 0) { + if (flags & BPF_F_PAD_ZEROS) + memset(dst, 0, dst__sz); + return ret; + } + + if (flags & BPF_F_PAD_ZEROS) + memset(dst + ret, 0, dst__sz - ret); + + return ret + 1; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(generic_btf_ids) @@ -3174,6 +3221,7 @@ 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_copy_from_user_task_str, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_get_kmem_cache) BTF_ID_FLAGS(func, bpf_iter_kmem_cache_new, KF_ITER_NEW | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_iter_kmem_cache_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)