Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1255798rdb; Fri, 9 Feb 2024 15:55:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXt9jrN75KkQZKjDLv7loZmNCwVgFOg5y3cvHThGL+2IFnBMP+xWyINnCiNDVAVtCP8egZGwjxq/eCJ6euCWGoghj1VmA3DwDXZjMlwyA== X-Google-Smtp-Source: AGHT+IHab3p7zNRFN3fnCCS5/ZREo/f2PzdmzhhgM+Bu79d3GIlqc4+PuNkik46N9cU98FnjRvUY X-Received: by 2002:a05:620a:19a3:b0:785:8a94:d5be with SMTP id bm35-20020a05620a19a300b007858a94d5bemr977387qkb.28.1707522942931; Fri, 09 Feb 2024 15:55:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707522942; cv=pass; d=google.com; s=arc-20160816; b=kujDvEuQkh+xoW9BeAQV0CzjhcbpsIlRPE66+O+nTZIVcwkV//mzV+D6CNpN5ehurk CeCJ3gBW/q0evR4bNecLjV+rgih3Cok79kzPbbjMA77dY4ZvmqfkTCnMtU/gHzrwcerq qeG0RUVRbB208e/OvR3O0x/SjHsfKNNoWZxi6L83VEMAQ6WJN5H0cPQeNywLPr/2/qhz s/XYT/Kiso4GYQi1UcfI8A03WkcmfI2FheprZUYVPuqH+8ju/jrT75FlJ+ea2YOQ5RPT cAwe76x87+vmV3/GqA2c75B8BO+6z7HXPgSLqYTPUjQjLb70ucUrivoue2eFdniHnO2o 6w1Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=jbhYVJ+YRmdFvd9fVgrgRmb2O4rRA+JPLjEMmqpd5Ak=; fh=XCCDvbEOwISLVItonJMuiaVVKi5u0G8vB8sOXTgnwBU=; b=oNVaWQrs8Z9V8HdEoCFF0STR+q0j5rC3tzCLVK2gA6dpLY8FbyOqY3ZYVGMjMA/TO3 8JpbytwDKOolYJ5d2/DBJQoWcTyuwyQg9vTYeIfALpZKe9cPnGUU1RbMfMCUwGaORmwv kepeAaOi3EObZcOB6WWX0zbJ0aEvXxOsqJrf8f92fe/TWAiVMiwShPdm+tyis1OFEQsb kVmAPeT2ZrwLGLvcOGDG+Q0sibD536lgj/5jlqTdnlsSKzVdbqAIuIHF5fHeLSFdvHpN uG6XArcyqBBGe2MSuvWCgjk1CenKgYp0U+gpGuyaLUlcpZ2fmko6v3lyDOLsgP6aporB 6NOw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Y6A7sSIC; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-60162-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60162-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org X-Forwarded-Encrypted: i=2; AJvYcCUmuV9JCGK5S3Z4EhnwHufYQ/3D2q57I3v2ARtMaAKd9An5oITEwsbsgnHGtL1zQrvqFaFJcQHYmvklVhR8Nq6qS0Xo8kN4eSn6jjL7+A== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d6-20020a05620a136600b007855ef6e1b6si583256qkl.205.2024.02.09.15.55.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 15:55:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60162-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Y6A7sSIC; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-60162-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60162-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id AD5891C24480 for ; Fri, 9 Feb 2024 23:55:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5BAAA38DFB; Fri, 9 Feb 2024 23:55:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Y6A7sSIC" Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F08D325748 for ; Fri, 9 Feb 2024 23:55:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707522937; cv=none; b=UZw1cG780qT9CyNqK2LpCzd4SO5PwrPKTHYzYrPh5hKmSn28tsgt3CXGkf1nU7XGZ1WAleoQuvYBdjlzBgfuSt3iY+xMb28g9WGY6JoOpJ/sMZbjS9YZiY1qXNZp1l0jNoK7xbjNkH9l4jsvu5R4VlxWW4HOLj7bpOAk9ziTLIk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707522937; c=relaxed/simple; bh=fc+hHoZ5CFIKYFO1vI9NXJRErDi6K3hwEsV9omxvjMk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=hx+ka9c5A9OZaGi7InydJIhi0kYE9S4w5z+rYtISyr4F/auqpSVwElv0Vhv4IM8pUACeDHDRirvSxP6l4CKoU7ZD3o7yyR+9x05krD8Kcubl7HET5o5vYpj4PM86a0I2r9ZVTOduwpRwTskdJJgIBj1/oYm9Dz4lpErR5EVFKWo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Y6A7sSIC; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6e0a608a36bso35416b3a.2 for ; Fri, 09 Feb 2024 15:55:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1707522935; x=1708127735; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=jbhYVJ+YRmdFvd9fVgrgRmb2O4rRA+JPLjEMmqpd5Ak=; b=Y6A7sSICgyk+4ycUB24TkDFkGOpwmVfRmw96Ud2BEktO8rKVIgkeG8m7aS6llVsm+E Po8MzOha3KuA1YGCfrkrDoItH43GTkUu7cOd/JBPXmDf7nsphII1ok6qMVg7Hs1IwGok g1V2p5MzPl/AuL3gP/cXY62Z8U1naqNLIskfU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707522935; x=1708127735; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=jbhYVJ+YRmdFvd9fVgrgRmb2O4rRA+JPLjEMmqpd5Ak=; b=buRMYGLpPUhl67dxfTIGXSa1/OtDxDqbafjAIlPcf18c1aI+zVLjBsbMlHGGXsXoag 7l3yF+HKuxr/+5mTbE3+xYVa7lISunGJA0Bg7WFLKxzWrH3hfT4TVxarGOkqO6bvJafX r+y/+0l1f7PMS/JaNRxz2nC7ZCgWUcbUkk7oIZogoP1U66+nNi580bRyQfxEbay8/5Wo mqoIcV15b3pEj0MyL7XGzTSXBkjs/9oLiB7O7KkjJF1kyndyAWV2UHJ3N83mX+NtRalx /1Uacp5BlcfcLf3//TPbvOynqvJfPB5XFg0FQcrQHrxxs7EjZNd2ZDJ1QFAMKhXr+sSO zHqg== X-Gm-Message-State: AOJu0Yy0eHVUrFTKJ54/Fd56qjjyzibNYXy83vxeHbHOoNvRLNkeq2JW zaGcY0rowq9JnsLHEvhwmbEhZI0ojg6NGRh3xi5UA3JoDjy8VhiE5Kxoh/wPwg== X-Received: by 2002:a05:6a00:2f97:b0:6e0:9ce2:b598 with SMTP id fm23-20020a056a002f9700b006e09ce2b598mr945628pfb.31.1707522935228; Fri, 09 Feb 2024 15:55:35 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVh1wpK1Nhm4zpQwXPeZ8biyVHpx2fewntmnKhTIhkvlmlcFkFKGzUQ0MdxSFTJraJqHOlbWa2zoIqhgq5+ix95kF1IGjlWxGi50hnqm5VSoOa4AHkBItwm5pDkxbuCJCqIox6JhUD6OwAKWf0Qo0CoAj8n6EDJt9366gLr9alYwFKllY3QmPVQbtGdvKFpx9ertKflu3XEtEADExQ3MKeKO1IJtkrbtdVfw4OKyCLvRi21XTk7PJgRiReV1Smtg0UaS2vhtfQpCPK3XgKc6AiI6+txEdqGFoYJoVOIy5ew4M9h0xtEJ0GHUw== Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id j25-20020aa783d9000000b006e0826bf44csm1135389pfn.172.2024.02.09.15.55.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 15:55:34 -0800 (PST) Date: Fri, 9 Feb 2024 15:55:33 -0800 From: Kees Cook To: Abhishek Pandit-Subedi Cc: linux-kernel@vger.kernel.org, David.Laight@aculab.com, pmalani@chromium.org, andriy.shevchenko@linux.intel.com, torvalds@linux-foundation.org, Andrew Morton , Herve Codina , "Matthew Wilcox (Oracle)" Subject: Re: [PATCH] minmax: Add notes to min_t and max_t Message-ID: <202402091551.16A4A2A7F@keescook> References: <20240209150657.1.I45addf7579e1233fa97c05ba72120cd1c57b4310@changeid> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240209150657.1.I45addf7579e1233fa97c05ba72120cd1c57b4310@changeid> On Fri, Feb 09, 2024 at 03:07:02PM -0800, Abhishek Pandit-Subedi wrote: > Both min_t and max_t are problematic as they can hide issues when > comparing differently sized types (and especially differently signed > types). Update the comments to nudge users to other options until > there is a better fix for these macros. > > Link: https://lore.kernel.org/all/01e3e09005e9434b8f558a893a47c053@AcuMS.aculab.com/ > Link: https://lore.kernel.org/all/CAHk-=whwEAc22wm8h9FESPB5X+P4bLDgv0erBQMa1buTNQW7tA@mail.gmail.com/ > > Signed-off-by: Abhishek Pandit-Subedi > --- > Andy Shevchenko made me aware of this particular footgun in > https://lore.kernel.org/linux-usb/ZcZ_he1jYx8w57mK@smile.fi.intel.com/. > > While David + others work on the full fix, I'm hoping to apply a > bandaid in the form of comments so the problem doesn't get worse by devs > (**cough** me **cough**) inadvertently doing the wrong thing. I think a better example for the docs would be something like u16 (rather than size_t) which shows very quickly the potential for truncation. See, for example: https://lore.kernel.org/all/20230811054528.never.165-kees@kernel.org/ > > > include/linux/minmax.h | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/include/linux/minmax.h b/include/linux/minmax.h > index 2ec559284a9f..96646f840a1f 100644 > --- a/include/linux/minmax.h > +++ b/include/linux/minmax.h > @@ -154,6 +154,18 @@ > > /** > * min_t - return minimum of two values, using the specified type > + * > + * Note: Downcasting types in this macro can cause incorrect results. Prefer to > + * use min() which does typechecking. > + * > + * Prefer to use clamp if you are trying to compare to size_t. > + * > + * Don't: > + * min_t(size_t, buf_size, sizeof(foobar)) > + * > + * Do: > + * clamp(buf_size, 0, sizeof(foobar)) > + * > * @type: data type to use > * @x: first value > * @y: second value Please keep the types immediately after the definition -- notes can go after. > @@ -162,6 +174,10 @@ > > /** > * max_t - return maximum of two values, using the specified type > + * > + * Note: Downcasting types in this macro can cause incorrect results. Prefer to > + * use max() which does typechecking. > + * > * @type: data type to use > * @x: first value > * @y: second value Same. But yes, I welcome the added comments! :) -- Kees Cook