From: "Timo Teräs" <timo.teras@iki.fi>
To: David Miller <davem@davemloft.net>,
eric.dumazet@gmail.com, netdev@vger.kernel.org
Cc: "Timo Teräs" <timo.teras@iki.fi>
Subject: [PATCH v2] ipv4: synchronize bind() with RTM_NEWADDR notifications
Date: Thu, 21 Oct 2010 16:06:23 +0300 [thread overview]
Message-ID: <1287666383-17615-1-git-send-email-timo.teras@iki.fi> (raw)
In-Reply-To: <4CC02ABF.8090008@iki.fi>
Otherwise we have race condition to user land:
1. process A: changes IP address
2. process A: kernel sends RTM_NEWADDR (and schedules out)
3. process B: gets notification
4. process B: tries to bind() to new IP, but fails with EADDRNOTAVAIL
because FIB is not yet updated and inet_addr_type() in inet_bind()
does not recognize the IP as local
5. process A: calls inetaddr_chain notifiers which updates FIB
Fix the error path to synchronize with configuration changes and retry
the address type check.
IPv6 side seems to handle the notifications properly: bind() immediately
after RTM_NEWADDR succeeds as expected. This is because ipv6_chk_addr()
uses inet6_addr_lst which is updated before address notification.
Signed-off-by: Timo Teräs <timo.teras@iki.fi>
---
Since there was no reply to my question if this is ok, I interpreted it
as "maybe". So here's the code for review. Hopefully this helps determining
if this is an acceptable fix.
net/ipv4/af_inet.c | 18 ++++++++++++++++--
net/ipv6/af_inet6.c | 13 ++++++++++---
2 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index 6a1100c..013ab11 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -481,8 +481,22 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
chk_addr_ret != RTN_LOCAL &&
chk_addr_ret != RTN_MULTICAST &&
- chk_addr_ret != RTN_BROADCAST)
- goto out;
+ chk_addr_ret != RTN_BROADCAST) {
+ /* inet_addr_type() does a FIB lookup to check the
+ * address type. Since FIB is updated after sending
+ * RTM_NEWADDR notification, an application may end up
+ * doing bind() before the FIB is updated. To avoid
+ * returning a false negative, wait for possible ongoing
+ * address changes to finish by acquiring rtnl lock and
+ * retry the address type lookup. */
+ rtnl_lock();
+ rtnl_unlock();
+ chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
+ if (chk_addr_ret != RTN_LOCAL &&
+ chk_addr_ret != RTN_MULTICAST &&
+ chk_addr_ret != RTN_BROADCAST)
+ goto out;
+ }
snum = ntohs(addr->sin_port);
err = -EACCES;
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index 56b9bf2..b1a83e1 100644
--- a/net/ipv6/af_inet6.c
+++ b/net/ipv6/af_inet6.c
@@ -300,7 +300,7 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
goto out;
}
- /* Reproduce AF_INET checks to make the bindings consitant */
+ /* Reproduce AF_INET checks to make the bindings consistent */
v4addr = addr->sin6_addr.s6_addr32[3];
chk_addr_ret = inet_addr_type(net, v4addr);
if (!sysctl_ip_nonlocal_bind &&
@@ -309,8 +309,15 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
chk_addr_ret != RTN_LOCAL &&
chk_addr_ret != RTN_MULTICAST &&
chk_addr_ret != RTN_BROADCAST) {
- err = -EADDRNOTAVAIL;
- goto out;
+ rtnl_lock();
+ rtnl_unlock();
+ chk_addr_ret = inet_addr_type(net, v4addr);
+ if (chk_addr_ret != RTN_LOCAL &&
+ chk_addr_ret != RTN_MULTICAST &&
+ chk_addr_ret != RTN_BROADCAST) {
+ err = -EADDRNOTAVAIL;
+ goto out;
+ }
}
} else {
if (addr_type != IPV6_ADDR_ANY) {
--
1.7.1
next prev parent reply other threads:[~2010-10-21 13:06 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-21 10:12 [PATCH] ipv4: synchronize bind() with RTM_NEWADDR notifications Timo Teräs
2010-10-21 10:25 ` Eric Dumazet
2010-10-21 10:41 ` Timo Teräs
2010-10-21 10:50 ` David Miller
2010-10-21 10:58 ` Timo Teräs
2010-10-21 11:03 ` David Miller
2010-10-21 11:29 ` Timo Teräs
2010-10-21 11:34 ` David Miller
2010-10-21 11:57 ` Timo Teräs
2010-10-21 13:06 ` Timo Teräs [this message]
2010-10-21 14:10 ` [PATCH v2] " Eric Dumazet
2010-10-21 19:01 ` Timo Teräs
2010-10-21 11:12 ` [PATCH] " Eric Dumazet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1287666383-17615-1-git-send-email-timo.teras@iki.fi \
--to=timo.teras@iki.fi \
--cc=davem@davemloft.net \
--cc=eric.dumazet@gmail.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).