Skip to contentSkip to conversation

usb: gadget: f_uac1_legacy: fix use-after-free in gaudio_open_snd_dev()

PATCH In review Adrian Korwel 2 participants 22 messages May 25 – Jun 25

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: gadget: f_uac1_legacy: fix use-after-free in gaudio_open_snd_dev()

Three bugs exist in this driver related to ALSA device file lifetime:

1. gaudio_open_snd_dev() opens the ALSA control file first, then the PCM playback file. If filp_open() for playback fails, the function returns without closing the already-opened control file handle.

2. playback_default_hw_params() return value was ignored. If it fails, both the control and playback file handles are leaked, causing gaudio_cleanup() to call filp_close() on already-freed file objects.

3. f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization, but the fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

   BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
   Read of size 8 at addr ffff88810d5523a8 by task bash/306
   ...
   gaudio_cleanup+0x59/0x100
   f_audio_bind+0x4b0/0x590

Fix all three issues:
- Close already-opened file handles on each error path in gaudio_open_snd_dev().
- Check and propagate the return value of playback_default_hw_params().
- Remove the 'bound' guard and call gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from the opts struct.

Additionally, f_audio_disable() was an empty stub. Add cancel_work_sync() to ensure the playback work item is not in flight when the function is unbound and the audio struct is freed.

Changes since v1:
- Added removal of the 'bound' guard in f_audio_bind() which was the root cause of the repeated-bind UAF
- Added cancel_work_sync() to f_audio_disable()
- Removed now-unused 'bound' field from struct f_uac1_legacy_opts

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1

implementation legacy")

---
 drivers/usb/gadget/function/f_uac1_legacy.c | 15 +++++++--------
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 3 files changed, 16 insertions(+), 10 deletions(-)
(quoted diff) +0 -0

b/drivers/usb/gadget/function/f_uac1_legacy.c
index 5d201a2e30e7..798fbb8550bc 100644

drivers/usb/gadget/function/f_uac1_legacy.c +0 -0
@@ -697,7 +697,9 @@ static int f_audio_get_alt(struct usb_function *f,

unsigned intf)

static void f_audio_disable(struct usb_function *f) {
- return;
+ struct f_audio *audio = func_to_audio(f);
+
+ cancel_work_sync(&audio->playback_work);
}

/*-------------------------------------------------------------------------*/
@@ -735,13 +737,10 @@ f_audio_bind(struct usb_configuration *c, struct usb_function *f)

        audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
        audio->card.gadget = c->cdev->gadget;

- /* set up ASLA audio devices */
- if (!audio_opts->bound) {
- status = gaudio_setup(&audio->card);
- if (status < 0)
- return status;
- audio_opts->bound = true;

-       }
+       /* set up ALSA audio devices */
+       status = gaudio_setup(&audio->card);
+       if (status < 0)
+               return status;
        us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
        if (IS_ERR(us))
                return PTR_ERR(us);
(quoted diff) +0 -0

b/drivers/usb/gadget/function/u_uac1_legacy.c
index 01016102fa17..5bcd3afd6366 100644

drivers/usb/gadget/function/u_uac1_legacy.c +9 -1
@@ -226,12 +226,20 @@ static int gaudio_open_snd_dev(struct gaudio *card)
 
                ERROR(card, "No such PCM playback device: %s\n", fn_play);
                snd->filp = NULL;
+               filp_close(card->control.filp, NULL);
+               card->control.filp = NULL;
                return ret;
        }
        pcm_file = snd->filp->private_data;
        snd->substream = pcm_file->substream;
        snd->card = card;
-       playback_default_hw_params(snd);
+       if (playback_default_hw_params(snd) < 0) {
+               filp_close(snd->filp, NULL);
+               snd->filp = NULL;
+               filp_close(card->control.filp, NULL);
+               card->control.filp = NULL;
+               return -EINVAL;
+       }
 
        /* Open PCM capture device and setup substream */
        snd = &card->capture;
(quoted diff) +0 -0

b/drivers/usb/gadget/function/u_uac1_legacy.h
index b5df9bcbbeba..fd22fd37fe53 100644

drivers/usb/gadget/function/u_uac1_legacy.h +0 -1
@@ -61,7 +61,6 @@ struct f_uac1_legacy_opts {
        char                            *fn_play;
        char                            *fn_cap;
        char                            *fn_cntl;
-       unsigned                        bound:1;
        unsigned                        fn_play_alloc:1;
        unsigned                        fn_cap_alloc:1;
        unsigned                        fn_cntl_alloc:1;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 23 lines · Adrian Korwel

Three bugs exist in this driver related to ALSA device file lifetime:

1. gaudio_open_snd_dev() opens the ALSA control file first, then the PCM playback file. If filp_open() for playback fails, the function returns without closing the already-opened control file handle.

2. playback_default_hw_params() return value was ignored. If it fails, both the control and playback file handles are leaked, causing gaudio_cleanup() to call filp_close() on already-freed file objects.

3. f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization, but the fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

   BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
   Read of size 8 at addr ffff88810d5523a8 by task bash/306
   ...
   gaudio_cleanup+0x59/0x100
   f_audio_bind+0x4b0/0x590
Quoted 8 lines

Fix all three issues:
- Close already-opened file handles on each error path in gaudio_open_snd_dev().
- Check and propagate the return value of playback_default_hw_params().
- Remove the 'bound' guard and call gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from the opts struct.

Why is this not broken up into smaller patches to corrispond with each issue/fix? Please do so.

Quoted 10 lines

Additionally, f_audio_disable() was an empty stub. Add cancel_work_sync() to ensure the playback work item is not in flight when the function is unbound and the audio struct is freed.

Changes since v1:
- Added removal of the 'bound' guard in f_audio_bind() which was the root cause of the repeated-bind UAF
- Added cancel_work_sync() to f_audio_disable()
- Removed now-unused 'bound' field from struct f_uac1_legacy_opts

The "changes" go below the --- line, right?

thanks,

greg k-h

#2 lore raw
Quoted 2 lines · Greg KH

Why is this not broken up into smaller patches...
The "changes" go below the --- line, right?

Apologies for the formatting issues. Please find v3 below as a series of 4 patches. Fixes split into separate patches and changelog moved below the --- line.

Adrian Korwel (4):
usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()
usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard
usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()
usb: typec: thunderbolt: cancel work before altmode is removed

 drivers/usb/gadget/function/f_uac1_legacy.c | 15 +++++++--------
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 drivers/usb/typec/altmodes/thunderbolt.c    |  2 ++
 4 files changed, 18 insertions(+), 10 deletions(-)
Quoted 54 lines · Greg KH
Quoted 23 lines · Adrian Korwel

Three bugs exist in this driver related to ALSA device file lifetime:

1. gaudio_open_snd_dev() opens the ALSA control file first, then the PCM playback file. If filp_open() for playback fails, the function returns without closing the already-opened control file handle.

2. playback_default_hw_params() return value was ignored. If it fails, both the control and playback file handles are leaked, causing gaudio_cleanup() to call filp_close() on already-freed file objects.

3. f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization, but the fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

   BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
   Read of size 8 at addr ffff88810d5523a8 by task bash/306
   ...
   gaudio_cleanup+0x59/0x100
   f_audio_bind+0x4b0/0x590
Quoted 8 lines

Fix all three issues:
- Close already-opened file handles on each error path in gaudio_open_snd_dev().
- Check and propagate the return value of playback_default_hw_params().
- Remove the 'bound' guard and call gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from the opts struct.

Why is this not broken up into smaller patches to corrispond with each issue/fix? Please do so.

Quoted 10 lines

Additionally, f_audio_disable() was an empty stub. Add cancel_work_sync() to ensure the playback work item is not in flight when the function is unbound and the audio struct is freed.

Changes since v1:
- Added removal of the 'bound' guard in f_audio_bind() which was the root cause of the repeated-bind UAF
- Added cancel_work_sync() to f_audio_disable()
- Removed now-unused 'bound' field from struct f_uac1_legacy_opts

The "changes" go below the --- line, right?

thanks,

greg k-h

#3 lore raw

[PATCH v3 1/4] usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()

gaudio_open_snd_dev() opens the ALSA control device file first, then opens the PCM playback device. On two error paths the control file handle is leaked:

When filp_open() for the playback device fails, the function returns immediately without closing the already-opened control file handle.

When playback_default_hw_params() fails, its return value was ignored and both the playback and control file handles were leaked.

Both leaks result in gaudio_cleanup() calling filp_close() on already freed file objects, causing a use-after-free.

Fix by closing previously opened file handles before returning on each error path, and by checking the return value of playback_default_hw_params().

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1

implementation legacy")

---
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/usb/gadget/function/u_uac1_legacy.c
index 01016102fa17..5bcd3afd6366 100644

drivers/usb/gadget/function/u_uac1_legacy.c +9 -1
@@ -226,12 +226,20 @@ static int gaudio_open_snd_dev(struct gaudio *card)
 
                ERROR(card, "No such PCM playback device: %s\n", fn_play);
                snd->filp = NULL;
+               filp_close(card->control.filp, NULL);
+               card->control.filp = NULL;
                return ret;
        }
        pcm_file = snd->filp->private_data;
        snd->substream = pcm_file->substream;
        snd->card = card;
-       playback_default_hw_params(snd);
+       if (playback_default_hw_params(snd) < 0) {
+               filp_close(snd->filp, NULL);
+               snd->filp = NULL;
+               filp_close(card->control.filp, NULL);
+               card->control.filp = NULL;
+               return -EINVAL;
+       }
 
        /* Open PCM capture device and setup substream */
        snd = &card->capture;
Signature · 1 line
2.43.0
Quoted 76 lines · Adrian Korwel
Quoted 2 lines · Greg KH

Why is this not broken up into smaller patches...
The "changes" go below the --- line, right?

Apologies for the formatting issues. Please find v3 below as a series of 4 patches. Fixes split into separate patches and changelog moved below the --- line.

Adrian Korwel (4):
usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()
usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard
usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()
usb: typec: thunderbolt: cancel work before altmode is removed

 drivers/usb/gadget/function/f_uac1_legacy.c | 15 +++++++--------
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 drivers/usb/typec/altmodes/thunderbolt.c    |  2 ++
 4 files changed, 18 insertions(+), 10 deletions(-)
Quoted 54 lines · Greg KH
Quoted 23 lines · Adrian Korwel

Three bugs exist in this driver related to ALSA device file lifetime:

1. gaudio_open_snd_dev() opens the ALSA control file first, then the PCM playback file. If filp_open() for playback fails, the function returns without closing the already-opened control file handle.

2. playback_default_hw_params() return value was ignored. If it fails, both the control and playback file handles are leaked, causing gaudio_cleanup() to call filp_close() on already-freed file objects.

3. f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization, but the fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

   BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
   Read of size 8 at addr ffff88810d5523a8 by task bash/306
   ...
   gaudio_cleanup+0x59/0x100
   f_audio_bind+0x4b0/0x590
Quoted 8 lines

Fix all three issues:
- Close already-opened file handles on each error path in gaudio_open_snd_dev().
- Check and propagate the return value of playback_default_hw_params().
- Remove the 'bound' guard and call gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from the opts struct.

Why is this not broken up into smaller patches to corrispond with each issue/fix? Please do so.

Quoted 10 lines

Additionally, f_audio_disable() was an empty stub. Add cancel_work_sync() to ensure the playback work item is not in flight when the function is unbound and the audio struct is freed.

Changes since v1:
- Added removal of the 'bound' guard in f_audio_bind() which was the root cause of the repeated-bind UAF
- Added cancel_work_sync() to f_audio_disable()
- Removed now-unused 'bound' field from struct f_uac1_legacy_opts

The "changes" go below the --- line, right?

thanks,

greg k-h

maintainer
#4 lore raw
Quoted 2 lines · Adrian Korwel

[PATCH v3 1/4] usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()

This should be in the subject line, not in the body of the emails.

Please resend a new version of all of these in the correct format.

thanks,

greg k-h

#5 lore raw
In reply to Adrian Korwel

[PATCH v3 2/4] usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard

f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization on repeated bind attempts. However the
fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

  BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
  Read of size 8 at addr ffff88810d5523a8 by task bash/306
  ...
  gaudio_cleanup+0x59/0x100
  f_audio_bind+0x4b0/0x590

Fix by removing the bound guard and calling gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from struct f_uac1_legacy_opts.

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1

implementation legacy")

---
 drivers/usb/gadget/function/f_uac1_legacy.c | 11 ++++-------
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 2 files changed, 4 insertions(+), 8 deletions(-)
(quoted diff) +0 -0

b/drivers/usb/gadget/function/f_uac1_legacy.c
index 5d201a2e30e7..6ad4b16769b7 100644

drivers/usb/gadget/function/f_uac1_legacy.c +0 -0
@@ -735,13 +735,10 @@ f_audio_bind(struct usb_configuration *c, struct

usb_function *f)

        audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
        audio->card.gadget = c->cdev->gadget;

- /* set up ASLA audio devices */
- if (!audio_opts->bound) {
- status = gaudio_setup(&audio->card);
- if (status < 0)
- return status;
- audio_opts->bound = true;

-       }
+       /* set up ALSA audio devices */
+       status = gaudio_setup(&audio->card);
+       if (status < 0)
+               return status;
        us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
        if (IS_ERR(us))
                return PTR_ERR(us);
(quoted diff) +0 -0

b/drivers/usb/gadget/function/u_uac1_legacy.h
index b5df9bcbbeba..fd22fd37fe53 100644

drivers/usb/gadget/function/u_uac1_legacy.h +0 -1
@@ -61,7 +61,6 @@ struct f_uac1_legacy_opts {
        char                            *fn_play;
        char                            *fn_cap;
        char                            *fn_cntl;
-       unsigned                        bound:1;
        unsigned                        fn_play_alloc:1;
        unsigned                        fn_cap_alloc:1;
        unsigned                        fn_cntl_alloc:1;
Signature · 1 line
2.43.0
Quoted 76 lines · Adrian Korwel
Quoted 2 lines · Greg KH

Why is this not broken up into smaller patches...
The "changes" go below the --- line, right?

Apologies for the formatting issues. Please find v3 below as a series of 4 patches. Fixes split into separate patches and changelog moved below the --- line.

Adrian Korwel (4):
usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()
usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard
usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()
usb: typec: thunderbolt: cancel work before altmode is removed

 drivers/usb/gadget/function/f_uac1_legacy.c | 15 +++++++--------
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 drivers/usb/typec/altmodes/thunderbolt.c    |  2 ++
 4 files changed, 18 insertions(+), 10 deletions(-)
Quoted 54 lines · Greg KH
Quoted 23 lines · Adrian Korwel

Three bugs exist in this driver related to ALSA device file lifetime:

1. gaudio_open_snd_dev() opens the ALSA control file first, then the PCM playback file. If filp_open() for playback fails, the function returns without closing the already-opened control file handle.

2. playback_default_hw_params() return value was ignored. If it fails, both the control and playback file handles are leaked, causing gaudio_cleanup() to call filp_close() on already-freed file objects.

3. f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization, but the fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

   BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
   Read of size 8 at addr ffff88810d5523a8 by task bash/306
   ...
   gaudio_cleanup+0x59/0x100
   f_audio_bind+0x4b0/0x590
Quoted 8 lines

Fix all three issues:
- Close already-opened file handles on each error path in gaudio_open_snd_dev().
- Check and propagate the return value of playback_default_hw_params().
- Remove the 'bound' guard and call gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from the opts struct.

Why is this not broken up into smaller patches to corrispond with each issue/fix? Please do so.

Quoted 10 lines

Additionally, f_audio_disable() was an empty stub. Add cancel_work_sync() to ensure the playback work item is not in flight when the function is unbound and the audio struct is freed.

Changes since v1:
- Added removal of the 'bound' guard in f_audio_bind() which was the root cause of the repeated-bind UAF
- Added cancel_work_sync() to f_audio_disable()
- Removed now-unused 'bound' field from struct f_uac1_legacy_opts

The "changes" go below the --- line, right?

thanks,

greg k-h

#6 lore raw
In reply to Adrian Korwel

[PATCH v3 3/4] usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()

f_audio_disable() was an empty stub that simply returned without cancelling the pending playback work item. The work function f_audio_playback_work() accesses audio->lock, audio->play_queue and audio->card which reside in the audio struct that is freed by f_audio_free() after disable returns.

Fix by adding cancel_work_sync() to ensure the playback work item is not in flight when the audio struct is freed.

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1

implementation legacy")

---
 drivers/usb/gadget/function/f_uac1_legacy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/usb/gadget/function/f_uac1_legacy.c
index 6ad4b16769b7..798fbb8550bc 100644

drivers/usb/gadget/function/f_uac1_legacy.c +0 -0
@@ -697,7 +697,9 @@ static int f_audio_get_alt(struct usb_function *f,

unsigned intf)

static void f_audio_disable(struct usb_function *f) {
- return;
+ struct f_audio *audio = func_to_audio(f);
+
+ cancel_work_sync(&audio->playback_work);
}

/*-------------------------------------------------------------------------*/

Signature · 1 line
2.43.0
Quoted 76 lines · Adrian Korwel
Quoted 2 lines · Greg KH

Why is this not broken up into smaller patches...
The "changes" go below the --- line, right?

Apologies for the formatting issues. Please find v3 below as a series of 4 patches. Fixes split into separate patches and changelog moved below the --- line.

Adrian Korwel (4):
usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()
usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard
usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()
usb: typec: thunderbolt: cancel work before altmode is removed

 drivers/usb/gadget/function/f_uac1_legacy.c | 15 +++++++--------
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 drivers/usb/typec/altmodes/thunderbolt.c    |  2 ++
 4 files changed, 18 insertions(+), 10 deletions(-)
Quoted 54 lines · Greg KH
Quoted 23 lines · Adrian Korwel

Three bugs exist in this driver related to ALSA device file lifetime:

1. gaudio_open_snd_dev() opens the ALSA control file first, then the PCM playback file. If filp_open() for playback fails, the function returns without closing the already-opened control file handle.

2. playback_default_hw_params() return value was ignored. If it fails, both the control and playback file handles are leaked, causing gaudio_cleanup() to call filp_close() on already-freed file objects.

3. f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization, but the fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

   BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
   Read of size 8 at addr ffff88810d5523a8 by task bash/306
   ...
   gaudio_cleanup+0x59/0x100
   f_audio_bind+0x4b0/0x590
Quoted 8 lines

Fix all three issues:
- Close already-opened file handles on each error path in gaudio_open_snd_dev().
- Check and propagate the return value of playback_default_hw_params().
- Remove the 'bound' guard and call gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from the opts struct.

Why is this not broken up into smaller patches to corrispond with each issue/fix? Please do so.

Quoted 10 lines

Additionally, f_audio_disable() was an empty stub. Add cancel_work_sync() to ensure the playback work item is not in flight when the function is unbound and the audio struct is freed.

Changes since v1:
- Added removal of the 'bound' guard in f_audio_bind() which was the root cause of the repeated-bind UAF
- Added cancel_work_sync() to f_audio_disable()
- Removed now-unused 'bound' field from struct f_uac1_legacy_opts

The "changes" go below the --- line, right?

thanks,

greg k-h

#7 lore raw
In reply to Adrian Korwel

[PATCH v3 4/4] usb: typec: thunderbolt: cancel work before altmode is removed

tbt_altmode_remove() frees resources associated with the Thunderbolt altmode but does not cancel the pending work item before returning. Since tbt is allocated with devm_kzalloc(), it is freed automatically when the device is released after remove() returns.

The work item tbt_altmode_work() can be scheduled via schedule_work() from tbt_altmode_vdm(), tbt_altmode_activate(), and the probe path, and may still be pending or running when tbt_altmode_remove() returns. The work function accesses tbt->lock, tbt->state, tbt->alt, and tbt->plug[] — all of which reside in the freed struct, resulting in a use-after-free.

Fix by calling cancel_work_sync() in tbt_altmode_remove() before releasing any resources, ensuring no work item referencing tbt can run after teardown begins.

Fixes 100e25738659 ("usb: typec: Add driver for Thunderbolt 3 Alternate Mode")
---
 drivers/usb/typec/altmodes/thunderbolt.c | 2 ++
 1 file changed, 2 insertions(+)
(quoted diff) +0 -0

b/drivers/usb/typec/altmodes/thunderbolt.c
index 32250b94262a..57c8dff0c51f 100644

drivers/usb/typec/altmodes/thunderbolt.c +2 -0
@@ -303,6 +303,8 @@ static void tbt_altmode_remove(struct typec_altmode *alt)
 {
        struct tbt_altmode *tbt = typec_altmode_get_drvdata(alt);
+       cancel_work_sync(&tbt->work);
+
        for (int i = TYPEC_PLUG_SOP_PP; i >= 0; --i) {
                if (tbt->plug[i])
                        typec_altmode_put_plug(tbt->plug[i]);
Signature · 1 line
2.43.0
Quoted 76 lines · Adrian Korwel
Quoted 2 lines · Greg KH

Why is this not broken up into smaller patches...
The "changes" go below the --- line, right?

Apologies for the formatting issues. Please find v3 below as a series of 4 patches. Fixes split into separate patches and changelog moved below the --- line.

Adrian Korwel (4):
usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()
usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard
usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()
usb: typec: thunderbolt: cancel work before altmode is removed

 drivers/usb/gadget/function/f_uac1_legacy.c | 15 +++++++--------
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 drivers/usb/typec/altmodes/thunderbolt.c    |  2 ++
 4 files changed, 18 insertions(+), 10 deletions(-)
Quoted 54 lines · Greg KH
Quoted 23 lines · Adrian Korwel

Three bugs exist in this driver related to ALSA device file lifetime:

1. gaudio_open_snd_dev() opens the ALSA control file first, then the PCM playback file. If filp_open() for playback fails, the function returns without closing the already-opened control file handle.

2. playback_default_hw_params() return value was ignored. If it fails, both the control and playback file handles are leaked, causing gaudio_cleanup() to call filp_close() on already-freed file objects.

3. f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization, but the fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

   BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
   Read of size 8 at addr ffff88810d5523a8 by task bash/306
   ...
   gaudio_cleanup+0x59/0x100
   f_audio_bind+0x4b0/0x590
Quoted 8 lines

Fix all three issues:
- Close already-opened file handles on each error path in gaudio_open_snd_dev().
- Check and propagate the return value of playback_default_hw_params().
- Remove the 'bound' guard and call gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from the opts struct.

Why is this not broken up into smaller patches to corrispond with each issue/fix? Please do so.

Quoted 10 lines

Additionally, f_audio_disable() was an empty stub. Add cancel_work_sync() to ensure the playback work item is not in flight when the function is unbound and the audio struct is freed.

Changes since v1:
- Added removal of the 'bound' guard in f_audio_bind() which was the root cause of the repeated-bind UAF
- Added cancel_work_sync() to f_audio_disable()
- Removed now-unused 'bound' field from struct f_uac1_legacy_opts

The "changes" go below the --- line, right?

thanks,

greg k-h

maintainer
#8 lore raw
Quoted 1 line · Adrian Korwel

[PATCH v3 4/4] usb: typec: thunderbolt: cancel work before altmode is removed

Something went really wrong with this series, always use git send-email or something else that can properly send patches :(

thanks,

greg k-h

#9 lore raw

PATCH 1/4 USB: serial: io_ti: fix heap overflow in build_i2c_fw_hdr()

build_i2c_fw_hdr() allocates a fixed-size buffer of (16*1024 - 512) + sizeof(struct ti_i2c_firmware_rec) bytes, then copies le16_to_cpu(img_header->Length) bytes into it without validating that Length fits within the available space after the firmware record header.

img_header->Length is a __le16 from the firmware file and can be up to 65535. check_fw_sanity() validates the total firmware size but not img_header->Length specifically.

Fix by rejecting images where img_header->Length exceeds the available destination space.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/usb/serial/io_ti.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/serial/io_ti.c +5 -0
index a35409bd766c..afe29fdf9536 100644
@@ -844,6 +844,11 @@ static int build_i2c_fw_hdr(u8 *header, const struct firmware *fw)
 	/* Pointer to fw_down memory image */
 	img_header = (struct ti_i2c_image_header *)&fw->data[4];
 
+	if (le16_to_cpu(img_header->Length) >
+			buffer_size - sizeof(struct ti_i2c_firmware_rec)) {
+		kfree(buffer);
+		return -EINVAL;
+	}
 	memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
 		&fw->data[4 + sizeof(struct ti_i2c_image_header)],
 		le16_to_cpu(img_header->Length));
Signature · 1 line
2.43.0
#10 lore raw

PATCH 1/4 usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()

gaudio_open_snd_dev() opens the ALSA control device file first, then opens the PCM playback device. On two error paths the control file handle is leaked:

When filp_open() for the playback device fails, the function returns immediately without closing the already-opened control file handle.

When playback_default_hw_params() fails, its return value was ignored and both the playback and control file handles were leaked.

Both leaks result in gaudio_cleanup() calling filp_close() on already freed file objects, causing a use-after-free.

Fix by closing previously opened file handles before returning on each error path, and by checking the return value of playback_default_hw_params().

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/u_uac1_legacy.c +9 -1
index 01016102fa17..5bcd3afd6366 100644
@@ -226,12 +226,20 @@ static int gaudio_open_snd_dev(struct gaudio *card)
 
 		ERROR(card, "No such PCM playback device: %s\n", fn_play);
 		snd->filp = NULL;
+		filp_close(card->control.filp, NULL);
+		card->control.filp = NULL;
 		return ret;
 	}
 	pcm_file = snd->filp->private_data;
 	snd->substream = pcm_file->substream;
 	snd->card = card;
-	playback_default_hw_params(snd);
+	if (playback_default_hw_params(snd) < 0) {
+		filp_close(snd->filp, NULL);
+		snd->filp = NULL;
+		filp_close(card->control.filp, NULL);
+		card->control.filp = NULL;
+		return -EINVAL;
+	}
 
 	/* Open PCM capture device and setup substream */
 	snd = &card->capture;
Signature · 1 line
2.43.0
#11 lore raw

PATCH 2/4 usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()

In reply to Adrian Korwel

gaudio_open_snd_dev() opens the ALSA control device file first, then opens the PCM playback device. On two error paths the control file handle is leaked:

When filp_open() for the playback device fails, the function returns immediately without closing the already-opened control file handle.

When playback_default_hw_params() fails, its return value was ignored and both the playback and control file handles were leaked.

Both leaks result in gaudio_cleanup() calling filp_close() on already freed file objects, causing a use-after-free.

Fix by closing previously opened file handles before returning on each error path, and by checking the return value of playback_default_hw_params().

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/u_uac1_legacy.c +9 -1
index 01016102fa17..5bcd3afd6366 100644
@@ -226,12 +226,20 @@ static int gaudio_open_snd_dev(struct gaudio *card)
 
 		ERROR(card, "No such PCM playback device: %s\n", fn_play);
 		snd->filp = NULL;
+		filp_close(card->control.filp, NULL);
+		card->control.filp = NULL;
 		return ret;
 	}
 	pcm_file = snd->filp->private_data;
 	snd->substream = pcm_file->substream;
 	snd->card = card;
-	playback_default_hw_params(snd);
+	if (playback_default_hw_params(snd) < 0) {
+		filp_close(snd->filp, NULL);
+		snd->filp = NULL;
+		filp_close(card->control.filp, NULL);
+		card->control.filp = NULL;
+		return -EINVAL;
+	}
 
 	/* Open PCM capture device and setup substream */
 	snd = &card->capture;
Signature · 1 line
2.43.0
#12 lore raw

PATCH 2/4 usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard

In reply to Adrian Korwel

f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization on repeated bind attempts. However the
fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

  BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
  Read of size 8 at addr ffff88810d5523a8 by task bash/306
  ...
  gaudio_cleanup+0x59/0x100
  f_audio_bind+0x4b0/0x590

Fix by removing the bound guard and calling gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from struct f_uac1_legacy_opts.

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/f_uac1_legacy.c | 11 ++++-------
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 2 files changed, 4 insertions(+), 8 deletions(-)
drivers/usb/gadget/function/f_uac1_legacy.c +4 -7
index 5d201a2e30e7..6ad4b16769b7 100644
@@ -735,13 +735,10 @@ f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 
 	audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
 	audio->card.gadget = c->cdev->gadget;
-	/* set up ASLA audio devices */
-	if (!audio_opts->bound) {
-		status = gaudio_setup(&audio->card);
-		if (status < 0)
-			return status;
-		audio_opts->bound = true;
-	}
+	/* set up ALSA audio devices */
+	status = gaudio_setup(&audio->card);
+	if (status < 0)
+		return status;
 	us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
 	if (IS_ERR(us))
 		return PTR_ERR(us);
drivers/usb/gadget/function/u_uac1_legacy.h +0 -1
index b5df9bcbbeba..fd22fd37fe53 100644
@@ -61,7 +61,6 @@ struct f_uac1_legacy_opts {
 	char				*fn_play;
 	char				*fn_cap;
 	char				*fn_cntl;
-	unsigned			bound:1;
 	unsigned			fn_play_alloc:1;
 	unsigned			fn_cap_alloc:1;
 	unsigned			fn_cntl_alloc:1;
Signature · 1 line
2.43.0
#13 lore raw

PATCH 3/4 usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()

In reply to Adrian Korwel

f_audio_disable() was an empty stub that simply returned without cancelling the pending playback work item. The work function f_audio_playback_work() accesses audio->lock, audio->play_queue and audio->card which reside in the audio struct that is freed by f_audio_free() after disable returns.

Fix by adding cancel_work_sync() to ensure the playback work item is not in flight when the audio struct is freed.

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/f_uac1_legacy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_uac1_legacy.c +3 -1
index 6ad4b16769b7..798fbb8550bc 100644
@@ -697,7 +697,9 @@ static int f_audio_get_alt(struct usb_function *f, unsigned intf)
 
 static void f_audio_disable(struct usb_function *f)
 {
-	return;
+	struct f_audio *audio = func_to_audio(f);
+
+	cancel_work_sync(&audio->playback_work);
 }
 
 /*-------------------------------------------------------------------------*/
Signature · 1 line
2.43.0
#14 lore raw

PATCH 3/4 usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard

In reply to Adrian Korwel

f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization on repeated bind attempts. However the
fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

  BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
  Read of size 8 at addr ffff88810d5523a8 by task bash/306
  ...
  gaudio_cleanup+0x59/0x100
  f_audio_bind+0x4b0/0x590

Fix by removing the bound guard and calling gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from struct f_uac1_legacy_opts.

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/f_uac1_legacy.c | 11 ++++-------
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 2 files changed, 4 insertions(+), 8 deletions(-)
drivers/usb/gadget/function/f_uac1_legacy.c +4 -7
index 5d201a2e30e7..6ad4b16769b7 100644
@@ -735,13 +735,10 @@ f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 
 	audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
 	audio->card.gadget = c->cdev->gadget;
-	/* set up ASLA audio devices */
-	if (!audio_opts->bound) {
-		status = gaudio_setup(&audio->card);
-		if (status < 0)
-			return status;
-		audio_opts->bound = true;
-	}
+	/* set up ALSA audio devices */
+	status = gaudio_setup(&audio->card);
+	if (status < 0)
+		return status;
 	us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
 	if (IS_ERR(us))
 		return PTR_ERR(us);
drivers/usb/gadget/function/u_uac1_legacy.h +0 -1
index b5df9bcbbeba..fd22fd37fe53 100644
@@ -61,7 +61,6 @@ struct f_uac1_legacy_opts {
 	char				*fn_play;
 	char				*fn_cap;
 	char				*fn_cntl;
-	unsigned			bound:1;
 	unsigned			fn_play_alloc:1;
 	unsigned			fn_cap_alloc:1;
 	unsigned			fn_cntl_alloc:1;
Signature · 1 line
2.43.0
#15 lore raw

PATCH 4/4 usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()

In reply to Adrian Korwel

f_audio_disable() was an empty stub that simply returned without cancelling the pending playback work item. The work function f_audio_playback_work() accesses audio->lock, audio->play_queue and audio->card which reside in the audio struct that is freed by f_audio_free() after disable returns.

Fix by adding cancel_work_sync() to ensure the playback work item is not in flight when the audio struct is freed.

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/f_uac1_legacy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_uac1_legacy.c +3 -1
index 6ad4b16769b7..798fbb8550bc 100644
@@ -697,7 +697,9 @@ static int f_audio_get_alt(struct usb_function *f, unsigned intf)
 
 static void f_audio_disable(struct usb_function *f)
 {
-	return;
+	struct f_audio *audio = func_to_audio(f);
+
+	cancel_work_sync(&audio->playback_work);
 }
 
 /*-------------------------------------------------------------------------*/
Signature · 1 line
2.43.0
#16 lore raw

PATCH 4/4 usb: typec: thunderbolt: cancel work before altmode is removed

In reply to Adrian Korwel

tbt_altmode_remove() frees resources associated with the Thunderbolt altmode but does not cancel the pending work item before returning. Since tbt is allocated with devm_kzalloc(), it is freed automatically when the device is released after remove() returns.

The work item tbt_altmode_work() can be scheduled via schedule_work() from tbt_altmode_vdm(), tbt_altmode_activate(), and the probe path, and may still be pending or running when tbt_altmode_remove() returns. The work function accesses tbt->lock, tbt->state, tbt->alt, and tbt->plug[] — all of which reside in the freed struct, resulting in a use-after-free.

Fix by calling cancel_work_sync() in tbt_altmode_remove() before releasing any resources, ensuring no work item referencing tbt can run after teardown begins.

Fixes 100e25738659 ("usb: typec: Add driver for Thunderbolt 3 Alternate Mode")
---
 drivers/usb/typec/altmodes/thunderbolt.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/typec/altmodes/thunderbolt.c +2 -0
index 32250b94262a..57c8dff0c51f 100644
@@ -303,6 +303,8 @@ static void tbt_altmode_remove(struct typec_altmode *alt)
 {
 	struct tbt_altmode *tbt = typec_altmode_get_drvdata(alt);
 
+	cancel_work_sync(&tbt->work);
+
 	for (int i = TYPEC_PLUG_SOP_PP; i >= 0; --i) {
 		if (tbt->plug[i])
 			typec_altmode_put_plug(tbt->plug[i]);
Signature · 1 line
2.43.0
#17 lore raw

PATCH 1/4 usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()

In reply to Greg KH

gaudio_open_snd_dev() opens the ALSA control device file first, then opens the PCM playback device. On two error paths the control file handle is leaked:

When filp_open() for the playback device fails, the function returns immediately without closing the already-opened control file handle.

When playback_default_hw_params() fails, its return value was ignored and both the playback and control file handles were leaked.

Both leaks result in gaudio_cleanup() calling filp_close() on already freed file objects, causing a use-after-free.

Fix by closing previously opened file handles before returning on each error path, and by checking the return value of playback_default_hw_params().

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/u_uac1_legacy.c +9 -1
index 01016102fa17..5bcd3afd6366 100644
@@ -226,12 +226,20 @@ static int gaudio_open_snd_dev(struct gaudio *card)
 
 		ERROR(card, "No such PCM playback device: %s\n", fn_play);
 		snd->filp = NULL;
+		filp_close(card->control.filp, NULL);
+		card->control.filp = NULL;
 		return ret;
 	}
 	pcm_file = snd->filp->private_data;
 	snd->substream = pcm_file->substream;
 	snd->card = card;
-	playback_default_hw_params(snd);
+	if (playback_default_hw_params(snd) < 0) {
+		filp_close(snd->filp, NULL);
+		snd->filp = NULL;
+		filp_close(card->control.filp, NULL);
+		card->control.filp = NULL;
+		return -EINVAL;
+	}
 
 	/* Open PCM capture device and setup substream */
 	snd = &card->capture;
Signature · 1 line
2.43.0
#18 lore raw

PATCH 2/4 usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard

f_audio_bind() guards gaudio_setup() with an 'audio_opts->bound' flag to prevent re-initialization on repeated bind attempts. However the
fail: error path unconditionally calls gaudio_cleanup(). On repeated bind attempts after failure, this closes file handles that were opened in a previous bind invocation and already freed by RCU, causing a use-after-free detected by KASAN:

  BUG: KASAN: slab-use-after-free in filp_flush+0x23/0x1b0
  Read of size 8 at addr ffff88810d5523a8 by task bash/306
  ...
  gaudio_cleanup+0x59/0x100
  f_audio_bind+0x4b0/0x590

Fix by removing the bound guard and calling gaudio_setup() unconditionally in f_audio_bind(), making setup and cleanup a matched pair within each bind invocation. Remove the now-unused 'bound' field from struct f_uac1_legacy_opts.

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/f_uac1_legacy.c | 11 ++++-------
 drivers/usb/gadget/function/u_uac1_legacy.h |  1 -
 2 files changed, 4 insertions(+), 8 deletions(-)
drivers/usb/gadget/function/f_uac1_legacy.c +4 -7
index 5d201a2e30e7..6ad4b16769b7 100644
@@ -735,13 +735,10 @@ f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 
 	audio_opts = container_of(f->fi, struct f_uac1_legacy_opts, func_inst);
 	audio->card.gadget = c->cdev->gadget;
-	/* set up ASLA audio devices */
-	if (!audio_opts->bound) {
-		status = gaudio_setup(&audio->card);
-		if (status < 0)
-			return status;
-		audio_opts->bound = true;
-	}
+	/* set up ALSA audio devices */
+	status = gaudio_setup(&audio->card);
+	if (status < 0)
+		return status;
 	us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1));
 	if (IS_ERR(us))
 		return PTR_ERR(us);
drivers/usb/gadget/function/u_uac1_legacy.h +0 -1
index b5df9bcbbeba..fd22fd37fe53 100644
@@ -61,7 +61,6 @@ struct f_uac1_legacy_opts {
 	char				*fn_play;
 	char				*fn_cap;
 	char				*fn_cntl;
-	unsigned			bound:1;
 	unsigned			fn_play_alloc:1;
 	unsigned			fn_cap_alloc:1;
 	unsigned			fn_cntl_alloc:1;
Signature · 1 line
2.43.0
#19 lore raw

PATCH 3/4 usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()

In reply to Adrian Korwel

f_audio_disable() was an empty stub that simply returned without cancelling the pending playback work item. The work function f_audio_playback_work() accesses audio->lock, audio->play_queue and audio->card which reside in the audio struct that is freed by f_audio_free() after disable returns.

Fix by adding cancel_work_sync() to ensure the playback work item is not in flight when the audio struct is freed.

Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/f_uac1_legacy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_uac1_legacy.c +3 -1
index 6ad4b16769b7..798fbb8550bc 100644
@@ -697,7 +697,9 @@ static int f_audio_get_alt(struct usb_function *f, unsigned intf)
 
 static void f_audio_disable(struct usb_function *f)
 {
-	return;
+	struct f_audio *audio = func_to_audio(f);
+
+	cancel_work_sync(&audio->playback_work);
 }
 
 /*-------------------------------------------------------------------------*/
Signature · 1 line
2.43.0
#20 lore raw

PATCH 4/4 usb: typec: thunderbolt: cancel work before altmode is removed

In reply to Adrian Korwel

tbt_altmode_remove() frees resources associated with the Thunderbolt altmode but does not cancel the pending work item before returning. Since tbt is allocated with devm_kzalloc(), it is freed automatically when the device is released after remove() returns.

The work item tbt_altmode_work() can be scheduled via schedule_work() from tbt_altmode_vdm(), tbt_altmode_activate(), and the probe path, and may still be pending or running when tbt_altmode_remove() returns. The work function accesses tbt->lock, tbt->state, tbt->alt, and tbt->plug[] — all of which reside in the freed struct, resulting in a use-after-free.

Fix by calling cancel_work_sync() in tbt_altmode_remove() before releasing any resources, ensuring no work item referencing tbt can run after teardown begins.

Fixes 100e25738659 ("usb: typec: Add driver for Thunderbolt 3 Alternate Mode")
---
 drivers/usb/typec/altmodes/thunderbolt.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/typec/altmodes/thunderbolt.c +2 -0
index 32250b94262a..57c8dff0c51f 100644
@@ -303,6 +303,8 @@ static void tbt_altmode_remove(struct typec_altmode *alt)
 {
 	struct tbt_altmode *tbt = typec_altmode_get_drvdata(alt);
 
+	cancel_work_sync(&tbt->work);
+
 	for (int i = TYPEC_PLUG_SOP_PP; i >= 0; --i) {
 		if (tbt->plug[i])
 			typec_altmode_put_plug(tbt->plug[i]);
Signature · 1 line
2.43.0
maintainer
#21 lore raw

Re: [PATCH 1/4] usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()

In reply to Adrian Korwel
Quoted 17 lines · Adrian Korwel

gaudio_open_snd_dev() opens the ALSA control device file first, then opens the PCM playback device. On two error paths the control file handle is leaked:

When filp_open() for the playback device fails, the function returns immediately without closing the already-opened control file handle.

When playback_default_hw_params() fails, its return value was ignored and both the playback and control file handles were leaked.

Both leaks result in gaudio_cleanup() calling filp_close() on already freed file objects, causing a use-after-free.

Fix by closing previously opened file handles before returning on each error path, and by checking the return value of playback_default_hw_params().

Quoted 6 lines
Fixes d355339eecd9 ("usb: gadget: function: make current f_uac1 implementation legacy")
---
 drivers/usb/gadget/function/u_uac1_legacy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Something is really odd with your email system, look at what is on the list from you for these patches. It looks like:

 525   C May 25 Adrian Korwel   (  43) ┬─>[PATCH 1/2] USB: serial: io_ti: fix heap overflow in get_manuf_info()                                        
 527   C May 25 Adrian Korwel   (  40) │ └─>[PATCH 2/2] USB: serial: io_ti: fix heap overflow in build_i2c_fw_hdr()
 528   T May 25 Adrian Korwel   (  77) └─>Re: [PATCH] USB: serial: io_ti: fix heap overflows in get_manuf_info() and build_i2c_fw_hdr()
 623   C May 25 Adrian Korwel   (  54) ┬─>[PATCH 1/4] usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()
 624   C May 25 Adrian Korwel   (  64) │ ├─>[PATCH 2/4] usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard
 625   C May 25 Adrian Korwel   (  40) │ ├─>[PATCH 4/4] usb: typec: thunderbolt: cancel work before altmode is removed
 626   C May 25 Adrian Korwel   (  35) │ └─>[PATCH 3/4] usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()
 627   C May 25 Adrian Korwel   (  40) └─>[PATCH 1/4] USB: serial: io_ti: fix heap overflow in build_i2c_fw_hdr()
 628   C May 25 Adrian Korwel   (  40)   ├─>[PATCH 4/4] usb: typec: thunderbolt: cancel work before altmode is removed
 629   C May 25 Adrian Korwel   (  35)   ├─>[PATCH 4/4] usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()
 630   C May 25 Adrian Korwel   (  54)   ├─>[PATCH 1/4] usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()
 631   C May 25 Adrian Korwel   (  64)   ├─>[PATCH 2/4] usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard
 632   C May 25 Adrian Korwel   (  64)   ├─>[PATCH 3/4] usb: gadget: f_uac1_legacy: fix use-after-free caused by bound guard
 633   C May 25 Adrian Korwel   (  35)   ├─>[PATCH 3/4] usb: gadget: f_uac1_legacy: cancel work in f_audio_disable()
 634   C May 25 Adrian Korwel   (  54)   └─>[PATCH 2/4] usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()
 636   C May 24 Adrian Korwel   (  41) [PATCH] usb: typec: thunderbolt: cancel work before altmode is removed  
 637   C May 24 Adrian Korwel   (  62) [PATCH] usb: gadget: f_uac1_legacy: fix file handle leaks in gaudio_open_snd_dev()

You have threading issues, patches in multiple places and resend, and all mixed up.

Please resend everything that has not been accepted, as new versions, in the correct way, and we will be glad to review them.

thanks,

greg k-h