From patchwork Tue Nov 19 17:53:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13880379 X-Patchwork-Delegate: bpf@iogearbox.net Received: from OSPPR02CU001.outbound.protection.outlook.com (mail-norwayeastazolkn19013085.outbound.protection.outlook.com [52.103.51.85]) (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 AFD271D1F44; Tue, 19 Nov 2024 17:54:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.51.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732038897; cv=fail; b=k2ZAMUhsUSdDQyU3C746BBdTkeg8vG1gQKFmKbNw9mBBZ7Pz1MbXRKDuctgACNwyDjUxjmzuSJWddGlOw1Cjz2qbj7HYGQSLhKYIAEsm0SEFHleKapjwmLmTJo0GPaIxJLvdSuMmCuhtc/i4ZGM16KlmSBJrSl27qZNHs6vMbcw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732038897; c=relaxed/simple; bh=0j9SdFmQmc/JsU7okW3qy/PSDzL8BqqsZkT7lPFh8UM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=nInkltf9GMOgCA/Bz91gHgWjf2loPbHhlv4GS/zWpF6tM7znvEmygobwZw5ezwL5S9KWuAwJIVX5sjQ4OxdLpTNguODWzv24rEpvfM1x3i4wwLLXDSOz5mlYUo3LAZyurP5NOIVJnmagGi2hZn63+o+pMy/nsOVwyOFlaUC8kMY= 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=MtKe/P2F; arc=fail smtp.client-ip=52.103.51.85 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="MtKe/P2F" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=M73Hyot29wZgh/EEiccwzCXlS4Ml/fK6Otbe4ekm7mcCr2dECOFcu99+of93duYD488h67cH64xLiK1vZ0Q0Uvz8/Z6rXgrdDx7MLNO2JPosEVpn7jq7B2QeG1nn8/jl+uvVdpmP86vp1weinti/mE4YvYT+bUuYu3gQZfigO7yjpjfYGdPh64C3IUXdoiU2ZBq8P30VBpopRejrqh+so39AzAxUKuheJ1BUbpWHEDaC6+NQRTVF0OrKivK4TrR+vxEUwS4zF047JZZDEs48VRAY8NQhmnR8+DPmVvMAYz2iwi5SCiZjxUKlRBF68SQm6EvLjCNToRxkc+alOwB3Vg== 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=EWktmrnogyzW9MyoepEcA+7pM6Gz/OOBM2kq56ApKTs=; b=kPUwl8Yh4Sas/iZdOD1E68/1DmKeJl4lUTCoHbXTPP/zAcru9liyvsEEdHU9Z986uX6XyHw4Jrlzx2oU3fbIKLxDPPxnkJCgK0s0Lvci+pg7yudk6jWN/evUACWJANHWhKvNq2Hf3N09QlPU/yWPxPMi6twcDJudzY3Vqb1/fphbwwsVfSfuazQG8sl2A9N+0aieDdQAmRCIM67RunFRvBYwDdaumrH4xb7vjT6zN4URrnT5tfuKLTlmMtsJS5XJ+OY7B1r4XqXxWDvcKDG9fgUBgiZ9AoE4t9iQQk9PUjjsIF2SRUayRiu4lz6z9piH5e0BWvs5aMfP/5m7PaRYrw== 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=EWktmrnogyzW9MyoepEcA+7pM6Gz/OOBM2kq56ApKTs=; b=MtKe/P2FLrdbiTfuYKK31TdSLCelT1rGAGOctH+5uB712YMd0Z74ArTRppt2zcHdDzXjHKIWKp2/TEU/rCmArEksGIQv+0G84T0N1K0hjYqDPnz24SWPkwF16uPZLAyhH2EcDara58tsV2qR+n5a2oUSVbpVAKXxyYlpwjN0ETF20IVJuakieoetj311BdKpi2g4NhLT571Cnbsb9/YJOiDfqEZ6Nfzw8tW89WvvE+J6WSvQu6NitfuD7etKMBene3eL4KCrqBzElmLt8MQZWmuCJ9cEQrJrg1FzY4z9CKKxJp+rVc1NdhRhc2DWJ+H7jsV3Ruwktl7Ry5ZQ9YqsMg== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB8PR03MB6170.eurprd03.prod.outlook.com (2603:10a6:10:141::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Tue, 19 Nov 2024 17:54: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%4]) with mapi id 15.20.8158.023; Tue, 19 Nov 2024 17:54: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 v4 1/5] bpf: Introduce task_file open-coded iterator kfuncs Date: Tue, 19 Nov 2024 17:53:58 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0187.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a::31) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241119175402.51778-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_|DB8PR03MB6170:EE_ X-MS-Office365-Filtering-Correlation-Id: 148acbbb-d00d-40b4-e5bd-08dd08c3445c X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|461199028|19110799003|8060799006|15080799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: mCdbfHCnp7cTsvF8BVQVUzFzB1TtYqbatZ9a2OJZtvlWsu8yfNRGLKrIVmuhshfuLKVw7yk/7jv/BjM1RoIXfemcQzskbkOAowQJBaouDpKVwfFJ/3v60wmE4vFcblDXq26ENi7rgUYFILnXtgztC6PB/EI9c1cfVtChD/mpu7beTCk8HRHmylYkalCBkE92n7ptGffiGUtYdyzFgy8bS+qHBrYR8Z2084a1FgiPE6cRnkKNb2QqMFW1fDoabS/Ian87iYUSoAG6IrJSR7H+EdZvgAPCCutkvrQNKNNV/0rC1zKhjY+JxQAY0pOYfQwBca03FK0pJe52buVqd1OIJGzKLMMIJahRA4dnIXNsVC2O+Pxka9QuIu3YeexnG7VQD5B6dd//phGx6oIPGxrerOd2/1o/gWANoHbjKalPP5o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MHqaa4VKjWDZxNdZ/ouBzcERJoW68aAa04q5CT1gR8wzv8wE5XLxa8Z7LUONVUxYGYXThGK4JrjLQk0+3QiHLPHGuqCiqmNOfT6kFgK3bMl7gSPNuk7UbFrr/lmhvoKyNX5d7tmBQ32dpkYVwne2EMoChKsrK0xvjzXYCSsawRyC5gO6CZQ0z8K+q0piDa9zZlKFlEvoFn8hAVHjFEQMuVZbzxQvgxxOaSavDKb/K7/9MlvY+p9PcpSjtReR5apCLd+zIxiMcpw3cpvxSxBmcuVzBOqQRCnhis2encTxQjnDuhGWe/yAfUNgohFiw0uSZu4DoWIn5zoCg1cGc3SKMhWTmVlsbehqEZVeKB7VAQMP355U+HKiD7lHaekn/fjlR7xmVRoB6JKwdrvNlwOJS1yf5QzWVtaROu3b8wBLAPhSQdZingk1XsSxL9d5snIIsmqfwNYhsBkxVUqkdUfKhyH65WWChTMBAsJb3jfmBNT1kxBaak7/qySjDpOenDzpIdlhVcHgIvIABsKJdErIXSDRlia+b/dmSeMtKB3wfTBAjVmHJzYt/bdKJ27ImB0eevNxqrFIo8dEV+X1ys1k2Jz0IOyk+piqcFRx3gkGuc7pMOO87PhUGfg96AdeT1VVfLmJbXzaFnvwVrQOWPHSvZ+vQGvTplj7q0jyRreFV1D0WfC5VAnPWIhRJ9CK1GcGVDxaRdK5A9qhQqVNhLnSv88QH0Y9MXFOJD8HEhADK9GyYwO57GsO8PqSQLKevf4Ed/huLGtYHEa1ejK7SXu3gNpWbc6ng7oRHDVMPdakNhLFKocSy/jEyTFS71wRqyim4KEZpDLr4H8Enw8iTti31Y7IlCpABz3fY58uWmllYRZ8EaSkRJ X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 148acbbb-d00d-40b4-e5bd-08dd08c3445c X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2024 17:54:52.7609 (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: DB8PR03MB6170 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 | 92 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 751c150f9e1c..ed99d2bfd425 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3130,6 +3130,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 5af9e130e500..7a3d8c9c3480 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -1031,6 +1031,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 = task_lookup_next_fdget_rcu(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 Nov 19 17:53:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13880380 X-Patchwork-Delegate: bpf@iogearbox.net Received: from PA4PR04CU001.outbound.protection.outlook.com (mail-francecentralazolkn19013087.outbound.protection.outlook.com [52.103.46.87]) (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 CEA8A1D1F40; Tue, 19 Nov 2024 17:55:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.46.87 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732038932; cv=fail; b=LpMlJAxB4mRpNFaF1pV9NdGyf6iFJWtV0n8oguOwZC8i5ZlFeipADfVoGkMfEFLGTHz7xDRfir44PWWOGHTu08V2kv14ax1z88nIqQkOZZa62E+bLggxMxVv2LE5hBIUEmYL1HL0/zlG5+tcwAMMd3ZS0KNbWRG1xZVY3wBCt70= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732038932; c=relaxed/simple; bh=qeRx/ElKW42WaCfHdn1+zez5cmtptzdhTZ21JSOEYYo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=CiwIFeHslCI7BcEMkR1kuozns9tnoIH0hgMD7GiWesRfUXxv34XfAHvtlp/XndA29p8d/ZdQaerkQmbs3OmjhYREg6RtYg8fkWDdV8WHIOY2NkUdSz19vTIMUgnEsdh7dB+vlDpc2RIpnBRVGZzV4oJ3O3hClSKGHUlJaNdfZq0= 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=GSF7Jp7C; arc=fail smtp.client-ip=52.103.46.87 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="GSF7Jp7C" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ou0wV+pz98l4ElxN3Yp7gS2HLobIZB7V/KXyjR+0EBHsJ5hwJDo/xewnJ7it5CfrPY7kNnWNqtbkcBq5/KYuK95dUb3YRDkAxI68oRMDFuwFyatZd/FPAQJ88A49mxg6JQI6MenFRk8904bpdZdBLMQgJ+ywy73fHXaMa3hJ5wD/xIhNETKrV95ndy9Q5vs5n4w+ebfYx6XD7G6SBjb1MMBNVwqJjF9srUMhSnnksItJb/VRG4MI6ZP9+sx6OUG58HT6u68QmLoc/N+S4/NZ0IcRZp2HLbunwqMUSOAOFRrvLXTVmiO9owORDmzc6b5RZgipx2p2sRtQ1BUOo58R4A== 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=b0BwRJvJZKTDwMWv9+DLauZ8Ea+GIe0lmNITRlWXxUw=; b=w0HLiLvkuC7yM4SFp3gUdDDifn3gJjhUpFq8ZniZ3MYxWmOubeT2xoKgvfB8apk/xjL3i+6uttOav6BtqVWG3a2N/HLBxMX12+ej3SWyB6RM4/yfVcEie/qV25pszpoYBE2oq6vHXywPDixYG1VXj9GY7JnnPPB1bb+vGfvNEyKC3BzYCj6QJ5giH83MFqNqTmLXgUysZzrXkIhks8hdxnYvEdSKiudcTPaB7zCjyejMIxTWOUw4Jwdn17nMAdfkPmoczP7BxJfNzmjW1MByn37Kfu4ATs2JDWwq+78RCH3jlTCo4gMJqAyzmqB/CMaXle+7Z0x9nG7ucXVktL9frA== 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=b0BwRJvJZKTDwMWv9+DLauZ8Ea+GIe0lmNITRlWXxUw=; b=GSF7Jp7ClGTGU8Ifn4z4lGRHW4xJT1tVEn6kemSkcEdP9bvUu5GeQm4YBc3HmE+9QkpMxWRIUSMRJWoWoqLyXEGtX3TWYuglo2cYuULTbWMg8qqdgta/xZdc5MILMdfyMiPAd4J1By6zfTLB56JOM9VeM0zmml2Gd6y4k1QtSUUKavX9HZum9D3QK4QfhQbO4BbDAx1yQe4Dk/RKkFFEr5JCOF/qBmN7TKVRmpGSViZYr4ANCKoE1+NYeaFuUCSIaWjqctK5A+3NjZhPn4n8vd8S4CXvhCGtcwFjiSrQTKzTnXMXA5EMMIKu4nEPflJR0mwTVaq+9JCH7V+nDLXhUg== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB8PR03MB6170.eurprd03.prod.outlook.com (2603:10a6:10:141::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Tue, 19 Nov 2024 17:55:27 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%4]) with mapi id 15.20.8158.023; Tue, 19 Nov 2024 17:55:27 +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 v4 2/5] selftests/bpf: Add tests for open-coded style process file iterator Date: Tue, 19 Nov 2024 17:53:59 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0187.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a::31) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241119175402.51778-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_|DB8PR03MB6170:EE_ X-MS-Office365-Filtering-Correlation-Id: 98ae0139-e300-4008-0f49-08dd08c359dc X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|461199028|19110799003|8060799006|15080799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: 237cxXW/ELxi5VkZAHqoC3uq3wdkD2RODPdT14a8wZj7iHMyakeqezO5uwOC9gWBuL+as85e/lXkNUOCfUjIqcx2ZdlYyZcAMNGG1DDRVGx0fBp30bLx1xB1dsfTP/58cVvFWtxdABbtTq9Gq4jR6QsWlW8wlX4Bjo8Shg1IAEYPFhLotYME5gSB7PrIdnUjfTm8c8zYWq6+h8uU0gwWnls1ORC30qlvzA7LEy2dIqIeANw1LGIEPaxrd8GP0Rpyv7ntmCZPMaNxff31C+MaPiiPg7RT2zq/9WWjxmW2kO8txaJV+yHBPGh8VUx9Ztir4LVYQLplGVEEEekKrfhpHvpMjzy6mXmIMHeJwrg6/ZrPtI/DyScifV74c00BgflXXijkEVDfEN3NB7GQhq8fcfglJggG1emaZyhpznVVc1tHcxNx46hWO6AnqIRGcs+sa5oycdrXkB04dIERlnHcaepH8i2nNLviUQrZr12ATUO3bu/5AnNeDNUbKW17ukUXdSo70Vq80Px3RUsrE1BmpRVEcj0V0VE8LjVFypThHV3nO8GbbKXYWNeMiGmNFqk316uWfQQiII/tLazAYAw/CFc1etOTwiWEfCUsVUw8zdzQgOZGJvm3PAleotRt4XRbf4boH1jBGxr6PmCuKAuhbf83mYnMQUGnTMcDLRuvBIPBe7PWfD9Uztkoe+cEQaW0o+lJNqNfseHbT0eewZjrZrSusShVMCkP3QD/3jAUQGnqD142/C0FdqdEjmsYB9tNX9xJikG230Lhwj5TrZyKoIuS/dsL1B6tlUSyTcAM8Wlr2GGf7RYKZzmNWiOEYAmLeM1+6Hye9BMcHb+I81hArvO6n3NO5x88rvVzTs+Shodpici/caBHrncmf4pJ5t9OHlk2nEMBejrQgDfhYFxqFZ3l/goqpFjr+t3WVqKM9o3WfuAHtGW/4g5GI1WCcw42kla6AuPaVXGL01q9+bc6+gRDNWDzOKZyThW8EBa3Q1GR1wZkTg/4tRTWzcG1HUCGLYnCFzNEsdb+Rx1reul11A== X-MS-Exchange-Ant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IvR/QXgMPKcn7UyoZRRIayuPVXAkX9JQhHCfJJnj1RVOxcvZfPcUsdQDAfOS47FNWLTHYP+7Az8a+21qyxrEzPWZxxI3PQ6m3BT+55/hVZvtsGhg/QZQZLmQF7NmB4yQYcJzkiAtlom1iJMyCbWsPaCj+7tKLVwB/tOcTByKoPfNcpZymR3esB90IBn/Fopmu2ukU3zNkYoi+4ghBdQ2+2k6JcVKVQSLMcwXihwXmz4/jN/gJ X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 98ae0139-e300-4008-0f49-08dd08c359dc X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2024 17:55:27.8620 (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: DB8PR03MB6170 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, 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 | 105 ++++++++++++++++ .../selftests/bpf/progs/iters_task_file.c | 71 +++++++++++ .../bpf/progs/iters_task_file_failure.c | 114 ++++++++++++++++++ 4 files changed, 297 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..4944810e9b2f 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,104 @@ static void subtest_css_iters(void) iters_css__destroy(skel); } +struct files_test_args { + bool *setup_end; + bool *test_end; +}; + +static int task_file_test_process(void *args) +{ + struct files_test_args *test_args = (struct files_test_args *)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; + } + + *test_args->setup_end = true; + + while (!*test_args->test_end) + ; + + close(sockfd); +cleanup_pipe: + close(pipefd[0]); + close(pipefd[1]); + return err; +} + +static void subtest_task_file_iters(void) +{ + int prog_fd, child_pid, wstatus, err = 0; + const int stack_size = 1024 * 1024; + struct iters_task_file *skel; + struct files_test_args args; + struct bpf_program *prog; + bool setup_end, test_end; + 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; + + prog = bpf_object__find_program_by_name(skel->obj, "test_bpf_iter_task_file"); + 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; + + stack = (char *)malloc(stack_size); + if (!ASSERT_OK_PTR(stack, "clone_stack")) + goto cleanup_skel; + + setup_end = false; + test_end = false; + + args.setup_end = &setup_end; + args.test_end = &test_end; + + /* Note that there is no CLONE_FILES */ + child_pid = clone(task_file_test_process, stack + stack_size, CLONE_VM | SIGCHLD, &args); + if (!ASSERT_GT(child_pid, -1, "child_pid")) + goto cleanup_stack; + + while (!setup_end) + ; + + skel->bss->pid = child_pid; + + err = bpf_prog_test_run_opts(prog_fd, NULL); + if (!ASSERT_OK(err, "prog_test_run")) + goto cleanup_stack; + + test_end = true; + + 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_OK(skel->bss->err, "run_task_file_iters_test_failure"); +cleanup_stack: + free(stack); +cleanup_skel: + iters_task_file__destroy(skel); +} + void test_iters(void) { RUN_TESTS(iters_state_safety); @@ -315,5 +417,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..f14b473936c7 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/iters_task_file.c @@ -0,0 +1,71 @@ +// 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, pid; + +SEC("syscall") +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_task_from_vpid(pid); + if (task == NULL) { + err = 1; + return 0; + } + + 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 = 2; + goto cleanup; + } + + if (item->fd != 0) { + 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; + } + + item = bpf_iter_task_file_next(&task_file_it); + if (item == NULL) { + err = 6; + goto cleanup; + } + + if (item->fd != 2) { + err = 7; + goto cleanup; + } + + item = bpf_iter_task_file_next(&task_file_it); + if (item != NULL) + err = 8; +cleanup: + bpf_iter_task_file_destroy(&task_file_it); + bpf_rcu_read_unlock(); + bpf_task_release(task); + 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 Nov 19 17:54:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13880381 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazolkn19013069.outbound.protection.outlook.com [52.103.32.69]) (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 B941D1D1741; Tue, 19 Nov 2024 17:56:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732038969; cv=fail; b=SYbrP5JP1jeo5LUby+udvQZc4zsyVJ1/VZa2tNFZmpsk3MaMY6vVm3/NrTo1JXpkqiQZgd5216XQM5HbpsKTMpHal042kIxLBezU9ATjBnzuM4zfM/DsdUMstQErEYxxADs00KITdpWAU3d0IQYcFyiInXrOmyZ1lc+3d/WKjrY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732038969; c=relaxed/simple; bh=Hnb8huvwU3mh/8ZR6urTMVV+Dt50GEkGMo9DjBjDEwE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=MDbn7QUDSVNUobMYxim/l9sRdnUAr0fYj/NHFKnUPtbfluUZtqjF3uNmBdOucrgiNaRTcpxUwiZn5lfOxn0tvRjOPqJKpsW+4Mp15cr65oDxM+jpIhkgwlhG4L7LKl8q78FN+3Cg9mZkeonMNx7X1LeHaKl1tXf+mZy2t2IkyGk= 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=CdMaQspG; arc=fail smtp.client-ip=52.103.32.69 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="CdMaQspG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IBnDb47q81wgxqvXox6XLPyzp+pd9x8Nc06NSMK7QwjX8R8VuBfxe48UpQ9owEXHLCn3d3RtPB7lPyeEIxTp8bJzNCzbQUbJMnR7OjaF9CUbZtb09QFUujCl+qns4DGH3t3cjQyvzNabEpdiHBZS5J5+1XXpXEsWt3V95C8p5Yf9ucTWW7UJIb2y20msiGwI4evpdqEAzfARdgUYuh/DddKSU99kbox0zKiyB557E+r3JQWL6JcTgVAOIp6HfwIif7oarUrd/f+3Q1RdSz6H/HD+478WFfEsYstd528CYurhjjEfLpYcZIwLx4H8K3BQK7he4Yph7Nt6YQJUByCOIw== 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=DNIczyi3cs7c9SzEaD7LW6yrXQ/WOkWlKbKUlJhVuQD2En3loz54WKnqZ1PjGSWqfCYGrfkzk2K/9aj8fphkIL8uyik+A+ZKVOHD1Aj8LjtS3Nc0IFqgCuaqAk+KTGKxTPkFItrJOvglidMO1tbA2/5DgI8CiRhUAo/cSH3m59cVESgOmHx2MXX6ChgUV018rMldokKy6ZaKlLAhpqholFxycAJSLBMgg5tgxExqJU37/EX8Fu9b/j2VC65WtTOiTIdbfYN99zYo+XISc2H3YU7yXL0FGSB1qpekueuaMVH/WOHlANZEsH8fl0tATU91ryGx/qWQR37wBKxW8N1Izg== 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=CdMaQspGUJ5gVF+WbMihXUnsrU+A1m3pAPyX+rv26dsH15Vy+gsEwTvwqcL39DfYYtJKgDGLU+s5SJiMXFoDUli58OZzqW9/H1VLBuvna34lRpwQnvohWB38IM8mHeF6VBpudKfFEx/qzcGsVAKkJcJUMk4R4pnbTzS1PCabH29m1rjsQLwIKP35+RI+km+4zQDCmbCRJxA8LfgGa9xkWCcwDPRUOBS1Ee1wnQlRTt9ZQS56pXrlNMm84+JR8b2XzBnj3tXIWF50X/ZK0dN+Y+h8P8J/N8cV1IzHCbMwcPoLBUAlLVttveHAabEKP0Veya81Y0Ffk642tNOJFnuuBA== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB8PR03MB6170.eurprd03.prod.outlook.com (2603:10a6:10:141::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Tue, 19 Nov 2024 17:56:05 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%4]) with mapi id 15.20.8158.023; Tue, 19 Nov 2024 17:56:05 +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 v4 3/5] bpf: Add bpf_fget_task() kfunc Date: Tue, 19 Nov 2024 17:54:00 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0187.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a::31) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241119175402.51778-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_|DB8PR03MB6170:EE_ X-MS-Office365-Filtering-Correlation-Id: 1acd5c80-4df3-4e77-ca5f-08dd08c3701b X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|461199028|19110799003|8060799006|15080799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: kVEL9NDfrs65XI+Uu78rEKIi9F55XzzBWBsqgu02p3Ti+KMcceyCyZW3JMB8sZkFzTiUtl+iY4mmvEpU/CZygFdJtrsLHQl+lygjy9WiC/xUxB6AOjS09vygPXrqsWDLOJixoSIRDtko8PEZtpXcAOk5VhlSIIjeSpSO10syXgcjOg1+o0RpK/y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bnAqGIqDxqZhVqomEBp4D/eRELJn8Xe6Mc X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1acd5c80-4df3-4e77-ca5f-08dd08c3701b X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2024 17:56:05.1075 (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: DB8PR03MB6170 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 | 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 Nov 19 17:54: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: 13880382 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazolkn19010011.outbound.protection.outlook.com [52.103.33.11]) (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 859281D1727; Tue, 19 Nov 2024 17:56:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.11 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732039008; cv=fail; b=LNOe5YiPZ6WSlftYSx/QsXd5/1+ki+5Omn+Qp5tVYCY1hYWhaCzosg6xdgxlvQMvKdJ47CEUDSYnijcceVp+jAl8C7vD5ZTvT5+7CWMQ1RqO7IvxF84QFR9oEEw6HQHCm3UH3iJM7WEsVSZtuOR5parxZqWqVsgkp0Pt2Kzh7x8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732039008; c=relaxed/simple; bh=jPqMnhqEVjrs/YtN0Nl5n4/7czmxm8I86RQdrPfoxmc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=FHZ8QZ8mQnfvQIf/gZ960A9wTNw/8krytIVh1pY6jVgiJMzxBMP5RC3febP5a6hVbisPThRhOPgVITG5QkkqdUai9mN0/G9KSz0hqhctTIQUDNYGVi93RXDOB9JSKi49d7ZB+J+UDSJqEayyz+AWzhWsyJ8lOgc4W/fK09PGa3k= 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=T0Eh/QXb; arc=fail smtp.client-ip=52.103.33.11 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="T0Eh/QXb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=RGOUbtcljzQcOjjUy4JzoqULL6fDmwMinCr0OEnAa1VUPBkZdZoZLHWB6wMZ1fXXoKF+mc+sS7NQQJ2C2c5nTm+Z1tjz6DcVr1WXFK0KbvATXmqKyTL+IYM5ETLQ//XyBPbqnQ89C/x4AG1VklzEgg5Mg4ZIYL0vCgb4rdZtv/gYZYAxUFS+Ml7l6BxzGMmjMKEW+gKf8bEQQBn3AMW8gf0LH8yP+Fj2VOykDWi++y/yfifLHHQUl5AD1qr/fgdK+X07lEX9ZyIOe49XntV2pOdno9i/vMTDv88nyohbN9dtV4Gjn+Tdbqce1EpYI19JnVaHzbtlWQrViQWNCp5jMQ== 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=yyjv667QTt66YdRMMFhmiG1OLZP10SLSGw6sVN5Hwfc=; b=hycAVN38ks7e3OY9F8eFZivQ1hVG3GGM+DT1+bGPrZv99vCULFTL/O2JtKPxdEMK78V6isU64fLIMo79p4/mn5T8/4aQzARKC8MlSNqLZGh8G86egr5LcPGufkXqDFpUJKbq0HcfoPb/QlmDkHbd2sJ7GVtvIx0jvh26LSu5l7AFYUPY1e7zyVHLtFuevfQAIScK5NBbcqsFkVpUrxppGUVLS4eHRq6/j2HXyshp/DMhaISCFkTfBL/GXp/6Rt3yFEi2I91zILPM49j0dEmHB0fUTJ8OcgRQALFwdjxwKi4kkuyi6Enxf4xor2kH87oyFuxxBCZB/dFTa00EU3IjnQ== 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=yyjv667QTt66YdRMMFhmiG1OLZP10SLSGw6sVN5Hwfc=; b=T0Eh/QXbBJJO2En9Vh18upsNDDbwqXbRT51dLEiMNTbKPTCagUli3ITUgn/LuSbPmi3jHcNKfsX8bszRtssUy+UgJKg5d7YIiBe2x3m463AEjVG0RbQ3opJqcOve7KJ+BKfT044PJRtWEL2ofd8IIxXWWveuNgrSNxrurg+S2GuEuvBtAswDgqmNi7nIJeXGtsLkIZGD5/tUBnSFnjyc3TreKOjDXBAAnMY8I0FHxGOCHtt15s8BYlRoxy5BHXAJtC59QK8XnWlFgAH2/ZtyZfhR03v9f5ItfmvQ0FB0MSc9x5RmPh9F/ejH+6R0eL0X01sHsqXwfe81f9q7sC6DZA== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB8PR03MB6170.eurprd03.prod.outlook.com (2603:10a6:10:141::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Tue, 19 Nov 2024 17:56:43 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%4]) with mapi id 15.20.8158.023; Tue, 19 Nov 2024 17:56:43 +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 v4 4/5] bpf: Make fs kfuncs available for SYSCALL and TRACING program types Date: Tue, 19 Nov 2024 17:54:01 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0187.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a::31) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241119175402.51778-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_|DB8PR03MB6170:EE_ X-MS-Office365-Filtering-Correlation-Id: 0b053433-899d-49c8-9685-08dd08c3872f X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|461199028|19110799003|8060799006|15080799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: U+7JQzZW97e8ZcgiuR/9+lyAP80cCLFlCweX3FahTbp3YaahxjW+exbx5AE0FfsHI6Ea0R6d+bqHteO/+aHbE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bcZb3ekJo2vGYKCHUTkQsjjrMPjcYnWCFhM+MOuWJc0p6hCwr8zKC870anjyZvV74QbFsteo2GiK+fKKuyslVJnTFRWayOTKq+jUMFF53JZZWMDf6EgZTxx+NAZsbRyXls1XxTCoeQ1w1Eg0+04qEYXA0WB X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0b053433-899d-49c8-9685-08dd08c3872f X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2024 17:56:43.8384 (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: DB8PR03MB6170 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 and TRACING program types. Signed-off-by: Juntong Deng --- fs/bpf_fs_kfuncs.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 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); From patchwork Tue Nov 19 17:54: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: 13880383 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011078.outbound.protection.outlook.com [52.103.32.78]) (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 050E314375D; Tue, 19 Nov 2024 17:57:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.78 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732039071; cv=fail; b=DP6FXXHkcNuylDIIKLdBTTTcwp/m2UFyEssYaxZXD91B+Fj/97rX+7WkQchJpbJlXG5Mgup5zWHWMBuwiHHXgqyFjInOO7CIMglpXVOwRgIcGfCJNIFYMD4Ma1h4gzNfdwVaRhbiemsxtUyGwGxs/ykkQtZPpE/v370otypQ57g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732039071; c=relaxed/simple; bh=cfjuXJcMBVBRVRlWMNWEFl9Ta8bYkTHoDzGDunfbbA0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=s5wPswJtuIgC/6uPixxnpq8K36zcBWbDubKJVuX92qIzZcWgFd6ky8Prm0Asl+uxUzAU8SkEpVhX6Yqy3AAnrWfzI1j+8xcbgPZHbutUcQ9g+27pX+V3yb2rC3VDs7ERHBVDt21Rgyo+sq8HHmyShoMSPZlXAmp7R5XwVuMDeMk= 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=lDcpFB/k; arc=fail smtp.client-ip=52.103.32.78 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="lDcpFB/k" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=fu40+0OGWEtjqLq/KslQqfN2HFFVvoosQhQ8Ro0cVc/YDYdlJcEvVxA1AAkmpyLW8u1FuIL2ob+b9ArG+XPZken+bl0f8rYJCvrW8ygoHqUlPmeoWsDiLZVtfpZL5JuPJ/QedlhfB1zZpGWc9eZsFru72fz+r0ydDfYHUliODP/wsL/QnzgDHfzWg3B4vgLS/b8moMNQ/Ho8nlFNCg1gQ+ixEl3X0iOZMw6pzRMC34hUGUjhR2icOA9c9ZWljNhx4KjlcRI01ob/bn6CjLfQPjSTwBXlrEdP6KyLKPXevuESItUa+Qy4Q6vUMlMNQUSOc3IwnVMMaKzHgGYkRUqC5w== 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=JSKDnppz9R+W1Lb3PYAiSs62avuREGXGlx1KBx57RU4=; b=gIDZNA0Q6bOl0tYMNXIvYGBl/PtqOhQ3eYogn6zG7us7BryL2CjncDFufwKXlnUYJHp+muR1Xpg6GQFEB9DpYU+bP5UUxAQa+74KU4+WVl8fjK1ucgXJx0/zheYhEtUqcv/CM3pDCKvUwQomSgV/vrUtLz0ioybJ7faMStbUmrmx+jSbtOy1f45s8asVnrOFvk8gSKRY/Qc/qqAlpCm77ByZRhcOKK2tM/1Dt4rlyN/eD+J2Yh7WVhZazOLPj6EeToHO+yiGh/sy6ACOi6/tluoeSvEwWM6Sx/SsVJTklVk9eyx71Jq/pKn6YfPUjTrSTAKRbjAyDV7BasT61i9BKQ== 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=JSKDnppz9R+W1Lb3PYAiSs62avuREGXGlx1KBx57RU4=; b=lDcpFB/kdnj0JXIvXsQtvUPsD3H5UHoONcn3hflx2xE4rQRfoVmii1A2YMpzK3OGQESpOo3Gd8GsTLHIWPvn8SDrY/N7PWjhXZkGRyVau66CYtju+8Jbd+lMFAwk0nfYMdEsaJMpZgfzOF/M/Y+oFYy1iQJ10DJeQ3jl9ZjaVzcTJICVq6Uc4izkPXTIkibAkOJFHX74BWhE/ntvZlOijGCIX9gGjwhfSjFfttI0YLAAJPNk4vzRrK06padwiannVCY4M8g5JeWI+Z9X5o0GKGP0/KSn1Ic9/41as0p4FJ3LMymm1B5bJgKZnhBYYIKVRUgA0a3O29ZjJ6s/a/XD7w== Received: from AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) by DB8PR03MB6170.eurprd03.prod.outlook.com (2603:10a6:10:141::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Tue, 19 Nov 2024 17:57:46 +0000 Received: from AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8]) by AM6PR03MB5080.eurprd03.prod.outlook.com ([fe80::a16:9eb8:6868:f6d8%4]) with mapi id 15.20.8158.023; Tue, 19 Nov 2024 17:57:46 +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 v4 5/5] selftests/bpf: Add tests for bpf_fget_task() kfunc Date: Tue, 19 Nov 2024 17:54:02 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0187.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a::31) To AM6PR03MB5080.eurprd03.prod.outlook.com (2603:10a6:20b:90::20) X-Microsoft-Original-Message-ID: <20241119175402.51778-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_|DB8PR03MB6170:EE_ X-MS-Office365-Filtering-Correlation-Id: f831a46b-b217-40ef-82ad-08dd08c3ac6d X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|461199028|19110799003|8060799006|15080799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: KQqQdJlZLCTAhtqPPqnesujcjbpl22oe2YsXDFDsT8mLT7m9FiA0bGRsbjNmFeMp1LFezSkhQ87Es9yo2G8EqDR5GEDQ9RfK11Ggjg8NL1g+xUnpEzJdmAkBaCo/HpZQeWthpf958NMazpatdrOVUU30uRmBsu5NEo4kPUEd0GDdE9Jf6z/qPpUTnvnudIXj3z04suNjEuFNMtYLi+4gj1x6ZZoE/79Ps3x7x63EONLboU9zc+qQjZ2TZ3swiwsjOhVY12p88mfAZe2t9LYlgSCxPtHl9nyiNqPBXOhDCbYgShDrY0PVQ+ay3q0ew2UBc02AjLdIJoEDpKg4YBO07k5+gzRI43uVEdEWINjndj+YWVwbENeLg4cbmYpbhciIKN5jgJhCExWZCl2Tr3WcdmyhLTfBLixES95c+f/Basjczo26tS80VRjMvYvQNm10aOzq5g43AhFyZYsmwfykuO4H/cwIDs7abInlu1uLlu04rQ109S5WF5RQiCWH+UBnQbHcTjJqDLcW/EILYsZTWxeJetWAxT4xFlXEN1v7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/++xAGwBI9Ii8C43Szr6QCgNuf/SknVD8I2qpD+Fp3rzIXwYBYhoCCpmfAT/FkCWxM00mkVH6cSDphFQ3FfOfLvovCzXc+IrXr1ZK0XyotHY05e5EleA62Bzn5qYAMlIvawEtyCJtLzD9XP+oJQRaMCeYiLml8BWpjq12R1+3xRTfYSr3lVrYNXZyRw0cGIAxoag72phr4nOodWUOateNi1gi/p0EtizkzFNCxkKwNqBi4/ncQfU8aia6UXt8JkYCnZ29sXZigoiB/IYVb2Y2+PgfxhN4xLYNz99zCgESDebyMZ/qLZ0Sa2XcYZQE5UKJWehwTjItkjPZnIa3altMvdKDFfw2FnOnGHg3j8zXnuUqoxFkHSx6otrjc7swr+ZWP4PFWVq9jBFyAtAk4deK0a62Zrc2AfnLuUXPntf7qSvCplNEonSO8dMN4cIvY29Z7vWlfJSRrtW4/FCV16Dc5eASwhAFPm64P5KexB4fIrtf+t/eN5iLjCLmApELALz6ptD1boKgnjS1XnRnvSqUEb02Mx/KFz55r7CG+l+yBfNnYw9dnjXLYnsSPJN+BloXwWJWTcJYSRZO61v2EGR31h4Sa6qg+0yR7jPdWoyjA8va4LU0MskmJ7oxq9yJd2hMUyaMKiSh8M4cSzoP8T10HjktUU1E+jy6d0AfnA3VEA+Ytkv9ihcnABUZt4rKw7P//sAZz6LmGcQQ6ruiKZR6l3oQ X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f831a46b-b217-40ef-82ad-08dd08c3ac6d X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5080.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2024 17:57:46.3432 (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: DB8PR03MB6170 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 | 49 +++++++++++++++++++ 4 files changed, 136 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..83dfe64aa3fb 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 = 0; + 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..ec40360ac1b2 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_fget_task.c @@ -0,0 +1,49 @@ +// 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; + +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; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, test_fd2); + if (file == NULL) { + err = 3; + return 0; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 9999); + if (file != NULL) { + err = 4; + bpf_put_file(file); + } + + return 0; +}