1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-26 09:29:01 +00:00

Pad the device name of SO_BINDTODEVICE up to sizeof(int).

An apparent kernel bug in Linux 2.6.20 and earlier causes the
SO_BINDTODEVICE setsockopt to return EINVAL if the device name is too
short. Pad with null characters to avoid this.

http://article.gmane.org/gmane.linux.network/71887
http://article.gmane.org/gmane.linux.network/72216
This commit is contained in:
david
2012-11-16 21:02:59 +00:00
parent 641dddafb5
commit b40d0e2982

View File

@@ -284,6 +284,20 @@ int block_socket(int sd) {
/* Use the SO_BINDTODEVICE sockopt to bind with a specific interface (Linux
only). Pass NULL or an empty string to remove device binding. */
int socket_bindtodevice(int sd, const char *device) {
char padded[sizeof(int)];
/* In Linux 2.6.20 and earlier, there is a bug in SO_BINDTODEVICE that causes
EINVAL to be returned if the optlen < sizeof(int); this happens for example
with the interface names "" and "lo". Pad the string with null characters
so it is above this limit if necessary.
http://article.gmane.org/gmane.linux.network/71887
http://article.gmane.org/gmane.linux.network/72216 */
if (strlen(device) + 1 < sizeof(padded)) {
/* We rely on strncpy padding with nulls here. */
strncpy(padded, device, sizeof(padded));
device = padded;
}
#ifdef SO_BINDTODEVICE
/* Linux-specific sockopt asking to use a specific interface. See socket(7). */
if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device) + 1) < 0)