From patchwork Sun Jan 26 16:38:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13950733 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 9A77FC02181 for ; Sun, 26 Jan 2025 16:39:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B50F22800FA; Sun, 26 Jan 2025 11:39:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AD82C280101; Sun, 26 Jan 2025 11:39:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92A362800FA; Sun, 26 Jan 2025 11:39:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 75B55280101 for ; Sun, 26 Jan 2025 11:39:33 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 31DD41219A6 for ; Sun, 26 Jan 2025 16:39:33 +0000 (UTC) X-FDA: 83050163826.05.037C1AF Received: from mout.perfora.net (mout.perfora.net [74.208.4.197]) by imf01.hostedemail.com (Postfix) with ESMTP id 5700140009 for ; Sun, 26 Jan 2025 16:39:31 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b="Pv/VYctF"; spf=pass (imf01.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.197 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=1737909571; 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=Adf0F9Bo5BhET/y5GTGKNoD7ZEXqa+Hj+iV/mgf4klE=; b=zonLmRRid/a8YsCgIj62EU+CXe9S+wlgPu+7+CStrwVJt/9fEU7d0kMafKbat3E1OI/QOU 1K9B1ZySZYeRA22CS6iz2CM/Ch8ZatA7C139dcnUzCNSaUHB2Ow4IZof+XRsmk030omGGq jgHLKHEG40IJi2gURuCGkJsHkqGkjcg= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b="Pv/VYctF"; spf=pass (imf01.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.197 as permitted sender) smtp.mailfrom=linux@jordanrome.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737909571; a=rsa-sha256; cv=none; b=MV6VKYzEQZAYVjTHS3mQgoY2k93RqE6z4rDTCT6RX/bes6hG4rWc8Q2tuufOqB/kOfsp7d WWjEnvE48VS+BHqKJSSADQm6MzmzRm8wkXalt6jry7nsMCYMpFOwe79OE3oODaWe60qw2r Zri7obpNG3zn19Ml4jX2XNDIAHruhhI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1737909551; x=1738514351; i=linux@jordanrome.com; bh=Adf0F9Bo5BhET/y5GTGKNoD7ZEXqa+Hj+iV/mgf4klE=; 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=Pv/VYctFnd9b5BJy2dlP9227zHpfELGGHdjgegCrHWq4mzIQ4KP3/5hX6/ma0eAC 4g4Mc9P15rH0FIxT5maFsbO3soWeNb3n+d+ADrIf9mBzIF8ibWMebHE/UnroVblYu jWo9RC6eUs8Cklrw137V9FLw/qivGr+k5zsSMvJWFRuvAYOVOWpKQUDGsVSyJ8/W5 LWrwIB4c4gUAB88OUWARaammA9fdqhEGTmN8BqBN3wPJ0nhHN/MFLXvZlUxcHjHlq YxOaGEllGA87uAHWb6PIs+yv+6r3kdniR6EPUl2hDvsNm4yTL0/c6uxYl6M/TshF5 2JDeQ2Cl2a5jNHTorQ== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.8]) by mrelay.perfora.net (mreueus003 [74.208.5.2]) with ESMTPSA (Nemesis) id 0MCKl5-1tl84U1fV3-00CKlw; Sun, 26 Jan 2025 17:39:11 +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 v5 2/3] bpf: Add bpf_copy_from_user_task_str kfunc Date: Sun, 26 Jan 2025 08:38:56 -0800 Message-ID: <20250126163857.410463-2-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250126163857.410463-1-linux@jordanrome.com> References: <20250126163857.410463-1-linux@jordanrome.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:T65WP1XOpfRRM3DN/qlyZdpDVUitGyuAEHgXSpe7k9XMqsh66jj miamZ/RhLuf7SgJd0rDUz7DPbJiG6coIXuKyerhRXl2fQKO0/xnmGavlT77AvuhoUYFbxn6 r3WmMhfHDY3DzyGNihChKsmdfa8ZZF6Ur1ZI7SNZCW5IMlhxZ6adgFTG4sn5mxyil3WBAUV ExwgtKLKvnT9KeDBRusPw== UI-OutboundReport: notjunk:1;M01:P0:3wxKDC7WeMQ=;c7VRqWP0aTLRkGyYiYpxWnB4r3e j3SI9FvuZ8GJEUmwN3+eJBLJZWBovUYkP6WlvgN4WMGgw7p9uyiuCIlTG/Fig3fInUArVtrsn EgeBmZMbquLfXRbkW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id: 5700140009 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: gmukeihmgy1fho38k4mq8ny49xaczs8g X-HE-Tag: 1737909571-319093 X-HE-Meta: U2FsdGVkX19KqzP3g8o6o4Mv5eJ516fsQY08QubcCtLcM3iXoNu300B/biLuonfqNXb9UB9GHC0jxokYIcpbAlLoHFnhqlf9t61rytZYvoENMIl9eyMypFNUwnJZaOMmc6y4DZx+MJzxwd/YXronQaYRQFv/c8aeI+5U8oIUEgxpI1nHyb5jBmXfpruoSFDp3FlS/rv8kuf2vYoKtC2hkUftEeMniTc47XDl58RodXjtmuQ/a6DSZud9JpwNpvyzvRw67Y0zBPe9EaeHpLh1/wmNXUpuqbu6Ttf9uoc/vjRQhrGRNxupq0GyTOcvStJvrv4lW9F6nr/ejfkoEcWYpG7+pgY9gy7SYKCM+unP1EJ8sBMaTYSgVOvl9+Cm6KslCpp3CzG+f/vcjIWOdxaBBKybtfMwsTztCSagPydu5hesD8IwvarN9Sa2SNc6HBGVR+XnRcz8a7I1v4h4AM8d39ONFH/YH+AlsNG3rZ6zhF30kQ985O0npaxyrb0EASbov50Di9fTtadX4ZraKsjMnQjUbMJnSXJF08IPbhXM4t65bf2NHJGuDBjQbCmVgRD7TvCDgAfLDDpidwMHUHbbsYERzK0RffZgAzVsw1ABcqNnVZwbx+UCSnHYvxSl/vQXxyEOeuo1/0dMWjhR5796kvywTgdfTb0CGyAs7BkHji3Hpv6FOhBVv/6JBNmDpwNneeo2xsV69Rdg5rWTIlSbN2YzBYipda+Aa4GKlSznuyzb5hEE5NFLRqoyWL60qDBUQjTelRHiv4z+ZmVO38JnNRXm9R/4aRblItz6FbG8Pt3SMDcq2s75+kUe4lF3YjRgpjzuhofKojf1qqICJKm3fu/trqx/2l7h1qmAOrBR4gBiGi7LLkCYkMntW+QSXIM+4DlW8Gz+ZigJh/O3pSueEqJWPm3CL2BCn9AEBBtcfaJ35YBz440Ahi4GfOfNTn8Vy2mhGQtic34szkxd/SA Il1q2efb niwwxH0qDYFVbdpHYn8LXKPEHicW4aH6ejjteebguCAMNZ5ktwMWg6KihUKpctDBNJeH1JZ24vGAzhtoAHrA4K7pee+mYRr0+byJOAP3H+usVVPY/QgnLkEXGtTPWDwzl12AjGB4e5X18YAZLUIU7+Myj+6xb07nQjDSuCjOor2VbZdomH5bRl7bet3YukXx3BSq7Sx3bhwIZGpHD2VyOE7PWeiuGp330c9vSYrdjkv+yM/l4GHFgxXKqQ86t7R7/cxd4Fz0K/n2BB21o3o0/1dNTgHrsqKvxhTtDb1SvrygZHNAQki8dsCOnCLXM3Bqevr3EuuRY6Zq6xiYXRH/U72pwYXraa1uI2c+Az1R3MGuUnFVxHM0MdoF0Rv9qvjqvq65i 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. Signed-off-by: Jordan Rome Acked-by: Andrii Nakryiko --- 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)