Skip to content

Commit

Permalink
sitnl: move large memory block from stack to heap
Browse files Browse the repository at this point in the history
It is not recommended to allocate big blocks on the stack, however
the sitnl sending routine is stacking a 16KB large buffer.

Allocate it using heap memory and avoid using the stack.

Addresses-Coverity: ("Large stack use")
Signed-off-by: Antonio Quartulli <antonio@openvpn.net>
  • Loading branch information
ordex committed May 24, 2021
1 parent 8d49172 commit 99f9049
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions openvpn/tun/linux/client/sitnl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,12 @@ namespace openvpn {
sitnl_parse_reply_cb cb, void *arg_cb)
{
int len, rem_len, fd, ret, rcv_len;
const size_t buf_len = 16 * 1024;
struct sockaddr_nl nladdr = { };
struct nlmsgerr *err;
struct nlmsghdr *h;
unsigned int seq;
char buf[1024 * 16];
void *buf = NULL;
struct iovec iov =
{
.iov_base = payload,
Expand Down Expand Up @@ -265,7 +266,13 @@ namespace openvpn {
}

/* prepare buffer to store RTNL replies */
memset(buf, 0, sizeof(buf));
buf = calloc(1, buf_len);
if (!buf)
{
ret = -ENOMEM;
goto out;
}

iov.iov_base = buf;

while (1)
Expand All @@ -275,7 +282,7 @@ namespace openvpn {
* using it again
*/
OPENVPN_LOG_RTNL(__func__ << ": checking for received messages");
iov.iov_len = sizeof(buf);
iov.iov_len = buf_len;
rcv_len = recvmsg(fd, &nlmsg, 0);
OPENVPN_LOG_RTNL(__func__ << ": rtnl: received " << rcv_len << " bytes");
if (rcv_len < 0)
Expand Down Expand Up @@ -394,6 +401,7 @@ namespace openvpn {
}
out:
close(fd);
free(buf);

return ret;
}
Expand Down

0 comments on commit 99f9049

Please sign in to comment.