Skip to content

Commit 82486aa

Browse files
congwangdavem330
authored andcommitted
ipv4: restore rt->fi for reference counting
IPv4 dst could use fi->fib_metrics to store metrics but fib_info itself is refcnt'ed, so without taking a refcnt fi and fi->fib_metrics could be freed while dst metrics still points to it. This triggers use-after-free as reported by Andrey twice. This patch reverts commit 2860583 ("ipv4: Kill rt->fi") to restore this reference counting. It is a quick fix for -net and -stable, for -net-next, as Eric suggested, we can consider doing reference counting for metrics itself instead of relying on fib_info. IPv6 is very different, it copies or steals the metrics from mx6_config in fib6_commit_metrics() so probably doesn't need a refcnt. Decnet has already done the refcnt'ing, see dn_fib_semantic_match(). Fixes: 2860583 ("ipv4: Kill rt->fi") Reported-by: Andrey Konovalov <andreyknvl@google.com> Tested-by: Andrey Konovalov <andreyknvl@google.com> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Acked-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3013c49 commit 82486aa

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

include/net/route.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct rtable {
6969

7070
struct list_head rt_uncached;
7171
struct uncached_list *rt_uncached_list;
72+
struct fib_info *fi; /* for refcnt to shared metrics */
7273
};
7374

7475
static inline bool rt_is_input_route(const struct rtable *rt)

net/ipv4/route.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,11 @@ static void ipv4_dst_destroy(struct dst_entry *dst)
13871387
{
13881388
struct rtable *rt = (struct rtable *) dst;
13891389

1390+
if (rt->fi) {
1391+
fib_info_put(rt->fi);
1392+
rt->fi = NULL;
1393+
}
1394+
13901395
if (!list_empty(&rt->rt_uncached)) {
13911396
struct uncached_list *ul = rt->rt_uncached_list;
13921397

@@ -1424,6 +1429,16 @@ static bool rt_cache_valid(const struct rtable *rt)
14241429
!rt_is_expired(rt);
14251430
}
14261431

1432+
static void rt_init_metrics(struct rtable *rt, struct fib_info *fi)
1433+
{
1434+
if (fi->fib_metrics != (u32 *)dst_default_metrics) {
1435+
fib_info_hold(fi);
1436+
rt->fi = fi;
1437+
}
1438+
1439+
dst_init_metrics(&rt->dst, fi->fib_metrics, true);
1440+
}
1441+
14271442
static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
14281443
const struct fib_result *res,
14291444
struct fib_nh_exception *fnhe,
@@ -1438,7 +1453,7 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
14381453
rt->rt_gateway = nh->nh_gw;
14391454
rt->rt_uses_gateway = 1;
14401455
}
1441-
dst_init_metrics(&rt->dst, fi->fib_metrics, true);
1456+
rt_init_metrics(rt, fi);
14421457
#ifdef CONFIG_IP_ROUTE_CLASSID
14431458
rt->dst.tclassid = nh->nh_tclassid;
14441459
#endif
@@ -1490,6 +1505,7 @@ struct rtable *rt_dst_alloc(struct net_device *dev,
14901505
rt->rt_gateway = 0;
14911506
rt->rt_uses_gateway = 0;
14921507
rt->rt_table_id = 0;
1508+
rt->fi = NULL;
14931509
INIT_LIST_HEAD(&rt->rt_uncached);
14941510

14951511
rt->dst.output = ip_output;

0 commit comments

Comments
 (0)