From: Yury Norov <yury.norov@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Kees Cook <keescook@chromium.org>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Wolfram Sang <wsa+renesas@sang-engineering.com>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org,
Steven Rostedt <rostedt@goodmis.org>
Subject: Re: [PATCH] find: Do not read beyond variable boundaries on small sizes
Date: Fri, 3 Dec 2021 10:26:38 -0800 [thread overview]
Message-ID: <20211203182638.GA450223@lapt> (raw)
In-Reply-To: <YaoN6wnNezMvyyd5@smile.fi.intel.com>
On Fri, Dec 03, 2021 at 02:30:35PM +0200, Andy Shevchenko 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()
Not that common - I found 19 examples of this cast, and most of them
are in drivers. The only non-driver case is kernel/trace/pid_list.c:
static inline bool upper_empty(union upper_chunk *chunk)
{
/*
* If chunk->data has no lower chunks, it will be the same
* as a zeroed bitmask. Use find_first_bit() to test it
* and if it doesn't find any bits set, then the array
* is empty.
*/
int bit = find_first_bit((unsigned long *)chunk->data,
sizeof(chunk->data) * 8);
return bit >= sizeof(chunk->data) * 8;
}
And it's OK wrt type conversion because chunk->data is:
union lower_chunk {
union lower_chunk *next;
unsigned long data[LOWER_SIZE]; // 2K in size
};
Although, this function should use bitmap_empty(), probably like this:
static inline bool upper_empty(union upper_chunk *chunk)
{
return bitmap_empty(chunk->data->data[0], BITS_PER_LONG);
}
Steven, can you comment on this?
> It's a bad practice and should be fixed accordingly, no?
Yes.
> > 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;
> > | ^~~~~~~
The driver should be fixed. I would suggest using one of ffs/fls/ffz from
include/asm/bitops.h
Thanks,
Yury
> > 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?
>
> --
> With Best Regards,
> Andy Shevchenko
>
next prev parent reply other threads:[~2021-12-03 18:26 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-03 10:08 [PATCH] find: Do not read beyond variable boundaries on small sizes Kees Cook
2021-12-03 12:30 ` Andy Shevchenko
2021-12-03 16:37 ` Kees Cook
2021-12-03 19:16 ` Yury Norov
2021-12-03 22:43 ` Kees Cook
2021-12-03 18:26 ` Yury Norov [this message]
2021-12-03 20:48 ` Steven Rostedt
2021-12-03 23:01 ` Kees Cook
2021-12-07 23:39 ` Yury Norov
2021-12-08 5:25 ` Yury Norov
2021-12-08 10:22 ` Andy Shevchenko
2021-12-08 13:07 ` David Laight
2021-12-08 19:19 ` Kees Cook
2021-12-08 19:34 ` Kees Cook
2021-12-08 23:23 ` Rasmus Villemoes
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211203182638.GA450223@lapt \
--to=yury.norov@gmail.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=keescook@chromium.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=rostedt@goodmis.org \
--cc=wsa+renesas@sang-engineering.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox