Skip to content

Commit

Permalink
clockdiff, rdisc, ninfod: get rid of vla's
Browse files Browse the repository at this point in the history
VLA's are slow and can lead to security issues, it is best to avoid these.

Reference: https://lwn.net/Articles/763641/
Signed-off-by: Sami Kerola <[email protected]>
  • Loading branch information
kerolasa committed Feb 3, 2019
1 parent b6f2dab commit 6dd4bdc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 5 additions & 2 deletions clockdiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,11 @@ int main(int argc, char **argv)
if (ctl.ip_opt_len) {
struct sockaddr_in myaddr = { 0 };
socklen_t addrlen = sizeof(myaddr);
unsigned char rspace[ctl.ip_opt_len];
uint8_t *rspace;

memset(rspace, 0, sizeof(rspace));
if ((rspace = calloc(ctl.ip_opt_len, sizeof(uint8_t))) == NULL)
error(1, errno, "allocating %zu bytes failed",
ctl.ip_opt_len * sizeof(uint8_t));
rspace[0] = IPOPT_TIMESTAMP;
rspace[1] = ctl.ip_opt_len;
rspace[2] = 5;
Expand All @@ -550,6 +552,7 @@ int main(int argc, char **argv)
error(0, errno, "IP_OPTIONS (fallback to icmp tstamps)");
ctl.ip_opt_len = 0;
}
free(rspace);
}

measure_status = measure(&ctl);
Expand Down
2 changes: 1 addition & 1 deletion ninfod/ni_ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ int ni_ifaddrs(struct ni_ifaddrs **ifap, sa_family_t family)
#endif

if (build) {
data = calloc(1, NLMSG_ALIGN(sizeof(struct ni_ifaddrs[icnt]))
data = calloc(1, NLMSG_ALIGN(sizeof(struct ni_ifaddrs) * icnt)
+ dlen + xlen);
ifa = (struct ni_ifaddrs *) data;
if (ifap != NULL)
Expand Down
10 changes: 7 additions & 3 deletions rdisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,13 +1212,15 @@ join(int sock, struct sockaddr_in *sin)
{
int i, j;
struct ip_mreqn mreq;
int joined[num_interfaces];

memset(joined, 0, sizeof(joined));
int *joined;

if (isbroadcast(sin))
return (0);

if ((joined = calloc(num_interfaces, sizeof(int))) == NULL) {
logperror("cannot allocate memory");
return (-1);
}
mreq.imr_multiaddr = sin->sin_addr;
for (i = 0; i < num_interfaces; i++) {
for (j = 0; j < i; j++) {
Expand All @@ -1234,11 +1236,13 @@ join(int sock, struct sockaddr_in *sin)
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(char *)&mreq, sizeof(mreq)) < 0) {
logperror("setsockopt (IP_ADD_MEMBERSHIP)");
free(joined);
return (-1);
}

joined[i] = interfaces[i].ifindex;
}
free(joined);
return (0);
}

Expand Down

0 comments on commit 6dd4bdc

Please sign in to comment.