Re: [PATCH v4 2/3] random: introduce generic vDSO getrandom() implementation

Christophe Leroy
2022-11-18T19:34:40Z

Le 18/11/2022 à 18:28, Jason A. Donenfeld a écrit :
> Provide a generic C vDSO getrandom() implementation, which operates on
> an opaque state returned by vgetrandom_alloc() and produces random bytes
> the same way as getrandom(). This has a the API signature:
>
> ssize_t vgetrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state);
>

...

> diff --git a/lib/vdso/getrandom.c b/lib/vdso/getrandom.c
> new file mode 100644
> index 000000000000..b253e9247706
> --- /dev/null
> +++ b/lib/vdso/getrandom.c
> @@ -0,0 +1,109 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (C) 2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/atomic.h>
> +#include <linux/fs.h>
> +#include <vdso/datapage.h>
> +#include <asm/vdso/getrandom.h>
> +#include <asm/vdso/vsyscall.h>
> +#include "getrandom.h"
> +
> +#undef memcpy
> +#define memcpy(d,s,l) __builtin_memcpy(d,s,l)
> +#undef memset
> +#define memset(d,c,l) __builtin_memset(d,c,l)
> +
> +#define CHACHA_FOR_VDSO_INCLUDE
> +#include "../crypto/chacha.c"
> +
> +static void memcpy_and_zero(void *dst, void *src, size_t len)
> +{
> +#define CASCADE(type) \
> + while (len >= sizeof(type)) { \
> + *(type *)dst = *(type *)src; \
> + *(type *)src = 0; \
> + dst += sizeof(type); \
> + src += sizeof(type); \
> + len -= sizeof(type); \
> + }
> +#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
> +#if BITS_PER_LONG == 64
> + CASCADE(u64);
> +#endif
> + CASCADE(u32);
> + CASCADE(u16);
> +#endif
> + CASCADE(u8);
> +#undef CASCADE
> +}
> +
> +static __always_inline ssize_t
> +__cvdso_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state)
> +{
> + struct vgetrandom_state *state = opaque_state;
> + const struct vdso_rng_data *rng_info = __arch_get_vdso_rng_data();

In order to ease wiring up to powerpc, can it be done the same way as
commit e876f0b69dc9 ("lib/vdso: Allow architectures to provide the vdso
data pointer")

> + u32 chacha_state[CHACHA_STATE_WORDS];
> + ssize_t ret = min_t(size_t, MAX_RW_COUNT, len);
> + unsigned long current_generation;
> + size_t batch_len;
> +

Thanks,
Christophe