On Thu, Jun 18, 2009 at 09:34:56PM +0200, Denys Vlasenko wrote:
> diff -d -urpN busybox.5/util-linux/mkswap.c busybox.6/util-linux/mkswap.c
> --- busybox.5/util-linux/mkswap.c     2009-06-17 23:00:05.000000000 +0200
> +++ busybox.6/util-linux/mkswap.c     2009-06-18 21:31:51.000000000 +0200
> @@ -51,6 +51,28 @@ static void mkswap_selinux_setcontext(in
>  #define mkswap_selinux_setcontext(fd, path) ((void)0)
>  #endif
>  
> +static void mkswap_generate_uuid(uint8_t *buf)
> +{
> +     unsigned i;
> +     char uuid_string[32];
> +
> +     /* rand() is guaranteed to generate at least [0, 2^15) range,
> +      * but lowest bits in some libc are not so "random".  */
> +     srand((unsigned)monotonic_us() + getpid());
> +     for (i = 0; i < 16; i++)
> +             buf[i] = rand() >> 5;
> +
> +     bin2hex(uuid_string, (void*) buf, 16);
> +     /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
> +     printf("UUID=%.8s"  "-%.4s-%.4s-%.4s-%.12s\n",
> +             uuid_string,
> +             uuid_string+8,
> +             uuid_string+8+4,
> +             uuid_string+8+4+4,
> +             uuid_string+8+4+4+4
> +     );
> +}
> +
>  #if 0 /* from Linux 2.6.23 */
>  /*
>   * Magic header for a swap area. The first part of the union is

FWIW here's the IETF Standards Track RFC for UUID generation:

  http://www.ietf.org/rfc/rfc4122.txt

UUIDs are not just a single big random number; they have a structure,
which this code does not follow.

Furthermore, the algorithm above is only as random as the time returned
by CLOCK_MONOTONIC and the process ID; if those are identical on two
systems then it will produce identical results. This in fact means that
it is quite likely that UUIDs will fail to be universally unique if a
sufficient number of systems (not even that large; orders of magnitude
less than the Ubuntu installed base, for one) run busybox mkswap around
the same time.

-- 
Colin Watson                                       [[email protected]]
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to