bpf: fix leak in LINK_UPDATE and enforce empty old_prog_fd
Subsystems (4)
PATCH v2 1/1 bpf: fix leak in LINK_UPDATE and enforce empty old_prog_fd
Fix bug of not putting bpf_link in LINK_UPDATE command. Also enforce zeroed old_prog_fd if no BPF_F_REPLACE flag is specified.
Signed-off-by Andrii Nakryiko <[email protected]>
---
This version will merge with no conflicts with the upcoming LINK_UPDATE refactoring patch (part of bpf_link observability patch set).
kernel/bpf/syscall.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/bpf/syscall.c +9 -2
@@ -3628,8 +3628,10 @@ static int link_update(union bpf_attr *attr)return PTR_ERR (link); new_prog =bpf_prog_get (attr->link_update.new_prog_fd); -if (IS_ERR (new_prog)) -return PTR_ERR (new_prog); +if (IS_ERR (new_prog)) { + ret =PTR_ERR (new_prog); + goto out_put_link; + }if (flags &BPF_F_REPLACE ) { old_prog =bpf_prog_get (attr->link_update.old_prog_fd);
@@ -3638,6 +3640,9 @@ static int link_update(union bpf_attr *attr) old_prog =NULL ; goto out_put_progs; } + }else if (attr->link_update.old_prog_fd) { + ret = -EINVAL ; + goto out_put_progs; }#ifdef CONFIG_CGROUP_BPF
@@ -3653,6 +3658,8 @@ static int link_update(union bpf_attr *attr)bpf_prog_put (old_prog);if (ret)bpf_prog_put (new_prog); +out_put_link: +bpf_link_put (link);return ret; }
Signature · 1 line
2.24.1
Alexei Starovoitov
AppliedQuoted 5 lines · Andrii Nakryiko
Fix bug of not putting
bpf_linkinLINK_UPDATEcommand. Also enforce zeroedold_prog_fdif noBPF_F_REPLACEflag is specified.Signed-off-by Andrii Nakryiko <[email protected]>
Applied. Thanks
Quoted 9 lines · Alexei Starovoitov
Quoted 5 lines · Andrii Nakryiko
Fix bug of not putting
bpf_linkinLINK_UPDATEcommand. Also enforce zeroedold_prog_fdif noBPF_F_REPLACEflag is specified.Signed-off-by Andrii Nakryiko <[email protected]>Applied. Thanks
Oops, forgot to include Fixes tag :( If it's not too late, can you please add?
Fixes 0c991ebc8c69 ("bpf: Implement bpf_prog replacement for an
active bpf_cgroup_link")
Thank you!