* [PATCH] netfilter: add overflow checks in xt_bpf.c
@ 2017-12-01 0:46 Jann Horn
2017-12-01 4:04 ` Willem de Bruijn
2017-12-04 10:36 ` Pablo Neira Ayuso
0 siblings, 2 replies; 5+ messages in thread
From: Jann Horn @ 2017-12-01 0:46 UTC (permalink / raw)
To: Willem de Bruijn, Pablo Neira Ayuso, Jozsef Kadlecsik,
Florian Westphal, David S. Miller
Cc: netdev, coreteam, netfilter-devel
Check whether inputs from userspace are too long (explicit length field too
big or string not null-terminated) to avoid out-of-bounds reads.
As far as I can tell, this can at worst lead to very limited kernel heap
memory disclosure or oopses.
This bug can be triggered by an unprivileged user even if the xt_bpf module
is not loaded: iptables is available in network namespaces, and the xt_bpf
module can be autoloaded.
Triggering the bug with a classic BPF filter with fake length 0x1000 causes
the following KASAN report:
==================================================================
BUG: KASAN: slab-out-of-bounds in bpf_prog_create+0x84/0xf0
Read of size 32768 at addr ffff8801eff2c494 by task test/4627
CPU: 0 PID: 4627 Comm: test Not tainted 4.15.0-rc1+ #1
[...]
Call Trace:
dump_stack+0x5c/0x85
print_address_description+0x6a/0x260
kasan_report+0x254/0x370
? bpf_prog_create+0x84/0xf0
memcpy+0x1f/0x50
bpf_prog_create+0x84/0xf0
bpf_mt_check+0x90/0xd6 [xt_bpf]
[...]
Allocated by task 4627:
kasan_kmalloc+0xa0/0xd0
__kmalloc_node+0x47/0x60
xt_alloc_table_info+0x41/0x70 [x_tables]
[...]
The buggy address belongs to the object at ffff8801eff2c3c0
which belongs to the cache kmalloc-2048 of size 2048
The buggy address is located 212 bytes inside of
2048-byte region [ffff8801eff2c3c0, ffff8801eff2cbc0)
[...]
==================================================================
Fixes: e6f30c731718 ("netfilter: x_tables: add xt_bpf match")
Signed-off-by: Jann Horn <jannh@google.com>
---
net/netfilter/xt_bpf.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/net/netfilter/xt_bpf.c b/net/netfilter/xt_bpf.c
index 041da0d9c06f..1f7fbd3c7e5a 100644
--- a/net/netfilter/xt_bpf.c
+++ b/net/netfilter/xt_bpf.c
@@ -27,6 +27,9 @@ static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
{
struct sock_fprog_kern program;
+ if (len > XT_BPF_MAX_NUM_INSTR)
+ return -EINVAL;
+
program.len = len;
program.filter = insns;
@@ -55,6 +58,9 @@ static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
mm_segment_t oldfs = get_fs();
int retval, fd;
+ if (strnlen(path, XT_BPF_PATH_MAX) == XT_BPF_PATH_MAX)
+ return -EINVAL;
+
set_fs(KERNEL_DS);
fd = bpf_obj_get_user(path, 0);
set_fs(oldfs);
--
2.15.0.531.g2ccb3012c9-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] netfilter: add overflow checks in xt_bpf.c
2017-12-01 0:46 [PATCH] netfilter: add overflow checks in xt_bpf.c Jann Horn
@ 2017-12-01 4:04 ` Willem de Bruijn
2017-12-01 4:08 ` Jann Horn
2017-12-04 10:36 ` Pablo Neira Ayuso
1 sibling, 1 reply; 5+ messages in thread
From: Willem de Bruijn @ 2017-12-01 4:04 UTC (permalink / raw)
To: Jann Horn
Cc: Willem de Bruijn, Pablo Neira Ayuso, Jozsef Kadlecsik,
Florian Westphal, David S. Miller, Network Development, coreteam,
netfilter-devel
On Thu, Nov 30, 2017 at 7:46 PM, Jann Horn <jannh@google.com> wrote:
> Check whether inputs from userspace are too long (explicit length field too
> big or string not null-terminated) to avoid out-of-bounds reads.
>
> As far as I can tell, this can at worst lead to very limited kernel heap
> memory disclosure or oopses.
>
> This bug can be triggered by an unprivileged user even if the xt_bpf module
> is not loaded: iptables is available in network namespaces, and the xt_bpf
> module can be autoloaded.
>
> Triggering the bug with a classic BPF filter with fake length 0x1000 causes
> the following KASAN report:
>
> ==================================================================
> BUG: KASAN: slab-out-of-bounds in bpf_prog_create+0x84/0xf0
> Read of size 32768 at addr ffff8801eff2c494 by task test/4627
>
> CPU: 0 PID: 4627 Comm: test Not tainted 4.15.0-rc1+ #1
> [...]
> Call Trace:
> dump_stack+0x5c/0x85
> print_address_description+0x6a/0x260
> kasan_report+0x254/0x370
> ? bpf_prog_create+0x84/0xf0
> memcpy+0x1f/0x50
> bpf_prog_create+0x84/0xf0
> bpf_mt_check+0x90/0xd6 [xt_bpf]
> [...]
> Allocated by task 4627:
> kasan_kmalloc+0xa0/0xd0
> __kmalloc_node+0x47/0x60
> xt_alloc_table_info+0x41/0x70 [x_tables]
> [...]
> The buggy address belongs to the object at ffff8801eff2c3c0
> which belongs to the cache kmalloc-2048 of size 2048
> The buggy address is located 212 bytes inside of
> 2048-byte region [ffff8801eff2c3c0, ffff8801eff2cbc0)
> [...]
> ==================================================================
>
> Fixes: e6f30c731718 ("netfilter: x_tables: add xt_bpf match")
> Signed-off-by: Jann Horn <jannh@google.com>
> ---
> net/netfilter/xt_bpf.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/net/netfilter/xt_bpf.c b/net/netfilter/xt_bpf.c
> index 041da0d9c06f..1f7fbd3c7e5a 100644
> --- a/net/netfilter/xt_bpf.c
> +++ b/net/netfilter/xt_bpf.c
> @@ -27,6 +27,9 @@ static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
> {
> struct sock_fprog_kern program;
>
> + if (len > XT_BPF_MAX_NUM_INSTR)
> + return -EINVAL;
> +
> program.len = len;
> program.filter = insns;
Next, this calls bpf_prog_create, which calls bpf_check_basics_ok to verify len.
> @@ -55,6 +58,9 @@ static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
> mm_segment_t oldfs = get_fs();
> int retval, fd;
>
> + if (strnlen(path, XT_BPF_PATH_MAX) == XT_BPF_PATH_MAX)
> + return -EINVAL;
> +
Good catch. It looks like this code needs a more thorough revision.
https://lkml.kernel.org/r/<20171201034859.GN21978@ZenIV.linux.org.uk>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] netfilter: add overflow checks in xt_bpf.c
2017-12-01 4:04 ` Willem de Bruijn
@ 2017-12-01 4:08 ` Jann Horn
2017-12-01 4:11 ` Willem de Bruijn
0 siblings, 1 reply; 5+ messages in thread
From: Jann Horn @ 2017-12-01 4:08 UTC (permalink / raw)
To: Willem de Bruijn
Cc: Willem de Bruijn, Pablo Neira Ayuso, Jozsef Kadlecsik,
Florian Westphal, David S. Miller, Network Development, coreteam,
netfilter-devel
On Fri, Dec 1, 2017 at 5:04 AM, Willem de Bruijn
<willemdebruijn.kernel@gmail.com> wrote:
> On Thu, Nov 30, 2017 at 7:46 PM, Jann Horn <jannh@google.com> wrote:
>> Check whether inputs from userspace are too long (explicit length field too
>> big or string not null-terminated) to avoid out-of-bounds reads.
>>
>> As far as I can tell, this can at worst lead to very limited kernel heap
>> memory disclosure or oopses.
>>
>> This bug can be triggered by an unprivileged user even if the xt_bpf module
>> is not loaded: iptables is available in network namespaces, and the xt_bpf
>> module can be autoloaded.
>>
>> Triggering the bug with a classic BPF filter with fake length 0x1000 causes
>> the following KASAN report:
>>
>> ==================================================================
>> BUG: KASAN: slab-out-of-bounds in bpf_prog_create+0x84/0xf0
>> Read of size 32768 at addr ffff8801eff2c494 by task test/4627
>>
>> CPU: 0 PID: 4627 Comm: test Not tainted 4.15.0-rc1+ #1
>> [...]
>> Call Trace:
>> dump_stack+0x5c/0x85
>> print_address_description+0x6a/0x260
>> kasan_report+0x254/0x370
>> ? bpf_prog_create+0x84/0xf0
>> memcpy+0x1f/0x50
>> bpf_prog_create+0x84/0xf0
>> bpf_mt_check+0x90/0xd6 [xt_bpf]
>> [...]
>> Allocated by task 4627:
>> kasan_kmalloc+0xa0/0xd0
>> __kmalloc_node+0x47/0x60
>> xt_alloc_table_info+0x41/0x70 [x_tables]
>> [...]
>> The buggy address belongs to the object at ffff8801eff2c3c0
>> which belongs to the cache kmalloc-2048 of size 2048
>> The buggy address is located 212 bytes inside of
>> 2048-byte region [ffff8801eff2c3c0, ffff8801eff2cbc0)
>> [...]
>> ==================================================================
>>
>> Fixes: e6f30c731718 ("netfilter: x_tables: add xt_bpf match")
>> Signed-off-by: Jann Horn <jannh@google.com>
>> ---
>> net/netfilter/xt_bpf.c | 6 ++++++
>> 1 file changed, 6 insertions(+)
>>
>> diff --git a/net/netfilter/xt_bpf.c b/net/netfilter/xt_bpf.c
>> index 041da0d9c06f..1f7fbd3c7e5a 100644
>> --- a/net/netfilter/xt_bpf.c
>> +++ b/net/netfilter/xt_bpf.c
>> @@ -27,6 +27,9 @@ static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
>> {
>> struct sock_fprog_kern program;
>>
>> + if (len > XT_BPF_MAX_NUM_INSTR)
>> + return -EINVAL;
>> +
>> program.len = len;
>> program.filter = insns;
>
> Next, this calls bpf_prog_create, which calls bpf_check_basics_ok to verify len.
Irrelevant:
- see the KASAN splat in the commit message
- bpf_check_basics_ok checks against BPF_MAXINSNS (4096), but a check against
XT_BPF_MAX_NUM_INSTR (64) is needed because that's the size of the
member in the
input struct
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] netfilter: add overflow checks in xt_bpf.c
2017-12-01 4:08 ` Jann Horn
@ 2017-12-01 4:11 ` Willem de Bruijn
0 siblings, 0 replies; 5+ messages in thread
From: Willem de Bruijn @ 2017-12-01 4:11 UTC (permalink / raw)
To: Jann Horn
Cc: Willem de Bruijn, Pablo Neira Ayuso, Jozsef Kadlecsik,
Florian Westphal, David S. Miller, Network Development, coreteam,
netfilter-devel
On Thu, Nov 30, 2017 at 11:08 PM, Jann Horn <jannh@google.com> wrote:
> On Fri, Dec 1, 2017 at 5:04 AM, Willem de Bruijn
> <willemdebruijn.kernel@gmail.com> wrote:
>> On Thu, Nov 30, 2017 at 7:46 PM, Jann Horn <jannh@google.com> wrote:
>>> Check whether inputs from userspace are too long (explicit length field too
>>> big or string not null-terminated) to avoid out-of-bounds reads.
>>>
>>> As far as I can tell, this can at worst lead to very limited kernel heap
>>> memory disclosure or oopses.
>>>
>>> This bug can be triggered by an unprivileged user even if the xt_bpf module
>>> is not loaded: iptables is available in network namespaces, and the xt_bpf
>>> module can be autoloaded.
>>>
>>> Triggering the bug with a classic BPF filter with fake length 0x1000 causes
>>> the following KASAN report:
>>>
>>> ==================================================================
>>> BUG: KASAN: slab-out-of-bounds in bpf_prog_create+0x84/0xf0
>>> Read of size 32768 at addr ffff8801eff2c494 by task test/4627
>>>
>>> CPU: 0 PID: 4627 Comm: test Not tainted 4.15.0-rc1+ #1
>>> [...]
>>> Call Trace:
>>> dump_stack+0x5c/0x85
>>> print_address_description+0x6a/0x260
>>> kasan_report+0x254/0x370
>>> ? bpf_prog_create+0x84/0xf0
>>> memcpy+0x1f/0x50
>>> bpf_prog_create+0x84/0xf0
>>> bpf_mt_check+0x90/0xd6 [xt_bpf]
>>> [...]
>>> Allocated by task 4627:
>>> kasan_kmalloc+0xa0/0xd0
>>> __kmalloc_node+0x47/0x60
>>> xt_alloc_table_info+0x41/0x70 [x_tables]
>>> [...]
>>> The buggy address belongs to the object at ffff8801eff2c3c0
>>> which belongs to the cache kmalloc-2048 of size 2048
>>> The buggy address is located 212 bytes inside of
>>> 2048-byte region [ffff8801eff2c3c0, ffff8801eff2cbc0)
>>> [...]
>>> ==================================================================
>>>
>>> Fixes: e6f30c731718 ("netfilter: x_tables: add xt_bpf match")
>>> Signed-off-by: Jann Horn <jannh@google.com>
>>> ---
>>> net/netfilter/xt_bpf.c | 6 ++++++
>>> 1 file changed, 6 insertions(+)
>>>
>>> diff --git a/net/netfilter/xt_bpf.c b/net/netfilter/xt_bpf.c
>>> index 041da0d9c06f..1f7fbd3c7e5a 100644
>>> --- a/net/netfilter/xt_bpf.c
>>> +++ b/net/netfilter/xt_bpf.c
>>> @@ -27,6 +27,9 @@ static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
>>> {
>>> struct sock_fprog_kern program;
>>>
>>> + if (len > XT_BPF_MAX_NUM_INSTR)
>>> + return -EINVAL;
>>> +
>>> program.len = len;
>>> program.filter = insns;
>>
>> Next, this calls bpf_prog_create, which calls bpf_check_basics_ok to verify len.
>
> Irrelevant:
>
> - see the KASAN splat in the commit message
> - bpf_check_basics_ok checks against BPF_MAXINSNS (4096), but a check against
> XT_BPF_MAX_NUM_INSTR (64) is needed because that's the size of the
> member in the
> input struct
Argh, of course. Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] netfilter: add overflow checks in xt_bpf.c
2017-12-01 0:46 [PATCH] netfilter: add overflow checks in xt_bpf.c Jann Horn
2017-12-01 4:04 ` Willem de Bruijn
@ 2017-12-04 10:36 ` Pablo Neira Ayuso
1 sibling, 0 replies; 5+ messages in thread
From: Pablo Neira Ayuso @ 2017-12-04 10:36 UTC (permalink / raw)
To: Jann Horn
Cc: Willem de Bruijn, Jozsef Kadlecsik, Florian Westphal,
David S. Miller, netdev, coreteam, netfilter-devel
On Fri, Dec 01, 2017 at 01:46:07AM +0100, Jann Horn wrote:
> Check whether inputs from userspace are too long (explicit length field too
> big or string not null-terminated) to avoid out-of-bounds reads.
>
> As far as I can tell, this can at worst lead to very limited kernel heap
> memory disclosure or oopses.
>
> This bug can be triggered by an unprivileged user even if the xt_bpf module
> is not loaded: iptables is available in network namespaces, and the xt_bpf
> module can be autoloaded.
>
> Triggering the bug with a classic BPF filter with fake length 0x1000 causes
> the following KASAN report:
>
> ==================================================================
> BUG: KASAN: slab-out-of-bounds in bpf_prog_create+0x84/0xf0
> Read of size 32768 at addr ffff8801eff2c494 by task test/4627
>
> CPU: 0 PID: 4627 Comm: test Not tainted 4.15.0-rc1+ #1
> [...]
> Call Trace:
> dump_stack+0x5c/0x85
> print_address_description+0x6a/0x260
> kasan_report+0x254/0x370
> ? bpf_prog_create+0x84/0xf0
> memcpy+0x1f/0x50
> bpf_prog_create+0x84/0xf0
> bpf_mt_check+0x90/0xd6 [xt_bpf]
> [...]
> Allocated by task 4627:
> kasan_kmalloc+0xa0/0xd0
> __kmalloc_node+0x47/0x60
> xt_alloc_table_info+0x41/0x70 [x_tables]
> [...]
> The buggy address belongs to the object at ffff8801eff2c3c0
> which belongs to the cache kmalloc-2048 of size 2048
> The buggy address is located 212 bytes inside of
> 2048-byte region [ffff8801eff2c3c0, ffff8801eff2cbc0)
> [...]
> ==================================================================
Applied, thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2017-12-04 10:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-01 0:46 [PATCH] netfilter: add overflow checks in xt_bpf.c Jann Horn
2017-12-01 4:04 ` Willem de Bruijn
2017-12-01 4:08 ` Jann Horn
2017-12-01 4:11 ` Willem de Bruijn
2017-12-04 10:36 ` Pablo Neira Ayuso
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).