From patchwork Sat May 18 08:30:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13667517 Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2057.outbound.protection.outlook.com [40.92.90.57]) (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 4B256DF78; Sat, 18 May 2024 08:31:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716021076; cv=fail; b=dSWntf8c/CCpZlGvzZ3HhYDlj6kUUkXmQF8lUZSIL9AURa6f1+QcRIbEiCGjwuxWNYrf48L9TsJzKsOIMn3cWHZzAxodIKpKZbvqIBBztvHIblnn/K57t+CT4+qIotFIo3tTeAC/l7WNxbGiCmQ3MZi13h5tMVJankzfR8CpGRE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716021076; c=relaxed/simple; bh=3xwGQAhbXCFkf16Lz+Dc3j9Jmk0bi1L7YBYfT9VOV+I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=eNLkqW4oPF/MeVeUdj6OjVTpInczErNx4RWJK+zP7h8TxV8EKhrGh32GCbh21z2IOm/gCrWYIgVywK14Tw848u/w054JO/nYEbvS+N+q1GEWOFwd2OlIQDsL9o61123ZMIyH4vXuAXomE9+gPSc5n3iE9t4Zs7KOnZJpXFLoTgI= 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=kfAbA9P0; arc=fail smtp.client-ip=40.92.90.57 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="kfAbA9P0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MibeWCed4wkxHJvJKHGY4I3nJj8xaSkW0wcoOdQcpcMFoRHtVGoPyJnjbnqS6EX8Sve1m1tdARsgQ7HyM+GDHpC73Xie0PyU/MsHqxV7L99FTMT/4schx8cBfl+2d7bqT1YC2aV05koaOWSltQlL3XbnYyGCE5lM+yK8Gz6JrAYxMcyU3Ilf6pq0WAYRCaYZ39oe4ow5NedP65sLxYScDytWQUOqMBzF3HRqHKborh6dnzQglFU9uXQCbpYQm8RqYaKji9kIes988DaPEL27vJTvMleM58lphMpypvsIfhrmar3KTEMxkgewFp9rfSH8pLLPo6MxJKm3QDWuuL8iyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=KcAfl55zXsrXnU4v2nvZBkiomyDZmRSXbll/OJENccs=; b=kKpKHWn5e/cwW8hbEpOASqzwr6FIzork0kSNiPxreffRCshpJsEZTMdrpxFg342n8I+mcVvUAXk5CD/c3IE/oNDgKoDbz0X6rvF0730QA3jLRu+oUPvbt4j5406EKLHUnzsw+imxA24FalKkJERBWQ0kBQmyJEwvJeCZqMEZ50/7syD0ioCsSqzuqIyZaCe/z9h+fRmCfXS3VdeR+MfaT9OELyNGn3o4imYVZBwuzwFLErzTtKXNNlnMTw5DbF7e1oof/URq+7dq9b2qQ3EBEVLOmvQppe5q4VgJc6HOoqjUwK5oYUt9sz4t0G8FWxZDYqPOlf9mSAz5Y2+ZaDkCNg== 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=KcAfl55zXsrXnU4v2nvZBkiomyDZmRSXbll/OJENccs=; b=kfAbA9P0GO1XxqyVjtpvCipkB08ttlISzzVV4ItbzCHqNHkTZOVmbJDtDSUc171ggR1Qxtom7jtQTbMaLiye0meUzhbUgPs9hJbTukOkOYXlzItpwsMwPilHy16RruHVWlNmom42xe+5WTS8ef+Cf2MK1A9NccMTFzbQ43HIHke+8EhJE7oZSEkqDS9uXiN3UDukbKbknQ1UCyxnq9bNesmoEb4vKdIeWnl3aWzhN5YeFD4dOkgQnRPs+rUvZmAhNkfvidDc+tJfhVOM3QDEurPPud/e+mP+YmNs3/iuipUqmep8aikyv9yXVU2osxbkB+ygHX+OjpvVFHm43+ZCDQ== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by GV2PR02MB8723.eurprd02.prod.outlook.com (2603:10a6:150:7c::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.31; Sat, 18 May 2024 08:31:08 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658%5]) with mapi id 15.20.7587.028; Sat, 18 May 2024 08:31:08 +0000 From: Erick Archer To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt Cc: Erick Archer , linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v2 1/2] Bluetooth: hci_core: Prefer struct_size over open coded arithmetic Date: Sat, 18 May 2024 10:30:38 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240518083039.7202-1-erick.archer@outlook.com> References: <20240518083039.7202-1-erick.archer@outlook.com> X-TMN: [w6RjX6grYCE1Xp2QGP5TfHCf/E2U24ak] X-ClientProxiedBy: MA2P292CA0026.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::12) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240518083039.7202-2-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-bluetooth@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: AS8PR02MB7237:EE_|GV2PR02MB8723:EE_ X-MS-Office365-Filtering-Correlation-Id: eb9a60ad-312a-40aa-9476-08dc7714dd99 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199019|3412199016|440099019|1602099003|1710799017; X-Microsoft-Antispam-Message-Info: reBDTKMAWzcD+eqd8XvyWiyEPy2qDL9kW/jr75QZrE7q6EG4S9K6+cbUMndwYSLDQGOnPc/WMr6XDQJKKbQmZnfaEW/KbfLO6TTf9DpsWenuO0dxamBueDERz4exhSLDu/Iwrthm6w2fRg/iuQ5bGm2IgIeoW+NMcGAVPHjkaJ1rU0I9fePjvz64Yo6qF1T4vsgwF/bTYGUc9uTLnln/kZPKiggQMqjOhTq9gwK667zAXlTdkgHtPgxblj7s3jiHFAbVv4gxQnXlxLyuo0E4YX/7KkQNyN0HllduaNyXscZTxgrj2JafcG1FAZdLnbhaN75LuZBOr666QHHomUJ38lGqdnxR66BzjnkBq3/X6zWlzLuBg54d8SqD7b620RZ2Ca3y9JFb8E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LzmyMaILKFhENHPUVTf/dDl64G+6NNtAMkwCYKjS15dYZEOJlirlY+FKar2mtXeGUHPzlq11f6DqLME/yXPM3LLw2D2K9bGBI6MhUm9/JC+8xzv/R2W4pY4OTfjDz/E+EXE37phJgbWqjvPiIJkxzqa0DQeRyqICXob/nwTkhJEMa8Tfdu59mca0eC/s8iLsUFZvnjuQx901zhbqFGtsR8LhmYFZ5lxPaUyp8wYFZp/bb/H4LGkIlNN8dzvBOBhOADCFhwRE7RnQxRxtLCrItlYLkhK1q+L8boVntXK6R3ca3pxbGpCxq0M7kwiNrFgm0Ik/hiGszlmO8mDj4ah5GVShUVLZ4o3pvlBN5JyQnl302NKrwrXodmF8CwKodtWZnYsOQMSOGIgRKX06tmSU5u5GMJPzKJQ4oCkMC0Xk/ILsJEzV0t1CVQAg6ISOUO2JEKIYhkkzwZWcvc82qihupSoxMBdVBn+6JKmadti3vQVnuGRO9aDcayX1Ymqz+ZBDpEs8DN31eyxqyaN+NOopi1p2PhA1wcQnBFbAAfgiBIC5K/0AMfOhrPjt2FTUrOLsp7Ee186eqM6rD0K X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: eb9a60ad-312a-40aa-9476-08dc7714dd99 X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 May 2024 08:31:08.2241 (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: GV2PR02MB8723 This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "dl" variable is a pointer to "struct hci_dev_list_req" and this structure ends in a flexible array: struct hci_dev_list_req { [...] struct hci_dev_req dev_req[]; /* hci_dev_req structures */ }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + count * size" in the kzalloc() and copy_to_user() functions. At the same time, prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). In this case, it is important to note that the logic needs a little refactoring to ensure that the "dev_num" member is initialized before the first access to the flex array. Specifically, add the assignment before the list_for_each_entry() loop. Also remove the "size" variable as it is no longer needed. This way, the code is more readable and safer. This code was detected with the help of Coccinelle, and audited and modified manually. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] Link: https://github.com/KSPP/linux/issues/160 [2] Reviewed-by: Kees Cook Signed-off-by: Erick Archer --- include/net/bluetooth/hci_sock.h | 2 +- net/bluetooth/hci_core.c | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/include/net/bluetooth/hci_sock.h b/include/net/bluetooth/hci_sock.h index 9949870f7d78..13e8cd4414a1 100644 --- a/include/net/bluetooth/hci_sock.h +++ b/include/net/bluetooth/hci_sock.h @@ -144,7 +144,7 @@ struct hci_dev_req { struct hci_dev_list_req { __u16 dev_num; - struct hci_dev_req dev_req[]; /* hci_dev_req structures */ + struct hci_dev_req dev_req[] __counted_by(dev_num); }; struct hci_conn_list_req { diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index dd3b0f501018..81fe0958056d 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -837,7 +837,7 @@ int hci_get_dev_list(void __user *arg) struct hci_dev *hdev; struct hci_dev_list_req *dl; struct hci_dev_req *dr; - int n = 0, size, err; + int n = 0, err; __u16 dev_num; if (get_user(dev_num, (__u16 __user *) arg)) @@ -846,12 +846,11 @@ int hci_get_dev_list(void __user *arg) if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr)) return -EINVAL; - size = sizeof(*dl) + dev_num * sizeof(*dr); - - dl = kzalloc(size, GFP_KERNEL); + dl = kzalloc(struct_size(dl, dev_req, dev_num), GFP_KERNEL); if (!dl) return -ENOMEM; + dl->dev_num = dev_num; dr = dl->dev_req; read_lock(&hci_dev_list_lock); @@ -874,9 +873,7 @@ int hci_get_dev_list(void __user *arg) read_unlock(&hci_dev_list_lock); dl->dev_num = n; - size = sizeof(*dl) + n * sizeof(*dr); - - err = copy_to_user(arg, dl, size); + err = copy_to_user(arg, dl, struct_size(dl, dev_req, n)); kfree(dl); return err ? -EFAULT : 0; From patchwork Sat May 18 08:30:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 13667518 Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05olkn2057.outbound.protection.outlook.com [40.92.90.57]) (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 685B317BA9; Sat, 18 May 2024 08:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.90.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716021078; cv=fail; b=GIW2ra2w04yGU1vAeav7XffG1itrUDB7Pep24e/EHIcUJe/93T3R/2HLGGKuubbUOYpeiLpfWmhTC0t7GByOvTWw7NW23RfHYEqCKzn67qxYxvzHZCJm6mPaGMf7iQOwhtJMINmZRnA8pdTStqnTcJebwmwUgE8pesfOJSRGIkY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716021078; c=relaxed/simple; bh=BfcUnnoGavhSUjWNm6uA9jJ5/41YkMxKsdVPjLPCZUc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=FUAYrOWeOaw+jWdf81taUHFJXnFOTj5JKCut2PZdydKIesV+4wgl6PNM6eUtpEJihj35ClCiNib/mb1iJcsBr7bknthxILPoSdDJ+JSQ0+fhZid/eixFY66X7Y9FFcQ2u4nx7Lh7Q1IR8O1Ood1O6ao39llpq9fhLsK54gxgfZg= 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=rLZrLKbk; arc=fail smtp.client-ip=40.92.90.57 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="rLZrLKbk" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DL8tE5YZ6lmdSwBqIB9IAb97DSoy2DmhZ2+cnNGkGf1zQf1REBd9yXodPAg9/kBXsMkGhp0T1QcuaBTPcbSBnWqNwrc1bZHBzJpJ2MRdaIa3g1VZI1GqaLpkgmQRyFKz0xMA5JiWsQc+nElgnK/WCLOH6c7NoynHdyVA+ZvABKH9ih8+T7wTI0RTuAKAJzkVRadmK+9V81AkiDUbOcQxK/9l9AWW2op8GKYhiPCQTr69OYb4vTgUlFXLo084c39+goQihNB6Ltyw0G9kaWKU81mhoJhDD4ir7WXt62Nr14Dl+y5B/BA3o7OTx+Pw1ClwsqZJt5CPAHa2bSB5rjik/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=daC3THgyOiQzGJtJX2KuIaT/NeDMy0RudLFDY/u4FOQ=; b=L4nhkD7MliCGsUQ6xGrkCURpKKONFA0VbSnDkIYzvqPxtZvZWHGjtK6kCq14OzPgaf586x243Otg1XnPNBVp2xKkoTDYumW+7bRLLBv37k/tVRwmkhSI/6ZhBuzJtyn66YfWpTzXCbuIndsc20htuxJZ3sIX+lXHUknr/tcaTgHQSdAfHcVdZe7WTP6YM08TtdyirbZJC/CkeUv17dDbxTdYozHs+YaBV3/GK1l+2Z68blHZooeYrS7cHwZ9djYd68L3Ko23anl7RPr07NO/GdKUehlEmf8/xxRZZpSihXSVDfenH+LObyelsYgksffWd8rN7mO/4gzOC5nQpXh+/A== 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=daC3THgyOiQzGJtJX2KuIaT/NeDMy0RudLFDY/u4FOQ=; b=rLZrLKbkgiC7QmEIgpBTpHgrR4+oXF7uMLqQynu9HuK6QvQNvwVhW5j82K1ivgWrqZsqQKsymjFsQhXmavmn7AiuNU939QDMXMdwQropKcbWfBH9IeKIYsRgzkGdd+SH5RjglpbXq58A/hpap6dzhsDg4hl7iXjpyJnleePbbgtj2/hOBKsmxHOrb64kd/tqStu1VxPfIVtkQToH31xscMysNChFpGCmvTWx6f6t2/UKHPzjKIrvKpLj2j3XVTIL/P7F7mKBZk6idmRsPZ2pVdZ6qCDexXtrUesMMx3PXazgSU77WRKsRJJXchZZOjxEkv3BVSf9RMbmDvqPyqLbGA== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by GV2PR02MB8723.eurprd02.prod.outlook.com (2603:10a6:150:7c::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.31; Sat, 18 May 2024 08:31:09 +0000 Received: from AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658]) by AS8PR02MB7237.eurprd02.prod.outlook.com ([fe80::409b:1407:979b:f658%5]) with mapi id 15.20.7587.028; Sat, 18 May 2024 08:31:09 +0000 From: Erick Archer To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Kees Cook , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt Cc: Erick Archer , linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v2 2/2] Bluetooth: hci_core: Prefer array indexing over pointer arithmetic Date: Sat, 18 May 2024 10:30:39 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240518083039.7202-1-erick.archer@outlook.com> References: <20240518083039.7202-1-erick.archer@outlook.com> X-TMN: [K7kVgqjXF+OcZcPiLQyP3yju7H8n/SLb] X-ClientProxiedBy: MA2P292CA0026.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::12) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240518083039.7202-3-erick.archer@outlook.com> Precedence: bulk X-Mailing-List: linux-bluetooth@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: AS8PR02MB7237:EE_|GV2PR02MB8723:EE_ X-MS-Office365-Filtering-Correlation-Id: 2e869dab-8e2c-4783-1da5-08dc7714de47 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199019|3412199016|440099019|1710799017; X-Microsoft-Antispam-Message-Info: o9/tebtvu7Ue7q3F+507HAQkdZneZz0j5KyvzNedAnAy1dMq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ThrcJyem97AWsREHknyRTHzYVFvkunhtjNsouS7EfVB2amgmHNbnoXOmFPg+E8ezEsZJDFi8a1gJWyeknf/x5VmCd+bRQZbbAgqglKeQatYLtt8Hog4jwQUrGJNIHVkMQgFgX7APw9J1oW5I1gNO9IsY+V1zm4tZ7UoZGmKGtzAub7JEBNOG0T2osYig8PkphAhDpIbfLHPr11cskNHFTWyFFhi4F8uDYYf99ENzD4b2NtbIBLaHM+ofKxOIWRkO1FvUKaF15hWnkByWOHijScPabnWbgeJ0/aFLXISxhjZJMq/nKQ8unKZch/vWqRXeBBO+dzbvM0dSl2floYfeykU9dPztJw7tggSocGpV4YbNtVNzla2n9JSASO9p4AgIbPS6aYL8ghvbuRcr4tixe1T0F1Ou+uU9cP43J1f5EboFC6XHDFdaK79PWEuSg3FjKQWP/T83w93hM7QPyq/UFjiQKbRdRj8W9DWZmbTA0Aw3t1FE0COzYPis5jrikMs9wgkz8UxJhOGXV0iz8AINKU6CfqizM3BBx3J4dnDVaa7X0E5aMF95T7W9ZoPliVZ9bAWHPHgLrFqTK4Omh05jGW1Oml0kfY2WqjF/GeKFLIDZJNwq8B3D2EegyoVR5uGCgBg1IIdEM7kk9f7D/tI9HKKZFMn7RnRhVdpJgyce4Vg9Z2T5uRWXYkncu8pOl+F1s6eSEB7Fd56fZa3voe9wlZjXVHDrR6iBcNUyviJSp+2bpnupPb7XcJH745Z8qIXZuYjd2JQGI6DChRMD+pzhOO02hvkZZVJfKvwieea0OQUQphzsPwhpl6q25sSeoGncpNuXlPop4Q9GfSnxVuJm X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2e869dab-8e2c-4783-1da5-08dc7714de47 X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 May 2024 08:31:09.4329 (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: GV2PR02MB8723 Refactor the list_for_each_entry() loop of hci_get_dev_list() function to use array indexing instead of pointer arithmetic. This way, the code is more readable and idiomatic. Reviewed-by: Kees Cook Signed-off-by: Erick Archer --- net/bluetooth/hci_core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 81fe0958056d..b3ee9ff17624 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -864,8 +864,8 @@ int hci_get_dev_list(void __user *arg) if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) flags &= ~BIT(HCI_UP); - (dr + n)->dev_id = hdev->id; - (dr + n)->dev_opt = flags; + dr[n].dev_id = hdev->id; + dr[n].dev_opt = flags; if (++n >= dev_num) break;