aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/bpf/helpers.c
diff options
context:
space:
mode:
authorDaniel Borkmann <daniel@iogearbox.net>2016-04-12 18:10:52 -0400
committerDavid S. Miller <davem@davemloft.net>2016-04-14 21:40:41 -0400
commit074f528eed408b467516e142fa4c45e5b0d2ba16 (patch)
treee42352604c4f0db159881faff4b7cef49393d878 /kernel/bpf/helpers.c
parent435faee1aae9c1ac231f89e4faf0437bfe29f425 (diff)
bpf: convert relevant helper args to ARG_PTR_TO_RAW_STACK
This patch converts all helpers that can use ARG_PTR_TO_RAW_STACK as argument type. For tc programs this is bpf_skb_load_bytes(), bpf_skb_get_tunnel_key(), bpf_skb_get_tunnel_opt(). For tracing, this optimizes bpf_get_current_comm() and bpf_probe_read(). The check in bpf_skb_load_bytes() for MAX_BPF_STACK can also be removed since the verifier already makes sure we stay within bounds on stack buffers. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'kernel/bpf/helpers.c')
-rw-r--r--kernel/bpf/helpers.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 50da680c479f..ad7a0573f71b 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -163,17 +163,26 @@ static u64 bpf_get_current_comm(u64 r1, u64 size, u64 r3, u64 r4, u64 r5)
163 struct task_struct *task = current; 163 struct task_struct *task = current;
164 char *buf = (char *) (long) r1; 164 char *buf = (char *) (long) r1;
165 165
166 if (!task) 166 if (unlikely(!task))
167 return -EINVAL; 167 goto err_clear;
168 168
169 strlcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm))); 169 strncpy(buf, task->comm, size);
170
171 /* Verifier guarantees that size > 0. For task->comm exceeding
172 * size, guarantee that buf is %NUL-terminated. Unconditionally
173 * done here to save the size test.
174 */
175 buf[size - 1] = 0;
170 return 0; 176 return 0;
177err_clear:
178 memset(buf, 0, size);
179 return -EINVAL;
171} 180}
172 181
173const struct bpf_func_proto bpf_get_current_comm_proto = { 182const struct bpf_func_proto bpf_get_current_comm_proto = {
174 .func = bpf_get_current_comm, 183 .func = bpf_get_current_comm,
175 .gpl_only = false, 184 .gpl_only = false,
176 .ret_type = RET_INTEGER, 185 .ret_type = RET_INTEGER,
177 .arg1_type = ARG_PTR_TO_STACK, 186 .arg1_type = ARG_PTR_TO_RAW_STACK,
178 .arg2_type = ARG_CONST_STACK_SIZE, 187 .arg2_type = ARG_CONST_STACK_SIZE,
179}; 188};