FAILED: patch "[PATCH] thunderbolt: Prevent XDomain delayed work use-after-free on" failed to apply to 5.15-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x 2c5d2d3c3f70cde2565d7b279b544893a2035842
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026072001-numeric-suffice-a245@gregkh' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 2c5d2d3c3f70cde2565d7b279b544893a2035842 Mon Sep 17 00:00:00 2001
From: Michael Bommarito <[email protected]>
Date: Wed, 27 May 2026 07:46:04 -0400
Subject: [PATCH] thunderbolt: Prevent XDomain delayed work use-after-free on
disconnect
tb_xdp_handle_request() runs on system_wq and queues xd->state_work via queue_delayed_work() in three request handlers:PROPERTIES_CHANGED_REQUEST, UUID_REQUEST (via start_handshake), and LINK_STATE_CHANGE_REQUEST. Similarly, update_xdomain() queues xd->properties_changed_work when local properties change.
Concurrently, tb_xdomain_remove() calls stop_handshake() which does cancel_delayed_work_sync() on both delayed works. Later, tb_xdomain_unregister() calls device_unregister() which eventually frees the xdomain. Since commit 559c1e1e0134 ("thunderbolt: Run tb_xdp_handle_request() in system workqueue") moved the request handler off tb->wq, the handler and the remove path are no longer serialized. If queue_delayed_work() executes after cancel_delayed_work_sync() but before the xdomain is freed, the delayed work fires on a freed object.
Add xd->removing that tb_xdomain_remove() sets under xd->lock before calling stop_handshake(). Each external queue site holds the same lock and checks removing before calling queue_delayed_work(). This provides the mutual exclusion needed:
either the queue site acquires the lock first and queues work that the subsequent cancel will see, or the remove path acquires the lock first and the queue site observes removing == true and skips the queue.
drivers/thunderbolt/xdomain.c +31 -10
@@ -803,9 +803,13 @@ static void tb_xdp_handle_request(struct work_struct *work)* the xdomain related to this connection as well in * case there is a change in services it offers. */ -if (xd &&device_is_registered (&xd->dev)) -queue_delayed_work (tb->wq, &xd->state_work, -msecs_to_jiffies (XDOMAIN_SHORT_TIMEOUT )); +if (xd) { +mutex_lock (&xd->lock); +if (!xd->removing &&device_is_registered (&xd->dev)) +queue_delayed_work (tb->wq, &xd->state_work, +msecs_to_jiffies (XDOMAIN_SHORT_TIMEOUT )); +mutex_unlock (&xd->lock); + }break ;case UUID_REQUEST_OLD :
@@ -818,8 +822,12 @@ static void tb_xdp_handle_request(struct work_struct *work)* received UUID request from the remote host. */ if (!ret && xd && xd->state ==XDOMAIN_STATE_ERROR ) { -dev_dbg (&xd->dev,"restarting handshake\n" ); -start_handshake (xd); +mutex_lock (&xd->lock); +if (!xd->removing) { +dev_dbg (&xd->dev,"restarting handshake\n" ); +start_handshake (xd); + } +mutex_unlock (&xd->lock); }break ;
@@ -885,9 +893,13 @@ static void tb_xdp_handle_request(struct work_struct *work) ret =tb_xdp_link_state_change_response (ctl, route, sequence,0 ); - xd->target_link_width = lsc->tlw; -queue_delayed_work (tb->wq, &xd->state_work, -msecs_to_jiffies (XDOMAIN_SHORT_TIMEOUT )); +mutex_lock (&xd->lock); +if (!xd->removing) { + xd->target_link_width = lsc->tlw; +queue_delayed_work (tb->wq, &xd->state_work, +msecs_to_jiffies (XDOMAIN_SHORT_TIMEOUT )); + } +mutex_unlock (&xd->lock); }else {tb_xdp_error_response (ctl, route, sequence,ERROR_NOT_READY );
@@ -971,8 +983,12 @@ static int update_xdomain(struct device *dev, void *data) xd =tb_to_xdomain (dev);if (xd) { -queue_delayed_work (xd->tb->wq, &xd->properties_changed_work, -msecs_to_jiffies (50 )); +mutex_lock (&xd->lock); +if (!xd->removing) +queue_delayed_work (xd->tb->wq, + &xd->properties_changed_work, +msecs_to_jiffies (50 )); +mutex_unlock (&xd->lock); }return 0 ;
@@ -2200,6 +2216,11 @@ static int unregister_service(struct device *dev, void *data)void tb_xdomain_remove (struct tb_xdomain *xd) {tb_xdomain_debugfs_remove (xd); + +mutex_lock (&xd->lock); + xd->removing = true; +mutex_unlock (&xd->lock); +stop_handshake (xd);tb_xdomain_link_exit (xd);
include/linux/thunderbolt.h +3 -0
@@ -213,6 +213,8 @@ enum tb_link_width {* @link_width: Width of the downstream facing link * @link_usb4: Downstream link is USB4 * @is_unplugged: The XDomain is unplugged +* @removing: Set by tb_xdomain_remove() under @lock to prevent +* concurrent delayed work queueing * @needs_uuid: If the XDomain does not have @remote_uuid it will be * queried first * @service_ids: Used to generate IDs for the services
@@ -262,6 +264,7 @@ struct tb_xdomain {enum tb_link_width link_width;bool link_usb4;bool is_unplugged; +bool removing;bool needs_uuid;struct ida service_ids;struct ida in_hopids;
Sasha Levin
maintainerPATCH 1/6 thunderbolt: Remove usage of the deprecated ida_simple_xx() API
From: Christophe JAILLET <[email protected]>
[ Upstream commit dec6a613574cd3dea799170b7aaa8fd76e22f176 ]
ida_alloc() and ida_free() should be preferred to the deprecated ida_simple_get() and ida_simple_remove().
Note that the upper limit of ida_simple_get() is exclusive, but the one of ida_alloc_range()/ida_alloc_max() is inclusive. So a -1 has been added when needed.
Stable-dep-of: 2c5d2d3c3f70 ("thunderbolt: Prevent XDomain delayed work use-after-free on disconnect")
--- drivers/thunderbolt/domain.c | 6 +++--- drivers/thunderbolt/nhi.c | 6 +++--- drivers/thunderbolt/nvm.c | 4 ++-- drivers/thunderbolt/switch.c | 6 +++--- drivers/thunderbolt/xdomain.c | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-)
drivers/thunderbolt/domain.c +3 -3
@@ -329,7 +329,7 @@ static void tb_domain_release(struct device *dev)tb_ctl_free (tb->ctl);destroy_workqueue (tb->wq); -ida_simple_remove (&tb_domain_ida, tb->index); +ida_free (&tb_domain_ida, tb->index);mutex_destroy (&tb->lock);kfree (tb); }
@@ -397,7 +397,7 @@ struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize tb->nhi = nhi;mutex_init (&tb->lock); - tb->index =ida_simple_get (&tb_domain_ida,0 ,0 ,GFP_KERNEL ); + tb->index =ida_alloc (&tb_domain_ida,GFP_KERNEL );if (tb->index <0 ) goto err_free;
@@ -421,7 +421,7 @@ struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize err_destroy_wq:destroy_workqueue (tb->wq); err_remove_ida: -ida_simple_remove (&tb_domain_ida, tb->index); +ida_free (&tb_domain_ida, tb->index); err_free:kfree (tb);
drivers/thunderbolt/nhi.c +3 -3
@@ -458,7 +458,7 @@ static int ring_request_msix(struct tb_ring *ring, bool no_suspend)if (!nhi->pdev->msix_enabled)return 0 ; - ret =ida_simple_get (&nhi->msix_ida,0 ,MSIX_MAX_VECS ,GFP_KERNEL ); + ret =ida_alloc_max (&nhi->msix_ida,MSIX_MAX_VECS -1 ,GFP_KERNEL );if (ret <0 )return ret;
@@ -478,7 +478,7 @@ static int ring_request_msix(struct tb_ring *ring, bool no_suspend)return 0 ; err_ida_remove: -ida_simple_remove (&nhi->msix_ida, ring->vector); +ida_free (&nhi->msix_ida, ring->vector);return ret; }
@@ -489,7 +489,7 @@ static void ring_release_msix(struct tb_ring *ring)return ;free_irq (ring->irq, ring); -ida_simple_remove (&ring->nhi->msix_ida, ring->vector); +ida_free (&ring->nhi->msix_ida, ring->vector); ring->vector =0 ; ring->irq =0 ; }
drivers/thunderbolt/nvm.c +2 -2
@@ -30,7 +30,7 @@ struct tb_nvm *tb_nvm_alloc(struct device *dev)if (!nvm)return ERR_PTR (-ENOMEM ); - ret =ida_simple_get (&nvm_ida,0 ,0 ,GFP_KERNEL ); + ret =ida_alloc (&nvm_ida,GFP_KERNEL );if (ret <0 ) {kfree (nvm);return ERR_PTR (ret);
@@ -159,7 +159,7 @@ void tb_nvm_free(struct tb_nvm *nvm)if (nvm->active)nvmem_unregister (nvm->active);vfree (nvm->buf); -ida_simple_remove (&nvm_ida, nvm->id); +ida_free (&nvm_ida, nvm->id); }kfree (nvm); }
drivers/thunderbolt/switch.c +3 -3
@@ -806,7 +806,7 @@ static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,if (max_hopid <0 || max_hopid > port_max_hopid) max_hopid = port_max_hopid; -return ida_simple_get (ida, min_hopid, max_hopid +1 ,GFP_KERNEL ); +return ida_alloc_range (ida, min_hopid, max_hopid,GFP_KERNEL ); }/**
@@ -844,7 +844,7 @@ int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)*/ void tb_port_release_in_hopid (struct tb_port *port,int hopid) { -ida_simple_remove (&port->in_hopids, hopid); +ida_free (&port->in_hopids, hopid); }/**
@@ -854,7 +854,7 @@ void tb_port_release_in_hopid(struct tb_port *port, int hopid)*/ void tb_port_release_out_hopid (struct tb_port *port,int hopid) { -ida_simple_remove (&port->out_hopids, hopid); +ida_free (&port->out_hopids, hopid); } static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
drivers/thunderbolt/xdomain.c +2 -2
@@ -808,7 +808,7 @@ static void tb_service_release(struct device *dev)struct tb_xdomain *xd =tb_service_parent (svc);tb_service_debugfs_remove (svc); -ida_simple_remove (&xd->service_ids, svc->id); +ida_free (&xd->service_ids, svc->id);kfree (svc->key);kfree (svc); }
@@ -910,7 +910,7 @@ static void enumerate_services(struct tb_xdomain *xd)break ; } - id =ida_simple_get (&xd->service_ids,0 ,0 ,GFP_KERNEL ); + id =ida_alloc (&xd->service_ids,GFP_KERNEL );if (id <0 ) {kfree (svc->key);kfree (svc);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/6 thunderbolt: Update property.c function documentation
From: Alan Borzeszkowski <[email protected]>
[ Upstream commit d015642ad36d78e6eba12d8ab96cea6fd4602b49 ]
Make property.c function documentation compliant with current kernel-doc standards. No functional changes.
Stable-dep-of: 2c5d2d3c3f70 ("thunderbolt: Prevent XDomain delayed work use-after-free on disconnect")
--- drivers/thunderbolt/property.c | 38 +++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-)
drivers/thunderbolt/property.c +28 -10
@@ -231,11 +231,13 @@ static struct tb_property_dir *__tb_property_parse_dir(const u32 *block,* * This function parses the XDomain properties data block into format that * can be traversed using the helper functions provided by this module. -* Upon success returns the parsed directory. In case of error returns -* %NULL. The resulting &struct tb_property_dir needs to be released by +* +* The resulting &struct tb_property_dir needs to be released by * calling tb_property_free_dir() when not needed anymore. * * The @block is expected to be root directory. +* +* Return: Pointer to &struct tb_property_dir, %NULL in case of failure. */ struct tb_property_dir *tb_property_parse_dir (const u32 *block,size_t block_len)
@@ -258,6 +260,8 @@ struct tb_property_dir *tb_property_parse_dir(const u32 *block,* * Creates new, empty property directory. If @uuid is %NULL then the * directory is assumed to be root directory. +* +* Return: Pointer to &struct tb_property_dir, %NULL in case of failure. */ struct tb_property_dir *tb_property_create_dir (const uuid_t *uuid) {
@@ -501,9 +505,11 @@ static ssize_t __tb_property_format_dir(const struct tb_property_dir *dir,* @block_len: Length of the property block * * This function formats the directory to the packed format that can be -* then send over the thunderbolt fabric to receiving host. Returns %0 in -* case of success and negative errno on faulure. Passing %NULL in @block -* returns number of entries the block takes. +* then sent over the thunderbolt fabric to receiving host. +* +* Passing %NULL in @block returns number of entries the block takes. +* +* Return: %0 on success, negative errno otherwise. */ ssize_t tb_property_format_dir (const struct tb_property_dir *dir,u32 *block,size_t block_len)
@@ -525,9 +531,9 @@ ssize_t tb_property_format_dir(const struct tb_property_dir *dir, u32 *block,* tb_property_copy_dir() - Take a deep copy of directory * @dir: Directory to copy * -* This function takes a deep copy of @dir and returns back the copy. In -* case of error returns %NULL. The resulting directory needs to be -* released by calling tb_property_free_dir(). +* The resulting directory needs to be released by calling tb_property_free_dir(). +* +* Return: Pointer to &struct tb_property_dir, %NULL in case of failure. */ struct tb_property_dir *tb_property_copy_dir (const struct tb_property_dir *dir) {
@@ -597,6 +603,8 @@ struct tb_property_dir *tb_property_copy_dir(const struct tb_property_dir *dir)* @parent: Directory to add the property * @key: Key for the property * @value: Immediate value to store with the property +* +* Return: %0 on success, negative errno otherwise. */ int tb_property_add_immediate (struct tb_property_dir *parent,const char *key,u32 value)
@@ -626,6 +634,8 @@ EXPORT_SYMBOL_GPL(tb_property_add_immediate);* @buflen: Number of bytes in the data buffer * * Function takes a copy of @buf and adds it to the directory. +* +* Return: %0 on success, negative errno otherwise. */ int tb_property_add_data (struct tb_property_dir *parent,const char *key,const void *buf,size_t buflen)
@@ -662,6 +672,8 @@ EXPORT_SYMBOL_GPL(tb_property_add_data);* @text: String to add * * Function takes a copy of @text and adds it to the directory. +* +* Return: %0 on success, negative errno otherwise. */ int tb_property_add_text (struct tb_property_dir *parent,const char *key,const char *text)
@@ -696,6 +708,8 @@ EXPORT_SYMBOL_GPL(tb_property_add_text);* @parent: Directory to add the property * @key: Key for the property * @dir: Directory to add +* +* Return: %0 on success, negative errno otherwise. */ int tb_property_add_dir (struct tb_property_dir *parent,const char *key,struct tb_property_dir *dir)
@@ -736,8 +750,10 @@ EXPORT_SYMBOL_GPL(tb_property_remove);* @key: Key to look for * @type: Type of the property * -* Finds and returns property from the given directory. Does not recurse -* into sub-directories. Returns %NULL if the property was not found. +* Finds and returns property from the given directory. Does not +* recurse into sub-directories. +* +* Return: Pointer to &struct tb_property, %NULL if the property was not found. */ struct tb_property *tb_property_find (struct tb_property_dir *dir,const char *key,enum tb_property_type type)
@@ -757,6 +773,8 @@ EXPORT_SYMBOL_GPL(tb_property_find);* tb_property_get_next() - Get next property from directory * @dir: Directory holding properties * @prev: Previous property in the directory (%NULL returns the first) +* +* Return: Pointer to &struct tb_property, %NULL if property was not found. */ struct tb_property *tb_property_get_next (struct tb_property_dir *dir,struct tb_property *prev)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/6 thunderbolt: Keep XDomain reference during the lifetime of a service
In reply to Sasha LevinFrom: Mika Westerberg <[email protected]>
[ Upstream commit 8b4060998637f06975fceee9b73845d8672d411e ]
This is needed because we release the service ID in tb_service_release() and the ID array is owned by the parent XDomain.
Stable-dep-of: 2c5d2d3c3f70 ("thunderbolt: Prevent XDomain delayed work use-after-free on disconnect")
--- drivers/thunderbolt/xdomain.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/thunderbolt/xdomain.c +2 -1
@@ -811,6 +811,7 @@ static void tb_service_release(struct device *dev)ida_free (&xd->service_ids, svc->id);kfree (svc->key);kfree (svc); +tb_xdomain_put (xd); }struct device_type tb_service_type = {
@@ -919,7 +920,7 @@ static void enumerate_services(struct tb_xdomain *xd) svc->id = id; svc->dev.bus = &tb_bus_type; svc->dev.type = &tb_service_type; - svc->dev.parent = &xd->dev; + svc->dev.parent =get_device (&xd->dev);dev_set_name (&svc->dev,"%s.%d" ,dev_name (&xd->dev), svc->id);tb_service_debugfs_init (svc);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/6 thunderbolt: Remove service debugfs entries during unregister
In reply to Sasha LevinFrom: Mika Westerberg <[email protected]>
[ Upstream commit 4d5fc3f4068568dfcb8cbe2852b4adc56394aa26 ]
We add them as part of the register path so to keep it symmetric remove them as part of the unregister path. This also removes them even if the service itself is not yet released (but is unregistered), thus allowing new register with the same service name to happen.
Stable-dep-of: 2c5d2d3c3f70 ("thunderbolt: Prevent XDomain delayed work use-after-free on disconnect")
--- drivers/thunderbolt/xdomain.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/thunderbolt/xdomain.c +11 -3
@@ -807,7 +807,6 @@ static void tb_service_release(struct device *dev) struct tb_service *svc = container_of(dev, struct tb_service, dev);struct tb_xdomain *xd =tb_service_parent (svc); -tb_service_debugfs_remove (svc);ida_free (&xd->service_ids, svc->id);kfree (svc->key);kfree (svc);
@@ -822,6 +821,14 @@ struct device_type tb_service_type = { };EXPORT_SYMBOL_GPL (tb_service_type); +static void __unregister_service (struct device *dev) +{ +struct tb_service *svc =tb_to_service (dev); + +tb_service_debugfs_remove (svc); +device_unregister (&svc->dev); +} +static int remove_missing_service (struct device *dev,void *data) {struct tb_xdomain *xd = data;
@@ -833,7 +840,7 @@ static int remove_missing_service(struct device *dev, void *data)if (!tb_property_find (xd->remote_properties, svc->key,TB_PROPERTY_TYPE_DIRECTORY )) -device_unregister (dev); +__unregister_service (dev);return 0 ; }
@@ -926,6 +933,7 @@ static void enumerate_services(struct tb_xdomain *xd)tb_service_debugfs_init (svc);if (device_register (&svc->dev)) { +tb_service_debugfs_remove (svc);put_device (&svc->dev);break ; }
@@ -1462,7 +1470,7 @@ void tb_xdomain_add(struct tb_xdomain *xd)static int unregister_service (struct device *dev,void *data) { -device_unregister (dev); +__unregister_service (dev);return 0 ; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/6 thunderbolt: Remove XDomain from the bus without holding tb->lock
In reply to Sasha LevinFrom: Mika Westerberg <[email protected]>
[ Upstream commit a8937f35cf39c39c64325aa84d0463d866850857 ]
Currently we call device_unregister() for services and the XDomain itself with tb->lock held. This prevents the service drivers from calling any functions that may take it. For this reason separate removing the XDomain from the topology data structures (where we need the lock) from unregistering the device from the bus (where remove callbacks of the drivers are being called).
Stable-dep-of: 2c5d2d3c3f70 ("thunderbolt: Prevent XDomain delayed work use-after-free on disconnect")
--- drivers/thunderbolt/domain.c | 30 ++++++++++++++++++ drivers/thunderbolt/icm.c | 5 +++ drivers/thunderbolt/switch.c | 14 +++++++++ drivers/thunderbolt/tb.c | 58 +++++++++++++++++------------------ drivers/thunderbolt/tb.h | 2 ++ drivers/thunderbolt/xdomain.c | 51 +++++++++++++++++++----------- 6 files changed, 113 insertions(+), 47 deletions(-)
drivers/thunderbolt/domain.c +30 -0
@@ -874,6 +874,36 @@ int tb_domain_disconnect_all_paths(struct tb *tb)return bus_for_each_dev (&tb_bus_type,NULL , tb, disconnect_xdomain); } +struct unregister_context { +const struct tb *tb; +int n; +}; + +static int unregister_unplugged_xdomain (struct device *dev,void *data) +{ +struct unregister_context *ctx = data; +struct tb_xdomain *xd; + + xd =tb_to_xdomain (dev); +if (xd && xd->tb == ctx->tb && xd->is_unplugged) { +tb_xdomain_unregister (xd); + ctx->n++; + } +return 0 ; +} + +int tb_domain_unregister_unplugged_xdomains (struct tb *tb) +{ +struct unregister_context ctx; + + ctx.tb =tb_domain_get (tb); + ctx.n =0 ; +bus_for_each_dev (&tb_bus_type,NULL , &ctx, unregister_unplugged_xdomain); +tb_domain_put (tb); + +return ctx.n; +} +int tb_domain_init (void ) {int ret;
drivers/thunderbolt/icm.c +5 -0
@@ -715,6 +715,7 @@ static void remove_xdomain(struct tb_xdomain *xd) sw =tb_to_switch (xd->dev.parent);tb_port_at (xd->route, sw)->xdomain =NULL ; + xd->is_unplugged = true;tb_xdomain_remove (xd); }
@@ -1730,6 +1731,8 @@ static void icm_handle_notification(struct work_struct *work)kfree (n->pkg);kfree (n); + +tb_domain_unregister_unplugged_xdomains (tb); } static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
@@ -2075,6 +2078,8 @@ static void icm_rescan_work(struct work_struct *work)if (tb->root_switch)icm_free_unplugged_children (tb->root_switch);mutex_unlock (&tb->lock); + +tb_domain_unregister_unplugged_xdomains (tb); }static void icm_complete (struct tb *tb)
drivers/thunderbolt/switch.c +14 -0
@@ -3035,6 +3035,20 @@ int tb_switch_resume(struct tb_switch *sw, bool runtime)tb_port_warn (port,"lost during suspend, disconnecting\n" );tb_sw_set_unplugged (port->remote->sw); + }else if (port->xdomain) { +/* +* If the user replaced the XDomain with +* another router, this will succeed in +* which case we must remove the XDomain +* before adding the new router. +*/ + err =tb_cfg_get_upstream_port (sw->tb->ctl, + port->xdomain->route); +if (err >0 ) { +tb_port_warn (port, +"XDomain was disconnected\n" ); + port->xdomain->is_unplugged = true; + } } } }
drivers/thunderbolt/tb.c +29 -29
100 lines not on this page · raw message
drivers/thunderbolt/tb.h +2 -0
@@ -700,6 +700,7 @@ int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd, int transmit_path, int transmit_ring, int receive_path, int receive_ring);int tb_domain_disconnect_all_paths (struct tb *tb); +int tb_domain_unregister_unplugged_xdomains (struct tb *tb);static inline struct tb *tb_domain_get (struct tb *tb) {
@@ -1025,6 +1026,7 @@ struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent, const uuid_t *remote_uuid);void tb_xdomain_add (struct tb_xdomain *xd);void tb_xdomain_remove (struct tb_xdomain *xd); +void tb_xdomain_unregister (struct tb_xdomain *xd);struct tb_xdomain *tb_xdomain_find_by_link_depth (struct tb *tb,u8 link,u8 depth);
drivers/thunderbolt/xdomain.c +33 -18
69 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 6/6 thunderbolt: Prevent XDomain delayed work use-after-free on disconnect
In reply to Sasha LevinFrom: Michael Bommarito <[email protected]>
[ Upstream commit 2c5d2d3c3f70cde2565d7b279b544893a2035842 ]
tb_xdp_handle_request() runs on system_wq and queues xd->state_work via queue_delayed_work() in three request handlers:PROPERTIES_CHANGED_REQUEST, UUID_REQUEST (via start_handshake), and LINK_STATE_CHANGE_REQUEST. Similarly, update_xdomain() queues xd->properties_changed_work when local properties change.
Concurrently, tb_xdomain_remove() calls stop_handshake() which does cancel_delayed_work_sync() on both delayed works. Later, tb_xdomain_unregister() calls device_unregister() which eventually frees the xdomain. Since commit 559c1e1e0134 ("thunderbolt: Run tb_xdp_handle_request() in system workqueue") moved the request handler off tb->wq, the handler and the remove path are no longer serialized. If queue_delayed_work() executes after cancel_delayed_work_sync() but before the xdomain is freed, the delayed work fires on a freed object.
Add xd->removing that tb_xdomain_remove() sets under xd->lock before calling stop_handshake(). Each external queue site holds the same lock and checks removing before calling queue_delayed_work(). This provides the mutual exclusion needed:
either the queue site acquires the lock first and queues work that the subsequent cancel will see, or the remove path acquires the lock first and the queue site observes removing == true and skips the queue.
--- drivers/thunderbolt/xdomain.c | 22 +++++++++++++++++----- include/linux/thunderbolt.h | 3 +++ 2 files changed, 20 insertions(+), 5 deletions(-)
drivers/thunderbolt/xdomain.c +17 -5
@@ -630,9 +630,13 @@ static void tb_xdp_handle_request(struct work_struct *work)* the xdomain related to this connection as well in * case there is a change in services it offers. */ -if (xd &&device_is_registered (&xd->dev)) { -queue_delayed_work (tb->wq, &xd->get_properties_work, -msecs_to_jiffies (50 )); +if (xd) { +mutex_lock (&xd->lock); +if (!xd->removing &&device_is_registered (&xd->dev)) +queue_delayed_work (tb->wq, + &xd->get_properties_work, +msecs_to_jiffies (50 )); +mutex_unlock (&xd->lock); }break ;
@@ -1486,6 +1490,10 @@ static int unregister_service(struct device *dev, void *data)*/ void tb_xdomain_remove (struct tb_xdomain *xd) { +mutex_lock (&xd->lock); + xd->removing = true; +mutex_unlock (&xd->lock); +stop_handshake (xd);if (!device_is_registered (&xd->dev)) {
@@ -1885,8 +1893,12 @@ static int update_xdomain(struct device *dev, void *data) xd =tb_to_xdomain (dev);if (xd) { -queue_delayed_work (xd->tb->wq, &xd->properties_changed_work, -msecs_to_jiffies (50 )); +mutex_lock (&xd->lock); +if (!xd->removing) +queue_delayed_work (xd->tb->wq, + &xd->properties_changed_work, +msecs_to_jiffies (50 )); +mutex_unlock (&xd->lock); }return 0 ;
include/linux/thunderbolt.h +3 -0
@@ -188,6 +188,8 @@ void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir);* @link_speed: Speed of the link in Gb/s * @link_width: Width of the link (1 or 2) * @is_unplugged: The XDomain is unplugged +* @removing: Set by tb_xdomain_remove() under @lock to prevent +* concurrent delayed work queueing * @needs_uuid: If the XDomain does not have @remote_uuid it will be * queried first * @service_ids: Used to generate IDs for the services
@@ -235,6 +237,7 @@ struct tb_xdomain {unsigned int link_speed;unsigned int link_width;bool is_unplugged; +bool removing;bool needs_uuid;struct ida service_ids;struct ida in_hopids;
Signature · 1 line
2.53.0