networkd: fix crash if fails to get network file (#8714)
authorSusant Sahani <145210+ssahani@users.noreply.github.com>
Tue, 17 Apr 2018 14:55:04 +0000 (20:25 +0530)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 17 Apr 2018 14:55:04 +0000 (16:55 +0200)
In some situation if networkd fails to get the network file
then networkd crashes becasse the link->network is not initalized;

```
veth99: Failed to get network dhcp-client-ipv4-only: No such file or directory
Segmentation fault

gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/sus/tt/systemd/build/systemd-networkd
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
warning: Loadable section ".note.gnu.property" outside of ELF segments
warning: Loadable section ".note.gnu.property" outside of ELF segments
warning: Loadable section ".note.gnu.property" outside of ELF segments

Program received signal SIGSEGV, Segmentation fault.
link_load (link=0x55555582ccd0) at ../src/network/networkd-link.c:2973
2973                 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
(gdb) bt
(gdb) p link->network
$1 = (Network *) 0x0
(gdb) list
2968                 if (r < 0) {
2969                         log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address);
2970                         goto dhcp4_address_fail;
2971                 }
2972
2973                 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
2974                 if (r < 0)
2975                         return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m");
2976
2977                 r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in);
(gdb) r
```

src/network/networkd-link.c

index 2a231c2..c921f92 100644 (file)
@@ -2970,7 +2970,7 @@ network_file_fail:
                         goto dhcp4_address_fail;
                 }
 
-                r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
+                r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0);
                 if (r < 0)
                         return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m");