Skip to contentSkip to conversation

bpf: Move check_uarg_tail_zero() upward

PATCH Superseded Mickaël Salaün 2 participants 4 messages Aug 7, 2017

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bpf: Move check_uarg_tail_zero() upward

The function check_uarg_tail_zero() may be useful for other part of the code in the syscall.c file. Move this function at the beginning of the file.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc David S. Miller <[email protected]>
Cc Kees Cook <[email protected]>
Cc Martin KaFai Lau <[email protected]>

---

This is needed for the Landlock patch series. :)

---
 kernel/bpf/syscall.c | 52 ++++++++++++++++++++++++++--------------------------
 1 file changed, 26 insertions(+), 26 deletions(-)
kernel/bpf/syscall.c +26 -26
index 6c772adabad2..c653ee0bd162 100644
@@ -48,6 +48,32 @@ static const struct bpf_map_ops * const bpf_map_types[] = {
 #undef BPF_MAP_TYPE
 };
 
+static int check_uarg_tail_zero(void __user *uaddr,
+				size_t expected_size,
+				size_t actual_size)
+{
+	unsigned char __user *addr;
+	unsigned char __user *end;
+	unsigned char val;
+	int err;
+
+	if (actual_size <= expected_size)
+		return 0;
+
+	addr = uaddr + expected_size;
+	end  = uaddr + actual_size;
+
+	for (; addr < end; addr++) {
+		err = get_user(val, addr);
+		if (err)
+			return err;
+		if (val)
+			return -E2BIG;
+	}
+
+	return 0;
+}
+
 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
 {
 	struct bpf_map *map;
@@ -1246,32 +1272,6 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
 	return fd;
 }
 
-static int check_uarg_tail_zero(void __user *uaddr,
-				size_t expected_size,
-				size_t actual_size)
-{
-	unsigned char __user *addr;
-	unsigned char __user *end;
-	unsigned char val;
-	int err;
-
-	if (actual_size <= expected_size)
-		return 0;
-
-	addr = uaddr + expected_size;
-	end  = uaddr + actual_size;
-
-	for (; addr < end; addr++) {
-		err = get_user(val, addr);
-		if (err)
-			return err;
-		if (val)
-			return -E2BIG;
-	}
-
-	return 0;
-}
-
 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
 				   const union bpf_attr *attr,
 				   union bpf_attr __user *uattr)
Signature · 1 line
2.13.3
#1 lore raw

PATCH 2/2 bpf: Extend check_uarg_tail_zero() checks

The function check_uarg_tail_zero() was created from bpf(2) for BPF_OBJ_GET_INFO_BY_FD without taking the access_ok() nor the PAGE_SIZE checks. Make this checks more generally available while unlikely to be triggered, extend the memory range check and add an explanation including why the ToCToU should not be a security concern.

---
 kernel/bpf/syscall.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
kernel/bpf/syscall.c +15 -11
index c653ee0bd162..b884fdc371e0 100644
@@ -48,6 +48,15 @@ static const struct bpf_map_ops * const bpf_map_types[] = {
 #undef BPF_MAP_TYPE
 };
 
+/*
+ * If we're handed a bigger struct than we know of, ensure all the unknown bits
+ * are 0 - i.e. new user-space does not rely on any kernel feature extensions
+ * we dont know about yet.
+ *
+ * There is a ToCToU between this function call and the following
+ * copy_from_user() call. However, this should not be a concern since this
+ * function is meant to be a future-proofing of bits.
+ */
 static int check_uarg_tail_zero(void __user *uaddr,
 				size_t expected_size,
 				size_t actual_size)
@@ -57,6 +66,12 @@ static int check_uarg_tail_zero(void __user *uaddr,
 	unsigned char val;
 	int err;
 
+	if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
+		return -EFAULT;
+
+	if (unlikely(actual_size > PAGE_SIZE))	/* silly large */
+		return -E2BIG;
+
 	if (actual_size <= expected_size)
 		return 0;
 
@@ -1393,17 +1408,6 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
 	if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
 		return -EPERM;
 
-	if (!access_ok(VERIFY_READ, uattr, 1))
-		return -EFAULT;
-
-	if (size > PAGE_SIZE)	/* silly large */
-		return -E2BIG;
-
-	/* If we're handed a bigger struct than we know of,
-	 * ensure all the unknown bits are 0 - i.e. new
-	 * user-space does not rely on any kernel feature
-	 * extensions we dont know about yet.
-	 */
 	err = check_uarg_tail_zero(uattr, sizeof(attr), size);
 	if (err)
 		return err;
Signature · 1 line
2.13.3
maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH net-next v1 2/2] bpf: Extend check_uarg_tail_zero() checks

Quoted 22 lines · Mickaël Salaün

The function check_uarg_tail_zero() was created from bpf(2) for BPF_OBJ_GET_INFO_BY_FD without taking the access_ok() nor the PAGE_SIZE checks. Make this checks more generally available while unlikely to be triggered, extend the memory range check and add an explanation including why the ToCToU should not be a security concern.

---
  kernel/bpf/syscall.c | 26 +++++++++++++++-----------
  1 file changed, 15 insertions(+), 11 deletions(-)
kernel/bpf/syscall.c +0 -0
index c653ee0bd162..b884fdc371e0 100644

kernel/bpf/syscall.c static const struct bpf_map_ops * const bpf_map_types[] = { · line 54

Quoted 7 lines
kernel/bpf/syscall.c +4 -0
@@ -48,6 +48,15 @@ static const struct bpf_map_ops * const bpf_map_types[] = {
  #undef BPF_MAP_TYPE
  };
 
+/*
+ * If we're handed a bigger struct than we know of, ensure all the unknown bits
+ * are 0 - i.e. new user-space does not rely on any kernel feature extensions
+ * we dont know about yet.

Nit: don't

kernel/bpf/syscall.c static const struct bpf_map_ops * const bpf_map_types[] = { · line 57

Quoted 3 lines
kernel/bpf/syscall.c +3 -0
+ *
+ * There is a ToCToU between this function call and the following
+ * copy_from_user() call. However, this should not be a concern since this

Lets make it a bit more clear to the reader: s/should not/is not/

Quoted 6 lines
kernel/bpf/syscall.c +2 -0
+ * function is meant to be a future-proofing of bits.
+ */
  static int check_uarg_tail_zero(void __user *uaddr,
  				size_t expected_size,
  				size_t actual_size)

kernel/bpf/syscall.c static int check_uarg_tail_zero(void __user *uaddr, · line 74

Quoted 9 lines
kernel/bpf/syscall.c +6 -0
@@ -57,6 +66,12 @@ static int check_uarg_tail_zero(void __user *uaddr,
  	unsigned char val;
  	int err;
 
+	if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
+		return -EFAULT;
+
+	if (unlikely(actual_size > PAGE_SIZE))	/* silly large */
+		return -E2BIG;
+

Yeah, moving the checks into check_uarg_tail_zero() is fine by me. Can we make the 'silly large' test first, so we don't generate unnecessary work if we bail out later anyway?

Other than that:

Thanks,
Daniel

Quoted 22 lines
kernel/bpf/syscall.c +0 -11
  	if (actual_size <= expected_size)
  		return 0;
 
@@ -1393,17 +1408,6 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
  	if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
  		return -EPERM;
 
-	if (!access_ok(VERIFY_READ, uattr, 1))
-		return -EFAULT;
-
-	if (size > PAGE_SIZE)	/* silly large */
-		return -E2BIG;
-
-	/* If we're handed a bigger struct than we know of,
-	 * ensure all the unknown bits are 0 - i.e. new
-	 * user-space does not rely on any kernel feature
-	 * extensions we dont know about yet.
-	 */
  	err = check_uarg_tail_zero(uattr, sizeof(attr), size);
  	if (err)
  		return err;
maintainer Acked-by on v1 1/2
#3 lore raw
In reply to Mickaël Salaün
Quoted 10 lines · Mickaël Salaün

The function check_uarg_tail_zero() may be useful for other part of the code in the syscall.c file. Move this function at the beginning of the file.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc David S. Miller <[email protected]>
Cc Kees Cook <[email protected]>
Cc Martin KaFai Lau <[email protected]>