From patchwork Tue Jan 7 02:06:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13927965 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 58D1BE77188 for ; Tue, 7 Jan 2025 02:06:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D56416B008C; Mon, 6 Jan 2025 21:06:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D058A6B0092; Mon, 6 Jan 2025 21:06:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BCDCC6B0093; Mon, 6 Jan 2025 21:06:48 -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 A15276B008C for ; Mon, 6 Jan 2025 21:06:48 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3C3CE1605EB for ; Tue, 7 Jan 2025 02:06:48 +0000 (UTC) X-FDA: 82979017296.15.30FE645 Received: from mout.perfora.net (mout.perfora.net [74.208.4.194]) by imf17.hostedemail.com (Postfix) with ESMTP id 412A340010 for ; Tue, 7 Jan 2025 02:06:46 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=LLfcfuEL; spf=pass (imf17.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=1736215606; 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=3ePT0FPt9l0Qzkz0xRTw2LrsfjW4Z5KL+i91TUZCoiY=; b=JyffQoIMkwnXVL5Yp327UkQpenk4A1AojxKZgubPUO6xS4x9q80GZDNlL5pz84t6NYUl4Y YSBl1/UOKAOOCE1VV77AyvPfkRkuEr1iI/gdUAsgCtZJIPDBXtU4oy6fTF+OK5ydGLj0A+ M1meQGXIGWD0KsqJCHYmQpeB45qQNKk= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=LLfcfuEL; spf=pass (imf17.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=1736215606; a=rsa-sha256; cv=none; b=FE421Jy2eGGBvocQY+aaIowVjZtMGhOvn1x59qlG17Itp2u7QfJOBSl8ap3WOf6lfAvYya wEsIEeXOJr2dZyYKEF0bdX8Js6ud5ALSrPEeB07oOO9xeUWrOojwa7kMuuJQYpGZsMv9cA u9cKKC7D0B+FhCVEjKbW6gvUwjruuFw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1736215603; x=1736820403; i=linux@jordanrome.com; bh=3ePT0FPt9l0Qzkz0xRTw2LrsfjW4Z5KL+i91TUZCoiY=; 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=LLfcfuELvgrXUfdzetejn93zJtVo9IkugFDPnz8j6EppWF2Gh4TlQ6o1SGnYNaQz uwG39vysRESCxE096SM+Nd4XNMLa1iAwOhFNSp1InLcHUxQsyMhM4B5cVPgXB8df9 SL8okuIyEiJDQFcvQiTzBaBCrC0gGCj5N03tBM4ky/noWSfXJvCWOcqcXb/4Lixfh EkiBKWTzhM9FoELLbJ7bMMvA5C2wgdmn1xvzbNCCsau23O2vCblP0aF6VAYPx1BBN uBrbcRvTvBd+0zwjEhshwk6Zplw8cTNCEHQzaXqSgFJL4Z4HTZhGGBZLJ3WaCkT0m yX6M3/swmOd+bdO8Nw== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.13]) by mrelay.perfora.net (mreueus002 [74.208.5.2]) with ESMTPSA (Nemesis) id 0MHIlZ-1tIfAm04jz-008nC5; Tue, 07 Jan 2025 03:06:43 +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 Subject: [bpf-next v2 1/2] bpf: Add bpf_copy_from_user_task_str kfunc Date: Mon, 6 Jan 2025 18:06:31 -0800 Message-ID: <20250107020632.170883-1-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Provags-ID: V03:K1:swEFtqhmW2Kwgld9kRCHV/0VrQFU6PZ5hV+JiC+7/AR7So7t/EL C/GGahB7cSR1JDBXJ1k2Sly455EiX0+T86stg8IH1wt13fcvppXhcq4p6tpO/wZJJXe7MkF R0q7AODVholSw2csXtbuLreZYLgF4miBagbv28HnOespwAyVXT4VqctkPEedZZDqRavoAxS WrZFxxHGQnYM3yAeOZDPg== UI-OutboundReport: notjunk:1;M01:P0:kxeY5GWJl9s=;kufRiJjbDrVCBGrzqFJbJ54XPzm TNtUTP/O7NGa3bD2XaPmYSMP+hXXdSkF2CcJkns5JFJSwUDcrVYZddEgruubIPZVsACzQuOTu k3v8+FfactRsAcbfBpDwGNMFvhPCsAijVbDrIOJvatCjT1FtCsgr6ylAQ1L1vogATPXs/VAzc icQibjBSP7IgysT5XyotChxt0gaKlpzY/CIPU9kWyeJ+iJQSmD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tzvUgrFl43I2PsqNFDKp/A8NaUxVXd08oKtZxKRQXZZlEAZ6nvxkAkI6atL2VFKpGvQlX597TG0HWl1q2UWjV7wQPafRXnvEnpckd1q1Za0R7g5OqvjDtNbJcLDy6sN+FX+g3yOwuJU7o3MYJoS1H7dGLPVdgaX5QuXg4sv8ZNzX03jKqxqio6A== 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. This required adding an additional function in memory.c, namely `copy_str_from_process_vm`, which works similar to `access_process_vm` but utilizes the `strncpy_from_user` helper and only supports reading/copying and not writing. Signed-off-by: Jordan Rome --- include/linux/mm.h | 3 ++ kernel/bpf/helpers.c | 46 ++++++++++++++++++++ mm/memory.c | 101 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) -- 2.43.5 diff --git a/include/linux/mm.h b/include/linux/mm.h index c39c4945946c..52b304b20630 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2484,6 +2484,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_str_from_process_vm(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/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index cd5f9884d85b..45d41b7a9906 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3072,6 +3072,51 @@ __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 NULL-terminated string from a task's address space to BPF space. + * If user string is too long this will still ensure zero termination in the + * dst buffer unless buffer size is 0. + * + * If BPF_F_PAD_ZEROS flag is set, memset the tail of @dst to 0 on success and + * memset all of @dst 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 count = dst__sz - 1; + int ret = 0; + + if (unlikely(flags & ~BPF_F_PAD_ZEROS)) + return -EINVAL; + + if (unlikely(!dst__sz)) + return 0; + + ret = copy_str_from_process_vm(tsk, (unsigned long)unsafe_ptr__ign, dst, count, 0); + + if (ret <= 0) { + if (flags & BPF_F_PAD_ZEROS) + memset((char *)dst, 0, dst__sz); + return ret; + } + + if (ret < count) { + if (flags & BPF_F_PAD_ZEROS) + memset((char *)dst + ret, 0, dst__sz - ret); + } else { + ((char *)dst)[count] = '\0'; + } + + return ret + 1; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(generic_btf_ids) @@ -3164,6 +3209,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) diff --git a/mm/memory.c b/mm/memory.c index 75c2dfd04f72..514490bd7d6d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6673,6 +6673,75 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr, return buf - old_buf; } +/* + * Copy a string from another process's address space as given in mm. + * Don't return partial results. If there is any error return -EFAULT. + */ +static int __copy_str_from_remote_vm(struct mm_struct *mm, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + void *old_buf = buf; + int err = 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)) { + mmap_read_unlock(mm); + return -EFAULT; + } + + while (len) { + int bytes, offset, retval; + void *maddr; + struct vm_area_struct *vma = NULL; + struct page *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. + */ + err = -EFAULT; + break; + } + + bytes = len; + offset = addr & (PAGE_SIZE - 1); + if (bytes > PAGE_SIZE - offset) + bytes = PAGE_SIZE - offset; + + maddr = kmap_local_page(page); + retval = strncpy_from_user(buf, (const char __user *)addr, bytes); + unmap_and_put_page(page, maddr); + + if (retval < 0) { + err = retval; + break; + } + + len -= retval; + buf += retval; + addr += retval; + + /* Found the end of the string */ + if (retval < bytes) + break; + } + mmap_read_unlock(mm); + + if (err) + return err; + + return buf - old_buf; +} + /** * access_remote_vm - access another process' address space * @mm: the mm_struct of the target address space @@ -6714,6 +6783,38 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, } EXPORT_SYMBOL_GPL(access_process_vm); +/** + * copy_str_from_process_vm - copy a string from another process's address space. + * @tsk: the task of the target address space + * @addr: start address to access + * @buf: source or destination buffer + * @len: number of bytes to transfer + * @gup_flags: flags modifying lookup behaviour + * + * The caller must hold a reference on @mm. + * + * Return: number of bytes copied from source to destination. If the string + * is shorter than @len then return the length of the string. + * On any error, return -EFAULT. + */ +int copy_str_from_process_vm(struct task_struct *tsk, unsigned long addr, + void *buf, int len, unsigned int gup_flags) +{ + struct mm_struct *mm; + int ret; + + mm = get_task_mm(tsk); + if (!mm) + return -EFAULT; + + ret = __copy_str_from_remote_vm(mm, addr, buf, len, gup_flags); + + mmput(mm); + + return ret; +} +EXPORT_SYMBOL_GPL(copy_str_from_process_vm); + /* * Print the name of a VMA. */ From patchwork Tue Jan 7 02:06:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rome X-Patchwork-Id: 13927966 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 C1FBFE77188 for ; Tue, 7 Jan 2025 02:07:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 455CD6B0089; Mon, 6 Jan 2025 21:07:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4066F6B0096; Mon, 6 Jan 2025 21:07:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A6A26B0098; Mon, 6 Jan 2025 21:07:07 -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 00BCC6B0089 for ; Mon, 6 Jan 2025 21:07:06 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B6C13C0624 for ; Tue, 7 Jan 2025 02:07:06 +0000 (UTC) X-FDA: 82979018052.29.59F5B80 Received: from mout.perfora.net (mout.perfora.net [74.208.4.196]) by imf03.hostedemail.com (Postfix) with ESMTP id DF50A20007 for ; Tue, 7 Jan 2025 02:07:04 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=xGorWKdu; spf=pass (imf03.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=1736215625; 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=bFV8AAWE7WB5PNv7bnDePrI5cbphl4Nbhj6dGOr7osY=; b=7lTdR01kLE6VMOoK71r85AUnnBeKp6JS2QHFC6vmJDuEJKl1nl2qRQcuh6CLslvNawwkEd M/DomOQpxrvJZmiwDZKbeevItWfR+icNKHM4G30V30gB9LgApFckfG+lcGWf35Ii6pUZ1G rdC1z4NpfcCAOF/88KS823KXBzbZtQQ= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=jordanrome.com header.s=s1-ionos header.b=xGorWKdu; spf=pass (imf03.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=1736215625; a=rsa-sha256; cv=none; b=FIEECQAo7Hxg6XkCsjw3Iez6aIqLr8B47JFdQU1OsWA2k07Xg7vXmeLyLfvOwvncfwtRVL NwT7jrT6TRcl6xhK6JziCSbz84QX0kVh5ENCEh6qb2WCVMifS7ntqTmg424roWBCLFB3E3 fU3Tg9y9Lo3Tx2KIdn6HRGia62aHBrA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jordanrome.com; s=s1-ionos; t=1736215607; x=1736820407; i=linux@jordanrome.com; bh=bFV8AAWE7WB5PNv7bnDePrI5cbphl4Nbhj6dGOr7osY=; 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=xGorWKdulKWxh1MyTFrXTIZYCFa9EweLvxPP+7DUeu9oJlWanSDt1X+tmV19shJr gr7c4nnnovIB3mbqsFL3Ok7mi7SqKkHfE3HA6+yytAscYcnX0YGSikc6Lv+WmnUl6 HFf9ubLuno8ovvY10Ig0d99K8teSFsg4nqy3bkTCX4UYuwX8hHN0jIlURdWqlrl4P JPZD2dctxC/j0JPTIWI/z3GU248jv/G7n1tEaHu7MvuZbScqMUKTChuj5qqhgdF52 N/OGGikmojyTBz38xEftUGTX4EgwapamP06YUqKjutBkd3vTV/Vky8C7rWzUOduOi 5zHnQKIgc1KeRZhiEg== X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from localhost ([69.171.251.1]) by mrelay.perfora.net (mreueus004 [74.208.5.2]) with ESMTPSA (Nemesis) id 1MJmjN-1tBWKk0mAT-00OUD6; Tue, 07 Jan 2025 03:06:47 +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 Subject: [bpf-next v2 2/2] selftests/bpf: Add tests for bpf_copy_from_user_task_str Date: Mon, 6 Jan 2025 18:06:32 -0800 Message-ID: <20250107020632.170883-2-linux@jordanrome.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250107020632.170883-1-linux@jordanrome.com> References: <20250107020632.170883-1-linux@jordanrome.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:1J9yVCZQ2NAHDr9yT0npIqJaYWAKXqcpvuue9cvHHU8bECyraFe TJN/mIMwB+l2oA2KtEmxyhlhfmONio2neaoKrXUeqWFW0cWVjQGKVV99WHFx5M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WjqRuWA/MX1W/1cJDtAIfvVVLV/fDKALZDSc0S78XdHp5 8lDXEIZyVYJGsyiHSqHrNEWKNgjYHZGHlePWlsikuhj3swRGPUhd2GMYwBsFjLXCocYHT9rfV CEXS5b6x2YKUyEeYyxVC0W4sA5ZfR8Dc/CuNU1/ X-Rspamd-Queue-Id: DF50A20007 X-Stat-Signature: u6esx4p7ohgfgpb5jpm46qsrsmm5kwj1 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1736215624-84197 X-HE-Meta: U2FsdGVkX19xjy/fVDPSt19xtQo0dfytEybCMEkjzMmSs9G7ld6eC65EQ6VCBfQgs/eWOZ2l52sSpBjGoJXlXsbTMlEt75xTQjDOVa0pjf5QSzmqdZpYQcTwVu3xKnQiOYsdVZBinZHAjRvSUPphQJEk9gFhAK16GBdXADAvxtBcANga4sBFSftxTbeygCwS99JuzQT9PxYHfYMojnrYJyQ/0cWQWU6UJxwhhfbkI8qyVt8JYWq8rhkcRkiAhTBWvIWrWD5OHk7AnyQstU3s2t0IwRFjopAH0l9WbqXllFkR5gBVuuQBBpGUs4ljvt4oWH9YM3Llpn9JPdQLDga1xfrEJy9BVFuEbGAj2O19Tx0eZafo2ol8Oa/mw5WnCFeYofQXVowBIYR8KS2WqH428/pFb/elb0vqXnZy4toPkBspQyt8HgRlta111gGorXapCtUIhh8cbmP9UrFaoEu1YbO6cy3dQj6P0QwA2UndEc+mGTKqcY524xCHsif1VelMV8AffHVDTcVth1Hx7vt+suSKea5iyUZAG4z8Cu+WdoAfan1PmxA24vnzl31cZmZF4KIXCOQk/wBCh6BoHLHu/R5Wgf2WmaspluPp1w69PAiW8RRcETOSE2It/Vx2B9s/jTD0SX5BZzFHACn2UwwILV5lFprSJrwCHjQ5cpR5FeM/5Xns+UYJJifIhfwaaE2zix9Kyt148JWYX0fT165n4TU7EDXxFheuT3oaQuiBHd4NZWvqMXDy/qpN6VlUCDiC0EdHq5Rp9p3+YUKBg10WWDL782/hexWowJvAme03vrrLWpTvVVQAiDq1tR4swIhzBkWC3z0t1hqW6w0DWdYX/qfaw6REsQx8us8fAmxoUrcKYs4RJ5ix4qm5WAuFcgK9Fq/JQ893jyM6S04A9bsQhkZn0MXGt8eHB/qpYz0dNYJh417dWbVb7S34t0StGr7iRmofarLwRlurLE3gbVU tJRBVlMf Zmwua+agQVwd84XrViVgNelhJ1TBtFnwzuLft7pDklZ2DH9PmVGdb0Ri3gpDDbUEpTtzUz520Rn/yTzuucnp5Nv3zZ3BH49C+mrvv+VBQ+wvIJ6xh5Hx9PGXs8On2ZxqAvT00uWRIpjkkoEoTrQWvigV3zWCJqwjr/5j7u42qpv/W3xfsTL4Z1W3orWf5JZiwTmEGsNzr7mmlBuwrm4fmcR7t1DAm7gfBhs+9ock+UtSDPX6TgU7F8fbSCiJSOEB+T4l+zuKd9yvsmhwf9hZ75nUdN4ik7voYuVz/Qjzy0h7Zvn4tqi2oQWbrjsdhdiKcmU89VuDP7hsP+7yIACPbfiBFLw== 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 | 7 +++ .../selftests/bpf/prog_tests/read_vsyscall.c | 1 + .../selftests/bpf/progs/bpf_iter_tasks.c | 55 +++++++++++++++++++ .../selftests/bpf/progs/read_vsyscall.c | 6 +- 4 files changed, 67 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..8ed864793bd1 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c @@ -34,6 +34,8 @@ #include "bpf_iter_ksym.skel.h" #include "bpf_iter_sockmap.skel.h" +static char test_data[] = "test_data"; + static void test_btf_id_or_null(void) { struct bpf_iter_test_kern3 *skel; @@ -328,12 +330,17 @@ static void test_task_sleepable(void) if (!ASSERT_OK_PTR(skel, "bpf_iter_tasks__open_and_load")) return; + skel->bss->user_ptr = test_data; 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); } 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..90691e34b915 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter_tasks.c +++ b/tools/testing/selftests/bpf/progs/bpf_iter_tasks.c @@ -9,6 +9,7 @@ char _license[] SEC("license") = "GPL"; uint32_t tid = 0; int num_unknown_tid = 0; int num_known_tid = 0; +void *user_ptr = 0; SEC("iter/task") int dump_task(struct bpf_iter__task *ctx) @@ -35,7 +36,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 +47,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 +84,57 @@ 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') { + 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') { + 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); + if (bpf_strncmp(task_str2, 10, "test_data\0") != 0 || ret != 10) { + BPF_SEQ_PRINTF(seq, "%s\n", info); + return 0; + } + + /* Shorter length than the string */ + ret = bpf_copy_from_user_task_str((char *)task_str3, 9, user_ptr, task, 0); + if (bpf_strncmp(task_str3, 9, "test_dat\0") != 0 || ret != 9) { + 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; + } + + ++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..623c1c5bd2d0 100644 --- a/tools/testing/selftests/bpf/progs/read_vsyscall.c +++ b/tools/testing/selftests/bpf/progs/read_vsyscall.c @@ -8,14 +8,15 @@ 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 +48,7 @@ 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; }