From patchwork Thu Aug 29 20:11:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13783790 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2050.outbound.protection.outlook.com [40.92.90.50]) (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 60BD81BAEF1; Thu, 29 Aug 2024 20:12:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.50 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724962336; cv=fail; b=hAlVtWy31JGGNVOsSxEKvihp5Fj/Rhrp3EsQYhPDruVSOJ6fUtmp81TD7n/h/eqt81i8jSeE/pm7rQrICV+r1WJsh2jNPUa6imX9IbywHuTblD5jmr6yHxZTMJ+FubkzfR38/nsR1rzzH+JoWZbTYGlHa33x/M9e/OmGFILHO7A= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724962336; c=relaxed/simple; bh=mXaUltMQOqY6mOy3v5upBPfZp2TJ2N/EvBax6MVebpE=; h=From:To:Cc:Subject:Date:Message-ID:Content-Type:MIME-Version; b=YZ0uZ+DdRFky2iWfuULLh+N52+Lty44Qw8EK1Eeicu/mBFj1Jtt7Ggn2hVCJQYz3ep5sTG1Dn12E1btXynsrq9fM2jZbgM4IKTaBGvOmYgekSm0qLUpbpwjkT8aldcj7DkoniHeVjYvCSePLPQt8Plg+WDcHH51F9h5XDmj5Lrg= 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=kcwI/nRg; arc=fail smtp.client-ip=40.92.90.50 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="kcwI/nRg" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DL5TdZkEEE0o71YDKGcCWXi2914ym+vSHVEitAvv/DX/YlvUo7bGzGksngbDNDGDc0zYN4plVkrC+51On5oYjqQDEDOYz/VOx/jY8Ug63K39hYjjfRYD9Ix0gGslvG8e/CzpxKSbwtEL0oiUgcWOPIZl1x2wZthZeikW1L894Y0eO1bepmzeczcTi0LFJnTm1N3OAo5SmMGclfpmmROpU+pEvZm0V//6mDBthfK9m2++adYai4o+roOoq2Oe5E+6YLtSX1lB83qCpFEQsqHbyaf8Kg8dEDO4veRhR9R5VaIyFjJlvFG5G1A2miYkgv/cJnd0qJ5LIP4fCuneEn26Hg== 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=aZw/Lw/cxn36AmgX3V4Ar93G+6lP7C8ph2DovvJqq10=; b=unZS/+3F+80XtLBbxpHTZh1XastT9o9/fgc6rb8FDWa/d32eg+6xjzYF2XZJKVBAkH/3WDTfbJvMl9XwT5H++tW4degEOQTu6GS1gluGl35lAw07jJzMpiXgSxXzPTc2y2egeYudl8vzRNf5lM40pVLci7iXlfebxvyKdo3cym5jLb28ew7SGZP+rbky+yB9jMzT5vo8W+5zdtfxmnqB2VJqaZOTE6vLmFU+0itQFz1vbUhMueWHgwc0LzuIepL1dXKOnmU982Lt4kTFmVPm0jCRhXH2s6o28Sim3rQ5txokdTDtuIMeF2U8m8SUCacKnxxni/+Fh4J08f800XLeSQ== 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=aZw/Lw/cxn36AmgX3V4Ar93G+6lP7C8ph2DovvJqq10=; b=kcwI/nRg/gyABaQilh86E6rdekkhquJyfJHGYNl2U5r5b0Blj0kN6YQDIACVxwPByLNLd4JiFJdvybkaaaWftsjNJotlCOIu+S0izKgBEM5rwsGGXlFXsvsKqchHSwnwe3Rr2k63STJzh1DpHDGlct00HDO/YHmH7XAQYMN7ajTkDrVPxIhNdCfSGcXTlV05stSIlgACXYfk8UBlBKxRE7za8+uIBbF2L7y/N5Apvg0T8qp5j1YC9C6oruBmyBkdZQPdeqQBIjHOho9tF0uvgP0Nm1mc8ZL6qLW3yQ0HvZOUjK6RSAZA5sAfVEu6mYDqw9F5F3WadjCk2xtPMAIk6g== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10409.eurprd03.prod.outlook.com (2603:10a6:150:163::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.28; Thu, 29 Aug 2024 20:12:10 +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.7897.021; Thu, 29 Aug 2024 20:12:10 +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 Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v4 1/2] bpf: Make the pointer returned by iter next method valid Date: Thu, 29 Aug 2024 21:11:17 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 X-TMN: [ITCKKKO8MRpVsLi+PvMuhIoOXkvwX773] X-ClientProxiedBy: LO4P123CA0217.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a6::6) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240829201117.25056-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_|GV1PR03MB10409:EE_ X-MS-Office365-Filtering-Correlation-Id: 83bf9d14-d9d0-42b7-bad0-08dcc866dd02 X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|8060799006|461199028|19110799003|15080799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: zUQmB4QdBN5XDo+ZXOUC4dR+E40cNKbwHaS6fA+qu1zmVQy/p+39woRSZdZACflf+v6r0iTH4n6ep1BnFBFiwPgN33vOd76PtqPaejt8YZj91udp+1AjM/65nOo6jpeQnTzizYlcLe8iuFyKH2s54Lsd9ClHINxbELAS6RfoIBAm4ySjNI22y2C2jSmqPZgpvjdQO3VUMSYN/1+wMSQSdl0C1FW/yUtMSvZ4t2rv/t+wQVyYH9I8WQioOzTFEtWh2iWCj8h5qFe/5ET5d1mrfhKUeUGCtAroYOGHZrmFokyxE9IkvUj8+9m2i/XHrAisSWFapiWC6xh9xvFWIVGhUQ8pkwCDtBG6qsjZRvPX0vjUoamYWGXfB8gbA0KRHZe8wTVMkeeyznOF0URlYVl4dDbI5AQSMD4KpQnxP1gg4365slGxQ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nDWPByDW+aWn+fyBEun9l/ryZ26nUOPyuWOXYp01frVFyGIE6Z2pwpWSLw/oz8BxoWOKXpUIdLc5sqJ5EMwCUEXuUWIwYMKP/+YZVtmP0tRnyybuAFuW580BU7fHj6xuCaS1+D8jBU4ayXziBC2unFSDTqZfTUlbwG4Z+5If7CgY3U2z3b+w33QgUV5PliyQntK2Ar3IBX+sRNTdA1czrJFaVCeTEAMSqa+tQ1s9ViELYnX1K2SXnXUhOK/6+A5f4ujdQ6ovcX+DoZ6inUNW6a20Hs+24XVuIrGcwJRtrKhAqgXOWuIjlMNGXY1tmjmYh0QMCgkTF8qMKvj4+XOLlLjb5mlG5+Dbc53sNjcF89kp1ScOQsy9t3aKVsTxT5JuXwQv9+9AdR6CjNJLjceG4FsrAXo72jUuy8SZchoJ9eYan0gBKA1P5AcL44DxIpOnT63LAN9J9QYiUVwYyWJDCVpFCjPRsxLdfYe+WjJX73BYl/4hu1AlCluPOW35oBIjX5jkjE2JMbBtNgeNWRbFOwTXithSAanpMAaKqTcH8p4 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 83bf9d14-d9d0-42b7-bad0-08dcc866dd02 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Aug 2024 20:12:10.3593 (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: GV1PR03MB10409 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. Another special case is bpf_iter_num_next, which returns a pointer with base type PTR_TO_MEM. PTR_TO_MEM should not be combined with type flag PTR_TRUSTED (PTR_TO_MEM already means the pointer is valid). The pointer returned by iter next method of other types of iterators is with PTR_TRUSTED. In addition, this patch adds get_iter_from_state to help us get the current iterator from the current state. Signed-off-by: Juntong Deng --- v3 -> v4: Eliminate the != PTR_TO_MEM part. v2 -> v3: Move modifications to check_kfunc_call. Handle PTR_TO_MEM case and add corresponding test case. Add get_iter_from_state. v1 -> v2: Handle KF_RCU_PROTECTED case and add corresponding test cases. kernel/bpf/verifier.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f32e3b9bb4e5..f1d764384305 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -8148,6 +8148,15 @@ static int widen_imprecise_scalars(struct bpf_verifier_env *env, return 0; } +static struct bpf_reg_state *get_iter_from_state(struct bpf_verifier_state *cur_st, + struct bpf_kfunc_call_arg_meta *meta) +{ + int iter_frameno = meta->iter.frameno; + int iter_spi = meta->iter.spi; + + return &cur_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr; +} + /* process_iter_next_call() is called when verifier gets to iterator's next * "method" (e.g., bpf_iter_num_next() for numbers iterator) call. We'll refer * to it as just "iter_next()" in comments below. @@ -8232,12 +8241,10 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, struct bpf_verifier_state *cur_st = env->cur_state, *queued_st, *prev_st; struct bpf_func_state *cur_fr = cur_st->frame[cur_st->curframe], *queued_fr; struct bpf_reg_state *cur_iter, *queued_iter; - int iter_frameno = meta->iter.frameno; - int iter_spi = meta->iter.spi; BTF_TYPE_EMIT(struct bpf_iter); - cur_iter = &env->cur_state->frame[iter_frameno]->stack[iter_spi].spilled_ptr; + cur_iter = get_iter_from_state(cur_st, meta); if (cur_iter->iter.state != BPF_ITER_STATE_ACTIVE && cur_iter->iter.state != BPF_ITER_STATE_DRAINED) { @@ -8265,7 +8272,7 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx, if (!queued_st) return -ENOMEM; - queued_iter = &queued_st->frame[iter_frameno]->stack[iter_spi].spilled_ptr; + queued_iter = get_iter_from_state(queued_st, meta); queued_iter->iter.state = BPF_ITER_STATE_ACTIVE; queued_iter->iter.depth++; if (prev_st) @@ -12853,6 +12860,17 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0].btf = desc_btf; regs[BPF_REG_0].type = PTR_TO_BTF_ID; regs[BPF_REG_0].btf_id = ptr_type_id; + + if (is_iter_next_kfunc(&meta)) { + struct bpf_reg_state *cur_iter; + + cur_iter = get_iter_from_state(env->cur_state, &meta); + + if (cur_iter->type & MEM_RCU) /* KF_RCU_PROTECTED */ + regs[BPF_REG_0].type |= MEM_RCU; + else + regs[BPF_REG_0].type |= PTR_TRUSTED; + } } if (is_kfunc_ret_null(&meta)) { From patchwork Thu Aug 29 20:13:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13783794 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02olkn2010.outbound.protection.outlook.com [40.92.50.10]) (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 AAC7228685; Thu, 29 Aug 2024 20:13:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.50.10 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724962431; cv=fail; b=N1Mub3Yx0vZm3VSegxoTCa6Iw3Vf/S7Fonos8jrhDSHfJa6X+fXkTMkRQOFQWsdz4AqdGYIJZ/1BnScAO9BMvGw8cxGPEs243kzOXkHRn6bw5V3QJuCyZqZDXc+WBguOxjzLXfpISdEKKcjctt4RnxqD2JhzJQxtZTTL/tZHo4U= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724962431; c=relaxed/simple; bh=A5V5/XbGEc5SroLnclBlFbsD0EJltvVJew9pg2zeQgc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=MbzORnDa8afOkcDnu400mKi8NqjMXNR3I9ZD32h8e5Ny5JYihgplvc5ENpILedUeZTZPaa70lDarYfR9oKIDXQ18dcQmt+0solNhhZ0wpG6bn+Rx1j/iSAsQc6pzFJrHZh/xfS/HcTECpQsqsCCoSHqU/UmfsvPgzi8TFktYXmU= 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=jY5IBC2H; arc=fail smtp.client-ip=40.92.50.10 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="jY5IBC2H" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pQG3HHF7e4kU6Arey3Q3z/8/Xp8YdOv+81s97zX/WianZGGdz0c4QLJ/DL3jYpA0dgyj6z01Cx6jopaGbUJtVNu4YNMpIWPjiqTBDaNMEfvsyTPK5MIVHq5h9XbzTWA1M6NHSj/Qetq3+M92xMapipgBKD+z/C/5XD95lW+QC/hhXLVA6hmRAKgya8B9jKt8j/q+ed82DB64sIFgBhELWNmlqng+m3ISBUfv5iYMlqdIiukrUU30AXMGzk1Ix5hSYjXm+1sPUbi76DsBaB53Hx5tTNyKBg8mcWxF6Ayz+6PBsCy4EgJ0lmMptmWNHVn4QzJ4low6esRN2aRTm532NQ== 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=50/Qrmy96M9zZnlXy21WkpIVBslH/oeNbAqqpwnyWxE=; b=YVg10+vr61QwapMbDelYIh0Bqkfyjcc1C2vWLqLMMtm+293rgtHh++wBMwoszOhGImVOrNafAZQOWB05rBbFztJ9fhGdaL/5r2Hox9Ny+iEWOu2vIDYLONrU0vbPOoXyNxBAbQaWPNmh69WBc76I2vwpZyoVGn6v8T3g6UDdo2pXGvvhTtWff8mcJ5Lj3PTrNWVWpwsTdEhHbTUvhg5vSlE9aYAR3NRSCs5FSlzangeShd2uUiqUlTN29++QTAb+oIX7Ijag7DWVu1aO6WDw3sdBa/3gK+/MfA7m50dA3308tzeu+tFcieic/sy+Lp2xjy7MznMkLGj43wo//yinfg== 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=50/Qrmy96M9zZnlXy21WkpIVBslH/oeNbAqqpwnyWxE=; b=jY5IBC2HfHXJeHEU0d2KVB5oq/bw+eUvMbye0gRI2KfEp4rhIj+kB9nOUfFyY7ZfSDXB2ZF3UYQIjYTIfCanU9TyH4ENW2yC2czhHmKqdJW91hDu3p3ld6CCl5oTuNdXu0Upe/wDQHJeNOBjQHMb42YyzJTwYOvyaH3rPAALkVzDzBg/bL0VBrIhxppRu6cIZltjXwIDNkDownnWa28mElDOay/EhpltOAHrB3sg2FDoUgY3cmDSUijz/twtvNhq6vXUb7RK5+Ibf31tH+3/jHaC6hT3SKr+Q7/CnNPNRQPeP6sSzQu3yPOdGK+Dy7SRfNcP7aecJI3RbfIhsVIw8A== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10409.eurprd03.prod.outlook.com (2603:10a6:150:163::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.28; Thu, 29 Aug 2024 20:13:46 +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.7897.021; Thu, 29 Aug 2024 20:13: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 Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v4 2/2] selftests/bpf: Add tests for iter next method returning valid pointer Date: Thu, 29 Aug 2024 21:13:15 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [9jQ2VCKOvvS+5OdNzroDzLQLP0HDjQBT] X-ClientProxiedBy: LO2P265CA0216.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:9e::36) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240829201315.25362-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_|GV1PR03MB10409:EE_ X-MS-Office365-Filtering-Correlation-Id: 0184ef61-8450-4f57-686c-08dcc8671663 X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|8060799006|461199028|19110799003|15080799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: cTR7HT0USc1sQYQOo7YEVUEKEn1nTY1KebaL2wTtYvwFnut5Q1L4S3mQ0vXAxqWli6NC3F6S/nojuQGnTbU4H8eqCH7b/skj96DRW3zKnpNN8yTT0fAMUv6xkv2+JaLRTDqTU+uEdNisjGHNF4ZcJew2gY9ylGyqIGtEvD7doilDJvUNVvHluXrgAmuGka6m031jABkQ7skAsts3g0UOZBnUQSzpUmps6ItrodOPkwSWpujUU6tYZb/ISfBzQ7jpHCzhMMm7jHQGaEqU99jiFeKj4SmtvWEUrtvrld29SCRulYFQhtTxhMsUg7RwyMF8URjtoUPrUEjrLXWZidv3RwfK4/VDqSKW+SwxxOFVxHkxVOzAUVWO4ouZ3HLoUxekw5FNCHlJ7qDZrzDZxsnflDmJX6g4yCfdKSXXAJ+V2I3fKHNni+KevOfexTtrW0lFQOgtQK9GNmAHKufQkYyBKBZRQLmtUjOBadIzA9MEKFGZR8uSziOdXs1tnsd//svfgZY4r3AqyvmQO0U7WJWQR3ceeU72TVd1ZUcNLH+Y5K3+KLntTeUlH8RQzdV5ztUOldK+pEA6p8EzcSR+4pptV8uQqmW47b3zasM/CvAkxUrOuBc2dDEzXklsUemrWPay/qpllEwEVaAiK0PiQ5umgQOwdOaOYcU1jHkJBa8kFy1WMDNmwZqmzdqc3C6eRuHdVH59VaBaw9PEY0tySI1GvQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-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NiAHqik/+CPVyWh6gvB7tYoM2PUAQWpVogjMA1YoQI/tniukDEtcqrJr46aFVEQcQi0wjII4Hn0JDR2blQadGmIKE0huhJYptDViBOX3smVWtSUxu4PquiSHnFl7hs62WC8Ns26YbECfamFzTNpu+9NfDfRQSKib7zBNSVZ1yyWXK/h1YbdBCubDAJcybYB64yiT1WkrDU4mhl7ON4J X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0184ef61-8450-4f57-686c-08dcc8671663 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Aug 2024 20:13:46.4163 (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: GV1PR03MB10409 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. iter_next_ptr_mem_not_trusted is used to test that base type PTR_TO_MEM should not be combined with type flag PTR_TRUSTED. Signed-off-by: Juntong Deng --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 20 +++ .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 5 + .../testing/selftests/bpf/prog_tests/iters.c | 5 +- .../selftests/bpf/progs/iters_testmod.c | 125 ++++++++++++++++++ 4 files changed, 154 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 8a71a91b752d..9cbcf1b06d6f 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -197,6 +197,22 @@ __bpf_kfunc void bpf_kfunc_nested_release_test(struct sk_buff *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_trusted_num_test(int *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) { @@ -558,6 +574,10 @@ BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test) BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_nonzero_offset_test, KF_ACQUIRE) BTF_ID_FLAGS(func, bpf_kfunc_nested_acquire_zero_offset_test, KF_ACQUIRE) BTF_ID_FLAGS(func, bpf_kfunc_nested_release_test, KF_RELEASE) +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_trusted_num_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 c6c314965bb1..8f58a6f94bef 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h @@ -148,4 +148,9 @@ struct sk_buff *bpf_kfunc_nested_acquire_nonzero_offset_test(struct sk_buff_head struct sk_buff *bpf_kfunc_nested_acquire_zero_offset_test(struct sock_common *ptr) __ksym; void bpf_kfunc_nested_release_test(struct sk_buff *ptr) __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_trusted_num_test(int *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..df1d3db60b1b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/iters_testmod.c @@ -0,0 +1,125 @@ +// 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; +} + +SEC("raw_tp/sys_enter") +__failure __msg("R1 cannot write into rdonly_mem") +/* Message should not be 'R1 cannot write into rdonly_trusted_mem' */ +int iter_next_ptr_mem_not_trusted(const void *ctx) +{ + struct bpf_iter_num num_it; + int *num_ptr; + + bpf_iter_num_new(&num_it, 0, 10); + + num_ptr = bpf_iter_num_next(&num_it); + if (num_ptr == NULL) + goto out; + + bpf_kfunc_trusted_num_test(num_ptr); +out: + bpf_iter_num_destroy(&num_it); + return 0; +}