local-addresses: check family more

Just for safety. No functional change, unless the kernel sends broken
messages.
This commit is contained in:
Yu Watanabe 2024-01-19 19:43:26 +09:00
parent 37359b1c81
commit 5cb56068d0

View file

@ -115,6 +115,8 @@ int local_addresses(
r = sd_rtnl_message_addr_get_family(m, &family); r = sd_rtnl_message_addr_get_family(m, &family);
if (r < 0) if (r < 0)
return r; return r;
if (!IN_SET(family, AF_INET, AF_INET6))
continue;
if (af != AF_UNSPEC && af != family) if (af != AF_UNSPEC && af != family)
continue; continue;
@ -215,6 +217,12 @@ int local_gateways(
size_t n_list = 0; size_t n_list = 0;
int r; int r;
/* The RTA_VIA attribute is used only for IPv4 routes with an IPv6 gateway. If IPv4 gateways are
* requested (af == AF_INET), then we do not return IPv6 gateway addresses. Similary, if IPv6
* gateways are requested (af == AF_INET6), then we do not return gateway addresses for IPv4 routes.
* So, the RTA_VIA attribute is only parsed when af == AF_UNSPEC. */
bool allow_via = af == AF_UNSPEC;
if (context) if (context)
rtnl = sd_netlink_ref(context); rtnl = sd_netlink_ref(context);
else { else {
@ -292,6 +300,8 @@ int local_gateways(
return r; return r;
if (!IN_SET(family, AF_INET, AF_INET6)) if (!IN_SET(family, AF_INET, AF_INET6))
continue; continue;
if (af != AF_UNSPEC && af != family)
continue;
r = sd_netlink_message_read_u32(m, RTA_OIF, &ifi); r = sd_netlink_message_read_u32(m, RTA_OIF, &ifi);
if (r < 0 && r != -ENODATA) if (r < 0 && r != -ENODATA)
@ -315,6 +325,9 @@ int local_gateways(
continue; continue;
} }
if (!allow_via)
continue;
if (family != AF_INET) if (family != AF_INET)
continue; continue;
@ -322,6 +335,9 @@ int local_gateways(
if (r < 0 && r != -ENODATA) if (r < 0 && r != -ENODATA)
return r; return r;
if (r >= 0) { if (r >= 0) {
if (via.family != AF_INET6)
return -EBADMSG;
r = add_local_gateway(&list, &n_list, af, ifi, priority, &via); r = add_local_gateway(&list, &n_list, af, ifi, priority, &via);
if (r < 0) if (r < 0)
return r; return r;
@ -344,6 +360,9 @@ int local_gateways(
if (ifindex > 0 && mr->ifindex != ifindex) if (ifindex > 0 && mr->ifindex != ifindex)
continue; continue;
if (!allow_via && family != mr->gateway.family)
continue;
r = add_local_gateway(&list, &n_list, af, ifi, priority, &mr->gateway); r = add_local_gateway(&list, &n_list, af, ifi, priority, &mr->gateway);
if (r < 0) if (r < 0)
return r; return r;