Bug Summary

File:src/usr.sbin/dhcrelay/dispatch.c
Warning:line 302, column 15
Use of zero-allocated memory

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name dispatch.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.sbin/dhcrelay/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.sbin/dhcrelay/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.sbin/dhcrelay/dispatch.c
1/* $OpenBSD: dispatch.c,v 1.23 2021/01/17 13:40:59 claudio Exp $ */
2
3/*
4 * Copyright 2004 Henning Brauer <henning@openbsd.org>
5 * Copyright (c) 1995, 1996, 1997, 1998, 1999
6 * The Internet Software Consortium. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of The Internet Software Consortium nor the names
18 * of its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
22 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * This software has been written for the Internet Software Consortium
36 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
37 * Enterprises. To learn more about the Internet Software Consortium,
38 * see ``http://www.vix.com/isc''. To learn more about Vixie
39 * Enterprises, see ``http://www.vix.com''.
40 */
41
42#include <sys/types.h>
43#include <sys/ioctl.h>
44#include <sys/socket.h>
45
46#include <net/if.h>
47#include <net/if_dl.h>
48#include <net/if_media.h>
49#include <net/if_types.h>
50
51#include <netinet/in.h>
52#include <netinet/if_ether.h>
53
54#include <errno(*__errno()).h>
55#include <ifaddrs.h>
56#include <limits.h>
57#include <poll.h>
58#include <stdlib.h>
59#include <string.h>
60#include <syslog.h>
61#include <time.h>
62#include <unistd.h>
63
64#include "dhcp.h"
65#include "dhcpd.h"
66#include "log.h"
67
68/*
69 * Macros implementation used to generate link-local addresses. This
70 * code was copied from: sys/netinet6/in6_ifattach.c.
71 */
72#define EUI64_UBIT0x02 0x02
73#define EUI64_TO_IFID(in6)do { (in6)->__u6_addr.__u6_addr8[8] ^= 0x02; } while (0) \
74 do { (in6)->s6_addr__u6_addr.__u6_addr8[8] ^= EUI64_UBIT0x02; } while (0)
75
76struct protocol *protocols;
77struct timeout *timeouts;
78static struct timeout *free_timeouts;
79static int interfaces_invalidated;
80
81void (*bootp_packet_handler)(struct interface_info *,
82 struct dhcp_packet *, int, struct packet_ctx *);
83
84static int interface_status(struct interface_info *ifinfo);
85
86struct interface_info *
87iflist_getbyname(const char *name)
88{
89 struct interface_info *intf;
90
91 TAILQ_FOREACH(intf, &intflist, entry)for((intf) = ((&intflist)->tqh_first); (intf) != ((void
*)0); (intf) = ((intf)->entry.tqe_next))
{
92 if (strcmp(intf->name, name) != 0)
93 continue;
94
95 return intf;
96 }
97
98 return NULL((void *)0);
99}
100
101void
102setup_iflist(void)
103{
104 struct interface_info *intf;
105 struct sockaddr_dl *sdl;
106 struct ifaddrs *ifap, *ifa;
107 struct if_data *ifi;
108 struct sockaddr_in *sin;
109 struct sockaddr_in6 *sin6;
110
111 TAILQ_INIT(&intflist)do { (&intflist)->tqh_first = ((void *)0); (&intflist
)->tqh_last = &(&intflist)->tqh_first; } while (
0)
;
112 if (getifaddrs(&ifap))
113 fatalx("getifaddrs failed");
114
115 for (ifa = ifap; ifa != NULL((void *)0); ifa = ifa->ifa_next) {
116 if ((ifa->ifa_flags & IFF_LOOPBACK0x8) ||
117 (ifa->ifa_flags & IFF_POINTOPOINT0x10))
118 continue;
119
120 /* Find interface or create it. */
121 intf = iflist_getbyname(ifa->ifa_name);
122 if (intf == NULL((void *)0)) {
123 intf = calloc(1, sizeof(*intf));
124 if (intf == NULL((void *)0))
125 fatal("calloc");
126
127 strlcpy(intf->name, ifa->ifa_name,
128 sizeof(intf->name));
129 TAILQ_INSERT_HEAD(&intflist, intf, entry)do { if (((intf)->entry.tqe_next = (&intflist)->tqh_first
) != ((void *)0)) (&intflist)->tqh_first->entry.tqe_prev
= &(intf)->entry.tqe_next; else (&intflist)->tqh_last
= &(intf)->entry.tqe_next; (&intflist)->tqh_first
= (intf); (intf)->entry.tqe_prev = &(&intflist)->
tqh_first; } while (0)
;
130 }
131
132 /* Signal disabled interface. */
133 if ((ifa->ifa_flags & IFF_UP0x1) == 0)
134 intf->dead = 1;
135
136 if (ifa->ifa_addr->sa_family == AF_LINK18) {
137 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
138 ifi = (struct if_data *)ifa->ifa_data;
139
140 /* Skip unsupported interfaces. */
141 if (ifi->ifi_type != IFT_ETHER0x06 &&
142 ifi->ifi_type != IFT_ENC0xf4 &&
143 ifi->ifi_type != IFT_CARP0xf7) {
144 TAILQ_REMOVE(&intflist, intf, entry)do { if (((intf)->entry.tqe_next) != ((void *)0)) (intf)->
entry.tqe_next->entry.tqe_prev = (intf)->entry.tqe_prev
; else (&intflist)->tqh_last = (intf)->entry.tqe_prev
; *(intf)->entry.tqe_prev = (intf)->entry.tqe_next; ; ;
} while (0)
;
145 free(intf);
146 continue;
147 }
148
149 if (ifi->ifi_type == IFT_ENC0xf4)
150 intf->hw_address.htype = HTYPE_IPSEC_TUNNEL31;
151 else
152 intf->hw_address.htype = HTYPE_ETHER1;
153
154 intf->index = sdl->sdl_index;
155 intf->hw_address.hlen = sdl->sdl_alen;
156 memcpy(intf->hw_address.haddr,
157 LLADDR(sdl)((caddr_t)((sdl)->sdl_data + (sdl)->sdl_nlen)), sdl->sdl_alen);
158 } else if (ifa->ifa_addr->sa_family == AF_INET2) {
159 sin = (struct sockaddr_in *)ifa->ifa_addr;
160 if (sin->sin_addr.s_addr == htonl(INADDR_LOOPBACK)(__uint32_t)(__builtin_constant_p(((u_int32_t)(0x7f000001))) ?
(__uint32_t)(((__uint32_t)(((u_int32_t)(0x7f000001))) & 0xff
) << 24 | ((__uint32_t)(((u_int32_t)(0x7f000001))) &
0xff00) << 8 | ((__uint32_t)(((u_int32_t)(0x7f000001))
) & 0xff0000) >> 8 | ((__uint32_t)(((u_int32_t)(0x7f000001
))) & 0xff000000) >> 24) : __swap32md(((u_int32_t)(
0x7f000001))))
||
161 intf->primary_address.s_addr != INADDR_ANY((u_int32_t)(0x00000000)))
162 continue;
163
164 intf->primary_address = sin->sin_addr;
165 } else if (ifa->ifa_addr->sa_family == AF_INET624) {
166 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
167 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)(((&sin6->sin6_addr)->__u6_addr.__u6_addr8[0] == 0xfe
) && (((&sin6->sin6_addr)->__u6_addr.__u6_addr8
[1] & 0xc0) == 0x80))
) {
168 intf->linklocal = sin6->sin6_addr;
169#ifdef __KAME__
170 /*
171 * Remove possible scope from address if
172 * link-local.
173 */
174 intf->linklocal.s6_addr__u6_addr.__u6_addr8[2] = 0;
175 intf->linklocal.s6_addr__u6_addr.__u6_addr8[3] = 0;
176#endif
177 } else
178 intf->gipv6 = 1;
179
180 /* At least one IPv6 address was found. */
181 intf->ipv6 = 1;
182 }
183 }
184
185 freeifaddrs(ifap);
186
187 /*
188 * Generate link-local IPv6 address for interfaces without it.
189 *
190 * For IPv6 DHCP Relay it doesn't matter what is used for
191 * link-addr field, so let's generate an address that won't
192 * change during execution so we can always find the interface
193 * to relay packets back. This is only used for layer 2 relaying
194 * when the interface might not have an address.
195 */
196 TAILQ_FOREACH(intf, &intflist, entry)for((intf) = ((&intflist)->tqh_first); (intf) != ((void
*)0); (intf) = ((intf)->entry.tqe_next))
{
197 if (memcmp(&intf->linklocal, &in6addr_any,
198 sizeof(in6addr_any)) != 0)
199 continue;
200
201 intf->linklocal.s6_addr__u6_addr.__u6_addr8[0] = 0xfe;
202 intf->linklocal.s6_addr__u6_addr.__u6_addr8[1] = 0x80;
203 intf->linklocal.s6_addr__u6_addr.__u6_addr8[8] = intf->hw_address.haddr[0];
204 intf->linklocal.s6_addr__u6_addr.__u6_addr8[9] = intf->hw_address.haddr[1];
205 intf->linklocal.s6_addr__u6_addr.__u6_addr8[10] = intf->hw_address.haddr[2];
206 intf->linklocal.s6_addr__u6_addr.__u6_addr8[11] = 0xff;
207 intf->linklocal.s6_addr__u6_addr.__u6_addr8[12] = 0xfe;
208 intf->linklocal.s6_addr__u6_addr.__u6_addr8[13] = intf->hw_address.haddr[3];
209 intf->linklocal.s6_addr__u6_addr.__u6_addr8[14] = intf->hw_address.haddr[4];
210 intf->linklocal.s6_addr__u6_addr.__u6_addr8[15] = intf->hw_address.haddr[5];
211 EUI64_TO_IFID(&intf->linklocal)do { (&intf->linklocal)->__u6_addr.__u6_addr8[8] ^=
0x02; } while (0)
;
212 }
213}
214
215struct interface_info *
216register_interface(const char *ifname, void (*handler)(struct protocol *),
217 int isserver)
218{
219 struct interface_info *intf;
220
221 if ((intf = iflist_getbyname(ifname)) == NULL((void *)0))
222 return NULL((void *)0);
223
224 /* Don't register disabled interfaces. */
225 if (intf->dead)
226 return NULL((void *)0);
227
228 /* Check if we already registered the interface. */
229 if (intf->ifr.ifr_name[0] != 0)
230 return intf;
231
232 if (strlcpy(intf->ifr.ifr_name, ifname,
233 sizeof(intf->ifr.ifr_name)) >= sizeof(intf->ifr.ifr_name))
234 fatalx("interface name '%s' too long", ifname);
235
236 if_register_receive(intf, isserver);
237 if_register_send(intf);
238 add_protocol(intf->name, intf->rfdesc, handler, intf);
239
240 return intf;
241}
242
243/*
244 * Wait for packets to come in using poll(). When a packet comes in,
245 * call receive_packet to receive the packet and possibly strip hardware
246 * addressing information from it, and then call through the
247 * bootp_packet_handler hook to try to do something with it.
248 */
249void
250dispatch(void)
251{
252 int count, i, to_msec, nfds = 0;
253 struct protocol *l;
254 struct pollfd *fds;
255 time_t howlong;
256
257 nfds = 0;
258 for (l = protocols; l; l = l->next)
1
Loop condition is false. Execution continues on line 261
259 nfds++;
260
261 fds = calloc(nfds, sizeof(struct pollfd));
2
Memory is allocated
262 if (fds == NULL((void *)0))
3
Assuming 'fds' is not equal to NULL
4
Taking false branch
263 fatalx("Can't allocate poll structures.");
264
265 do {
266 /*
267 * Call any expired timeouts, and then if there's still
268 * a timeout registered, time out the select call then.
269 */
270another:
271 if (timeouts) {
5
Assuming 'timeouts' is non-null
6
Taking true branch
10
Assuming 'timeouts' is null
11
Taking false branch
272 if (timeouts->when <= cur_time) {
7
Assuming 'cur_time' is >= field 'when'
8
Taking true branch
273 struct timeout *t = timeouts;
274
275 timeouts = timeouts->next;
276 (*(t->func))(t->what);
277 t->next = free_timeouts;
278 free_timeouts = t;
279 goto another;
9
Control jumps to line 271
280 }
281
282 /*
283 * Figure timeout in milliseconds, and check for
284 * potential overflow, so we can cram into an
285 * int for poll, while not polling with a
286 * negative timeout and blocking indefinitely.
287 */
288 howlong = timeouts->when - cur_time;
289 if (howlong > INT_MAX2147483647 / 1000)
290 howlong = INT_MAX2147483647 / 1000;
291 to_msec = howlong * 1000;
292 } else
293 to_msec = -1;
294
295 /* Set up the descriptors to be polled. */
296 i = 0;
297
298 for (l = protocols; l; l = l->next) {
12
Loop condition is true. Entering loop body
299 struct interface_info *ip = l->local;
300
301 if (ip && (l->handler != got_one || !ip->dead)) {
13
Assuming 'ip' is non-null
14
Assuming field 'handler' is not equal to got_one
302 fds[i].fd = l->fd;
15
Use of zero-allocated memory
303 fds[i].events = POLLIN0x0001;
304 fds[i].revents = 0;
305 i++;
306 }
307 }
308
309 if (i == 0)
310 fatalx("No live interfaces to poll on - exiting.");
311
312 /* Wait for a packet or a timeout... XXX */
313 count = poll(fds, nfds, to_msec);
314
315 /* Not likely to be transitory... */
316 if (count == -1) {
317 if (errno(*__errno()) == EAGAIN35 || errno(*__errno()) == EINTR4) {
318 time(&cur_time);
319 continue;
320 }
321 else
322 fatal("poll");
323 }
324
325 /* Get the current time... */
326 time(&cur_time);
327
328 i = 0;
329 for (l = protocols; l; l = l->next) {
330 struct interface_info *ip = l->local;
331
332 if ((fds[i].revents & (POLLIN0x0001 | POLLHUP0x0010))) {
333 fds[i].revents = 0;
334 if (ip && (l->handler != got_one ||
335 !ip->dead))
336 (*(l->handler))(l);
337 if (interfaces_invalidated)
338 break;
339 }
340 i++;
341 }
342 interfaces_invalidated = 0;
343 } while (1);
344}
345
346
347void
348got_one(struct protocol *l)
349{
350 struct packet_ctx pc;
351 ssize_t result;
352 union {
353 /*
354 * Packet input buffer. Must be as large as largest
355 * possible MTU.
356 */
357 unsigned char packbuf[4095];
358 struct dhcp_packet packet;
359 } u;
360 struct interface_info *ip = l->local;
361
362 memset(&pc, 0, sizeof(pc));
363
364 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &pc)) == -1) {
365 log_warn("receive_packet failed on %s", ip->name);
366 ip->errors++;
367 if ((!interface_status(ip)) ||
368 (ip->noifmedia && ip->errors > 20)) {
369 /* our interface has gone away. */
370 log_warnx("Interface %s no longer appears valid.",
371 ip->name);
372 ip->dead = 1;
373 interfaces_invalidated = 1;
374 close(l->fd);
375 remove_protocol(l);
376 free(ip);
377 }
378 return;
379 }
380 if (result == 0)
381 return;
382
383 if (bootp_packet_handler)
384 (*bootp_packet_handler)(ip, &u.packet, result, &pc);
385}
386
387int
388interface_status(struct interface_info *ifinfo)
389{
390 char *ifname = ifinfo->name;
391 int ifsock = ifinfo->rfdesc;
392 struct ifreq ifr;
393 struct ifmediareq ifmr;
394
395 /* get interface flags */
396 memset(&ifr, 0, sizeof(ifr));
397 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
398 if (ioctl(ifsock, SIOCGIFFLAGS(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct ifreq) & 0x1fff) << 16) | ((('i')) <<
8) | ((17)))
, &ifr) == -1) {
399 log_warn("ioctl(SIOCGIFFLAGS) on %s", ifname);
400 goto inactive;
401 }
402 /*
403 * if one of UP and RUNNING flags is dropped,
404 * the interface is not active.
405 */
406 if ((ifr.ifr_flagsifr_ifru.ifru_flags & (IFF_UP0x1|IFF_RUNNING0x40)) != (IFF_UP0x1|IFF_RUNNING0x40)) {
407 goto inactive;
408 }
409 /* Next, check carrier on the interface, if possible */
410 if (ifinfo->noifmedia)
411 goto active;
412 memset(&ifmr, 0, sizeof(ifmr));
413 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
414 if (ioctl(ifsock, SIOCGIFMEDIA(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct ifmediareq) & 0x1fff) << 16) | ((('i')) <<
8) | ((56)))
, (caddr_t)&ifmr) == -1) {
415 if (errno(*__errno()) != EINVAL22) {
416 log_debug("ioctl(SIOCGIFMEDIA) on %s", ifname);
417 ifinfo->noifmedia = 1;
418 goto active;
419 }
420 /*
421 * EINVAL (or ENOTTY) simply means that the interface
422 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
423 */
424 ifinfo->noifmedia = 1;
425 goto active;
426 }
427 if (ifmr.ifm_status & IFM_AVALID0x0000000000000001ULL) {
428 switch (ifmr.ifm_active & IFM_NMASK0x000000000000ff00ULL) {
429 case IFM_ETHER0x0000000000000100ULL:
430 if (ifmr.ifm_status & IFM_ACTIVE0x0000000000000002ULL)
431 goto active;
432 else
433 goto inactive;
434 break;
435 default:
436 goto inactive;
437 }
438 }
439inactive:
440 return (0);
441active:
442 return (1);
443}
444
445/* Add a protocol to the list of protocols... */
446void
447add_protocol(char *name, int fd, void (*handler)(struct protocol *),
448 void *local)
449{
450 struct protocol *p;
451
452 p = malloc(sizeof(*p));
453 if (!p)
454 fatalx("can't allocate protocol struct for %s", name);
455
456 p->fd = fd;
457 p->handler = handler;
458 p->local = local;
459 p->next = protocols;
460 protocols = p;
461}
462
463void
464remove_protocol(struct protocol *proto)
465{
466 struct protocol *p, *next, *prev;
467
468 prev = NULL((void *)0);
469 for (p = protocols; p; p = next) {
470 next = p->next;
471 if (p == proto) {
472 if (prev)
473 prev->next = p->next;
474 else
475 protocols = p->next;
476 free(p);
477 }
478 }
479}