From patchwork Sun Jan 26 12:41:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13950669 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 2ED87C02181 for ; Sun, 26 Jan 2025 12:42:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40D736B00D2; Sun, 26 Jan 2025 07:42:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BD956B00D3; Sun, 26 Jan 2025 07:42:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2AD9D2800CA; Sun, 26 Jan 2025 07:42:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 13AB76B00D2 for ; Sun, 26 Jan 2025 07:42:02 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7AD42A17BF for ; Sun, 26 Jan 2025 12:42:01 +0000 (UTC) X-FDA: 83049565242.13.0795614 Received: from mout.perfora.net (mout.perfora.net [74.208.4.197]) by imf28.hostedemail.com (Postfix) with ESMTP id 85EC6C0008 for ; Sun, 26 Jan 2025 12:41:59 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=hMussjKm; dmarc=none; spf=pass (imf28.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.197 as permitted sender) smtp.mailfrom=linux@jordanrome.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737895319; a=rsa-sha256; cv=none; b=1zI+eHAMwPDsEBsoedd5UT+RkOISArWeIdLYrvasvMbt3e7HuK0JzeWWPzxAdN8caH+FRG JywH3oBzVNpjeHiM3jfWcgI7I/K7TDSNgqtVH8hfPSao1UCmfHoFYRKuTQ8w//idPw+X2h Sem3SUZEx8orFKJN3mzz0c9vnixzIEA= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=hMussjKm; dmarc=none; spf=pass (imf28.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.197 as permitted sender) smtp.mailfrom=linux@jordanrome.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737895319; 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=6uJ6digrQkDkVFKg/S+cWGYkraPaHq/6PgGWgLVRGndSKJI6BZhWAY7cxP8EfzAfL0EidE S11wQC4foUMRsoTUrRNNkL14BCUluzKAqUzepW0b0qSa2Ja0fU149v6Vt1O4deLVMDzkgb 3wcM6YGC5WZ/Y7dj/bs68ebh6iLytLg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1737895317; x=1738500117; 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=hMussjKmkS8xirvHU/M3ZK4/Vy76M3yORhYOyQYOXJ+Xohrbqi0f2KgGGDkJRJWW mhFYtd478JvYRhxrA9LB4m4/ckxTued+9F7fL7CvEnvVng41ZzMLTdBQJHVSiOlqf ouXY8EHWOyUDK080h/JSK21J7tIoLHWaJ0oBIMs9dbd77KGHvyo2t6yib7qWDUrYo +x8IUj/Ts3l+Nan52T/pkjEidwaZLMgCoQ6vNVUZeSoAX/As3qcSvHp9W7jRcJS7Y THmODTI4f2LvzFe6AVoX6yb2Yk/3xWgYdPvRVXCZPVFbxGXVAmvm6t1APjvnHgKUi ghsckoOO3aNuzN0f5g== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.10]) by mrelay.perfora.net (mreueus002 [74.208.5.2]) with ESMTPSA (Nemesis) id 0MN2Bq-1tZq6H00TI-009eP3; Sun, 26 Jan 2025 13:41:57 +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 v4 2/3] bpf: Add bpf_copy_from_user_task_str kfunc Date: Sun, 26 Jan 2025 04:41:46 -0800 Message-ID: <20250126124147.3154108-2-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250126124147.3154108-1-linux@jordanrome.com> References: <20250126124147.3154108-1-linux@jordanrome.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:Bjg3kl/SxsbzXz54Tq5IdMdSap5RqVJxqDCBn1QQsJVhmBA5fWt i3KvIk8h/8wJN6K33pHR+p3RJz8FlsKAYVvZX68rB3vegnqmMhe2zTbq8joxYRK7Iyb5Q2w i2mfbEmT7NMgu6RCfi0sBKmIIvklmsCW3XSp8gMZps2QY7WKJI1kU3j9rlRIEnuaTla+9jb WqGOz/w53TIzcAVVdt7Tg== UI-OutboundReport: notjunk:1;M01:P0:d/sj0gjjfgE=;aswIhU3hj3vC18Bfd3tWFjpnAmO BG/h6/Q98SHTzh4tGkg/t5QK8dgNqS/cQ5buVxrVWO7FTjtYcOWAD8Nxtay77T/wpTrY4pizC nK3n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RbnhsDJghDLc4kF+Y2N1iRT0oy LTxgvXjV w4xwHVEEggDASyNUJus9zGGzlc5W65e29oPSfIBUmnKYkcCMUkZ3TnRXdwdEptqLgGKNy52ggV1IxkQYuOjfqZV4UH9Awwk0qK1djBHEclq5tAEb5aWiYC9lsTAltR8SUvR583phjTMQ0dZZjn+y6N1Cx+FT1eCugmj8CbBlrLMMHZbHwks1uIjw1C+IbDXDvbDcdAb4PurDtjuLl61UG7KhG3pTGd3/j+KuDpiEffYJIi+g+daWitREXIbrKvpgP2i2yBzxIY/NpZgv1P/giWUrubKVams/jNEh01dKv6648JLQ5yfgywk1gT3dF+57ISIqmzz2ZbanMXpQ1pJ+gWOYiG1kk+0Q9xXXri8XokERqTQmNuRb01Vz0iQrjl032pUah 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 --- 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)