From patchwork Tue Dec 10 14:03:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13901462 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05olkn2096.outbound.protection.outlook.com [40.92.91.96]) (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 3E1581AA1D9; Tue, 10 Dec 2024 14:04:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.91.96 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839498; cv=fail; b=X7knvkN931kxWN7F24D1e4aWChRjsuSZllZIOF+2o+pzvN1AyhhskvI5srLQwif5Isc3MPKBy6tEIEKqdGvzhbIEDWmL564HqcpDFOygl3M9MAq8/HuouaULW75eRKmwOUD7GqbBqdr4Ba+x/dM+ikrswR634CPXqRynf60n5DU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839498; c=relaxed/simple; bh=+nRoNWzsUxOYQ7IeJVDM9/oq6X3M7yuf73NdPtR48oc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=s6ackNoS9dU1xGYISgSHhN2vFW5de+tsgB50wLmi0CJ0gCf2Uw65w1/DJz7lfNG/g7fKkpGxjXt/YgO8ywOPGmU+X/uPGfZXX3ZF+DTZ2+zS2pyZ/jL+Q7643k2Pgtzc/lLau6lZF4BmRnwXkL2I0DpxwXZFELNqys/l4/cNgnc= 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=G8mmumlr; arc=fail smtp.client-ip=40.92.91.96 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="G8mmumlr" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pp5/s+opSpg+4ybsi/nd9MVRPTSRctB2CHDtd1g/YP8PEP+SPDWWbvFEW1Ya/bIFCq3fjHMNKHlPuwJF+mLDs/XkZbrXSnLoaOVuxqlFH1VIgFJnda9m87F0BDLxubFIU1KsRi3NSADHCWCxcPdq5lj86PE1E7Hy8VrVCiM9MIoY55Qp0x9yrKBr+KE1Fqc23hb7FjU0XjVCvArb0KIjIV9fB0PbYlypUiIQ1p8YChsJzu6FGhJdSt2IH2rdc53LOVByfp4uHxy0Qd3+Q5w+HU2WygINREh2D0DqNmTIV0ny32LjsD68xq86V/jC1mqDCU/xcI7GxXKsbuzJ9kcIpg== 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=Oua7KwJZiSn+X+T1BbtP3eluryriam8DpXTssfquqIU=; b=ME3DXhlUrWvbWANVqTqHACn4Nil6P2KQ1EASnjUnrhElJH3iWXgW6uo+qN4iPbTCacJkGWmYYtqbeMIvOyf+FjCAAn0WwtCiy/yvNpLXtqT0QpcxJNBwGZtl7XhD8ZJDw7tV61u5tGPG1yKWjk6P5eIzJDgM8Vc4YEvObbOz51mzVlw3AEhxwwzbJiQ0p0r2kCg4PmNI77zKAAM05iRlUmi23Snw7IKYCGOKSXlAeMfaKIPxUJ3I3RjumUvQb8+8RXXv+eMRpnLszUbiyrS6R5QtpvgCkxHpi4v5LoZfDE0NnRW6ZHNwfFPn3ruH+R+rLPiG/OtDssactPw3SfK94Q== 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=Oua7KwJZiSn+X+T1BbtP3eluryriam8DpXTssfquqIU=; b=G8mmumlrI9C772zXfIC4oIJHNDqM0V4E/QElT/nOtiuLF3rPv2eImalZPmYrvSsjJUysKCf17mmi0DpXSqBEULKUJ0Lcr6sUtUWoLjKV7Y0zUlFxY2694hT+rnhA47VYDtyghI8cUjZBk3gTOODBQRJVuElVMOEdPMkedwZe3cZK9nmqCs/DqFnX8vvGe9zJNhY96ET1IS6ZLD+H/JWbitZe0+XPNfReJs35ezW+TAU0TzQ7mQux6GAPW24Q2q4DmYu5iqgB+EB7ry8O5FBU8vPRpgW/Z/WiBFqUNzOqXs5WIjxGbWNnMDI+BgBMPqCxN+2MISLrZiWD+hLJIbguVA== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB3PR0302MB9037.eurprd03.prod.outlook.com (2603:10a6:10:439::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Tue, 10 Dec 2024 14:04:51 +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.8230.016; Tue, 10 Dec 2024 14:04:51 +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 v5 1/5] bpf: Introduce task_file open-coded iterator kfuncs Date: Tue, 10 Dec 2024 14:03:50 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO6P265CA0010.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:339::7) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241210140354.25560-1-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: linux-fsdevel@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_|DB3PR0302MB9037:EE_ X-MS-Office365-Filtering-Correlation-Id: 69f5a801-540e-40b2-8bc3-08dd19239d03 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|5072599009|8060799006|15080799006|19110799003|440099028|3412199025; X-Microsoft-Antispam-Message-Info: WLVL2kNtnXLQaYazXkye+XQsDkcGM8mJ37Z/cfJYjgr/7Qg6gahqy6cl4Zor6cguKwpYIdt4tkHm8t8KcXFUELRptH7NYJoUeVzMmbOBlzkDRlA9zY6wj9aSNZXCCe3d/EkMy4F2DkqdRSPBErFQQ2sZVsYuJ42zDgDnG2Y0Q8Gn2kpaK+ezY1B1jNspv/FfWmKYH8n6LbOGuL/d3+HtWgYX4euZFL59AkHnknMZ7tXyEkeNxrT8ghbLIKzv1JNh0iKkHQ7GIXlwdcdenIw8yQ0X2gJlFZvWdEcw7xxUpBx9ljv2W0Q2qE1o7W0idLb8FL1yVGnw/vi3mUTJaJ+L6jW0cSIC4hxU+ra72gD/9KaH0ztd46cxr9knxmq9DkNwAInmE3Cv6LrkqosGRvESMqT0sRc5msAQx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8bKIg7HNhM54+heF7NDxbYCgrDQyWlpEwttiCAVxknoWPvcNbp6rd0LlwG/u3nPVgfwJMHLO65pYZiQN71qgAyvfOxv7HILRT9joRDNs/vGzqrMF3yQxxcnw1Vmx9vNVytXVMNK8yVXMYDkZ84hI0OwzNR3RzSULSVlsXMWzR10+OyP7X7vZiEAcb1aboGROCiUCPo3AF9AY3m/oqIF7Y5pCOgWbI/O3IxV1ck8LV/JoSuHf/HRlnQRFXOx4JA8DMIDHYjPDaIe1N6ur314L7MM7Qx5E5eTkxeyS6h1GSNeA1mUHfgITuWcTwT70xZESZxO0SDEINC742l6nKFUR3QrmfhynJPRK8eZU9cKQ9kfJO+3gVwf9qEvvUCNkR5xQwGvBQUe2zF+mdgiqmpJxoJ3Dq+lFVOK28ykRfiAKWxygfYQR1TsnVn0o2K/k9RePEmntVVaDghaPNDU84yp/3RCtW0DmVwNkwyx3x1805MbRZ05GLGZ1OynH8YQjybQ3tQA15etEWMm94nZu8F1P8XxQQwNw+UFP1silpG84YNne6edHlyAFxttj8yCq+VRAJ09XrFS5AXLRy1+e5c4nKHkObtXT6H4tEaZLyjpltl5Drq3QB49r/9EAi8LBwP6ifu5IoTuRoKUFqUqqbI6xHY80xwP20pYZwsgkrBuTnVQatcc3y4tzayC2LGhxoCFyCuQSxiM/z6+eK9UrH15oXrsgn89heFK8Ia5sR/W8XUA5dUEbWrkqobOYnhODr6/74HSLFznFRuX6jVJv1jqeC8/EqRzQAHRGH4IFOsYkz0Ne1ppjIGItnyEW+uWkZHfjAp7mXZ2x2Vna995KDh3lZgzPk3LS5vu3hEFMGll2G3O9wzeLDw1601aBhy3DQRR X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 69f5a801-540e-40b2-8bc3-08dd19239d03 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2024 14:04:50.8964 (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: DB3PR0302MB9037 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 | 92 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 532ea74d4850..b2a73b49d1a9 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3145,6 +3145,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 | KF_RCU_PROTECTED) +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..133eacbff92a 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -1027,6 +1027,98 @@ __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 + * + * Note that task file iterator requires to be used within RCU CS. + * + * @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 = 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); +} + __bpf_kfunc_end_defs(); DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work); From patchwork Tue Dec 10 14:03:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13901463 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05olkn2090.outbound.protection.outlook.com [40.92.91.90]) (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 0C5E05674D; Tue, 10 Dec 2024 14:05:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.91.90 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839553; cv=fail; b=QRP4npdqAKWUc+Qah8SRyRf1tnhP3MFnpQHzCnOPobdYPDnim+xuM7McwYJRygDQDQcD7gYuzhCh1phcMYwZbDB9SpbymlcgJz48ZY3ee6VzBWirtEwsAZxsgJ2BigTCUunDUXXPAwa9/5yB3ULr0z9bSMFpcCVYwyvXdrlEkbg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839553; c=relaxed/simple; bh=AMOEaliEoExboxv8tzmYilmMcmoFdvDLT9NnVIzRarQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=EVyi0Yp5JBYghbJvpnevS+AKxCjjwkul+OEKZE7uCZOqH/RZ3+XVwaucWr01M/sUTdqI4w/EL+74mwlXYvldvWU+G0wzMz3AKGRo7FPtnJ9l7eZLaUgssW5PE12DH3rw3pw7ONViQdAiCJSJ/QrNnxkD7GOiioeb1CIo1w9T9Wg= 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=Y9sBvc2t; arc=fail smtp.client-ip=40.92.91.90 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="Y9sBvc2t" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=shkEp1/GDzR8f56qGBPD2b8jIUieIWYvmts+uXXaVQwTNKkbHucWNy57TV6ytefYji1uYR5Ghd0FRiegAXPcusYuyRcTouJTFCeWhc4UWtnW2PwhdGyZhs3yfVnqLzlVNMYAEV6xnMyLAzLo/gaC/Kp4yeWknqjXqewWH0TUaMU7d/nwVEn21LwJ0VDeBrcu2/V0pFU1WY2tdyPgpntuNgqvK6Xouc/a6dWXRcpufv/O4dT1SoZajrQErFovzp7QWnSEHCqZqck8ejNgqXuzoMzEA9PQ8u2UZT57RJWMwVaQsDxCBgmRh7CCuDgru4TL8NBaplXEnti+YbnIZfKSkw== 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=XirGjwy5aUVxamVwZw/UVQgueBUFVlNHo7sPf5vx0S0=; b=Yz0OKh5hhCl0CYTIHQY81DoeWGF7mzQFk1KZuiNKlFs2Ns2oTIihDwAPc6VTveKw/lj7SF/HQb55c7a+bxnhwfwzGfNeTe70uo15O8QRZnRD/tZqybTSse5wf9licQqREjmW2GiLoVoGsrmhIuNzEQMbhDP/jKVsQWJorcN1NkvNlhOzpLgRtxFMwxscH0HMILn+wCwrz+waG43gIr7orbT75gr4pE9n5ciYdbh0edm0+8Pg/mlHqxKMp0A07/acCTeurwiKsb2RLLBTljBy4JU2c+l4MmM2+xr+5EOTFvVbMRH/HkPXzFQoXNoY0XkN9q/xsxBw2CNOsmdmM4iJ6A== 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=XirGjwy5aUVxamVwZw/UVQgueBUFVlNHo7sPf5vx0S0=; b=Y9sBvc2tbvjTbyzs3ldlBY/EeL/iYvOAg5jLb57Qu33kAUDsj4TdVA/8M0I9CDDjZOwni0/FllNNO+EZ5BtPIEVHWll2UOYvgiOkXrxAmi+5q7y7GgxCXeAZV1QumzPUcjNJsUOWa4F+wx7t0PVy4rXa6IhsMlKt7ZaBibasWxRNgoKa+NVC/F7jdq9PxtAWv4a0zZM72vHPv2/V9Fu/FBvDJaowMbZEn7+YAxKgN9uApUrIjnaEtwPwOQGlaZ84gsVDROkYnZ8B3I62VLp5fBO4FGdBE5ifQRsAFwJo5zMgec0OJ1WG2aFMdht/XfI2haWEm0ohN9T59AipjQrBGw== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB3PR0302MB9037.eurprd03.prod.outlook.com (2603:10a6:10:439::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Tue, 10 Dec 2024 14:05:48 +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.8230.016; Tue, 10 Dec 2024 14:05:48 +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 v5 2/5] selftests/bpf: Add tests for open-coded style process file iterator Date: Tue, 10 Dec 2024 14:03:51 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO6P265CA0010.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:339::7) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241210140354.25560-2-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: linux-fsdevel@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_|DB3PR0302MB9037:EE_ X-MS-Office365-Filtering-Correlation-Id: deb463f7-5bf0-4720-2b2e-08dd1923bf08 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|5072599009|8060799006|15080799006|19110799003|440099028|3412199025; X-Microsoft-Antispam-Message-Info: DUEWl0KZIzHuBGm91HJdYMXDgch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EQRW/S/E3411FF+2xnaiJvFyrTjdvM5hII2GGzLk7quAerE8LEFulrzpxugWac8D+1ZuL/fgNtUJxaXDrgRGKRQ2k6Fr+mlCWrD6Z1uCNl4VCgTe/wdNOABVcMiI1JEL3mbh3Sh5tAJA6cTSUWmG0xnaR+XZe84uNwZWVb6lnI0KZiK5U5sHylhaXXELPIY+1XrBVu7Y1 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: deb463f7-5bf0-4720-2b2e-08dd1923bf08 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2024 14:05:47.9060 (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: DB3PR0302MB9037 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, or not in RCU CS, 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 | 88 ++++++++++++++ .../bpf/progs/iters_task_file_failure.c | 114 ++++++++++++++++++ 4 files changed, 288 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..81bcd20041d8 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/iters_task_file.c @@ -0,0 +1,88 @@ +// 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_rcu_read_lock(); + 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); + bpf_rcu_read_unlock(); + 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..c3de9235b888 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/iters_task_file_failure.c @@ -0,0 +1,114 @@ +// 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 an RCU CS when using bpf_iter_task_file") +int bpf_iter_task_file_new_without_rcu_lock(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_destroy(&task_file_it); + return 0; +} + +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_rcu_read_lock(); + 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); + bpf_rcu_read_unlock(); + 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_rcu_read_lock(); + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + bpf_rcu_read_unlock(); + 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_rcu_read_lock(); + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + bpf_rcu_read_unlock(); + 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_rcu_read_lock(); + bpf_iter_task_file_new(&task_file_it, task); + + bpf_rcu_read_unlock(); + 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 10 14:03:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13901464 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2075.outbound.protection.outlook.com [40.92.89.75]) (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 3F4541A2642; Tue, 10 Dec 2024 14:08:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.75 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839718; cv=fail; b=mhq31tW/1lA+N7vXJ9Uoxwwzl4Twuy0KQuCgj2ox/Y0WlseFGjypkMY0BJAcfbmSKHpvg+rodvG112J+l5PqI7uScbeHsFXwNeDaG217A7k0H/ZvFvpo/BorkVhupFZOyD/4SXBHm3wpMDmM52MArlIcyhoi14HUqUz/3SPPpC4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839718; c=relaxed/simple; bh=Hnb8huvwU3mh/8ZR6urTMVV+Dt50GEkGMo9DjBjDEwE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ZwOhyDR+T7UuwmwXXQFrxUoc4HyPkc2Dnag2sBy1xK57kZfdKkCbmTKV+ya2htPorTZ3HyM9E9EpJQe5FXBhQQRmZF019ONWyI5R71I2t01atNlJs7/yh6HOib578bfUrL0Mq1uWYgesVZ5Tg4iGaIT3n3M1FJlY2Dz9ywR6ksI= 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=H8UFk2hZ; arc=fail smtp.client-ip=40.92.89.75 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="H8UFk2hZ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wc7weUcfrqk30dJQ55hSdGGkPyToiGt7+XCUwO4ubZVsQgqqF18/yE6TDU2+aKgM5mJ1hiyg7BS5maBPNn+8+84vLYZN1eVuRpdHNMfnQAauSZ8stS0pzO6Da9iSo73IHS20X2996xT6XTPpDB+0DUNBLQalYih3CeU7pcqjOFgGL3CdVXukIlJeOeKRSlcL/a2qTR0sMVLZshgeka55HHEFmNdKXgPfJqCB9SodjGg0fi/oXBHtDdtFIzRt2yPglbwgRhpR9AQwTk9fJGb1Gydgoso2ZU6BrU01vf9A7t62atDOTebDbLHYGwIqlIIdPNv87rRaBAfNlWTG7BOXng== 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=qKlFj67NarotCICknA5xg6wjcNX48uJQG04GY9xwyx0=; b=IDkfofYiH324d++ZDSUEIGa2km1557Bid88ZFUGaRWabI9ltEKh88KMLI6IA1RHOHMcwNWOaWSwOHqXbO8VSm8kJl9w/y+b7UgtuB0J1kuErcHQCy8eMIrmYnbIjUkUa2yA1boPBWCBZ9RVEiy4GOnVQat6xiAlu+3aShZJGeXKSsAvQfcOMLuQG8T8jr4AQRAf78XpT7fFOkBrcUVNFrKkAqMEZ7jqG7xFySq05XcFNmZ93JYbCgPyYkdlt18wc8BI1n9R6YnS3KUf0unzRlo9x0OY8+GsKuC42i3rED5RTxQFGLHl4O7dh3Jw1bfEFlBwXBBDXC1VgH2ymL3pflQ== 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=qKlFj67NarotCICknA5xg6wjcNX48uJQG04GY9xwyx0=; b=H8UFk2hZn6IJjGnheUTUFltsI1NWVPxU3IBUA4+Gg3IoDXOQbZNleHHI3sSkcgAeX62Nbxv/ndYXSzx2d7ekW0h6JPqx+vVFLiSacaLNikVN98HBw/JwnbrndihdWlvC63ZBHzhIsH+/efm+GnkMv2JB9QazxMLetn8WfUFDzdD6PnrWN9jzajhnobYcysinN4dv/XMxMuIpU3ccEwL1l1GUMC4MNUqtcHhneUHkvBEjOxzIAh/P8rIS+0EasMPSz6WOuqaVPZE6exJXZul8ETO1bUZo2wZUGF9unB0iYYUQJGk18lJyKGvm6ID8QFmvMYqcE+BRT1eDtqbU0hNhEQ== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB3PR0302MB9037.eurprd03.prod.outlook.com (2603:10a6:10:439::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Tue, 10 Dec 2024 14:08:33 +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.8230.016; Tue, 10 Dec 2024 14:08:33 +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 v5 3/5] bpf: Add bpf_fget_task() kfunc Date: Tue, 10 Dec 2024 14:03:52 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO6P265CA0010.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:339::7) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241210140354.25560-3-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: linux-fsdevel@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_|DB3PR0302MB9037:EE_ X-MS-Office365-Filtering-Correlation-Id: 2cbdb113-7f6d-48a6-a884-08dd192421dd X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|461199028|5072599009|15080799006|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: fLhd1B4yOpW+yRg1lACrHHZ8gODeg/YXP4+7xtWlNp07TWJ3XRe6S5ri3Y7GkNsrjYYNb0McW02nzRb3k3AkuUOh0xKhTvyJfUFPzxyaNz/1WERsTDzc5BsV/cJJoWkIG2VKZyPkHd/wzKma0vxBkv2GeCX/Ao9PvjxrpKazntqgtUEHksYpBBNQuHUIHkT2xmdBh9udPf8SzhlUqGy0c7WHeJaPAqv8liY7A3s7LQLEUDnSFBNXaRJrONrNnnALIL20kYHdIfQ7nTw9SupGtPj4oGeJhSOAmFMmrNoC+W5tEc+gzfTjrNqF5t6YX6w8b01TzKM1ZFPmwLF4gbu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JM4GXhxbYZZPJGswBamZKjm0kZyS2W/dZe81SVehIKtpaSCMGsGbRkztSMOZ/0nWqUxzm+ZVWm04Iol7IIsnFtf/f0IE37MDFccRushVhST5ASOjrlKRJY7/BJyEpAl7LxaWS05BuSkolMu2U+GVFp2GdnnoXakSzNFj5g//KCv/9CZ0RSWMuo8xRykB3k6mO2w05q42DvP3ncxyMoaBzZ0RnSRS8U9ABdXh8W7y0+LWmiXL4ykV1IqvTv/ptavjpktXjSJo43yqZjbjeIhEk+i787oMrDPF3A4g7sd29LoZb/yXi/jZlCMI0F0ba2MqfD5J92rDxdvfcsv8jiR2RM97L1DV9xflrVZEdQPM/frboVSdOcMw79firPCn6a3FVCYykVMgFApA7H+m5wksYwutdgrFjYUuCPol5hW3DgiEyo2AYMVTrQePJd2p/95sqK9ka+rxhNaMvp34TPfz3nzs3ClIdEjIFfN5HEuF1Nf36lQyX6FKn78+nxkznXk9Js5WUnAjZABCkpecL9nS91mbWD/Gjf9tBkp1eBoB4DYdLAa5W7xniVrZ7k/vQbdwSYLtnSKR8kZ8FAQhgQ6XeceC86LmYeehW+gv8uiiSdN9oU4uB6Na/n4SN7ZNNKghRtXzTEsnTKoxXdWl6o6r8B/pDHJ2Fw4RN/zyK30EAiagB5JoXfXfLwTekab4XrOoGtTHvCVtq6BvcE3zex6w+AhNeptOVpPDxU0HWeUKkfYWxOYcj95LQWiI/61CiPWc/CM8kk21hf16DGBJiNQPKZORUiuWGBlI3comvbBgV2Zoq154nAYQBE0vp90ey0itzprXtZzpBMZXY9RKUamzqvvx0/n3AUYy3+OQokw5RnV3L+xWN1Bkmy/iTFlV78I09P1udC2FsAAD+bJTcPlOUWKj2O/DHoy/Y12khgUKAPDEac2XE9 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2cbdb113-7f6d-48a6-a884-08dd192421dd X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2024 14:08:33.6695 (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: DB3PR0302MB9037 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 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 3fe9f59ef867..19a9d45c47f9 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -152,6 +152,26 @@ __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) +{ + struct file *file; + + file = fget_task(task, fd); + return file; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_fs_kfunc_set_ids) @@ -161,6 +181,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 10 14:03:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13901473 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2027.outbound.protection.outlook.com [40.92.89.27]) (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 4D7561A7253; Tue, 10 Dec 2024 14:09:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.27 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839800; cv=fail; b=LgSHVToXy5dXzaUaZtDYhH74cRFU8PAL+vswLJ5SalO2wGHI6zLI18yTbmc5ABGxEve7yF7kgUiEGhPNbDPDDMzXf0iuJ5uB0SGuTCRzO6UHtkzMzr02bTwK7yspgnGpHbVJj368cCA5OQPjPKxlEidWaT+CAPGlJhI53ERab5Q= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839800; c=relaxed/simple; bh=yWiZsKMONhwdBwJE8lBkbb3su9Fs4JjeYHg26BAsWbc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=l0OAKbnolLuZXScZE7IwmxZsy7346wsailB36lq/GhwayLBfd7XGXxgtc4aaruOuAGgKGyTE1kTnbYn9WJedKYzF2mhPzskgNZiOBQoggAlho1J/cPku6WpuOuj0WNvfDowl1ZWubYD03Sr//BnWuFr7lkQw/czvcv3j4dcWSG0= 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=BzcC49Gr; arc=fail smtp.client-ip=40.92.89.27 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="BzcC49Gr" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IB2In/KSfyRb591NNuCaZoJGJvYGD1bC4wrT454PDhOANf15hCTSGcC/0w3qIsgl+dXxwNeHrBDPwWXCdHTRJ6lDOw/Rsup2sHSonzm54dSffcdZXrWBc5D2irvfAf75N6nesyIe3YuZVabClKZLAwAMUjE9eDdNyyHlJyFgdLe0s837uPBe8roM5BL9DcVmSknepOz7ymOMNUGaNBE6VagwW60DkFfxsaYBKtV4JqJN2NuiJV8EgKcbYfONWqDVInwxbaIykFb2rvmmDN26OiOn8s2nhjNWNdV5gBoemwbhRB8qf5ZKhJfqCanntN+QjyywUW6doOHzPebkgCSY0A== 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=MxEfFHWq6mWVOCCWJzOoK3xDtWzGNst23SaI/LMXvXE=; b=JyuJO0z6CR6LGH/uk1yn9AXSqRgy/UXRAfF7Iv/ZgbPNaOx125Q79FBrbBdzNAgJAsX3oRuj16HaIQXBs06qMlvnD/35zOodZ5WJFJhAN14AYfqArB0VN7R4O95qr2O/3Y+slvAKTZLGCvgIfMmJtMd54JfIZHKRFOMo3xZ/1ryY6TJp4lbH+p3VpWH141xdyPluZpLaP+DHvnwOR0T6cURnJX29X77WEc/qxQnFZnsuXjJEkLAUsrA6i4D0gFfCuOTd+lqaifemrd1yD0cCfznomQZ3Bluk0Z6BoJjIm7D55QAZYmwLcE/4E6ZB0wY+/p6mekv0ic135TJP+XJwmA== 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=MxEfFHWq6mWVOCCWJzOoK3xDtWzGNst23SaI/LMXvXE=; b=BzcC49GrXhybXlQe+SN2YETLkvP1+gjE2Y1WjATFwaGXl3WK+TjdH8QcvDC7bEdjVZTDm6FmRvfUY1y3GnDXP8QulYJRcppsyD7sTi0FOIFZ38DMgyiFvugwyxUDd8l+5WK+hH+A6JeLjLuEj2hRZ5hZTpNF/rCOFLN36mEzKEgYxWbohCCVBK+mqfH7M1jCY9zWUU4/GTpLpSvCXo9GSA1g5w4+MOnK4otPPgUcYEnn3P/N/Xt+wwe2JGSIgslh4ztWW8s1EasHlnvBWr937PxXs+aUbkiwXlY5e6t/M/JaP/V7P/G88XTAMg86LyyspH9S6d7MvatIP+kaUqX6zA== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB3PR0302MB9037.eurprd03.prod.outlook.com (2603:10a6:10:439::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Tue, 10 Dec 2024 14:09:52 +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.8230.016; Tue, 10 Dec 2024 14:09:52 +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 v5 4/5] bpf: Make fs kfuncs available for SYSCALL and TRACING program types Date: Tue, 10 Dec 2024 14:03:53 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO6P265CA0010.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:339::7) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241210140354.25560-4-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: linux-fsdevel@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_|DB3PR0302MB9037:EE_ X-MS-Office365-Filtering-Correlation-Id: f14032d7-f8dc-4aa8-85e2-08dd192450f8 X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|461199028|5072599009|15080799006|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: 2ug1JTsjGf/5NWl1Jzvsls4r+gNMbOZdtlOqarxUcLgMOCjzspe4oGh/UO+pDhqBspdE3so7xD1jzV6Li9DYFxoQPNwC7PORbsunC7TK3c3PNEHors05Ln9U6Mf/LEWC37PNm0D+dJ3IdowLQnv16MEDea5U2/l6JlcuLqRX6n4WELBcJSpnKFW1sDClWB4PgYA1JPoNEqlR7lIRYVWnS3yoD7gDEVoQO8+HMt8VVZrUfU4KRYpozXcESGNVCq8Bl5cMTLl0murUacc6vRggmeYoAonWi2/lI9zdHU6j5Q406C44aWd5L9SwnUaVsH4eNUz9tSU/6XYJPBEoaaMQGAh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1JBapu43WrkXN1XRPhQIEIoz2b5g/klumTKz6NlcJWOQXYoByduzZE2i1k6VSVX+wEissdjiHUuUbOeJbMpE/4XjITInXuicDF/vassTWKZ+C0CFIMw8XkTzKB5OKOMrNvCMcPn5oklIUL2ssnMdQnaKc4/hsnJIhiXQzbVEKS7c9x50tNBn8siOizHdgQQ+wIm4/574yV7/b+arADF6rkoLTDaiKncB0EKixmyOo2ZIgVKmdGN39G+nP3f5NoQq5+T4V/K10PGkFApNCmjLwrkVcb4RRe2KqrGZRCJtzUBVPLIoxAtTZa68LKkswz8Dewt2uqM4k/asfk1CzYR2cDoqCjCFHTJxQC0x0h7PdihygzeAKra/6/r/VQbzWdJpurOJpIZdmN1Pvd0yB8HsCJAAEwNBnYGqpBJoKFsVpc26hRZNIlSRwQ8Z9FXyW0k/LBZeCYlvEx8Km+4NFVH400qQfUt3nTAyfFtRtr6mao3cFYW56wS5rFW9K37fEy0Z6Id3ZmwjkTD6oLZymLmb6mhNMAC6SbHP9EtSTJCO9qJQrbFTu6L54hCOnM2atVWfkQn7UB4AoaPuJBjZc2ICrqg2SF5XEd+BAKygQeVPelyLCNMUBV3MI4s78jNrDSyhvmFENK1x+/rvBIrErSTrMAgn6pfjpjxxCyClotxj77A+gc7QKPwfNdNCWYv6OfGng6xKLUUE7FUIzZHdZeo9xLkUjM20brRjyrDFATNDfCj9GQJgy2mg/7IQhdeiPyQviYOGxWDiXJuBOqrG1HrRfIGQcbTQDzd2DMdT8adTwYCgwea1IglBOr8FsNTPvxLD/a3b2/u4weK+L3EwYCtApCCswpd3ttXjrONznRoJ4k2JB68aaLzyBuoEtcQIq9mC6R6wWSwedOP3g2FJpxxf59gBhCjkxBseNCTk8+VCzVTm3tNBkrdajE1TjdWSmfhVU7VGG8iUDmDbdK X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f14032d7-f8dc-4aa8-85e2-08dd192450f8 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2024 14:09:52.6884 (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: DB3PR0302MB9037 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 and TRACING program types. Signed-off-by: Juntong Deng --- fs/bpf_fs_kfuncs.c | 21 +++++-------------- .../selftests/bpf/progs/verifier_vfs_reject.c | 10 --------- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 19a9d45c47f9..609d6b2af1db 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. @@ -184,23 +178,18 @@ 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); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_fs_kfunc_set); + return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &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 10 14:03:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13901474 Received: from EUR02-VI1-obe.outbound.protection.outlook.com (mail-vi1eur02olkn2046.outbound.protection.outlook.com [40.92.48.46]) (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 5F6541AA1D4; Tue, 10 Dec 2024 14:10:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.48.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839844; cv=fail; b=Azvlg5kDRzzRh/WUhWinAopDNnGfOGjXKSXyiWxPppjl8bmf6sFVD+AlrUllKKw96fWZ4Ln3VumFQsBkx/FTBM8raOEwvRnSMWcacHQ4gIb08bVxaBfxugdYnUAiLFQZs66FMwgBpvqy//IhFdvQFZaYhnwzbZIO8rEdSFcuoow= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733839844; c=relaxed/simple; bh=wK3FzXuQpDXmaSzr0vURQaYKg0fxON8Sw/uCWWRLeiM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=GQD9ntqAk1J07f5TQMuDH+K6ougE7KQSG27OuWRNW5XXR9F+P8WCbj3stALQZvq5EoGwgFtKrGTv3xAgpxgamxhfbyMkz6XfTTgQLm9NADG1hfuvkua/4Zq/3QtyWl2qZ26kliTZVQCyZF3DrMiH994EySopVCyXMVet7hdWOv0= 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=QqPp+g/O; arc=fail smtp.client-ip=40.92.48.46 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="QqPp+g/O" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TsqZHyGXECfXuuFOOnP+ChnLSh7ZTYsdawWV5Y07LnWf6p06sI/nMtkDChlbHLnHzsa6u4ir3u469E4EixGCXuaeOT+VD+ZZcT3247yyWPhSwQdjOt+kU6c/JeY+lszItCiTjM4LefhdvWvfr77dKXdYwYtWJUPE7P6alC/AEU/yATNbVYWNawteNbEoT1bUhKMAB6qu3+6ACvJwVguJnUrQZJ0DkUoXFwhalQQzz+cqFjS24NT9NBS6NC2bKfwSIkDDz7yGpDZEez/3yYJkuz6f99RfaXU/RU3DCAZwB+HZfXr0a8gMdMGfDzxo2uNOxcZValhLJOIbW5+oF8yDIA== 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=LgtfbxzYQu7uAQNbSTFWlhzht014qNv+Oj/7Ne1IMWUcVmdci80HVsfg/ocZ6SPSVlB1ddw+B646U00Yq7Jkdps95JgJ+UkA96BUL+WAIDe3ANg/MpaMIO0VGiE5wuvIjugvlKjCH2ndS2sKL5DQ95rxNWyHhe1X+rs34vS20NHkoHa1InxBkx9iBsE7agGcM7KcdXrd3T0XPrSW3Z9EcjNIPZI3eDtS54ZekAZ4P372S7y43e51I6FqFlC20eVJQPRGAAdJ/EybnyT2B9f2I7beBqp3bJynesdtwB/V0FpAn6CRdRhRD3nxpSXQXmKz2AMoyA4dqXlxnlefdtkMAw== 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=QqPp+g/OWgALC+xThaw7AF33at9VScd9yGzTAUdH7EMlJGhSuu7AxuUK7PZKi3aB5O+B6klgeCRlUBai2iEoaJahyL4Fy3U6KtkHGguW9e/ZLRy69W7j9NOwyZu3XUI/9O0RF7WlZpCYnM1j1e3kbZduMyIUj7gSgXnC2lUOeqUNAWdCT1nzYB9Awndpq5F2WsKU2S0Brsh3Dxqdn9eGK/S2Pxiu0oFtRi2jZUdt85nGJos2yon/2yvAw4KDiX6rhsH2B9sffAEeisfTr8uqhxdzRGIi6EKLeUFZ1fDZ9/IhAkko/6aXlo+/gL4Nq+ADyOvjfGsyB7y77CQ1F9PBEQ== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by AS8PR03MB7704.eurprd03.prod.outlook.com (2603:10a6:20b:403::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.19; Tue, 10 Dec 2024 14:10: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.8230.016; Tue, 10 Dec 2024 14:10: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 v5 5/5] selftests/bpf: Add tests for bpf_fget_task() kfunc Date: Tue, 10 Dec 2024 14:03:54 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO6P265CA0010.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:339::7) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241210140354.25560-5-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: linux-fsdevel@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_|AS8PR03MB7704:EE_ X-MS-Office365-Filtering-Correlation-Id: 5740ffb6-73c3-496a-dd16-08dd19246cdd X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|15080799006|5072599009|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: RyVWd0305g+G3J0rK4hQl9DSObi4eZwr2vi7ET3OPeefI1pBujmdUSl7MeAra4MYJ4bJOg0CknOH48STWlH54feua57hdlgtUhUt+SIRjn5dL1tufw6jvnmcikZFmaROxTxvX8akUQ444LUvnLNmCc0YqhOL6rHe7tcg/wLboWUmP2Z4kWEvXE1gAWhd4XbMVINJaodO4l44mSoP82U+A/Rp8oe7PI0KkUTKeUrroF/CFogTWEc8EvEzesfmSQrcBHHD1MygzktbzoVm96tTSu+3Sh9C54VHpPh75PFeQw+l5L65rEA+Qgy587E7f844CWp686moXHzE26lgD8dTEAdiX63dOcYQdmmadhyY1M6Bg6tPTkS/2wwZ35rznyYnaORanCJtej6llwH6J8LCvjXfbNuSwYgLKTlhuF7AjAbszdLqFyOhQjZlj92tF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cTL5Lo1QAB3GpQnfEdPGBFfet6tX1wVZLMFt+09L8b6+kOlgZxad1RDZkYKCLJiBpTqvELPR6gnaEnxFRPybea837cdSrCFpZhwD0nqA8yLn9MdDvA5GdyL0koRa1ZhyEG/lnRvjHviH+hgi2xYURnZEAXpjJWsroRxO78jYm/ThbZZBo3kPLpbeSlsz7zbRqYd77E5iSFwgJDgzx91PK5RRTtJFBMwRPxKzhmnHePnMDINP+5jIrZj9ZC1xGQHCDewl19ySjEGflEYTYW7BNr2IAb2l4f5EXLDmFYfSnfe8CRszmgzQqxCzK8zb5bNnQYr37dQzmKlZ2rIO5XJ6n+7jmn8TmqWWehHBkW2oxm4uFkYulAc77G6koC2Nzd5DGlhqKxXhbizjZDfM2EP5m+7z6ZPa4JJB2/L6COKkQsHWpwMqVkRQTNoD0+KGA4LyKnHdcd49nfi9/ctpiL/XUW6bUjB2nhXTclywJM8tQGVNopqs66I8foRW7tB5rdedy7QpX4UT4kB/UnkOHJFbI6Bu7BNJGKEqXeYzHzdMp6LVcdkld1dapobjyZmqf/eZJKsxsUdlkondg9xLzmLyA9kp2wCLvSPCS/RpHVB3zK1IQ0TczKerGd2kRxREkiqKocTIlvmlZiLo5c1KVMDASmhTCm9DNDy+u/qXRY8dX0rO8dS20uOejQkzO8i5qXDo8BYhxFuK4EBrMPCYxoqeqOAqZXoMCHPr3q9AmF77nI3y4qvdwIT9HP7KuF3JY+8TOSgq9m/gQ2z1DYRBWWGQn19Lrof4wSz4qH7qjKnj/A0OebT+6cSlb X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5740ffb6-73c3-496a-dd16-08dd19246cdd X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2024 14:10:39.4951 (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: AS8PR03MB7704 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; +}