[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <E1A4953A-8801-48FA-A744-63DA548C5924@chromium.org>
Date: Fri, 03 Dec 2021 08:37:59 -0800
From: Kees Cook <keescook@...omium.org>
To: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
CC: Yury Norov <yury.norov@...il.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>,
Wolfram Sang <wsa+renesas@...g-engineering.com>,
linux-kernel@...r.kernel.org, linux-hardening@...r.kernel.org
Subject: Re: [PATCH] find: Do not read beyond variable boundaries on small sizes
On December 3, 2021 4:30:35 AM PST, Andy Shevchenko <andriy.shevchenko@...ux.intel.com> wrote:
>On Fri, Dec 03, 2021 at 02:08:46AM -0800, Kees Cook wrote:
>> It's common practice to cast small variable arguments to the find_*_bit()
>
>It's a bad practice and should be fixed accordingly, no?
There's an argument to be made that the first arg should be void * but that's a pretty invasive change at this point (and orthogonal to this fix).
I'd be happy to send a treewide change for that too, if folks wanted?
>
>> helpers to unsigned long and then use a size argument smaller than
>> sizeof(unsigned long):
>>
>> unsigned int bits;
>> ...
>> out = find_first_bit((unsigned long *)&bits, 32);
>>
>> This leads to the find helper dereferencing a full unsigned long,
>> regardless of the size of the actual variable. The unwanted bits
>> get masked away, but strictly speaking, a read beyond the end of
>> the target variable happens. Builds under -Warray-bounds complain
>> about this situation, for example:
>>
>> In file included from ./include/linux/bitmap.h:9,
>> from drivers/iommu/intel/iommu.c:17:
>> drivers/iommu/intel/iommu.c: In function 'domain_context_mapping_one':
>> ./include/linux/find.h:119:37: error: array subscript 'long unsigned int[0]' is partly outside array bounds of 'int[1]' [-Werror=array-bounds]
>> 119 | unsigned long val = *addr & GENMASK(size - 1, 0);
>> | ^~~~~
>> drivers/iommu/intel/iommu.c:2115:18: note: while referencing 'max_pde'
>> 2115 | int pds, max_pde;
>> | ^~~~~~~
>>
>> Instead, just carefully read the correct variable size, all of which
>> happens at compile time since small_const_nbits(size) has already
>> determined that arguments are constant expressions.
>
>What is the performance impact?
There should be none. It's entirely using constant expressions, so all of it gets reduce at compile time to a single path without conditionals. The spot checks I did on the machine code showed no differences either (since I think optimization was doing the masking vis smaller width dereference).
>
--
Kees Cook
Powered by blists - more mailing lists