Subject: Re: [PATCH net] net: devlink: fix UAF in devlink_compat_running_version()

From: Jakub Kicinski <[email-masked]>
Date: Wed, 23 Nov 2022 18:47:38 -0800

On Wed, 23 Nov 2022 14:40:24 +0800 Yang Yingliang wrote:
> > +err_dl_unregister:
> > +	devl_unregister(devlink);  
> It races with dev_ethtool():
> dev_ethtool
>    devlink_try_get()
>                                  nsim_drv_probe
>                                  devl_lock()
>      devl_lock()
>                                  devlink_unregister()
>                                    devlink_put()
>                                    wait_for_completion() <- the refcount 
> is got in dev_ethtool, it causes ABBA deadlock

Yeah.. so my original design for the locking had a "devlink_is_alive()"
check for this exact reason:

https://lore.kernel.org/netdev/[email-masked]/

and the devlink structure was properly refcounted (devlink_put() calls
devlink_free() when the last reference is released).

Pure references then need to check if the instance is still alive
after locking it. Which is fine, it should only happen in core code.

I think we should go back to that idea.

The waiting for references is a nightmare in the netdev code.