Re: Catching use-after-free easily in linux kernel.

From: Al Viro
Date: Tue Mar 11 2025 - 00:42:42 EST


On Tue, Mar 11, 2025 at 08:46:36AM +0530, Amit wrote:
> On Tue, Mar 11, 2025, 6:05 AM Al Viro <viro@xxxxxxxxxxxxxxxxxx> wrote:
>
> > On Mon, Mar 10, 2025 at 01:24:54PM +0530, Amit wrote:
> > > Hi,
> > >
> > > We can catch use-after-free easily if we do the following:
> > >
> > > kfree(x);
> > > (x) = NULL;
> > >
> > > Now, if someone uses 'x' again then the kernel will crash and we will
> > know where
> > > the use-after-free is happening and then we can fix it.
> >
> > That assumes that no pointer is ever stored in more than one place.
> > Which is very clearly false.
> >
>
>
> I will do some experiments and then I will reply if I find something.
>
> I will introduce a global macro and then change all kfree() to this macro
> name using cscope probably.
>
> Then I will compile the kernel and run the new kernel and see if some crash
> is happening or not.

What would that test, exactly? And why would that be any more useful than
adding global variables named wank and magic and replacing every kfree(p) with
((magic = wank++),kfree(p))? That also would not introduce any crashes...