From patchwork Thu Feb 13 15:21:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13973455 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 2FA33C021A0 for ; Thu, 13 Feb 2025 15:22:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB658280003; Thu, 13 Feb 2025 10:22:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B3F6E280001; Thu, 13 Feb 2025 10:22:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9DF38280003; Thu, 13 Feb 2025 10:22:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7F811280001 for ; Thu, 13 Feb 2025 10:22:19 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4ABF680239 for ; Thu, 13 Feb 2025 15:22:19 +0000 (UTC) X-FDA: 83115287598.01.776C5AE Received: from mout.perfora.net (mout.perfora.net [74.208.4.194]) by imf25.hostedemail.com (Postfix) with ESMTP id 71498A0005 for ; Thu, 13 Feb 2025 15:22:17 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=k2r47qhw; spf=pass (imf25.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.194 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=1739460137; 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:references:dkim-signature; bh=X3iGHmUioV23gVtaGze20y7Mei3yFn3BxL7pJl4WLaA=; b=DwRGpG0++1o0DodjQyBrHuBhtEGLmxhb0y0+MX7Gz/JhxVNV2S8BNmpyCLf1pUp1fGSaKE T5ewkLXXFwunUFmCRNx4nw3E9paxtLiC9LAhrfXl3v7FsL6xouE9XEHLRGTvVQsPiHbEZi 6iEGeicIGYcxiJWh9Yy1NC3F8+kOgEg= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=k2r47qhw; spf=pass (imf25.hostedemail.com: domain of linux@jordanrome.com designates 74.208.4.194 as permitted sender) smtp.mailfrom=linux@jordanrome.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739460137; a=rsa-sha256; cv=none; b=WD7JF2d5LhMdT+EbLGgNH9lSq03nXl0ExJFEoOBtOxACCUXu+SYIac//rsS9nKYHlm8huU O/cUGcEa+TRdCpF+Wg3g7ZX6K1gDeVZLukfToP+TgUipGYlUqUmfCwkaEE05VFa5P3Z7N2 mjcI71+TW3GlB3kdVMUGkFp3ZY8SZlE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1739460094; x=1740064894; i=linux@jordanrome.com; bh=X3iGHmUioV23gVtaGze20y7Mei3yFn3BxL7pJl4WLaA=; 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=k2r47qhwsUB2AsSjeGfNPddps1WP5onEH0tpEiBLqPK/flmsr3GfhquLAz0pdxf6 HxpDiYGB4w3sP7ylMaszAI3viMj6axJuoFhZRXieYrXaHXPyajYw8jNN8SAPozgyn JTZXlCOIVZ0Qz8UGoe5nDU1TLh2eodRi/35WcsJNoucA/O6dZYYYMStghWUMSz6B7 OQ3qDaCS5B7FFM7K45covETNQY3yAVj1J+x0BwNc6RRZa1vMQoG9hEfwucUHAg0hB ORpxiIZU+EyFvQo/y4cxC5R4exRJUO4EESGrddzdXRI3YiFslSEUoVbnRZ91+slHm 8EhFJ4+5WpeyAlW8bA== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.11]) by mrelay.perfora.net (mreueus004 [74.208.5.2]) with ESMTPSA (Nemesis) id 1MCauF-1tZYbe40cF-00FmSb; Thu, 13 Feb 2025 16:21:34 +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 v8 1/3] mm: add copy_remote_vm_str Date: Thu, 13 Feb 2025 07:21:23 -0800 Message-ID: <20250213152125.1837400-1-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Provags-ID: V03:K1:nXmO420NAFvU9HAMP9B608FaxDpJVtP9EE9EoTcKYfcwiR5cBdh XszBwE98aD1Zr1wCxNib1iPNA9NcjpWQeDHhJX38cnD8KKmZfKlClTXgHmLGd2rgnxOR4k9 qa9X+LIT7MUOzfJi/EdEOm5YczWXZO5TA/LMd57x/mRJ09JZaZJn08SvZF2miYjTsTP7z54 xisVqD/gZ2mNmGPUBooJA== UI-OutboundReport: notjunk:1;M01:P0:kEWmGGWuR6o=;TKYm8YujZN9sSEkyW0MTmoSBxbH 1ZZ2kNkmqWS53IhMP9qmgj2eilDB+SZQJeTa9FQRIw4RHyj7xbHBLVqdk4stq0t8bD9Ri2VS3 kjBQ48vx7giUiXwNXWjenKLapRxPlclNB2koVc55de7gtU6v5phm/t0gLgB+D+WvT1AVkcUmE AvzgasCNk+XIvj2k2W2u29lhRf3TssTg8ebcYOMhV/muboZRFf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A3DyNb9Qy3GDBPzbklmr6NFBXuy+vbjvGiZkhmEJHq4OBOJcn49T+AH3vgDWUQSXJqFz5zgYlDU1yOS7KRDg1zO/ow5kzhrkk1bdf0YZIwcAoRM/dEn8WCbijVH3RP7ZMrJmYrYFU4oN OcVXl67H IcpiETp5OcQbTr9Ae2FcnSF5hFc2msPw2dlgk3Foorgh1wN6TUr/tn2t6ryASCIF/jvsu2SdWCaKKP18siIQHZNKek8L+28o/O3ehe3ucZiYDDTItSUuL64p0iyTZm9uKnTyLqdL520dY1OHRww/+zkySde87NXV5O/d+taNS7R9AguJju3OwHFS2rY+EouDrKBXD3w13V2fGF5ha79+z/CgvVrvfzV3J9KgabhksDWA91bwAAAdKzIk1pPe7M9UV7arjSu1psdCVeP8sNTYYvZNtbO4DG31h41mW7GlklsALPixAkC4If/aS+Z6mp3QscYrzXXjCmsghe6wmRJU4ck2eynfu3iie1sbyoa/yBEObb/Cb7+tXxHOJE+Q7IJ3zbIOZ 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: Similar to `access_process_vm` but specific to strings. Also chunks reads by page and utilizes `strscpy` for handling null termination. The primary motivation for this change is to copy strings from a non-current task/process in BPF. There is already a helper `bpf_copy_from_user_task`, which uses `access_process_vm` but one to handle strings would be very helpful. Reviewed-by: Shakeel Butt Signed-off-by: Jordan Rome --- include/linux/mm.h | 3 ++ mm/memory.c | 122 +++++++++++++++++++++++++++++++++++++++++++++ mm/nommu.c | 76 ++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) -- 2.43.5 diff --git a/include/linux/mm.h b/include/linux/mm.h index 7b1068ddcbb7..aee23d84ce01 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2486,6 +2486,9 @@ extern int access_process_vm(struct task_struct *tsk, unsigned long addr, extern int access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf, int len, unsigned int gup_flags); +extern int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, + void *buf, int len, unsigned int gup_flags); + long get_user_pages_remote(struct mm_struct *mm, unsigned long start, unsigned long nr_pages, unsigned int gup_flags, struct page **pages, diff --git a/mm/memory.c b/mm/memory.c index 539c0f7c6d54..014fe35af071 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6803,6 +6803,128 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, } EXPORT_SYMBOL_GPL(access_process_vm); +/* + * Copy a string from another process's address space as given in mm. + * If there is any error return -EFAULT. + */ +static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + void *old_buf = buf; + int err = 0; + + *(char *)buf = '\0'; + + if (mmap_read_lock_killable(mm)) + return -EFAULT; + + /* Untag the address before looking up the VMA */ + addr = untagged_addr_remote(mm, addr); + + /* Avoid triggering the temporary warning in __get_user_pages */ + if (!vma_lookup(mm, addr)) { + err = -EFAULT; + goto out; + } + + while (len) { + int bytes, offset, retval; + void *maddr; + struct page *page; + struct vm_area_struct *vma = NULL; + + page = get_user_page_vma_remote(mm, addr, gup_flags, &vma); + + if (IS_ERR(page)) { + /* + * Treat as a total failure for now until we decide how + * to handle the CONFIG_HAVE_IOREMAP_PROT case and + * stack expansion. + */ + *(char *)buf = '\0'; + err = -EFAULT; + goto out; + } + + bytes = len; + offset = addr & (PAGE_SIZE - 1); + if (bytes > PAGE_SIZE - offset) + bytes = PAGE_SIZE - offset; + + maddr = kmap_local_page(page); + retval = strscpy(buf, maddr + offset, bytes); + + if (retval >= 0) { + /* Found the end of the string */ + buf += retval; + unmap_and_put_page(page, maddr); + break; + } + + buf += bytes - 1; + /* + * Because strscpy always NUL terminates we need to + * copy the last byte in the page if we are going to + * load more pages + */ + if (bytes != len) { + addr += bytes - 1; + copy_from_user_page(vma, page, addr, buf, + maddr + (PAGE_SIZE - 1), 1); + + buf += 1; + addr += 1; + } + len -= bytes; + + unmap_and_put_page(page, maddr); + } + +out: + mmap_read_unlock(mm); + if (err) + return err; + + return buf - old_buf; +} + +/** + * copy_remote_vm_str - copy a string from another process's address space. + * @tsk: the task of the target address space + * @addr: start address to read from + * @buf: destination buffer + * @len: number of bytes to copy + * @gup_flags: flags modifying lookup behaviour + * + * The caller must hold a reference on @mm. + * + * Return: number of bytes copied from @addr (source) to @buf (destination); + * not including the trailing NUL. Always guaranteed to leave NUL-terminated + * buffer. On any error, return -EFAULT. + */ +int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + struct mm_struct *mm; + int ret; + + if (unlikely(len < 1)) + return 0; + + mm = get_task_mm(tsk); + if (!mm) { + *(char *)buf = '\0'; + return -EFAULT; + } + + ret = __copy_remote_vm_str(mm, addr, buf, len, gup_flags); + + mmput(mm); + + return ret; +} +EXPORT_SYMBOL_GPL(copy_remote_vm_str); + /* * Print the name of a VMA. */ diff --git a/mm/nommu.c b/mm/nommu.c index baa79abdaf03..11d2341c634e 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1708,6 +1708,82 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, in } EXPORT_SYMBOL_GPL(access_process_vm); +/* + * Copy a string from another process's address space as given in mm. + * If there is any error return -EFAULT. + */ +static int __copy_remote_vm_str(struct mm_struct *mm, unsigned long addr, + void *buf, int len) +{ + unsigned long addr_end; + struct vm_area_struct *vma; + int ret = -EFAULT; + + *(char *)buf = '\0'; + + if (mmap_read_lock_killable(mm)) + return ret; + + /* the access must start within one of the target process's mappings */ + vma = find_vma(mm, addr); + if (!vma) + goto out; + + if (check_add_overflow(addr, len, &addr_end)) + goto out; + /* don't overrun this mapping */ + if (addr_end > vma->vm_end) + len = vma->vm_end - addr; + + /* only read mappings where it is permitted */ + if (vma->vm_flags & VM_MAYREAD) { + ret = strscpy(buf, (char *)addr, len); + if (ret < 0) + ret = len - 1; + } + +out: + mmap_read_unlock(mm); + return ret; +} + +/** + * copy_remote_vm_str - copy a string from another process's address space. + * @tsk: the task of the target address space + * @addr: start address to read from + * @buf: destination buffer + * @len: number of bytes to copy + * @gup_flags: flags modifying lookup behaviour (unused) + * + * The caller must hold a reference on @mm. + * + * Return: number of bytes copied from @addr (source) to @buf (destination); + * not including the trailing NUL. Always guaranteed to leave NUL-terminated + * buffer. On any error, return -EFAULT. + */ +int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + struct mm_struct *mm; + int ret; + + if (unlikely(len < 1)) + return 0; + + mm = get_task_mm(tsk); + if (!mm) { + *(char *)buf = '\0'; + return -EFAULT; + } + + ret = __copy_remote_vm_str(mm, addr, buf, len); + + mmput(mm); + + return ret; +} +EXPORT_SYMBOL_GPL(copy_remote_vm_str); + /** * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode * @inode: The inode to check From patchwork Thu Feb 13 15:21:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13973454 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 090D3C0219D for ; Thu, 13 Feb 2025 15:22:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BB38280002; Thu, 13 Feb 2025 10:22:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 969E7280001; Thu, 13 Feb 2025 10:22:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80AD9280002; Thu, 13 Feb 2025 10:22:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 637AC280001 for ; Thu, 13 Feb 2025 10:22:03 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6603B4B35A for ; Thu, 13 Feb 2025 15:22:02 +0000 (UTC) X-FDA: 83115286884.20.BFAACC5 Received: from mout.perfora.net (mout.perfora.net [74.208.4.196]) by imf17.hostedemail.com (Postfix) with ESMTP id 95F9B4000F for ; Thu, 13 Feb 2025 15:22:00 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b="CwR/Zc7z"; spf=pass (imf17.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=1739460120; a=rsa-sha256; cv=none; b=d12e3ni63QQGSsi6RUXkEplJE0NzppT4ygMCrrXHBTYxKJMmhCFcHUzgep2IhVWHdaacWm 0LKvpg8altGH5sG9yK4w6NkXsiFvOneo2PIu5Ih1owhS93tHZErb2vLI9kGM6A3RSVbXJV OrwiLiN+t8F/i4cfsMR/Eqa5btXsMBo= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b="CwR/Zc7z"; spf=pass (imf17.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=1739460120; 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=L5Z5XHRJbrEupOAMd2oFS/FO+RvalBGuK+naQDQpmAMqDOx/Ua66cn7l+r0xIxrIPhwNDX NvFUR1cxWrYQAgKMSitDny7kS2MnGGCr348voZmNBpx5Z/s3vtrHw1AeOM3GGmXa+0BbpK s331ib6iIeTutzkE55ZgSNYc6+CE70o= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1739460097; x=1740064897; 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=CwR/Zc7zCuTY4OsXyO6Qmd74qGjuvjIS1OE0pRFormT09eJ4CxF+EiqJjfRs8OG/ nKA8n9gaIpXpndrne0Wk/vN3FGF3SK3SdjlTIbJq9umQYo8EwIKTEsRqVJlcYLZtL MfYeiXJhNt7vGvz0HVneNUowxpvkY/GwI98IlhGcOCSAKEBXb/JGx16NSoCaggyOk 6oHck8x4w5A8Yb0FCt98qyqLfQG+E1inzXFsYj4COHsi/tZlo23eAVqjyoAwLslhB QJDZ2MSQSlohfaanP/C+LpuWUg2H5354T5cUDc+9iXkoGfDNV4+7S29tuueqYcBTO ccT15K6cD7soGrHstA== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.115]) by mrelay.perfora.net (mreueus003 [74.208.5.2]) with ESMTPSA (Nemesis) id 0MH1qm-1tdhEB1qtO-00DXUA; Thu, 13 Feb 2025 16:21:37 +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 v8 2/3] bpf: Add bpf_copy_from_user_task_str kfunc Date: Thu, 13 Feb 2025 07:21:24 -0800 Message-ID: <20250213152125.1837400-2-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250213152125.1837400-1-linux@jordanrome.com> References: <20250213152125.1837400-1-linux@jordanrome.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:M2HqTh52VzVoW28hZaqWA8+HYrSAYL9Nms0h4Rw5isAfIEJewQ1 twyuDCDsOcjnxNjxwbS9qKv/fJhN7ZDKDK2BJjFIyaIk2tgWwJdDxaIpFxUVZa3FH9CcQjh DQQ3eS6f6v9yds5bNQ2yO2mUtp/zc9gpi8KpG/TOcd5EaXnvIUwcr4aXwi4SQYtZD45Kwpt /QVaH9FcwHfNLezznwm9g== UI-OutboundReport: notjunk:1;M01:P0:XFeo8jdd6/k=;jZRp5DXm0QPSSXAQxaDXmup3ZT0 zR+/WroUAQFWUZCBDkVgG/V2UASfi8OoDYhzGkS89SVYOWfD0xltc0B5XkK9TXS/lRDRy6Azi d6/6O0yjD9XkWd/ZDJhVK0UuyM8MVolFgm/opRcqVBbCafACwiriTEvobZAYvxGMAgu2tvVHb iGOHV5cDB+1EINtc/Numz+kf4qFr9tSgL2ieceUJ2NeLZEFoDsV6panrVEsjcfXMI+maPd8Nk ohg4ygLEDE7MWuVpgQCrwxwXiCPc8TYJGzvjaDv0wVyb3bOTDFkZrxyTBL6Z1evwjUAcqB/EJ U0LvKowLd7lJ8YcOADqYf9lTmEszlnV6pBGboNZ0A55ElQpxjA5mPOu+vTZNVopspo3YLmRMa Wz8RlzOjlDXyTlRxglb3hNJaEHBPOAM+Q2j+ZQrGOZmEifmq5GnwAJ+6KYtrLeWPuhQJKI/ve fGgSVS50GyrpsTgQrZ5r9CzkUtDCK/J2pctHqRwdInw8Q5X0Tf5MVVgFV/2iCpzBxWms0UgNv yG+ybmgcV3pFRyRR4rbROm/VS5MQk78eqHzyDWlaB0H1+XxaLEAVCjht9ZK9mzGhwGEaV+JBr mlXO76Lroz3roGJf2TucOPu/ofHoCAldH4Yz7q8wRE7eN79t15h12Ox9AgqJ41MtYDZEIjIvD Bg1Xe/Mcqh8hD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1yEk/ndwaV6tBEkHjdIayHKvweylbbuTXLyMKFSSIEbSG9Fv2TYinmdkaKbI2VlITelQ== 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) From patchwork Thu Feb 13 15:21:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13973453 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 222DCC0219D for ; Thu, 13 Feb 2025 15:21:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E8F96B007B; Thu, 13 Feb 2025 10:21:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 598FC6B0082; Thu, 13 Feb 2025 10:21:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 439C06B0083; Thu, 13 Feb 2025 10:21:44 -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 29C2F6B007B for ; Thu, 13 Feb 2025 10:21:44 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C43E11A0F1C for ; Thu, 13 Feb 2025 15:21:43 +0000 (UTC) X-FDA: 83115286086.09.0671473 Received: from mout.perfora.net (mout.perfora.net [74.208.4.197]) by imf02.hostedemail.com (Postfix) with ESMTP id 67FE080018 for ; Thu, 13 Feb 2025 15:21:41 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=VLb5JBuU; spf=pass (imf02.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=1739460101; 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=LhJJH5WuMX5aq6LX3M5eXAA/nYkW+PydFm8hbRxet3Y=; b=YETcbBvq3xGIrkTYhsewPKrxvUILyAeRBcAf4MMyvb+ZP9E5zQtMAzEvfAULwU762Qvn7b QVxv2DVDeiJcYey7vTHOYUbOUbLW7BtRShdZoiY0VfpPvxkr43z1Vdm0xc6JHWsNsNeNZY sBURUkSK4W6dsCFy9Aa9jPWieXhPve4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=VLb5JBuU; spf=pass (imf02.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=1739460101; a=rsa-sha256; cv=none; b=gMOynV+8npXXpv6OVITrpJUpNutBHW128raH8I8DYjeiKtfcSAoYsgFyK0HCnQr5Lf92QO kweY0mwgqecWsT6Tc0Ijh4dvIrqdvV5s70LJJHgCeqeBWeD++XLAt0JZIDE8fjeX7IIpYq EZce6IBTjnDw4jM1Kd/lI+U3MLWqBgA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1739460100; x=1740064900; i=linux@jordanrome.com; bh=LhJJH5WuMX5aq6LX3M5eXAA/nYkW+PydFm8hbRxet3Y=; 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=VLb5JBuUHlfYw+OraxHRJ6EObYdRWYZz5P2vI4DbiNjhScDL3+cMCVGh4qokVZuA 3bNJd1eCwXscRIIZB/BaQDD2OTvLzKX4qu/dh4MuuwTZeJk1JtIerljaSvtfDWlVN FgzQ0Xrfe1CWTna5x8gH7OaK+cLs8Dldu9z9iiVVT3/nf60vS4nKXHOkFNQW1ATWx K0/VdwElU0j3I59d7Ek2O3O776V7yRbJiGBqAQh4plFLHJi4P9OMi6zbpcZYZsV4q obgMDaxHTn2aO4JB8XMrCqJRAZvMH29nWNEVJ8FqqdgWHzhcEz8VAB7tvO+rFvs6Z rzjDH6FOsq9o97fvKA== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.116]) by mrelay.perfora.net (mreueus004 [74.208.5.2]) with ESMTPSA (Nemesis) id 1MNtKu-1u2TN53wZE-00IAZ6; Thu, 13 Feb 2025 16:21:40 +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 v8 3/3] selftests/bpf: Add tests for bpf_copy_from_user_task_str Date: Thu, 13 Feb 2025 07:21:25 -0800 Message-ID: <20250213152125.1837400-3-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250213152125.1837400-1-linux@jordanrome.com> References: <20250213152125.1837400-1-linux@jordanrome.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:QGmsxtd2pwzybjQU2337r3gVE4JGTCnpugI9n8QggzZMQPhZKZG 0wgR1zf3MmqORF56fgKuGj44TsdK9nwOPzFfb41XgwUb+P+8SVvEzUGUPJQt0q3AiD5ZViR CmIWgykpXpZJC5F2Y2cKXcY+Qd2IRMksrbDPCazkSta8Hl7Iq8EOFdQ8JgqJz7cXNBHrvea kApddgAMNHI6H7asCSX2g== UI-OutboundReport: notjunk:1;M01:P0:lhigDtmcOQM=;G5M1VqpqLY7VYJC7ReA8nhR+7EI e32kjD4wfIBoEFq3RGuulOgJriqKdIZXIc3klRVPysQS2uCYb+Od1JcOdXOo1QX80557XRtj2 Oia3y32K/KE0WaPNsyLm4UZEg+znJj6xS/0D33eSZ4n6Ngw17bsU5db2eYv2r86w9s2UiNVnn 6OehSn6E/dYa8Sws9ajRuJq7vAj/Db3odjmjXrOnvPrlLnKBPy5OSOgxe3WyM+op/RFGw90ui +wXZ0xEGuaBL1UgrTlnQBlwHOsz8ChAJKKk+vJdbJvTMNakMl6GxYemNw/lB+XUhiAfUhM/JL KuTKw7pI4EA+ktlKg72lRjWA6bcOI4jUA8mDX6WqR8NhskjD51nR92k8/F+v52p1lDvRctmKY oo5TPy7tCEwbdq3s62M8GTWrP7DeGFLSVOsPUuJRt00rqLSDgUBFhxD+7NUtPUGbg+6QSXlk0 p0TXzn7eT+wDp2Vukn2p9lTptdd+6nne5Z1LPNgscPPowVgtBIb1nea8QGpjy/SdX2Km1p0O/ DbC/ccOpq1+Di8nWE4pSpCpbG5H/XPMC8gdwNqHOVT6WruOu2F+PTrh6Jsjz4RvIGf7y2wNtz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Lj1BsyzpH0o6OkouvskcQffpo8F3Jk5RkuRy8EV7Jt1IqtbWAI08n5c0w7koAVMy/tqU7LY6rT7CA1fkSgG1JT24flZsfjfAiLgbZSBabs/4QQSmHmRGfRyQpEEaFFKrKk4ATdLHChEQCA 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 adds tests for both the happy path and the error path (with and without the BPF_F_PAD_ZEROS flag). Signed-off-by: Jordan Rome --- .../selftests/bpf/prog_tests/bpf_iter.c | 68 +++++++++++ .../selftests/bpf/prog_tests/read_vsyscall.c | 1 + .../selftests/bpf/progs/bpf_iter_tasks.c | 110 ++++++++++++++++++ .../selftests/bpf/progs/read_vsyscall.c | 11 +- 4 files changed, 188 insertions(+), 2 deletions(-) -- 2.43.5 diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c index 6f1bfacd7375..add4a18c33bd 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c @@ -323,19 +323,87 @@ static void test_task_pidfd(void) static void test_task_sleepable(void) { struct bpf_iter_tasks *skel; + int pid, status, err, data_pipe[2], finish_pipe[2], c; + char *test_data = NULL; + char *test_data_long = NULL; + char *data[2]; + + if (!ASSERT_OK(pipe(data_pipe), "data_pipe") || + !ASSERT_OK(pipe(finish_pipe), "finish_pipe")) + return; skel = bpf_iter_tasks__open_and_load(); if (!ASSERT_OK_PTR(skel, "bpf_iter_tasks__open_and_load")) return; + pid = fork(); + if (!ASSERT_GE(pid, 0, "fork")) + return; + + if (pid == 0) { + /* child */ + close(data_pipe[0]); + close(finish_pipe[1]); + + test_data = malloc(sizeof(char) * 10); + strncpy(test_data, "test_data", 10); + test_data[9] = '\0'; + + test_data_long = malloc(sizeof(char) * 5000); + for (int i = 0; i < 5000; ++i) { + if (i % 2 == 0) + test_data_long[i] = 'b'; + else + test_data_long[i] = 'a'; + } + test_data_long[4999] = '\0'; + + data[0] = test_data; + data[1] = test_data_long; + + write(data_pipe[1], &data, sizeof(data)); + + /* keep child alive until after the test */ + err = read(finish_pipe[0], &c, 1); + if (err != 1) + exit(-1); + + close(data_pipe[1]); + close(finish_pipe[0]); + _exit(0); + } + + /* parent */ + close(data_pipe[1]); + close(finish_pipe[0]); + + err = read(data_pipe[0], &data, sizeof(data)); + ASSERT_EQ(err, sizeof(data), "read_check"); + + skel->bss->user_ptr = data[0]; + skel->bss->user_ptr_long = data[1]; + skel->bss->pid = pid; + do_dummy_read(skel->progs.dump_task_sleepable); ASSERT_GT(skel->bss->num_expected_failure_copy_from_user_task, 0, "num_expected_failure_copy_from_user_task"); ASSERT_GT(skel->bss->num_success_copy_from_user_task, 0, "num_success_copy_from_user_task"); + ASSERT_GT(skel->bss->num_expected_failure_copy_from_user_task_str, 0, + "num_expected_failure_copy_from_user_task_str"); + ASSERT_GT(skel->bss->num_success_copy_from_user_task_str, 0, + "num_success_copy_from_user_task_str"); bpf_iter_tasks__destroy(skel); + + write(finish_pipe[1], &c, 1); + err = waitpid(pid, &status, 0); + ASSERT_EQ(err, pid, "waitpid"); + ASSERT_EQ(status, 0, "zero_child_exit"); + + close(data_pipe[0]); + close(finish_pipe[1]); } static void test_task_stack(void) diff --git a/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c b/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c index c7b9ba8b1d06..a8d1eaa67020 100644 --- a/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c +++ b/tools/testing/selftests/bpf/prog_tests/read_vsyscall.c @@ -24,6 +24,7 @@ struct read_ret_desc { { .name = "copy_from_user", .ret = -EFAULT }, { .name = "copy_from_user_task", .ret = -EFAULT }, { .name = "copy_from_user_str", .ret = -EFAULT }, + { .name = "copy_from_user_task_str", .ret = -EFAULT }, }; void test_read_vsyscall(void) diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_tasks.c b/tools/testing/selftests/bpf/progs/bpf_iter_tasks.c index bc10c4e4b4fa..966ee5a7b066 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter_tasks.c +++ b/tools/testing/selftests/bpf/progs/bpf_iter_tasks.c @@ -9,6 +9,13 @@ char _license[] SEC("license") = "GPL"; uint32_t tid = 0; int num_unknown_tid = 0; int num_known_tid = 0; +void *user_ptr = 0; +void *user_ptr_long = 0; +uint32_t pid = 0; + +static char big_str1[5000]; +static char big_str2[5005]; +static char big_str3[4996]; SEC("iter/task") int dump_task(struct bpf_iter__task *ctx) @@ -35,7 +42,9 @@ int dump_task(struct bpf_iter__task *ctx) } int num_expected_failure_copy_from_user_task = 0; +int num_expected_failure_copy_from_user_task_str = 0; int num_success_copy_from_user_task = 0; +int num_success_copy_from_user_task_str = 0; SEC("iter.s/task") int dump_task_sleepable(struct bpf_iter__task *ctx) @@ -44,6 +53,9 @@ int dump_task_sleepable(struct bpf_iter__task *ctx) struct task_struct *task = ctx->task; static const char info[] = " === END ==="; struct pt_regs *regs; + char task_str1[10] = "aaaaaaaaaa"; + char task_str2[10], task_str3[10]; + char task_str4[20] = "aaaaaaaaaaaaaaaaaaaa"; void *ptr; uint32_t user_data = 0; int ret; @@ -78,8 +90,106 @@ int dump_task_sleepable(struct bpf_iter__task *ctx) BPF_SEQ_PRINTF(seq, "%s\n", info); return 0; } + ++num_success_copy_from_user_task; + /* Read an invalid pointer and ensure we get an error */ + ptr = NULL; + ret = bpf_copy_from_user_task_str((char *)task_str1, sizeof(task_str1), ptr, task, 0); + if (ret >= 0 || task_str1[9] != 'a' || task_str1[0] != '\0') { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + /* Read an invalid pointer and ensure we get error with pad zeros flag */ + ptr = NULL; + ret = bpf_copy_from_user_task_str((char *)task_str1, sizeof(task_str1), + ptr, task, BPF_F_PAD_ZEROS); + if (ret >= 0 || task_str1[9] != '\0' || task_str1[0] != '\0') { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + ++num_expected_failure_copy_from_user_task_str; + + /* Same length as the string */ + ret = bpf_copy_from_user_task_str((char *)task_str2, 10, user_ptr, task, 0); + /* only need to do the task pid check once */ + if (bpf_strncmp(task_str2, 10, "test_data\0") != 0 || ret != 10 || task->tgid != pid) { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + /* Shorter length than the string */ + ret = bpf_copy_from_user_task_str((char *)task_str3, 2, user_ptr, task, 0); + if (bpf_strncmp(task_str3, 2, "t\0") != 0 || ret != 2) { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + /* Longer length than the string */ + ret = bpf_copy_from_user_task_str((char *)task_str4, 20, user_ptr, task, 0); + if (bpf_strncmp(task_str4, 10, "test_data\0") != 0 || ret != 10 + || task_str4[sizeof(task_str4) - 1] != 'a') { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + /* Longer length than the string with pad zeros flag */ + ret = bpf_copy_from_user_task_str((char *)task_str4, 20, user_ptr, task, BPF_F_PAD_ZEROS); + if (bpf_strncmp(task_str4, 10, "test_data\0") != 0 || ret != 10 + || task_str4[sizeof(task_str4) - 1] != '\0') { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + /* Longer length than the string past a page boundary */ + ret = bpf_copy_from_user_task_str(big_str1, 5000, user_ptr, task, 0); + if (bpf_strncmp(big_str1, 10, "test_data\0") != 0 || ret != 10) { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + /* String that crosses a page boundary */ + ret = bpf_copy_from_user_task_str(big_str1, 5000, user_ptr_long, task, BPF_F_PAD_ZEROS); + if (bpf_strncmp(big_str1, 4, "baba") != 0 || ret != 5000 + || bpf_strncmp(big_str1 + 4996, 4, "bab\0") != 0) { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + for (int i = 0; i < 4999; ++i) { + if (i % 2 == 0) { + if (big_str1[i] != 'b') { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + } else { + if (big_str1[i] != 'a') { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + } + } + + /* Longer length than the string that crosses a page boundary */ + ret = bpf_copy_from_user_task_str(big_str2, 5005, user_ptr_long, task, BPF_F_PAD_ZEROS); + if (bpf_strncmp(big_str2, 4, "baba") != 0 || ret != 5000 + || bpf_strncmp(big_str2 + 4996, 5, "bab\0\0") != 0) { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + /* Shorter length than the string that crosses a page boundary */ + ret = bpf_copy_from_user_task_str(big_str3, 4996, user_ptr_long, task, 0); + if (bpf_strncmp(big_str3, 4, "baba") != 0 || ret != 4996 + || bpf_strncmp(big_str3 + 4992, 4, "bab\0") != 0) { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + ++num_success_copy_from_user_task_str; + if (ctx->meta->seq_num == 0) BPF_SEQ_PRINTF(seq, " tgid gid data\n"); diff --git a/tools/testing/selftests/bpf/progs/read_vsyscall.c b/tools/testing/selftests/bpf/progs/read_vsyscall.c index 39ebef430059..395591374d4f 100644 --- a/tools/testing/selftests/bpf/progs/read_vsyscall.c +++ b/tools/testing/selftests/bpf/progs/read_vsyscall.c @@ -8,14 +8,16 @@ int target_pid = 0; void *user_ptr = 0; -int read_ret[9]; +int read_ret[10]; char _license[] SEC("license") = "GPL"; /* - * This is the only kfunc, the others are helpers + * These are the kfuncs, the others are helpers */ int bpf_copy_from_user_str(void *dst, u32, const void *, u64) __weak __ksym; +int bpf_copy_from_user_task_str(void *dst, u32, const void *, + struct task_struct *, u64) __weak __ksym; SEC("fentry/" SYS_PREFIX "sys_nanosleep") int do_probe_read(void *ctx) @@ -47,6 +49,11 @@ int do_copy_from_user(void *ctx) 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); + read_ret[9] = bpf_copy_from_user_task_str((char *)buf, + sizeof(buf), + user_ptr, + bpf_get_current_task_btf(), + 0); return 0; }