net: ipv6: route: Fix a sleep-in-atomic-context bug in ip6_convert_metrics()
Subsystems (3)
PATCH 1/1 net: ipv6: route: Fix a sleep-in-atomic-context bug in ip6_convert_metrics()
The kernel module may sleep with holding a spinlock.
The function call paths (from bottom to top) in Linux-4.16 are:
[FUNC] kzalloc(GFP_KERNEL)net/ipv6/route.c, 2430:
kzalloc in ip6_convert_metricsnet/ipv6/route.c, 2890:
ip6_convert_metrics in ip6_route_addnet/ipv6/addrconf.c, 2322:
ip6_route_add in addrconf_prefix_routenet/ipv6/addrconf.c, 3331:
addrconf_prefix_route in fixup_permanent_addrnet/ipv6/addrconf.c, 3354:
fixup_permanent_addr in addrconf_permanent_addrnet/ipv6/addrconf.c, 3358:
_raw_write_lock_bh in addrconf_permanent_addr
To fix this bug, GFP_KERNEL is replaced with GFP_ATOMIC.
This bug is found by my static analysis tool DSAC.
--- net/ipv6/route.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/route.c +1 -1
@@ -2742,7 +2742,7 @@ static int ip6_convert_metrics(struct net *net, struct fib6_info *rt,if (!cfg->fc_mx)return 0 ; - p =kzalloc (sizeof (*rt->fib6_metrics),GFP_KERNEL ); + p =kzalloc (sizeof (*rt->fib6_metrics),GFP_ATOMIC );if (unlikely (!p))return -ENOMEM ;
Signature · 1 line
2.17.0
Quoted 1 line · Jia-Ju Bai
The kernel module may sleep with holding a spinlock.
...
Quoted 6 lines
net/ipv6/route.c +0
-0
net/ipv6/route.c static int ip6_convert_metrics(struct net *net, struct fib6_info *rt, · line 2748
Quoted 8 lines
net/ipv6/route.c +1
-1@@ -2742,7 +2742,7 @@ static int ip6_convert_metrics(struct net *net, struct fib6_info *rt,if (!cfg->fc_mx)return 0 ; - p =kzalloc (sizeof (*rt->fib6_metrics),GFP_KERNEL ); + p =kzalloc (sizeof (*rt->fib6_metrics),GFP_ATOMIC );if (unlikely (!p))return -ENOMEM ;
This is the wrong solution. I'll take care of it next week after the holiday weekend.
Quoted 18 lines · Jia-Ju Bai
The kernel module may sleep with holding a spinlock.
The function call paths (from bottom to top) in Linux-4.16 are:
[FUNC]
kzalloc(GFP_KERNEL)net/ipv6/route.c, 2430:
kzalloc inip6_convert_metricsnet/ipv6/route.c, 2890:
ip6_convert_metricsinip6_route_addnet/ipv6/addrconf.c, 2322:
ip6_route_addinaddrconf_prefix_routenet/ipv6/addrconf.c, 3331:
addrconf_prefix_routeinfixup_permanent_addrnet/ipv6/addrconf.c, 3354:
fixup_permanent_addrinaddrconf_permanent_addrnet/ipv6/addrconf.c, 3358:
_raw_write_lock_bhinaddrconf_permanent_addr
Quoted 3 lines
To fix this bug,
GFP_KERNELis replaced withGFP_ATOMIC.This bug is found by my static analysis tool DSAC.
No kernel change is needed. Your static analysis tool and you in sending out patches need to take into context.
ip6_convert_metrics only calls kzalloc when fc_mx is set. fc_mx is only set via the RTA_METRICS attribute and only from the userspace call path. Hence, kzalloc with GFP_KERNEL is the appropriate argument.
Quoted 21 lines · David Ahern
Quoted 20 lines · Jia-Ju Bai
The kernel module may sleep with holding a spinlock.
The function call paths (from bottom to top) in Linux-4.16 are:
[FUNC]
kzalloc(GFP_KERNEL)net/ipv6/route.c, 2430:
kzalloc inip6_convert_metricsnet/ipv6/route.c, 2890:
ip6_convert_metricsinip6_route_addnet/ipv6/addrconf.c, 2322:
ip6_route_addinaddrconf_prefix_routenet/ipv6/addrconf.c, 3331:
addrconf_prefix_routeinfixup_permanent_addrnet/ipv6/addrconf.c, 3354:
fixup_permanent_addrinaddrconf_permanent_addrnet/ipv6/addrconf.c, 3358:
_raw_write_lock_bhinaddrconf_permanent_addrTo fix this bug,
GFP_KERNELis replaced withGFP_ATOMIC.
Quoted 7 lines
Quoted 1 line
This bug is found by my static analysis tool DSAC.
No kernel change is needed. Your static analysis tool and you in sending out patches need to take into context.
ip6_convert_metricsonly calls kzalloc whenfc_mxis set.fc_mxis only set via theRTA_METRICSattribute and only from the userspace call path. Hence, kzalloc withGFP_KERNELis the appropriate argument.
Oh, sorry for my false report.
Best wishes,
Jia-Ju Bai