[PATCH] selftests/lam: Test get_user() LAM pointer handling

Maciej Wieczor-Retman posted 1 patch 3 weeks, 5 days ago
There is a newer version of this series
tools/testing/selftests/x86/lam.c | 85 +++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+)
[PATCH] selftests/lam: Test get_user() LAM pointer handling
Posted by Maciej Wieczor-Retman 3 weeks, 5 days ago
Recent change in how get_user() handles pointers [1] has a specific case
for LAM. It assigns a different bitmask that's later used to check
whether a pointer comes from userland in get_user().

While currently commented out (until LASS [2] is merged into the kernel)
it's worth making changes to the LAM selftest ahead of time.

Add test case to LAM that utilizes a ioctl (FIOASYNC) syscall which uses
get_user() in its implementation. Execute the syscall with differently
tagged pointers to verify that valid user pointers are passing through
and invalid kernel/non-canonical pointers are not.

Code was tested on a Sierra Forest Xeon machine that's LAM capable. The
test was ran without issues with both the LAM lines from [1] untouched
and commented out. The test was also ran without issues with LAM_SUP
both enabled and disabled.

[1] https://lore.kernel.org/all/20241024013214.129639-1-torvalds@linux-foundation.org/
[2] https://lore.kernel.org/all/20240710160655.3402786-1-alexander.shishkin@linux.intel.com/

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
---
 tools/testing/selftests/x86/lam.c | 85 +++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
index 0ea4f6813930..3c53d4b7aa61 100644
--- a/tools/testing/selftests/x86/lam.c
+++ b/tools/testing/selftests/x86/lam.c
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/syscall.h>
+#include <sys/ioctl.h>
 #include <time.h>
 #include <signal.h>
 #include <setjmp.h>
@@ -43,10 +44,19 @@
 #define FUNC_INHERITE           0x20
 #define FUNC_PASID              0x40
 
+/* get_user() pointer test cases */
+#define GET_USER_USER           0
+#define GET_USER_KERNEL_TOP     1
+#define GET_USER_KERNEL_BOT     2
+#define GET_USER_KERNEL         3
+
 #define TEST_MASK               0x7f
+#define L5_SIGN_EXT_MASK        (0xFFUL << 56)
+#define L4_SIGN_EXT_MASK        (0x1FFFFUL << 47)
 
 #define LOW_ADDR                (0x1UL << 30)
 #define HIGH_ADDR               (0x3UL << 48)
+#define L5_ADDR                 (0x1UL << 48)
 
 #define MALLOC_LEN              32
 
@@ -370,6 +380,54 @@ static int handle_syscall(struct testcases *test)
 	return ret;
 }
 
+static int get_user_syscall(struct testcases *test)
+{
+	int ret = 0;
+	int ptr_value = 0;
+	void *ptr = &ptr_value;
+	int fd;
+
+	uint64_t bitmask = ((uint64_t)ptr & L5_ADDR) ? L5_SIGN_EXT_MASK :
+						       L4_SIGN_EXT_MASK;
+
+	if (test->lam != 0)
+		if (set_lam(test->lam) != 0)
+			return 2;
+
+	fd = memfd_create("lam_ioctl", 0);
+	if (fd == -1)
+		exit(EXIT_FAILURE);
+
+	switch (test->later) {
+	case GET_USER_USER:
+		/* Control group - properly tagger user pointer */
+		ptr = (void *)set_metadata((uint64_t)ptr, test->lam);
+		break;
+	case GET_USER_KERNEL_TOP:
+		/* Kernel address with top bit cleared */
+		bitmask &= (bitmask >> 1);
+		ptr = (void *)((uint64_t)ptr | bitmask);
+		break;
+	case GET_USER_KERNEL_BOT:
+		/* Kernel address with bottom sign-extension bit cleared */
+		bitmask &= (bitmask << 1);
+		ptr = (void *)((uint64_t)ptr | bitmask);
+		break;
+	case GET_USER_KERNEL:
+		/* Try to pass a kernel address */
+		ptr = (void *)((uint64_t)ptr | bitmask);
+		break;
+	default:
+		printf("Invalid test case value passed!\n");
+		break;
+	}
+
+	if (ioctl(fd, FIOASYNC, ptr) != 0)
+		ret = 1;
+
+	return ret;
+}
+
 int sys_uring_setup(unsigned int entries, struct io_uring_params *p)
 {
 	return (int)syscall(__NR_io_uring_setup, entries, p);
@@ -883,6 +941,33 @@ static struct testcases syscall_cases[] = {
 		.test_func = handle_syscall,
 		.msg = "SYSCALL:[Negative] Disable LAM. Dereferencing pointer with metadata.\n",
 	},
+	{
+		.later = GET_USER_USER,
+		.lam = LAM_U57_BITS,
+		.test_func = get_user_syscall,
+		.msg = "GET_USER: get_user() and pass a properly tagged user pointer.\n",
+	},
+	{
+		.later = GET_USER_KERNEL_TOP,
+		.expected = 1,
+		.lam = LAM_U57_BITS,
+		.test_func = get_user_syscall,
+		.msg = "GET_USER:[Negative] get_user() with a kernel pointer and the top bit cleared.\n",
+	},
+	{
+		.later = GET_USER_KERNEL_BOT,
+		.expected = 1,
+		.lam = LAM_U57_BITS,
+		.test_func = get_user_syscall,
+		.msg = "GET_USER:[Negative] get_user() with a kernel pointer and the bottom sign-extension bit cleared.\n",
+	},
+	{
+		.later = GET_USER_KERNEL,
+		.expected = 1,
+		.lam = LAM_U57_BITS,
+		.test_func = get_user_syscall,
+		.msg = "GET_USER:[Negative] get_user() and pass a kernel pointer.\n",
+	},
 };
 
 static struct testcases mmap_cases[] = {
-- 
2.46.2
Re: [PATCH] selftests/lam: Test get_user() LAM pointer handling
Posted by Kirill A. Shutemov 3 weeks, 4 days ago
On Tue, Oct 29, 2024 at 03:14:20PM +0100, Maciej Wieczor-Retman wrote:
> Recent change in how get_user() handles pointers [1] has a specific case
> for LAM. It assigns a different bitmask that's later used to check
> whether a pointer comes from userland in get_user().
> 
> While currently commented out (until LASS [2] is merged into the kernel)
> it's worth making changes to the LAM selftest ahead of time.
> 
> Add test case to LAM that utilizes a ioctl (FIOASYNC) syscall which uses
> get_user() in its implementation. Execute the syscall with differently
> tagged pointers to verify that valid user pointers are passing through
> and invalid kernel/non-canonical pointers are not.
> 
> Code was tested on a Sierra Forest Xeon machine that's LAM capable. The
> test was ran without issues with both the LAM lines from [1] untouched
> and commented out. The test was also ran without issues with LAM_SUP
> both enabled and disabled.
> 
> [1] https://lore.kernel.org/all/20241024013214.129639-1-torvalds@linux-foundation.org/
> [2] https://lore.kernel.org/all/20240710160655.3402786-1-alexander.shishkin@linux.intel.com/
> 
> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
> ---
>  tools/testing/selftests/x86/lam.c | 85 +++++++++++++++++++++++++++++++
>  1 file changed, 85 insertions(+)
> 
> diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
> index 0ea4f6813930..3c53d4b7aa61 100644
> --- a/tools/testing/selftests/x86/lam.c
> +++ b/tools/testing/selftests/x86/lam.c
> @@ -4,6 +4,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <sys/syscall.h>
> +#include <sys/ioctl.h>
>  #include <time.h>
>  #include <signal.h>
>  #include <setjmp.h>
> @@ -43,10 +44,19 @@
>  #define FUNC_INHERITE           0x20
>  #define FUNC_PASID              0x40
>  
> +/* get_user() pointer test cases */
> +#define GET_USER_USER           0
> +#define GET_USER_KERNEL_TOP     1
> +#define GET_USER_KERNEL_BOT     2
> +#define GET_USER_KERNEL         3
> +
>  #define TEST_MASK               0x7f
> +#define L5_SIGN_EXT_MASK        (0xFFUL << 56)
> +#define L4_SIGN_EXT_MASK        (0x1FFFFUL << 47)
>  
>  #define LOW_ADDR                (0x1UL << 30)
>  #define HIGH_ADDR               (0x3UL << 48)
> +#define L5_ADDR                 (0x1UL << 48)
>  
>  #define MALLOC_LEN              32
>  
> @@ -370,6 +380,54 @@ static int handle_syscall(struct testcases *test)
>  	return ret;
>  }
>  
> +static int get_user_syscall(struct testcases *test)
> +{
> +	int ret = 0;
> +	int ptr_value = 0;
> +	void *ptr = &ptr_value;
> +	int fd;
> +
> +	uint64_t bitmask = ((uint64_t)ptr & L5_ADDR) ? L5_SIGN_EXT_MASK :
> +						       L4_SIGN_EXT_MASK;

Emm. Do you expect stack to be above at the very top of address space on
5-level paging machines? It is not true. We don't allocate any memory
above 46-bit unless asked explicitly.

See tools/testing/selftests/mm/va_high_addr_switch.c

-- 
  Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH] selftests/lam: Test get_user() LAM pointer handling
Posted by Maciej Wieczor-Retman 3 weeks, 4 days ago
On 2024-10-30 at 14:31:51 +0200, Kirill A. Shutemov wrote:
>On Tue, Oct 29, 2024 at 03:14:20PM +0100, Maciej Wieczor-Retman wrote:
>> Recent change in how get_user() handles pointers [1] has a specific case
>> for LAM. It assigns a different bitmask that's later used to check
>> whether a pointer comes from userland in get_user().
>> 
>> While currently commented out (until LASS [2] is merged into the kernel)
>> it's worth making changes to the LAM selftest ahead of time.
>> 
>> Add test case to LAM that utilizes a ioctl (FIOASYNC) syscall which uses
>> get_user() in its implementation. Execute the syscall with differently
>> tagged pointers to verify that valid user pointers are passing through
>> and invalid kernel/non-canonical pointers are not.
>> 
>> Code was tested on a Sierra Forest Xeon machine that's LAM capable. The
>> test was ran without issues with both the LAM lines from [1] untouched
>> and commented out. The test was also ran without issues with LAM_SUP
>> both enabled and disabled.
>> 
>> [1] https://lore.kernel.org/all/20241024013214.129639-1-torvalds@linux-foundation.org/
>> [2] https://lore.kernel.org/all/20240710160655.3402786-1-alexander.shishkin@linux.intel.com/
>> 
>> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
>> ---
>>  tools/testing/selftests/x86/lam.c | 85 +++++++++++++++++++++++++++++++
>>  1 file changed, 85 insertions(+)
>> 
>> diff --git a/tools/testing/selftests/x86/lam.c b/tools/testing/selftests/x86/lam.c
>> index 0ea4f6813930..3c53d4b7aa61 100644
>> --- a/tools/testing/selftests/x86/lam.c
>> +++ b/tools/testing/selftests/x86/lam.c
>> @@ -4,6 +4,7 @@
>>  #include <stdlib.h>
>>  #include <string.h>
>>  #include <sys/syscall.h>
>> +#include <sys/ioctl.h>
>>  #include <time.h>
>>  #include <signal.h>
>>  #include <setjmp.h>
>> @@ -43,10 +44,19 @@
>>  #define FUNC_INHERITE           0x20
>>  #define FUNC_PASID              0x40
>>  
>> +/* get_user() pointer test cases */
>> +#define GET_USER_USER           0
>> +#define GET_USER_KERNEL_TOP     1
>> +#define GET_USER_KERNEL_BOT     2
>> +#define GET_USER_KERNEL         3
>> +
>>  #define TEST_MASK               0x7f
>> +#define L5_SIGN_EXT_MASK        (0xFFUL << 56)
>> +#define L4_SIGN_EXT_MASK        (0x1FFFFUL << 47)
>>  
>>  #define LOW_ADDR                (0x1UL << 30)
>>  #define HIGH_ADDR               (0x3UL << 48)
>> +#define L5_ADDR                 (0x1UL << 48)
>>  
>>  #define MALLOC_LEN              32
>>  
>> @@ -370,6 +380,54 @@ static int handle_syscall(struct testcases *test)
>>  	return ret;
>>  }
>>  
>> +static int get_user_syscall(struct testcases *test)
>> +{
>> +	int ret = 0;
>> +	int ptr_value = 0;
>> +	void *ptr = &ptr_value;
>> +	int fd;
>> +
>> +	uint64_t bitmask = ((uint64_t)ptr & L5_ADDR) ? L5_SIGN_EXT_MASK :
>> +						       L4_SIGN_EXT_MASK;
>
>Emm. Do you expect stack to be above at the very top of address space on
>5-level paging machines? It is not true. We don't allocate any memory
>above 46-bit unless asked explicitly.

Right, I'm not sure why I thought that would work here.

>See tools/testing/selftests/mm/va_high_addr_switch.c

Thanks for the tip, I'll use mmap/munmap to determine the enabled pagetable level.

>
>-- 
>  Kiryl Shutsemau / Kirill A. Shutemov

-- 
Kind regards
Maciej Wieczór-Retman