From patchwork Mon Oct 14 09:21:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13834498 X-Patchwork-Delegate: bpf@iogearbox.net Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2084.outbound.protection.outlook.com [40.92.90.84]) (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 5183D15B546; Mon, 14 Oct 2024 09:22:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.84 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728897730; cv=fail; b=YAlkcd5YqJWvpFX9E1hftx6piORJP2trfOQ914i052nddAF4xxJhmAbDXEKKKhmGS9Cr9V3aFZK8DHVyl/cTD4StxZGvWG0AUK7T76ORNzPo8FYPREE0MDOqfEJ0wY//ahdUPY4uQNt8F9pLW3/5lNbPdrnrGIMROAHUkbgIrZw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728897730; c=relaxed/simple; bh=9ulxQdit+rGXl48oOMSNU2e2NGLx2VdX5OtigzaV0Mg=; h=From:To:Cc:Subject:Date:Message-ID:Content-Type:MIME-Version; b=rqFDsFSeO11mFQc9dn7uwrFHl9awu/iqQDeICq4iNjYHBmxKN8R8YhNjik+r9EZc3flxcc7FuNUVnb50TdFXv5ZxtYyG6HsSJqSgAGFyW0F1sC++lDAXsRjQTNPQaoqxDFCVDS0qmVOYZr9scjyudSwDR5p12VA1WRoBj4ZAFEI= 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=tc7rJU4P; arc=fail smtp.client-ip=40.92.90.84 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="tc7rJU4P" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ok9Z69uEpvftni1IXcZ2o0CnAo+vP4eLJnfQ3E4Rw4w2BotgRBb/sEuSwyQ1Qes6TKEYHK68eSpYb3khWiz7O6toza2Imm5zuge+fgF3aEQa9GNwdQVNIizOVB3MroetcIDLiNHVS3sAQMs4/s1v4H9s+12vkEM5j4sX3TquGHLM1kVMIpSMWzyxSykNtK187U/dAEeAGxUFadhROnkFcki3D1nUebDIueGb0hVBMj+mvayKRpAOCbnm42bpVVQGJhEBUlfBvKMxL7VHNsPZggNC8vO+v8UJwxzDgm5ryRNd3cgBlKBgMxlWX1BeQVuOzrpeeiEIs4faHvcPBEjagQ== 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=VRSsGNcek8SbuhFMieVURqPmtndJRkbAiZjce3o2z6k=; b=cZwKF5Ozb6G2xAlXOaI2mNt6FfmZuiQCWt81f4yWSZZA659bQPeY88pPVkSMaXuSZIx4mnQrBUhy0FF4nszmx+wtuMa7YP7y5knsl532MhgDqxpLT3ucP8YAfQBiXY+Ql4Si0iHTKk8Y4OMlVZYSa92IJ4k9r1vJL+cDetmZi8pESls8lYF0ToilIUBzkFR2LNV4f6zusjTGOpUuNOTXs3Mpqnx+eZaLJdEUTQntuV9OMZ9+ab2wbVEebN5C2rEKBX/8WRp9sZ0eQCMsSWrq4z09OaM4hc9nWCmvoTwZ0R10i4AfG5+M3XtUfXd54Z5klG16EU1W+0I6rgg/9Rm1rA== 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=VRSsGNcek8SbuhFMieVURqPmtndJRkbAiZjce3o2z6k=; b=tc7rJU4PorVPW0vMxEbWK/WStZ8MQ5aL/grOC5KlcxCeU0XmrmXKyUOZ8axJMsZuCiRPQPVFlXixadokvO7IgrFX/oaelw2QX2Mr84mjV1lJtevZ+pPtQeRREic2ylRGQJ6gTqcDdyCdPG4Gg7rijOcPh461ulyo+4sggAzBsR1K8XkAevoY3UbevU91atStZEp55AberZVt+Vgl30Zpp6JZtch+1tX7YNEzxKXZqQZRHFrndQNGdGpFOAj2gt+FeTOeALhkzIVFgtC+6/STBkRout0Ss4Pw0sadANZQiTyIVEMOO7ou7ZIl4bC667A9GK6VHw7j0+n0jg7LVOpKOQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB6949.eurprd03.prod.outlook.com (2603:10a6:20b:293::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.26; Mon, 14 Oct 2024 09:22:05 +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.8048.017; Mon, 14 Oct 2024 09:22:05 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next 1/2] bpf: Add bpf_task_from_vpid() kfunc Date: Mon, 14 Oct 2024 10:21:08 +0100 Message-ID: X-Mailer: git-send-email 2.39.5 X-ClientProxiedBy: LO4P265CA0074.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2bd::13) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241014092108.15948-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_|AS8PR03MB6949:EE_ X-MS-Office365-Filtering-Correlation-Id: b6c566bb-2a46-460e-1816-08dcec31ab15 X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|5072599009|461199028|5062599005|8060799006|15080799006|3412199025|440099028; X-Microsoft-Antispam-Message-Info: 5A17Ez9hyJfntIy5ItYzJHFVGM3OeQuH0atADF5ciEmL8xsTHm4f8um5kVt8dpztjzCTiKEnri+kUKx9kuBlm0HSHVNRepfvy0jORQRq8SxQaBothLMJUMZMYKtAgZw6/lKznoQR+TpxRyrRfuTjhKdpCD9KVCIktnGBXfVUxMdWbe0XAEdsTQKXX1gPYCy7mEJuu1rbhCVQ3XxV5LOht0x1o38unDPtsapxVHbRy1ZdCGms9bsZUMAq36u6H5yI2MpnR233ZvRAaaW52eF4WAq799FYjbSqvbdRTq0LyP6JzoHUVBgI+es813lfK3QVBElA2yXHp08tKq6Md/JZVFGNAc+ULAgS8KzxOnRwkvP4EC7VraNUsCkq4IcDBZXMBYZz/i4KqGDRiKledKF+plTA1dz3q7gTv4reOBEEm0f9osCe+v9UlBP8kjmEJ6dR/bNp371AU8G0rI2SV5qTNW6XoL1TRWd1fBuFNqyywZ87oM1AMNqiZNQP67lY5kBxJURLdAoNuotZvwHIMM96qDcDzajtgla+EGQ6HTlbbwlEWip1os9opWjXD+tN2GCBfcnEbUFNGOUXfQt5CP3NIluNPxmv6jwqTMjMmK5upFn2662+QGyQ41C6bBtGaFc+UbjIQuo5btV3CcHLf2b0u4vshttOm0wZBugDEYvvhE3pFoF+UuqD4ZQyQ1OkVGq98KNaHPHEBTvZ0zFm0YjZqsWSjmZBrsEclGNQAOa6y2U= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hZSpOXfG+LkAyjUPMJ3vJgBYad5RJMdBqLaWilrCli2hetcaHRlzo4WLBC31kfr0Jzmxx7s0ytju9/klSuUwz5W71JIRfy30Pe04pYCzyC4HRs3v3fZVIDSUKYtDtJOdVIZPguFXP+/lYh/KbWoFJ7UHycBsYByVmloBwrfneP8FAlDYbJ2NPz45DKc8eUaVV6U1IA5T3BJN3hkrSGhxQqbfyNaB2idg4FHBnabfPQ2/XG1Q91Yf8CbZgxQ9fIuRupd+HknDKcahp/QBHWyW/ArgXQzQrr/f4teNOUmycA6+NcrSbx5i7WwI5+rdqz3TF1y1Q075iLSnnmM5z8rk1ALPpP+6kRw8DnhrFSCuqBy1Cwuc3bsvGCjlJ1uKkA2omBiX1p8Sk+9O8aFWZysLuEoA6ygeyi5ryosOKOG2tUOFORiFn+VK5OhSnZ4GxfuQopQxk7K3Gk+tWuTXp8A2U0whqcs4rGUbh5QvZ2XZAA9gy6c7LtA6+k7bon85jkEsDu5wkOl9+Vl0c4e6C4Gb9NxBUD30LS3JzdMKyJVy+NXLuXTFnL4WyJtf/YKZNniCasI6puVaWMFuLxCa0J4aCVqDGbC4LGQGPSmxb3t2rXZoyZxncfiPhi0IDgLHR4q7exm/jAx1mq3mTMxJZQEob5ymFhcYOjOb3rWM4h/rLF7REo22uqekBG7gku5ZS59NqXSeq1C342ZEU8daWDvgjBNog0kqbMcipq6xmaXcY+ML1Pc4WYHG6CR5p8j2IaPO3Pylvc3E65+hvoZokSJ5kSLiMJOkcX2i1kuoaIfs9nub77meyneAq07z3NX/F0hzcMtWVzSkqhVeUf2U7MKaCkNrYIL4cCUuBVpbIbZxgeS9WArP88QjrNL3acMCMi6qmQf+6abbHPamcJN48gyHfjW4lN9U8lK/DoOLBS6L96t7TmZ2Dqe/F6PiHM/eSxxTuhQGmwoCNMeGJiyZfcXEiSLdS5aANf2UNUh6JolYERc/pRXleRygAX+OSJL38iKGLqH1Obl4Lrw0RxSjfiG2iHG8KrPkIPIQhxLReA0v87FaZPs6TGGHZKBRJCEp+g9QzziiH9kc/Oif6tlljfeXeNpuntiWj3ON/PTNKoAZ+plT4CON+Ky0GqLLaqKAvZkwS0VbegPVvs7aeGYf0vuqmZXmCcjlT12t+r7DtjGmyPMSuCsqihOl67r1dLTTZOvxFTT10jTsXItup1JKvHeQCoDdgco+j6GpW+K4c/pmeM8wykbeRO8iBD0Ui+BUioBl X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b6c566bb-2a46-460e-1816-08dcec31ab15 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Oct 2024 09:22:05.0251 (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: AS8PR03MB6949 X-Patchwork-Delegate: bpf@iogearbox.net bpf_task_from_pid() that currently exists looks up the struct task_struct corresponding to the pid in the root pid namespace (init_pid_ns). This patch adds bpf_task_from_vpid() which looks up the struct task_struct corresponding to vpid in the pid namespace of the current process. This is useful for getting information about other processes in the same pid namespace. Signed-off-by: Juntong Deng --- kernel/bpf/helpers.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 4053f279ed4c..e977c12d60e1 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2521,6 +2521,25 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) return p; } +/** + * bpf_task_from_vpid - Find a struct task_struct from its vpid by looking it up + * in the pid namespace of the current task. If a task is returned, it must + * either be stored in a map, or released with bpf_task_release(). + * @vpid: The vpid of the task being looked up. + */ +__bpf_kfunc struct task_struct *bpf_task_from_vpid(s32 vpid) +{ + struct task_struct *p; + + rcu_read_lock(); + p = find_task_by_vpid(vpid); + if (p) + p = bpf_task_acquire(p); + rcu_read_unlock(); + + return p; +} + /** * bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data. * @p: The dynptr whose data slice to retrieve @@ -3034,6 +3053,7 @@ BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU) BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) BTF_KFUNCS_END(generic_btf_ids) From patchwork Mon Oct 14 09:25:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13834510 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 94A5E14D428; Mon, 14 Oct 2024 09:27:22 +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=1728898044; cv=fail; b=W9EznRKMS+g61OWMOI3yK8hcldFGlg3EcpRPhIaRFfiI3yEaZRa1dKpL+Sm8llidpEThYRreT56VbkvowdMl/vgWjTjxHWva4o+/ikxhrbxE6ABkEN05hRresuwDKPNI035Muh/KyilAzmaQq05dvsanKqFpjFfqb20BGThCFE0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728898044; c=relaxed/simple; bh=dDEQw/DoFpjIJd6isXOsP+OEkdAkWaVzlNrDBWs6VnQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ZrGnEHjy/CasemifhlG7+1KVJF4GB65nOK9WJGxvtGYW4OEt9ZzKS2HI9IqLoE1iqliQWFm3wvulCBgCNc7XC3MZiBfY8V9mS5BZrb7qWAI947b7rt+e3GIxSf8Tdli7mchYAiwTOUWDY5ffzZ5UuQuYsdwnJq4pZzgGtAF3zZY= 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=I4GtDD6g; 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="I4GtDD6g" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=s0UyM/2sVt545MzDdhgh4MzJxkGjIIlox6wEFrvzY/1oruZWiuE4hzd3tppjtKi14XnZ01KEtnlr85+sYAo+cOMosWiCOiYQp0A6Zs5YQ14qNVr9XNYYANUgO+ggY1vEBJ6PNS7TfGyH6ZUNb5Ki8O3uAZdoSz13omxPaaWu7pkMKl8T41HVj5SBkkL9TDfldDROd7XEoM0Q1OFCX/gHcDbeSOgxQvX1AZ7yOWMyUBJ5DEXpd/bcQDjYX/7rLE6rUkHEStCLJxaLN5GgaF6aZrCZKB4xS4asi1rfs5qeIT0psNK5IOqbCXRzAuL9/MLgUjr1ncZDWlI88cH0kzq3IQ== 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=CqBK9SQsNWOBtMIult+DaR/J5AP3PER2JbTGX217M3c=; b=r7XD7H+ZlGNDO4N/wtUw8Iuspy3ASkw4t6z/dfmj4oHQyqkMeaYx4mvyU1+N7rtvklsCkniWdf8oSdh5AMuVIBNXyNrYt2LUMMUpViWdoSQpIzIr7/c2LSYl+WaE+qe7f64KEgZLOfbJa0R4MzgjE2P1bJWOn4ePIVtXN2dDSqcGVBwd0FQnzVOTNjvCVPhPYnlYa024KNqVO6a08jYe2lBIRstUggvglFj/qmqom2r3dj/g2nuARZiqirwfJB0+HShlAgP8eVvs7bNX2qYCsng4vX1RuLQqrsxM3Vr8bj5H1xHo1trQo/v8EIAiMKB/Q6TG3VQdOQobabY2RqVWJA== 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=CqBK9SQsNWOBtMIult+DaR/J5AP3PER2JbTGX217M3c=; b=I4GtDD6gE1Qv3nPPOS4KHSoPKz1nQE2CWV6JcSs9VUdTZlSQv5l/ynIV5QQ6/B5H7R6Uf3hHTB9UYYxqEjAaTVgQPsQA8nC5AkuHaL9hKI6G+D9vBD/GJg+90pMdOejZproTCmH84RrDtNO8MyN/+c1vtxsoknHnbLbFe9Tw0ZwRt+DMgtMSsXXZL0BF9qNb4CgSRIbexGJJanMDqBXTYMl1vww/gtejTTDcdEvUaUWZ3umnEiZGEyLAuYkB9D7uGbUHXUwPm6A7AgGwFK2/bg6Mhclb+DLhsCxGoogQIxNo4BQdUghsnJV5UtqRDePPz6ZTuUhyV1oqvszCaJB6Aw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AM9PR03MB7060.eurprd03.prod.outlook.com (2603:10a6:20b:2d9::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.18; Mon, 14 Oct 2024 09:27:20 +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.8048.017; Mon, 14 Oct 2024 09:27:20 +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 2/2] selftests/bpf: Add tests for bpf_task_from_vpid() kfunc Date: Mon, 14 Oct 2024 10:25:53 +0100 Message-ID: X-Mailer: git-send-email 2.39.5 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0150.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2c7::10) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20241014092553.16330-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_|AM9PR03MB7060:EE_ X-MS-Office365-Filtering-Correlation-Id: 90940b97-0233-48fd-68b3-08dcec3266d5 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|5072599009|15080799006|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: ACHKZj0hcDTgRI/hIV12z6+OaUmJlkWzUi+SimzaAW5qnY7vr4FyQcprASP7BMT7MUIMcoFUxTdkMiJdUDFOBn6dVR1QC7TKFKMaqCi4ZW6dO4SemWkj/zah9cr/7I9BQnxIDV/Tew0vAh8QRlCYZTlOl6hDqNlPjpH1mWo7uMaSelzUKXeH7EZNMhSWmuw0/S234M5TC7eFIGPUWuB/JnvkbQ0rtSNroSHEZB2QWBGoie88F5xw04pbMpobiyVeDueauPg97fHWoFMsqcCPRzGhfuBvobQVps13gdguVlFwVkXlQkVDTjiI6smIsTWTZpnb8xKpvw7PVKpo56IfmuUYAqueQRPZX4VMFpIYREAh2QTxpxjjpZvtA6NfmEMayTVpHkR4lkcZ+Wf2ALTOytjMJZRbUGw7gE9bZ7ggWTcAfzmSZzK2Xj65zWZ6O9qI3I2lzRx5FN07PYDNl9lP7kBCaP/CXu7DfOvZmTzDEo4LSN/lZKRjlPLWa15d9eoUSzahKHiMehWJEqLOEGrvacNUuBDX4rynQr7+z5JSMEK9rfh0Oww4Hk7YazsXFmJnELjr7n9qQKFm3QTF9748CjksYxrnyiWM4oEf5AmB91R2AqYxUSKYiwd1Sah7lzs4q1lXd8DtD8h587n3iOufTVpm+r8aXvdsyLhqC5U/Fxz+3pfncTVPADRgaUK1KJT4AuqhdnNcHE4/N5JcZQffmw== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1uvlHQbj6dLH43VliBjRiJhrNy8lrgvCUQJcD64ZxE0SrnMign+UN7gxFPuA8z+EpUFvf1MOdb/V4lmr6txCb2GUMtT+GExCK8GR4ke8VTXZJfZT3EjtpC8ctnl+l8cf/WezvKazb6huXIckOhb5rEq2Cn3ta6I76P5i057yajvXNp28wrAMalST5CjQRiiFKtF8wy1OaPHP8E72k16F7Vr/X+azHJEgaNuWYIM+oUVCZz56vuMoDxRBGQQXJ32msNFK62/uWFf4ClGBS3uD3wiPKQCYFDxgU/HXj6kVhUtLmtudAvB0pjH2QQJdvhjZcUJ9gtf2vF0RHL75GAtIgFi/QyxmsfkBatjeVUIMfkrbVwfAXcVsXK9T3E5w1N609K6aR3aCq61RUQ9camiYz64jUy/BeVZqYBWlWz7gPnA5W3/cfP24JMt1IcNN0x2hIpiCJjMpv1YDYqRkaEuv/wKsRvfcEaHa7CLtPo+Kpt0/2FO9QIK8EQkR2GIAu+tKulbGxMBU/xOqfdGTNfWXPhe147e1bcvd2/Ef3NuW1DJKjAH2eag1/oEwyaHxDJfC0ILMLvU7Y+523BalAT+MonfPVs8lNE6O8bl01wjuOjZZroYGswZYYCtfzs1sePhPqquZ+JxOWtZEQGOM2VEtJd+YhRHfKMwFn5+IiZRkcYuk/HAy63NfCNlzOfzX5Q3AbbbNPHFH6yjrabnMNppjk+FE3K1QMEYGpALDllkxadOJ8gEvpLe852giWDNqWMzIPbi3hamcKchT8Im+OO8jl2WZvNN33rLcHvP3XqB7RBu8B2MJLeHVskTjGSMWr3gG7vZpspcHnilhYouv7LViWg0P0kPbo+WjOkpSFaLPmtfUNi99HdI22dKXAslwoWFjy6if75mZD6sGh02fIs8nUK50/gHLONuqE1V4fbplOf28mGysvfUCdd97f6h0WqJT39zGCSEydsz43jHFdIAnEEGVrZH6gaYaAb0OgpTZpdsTI9ppZgoekWl2MQZttAA8koUwpeJRn09ABV+nU43ywxopvXxx6I/GQnHgE3rcvJvL7KVDxPUsGKd1ZoW77kYWk7iBUg96Io459X/A25vDyS27jVfRV8OfumixIs3DZV5tmdhxFjiNqGHKQv16jTddsJrn5NxrLhdkmFGUmMA7OQIjY0sTvNJ46Uq5Mz6bs+iRx8TaYvlzd4s2zJqVa3jtwsMsdUjHJG03DnDXRptpk53xkk26fw4+hPm491aLHYEq0ZG5UcvL61jM1hFDFdra X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 90940b97-0233-48fd-68b3-08dcec3266d5 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Oct 2024 09:27:19.9980 (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: AM9PR03MB7060 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds test cases for bpf_task_from_vpid() kfunc. task_kfunc_from_vpid_no_null_check is used to test the case where the return value is not checked for NULL pointer. test_task_from_vpid_current is used to test obtaining the struct task_struct of the process in the pid namespace based on vpid. test_task_from_vpid_invalid is used to test the case of invalid vpid. test_task_from_vpid_current and test_task_from_vpid_invalid will run in the new namespace. Signed-off-by: Juntong Deng --- .../selftests/bpf/prog_tests/task_kfunc.c | 80 +++++++++++++++++++ .../selftests/bpf/progs/task_kfunc_common.h | 1 + .../selftests/bpf/progs/task_kfunc_failure.c | 14 ++++ .../selftests/bpf/progs/task_kfunc_success.c | 51 ++++++++++++ 4 files changed, 146 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/task_kfunc.c b/tools/testing/selftests/bpf/prog_tests/task_kfunc.c index d4579f735398..83b90335967a 100644 --- a/tools/testing/selftests/bpf/prog_tests/task_kfunc.c +++ b/tools/testing/selftests/bpf/prog_tests/task_kfunc.c @@ -68,6 +68,74 @@ static void run_success_test(const char *prog_name) task_kfunc_success__destroy(skel); } +static int run_vpid_test(void *prog_name) +{ + struct task_kfunc_success *skel; + struct bpf_program *prog; + int prog_fd, err = 0; + + if (getpid() != 1) + return 1; + + skel = open_load_task_kfunc_skel(); + if (!skel) + return 2; + + if (skel->bss->err) { + err = 3; + goto cleanup; + } + + prog = bpf_object__find_program_by_name(skel->obj, prog_name); + if (!prog) { + err = 4; + goto cleanup; + } + + prog_fd = bpf_program__fd(prog); + if (prog_fd < 0) { + err = 5; + goto cleanup; + } + + if (bpf_prog_test_run_opts(prog_fd, NULL)) { + err = 6; + goto cleanup; + } + + if (skel->bss->err) + err = 7 + skel->bss->err; +cleanup: + task_kfunc_success__destroy(skel); + return err; +} + +static void run_vpid_success_test(const char *prog_name) +{ + const int stack_size = 1024 * 1024; + int child_pid, wstatus; + char *stack; + + stack = (char *)malloc(stack_size); + if (!ASSERT_OK_PTR(stack, "clone_stack")) + return; + + child_pid = clone(run_vpid_test, stack + stack_size, + CLONE_NEWPID | SIGCHLD, (void *)prog_name); + if (!ASSERT_GT(child_pid, -1, "child_pid")) + goto cleanup; + + if (!ASSERT_GT(waitpid(child_pid, &wstatus, 0), -1, "waitpid")) + goto cleanup; + + if (WEXITSTATUS(wstatus) > 7) + ASSERT_OK(WEXITSTATUS(wstatus) - 7, "vpid_test_failure"); + else + ASSERT_OK(WEXITSTATUS(wstatus), "run_vpid_test_err"); +cleanup: + free(stack); +} + static const char * const success_tests[] = { "test_task_acquire_release_argument", "test_task_acquire_release_current", @@ -83,6 +151,11 @@ static const char * const success_tests[] = { "test_task_kfunc_flavor_relo_not_found", }; +static const char * const vpid_success_tests[] = { + "test_task_from_vpid_current", + "test_task_from_vpid_invalid", +}; + void test_task_kfunc(void) { int i; @@ -94,5 +167,12 @@ void test_task_kfunc(void) run_success_test(success_tests[i]); } + for (i = 0; i < ARRAY_SIZE(vpid_success_tests); i++) { + if (!test__start_subtest(vpid_success_tests[i])) + continue; + + run_vpid_success_test(vpid_success_tests[i]); + } + RUN_TESTS(task_kfunc_failure); } diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_common.h b/tools/testing/selftests/bpf/progs/task_kfunc_common.h index 6720c4b5be41..e9c4fea7a4bb 100644 --- a/tools/testing/selftests/bpf/progs/task_kfunc_common.h +++ b/tools/testing/selftests/bpf/progs/task_kfunc_common.h @@ -23,6 +23,7 @@ struct { struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym; void bpf_task_release(struct task_struct *p) __ksym; struct task_struct *bpf_task_from_pid(s32 pid) __ksym; +struct task_struct *bpf_task_from_vpid(s32 vpid) __ksym; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym; diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_failure.c b/tools/testing/selftests/bpf/progs/task_kfunc_failure.c index ad88a3796ddf..4c07ea193f72 100644 --- a/tools/testing/selftests/bpf/progs/task_kfunc_failure.c +++ b/tools/testing/selftests/bpf/progs/task_kfunc_failure.c @@ -247,6 +247,20 @@ int BPF_PROG(task_kfunc_from_pid_no_null_check, struct task_struct *task, u64 cl return 0; } +SEC("tp_btf/task_newtask") +__failure __msg("Possibly NULL pointer passed to trusted arg0") +int BPF_PROG(task_kfunc_from_vpid_no_null_check, struct task_struct *task, u64 clone_flags) +{ + struct task_struct *acquired; + + acquired = bpf_task_from_vpid(task->pid); + + /* Releasing bpf_task_from_vpid() lookup without a NULL check. */ + bpf_task_release(acquired); + + return 0; +} + SEC("lsm/task_free") __failure __msg("R1 must be a rcu pointer") int BPF_PROG(task_kfunc_from_lsm_task_free, struct task_struct *task) diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_success.c b/tools/testing/selftests/bpf/progs/task_kfunc_success.c index a55149015063..5fb4fc19d26a 100644 --- a/tools/testing/selftests/bpf/progs/task_kfunc_success.c +++ b/tools/testing/selftests/bpf/progs/task_kfunc_success.c @@ -366,3 +366,54 @@ int BPF_PROG(task_kfunc_acquire_trusted_walked, struct task_struct *task, u64 cl return 0; } + +SEC("syscall") +int test_task_from_vpid_current(const void *ctx) +{ + struct task_struct *current, *v_task; + + v_task = bpf_task_from_vpid(1); + if (!v_task) { + err = 1; + return 0; + } + + current = bpf_get_current_task_btf(); + + /* The current process should be the init process (pid 1) in the new pid namespace. */ + if (current != v_task) + err = 2; + + bpf_task_release(v_task); + return 0; +} + +SEC("syscall") +int test_task_from_vpid_invalid(const void *ctx) +{ + struct task_struct *v_task; + + v_task = bpf_task_from_vpid(-1); + if (v_task) { + err = 1; + goto err; + } + + /* There should be only one process (current process) in the new pid namespace. */ + v_task = bpf_task_from_vpid(2); + if (v_task) { + err = 2; + goto err; + } + + v_task = bpf_task_from_vpid(9999); + if (v_task) { + err = 3; + goto err; + } + + return 0; +err: + bpf_task_release(v_task); + return 0; +}