Skip to contentSkip to conversation

net: socket: Fix potential spectre v1 gadgets

PATCH In review Jeremy Cline 3 participants 9 messages Jul 27, 2018 – Aug 13, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover net: socket: Fix potential spectre v1 gadgets

Hi folks,

This fixes a pair of potential spectre v1 gadgets.

Note that because the speculation window is large, the policy is to stop the speculative out-of-bounds load and not worry if the attack can be completed with a dependent load or store[0].

[0] https://marc.info/?l=linux-kernel&m=152449131114778

Jeremy Cline (2):
net: socket: fix potential spectre v1 gadget in socketcall
net: socket: Fix potential spectre v1 gadget in sock_is_registered

 net/socket.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 net: socket: fix potential spectre v1 gadget in socketcall

'call' is a user-controlled value, so sanitize the array index after the bounds check to avoid speculating past the bounds of the 'nargs' array.

Found with the help of Smatch:

net/socket.c:2508 __do_sys_socketcall() warn: potential spectre issue 'nargs' [r] (local cap)

---
 net/socket.c | 2 ++
 1 file changed, 2 insertions(+)
net/socket.c +2 -0
index 3015ddace71e..f15d5cbb3ba4 100644
@@ -89,6 +89,7 @@
 #include <linux/magic.h>
 #include <linux/slab.h>
 #include <linux/xattr.h>
+#include <linux/nospec.h>
 
 #include <linux/uaccess.h>
 #include <asm/unistd.h>
@@ -2504,6 +2505,7 @@ SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
 
 	if (call < 1 || call > SYS_SENDMMSG)
 		return -EINVAL;
+	call = array_index_nospec(call, SYS_SENDMMSG + 1);
 
 	len = nargs[call];
 	if (len > sizeof(a))
Signature · 1 line
2.17.1
#3 lore raw

PATCH 2/2 net: socket: Fix potential spectre v1 gadget in sock_is_registered

In reply to Jeremy Cline

'family' can be a user-controlled value, so sanitize it after the bounds check to avoid speculative out-of-bounds access.

---
 net/socket.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/socket.c +2 -1
index f15d5cbb3ba4..608e29ae6baf 100644
@@ -2672,7 +2672,8 @@ EXPORT_SYMBOL(sock_unregister);
 
 bool sock_is_registered(int family)
 {
-	return family < NPROTO && rcu_access_pointer(net_families[family]);
+	return family < NPROTO &&
+		rcu_access_pointer(net_families[array_index_nospec(family, NPROTO)]);
 }
 
 static int __init sock_init(void)
Signature · 1 line
2.17.1
#4 lore raw

Re: [PATCH 2/2] net: socket: Fix potential spectre v1 gadget in sock_is_registered

In reply to Jeremy Cline
Quoted 15 lines · Jeremy Cline

'family' can be a user-controlled value, so sanitize it after the bounds check to avoid speculative out-of-bounds access.

---
 net/socket.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/socket.c +0 -0
index f15d5cbb3ba4..608e29ae6baf 100644

net/socket.c EXPORT_SYMBOL(sock_unregister); · line 2679

Quoted 9 lines
net/socket.c +2 -1
@@ -2672,7 +2672,8 @@ EXPORT_SYMBOL(sock_unregister);
 
 bool sock_is_registered(int family)
 {
-	return family < NPROTO && rcu_access_pointer(net_families[family]);
+	return family < NPROTO &&
+		rcu_access_pointer(net_families[array_index_nospec(family, NPROTO)]);
 }
 
 static int __init sock_init(void)

This is another one where I think it would be better to do the nospec clamp higher up the call chain. The untrusted 'family' value comes from __sock_diag_cmd():

__sock_diag_cmd
sock_load_diag_module
sock_is_registered

That function has a bounds check, and also uses the value in some other array accesses:

	if (req->sdiag_family >= AF_MAX)
		return -EINVAL;

	if (sock_diag_handlers[req->sdiag_family] == NULL)
		sock_load_diag_module(req->sdiag_family, 0);

	mutex_lock(&sock_diag_table_mutex);
	hndl = sock_diag_handlers[req->sdiag_family];
	...

So I think clamping 'req->sdiag_family' right after the bounds check would be the way to go.

Signature · 1 line
Josh
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] net: socket: fix potential spectre v1 gadget in socketcall

In reply to Jeremy Cline
Quoted 28 lines · Jeremy Cline

'call' is a user-controlled value, so sanitize the array index after the bounds check to avoid speculating past the bounds of the 'nargs' array.

Found with the help of Smatch:

net/socket.c:2508 __do_sys_socketcall() warn: potential spectre issue 'nargs' [r] (local cap)

---
 net/socket.c | 2 ++
 1 file changed, 2 insertions(+)
net/socket.c +1 -0
index 3015ddace71e..f15d5cbb3ba4 100644
@@ -89,6 +89,7 @@
 #include <linux/magic.h>
 #include <linux/slab.h>
 #include <linux/xattr.h>
+#include <linux/nospec.h>
 
 #include <linux/uaccess.h>
 #include <asm/unistd.h>

net/socket.c SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) · line 2511

Quoted 7 lines
net/socket.c +1 -0
@@ -2504,6 +2505,7 @@ SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
 
 	if (call < 1 || call > SYS_SENDMMSG)
 		return -EINVAL;
+	call = array_index_nospec(call, SYS_SENDMMSG + 1);
 
 	len = nargs[call];
 	if (len > sizeof(a))
Signature · 1 line
Josh
#5 lore raw

Re: [PATCH 2/2] net: socket: Fix potential spectre v1 gadget in sock_is_registered

In reply to Josh Poimboeuf
Quoted 47 lines · Josh Poimboeuf
Quoted 15 lines · Jeremy Cline

'family' can be a user-controlled value, so sanitize it after the bounds check to avoid speculative out-of-bounds access.

---
 net/socket.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/socket.c +0 -0
index f15d5cbb3ba4..608e29ae6baf 100644

net/socket.c EXPORT_SYMBOL(sock_unregister); · line 2679

Quoted 9 lines
net/socket.c +2 -1
@@ -2672,7 +2672,8 @@ EXPORT_SYMBOL(sock_unregister);
 
 bool sock_is_registered(int family)
 {
-	return family < NPROTO && rcu_access_pointer(net_families[family]);
+	return family < NPROTO &&
+		rcu_access_pointer(net_families[array_index_nospec(family, NPROTO)]);
 }
 
 static int __init sock_init(void)

This is another one where I think it would be better to do the nospec clamp higher up the call chain. The untrusted 'family' value comes from __sock_diag_cmd():

__sock_diag_cmd
sock_load_diag_module
sock_is_registered

That function has a bounds check, and also uses the value in some other array accesses:

	if (req->sdiag_family >= AF_MAX)
		return -EINVAL;

	if (sock_diag_handlers[req->sdiag_family] == NULL)
		sock_load_diag_module(req->sdiag_family, 0);

	mutex_lock(&sock_diag_table_mutex);
	hndl = sock_diag_handlers[req->sdiag_family];
	...
Quoted 3 lines

So I think clamping 'req->sdiag_family' right after the bounds check would be the way to go.

Indeed, the clamp there would cover this clamp. I had a scheme that I quickly fix all the gadgets in functions with local comparisons, but clearly that's going to result in call chains with multiple clamps.

I can fix this in a follow-up with a clamp here, or respin this patch set, whatever is easier for David.

Thanks for the review!

#6 lore raw

Re: [PATCH 2/2] net: socket: Fix potential spectre v1 gadget in sock_is_registered

Quoted 52 lines · Jeremy Cline
Quoted 47 lines · Josh Poimboeuf
Quoted 15 lines · Jeremy Cline

'family' can be a user-controlled value, so sanitize it after the bounds check to avoid speculative out-of-bounds access.

---
 net/socket.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/socket.c +0 -0
index f15d5cbb3ba4..608e29ae6baf 100644

net/socket.c EXPORT_SYMBOL(sock_unregister); · line 2679

Quoted 9 lines
net/socket.c +2 -1
@@ -2672,7 +2672,8 @@ EXPORT_SYMBOL(sock_unregister);
 
 bool sock_is_registered(int family)
 {
-	return family < NPROTO && rcu_access_pointer(net_families[family]);
+	return family < NPROTO &&
+		rcu_access_pointer(net_families[array_index_nospec(family, NPROTO)]);
 }
 
 static int __init sock_init(void)

This is another one where I think it would be better to do the nospec clamp higher up the call chain. The untrusted 'family' value comes from __sock_diag_cmd():

__sock_diag_cmd
sock_load_diag_module
sock_is_registered

That function has a bounds check, and also uses the value in some other array accesses:

	if (req->sdiag_family >= AF_MAX)
		return -EINVAL;

	if (sock_diag_handlers[req->sdiag_family] == NULL)
		sock_load_diag_module(req->sdiag_family, 0);

	mutex_lock(&sock_diag_table_mutex);
	hndl = sock_diag_handlers[req->sdiag_family];
	...
Quoted 3 lines

So I think clamping 'req->sdiag_family' right after the bounds check would be the way to go.

Quoted 6 lines

Indeed, the clamp there would cover this clamp. I had a scheme that I quickly fix all the gadgets in functions with local comparisons, but clearly that's going to result in call chains with multiple clamps.

I can fix this in a follow-up with a clamp here, or respin this patch set, whatever is easier for David.

Hi Jeremy,

Just checking up on this... since this patch was merged, will you be doing a followup patch?

Signature · 1 line
Josh
#7 lore raw

Re: [PATCH 2/2] net: socket: Fix potential spectre v1 gadget in sock_is_registered

Quoted 57 lines · Josh Poimboeuf
Quoted 56 lines · Jeremy Cline
Quoted 47 lines · Josh Poimboeuf
Quoted 15 lines · Jeremy Cline

'family' can be a user-controlled value, so sanitize it after the bounds check to avoid speculative out-of-bounds access.

---
 net/socket.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/socket.c +0 -0
index f15d5cbb3ba4..608e29ae6baf 100644

net/socket.c EXPORT_SYMBOL(sock_unregister); · line 2679

Quoted 9 lines
net/socket.c +2 -1
@@ -2672,7 +2672,8 @@ EXPORT_SYMBOL(sock_unregister);
 
 bool sock_is_registered(int family)
 {
-	return family < NPROTO && rcu_access_pointer(net_families[family]);
+	return family < NPROTO &&
+		rcu_access_pointer(net_families[array_index_nospec(family, NPROTO)]);
 }
 
 static int __init sock_init(void)

This is another one where I think it would be better to do the nospec clamp higher up the call chain. The untrusted 'family' value comes from __sock_diag_cmd():

__sock_diag_cmd
sock_load_diag_module
sock_is_registered

That function has a bounds check, and also uses the value in some other array accesses:

	if (req->sdiag_family >= AF_MAX)
		return -EINVAL;

	if (sock_diag_handlers[req->sdiag_family] == NULL)
		sock_load_diag_module(req->sdiag_family, 0);

	mutex_lock(&sock_diag_table_mutex);
	hndl = sock_diag_handlers[req->sdiag_family];
	...
Quoted 3 lines

So I think clamping 'req->sdiag_family' right after the bounds check would be the way to go.

Indeed, the clamp there would cover this clamp. I had a scheme that I quickly fix all the gadgets in functions with local comparisons, but clearly that's going to result in call chains with multiple clamps.

Quoted 8 lines
Quoted 2 lines

I can fix this in a follow-up with a clamp here, or respin this patch set, whatever is easier for David.

Hi Jeremy,

Just checking up on this... since this patch was merged, will you be doing a followup patch?

Yes, apologies, I've been traveling. I'll have a patch tomorrow.