seccomp: Release fp pointer when leaving from seccomp_attach_filter().
Subsystems (3)
PATCH 1/1 seccomp: Release fp pointer when leaving from seccomp_attach_filter().
kmemleak reported some memory leak as below.
unreferenced object 0xffff8800d6ea4000 (size 512):
comm "sshd", pid 278, jiffies 4294898315 (age 46.653s) hex dump (first 32 bytes):
21 00 00 00 04 00 00 00 15 00 01 00 3e 00 00 c0 !...........>...
06 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00 ........!.......
backtrace:
[<ffffffff8151414e>] kmemleak_alloc+0x4e/0xb0
[<ffffffff811a3a40>] __kmalloc+0x280/0x320
[<ffffffff8110842e>] prctl_set_seccomp+0x11e/0x3b0
[<ffffffff8107bb6b>] SyS_prctl+0x3bb/0x4a0
[<ffffffff8152ef2d>] system_call_fastpath+0x1a/0x1f
[<ffffffffffffffff>] 0xffffffffffffffffThis memory leak happend in seccomp_attach_filter().
The fp pointer was allocated via kzalloc so that it needs to realase memory when leaving from function.
This patch changed two things.
One is set -ENOMEM to ret, if fp is unable to get memory. The other is removes "return 0" statement, and frees fp pointer before leaving.
--- kernel/seccomp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
kernel/seccomp.c +5 -3
@@ -259,8 +259,10 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) filter =kzalloc (sizeof (struct seccomp_filter ) +sizeof (struct sock_filter_int ) * new_len,GFP_KERNEL |__GFP_NOWARN); -if (!filter) +if (!filter) { + ret = -ENOMEM ; goto free_prog; + } ret =sk_convert_filter (fp, fprog->len, filter->insnsi, &new_len);if (ret)
@@ -275,10 +277,10 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)*/ filter->prev = current->seccomp.filter; current->seccomp.filter = filter; -return 0 ; free_filter: -kfree (filter); +if (ret) +kfree (filter); free_prog:kfree (fp);return ret;
Signature · 1 line
1.9.1
Quoted 1 line · Masami Ichikawa
kmemleak reported some memory leak as below.
grrr. yes. sorry.
Quoted 32 lines
unreferenced object
0xffff8800d6ea4000(size 512):
comm "sshd", pid 278, jiffies 4294898315 (age 46.653s) hex dump (first 32 bytes):21 00 00 00 04 00 00 00 15 00 01 00 3e 00 00 c0 !...........>... 06 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00 ........!....... backtrace: [<ffffffff8151414e>] kmemleak_alloc+0x4e/0xb0 [<ffffffff811a3a40>] __kmalloc+0x280/0x320 [<ffffffff8110842e>] prctl_set_seccomp+0x11e/0x3b0 [<ffffffff8107bb6b>] SyS_prctl+0x3bb/0x4a0 [<ffffffff8152ef2d>] system_call_fastpath+0x1a/0x1f [<ffffffffffffffff>] 0xffffffffffffffffThis memory leak happend in
seccomp_attach_filter().
The fp pointer was allocated via kzalloc so that it needs to realase memory when leaving from function.This patch changed two things.
One is set-ENOMEMto ret, if fp is unable to get memory. The other is removes "return 0" statement, and frees fp pointer before leaving.Signed-off-by Masami Ichikawa <[email protected]>--- kernel/seccomp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)kernel/seccomp.c +0
-0
kernel/seccomp.c static long seccomp_attach_filter(struct sock_fprog *fprog) · line 265
Quoted 8 lines
kernel/seccomp.c +3
-1@@ -259,8 +259,10 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) filter =kzalloc (sizeof (struct seccomp_filter ) +sizeof (struct sock_filter_int ) * new_len,GFP_KERNEL |__GFP_NOWARN); -if (!filter) +if (!filter) { + ret = -ENOMEM ; goto free_prog; + }
agree. that's a good addition.
kernel/seccomp.c static long seccomp_attach_filter(struct sock_fprog *fprog) · line 278
Quoted 7 lines
kernel/seccomp.c +0
-1ret =sk_convert_filter (fp, fprog->len, filter->insnsi, &new_len);if (ret)@@ -275,10 +277,10 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)*/ filter->prev = current->seccomp.filter; current->seccomp.filter = filter; -return 0 ;
I think mixing error and ok return paths is ugly. Can you add kfree(fp) here instead of removing return 0?
Thanks!
Quoted 10 lines
kernel/seccomp.c +2
-2free_filter: -kfree (filter); +if (ret) +kfree (filter); free_prog:kfree (fp);return ret; --1.9.1
Quoted 21 lines · Alexei Starovoitov
Quoted 1 line · Masami Ichikawa
kmemleak reported some memory leak as below.
grrr. yes. sorry.
Quoted 16 lines
unreferenced object
0xffff8800d6ea4000(size 512):
comm "sshd", pid 278, jiffies 4294898315 (age 46.653s) hex dump (first 32 bytes):21 00 00 00 04 00 00 00 15 00 01 00 3e 00 00 c0 !...........>... 06 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00 ........!....... backtrace: [<ffffffff8151414e>] kmemleak_alloc+0x4e/0xb0 [<ffffffff811a3a40>] __kmalloc+0x280/0x320 [<ffffffff8110842e>] prctl_set_seccomp+0x11e/0x3b0 [<ffffffff8107bb6b>] SyS_prctl+0x3bb/0x4a0 [<ffffffff8152ef2d>] system_call_fastpath+0x1a/0x1f [<ffffffffffffffff>] 0xffffffffffffffffThis memory leak happend in
seccomp_attach_filter().
The fp pointer was allocated via kzalloc so that it needs to realase memory when leaving from function.
Thanks for the catch!
Quoted 37 lines
Quoted 15 lines
This patch changed two things.
One is set-ENOMEMto ret, if fp is unable to get memory. The other is removes "return 0" statement, and frees fp pointer before leaving.Signed-off-by Masami Ichikawa <[email protected]>--- kernel/seccomp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)kernel/seccomp.c +0
-0kernel/seccomp.c static long seccomp_attach_filter(struct sock_fprog *fprog) · line 265
Quoted 8 lines
kernel/seccomp.c +3
-1@@ -259,8 +259,10 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) filter =kzalloc (sizeof (struct seccomp_filter ) +sizeof (struct sock_filter_int ) * new_len,GFP_KERNEL |__GFP_NOWARN); -if (!filter) +if (!filter) { + ret = -ENOMEM ; goto free_prog; + }agree. that's a good addition.
kernel/seccomp.c static long seccomp_attach_filter(struct sock_fprog *fprog) · line 278
Quoted 7 lines
kernel/seccomp.c +0
-1ret =sk_convert_filter (fp, fprog->len, filter->insnsi, &new_len);if (ret)@@ -275,10 +277,10 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)*/ filter->prev = current->seccomp.filter; current->seccomp.filter = filter; -return 0 ;I think mixing error and ok return paths is ugly. Can you add kfree(fp) here instead of removing return 0?
Quoted 12 lines
Thanks!
Quoted 10 lines
kernel/seccomp.c +2
-2free_filter: -kfree (filter); +if (ret) +kfree (filter); free_prog:kfree (fp);return ret; --1.9.1
Yeah, I'd prefer a different approach that follows the existing conventions in the code. I'll send a separate patch.
-Kees
Signature · 2 lines
Kees Cook Chrome OS Security
Quoted 19 lines · Kees Cook
Quoted 18 lines · Alexei Starovoitov
Quoted 1 line · Masami Ichikawa
kmemleak reported some memory leak as below.
grrr. yes. sorry.
Quoted 13 lines
unreferenced object
0xffff8800d6ea4000(size 512):
comm "sshd", pid 278, jiffies 4294898315 (age 46.653s) hex dump (first 32 bytes):21 00 00 00 04 00 00 00 15 00 01 00 3e 00 00 c0 !...........>... 06 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00 ........!....... backtrace: [<ffffffff8151414e>] kmemleak_alloc+0x4e/0xb0 [<ffffffff811a3a40>] __kmalloc+0x280/0x320 [<ffffffff8110842e>] prctl_set_seccomp+0x11e/0x3b0 [<ffffffff8107bb6b>] SyS_prctl+0x3bb/0x4a0 [<ffffffff8152ef2d>] system_call_fastpath+0x1a/0x1f [<ffffffffffffffff>] 0xffffffffffffffff
Quoted 41 lines
Quoted 3 lines
Quoted 3 lines
This memory leak happend in
seccomp_attach_filter().
The fp pointer was allocated via kzalloc so that it needs to realase memory when leaving from function.Thanks for the catch!
Quoted 35 lines
Quoted 15 lines
This patch changed two things.
One is set-ENOMEMto ret, if fp is unable to get memory. The other is removes "return 0" statement, and frees fp pointer before leaving.Signed-off-by Masami Ichikawa <[email protected]>--- kernel/seccomp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)kernel/seccomp.c +0
-0kernel/seccomp.c static long seccomp_attach_filter(struct sock_fprog *fprog) · line 265
Quoted 8 lines
kernel/seccomp.c +3
-1@@ -259,8 +259,10 @@ static long seccomp_attach_filter(struct sock_fprog *fprog) filter =kzalloc (sizeof (struct seccomp_filter ) +sizeof (struct sock_filter_int ) * new_len,GFP_KERNEL |__GFP_NOWARN); -if (!filter) +if (!filter) { + ret = -ENOMEM ; goto free_prog; + }agree. that's a good addition.
kernel/seccomp.c static long seccomp_attach_filter(struct sock_fprog *fprog) · line 278
Quoted 7 lines
kernel/seccomp.c +0
-1ret =sk_convert_filter (fp, fprog->len, filter->insnsi, &new_len);if (ret)@@ -275,10 +277,10 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)*/ filter->prev = current->seccomp.filter; current->seccomp.filter = filter; -return 0 ;I think mixing error and ok return paths is ugly.
I troubled that have two kfree(fp) path or merge error and success path. But separate paths is easy to read.
Quoted 15 lines
Quoted 14 lines
Can you add kfree(fp) here instead of removing return 0?
Thanks!
Quoted 10 lines
kernel/seccomp.c +2
-2free_filter: -kfree (filter); +if (ret) +kfree (filter); free_prog:kfree (fp);return ret; --1.9.1
Quoted 2 lines
Yeah, I'd prefer a different approach that follows the existing conventions in the code. I'll send a separate patch.
I see.
Thank you for considering it.
Quoted 5 lines
-Kees
--
Kees Cook
Chrome OS Security
Cheeers,
Signature · 1 line
Masami Ichikawa