Skip to content

Commit

Permalink
core: work around interface enum buffer overrun
Browse files Browse the repository at this point in the history
When a system has too many interfaces and too many addresses,
8 kilobytes isn't enough to fit all of the netlink responses.
As the result, kamailio gets stuck in a loop where it tries
to do a 0-length recv.

Increase the buffer to 32K. It's a miniscule amount for modern
times anyway. Also, add diagnostics to make further troubleshooting
easier.

Proper fix would be to switch to libnl here, which would make a
good weekend project.

(cherry picked from commit d2fd204)
  • Loading branch information
theraphim authored and miconda committed Feb 9, 2021
1 parent f64d41e commit 252b0dd
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/core/socket_info.c
Expand Up @@ -1070,6 +1070,7 @@ static int nl_bound_sock(void)
req.g.rtgen_family = family;\
} while(0);

#define NETLINK_BUFFER_SIZE 32768

static int get_flags(int family){
struct {
Expand All @@ -1079,7 +1080,7 @@ static int get_flags(int family){
int rtn = 0;
struct nlmsghdr* nlp;
struct ifinfomsg *ifi;
char buf[8192];
char buf[NETLINK_BUFFER_SIZE];
char *p = buf;
int nll = 0;
int nl_sock = -1;
Expand All @@ -1095,6 +1096,10 @@ static int get_flags(int family){
}

while(1) {
if ((sizeof(buf) - nll) == 0) {
LM_ERR("netlink buffer overflow in get_flags");
goto error;
}
rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
nlp = (struct nlmsghdr *) p;
if(nlp->nlmsg_type == NLMSG_DONE){
Expand Down Expand Up @@ -1148,7 +1153,7 @@ static int build_iface_list(void)
struct nlmsghdr* nlp;
struct ifaddrmsg *ifi;
int rtl;
char buf[8192];
char buf[NETLINK_BUFFER_SIZE];
char *p = buf;
int nll = 0;
struct rtattr * rtap;
Expand Down Expand Up @@ -1184,6 +1189,10 @@ static int build_iface_list(void)
nll = 0;
p = buf;
while(1) {
if ((sizeof(buf) - nll) == 0) {
LM_ERR("netlink buffer overflow in build_iface_list");
goto error;
}
rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
LM_DBG("received %d byles \n", rtn);
nlp = (struct nlmsghdr *) p;
Expand Down

0 comments on commit 252b0dd

Please sign in to comment.