From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CCB76C4332F for ; Fri, 25 Nov 2022 16:15:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229878AbiKYQPF convert rfc822-to-8bit (ORCPT ); Fri, 25 Nov 2022 11:15:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229564AbiKYQPD (ORCPT ); Fri, 25 Nov 2022 11:15:03 -0500 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A41E24A58C for ; Fri, 25 Nov 2022 08:15:02 -0800 (PST) Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-207-VFIJ4Hx7N3WIl9yXfbkxLg-1; Fri, 25 Nov 2022 16:14:59 +0000 X-MC-Unique: VFIJ4Hx7N3WIl9yXfbkxLg-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.42; Fri, 25 Nov 2022 16:14:58 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.044; Fri, 25 Nov 2022 16:14:58 +0000 From: David Laight To: 'Andy Shevchenko' CC: LKML , Andrew Morton , Steven Rostedt , "'Joe Perches'" , Linus Torvalds Subject: RE: [PATCH 0/1] Slightly relax the type checking done by min() and max(). Thread-Topic: [PATCH 0/1] Slightly relax the type checking done by min() and max(). Thread-Index: AdkA3hAUrkOWi1xYRg2SAA6P778SAwAA2U8AAAAaTjAAATacgAAAJxcA Date: Fri, 25 Nov 2022 16:14:58 +0000 Message-ID: <0b38ced96519407e95962aef2771bbc6@AcuMS.aculab.com> References: <7e594ad64e444d448c747688b8f28249@AcuMS.aculab.com> In-Reply-To: Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: 'Andy Shevchenko' > Sent: 25 November 2022 15:58 > > On Fri, Nov 25, 2022 at 03:27:07PM +0000, David Laight wrote: > > From: Andy Shevchenko > > > Sent: 25 November 2022 15:21 > > > On Fri, Nov 25, 2022 at 03:00:40PM +0000, David Laight wrote: > > > > The min() and max() defines include a type check to avoid the unexpected > > > > behaviour when a negative value is compared against and unsigned value. > > > > However a lot of code hits this check and uses min_t() to avoid the error. > > > > Many of these are just plain wrong. > > > > > > > > Those casting to u8 or u16 are particularly suspect, eg: > > > > drivers/usb/misc/usb251xb.c:528: > > > > hub->max_current_sp = min_t(u8, property_u32 / 2000, 50); > > > > > > I don't buy this. What's exactly wrong with this code? > > > > Consider what happens if propery_u32 is 512000. > > The returned value is 0 not 50. > > I considered that and there are two things to consider on your side: > 1) it's coming from device property; > 2) device property is validated using YAML schema. > > On top of that, the wrong property is on the user. We have a lot of stuff that > user may put wrongly, but it's user's choice. > > Any better example, please? How about: data_size = min_t(u16, buf_size, len); https://elixir.bootlin.com/linux/v6.1-rc6/source/kernel/printk/printk_ringbuffer.c#L1738 Now, maybe, you could claim that buf_size > 64k never happens. But the correct cast here is u32 to match buf_size. len (being u16) will be promoted to int before the compare. Just search the kernel for "min_t(u8," or "min_t(u16," while some might be ok, I really wouldn't want to verify each case. If you look hard enough there are also some: u32_var = min_t(u32, u32_val, u64_val); where the intent is to limit values that might be invalid for u32. I did try compiling a kernel with min_t() defined to be min() (no casts) but there were too many false positives without allowing all unsigned v unsigned compares. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)