Skip to contentSkip to conversation

seccomp: fix memory leak on filter attach

PATCH Superseded Kees Cook 4 participants 7 messages Apr 14, 2014

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 seccomp: fix memory leak on filter attach

This sets the correct error code when final filter memory is unavailable, and frees the raw filter no matter what.

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
---
 kernel/seccomp.c |    2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -0
index fd609bd9d6dd..2d7dcdfb7422 100644
@@ -256,6 +256,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
 		goto free_prog;
 
 	/* Allocate a new seccomp_filter */
+	ret = -ENOMEM;
 	filter = kzalloc(sizeof(struct seccomp_filter) +
 			 sizeof(struct sock_filter_int) * new_len,
 			 GFP_KERNEL|__GFP_NOWARN);
@@ -265,6 +266,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
 	ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
 	if (ret)
 		goto free_filter;
+	kfree(fp);
 
 	atomic_set(&filter->usage, 1);
 	filter->len = new_len;
Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
Acked-by on v1 1/1
#1 lore raw
Quoted 16 lines · Kees Cook

This sets the correct error code when final filter memory is unavailable, and frees the raw filter no matter what.

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 3 lines
Quoted 31 lines
---
 kernel/seccomp.c |    2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -1
index fd609bd9d6dd..2d7dcdfb7422 100644
@@ -256,6 +256,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
                goto free_prog;
 
        /* Allocate a new seccomp_filter */
+       ret = -ENOMEM;
        filter = kzalloc(sizeof(struct seccomp_filter) +
                         sizeof(struct sock_filter_int) * new_len,
                         GFP_KERNEL|__GFP_NOWARN);
@@ -265,6 +266,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
        ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
        if (ret)
                goto free_filter;
+       kfree(fp);
 
        atomic_set(&filter->usage, 1);
        filter->len = new_len;
--

1.7.9.5

--
Kees Cook
Chrome OS Security

author
#2 lore raw
Quoted 51 lines · Alexei Starovoitov
Quoted 16 lines · Kees Cook

This sets the correct error code when final filter memory is unavailable, and frees the raw filter no matter what.

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 3 lines
Quoted 28 lines
---
 kernel/seccomp.c |    2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -1
index fd609bd9d6dd..2d7dcdfb7422 100644
@@ -256,6 +256,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
                goto free_prog;
 
        /* Allocate a new seccomp_filter */
+       ret = -ENOMEM;
        filter = kzalloc(sizeof(struct seccomp_filter) +
                         sizeof(struct sock_filter_int) * new_len,
                         GFP_KERNEL|__GFP_NOWARN);
@@ -265,6 +266,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
        ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
        if (ret)
                goto free_filter;
+       kfree(fp);
 
        atomic_set(&filter->usage, 1);
        filter->len = new_len;
--

1.7.9.5

Quoted 3 lines
Quoted 3 lines

--
Kees Cook
Chrome OS Security

James (or anyone), can you be sure to pull this soon? I'd like to get it into 3.14 stable ASAP. Under kernel memory pressure, it would be possible for the prctl to return 0 without having attached the seccomp filter.

Thanks!

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#3 lore raw
Quoted 52 lines · Kees Cook
Quoted 51 lines · Alexei Starovoitov
Quoted 16 lines · Kees Cook

This sets the correct error code when final filter memory is unavailable, and frees the raw filter no matter what.

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 3 lines
Quoted 28 lines
---
 kernel/seccomp.c |    2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -1
index fd609bd9d6dd..2d7dcdfb7422 100644
@@ -256,6 +256,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
                goto free_prog;
 
        /* Allocate a new seccomp_filter */
+       ret = -ENOMEM;
        filter = kzalloc(sizeof(struct seccomp_filter) +
                         sizeof(struct sock_filter_int) * new_len,
                         GFP_KERNEL|__GFP_NOWARN);
@@ -265,6 +266,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
        ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
        if (ret)
                goto free_filter;
+       kfree(fp);
 
        atomic_set(&filter->usage, 1);
        filter->len = new_len;
--

1.7.9.5

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

--
Kees Cook
Chrome OS Security

James (or anyone), can you be sure to pull this soon? I'd like to get it into 3.14 stable ASAP. Under kernel memory pressure, it would be possible for the prctl to return 0 without having attached the seccomp filter.

I don't think it affects stable.

Fixes bd4cf0ed331a ("net: filter: rework/optimize internal BPF

interpreter's instruction set")

Quoted 7 lines

Thanks!

-Kees

--
Kees Cook
Chrome OS Security

author
#4 lore raw
Quoted 62 lines · Alexei Starovoitov
Quoted 52 lines · Kees Cook
Quoted 51 lines · Alexei Starovoitov
Quoted 16 lines · Kees Cook

This sets the correct error code when final filter memory is unavailable, and frees the raw filter no matter what.

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 3 lines
Quoted 28 lines
---
 kernel/seccomp.c |    2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -1
index fd609bd9d6dd..2d7dcdfb7422 100644
@@ -256,6 +256,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
                goto free_prog;
 
        /* Allocate a new seccomp_filter */
+       ret = -ENOMEM;
        filter = kzalloc(sizeof(struct seccomp_filter) +
                         sizeof(struct sock_filter_int) * new_len,
                         GFP_KERNEL|__GFP_NOWARN);
@@ -265,6 +266,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
        ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
        if (ret)
                goto free_filter;
+       kfree(fp);
 
        atomic_set(&filter->usage, 1);
        filter->len = new_len;
--

1.7.9.5

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

--
Kees Cook
Chrome OS Security

James (or anyone), can you be sure to pull this soon? I'd like to get it into 3.14 stable ASAP. Under kernel memory pressure, it would be possible for the prctl to return 0 without having attached the seccomp filter.

Quoted 3 lines

I don't think it affects stable.

Fixes bd4cf0ed331a ("net: filter: rework/optimize internal BPF

interpreter's instruction set")

Oh! You're completely right. I've been running -next too long. :) Okay, no urgency at all and the Cc can be dropped. Whew!

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
Acked-by on v1 1/1
#5 lore raw
In reply to Kees Cook
Quoted 16 lines · Kees Cook

This sets the correct error code when final filter memory is unavailable, and frees the raw filter no matter what.

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 3 lines
Tested-by on v1 1/1
#6 lore raw
In reply to Kees Cook
Quoted 16 lines · Kees Cook

This sets the correct error code when final filter memory is unavailable, and frees the raw filter no matter what.

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 3 lines
Quoted 31 lines
---
 kernel/seccomp.c |    2 ++
 1 file changed, 2 insertions(+)
kernel/seccomp.c +2 -1
index fd609bd9d6dd..2d7dcdfb7422 100644
@@ -256,6 +256,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
                goto free_prog;
 
        /* Allocate a new seccomp_filter */
+       ret = -ENOMEM;
        filter = kzalloc(sizeof(struct seccomp_filter) +
                         sizeof(struct sock_filter_int) * new_len,
                         GFP_KERNEL|__GFP_NOWARN);
@@ -265,6 +266,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
        ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
        if (ret)
                goto free_filter;
+       kfree(fp);
 
        atomic_set(&filter->usage, 1);
        filter->len = new_len;
--

1.7.9.5

--
Kees Cook
Chrome OS Security

Signature · 1 line
Masami Ichikawa