From: Willy Tarreau Date: Thu, 5 Mar 2009 23:48:23 +0000 (+0100) Subject: [BUG] interface binding: length must include the trailing zero X-Git-Tag: v1.3.16-rc1~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=604e83097f77b911cc4af16ecdfe5be8d825c19d;p=thirdparty%2Fhaproxy.git [BUG] interface binding: length must include the trailing zero The interface length passed to the setsockopt(SO_BINDTODEVICE) must include the trailing \0. Otherwise it will randomly fail. --- diff --git a/src/backend.c b/src/backend.c index d386805ebb..f511b7799e 100644 --- a/src/backend.c +++ b/src/backend.c @@ -1723,7 +1723,7 @@ int connect_server(struct session *s) #ifdef SO_BINDTODEVICE /* Note: this might fail if not CAP_NET_RAW */ if (s->srv->iface_name) - setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, s->srv->iface_name, s->srv->iface_len); + setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, s->srv->iface_name, s->srv->iface_len + 1); #endif ret = tcpv4_bind_socket(fd, flags, &s->srv->source_addr, remote); if (ret) { @@ -1767,7 +1767,7 @@ int connect_server(struct session *s) #ifdef SO_BINDTODEVICE /* Note: this might fail if not CAP_NET_RAW */ if (s->be->iface_name) - setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, s->be->iface_name, s->be->iface_len); + setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, s->be->iface_name, s->be->iface_len + 1); #endif ret = tcpv4_bind_socket(fd, flags, &s->be->source_addr, remote); if (ret) { diff --git a/src/checks.c b/src/checks.c index 1acff0d840..37e0c29616 100644 --- a/src/checks.c +++ b/src/checks.c @@ -594,7 +594,7 @@ void process_chk(struct task *t, int *next) /* Note: this might fail if not CAP_NET_RAW */ if (s->iface_name) setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, - s->iface_name, s->iface_len); + s->iface_name, s->iface_len + 1); #endif ret = tcpv4_bind_socket(fd, flags, &s->source_addr, remote); if (ret) { @@ -625,7 +625,7 @@ void process_chk(struct task *t, int *next) /* Note: this might fail if not CAP_NET_RAW */ if (s->proxy->iface_name) setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, - s->proxy->iface_name, s->proxy->iface_len); + s->proxy->iface_name, s->proxy->iface_len + 1); #endif ret = tcpv4_bind_socket(fd, flags, &s->proxy->source_addr, remote); if (ret) { diff --git a/src/proto_tcp.c b/src/proto_tcp.c index 78f63fcea4..2fb6a85c70 100644 --- a/src/proto_tcp.c +++ b/src/proto_tcp.c @@ -245,7 +245,7 @@ int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) /* Note: this might fail if not CAP_NET_RAW */ if (listener->interface) { if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, - listener->interface, strlen(listener->interface)) == -1) { + listener->interface, strlen(listener->interface) + 1) == -1) { msg = "cannot bind listener to device"; err |= ERR_WARN; }