2022-04-28 09:22:38

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH net v4] nfc: ... device_is_registered() is data race-able

On Thu, Apr 28, 2022 at 03:55:01PM +0800, Lin Ma wrote:
> Hello Greg,
>
>
> >
> > You should not be making these types of checks outside of the driver
> > core.
> >
> > > This is by no means matching our expectations as one of our previous patch relies on the device_is_registered code.
> >
> > Please do not do that.
> >
> > >
> > > -> the patch: 3e3b5dfcd16a ("NFC: reorder the logic in nfc_{un,}register_device")
> > >
> > <...>
> > >
> > > In another word, the device_del -> kobject_del -> __kobject_del is not protected by the device_lock.
> >
> > Nor should it be.
> >
>
> I may have mistakenly presented my point. In fact, there is nothing wrong with the device core, nothing to do with the internal of device_del and device_is_registered implementation. And, of course, we will not add any code or do any modification to the device/driver base code.
>
> The point is the combination of device_is_registered + device_del, which is used in NFC core, is not safe.

It shouldn't be, if you are using it properly :)

> That is to say, even the device_is_registered can return True even the device_del is executing in another thread.

Yes, you should almost never use that call. Seems the nfc subsystem is
the most common user of it for some reason :(

> (By debugging we think this is true, correct me if it is not)
>
> Hence we want to add additional state in nfc_dev object to fix that, not going to add any state in device/driver core.

What state are you trying to track here exactly?

thanks,

greg k-h


2022-04-28 15:09:19

by Lin Ma

[permalink] [raw]
Subject: Re: [PATCH net v4] nfc: ... device_is_registered() is data race-able

Hello Greg,

>
> It shouldn't be, if you are using it properly :)
>
> [...]
>
> Yes, you should almost never use that call. Seems the nfc subsystem is
> the most common user of it for some reason :(

Cool, and I believe that the current nfc core code does not use it properly. :(

>
> What state are you trying to track here exactly?
>

Forget about the firmware downloading race that raised by Duoming in this channel,
all the netlink handler code in net/nfc/core.c depends on the device_is_registered
macro.

My idea is to introduce a patch like below:

include/net/nfc/nfc.h | 1 +
net/nfc/core.c | 26 ++++++++++++++------------
2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h
index 5dee575fbe86..d84e53802b06 100644
--- a/include/net/nfc/nfc.h
+++ b/include/net/nfc/nfc.h
@@ -168,6 +168,7 @@ struct nfc_dev {
int targets_generation;
struct device dev;
bool dev_up;
+ bool dev_register;
bool fw_download_in_progress;
u8 rf_mode;
bool polling;
diff --git a/net/nfc/core.c b/net/nfc/core.c
index dc7a2404efdf..208e6bb0804e 100644
--- a/net/nfc/core.c
+++ b/net/nfc/core.c
@@ -38,7 +38,7 @@ int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)

device_lock(&dev->dev);

- if (!device_is_registered(&dev->dev)) {
+ if (!dev->dev_register) {
rc = -ENODEV;
goto error;
}
@@ -94,7 +94,7 @@ int nfc_dev_up(struct nfc_dev *dev)

device_lock(&dev->dev);

- if (!device_is_registered(&dev->dev)) {
+ if (!dev->dev_register) {
rc = -ENODEV;
goto error;
}

[...]

@@ -1134,6 +1134,7 @@ int nfc_register_device(struct nfc_dev *dev)
dev->rfkill = NULL;
}
}
+ dev->dev_register = true;
device_unlock(&dev->dev);

rc = nfc_genl_device_added(dev);
@@ -1162,6 +1163,7 @@ void nfc_unregister_device(struct nfc_dev *dev)
"was removed\n", dev_name(&dev->dev));

device_lock(&dev->dev);
+ dev->dev_register = false;
if (dev->rfkill) {
rfkill_unregister(dev->rfkill);
rfkill_destroy(dev->rfkill);
--
2.35.1

The added dev_register variable can function like the original device_is_registered and does not race-able
because of the protection of device_lock.

I think after such a patch is adopted, the reorder version of patch from Duoming
-> https://lists.openwall.net/netdev/2022/04/25/10
can be used to fix the firmware downloading bug.

Do you agree on this or should we use another macro that is suitable than device_is_registered?

> thanks,
>
> greg k-h

Thanks
Lin

2022-04-28 18:42:17

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH net v4] nfc: ... device_is_registered() is data race-able

On Thu, Apr 28, 2022 at 04:49:18PM +0800, Lin Ma wrote:
> Hello Greg,
>
> >
> > It shouldn't be, if you are using it properly :)
> >
> > [...]
> >
> > Yes, you should almost never use that call. Seems the nfc subsystem is
> > the most common user of it for some reason :(
>
> Cool, and I believe that the current nfc core code does not use it properly. :(
>
> >
> > What state are you trying to track here exactly?
> >
>
> Forget about the firmware downloading race that raised by Duoming in this channel,
> all the netlink handler code in net/nfc/core.c depends on the device_is_registered
> macro.
>
> My idea is to introduce a patch like below:
>
> include/net/nfc/nfc.h | 1 +
> net/nfc/core.c | 26 ++++++++++++++------------
> 2 files changed, 15 insertions(+), 12 deletions(-)
>
> diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h
> index 5dee575fbe86..d84e53802b06 100644
> --- a/include/net/nfc/nfc.h
> +++ b/include/net/nfc/nfc.h
> @@ -168,6 +168,7 @@ struct nfc_dev {
> int targets_generation;
> struct device dev;
> bool dev_up;
> + bool dev_register;
> bool fw_download_in_progress;
> u8 rf_mode;
> bool polling;
> diff --git a/net/nfc/core.c b/net/nfc/core.c
> index dc7a2404efdf..208e6bb0804e 100644
> --- a/net/nfc/core.c
> +++ b/net/nfc/core.c
> @@ -38,7 +38,7 @@ int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
>
> device_lock(&dev->dev);
>
> - if (!device_is_registered(&dev->dev)) {
> + if (!dev->dev_register) {
> rc = -ENODEV;
> goto error;
> }
> @@ -94,7 +94,7 @@ int nfc_dev_up(struct nfc_dev *dev)
>
> device_lock(&dev->dev);
>
> - if (!device_is_registered(&dev->dev)) {
> + if (!dev->dev_register) {
> rc = -ENODEV;
> goto error;
> }
>
> [...]
>
> @@ -1134,6 +1134,7 @@ int nfc_register_device(struct nfc_dev *dev)
> dev->rfkill = NULL;
> }
> }
> + dev->dev_register = true;
> device_unlock(&dev->dev);
>
> rc = nfc_genl_device_added(dev);
> @@ -1162,6 +1163,7 @@ void nfc_unregister_device(struct nfc_dev *dev)
> "was removed\n", dev_name(&dev->dev));
>
> device_lock(&dev->dev);
> + dev->dev_register = false;
> if (dev->rfkill) {
> rfkill_unregister(dev->rfkill);
> rfkill_destroy(dev->rfkill);
> --
> 2.35.1
>
> The added dev_register variable can function like the original device_is_registered and does not race-able
> because of the protection of device_lock.

Yes, that looks better, but what is the root problem here that you are
trying to solve? Why does NFC need this when no other subsystem does?

thansk,

greg k-h

2022-05-02 16:14:08

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH net v4] nfc: ... device_is_registered() is data race-able

On Thu, 28 Apr 2022 11:20:16 +0200 Greg KH wrote:
> > The added dev_register variable can function like the original
> > device_is_registered and does not race-able because of the
> > protection of device_lock.
>
> Yes, that looks better, but what is the root problem here that you are
> trying to solve? Why does NFC need this when no other subsystem does?

Yeah :( The NFC and NCI locking is shaky at best, grounds-up redesign
with clear rules would be great... but then again I'm not sure if anyone
is actually using this code IRL, so the motivation to invest time is
rather weak.