Skip to contentSkip to conversation

bpf: avoid copying junk bytes in bpf_get_current_comm()

PATCH In review Alexei Starovoitov 3 participants 6 messages Mar 10, 2016 – Mar 11, 2016

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

PATCH 1/1 bpf: avoid copying junk bytes in bpf_get_current_comm()

Lots of places in the kernel use memcpy(buf, comm, TASK_COMM_LEN); but the result is typically passed to print("%s", buf) and extra bytes after zero don't cause any harm.
In bpf the result of bpf_get_current_comm() is used as the part of map key and was causing spurious hash map mismatches.
Use strlcpy() to guarantee zero-terminated string.
bpf verifier checks that output buffer is zero-initialized, so even for short task names the output buffer don't have junk bytes. Note it's not a security concern, since kprobe+bpf is root only.

Fixes ffeedafbf023 ("bpf: introduce current->pid, tgid, uid, gid, comm accessors")

---
Targeting net-next, since it's too late for net.
I think it makes sense for stable as well.

 kernel/bpf/helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/helpers.c +1 -1
index 4504ca66118d..50da680c479f 100644
@@ -166,7 +166,7 @@ static u64 bpf_get_current_comm(u64 r1, u64 size, u64 r3, u64 r4, u64 r5)
 	if (!task)
 		return -EINVAL;
 
-	memcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));
+	strlcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));
 	return 0;
 }
 
Signature · 1 line
2.8.0.rc1
maintainer
#1 lore raw

From: Alexei Starovoitov <[email protected]>
Date: Wed, 9 Mar 2016 20:02:33 -0800

Quoted 10 lines

Lots of places in the kernel use memcpy(buf, comm, TASK_COMM_LEN); but the result is typically passed to print("%s", buf) and extra bytes after zero don't cause any harm.
In bpf the result of bpf_get_current_comm() is used as the part of map key and was causing spurious hash map mismatches.
Use strlcpy() to guarantee zero-terminated string.
bpf verifier checks that output buffer is zero-initialized, so even for short task names the output buffer don't have junk bytes. Note it's not a security concern, since kprobe+bpf is root only.

Quoted 6 lines
Fixes ffeedafbf023 ("bpf: introduce current->pid, tgid, uid, gid, comm accessors")

---
Targeting net-next, since it's too late for net.
I think it makes sense for stable as well.

Applied and queued up for -stable, thanks.

maintainer
#2 lore raw
In reply to Alexei Starovoitov
Quoted 7 lines · Alexei Starovoitov

Lots of places in the kernel use memcpy(buf, comm, TASK_COMM_LEN); but the result is typically passed to print("%s", buf) and extra bytes after zero don't cause any harm.
In bpf the result of bpf_get_current_comm() is used as the part of map key and was causing spurious hash map mismatches.
Use strlcpy() to guarantee zero-terminated string.
bpf verifier checks that output buffer is zero-initialized,

Sorry for late reply, more below:

Quoted 6 lines

so even for short task names the output buffer don't have junk bytes. Note it's not a security concern, since kprobe+bpf is root only.

Fixes ffeedafbf023 ("bpf: introduce current->pid, tgid, uid, gid, comm accessors")

[...]

kernel/bpf/helpers.c static u64 bpf_get_current_comm(u64 r1, u64 size, u64 r3, u64 r4, u64 r5) · line 169

Quoted 10 lines
kernel/bpf/helpers.c +1 -1
index 4504ca66118d..50da680c479f 100644
@@ -166,7 +166,7 @@ static u64 bpf_get_current_comm(u64 r1, u64 size, u64 r3, u64 r4, u64 r5)
  	if (!task)
  		return -EINVAL;
 
-	memcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));
+	strlcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));

If I see this correctly, __set_task_comm() makes sure comm is always zero terminated, so that seems good, but isn't it already sufficient when switching to strlcpy() to simply use:

    strlcpy(buf, task->comm, size);

The min_t() seems unnecessary work to me, why do we still need it? size is guaranteed to be > 0 through the eBPF verifier, so strlcpy() should take care of the rest.

Thanks,
Daniel

#3 lore raw
Quoted 36 lines · Daniel Borkmann
Quoted 7 lines · Alexei Starovoitov

Lots of places in the kernel use memcpy(buf, comm, TASK_COMM_LEN); but the result is typically passed to print("%s", buf) and extra bytes after zero don't cause any harm.
In bpf the result of bpf_get_current_comm() is used as the part of map key and was causing spurious hash map mismatches.
Use strlcpy() to guarantee zero-terminated string.
bpf verifier checks that output buffer is zero-initialized,

Sorry for late reply, more below:

Quoted 7 lines

so even for short task names the output buffer don't have junk bytes. Note it's not a security concern, since kprobe+bpf is root only.

Fixes ffeedafbf023 ("bpf: introduce current->pid, tgid, uid, gid,

comm accessors")

[...]

kernel/bpf/helpers.c static u64 bpf_get_current_comm(u64 r1, u64 size, · line 170

Quoted 11 lines
kernel/bpf/helpers.c +1 -1
index 4504ca66118d..50da680c479f 100644
@@ -166,7 +166,7 @@ static u64 bpf_get_current_comm(u64 r1, u64 size,
 u64 r3, u64 r4, u64 r5)
      if (!task)
          return -EINVAL;
 
-    memcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));
+    strlcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));

If I see this correctly, __set_task_comm() makes sure comm is always zero terminated, so that seems good, but isn't it already sufficient when switching
to strlcpy() to simply use:

Quoted 5 lines
     strlcpy(buf, task->comm, size);

The min_t() seems unnecessary work to me, why do we still need it? size is guaranteed to be > 0 through the eBPF verifier, so strlcpy() should take care of the rest.

that's one clever optimization. yep. we can drop min_t. btw I wanted to add memset to __set_task_comm, keep memcpy in bpf_get_current_comm and optimize perf_event_comm_event (which doing: memset+strlcpy and can be replaced with memcpy), but figured that such 'fix' is not suitable for stable. I guess we can do in the next cycle? strlen is not cheap. Especially since it turned out that bpf_get_current_comm() is used very often in the hot path in bcc/tools.

Also for the next cycle I'm planning to extend verifier to allow uninitialized stack to be passed to functions like bpf_get_current_comm() and they would have to zero it in error cases. Then we can save few more cycles from the programs.

maintainer
#4 lore raw
Quoted 43 lines · Alexei Starovoitov
Quoted 36 lines · Daniel Borkmann
Quoted 7 lines · Alexei Starovoitov

Lots of places in the kernel use memcpy(buf, comm, TASK_COMM_LEN); but the result is typically passed to print("%s", buf) and extra bytes after zero don't cause any harm.
In bpf the result of bpf_get_current_comm() is used as the part of map key and was causing spurious hash map mismatches.
Use strlcpy() to guarantee zero-terminated string.
bpf verifier checks that output buffer is zero-initialized,

Sorry for late reply, more below:

Quoted 7 lines

so even for short task names the output buffer don't have junk bytes. Note it's not a security concern, since kprobe+bpf is root only.

Fixes ffeedafbf023 ("bpf: introduce current->pid, tgid, uid, gid,

comm accessors")

[...]

kernel/bpf/helpers.c static u64 bpf_get_current_comm(u64 r1, u64 size, · line 170

Quoted 11 lines
kernel/bpf/helpers.c +1 -1
index 4504ca66118d..50da680c479f 100644
@@ -166,7 +166,7 @@ static u64 bpf_get_current_comm(u64 r1, u64 size,
 u64 r3, u64 r4, u64 r5)
      if (!task)
          return -EINVAL;
 
-    memcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));
+    strlcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm)));

If I see this correctly, __set_task_comm() makes sure comm is always zero terminated, so that seems good, but isn't it already sufficient when switching
to strlcpy() to simply use:

Quoted 5 lines
     strlcpy(buf, task->comm, size);

The min_t() seems unnecessary work to me, why do we still need it? size is guaranteed to be > 0 through the eBPF verifier, so strlcpy() should take care of the rest.

Quoted 8 lines

that's one clever optimization. yep. we can drop min_t. btw I wanted to add memset to __set_task_comm, keep memcpy in bpf_get_current_comm and optimize perf_event_comm_event (which doing: memset+strlcpy and can be replaced with memcpy), but figured that such 'fix' is not suitable for stable. I guess we can do in the next cycle? strlen is not cheap. Especially since it turned out that bpf_get_current_comm() is used very often in the hot path in bcc/tools.

Would strscpy() help in this case (see 30035e45753b ("string: provide strscpy()"))?

Quoted 4 lines

Also for the next cycle I'm planning to extend verifier to allow uninitialized stack to be passed to functions like bpf_get_current_comm() and they would have to zero it in error cases. Then we can save few more cycles from the programs.

That would be useful also for other helpers indeed.

#5 lore raw
Quoted 2 lines · Daniel Borkmann

Would strscpy() help in this case (see 30035e45753b ("string: provide strscpy()"))?

I've looked at it too, but 990486c8af04 scared me a little, it's not easily backport-able and mainly I don't think it's faster than strlcpy for small strings like comm. memcpy going to be faster for sure.