Skip to content

Commit 4adb7a4

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
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]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5ca1ca0 commit 4adb7a4

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

kernel/bpf/syscall.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,8 +3628,10 @@ static int link_update(union bpf_attr *attr)
36283628
return PTR_ERR(link);
36293629

36303630
new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
3631-
if (IS_ERR(new_prog))
3632-
return PTR_ERR(new_prog);
3631+
if (IS_ERR(new_prog)) {
3632+
ret = PTR_ERR(new_prog);
3633+
goto out_put_link;
3634+
}
36333635

36343636
if (flags & BPF_F_REPLACE) {
36353637
old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
@@ -3638,6 +3640,9 @@ static int link_update(union bpf_attr *attr)
36383640
old_prog = NULL;
36393641
goto out_put_progs;
36403642
}
3643+
} else if (attr->link_update.old_prog_fd) {
3644+
ret = -EINVAL;
3645+
goto out_put_progs;
36413646
}
36423647

36433648
#ifdef CONFIG_CGROUP_BPF
@@ -3653,6 +3658,8 @@ static int link_update(union bpf_attr *attr)
36533658
bpf_prog_put(old_prog);
36543659
if (ret)
36553660
bpf_prog_put(new_prog);
3661+
out_put_link:
3662+
bpf_link_put(link);
36563663
return ret;
36573664
}
36583665

0 commit comments

Comments
 (0)