mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-04 09:59:32 +00:00
![Subash Abhinov Kasiviswanathan](/assets/img/avatar_default.png)
An out of bounds error was detected on an ARM64 target with
Android based kernel 4.9. This occurs while trying to
restore mark on a skb from an inet request socket.
BUG: KASAN: slab-out-of-bounds in socket_match.isra.2+0xc8/0x1f0 net/netfilter/xt_socket.c:248
Read of size 4 at addr ffffffc06a8d824c by task syz-fuzzer/1532
CPU: 7 PID: 1532 Comm: syz-fuzzer Tainted: G W O 4.9.41+ #1
Call trace:
[<ffffff900808d2f8>] dump_backtrace+0x0/0x440 arch/arm64/kernel/traps.c:76
[<ffffff900808d760>] show_stack+0x28/0x38 arch/arm64/kernel/traps.c:226
[<ffffff90085f7dc8>] __dump_stack lib/dump_stack.c:15 [inline]
[<ffffff90085f7dc8>] dump_stack+0xe4/0x134 lib/dump_stack.c:51
[<ffffff900830f358>] print_address_description+0x68/0x258 mm/kasan/report.c:248
[<ffffff900830f770>] kasan_report_error mm/kasan/report.c:347 [inline]
[<ffffff900830f770>] kasan_report.part.2+0x228/0x2f0 mm/kasan/report.c:371
[<ffffff900830fdec>] kasan_report+0x5c/0x70 mm/kasan/report.c:372
[<ffffff900830de98>] check_memory_region_inline mm/kasan/kasan.c:308 [inline]
[<ffffff900830de98>] __asan_load4+0x88/0xa0 mm/kasan/kasan.c:740
[<ffffff90097498f8>] socket_match.isra.2+0xc8/0x1f0 net/netfilter/xt_socket.c:248
[<ffffff9009749a5c>] socket_mt4_v1_v2_v3+0x3c/0x48 net/netfilter/xt_socket.c:272
[<ffffff90097f7e4c>] ipt_do_table+0x54c/0xad8 net/ipv4/netfilter/ip_tables.c:311
[<ffffff90097fcf14>] iptable_mangle_hook+0x6c/0x220 net/ipv4/netfilter/iptable_mangle.c:90
...
Allocated by task 1532:
save_stack_trace_tsk+0x0/0x2a0 arch/arm64/kernel/stacktrace.c:131
save_stack_trace+0x28/0x38 arch/arm64/kernel/stacktrace.c:215
save_stack mm/kasan/kasan.c:495 [inline]
set_track mm/kasan/kasan.c:507 [inline]
kasan_kmalloc+0xd8/0x188 mm/kasan/kasan.c:599
kasan_slab_alloc+0x14/0x20 mm/kasan/kasan.c:537
slab_post_alloc_hook mm/slab.h:417 [inline]
slab_alloc_node mm/slub.c:2728 [inline]
slab_alloc mm/slub.c:2736 [inline]
kmem_cache_alloc+0x14c/0x2e8 mm/slub.c:2741
reqsk_alloc include/net/request_sock.h:87 [inline]
inet_reqsk_alloc+0x4c/0x238 net/ipv4/tcp_input.c:6236
tcp_conn_request+0x2b0/0xea8 net/ipv4/tcp_input.c:6341
tcp_v4_conn_request+0xe0/0x100 net/ipv4/tcp_ipv4.c:1256
tcp_rcv_state_process+0x384/0x18a8 net/ipv4/tcp_input.c:5926
tcp_v4_do_rcv+0x2f0/0x3e0 net/ipv4/tcp_ipv4.c:1430
tcp_v4_rcv+0x1278/0x1350 net/ipv4/tcp_ipv4.c:1709
ip_local_deliver_finish+0x174/0x3e0 net/ipv4/ip_input.c:216
v1->v2: Change socket_mt6_v1_v2_v3() as well as mentioned by Eric
v2->v3: Put the correct fixes tag
Fixes: 01555e74bd
("netfilter: xt_socket: add XT_SOCKET_RESTORESKMARK flag")
Signed-off-by: Subash Abhinov Kasiviswanathan <subashab@codeaurora.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
314 lines
8 KiB
C
314 lines
8 KiB
C
/*
|
|
* Transparent proxy support for Linux/iptables
|
|
*
|
|
* Copyright (C) 2007-2008 BalaBit IT Ltd.
|
|
* Author: Krisztian Kovacs
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
*/
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
#include <linux/module.h>
|
|
#include <linux/skbuff.h>
|
|
#include <linux/netfilter/x_tables.h>
|
|
#include <linux/netfilter_ipv4/ip_tables.h>
|
|
#include <net/tcp.h>
|
|
#include <net/udp.h>
|
|
#include <net/icmp.h>
|
|
#include <net/sock.h>
|
|
#include <net/inet_sock.h>
|
|
#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
|
|
|
|
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
|
#include <linux/netfilter_ipv6/ip6_tables.h>
|
|
#include <net/inet6_hashtables.h>
|
|
#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
|
|
#endif
|
|
|
|
#include <net/netfilter/nf_socket.h>
|
|
#include <linux/netfilter/xt_socket.h>
|
|
|
|
/* "socket" match based redirection (no specific rule)
|
|
* ===================================================
|
|
*
|
|
* There are connections with dynamic endpoints (e.g. FTP data
|
|
* connection) that the user is unable to add explicit rules
|
|
* for. These are taken care of by a generic "socket" rule. It is
|
|
* assumed that the proxy application is trusted to open such
|
|
* connections without explicit iptables rule (except of course the
|
|
* generic 'socket' rule). In this case the following sockets are
|
|
* matched in preference order:
|
|
*
|
|
* - match: if there's a fully established connection matching the
|
|
* _packet_ tuple
|
|
*
|
|
* - match: if there's a non-zero bound listener (possibly with a
|
|
* non-local address) We don't accept zero-bound listeners, since
|
|
* then local services could intercept traffic going through the
|
|
* box.
|
|
*/
|
|
static bool
|
|
socket_match(const struct sk_buff *skb, struct xt_action_param *par,
|
|
const struct xt_socket_mtinfo1 *info)
|
|
{
|
|
struct sk_buff *pskb = (struct sk_buff *)skb;
|
|
struct sock *sk = skb->sk;
|
|
|
|
if (!sk)
|
|
sk = nf_sk_lookup_slow_v4(xt_net(par), skb, xt_in(par));
|
|
if (sk) {
|
|
bool wildcard;
|
|
bool transparent = true;
|
|
|
|
/* Ignore sockets listening on INADDR_ANY,
|
|
* unless XT_SOCKET_NOWILDCARD is set
|
|
*/
|
|
wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
|
|
sk_fullsock(sk) &&
|
|
inet_sk(sk)->inet_rcv_saddr == 0);
|
|
|
|
/* Ignore non-transparent sockets,
|
|
* if XT_SOCKET_TRANSPARENT is used
|
|
*/
|
|
if (info->flags & XT_SOCKET_TRANSPARENT)
|
|
transparent = nf_sk_is_transparent(sk);
|
|
|
|
if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
|
|
transparent && sk_fullsock(sk))
|
|
pskb->mark = sk->sk_mark;
|
|
|
|
if (sk != skb->sk)
|
|
sock_gen_put(sk);
|
|
|
|
if (wildcard || !transparent)
|
|
sk = NULL;
|
|
}
|
|
|
|
return sk != NULL;
|
|
}
|
|
|
|
static bool
|
|
socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
|
|
{
|
|
static struct xt_socket_mtinfo1 xt_info_v0 = {
|
|
.flags = 0,
|
|
};
|
|
|
|
return socket_match(skb, par, &xt_info_v0);
|
|
}
|
|
|
|
static bool
|
|
socket_mt4_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
|
|
{
|
|
return socket_match(skb, par, par->matchinfo);
|
|
}
|
|
|
|
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
|
static bool
|
|
socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
|
|
{
|
|
const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
|
|
struct sk_buff *pskb = (struct sk_buff *)skb;
|
|
struct sock *sk = skb->sk;
|
|
|
|
if (!sk)
|
|
sk = nf_sk_lookup_slow_v6(xt_net(par), skb, xt_in(par));
|
|
if (sk) {
|
|
bool wildcard;
|
|
bool transparent = true;
|
|
|
|
/* Ignore sockets listening on INADDR_ANY
|
|
* unless XT_SOCKET_NOWILDCARD is set
|
|
*/
|
|
wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
|
|
sk_fullsock(sk) &&
|
|
ipv6_addr_any(&sk->sk_v6_rcv_saddr));
|
|
|
|
/* Ignore non-transparent sockets,
|
|
* if XT_SOCKET_TRANSPARENT is used
|
|
*/
|
|
if (info->flags & XT_SOCKET_TRANSPARENT)
|
|
transparent = nf_sk_is_transparent(sk);
|
|
|
|
if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
|
|
transparent && sk_fullsock(sk))
|
|
pskb->mark = sk->sk_mark;
|
|
|
|
if (sk != skb->sk)
|
|
sock_gen_put(sk);
|
|
|
|
if (wildcard || !transparent)
|
|
sk = NULL;
|
|
}
|
|
|
|
return sk != NULL;
|
|
}
|
|
#endif
|
|
|
|
static int socket_mt_enable_defrag(struct net *net, int family)
|
|
{
|
|
switch (family) {
|
|
case NFPROTO_IPV4:
|
|
return nf_defrag_ipv4_enable(net);
|
|
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
|
case NFPROTO_IPV6:
|
|
return nf_defrag_ipv6_enable(net);
|
|
#endif
|
|
}
|
|
WARN_ONCE(1, "Unknown family %d\n", family);
|
|
return 0;
|
|
}
|
|
|
|
static int socket_mt_v1_check(const struct xt_mtchk_param *par)
|
|
{
|
|
const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
|
|
int err;
|
|
|
|
err = socket_mt_enable_defrag(par->net, par->family);
|
|
if (err)
|
|
return err;
|
|
|
|
if (info->flags & ~XT_SOCKET_FLAGS_V1) {
|
|
pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V1);
|
|
return -EINVAL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int socket_mt_v2_check(const struct xt_mtchk_param *par)
|
|
{
|
|
const struct xt_socket_mtinfo2 *info = (struct xt_socket_mtinfo2 *) par->matchinfo;
|
|
int err;
|
|
|
|
err = socket_mt_enable_defrag(par->net, par->family);
|
|
if (err)
|
|
return err;
|
|
|
|
if (info->flags & ~XT_SOCKET_FLAGS_V2) {
|
|
pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V2);
|
|
return -EINVAL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int socket_mt_v3_check(const struct xt_mtchk_param *par)
|
|
{
|
|
const struct xt_socket_mtinfo3 *info =
|
|
(struct xt_socket_mtinfo3 *)par->matchinfo;
|
|
int err;
|
|
|
|
err = socket_mt_enable_defrag(par->net, par->family);
|
|
if (err)
|
|
return err;
|
|
if (info->flags & ~XT_SOCKET_FLAGS_V3) {
|
|
pr_info("unknown flags 0x%x\n",
|
|
info->flags & ~XT_SOCKET_FLAGS_V3);
|
|
return -EINVAL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static struct xt_match socket_mt_reg[] __read_mostly = {
|
|
{
|
|
.name = "socket",
|
|
.revision = 0,
|
|
.family = NFPROTO_IPV4,
|
|
.match = socket_mt4_v0,
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
(1 << NF_INET_LOCAL_IN),
|
|
.me = THIS_MODULE,
|
|
},
|
|
{
|
|
.name = "socket",
|
|
.revision = 1,
|
|
.family = NFPROTO_IPV4,
|
|
.match = socket_mt4_v1_v2_v3,
|
|
.checkentry = socket_mt_v1_check,
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
(1 << NF_INET_LOCAL_IN),
|
|
.me = THIS_MODULE,
|
|
},
|
|
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
|
{
|
|
.name = "socket",
|
|
.revision = 1,
|
|
.family = NFPROTO_IPV6,
|
|
.match = socket_mt6_v1_v2_v3,
|
|
.checkentry = socket_mt_v1_check,
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
(1 << NF_INET_LOCAL_IN),
|
|
.me = THIS_MODULE,
|
|
},
|
|
#endif
|
|
{
|
|
.name = "socket",
|
|
.revision = 2,
|
|
.family = NFPROTO_IPV4,
|
|
.match = socket_mt4_v1_v2_v3,
|
|
.checkentry = socket_mt_v2_check,
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
(1 << NF_INET_LOCAL_IN),
|
|
.me = THIS_MODULE,
|
|
},
|
|
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
|
{
|
|
.name = "socket",
|
|
.revision = 2,
|
|
.family = NFPROTO_IPV6,
|
|
.match = socket_mt6_v1_v2_v3,
|
|
.checkentry = socket_mt_v2_check,
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
(1 << NF_INET_LOCAL_IN),
|
|
.me = THIS_MODULE,
|
|
},
|
|
#endif
|
|
{
|
|
.name = "socket",
|
|
.revision = 3,
|
|
.family = NFPROTO_IPV4,
|
|
.match = socket_mt4_v1_v2_v3,
|
|
.checkentry = socket_mt_v3_check,
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
(1 << NF_INET_LOCAL_IN),
|
|
.me = THIS_MODULE,
|
|
},
|
|
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
|
{
|
|
.name = "socket",
|
|
.revision = 3,
|
|
.family = NFPROTO_IPV6,
|
|
.match = socket_mt6_v1_v2_v3,
|
|
.checkentry = socket_mt_v3_check,
|
|
.matchsize = sizeof(struct xt_socket_mtinfo1),
|
|
.hooks = (1 << NF_INET_PRE_ROUTING) |
|
|
(1 << NF_INET_LOCAL_IN),
|
|
.me = THIS_MODULE,
|
|
},
|
|
#endif
|
|
};
|
|
|
|
static int __init socket_mt_init(void)
|
|
{
|
|
return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
|
|
}
|
|
|
|
static void __exit socket_mt_exit(void)
|
|
{
|
|
xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
|
|
}
|
|
|
|
module_init(socket_mt_init);
|
|
module_exit(socket_mt_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
|
|
MODULE_DESCRIPTION("x_tables socket match module");
|
|
MODULE_ALIAS("ipt_socket");
|
|
MODULE_ALIAS("ip6t_socket");
|