mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: 徐晗翔 <xuhx@hust.edu.cn>
To: heikki.krogerus@linux.intel.com
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: usb: typec: ucsi: displayport: data race on dp->header/ vdo_data/vdo_size -- work reads them unlocked while writers hold con->lock (VDM mismatch)
Date: Wed, 4 Nov 2026 21:01:06 +0800 (GMT+08:00)	[thread overview]
Message-ID: <568b84ec.1e597.1a1a6a61b78.Coremail.xuhx@hust.edu.cn> (raw)
In-Reply-To: <2026102727-data-sequel-ce41@gregkh>

The UCSI displayport altmode's reply triple (dp->header / vdo_data /
vdo_size, ucsi/displayport.c:28-30) is written by three writers that
all hold con->lock (enter :97-102, exit :142-147, vdm :234+) but read
by ucsi_displayport_work (:282-295) with NO lock at all -- the work
is scheduled asynchronously (:104/:149/:265) and never acquires
con->lock, so the lock only serializes the writers among themselves
and does not cover the reader.  When the worker runs while a writer
updates the triple (the mode_selection activate path entering
ucsi_displayport_enter, or the next EC event arriving while the
worker is inside typec_altmode_vdm -> the partner driver), the VDM
message assembled from the mixed fields is sent to the partner
driver: a stale/mixed header with fresh data or vice versa -- a DP
state machine glitch that the partner sees as a malformed or
misdirected VDM (self-healing on the next event, but still a protocol
violation and a plain KCSAN data race).  Lifetime is NOT at risk
(vdo_data only ever points at the persistent &dp->data.status or
NULL; the worker is cancel_work_sync'ed before teardown), so this is
a small locking fix: read/clear the triple under the same lock (or a
dedicated dp->lock) in the worker.

The detailed description:

* The racy pair (Linux 7.3, drivers/usb/typec/ucsi/displayport.c)

  Writers (all inside ucsi_con_mutex_lock ... unlock, :57/:119/:209):
    - enter path: :97-102 sets dp->header = ENTER_ACK (with
      vdo_data/vdo_size) and schedules dp->work (:104);
    - exit path: :142-147 + :149;
    - vdm path: :234+ (with :183-184 for the ATTENTION-derived
      variant) + :265.

  Reader (no lock): ucsi_displayport_work (:282-295) reads
  dp->header / dp->vdo_data / dp->vdo_size (:287-288) and clears
  them (:292-294), then calls typec_altmode_vdm(dp->alt, ...) ->
  bus.c:214-235 -> the partner driver's dp_altmode_vdm
  (altmodes/displayport.c:372-443, under the driver's own dp->lock).

  The worker never takes con->lock, and nothing stops a writer from
  taking con->lock while the unlocked worker runs (the mode_selection
  activate path enters ucsi_displayport_enter without holding
  con->lock -- it takes it inside).  schedule_work coalescing does
  not close the window.

* Concrete interleavings

  1. mode_selection activate vs an in-flight worker: the activate
     path (thread A) writes ENTER_ACK and schedules; the previous
     round's worker (thread B) is concurrently reading the old
     header/vdo_data at :287-288 -- the partner receives a mixed
     old/new VDM.
  2. back-to-back vdm events: the worker is inside
     typec_altmode_vdm (which enters the partner driver and takes
     its dp->lock -- nonzero time), the next EC event drives
     ucsi_displayport_vdm to overwrite the triple under con->lock --
     same mixed-VDM outcome.

* Scope / severity

  vdo_data only ever points at the persistent &dp->data.status (or
  NULL, :101/:146/:183) and vdo_size is a u8 -- no dangling pointer,
  no UAF; the worker is drained by ucsi_displayport_remove_partner's
  cancel_work_sync (:297-313, :308) before the altmode teardown
  (verified: ucsi.c:834-841 ordering).  So the impact is: KCSAN
  data-race reports plus transient VDM mismatches (partner-side DP
  state machine warnings/retries; self-healing).  Severity low; the
  fix is small.

* Suggested fix 

  Have ucsi_displayport_work() snapshot the triple under
  ucsi_con_mutex_lock(dp->con) (or a new dedicated dp->lock shared by
  writers and the worker), then send the snapshot outside the lock.
  Alternatively, writers coalesce under the lock with a
  cancel_work_sync before overwriting -- more costly; the snapshot
  is the clean minimal fix.


  Please keep me in Cc; I am not subscribed to the lists.



Regards,
Hanxiang Xu
xuhx@hust.edu.cn

  parent reply	other threads:[~2026-11-04 13:01 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 12:35 usb: devio: usbdev_release() has no barrier against an in-flight async_completed() callback (use-after-free window on struct usb_dev_state)gregkh@linuxfoundation.org 徐晗翔
2026-10-27 14:12 ` Greg KH
2026-11-03 11:21   ` mm/slub: has_pcs_used() reads remote per-CPU sheaf state from flush_all_cpus_locked() without synchronization (data race; KCSAN blind spot: slub.o is uninstrumented) 徐晗翔
2026-11-04  9:41     ` Qing Wang
2026-11-04 10:07       ` Qing Wang
2026-11-03 11:30   ` futex/hrtimer: hrtimer_sleeper::task is read unsynchronized in 16 places on v7.3 -- the accessor series that fixes this is not in 7.3; please make sure it reaches the affected trees (backport nudge + stock-kernel KCSAN evidence) 徐晗翔
2026-11-03 11:48   ` cgroup: fork vs cgroup.procs write race in favordynmods mode -- v7.3 is affected; please make sure bf6fbf46b5ff reaches the stable trees (repro incl. uninstrumented natural hit) 徐晗翔
2026-11-03 14:51     ` Sasha Levin
2026-11-03 11:57   ` net: rtnetlink: rtnl_link_unregister() vs in-flight RTM_GETLINK dump readers -- v7.3 lacks the synchronize_rcu() fix (Fixes: 6b57ff21a310 family); A/B reproduction on stock 7.3 徐晗翔
2026-11-03 15:49     ` Eric Dumazet
2026-11-04 15:03     ` Sasha Levin
2026-11-03 12:07   ` wifi: wilc1000: SDIO/SPI remove destroys the srcu_struct before wiphy_unregister() -- late cfg80211 callbacks write through dangling srcu state (unfixed upstream; controlled + natural Oops) 徐晗翔
2026-11-03 12:10   ` gpio: gpiolib: final put of the gpio_device happens inside the SRCU read critical section on the setup-failure path -- deterministic use-after-free read at guard release (unfixed upstream) 徐晗翔
2026-11-04 12:54   ` soc: qcom: pmic_glink_altmode: fix use-after-free on unbind (no cancel_work_sync for port work and enable_work) 徐晗翔
2026-11-04 13:04     ` Greg KH
2026-11-04 12:57   ` usb: typec: anx7411: unregister partner/altmodes before stopping the IRQ work -- use-after-free, double unregister, orphan partner on remove 徐晗翔
2026-11-04 13:28     ` Greg KH
2026-11-04 12:58   ` usb: typec: ucsi: deadlock on cros_ec unbind -- connector lock held across cancel_delayed_work_sync vs the mode-selection work's unbounded trylock polling 徐晗翔
2026-11-04 13:00   ` usb: typec: stusb160x/rt1719/hd3ss3220: IRQ thread vs remove -- double unregister / use-after-free on the partner and port 徐晗翔
2026-11-04 13:01   ` 徐晗翔 [this message]
2026-11-04 13:02   ` usb: typec: class: three lifetime/locking gaps around the mux/switch handles and mode_selection (drvdata UAF after provider unbind; no concurrent protocol for set/put; unlocked state_update vs delete) 徐晗翔

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=568b84ec.1e597.1a1a6a61b78.Coremail.xuhx@hust.edu.cn \
    --to=xuhx@hust.edu.cn \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®