[PATCH libxcb] Fix alignment issues in FD passing code

Mouse mouse at Rodents-Montreal.ORG
Mon Nov 11 16:52:08 PST 2013


> A char array on the stack is not guaranteed to have more than byte
> alignment.  This means that casting it to a 'struct cmsghdr' and
> accessing its members may result in unaligned access.  This will
> generate SIGBUS on struct alignment architectures like
> OpenBSD/sparc64.  The canonical solution is to use a union to force
> proper alignment.

This is good enough for access to the struct cmsghdr.  It is, however,
not good enough to use CMSG_DATA (nor, I think, CMSG_NXTHDR) on the
resulting cmsghdr; as far as I can tell there is no way to align the
buffer correctly for that.  If you're willing to make the assumption
(which I find no grounds for in 3542) that it's the alignment of some
object type, you can malloc the buffer, but in at least some
implementations, the alignment required is chosen by the kernel and can
potentially vary, either between runs (eg, across a
mostly-binary-compatible OS version change) or even at run time.  In
any case, I see no grounds, except for an unwarranted familiarity with
certain common implementations, for assuming that the alignment of
struct cmsghdr is sufficient.

This is one of the reasons I consider the CMSG_* interface
fundamentally broken.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse at rodents-montreal.org
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


More information about the xorg-devel mailing list