Re: [PATCH] radix-tree: avoid NULL dereference

From: Mark Rutland
Date: Fri Jul 06 2018 - 11:36:56 EST


On Fri, Jul 06, 2018 at 03:36:04PM +0100, Mark Rutland wrote:
> On Fri, Jul 06, 2018 at 07:25:40AM -0700, Matthew Wilcox wrote:
> > On Fri, Jul 06, 2018 at 02:41:44PM +0100, Mark Rutland wrote:
> > > When idr_alloc() is called for the first time on an IDR (which has no
> > > nodes in its radix tree), we end up with calculate_count() calling
> > > get_slot_offset() with a NULL node, leading to a NULL pointer
> > > dereference caught by UBSAN:
> >
> > Thanks!
> >
> > > The result of the load is passed into node_tag_get(), which ignores the
> > > value when node is NULL. Typically, the compiler inlines both
> > > get_slot_offset() and node_tag_get() into calculate_count(), optimizing
> > > away the NULL-pointer dereference, and hence this doesn't typically
> > > result in a boot failure.
> > >
> > > We can't rely on the compiler always doing this, and must avoid
> > > dereferencing fields from node when it is potentially NULL.
> > >
> > > To do so, this patch folds the generation of offset into tag_get(), such
> > > that this only happens when node is not NULL. Callers are updated to
> > > pass the relevant slot, rather than the offset derived from it.
> >
> > I did think about this ... honest! My reasoning clearly glitched at some
> > point. I think this is the right way to fix the problem.
> >
> > Acked-by: Matthew Wilcox <willy@xxxxxxxxxxxxx>
>
> Cheers!
>
> I assume that Andrew will pick this up, if he's also happy with it.

I've just started fuzzing, and found this also applies with
node_tag_set(). I'll spin a v2 with that fixed up, too.

Thanks,
Mark.