Skip to contentSkip to conversation

Initialize Vector registers in the first-use trap

PATCH Applied Andy Chiu 4 participants 8 messages Jun 27, 2023 – Jul 4, 2023

Subsystems (2)
lore
Share & tools
lore

Andy Chiu

author
#0 lore raw

cover Initialize Vector registers in the first-use trap

Before applying this series, We only initialize the space for saving Vector registers. This is not enough as Vector registers themselves also neeeded to be initialized before dropping back into userspace. Or, we may risk leaking a process's data left in Vector register to another process. This can be verified by only applying the second patch and running the test.

Andy Chiu (2):
riscv: vector: clear V-reg in the first-use trap
selftests: Test RISC-V Vector's first-use handler

 arch/riscv/kernel/vector.c                    |  1 +
 .../testing/selftests/riscv/vector/.gitignore |  1 +
 tools/testing/selftests/riscv/vector/Makefile |  6 +-
 .../selftests/riscv/vector/v_initval_nolibc.c | 68 +++++++++++++++++++
 4 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/riscv/vector/v_initval_nolibc.c
Signature · 7 lines
2.17.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#1 lore raw

PATCH 1/2 riscv: vector: clear V-reg in the first-use trap

If there is no context switch happens after we enable V for a process, then we return to user space with whatever left on the CPU's V registers accessible to the process. The leaked data could belong to another process's V-context saved from last context switch, impacting process's confidentiality on the system.

To prevent this from happening, we clear V registers by restoring zero'd V context after turining on V.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")
---
 arch/riscv/kernel/vector.c | 1 +
 1 file changed, 1 insertion(+)
arch/riscv/kernel/vector.c +1 -0
index f9c8e19ab301..8d92fb6c522c 100644
@@ -167,6 +167,7 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)
 		return true;
 	}
 	riscv_v_vstate_on(regs);
+	riscv_v_vstate_restore(current, regs);
 	return true;
 }
 
Signature · 7 lines
2.17.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 1/2
#2 lore raw

Re: [v1, 1/2] riscv: vector: clear V-reg in the first-use trap

Quoted 11 lines · Andy Chiu

If there is no context switch happens after we enable V for a process, then we return to user space with whatever left on the CPU's V registers accessible to the process. The leaked data could belong to another process's V-context saved from last context switch, impacting process's confidentiality on the system.

To prevent this from happening, we clear V registers by restoring zero'd V context after turining on V.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#3 lore raw

PATCH 2/2 selftests: Test RISC-V Vector's first-use handler

In reply to Andy Chiu

This add a test to check if the kernel zero-initializes all V registers after the first-use trap handler returns.

If V registers are not zero-initialized, then the test should fail one out of several runs:

```
 root@sifive-fpga:~# ./v_initval_nolibc
 # vl = 256
 not ok 1 detect stale values on v-regesters
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 4c 41 4e 47 3d 43 0   50 41 54 48 3d 2f 75 73
 72 2f 6c 6f 63 61 6c 2f   73 62 69 6e 3a 2f 75 73
 72 2f 6c 6f 63 61 6c 2f   62 69 6e 3a 2f 75 73 72
 ff ff 81 0 0 0 0 0   0 0 0 0 0 0 0 0
```

Otherwise, the test passes without errors each run.

---
 .../testing/selftests/riscv/vector/.gitignore |  1 +
 tools/testing/selftests/riscv/vector/Makefile |  6 +-
 .../selftests/riscv/vector/v_initval_nolibc.c | 68 +++++++++++++++++++
 3 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/riscv/vector/v_initval_nolibc.c
tools/testing/selftests/riscv/vector/.gitignore +1 -0
index 4f2b4e8a3b08..9ae7964491d5 100644
@@ -1,2 +1,3 @@
 vstate_exec_nolibc
 vstate_prctl
+v_initval_nolibc
tools/testing/selftests/riscv/vector/Makefile +5 -1
index cd6e80bf995d..bfff0ff4f3be 100644
@@ -2,7 +2,7 @@
 # Copyright (C) 2021 ARM Limited
 # Originally tools/testing/arm64/abi/Makefile
 
-TEST_GEN_PROGS := vstate_prctl
+TEST_GEN_PROGS := vstate_prctl v_initval_nolibc
 TEST_GEN_PROGS_EXTENDED := vstate_exec_nolibc
 
 include ../../lib.mk
@@ -13,3 +13,7 @@ $(OUTPUT)/vstate_prctl: vstate_prctl.c ../hwprobe/sys_hwprobe.S
 $(OUTPUT)/vstate_exec_nolibc: vstate_exec_nolibc.c
 	$(CC) -nostdlib -static -include ../../../../include/nolibc/nolibc.h \
 		-Wall $(CFLAGS) $(LDFLAGS) $^ -o $@ -lgcc
+
+$(OUTPUT)/v_initval_nolibc: v_initval_nolibc.c
+	$(CC) -nostdlib -static -include ../../../../include/nolibc/nolibc.h \
+		-Wall $(CFLAGS) $(LDFLAGS) $^ -o $@ -lgcc
tools/testing/selftests/riscv/vector/v_initval_nolibc.c new file +68 -0

68 lines not on this page · raw message

Signature · 7 lines
2.17.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 2/2
#4 lore raw

Re: [v1, 2/2] selftests: Test RISC-V Vector's first-use handler

Quoted 49 lines · Andy Chiu

This add a test to check if the kernel zero-initializes all V registers after the first-use trap handler returns.

If V registers are not zero-initialized, then the test should fail one out of several runs:

```
 root@sifive-fpga:~# ./v_initval_nolibc
 # vl = 256
 not ok 1 detect stale values on v-regesters
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 4c 41 4e 47 3d 43 0   50 41 54 48 3d 2f 75 73
 72 2f 6c 6f 63 61 6c 2f   73 62 69 6e 3a 2f 75 73
 72 2f 6c 6f 63 61 6c 2f   62 69 6e 3a 2f 75 73 72
 ff ff 81 0 0 0 0 0   0 0 0 0 0 0 0 0
```

Otherwise, the test passes without errors each run.

---
 .../testing/selftests/riscv/vector/.gitignore |  1 +
 tools/testing/selftests/riscv/vector/Makefile |  6 +-
 .../selftests/riscv/vector/v_initval_nolibc.c | 68 +++++++++++++++++++
 3 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/riscv/vector/v_initval_nolibc.c
tools/testing/selftests/riscv/vector/.gitignore +1 -0
index 4f2b4e8a3b08..9ae7964491d5 100644
@@ -1,2 +1,3 @@
 vstate_exec_nolibc
 vstate_prctl
+v_initval_nolibc
tools/testing/selftests/riscv/vector/Makefile +1 -1
index cd6e80bf995d..bfff0ff4f3be 100644
@@ -2,7 +2,7 @@
 # Copyright (C) 2021 ARM Limited
 # Originally tools/testing/arm64/abi/Makefile
 
-TEST_GEN_PROGS := vstate_prctl
+TEST_GEN_PROGS := vstate_prctl v_initval_nolibc
 TEST_GEN_PROGS_EXTENDED := vstate_exec_nolibc
 
 include ../../lib.mk

tools/testing/selftests/riscv/vector/Makefile $(OUTPUT)/vstate_prctl: vstate_prctl.c ../hwprobe/sys_hwprobe.S · line 19

Quoted 7 lines
tools/testing/selftests/riscv/vector/Makefile +4 -0
@@ -13,3 +13,7 @@ $(OUTPUT)/vstate_prctl: vstate_prctl.c ../hwprobe/sys_hwprobe.S
 $(OUTPUT)/vstate_exec_nolibc: vstate_exec_nolibc.c
 	$(CC) -nostdlib -static -include ../../../../include/nolibc/nolibc.h \
 		-Wall $(CFLAGS) $(LDFLAGS) $^ -o $@ -lgcc
+
+$(OUTPUT)/v_initval_nolibc: v_initval_nolibc.c
+	$(CC) -nostdlib -static -include ../../../../include/nolibc/nolibc.h \
+		-Wall $(CFLAGS) $(LDFLAGS) $^ -o $@ -lgcc

Hmm, does this build with clang? (No, bigge on my end, and can be fixed later.)

Quoted 48 lines
tools/testing/selftests/riscv/vector/v_initval_nolibc.c new file +42 -0
new file mode 100644
index 000000000000..66764edb0d52
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "../../kselftest.h"
+#define MAX_VSIZE	(8192 * 32)
+
+void dump(char *ptr, int size)
+{
+	int i = 0;
+
+	for (i = 0; i < size; i++) {
+		if (i != 0) {
+			if (i % 16 == 0)
+				printf("\n");
+			else if (i % 8 == 0)
+				printf("  ");
+		}
+		printf("%02x ", ptr[i]);
+	}
+	printf("\n");
+}
+
+int main(void)
+{
+	int i;
+	unsigned long vl;
+	char *datap, *tmp;
+
+	datap = malloc(MAX_VSIZE);
+	if (!datap) {
+		ksft_test_result_fail("fail to allocate memory for size = %lu\n", MAX_VSIZE);
+		exit(-1);
+	}
+
+	tmp = datap;
+	asm volatile (
+		".option push\n\t"
+		".option arch, +v\n\t"
+		"vsetvli	%0, x0, e8, m8, ta, ma\n\t"
+		"vse8.v		v0, (%2)\n\t"
+		"add		%1, %2, %0\n\t"
+		"vse8.v		v8, (%1)\n\t"
+		"add		%1, %1, %0\n\t"

tools/testing/selftests/riscv/vector/v_initval_nolibc.c line 52

Quoted 10 lines
tools/testing/selftests/riscv/vector/v_initval_nolibc.c new file +10 -0
+		"vse8.v		v16, (%1)\n\t"
+		"add		%1, %1, %0\n\t"
+		"vse8.v		v24, (%1)\n\t"
+		".option pop\n\t"
+		: "=&r" (vl), "=r" (tmp) : "r" (datap) : "memory");
+
+	ksft_print_msg("vl = %lu\n", vl);
+
+	if (datap[0] != 0x00 && datap[0] != 0xff) {
+		ksft_test_result_fail("v-regesters are not properly initialized\n");

Nit: "v-registers"

tools/testing/selftests/riscv/vector/v_initval_nolibc.c line 59

Quoted 7 lines
tools/testing/selftests/riscv/vector/v_initval_nolibc.c new file +7 -0
+		dump(datap, vl * 4);
+		exit(-1);
+	}
+
+	for (i = 1; i < vl * 4; i++) {
+		if (datap[i] != datap[0]) {
+			ksft_test_result_fail("detect stale values on v-regesters\n");

Nit (dito): "v-registers", and maybe "detected".

With, or without the changes above,

Björn

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#5 lore raw

Re: [v1, 2/2] selftests: Test RISC-V Vector's first-use handler

Quoted 50 lines · Björn Töpel
Quoted 47 lines · Andy Chiu

This add a test to check if the kernel zero-initializes all V registers after the first-use trap handler returns.

If V registers are not zero-initialized, then the test should fail one out of several runs:

```
 root@sifive-fpga:~# ./v_initval_nolibc
 # vl = 256
 not ok 1 detect stale values on v-regesters
 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
 0 4c 41 4e 47 3d 43 0   50 41 54 48 3d 2f 75 73
 72 2f 6c 6f 63 61 6c 2f   73 62 69 6e 3a 2f 75 73
 72 2f 6c 6f 63 61 6c 2f   62 69 6e 3a 2f 75 73 72
 ff ff 81 0 0 0 0 0   0 0 0 0 0 0 0 0
```

Otherwise, the test passes without errors each run.

---
 .../testing/selftests/riscv/vector/.gitignore |  1 +
 tools/testing/selftests/riscv/vector/Makefile |  6 +-
 .../selftests/riscv/vector/v_initval_nolibc.c | 68 +++++++++++++++++++
 3 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/riscv/vector/v_initval_nolibc.c
tools/testing/selftests/riscv/vector/.gitignore +1 -0
index 4f2b4e8a3b08..9ae7964491d5 100644
@@ -1,2 +1,3 @@
 vstate_exec_nolibc
 vstate_prctl
+v_initval_nolibc
tools/testing/selftests/riscv/vector/Makefile +1 -1
index cd6e80bf995d..bfff0ff4f3be 100644
@@ -2,7 +2,7 @@
 # Copyright (C) 2021 ARM Limited
 # Originally tools/testing/arm64/abi/Makefile
 
-TEST_GEN_PROGS := vstate_prctl
+TEST_GEN_PROGS := vstate_prctl v_initval_nolibc
 TEST_GEN_PROGS_EXTENDED := vstate_exec_nolibc
 
Quoted 12 lines

tools/testing/selftests/riscv/vector/Makefile $(OUTPUT)/vstate_prctl: vstate_prctl.c ../hwprobe/sys_hwprobe.S · line 19

Quoted 9 lines
tools/testing/selftests/riscv/vector/Makefile +4 -0
 include ../../lib.mk
@@ -13,3 +13,7 @@ $(OUTPUT)/vstate_prctl: vstate_prctl.c ../hwprobe/sys_hwprobe.S
 $(OUTPUT)/vstate_exec_nolibc: vstate_exec_nolibc.c
      $(CC) -nostdlib -static -include ../../../../include/nolibc/nolibc.h \
              -Wall $(CFLAGS) $(LDFLAGS) $^ -o $@ -lgcc
+
+$(OUTPUT)/v_initval_nolibc: v_initval_nolibc.c
+     $(CC) -nostdlib -static -include ../../../../include/nolibc/nolibc.h \
+             -Wall $(CFLAGS) $(LDFLAGS) $^ -o $@ -lgcc

Hmm, does this build with clang? (No, bigge on my end, and can be fixed later.)

Oops, I didn't try. I will respin the series if clang runs into problems. I am not very confident here, but do we need to gate build these tests if CONFIG_RISCV_ISA_V is not set? If my code fits clang well then it probably would be some toolchain dependency issues (guessing the ".option arch" part). Kconfig should be able to resolve this part.

Quoted 70 lines
Quoted 49 lines
tools/testing/selftests/riscv/vector/v_initval_nolibc.c new file +42 -0
new file mode 100644
index 000000000000..66764edb0d52
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include "../../kselftest.h"
+#define MAX_VSIZE    (8192 * 32)
+
+void dump(char *ptr, int size)
+{
+     int i = 0;
+
+     for (i = 0; i < size; i++) {
+             if (i != 0) {
+                     if (i % 16 == 0)
+                             printf("\n");
+                     else if (i % 8 == 0)
+                             printf("  ");
+             }
+             printf("%02x ", ptr[i]);
+     }
+     printf("\n");
+}
+
+int main(void)
+{
+     int i;
+     unsigned long vl;
+     char *datap, *tmp;
+
+     datap = malloc(MAX_VSIZE);
+     if (!datap) {
+             ksft_test_result_fail("fail to allocate memory for size = %lu\n", MAX_VSIZE);
+             exit(-1);
+     }
+
+     tmp = datap;
+     asm volatile (
+             ".option push\n\t"
+             ".option arch, +v\n\t"
+             "vsetvli        %0, x0, e8, m8, ta, ma\n\t"
+             "vse8.v         v0, (%2)\n\t"
+             "add            %1, %2, %0\n\t"
+             "vse8.v         v8, (%1)\n\t"
+             "add            %1, %1, %0\n\t"

tools/testing/selftests/riscv/vector/v_initval_nolibc.c line 52

Quoted 10 lines
tools/testing/selftests/riscv/vector/v_initval_nolibc.c new file +10 -0
+             "vse8.v         v16, (%1)\n\t"
+             "add            %1, %1, %0\n\t"
+             "vse8.v         v24, (%1)\n\t"
+             ".option pop\n\t"
+             : "=&r" (vl), "=r" (tmp) : "r" (datap) : "memory");
+
+     ksft_print_msg("vl = %lu\n", vl);
+
+     if (datap[0] != 0x00 && datap[0] != 0xff) {
+             ksft_test_result_fail("v-regesters are not properly initialized\n");

Nit: "v-registers"

tools/testing/selftests/riscv/vector/v_initval_nolibc.c line 59

Quoted 7 lines
tools/testing/selftests/riscv/vector/v_initval_nolibc.c new file +7 -0
+             dump(datap, vl * 4);
+             exit(-1);
+     }
+
+     for (i = 1; i < vl * 4; i++) {
+             if (datap[i] != datap[0]) {
+                     ksft_test_result_fail("detect stale values on v-regesters\n");
Quoted 7 lines

Nit (dito): "v-registers", and maybe "detected".

With, or without the changes above,

Björn

Thanks,
Andy

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#6 lore raw
In reply to Andy Chiu
Quoted 8 lines · Andy Chiu

Before applying this series, We only initialize the space for saving Vector registers. This is not enough as Vector registers themselves also neeeded to be initialized before dropping back into userspace. Or, we may risk leaking a process's data left in Vector register to another process. This can be verified by only applying the second patch and running the test.

[...]

Applied, thanks!

[1/2] riscv: vector: clear V-reg in the first-use trap

      https://git.kernel.org/palmer/c/75b59f2a90aa
[2/2] selftests: Test RISC-V Vector's first-use handler
      https://git.kernel.org/palmer/c/5c93c4c72fbc

Best regards,

Signature · 7 lines
Palmer Dabbelt <[email protected]>


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#7 lore raw
In reply to Andy Chiu
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 8 lines · On Tue, 27 Jun 2023 01:55:53 +0000 you wrote:

Before applying this series, We only initialize the space for saving Vector registers. This is not enough as Vector registers themselves also neeeded to be initialized before dropping back into userspace. Or, we may risk leaking a process's data left in Vector register to another process. This can be verified by only applying the second patch and running the test.

[...]

Here is the summary with links:
- [v1,1/2] riscv: vector: clear V-reg in the first-use trap

    https://git.kernel.org/riscv/c/75b59f2a90aa
  - [v1,2/2] selftests: Test RISC-V Vector's first-use handler
    https://git.kernel.org/riscv/c/5c93c4c72fbc

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv