Patrick Williams | d767d3f | 2021-08-30 15:17:28 -0500 | [diff] [blame^] | 1 | From ac6c7f2d2389c5c0ae90554a58f1c75f60cc8e5a Mon Sep 17 00:00:00 2001 |
| 2 | From: Yu Watanabe <watanabe.yu+github@gmail.com> |
| 3 | Date: Thu, 24 Jun 2021 00:48:23 +0900 |
| 4 | Subject: [PATCH] sd-dhcp-client: check error earlier and reduce indentation |
| 5 | |
| 6 | Upstream-Status: Backport |
| 7 | CVE: CVE-2020-13529 |
| 8 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
| 9 | --- |
| 10 | src/libsystemd-network/sd-dhcp-client.c | 128 ++++++++++++------------ |
| 11 | 1 file changed, 64 insertions(+), 64 deletions(-) |
| 12 | |
| 13 | diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c |
| 14 | index d472fcd941..86bc3c6181 100644 |
| 15 | --- a/src/libsystemd-network/sd-dhcp-client.c |
| 16 | +++ b/src/libsystemd-network/sd-dhcp-client.c |
| 17 | @@ -1770,21 +1770,21 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, i |
| 18 | case DHCP_STATE_SELECTING: |
| 19 | |
| 20 | r = client_handle_offer(client, message, len); |
| 21 | - if (r >= 0) { |
| 22 | + if (r == -ENOMSG) |
| 23 | + return 0; /* invalid message, let's ignore it */ |
| 24 | + if (r < 0) |
| 25 | + goto error; |
| 26 | |
| 27 | - client->state = DHCP_STATE_REQUESTING; |
| 28 | - client->attempt = 0; |
| 29 | + client->state = DHCP_STATE_REQUESTING; |
| 30 | + client->attempt = 0; |
| 31 | |
| 32 | - r = event_reset_time(client->event, &client->timeout_resend, |
| 33 | - clock_boottime_or_monotonic(), |
| 34 | - 0, 0, |
| 35 | - client_timeout_resend, client, |
| 36 | - client->event_priority, "dhcp4-resend-timer", true); |
| 37 | - if (r < 0) |
| 38 | - goto error; |
| 39 | - } else if (r == -ENOMSG) |
| 40 | - /* invalid message, let's ignore it */ |
| 41 | - return 0; |
| 42 | + r = event_reset_time(client->event, &client->timeout_resend, |
| 43 | + clock_boottime_or_monotonic(), |
| 44 | + 0, 0, |
| 45 | + client_timeout_resend, client, |
| 46 | + client->event_priority, "dhcp4-resend-timer", true); |
| 47 | + if (r < 0) |
| 48 | + goto error; |
| 49 | |
| 50 | break; |
| 51 | |
| 52 | @@ -1794,47 +1794,9 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, i |
| 53 | case DHCP_STATE_REBINDING: |
| 54 | |
| 55 | r = client_handle_ack(client, message, len); |
| 56 | - if (r >= 0) { |
| 57 | - client->start_delay = 0; |
| 58 | - (void) event_source_disable(client->timeout_resend); |
| 59 | - client->receive_message = |
| 60 | - sd_event_source_unref(client->receive_message); |
| 61 | - client->fd = safe_close(client->fd); |
| 62 | - |
| 63 | - if (IN_SET(client->state, DHCP_STATE_REQUESTING, |
| 64 | - DHCP_STATE_REBOOTING)) |
| 65 | - notify_event = SD_DHCP_CLIENT_EVENT_IP_ACQUIRE; |
| 66 | - else if (r != SD_DHCP_CLIENT_EVENT_IP_ACQUIRE) |
| 67 | - notify_event = r; |
| 68 | - |
| 69 | - client->state = DHCP_STATE_BOUND; |
| 70 | - client->attempt = 0; |
| 71 | - |
| 72 | - client->last_addr = client->lease->address; |
| 73 | - |
| 74 | - r = client_set_lease_timeouts(client); |
| 75 | - if (r < 0) { |
| 76 | - log_dhcp_client(client, "could not set lease timeouts"); |
| 77 | - goto error; |
| 78 | - } |
| 79 | - |
| 80 | - r = dhcp_network_bind_udp_socket(client->ifindex, client->lease->address, client->port, client->ip_service_type); |
| 81 | - if (r < 0) { |
| 82 | - log_dhcp_client(client, "could not bind UDP socket"); |
| 83 | - goto error; |
| 84 | - } |
| 85 | - |
| 86 | - client->fd = r; |
| 87 | - |
| 88 | - client_initialize_io_events(client, client_receive_message_udp); |
| 89 | - |
| 90 | - if (notify_event) { |
| 91 | - client_notify(client, notify_event); |
| 92 | - if (client->state == DHCP_STATE_STOPPED) |
| 93 | - return 0; |
| 94 | - } |
| 95 | - |
| 96 | - } else if (r == -EADDRNOTAVAIL) { |
| 97 | + if (r == -ENOMSG) |
| 98 | + return 0; /* invalid message, let's ignore it */ |
| 99 | + if (r == -EADDRNOTAVAIL) { |
| 100 | /* got a NAK, let's restart the client */ |
| 101 | client_notify(client, SD_DHCP_CLIENT_EVENT_EXPIRED); |
| 102 | |
| 103 | @@ -1853,21 +1815,59 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, i |
| 104 | RESTART_AFTER_NAK_MIN_USEC, RESTART_AFTER_NAK_MAX_USEC); |
| 105 | |
| 106 | return 0; |
| 107 | - } else if (r == -ENOMSG) |
| 108 | - /* invalid message, let's ignore it */ |
| 109 | - return 0; |
| 110 | + } |
| 111 | + if (r < 0) |
| 112 | + goto error; |
| 113 | + |
| 114 | + client->start_delay = 0; |
| 115 | + (void) event_source_disable(client->timeout_resend); |
| 116 | + client->receive_message = sd_event_source_unref(client->receive_message); |
| 117 | + client->fd = safe_close(client->fd); |
| 118 | + |
| 119 | + if (IN_SET(client->state, DHCP_STATE_REQUESTING, DHCP_STATE_REBOOTING)) |
| 120 | + notify_event = SD_DHCP_CLIENT_EVENT_IP_ACQUIRE; |
| 121 | + else if (r != SD_DHCP_CLIENT_EVENT_IP_ACQUIRE) |
| 122 | + notify_event = r; |
| 123 | + |
| 124 | + client->state = DHCP_STATE_BOUND; |
| 125 | + client->attempt = 0; |
| 126 | + |
| 127 | + client->last_addr = client->lease->address; |
| 128 | + |
| 129 | + r = client_set_lease_timeouts(client); |
| 130 | + if (r < 0) { |
| 131 | + log_dhcp_client(client, "could not set lease timeouts"); |
| 132 | + goto error; |
| 133 | + } |
| 134 | + |
| 135 | + r = dhcp_network_bind_udp_socket(client->ifindex, client->lease->address, client->port, client->ip_service_type); |
| 136 | + if (r < 0) { |
| 137 | + log_dhcp_client(client, "could not bind UDP socket"); |
| 138 | + goto error; |
| 139 | + } |
| 140 | + |
| 141 | + client->fd = r; |
| 142 | + |
| 143 | + client_initialize_io_events(client, client_receive_message_udp); |
| 144 | + |
| 145 | + if (notify_event) { |
| 146 | + client_notify(client, notify_event); |
| 147 | + if (client->state == DHCP_STATE_STOPPED) |
| 148 | + return 0; |
| 149 | + } |
| 150 | |
| 151 | break; |
| 152 | |
| 153 | case DHCP_STATE_BOUND: |
| 154 | r = client_handle_forcerenew(client, message, len); |
| 155 | - if (r >= 0) { |
| 156 | - r = client_timeout_t1(NULL, 0, client); |
| 157 | - if (r < 0) |
| 158 | - goto error; |
| 159 | - } else if (r == -ENOMSG) |
| 160 | - /* invalid message, let's ignore it */ |
| 161 | - return 0; |
| 162 | + if (r == -ENOMSG) |
| 163 | + return 0; /* invalid message, let's ignore it */ |
| 164 | + if (r < 0) |
| 165 | + goto error; |
| 166 | + |
| 167 | + r = client_timeout_t1(NULL, 0, client); |
| 168 | + if (r < 0) |
| 169 | + goto error; |
| 170 | |
| 171 | break; |
| 172 | |