[U-Boot] [PATCH V2 2/3] lib: uuid: add functions to generate UUID version 4

Wolfgang Denk wd at denx.de
Thu Mar 13 19:41:24 CET 2014


Dear Przemyslaw Marczak,

In message <cc0f558724a4d3ea3497b84601038f5f18f37a7b.1394037321.git.p.marczak at samsung.com> you wrote:
> This patch adds support to generate UUID (Universally Unique Identifier)
> in version 4 based on RFC4122, which is randomly.
...
> +struct uuid {
> +	unsigned int time_low;
> +	unsigned short time_mid;
> +	unsigned short time_hi_and_version;
> +	unsigned char clock_seq_hi_and_reserved;
> +	unsigned char clock_seq_low;
> +	unsigned char node[6];
> +};

This struct starts with an uint, so it requires alignment on a 32 bit
boundary (i. e. an address that is a multiple of 4).

> +void gen_rand_uuid(unsigned char *uuid_bin)
> +{
> +	struct uuid *uuid = (struct uuid *)uuid_bin;

Here you cast a pointer to the (unaligned) character buffer to a
struct buffer, which requires alignment.

> +	unsigned int *ptr = (unsigned int *)uuid_bin;

> +	/* Set all fields randomly */
> +	for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
> +		*(ptr + i) = rand();

This code is dangerous - if the size of the struct should not be a
multiple of sizeof(uint), there would remain uninitialized data.

And note that it is likely that all these accesses are unaligned and
might cause exceptions.

> +	/* Set V4 format */
> +	uuid->time_hi_and_version &= UUID_VERSION_CLEAR_BITS;
> +	uuid->time_hi_and_version |= UUID_VERSION << UUID_VERSION_SHIFT;

Potentially unaligned accesses.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
As a general rule, the freedom of any people can  be  judged  by  the
volume of their laughter.


More information about the U-Boot mailing list