From patchwork Tue Dec 17 23:37:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13912705 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2091.outbound.protection.outlook.com [40.92.90.91]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 08A881E489; Tue, 17 Dec 2024 23:38:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.91 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734478711; cv=fail; b=IxPmwpqHOaIrLVeHOeTnAbe5+nKRH3KTUXxk/YJEQbKveoCzkO/wuf1C3/yAD2x14+2Fmxw8DJ0UYNsHacNQoqNlUfY1jfkWX/pET+1IdXNqTi1xm70uKVsFYiEsC9t+VHy1oBvP2Wrz9kKeA8CrB3Itx3fusXCHa1qdsMLv+pU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734478711; c=relaxed/simple; bh=vEZUKwdceXRJn7QWyvgBDn2Qwv35oHDkljrnPox/JKk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=dz2CDeQWXamblEeeKbg+FIVAx1eIfbvD6cbk3shrer5AgaHYc47QExQUI6hcuIXTwenDxwUxj7Rd7ZdJL8B9TeQdO+tCw48EdmH0noTl+bmJiYbDbs1HXEHZigDVr0iQLLZmdBtxU9FdBtOS77KOFEAGlurdM4qsuy09wqh2DG8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=JJxCgpdT; arc=fail smtp.client-ip=40.92.90.91 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="JJxCgpdT" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=w1Ogg/uCXdfVX6YWNduwDRAk4cVHKbYbp6P6fbnLpz7ZLcCguSAGIpLBx1iLFM7GkOU1ZKfLFL+VavAFfXo3WO6vjmi0Hgu96NDGNeNtFivpW4c/P1ro579hIrP/g1JpeEqa0oHquTZpHoA3OMelJseFgg3mM1Gwf+tyAbiM33ODwmLMk2+yViu1qz32DjhwKHa4u08Oph2AUBI9Ao/pDtQwAkaC/NUqsx6UROWUG5zHUeX2kQKtHH2OhHfZipRMwEqfP/f7ejXmcD92UCqptw/o1iD4Sqb4CUUrEaF5sQrSVYlDyZVHhQF9l3D3TL5Ene1CdAaWSpaIY0Uaj7BOpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=lpX9may4ZFhg+0QB0gs5iq3gsgRIQcvjtnbFkTIR2K4=; b=DsL0O0jzV/3suZU2A5ByHy5rzFUoaoZ4DCePHECOOX5ZHTvvGgJkC1FBRI51U65bcmgpMz8BTeY/b6NhnqT9ZIL0rAnojwbhZ1ekFy/dl9Zvh1w5duv+hJFx7VbIMuLhF4PUuZWIJQUrUl7i5qEAcJZzpwE6W3nFfMKf1e6+Qtdpa/TY4o+/99SSOdiBWcJo8R+hWW2JfPV13vCLFxRrk31pkjaLm36vfWPmEGGVs5CIuYWADB1ilzDw0NgLAaPDNstE0J2wLXw+vgIEuZNl1Hg3xI3ljCAsWkTW8s8eaQsAYr9pj+gGzMrQ+WHTHIPLPk41CRp2lNZLbzAtcFuJnA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lpX9may4ZFhg+0QB0gs5iq3gsgRIQcvjtnbFkTIR2K4=; b=JJxCgpdTK/49qysoPR5AjA387oaqlCBTiBSBaV9Z45oW4bHDDy9MzaRPj1C8DuOEOZsqUiUspPcZ9uqVlD/mZogNZyYjvU+D//OZ4lURcwbDv/4RMb/+5VOY2uTg5zR5p+GfZgIWoZY0bTlvvylClzB/Xomwzd4ARmgCDW6b2xXPsBOQsYHcX08XE7BqH9m1XQO4t9CnQY3TjOXmxQGShddOxis4vnNacPmd07yCOiBOt+Mc8W5KUkTQb5UgSqW0r1HY1/sD0keBcdIBBHVMVKuFkn09u870k9V+S9Rs+/CB7GiSKNkflN+BI6v75aTO4cPn1fl0ks0odjqlrz0V5g== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by VI0PR03MB10927.eurprd03.prod.outlook.com (2603:10a6:800:26b::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.21; Tue, 17 Dec 2024 23:38:25 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%5]) with mapi id 15.20.8272.005; Tue, 17 Dec 2024 23:38:24 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com, brauner@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH bpf-next v6 1/5] bpf: Introduce task_file open-coded iterator kfuncs Date: Tue, 17 Dec 2024 23:37:01 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0117.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:192::14) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241217233705.208089-1-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5080:EE_|VI0PR03MB10927:EE_ X-MS-Office365-Filtering-Correlation-Id: 337f8517-8427-451f-875d-08dd1ef3e640 X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|8060799006|5072599009|461199028|15080799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: /4wyIEWJauodKoFmZ8hJ9I/lyj5kCy2BkyLpZkLxSy7qEtuWIZ4Ek7ntT0ygx7UGo7kYya3B4IgNGnR+0crEk1BiiKcCrPfKsv7IyIQ+KT7BsM1DJvfgttbeAQYfdXEeZNgKQeBUkofRA2qo4NhdD2Zh21IjQ+p22uM9lNrhK7OjpJPCjDUJVn+fCcnyZaFATSUwPyE4xIYxfpe1oezCO75jRpDFf9e89y3454ZNM09g0moZEfURL1J19eqGsw2DGCpHxstKnxlilGnmpZcNvjdM1LYU9XAioCGbDONHDRfFVO4bQOo2eEVaVL4PEb+yEcwDkt4rNO/YSRhLutsyxpfOBNluieJp92EvV9/c/G3w3sVEDhhCdcySnoCaiOMZ968ICzQNM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cBUL/F7DQrTo+m9VX3fb+rA9UqHlSc3GjgV8oift68guZxlu+2XPSLVcD7iO5NVXX/cAvrj2CrQWDYcvZhXBolrrFb0btcdZNDebCT+VBXMP3i+DJRC5RMD+BGF37qTwGs95GbH1ipTe+bFlT27TQwsuqZpNN7SGipFCGIWTVqytwXCUY366XsKSrSp2i3fyRc3pPfTB9x9uNfbHdz4xUA/Ua+LkPN0l1poMYK+SZwO7swfNPWjD5BZxGHyjw1l+Ame8vWobMNTiUCQ3elIMWh1NedcDRxrGHeOOVYQGeq1cNO5I8cVxCCXZUbNosyXA6R7hdeUupBzDakCutbY1HX4xwe6+cDQRcto1TdFCNb8+K6jGKZLsXRkJCnXPJKctKJ88wzKfUp6oZW5KzHcpwceuBd9pStoLYfDNpsZWmRyWnPVZQRY9G5thbFjuT3bjc6dcDju34EThWy/uqc4PifbWLWezynFYBUFdIx/nVO80jUU0ZYjLcqutENz2pdIuLcSbZZWy/7FvApfn/Cj9OvIP713PuMJB4hsC/tQBjTWHwAzjhtFXZZfCsKdhBX6oS0vEXVczUeDgWWKBfosCVvn4jr//3ynz3qBtAWmZOUfPLU+x3bXUnR0puzRq23c3+SSZzak5/Ywr4PptZZtHH2B8PGlAAI2PUa4u8HNR8/B8RqjcvLEaf0i9Q7WBDdX35BxQ8jJsMCbbKbw75cBVxeO6B4pLxx7Jy5k33B7APQhmbtJCnXwGYnMx1rxo0vsoMP7FLOSLF34Z3Qvgil83e3mqztIqSFdplmELn3gI0B+mDmsFjKZKQQxVtJLgy0RP3M/D1XgnJN9ueSMBAfHwit6Ir6FSxCOXS9whDbGam22wyvbkGdgb6WI2kJp6F9v4T1YO7wBhUCaUhV1m9Z74tP X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 337f8517-8427-451f-875d-08dd1ef3e640 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 23:38:24.7806 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI0PR03MB10927 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds the open-coded iterator style process file iterator kfuncs bpf_iter_task_file_{new,next,destroy} that iterates over all files opened by the specified process. bpf_iter_task_file_next returns a pointer to bpf_iter_task_file_item, which currently contains *task, *file, fd. This is an extensible structure that enables compatibility with different versions through CO-RE. The reference to struct file acquired by the previous bpf_iter_task_file_next() is released in the next bpf_iter_task_file_next(), and the last reference is released in the last bpf_iter_task_file_next() that returns NULL. In the bpf_iter_task_file_destroy(), if the iterator does not iterate to the end, then the last struct file reference is released at this time. Signed-off-by: Juntong Deng --- kernel/bpf/helpers.c | 3 ++ kernel/bpf/task_iter.c | 91 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index cd5f9884d85b..61a652bea0ba 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3147,6 +3147,9 @@ BTF_ID_FLAGS(func, bpf_iter_css_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_iter_task_new, KF_ITER_NEW | KF_TRUSTED_ARGS | KF_RCU_PROTECTED) BTF_ID_FLAGS(func, bpf_iter_task_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_task_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_iter_task_file_new, KF_ITER_NEW | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_task_file_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_task_file_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_dynptr_adjust) BTF_ID_FLAGS(func, bpf_dynptr_is_null) BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index 98d9b4c0daff..149a95762f68 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -1027,6 +1027,97 @@ __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it) { } +struct bpf_iter_task_file_item { + struct task_struct *task; + struct file *file; + unsigned int fd; +} __aligned(8); + +struct bpf_iter_task_file { + __u64 __opaque[4]; +} __aligned(8); + +struct bpf_iter_task_file_kern { + struct bpf_iter_task_file_item item; + unsigned int next_fd; +} __aligned(8); + +/** + * bpf_iter_task_file_new() - Initialize a new task file iterator for a task, + * used to iterate over all files opened by a specified task + * + * @it: the new bpf_iter_task_file to be created + * @task: a pointer pointing to a task to be iterated over + */ +__bpf_kfunc int bpf_iter_task_file_new(struct bpf_iter_task_file *it, struct task_struct *task) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + struct bpf_iter_task_file_item *item = &kit->item; + + BUILD_BUG_ON(sizeof(struct bpf_iter_task_file_kern) > sizeof(struct bpf_iter_task_file)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_task_file_kern) != + __alignof__(struct bpf_iter_task_file)); + + item->task = get_task_struct(task); + item->file = NULL; + item->fd = 0; + kit->next_fd = 0; + + return 0; +} + +/** + * bpf_iter_task_file_next() - Get the next file in bpf_iter_task_file + * + * bpf_iter_task_file_next acquires a reference to the struct file. + * + * The reference to struct file acquired by the previous + * bpf_iter_task_file_next() is released in the next bpf_iter_task_file_next(), + * and the last reference is released in the last bpf_iter_task_file_next() + * that returns NULL. + * + * @it: the bpf_iter_task_file to be checked + * + * @returns a pointer to bpf_iter_task_file_item + */ +__bpf_kfunc struct bpf_iter_task_file_item *bpf_iter_task_file_next(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + struct bpf_iter_task_file_item *item = &kit->item; + + if (item->file) + fput(item->file); + + item->file = fget_task_next(item->task, &kit->next_fd); + item->fd = kit->next_fd; + + kit->next_fd++; + + if (!item->file) + return NULL; + + return item; +} + +/** + * bpf_iter_task_file_destroy() - Destroy a bpf_iter_task_file + * + * If the iterator does not iterate to the end, then the last + * struct file reference is released at this time. + * + * @it: the bpf_iter_task_file to be destroyed + */ +__bpf_kfunc void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + struct bpf_iter_task_file_item *item = &kit->item; + + if (item->file) + fput(item->file); + + put_task_struct(item->task); +} + __bpf_kfunc_end_defs(); DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work); From patchwork Tue Dec 17 23:37:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13912708 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05olkn2053.outbound.protection.outlook.com [40.92.91.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 234671E489; Tue, 17 Dec 2024 23:39:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.91.53 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734478795; cv=fail; b=UnUN0cYIVJKG68+sPwINuEzTbNKdKxCFb+Fxb0sROI4oce32poAxDsNvwDVWJgEAiAI5veB9sfT8GHHcuaCN/t7RilN8h0KjTrC/2OA8e9+Gkys/aHpv+2ylFJrsylfNdy8GKDMgjQv6Y9C3grBnMfuFW1gttQMeDaNCI7+52Zw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734478795; c=relaxed/simple; bh=kuBz/7vH7Rj2W0pBLgQzDhL5QWXpGKpmwS852TxxuzE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=k/t6658cXG0D7tjVcgcy9k7HVJRhbNzSDaWdTNBB/gQ2os153dSD/CQZjA34vGQ3EopGxmtYvdYz+P3+McfjqbdeyYNIpOy0Xk9QQkVOVWJ2vMoSedA+Ubny9e/WS/VqgFpI472CQm0070aZantXkdH3Jxja3MbUqdv4coHrWHw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=YtoQyASp; arc=fail smtp.client-ip=40.92.91.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="YtoQyASp" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=KfQxtRan6eDI3UrJQonvxOuFwpkq0JwGAAaDx8on3Jje6wuRp2xQVoCi8fNKVnZOTbkLeC6xs3eLTmlMPrV4LGKtfn5odLTroZNE5YPrMOz/2hyaTLslZ2OFAfUTiDyHcaNWn2OBI7tcxGHrpmY3OhSKz/KQT46qljNRTYcipk69norkNdOX4fOjTpfUo6VDhV0VpZaSnXZoQJdljX5bxkzlt+go/R/9I2kHwqrIYXAsgsv2YSysWCPUNNCu0LSb3IEO4+uDgewi/QtP/81wPjuTWIgbmO1gS1ynTZDwfhIzp6L5DPo+NAspxEdC//J1vMcOVGpa4WwGTMEtot9tTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=CncPRTiZu4tnS+BqX9DWEhZoGG4ps2jYS3F22V4SvIo=; b=Tb9b9NRoatsQwEjGAkllTOkwmWEGCDJPfFeqU9HPRUPbYRSHy++OVAPvaHadWrJThlZjgEdwklCihke/C9S4duw8s6w0K0DG4unEbBy9Se5tSzP2NnWYcae1TuBBXxNoZlzyU2IiiTSqU7FIyH+yrDJTItWdFCYS4njrMM/U8sLzudLVuMGpNSBBjR3sXiMdaYS7BgE9Kgg32zbTZKRyutRW7yBpEtgqekKwA9pj7a5qMYuJ5Ihi+2h7zIwi0CEpUOwyylc9ElpFH87+FPMorNf/uV5N1RQUK/RrcAWwoJvY72iTJozXXUt8vpn0NxP7y2/0BvCfTGSnlh7yy+mGJA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=CncPRTiZu4tnS+BqX9DWEhZoGG4ps2jYS3F22V4SvIo=; b=YtoQyASpccATrOC94x8SNMJnhYFBE7E2VLjXLLxGz6Yb8X2BpAz12UcfvB5cHIBZ2Znb8prRR8SUOpqR1E6EXWDYM9J6+flbC4t1EFVC+VjGr3g//nqHSBtyfCFdwgDBFcDf53zhhsujD9mOuiYAFoHGS5IIG7bHW+KMhMpgX2ixYRtIqStM0vpDg5PBvs0K2VUVWtZyrd5tUNhW41T2H5pu04Djb7SZwl3PmLFiVN6S+Y7lnACR82NgG7Jz4T3UsEIAZEeF20vtQBbzrbJcfDvpwoCFPaQZ+WhGg9V4eJ5PADvjTcTLZySgSpBUW0uSF4Bcj56DM8IZzZuUhcaVkA== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by VI0PR03MB10927.eurprd03.prod.outlook.com (2603:10a6:800:26b::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.21; Tue, 17 Dec 2024 23:39:50 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%5]) with mapi id 15.20.8272.005; Tue, 17 Dec 2024 23:39:50 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com, brauner@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH bpf-next v6 2/5] selftests/bpf: Add tests for open-coded style process file iterator Date: Tue, 17 Dec 2024 23:37:02 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0117.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:192::14) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241217233705.208089-2-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5080:EE_|VI0PR03MB10927:EE_ X-MS-Office365-Filtering-Correlation-Id: 5a51f5da-b4e3-4b40-1998-08dd1ef41921 X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|8060799006|5072599009|461199028|15080799006|440099028|3412199025; X-Microsoft-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CZkF9VF+dWwX+yVUDa0TwPPCyRshmCwbrgQofXP2wOwWa2z1A1WfIcIgh1jXrftuoMLxkqVId7+ugh+L2SJFfBRFF7AiddgRGCVd7LP79tVYmEyOqQWYA3HMBgpwHWpgMSR/hYGy8kuA5KKhXT1cmuhcfk/FLKkl6Pnv3bqoww8cr7L+d7QQ1Sk8M88a96fG+gxWuSIhd612RkxD2XhXDIW7OgovxurapQYbT33rA X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5a51f5da-b4e3-4b40-1998-08dd1ef41921 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 23:39:50.1246 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI0PR03MB10927 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for open-coded style process file iterator. Test cases related to process files are run in the newly created child process. Close all opened files inherited from the parent process in the child process to avoid the files opened by the parent process affecting the test results. In addition, this patch adds failure test cases where bpf programs cannot pass the verifier due to uninitialized or untrusted arguments, etc. Signed-off-by: Juntong Deng --- .../testing/selftests/bpf/bpf_experimental.h | 7 ++ .../testing/selftests/bpf/prog_tests/iters.c | 79 ++++++++++++++++ .../selftests/bpf/progs/iters_task_file.c | 86 ++++++++++++++++++ .../bpf/progs/iters_task_file_failure.c | 91 +++++++++++++++++++ 4 files changed, 263 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/iters_task_file.c create mode 100644 tools/testing/selftests/bpf/progs/iters_task_file_failure.c diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index cd8ecd39c3f3..ce1520c56b55 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -588,4 +588,11 @@ extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksym extern struct kmem_cache *bpf_iter_kmem_cache_next(struct bpf_iter_kmem_cache *it) __weak __ksym; extern void bpf_iter_kmem_cache_destroy(struct bpf_iter_kmem_cache *it) __weak __ksym; +struct bpf_iter_task_file; +struct bpf_iter_task_file_item; +extern int bpf_iter_task_file_new(struct bpf_iter_task_file *it, struct task_struct *task) __ksym; +extern struct bpf_iter_task_file_item * +bpf_iter_task_file_next(struct bpf_iter_task_file *it) __ksym; +extern void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) __ksym; + #endif diff --git a/tools/testing/selftests/bpf/prog_tests/iters.c b/tools/testing/selftests/bpf/prog_tests/iters.c index 3cea71f9c500..cfe5b56cc027 100644 --- a/tools/testing/selftests/bpf/prog_tests/iters.c +++ b/tools/testing/selftests/bpf/prog_tests/iters.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ +#define _GNU_SOURCE +#include #include #include #include @@ -16,11 +18,13 @@ #include "iters_num.skel.h" #include "iters_testmod.skel.h" #include "iters_testmod_seq.skel.h" +#include "iters_task_file.skel.h" #include "iters_task_vma.skel.h" #include "iters_task.skel.h" #include "iters_css_task.skel.h" #include "iters_css.skel.h" #include "iters_task_failure.skel.h" +#include "iters_task_file_failure.skel.h" static void subtest_num_iters(void) { @@ -291,6 +295,78 @@ static void subtest_css_iters(void) iters_css__destroy(skel); } +static int task_file_test_process(void *args) +{ + int pipefd[2], sockfd, err = 0; + + /* Create a clean file descriptor table for the test process */ + close_range(0, ~0U, 0); + + if (pipe(pipefd) < 0) + return 1; + + sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sockfd < 0) { + err = 2; + goto cleanup_pipe; + } + + usleep(1); + + close(sockfd); +cleanup_pipe: + close(pipefd[0]); + close(pipefd[1]); + return err; +} + +static void subtest_task_file_iters(void) +{ + const int stack_size = 1024 * 1024; + struct iters_task_file *skel; + int child_pid, wstatus, err; + char *stack; + + skel = iters_task_file__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + if (!ASSERT_OK(skel->bss->err, "pre_test_err")) + goto cleanup_skel; + + skel->bss->parent_pid = getpid(); + skel->bss->count = 0; + + err = iters_task_file__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto cleanup_skel; + + stack = (char *)malloc(stack_size); + if (!ASSERT_OK_PTR(stack, "clone_stack")) + goto cleanup_attach; + + /* Note that there is no CLONE_FILES */ + child_pid = clone(task_file_test_process, stack + stack_size, CLONE_VM | SIGCHLD, NULL); + if (!ASSERT_GT(child_pid, -1, "child_pid")) + goto cleanup_stack; + + if (!ASSERT_GT(waitpid(child_pid, &wstatus, 0), -1, "waitpid")) + goto cleanup_stack; + + if (!ASSERT_OK(WEXITSTATUS(wstatus), "run_task_file_iters_test_err")) + goto cleanup_stack; + + ASSERT_EQ(skel->bss->count, 1, "run_task_file_iters_test_count_err"); + ASSERT_OK(skel->bss->err, "run_task_file_iters_test_failure"); + +cleanup_stack: + free(stack); +cleanup_attach: + iters_task_file__detach(skel); +cleanup_skel: + iters_task_file__destroy(skel); +} + void test_iters(void) { RUN_TESTS(iters_state_safety); @@ -315,5 +391,8 @@ void test_iters(void) subtest_css_task_iters(); if (test__start_subtest("css")) subtest_css_iters(); + if (test__start_subtest("task_file")) + subtest_task_file_iters(); RUN_TESTS(iters_task_failure); + RUN_TESTS(iters_task_file_failure); } diff --git a/tools/testing/selftests/bpf/progs/iters_task_file.c b/tools/testing/selftests/bpf/progs/iters_task_file.c new file mode 100644 index 000000000000..47941530e51b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/iters_task_file.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "bpf_experimental.h" +#include "task_kfunc_common.h" + +char _license[] SEC("license") = "GPL"; + +int err, parent_pid, count; + +extern const void pipefifo_fops __ksym; +extern const void socket_file_ops __ksym; + +SEC("fentry/" SYS_PREFIX "sys_nanosleep") +int test_bpf_iter_task_file(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct bpf_iter_task_file_item *item; + struct task_struct *task; + + task = bpf_get_current_task_btf(); + if (task->parent->pid != parent_pid) + return 0; + + count++; + + bpf_iter_task_file_new(&task_file_it, task); + + item = bpf_iter_task_file_next(&task_file_it); + if (item == NULL) { + err = 1; + goto cleanup; + } + + if (item->fd != 0) { + err = 2; + goto cleanup; + } + + if (item->file->f_op != &pipefifo_fops) { + err = 3; + goto cleanup; + } + + item = bpf_iter_task_file_next(&task_file_it); + if (item == NULL) { + err = 4; + goto cleanup; + } + + if (item->fd != 1) { + err = 5; + goto cleanup; + } + + if (item->file->f_op != &pipefifo_fops) { + err = 6; + goto cleanup; + } + + item = bpf_iter_task_file_next(&task_file_it); + if (item == NULL) { + err = 7; + goto cleanup; + } + + if (item->fd != 2) { + err = 8; + goto cleanup; + } + + if (item->file->f_op != &socket_file_ops) { + err = 9; + goto cleanup; + } + + item = bpf_iter_task_file_next(&task_file_it); + if (item != NULL) + err = 10; +cleanup: + bpf_iter_task_file_destroy(&task_file_it); + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/iters_task_file_failure.c b/tools/testing/selftests/bpf/progs/iters_task_file_failure.c new file mode 100644 index 000000000000..66e522583850 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/iters_task_file_failure.c @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "bpf_experimental.h" +#include "task_kfunc_common.h" + +char _license[] SEC("license") = "GPL"; + +SEC("syscall") +__failure __msg("expected uninitialized iter_task_file as arg #1") +int bpf_iter_task_file_new_inited_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task; + + task = bpf_get_current_task_btf(); + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + return 0; +} + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg1") +int bpf_iter_task_file_new_null_task(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task = NULL; + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + return 0; +} + +SEC("syscall") +__failure __msg("R2 must be referenced or trusted") +int bpf_iter_task_file_new_untrusted_task(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task; + + task = bpf_get_current_task_btf()->parent; + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + return 0; +} + +SEC("syscall") +__failure __msg("Unreleased reference") +int bpf_iter_task_file_no_destory(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task; + + task = bpf_get_current_task_btf(); + + bpf_iter_task_file_new(&task_file_it, task); + + return 0; +} + +SEC("syscall") +__failure __msg("expected an initialized iter_task_file as arg #1") +int bpf_iter_task_file_next_uninit_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + + bpf_iter_task_file_next(&task_file_it); + + return 0; +} + +SEC("syscall") +__failure __msg("expected an initialized iter_task_file as arg #1") +int bpf_iter_task_file_destroy_uninit_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + + bpf_iter_task_file_destroy(&task_file_it); + + return 0; +} From patchwork Tue Dec 17 23:37:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13912709 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2041.outbound.protection.outlook.com [40.92.89.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D43431DDC1E; Tue, 17 Dec 2024 23:43:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.41 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734478991; cv=fail; b=rncxiFGHqWg25HwJOTH9GrprIZ0eRxEPU5LQhX8JloqxHzNpccsAlvWnCrq0Rl5aqWgqmpo7RcTQykx5JAo12JHGKBSV6QGGtvpMXriBOqGSmKk/u8uIts1L1WiwlKdzsfWhK/p9EKQFk1rTgP7JvEWMmvICb++9b1vT20w7hN8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734478991; c=relaxed/simple; bh=KIxnMIr3pjygOZQfnbAT4gl8laW2esmTBi9+dragCrw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=oNKKOiJWFuL6zDukwKje6VwnMiCDJfoieo39WIvzv3/VKgrj3q6qnzFjdDfj5c8GSEdg8ETkSneFrxNOOyBgJYG0MU4cqdeT7odZnaTH1qaYgsU66NSaEgn9S4uChUC9/DP/FFE0Om4MIU5YwdmeqkBCUQojWFgAoOi3Rk4ONw8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=hX3hcR5R; arc=fail smtp.client-ip=40.92.89.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="hX3hcR5R" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xTFfgvOmY/40U8ELMJZyBqKy/wnBQWbWtZokm+b2vWKKz8kSCOeqq3c5E5Z/TW5ELwLWwkOi4T5TOCXQ0ZooMh58f/MDyCpOWpz6zEhuEDpaJ5cVnNbEJLkXiiPtPRmFTJP7+fMACU+A9AT7gFokCfahWB3aH/QSfNvJfUSU+LYHg1uc0dk8mVHXUyHwjVsxD7HDCFzZqRN1XTrFnH2+fk5ExTOFMdA/C8yh6rQyucqvnuo/tW/elZEYPT/yHxbutwFizw14se8wxcg0GddKt818n9icP2/XuPBH7CF82CV6OxxVnKLb05pYoDb3rF0dkr6UZpNX7S+y64QTrm+WZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QRVV0jX1KjzggGbvz87UhS2xPIc9n8VFZ0MWXdCk1fc=; b=T7eADwxNFBbE1eS6YJZlRhrkmwuvkS1bHvYUlu/7D67wwnfB3pREys6Sww2cV3iJd9yhlfKj80qhnjEONYuAlKNhkhgd4HzyvK7JhBw9r6fQ9g3TtHCKnNz3aBvg6dJaUedHNflRLI/yrMCwdJ7hxRIZOhSqPiON9ZuAPvkzR0VXFskMZOdZOl3Q6ks/InGb3FaW3FA5yZnKQsek0y+5zjbs/BBGtgDp1DLbQm4pZU577Rb6DWUk6eqIQzPTkqpsS12bqm4Pq0L3Q+a8vqq/lrdPF5g+gGh0lEnTgNkijNMDCh3ick5HZyHdoJkiNCiPDYvq44UGvAXHofTCULukZQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QRVV0jX1KjzggGbvz87UhS2xPIc9n8VFZ0MWXdCk1fc=; b=hX3hcR5RxivWIWVijSeY2zSJSZ74i+Jope63OfB21sVhwFiFMGfLpA57s5oegfTd04pY+5tiNSyQqxWkOim6xCMkIiRVsudxSBsvcAN9wPg8qsakQJ15VXO6O2AEDyNgr50x6hcaWH1+Q5pKBYQs++S8E6nqXqQK45fri2bFj5Pwexh6BTN+/0MeSiEcANtavKyieEC/A+hcmOSJ3fAfvv3x66pl7KCuJYPfkRbeAwfGrIS/JAG59LJaHP8gRy0KCKKFHoj3u7UDaQB8J/IzPArIFhR7C+TTdOvSKWHp24IflO8StxTZ+cASA2Bl1ASJyN0gTUCJtAIqBK8wXmFgSg== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by VI0PR03MB10927.eurprd03.prod.outlook.com (2603:10a6:800:26b::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.21; Tue, 17 Dec 2024 23:43:06 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%5]) with mapi id 15.20.8272.005; Tue, 17 Dec 2024 23:43:06 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com, brauner@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH bpf-next v6 3/5] bpf: Add bpf_fget_task() kfunc Date: Tue, 17 Dec 2024 23:37:03 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0117.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:192::14) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241217233705.208089-3-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5080:EE_|VI0PR03MB10927:EE_ X-MS-Office365-Filtering-Correlation-Id: d3b8d594-1884-44e9-2e42-08dd1ef48df7 X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|8060799006|5072599009|461199028|15080799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: zyQCpcVOuxZJRd3qifRA5t3lT5IS9+MMX1Ay1lD5tccAnhJ7yy8CLeTcBPCLQlPrZsG8vVaVupCGLvCtiBfe9NDcD46eE7mnMmBmjdUb1VOJp14MAecYM0QDdxJbTjilRwHRM5xWPFLr3ZRaZEVY1hBCeGN3lWUuIk+GwN5SFzyZl2e4j10o+Hq8/r7nb5iTfu0Fq65a5EOJKF93/056MR01KGd2RITKKjokp73y30fhmHt92EwysG98dyeJaxD+l9U7aquITC76gr++1NB5s9c63Ct6FW7x5mC9w33oLOjb1Ld2hb8b32tbjqEVYkZ0aAoxvgWBnG0oB0BbQEn5p/f1NAfDAMVcf8wxzkdifSYlXksAJlUL0mmYQoAA0FXhVqSdqeZL1lx53YsY3Nl2s2vVvJ1e+CrGa65Sz1C0I+LNMy9atlcboLPAxv/0gl9sW6dpOIbIY0sX+77p1dvl3S68yR8wrlBmueRiv27ymgvgrfkf0YBzfOn25T604KWv4fOFZr7Qq+UdxUTMdgH2mawIRkmZy6I1p1cDQkZmS31eVJyniT5sVsah+T2DuDhvmgsmO2zanWvWtzSW2OpOOtGK3tLqqgvou+txD67nOVk0gy4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JjwRy6fr2u99rCyuQqRkzENte+pk7fyBvKZkpmUbpmEOqcjBrOhem8OTXZekrHm1O09QKD90kr3yRBv3m++WTJpIyfYyGeQ09Oq7XpUaD1SHQioSnJEZzT0WSKmBLd/65l9ysiQ0VOtXhIP23vNNo9vwDkgBHv9fzyUdcVlUgKaiAarpDCyRlp1zwh+KqGYUXHZ/FB/vhaaU98DJFnGsPGBU8ciJcKgcg8cKmoaYFutuev++m35RDcxxuAsKkyKKVIcL94OewOmXzxSRempUaoY0N0ezOJ4NYQf+CKmw9jz39BllVicjw3/NM/tthxdNQ5JoAE061SPXAcR71W3VOQNft+jXwaU9Lca8/yTmVB3hNI5lMZBHY0rQ3SuRg0g9VrZtcmKIuz/rbZmhK8YR39JqbL8vICEX49CZZv+JikydUDiMUCKpfzB9If07xdjlcom7WoxZlLLIApwkJIZFNcTEyJKTM8qpV8MV/OwGm4eF3Hft1iUp5aMGD8MitEDooN+EjJwBsiXZJar2T36h5SySn445UlPXm7X0yc7VcBO6bDER09kO8P9zn+E7pdxVQb1JASfh/ysiWMBxfCLvmMb8J60hovinZ7HilH6fnQb+y3xMZkGmILYyPgabeTchK8Fe8wTuwVeY2vB X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: d3b8d594-1884-44e9-2e42-08dd1ef48df7 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 23:43:06.5549 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI0PR03MB10927 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds bpf_fget_task() kfunc. bpf_fget_task() is used to get a pointer to the struct file corresponding to the task file descriptor. Note that this function acquires a reference to struct file. Signed-off-by: Juntong Deng --- fs/bpf_fs_kfuncs.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 3fe9f59ef867..4a810046dcf3 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -152,6 +152,23 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, return bpf_get_dentry_xattr(dentry, name__str, value_p); } +/** + * bpf_fget_task() - Get a pointer to the struct file corresponding to + * the task file descriptor + * + * Note that this function acquires a reference to struct file. + * + * @task: the specified struct task_struct + * @fd: the file descriptor + * + * @returns the corresponding struct file pointer if found, + * otherwise returns NULL + */ +__bpf_kfunc struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) +{ + return fget_task(task, fd); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_fs_kfunc_set_ids) @@ -161,6 +178,7 @@ BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE) BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_fget_task, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) BTF_KFUNCS_END(bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id) From patchwork Tue Dec 17 23:37:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13912712 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2049.outbound.protection.outlook.com [40.92.90.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A72871DF969; Tue, 17 Dec 2024 23:45:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.49 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734479144; cv=fail; b=ajgNme6Yfc4iZ82hBIClGTCK2b91jtkID8yj1zM5DQUUbaZEYViE5FJNQJXdy9Lk39M3vRPokXhFhBHqNiPztxKL98zpwNntdi3xlAq26/WC3RyQZq1YQvBZ+JzwIxQPE09ClaoCC0bdNX15mhaCT4IheU6h74UCLpFxmI6F/PI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734479144; c=relaxed/simple; bh=TlAmZFVAXDw8hB2+tk50ydgHNEMHZe3/jN/g6w5HqoE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=RSg4BBB8sVJfYrAQroglr4Luy7tRRkQfLxIFOYGoDzy0nAHS+40sEkBl/faO8jY4isayem6ZY6iMxcpEXdpoU/1C8d3laQKpzqrnS+s1KJNGP0HR3Y7ff5Rq9SKRunLrifkzg2jRExEjTJJpGGfoXMXuZhmYCzM5V/b478KbDTw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=jKZYIL/0; arc=fail smtp.client-ip=40.92.90.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="jKZYIL/0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eV3KFUsefdn28rjWYZnX8kPMyqo2Ru0ArO3U3XBYhFL1uOAD3/iKXvfx+y86Qu78ooAWOiF/7NHpPuHWtCI3AnoT2TBx/S5h0cmGwVYwdp8fvuPtwzyUvS64MDypk9nSgTPo0ersHLM4nSiX2jaQ6kNouW2MZd2qd5SybsoCQxV+e6oETC+AeArFOf7pekLqko3K62h4UFtT15cRIliCxYJxCB+6ZPkFKK0YieICnZb9wSGdRP96rLZMEMr/2uWaZ2Mw3iBDXKVe3RCj1RBmJz5xea6Quv+DN641UY51i9HaGBxEWp3druNTOjgktLnIVWsOldNkBkIEyiT6pSDEMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=xa9vl8cG4E6D+oqH6Dk5TUBdYgnMGsYLe3tXzjbKMJU=; b=bSZtQjv0VasXx7OY+0EKIMKmi8+Rzy7jqP50akcuQYt989LtzVex42pLrru8xpPi5byPQIikXyA0RAsZPXhhIcIUMWtkoVY6IslXR4+lMGbinEr2TsnW0WS1SSGlr2ATmazJCaC76cleqViezo7X/bjhRbfX6cgrt3yeNCezGAYOPy4bcc8jHRCanswD43LD1D7EK4OTQNZGwAOdpD385YZTLAZt9X0xPkKtqoaHfUgvsFwf5YX3ZYLbahTIYfTcKijUfKsCZQsbLcCHiEsta7ehSxH9Y2pnZz9Fi3NXrYoQYqKGCr/xjdouXdHAs2MILJ7X3RPBrJtmFCY2a1XwnQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xa9vl8cG4E6D+oqH6Dk5TUBdYgnMGsYLe3tXzjbKMJU=; b=jKZYIL/0t38flFQdx6n5ar4x7Dw5z7Q2j53S1U8/Is5ES0mdo9ckSnXUuSoSIQrQcDY0IIKiEEK04ifs9shtB10SCx7VxuLLrK6hCqxHOu/BFQFWYkxqrl+MVQGjZr1su4fkLWdmoFPyItYIaglZ2SEaMKQsJUMrJrZQS3O0dtVxupnzPq/OL5+Fzhmkshjsh0FV0MUIUUj2rUNQRkBAhHb8R2ZFc3wRMpXQDm5NG+WeuTZYVBvJp6MjDwtxw6doEu/mGPdw1MZ8qwjefcJ0f8GYqekBsq+SBM+ghLEXNETZMT8VqbtljhRFFacv808v3yrgIcTnZTBeqpMicrGgaA== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by GV1PR03MB10502.eurprd03.prod.outlook.com (2603:10a6:150:15d::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.21; Tue, 17 Dec 2024 23:45:39 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%5]) with mapi id 15.20.8272.005; Tue, 17 Dec 2024 23:45:39 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com, brauner@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH bpf-next v6 4/5] bpf: Make fs kfuncs available for SYSCALL program type Date: Tue, 17 Dec 2024 23:37:04 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0117.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:192::14) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241217233705.208089-4-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5080:EE_|GV1PR03MB10502:EE_ X-MS-Office365-Filtering-Correlation-Id: 5205a6f2-1117-4401-2674-08dd1ef4e90e X-Microsoft-Antispam: BCL:0;ARA:14566002|15080799006|19110799003|5072599009|8060799006|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: Zk/lRxrqxf2v4SgLO2zpq0ikQesIMbod2ueQKZa100NnYHVCTMtIyIfpVQqPUbG3n65vxVc+Dcq9W1gqniK83c8lzAfDF4JFqytmnl69ZPFk8IxQMFsNmgN8JBNU1XxRrNUMMaQPrlCsYDIMgs1hEia4b5GEevRCrVnLNKRMXjwKASi17ty7JEsPGslGvNrp/RMAt95XH3FBTc76o4uNwbLRaWcZyvxUeDyzslOIcnSZhZi+HXF+cQjNOdd48LsgHH0HrYGonIZrCn2fRJ+mkCCq4rn8DCJ3EZHpZOQdjrUi0vpoKwcwDQWTq2aBnscDPVnh4IPAO0q2jFbTieFTu+brrJXUQ2cW3iAuCaLrx+D3Vf3ilzezn/R8l5Ajz1l/34/8GB21zpNuPWrUy1aGBZGx2yaAjLGV395ZxWXbJFURDN4cB0yARZQ2CtQ2fnJHe2bavrURNstC9vezhR7dHrd4LMeZSnYNGlFBRp3ApSmhx9XUvnhvZn4eOwgkGf9AVbKBqOtbwxbFuHLa/7UyBFPO98n9ioIXgxCB1XMPBs3UrFKv4RPyZyxGlSCLbPP5tu0hJkluEdkn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5P+JtUlqQRCQ4aSTPF63hTQgq13tQGXV4b3CszWk02nsJwkdDzg94JiI46hGrG2fY541sUXJxosNOR3R9p9ojZtvOqj+5IW3jsxSV4DbFb0p11GabZKxtuDZXrtTkhPb00YVK865ruf93utfXl5RaTceCbg9ZfU99r4d658FYbpi/UtTlhMSLhS1yCDPM+00j/jwX/K1ZSul7a0OqUJrqLcYQhkHzA4j2Q7Vnj1HiN0eajiSDTDBOINJJdkGrbR5lpc0uxg31L0lCSfiaKOAaYmTWmrepQFrPFuKh/K8agNUK8bbI8w0svuM98nLXnq4dk+CKhV4Nb6RIb2bQtUIUWfO4Ol90CJBCgZTofuqxGyhDjcGHgwMqU4A4WA2TnNSzoR1CON+TncU7YZKqN4I4YbAo4peWXc8DEOW1R8dRS52Gdj/LNr5L+GIxZ1EfJvh4xXx5UKj2VhAMV7LYjfst4oCAf6Wh1DliFAHXuWaFbwzm3omnL6pxV6U9zglJfCMXmzIpY9Ir9HUm5LDel/4XnlOrY6YP/fhXZihiPCg0Z4cBnwFlP1+RMeOJd+ox2VGTJ/jASNEEaUHhM6olDm2Uq1zojBTxB/hhRkACtSD13AsMO2dHaGaAu/CKny533cSil3IeqkvQS/a+q1kzUClZfK5q3k4HB2yVxeuvIddBlpK9AUqc2m0Aa37DdW045SXpjf4dcVxLjG X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5205a6f2-1117-4401-2674-08dd1ef4e90e X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 23:45:38.9453 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1PR03MB10502 X-Patchwork-Delegate: bpf@iogearbox.net Currently fs kfuncs are only available for LSM program type, but fs kfuncs are generic and useful for scenarios other than LSM. This patch makes fs kfuncs available for SYSCALL program type. Signed-off-by: Juntong Deng --- fs/bpf_fs_kfuncs.c | 20 ++++--------------- .../selftests/bpf/progs/verifier_vfs_reject.c | 10 ---------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 4a810046dcf3..6010fccc9db8 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -26,8 +26,6 @@ __bpf_kfunc_start_defs(); * acquired by this BPF kfunc will result in the BPF program being rejected by * the BPF verifier. * - * This BPF kfunc may only be called from BPF LSM programs. - * * Internally, this BPF kfunc leans on get_task_exe_file(), such that calling * bpf_get_task_exe_file() would be analogous to calling get_task_exe_file() * directly in kernel context. @@ -49,8 +47,6 @@ __bpf_kfunc struct file *bpf_get_task_exe_file(struct task_struct *task) * passed to this BPF kfunc. Attempting to pass an unreferenced file pointer, or * any other arbitrary pointer for that matter, will result in the BPF program * being rejected by the BPF verifier. - * - * This BPF kfunc may only be called from BPF LSM programs. */ __bpf_kfunc void bpf_put_file(struct file *file) { @@ -70,8 +66,6 @@ __bpf_kfunc void bpf_put_file(struct file *file) * reference, or else the BPF program will be outright rejected by the BPF * verifier. * - * This BPF kfunc may only be called from BPF LSM programs. - * * Return: A positive integer corresponding to the length of the resolved * pathname in *buf*, including the NUL termination character. On error, a * negative integer is returned. @@ -181,23 +175,17 @@ BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_fget_task, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) BTF_KFUNCS_END(bpf_fs_kfunc_set_ids) -static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id) -{ - if (!btf_id_set8_contains(&bpf_fs_kfunc_set_ids, kfunc_id) || - prog->type == BPF_PROG_TYPE_LSM) - return 0; - return -EACCES; -} - static const struct btf_kfunc_id_set bpf_fs_kfunc_set = { .owner = THIS_MODULE, .set = &bpf_fs_kfunc_set_ids, - .filter = bpf_fs_kfuncs_filter, }; static int __init bpf_fs_kfuncs_init(void) { - return register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set); + int ret; + + ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set); + return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_fs_kfunc_set); } late_initcall(bpf_fs_kfuncs_init); diff --git a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c index d6d3f4fcb24c..5aab75fd2fa5 100644 --- a/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +++ b/tools/testing/selftests/bpf/progs/verifier_vfs_reject.c @@ -148,14 +148,4 @@ int BPF_PROG(path_d_path_kfunc_invalid_buf_sz, struct file *file) return 0; } -SEC("fentry/vfs_open") -__failure __msg("calling kernel function bpf_path_d_path is not allowed") -int BPF_PROG(path_d_path_kfunc_non_lsm, struct path *path, struct file *f) -{ - /* Calling bpf_path_d_path() from a non-LSM BPF program isn't permitted. - */ - bpf_path_d_path(path, buf, sizeof(buf)); - return 0; -} - char _license[] SEC("license") = "GPL"; From patchwork Tue Dec 17 23:37:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13912713 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02olkn2095.outbound.protection.outlook.com [40.92.50.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FE201DF969; Tue, 17 Dec 2024 23:46:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.50.95 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734479214; cv=fail; b=Fdu9sRU4uii1qc7L8GWwpop0gCldOORkizlZmC9aG9U/9dMeZrLuhnnuXLI/UwhzSBiNJ4uS0r/e7JkDGJWSAj/0sJGGXTgmlzaaCPQcxqn6nUIgBYGMF15qYYhnflbIQohui4Nmfh0utyIFmrXYxNTHGZkOxZqJV0vbXGPeZls= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734479214; c=relaxed/simple; bh=wK3FzXuQpDXmaSzr0vURQaYKg0fxON8Sw/uCWWRLeiM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=uZ6Y1Eqt+nVpCFJ0Q7vnnhT0XkvASFm0vI25kMTPP3jIBNzx8nnqA+LDK5+FfjiMPEu6/NqGypadybfhUCwXbHe6NloCiURS3LMioayyuEtIUKx63/vCdF6gcEn5ojrRFi0FokERU29szKIyTrXBulKW5RAnfti8nUoYps90TfY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=RowY4RSp; arc=fail smtp.client-ip=40.92.50.95 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="RowY4RSp" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=fm8RkMDwxPYaVXf5ZLb0cFK2a51TcYwGLte1gG2dTZpu8ZiWQuGkKMIR8GqVzie/IVvhjG9ixjjv51PFHHfDWsWLsobKpscw2MFOifA20WWklkuDHwlXUrqDAoo0a2snSEgBpKCqcBMyKA0L0f9n3chdlQlhkv78jIw+8vMOOrC8Ng/M7LtAA8Wd66CKTmw69j/N1Hg0JiQ6jkmQL4z2d+AUaVKwikAkdmFWAIAvRL5m1AY/z/l6Liw6w47lzGYoey6mOi92VM7CmqGuVwElZjJbXYxcvrZ/UdfNS0/z1hR5TUhn8NzpDdn8nRd9l2Iwnf/79HwJZPbhC5PG4cXi1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yaoaGdtKnNqICPXFl5gVmGgFsml4c7tLCw2b/4JCy3k=; b=NeJDmUOP6E1angZQR3IIeX3uc/jzQu/Vi+EDNPhYpp5vv/B3HOScRWaT72rrb6g522znpiZvi+4Onrkm6jF7sCBgZF7hbjDqrGQx+rCN4//gnjq/zD+vvh407bUb/J3L1j6uEzEOFSgmmEZSQlZzlyzWeJ0TgAadIW8456BIv78NvGOoY2P182CYoig/Z78UaDWj1e4ciGcyf57B50La9i2EkDqI9XvyG1/L15s+GUU/voA5nFosGhX4vAJltFmet043CLWWgZvWUri78ohd6/4XP41rzXdJ9V/SJ7asKoWwYphwWL/pFlkBcX5ISLMLk3+cs1OK3W57HxD1hdg5Xw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=yaoaGdtKnNqICPXFl5gVmGgFsml4c7tLCw2b/4JCy3k=; b=RowY4RSpjf6ANc77/2Q+WMqUA8AMPHou20EI5ZDIAvA6muhn6gBtMpndwmnoQOwAiGNsylsYAccTekp6FBZRTfp4CFi9uawkAuVI6ClgL35t5hj2i/kXUhRSd812yJXsH5FAe0n7AyO/735SnH8zLNLEW43DSO3v6n3+dtsozzB+5DH21NCwk9IlVomFepcZl8bXW7hZwrmuSO/GHc3uZQX5RmTziwjSHIDC5jXTgXGt+vC4nUYJBnLf13S5QhQA9C/tooyWHLM4mlP0to70Cczmu9FEkZZ3PMvsvMnB605hh8hQWylbb/M8QGFc4i5QU7//NVyCFgQRWGKho27H1A== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by GV1PR03MB10502.eurprd03.prod.outlook.com (2603:10a6:150:15d::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.21; Tue, 17 Dec 2024 23:46:49 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%5]) with mapi id 15.20.8272.005; Tue, 17 Dec 2024 23:46:49 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com, brauner@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH bpf-next v6 5/5] selftests/bpf: Add tests for bpf_fget_task() kfunc Date: Tue, 17 Dec 2024 23:37:05 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0117.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:192::14) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241217233705.208089-5-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5080:EE_|GV1PR03MB10502:EE_ X-MS-Office365-Filtering-Correlation-Id: 7defab5f-a513-4c20-4f9f-08dd1ef512e1 X-Microsoft-Antispam: BCL:0;ARA:14566002|15080799006|19110799003|5072599009|8060799006|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: lASmZNYwIOQuRFyCRs4L7sdxHwhd29dY7x9c6KGElDnAgT9M5LoJCRaoiY9UaqhYseCxVTW1eV39F9uFdx0xDBMQ/0m1jvN8ZW0qkuSIaIC9xppgzrAZnsnZkqpJe23/Dw6EgdcAeC/v1e/eOKedAUri9XYqdrBiRlMGfVNWOgPRTwGHwbrCyiRhg+K21y5HB+2wJeqvTgESmBh1B57oMGJYcXwzKkEwMVStDOL6BKH8wwAYQlV39JUbEPoyMGZdIcAPtleDCocgB5uogZdg+BjTzqVCJl/E2/rnFU4isO+55xO1NYCAdniNXYq4/HIGK9LjGRgYTZruMpeuHuCi3Gstd3NKzvBWnrrcWm9cd1wQ4ON5+dmA6HvoKB3B+xFGrbOHFMGV940cCbB5Qt+/IIcNmQna0Ev8un02xuqF69DFYMziyOwnUDb8/7Iy6iCbSjeifVTiIaf2PpXcE4B4OOYRnQa2llD8p8gRlXSapoRXuZ8c+iytmvzdb9ccLOwlj2wvdNyp9uEHRYnPFEl9KLquaD42c7eZBrDwfiWhi0db+UIgPrYv6HbF0QQILh/OJJI/Vj4fYu4aKyBCrpCuaR98jyhE2uRlkvxm3uN4DZk6BgVAUhZlf/2d0Nlj0SYAQauPr+xIT/iuYqjhd63h33g97ndBDnMPcyOkWaHMlbo42K8kZO7c1rODqS67sRxIEh+MC+OKcfdj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Rka7t4V9oqRxPxuG5qm40/i/b3fKmTHLl6L2wIkY7ctM3inH0RsmX84ptjI64InWV5u+dymOuSRKqf6QDPirJscvKKGAwc6poM6LWVZG/fD5RIMSlejELShwS1z+xWNGuAhSzmbvbGI5o+pNVNGWv+8emwOQ/LaddSXOTd9XxiOFlK9pozW86GP/snZ+t9c8fvm/nujkCEp9Pi9VNWbKU0MsOQu0BB5QMax87OY/dOsplZcIbkm+mqT/ryvENxOWUGNzTCQ/OdirC9yFJAsY4UTiaezSkY5zyG9VbMtE7ToG8fBi+hEiATYUYDHWC+98oCk8JwtD4jCKrae7qD4F/JYumJZaANAgxa+tPEfuBAM8cG4pHP0X9RMWWphinW6zdc+BbBUqu2QW2OlFPik77AVbt07V1Tqdj3GwQYkcOAmPJ3vM2kUiV9UW6SGS+PL1+dxEf1B3cbWIngsMOfNOIj8eX1xnx5r5UyJZugX4VVjI3fEfoEypDWtRo6dbN/pguwKLaZWT7A+EVUqAiPgv+25i5yWL/elsdZ4xdBRfONUC X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7defab5f-a513-4c20-4f9f-08dd1ef512e1 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 23:46:49.1482 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1PR03MB10502 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for bpf_fget_task() kfunc. test_bpf_fget_task is used to test obtaining struct file based on the file descriptor in the current process. bpf_fget_task_null_task and bpf_fget_task_untrusted_task are used to test the failure cases of passing NULL or untrusted pointer as argument. Signed-off-by: Juntong Deng --- .../testing/selftests/bpf/bpf_experimental.h | 8 +++ .../selftests/bpf/prog_tests/fs_kfuncs.c | 46 ++++++++++++++ .../selftests/bpf/progs/fs_kfuncs_failure.c | 33 ++++++++++ .../selftests/bpf/progs/test_fget_task.c | 63 +++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fs_kfuncs_failure.c create mode 100644 tools/testing/selftests/bpf/progs/test_fget_task.c diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index ce1520c56b55..e0c9e7d9ba0a 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -221,6 +221,14 @@ extern void bpf_put_file(struct file *file) __ksym; */ extern int bpf_path_d_path(struct path *path, char *buf, size_t buf__sz) __ksym; +/* Description + * Get a pointer to the struct file corresponding to the task file descriptor + * Note that this function acquires a reference to struct file. + * Returns + * The corresponding struct file pointer if found, otherwise returns NULL + */ +extern struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) __ksym; + /* This macro must be used to mark the exception callback corresponding to the * main program. For example: * diff --git a/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c b/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c index 5a0b51157451..89f5e09672b3 100644 --- a/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c +++ b/tools/testing/selftests/bpf/prog_tests/fs_kfuncs.c @@ -9,6 +9,8 @@ #include #include "test_get_xattr.skel.h" #include "test_fsverity.skel.h" +#include "test_fget_task.skel.h" +#include "fs_kfuncs_failure.skel.h" static const char testfile[] = "/tmp/test_progs_fs_kfuncs"; @@ -139,6 +141,45 @@ static void test_fsverity(void) remove(testfile); } +static void test_fget_task(void) +{ + int pipefd[2], prog_fd, err; + struct test_fget_task *skel; + struct bpf_program *prog; + + skel = test_fget_task__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + if (!ASSERT_OK(skel->bss->err, "pre_test_err")) + goto cleanup_skel; + + prog = bpf_object__find_program_by_name(skel->obj, "test_bpf_fget_task"); + if (!ASSERT_OK_PTR(prog, "find_program_by_name")) + goto cleanup_skel; + + prog_fd = bpf_program__fd(prog); + if (!ASSERT_GT(prog_fd, -1, "bpf_program__fd")) + goto cleanup_skel; + + if (pipe(pipefd) < 0) + goto cleanup_skel; + + skel->bss->test_fd1 = pipefd[0]; + skel->bss->test_fd2 = pipefd[1]; + + err = bpf_prog_test_run_opts(prog_fd, NULL); + if (!ASSERT_OK(err, "prog_test_run")) + goto cleanup_pipe; + + ASSERT_OK(skel->bss->err, "run_bpf_fget_task_test_failure"); +cleanup_pipe: + close(pipefd[0]); + close(pipefd[1]); +cleanup_skel: + test_fget_task__destroy(skel); +} + void test_fs_kfuncs(void) { if (test__start_subtest("xattr")) @@ -146,4 +187,9 @@ void test_fs_kfuncs(void) if (test__start_subtest("fsverity")) test_fsverity(); + + if (test__start_subtest("fget_task")) + test_fget_task(); + + RUN_TESTS(fs_kfuncs_failure); } diff --git a/tools/testing/selftests/bpf/progs/fs_kfuncs_failure.c b/tools/testing/selftests/bpf/progs/fs_kfuncs_failure.c new file mode 100644 index 000000000000..57aa6d2787ac --- /dev/null +++ b/tools/testing/selftests/bpf/progs/fs_kfuncs_failure.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "bpf_experimental.h" + +char _license[] SEC("license") = "GPL"; + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int bpf_fget_task_null_task(void *ctx) +{ + struct task_struct *task = NULL; + + bpf_fget_task(task, 1); + + return 0; +} + +SEC("syscall") +__failure __msg("R1 must be referenced or trusted") +int bpf_fget_task_untrusted_task(void *ctx) +{ + struct task_struct *task; + + task = bpf_get_current_task_btf()->parent; + + bpf_fget_task(task, 1); + + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/test_fget_task.c b/tools/testing/selftests/bpf/progs/test_fget_task.c new file mode 100644 index 000000000000..fee5d5e1244a --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_fget_task.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "bpf_experimental.h" +#include "task_kfunc_common.h" + +char _license[] SEC("license") = "GPL"; + +int err, test_fd1, test_fd2; + +extern const void pipefifo_fops __ksym; + +SEC("syscall") +int test_bpf_fget_task(void *ctx) +{ + struct task_struct *task; + struct file *file; + + task = bpf_get_current_task_btf(); + if (task == NULL) { + err = 1; + return 0; + } + + file = bpf_fget_task(task, test_fd1); + if (file == NULL) { + err = 2; + return 0; + } + + if (file->f_op != &pipefifo_fops) { + err = 3; + bpf_put_file(file); + return 0; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, test_fd2); + if (file == NULL) { + err = 4; + return 0; + } + + if (file->f_op != &pipefifo_fops) { + err = 5; + bpf_put_file(file); + return 0; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 9999); + if (file != NULL) { + err = 6; + bpf_put_file(file); + } + + return 0; +}