From patchwork Fri May 17 17:21:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 797712 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05olkn2096.outbound.protection.outlook.com [40.92.91.96]) (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 0583313DBBF; Fri, 17 May 2024 17:22:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.91.96 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715966543; cv=fail; b=BozBxhwBWsVJUROJFshgG+zJaQt15Umrq7iWjscDkLguXmk9ZQOm92A6Xz5SEtN/sYuoPf+ZHdt+NQjnlokw6xQVwhxxFf/15+Wm3e8bDukl1qpHxdwJE6/4B7HrtnEJ4iZeKiuZclpMJWhUTRdDakrGZbIBp/MtRl69ymiQuUE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715966543; c=relaxed/simple; bh=kB3U8m3Re8BlTJpMXSRzGiKYmnKFQfXu+Ge3aVEA/X0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=B/QuG8/QJWSF32VpYN7ZXXHCDOb9cqHuR7Jh5vleShkz4sc8aWxhnmTGLkYDSPoZcUolN+K1YoxxLybi2yYIUw5WZsjfTllgXIC+m1mXkiiXsqHkYeJaA+phx4J6UkOzK93AzjwMEgDqGuME3ACmq+eTYLVoAejYtC6CqafsKC4= 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=LsggTa4C; arc=fail smtp.client-ip=40.92.91.96 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="LsggTa4C" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OBUSbwmEAt2LhedvgLvorDmv+ITPrlUNxxR29kRJlIuCW42z1V2REbHtLm2g4L/ln5BWY05LHdM1u4v6iCQLxaIIzTVkNiyiZccGftv5xFmCFYDBys99N3p6z4MCHaW8mi/M7hob3gOJbEXYPgdCQCcuJWHvKaAj/bNQcWFmaEsEQlrIckZMdacqdO+61QMPdgGFlhGnkYyxjCAM3LVxINgCiPMc64lsE+S9b8qasNS39nfeQKFdET6XWtIhvKjBauFMW+x8jUtdtY+jCPE4i6hvY3xP81MZ5lDETFSeBSuD/3GnPiSowIzQPLiOBCajYwiHGcBi+AwDU5jhEF9fkQ== 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=86j+Z41CMZV6tGeARCPhrhJgbrEp/mNU151SDWbLPpU=; b=X6UgS0jCPBzlH/dGKyflXahFyeIAtR5Rgntj0UiMv0hUK25qBBbKYT1hOFXpAAN9KlMiPCTDZh8Zi6H7AKjQjIXLMJgiOVIKozlYo2WyjvZYJt1/ciDL6jCpmVgOT8mIlwEWkV5M8i0yuwoQoziPjUzeVuchc1q6JJ08ARIuwFLjK73mjyT1OXESebk6xHbBfkeoR14LpnJSijxbrGuOzp9UUKCaS74rRLwbS6VzmbvOEH+Qv2VdFEAokv1IhbxaFJddp1U+kvoRUaScGVw84e8lGSCaaEvuV45u2GTdn0/t2NcCY27gNheEUVWbO2VJ1ecaVvrgZDPWLJ6h4XG+VA== 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=86j+Z41CMZV6tGeARCPhrhJgbrEp/mNU151SDWbLPpU=; b=LsggTa4CkpA86XT9DvsgMuj39GVQEdtCNNfuTEmvyjOXko3UtkVQFbppaIBqZEgCpEUybaEntKXjWoV8bC3fXWOT7Y0tWds5HpSUvY8W6N8Y95NOjG1+0p208ViFl/4oG4blXabNj4ctdE4kKW+cRtSfbyxazXhlUgvX/3wbsWVexXj3QLKBtqRmknolNgkzljImc+4sTlba1wysEPDyuNyyCkOHjUAXsHSctD1EE9XtekIwIVzG4DIJvskNSdRDxTQ3UcT0h7qOK8zjTz0ih9wk8ej/xQ5+0IUZmMA9nWKjEADFwwTKGHeubylxIZCmIBcw4qqUqUt/cCQeJzVODA== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by AM7PR02MB6195.eurprd02.prod.outlook.com (2603:10a6:20b:1ac::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.30; Fri, 17 May 2024 17:22:17 +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; Fri, 17 May 2024 17:22:17 +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 , "Jiri Slaby (SUSE)" , Greg Kroah-Hartman , Geert Uytterhoeven 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 v3 1/2] tty: rfcomm: prefer struct_size over open coded arithmetic Date: Fri, 17 May 2024 19:21:49 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240517172150.5476-1-erick.archer@outlook.com> References: <20240517172150.5476-1-erick.archer@outlook.com> X-TMN: [b5TU28qmp+pS+FOlh/7S+DHVSTaDAXxo] X-ClientProxiedBy: MA2P292CA0028.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::15) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240517172150.5476-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_|AM7PR02MB6195:EE_ X-MS-Office365-Filtering-Correlation-Id: e1d04504-0832-4226-6677-08dc7695e688 X-Microsoft-Antispam: BCL:0; ARA:14566002|461199019|1602099003|3412199016|440099019|1710799017; X-Microsoft-Antispam-Message-Info: /U7KEYlf2eJ5ITIf4YCxhqzXOnzG3WGnsz9N1gM2DYf3SuaaafHiX7XyU4dSyL8cpKAxwsm+4i2S2ZMXQA3lHxH3vdt5i7HvMSlwYvUFMdp1cJ58W9V88emkbd9MwUz3NGQjpugyhySG+SwFlhzUSxqXrlGKNBxHnLgu2ewiVNCsfqJr4bK/uoMdlqlm4cXwGs2fZ5Zs9ApmjkrgDgA793G27yR4wtwE5cCq+GtccwiZ7vluUQYjFhsp15WvrZxybYSJTWGZ9ZjY7wg/EHkEENsw4oYi5mYwLpd6a7B+khu09uxIUs44kN7QsBozp2yegkZ0pnr8yZstP8PfYasx6QhUrfuH1a3KI1NFTGInwo8wyOownYrbG0fo/Tgwg4Tgqh9bSPGn9MQxsZTHFZBSabQ6QC8kOotgmxCR0+BdNu+w3+aNWOwV+Ivs+IWQmNAE9bqjYsiMkPk1rbe27CfYnyYH5TnCDgtNoxP2W213+FzQmPD2hqGNkVfMckF6Y7sH+g7Snzs6QVE17awCAcQvKgTxgoWu5srGFAMcRo5SSOxX9K5SGWIlQKv2ZmJpp3sjdNIEERl46VUVE/DYeFLlVZpt3GS1dPAVnJeOiA7Us4PsWqiXXWVL1Uw65dHvFfcjpc3NzOdfLiHEP8nDZU70FlyI6bBGL6pTr84llH10WnNEN8nz1wlBITeWb4XZfdziHkVoaA1PruhHcWU+ttZ5Fw== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jPjmg+GxcFY28kyjlTMNRe/nKW4lBT8tgAxrTZax9kPg52Dv7SL2+mq4mpsi56QVhN4wdBBxIJK6oXXWxHTLYclnPnKm36tse09J+/lreKIcviHF4JHGZRK8kUDYlTyThd5QLXEhpj8XGcDrv3H6KoutguXWpGr6LGJWKkUlqHVJiN8z8vu1ZqypJO/nI/BlTG0tsGev3L3sfpNNWYYkW10qpxyQQk8aHO3dwsvi83/jjQPyOFsDM312NDJVffjYRFb31r6M9nwii5EtjiCfu80yLRan0dcqXuQZIeJp6xz+9f6N+1afpaZpf1MzP1Yw+ttEXkyU9WDwPbNs9MVVFnimEZ3Icl7YL1/EsYS8+YYfxr2Qnsy6ppDo+W6xq6e2HQOf8eGW3AOpUSf81M+BV8pltVB9FAggN0vc1oyP6diSTYSSANVw9R90x4zMN4+tWoUSRKPHnbxuSj8gemih3GpyCYum/IbTGBEg4kZZ9xbgakmCfe+N6qe6n6TKJ+PbzGNJZAXnc7yUb9qXuqTMl+Rkycl2agmvxzKniJ/79QV4S5yy0PXNjGau/thMLN7Ps6mUXfE/qhXFdJcKhY9TvpWE+ZRYpNZD1Ogn4eHbWsw3rEXFAUxfm5hFhE9vQrBzjdUKfrNdZtAcRVIBcxIU31Q/Fm9pyrLoP6LflycTw86ynH7q70OXILm242uoLJbjHfIojJ3jBiKDpF4S/i3F0Mx05WGSw5Ni+qNXgPHZ2yoOGczc8MUORIOe5zWzxqDQmtoUxJfX7x/85rE7cy50NrZ9fr2DF1DaI0LuzUoyiTWgwxqBXa0CyM+gR5AJqd9i5kSMC06jL+1z1szIQIUyM08U9lsBk6GtErtGCx1+zkmWtao1D1NpkqZYIBw1NfGCLcCKoMeH9QEOOqw5R5EWtCqFfUdB/LZ825BhzG29+Oovpu3p2s8t6Xwu6MkQ4WiQOXsOswTHAcPBMCjAzPxDXzS+BmZubCj8STkixaI6rGeyjl70hUh5AUhQpdAJr7ArqF4MN4HjcT5DrbAfMqGch92rf3aX4UtV4rqbdjU2DSadF9bvr08OxB7CrFPyoKkbm9PMiq7Nr6juLTXR8b7vPSR+RlMch0lB4zyfyyujABR+cBmOecykU9N7f2svzjR98W20NSPzly3vx7oDQ+BDJh4TOs4k6nrLG342VTCP95SxJ9Mi2KWYkcfwDKkiD+TRDFMOpHhnsBxoDTCkC3P77qv3Xv0d+NrggD9gmsgiduTW9W0prnmejaCoCnTsBS9E X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e1d04504-0832-4226-6677-08dc7695e688 X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2024 17:22:17.2382 (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: AM7PR02MB6195 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 rfcomm_dev_list_req" and this structure ends in a flexible array: struct rfcomm_dev_list_req { [...] struct rfcomm_dev_info dev_info[]; }; 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/rfcomm.h | 2 +- net/bluetooth/rfcomm/tty.c | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/include/net/bluetooth/rfcomm.h b/include/net/bluetooth/rfcomm.h index 99d26879b02a..c05882476900 100644 --- a/include/net/bluetooth/rfcomm.h +++ b/include/net/bluetooth/rfcomm.h @@ -355,7 +355,7 @@ struct rfcomm_dev_info { struct rfcomm_dev_list_req { u16 dev_num; - struct rfcomm_dev_info dev_info[]; + struct rfcomm_dev_info dev_info[] __counted_by(dev_num); }; int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg); diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c index 69c75c041fe1..44b781e7569e 100644 --- a/net/bluetooth/rfcomm/tty.c +++ b/net/bluetooth/rfcomm/tty.c @@ -504,7 +504,7 @@ static int rfcomm_get_dev_list(void __user *arg) struct rfcomm_dev *dev; struct rfcomm_dev_list_req *dl; struct rfcomm_dev_info *di; - int n = 0, size, err; + int n = 0, err; u16 dev_num; BT_DBG(""); @@ -515,12 +515,11 @@ static int rfcomm_get_dev_list(void __user *arg) if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di)) return -EINVAL; - size = sizeof(*dl) + dev_num * sizeof(*di); - - dl = kzalloc(size, GFP_KERNEL); + dl = kzalloc(struct_size(dl, dev_info, dev_num), GFP_KERNEL); if (!dl) return -ENOMEM; + dl->dev_num = dev_num; di = dl->dev_info; mutex_lock(&rfcomm_dev_lock); @@ -542,9 +541,7 @@ static int rfcomm_get_dev_list(void __user *arg) mutex_unlock(&rfcomm_dev_lock); dl->dev_num = n; - size = sizeof(*dl) + n * sizeof(*di); - - err = copy_to_user(arg, dl, size); + err = copy_to_user(arg, dl, struct_size(dl, dev_info, n)); kfree(dl); return err ? -EFAULT : 0; From patchwork Fri May 17 17:21:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Erick Archer X-Patchwork-Id: 797487 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05olkn2096.outbound.protection.outlook.com [40.92.91.96]) (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 8A69E13DDDB; Fri, 17 May 2024 17:22:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.91.96 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715966545; cv=fail; b=VSnASxnu8QDGHBY2YrWoff3Xg+sWRMKt1+VZGSwd7QKOvOuu41DmSfJASGxLe994mSKdM6/F/7/rBoPifMcuPcNUUQk49lAQp6QZVuRWPhdIhczCOtZk0+LPCX6t63Y94modKuWcJm11rLc0q7uTt58khi/CdWglmadbTvqjkpI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715966545; c=relaxed/simple; bh=R2g7aB2FQvoAzLDlfjL4We59knASFYxPNbiWCI5FTAM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=saI3OlhHIUQFlpQiHJm3w8GabtJ/mVtUgOvvBEDOWM/C5YmP+0rUOmIhGFx/gWWDfdi7G/5YQxWVs9di+B/aDxp1qLwHRdJArb8t3ES24vzEJbdi6W0nexQA9VnppnkSF3QgfzMDGrXpq6ckuHchOlzqgzo9euW/vvVxAoVZk3g= 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=a6I7+rgG; arc=fail smtp.client-ip=40.92.91.96 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="a6I7+rgG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cHdJhw2kOAtViKbNkQpIANPf6wpCMFlhyIRoI3m9qCcELp7hfHseBdkXQRWNl7yGSub2E+hDDCcYjf50ErmKIUInl5RiKs5lbbdlawulSj8HPHY+11qeNebxuPhChFQ/kCFrk5UI4FI1UbAk7alNLsIFCcera5lH5k8A9VpnnHt5LCEhcSeWbCqIP8DRZ+GBnISDD+LHUa9dGXQptX14vtcL4kvqzdl+OUQDlL/U57MaLAgl2jb08/TfvyEdyWbqRwHDh/Q1RLugZzYacP2KC7Cbb6kYDvPoQ24X/7adPYSeXioEXF9gTQYXhNprciTOor2VDOULha2HnR6d+CmM3A== 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=NdXNwbNaULL/bRsVSHS0cQxwWklP6P8Qao9V20EgFTI=; b=VSw72wymh9GwpQjfnwCl9hcAvB4SXEfRw1G3Zbtl20S/VB3AmxeaAZax5iTjdcqp+xCTpShOrww360ZcpTazDMo8TX+Ejusyprxcf2yJ0yzg7dBwtxz3vVpJ0ymNCgJ9wIs1r3gBUWTF0SE5qUzFUhMIgncnV3sQfjhlFkbJC/m2VgGBYG0bKJFOGMMDfZGRUyMI3GpNJ8QSuGva/gLP+cHPYjirH/pdRJN2xDmqqBHOdB5r+Oz5x0+5vk15FJd65/1nm6V+Ehf4oJqDQzuq6/Fb638rd0AMDhjPsSUr+JpMpEZFzXJkQY2hjKu4F1RRtmC8etHTi+b6gfp3j1ieQg== 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=NdXNwbNaULL/bRsVSHS0cQxwWklP6P8Qao9V20EgFTI=; b=a6I7+rgG9Y1VwI06kSc2TZQBz4Raq12Rk1+gn/CLyDgH5WK73Sdcwr487zQ1yryCPGwM4NCWHewiOjQqhxzWlKOmvLsYAoRzF4MlBpIQu6SD/da1hv8W4+gWwtOd7OttaZ8a3P3bhPbPVigh7x9imwsCtK4X+x+OGO3cCpWXgGYSutxZgCmiIveIt37dL1hAoHuKCn0DvyALLxqdZieT4X2zX6lHzu/CVjWKj+3O4rdWCoemfClzV0LWiiIHhoBhOnoID2/0cJzqmQk+9S/9EaiqNProkd13P7E5hZWz4RBg/UDuG1NKORUY8Ti1jpzNOFNhjwuxihpBBpK0rKgQgg== Received: from AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) by AM7PR02MB6195.eurprd02.prod.outlook.com (2603:10a6:20b:1ac::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.30; Fri, 17 May 2024 17:22:18 +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; Fri, 17 May 2024 17:22:18 +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 , "Jiri Slaby (SUSE)" , Greg Kroah-Hartman , Geert Uytterhoeven 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 v3 2/2] tty: rfcomm: prefer array indexing over pointer arithmetic Date: Fri, 17 May 2024 19:21:50 +0200 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240517172150.5476-1-erick.archer@outlook.com> References: <20240517172150.5476-1-erick.archer@outlook.com> X-TMN: [AyUf7TvpU/T8yh7Z6kMAXUGQltDUUjYT] X-ClientProxiedBy: MA2P292CA0028.ESPP292.PROD.OUTLOOK.COM (2603:10a6:250::15) To AS8PR02MB7237.eurprd02.prod.outlook.com (2603:10a6:20b:3f1::10) X-Microsoft-Original-Message-ID: <20240517172150.5476-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_|AM7PR02MB6195:EE_ X-MS-Office365-Filtering-Correlation-Id: 8631edf8-2f37-448b-dc92-08dc7695e758 X-Microsoft-Antispam: BCL:0; ARA:14566002|461199019|3412199016|440099019|1710799017; X-Microsoft-Antispam-Message-Info: Gqqmtbug8A5m1O38cwGqMZwycpDHjg6WnO8lzrM6adku2vGsxCFt5p1/qqOjZTI5bpsraaIlvhLUPak7WgKWl+E+Fi3k6w59S6O5pdfc2istKFhmBOK4t8btehNU+3aiHLdXnTpPJHjxCbFJXWJ2T4YrZL1WbHgKoYC7M7sQUeM2UJaenZzaFKPJGp8lB9qNfltO3t98JKqrvSZ9vMcI2LuxVbxM9HOUt4wWC1N20dOlVpM2VCToF9BN+bltrULDCPD/Cjmnm6rggePJpx9tgU+kpdyIhDoRloceQMcD0+1L77Eg15eitPZOE4uqoyGJ2u8ZAISeqnUXfUuh864lGIGRtkkeHo9bXceZXG7kOVz5ZR5f7+pC4ElOpdnfFlrTuVNN5ljCVgxKfdvMCYbHuxpaGyLpJbarmrXHPz51rfqdM7DDd4dEVGrcbqIAbFTRysXuiWkFoGUVjGkqGBZGQtzZd1Q473GlJxBMPKtyG/HU8JAngkmBLKdYhB86Gm+xyij5A6cjCH8YyyJyCImin5AizP6oYA/ZSRLUZ2yqdHJpJ+9mxuJ6gxa8pDDayjnrjqnFS6lwSgeUpCNch/k51tiriyqySHvdOUsYCpX2gLnuZvT2hXN7MJ1vX5y7dbu/ X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pWiWxQu8l72kPvWqiweokE6HDiFf/PKZG0O3O3XKKoAyCPOKUMSUOCGZRidQ2PT5GpRZZ+CMtCtCAkzzZC3+olRIZAa0nQx82Y2AoWPZavpxHpMfmperS8wO3SC5pg0r6DKqtGYk8n5fZUFQACZA+CeChBMjlOCXrpKrwXWuK01mIERnpYH24WBQRt63MPboGZJCJe6CFprOZUFor021bNAOoBWHuc2zysB5eyFbxdEPuGMRJUOId5fBCUX2yUMPzieFMacT+HEa8mXqNf9hepmhT3oms2sSrDxo8QHNBneZCLA6SOh/6rPr9G4PCUL0mjKXaOKtVvxGr1uWYf4FNDCr+ibn4hwYONtlwRJIaYvaWpBmH050oei6lI6DVVEs47/5X0EfVRmK4P1bFmlRbCYhojTZ5mdlj/z9tiOSpp/3WrNm/fPzhmGME9kFVZnsSY2vJRceUCWoTOv5Pias16P0VH+R73gs4uYrBEGv33ZB8c4O5S8XoO9pfdCC6eohLbFBUo+jjm0o2qIt1axEXDKt0Rv2nKhxwVTmOMbZ0lMqJNXpf+pqXpswHM5P+B+vQO0/htD5tiXsJgB023ZIPDVwP5KsF9XssPiXh6R45HCTEagbX41UVXS7ExoYQF76EoKOUYJcylBOq6p33KI6lRxLKKqNRLBq1yn9IW0L1y9Mkr4uvG7N3JgCyoJI2rwZ27rQBcYIEX9zlDWTuiMqICkX2vFdviabO5XEvNqxecW1VlrPGDAcFkXoMsJG84Oez3HsvT6ixzfofRgeZd99sjvhtZVSb9T79vOq9oIa6W6ZaK9UtSVzGUmhtiaZZV5icI8fh35XRk8t4u/zvfQKYESOBIYEv5U6Bv1wUwK+CJN2cF5v1AD3iohRTWIj8r3W/nUijXwY7tWyVOm0r8EpdPtM5N2+33oOpJVw4QpLHnoHFiyDVtodnJAM+gopXYEfJ8KCHoopdGq4AeRR/XOY2GXKoqaulx9JMauEVUnF/qll+greugsXZto+yzFSZasMAzXqlexRfBk80Mqe0uY6pWhys0hLQQHLONL939GVBbh3PbDVWBLTUwwGezA/S52uKV1/BD0gMfkZKt44+GKP4wlwf6yrOOcDDV1AFKII1JFm138fGIAJ5DVpOzw8cN6oBC+2Dj6AJEnyaa6pU2q/s8sObq+b5iP3JCIvOkkdyEfWFVhg+SDbQOWWRwp8HFigAdTg93ClQJf8qm+UfzX1RvsIaBShOEIWAIeOIWHRXrec1apGONEDAH038SJPjoO8 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8631edf8-2f37-448b-dc92-08dc7695e758 X-MS-Exchange-CrossTenant-AuthSource: AS8PR02MB7237.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2024 17:22:18.5226 (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: AM7PR02MB6195 Refactor the list_for_each_entry() loop of rfcomm_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/rfcomm/tty.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c index 44b781e7569e..af80d599c337 100644 --- a/net/bluetooth/rfcomm/tty.c +++ b/net/bluetooth/rfcomm/tty.c @@ -527,12 +527,12 @@ static int rfcomm_get_dev_list(void __user *arg) list_for_each_entry(dev, &rfcomm_dev_list, list) { if (!tty_port_get(&dev->port)) continue; - (di + n)->id = dev->id; - (di + n)->flags = dev->flags; - (di + n)->state = dev->dlc->state; - (di + n)->channel = dev->channel; - bacpy(&(di + n)->src, &dev->src); - bacpy(&(di + n)->dst, &dev->dst); + di[n].id = dev->id; + di[n].flags = dev->flags; + di[n].state = dev->dlc->state; + di[n].channel = dev->channel; + bacpy(&di[n].src, &dev->src); + bacpy(&di[n].dst, &dev->dst); tty_port_put(&dev->port); if (++n >= dev_num) break;