* [PATCH v2] usb: typec: altmodes/displayport: create sysfs nodes after assigning driver data
@ 2024-02-26 23:42 RD Babiera
2024-02-27 5:20 ` Greg KH
0 siblings, 1 reply; 3+ messages in thread
From: RD Babiera @ 2024-02-26 23:42 UTC (permalink / raw)
To: rdbabiera, heikki.krogerus, gregkh
Cc: badhri, linux-usb, linux-kernel, stable
The DisplayPort driver's sysfs nodes may be present to the userspace before
typec_altmode_set_drvdata() completes in dp_altmode_probe. This means that
a sysfs read can trigger a NULL pointer error by deferencing dp->hpd in
hpd_show or dp->lock in pin_assignment_show, as dev_get_drvdata() returns
NULL in those cases.
Create sysfs nodes after typec_altmode_set_drvdata call.
Fixes: 0e3bb7d6894d ("usb: typec: Add driver for DisplayPort alternate mode")
Cc: stable@vger.kernel.org
Signed-off-by: RD Babiera <rdbabiera@google.com>
---
Changes from v1:
* Moved sysfs node creation instead of NULL checking dev_get_drvdata().
---
drivers/usb/typec/altmodes/displayport.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/typec/altmodes/displayport.c b/drivers/usb/typec/altmodes/displayport.c
index 5a80776c7255..5bbdd2c04237 100644
--- a/drivers/usb/typec/altmodes/displayport.c
+++ b/drivers/usb/typec/altmodes/displayport.c
@@ -731,10 +731,6 @@ int dp_altmode_probe(struct typec_altmode *alt)
DP_CAP_PIN_ASSIGN_DFP_D(alt->vdo)))
return -ENODEV;
- ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group);
- if (ret)
- return ret;
-
dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
if (!dp)
return -ENOMEM;
@@ -766,6 +762,10 @@ int dp_altmode_probe(struct typec_altmode *alt)
if (plug)
typec_altmode_set_drvdata(plug, dp);
+ ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group);
+ if (ret)
+ return ret;
+
dp->state = plug ? DP_STATE_ENTER_PRIME : DP_STATE_ENTER;
schedule_work(&dp->work);
base-commit: a560a5672826fc1e057068bda93b3d4c98d037a2
--
2.44.0.rc1.240.g4c46232300-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] usb: typec: altmodes/displayport: create sysfs nodes after assigning driver data
2024-02-26 23:42 [PATCH v2] usb: typec: altmodes/displayport: create sysfs nodes after assigning driver data RD Babiera
@ 2024-02-27 5:20 ` Greg KH
2024-02-28 22:59 ` RD Babiera
0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2024-02-27 5:20 UTC (permalink / raw)
To: RD Babiera; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel, stable
On Mon, Feb 26, 2024 at 11:42:29PM +0000, RD Babiera wrote:
> The DisplayPort driver's sysfs nodes may be present to the userspace before
> typec_altmode_set_drvdata() completes in dp_altmode_probe. This means that
> a sysfs read can trigger a NULL pointer error by deferencing dp->hpd in
> hpd_show or dp->lock in pin_assignment_show, as dev_get_drvdata() returns
> NULL in those cases.
>
> Create sysfs nodes after typec_altmode_set_drvdata call.
>
> Fixes: 0e3bb7d6894d ("usb: typec: Add driver for DisplayPort alternate mode")
> Cc: stable@vger.kernel.org
> Signed-off-by: RD Babiera <rdbabiera@google.com>
> ---
> Changes from v1:
> * Moved sysfs node creation instead of NULL checking dev_get_drvdata().
> ---
> drivers/usb/typec/altmodes/displayport.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/usb/typec/altmodes/displayport.c b/drivers/usb/typec/altmodes/displayport.c
> index 5a80776c7255..5bbdd2c04237 100644
> --- a/drivers/usb/typec/altmodes/displayport.c
> +++ b/drivers/usb/typec/altmodes/displayport.c
> @@ -731,10 +731,6 @@ int dp_altmode_probe(struct typec_altmode *alt)
> DP_CAP_PIN_ASSIGN_DFP_D(alt->vdo)))
> return -ENODEV;
>
> - ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group);
> - if (ret)
> - return ret;
> -
> dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
> if (!dp)
> return -ENOMEM;
> @@ -766,6 +762,10 @@ int dp_altmode_probe(struct typec_altmode *alt)
> if (plug)
> typec_altmode_set_drvdata(plug, dp);
>
> + ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group);
> + if (ret)
> + return ret;
> +
Now I am going to push back again and ask why you are even attempting to
create sysfs files "by hand" here at all?
Why is this just not set to be a default group? That way the group is
managed properly by the driver core and the driver doesn't have to worry
about ANY of this at all. Bonus is that you remove the "you raced with
userspace and lost" problem that this code still has even with the
change you made here.
Huge hint, if you EVER have to call sysfs_*() from a driver, usually
something is wrong (there are a few exceptions, this is not one.)
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] usb: typec: altmodes/displayport: create sysfs nodes after assigning driver data
2024-02-27 5:20 ` Greg KH
@ 2024-02-28 22:59 ` RD Babiera
0 siblings, 0 replies; 3+ messages in thread
From: RD Babiera @ 2024-02-28 22:59 UTC (permalink / raw)
To: Greg KH; +Cc: heikki.krogerus, badhri, linux-usb, linux-kernel, stable
On Mon, Feb 26, 2024 at 9:20 PM Greg KH <gregkh@linuxfoundation.org> wrote:
> Now I am going to push back again and ask why you are even attempting to
> create sysfs files "by hand" here at all?
>
> Why is this just not set to be a default group? That way the group is
> managed properly by the driver core and the driver doesn't have to worry
> about ANY of this at all. Bonus is that you remove the "you raced with
> userspace and lost" problem that this code still has even with the
> change you made here.
To answer both questions, the driver had always created its sysfs
nodes manually so I did not suspect this was not the preferred way
to handle driver sysfs creation until I found your article on default attributes
after your email. Will fix the patch up.
thanks again,
rd
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-02-28 22:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-26 23:42 [PATCH v2] usb: typec: altmodes/displayport: create sysfs nodes after assigning driver data RD Babiera
2024-02-27 5:20 ` Greg KH
2024-02-28 22:59 ` RD Babiera
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).