Remove unnecessary casts from setsockopt().

Prevent access to initialized memory in raw_socket()'s bind() call by
using memset().
This commit is contained in:
Nicholas J. Kain 2010-11-13 06:13:10 -05:00
parent f59ccfb5b0
commit 35c4c773cd
2 changed files with 10 additions and 8 deletions

View File

@ -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 kernel_packet(struct dhcpMessage *payload, uint32_t source_ip,
int source_port, uint32_t dest_ip, int dest_port) 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; struct sockaddr_in client;
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
goto out; 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; goto out_fd;
memset(&client, 0, sizeof(client)); memset(&client, 0, sizeof(client));

View File

@ -91,7 +91,7 @@ int listen_socket(unsigned int ip, int port, char *inf)
struct ifreq interface; struct ifreq interface;
int fd; int fd;
struct sockaddr_in addr; struct sockaddr_in addr;
int n = 1; int opt = 1;
log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf); log_line("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { 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; goto out;
} }
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
addr.sin_addr.s_addr = ip; 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; 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; goto out_fd;
/* Restrict operations to the physical device @inf */
strlcpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ); strlcpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
(char *)&interface, sizeof interface) < 0) &interface, sizeof interface) < 0)
goto out_fd; goto out_fd;
if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1)
@ -129,6 +130,7 @@ int raw_socket(int ifindex)
int fd; int fd;
struct sockaddr_ll sock; struct sockaddr_ll sock;
memset(&sock, 0, sizeof sock);
log_line("Opening raw socket on ifindex %d", ifindex); log_line("Opening raw socket on ifindex %d", ifindex);
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
log_error("socket call failed: %s", strerror(errno)); log_error("socket call failed: %s", strerror(errno));
@ -138,7 +140,7 @@ int raw_socket(int ifindex)
sock.sll_family = AF_PACKET; sock.sll_family = AF_PACKET;
sock.sll_protocol = htons(ETH_P_IP); sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex; 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)); log_error("bind call failed: %s", strerror(errno));
goto out_fd; goto out_fd;
} }