Skip to content

Brittle implicit alignment assumptions in Steam Controller driver #15422

@Kuratius

Description

@Kuratius

As discussed in #15378 , I believe the Steam Controller driver implicitly assumes that the alignment of struct members is always the same on every platform, as it casts a pointer to a byte array into a struct pointer and then performs accesses to the struct that rely on the alignment of struct members being the same across different architectures.
This is unsafe.
Structs in C can have padding added to them by the compiler, so their members aren't guaranteed to be at the same position in the byte array on every architecture, or even to have the same endianness.
As the steam controller can be used with any computing device that supports USB or Bluetooth, this assumption is likely to break if it isn't broken already on some platforms for which an SDL port exists.
This issue might also exist in the Steam Deck controller code as it reuses the same structs, but there it is mitigated by the fact that the steam deck only exists with an x86 cpu so the alignment assumptions (likely) can't change at a whim unless there is some future ARM or SPARC based steam deck.
It might affect the future Steam Controller 2, so its code should also be checked for this problem.
The Linux Kernel driver does not make any such assumptions as it directly accesses the raw bytes.

Please see

ValveInReport_t *pInReport = (ValveInReport_t *)pData;

and

as well as the kernel driver
https://github.com/torvalds/linux/blob/a5d1079c28a5bc6caa30ef4099ef04ed17d2c6aa/drivers/hid/hid-steam.c#L1430
which avoids this problem by accessing relevant bytes directly rather than assuming the struct members to always have the same alignment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions