Skip to content

Commit f2913b1

Browse files
yoshfujigregkh
authored andcommitted
ipv6: fix race condition regarding dst->expires and dst->from.
[ Upstream commit ecd9883 ] Eric Dumazet wrote: | Some strange crashes happen in rt6_check_expired(), with access | to random addresses. | | At first glance, it looks like the RTF_EXPIRES and | stuff added in commit 1716a96 | (ipv6: fix problem with expired dst cache) | are racy : same dst could be manipulated at the same time | on different cpus. | | At some point, our stack believes rt->dst.from contains a dst pointer, | while its really a jiffie value (as rt->dst.expires shares the same area | of memory) | | rt6_update_expires() should be fixed, or am I missing something ? | | CC Neil because of https://bugzilla.redhat.com/show_bug.cgi?id=892060 Because we do not have any locks for dst_entry, we cannot change essential structure in the entry; e.g., we cannot change reference to other entity. To fix this issue, split 'from' and 'expires' field in dst_entry out of union. Once it is 'from' is assigned in the constructor, keep the reference until the very last stage of the life time of the object. Of course, it is unsafe to change 'from', so make rt6_set_from simple just for fresh entries. Reported-by: Eric Dumazet <eric.dumazet@gmail.com> Reported-by: Neil Horman <nhorman@tuxdriver.com> CC: Gao Feng <gaofeng@cn.fujitsu.com> Signed-off-by: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> Reviewed-by: Eric Dumazet <edumazet@google.com> Reported-by: Steinar H. Gunderson <sesse@google.com> Reviewed-by: Neil Horman <nhorman@tuxdriver.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a515e3e commit f2913b1

4 files changed

Lines changed: 18 additions & 38 deletions

File tree

‎include/net/dst.h‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ struct dst_entry {
3636
struct net_device *dev;
3737
struct dst_ops *ops;
3838
unsigned long _metrics;
39-
union {
40-
unsigned long expires;
41-
/* point to where the dst_entry copied from */
42-
struct dst_entry *from;
43-
};
39+
unsigned long expires;
4440
struct dst_entry *path;
45-
void *__pad0;
41+
struct dst_entry *from;
4642
#ifdef CONFIG_XFRM
4743
struct xfrm_state *xfrm;
4844
#else

‎include/net/ip6_fib.h‎

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -166,50 +166,35 @@ static inline struct inet6_dev *ip6_dst_idev(struct dst_entry *dst)
166166

167167
static inline void rt6_clean_expires(struct rt6_info *rt)
168168
{
169-
if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from)
170-
dst_release(rt->dst.from);
171-
172169
rt->rt6i_flags &= ~RTF_EXPIRES;
173-
rt->dst.from = NULL;
174170
}
175171

176172
static inline void rt6_set_expires(struct rt6_info *rt, unsigned long expires)
177173
{
178-
if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from)
179-
dst_release(rt->dst.from);
180-
181-
rt->rt6i_flags |= RTF_EXPIRES;
182174
rt->dst.expires = expires;
175+
rt->rt6i_flags |= RTF_EXPIRES;
183176
}
184177

185-
static inline void rt6_update_expires(struct rt6_info *rt, int timeout)
178+
static inline void rt6_update_expires(struct rt6_info *rt0, int timeout)
186179
{
187-
if (!(rt->rt6i_flags & RTF_EXPIRES)) {
188-
if (rt->dst.from)
189-
dst_release(rt->dst.from);
190-
/* dst_set_expires relies on expires == 0
191-
* if it has not been set previously.
192-
*/
193-
rt->dst.expires = 0;
194-
}
195-
196-
dst_set_expires(&rt->dst, timeout);
197-
rt->rt6i_flags |= RTF_EXPIRES;
180+
struct rt6_info *rt;
181+
182+
for (rt = rt0; rt && !(rt->rt6i_flags & RTF_EXPIRES);
183+
rt = (struct rt6_info *)rt->dst.from);
184+
if (rt && rt != rt0)
185+
rt0->dst.expires = rt->dst.expires;
186+
187+
dst_set_expires(&rt0->dst, timeout);
188+
rt0->rt6i_flags |= RTF_EXPIRES;
198189
}
199190

200191
static inline void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
201192
{
202193
struct dst_entry *new = (struct dst_entry *) from;
203194

204-
if (!(rt->rt6i_flags & RTF_EXPIRES) && rt->dst.from) {
205-
if (new == rt->dst.from)
206-
return;
207-
dst_release(rt->dst.from);
208-
}
209-
210195
rt->rt6i_flags &= ~RTF_EXPIRES;
211-
rt->dst.from = new;
212196
dst_hold(new);
197+
rt->dst.from = new;
213198
}
214199

215200
static inline void ip6_rt_put(struct rt6_info *rt)

‎net/core/dst.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
179179
dst_init_metrics(dst, dst_default_metrics, true);
180180
dst->expires = 0UL;
181181
dst->path = dst;
182+
dst->from = NULL;
182183
#ifdef CONFIG_XFRM
183184
dst->xfrm = NULL;
184185
#endif

‎net/ipv6/route.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ static void ip6_dst_destroy(struct dst_entry *dst)
300300
{
301301
struct rt6_info *rt = (struct rt6_info *)dst;
302302
struct inet6_dev *idev = rt->rt6i_idev;
303+
struct dst_entry *from = dst->from;
303304

304305
if (rt->n)
305306
neigh_release(rt->n);
@@ -312,8 +313,8 @@ static void ip6_dst_destroy(struct dst_entry *dst)
312313
in6_dev_put(idev);
313314
}
314315

315-
if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
316-
dst_release(dst->from);
316+
dst->from = NULL;
317+
dst_release(from);
317318

318319
if (rt6_has_peer(rt)) {
319320
struct inet_peer *peer = rt6_peer_ptr(rt);
@@ -1054,7 +1055,6 @@ struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_ori
10541055

10551056
rt->rt6i_gateway = ort->rt6i_gateway;
10561057
rt->rt6i_flags = ort->rt6i_flags;
1057-
rt6_clean_expires(rt);
10581058
rt->rt6i_metric = 0;
10591059

10601060
memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
@@ -1859,8 +1859,6 @@ static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
18591859
if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
18601860
(RTF_DEFAULT | RTF_ADDRCONF))
18611861
rt6_set_from(rt, ort);
1862-
else
1863-
rt6_clean_expires(rt);
18641862
rt->rt6i_metric = 0;
18651863

18661864
#ifdef CONFIG_IPV6_SUBTREES

0 commit comments

Comments
 (0)