From patchwork Mon Aug 19 10:13:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13768148 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AS8PR04CU009.outbound.protection.outlook.com (mail-westeuropeazolkn19011027.outbound.protection.outlook.com [52.103.33.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F38E1165F14; Mon, 19 Aug 2024 10:16:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.27 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724062591; cv=fail; b=rDxU5Phy+8DajbVRN4KbxrHJIMwqMqeY9ZsrqAYWJbf6Dv87ScWbEwyDU+xL6/IWUNtuJJj9efobHF+WE/ptbD1FED6H4hahNpe87p2JVyyyAeA8q5Z3bdCQNq4hJiXLX+gDyrd6Z/FSycjnhbLhyDhoxykASJ9r+SlAvMDwEsY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724062591; c=relaxed/simple; bh=KsQdJsgBkgsrgplVh4rZ+1UCcpaCmhEvl8mfS6/cNMs=; h=From:To:Cc:Subject:Date:Message-ID:Content-Type:MIME-Version; b=INRP67TqQX+vivEK5YUOHKVJduBLIFbemo76DknmlncHvMiAO0SYNg15DA8LlQIaQUV13o51xu2fDRGYfpYLjXCcTLOrwqUe1ECWCPVOPyXSIUIWjt3HTNHdkzvEfac3BBr+6bN+xA2ZozczPHMhJ5jE/Di66wWkIiLyn7eOkI8= 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=mhu9opnl; arc=fail smtp.client-ip=52.103.33.27 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="mhu9opnl" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=LWGcdwFgS692x5Klf504x0b4e9sNuyxQeTY/FspvgMfISahUS+7b9DUwP2Hi770c18ERSVWMbsYUm8K5x0sreu2N9aoUwpvnFkbs4GO83++wvKsgK5lvRHUmekJAheORhmyObXm1GGvISHz5LxEo5FwzqE/A5jW7pwS7V5V57dZPvUvvWSnqjQHQu/jdC4R54FE2xVzhOxzPVCCI3ju9qj4iJZIv9UXm6CrIsF1vGnjEDAuiS49M4eyaOSp4+bJoq7C6XQ7QpDpDx53HLdvOFalE6BWE3H3f6kHmHdpaTtDWX3QZXmwhBYHROvvb3RfAPrMtvqJFT5GLDKcXgho6MA== 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=WSO1Fp/Uii6BaHYkbchzc2eyOtlTYi0WWMedNvYDons=; b=xwDqax3ME91WyW4a+Qe0Mfet9HEPkUxl64Rwq065jzXK3j+UFREtwYpsr+iTp84S+ISJrAvlqWRp79rKhBkUHXw0UaMg+hGlYUhe+ojgaXWad/yMJhilEvVcd2BSnFiL7F8bzfzjsggE8ShfFDFzOeguxZZsmRnbLBh0Co/XC4Q7fQRJxoDVfqt57kyd62t2i5DdWZIQUZxJpoQXYRmCelWIGJZvBtXYVaJHjVjClTdwfvZzsCZlL1rxaOh65bzXE4wS9gxxpp0P0ZWPSNtjA7xIZqctbIBeH+LiSDWr2pCZlmzueXhF6RXtk4/r1yVl7Yic0R1Kyrmszs+sXJ/cAg== 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=WSO1Fp/Uii6BaHYkbchzc2eyOtlTYi0WWMedNvYDons=; b=mhu9opnlhDxaKaNWynsS+JGHcpKl8O3RuQYRnOF2ayXj8jBksoKLg+xrN0Bi/Lf/sRaeZGQMeZgXMuwneYnb+hCFOyzho7iZP5dMISjVhkOD9TQlFGJHYVpzZyNFXuTakNqR6vTrW/ncGqM68VFQ7hfvryS/WkDkeqobGyaysaRZ6dSXdOFe5RPdXdwW08ZjfLfTF9Qm/ePBxIztKUGUTqYIujpU82PM9zl0g4uXmKdsQFQ01kd9yMiORRXw9vIjHCGycCY80D4qrgcf+Z5rOpfenrUP0yDC6K406xLJ7aNm8Qffz9SeZwodrnYDUp8L1dz1mQPdf5s6iKL/eOJt6A== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by DB9PR03MB7291.eurprd03.prod.outlook.com (2603:10a6:10:221::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.21; Mon, 19 Aug 2024 10:16:23 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7875.019; Mon, 19 Aug 2024 10:16:23 +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 Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v2 1/2] bpf: Make the pointer returned by iter next method valid Date: Mon, 19 Aug 2024 11:13:52 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 X-TMN: [wqRm7CFf1S7KHOwdTPMhVXW/T1gw0IwD] X-ClientProxiedBy: TYCPR01CA0095.jpnprd01.prod.outlook.com (2603:1096:405:3::35) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240819101352.62730-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_|DB9PR03MB7291:EE_ X-MS-Office365-Filtering-Correlation-Id: b6d3f162-3283-4c52-7141-08dcc037f9ae X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|8060799006|15080799003|461199028|5072599009|3412199025|440099028; X-Microsoft-Antispam-Message-Info: NtoUiFGpdX7sFCyWbRqrKBADYKhsd0/e0duRCEdsWlp8S/NdhvPAfFjCu9r2zC4tDDbaQ1skz9a2GwLl+Ioe44aAcNM9Ulgp0LoUOSEhJCKcgHxVBndxfvS1pFPIV1+qgVRFrG7RI4WW9lD9Illj8C/ZDXqQ55iHKZ88w1EbIdvOgidpT6sNkaTxYcqLgjCMCabjirmLrNIxpi11RKJGPL27VsdpucV+DeSvSFf1FUDTZOvAWi2wnjVHFoFKmIDLRYHU8JU8kO+OJ5osB5elOdolIQzIWBc2YNbWe0H/Y7d6T2WBJWMHtSWF/GjaU/4U1WOQIjditUiQJ2iYVJBYMzwi43fk+stfghOFDJagM6HkCrwUl4oHrRMrg8jlXU60xmAGRvV7FEwpK75d1KoKvt7/ObRjOLUnljDfp1E9qXthObVhsrys1fJ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ELy9zWQh3YQWB+IV9vyuBUgFBGeZky57NnK7VL9kaYdlGUsGfLNw/6ZseXNNcFNX4UYqB/0AydDCZ6pUjSekyEIToRTdB0PaY6IncMrrqm36cLSEFE8EOTNk4txpkYcBYszdWoGujebXQQBCjXWAFBzkuVTerLXa/gYflRM7nIhOANJUlH9h+WvFyID9vycCTMAzYwabryp7pwQEbl6RegJoqq5fqFtPa3ve5D6egDy72BvieI4tL23jfsrFghhaT0+FYfLavZf/L7NB38amhqOqfd7bRp7iVZRJ9r1KVGrNA6A2c8/7jBSiHnNVx3MDOMYvMLYU7xaRZ2dQeF/VePNELrApwCZRHIn7/OnebI84haYqkODyIIthoT1IMbN7FKFuszrvbXAVOTqUUab5z7lcgvuKe/gXKjDdAfsXG6yCKjUxJDlIJN2Z/+CJmgr/L2rQUTrLJx9zpzJD+bsW86AqrecST7VQ6i13CDy+HygWw8VAWwRDCi6P5n3F1CSdL3vB5cg4YsmR/IA+s9mwu2ICmL6IxNVccKAnMtVy4wB X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b6d3f162-3283-4c52-7141-08dcc037f9ae X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Aug 2024 10:16:23.0717 (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: DB9PR03MB7291 X-Patchwork-Delegate: bpf@iogearbox.net Currently we cannot pass the pointer returned by iter next method as argument to KF_TRUSTED_ARGS or KF_RCU kfuncs, because the pointer returned by iter next method is not "valid". This patch sets the pointer returned by iter next method to be valid. This is based on the fact that if the iterator is implemented correctly, then the pointer returned from the iter next method should be valid. This does not make NULL pointer valid. If the iter next method has KF_RET_NULL flag, then the verifier will ask the ebpf program to check NULL pointer. KF_RCU_PROTECTED iterator is a special case, the pointer returned by iter next method should only be valid within RCU critical section, so it should be with MEM_RCU, not PTR_TRUSTED. The pointer returned by iter next method of other types of iterators is with PTR_TRUSTED. Signed-off-by: Juntong Deng --- v1 -> v2: Handle KF_RCU_PROTECTED case and add corresponding test cases kernel/bpf/verifier.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index ebec74c28ae3..d083925c2ba8 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -8233,6 +8233,12 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, verbose(env, "bug: bad parent state for iter next call"); return -EFAULT; } + + if (cur_iter->type & MEM_RCU) /* KF_RCU_PROTECTED */ + cur_fr->regs[BPF_REG_0].type |= MEM_RCU; + else + cur_fr->regs[BPF_REG_0].type |= PTR_TRUSTED; + /* Note cur_st->parent in the call below, it is necessary to skip * checkpoint created for cur_st by is_state_visited() * right at this instruction. From patchwork Mon Aug 19 10:19:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13768149 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazolkn19013064.outbound.protection.outlook.com [52.103.32.64]) (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 67A5F15F316; Mon, 19 Aug 2024 10:20:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.64 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724062853; cv=fail; b=VqaSYsmbilaun5PR8PHU4DD4d/u2GkElkJ2Et3auicwpLLDJ3TT7jiaKOlTZlmxdk7naXm79dJW72OjLUSBD6Q75iiJwlexFBd52vz0BfRrwpC6E7q3cTuCfrQKsUhiwYQ0QIcuPkJTW7Zgof96U2stQTxm0y33tuYtPZa4trF8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724062853; c=relaxed/simple; bh=Dhefy81E9cEfL5ord7aekh9E3am4uKx5YuQdieh62WA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=X/3rWj1oWw02ond61+vUlWnpRiKXl8cIxH/z7/1ynR4ngaoJBbx8DRvtqKayGFDNnONNXfkVvvO44NwKmXy8V4xvxXKQMU+WdYh6ZK6YFbpVrVzvxj2/VxD6Ksa+DXGJpyzkpD3H947nodHL91v/N085Bi69IRvCYn89OCNpc7M= 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=laEeXnMW; arc=fail smtp.client-ip=52.103.32.64 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="laEeXnMW" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TetdGcdSMxyDnyMaiG9OJKY4gMocDs7Ss6RDVGdNJI1w33mKdfWFdXLWgguc1UFJrUEz3gxC/Qlnnap1EyNP/BmB2f7MltLWt5D77DcY17MqlSfrlJIO8Wp+2EbUKfKfXTsUNjsnWIa3ryqMzE3VUbCmtk3tHvGBSY5BpfiFU7117tX6eatmViYBJDVTSUZoMe5xQYwMD4yHkpkkJmuBlp8eXXzes66CXzcp4sHpAe5d5norz5wm9/EnAU/f5EyMPMFJwpDrhYXokigHL7foaka2JZubPkDfrtT4dTNDChdbuvfxEZkJQ/A+C1zFN1Ikx7PEz58xmHyXBuyV4i7FxQ== 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=qa5XMel2MwKksBCeE0KrhC1Jb3sMC7a7F8FMvWelbvs=; b=JbOy+hErI55yS2bxFmi6DMSi/z4iWPUfQgbNHg55l12atT1gcSBqObJmvjJv+YvpGW4PoEVJ2RstuBkKgpkV1TiRMZNUiFpQJOEXIAl2uB+ksFcV0QYl63rokKOmnT87MalCHjnwrlpIt/OC9ONDpMvtoAFG0F1rQ3+stX245fbuSJPZylrpuO+Vovh72YfFoOxVX3lQtlhRWCRCu/dzCD6iA+oAjRynRf1UXRenyDspYt0c6PV0jzLwe8FnZ4oUzFoGCH8Sr0rrzb3V5p48UvFu3seHpjy6NO851L3a8k8aRjf6I+2vmVFfBCMKvARDlEyoGYtAp06K1ZIYPXT5Pg== 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=qa5XMel2MwKksBCeE0KrhC1Jb3sMC7a7F8FMvWelbvs=; b=laEeXnMW1+jU3fb+P/Hy0NZyOzHfo1zj4DSukGDuZJqp6teZ0ZjxtUr5VCaotXrOv+U4ie9gliD99DOQoX+UvhKsI0vh+4j+8Wg96u5gQ83Cs9zk7LY/oaAxsiH/YTE6erMjve47oKZPzQoMIyoX2m+UuQNGmgRuooUcPe7Y41O59Bwz+AFsrdoXe3vInS8Br1jimKaX3MgqNrZo7RVamGVJBvDR43TWk5kCfQt/4wNP26Df4sO0GZpc82nk9+CIWsvTplKZzb5d6g+KURz+iW66O/+RwPss2eEG4b45XGUE2tbQT5CML9g64DqkBpjo+3nMrp0YHMWmY5B78niIpQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by DB9PR03MB7291.eurprd03.prod.outlook.com (2603:10a6:10:221::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.21; Mon, 19 Aug 2024 10:20:48 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7875.019; Mon, 19 Aug 2024 10:20:48 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Add tests for iter next method returning valid pointer Date: Mon, 19 Aug 2024 11:19:21 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [C1wkyTxqE+d32M78Y2MqmRlFaEVz+h8V] X-ClientProxiedBy: TY2PR0101CA0013.apcprd01.prod.exchangelabs.com (2603:1096:404:92::25) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240819101921.63606-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_|DB9PR03MB7291:EE_ X-MS-Office365-Filtering-Correlation-Id: 3beb64e6-ecd2-464b-fa7e-08dcc0389815 X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|8060799006|15080799003|461199028|5072599009|3412199025|440099028; X-Microsoft-Antispam-Message-Info: WKO5n526cj1NKZ97tLBXtSjDsmoJtcnqjErEo927Hd9kOlU10eKK5jatNKpb2evYovPoNl64KKcBSXzIe7l2nTkc/6VX0y6uiIx0SAO3pdEUAUHJZNjo6L8/VwNyYxIgQbHsTaKp+ViLIOzGic8KOswHMh2dbK6xJZ8/oTI826Pm6HgwphNYhM94+DgDr/0We4uwbaw5mFajleRlb491kC0Tkv25IvDMavBOt8WkI4SunC21MUW0zJNqEppqsH2LQJj4+jqGNKtIUQ/2Hii+29dZiN1zhismzJ8TQtDK1i/w5XIuFcnFX3uxKCxSMNZZHnEfHJcKDdTymKd+JpihLE4F6LioMnIMWsWGbH/uni+0lzf4LlBCwNAUvugo4bcgM3tmcCVEP3fizHbwjQVS1FUaTEf2XTocldz2W2UedWkmRZK2LIcscXpjxs9BM5jpuK/CjVm1Lwr41szDi8ugsvMkbqIYtJawDBK99lMknV42kwqZ0dFLl99PG01qpfgZ1RAJI/YdDsCTl8xj1ZgOB1s+UYPJjEhwViV4DdyndZupXnTfxiUjiSSTJsRzG4mmbGfU23E0X/o7F9RY+QDtq9/kM9qjr0DayCnuHo7HmRy9/vqThfKFBswHU2Y5zCcvoo31hrFdDN1gDPxUVc+wT56l05uRMwkrnedl1zL167iWsBc1HsiXOXZhnnBj+CT4Uv+Uky8wB/yO6V1Y4R2j7Q== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 6Xad8LP72fzDvdPoJcR2MfIAz3IdcJ28ZglN4sXsWSzWUvK0bVpJBWZuGV8fJgV915Dgd4T8DTiXoR7KsWnDnkicn6P4fY9Ca1ZJM1IH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DW5euHIhMutfz6JzSFCiHBZxNQK9yQ8CoxD6rj2AamK3uftjCTkYBiLgqd4ZrjHhP4PGOKGUMFC/OP45Eb1tg/Vmd6FT X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3beb64e6-ecd2-464b-fa7e-08dcc0389815 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Aug 2024 10:20:48.6216 (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: DB9PR03MB7291 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for iter next method returning valid pointer, which can also used as usage examples. Currently iter next method should return valid pointer. iter_next_trusted is the correct usage and test if iter next method return valid pointer. bpf_iter_task_vma_next has KF_RET_NULL flag, so the returned pointer may be NULL. We need to check if the pointer is NULL before using it. iter_next_trusted_or_null is the incorrect usage. There is no checking before using the pointer, so it will be rejected by the verifier. iter_next_rcu and iter_next_rcu_or_null are similar test cases for KF_RCU_PROTECTED iterators. iter_next_rcu_not_trusted is used to test that the pointer returned by iter next method of KF_RCU_PROTECTED iterator cannot be passed in KF_TRUSTED_ARGS kfuncs. Signed-off-by: Juntong Deng --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 15 +++ .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 4 + .../testing/selftests/bpf/prog_tests/iters.c | 5 +- .../selftests/bpf/progs/iters_testmod.c | 105 ++++++++++++++++++ 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/iters_testmod.c diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index a80b0d2c6f38..2cf0716ec64e 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -176,6 +176,18 @@ __bpf_kfunc void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr, { } +__bpf_kfunc void bpf_kfunc_trusted_vma_test(struct vm_area_struct *ptr) +{ +} + +__bpf_kfunc void bpf_kfunc_trusted_task_test(struct task_struct *ptr) +{ +} + +__bpf_kfunc void bpf_kfunc_rcu_task_test(struct task_struct *ptr) +{ +} + __bpf_kfunc struct bpf_testmod_ctx * bpf_testmod_ctx_create(int *err) { @@ -533,6 +545,9 @@ BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_kfunc_common_test) BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test) +BTF_ID_FLAGS(func, bpf_kfunc_trusted_vma_test, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_kfunc_trusted_task_test, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_kfunc_rcu_task_test, KF_RCU) BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_testmod_ctx_release, KF_RELEASE) BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h index e587a79f2239..db657f7a7802 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h @@ -144,4 +144,8 @@ void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr, struct bpf_dynptr *ptr__nulla struct bpf_testmod_ctx *bpf_testmod_ctx_create(int *err) __ksym; void bpf_testmod_ctx_release(struct bpf_testmod_ctx *ctx) __ksym; +void bpf_kfunc_trusted_vma_test(struct vm_area_struct *ptr) __ksym; +void bpf_kfunc_trusted_task_test(struct task_struct *ptr) __ksym; +void bpf_kfunc_rcu_task_test(struct task_struct *ptr) __ksym; + #endif /* _BPF_TESTMOD_KFUNC_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/iters.c b/tools/testing/selftests/bpf/prog_tests/iters.c index 3c440370c1f0..89ff23c4a8bc 100644 --- a/tools/testing/selftests/bpf/prog_tests/iters.c +++ b/tools/testing/selftests/bpf/prog_tests/iters.c @@ -14,6 +14,7 @@ #include "iters_state_safety.skel.h" #include "iters_looping.skel.h" #include "iters_num.skel.h" +#include "iters_testmod.skel.h" #include "iters_testmod_seq.skel.h" #include "iters_task_vma.skel.h" #include "iters_task.skel.h" @@ -297,8 +298,10 @@ void test_iters(void) RUN_TESTS(iters); RUN_TESTS(iters_css_task); - if (env.has_testmod) + if (env.has_testmod) { + RUN_TESTS(iters_testmod); RUN_TESTS(iters_testmod_seq); + } if (test__start_subtest("num")) subtest_num_iters(); diff --git a/tools/testing/selftests/bpf/progs/iters_testmod.c b/tools/testing/selftests/bpf/progs/iters_testmod.c new file mode 100644 index 000000000000..570dfadd4648 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/iters_testmod.c @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include "bpf_experimental.h" +#include +#include "bpf_misc.h" +#include "../bpf_testmod/bpf_testmod_kfunc.h" + +char _license[] SEC("license") = "GPL"; + +SEC("raw_tp/sys_enter") +__success +int iter_next_trusted(const void *ctx) +{ + struct task_struct *cur_task = bpf_get_current_task_btf(); + struct bpf_iter_task_vma vma_it; + struct vm_area_struct *vma_ptr; + + bpf_iter_task_vma_new(&vma_it, cur_task, 0); + + vma_ptr = bpf_iter_task_vma_next(&vma_it); + if (vma_ptr == NULL) + goto out; + + bpf_kfunc_trusted_vma_test(vma_ptr); +out: + bpf_iter_task_vma_destroy(&vma_it); + return 0; +} + +SEC("raw_tp/sys_enter") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int iter_next_trusted_or_null(const void *ctx) +{ + struct task_struct *cur_task = bpf_get_current_task_btf(); + struct bpf_iter_task_vma vma_it; + struct vm_area_struct *vma_ptr; + + bpf_iter_task_vma_new(&vma_it, cur_task, 0); + + vma_ptr = bpf_iter_task_vma_next(&vma_it); + + bpf_kfunc_trusted_vma_test(vma_ptr); + + bpf_iter_task_vma_destroy(&vma_it); + return 0; +} + +SEC("raw_tp/sys_enter") +__success +int iter_next_rcu(const void *ctx) +{ + struct task_struct *cur_task = bpf_get_current_task_btf(); + struct bpf_iter_task task_it; + struct task_struct *task_ptr; + + bpf_iter_task_new(&task_it, cur_task, 0); + + task_ptr = bpf_iter_task_next(&task_it); + if (task_ptr == NULL) + goto out; + + bpf_kfunc_rcu_task_test(task_ptr); +out: + bpf_iter_task_destroy(&task_it); + return 0; +} + +SEC("raw_tp/sys_enter") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int iter_next_rcu_or_null(const void *ctx) +{ + struct task_struct *cur_task = bpf_get_current_task_btf(); + struct bpf_iter_task task_it; + struct task_struct *task_ptr; + + bpf_iter_task_new(&task_it, cur_task, 0); + + task_ptr = bpf_iter_task_next(&task_it); + + bpf_kfunc_rcu_task_test(task_ptr); + + bpf_iter_task_destroy(&task_it); + return 0; +} + +SEC("raw_tp/sys_enter") +__failure __msg("R1 must be referenced or trusted") +int iter_next_rcu_not_trusted(const void *ctx) +{ + struct task_struct *cur_task = bpf_get_current_task_btf(); + struct bpf_iter_task task_it; + struct task_struct *task_ptr; + + bpf_iter_task_new(&task_it, cur_task, 0); + + task_ptr = bpf_iter_task_next(&task_it); + if (task_ptr == NULL) + goto out; + + bpf_kfunc_trusted_task_test(task_ptr); +out: + bpf_iter_task_destroy(&task_it); + return 0; +}