bpf: fix a buffer out-of-bound access when filling raw_tp link_info
Subsystems (4)
PATCH 1/1 bpf: fix a buffer out-of-bound access when filling raw_tp link_info
Commit f2e10bff16a0 ("bpf: Add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link") added link query for raw_tp. One of fields in link_info is to
fill a user buffer with tp_name. The Scurrent checking only declares "ulen && !ubuf" as invalid. So "!ulen && ubuf" will be valid. Later on, we do "copy_to_user(ubuf, tp_name, ulen - 1)" which may overwrite user memory incorrectly.
This patch fixed the problem by disallowing "!ulen && ubuf" case as well.
--- kernel/bpf/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +1 -1
@@ -2634,7 +2634,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,u32 ulen = info->raw_tracepoint.tp_name_len;size_t tp_len =strlen (tp_name); -if (ulen && !ubuf) +if (!ulen ^ !ubuf)return -EINVAL ; info->raw_tracepoint.tp_name_len = tp_len +1 ;
Signature · 1 line
2.24.1
Quoted 22 lines · Yonghong Song
Commit
f2e10bff16a0("bpf: Add support forBPF_OBJ_GET_INFO_BY_FDforbpf_link") added link query forraw_tp. One of fields inlink_infois to
fill a user buffer withtp_name. The Scurrent checking only declares "ulen && !ubuf" as invalid. So "!ulen && ubuf" will be valid. Later on, we do "copy_to_user(ubuf, tp_name, ulen - 1)" which may overwrite user memory incorrectly.This patch fixed the problem by disallowing "!ulen && ubuf" case as well.
Fixes f2e10bff16a0 ("bpf: Add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link")Cc Andrii Nakryiko <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, · line 2638
Quoted 6 lines
kernel/bpf/syscall.c +1
-1@@ -2634,7 +2634,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,u32 ulen = info->raw_tracepoint.tp_name_len;size_t tp_len =strlen (tp_name); -if (ulen && !ubuf) +if (!ulen ^ !ubuf)return -EINVAL ;
I think my original idea was to allow ulen == 0 && ubuf != NULL as a still valid way to get real ulen, but it's clearly wrong with ulen-1 below. So instead of special-casing ulen==0 for the case I wanted to support, it's easier to disallow ulen==0 && ubuf!=NULL.
So thanks for the fix!
Quoted 5 lines
kernel/bpf/syscall.c +0
-1info->raw_tracepoint.tp_name_len = tp_len +1 ; --2.24.1
Alexei Starovoitov
AppliedQuoted 36 lines · Andrii Nakryiko
Quoted 22 lines · Yonghong Song
Commit
f2e10bff16a0("bpf: Add support forBPF_OBJ_GET_INFO_BY_FDforbpf_link") added link query forraw_tp. One of fields inlink_infois to
fill a user buffer withtp_name. The Scurrent checking only declares "ulen && !ubuf" as invalid. So "!ulen && ubuf" will be valid. Later on, we do "copy_to_user(ubuf, tp_name, ulen - 1)" which may overwrite user memory incorrectly.This patch fixed the problem by disallowing "!ulen && ubuf" case as well.
Fixes f2e10bff16a0 ("bpf: Add support for BPF_OBJ_GET_INFO_BY_FD for bpf_link")Cc Andrii Nakryiko <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/syscall.c +0
-0kernel/bpf/syscall.c static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, · line 2638
Quoted 6 lines
kernel/bpf/syscall.c +1
-1@@ -2634,7 +2634,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,u32 ulen = info->raw_tracepoint.tp_name_len;size_t tp_len =strlen (tp_name); -if (ulen && !ubuf) +if (!ulen ^ !ubuf)return -EINVAL ;I think my original idea was to allow ulen == 0 && ubuf != NULL as a still valid way to get real ulen, but it's clearly wrong with ulen-1 below. So instead of special-casing ulen==0 for the case I wanted to support, it's easier to disallow ulen==0 && ubuf!=NULL.
Quoted 3 lines
So thanks for the fix!
Acked-by Andrii Nakryiko <[email protected]>
Applied. Thanks