initrd: don't use inet_aton() to parse IPv4 address

inet_aton() is very accepting when parsing the address. For example,
it accepts addresses with fewer octets (interpreting the last octet
as a number in network byte order for multiple bytes). It also ignores
any trailing garbage after the first delimiting whitespace (at least,
the glibc implementation). It also accepts octets in hex and octal
notation.

For the initrd reader we want to be more forgiving than inet_pton()
and also accept addresses like 255.000.000.000 (octal notation). For
that we would want to use inet_aton(). But we should not accept all the
craziness that inet_aton() otherwise accepts.

Use nm_utils_parse_inaddr_bin_full() instead. This function implements
our way how we want to interpret IP addresses in string representation.
Under the hood, of course it also uses inet_pton() and even inet_aton(),
but it is stricter than inet_aton() and only accepts certain formats.
This commit is contained in:
Thomas Haller 2019-09-21 15:39:19 +02:00
parent 9618f1bb4b
commit d68373c305

View file

@ -96,24 +96,17 @@ dt_get_hwaddr_property (const char *base,
static NMIPAddress *
str_addr (const char *str, int *family)
{
struct in_addr inp;
NMIPAddr addr_bin;
if (*family == AF_UNSPEC)
*family = guess_ip_address_family (str);
if (*family == AF_UNSPEC) {
if (!nm_utils_parse_inaddr_bin_full (*family,
TRUE,
str,
family,
&addr_bin)) {
_LOGW (LOGD_CORE, "Malformed IP address: '%s'", str);
return NULL;
}
if (*family == AF_INET && inet_aton (str, &inp)) {
/* For IPv4, we need to be more tolerant than
* nm_ip_address_new(), to recognize things like
* the extra zeroes in "255.255.255.000" */
return nm_ip_address_new_binary (*family, &inp, 0, NULL);
}
return nm_ip_address_new (*family, str, 0, NULL);
return nm_ip_address_new_binary (*family, &addr_bin, 0, NULL);
}
NMConnection *