Skip to content

usb: typec: fix CD321x partner identity use-after-free and empty USB mode list - #4

Merged
DjDeveloperr merged 2 commits into
omacom:tbfrom
maralcbr:typec-fixes
Sep 12, 2026
Merged

DjDeveloperr merged 2 commits into
omacom:tbfrom
maralcbr:typec-fixes

Conversation

@maralcbr

Copy link
Copy Markdown
Member

Two Type-C fixes found while bringing up USB-C displays on a MacBook Pro (16-inch, M2 Max, apple,j416c) running the tb branch. Both are verified on that machine.

1. usb: typec: tipd: fix partner identity use-after-free on CD321x

cd321x_update_work() points desc.identity at a stack local, and typec_register_partner() stores that pointer instead of copying the structure. The partner outlives the work function, so it is left referencing a freed stack page.

Reading /sys/class/typec/port0-partner/type as an unprivileged user is enough to hit it:

Unable to handle kernel paging request at virtual address ffff800083a87d0c
Internal error: Oops: 0000000096000007 [#1] SMP
pc : get_pd_product_type+0x54/0xa4 [typec]
lr : type_show+0x14/0x3c [typec]

get_pd_product_type() null-checks partner->identity and then reads id->id_header. The pointer isn't null, just dangling; with VMAP_STACK the dead stack is unmapped, hence the vmalloc-range fault address.

The driver already keeps a persistent copy in cd321x->cur_partner_identity but only assigns it after registration. The fix fills it in first and registers against that, which is what the generic tps6598x path already does.

Fixes: 82432bb ("usb: typec: tipd: Handle mode transitions for CD321x")

2. usb: typec: fix sysfs_emit_at() offset underflow on an empty mode list

usb_mode_show() and usb_capability_show() overwrite a trailing space with a newline using sysfs_emit_at(buf, len - 1, "\n"). When none of USB2/USB3/USB4 is advertised the loop never runs, len stays 0, and the call is made with at == -1:

invalid sysfs_emit_at: buf:00000000ffaad2a3 at:-1
WARNING: fs/sysfs/file.c:785 at sysfs_emit_at+0x54/0xd4

The attribute then also reads back empty instead of a newline like every other list attribute. Seen on the same machine, where one of the partners registered by the CD321x has no USB capability bits set, by reading /sys/class/typec/port1-partner/usb_mode.

Fixes: 2140a95 ("usb: typec: Add attribute file showing the USB Modes of the partner")

Testing

Built and booted on the M2 Max with both patches applied. The read that previously oopsed the machine now returns normally, usb_mode returns a newline instead of warning, and Type-C displays continue to work across replugs on all ports.

Both changes are against code that is the same in mainline, so they can be sent to linux-usb if you want them upstream.

Marcelo added 2 commits September 10, 2026 09:37
cd321x_update_work() points desc.identity at st.partner_identity, where
"st" is a stack local. typec_register_partner() stores that pointer in
partner->identity rather than copying the structure, so once the work
function returns the partner is left referencing a freed stack page.

Any later read of the partner's PD identity then dereferences it. On a
MacBook Pro (16-inch, M2 Max) reading

  /sys/class/typec/port0-partner/type

as an unprivileged user is enough:

  Unable to handle kernel paging request at virtual address ffff800083a87d0c
  Internal error: Oops: 0000000096000007 [omacom#1] SMP
  CPU: 10 UID: 1000 PID: 83017 Comm: cat
  pc : get_pd_product_type+0x54/0xa4 [typec]
  lr : type_show+0x14/0x3c [typec]

get_pd_product_type() NULL-checks partner->identity and then reads
id->id_header, which is the faulting access; the pointer is not NULL,
just dangling. With VMAP_STACK the dead stack is unmapped, hence the
vmalloc-range fault address.

The driver already keeps a persistent copy in cd321x->cur_partner_identity,
but only assigns it after registration. Fill it in first and register the
partner against that instead. The generic tps6598x path already does the
equivalent, pointing desc.identity at tps->partner_identity.

Fixes: 82432bb ("usb: typec: tipd: Handle mode transitions for CD321x")
Signed-off-by: Marcelo <[email protected]>
usb_mode_show() and usb_capability_show() build a space-separated list
and then overwrite the trailing space with a newline:

	sysfs_emit_at(buf, len - 1, "\n");

When the partner or port advertises none of USB2/USB3/USB4 the loop body
never runs, len stays 0, and the call is made with at == -1:

  invalid sysfs_emit_at: buf:00000000ffaad2a3 at:-1
  WARNING: fs/sysfs/file.c:785 at sysfs_emit_at+0x54/0xd4, CPU#1: cat/892

sysfs_emit_at() rejects the negative offset, so the attribute also reads
back empty rather than as a newline like every other list attribute.

Seen on a MacBook Pro (16-inch, M2 Max), where one of the Type-C partners
registered by the CD321x has no USB capability bits set, by reading

  /sys/class/typec/port1-partner/usb_mode

Return a bare newline when nothing was written.

Fixes: 2140a95 ("usb: typec: Add attribute file showing the USB Modes of the partner")
Signed-off-by: Marcelo <[email protected]>

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, fixes are on point!

@DjDeveloperr
DjDeveloperr merged commit 0065426 into omacom:tb Sep 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants