Skip to content

Commit

Permalink
netfilter: tproxy: do not assign timewait sockets to skb->sk
Browse files Browse the repository at this point in the history
Assigning a socket in timewait state to skb->sk can trigger
kernel oops, e.g. in nfnetlink_log, which does:

if (skb->sk) {
        read_lock_bh(&skb->sk->sk_callback_lock);
        if (skb->sk->sk_socket && skb->sk->sk_socket->file) ...

in the timewait case, accessing sk->sk_callback_lock and sk->sk_socket
is invalid.

Either all of these spots will need to add a test for sk->sk_state != TCP_TIME_WAIT,
or xt_TPROXY must not assign a timewait socket to skb->sk.

This does the latter.

If a TW socket is found, assign the tproxy nfmark, but skip the skb->sk assignment,
thus mimicking behaviour of a '-m socket .. -j MARK/ACCEPT' re-routing rule.

The 'SYN to TW socket' case is left unchanged -- we try to redirect to the
listener socket.

Cc: Balazs Scheidler <[email protected]>
Cc: KOVACS Krisztian <[email protected]>
Signed-off-by: Florian Westphal <[email protected]>
Signed-off-by: Patrick McHardy <[email protected]>
  • Loading branch information
Florian Westphal authored and kaber committed Feb 17, 2011
1 parent de9963f commit d503b30
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
12 changes: 1 addition & 11 deletions include/net/netfilter/nf_tproxy_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,8 @@ nf_tproxy_get_sock_v6(struct net *net, const u8 protocol,
}
#endif

static inline void
nf_tproxy_put_sock(struct sock *sk)
{
/* TIME_WAIT inet sockets have to be handled differently */
if ((sk->sk_protocol == IPPROTO_TCP) && (sk->sk_state == TCP_TIME_WAIT))
inet_twsk_put(inet_twsk(sk));
else
sock_put(sk);
}

/* assign a socket to the skb -- consumes sk */
int
void
nf_tproxy_assign_sock(struct sk_buff *skb, struct sock *sk);

#endif
27 changes: 12 additions & 15 deletions net/netfilter/nf_tproxy_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,23 @@ nf_tproxy_destructor(struct sk_buff *skb)
skb->destructor = NULL;

if (sk)
nf_tproxy_put_sock(sk);
sock_put(sk);
}

/* consumes sk */
int
void
nf_tproxy_assign_sock(struct sk_buff *skb, struct sock *sk)
{
bool transparent = (sk->sk_state == TCP_TIME_WAIT) ?
inet_twsk(sk)->tw_transparent :
inet_sk(sk)->transparent;

if (transparent) {
skb_orphan(skb);
skb->sk = sk;
skb->destructor = nf_tproxy_destructor;
return 1;
} else
nf_tproxy_put_sock(sk);

return 0;
/* assigning tw sockets complicates things; most
* skb->sk->X checks would have to test sk->sk_state first */
if (sk->sk_state == TCP_TIME_WAIT) {
inet_twsk_put(inet_twsk(sk));
return;
}

skb_orphan(skb);
skb->sk = sk;
skb->destructor = nf_tproxy_destructor;
}
EXPORT_SYMBOL_GPL(nf_tproxy_assign_sock);

Expand Down
22 changes: 20 additions & 2 deletions net/netfilter/xt_TPROXY.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
#include <net/netfilter/nf_tproxy_core.h>
#include <linux/netfilter/xt_TPROXY.h>

static bool tproxy_sk_is_transparent(struct sock *sk)
{
if (sk->sk_state != TCP_TIME_WAIT) {
if (inet_sk(sk)->transparent)
return true;
sock_put(sk);
} else {
if (inet_twsk(sk)->tw_transparent)
return true;
inet_twsk_put(inet_twsk(sk));
}
return false;
}

static inline __be32
tproxy_laddr4(struct sk_buff *skb, __be32 user_laddr, __be32 daddr)
{
Expand Down Expand Up @@ -141,14 +155,16 @@ tproxy_tg4(struct sk_buff *skb, __be32 laddr, __be16 lport,
skb->dev, NFT_LOOKUP_LISTENER);

/* NOTE: assign_sock consumes our sk reference */
if (sk && nf_tproxy_assign_sock(skb, sk)) {
if (sk && tproxy_sk_is_transparent(sk)) {
/* This should be in a separate target, but we don't do multiple
targets on the same rule yet */
skb->mark = (skb->mark & ~mark_mask) ^ mark_value;

pr_debug("redirecting: proto %hhu %pI4:%hu -> %pI4:%hu, mark: %x\n",
iph->protocol, &iph->daddr, ntohs(hp->dest),
&laddr, ntohs(lport), skb->mark);

nf_tproxy_assign_sock(skb, sk);
return NF_ACCEPT;
}

Expand Down Expand Up @@ -306,14 +322,16 @@ tproxy_tg6_v1(struct sk_buff *skb, const struct xt_action_param *par)
par->in, NFT_LOOKUP_LISTENER);

/* NOTE: assign_sock consumes our sk reference */
if (sk && nf_tproxy_assign_sock(skb, sk)) {
if (sk && tproxy_sk_is_transparent(sk)) {
/* This should be in a separate target, but we don't do multiple
targets on the same rule yet */
skb->mark = (skb->mark & ~tgi->mark_mask) ^ tgi->mark_value;

pr_debug("redirecting: proto %hhu %pI6:%hu -> %pI6:%hu, mark: %x\n",
tproto, &iph->saddr, ntohs(hp->source),
laddr, ntohs(lport), skb->mark);

nf_tproxy_assign_sock(skb, sk);
return NF_ACCEPT;
}

Expand Down
13 changes: 11 additions & 2 deletions net/netfilter/xt_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
#include <net/netfilter/nf_conntrack.h>
#endif

static void
xt_socket_put_sk(struct sock *sk)
{
if (sk->sk_state == TCP_TIME_WAIT)
inet_twsk_put(inet_twsk(sk));
else
sock_put(sk);
}

static int
extract_icmp4_fields(const struct sk_buff *skb,
u8 *protocol,
Expand Down Expand Up @@ -164,7 +173,7 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
(sk->sk_state == TCP_TIME_WAIT &&
inet_twsk(sk)->tw_transparent));

nf_tproxy_put_sock(sk);
xt_socket_put_sk(sk);

if (wildcard || !transparent)
sk = NULL;
Expand Down Expand Up @@ -298,7 +307,7 @@ socket_mt6_v1(const struct sk_buff *skb, struct xt_action_param *par)
(sk->sk_state == TCP_TIME_WAIT &&
inet_twsk(sk)->tw_transparent));

nf_tproxy_put_sock(sk);
xt_socket_put_sk(sk);

if (wildcard || !transparent)
sk = NULL;
Expand Down

0 comments on commit d503b30

Please sign in to comment.