From 35c4c773cda1d8e43972dd1f7b1891207efbc15d Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sat, 13 Nov 2010 06:13:10 -0500 Subject: [PATCH] Remove unnecessary casts from setsockopt(). Prevent access to initialized memory in raw_socket()'s bind() call by using memset(). --- ndhc/packet.c | 4 ++-- ndhc/socket.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ndhc/packet.c b/ndhc/packet.c index d6c55e6..3e678d0 100644 --- a/ndhc/packet.c +++ b/ndhc/packet.c @@ -174,13 +174,13 @@ int raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port, uint32_t dest_ip, int dest_port) { - int n = 1, fd, result = -1; + int opt = 1, fd, result = -1; struct sockaddr_in client; if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) goto out; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) goto out_fd; memset(&client, 0, sizeof(client)); diff --git a/ndhc/socket.c b/ndhc/socket.c index b09a644..38373c1 100644 --- a/ndhc/socket.c +++ b/ndhc/socket.c @@ -91,7 +91,7 @@ int listen_socket(unsigned int ip, int port, char *inf) struct ifreq interface; int fd; struct sockaddr_in addr; - int n = 1; + int opt = 1; log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf); if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { @@ -99,19 +99,20 @@ int listen_socket(unsigned int ip, int port, char *inf) goto out; } - memset(&addr, 0, sizeof(addr)); + memset(&addr, 0, sizeof addr); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = ip; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof n) == -1) + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) goto out_fd; - if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *)&n, sizeof n) == -1) + if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof opt) == -1) goto out_fd; + /* Restrict operations to the physical device @inf */ strlcpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ); if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, - (char *)&interface, sizeof interface) < 0) + &interface, sizeof interface) < 0) goto out_fd; if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) @@ -129,6 +130,7 @@ int raw_socket(int ifindex) int fd; struct sockaddr_ll sock; + memset(&sock, 0, sizeof sock); log_line("Opening raw socket on ifindex %d", ifindex); if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { log_error("socket call failed: %s", strerror(errno)); @@ -138,7 +140,7 @@ int raw_socket(int ifindex) sock.sll_family = AF_PACKET; sock.sll_protocol = htons(ETH_P_IP); sock.sll_ifindex = ifindex; - if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) { + if (bind(fd, (struct sockaddr *)&sock, sizeof(sock)) < 0) { log_error("bind call failed: %s", strerror(errno)); goto out_fd; }