Skip to contentSkip to conversation

x86, pkeys: check against max pkey to avoid overflows

PATCH In review Dave Hansen 5 participants 9 messages Feb 23, 2017 – Mar 1, 2017

Subsystems (3)
lore
Share & tools
lore

Dave Hansen

author maintainer
#0 lore raw

PATCH 1/2 x86, pkeys: check against max pkey to avoid overflows

From: Dave Hansen <[email protected]>

Kirill got a warning from UBSAN about undefined behavior when using protection keys. He is running on hardware that actually has support for it, which is not widely available.

The warning was because we did some very large shifts of integers when doing a pkey_free() of a large, invalid value because we never check that the pkey "fits" into the mm_pkey_allocation_map().

I do not believe there is any danger here of anything bad happening other than some aliasing issues where somebody could do:

	pkey_free(35);

and the kernel would effectively execute:

	pkey_free(8);

While this might be confusing to an app that was doing something stupid, it has to do something stupid and the effects are limited to the app shooting itself in the foot.

---

 b/arch/x86/include/asm/pkeys.h |   15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/include/asm/pkeys.h renamed from arch/x86/include/asm/pkeys.h~do-max-pkey-check +9 -6
diff -puN arch/x86/include/asm/pkeys.h~do-max-pkey-check arch/x86/include/asm/pkeys.h
@@ -46,6 +46,15 @@ extern int __arch_set_user_pkey_access(s
 static inline
 bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
 {
+	/*
+	 * "Allocated" pkeys are those that have been returned
+	 * from pkey_alloc().  pkey 0 is special, and never
+	 * returned from pkey_alloc().
+	 */
+	if (pkey <= 0)
+		return false;
+	if (pkey >= arch_max_pkey())
+		return false;
 	return mm_pkey_allocation_map(mm) & (1U << pkey);
 }
 
@@ -82,12 +91,6 @@ int mm_pkey_alloc(struct mm_struct *mm)
 static inline
 int mm_pkey_free(struct mm_struct *mm, int pkey)
 {
-	/*
-	 * pkey 0 is special, always allocated and can never
-	 * be freed.
-	 */
-	if (!pkey)
-		return -EINVAL;
 	if (!mm_pkey_is_allocated(mm, pkey))
 		return -EINVAL;
 

_

author maintainer
#1 lore raw

PATCH 2/2 selftests, x86, pkeys: test with random, unallocated protection keys

Shuah, I assume you'll take this patch in through the selftests tree.

--
From: Dave Hansen <[email protected]>

The kernel pkeys code had a minor bug where it did some large shifts to an integer which is undefined behavior in C. It didn't cause any real harm, but it is screwy behavior that the kernel should have rejected.

Add a test case for this.

ec: Kirill A. Shutemov <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---

 b/tools/testing/selftests/x86/protection_keys.c |   25 ++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
tools/testing/selftests/x86/protection_keys.c renamed from tools/testing/selftests/x86/protection_keys.c~pkeys-better-selftests-of-random-pkey +25 -0
diff -puN tools/testing/selftests/x86/protection_keys.c~pkeys-better-selftests-of-random-pkey tools/testing/selftests/x86/protection_keys.c
@@ -1123,6 +1123,30 @@ void test_pkey_syscalls_on_non_allocated
 }
 
 /* Assumes that all pkeys other than 'pkey' are unallocated */
+void test_pkey_syscalls_on_non_allocated_random_pkey(int *ptr, u16 pkey)
+{
+	int err;
+	int nr_tests = 0;
+
+	while (nr_tests < 1000) {
+		int test_pkey = rand();
+
+		/* do not test with the pkey we know is good */
+		if (pkey == test_pkey)
+			continue;
+
+		dprintf1("trying free/mprotect bad pkey: %2d\n", test_pkey);
+		err = sys_pkey_free(test_pkey);
+		pkey_assert(err);
+
+		err = sys_mprotect_pkey(ptr, PAGE_SIZE, PROT_READ, test_pkey);
+		pkey_assert(err);
+
+		nr_tests++;
+	}
+}
+
+/* Assumes that all pkeys other than 'pkey' are unallocated */
 void test_pkey_syscalls_bad_args(int *ptr, u16 pkey)
 {
 	int err;
@@ -1320,6 +1344,7 @@ void (*pkey_tests[])(int *ptr, u16 pkey)
 	test_executing_on_unreadable_memory,
 	test_ptrace_of_child,
 	test_pkey_syscalls_on_non_allocated_pkey,
+	test_pkey_syscalls_on_non_allocated_random_pkey,
 	test_pkey_syscalls_bad_args,
 	test_pkey_alloc_exhaust,
 };

_

maintainer
#2 lore raw

Re: [PATCH 2/2] selftests, x86, pkeys: test with random, unallocated protection keys

Quoted 1 line · Dave Hansen

Shuah, I assume you'll take this patch in through the selftests tree.

Yes I can do that.

-- Shuah

Quoted 67 lines

--
From: Dave Hansen <[email protected]>

The kernel pkeys code had a minor bug where it did some large shifts to an integer which is undefined behavior in C. It didn't cause any real harm, but it is screwy behavior that the kernel should have rejected.

Add a test case for this.

ec: Kirill A. Shutemov <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---

 b/tools/testing/selftests/x86/protection_keys.c |   25 ++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff -puN tools/testing/selftests/x86/protection_keys.c~pkeys-better-selftests-of-random-pkey tools/testing/selftests/x86/protection_keys.c

tools/testing/selftests/x86/protection_keys.c renamed from tools/testing/selftests/x86/protection_keys.c~pkeys-better-selftests-of-random-pkey +25 -0
@@ -1123,6 +1123,30 @@ void test_pkey_syscalls_on_non_allocated
 }
 
 /* Assumes that all pkeys other than 'pkey' are unallocated */
+void test_pkey_syscalls_on_non_allocated_random_pkey(int *ptr, u16 pkey)
+{
+	int err;
+	int nr_tests = 0;
+
+	while (nr_tests < 1000) {
+		int test_pkey = rand();
+
+		/* do not test with the pkey we know is good */
+		if (pkey == test_pkey)
+			continue;
+
+		dprintf1("trying free/mprotect bad pkey: %2d\n", test_pkey);
+		err = sys_pkey_free(test_pkey);
+		pkey_assert(err);
+
+		err = sys_mprotect_pkey(ptr, PAGE_SIZE, PROT_READ, test_pkey);
+		pkey_assert(err);
+
+		nr_tests++;
+	}
+}
+
+/* Assumes that all pkeys other than 'pkey' are unallocated */
 void test_pkey_syscalls_bad_args(int *ptr, u16 pkey)
 {
 	int err;
@@ -1320,6 +1344,7 @@ void (*pkey_tests[])(int *ptr, u16 pkey)
 	test_executing_on_unreadable_memory,
 	test_ptrace_of_child,
 	test_pkey_syscalls_on_non_allocated_pkey,
+	test_pkey_syscalls_on_non_allocated_random_pkey,
 	test_pkey_syscalls_bad_args,
 	test_pkey_alloc_exhaust,
 };

_

#3 lore raw

Re: [PATCH 2/2] selftests, x86, pkeys: test with random, unallocated protection keys

Quoted 4 lines · Shuah Khan
Quoted 1 line · Dave Hansen

Shuah, I assume you'll take this patch in through the selftests tree.

Yes I can do that.

No, let's not do that please, we have a fix and a self-tests update, I'd like them to be next in the Git space.

I'll apply the testcase too when applying the pkeys fix, ok?

Thanks,

Ingo

maintainer
#4 lore raw

Re: [PATCH 2/2] selftests, x86, pkeys: test with random, unallocated protection keys

Quoted 8 lines · Ingo Molnar
Quoted 4 lines · Shuah Khan
Quoted 1 line · Dave Hansen

Shuah, I assume you'll take this patch in through the selftests tree.

Yes I can do that.

Quoted 10 lines

No, let's not do that please, we have a fix and a self-tests update, I'd like them to be next in the Git space.

I'll apply the testcase too when applying the pkeys fix, ok?

Thanks,

Ingo

Yup. I noticed the first patch has to go through x86 tree, Sounds good to me. Please take both through. I am sending pull request for 4.11-rc1 today. I have a few pkeys patches in there. Hope there won't be any conflicts.

thanks,
-- Shuah

#5 lore raw

Re: [PATCH 2/2] selftests, x86, pkeys: test with random, unallocated protection keys

Quoted 21 lines · Shuah Khan
Quoted 8 lines · Ingo Molnar
Quoted 4 lines · Shuah Khan
Quoted 1 line · Dave Hansen

Shuah, I assume you'll take this patch in through the selftests tree.

Yes I can do that.

Quoted 10 lines

No, let's not do that please, we have a fix and a self-tests update, I'd like them to be next in the Git space.

I'll apply the testcase too when applying the pkeys fix, ok?

Thanks,

Ingo

Quoted 4 lines

Yup. I noticed the first patch has to go through x86 tree, Sounds good to me. Please take both through. I am sending pull request for 4.11-rc1 today. I have a few pkeys patches in there. Hope there won't be any conflicts.

Ok, I'll wait for your changes to hit upstream, to not create unnecessary conflicts.

Thanks,

Ingo

#6 lore raw

Re: [PATCH 2/2] selftests, x86, pkeys: test with random, unallocated protection keys

In reply to Dave Hansen
Quoted 29 lines · Dave Hansen

Shuah, I assume you'll take this patch in through the selftests tree.

--
From: Dave Hansen <[email protected]>

The kernel pkeys code had a minor bug where it did some large shifts to an integer which is undefined behavior in C. It didn't cause any real harm, but it is screwy behavior that the kernel should have rejected.

Add a test case for this.

ec: Kirill A. Shutemov <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---

 b/tools/testing/selftests/x86/protection_keys.c |   25 ++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff -puN tools/testing/selftests/x86/protection_keys.c~pkeys-better-selftests-of-random-pkey tools/testing/selftests/x86/protection_keys.c

tools/testing/selftests/x86/protection_keys.c void test_pkey_syscalls_on_non_allocated · line 1132

Quoted 10 lines
tools/testing/selftests/x86/protection_keys.c renamed from tools/testing/selftests/x86/protection_keys.c~pkeys-better-selftests-of-random-pkey +7 -0
@@ -1123,6 +1123,30 @@ void test_pkey_syscalls_on_non_allocated
 }
 
 /* Assumes that all pkeys other than 'pkey' are unallocated */
+void test_pkey_syscalls_on_non_allocated_random_pkey(int *ptr, u16 pkey)
+{
+	int err;
+	int nr_tests = 0;
+
+	while (nr_tests < 1000) {
+		int test_pkey = rand();

rand(3) doesn't generate negative numbers. Would be good to cover this case too.

		int test_pkey = rand() - RAND_MAX/2;

?

Otherwise looks good to me.

Signature · 1 line
 Kirill A. Shutemov
Acked-by on v1 1/2
#7 lore raw
In reply to Dave Hansen
Quoted 18 lines · Dave Hansen

From: Dave Hansen <[email protected]>

Kirill got a warning from UBSAN about undefined behavior when using protection keys. He is running on hardware that actually has support for it, which is not widely available.

The warning was because we did some very large shifts of integers when doing a pkey_free() of a large, invalid value because we never check that the pkey "fits" into the mm_pkey_allocation_map().

I do not believe there is any danger here of anything bad happening other than some aliasing issues where somebody could do:

	pkey_free(35);

and the kernel would effectively execute:

Quoted 7 lines
	pkey_free(8);

While this might be confusing to an app that was doing something stupid, it has to do something stupid and the effects are limited to the app shooting itself in the foot.

Signature · 1 line
 Kirill A. Shutemov
#8 lore raw

[tip:x86/urgent] x86/pkeys: Check against max pkey to avoid overflows

In reply to Dave Hansen
x86/pkeys: Check against max pkey to avoid overflows
Commit-ID:  58ab9a088ddac4efe823471275859d64f735577e
Gitweb:     http://git.kernel.org/tip/58ab9a088ddac4efe823471275859d64f735577e
Author:     Dave Hansen <[email protected]>
AuthorDate: Thu, 23 Feb 2017 14:26:03 -0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 1 Mar 2017 10:51:50 +0100

x86/pkeys: Check against max pkey to avoid overflows

Kirill reported a warning from UBSAN about undefined behavior when using protection keys. He is running on hardware that actually has support for it, which is not widely available.

The warning triggers because of very large shifts of integers when doing a pkey_free() of a large, invalid value. This happens because we never check that the pkey "fits" into the mm_pkey_allocation_map().

I do not believe there is any danger here of anything bad happening other than some aliasing issues where somebody could do:

	pkey_free(35);

and the kernel would effectively execute:

	pkey_free(8);

While this might be confusing to an app that was doing something stupid, it has to do something stupid and the effects are limited to the app shooting itself in the foot.

---
 arch/x86/include/asm/pkeys.h | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/include/asm/pkeys.h +9 -6
index 34684ad..b3b09b9 100644
@@ -46,6 +46,15 @@ extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 static inline
 bool mm_pkey_is_allocated(struct mm_struct *mm, int pkey)
 {
+	/*
+	 * "Allocated" pkeys are those that have been returned
+	 * from pkey_alloc().  pkey 0 is special, and never
+	 * returned from pkey_alloc().
+	 */
+	if (pkey <= 0)
+		return false;
+	if (pkey >= arch_max_pkey())
+		return false;
 	return mm_pkey_allocation_map(mm) & (1U << pkey);
 }
 
@@ -82,12 +91,6 @@ int mm_pkey_alloc(struct mm_struct *mm)
 static inline
 int mm_pkey_free(struct mm_struct *mm, int pkey)
 {
-	/*
-	 * pkey 0 is special, always allocated and can never
-	 * be freed.
-	 */
-	if (!pkey)
-		return -EINVAL;
 	if (!mm_pkey_is_allocated(mm, pkey))
 		return -EINVAL;