Skip to contentSkip to conversation

aarch64: vdso: Wire up getrandom() vDSO implementation

PATCH Superseded Adhemerval Zanella 5 participants 29 messages Aug 26, 2024 – Aug 27, 2024

Subsystems (7)
x86 Kselftest Crypto API arm64 +3 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aarch64: vdso: Wire up getrandom() vDSO implementation

Hook up the generic vDSO implementation to the aarch64 vDSO data page. The _vdso_rng_data required data is placed within the _vdso_data vvar page, by using a offset larger than the vdso_data (__VDSO_RND_DATA_OFFSET).

The vDSO function requires a ChaCha20 implementation that does not write to the stack, and that can do an entire ChaCha20 permutation. The one provided is based on the current chacha-neon-core.S and uses NEON on the permute operation.

---
 arch/arm64/Kconfig                         |   1 +
 arch/arm64/include/asm/vdso/getrandom.h    |  50 +++++++
 arch/arm64/include/asm/vdso/vsyscall.h     |   9 ++
 arch/arm64/kernel/vdso/Makefile            |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S          |   4 +
 arch/arm64/kernel/vdso/vgetrandom-chacha.S | 153 +++++++++++++++++++++
 arch/arm64/kernel/vdso/vgetrandom.c        |  13 ++
 tools/testing/selftests/vDSO/Makefile      |   4 +-
 8 files changed, 238 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm64/include/asm/vdso/getrandom.h
 create mode 100644 arch/arm64/kernel/vdso/vgetrandom-chacha.S
 create mode 100644 arch/arm64/kernel/vdso/vgetrandom.c
arch/arm64/Kconfig +1 -0
index b3fc891f1544..e3f4c5bf0661 100644
@@ -237,6 +237,7 @@ config ARM64
 	select HAVE_KPROBES
 	select HAVE_KRETPROBES
 	select HAVE_GENERIC_VDSO
+	select VDSO_GETRANDOM
 	select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU
 	select IRQ_DOMAIN
 	select IRQ_FORCED_THREADING
arch/arm64/include/asm/vdso/getrandom.h new file +50 -0

50 lines not on this page · raw message

arch/arm64/include/asm/vdso/vsyscall.h +9 -0
index f94b1457c117..7ddb2bc3b57b 100644
@@ -2,6 +2,8 @@
 #ifndef __ASM_VDSO_VSYSCALL_H
 #define __ASM_VDSO_VSYSCALL_H
 
+#define __VDSO_RND_DATA_OFFSET  480
+
 #ifndef __ASSEMBLY__
 
 #include <linux/timekeeper_internal.h>
@@ -21,6 +23,13 @@ struct vdso_data *__arm64_get_k_vdso_data(void)
 }
 #define __arch_get_k_vdso_data __arm64_get_k_vdso_data
 
+static __always_inline
+struct vdso_rng_data *__arm64_get_k_vdso_rnd_data(void)
+{
+	return (void *)__arm64_get_k_vdso_data() + __VDSO_RND_DATA_OFFSET;
+}
+#define __arch_get_k_vdso_rng_data __arm64_get_k_vdso_rnd_data
+
 static __always_inline
 void __arm64_update_vsyscall(struct vdso_data *vdata, struct timekeeper *tk)
 {
arch/arm64/kernel/vdso/Makefile +6 -1
index d11da6461278..37dad3bb953a 100644
@@ -9,7 +9,7 @@
 # Include the generic Makefile to check the built vdso.
 include $(srctree)/lib/vdso/Makefile
 
-obj-vdso := vgettimeofday.o note.o sigreturn.o
+obj-vdso := vgettimeofday.o note.o sigreturn.o vgetrandom.o vgetrandom-chacha.o
 
 # Build rules
 targets := $(obj-vdso) vdso.so vdso.so.dbg
@@ -40,8 +40,13 @@ CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
 				$(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
 				$(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
 				-Wmissing-prototypes -Wmissing-declarations
+CFLAGS_REMOVE_vgetrandom.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
+			     $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
+			     $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
+			     -Wmissing-prototypes -Wmissing-declarations
 
 CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables
+CFLAGS_vgetrandom.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables
 
 ifneq ($(c-gettimeofday-y),)
   CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y)
arch/arm64/kernel/vdso/vdso.lds.S +4 -0
index 45354f2ddf70..f8dbcece20e2 100644
@@ -12,6 +12,8 @@
 #include <asm/page.h>
 #include <asm/vdso.h>
 #include <asm-generic/vmlinux.lds.h>
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>
 
 OUTPUT_FORMAT("elf64-littleaarch64", "elf64-bigaarch64", "elf64-littleaarch64")
 OUTPUT_ARCH(aarch64)
@@ -19,6 +21,7 @@ OUTPUT_ARCH(aarch64)
 SECTIONS
 {
 	PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE);
+	PROVIDE(_vdso_rng_data = _vdso_data + __VDSO_RND_DATA_OFFSET);
 #ifdef CONFIG_TIME_NS
 	PROVIDE(_timens_data = _vdso_data + PAGE_SIZE);
 #endif
@@ -102,6 +105,7 @@ VERSION
 		__kernel_gettimeofday;
 		__kernel_clock_gettime;
 		__kernel_clock_getres;
+		__kernel_getrandom;
 	local: *;
 	};
 }
arch/arm64/kernel/vdso/vgetrandom-chacha.S new file +153 -0

153 lines not on this page · raw message

arch/arm64/kernel/vdso/vgetrandom.c new file +13 -0
new file mode 100644
index 000000000000..b6d6f4db3a98
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/types.h>
+#include <linux/mm.h>
+
+#include "../../../../lib/vdso/getrandom.c"
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len);
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len)
+{
+	return __cvdso_getrandom(buffer, len, flags, opaque_state, opaque_len);
+}
tools/testing/selftests/vDSO/Makefile +2 -2
index 10ffdda3f2fa..f07ea679a4cc 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
 TEST_GEN_PROGS += vdso_standalone_test_x86
 endif
 TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
 TEST_GEN_PROGS += vdso_test_getrandom
 ifneq ($(SODIUM),)
 TEST_GEN_PROGS += vdso_test_chacha
Signature · 1 line
2.43.0
#1 lore raw

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

arch/arm64/include/asm/vdso/getrandom.h line 23

Quoted 6 lines · Adhemerval Zanella
arch/arm64/include/asm/vdso/getrandom.h new file +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+	register long int x8 asm ("x8") = __NR_getrandom;
+	register long int x0 asm ("x0") = (long int) buffer;
+	register long int x1 asm ("x1") = (long int) len;
+	register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Quoted 1 line

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

arch/arm64/kernel/vdso/vdso.lds.S line 16

Quoted 5 lines

#include <asm/page.h>
#include <asm/vdso.h>
#include <asm-generic/vmlinux.lds.h>

arch/arm64/kernel/vdso/vdso.lds.S +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Quoted 2 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Quoted 6 lines
tools/testing/selftests/vDSO/Makefile +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
 TEST_GEN_PROGS += vdso_standalone_test_x86
 endif
 TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
 TEST_GEN_PROGS += vdso_test_getrandom
 ifneq ($(SODIUM),)
 TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

  $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
  $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Jason

#2 lore raw
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

arch/arm64/include/asm/vdso/getrandom.h line 23

Quoted 6 lines
arch/arm64/include/asm/vdso/getrandom.h new file +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+	register long int x8 asm ("x8") = __NR_getrandom;
+	register long int x0 asm ("x0") = (long int) buffer;
+	register long int x1 asm ("x1") = (long int) len;
+	register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

arch/arm64/kernel/vdso/vdso.lds.S line 16

Quoted 6 lines

#include <asm/page.h>
#include <asm/vdso.h>
#include <asm-generic/vmlinux.lds.h>

arch/arm64/kernel/vdso/vdso.lds.S +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 7 lines
tools/testing/selftests/vDSO/Makefile +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
 TEST_GEN_PROGS += vdso_standalone_test_x86
 endif
 TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
 TEST_GEN_PROGS += vdso_test_getrandom
 ifneq ($(SODIUM),)
 TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
  $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
  $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64. In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make

  CC       vdso_test_gettimeofday
  CC       vdso_test_getcpu
  CC       vdso_test_abi
  CC       vdso_test_clock_getres
  CC       vdso_standalone_test_x86
  CC       vdso_test_correctness
  CC       vdso_test_getrandom
  CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7,
                 from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                 from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                 from /usr/include/limits.h:195,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                 from /usr/include/sodium/export.h:7,
                 from /usr/include/sodium/crypto_stream_chacha20.h:14,
                 from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
   99 | # if INT_MAX == 32767
      |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
  102 | #  if INT_MAX == 2147483647
      |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
  126 | # if LONG_MAX == 2147483647
      |      ^~~~~~~~

make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

I will try to figure out to be build it correctly, but I think it would be better to vgetrandom-chacha.S with a different rule.

#3 lore raw
Quoted 102 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

arch/arm64/include/asm/vdso/getrandom.h line 23

Quoted 6 lines
arch/arm64/include/asm/vdso/getrandom.h new file +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+	register long int x8 asm ("x8") = __NR_getrandom;
+	register long int x0 asm ("x0") = (long int) buffer;
+	register long int x1 asm ("x1") = (long int) len;
+	register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

arch/arm64/kernel/vdso/vdso.lds.S line 16

Quoted 6 lines

#include <asm/page.h>
#include <asm/vdso.h>
#include <asm-generic/vmlinux.lds.h>

arch/arm64/kernel/vdso/vdso.lds.S +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 7 lines
tools/testing/selftests/vDSO/Makefile +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
 TEST_GEN_PROGS += vdso_standalone_test_x86
 endif
 TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
 TEST_GEN_PROGS += vdso_test_getrandom
 ifneq ($(SODIUM),)
 TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
  $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
  $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64. In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make

  CC       vdso_test_gettimeofday
  CC       vdso_test_getcpu
  CC       vdso_test_abi
  CC       vdso_test_clock_getres
  CC       vdso_standalone_test_x86
  CC       vdso_test_correctness
  CC       vdso_test_getrandom
  CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7,
                 from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                 from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                 from /usr/include/limits.h:195,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                 from /usr/include/sodium/export.h:7,
                 from /usr/include/sodium/crypto_stream_chacha20.h:14,
                 from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
   99 | # if INT_MAX == 32767
      |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
  102 | #  if INT_MAX == 2147483647
      |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
  126 | # if LONG_MAX == 2147483647
      |      ^~~~~~~~
Quoted 1 line

make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

Jason

#4 lore raw
Quoted 105 lines · Jason A. Donenfeld
Quoted 102 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

arch/arm64/include/asm/vdso/getrandom.h line 23

Quoted 6 lines
arch/arm64/include/asm/vdso/getrandom.h new file +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+	register long int x8 asm ("x8") = __NR_getrandom;
+	register long int x0 asm ("x0") = (long int) buffer;
+	register long int x1 asm ("x1") = (long int) len;
+	register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

arch/arm64/kernel/vdso/vdso.lds.S line 16

Quoted 6 lines

#include <asm/page.h>
#include <asm/vdso.h>
#include <asm-generic/vmlinux.lds.h>

arch/arm64/kernel/vdso/vdso.lds.S +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 7 lines
tools/testing/selftests/vDSO/Makefile +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
 TEST_GEN_PROGS += vdso_standalone_test_x86
 endif
 TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
 TEST_GEN_PROGS += vdso_test_getrandom
 ifneq ($(SODIUM),)
 TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
  $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
  $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64. In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make

  CC       vdso_test_gettimeofday
  CC       vdso_test_getcpu
  CC       vdso_test_abi
  CC       vdso_test_clock_getres
  CC       vdso_standalone_test_x86
  CC       vdso_test_correctness
  CC       vdso_test_getrandom
  CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7,
                 from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                 from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                 from /usr/include/limits.h:195,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                 from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                 from /usr/include/sodium/export.h:7,
                 from /usr/include/sodium/crypto_stream_chacha20.h:14,
                 from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
   99 | # if INT_MAX == 32767
      |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
  102 | #  if INT_MAX == 2147483647
      |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
  126 | # if LONG_MAX == 2147483647
      |      ^~~~~~~~
Quoted 1 line

make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master. With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make

  CC       vdso_test_gettimeofday
  CC       vdso_test_getcpu
  CC       vdso_test_abi
  CC       vdso_test_clock_getres
  CC       vdso_standalone_test_x86
  CC       vdso_test_correctness
  CC       vdso_test_getrandom
  CC       vdso_test_chacha

/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

If I move -lsodium to the end of the compiler command it works.

#5 lore raw

Le 27/08/2024 à 15:39, Adhemerval Zanella Netto a écrit :

Quoted 123 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 105 lines · Jason A. Donenfeld
Quoted 102 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

arch/arm64/include/asm/vdso/getrandom.h line 23

Quoted 6 lines
arch/arm64/include/asm/vdso/getrandom.h new file +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+  register long int x8 asm ("x8") = __NR_getrandom;
+  register long int x0 asm ("x0") = (long int) buffer;
+  register long int x1 asm ("x1") = (long int) len;
+  register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

arch/arm64/kernel/vdso/vdso.lds.S line 16

Quoted 6 lines
  #include <asm/page.h>
  #include <asm/vdso.h>
  #include <asm-generic/vmlinux.lds.h>
arch/arm64/kernel/vdso/vdso.lds.S +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 7 lines
tools/testing/selftests/vDSO/Makefile +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
  SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
  TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
  TEST_GEN_PROGS += vdso_standalone_test_x86
  endif
  TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
  TEST_GEN_PROGS += vdso_test_getrandom
  ifneq ($(SODIUM),)
  TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
   $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
   $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64. In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make

   CC       vdso_test_gettimeofday
   CC       vdso_test_getcpu
   CC       vdso_test_abi
   CC       vdso_test_clock_getres
   CC       vdso_standalone_test_x86
   CC       vdso_test_correctness
   CC       vdso_test_getrandom
   CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7,
                  from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                  from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                  from /usr/include/limits.h:195,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                  from /usr/include/sodium/export.h:7,
                  from /usr/include/sodium/crypto_stream_chacha20.h:14,
                  from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
    99 | # if INT_MAX == 32767
       |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
   102 | #  if INT_MAX == 2147483647
       |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
   126 | # if LONG_MAX == 2147483647
       |      ^~~~~~~~
Quoted 1 line

make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master. With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make

   CC       vdso_test_gettimeofday
   CC       vdso_test_getcpu
   CC       vdso_test_abi
   CC       vdso_test_clock_getres
   CC       vdso_standalone_test_x86
   CC       vdso_test_correctness
   CC       vdso_test_getrandom
   CC       vdso_test_chacha
Quoted 7 lines

/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

If I move -lsodium to the end of the compiler command it works.

Try a "make clean" maybe ?

I have Fedora 38 and no build problem with latest random tree:

$ make V=1
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_gettimeofday.c parse_vdso.c -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_gettimeofday
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_getcpu.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getcpu
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_abi.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_abi
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_clock_getres.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_clock_getres
gcc -std=gnu99 -D_GNU_SOURCE= -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector    vdso_standalone_test_x86.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_standalone_test_x86
gcc -std=gnu99 -D_GNU_SOURCE=  -ldl  vdso_test_correctness.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_correctness
gcc -std=gnu99 -D_GNU_SOURCE= -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include/uapi 
   vdso_test_getrandom.c parse_vdso.c  -o 
/home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getrandom
gcc -std=gnu99 -D_GNU_SOURCE= -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../arch/x86/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 -Wa,--noexecstack -lsodium     vdso_test_chacha.c /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/arch/x86/vdso/vgetrandom-chacha.S 
 -o 
/home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_chacha
$
#6 lore raw
Quoted 138 lines · Christophe Leroy

Le 27/08/2024 à 15:39, Adhemerval Zanella Netto a écrit :

Quoted 127 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 105 lines · Jason A. Donenfeld
Quoted 95 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

arch/arm64/include/asm/vdso/getrandom.h line 23

Quoted 6 lines
arch/arm64/include/asm/vdso/getrandom.h new file +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+  register long int x8 asm ("x8") = __NR_getrandom;
+  register long int x0 asm ("x0") = (long int) buffer;
+  register long int x1 asm ("x1") = (long int) len;
+  register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET  480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

arch/arm64/kernel/vdso/vdso.lds.S line 16

Quoted 6 lines

  #include <asm/page.h>
  #include <asm/vdso.h>
  #include <asm-generic/vmlinux.lds.h>

arch/arm64/kernel/vdso/vdso.lds.S +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO.  Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 12 lines
tools/testing/selftests/vDSO/Makefile +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)

  SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)

  TEST_GEN_PROGS := vdso_test_gettimeofday

(quoted diff) +0 -0
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))

  TEST_GEN_PROGS += vdso_standalone_test_x86
  endif
  TEST_GEN_PROGS += vdso_test_correctness

tools/testing/selftests/vDSO/Makefile +1 -1
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
Quoted 3 lines

  TEST_GEN_PROGS += vdso_test_getrandom
  ifneq ($(SODIUM),)
  TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
   $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
   $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64.  In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make
   CC       vdso_test_gettimeofday
   CC       vdso_test_getcpu
   CC       vdso_test_abi
   CC       vdso_test_clock_getres
   CC       vdso_standalone_test_x86
   CC       vdso_test_correctness
   CC       vdso_test_getrandom
   CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7, from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                  from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                  from /usr/include/limits.h:195,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                  from /usr/include/sodium/export.h:7,
                  from /usr/include/sodium/crypto_stream_chacha20.h:14,
                  from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "(" 99 | # if INT_MAX == 32767

Quoted 8 lines

       |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "(" 102 | #  if INT_MAX == 2147483647
       |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "(" 126 | # if LONG_MAX == 2147483647
       |      ^~~~~~~~
make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master.  With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make
   CC       vdso_test_gettimeofday
   CC       vdso_test_getcpu
   CC       vdso_test_abi
   CC       vdso_test_clock_getres
   CC       vdso_standalone_test_x86
   CC       vdso_test_correctness
   CC       vdso_test_getrandom
   CC       vdso_test_chacha
/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

Quoted 3 lines

If I move -lsodium to the end of the compiler command it works.

Try a "make clean" maybe ?

I have Fedora 38 and no build problem with latest random tree:

Quoted 10 lines
$ make V=1
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_gettimeofday.c parse_vdso.c -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_gettimeofday
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_getcpu.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getcpu
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_abi.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_abi
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_clock_getres.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_clock_getres
gcc -std=gnu99 -D_GNU_SOURCE= -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector    vdso_standalone_test_x86.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_standalone_test_x86
gcc -std=gnu99 -D_GNU_SOURCE=  -ldl  vdso_test_correctness.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_correctness
gcc -std=gnu99 -D_GNU_SOURCE= -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include/uapi    vdso_test_getrandom.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getrandom
gcc -std=gnu99 -D_GNU_SOURCE= -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../arch/x86/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 -Wa,--noexecstack -lsodium     vdso_test_chacha.c /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/arch/x86/vdso/vgetrandom-chacha.S  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_chacha
$

It is a clean tree (git clean -dfx), and I take there is no need to build a kernel prior hand.

#7 lore raw

Le 27/08/2024 à 16:01, Adhemerval Zanella Netto a écrit :

Quoted 152 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 138 lines · Christophe Leroy
Quoted 123 lines · Adhemerval Zanella Netto

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 105 lines · Jason A. Donenfeld
Quoted 102 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+  register long int x8 asm ("x8") = __NR_getrandom;
+  register long int x0 asm ("x0") = (long int) buffer;
+  register long int x1 asm ("x1") = (long int) len;
+  register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

Review

Quoted 6 lines
   #include <asm/page.h>
   #include <asm/vdso.h>
   #include <asm-generic/vmlinux.lds.h>
(quoted diff) +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 7 lines
(quoted diff) +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
   SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
   TEST_GEN_PROGS := vdso_test_gettimeofday

Review ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
(quoted diff) +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
   TEST_GEN_PROGS += vdso_standalone_test_x86
   endif
   TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
   TEST_GEN_PROGS += vdso_test_getrandom
   ifneq ($(SODIUM),)
   TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
    $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
    $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64. In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make

    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7,
                   from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                   from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                   from /usr/include/limits.h:195,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                   from /usr/include/sodium/export.h:7,
                   from /usr/include/sodium/crypto_stream_chacha20.h:14,
                   from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
     99 | # if INT_MAX == 32767
        |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
    102 | #  if INT_MAX == 2147483647
        |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
    126 | # if LONG_MAX == 2147483647
        |      ^~~~~~~~
Quoted 1 line

make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master. With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make

    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
Quoted 7 lines

/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

If I move -lsodium to the end of the compiler command it works.

Try a "make clean" maybe ?

I have Fedora 38 and no build problem with latest random tree:

Quoted 10 lines
$ make V=1
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_gettimeofday.c parse_vdso.c -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_gettimeofday
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_getcpu.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getcpu
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_abi.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_abi
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_clock_getres.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_clock_getres
gcc -std=gnu99 -D_GNU_SOURCE= -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector    vdso_standalone_test_x86.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_standalone_test_x86
gcc -std=gnu99 -D_GNU_SOURCE=  -ldl  vdso_test_correctness.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_correctness
gcc -std=gnu99 -D_GNU_SOURCE= -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include/uapi    vdso_test_getrandom.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getrandom
gcc -std=gnu99 -D_GNU_SOURCE= -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../arch/x86/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 -Wa,--noexecstack -lsodium     vdso_test_chacha.c /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/arch/x86/vdso/vgetrandom-chacha.S  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_chacha
$
Quoted 2 lines

It is a clean tree (git clean -dfx), and I take there is no need to build a kernel prior hand.

I meeant 'make clean'

Right, I have not built any x86 kernel at the moment.

Just :

$ pwd
/home/chleroy/linux-powerpc/tools/testing/selftests/vDSO
$ make clean

then

$ make V=1

Christophe

#8 lore raw
Quoted 164 lines · Christophe Leroy

Le 27/08/2024 à 16:01, Adhemerval Zanella Netto a écrit :

Quoted 152 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 138 lines · Christophe Leroy
Quoted 127 lines · Adhemerval Zanella Netto

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 105 lines · Jason A. Donenfeld
Quoted 95 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+  register long int x8 asm ("x8") = __NR_getrandom;
+  register long int x0 asm ("x0") = (long int) buffer;
+  register long int x1 asm ("x1") = (long int) len;
+  register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET  480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

Review

Quoted 6 lines

   #include <asm/page.h>
   #include <asm/vdso.h>
   #include <asm-generic/vmlinux.lds.h>

(quoted diff) +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO.  Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 12 lines
(quoted diff) +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)

   SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)

   TEST_GEN_PROGS := vdso_test_gettimeofday

(quoted diff) +0 -0
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))

   TEST_GEN_PROGS += vdso_standalone_test_x86
   endif
   TEST_GEN_PROGS += vdso_test_correctness

(quoted diff) +1 -1
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
Quoted 3 lines

   TEST_GEN_PROGS += vdso_test_getrandom
   ifneq ($(SODIUM),)
   TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
    $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
    $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64.  In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make
    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7, from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                   from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                   from /usr/include/limits.h:195,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                   from /usr/include/sodium/export.h:7,
                   from /usr/include/sodium/crypto_stream_chacha20.h:14,
                   from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "(" 99 | # if INT_MAX == 32767

Quoted 8 lines

        |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "(" 102 | #  if INT_MAX == 2147483647
        |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "(" 126 | # if LONG_MAX == 2147483647
        |      ^~~~~~~~
make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master.  With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make
    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

Quoted 3 lines

If I move -lsodium to the end of the compiler command it works.

Try a "make clean" maybe ?

I have Fedora 38 and no build problem with latest random tree:

Quoted 10 lines
$ make V=1
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_gettimeofday.c parse_vdso.c -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_gettimeofday
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_getcpu.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getcpu
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_abi.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_abi
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_clock_getres.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_clock_getres
gcc -std=gnu99 -D_GNU_SOURCE= -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector    vdso_standalone_test_x86.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_standalone_test_x86
gcc -std=gnu99 -D_GNU_SOURCE=  -ldl  vdso_test_correctness.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_correctness
gcc -std=gnu99 -D_GNU_SOURCE= -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include/uapi    vdso_test_getrandom.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getrandom
gcc -std=gnu99 -D_GNU_SOURCE= -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../arch/x86/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 -Wa,--noexecstack -lsodium     vdso_test_chacha.c /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/arch/x86/vdso/vgetrandom-chacha.S  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_chacha
$
Quoted 2 lines

It is a clean tree (git clean -dfx), and I take there is no need to build a kernel prior hand.

I meeant 'make clean'

Right, I have not built any x86 kernel at the moment.

Just :

Quoted 8 lines
$ pwd
/home/chleroy/linux-powerpc/tools/testing/selftests/vDSO
$ make clean

then

$ make V=1

The issue is Ubuntu linker is configure to use --as-needed by default, this patch fixes the issue:

tools/testing/selftests/vDSO/Makefile +1 -1
index 10ffdda3f2fa..151baf650e4c 100644
@@ -45,4 +45,4 @@ $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack -Wl,-no-as-needed $(SODIUM)
#9 lore raw
Quoted 184 lines · Adhemerval Zanella Netto
Quoted 164 lines · Christophe Leroy

Le 27/08/2024 à 16:01, Adhemerval Zanella Netto a écrit :

Quoted 152 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 138 lines · Christophe Leroy
Quoted 127 lines · Adhemerval Zanella Netto

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 105 lines · Jason A. Donenfeld
Quoted 95 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+  register long int x8 asm ("x8") = __NR_getrandom;
+  register long int x0 asm ("x0") = (long int) buffer;
+  register long int x1 asm ("x1") = (long int) len;
+  register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET  480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

Review

Quoted 6 lines

   #include <asm/page.h>
   #include <asm/vdso.h>
   #include <asm-generic/vmlinux.lds.h>

(quoted diff) +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO.  Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 12 lines
(quoted diff) +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)

   SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)

   TEST_GEN_PROGS := vdso_test_gettimeofday

(quoted diff) +0 -0
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))

   TEST_GEN_PROGS += vdso_standalone_test_x86
   endif
   TEST_GEN_PROGS += vdso_test_correctness

(quoted diff) +1 -1
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
Quoted 3 lines

   TEST_GEN_PROGS += vdso_test_getrandom
   ifneq ($(SODIUM),)
   TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
    $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
    $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64.  In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make
    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7, from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                   from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                   from /usr/include/limits.h:195,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                   from /usr/include/sodium/export.h:7,
                   from /usr/include/sodium/crypto_stream_chacha20.h:14,
                   from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "(" 99 | # if INT_MAX == 32767

Quoted 8 lines

        |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "(" 102 | #  if INT_MAX == 2147483647
        |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "(" 126 | # if LONG_MAX == 2147483647
        |      ^~~~~~~~
make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master.  With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make
    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

Quoted 3 lines

If I move -lsodium to the end of the compiler command it works.

Try a "make clean" maybe ?

I have Fedora 38 and no build problem with latest random tree:

Quoted 10 lines
$ make V=1
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_gettimeofday.c parse_vdso.c -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_gettimeofday
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_getcpu.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getcpu
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_abi.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_abi
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_clock_getres.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_clock_getres
gcc -std=gnu99 -D_GNU_SOURCE= -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector    vdso_standalone_test_x86.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_standalone_test_x86
gcc -std=gnu99 -D_GNU_SOURCE=  -ldl  vdso_test_correctness.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_correctness
gcc -std=gnu99 -D_GNU_SOURCE= -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include/uapi    vdso_test_getrandom.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getrandom
gcc -std=gnu99 -D_GNU_SOURCE= -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../arch/x86/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 -Wa,--noexecstack -lsodium     vdso_test_chacha.c /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/arch/x86/vdso/vgetrandom-chacha.S  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_chacha
$
Quoted 2 lines

It is a clean tree (git clean -dfx), and I take there is no need to build a kernel prior hand.

I meeant 'make clean'

Right, I have not built any x86 kernel at the moment.

Just :

Quoted 8 lines
$ pwd
/home/chleroy/linux-powerpc/tools/testing/selftests/vDSO
$ make clean

then

$ make V=1

The issue is Ubuntu linker is configure to use --as-needed by default, this patch fixes the issue:

tools/testing/selftests/vDSO/Makefile +0 -0
index 10ffdda3f2fa..151baf650e4c 100644

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \ · line 48

Quoted 5 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -45,4 +45,4 @@ $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack -Wl,-no-as-needed $(SODIUM)

Oh, it's an as-needed thing. In that case, does this fix it for you?

tools/testing/selftests/vDSO/Makefile +5 -3
index 10ffdda3f2fa..834aa862ba2c 100644
@@ -1,7 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
 ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
-SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_LIBS := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_CFLAGS := $(shell pkg-config --cflags libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
 TEST_GEN_PROGS += vdso_test_getcpu
@@ -13,7 +14,7 @@ endif
 TEST_GEN_PROGS += vdso_test_correctness
 ifeq ($(uname_M),x86_64)
 TEST_GEN_PROGS += vdso_test_getrandom
-ifneq ($(SODIUM),)
+ifneq ($(SODIUM_LIBS),)
 TEST_GEN_PROGS += vdso_test_chacha
 endif
 endif
@@ -41,8 +42,9 @@ $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \
                                          -isystem $(top_srcdir)/include/uapi
 
 $(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(ARCH)/vdso/vgetrandom-chacha.S
+$(OUTPUT)/vdso_test_chacha: LDLIBS += $(SODIUM_LIBS)
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack $(SODIUM_CFLAGS)
#10 lore raw
Quoted 218 lines · Jason A. Donenfeld
Quoted 184 lines · Adhemerval Zanella Netto
Quoted 164 lines · Christophe Leroy

Le 27/08/2024 à 16:01, Adhemerval Zanella Netto a écrit :

Quoted 152 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 138 lines · Christophe Leroy
Quoted 127 lines · Adhemerval Zanella Netto

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 105 lines · Jason A. Donenfeld
Quoted 95 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+  register long int x8 asm ("x8") = __NR_getrandom;
+  register long int x0 asm ("x0") = (long int) buffer;
+  register long int x1 asm ("x1") = (long int) len;
+  register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET  480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

Review

Quoted 6 lines

   #include <asm/page.h>
   #include <asm/vdso.h>
   #include <asm-generic/vmlinux.lds.h>

(quoted diff) +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO.  Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 12 lines
(quoted diff) +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)

   SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)

   TEST_GEN_PROGS := vdso_test_gettimeofday

(quoted diff) +0 -0
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))

   TEST_GEN_PROGS += vdso_standalone_test_x86
   endif
   TEST_GEN_PROGS += vdso_test_correctness

(quoted diff) +1 -1
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
Quoted 3 lines

   TEST_GEN_PROGS += vdso_test_getrandom
   ifneq ($(SODIUM),)
   TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
    $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
    $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64.  In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make
    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7, from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                   from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                   from /usr/include/limits.h:195,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                   from /usr/include/sodium/export.h:7,
                   from /usr/include/sodium/crypto_stream_chacha20.h:14,
                   from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "(" 99 | # if INT_MAX == 32767

Quoted 8 lines

        |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "(" 102 | #  if INT_MAX == 2147483647
        |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "(" 126 | # if LONG_MAX == 2147483647
        |      ^~~~~~~~
make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master.  With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make
    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

Quoted 3 lines

If I move -lsodium to the end of the compiler command it works.

Try a "make clean" maybe ?

I have Fedora 38 and no build problem with latest random tree:

Quoted 10 lines
$ make V=1
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_gettimeofday.c parse_vdso.c -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_gettimeofday
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_getcpu.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getcpu
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_abi.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_abi
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_clock_getres.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_clock_getres
gcc -std=gnu99 -D_GNU_SOURCE= -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector    vdso_standalone_test_x86.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_standalone_test_x86
gcc -std=gnu99 -D_GNU_SOURCE=  -ldl  vdso_test_correctness.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_correctness
gcc -std=gnu99 -D_GNU_SOURCE= -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include/uapi    vdso_test_getrandom.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getrandom
gcc -std=gnu99 -D_GNU_SOURCE= -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../arch/x86/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 -Wa,--noexecstack -lsodium     vdso_test_chacha.c /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/arch/x86/vdso/vgetrandom-chacha.S  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_chacha
$
Quoted 2 lines

It is a clean tree (git clean -dfx), and I take there is no need to build a kernel prior hand.

I meeant 'make clean'

Right, I have not built any x86 kernel at the moment.

Just :

Quoted 8 lines
$ pwd
/home/chleroy/linux-powerpc/tools/testing/selftests/vDSO
$ make clean

then

$ make V=1

The issue is Ubuntu linker is configure to use --as-needed by default, this patch fixes the issue:

tools/testing/selftests/vDSO/Makefile +0 -0
index 10ffdda3f2fa..151baf650e4c 100644

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \ · line 48

Quoted 5 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -45,4 +45,4 @@ $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack -Wl,-no-as-needed $(SODIUM)

Oh, it's an as-needed thing. In that case, does this fix it for you?

tools/testing/selftests/vDSO/Makefile +3 -2
index 10ffdda3f2fa..834aa862ba2c 100644
@@ -1,7 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
 ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
-SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_LIBS := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_CFLAGS := $(shell pkg-config --cflags libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
 TEST_GEN_PROGS += vdso_test_getcpu
@@ -13,7 +14,7 @@ endif
 TEST_GEN_PROGS += vdso_test_correctness
 ifeq ($(uname_M),x86_64)
 TEST_GEN_PROGS += vdso_test_getrandom
-ifneq ($(SODIUM),)
+ifneq ($(SODIUM_LIBS),)
 TEST_GEN_PROGS += vdso_test_chacha
 endif
 endif

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \ · line 50

Quoted 10 lines
tools/testing/selftests/vDSO/Makefile +2 -1
@@ -41,8 +42,9 @@ $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \
                                          -isystem $(top_srcdir)/include/uapi
 
 $(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(ARCH)/vdso/vgetrandom-chacha.S
+$(OUTPUT)/vdso_test_chacha: LDLIBS += $(SODIUM_LIBS)
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack $(SODIUM_CFLAGS)

Nops, 'pkg-config --cflags libsodium' is empty. The -Wl,-no-as-needed is simpler I think.

#11 lore raw
Quoted 223 lines · Adhemerval Zanella Netto
Quoted 219 lines · Jason A. Donenfeld
Quoted 184 lines · Adhemerval Zanella Netto
Quoted 164 lines · Christophe Leroy
Quoted 152 lines · Adhemerval Zanella Netto

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 138 lines · Christophe Leroy
Quoted 123 lines · Adhemerval Zanella Netto

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 105 lines · Jason A. Donenfeld
Quoted 102 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+  register long int x8 asm ("x8") = __NR_getrandom;
+  register long int x0 asm ("x0") = (long int) buffer;
+  register long int x1 asm ("x1") = (long int) len;
+  register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

Review

Quoted 6 lines
   #include <asm/page.h>
   #include <asm/vdso.h>
   #include <asm-generic/vmlinux.lds.h>
(quoted diff) +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 7 lines
(quoted diff) +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
   SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
   TEST_GEN_PROGS := vdso_test_gettimeofday

Review ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
(quoted diff) +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
   TEST_GEN_PROGS += vdso_standalone_test_x86
   endif
   TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
   TEST_GEN_PROGS += vdso_test_getrandom
   ifneq ($(SODIUM),)
   TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
    $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
    $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64. In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make

    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7,
                   from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                   from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                   from /usr/include/limits.h:195,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                   from /usr/include/sodium/export.h:7,
                   from /usr/include/sodium/crypto_stream_chacha20.h:14,
                   from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
     99 | # if INT_MAX == 32767
        |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
    102 | #  if INT_MAX == 2147483647
        |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
    126 | # if LONG_MAX == 2147483647
        |      ^~~~~~~~
Quoted 1 line

make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master. With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make

    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
Quoted 7 lines

/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

If I move -lsodium to the end of the compiler command it works.

Try a "make clean" maybe ?

I have Fedora 38 and no build problem with latest random tree:

Quoted 10 lines
$ make V=1
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_gettimeofday.c parse_vdso.c -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_gettimeofday
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_getcpu.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getcpu
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_abi.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_abi
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_clock_getres.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_clock_getres
gcc -std=gnu99 -D_GNU_SOURCE= -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector    vdso_standalone_test_x86.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_standalone_test_x86
gcc -std=gnu99 -D_GNU_SOURCE=  -ldl  vdso_test_correctness.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_correctness
gcc -std=gnu99 -D_GNU_SOURCE= -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include/uapi    vdso_test_getrandom.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getrandom
gcc -std=gnu99 -D_GNU_SOURCE= -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../arch/x86/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 -Wa,--noexecstack -lsodium     vdso_test_chacha.c /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/arch/x86/vdso/vgetrandom-chacha.S  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_chacha
$
Quoted 2 lines

It is a clean tree (git clean -dfx), and I take there is no need to build a kernel prior hand.

I meeant 'make clean'

Right, I have not built any x86 kernel at the moment.

Just :

Quoted 8 lines
$ pwd
/home/chleroy/linux-powerpc/tools/testing/selftests/vDSO
$ make clean

then

$ make V=1

The issue is Ubuntu linker is configure to use --as-needed by default, this patch fixes the issue:

tools/testing/selftests/vDSO/Makefile +0 -0
index 10ffdda3f2fa..151baf650e4c 100644

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \ · line 48

Quoted 5 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -45,4 +45,4 @@ $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack -Wl,-no-as-needed $(SODIUM)

Oh, it's an as-needed thing. In that case, does this fix it for you?

tools/testing/selftests/vDSO/Makefile +3 -2
index 10ffdda3f2fa..834aa862ba2c 100644
@@ -1,7 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
 ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
-SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_LIBS := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_CFLAGS := $(shell pkg-config --cflags libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
 TEST_GEN_PROGS += vdso_test_getcpu
@@ -13,7 +14,7 @@ endif
 TEST_GEN_PROGS += vdso_test_correctness
 ifeq ($(uname_M),x86_64)
 TEST_GEN_PROGS += vdso_test_getrandom
-ifneq ($(SODIUM),)
+ifneq ($(SODIUM_LIBS),)
 TEST_GEN_PROGS += vdso_test_chacha
 endif
 endif
@@ -41,8 +42,9 @@ $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \
                                          -isystem $(top_srcdir)/include/uapi
 
Quoted 12 lines

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \ · line 50

Quoted 9 lines
tools/testing/selftests/vDSO/Makefile +2 -1
 $(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(ARCH)/vdso/vgetrandom-chacha.S
+$(OUTPUT)/vdso_test_chacha: LDLIBS += $(SODIUM_LIBS)
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack $(SODIUM_CFLAGS)

Nops, 'pkg-config --cflags libsodium' is empty. The -Wl,-no-as-needed is simpler I think.

The --cflags thing is for a different issue Ruoyao found. My intended fix here was the LDLIBS += $(SODIUM_LIBS) part, which moves the -lsodium closer to the end of the command line. But it still doesn't work? Surprising...

#12 lore raw
Quoted 238 lines · Jason A. Donenfeld
Quoted 223 lines · Adhemerval Zanella Netto
Quoted 219 lines · Jason A. Donenfeld
Quoted 184 lines · Adhemerval Zanella Netto
Quoted 164 lines · Christophe Leroy
Quoted 152 lines · Adhemerval Zanella Netto

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 138 lines · Christophe Leroy
Quoted 123 lines · Adhemerval Zanella Netto

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 105 lines · Jason A. Donenfeld
Quoted 102 lines · Adhemerval Zanella Netto
Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+  register long int x8 asm ("x8") = __NR_getrandom;
+  register long int x0 asm ("x0") = (long int) buffer;
+  register long int x1 asm ("x1") = (long int) len;
+  register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

Review

Quoted 6 lines
   #include <asm/page.h>
   #include <asm/vdso.h>
   #include <asm-generic/vmlinux.lds.h>
(quoted diff) +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 7 lines
(quoted diff) +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
   SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
   TEST_GEN_PROGS := vdso_test_gettimeofday

Review ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
(quoted diff) +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
   TEST_GEN_PROGS += vdso_standalone_test_x86
   endif
   TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
   TEST_GEN_PROGS += vdso_test_getrandom
   ifneq ($(SODIUM),)
   TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
    $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
    $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64. In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make

    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7,
                   from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                   from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                   from /usr/include/limits.h:195,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                   from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                   from /usr/include/sodium/export.h:7,
                   from /usr/include/sodium/crypto_stream_chacha20.h:14,
                   from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
     99 | # if INT_MAX == 32767
        |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
    102 | #  if INT_MAX == 2147483647
        |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
    126 | # if LONG_MAX == 2147483647
        |      ^~~~~~~~
Quoted 1 line

make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

Quoted 3 lines

You get that even with the latest random.git? I thought Christophe's patch fixed that, but maybe not and I should just remove the dependency on the sodium header instead.

On x86_64 I tested with Linux master. With random.git it is a different issue:

linux-git/tools/testing/selftests/vDSO$ make

    CC       vdso_test_gettimeofday
    CC       vdso_test_getcpu
    CC       vdso_test_abi
    CC       vdso_test_clock_getres
    CC       vdso_standalone_test_x86
    CC       vdso_test_correctness
    CC       vdso_test_getrandom
    CC       vdso_test_chacha
Quoted 7 lines

/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

If I move -lsodium to the end of the compiler command it works.

Try a "make clean" maybe ?

I have Fedora 38 and no build problem with latest random tree:

Quoted 10 lines
$ make V=1
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_gettimeofday.c parse_vdso.c -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_gettimeofday
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_getcpu.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getcpu
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_abi.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_abi
gcc -std=gnu99 -D_GNU_SOURCE=    vdso_test_clock_getres.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_clock_getres
gcc -std=gnu99 -D_GNU_SOURCE= -nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector    vdso_standalone_test_x86.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_standalone_test_x86
gcc -std=gnu99 -D_GNU_SOURCE=  -ldl  vdso_test_correctness.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_correctness
gcc -std=gnu99 -D_GNU_SOURCE= -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -isystem /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include/uapi    vdso_test_getrandom.c parse_vdso.c  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_getrandom
gcc -std=gnu99 -D_GNU_SOURCE= -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../arch/x86/include -idirafter /home/chleroy/linux-powerpc/tools/testing/selftests/../../../include -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 -Wa,--noexecstack -lsodium     vdso_test_chacha.c /home/chleroy/linux-powerpc/tools/testing/selftests/../../../tools/arch/x86/vdso/vgetrandom-chacha.S  -o /home/chleroy/linux-powerpc/tools/testing/selftests/vDSO/vdso_test_chacha
$
Quoted 2 lines

It is a clean tree (git clean -dfx), and I take there is no need to build a kernel prior hand.

I meeant 'make clean'

Right, I have not built any x86 kernel at the moment.

Just :

Quoted 8 lines
$ pwd
/home/chleroy/linux-powerpc/tools/testing/selftests/vDSO
$ make clean

then

$ make V=1

The issue is Ubuntu linker is configure to use --as-needed by default, this patch fixes the issue:

tools/testing/selftests/vDSO/Makefile +0 -0
index 10ffdda3f2fa..151baf650e4c 100644

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \ · line 48

Quoted 5 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -45,4 +45,4 @@ $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack -Wl,-no-as-needed $(SODIUM)

Oh, it's an as-needed thing. In that case, does this fix it for you?

tools/testing/selftests/vDSO/Makefile +3 -2
index 10ffdda3f2fa..834aa862ba2c 100644
@@ -1,7 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
 ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
-SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_LIBS := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_CFLAGS := $(shell pkg-config --cflags libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
 TEST_GEN_PROGS += vdso_test_getcpu
@@ -13,7 +14,7 @@ endif
 TEST_GEN_PROGS += vdso_test_correctness
 ifeq ($(uname_M),x86_64)
 TEST_GEN_PROGS += vdso_test_getrandom
-ifneq ($(SODIUM),)
+ifneq ($(SODIUM_LIBS),)
 TEST_GEN_PROGS += vdso_test_chacha
 endif
 endif
@@ -41,8 +42,9 @@ $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \
                                          -isystem $(top_srcdir)/include/uapi
 
Quoted 12 lines

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \ · line 50

Quoted 9 lines
tools/testing/selftests/vDSO/Makefile +2 -1
 $(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(ARCH)/vdso/vgetrandom-chacha.S
+$(OUTPUT)/vdso_test_chacha: LDLIBS += $(SODIUM_LIBS)
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack $(SODIUM_CFLAGS)

Nops, 'pkg-config --cflags libsodium' is empty. The -Wl,-no-as-needed is simpler I think.

Quoted 4 lines

The --cflags thing is for a different issue Ruoyao found. My intended fix here was the LDLIBS += $(SODIUM_LIBS) part, which moves the -lsodium closer to the end of the command line. But it still doesn't work? Surprising...

Oops, it does work indeed (my mistake here).

#13 lore raw

PATCH 1/1 selftests/vDSO: separate LDLIBS from CFLAGS for libsodium

On systems that set -Wl,--as-needed, putting the -lsodium in the wrong place on the command line means we get a linker error:

CC vdso_test_chacha
/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

Fix this by passing pkg-config's --libs output to the LDFLAGS field instead of the CFLAGS field, as is customary.

---
 tools/testing/selftests/vDSO/Makefile | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/testing/selftests/vDSO/Makefile +5 -3
index 180854eb9fec..834aa862ba2c 100644
@@ -1,7 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
 ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
-SODIUM := $(shell pkg-config --libs --cflags libsodium 2>/dev/null)
+SODIUM_LIBS := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_CFLAGS := $(shell pkg-config --cflags libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
 TEST_GEN_PROGS += vdso_test_getcpu
@@ -13,7 +14,7 @@ endif
 TEST_GEN_PROGS += vdso_test_correctness
 ifeq ($(uname_M),x86_64)
 TEST_GEN_PROGS += vdso_test_getrandom
-ifneq ($(SODIUM),)
+ifneq ($(SODIUM_LIBS),)
 TEST_GEN_PROGS += vdso_test_chacha
 endif
 endif
@@ -41,8 +42,9 @@ $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \
                                          -isystem $(top_srcdir)/include/uapi
 
 $(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(ARCH)/vdso/vgetrandom-chacha.S
+$(OUTPUT)/vdso_test_chacha: LDLIBS += $(SODIUM_LIBS)
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack $(SODIUM_CFLAGS)
Signature · 1 line
2.46.0
author Reviewed-by on v1 1/1
#14 lore raw

Re: [PATCH] selftests/vDSO: separate LDLIBS from CFLAGS for libsodium

Quoted 41 lines · Jason A. Donenfeld

On systems that set -Wl,--as-needed, putting the -lsodium in the wrong place on the command line means we get a linker error:

CC vdso_test_chacha
/usr/bin/ld: /tmp/ccKpjnSM.o: in function `main':
vdso_test_chacha.c:(.text+0x276): undefined reference to `crypto_stream_chacha20'
collect2: error: ld returned 1 exit status

Fix this by passing pkg-config's --libs output to the LDFLAGS field instead of the CFLAGS field, as is customary.

---
 tools/testing/selftests/vDSO/Makefile | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/testing/selftests/vDSO/Makefile +3 -2
index 180854eb9fec..834aa862ba2c 100644
@@ -1,7 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
 ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
-SODIUM := $(shell pkg-config --libs --cflags libsodium 2>/dev/null)
+SODIUM_LIBS := $(shell pkg-config --libs libsodium 2>/dev/null)
+SODIUM_CFLAGS := $(shell pkg-config --cflags libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
 TEST_GEN_PROGS += vdso_test_getcpu
@@ -13,7 +14,7 @@ endif
 TEST_GEN_PROGS += vdso_test_correctness
 ifeq ($(uname_M),x86_64)
 TEST_GEN_PROGS += vdso_test_getrandom
-ifneq ($(SODIUM),)
+ifneq ($(SODIUM_LIBS),)
 TEST_GEN_PROGS += vdso_test_chacha
 endif
 endif

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \ · line 49

Quoted 8 lines
tools/testing/selftests/vDSO/Makefile +1 -0
@@ -41,8 +42,9 @@ $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \
                                          -isystem $(top_srcdir)/include/uapi
 
 $(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(ARCH)/vdso/vgetrandom-chacha.S
+$(OUTPUT)/vdso_test_chacha: LDLIBS += $(SODIUM_LIBS)
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -idirafter $(top_srcdir)/arch/$(ARCH)/include \
                                       -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \

This seems to be wrong '-DBULID_VDSO'.

tools/testing/selftests/vDSO/Makefile $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \ · line 50

Quoted 2 lines
tools/testing/selftests/vDSO/Makefile +1 -1
-                                      -Wa,--noexecstack $(SODIUM)
+                                      -Wa,--noexecstack $(SODIUM_CFLAGS)

LGTM, thanks.

#15 lore raw

Re: [PATCH] selftests/vDSO: separate LDLIBS from CFLAGS for libsodium

Quoted 3 lines · Adhemerval Zanella Netto
Quoted 1 line
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \

This seems to be wrong '-DBULID_VDSO'.

Holy smokes, thanks. It turns out that wasn't needed, and neither is CONFIG_FUNCTION_ALIGNMENT now. I'll send a patch removing these.

#16 lore raw
In reply to Adhemerval Zanella Netto

Le 27/08/2024 à 15:17, Adhemerval Zanella Netto a écrit :

Quoted 102 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Quoted 7 lines · Jason A. Donenfeld

Hi Adhemerval,

Thanks for posting this! Exciting to have it here.

Just some small nits for now:

On Mon, Aug 26, 2024 at 06:10:40PM +0000, Adhemerval Zanella wrote:

Quoted 9 lines

arch/arm64/include/asm/vdso/getrandom.h line 23

Quoted 6 lines
arch/arm64/include/asm/vdso/getrandom.h new file +6 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+    register long int x8 asm ("x8") = __NR_getrandom;
+    register long int x0 asm ("x0") = (long int) buffer;
+    register long int x1 asm ("x1") = (long int) len;
+    register long int x2 asm ("x2") = (long int) flags;

Usually it's written just as long or unsigned long, and likewise with the cast. Also, no space after the cast.

Ack.

Quoted 4 lines
Quoted 2 lines

+#define __VDSO_RND_DATA_OFFSET 480

This is the size of the data currently there?

Yes, I used the same strategy x86 did.

Quoted 8 lines

arch/arm64/kernel/vdso/vdso.lds.S line 16

Quoted 6 lines
  #include <asm/page.h>
  #include <asm/vdso.h>
  #include <asm-generic/vmlinux.lds.h>
arch/arm64/kernel/vdso/vdso.lds.S +2 -0
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>

Possible to keep the asm/ together?

Ack.

Quoted 5 lines
Quoted 3 lines

+ * ARM64 ChaCha20 implementation meant for vDSO. Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes

nonnce -> nonce

Ack.

Quoted 18 lines
Quoted 7 lines
tools/testing/selftests/vDSO/Makefile +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
  SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
  TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 8 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
  TEST_GEN_PROGS += vdso_standalone_test_x86
  endif
  TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
  TEST_GEN_PROGS += vdso_test_getrandom
  ifneq ($(SODIUM),)
  TEST_GEN_PROGS += vdso_test_chacha

You'll need to add the symlink to get the chacha selftest running:

Quoted 4 lines
   $ ln -s ../../../arch/arm64/kernel/vdso tools/arch/arm64/vdso
   $ git add tools/arch/arm64/vdso

Also, can you confirm that the chacha selftest runs and works?

Yes, last time I has to built it manually since the Makefile machinery seem to be broken even on x86_64. In a Ubuntu vm I have:

tools/testing/selftests/vDSO$ make

   CC       vdso_test_gettimeofday
   CC       vdso_test_getcpu
   CC       vdso_test_abi
   CC       vdso_test_clock_getres
   CC       vdso_standalone_test_x86
   CC       vdso_test_correctness
   CC       vdso_test_getrandom
   CC       vdso_test_chacha
In file included from /home/azanella/Projects/linux/linux-git/include/linux/limits.h:7,
                  from /usr/include/x86_64-linux-gnu/bits/local_lim.h:38,
                  from /usr/include/x86_64-linux-gnu/bits/posix1_lim.h:161,
                  from /usr/include/limits.h:195,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:205,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/syslimits.h:7,
                  from /usr/lib/gcc/x86_64-linux-gnu/13/include/limits.h:34,
                  from /usr/include/sodium/export.h:7,
                  from /usr/include/sodium/crypto_stream_chacha20.h:14,
                  from vdso_test_chacha.c:6:
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
    99 | # if INT_MAX == 32767
       |      ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
   102 | #  if INT_MAX == 2147483647
       |       ^~~~~~~
/usr/include/x86_64-linux-gnu/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
   126 | # if LONG_MAX == 2147483647
       |      ^~~~~~~~
Quoted 5 lines

make: *** [../lib.mk:222: /home/azanella/Projects/linux/linux-git/tools/testing/selftests/vDSO/vdso_test_chacha] Error 1

I will try to figure out to be build it correctly, but I think it would be better to vgetrandom-chacha.S with a different rule.

Hi, can you try with the following commit : https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git/commit/?id=e1af61334ade39a9af3031b7189f9acb419648a4

Thanks
Christophe

#17 lore raw
In reply to Adhemerval Zanella

arch/arm64/include/asm/vdso/getrandom.h line 28

Quoted 11 lines · Adhemerval Zanella
arch/arm64/include/asm/vdso/getrandom.h new file +11 -0
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+	register long int x8 asm ("x8") = __NR_getrandom;
+	register long int x0 asm ("x0") = (long int) buffer;
+	register long int x1 asm ("x1") = (long int) len;
+	register long int x2 asm ("x2") = (long int) flags;
+
+	asm ("svc 0" : "=r"(x0) : "r"(x8), "0"(x0), "r"(x1), "r"(x2));
+
+	return x0;
+}

More generally, it might be best to follow the format used by arch/arm64/include/asm/vdso/gettimeofday.h.

#18 lore raw
In reply to Adhemerval Zanella

Le 26/08/2024 à 20:10, Adhemerval Zanella a écrit :

Quoted 21 lines

Hook up the generic vDSO implementation to the aarch64 vDSO data page. The _vdso_rng_data required data is placed within the _vdso_data vvar page, by using a offset larger than the vdso_data (__VDSO_RND_DATA_OFFSET).

The vDSO function requires a ChaCha20 implementation that does not write to the stack, and that can do an entire ChaCha20 permutation. The one provided is based on the current chacha-neon-core.S and uses NEON on the permute operation.

---
  arch/arm64/Kconfig                         |   1 +
  arch/arm64/include/asm/vdso/getrandom.h    |  50 +++++++
  arch/arm64/include/asm/vdso/vsyscall.h     |   9 ++
  arch/arm64/kernel/vdso/Makefile            |   7 +-
  arch/arm64/kernel/vdso/vdso.lds.S          |   4 +
  arch/arm64/kernel/vdso/vgetrandom-chacha.S | 153 +++++++++++++++++++++
  arch/arm64/kernel/vdso/vgetrandom.c        |  13 ++
  tools/testing/selftests/vDSO/Makefile      |   4 +-
  8 files changed, 238 insertions(+), 3 deletions(-)
Quoted 3 lines

create mode 100644 arch/arm64/include/asm/vdso/getrandom.h create mode 100644 arch/arm64/kernel/vdso/vgetrandom-chacha.S create mode 100644 arch/arm64/kernel/vdso/vgetrandom.c

Were you able to use selftests ? I think you are missing the symbolic link to vdso directory (assuming you are using latest master branch from https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git)

arch/arm64/Kconfig config ARM64 · line 240

Quoted 10 lines
arch/arm64/Kconfig +1 -0
index b3fc891f1544..e3f4c5bf0661 100644
@@ -237,6 +237,7 @@ config ARM64
  	select HAVE_KPROBES
  	select HAVE_KRETPROBES
  	select HAVE_GENERIC_VDSO
+	select VDSO_GETRANDOM

You don't keep things in alphabetical here order on ARM64 ?

Quoted 45 lines
arch/arm64/Kconfig +0 -0
  	select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU
  	select IRQ_DOMAIN
  	select IRQ_FORCED_THREADING
arch/arm64/include/asm/vdso/getrandom.h new file +36 -0
new file mode 100644
index 000000000000..6e2b136813ca
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __ASM_VDSO_GETRANDOM_H
+#define __ASM_VDSO_GETRANDOM_H
+
+#ifndef __ASSEMBLY__
+
+#include <asm/unistd.h>
+#include <vdso/datapage.h>
+
+/**
+ * getrandom_syscall - Invoke the getrandom() syscall.
+ * @buffer:	Destination buffer to fill with random bytes.
+ * @len:	Size of @buffer in bytes.
+ * @flags:	Zero or more GRND_* flags.
+ * Returns:	The number of random bytes written to @buffer, or a negative value indicating an error.
+ */
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+	register long int x8 asm ("x8") = __NR_getrandom;
+	register long int x0 asm ("x0") = (long int) buffer;
+	register long int x1 asm ("x1") = (long int) len;
+	register long int x2 asm ("x2") = (long int) flags;
+
+	asm ("svc 0" : "=r"(x0) : "r"(x8), "0"(x0), "r"(x1), "r"(x2));
+
+	return x0;
+}
+
+static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void)
+{
+	return &_vdso_rng_data;
+}
+
+/**
+ * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.

arch/arm64/include/asm/vdso/getrandom.h line 46

Quoted 10 lines
arch/arm64/include/asm/vdso/getrandom.h new file +10 -0
+ * @dst_bytes:	Destination buffer to hold @nblocks * 64 bytes of output.
+ * @key:	32-byte input key.
+ * @counter:	8-byte counter, read on input and updated on return.
+ * @nblocks:	Number of blocks to generate.
+ *
+ * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write
+ * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
+ * leaking into forked child processes.
+ */
+extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);

For Jason: We all redefine this prototype, should we have it in a central place, or do you expect some architecture to provide some static inline for it ?

Quoted 9 lines
arch/arm64/include/asm/vdso/getrandom.h new file +4 -0
+
+#endif /* !__ASSEMBLY__ */
+
+#endif /* __ASM_VDSO_GETRANDOM_H */
arch/arm64/include/asm/vdso/vsyscall.h +0 -0
index f94b1457c117..7ddb2bc3b57b 100644

arch/arm64/include/asm/vdso/vsyscall.h line 6

Quoted 5 lines
arch/arm64/include/asm/vdso/vsyscall.h +2 -0
@@ -2,6 +2,8 @@
  #ifndef __ASM_VDSO_VSYSCALL_H
  #define __ASM_VDSO_VSYSCALL_H
 
+#define __VDSO_RND_DATA_OFFSET  480
+

How is this offset calculated or defined ? What happens if the other structures grow ? Could you use some sizeof(something) instead of something from asm-offsets if you also need it in ASM ?

arch/arm64/kernel/vdso/vgetrandom-chacha.S

Quoted 74 lines
arch/arm64/include/asm/vdso/vsyscall.h +7 -0
  #ifndef __ASSEMBLY__
 
  #include <linux/timekeeper_internal.h>
@@ -21,6 +23,13 @@ struct vdso_data *__arm64_get_k_vdso_data(void)
  }
  #define __arch_get_k_vdso_data __arm64_get_k_vdso_data
 
+static __always_inline
+struct vdso_rng_data *__arm64_get_k_vdso_rnd_data(void)
+{
+	return (void *)__arm64_get_k_vdso_data() + __VDSO_RND_DATA_OFFSET;
+}
+#define __arch_get_k_vdso_rng_data __arm64_get_k_vdso_rnd_data
+
  static __always_inline
  void __arm64_update_vsyscall(struct vdso_data *vdata, struct timekeeper *tk)
  {
arch/arm64/kernel/vdso/Makefile +6 -1
index d11da6461278..37dad3bb953a 100644
@@ -9,7 +9,7 @@
  # Include the generic Makefile to check the built vdso.
  include $(srctree)/lib/vdso/Makefile
 
-obj-vdso := vgettimeofday.o note.o sigreturn.o
+obj-vdso := vgettimeofday.o note.o sigreturn.o vgetrandom.o vgetrandom-chacha.o
 
  # Build rules
  targets := $(obj-vdso) vdso.so vdso.so.dbg
@@ -40,8 +40,13 @@ CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
  				$(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
  				$(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
  				-Wmissing-prototypes -Wmissing-declarations
+CFLAGS_REMOVE_vgetrandom.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
+			     $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
+			     $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
+			     -Wmissing-prototypes -Wmissing-declarations
 
  CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables
+CFLAGS_vgetrandom.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables
 
  ifneq ($(c-gettimeofday-y),)
    CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y)
arch/arm64/kernel/vdso/vdso.lds.S +4 -0
index 45354f2ddf70..f8dbcece20e2 100644
@@ -12,6 +12,8 @@
  #include <asm/page.h>
  #include <asm/vdso.h>
  #include <asm-generic/vmlinux.lds.h>
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>
 
  OUTPUT_FORMAT("elf64-littleaarch64", "elf64-bigaarch64", "elf64-littleaarch64")
  OUTPUT_ARCH(aarch64)
@@ -19,6 +21,7 @@ OUTPUT_ARCH(aarch64)
  SECTIONS
  {
  	PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE);
+	PROVIDE(_vdso_rng_data = _vdso_data + __VDSO_RND_DATA_OFFSET);
  #ifdef CONFIG_TIME_NS
  	PROVIDE(_timens_data = _vdso_data + PAGE_SIZE);
  #endif
@@ -102,6 +105,7 @@ VERSION
  		__kernel_gettimeofday;
  		__kernel_clock_gettime;
  		__kernel_clock_getres;
+		__kernel_getrandom;
  	local: *;
  	};
  }
arch/arm64/kernel/vdso/vgetrandom-chacha.S +0 -0

[skipped ASM as I have not spoken ARM asm since I was at school in the 90's]

arch/arm64/kernel/vdso/vgetrandom.c line 6

Quoted 12 lines
arch/arm64/kernel/vdso/vgetrandom.c new file +6 -0
new file mode 100644
index 000000000000..b6d6f4db3a98
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/types.h>
+#include <linux/mm.h>
+
+#include "../../../../lib/vdso/getrandom.c"

For gettimeofday ARM64 uses c-gettimeofday-y in the Makefile instead.

You should do the same with c-getrandom-y

Quoted 21 lines
arch/arm64/kernel/vdso/vgetrandom.c new file +7 -0
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len);
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len)
+{
+	return __cvdso_getrandom(buffer, len, flags, opaque_state, opaque_len);
+}
tools/testing/selftests/vDSO/Makefile +1 -1
index 10ffdda3f2fa..f07ea679a4cc 100644
@@ -1,6 +1,6 @@
  # SPDX-License-Identifier: GPL-2.0
  uname_M := $(shell uname -m 2>/dev/null || echo not)
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 
  SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 

TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 14

Quoted 5 lines
tools/testing/selftests/vDSO/Makefile +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
  TEST_GEN_PROGS += vdso_standalone_test_x86
  endif
  TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))

Does that work for you when you cross-compile ? For powerpc when I cross compile I still get the x86_64 from uname_M here, which is unexpected.

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 17

Quoted 3 lines
tools/testing/selftests/vDSO/Makefile +0 -0
  TEST_GEN_PROGS += vdso_test_getrandom
  ifneq ($(SODIUM),)
  TEST_GEN_PROGS += vdso_test_chacha

Christophe

#19 lore raw
Quoted 8 lines · Christophe Leroy
Quoted 8 lines

+/**
+ * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.
+ * @dst_bytes: Destination buffer to hold @nblocks * 64 bytes of output.
+ * @key: 32-byte input key.
+ * @counter: 8-byte counter, read on input and updated on return.
+ * @nblocks: Number of blocks to generate.
+ *
+ * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write

Quoted 8 lines
Quoted 4 lines

+ * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
+ * leaking into forked child processes.
+ */
+extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);

For Jason: We all redefine this prototype, should we have it in a central place, or do you expect some architecture to provide some static inline for it ?

Given the doc comment and such, that would be nice. But I didn't see a straight forward way of doing that when I tried before. If you want to try and send another fixup commit, that'd be welcomed.

Quoted 6 lines

arch/arm64/include/asm/vdso/vsyscall.h line 6

Quoted 2 lines
arch/arm64/include/asm/vdso/vsyscall.h +2 -0
+#define __VDSO_RND_DATA_OFFSET  480
+

How is this offset calculated or defined ? What happens if the other structures grow ? Could you use some sizeof(something) instead of something from asm-offsets if you also need it in ASM ?

FYI, there's a similar static calculation like this in the x86 code:

+#if !defined(_SINGLE_DATA)
+#define _SINGLE_DATA
+DECLARE_VVAR_SINGLE(640, struct vdso_rng_data, _vdso_rng_data) +#endif

Quoted 6 lines
Quoted 6 lines
  uname_M := $(shell uname -m 2>/dev/null || echo not)
tools/testing/selftests/vDSO/Makefile +1 -1
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 
  SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
Quoted 10 lines

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 14

Quoted 7 lines

TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile +1 -1
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
  TEST_GEN_PROGS += vdso_standalone_test_x86
  endif
  TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))

Does that work for you when you cross-compile ? For powerpc when I cross compile I still get the x86_64 from uname_M here, which is unexpected.

That sounds like a legitimate bug you're pointing out, but not one with Adhemerval's code, right? Rather, it's something to be fixed inside of these self tests as a whole?

Jason

#20 lore raw
Quoted 19 lines · Jason A. Donenfeld
Quoted 8 lines · Christophe Leroy
Quoted 8 lines

+/**
+ * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.
+ * @dst_bytes: Destination buffer to hold @nblocks * 64 bytes of output.
+ * @key: 32-byte input key.
+ * @counter: 8-byte counter, read on input and updated on return.
+ * @nblocks: Number of blocks to generate.
+ *
+ * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write

Quoted 8 lines
Quoted 4 lines

+ * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
+ * leaking into forked child processes.
+ */
+extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);

For Jason: We all redefine this prototype, should we have it in a central place, or do you expect some architecture to provide some static inline for it ?

Quoted 3 lines

Given the doc comment and such, that would be nice. But I didn't see a straight forward way of doing that when I tried before. If you want to try and send another fixup commit, that'd be welcomed.

I'll give it a shot.

#21 lore raw

PATCH 1/1 random: vDSO: move prototype of arch chacha function to vdso/getrandom.h

Having the prototype for __arch_chacha20_blocks_nostack in arch/x86/include/asm/vdso/getrandom.h meant that the prototype and large doc comment were cloned by every architecture, which has been causing unnecessary churn. Instead move it into include/vdso/getrandom.h, where it can be shared by all archs implementing it.

---
 arch/x86/include/asm/vdso/getrandom.h | 13 -------------
 include/vdso/getrandom.h              | 13 +++++++++++++
 2 files changed, 13 insertions(+), 13 deletions(-)
arch/x86/include/asm/vdso/getrandom.h +0 -13
index b96e674cafde..ff5334ad32a0 100644
@@ -37,19 +37,6 @@ static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void
 	return &__vdso_rng_data;
 }
 
-/**
- * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.
- * @dst_bytes:	Destination buffer to hold @nblocks * 64 bytes of output.
- * @key:	32-byte input key.
- * @counter:	8-byte counter, read on input and updated on return.
- * @nblocks:	Number of blocks to generate.
- *
- * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write
- * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
- * leaking into forked child processes.
- */
-extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);
-
 #endif /* !__ASSEMBLY__ */
 
 #endif /* __ASM_VDSO_GETRANDOM_H */
include/vdso/getrandom.h +13 -0
index a8b7c14b0ae0..4cf02e678f5e 100644
@@ -43,4 +43,17 @@ struct vgetrandom_state {
 	bool 			in_use;
 };
 
+/**
+ * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.
+ * @dst_bytes:	Destination buffer to hold @nblocks * 64 bytes of output.
+ * @key:	32-byte input key.
+ * @counter:	8-byte counter, read on input and updated on return.
+ * @nblocks:	Number of blocks to generate.
+ *
+ * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write
+ * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
+ * leaking into forked child processes.
+ */
+extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);
+
 #endif /* _VDSO_GETRANDOM_H */
Signature · 1 line
2.46.0
#22 lore raw

PATCH v2 1/1 random: vDSO: move prototype of arch chacha function to vdso/getrandom.h

Having the prototype for __arch_chacha20_blocks_nostack in arch/x86/include/asm/vdso/getrandom.h meant that the prototype and large doc comment were cloned by every architecture, which has been causing unnecessary churn. Instead move it into include/vdso/getrandom.h, where it can be shared by all archs implementing it.

As a side bonus, this then lets us use that prototype in the vdso_test_chacha self test, to ensure that it matches the source, and indeed doing so turned up some inconsistencies, which are rectified here.

---
 arch/x86/include/asm/vdso/getrandom.h           | 13 -------------
 include/vdso/getrandom.h                        | 13 +++++++++++++
 tools/testing/selftests/vDSO/vdso_test_chacha.c | 10 +++++++---
 3 files changed, 20 insertions(+), 16 deletions(-)
arch/x86/include/asm/vdso/getrandom.h +0 -13
index b96e674cafde..ff5334ad32a0 100644
@@ -37,19 +37,6 @@ static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void
 	return &__vdso_rng_data;
 }
 
-/**
- * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.
- * @dst_bytes:	Destination buffer to hold @nblocks * 64 bytes of output.
- * @key:	32-byte input key.
- * @counter:	8-byte counter, read on input and updated on return.
- * @nblocks:	Number of blocks to generate.
- *
- * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write
- * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
- * leaking into forked child processes.
- */
-extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);
-
 #endif /* !__ASSEMBLY__ */
 
 #endif /* __ASM_VDSO_GETRANDOM_H */
include/vdso/getrandom.h +13 -0
index a8b7c14b0ae0..4cf02e678f5e 100644
@@ -43,4 +43,17 @@ struct vgetrandom_state {
 	bool 			in_use;
 };
 
+/**
+ * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.
+ * @dst_bytes:	Destination buffer to hold @nblocks * 64 bytes of output.
+ * @key:	32-byte input key.
+ * @counter:	8-byte counter, read on input and updated on return.
+ * @nblocks:	Number of blocks to generate.
+ *
+ * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write
+ * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
+ * leaking into forked child processes.
+ */
+extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);
+
 #endif /* _VDSO_GETRANDOM_H */
tools/testing/selftests/vDSO/vdso_test_chacha.c +7 -3
index e38f44e5f803..ca5639d02969 100644
@@ -7,16 +7,20 @@
 #include <sys/random.h>
 #include <string.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include "../kselftest.h"
 
-extern void __arch_chacha20_blocks_nostack(uint8_t *dst_bytes, const uint8_t *key, uint32_t *counter, size_t nblocks);
+typedef uint8_t u8;
+typedef uint32_t u32;
+typedef uint64_t u64;
+#include <vdso/getrandom.h>
 
 int main(int argc, char *argv[])
 {
 	enum { TRIALS = 1000, BLOCKS = 128, BLOCK_SIZE = 64 };
 	static const uint8_t nonce[8] = { 0 };
 	uint32_t counter[2];
-	uint8_t key[32];
+	uint32_t key[8];
 	uint8_t output1[BLOCK_SIZE * BLOCKS], output2[BLOCK_SIZE * BLOCKS];
 
 	ksft_print_header();
@@ -27,7 +31,7 @@ int main(int argc, char *argv[])
 			printf("getrandom() failed!\n");
 			return KSFT_SKIP;
 		}
-		crypto_stream_chacha20(output1, sizeof(output1), nonce, key);
+		crypto_stream_chacha20(output1, sizeof(output1), nonce, (uint8_t *)key);
 		for (unsigned int split = 0; split < BLOCKS; ++split) {
 			memset(output2, 'X', sizeof(output2));
 			memset(counter, 0, sizeof(counter));
Signature · 1 line
2.46.0
#23 lore raw

Re: [PATCH v2] random: vDSO: move prototype of arch chacha function to vdso/getrandom.h

Le 27/08/2024 à 17:47, Jason A. Donenfeld a écrit :

Quoted 10 lines

Having the prototype for __arch_chacha20_blocks_nostack in arch/x86/include/asm/vdso/getrandom.h meant that the prototype and large doc comment were cloned by every architecture, which has been causing unnecessary churn. Instead move it into include/vdso/getrandom.h, where it can be shared by all archs implementing it.

As a side bonus, this then lets us use that prototype in the vdso_test_chacha self test, to ensure that it matches the source, and indeed doing so turned up some inconsistencies, which are rectified here.

Side bonus that I dislike. Or ... it is all that u32 key stuff that I dislike.

If it was really u32 I would be able to read it with a LWZ instruction (Load Word Zero extended). That's what I did at the begining. But if I want the selftest to work, I have to use LWBRX (Load Word Byte Reversed ...)instead because the bytes in the word are in reversed order in reality.

So either it is a table of 32 bytes, or it is as defined in RFC 7539:

A 256-bit key, treated as a concatenation of eight 32-bit
little-endian integers.

And in that case it is not a table of 8x u32 but table of 8x __le32

Quoted 42 lines

---

tools/testing/selftests/vDSO/vdso_test_chacha.c +7 -3
index e38f44e5f803..ca5639d02969 100644
@@ -7,16 +7,20 @@
  #include <sys/random.h>
  #include <string.h>
  #include <stdint.h>
+#include <stdbool.h>
  #include "../kselftest.h"
 
-extern void __arch_chacha20_blocks_nostack(uint8_t *dst_bytes, const uint8_t *key, uint32_t *counter, size_t nblocks);
+typedef uint8_t u8;
+typedef uint32_t u32;
+typedef uint64_t u64;
+#include <vdso/getrandom.h>
 
  int main(int argc, char *argv[])
  {
  	enum { TRIALS = 1000, BLOCKS = 128, BLOCK_SIZE = 64 };
  	static const uint8_t nonce[8] = { 0 };
  	uint32_t counter[2];
-	uint8_t key[32];
+	uint32_t key[8];
  	uint8_t output1[BLOCK_SIZE * BLOCKS], output2[BLOCK_SIZE * BLOCKS];
 
  	ksft_print_header();
@@ -27,7 +31,7 @@ int main(int argc, char *argv[])
  			printf("getrandom() failed!\n");
  			return KSFT_SKIP;
  		}
-		crypto_stream_chacha20(output1, sizeof(output1), nonce, key);
+		crypto_stream_chacha20(output1, sizeof(output1), nonce, (uint8_t *)key);
  		for (unsigned int split = 0; split < BLOCKS; ++split) {
  			memset(output2, 'X', sizeof(output2));
  			memset(counter, 0, sizeof(counter));
#24 lore raw

Re: [PATCH v2] random: vDSO: move prototype of arch chacha function to vdso/getrandom.h

Quoted 23 lines · Christophe Leroy
Quoted 10 lines · Jason A. Donenfeld

Having the prototype for __arch_chacha20_blocks_nostack in arch/x86/include/asm/vdso/getrandom.h meant that the prototype and large doc comment were cloned by every architecture, which has been causing unnecessary churn. Instead move it into include/vdso/getrandom.h, where it can be shared by all archs implementing it.

As a side bonus, this then lets us use that prototype in the vdso_test_chacha self test, to ensure that it matches the source, and indeed doing so turned up some inconsistencies, which are rectified here.

Side bonus that I dislike. Or ... it is all that u32 key stuff that I dislike.

If it was really u32 I would be able to read it with a LWZ instruction (Load Word Zero extended). That's what I did at the begining. But if I want the selftest to work, I have to use LWBRX (Load Word Byte Reversed ...)instead because the bytes in the word are in reversed order in reality.

Quoted 6 lines

So either it is a table of 32 bytes, or it is as defined in RFC 7539:

A 256-bit key, treated as a concatenation of eight 32-bit
little-endian integers.

And in that case it is not a table of 8x u32 but table of 8x __le32

It's a table of bytes that are 4-byte aligned. Or, sure, a table of __le32.

#25 lore raw
In reply to Christophe Leroy
Quoted 28 lines · Christophe Leroy

Le 26/08/2024 à 20:10, Adhemerval Zanella a écrit :

Quoted 20 lines

Hook up the generic vDSO implementation to the aarch64 vDSO data page. The _vdso_rng_data required data is placed within the _vdso_data vvar page, by using a offset larger than the vdso_data (__VDSO_RND_DATA_OFFSET).

The vDSO function requires a ChaCha20 implementation that does not write to the stack, and that can do an entire ChaCha20 permutation. The one provided is based on the current chacha-neon-core.S and uses NEON on the permute operation.

---
  arch/arm64/Kconfig                         |   1 + arch/arm64/include/asm/vdso/getrandom.h    |  50 +++++++ arch/arm64/include/asm/vdso/vsyscall.h     |   9 ++ arch/arm64/kernel/vdso/Makefile            |   7 +- arch/arm64/kernel/vdso/vdso.lds.S          |   4 + arch/arm64/kernel/vdso/vgetrandom-chacha.S | 153 +++++++++++++++++++++ arch/arm64/kernel/vdso/vgetrandom.c        |  13 ++ tools/testing/selftests/vDSO/Makefile      |   4 +-

Quoted 4 lines
  8 files changed, 238 insertions(+), 3 deletions(-)

  create mode 100644 arch/arm64/include/asm/vdso/getrandom.h create mode 100644 arch/arm64/kernel/vdso/vgetrandom-chacha.S create mode 100644 arch/arm64/kernel/vdso/vgetrandom.c

Quoted 1 line

Were you able to use selftests ? I think you are missing the symbolic link to vdso directory (assuming you are using latest master branch from https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git)

It is missing indeed, last time I use a old brach that has a different Makefile machinery and it I could it built more easily.

Quoted 13 lines
Quoted 11 lines
arch/arm64/Kconfig +0 -0
index b3fc891f1544..e3f4c5bf0661 100644
@@ -237,6 +237,7 @@ config ARM64

      select HAVE_KPROBES
      select HAVE_KRETPROBES
      select HAVE_GENERIC_VDSO
+    select VDSO_GETRANDOM

You don't keep things in alphabetical here order on ARM64 ?

It seems to most part, but the file does have some outliers (HAVE_SOFTIRQ_ON_OWN_STACK
for instance). I moved to the end of the list.

Quoted 59 lines
Quoted 46 lines

      select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU select IRQ_DOMAIN
      select IRQ_FORCED_THREADING

arch/arm64/include/asm/vdso/getrandom.h new file +36 -0
new file mode 100644
index 000000000000..6e2b136813ca
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __ASM_VDSO_GETRANDOM_H
+#define __ASM_VDSO_GETRANDOM_H
+
+#ifndef __ASSEMBLY__
+
+#include <asm/unistd.h>
+#include <vdso/datapage.h>
+
+/**
+ * getrandom_syscall - Invoke the getrandom() syscall.
+ * @buffer:    Destination buffer to fill with random bytes.
+ * @len:    Size of @buffer in bytes.
+ * @flags:    Zero or more GRND_* flags.
+ * Returns:    The number of random bytes written to @buffer, or a negative value indicating an error.
+ */
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+    register long int x8 asm ("x8") = __NR_getrandom;
+    register long int x0 asm ("x0") = (long int) buffer;
+    register long int x1 asm ("x1") = (long int) len;
+    register long int x2 asm ("x2") = (long int) flags;
+
+    asm ("svc 0" : "=r"(x0) : "r"(x8), "0"(x0), "r"(x1), "r"(x2));
+
+    return x0;
+}
+
+static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void)
+{
+    return &_vdso_rng_data;
+}
+
+/**
+ * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.

arch/arm64/include/asm/vdso/getrandom.h line 46

Quoted 10 lines
arch/arm64/include/asm/vdso/getrandom.h new file +10 -0
+ * @dst_bytes:    Destination buffer to hold @nblocks * 64 bytes of output.
+ * @key:    32-byte input key.
+ * @counter:    8-byte counter, read on input and updated on return.
+ * @nblocks:    Number of blocks to generate.
+ *
+ * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write
+ * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
+ * leaking into forked child processes.
+ */
+extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);

For Jason: We all redefine this prototype, should we have it in a central place, or do you expect some architecture to provide some static inline for it ?

Quoted 15 lines
Quoted 11 lines
arch/arm64/include/asm/vdso/getrandom.h new file +4 -0
+
+#endif /* !__ASSEMBLY__ */
+
+#endif /* __ASM_VDSO_GETRANDOM_H */
arch/arm64/include/asm/vdso/vsyscall.h +0 -0
index f94b1457c117..7ddb2bc3b57b 100644
@@ -2,6 +2,8 @@
   #ifndef __ASM_VDSO_VSYSCALL_H
   #define __ASM_VDSO_VSYSCALL_H
Quoted 2 lines

  +#define __VDSO_RND_DATA_OFFSET  480
+

How is this offset calculated or defined ? What happens if the other structures grow ? Could you use some sizeof(something) instead of something from asm-offsets if you also need it in ASM ?

That is something I talked to Jason some time ago, since a similar strategy to use a 'magic' offset is used on x86_64. Ideally I think the vdso_rnd_data should be moved to a common static structure along with _vdso_data, so the there is no need to come up with magic offset like this. It seems that the powerpc does follow this pattern, but other ports no.

However, since each architecture does some specific machinery with the vdso datapages; it would require some more extensive refactoring on multiple architectures to get this right.

Quoted 86 lines

arch/arm64/kernel/vdso/vgetrandom-chacha.S

Quoted 68 lines

  #ifndef __ASSEMBLY__
    #include <linux/timekeeper_internal.h>

arch/arm64/include/asm/vdso/vsyscall.h +0 -0
@@ -21,6 +23,13 @@ struct vdso_data *__arm64_get_k_vdso_data(void)
   }
   #define __arch_get_k_vdso_data __arm64_get_k_vdso_data

  +static __always_inline

arch/arm64/include/asm/vdso/vsyscall.h +6 -0
+struct vdso_rng_data *__arm64_get_k_vdso_rnd_data(void)
+{
+    return (void *)__arm64_get_k_vdso_data() + __VDSO_RND_DATA_OFFSET;
+}
+#define __arch_get_k_vdso_rng_data __arm64_get_k_vdso_rnd_data
+

  static __always_inline
  void __arm64_update_vsyscall(struct vdso_data *vdata, struct timekeeper *tk) {

arch/arm64/kernel/vdso/Makefile +0 -0
index d11da6461278..37dad3bb953a 100644
@@ -9,7 +9,7 @@
   # Include the generic Makefile to check the built vdso.

  include $(srctree)/lib/vdso/Makefile
  -obj-vdso := vgettimeofday.o note.o sigreturn.o
+obj-vdso := vgettimeofday.o note.o sigreturn.o vgetrandom.o vgetrandom-chacha.o # Build rules
  targets := $(obj-vdso) vdso.so vdso.so.dbg

arch/arm64/kernel/vdso/Makefile +0 -0
@@ -40,8 +40,13 @@ CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
                   $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
                   $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \

                  -Wmissing-prototypes -Wmissing-declarations +CFLAGS_REMOVE_vgetrandom.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
+                 $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
+                 $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
+                 -Wmissing-prototypes -Wmissing-declarations CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables +CFLAGS_vgetrandom.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables ifneq ($(c-gettimeofday-y),)
    CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y)

arch/arm64/kernel/vdso/vdso.lds.S +2 -0
index 45354f2ddf70..f8dbcece20e2 100644
@@ -12,6 +12,8 @@
   #include <asm/page.h>
   #include <asm/vdso.h>
   #include <asm-generic/vmlinux.lds.h>
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>
     OUTPUT_FORMAT("elf64-littleaarch64", "elf64-bigaarch64", "elf64-littleaarch64")
   OUTPUT_ARCH(aarch64)
@@ -19,6 +21,7 @@ OUTPUT_ARCH(aarch64)

  SECTIONS
  {
      PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE); +    PROVIDE(_vdso_rng_data = _vdso_data + __VDSO_RND_DATA_OFFSET); #ifdef CONFIG_TIME_NS
      PROVIDE(_timens_data = _vdso_data + PAGE_SIZE); #endif

arch/arm64/kernel/vdso/vdso.lds.S +1 -0
@@ -102,6 +105,7 @@ VERSION
           __kernel_gettimeofday;
           __kernel_clock_gettime;
           __kernel_clock_getres;
+        __kernel_getrandom;
       local: *;
       };
   }
arch/arm64/kernel/vdso/vgetrandom-chacha.S +0 -0

[skipped ASM as I have not spoken ARM asm since I was at school in the 90's]

arch/arm64/kernel/vdso/vgetrandom.c line 6

Quoted 12 lines
arch/arm64/kernel/vdso/vgetrandom.c new file +6 -0
new file mode 100644
index 000000000000..b6d6f4db3a98
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/types.h>
+#include <linux/mm.h>
+
+#include "../../../../lib/vdso/getrandom.c"

For gettimeofday ARM64 uses c-gettimeofday-y in the Makefile instead.

Quoted 1 line

You should do the same with c-getrandom-y

Ack.

Quoted 18 lines
Quoted 17 lines
arch/arm64/kernel/vdso/vgetrandom.c new file +7 -0
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len);
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len)
+{
+    return __cvdso_getrandom(buffer, len, flags, opaque_state, opaque_len);
+}
tools/testing/selftests/vDSO/Makefile +1 -1
index 10ffdda3f2fa..f07ea679a4cc 100644
@@ -1,6 +1,6 @@
   # SPDX-License-Identifier: GPL-2.0
   uname_M := $(shell uname -m 2>/dev/null || echo not)
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
Quoted 10 lines

tools/testing/selftests/vDSO/Makefile ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64)) · line 14

Quoted 8 lines
tools/testing/selftests/vDSO/Makefile +0 -0
   SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)

    TEST_GEN_PROGS := vdso_test_gettimeofday

tools/testing/selftests/vDSO/Makefile +0 -0
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))

  TEST_GEN_PROGS += vdso_standalone_test_x86
  endif
  TEST_GEN_PROGS += vdso_test_correctness

tools/testing/selftests/vDSO/Makefile +1 -1
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))

Does that work for you when you cross-compile ? For powerpc when I cross compile I still get the x86_64 from uname_M here, which is unexpected.

Right, I haven't test cross-compilation on the selftests so I am not sure. I will check it.

Quoted 6 lines
Quoted 4 lines

  TEST_GEN_PROGS += vdso_test_getrandom
  ifneq ($(SODIUM),)
  TEST_GEN_PROGS += vdso_test_chacha

Christophe

#26 lore raw
In reply to Adhemerval Zanella

Hi Adhemerval,

...

Quoted 20 lines
arch/arm64/kernel/vdso/vgetrandom-chacha.S new file +14 -0
new file mode 100644
index 000000000000..3fb9715dd6f0
@@ -0,0 +1,153 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/linkage.h>
+#include <asm/cache.h>
+
+       .text
+
+/*
+ * ARM64 ChaCha20 implementation meant for vDSO.  Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes
+ * counter.  Importantly does not spill to the stack.
+ *
+ * void __arch_chacha20_blocks_nostack(uint8_t *dst_bytes,
+ *                                    const uint8_t *key,

arch/arm64/kernel/vdso/vgetrandom-chacha.S line 24

Quoted 10 lines
arch/arm64/kernel/vdso/vgetrandom-chacha.S new file +10 -0
+ *                                    uint32_t *counter,
+ *                                    size_t nblocks)
+ *
+ *     x0: output bytes
+ *     x1: 32-byte key input
+ *     x2: 8-byte counter input/output
+ *     x3: number of 64-byte block to write to output
+ */
+SYM_FUNC_START(__arch_chacha20_blocks_nostack)
+

Shouldn't we preserve d8-d15 here?

Quoted 129 lines
arch/arm64/kernel/vdso/vgetrandom-chacha.S new file +129 -0
+       /* v0 = "expand 32-byte k" */
+       adr_l           x8, CTES
+       ld1             {v5.4s}, [x8]
+       /* v1,v2 = key */
+       ld1             { v6.4s, v7.4s }, [x1]
+       /* v3 = counter || zero noonce  */
+       ldr             d8, [x2]
+
+       adr_l           x8, ONE
+       ldr             q13, [x8]
+
+       adr_l           x10, ROT8
+       ld1             {v12.4s}, [x10]
+.Lblock:
+       /* copy state to auxiliary vectors for the final add after the permute.  */
+       mov             v0.16b, v5.16b
+       mov             v1.16b, v6.16b
+       mov             v2.16b, v7.16b
+       mov             v3.16b, v8.16b
+
+       mov             w4, 20
+.Lpermute:
+       /*
+        * Permute one 64-byte block where the state matrix is stored in the four NEON
+        * registers v0-v3.  It performs matrix operations on four words in parallel,
+        * but requires shuffling to rearrange the words after each round.
+        */
+
+.Ldoubleround:
+       /* x0 += x1, x3 = rotl32(x3 ^ x0, 16) */
+       add             v0.4s, v0.4s, v1.4s
+       eor             v3.16b, v3.16b, v0.16b
+       rev32           v3.8h, v3.8h
+
+       /* x2 += x3, x1 = rotl32(x1 ^ x2, 12) */
+       add             v2.4s, v2.4s, v3.4s
+       eor             v4.16b, v1.16b, v2.16b
+       shl             v1.4s, v4.4s, #12
+       sri             v1.4s, v4.4s, #20
+
+       /* x0 += x1, x3 = rotl32(x3 ^ x0, 8) */
+       add             v0.4s, v0.4s, v1.4s
+       eor             v3.16b, v3.16b, v0.16b
+       tbl             v3.16b, {v3.16b}, v12.16b
+
+       /* x2 += x3, x1 = rotl32(x1 ^ x2, 7) */
+       add             v2.4s, v2.4s, v3.4s
+       eor             v4.16b, v1.16b, v2.16b
+       shl             v1.4s, v4.4s, #7
+       sri             v1.4s, v4.4s, #25
+
+       /* x1 = shuffle32(x1, MASK(0, 3, 2, 1)) */
+       ext             v1.16b, v1.16b, v1.16b, #4
+       /* x2 = shuffle32(x2, MASK(1, 0, 3, 2)) */
+       ext             v2.16b, v2.16b, v2.16b, #8
+       /* x3 = shuffle32(x3, MASK(2, 1, 0, 3)) */
+       ext             v3.16b, v3.16b, v3.16b, #12
+
+       /* x0 += x1, x3 = rotl32(x3 ^ x0, 16) */
+       add             v0.4s, v0.4s, v1.4s
+       eor             v3.16b, v3.16b, v0.16b
+       rev32           v3.8h, v3.8h
+
+       /* x2 += x3, x1 = rotl32(x1 ^ x2, 12) */
+       add             v2.4s, v2.4s, v3.4s
+       eor             v4.16b, v1.16b, v2.16b
+       shl             v1.4s, v4.4s, #12
+       sri             v1.4s, v4.4s, #20
+
+       /* x0 += x1, x3 = rotl32(x3 ^ x0, 8) */
+       add             v0.4s, v0.4s, v1.4s
+       eor             v3.16b, v3.16b, v0.16b
+       tbl             v3.16b, {v3.16b}, v12.16b
+
+       /* x2 += x3, x1 = rotl32(x1 ^ x2, 7) */
+       add             v2.4s, v2.4s, v3.4s
+       eor             v4.16b, v1.16b, v2.16b
+       shl             v1.4s, v4.4s, #7
+       sri             v1.4s, v4.4s, #25
+
+       /* x1 = shuffle32(x1, MASK(2, 1, 0, 3)) */
+       ext             v1.16b, v1.16b, v1.16b, #12
+       /* x2 = shuffle32(x2, MASK(1, 0, 3, 2)) */
+       ext             v2.16b, v2.16b, v2.16b, #8
+       /* x3 = shuffle32(x3, MASK(0, 3, 2, 1)) */
+       ext             v3.16b, v3.16b, v3.16b, #4
+
+       subs            w4, w4, #2
+       b.ne            .Ldoubleround
+
+       /* output0 = state0 + v0 */
+       add             v0.4s, v0.4s, v5.4s
+       /* output1 = state1 + v1 */
+       add             v1.4s, v1.4s, v6.4s
+       /* output2 = state2 + v2 */
+       add             v2.4s, v2.4s, v7.4s
+       /* output2 = state3 + v3 */
+       add             v3.4s, v3.4s, v8.4s
+       st1             { v0.4s - v3.4s }, [x0]
+
+       /* ++copy3.counter */
+       add             d8, d8, d13
+
+       /* output += 64, --nblocks */
+       add             x0, x0, 64
+       subs            x3, x3, #1
+       b.ne            .Lblock
+
+       /* counter = copy3.counter */
+       str             d8, [x2]
+
+       /* Zero out the potentially sensitive regs, in case nothing uses these again. */
+       eor             v0.16b, v0.16b, v0.16b
+       eor             v1.16b, v1.16b, v1.16b
+       eor             v2.16b, v2.16b, v2.16b
+       eor             v3.16b, v3.16b, v3.16b
+       eor             v6.16b, v6.16b, v6.16b
+       eor             v7.16b, v7.16b, v7.16b
+       ret
+SYM_FUNC_END(__arch_chacha20_blocks_nostack)
+
+        .section        ".rodata", "a", %progbits
+        .align          L1_CACHE_SHIFT
+
+CTES:  .word           1634760805, 857760878,  2036477234, 1797285236
+ONE:    .xword         1, 0
+ROT8:  .word           0x02010003, 0x06050407, 0x0a09080b, 0x0e0d0c0f
+
+emit_aarch64_feature_1_and
reviewer
#27 lore raw
In reply to Adhemerval Zanella
Quoted 9 lines · Adhemerval Zanella

Hook up the generic vDSO implementation to the aarch64 vDSO data page. The _vdso_rng_data required data is placed within the _vdso_data vvar page, by using a offset larger than the vdso_data (__VDSO_RND_DATA_OFFSET).

The vDSO function requires a ChaCha20 implementation that does not write to the stack, and that can do an entire ChaCha20 permutation. The one provided is based on the current chacha-neon-core.S and uses NEON on the permute operation.

Is there a fallback for when NEON isn't present? The kernel supports some (deeply embedded) implementations where NEON is not present, and AFAICT this will UNDEF on those machines.

Mark.

Quoted 369 lines
---
 arch/arm64/Kconfig                         |   1 +
 arch/arm64/include/asm/vdso/getrandom.h    |  50 +++++++
 arch/arm64/include/asm/vdso/vsyscall.h     |   9 ++
 arch/arm64/kernel/vdso/Makefile            |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S          |   4 +
 arch/arm64/kernel/vdso/vgetrandom-chacha.S | 153 +++++++++++++++++++++
 arch/arm64/kernel/vdso/vgetrandom.c        |  13 ++
 tools/testing/selftests/vDSO/Makefile      |   4 +-
 8 files changed, 238 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm64/include/asm/vdso/getrandom.h
 create mode 100644 arch/arm64/kernel/vdso/vgetrandom-chacha.S
 create mode 100644 arch/arm64/kernel/vdso/vgetrandom.c
arch/arm64/Kconfig +1 -0
index b3fc891f1544..e3f4c5bf0661 100644
@@ -237,6 +237,7 @@ config ARM64
 	select HAVE_KPROBES
 	select HAVE_KRETPROBES
 	select HAVE_GENERIC_VDSO
+	select VDSO_GETRANDOM
 	select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU
 	select IRQ_DOMAIN
 	select IRQ_FORCED_THREADING
arch/arm64/include/asm/vdso/getrandom.h new file +50 -0
new file mode 100644
index 000000000000..6e2b136813ca
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __ASM_VDSO_GETRANDOM_H
+#define __ASM_VDSO_GETRANDOM_H
+
+#ifndef __ASSEMBLY__
+
+#include <asm/unistd.h>
+#include <vdso/datapage.h>
+
+/**
+ * getrandom_syscall - Invoke the getrandom() syscall.
+ * @buffer:	Destination buffer to fill with random bytes.
+ * @len:	Size of @buffer in bytes.
+ * @flags:	Zero or more GRND_* flags.
+ * Returns:	The number of random bytes written to @buffer, or a negative value indicating an error.
+ */
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+	register long int x8 asm ("x8") = __NR_getrandom;
+	register long int x0 asm ("x0") = (long int) buffer;
+	register long int x1 asm ("x1") = (long int) len;
+	register long int x2 asm ("x2") = (long int) flags;
+
+	asm ("svc 0" : "=r"(x0) : "r"(x8), "0"(x0), "r"(x1), "r"(x2));
+
+	return x0;
+}
+
+static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void)
+{
+	return &_vdso_rng_data;
+}
+
+/**
+ * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.
+ * @dst_bytes:	Destination buffer to hold @nblocks * 64 bytes of output.
+ * @key:	32-byte input key.
+ * @counter:	8-byte counter, read on input and updated on return.
+ * @nblocks:	Number of blocks to generate.
+ *
+ * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write
+ * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
+ * leaking into forked child processes.
+ */
+extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);
+
+#endif /* !__ASSEMBLY__ */
+
+#endif /* __ASM_VDSO_GETRANDOM_H */
arch/arm64/include/asm/vdso/vsyscall.h +9 -0
index f94b1457c117..7ddb2bc3b57b 100644
@@ -2,6 +2,8 @@
 #ifndef __ASM_VDSO_VSYSCALL_H
 #define __ASM_VDSO_VSYSCALL_H
 
+#define __VDSO_RND_DATA_OFFSET  480
+
 #ifndef __ASSEMBLY__
 
 #include <linux/timekeeper_internal.h>
@@ -21,6 +23,13 @@ struct vdso_data *__arm64_get_k_vdso_data(void)
 }
 #define __arch_get_k_vdso_data __arm64_get_k_vdso_data
 
+static __always_inline
+struct vdso_rng_data *__arm64_get_k_vdso_rnd_data(void)
+{
+	return (void *)__arm64_get_k_vdso_data() + __VDSO_RND_DATA_OFFSET;
+}
+#define __arch_get_k_vdso_rng_data __arm64_get_k_vdso_rnd_data
+
 static __always_inline
 void __arm64_update_vsyscall(struct vdso_data *vdata, struct timekeeper *tk)
 {
arch/arm64/kernel/vdso/Makefile +6 -1
index d11da6461278..37dad3bb953a 100644
@@ -9,7 +9,7 @@
 # Include the generic Makefile to check the built vdso.
 include $(srctree)/lib/vdso/Makefile
 
-obj-vdso := vgettimeofday.o note.o sigreturn.o
+obj-vdso := vgettimeofday.o note.o sigreturn.o vgetrandom.o vgetrandom-chacha.o
 
 # Build rules
 targets := $(obj-vdso) vdso.so vdso.so.dbg
@@ -40,8 +40,13 @@ CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
 				$(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
 				$(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
 				-Wmissing-prototypes -Wmissing-declarations
+CFLAGS_REMOVE_vgetrandom.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
+			     $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
+			     $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
+			     -Wmissing-prototypes -Wmissing-declarations
 
 CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables
+CFLAGS_vgetrandom.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables
 
 ifneq ($(c-gettimeofday-y),)
   CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y)
arch/arm64/kernel/vdso/vdso.lds.S +4 -0
index 45354f2ddf70..f8dbcece20e2 100644
@@ -12,6 +12,8 @@
 #include <asm/page.h>
 #include <asm/vdso.h>
 #include <asm-generic/vmlinux.lds.h>
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>
 
 OUTPUT_FORMAT("elf64-littleaarch64", "elf64-bigaarch64", "elf64-littleaarch64")
 OUTPUT_ARCH(aarch64)
@@ -19,6 +21,7 @@ OUTPUT_ARCH(aarch64)
 SECTIONS
 {
 	PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE);
+	PROVIDE(_vdso_rng_data = _vdso_data + __VDSO_RND_DATA_OFFSET);
 #ifdef CONFIG_TIME_NS
 	PROVIDE(_timens_data = _vdso_data + PAGE_SIZE);
 #endif
@@ -102,6 +105,7 @@ VERSION
 		__kernel_gettimeofday;
 		__kernel_clock_gettime;
 		__kernel_clock_getres;
+		__kernel_getrandom;
 	local: *;
 	};
 }
arch/arm64/kernel/vdso/vgetrandom-chacha.S new file +153 -0
new file mode 100644
index 000000000000..3fb9715dd6f0
@@ -0,0 +1,153 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/linkage.h>
+#include <asm/cache.h>
+
+	.text
+
+/*
+ * ARM64 ChaCha20 implementation meant for vDSO.  Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes
+ * counter.  Importantly does not spill to the stack.
+ *
+ * void __arch_chacha20_blocks_nostack(uint8_t *dst_bytes,
+ *				       const uint8_t *key,
+ * 				       uint32_t *counter,
+ *				       size_t nblocks)
+ *
+ * 	x0: output bytes
+ *	x1: 32-byte key input
+ *	x2: 8-byte counter input/output
+ *	x3: number of 64-byte block to write to output
+ */
+SYM_FUNC_START(__arch_chacha20_blocks_nostack)
+
+	/* v0 = "expand 32-byte k" */
+	adr_l		x8, CTES
+	ld1		{v5.4s}, [x8]
+	/* v1,v2 = key */
+	ld1		{ v6.4s, v7.4s }, [x1]
+	/* v3 = counter || zero noonce  */
+	ldr		d8, [x2]
+
+	adr_l		x8, ONE
+	ldr		q13, [x8]
+
+	adr_l		x10, ROT8
+	ld1		{v12.4s}, [x10]
+.Lblock:
+	/* copy state to auxiliary vectors for the final add after the permute.  */
+	mov		v0.16b, v5.16b
+	mov		v1.16b, v6.16b
+	mov		v2.16b, v7.16b
+	mov		v3.16b, v8.16b
+
+	mov		w4, 20
+.Lpermute:
+	/*
+	 * Permute one 64-byte block where the state matrix is stored in the four NEON
+	 * registers v0-v3.  It performs matrix operations on four words in parallel,
+	 * but requires shuffling to rearrange the words after each round.
+	 */
+
+.Ldoubleround:
+	/* x0 += x1, x3 = rotl32(x3 ^ x0, 16) */
+	add		v0.4s, v0.4s, v1.4s
+	eor		v3.16b, v3.16b, v0.16b
+	rev32		v3.8h, v3.8h
+
+	/* x2 += x3, x1 = rotl32(x1 ^ x2, 12) */
+	add		v2.4s, v2.4s, v3.4s
+	eor		v4.16b, v1.16b, v2.16b
+	shl		v1.4s, v4.4s, #12
+	sri		v1.4s, v4.4s, #20
+
+	/* x0 += x1, x3 = rotl32(x3 ^ x0, 8) */
+	add		v0.4s, v0.4s, v1.4s
+	eor		v3.16b, v3.16b, v0.16b
+	tbl		v3.16b, {v3.16b}, v12.16b
+
+	/* x2 += x3, x1 = rotl32(x1 ^ x2, 7) */
+	add		v2.4s, v2.4s, v3.4s
+	eor		v4.16b, v1.16b, v2.16b
+	shl		v1.4s, v4.4s, #7
+	sri		v1.4s, v4.4s, #25
+
+	/* x1 = shuffle32(x1, MASK(0, 3, 2, 1)) */
+	ext		v1.16b, v1.16b, v1.16b, #4
+	/* x2 = shuffle32(x2, MASK(1, 0, 3, 2)) */
+	ext		v2.16b, v2.16b, v2.16b, #8
+	/* x3 = shuffle32(x3, MASK(2, 1, 0, 3)) */
+	ext		v3.16b, v3.16b, v3.16b, #12
+
+	/* x0 += x1, x3 = rotl32(x3 ^ x0, 16) */
+	add		v0.4s, v0.4s, v1.4s
+	eor		v3.16b, v3.16b, v0.16b
+	rev32		v3.8h, v3.8h
+
+	/* x2 += x3, x1 = rotl32(x1 ^ x2, 12) */
+	add		v2.4s, v2.4s, v3.4s
+	eor		v4.16b, v1.16b, v2.16b
+	shl		v1.4s, v4.4s, #12
+	sri		v1.4s, v4.4s, #20
+
+	/* x0 += x1, x3 = rotl32(x3 ^ x0, 8) */
+	add		v0.4s, v0.4s, v1.4s
+	eor		v3.16b, v3.16b, v0.16b
+	tbl		v3.16b, {v3.16b}, v12.16b
+
+	/* x2 += x3, x1 = rotl32(x1 ^ x2, 7) */
+	add		v2.4s, v2.4s, v3.4s
+	eor		v4.16b, v1.16b, v2.16b
+	shl		v1.4s, v4.4s, #7
+	sri		v1.4s, v4.4s, #25
+
+	/* x1 = shuffle32(x1, MASK(2, 1, 0, 3)) */
+	ext		v1.16b, v1.16b, v1.16b, #12
+	/* x2 = shuffle32(x2, MASK(1, 0, 3, 2)) */
+	ext		v2.16b, v2.16b, v2.16b, #8
+	/* x3 = shuffle32(x3, MASK(0, 3, 2, 1)) */
+	ext		v3.16b, v3.16b, v3.16b, #4
+
+	subs		w4, w4, #2
+	b.ne		.Ldoubleround
+
+	/* output0 = state0 + v0 */
+	add		v0.4s, v0.4s, v5.4s
+	/* output1 = state1 + v1 */
+	add		v1.4s, v1.4s, v6.4s
+	/* output2 = state2 + v2 */
+	add		v2.4s, v2.4s, v7.4s
+	/* output2 = state3 + v3 */
+	add		v3.4s, v3.4s, v8.4s
+	st1		{ v0.4s - v3.4s }, [x0]
+
+	/* ++copy3.counter */
+	add		d8, d8, d13
+
+	/* output += 64, --nblocks */
+	add		x0, x0, 64
+	subs		x3, x3, #1
+	b.ne		.Lblock
+
+	/* counter = copy3.counter */
+	str		d8, [x2]
+
+	/* Zero out the potentially sensitive regs, in case nothing uses these again. */
+	eor		v0.16b, v0.16b, v0.16b
+	eor		v1.16b, v1.16b, v1.16b
+	eor		v2.16b, v2.16b, v2.16b
+	eor		v3.16b, v3.16b, v3.16b
+	eor		v6.16b, v6.16b, v6.16b
+	eor		v7.16b, v7.16b, v7.16b
+	ret
+SYM_FUNC_END(__arch_chacha20_blocks_nostack)
+
+        .section        ".rodata", "a", %progbits
+        .align          L1_CACHE_SHIFT
+
+CTES:	.word		1634760805, 857760878, 	2036477234, 1797285236
+ONE:    .xword		1, 0
+ROT8:	.word		0x02010003, 0x06050407, 0x0a09080b, 0x0e0d0c0f
+
+emit_aarch64_feature_1_and
arch/arm64/kernel/vdso/vgetrandom.c new file +13 -0
new file mode 100644
index 000000000000..b6d6f4db3a98
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/types.h>
+#include <linux/mm.h>
+
+#include "../../../../lib/vdso/getrandom.c"
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len);
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len)
+{
+	return __cvdso_getrandom(buffer, len, flags, opaque_state, opaque_len);
+}
tools/testing/selftests/vDSO/Makefile +2 -2
index 10ffdda3f2fa..f07ea679a4cc 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
 TEST_GEN_PROGS += vdso_standalone_test_x86
 endif
 TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
 TEST_GEN_PROGS += vdso_test_getrandom
 ifneq ($(SODIUM),)
 TEST_GEN_PROGS += vdso_test_chacha
Signature · 1 line
2.43.0
#28 lore raw
Quoted 11 lines · Mark Rutland
Quoted 9 lines · Adhemerval Zanella

Hook up the generic vDSO implementation to the aarch64 vDSO data page. The _vdso_rng_data required data is placed within the _vdso_data vvar page, by using a offset larger than the vdso_data (__VDSO_RND_DATA_OFFSET).

The vDSO function requires a ChaCha20 implementation that does not write to the stack, and that can do an entire ChaCha20 permutation. The one provided is based on the current chacha-neon-core.S and uses NEON on the permute operation.

Quoted 5 lines

Is there a fallback for when NEON isn't present? The kernel supports some (deeply embedded) implementations where NEON is not present, and AFAICT this will UNDEF on those machines.

Mark.

Not right know, in this case I think it better to just do something similar to Loongarch and fallback to the syscall. I will add this on the next version.

Quoted 370 lines
Quoted 370 lines
---
 arch/arm64/Kconfig                         |   1 +
 arch/arm64/include/asm/vdso/getrandom.h    |  50 +++++++
 arch/arm64/include/asm/vdso/vsyscall.h     |   9 ++
 arch/arm64/kernel/vdso/Makefile            |   7 +-
 arch/arm64/kernel/vdso/vdso.lds.S          |   4 +
 arch/arm64/kernel/vdso/vgetrandom-chacha.S | 153 +++++++++++++++++++++
 arch/arm64/kernel/vdso/vgetrandom.c        |  13 ++
 tools/testing/selftests/vDSO/Makefile      |   4 +-
 8 files changed, 238 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm64/include/asm/vdso/getrandom.h
 create mode 100644 arch/arm64/kernel/vdso/vgetrandom-chacha.S
 create mode 100644 arch/arm64/kernel/vdso/vgetrandom.c
arch/arm64/Kconfig +1 -0
index b3fc891f1544..e3f4c5bf0661 100644
@@ -237,6 +237,7 @@ config ARM64
 	select HAVE_KPROBES
 	select HAVE_KRETPROBES
 	select HAVE_GENERIC_VDSO
+	select VDSO_GETRANDOM
 	select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU
 	select IRQ_DOMAIN
 	select IRQ_FORCED_THREADING
arch/arm64/include/asm/vdso/getrandom.h new file +50 -0
new file mode 100644
index 000000000000..6e2b136813ca
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __ASM_VDSO_GETRANDOM_H
+#define __ASM_VDSO_GETRANDOM_H
+
+#ifndef __ASSEMBLY__
+
+#include <asm/unistd.h>
+#include <vdso/datapage.h>
+
+/**
+ * getrandom_syscall - Invoke the getrandom() syscall.
+ * @buffer:	Destination buffer to fill with random bytes.
+ * @len:	Size of @buffer in bytes.
+ * @flags:	Zero or more GRND_* flags.
+ * Returns:	The number of random bytes written to @buffer, or a negative value indicating an error.
+ */
+static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsigned int flags)
+{
+	register long int x8 asm ("x8") = __NR_getrandom;
+	register long int x0 asm ("x0") = (long int) buffer;
+	register long int x1 asm ("x1") = (long int) len;
+	register long int x2 asm ("x2") = (long int) flags;
+
+	asm ("svc 0" : "=r"(x0) : "r"(x8), "0"(x0), "r"(x1), "r"(x2));
+
+	return x0;
+}
+
+static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void)
+{
+	return &_vdso_rng_data;
+}
+
+/**
+ * __arch_chacha20_blocks_nostack - Generate ChaCha20 stream without using the stack.
+ * @dst_bytes:	Destination buffer to hold @nblocks * 64 bytes of output.
+ * @key:	32-byte input key.
+ * @counter:	8-byte counter, read on input and updated on return.
+ * @nblocks:	Number of blocks to generate.
+ *
+ * Generates a given positive number of blocks of ChaCha20 output with nonce=0, and does not write
+ * to any stack or memory outside of the parameters passed to it, in order to mitigate stack data
+ * leaking into forked child processes.
+ */
+extern void __arch_chacha20_blocks_nostack(u8 *dst_bytes, const u32 *key, u32 *counter, size_t nblocks);
+
+#endif /* !__ASSEMBLY__ */
+
+#endif /* __ASM_VDSO_GETRANDOM_H */
arch/arm64/include/asm/vdso/vsyscall.h +9 -0
index f94b1457c117..7ddb2bc3b57b 100644
@@ -2,6 +2,8 @@
 #ifndef __ASM_VDSO_VSYSCALL_H
 #define __ASM_VDSO_VSYSCALL_H
 
+#define __VDSO_RND_DATA_OFFSET  480
+
 #ifndef __ASSEMBLY__
 
 #include <linux/timekeeper_internal.h>
@@ -21,6 +23,13 @@ struct vdso_data *__arm64_get_k_vdso_data(void)
 }
 #define __arch_get_k_vdso_data __arm64_get_k_vdso_data
 
+static __always_inline
+struct vdso_rng_data *__arm64_get_k_vdso_rnd_data(void)
+{
+	return (void *)__arm64_get_k_vdso_data() + __VDSO_RND_DATA_OFFSET;
+}
+#define __arch_get_k_vdso_rng_data __arm64_get_k_vdso_rnd_data
+
 static __always_inline
 void __arm64_update_vsyscall(struct vdso_data *vdata, struct timekeeper *tk)
 {
arch/arm64/kernel/vdso/Makefile +6 -1
index d11da6461278..37dad3bb953a 100644
@@ -9,7 +9,7 @@
 # Include the generic Makefile to check the built vdso.
 include $(srctree)/lib/vdso/Makefile
 
-obj-vdso := vgettimeofday.o note.o sigreturn.o
+obj-vdso := vgettimeofday.o note.o sigreturn.o vgetrandom.o vgetrandom-chacha.o
 
 # Build rules
 targets := $(obj-vdso) vdso.so vdso.so.dbg
@@ -40,8 +40,13 @@ CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
 				$(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
 				$(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
 				-Wmissing-prototypes -Wmissing-declarations
+CFLAGS_REMOVE_vgetrandom.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
+			     $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) \
+			     $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
+			     -Wmissing-prototypes -Wmissing-declarations
 
 CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables
+CFLAGS_vgetrandom.o = -O2 -mcmodel=tiny -fasynchronous-unwind-tables
 
 ifneq ($(c-gettimeofday-y),)
   CFLAGS_vgettimeofday.o += -include $(c-gettimeofday-y)
arch/arm64/kernel/vdso/vdso.lds.S +4 -0
index 45354f2ddf70..f8dbcece20e2 100644
@@ -12,6 +12,8 @@
 #include <asm/page.h>
 #include <asm/vdso.h>
 #include <asm-generic/vmlinux.lds.h>
+#include <vdso/datapage.h>
+#include <asm/vdso/vsyscall.h>
 
 OUTPUT_FORMAT("elf64-littleaarch64", "elf64-bigaarch64", "elf64-littleaarch64")
 OUTPUT_ARCH(aarch64)
@@ -19,6 +21,7 @@ OUTPUT_ARCH(aarch64)
 SECTIONS
 {
 	PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE);
+	PROVIDE(_vdso_rng_data = _vdso_data + __VDSO_RND_DATA_OFFSET);
 #ifdef CONFIG_TIME_NS
 	PROVIDE(_timens_data = _vdso_data + PAGE_SIZE);
 #endif
@@ -102,6 +105,7 @@ VERSION
 		__kernel_gettimeofday;
 		__kernel_clock_gettime;
 		__kernel_clock_getres;
+		__kernel_getrandom;
 	local: *;
 	};
 }
arch/arm64/kernel/vdso/vgetrandom-chacha.S new file +153 -0
new file mode 100644
index 000000000000..3fb9715dd6f0
@@ -0,0 +1,153 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/linkage.h>
+#include <asm/cache.h>
+
+	.text
+
+/*
+ * ARM64 ChaCha20 implementation meant for vDSO.  Produces a given positive
+ * number of blocks of output with nonnce 0, taking an input key and 8-bytes
+ * counter.  Importantly does not spill to the stack.
+ *
+ * void __arch_chacha20_blocks_nostack(uint8_t *dst_bytes,
+ *				       const uint8_t *key,
+ * 				       uint32_t *counter,
+ *				       size_t nblocks)
+ *
+ * 	x0: output bytes
+ *	x1: 32-byte key input
+ *	x2: 8-byte counter input/output
+ *	x3: number of 64-byte block to write to output
+ */
+SYM_FUNC_START(__arch_chacha20_blocks_nostack)
+
+	/* v0 = "expand 32-byte k" */
+	adr_l		x8, CTES
+	ld1		{v5.4s}, [x8]
+	/* v1,v2 = key */
+	ld1		{ v6.4s, v7.4s }, [x1]
+	/* v3 = counter || zero noonce  */
+	ldr		d8, [x2]
+
+	adr_l		x8, ONE
+	ldr		q13, [x8]
+
+	adr_l		x10, ROT8
+	ld1		{v12.4s}, [x10]
+.Lblock:
+	/* copy state to auxiliary vectors for the final add after the permute.  */
+	mov		v0.16b, v5.16b
+	mov		v1.16b, v6.16b
+	mov		v2.16b, v7.16b
+	mov		v3.16b, v8.16b
+
+	mov		w4, 20
+.Lpermute:
+	/*
+	 * Permute one 64-byte block where the state matrix is stored in the four NEON
+	 * registers v0-v3.  It performs matrix operations on four words in parallel,
+	 * but requires shuffling to rearrange the words after each round.
+	 */
+
+.Ldoubleround:
+	/* x0 += x1, x3 = rotl32(x3 ^ x0, 16) */
+	add		v0.4s, v0.4s, v1.4s
+	eor		v3.16b, v3.16b, v0.16b
+	rev32		v3.8h, v3.8h
+
+	/* x2 += x3, x1 = rotl32(x1 ^ x2, 12) */
+	add		v2.4s, v2.4s, v3.4s
+	eor		v4.16b, v1.16b, v2.16b
+	shl		v1.4s, v4.4s, #12
+	sri		v1.4s, v4.4s, #20
+
+	/* x0 += x1, x3 = rotl32(x3 ^ x0, 8) */
+	add		v0.4s, v0.4s, v1.4s
+	eor		v3.16b, v3.16b, v0.16b
+	tbl		v3.16b, {v3.16b}, v12.16b
+
+	/* x2 += x3, x1 = rotl32(x1 ^ x2, 7) */
+	add		v2.4s, v2.4s, v3.4s
+	eor		v4.16b, v1.16b, v2.16b
+	shl		v1.4s, v4.4s, #7
+	sri		v1.4s, v4.4s, #25
+
+	/* x1 = shuffle32(x1, MASK(0, 3, 2, 1)) */
+	ext		v1.16b, v1.16b, v1.16b, #4
+	/* x2 = shuffle32(x2, MASK(1, 0, 3, 2)) */
+	ext		v2.16b, v2.16b, v2.16b, #8
+	/* x3 = shuffle32(x3, MASK(2, 1, 0, 3)) */
+	ext		v3.16b, v3.16b, v3.16b, #12
+
+	/* x0 += x1, x3 = rotl32(x3 ^ x0, 16) */
+	add		v0.4s, v0.4s, v1.4s
+	eor		v3.16b, v3.16b, v0.16b
+	rev32		v3.8h, v3.8h
+
+	/* x2 += x3, x1 = rotl32(x1 ^ x2, 12) */
+	add		v2.4s, v2.4s, v3.4s
+	eor		v4.16b, v1.16b, v2.16b
+	shl		v1.4s, v4.4s, #12
+	sri		v1.4s, v4.4s, #20
+
+	/* x0 += x1, x3 = rotl32(x3 ^ x0, 8) */
+	add		v0.4s, v0.4s, v1.4s
+	eor		v3.16b, v3.16b, v0.16b
+	tbl		v3.16b, {v3.16b}, v12.16b
+
+	/* x2 += x3, x1 = rotl32(x1 ^ x2, 7) */
+	add		v2.4s, v2.4s, v3.4s
+	eor		v4.16b, v1.16b, v2.16b
+	shl		v1.4s, v4.4s, #7
+	sri		v1.4s, v4.4s, #25
+
+	/* x1 = shuffle32(x1, MASK(2, 1, 0, 3)) */
+	ext		v1.16b, v1.16b, v1.16b, #12
+	/* x2 = shuffle32(x2, MASK(1, 0, 3, 2)) */
+	ext		v2.16b, v2.16b, v2.16b, #8
+	/* x3 = shuffle32(x3, MASK(0, 3, 2, 1)) */
+	ext		v3.16b, v3.16b, v3.16b, #4
+
+	subs		w4, w4, #2
+	b.ne		.Ldoubleround
+
+	/* output0 = state0 + v0 */
+	add		v0.4s, v0.4s, v5.4s
+	/* output1 = state1 + v1 */
+	add		v1.4s, v1.4s, v6.4s
+	/* output2 = state2 + v2 */
+	add		v2.4s, v2.4s, v7.4s
+	/* output2 = state3 + v3 */
+	add		v3.4s, v3.4s, v8.4s
+	st1		{ v0.4s - v3.4s }, [x0]
+
+	/* ++copy3.counter */
+	add		d8, d8, d13
+
+	/* output += 64, --nblocks */
+	add		x0, x0, 64
+	subs		x3, x3, #1
+	b.ne		.Lblock
+
+	/* counter = copy3.counter */
+	str		d8, [x2]
+
+	/* Zero out the potentially sensitive regs, in case nothing uses these again. */
+	eor		v0.16b, v0.16b, v0.16b
+	eor		v1.16b, v1.16b, v1.16b
+	eor		v2.16b, v2.16b, v2.16b
+	eor		v3.16b, v3.16b, v3.16b
+	eor		v6.16b, v6.16b, v6.16b
+	eor		v7.16b, v7.16b, v7.16b
+	ret
+SYM_FUNC_END(__arch_chacha20_blocks_nostack)
+
+        .section        ".rodata", "a", %progbits
+        .align          L1_CACHE_SHIFT
+
+CTES:	.word		1634760805, 857760878, 	2036477234, 1797285236
+ONE:    .xword		1, 0
+ROT8:	.word		0x02010003, 0x06050407, 0x0a09080b, 0x0e0d0c0f
+
+emit_aarch64_feature_1_and
arch/arm64/kernel/vdso/vgetrandom.c new file +13 -0
new file mode 100644
index 000000000000..b6d6f4db3a98
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/types.h>
+#include <linux/mm.h>
+
+#include "../../../../lib/vdso/getrandom.c"
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len);
+
+ssize_t __kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state, size_t opaque_len)
+{
+	return __cvdso_getrandom(buffer, len, flags, opaque_state, opaque_len);
+}
tools/testing/selftests/vDSO/Makefile +2 -2
index 10ffdda3f2fa..f07ea679a4cc 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 uname_M := $(shell uname -m 2>/dev/null || echo not)
-ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/)
 SODIUM := $(shell pkg-config --libs libsodium 2>/dev/null)
 
 TEST_GEN_PROGS := vdso_test_gettimeofday
@@ -11,7 +11,7 @@ ifeq ($(ARCH),$(filter $(ARCH),x86 x86_64))
 TEST_GEN_PROGS += vdso_standalone_test_x86
 endif
 TEST_GEN_PROGS += vdso_test_correctness
-ifeq ($(uname_M),x86_64)
+ifeq ($(uname_M), $(filter x86_64 aarch64, $(uname_M)))
 TEST_GEN_PROGS += vdso_test_getrandom
 ifneq ($(SODIUM),)
 TEST_GEN_PROGS += vdso_test_chacha
Signature · 1 line
2.43.0