[PATCH v2] stdlib: random_r: fix unaligned access in initstate and initstate_r [BZ #30584]
Andreas K. Huettel
dilfridge@gentoo.org
Mon Dec 30 18:17:19 GMT 2024
>
> Sorry for not catching this before. I'm kind of surpised that &b[idx]
> doesn't assert that b is already 4-byte aligned. There is also a
> pointer wraparound issue here because read_state/write_state get called
> with -1, which is now converted to ~(size_t) 0. So perhaps use “int idx”
> and write:
>
> /* Use literal 4 to avoid conversion to an unsigned type and pointer
> wraparound. */
> memcpy ((char *) b + idx * 4, &v, 4);
>
> (I think C is a bit ambiguous whether &b[idx] actually qualifies as
> access.)
>
> The int32_t types are part of the public API, so those are kind of hard
> to avoid. We could memcpy into those pointers, too, but if this more
> limited approach works, then I suppose that's okay.
>
>
> > +static int
> > +do_test (void)
> > +{
> > + struct random_data rand_state = { .state = NULL };
> > + _Alignas (double) char buf[128 + sizeof (int32_t)];
> > +
> > + /* Test initstate_r with an unaligned `state` array. */
> > + initstate_r (time (NULL), buf + 1, sizeof buf, &rand_state);
> > +
> > + /* Ditto initstate. */
> > + initstate (time (NULL), buf + 1, sizeof buf);
> > +
> > + return 0;
> > +}
>
> Looks okay now.
Then let's add it for 2.41 ... -a
>
> Thanks,
> Florian
>
>
--
Andreas K. Hüttel
dilfridge@gentoo.org
Gentoo Linux developer
(council, comrel, toolchain, base-system, perl, libreoffice)
https://wiki.gentoo.org/wiki/User:Dilfridge
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20241230/eced0273/attachment.sig>
More information about the Libc-alpha
mailing list