From patchwork Wed Nov 6 19:38:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13865495 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2107.outbound.protection.outlook.com [40.92.89.107]) (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 0C2DD209F5F; Wed, 6 Nov 2024 19:39:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.107 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730921974; cv=fail; b=ZVaKs7w+RHQ0AVhVWiy6qoSjmUPqRtpoz/ZAnvV/t47Ej/8QIa9G2pJ+AaT8ID46rMEXfTYscz3jr32Ex4d65OeInI5CTI7veM01b65kEgAQYhTvYgVps6mlDl3aFBnyRqLcGM5kQO+WabpbRhGERma4O2q551EMEK+4GrOCl84= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730921974; c=relaxed/simple; bh=imCKjTt+FjZs71vA+1DWP4iRwmafPtQ7z6q2v3riIQE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=HlBEds+6pQ3pPgvx3RSwbKaZpThEiyU79Jj2HMM9gYp4mOS23AzHh7nI2p+wyowej+A/oL8AIF1l9rvMr0iyjb8+nspyPki3AosdYkx9Bcm/JhycJ7sRapkvcFmhleqB6IYGJv9gQuA5PNjsXrfJoZB4Y0hSxOgOyz65LBCmC/0= 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=EzGwyc0N; arc=fail smtp.client-ip=40.92.89.107 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="EzGwyc0N" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Jk+38LfN0uHu6852d6eFrETgibUgKRii2N+ZqYrsNKY1DqVwiVyxqk2GKFTsCZE9lvV+PWrV+keWQ3eBY9LxCB5VeoytoKZatL5x3uOXCZKb4HT2Lqv3XRB27BFg5DFJjMyoZW+NR3X0ROoNZMJBmz/QiwpE/lkLiD2Waub9nc+TEer15ah0cTtQi1PRyzuED9po3oAat9MzyrFM9a/qRNlt51Moi29c3GrEF/0uOjV3/3Ip2c5FOfW4CIvKAtSG6NJw17IjTOqTMxLOO3zat9NMM1Ac5UK8S4vxIGLLeSnZ6ym3qSBhj3reCTCVCZ92UoXe2lRy2AdGPFNPYGw8Qw== 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=P96QpylSM/F4xNLnWHy7URkJkQ9HGs/KE5ZWx90fjjQ=; b=nFs7f7SNRKt4kFwyk6h7o6hfr/6l+o5GabOQQrhpoe2ZiX8vNbote4y6dGMG/JROi3T+btUsomojeQZdV/iBEPq9gc1WQNfnoTrpZ+2nMm+JZ7aXujrFivFyOrQ+PRoq9z1KfduDXw4+SFKdUeO6ExTIyt0vQaNijFmDBoYTj0HYPjMPF64o+el5fybQ1XINy1GYp1fkkWxO9YPmCPUJ//GH4JUpXZmpuRooB1hKEZdA5Q9XUDJhPuTY4RI7BoVJrvph8mSkHz7pwtqm/e47ANMZTTMCDXtYSVwantwEZpE7hi8Ykgn42luiPM1s1w+9yygGlBgfzPCLTY9K3gQNfA== 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=P96QpylSM/F4xNLnWHy7URkJkQ9HGs/KE5ZWx90fjjQ=; b=EzGwyc0NYPIq7MvfhOHwN8Ht8/jrNyRl+u4K8Qy6AabSHTVxXTAand9efKg5oSqCXsG3cQ+tq7mNKxYfOr0CyNh6RY9SdD+z8ql/4GaOrrHLl11GBVI/zO9ApUSFaIkurZp53x+AIUiIUzy0jwqQiHn+qapK4U61wQX28y4u/OwWZb3eFHUFdB87PVng5wucPC0dShOutVkJG/IlEuu0iArKDplGfpZJSnkSR8T8eeqZD3KKoOmRNiNz6U+4lYNGPWSGAaHm+ZZ3pd9F08GvEz4MN0ejXT8qgRl2am5iUy+DN77C5ZEzRlUlQFAzebU/YaHu3wBL2MZW5OUmeHaXDw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS2PR03MB9792.eurprd03.prod.outlook.com (2603:10a6:20b:608::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8137.18; Wed, 6 Nov 2024 19:39:28 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Wed, 6 Nov 2024 19:39:28 +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 v3 1/4] bpf/crib: Introduce task_file open-coded iterator kfuncs Date: Wed, 6 Nov 2024 19:38:45 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0280.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:37a::17) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241106193848.160447-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: AM6PR03MB5848:EE_|AS2PR03MB9792:EE_ X-MS-Office365-Filtering-Correlation-Id: cb6fa12a-530c-48c8-2e90-08dcfe9aba1a X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|15080799006|19110799003|5072599009|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: es8Kqm/eKjHZ6UQAoQoE9FZcNxAbXaRL6V+U2sZ1FEVRnhWhxiL7Bx0sS3xoauVL3ZNAo/dgwgCTFi9xdR56iStz9DRjaMAYQiJKxfIy2nHYLBbkJi2YimSmX/N/90WemFQu9Wp+2Ouqdu0LNJ2ZIZthQKP/zGRwPfcYOuuFZx4BYgZpTPD9mh6jhWXATE/MBjm0J2h6YUTKvru7mcZ2jr4EfhZJOs8P5LgDAwzKtVOl/vr+7q5X0UUES6rk4ogarw3wEkRa61YbbTbyXpUQTrboWCFIDr2mfEj5p4NuVmzfPMTw2pVEvcTPI+N3EjB0A6Zt1UP91jZfyGkROZem68+lZ13qW3hByIKLDG4WqzDt6W0dCiFB4qxZJW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knba0MgSHZvkPXm81+m7ZcVFUZ7rwlTVqgAP1eFCwHaHMTNpGC/MVHxicu3BMkOUQ4SJGdK+GTVOiGKTYAvTZo6IsfXFUC2pInNCVK7uvI9J8pcHKdjrdMqJ1Nw+7OilO1inBjldKi2zc6DhLQMxamhRAezUWoUhtP6FjpAnZuzmq4zGzWol9CQ5Yo30d6AK27RPo5CuVPjkKgh3Mrx/I2rqoiEHaOvsAlmFzMl354i2KmobUdfOeVAq8a3tNnyidu/dRKOoCgrjmJVsrKH055sQaAfRxu3HzE/irzrbCq25VUsElIK1phWq7TUzCbGQx36YLd6WhhgxbcTiqYFKxXbmd10shJR7GyGF+GT/JK1Sim8eW8FN5C+LBVcnf/mupnvrKjaZ0vzfnWwksvoduC1MCFnKewEZ/83Q+YtrAejyd5t2VbPQlrF4aQUTLzsyx4wiJzWQAWQNWahCaW6Wr3Pcy0mQesA5rwhCOriwB3ezN+DUZLBcwbnS02bUkNELAQBhiWErp0aofqIsTJh8d8eo6GF/hI8OXpiVZk/5WAQ0NY/FtsETQ6rflKJW4T7sA24z/jU8ljPY64NhoxZtte1d2mm2P8O7eTzJHfME0nsQP36DsPa0f3vm3bW4sZapebPdszwdfhL8c7lDI3bSmFFQHCwFf/toNZ5J+WijwBr7f56Wcx/KZh1N4lF3Y+PEpbd4MgBPoX8pO0QiFIyrGvxfL6oBHQ9xcHckBRi4+wbkwbxVHEzuDb7Yre34MQL4UblWvKvrgV6ZNl8iIinCZgDhbUW8EDSt7WkToRx7uJ5i1nUGcuDLUOOcQEynDf1eC6cimYkunGi5WuhPtS9wqL4ui42lbixM3nuRnFHrBg4YbbfnyWW/8eDWPD3g5AcCvh+ad1J5ps+CF42NNs9vK9WqYs9o X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: cb6fa12a-530c-48c8-2e90-08dcfe9aba1a X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Nov 2024 19:39:28.2714 (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: AS2PR03MB9792 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. In addition, this patch adds bpf_iter_task_file_get_fd() getter to get the file descriptor corresponding to the file in the current iteration. 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 | 4 ++ kernel/bpf/task_iter.c | 96 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 395221e53832..1f0f7ca1c47a 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3096,6 +3096,10 @@ BTF_ID_FLAGS(func, bpf_iter_css_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_iter_task_new, KF_ITER_NEW | KF_TRUSTED_ARGS | KF_RCU_PROTECTED) BTF_ID_FLAGS(func, bpf_iter_task_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_task_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_iter_task_file_new, KF_ITER_NEW | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_task_file_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_task_file_get_fd) +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..32e15403a5a6 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -1031,6 +1031,102 @@ __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it) { } +struct bpf_iter_task_file { + __u64 __opaque[3]; +} __aligned(8); + +struct bpf_iter_task_file_kern { + struct task_struct *task; + struct file *file; + int fd; +} __aligned(8); + +/** + * bpf_iter_task_file_new() - Initialize a new task file iterator for a task, + * used to iterate over all files opened by a specified task + * + * @it: the new bpf_iter_task_file to be created + * @task: a pointer pointing to a task to be iterated over + */ +__bpf_kfunc int bpf_iter_task_file_new(struct bpf_iter_task_file *it, + struct task_struct *task) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + 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)); + + kit->task = task; + kit->fd = -1; + kit->file = NULL; + + 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 returned 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 the struct file of the next file if further files + * are available, otherwise returns NULL + */ +__bpf_kfunc struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + if (kit->file) + fput(kit->file); + + kit->fd++; + + rcu_read_lock(); + kit->file = task_lookup_next_fdget_rcu(kit->task, &kit->fd); + rcu_read_unlock(); + + return kit->file; +} + +/** + * bpf_iter_task_file_get_fd() - Get the file descriptor corresponding to + * the file in the current iteration + * + * @it: the bpf_iter_task_file to be checked + * + * @returns the file descriptor. If -1 is returned, it means the iteration + * has not started yet. + */ +__bpf_kfunc int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it__iter) +{ + struct bpf_iter_task_file_kern *kit = (void *)it__iter; + + return kit->fd; +} + +/** + * 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; + + if (kit->file) + fput(kit->file); +} + __bpf_kfunc_end_defs(); DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work); From patchwork Wed Nov 6 19:38:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13865496 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2055.outbound.protection.outlook.com [40.92.90.55]) (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 D690420E33B; Wed, 6 Nov 2024 19:40:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.55 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730922022; cv=fail; b=rduZXxZHcxH4LL9O4pSfqPDqyNTvgT1V1+9OZku4giXuQeOVuQjggoNqX3IM4WT+900Ue00ItmQFqCIjsm542G75ZU+htn87yh9nrDftaZKm0nJ3dUuHZpf1WJgrtJiIGs35SKDl6VnJuBnkw3g9549U9GjfYLJdfOo+X4Ygq2g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730922022; c=relaxed/simple; bh=zdCm6U2s70xPqn85yHFyAGOgLwiup2BcmBZ19RSbvj8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=nC6jt7rC2yLx74Yg6sNmRIQn270wzjJrb2icNnnxNPUNewdd24SW7tB/g6s3htuxzYb6iFfRc7/Q07pFBZkGJNT+AsCgc0RoRam8oz2zgMtbnv7LwemCnYjw+ZWFt2K8QsDykq/Y4zasu86aIzaJplDKsHgR9XSEpcpaTxt4F20= 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=RiTnEFAE; arc=fail smtp.client-ip=40.92.90.55 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="RiTnEFAE" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lFSSXjFprfxRkkIDNaCF2+Kk87imxv6a0jcKvDbj8IPcZ7mzyPKNNqPVuWYMtvxVdWdt1ULeajZ+5HrddoDJCZj0XzkJS7usOIaMpZt9doemHqx+SqcvdhO9MQmGUlX92pfMAmn4JVP+z8H7Vj2viz2PMpuSRfM820MLmnbEJkkgKYGk+g6kibMJe1S4JwxThouustZUGdb1UGH06WEW6PB9CXWrLdSGU6ZptnjFAt4OnlsKh1UQkvsQgM5b211w06XTNPp6Ns7G4v8D+OwSTKUsm3iDWy5MtMqv2LsqRfxcxheRz9VgIC3mCG0Xxy3q0BrIC8/Nzk9X4MQDf1e9PQ== 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=wPdVXSrZgZT19PDdftNK60UiDKPEwrcnyZ1Np9R5D/g=; b=Ao4E2CpOMQZ9xvoHEWlrHjftwVXUKMxL5JkuIOKpl0Jtn6MFS0qKawZMXvD4G6zxBQIqJuukDjvFzIEbv8dnfqp0tE+F/CCAV68joDIMPv7gk+YYqGuj4jgonWOdWpbkdCHZBtC2LSf2sWu1PVSgzFCam3WdDllMQNiNUHzQN9Z3oCLsttBmXfJTo9Z3EjRAsWMkdagdcELmVQRD9K1KSUrxy2IQSUm0qCElJLnAS8MnYd3Gjeka+1Dyt4O5JSHzy3DFb634OBFR2bRTA74xARW/umi+FDl5fUpI2J3ihVNnI8K2GNAVEH4jhDly5ROLkaN3jfQ8q4zWwM5bG47lvg== 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=wPdVXSrZgZT19PDdftNK60UiDKPEwrcnyZ1Np9R5D/g=; b=RiTnEFAEUJ3mhodQvLfEChD4OrRt1AjdH9kBQlswZRZARU3zaD4cIcZ8pIPK8pjmqQNv4j5lpGXm8MKn0nn6wtnaRKFuV23h/QRuEptzvRhgLtM3mcFiX4wS+l1j3gD+KTdG5sEQJ8u/jtAyRlZRjHxjx8zfb7nxYG2NOcU7tpWo/zmwiST5zfTOjvAMBC7K35Q3VovPsxdwOn4TUJpdzOPm3J+mAh8E7/zKHz28rLNPUmLNp97PT0lzrZNRMLa5DGPX+acZSAc0y34SYCn59mTH4y8MXpnIA7D7x1etptvuKPwtPTPyC7P02qrbQyfLSMpaeqDqAsDqOXtoEJ4Shw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS2PR03MB9792.eurprd03.prod.outlook.com (2603:10a6:20b:608::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8137.18; Wed, 6 Nov 2024 19:40:15 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Wed, 6 Nov 2024 19:40:15 +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 v3 2/4] selftests/bpf: Add tests for open-coded style process file iterator Date: Wed, 6 Nov 2024 19:38:46 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0280.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:37a::17) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241106193848.160447-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: AM6PR03MB5848:EE_|AS2PR03MB9792:EE_ X-MS-Office365-Filtering-Correlation-Id: 2612158b-bb2d-4db5-4515-08dcfe9ad5f5 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|15080799006|19110799003|5072599009|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: CovV1GQYQUUoeAdmpxPVilwm9TJ1EoU1FCdigpYKxzxy7FXaRYShBatwzNl5j8pO2BR1Kowowcgn9YkUuL03j6cfHM6vNbu5hZS/n9HnMVWRm9wHh27AWVwdNOYSPG7Cch1aD2kjR+6nlJNv2pXM/zvmQDjyZt6Uq/zSqJ20OVH4g2gr9VDCQIMF9ORa7ALrcIhe0jr1kJCijjP8aPTAEAuXt63ATNcYY5roTJzJwoBzwXFbl1qgR+EpZRIv+Db0Ln1eIA5KESyPT7+ip/7e8nrYLWwb+RtZcAwG3/8yQr42AkEvJQo8Ji8oLu+R50yhLdY7IU1lAw7qm7eYdC7GHUkAMl4ZXHchpIdLfkj9zxBxmVu+jocb1gCCpyWVfvxA59c5NJUjDl6/34NOOhLghVHxXaEMFxxNp617thq+XvxbwbwJSHYTub5a3gUc8Jfwjrvr8vNa7IfOmWIpX2b7m6TPh67kB7eoK5d/7O4epYWdM3k/Q53SkMS6ComeS56Uj4wzS7LxC9YTZluKTTGOgwCMxTFDjnQGLafc1lHnSdoxoIqgeGaYZ1oZ+s5/1a+PbUY9/vs5G8IESElTF2dax1g7qVLMXOxACeXWRz6moKvzG9ZX7jcVPhy8O4PGpNK7CI0ic73hV18OXQ83DGZ5h1a8Ag7N6rKPKDYoytLfRPco0HIbnHqMn5n4+mgO+5iRuDiRMngSN6r7sZW7bzxCRG6OS7TJLpFeR1kXxvBYVqn/uOYpY0kklrg+VSudAr3gvgA3/219TacJhAfrWkavsDdcNvMhrJdvh3cRgs+XaAxhd3nHCIFemCs5WGRoA+NGNigkaq+G3kWOwC4TTLxTr0MFLYW/+rcdCwzZFOrFWRlowyt+PREMVFNfRra340VMKx2aVJzR+mHYDv9tK/ks9cCDsoV7METuuJTEx1jyxCxbnt0tY/FTSQpjZzqArfoQglF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kPMjVg4rp6qpH73ek9I28j0/I2hQpIYk90U3QnOXK7rJovukifZ8gZYcPHExEf35JE0guyzTM/GNhtHvjC33dlDPY5PVVnF3t4LYRmmPUCMZ3lpkmENBkMeUZT0Oz9uxZVTs1Ie1Ca7Hg6GtCzK/7ElyNJ821BMXRc0z1mm0QuJGssDLTO77VsrumoGB51Yn7d+1aTVFcFwY2TTfcdHZy48BDICoxJ9GAkci79skAFTdeU6TkJTNyuAdpsIJM9yl5h1AhxvPfMZ2RT+BGY3uU0uBwlqIGwTUodgQZGFCTWp5EDbvrlEpPthQybV6ZeOBWWvT0Kk2Chjltw8jD4O X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2612158b-bb2d-4db5-4515-08dcfe9ad5f5 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Nov 2024 19:40:14.9957 (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: AS2PR03MB9792 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for open-coded style process file iterator. Test cases related to process files are run in the newly created child process. Close all opened files inherited from the parent process in the child process to avoid the files opened by the parent process affecting the test results. In addition, this patch adds failure test cases where bpf programs cannot pass the verifier due to uninitialized or untrusted arguments, etc. Signed-off-by: Juntong Deng --- tools/testing/selftests/bpf/prog_tests/crib.c | 125 ++++++++++++++++++ .../testing/selftests/bpf/progs/crib_common.h | 21 +++ .../selftests/bpf/progs/crib_files_failure.c | 86 ++++++++++++ .../selftests/bpf/progs/crib_files_success.c | 73 ++++++++++ 4 files changed, 305 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/crib.c create mode 100644 tools/testing/selftests/bpf/progs/crib_common.h create mode 100644 tools/testing/selftests/bpf/progs/crib_files_failure.c create mode 100644 tools/testing/selftests/bpf/progs/crib_files_success.c diff --git a/tools/testing/selftests/bpf/prog_tests/crib.c b/tools/testing/selftests/bpf/prog_tests/crib.c new file mode 100644 index 000000000000..48c5156504ad --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/crib.c @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include "crib_files_failure.skel.h" +#include "crib_files_success.skel.h" + +struct files_test_args { + bool *setup_end; + bool *cr_end; +}; + +static int files_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->cr_end) + ; + + close(sockfd); +cleanup_pipe: + close(pipefd[0]); + close(pipefd[1]); + return err; +} + +static void run_files_success_test(const char *prog_name) +{ + int prog_fd, child_pid, wstatus, err = 0; + const int stack_size = 1024 * 1024; + struct crib_files_success *skel; + struct files_test_args args; + struct bpf_program *prog; + bool setup_end, cr_end; + char *stack; + + skel = crib_files_success__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, prog_name); + 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")) + return; + + setup_end = false; + cr_end = false; + + args.setup_end = &setup_end; + args.cr_end = &cr_end; + + /* Note that there is no CLONE_FILES */ + child_pid = clone(files_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; + + cr_end = true; + + if (!ASSERT_GT(waitpid(child_pid, &wstatus, 0), -1, "waitpid")) + goto cleanup_stack; + + if (!ASSERT_OK(WEXITSTATUS(wstatus), "run_files_test_err")) + goto cleanup_stack; + + ASSERT_OK(skel->bss->err, "run_files_test_failure"); +cleanup_stack: + free(stack); +cleanup_skel: + crib_files_success__destroy(skel); +} + +static const char * const files_success_tests[] = { + "test_bpf_iter_task_file", +}; + +void test_crib(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(files_success_tests); i++) { + if (!test__start_subtest(files_success_tests[i])) + continue; + + run_files_success_test(files_success_tests[i]); + } + + RUN_TESTS(crib_files_failure); +} diff --git a/tools/testing/selftests/bpf/progs/crib_common.h b/tools/testing/selftests/bpf/progs/crib_common.h new file mode 100644 index 000000000000..93b8f9b1bdf8 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/crib_common.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __CRIB_COMMON_H +#define __CRIB_COMMON_H + +#include +#include +#include +#include + +extern struct task_struct *bpf_task_from_vpid(s32 vpid) __ksym; +extern void bpf_task_release(struct task_struct *p) __ksym; + +struct bpf_iter_task_file; +extern int bpf_iter_task_file_new(struct bpf_iter_task_file *it, + struct task_struct *task) __ksym; +extern struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) __ksym; +extern int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it__iter) __ksym; +extern void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) __ksym; + +#endif /* __CRIB_COMMON_H */ diff --git a/tools/testing/selftests/bpf/progs/crib_files_failure.c b/tools/testing/selftests/bpf/progs/crib_files_failure.c new file mode 100644 index 000000000000..ebae01d87ff9 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/crib_files_failure.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "crib_common.h" + +char _license[] SEC("license") = "GPL"; + +SEC("syscall") +__failure __msg("expected uninitialized iter_task_file as arg #1") +int bpf_iter_task_file_new_inited_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task; + + task = bpf_get_current_task_btf(); + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + return 0; +} + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg1") +int bpf_iter_task_file_new_untrusted_task(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task = NULL; + + bpf_iter_task_file_new(&task_file_it, task); + + bpf_iter_task_file_destroy(&task_file_it); + return 0; +} + +SEC("syscall") +__failure __msg("Unreleased reference") +int bpf_iter_task_file_no_destory(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + struct task_struct *task; + + task = bpf_get_current_task_btf(); + + bpf_iter_task_file_new(&task_file_it, task); + + return 0; +} + +SEC("syscall") +__failure __msg("expected an initialized iter_task_file as arg #1") +int bpf_iter_task_file_next_uninit_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + + bpf_iter_task_file_next(&task_file_it); + + return 0; +} + +SEC("syscall") +__failure __msg("expected an initialized iter_task_file as arg #1") +int bpf_iter_task_file_get_fd_uninit_iter(void *ctx) +{ + struct bpf_iter_task_file task_file_it; + + bpf_iter_task_file_get_fd(&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; +} diff --git a/tools/testing/selftests/bpf/progs/crib_files_success.c b/tools/testing/selftests/bpf/progs/crib_files_success.c new file mode 100644 index 000000000000..8de43dedbb02 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/crib_files_success.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "crib_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 task_struct *task; + struct file *file; + int fd; + + task = bpf_task_from_vpid(pid); + if (task == NULL) { + err = 1; + return 0; + } + + bpf_iter_task_file_new(&task_file_it, task); + + file = bpf_iter_task_file_next(&task_file_it); + if (file == NULL) { + err = 2; + goto cleanup; + } + + fd = bpf_iter_task_file_get_fd(&task_file_it); + if (fd != 0) { + err = 3; + goto cleanup; + } + + file = bpf_iter_task_file_next(&task_file_it); + if (file == NULL) { + err = 4; + goto cleanup; + } + + fd = bpf_iter_task_file_get_fd(&task_file_it); + if (fd != 1) { + err = 5; + goto cleanup; + } + + file = bpf_iter_task_file_next(&task_file_it); + if (file == NULL) { + err = 6; + goto cleanup; + } + + fd = bpf_iter_task_file_get_fd(&task_file_it); + if (fd != 2) { + err = 7; + goto cleanup; + } + + file = bpf_iter_task_file_next(&task_file_it); + if (file != NULL) + err = 8; + +cleanup: + bpf_iter_task_file_destroy(&task_file_it); + bpf_task_release(task); + return 0; +} From patchwork Wed Nov 6 19:38:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13865497 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2043.outbound.protection.outlook.com [40.92.90.43]) (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 CB0E320FA96; Wed, 6 Nov 2024 19:41:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.43 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730922066; cv=fail; b=b4f8hORG0CchMjEKYh4TeKzhn7zFw2DrpvzhHigKke/7DKlW22f6roXw8BE0CxHBMitwsyWoAhp8IjF+WJGIICgzTef5HBtHYNv8b9L7BDNmG+B3gzqKYfnx24LmzKZ1c+fhyT78JOSZCWgPR+7b9rbjKkLfgw9E5GOm3MzYFZc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730922066; c=relaxed/simple; bh=ChFHYJSmtb1Af0VsHQB0kNLIRsq6RI/GToZk/efCpTQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=YxdH7bA11iNrBAIvyyTERqtZcYMjRCoEuC+4+YVCfd7mKVf9zySa5XWQbaU0EOFqwDLYG3uYmNAvyo3yg/Yi/y6lBHTUXhSOinNNDvHdEsBMs7gt4VQXbu4ep3yJAsF0FDRfDS5dSNx6zFyyk+fwPN5ammnAAIrCdTOVBer4cDg= 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=JtdMU+H5; arc=fail smtp.client-ip=40.92.90.43 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="JtdMU+H5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Dgzacpo8QSZIaoU6bb7kxnl3bkYSA9FHuP2wYLC8vMKYK7fs6XXz8wvqGE0hmzFmS/X11ZCYdiQmASSzacMm3M6cVWuI/2vzW4M9B9zg77ohFtYf4fdpd4P/57g6V6wrb8ZZ/753Ser8nPzThGE8oVLgAEr+U7r1mQNiSQjvkPcaX37n2JvqAwFGebYpEfoF6YLlRam70qHRjet6Gxvni4U6X3QdIItD5QuvN4hYS/YrvdWXgrDXaZ/Os9xnTBFf4a77JXHLS1aZjxbD9dk9Hu5ufrsbmsd2spoWj6X/o1mcLo7bCSXamgyfYPkqiBCFFlmzlicXkGWdrYrVEznrig== 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=L+EPXcAd3NsboONqndjrNljlrQs3hawE6sVQwYnwiMY=; b=W4JWSLuSd6c23dMWl62ExUEnHmWpdSiHf72tn9A/jINUiqAuUwHk0U04jeZr3OtcFAfHVC98+PSZxKY2f46hbsIPCGuIKucmHl97h9kCIs+0I9hnscq3aTikpCjxgbwMp/e1SoMyTa1TBMa3RHXMT0lcm1EJcfsD3GRzYZM8+J6ex6QIYnot9XhaOK/f62pE6616SmV198Au2rOfFWOeNv9XgS/0xeofMMmrtl2A1Ew63ySI9M7vAbhHZXcuM5P8bdZ5RY3XAuBVQGnkLy5xTKEHms23YOzvdIfRZggt1wVxNzJXx+8bsLLVOTQQeOBcyYCmnNqUvcZHqjYwux3R/Q== 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=L+EPXcAd3NsboONqndjrNljlrQs3hawE6sVQwYnwiMY=; b=JtdMU+H5w/v6mmSGmFPhsVe/8g9JBmMbCgjCR8L8+5ewJpi+/mniBYPfb2g6KuYGiSaWJ8/sv3thsm4xqjyFzi4FeWpE3AtwxOax3yxbW2DZOuhzajkZbehlNQC3VHxzvvKzfFYqRF8heMVbBVhqIGbx1neiZHIxYSu8jq/jTpIDj46DOoctbT0BrbttA5IrVVjXkIsjJ9Kgb1lS9zhllo2a4NJiHI6HjmEi8LRvvAL/RFX4FfsCIT8wgmf/MN9VhLeBfkzsftejPRqzBCeMgHq6ClMOjtQwehfP1hNgWl+l7CTJWFN8Zy7B+1fOz9qSL8RIjhDiAJhsmAKjZHAHcg== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS2PR03MB9792.eurprd03.prod.outlook.com (2603:10a6:20b:608::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8137.18; Wed, 6 Nov 2024 19:41:02 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Wed, 6 Nov 2024 19:41:02 +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 v3 3/4] bpf/crib: Add struct file related CRIB kfuncs Date: Wed, 6 Nov 2024 19:38:47 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0280.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:37a::17) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241106193848.160447-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: AM6PR03MB5848:EE_|AS2PR03MB9792:EE_ X-MS-Office365-Filtering-Correlation-Id: 6293b83d-ba6d-47a2-76bc-08dcfe9af224 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|15080799006|19110799003|5072599009|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: GDjU5pyLYQb1BkKjo6wTCz1NMIo+PW6GoUEvJS/8cROQNO1rFsf1Mv7zy+bN8Ym+7SOQnCo7hKLXnstBj1oiv7mmoW0Za9V7x7K+J3totrXiCiTnI1NZhYaCvFd4C2w5NtwtswmLO4zZCHNtINoj97rvWxwGBBlHvqxcnoRgop5yYF5Nq85hIXd4H7PmJsPPkguv29FV00ejkxBuWZSQZR4oAhNXwhozHbSbNs6cpLAysi9jttPrBqJwI1M2XgRuNX4oVg+2yW60HMsiEAqaPwME3EucTN+oz9yGy4N4Nr6MbpKUOGgfttK5wxb5n3DeuBMR+NrXIh+q98QeKAAGQ4rCRGtjtiRqDAbv14NphTGy6Gm9+pIXDbZoy7zj9PPcRcV9qJmyLV65Jj0SAwlM/M5mA6QS9Fzg2Ke9egcz/DckitJtD5LSy6apUUtWM5gZSzzoGLvPJvdyqW2O63SWQLy40HGz9rnLRtwaKWT/CoCeUT8V+wrDS1MWTYhHyCLg9MAIDYfdn2m2a+hvNU3m7GQqYnlW1dfJ8jW6olTKXX/aELuGXvzZvMcufsNQg1IQL+d6OoV/oD1YmEZZga+jR2a8ooRdp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U6pFxuSaUkHN0H1qpsIZT+0aWUS5rRJcr6kG/XUwc6rXOAMa4hDn+8xxgkVZmekFozaAJzfau27xRB/aNm9Xd/cSTciTBj8ogDmRDNEHGJUn9uMOWIVMR0Vloh4saxWc5iWJQG/GIuohocMH/Aggx4aUhsb8/2e5OqNZdu3mWpNlIHjzmABIOdVVYKoLCr7Vk4twC79PNdEdmTdCrA+XMPxYHPvTs7jF26WXK5/EWGZxWm0DcVwwgQX0mmRq5mDF6g2Fg92/Nib/p9u4u+xCd5YOD3hulQ427lKfCTNrtv1jmfZzh0tMX1WR5zgf8hIrmd6pBqLYzRKWfiUtfAIBstFeoGv6R1/I39AYdLKEfSLo7ghb8w692js1+1tnaPsRdOeGBzuauq5bIPaFEQjGzhevuqYypk/mMPkDzLbkSxleBLFU45OZn8VNcS38SU09mJ6CTjZEu7zutLxBPY9MZia3fk/guUSTFdyfX+/I+S7lDE9oRYn7qsCabTHZT0SfDYsgXpe/Ufs5kUMM5IEBaipEgdhqOGzlCrXry190rTGbZu6+qjxV2zdLgBFDZUFyAC9BhSWvWxnU0ZlxoAvIV53wIZViVl8C5gN7kubV6aadn51fSOEkfpzpIYcaiSp6nicISW0u8P0NNNDdG21apJThJTeLwPzUz8HUyP6idYU7lmFtL/hZHS4YM19m X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6293b83d-ba6d-47a2-76bc-08dcfe9af224 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Nov 2024 19:41:02.2937 (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: AS2PR03MB9792 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds struct file related CRIB kfuncs. 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. bpf_get_file_ops_type() is used to determine what exactly this file is based on the file operations, such as socket, eventfd, timerfd, pipe, etc, in order to perform different checkpoint/restore processing for different file types. This function currently has only one return value, FILE_OPS_UNKNOWN, but will increase with the file types that CRIB supports for checkpoint/restore. Since the struct file_operations of most file types are currently static (e.g., socket_file_ops) and cannot be directly accessed in crib/files.c, the future plan is to add functions like is_socket_file_fops() to the corresponding files (e.g., net/socket.c). Signed-off-by: Juntong Deng --- kernel/bpf/Makefile | 1 + kernel/bpf/crib/Makefile | 3 +++ kernel/bpf/crib/crib.c | 28 +++++++++++++++++++++ kernel/bpf/crib/files.c | 54 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 kernel/bpf/crib/Makefile create mode 100644 kernel/bpf/crib/crib.c create mode 100644 kernel/bpf/crib/files.c diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 105328f0b9c0..933d36264e5e 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -53,3 +53,4 @@ obj-$(CONFIG_BPF_SYSCALL) += relo_core.o obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o +obj-$(CONFIG_BPF_SYSCALL) += crib/ diff --git a/kernel/bpf/crib/Makefile b/kernel/bpf/crib/Makefile new file mode 100644 index 000000000000..4e1bae1972dd --- /dev/null +++ b/kernel/bpf/crib/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_BPF_SYSCALL) += crib.o files.o diff --git a/kernel/bpf/crib/crib.c b/kernel/bpf/crib/crib.c new file mode 100644 index 000000000000..e00f4a4f0d42 --- /dev/null +++ b/kernel/bpf/crib/crib.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Checkpoint/Restore In eBPF (CRIB) + */ + +#include +#include +#include + +BTF_KFUNCS_START(bpf_crib_kfuncs) + +BTF_ID_FLAGS(func, bpf_fget_task, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_get_file_ops_type, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE) + +BTF_KFUNCS_END(bpf_crib_kfuncs) + +static const struct btf_kfunc_id_set bpf_crib_kfunc_set = { + .owner = THIS_MODULE, + .set = &bpf_crib_kfuncs, +}; + +static int __init bpf_crib_init(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_crib_kfunc_set); +} + +late_initcall(bpf_crib_init); diff --git a/kernel/bpf/crib/files.c b/kernel/bpf/crib/files.c new file mode 100644 index 000000000000..61201923a35f --- /dev/null +++ b/kernel/bpf/crib/files.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include + +/** + * This enum will grow with the file types that CRIB supports for + * checkpoint/restore. + */ +enum { + FILE_OPS_UNKNOWN = 0 +}; + +__bpf_kfunc_start_defs(); + +/** + * 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_get_file_ops_type() - Determine what exactly this file is based on + * the file operations, such as socket, eventfd, timerfd, pipe, etc + * + * This function will grow with the file types that CRIB supports for + * checkpoint/restore. + * + * @file: a pointer to the struct file + * + * @returns the file operations type + */ +__bpf_kfunc unsigned int bpf_get_file_ops_type(struct file *file) +{ + return FILE_OPS_UNKNOWN; +} + +__bpf_kfunc_end_defs(); From patchwork Wed Nov 6 19:38:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13865498 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2086.outbound.protection.outlook.com [40.92.89.86]) (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 ACB6220A5E0; Wed, 6 Nov 2024 19:41:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.89.86 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730922111; cv=fail; b=d/uH/fXikeg2vRXcmQg1A+OJxiTTDfa/J9LflfmIRMvNk+wvk7aBkDEd4s1FjxHrmV8IVHjTbaTsM+tcJBp7YFma1ZxjyxoAwlcoGg6perBQYVNIwHQfE3MNcDBV2yfgK4KfIE0XrLnb2FlzX6OoE9JnSyMH9vHmOsxiV6l6U7s= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730922111; c=relaxed/simple; bh=KXnSoYq23pKPhySia7NA6P3OqJA/RlteqQmtQgIkxMo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=fUUWWG69BnpJ0xwhyC5M9amrmPy63IFpJSf0KWu/djvvlNsGiLkJXMxJia+rK0GSLWZ5f8pgcNr/Fis0IbfJeEQwuIEU4FtBkY3sc+doWxXOZDaRhNJaFqE6S35bH24MD2ktP6qIWbm+cSArbQabHQAW89ZF97a+bSzyZ/8wixs= 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=howoHFO0; arc=fail smtp.client-ip=40.92.89.86 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="howoHFO0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=B1Pp2mNKOEH6kjHTTEe5KacS9HaE39lYl74xZ63C2w9vnidyq6L73FRmrovfttgE7LcJv2/sWI8HHkhrx/ZN+pQzPrrpSDNLrl/NGOmpWBtwoe+bC9ynivXcKrQtN9el4lN8YlkP8ROOG0YX1n8Zfwv1LIPO8p5y7WgoUU2sIZPw5/6pEaurb7R7abEoCjsv/u8piZk4qtPNjhcva+HZ+0bbgmb2ouZlNJ/cSPHPoBTpJ3LhvxJlnf2L2BJASK56kiXr2g9Tn8tygHHZYR4sr89JUr8ZdBhP0YxDU+SOpUEDJOSpkRS8CGYNafW7vik7VNBuS3Q1hBRs08iSigtGuQ== 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=jv1sJZwx9eKR8V9NrmGKUvTF3Jqh7OwSXf9NEFqVf5k=; b=ejhzey36M4tCTdJgZ7yybr5H9lFqLQuF4/i0X85aSRv7ZF9hnVDMpoZ2e5I+tWvmC4sEmJrld7Py/YW6fXbeii2UN3NvASVV3v7Wsw5kOum8sf9GC9IeSuhCmnTkie3xL02nj6CLgXY6x3+oAvyCzwzmTKtoPxTrTf67IKC/doR1P20daI1dQ4gfAgE2ZXXNuE7iP9zG8yMRO/zSvHcHyjxAnon2ci865nrt/v0pYe1mkkiP2JsW+95QfHg2XWeiUEvJ5Qry21jyjBOG9Zir110tPelO/+0Y4nrmrhy7rlWH0okNqL/CXWFCPVK8+aAZRn9KXgP59Yk6xGeDP5Q0DA== 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=jv1sJZwx9eKR8V9NrmGKUvTF3Jqh7OwSXf9NEFqVf5k=; b=howoHFO0QeWQ7vFJ/diSapXIzjwwCyaDPfgmy/Pm7g1WM5L6bP6c7eIiR9rBgWIxRw+0QBd0+aKgGiYdkr1aAmTCvNzO0ugxDF4uAjqh4StcibZhGG5UjStcVXs3XpaKbTYaAH5Ks7IADParajdf9iRmixfyBMUMzZWk/EOlkTZAVNbh+wbh/8x1AXx+wMlbNy8AQ7HFxsuKNhZrX106QB1BE5cBRqEKRI9/qGuNm7ym/XV2ZZV9YGi+WlHx5uoihIUE1qcP3eyeidviqBup1s6uZo3BH5zdqquTkblK9MmKpm29BdqEOScXlbhFAVDatyYc9u52LCi+8vUV6k/N/Q== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS2PR03MB9792.eurprd03.prod.outlook.com (2603:10a6:20b:608::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8137.18; Wed, 6 Nov 2024 19:41:47 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%4]) with mapi id 15.20.8093.027; Wed, 6 Nov 2024 19:41:47 +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 v3 4/4] selftests/bpf: Add tests for struct file related CRIB kfuncs Date: Wed, 6 Nov 2024 19:38:48 +0000 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0280.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:37a::17) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241106193848.160447-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: AM6PR03MB5848:EE_|AS2PR03MB9792:EE_ X-MS-Office365-Filtering-Correlation-Id: 436bb76b-76aa-4469-6468-08dcfe9b0d05 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|15080799006|19110799003|5072599009|8060799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: 7KYmVp8YR3KN6qJnOmGZdSYoWW86NBvzaTR+n5sTB86mDjEfPhT8Y/RkOo8O0v3ZJ4ryKrAUfNUbnIzIQXBIu4JXAZq5FOwqMm2YFwblFiFMG0t2S6gTkG8+FJn/YGe1IdOgugr1Jwpx5YoIyMl00FUNL7D7YX4SLFE+gk9dBSvOKzPLdjBmpgYBXvylRUnkZwdPTYdtfN6Bi2LTY1l7APa385Dx/UEEZvnseskMadsmCsBS6aCMbhc4o6/z+sWFijGvKxyQSDY2KTmgHISSuZp31SQcLcvZTWW+FZufeGR0xKKmZa8z/gE44T/DEkxJSP0egEAdgM/BGDHmEybMaWgOJ2K3X0eevxOD0CGHAsns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DElIqEHvw8DXdKN467nhpzybR/fbM/New7EtFuW+MZntdqgC+q39/rbf2jRj4yt/1tkXWxM8RS+urJ7TAppGkKBAdiatruQ3F9+okyo15Cu1EM1lFknJNpX8anQ/zzDg44BAtQRdSrEca4QUnF9LwMZwjjUZa/gz+qnG+9/iPzrf1DbyzAcahcjqg5fVHQPsQGoEcjcuK0iFkeruufGuj4etUMjXpvYQZ7CkYP6yNNdY6F9526A+3+CVqG6n+q5m/H6KBIFl+l7X51Fjq8gtSqTpTBV64PQliKAa0G+bUjhXR1feotjt2Wxv8KFqm+6W7syIzlGZVzcCb55TS325D7qlHgCUu823fL+f34q+acvupBUuQRUsK/MoZFrAB09vtOLpY0rgVRRvTsqAGuRHNPvOgSn00ytUb6sY5uAhtUl8xMDuPYr37S+jEwIu8b2uYPmk67Q7KgTEFhHomMbD//xVLq+cfvSrnaoQenNRiBiwbjNSW7As9DqP9hbFm1b36VLKlg6VSgYJnfFSLCLis2ByZgonGzm5nPK5UGevtTT7uS5EYMHeKNbsM4N07i7iI21bMoc57PHZXMvt+nd4SXJ1aO+yLN0ePV57aVUv5hIX/0nfGTdqUIprT/N61wXkzSNqzIJAqU0pDbadWsb5nirirVgtfuV27BGiL+xMyFIuKaPJo9mquG4AndW4uZOiRes4PmQlFFpelNeWnBxVJaXz6lCLFHsPa8+hxBJv3ZUxQJP6UPOIokkWMKgcTGF5/wUjWpQGqS62SLxoYYKYv2qOTCUS+9uaksbDxa13+9zd/4gLFvv7Z1ymOj14zD8fAZJPE0UQXMM7XRtkVtmPnqj6xCHRiHZVQ6vQgEjYtff4INU46n1y2t+Ts6JU2YfF+wQzSJ1JmkfLgnryNOdTv+ItogXkqGBSzxAmvQMZx3ITkLYt+3A7wn8 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 436bb76b-76aa-4469-6468-08dcfe9b0d05 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Nov 2024 19:41:47.4011 (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: AS2PR03MB9792 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for struct file related CRIB kfuncs. The test case for bpf_fget_task() is written based on files_test_process(), so there should only be 3 opened files, corresponding to file descriptors 0, 1, 2. bpf_get_file_ops_type() currently only returns FILE_OPS_UNKNOWN, so no test cases are needed for now. In addition, this patch adds failure test cases where bpf programs cannot pass the verifier due to untrusted pointer arguments. Signed-off-by: Juntong Deng --- tools/testing/selftests/bpf/prog_tests/crib.c | 1 + .../testing/selftests/bpf/progs/crib_common.h | 4 ++ .../selftests/bpf/progs/crib_files_failure.c | 22 +++++++++ .../selftests/bpf/progs/crib_files_success.c | 46 +++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/crib.c b/tools/testing/selftests/bpf/prog_tests/crib.c index 48c5156504ad..5ef887e43170 100644 --- a/tools/testing/selftests/bpf/prog_tests/crib.c +++ b/tools/testing/selftests/bpf/prog_tests/crib.c @@ -108,6 +108,7 @@ static void run_files_success_test(const char *prog_name) static const char * const files_success_tests[] = { "test_bpf_iter_task_file", + "test_bpf_fget_task", }; void test_crib(void) diff --git a/tools/testing/selftests/bpf/progs/crib_common.h b/tools/testing/selftests/bpf/progs/crib_common.h index 93b8f9b1bdf8..0bc77d1b02b2 100644 --- a/tools/testing/selftests/bpf/progs/crib_common.h +++ b/tools/testing/selftests/bpf/progs/crib_common.h @@ -18,4 +18,8 @@ extern struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) __ksy extern int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it__iter) __ksym; extern void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) __ksym; +extern struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) __ksym; +extern unsigned int bpf_get_file_ops_type(struct file *file) __ksym; +extern void bpf_put_file(struct file *file) __ksym; + #endif /* __CRIB_COMMON_H */ diff --git a/tools/testing/selftests/bpf/progs/crib_files_failure.c b/tools/testing/selftests/bpf/progs/crib_files_failure.c index ebae01d87ff9..9360aad50c15 100644 --- a/tools/testing/selftests/bpf/progs/crib_files_failure.c +++ b/tools/testing/selftests/bpf/progs/crib_files_failure.c @@ -84,3 +84,25 @@ int bpf_iter_task_file_destroy_uninit_iter(void *ctx) return 0; } + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int bpf_fget_task_untrusted_file(void *ctx) +{ + struct task_struct *task = NULL; + + bpf_fget_task(task, 1); + + return 0; +} + +SEC("syscall") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int bpf_get_file_ops_type_untrusted_file(void *ctx) +{ + struct file *file = NULL; + + bpf_get_file_ops_type(file); + + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/crib_files_success.c b/tools/testing/selftests/bpf/progs/crib_files_success.c index 8de43dedbb02..f2e8becbfd04 100644 --- a/tools/testing/selftests/bpf/progs/crib_files_success.c +++ b/tools/testing/selftests/bpf/progs/crib_files_success.c @@ -71,3 +71,49 @@ int test_bpf_iter_task_file(void *ctx) bpf_task_release(task); return 0; } + +SEC("syscall") +int test_bpf_fget_task(void *ctx) +{ + struct task_struct *task; + struct file *file; + + task = bpf_task_from_vpid(pid); + if (task == NULL) { + err = 1; + return 0; + } + + file = bpf_fget_task(task, 0); + if (file == NULL) { + err = 2; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 1); + if (file == NULL) { + err = 3; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 2); + if (file == NULL) { + err = 4; + goto cleanup; + } + + bpf_put_file(file); + + file = bpf_fget_task(task, 3); + if (file != NULL) { + err = 5; + bpf_put_file(file); + } +cleanup: + bpf_task_release(task); + return 0; +}