Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933771AbeALNpi (ORCPT + 1 other); Fri, 12 Jan 2018 08:45:38 -0500 Received: from mail-db5eur01on0064.outbound.protection.outlook.com ([104.47.2.64]:14982 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933478AbeALNpd (ORCPT ); Fri, 12 Jan 2018 08:45:33 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=ed.blake@sondrel.com; From: Ed Blake To: gregkh@linuxfoundation.org, nunojpg@gmail.com Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, Ed Blake Subject: [PATCH] serial: 8250_dw: Avoid overflow in dw8250_set_termios Date: Fri, 12 Jan 2018 13:45:07 +0000 Message-Id: <20180112134507.9030-1-ed.blake@sondrel.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [195.88.9.101] X-ClientProxiedBy: VI1PR0601CA0030.eurprd06.prod.outlook.com (2603:10a6:800:1e::40) To HE1P191MB0156.EURP191.PROD.OUTLOOK.COM (2603:10a6:3:c6::22) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4172133b-e3b1-470c-cf18-08d559c2be98 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020082)(4652020)(5600026)(4604075)(2017052603307)(7153060)(7193020);SRVR:HE1P191MB0156; X-Microsoft-Exchange-Diagnostics: 1;HE1P191MB0156;3:21P0qp7xfmO8BD3KyTNPpUxJYcpmxNI+UabZMj2DOR/z6tYOBDy3C73sO5KNRPGdPAPsrejDjZzQWyPD9WgHXOZSsvwgCWqmc2XBtsRmAvnvd1jxNjBXIw0ldmkX0Df4eEdV08ra9yXBJ3DIBBXHH2qDfEc5QANikVlcPen4TRMmFHr+SVAOAXxfYZl/cjVUuW6o7m3U21zPNp17GK+bR9fq3SUIN01KwEMBOEWJrvTfcPi+zr4T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Ia79R1PW1tcDsM4VcHcrg3Qe9Zb5FgDTWKEI1C/Gf4+vRxnfqniQ9k8UEG7bo720dnqB+s3rtg6ALg/4vngK6Tg8jlj8MOCSFG/ixNde3m7lvDYxX2XQ7Hb91wqTcVIiYKusahIDEAbnQQWaEBbpumG3aldcbvw1Q7A2epfV0sjZ3uoa22oPRBs1y+2St1PFTHb6n0YNgjTwuBSZiji8+aLy1N4mZm4v5F5cxDvqNpFdnw+HijsqfQL X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(85827821059158); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040470)(2401047)(8121501046)(5005006)(3231023)(944501144)(3002001)(93006095)(93001095)(10201501046)(6041268)(20161123560045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123564045)(6072148)(201708071742011);SRVR:HE1P191MB0156;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:HE1P191MB0156; X-Forefront-PRVS: 0550778858 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(396003)(39840400004)(39380400002)(366004)(376002)(346002)(189003)(199004)(8676002)(53936002)(81156014)(2906002)(5660300001)(97736004)(3846002)(6116002)(1076002)(47776003)(66066001)(305945005)(8936002)(36756003)(7736002)(81166006)(86362001)(6666003)(16586007)(15760500003)(7696005)(6486002)(50466002)(575784001)(48376002)(68736007)(69596002)(386003)(16526018)(53416004)(105586002)(478600001)(39060400002)(52116002)(51416003)(25786009)(316002)(50226002)(107886003)(106356001)(4326008);DIR:OUT;SFP:1101;SCL:1;SRVR:HE1P191MB0156;H:blake-linux.sondrel.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;HE1P191MB0156;23:BNls5vQfXs2dRw9Ns230DtC9wACq6+ekOX7NwaS7b?= =?us-ascii?Q?96VP8eaFlxA93ja6wDFFGR8pUNGX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DicQ3OASRPnQvgskt7LN/EEI/pNdiAThcXjXP9DnIwZAv94MKccKtqdd3Hlz7eX+9jy52yNkpobU8JN1RJwQtXT4DU0JU/JzymnALH7CU7ScMFD7wNaO6W SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: sondrel.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2018 13:45:29.3849 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4172133b-e3b1-470c-cf18-08d559c2be98 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4faa3872-698e-4896-80ec-148b916cb1ba X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1P191MB0156 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: When searching for an achievable input clock rate that is within +/-1.6% of an integer multiple of the target baudx16 rate, there is the potential to overflow the i * rate calculations. For example, on a 32-bit system with a baud rate of 4000000, the i * max_rate calculation will overflow if i reaches 67 without finding an acceptable rate. Fix this by setting the upper boundary of the loop appropriately to avoid overflow. Reported-by: Nuno Goncalves Signed-off-by: Ed Blake --- drivers/tty/serial/8250/8250_dw.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/tty/serial/8250/8250_dw.c b/drivers/tty/serial/8250/8250_dw.c index 5bb0c42c88dd..04b44829f0e3 100644 --- a/drivers/tty/serial/8250/8250_dw.c +++ b/drivers/tty/serial/8250/8250_dw.c @@ -252,7 +252,7 @@ static void dw8250_set_termios(struct uart_port *p, struct ktermios *termios, struct ktermios *old) { unsigned int baud = tty_termios_baud_rate(termios); - unsigned int target_rate, min_rate, max_rate; + unsigned int target_rate, min_rate, max_rate, div_max; struct dw8250_data *d = p->private_data; long rate; int i, ret; @@ -265,12 +265,14 @@ static void dw8250_set_termios(struct uart_port *p, struct ktermios *termios, min_rate = target_rate - (target_rate >> 6); max_rate = target_rate + (target_rate >> 6); - for (i = 1; i <= UART_DIV_MAX; i++) { + /* Avoid overflow */ + div_max = min(UINT_MAX / max_rate, (unsigned int)UART_DIV_MAX); + for (i = 1; i <= div_max; i++) { rate = clk_round_rate(d->clk, i * target_rate); if (rate >= i * min_rate && rate <= i * max_rate) break; } - if (i <= UART_DIV_MAX) { + if (i <= div_max) { clk_disable_unprepare(d->clk); ret = clk_set_rate(d->clk, rate); clk_prepare_enable(d->clk); -- 2.11.0