Skip to contentSkip to conversation

bpf: fix mem leak in error path of lwt bpf setup

PATCH In review Mathieu Xhonneux 3 participants 3 messages May 20, 2018 – May 21, 2018

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

PATCH 1/1 bpf: fix mem leak in error path of lwt bpf setup

In bpf_parse_prog, if bpf_prog_get_type fails, the function is immediately terminated without freeing the previously allocated prog->name.
This patch adds a kfree before the return.

---
 net/core/lwt_bpf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/core/lwt_bpf.c +3 -1
index e7e626fb87bb..e142a7a32e46 100644
@@ -223,8 +223,10 @@ static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
 
 	fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
 	p = bpf_prog_get_type(fd, type);
-	if (IS_ERR(p))
+	if (IS_ERR(p)) {
+		kfree(prog->name);
 		return PTR_ERR(p);
+	}
 
 	prog->prog = p;
 
Signature · 1 line
2.16.1
#1 lore raw
Quoted 15 lines · Mathieu Xhonneux

In bpf_parse_prog, if bpf_prog_get_type fails, the function is immediately terminated without freeing the previously allocated prog->name.
This patch adds a kfree before the return.

---
 net/core/lwt_bpf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/core/lwt_bpf.c +0 -0
index e7e626fb87bb..e142a7a32e46 100644

net/core/lwt_bpf.c static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog, · line 227

Quoted 6 lines
net/core/lwt_bpf.c +2 -1
@@ -223,8 +223,10 @@ static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
 
 	fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
 	p = bpf_prog_get_type(fd, type);
-	if (IS_ERR(p))
+	if (IS_ERR(p)) {
+		kfree(prog->name);

I don't think it is needed.
The caller, "bpf_build_state()", does bpf_destroy_state() during error out and it will eventually free up "name".

Quoted 8 lines
net/core/lwt_bpf.c +1 -0
 		return PTR_ERR(p);
+	}
 
 	prog->prog = p;
 
Signature · 1 line
2.16.1
#2 lore raw
Quoted 17 lines · Martin KaFai Lau
Quoted 16 lines · Mathieu Xhonneux

In bpf_parse_prog, if bpf_prog_get_type fails, the function is immediately terminated without freeing the previously allocated prog->name.
This patch adds a kfree before the return.

---
 net/core/lwt_bpf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/core/lwt_bpf.c +0 -0
index e7e626fb87bb..e142a7a32e46 100644
@@ -223,8 +223,10 @@ static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
 
Quoted 8 lines

net/core/lwt_bpf.c static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog, · line 227

Quoted 5 lines
net/core/lwt_bpf.c +2 -1
 	fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
 	p = bpf_prog_get_type(fd, type);
-	if (IS_ERR(p))
+	if (IS_ERR(p)) {
+		kfree(prog->name);

I don't think it is needed.
The caller, "bpf_build_state()", does bpf_destroy_state() during error out and it will eventually free up "name".

Agree, it's not needed in lwt/bpf due to call to destructor in error path.

Quoted 8 lines
Quoted 8 lines
net/core/lwt_bpf.c +1 -0
 		return PTR_ERR(p);
+	}
 
 	prog->prog = p;
 
Signature · 1 line
2.16.1