Discussion:
[Dnsmasq-discuss] [PATCH] simplify bindtodevice()
Kurt H Maier
2017-12-21 04:01:10 UTC
Permalink
Right now bindtodevice() declares an ifreq, copies the device name into
it, and passes a pointer to the entire structure as the optval for
setsockopt. This only works because ifr_name happens to be the first
element in the ifreq data structure. What actually gets passed to
setsockopt looks like
"wlp3s0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\255nzHmU\0\0"...
and is 40 bytes long.

We could change it so we pass ifr.ifr_name, but this is wasteful too,
since device is already in the format we want.

Attached is a patch that uses device directly, and passes IFNAMSIZ as
optlen.

Thanks,
khm


diff --git a/src/dhcp-common.c b/src/dhcp-common.c
index eae9ae3..8e128fa 100644
--- a/src/dhcp-common.c
+++ b/src/dhcp-common.c
@@ -485,11 +485,8 @@ char *whichdevice(void)

void bindtodevice(char *device, int fd)
{
- struct ifreq ifr;
-
- strcpy(ifr.ifr_name, device);
/* only allowed by root. */
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) == -1 &&
+ if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, device, IFNAMSIZ) == -1 &&
errno != EPERM)
die(_("failed to set SO_BINDTODEVICE on DHCP socket: %s"), NULL, EC_BADNET);
}
Simon Kelley
2018-01-26 15:22:54 UTC
Permalink
Patch applied. Many thanks.


Cheers,

Simon.
Post by Kurt H Maier
Right now bindtodevice() declares an ifreq, copies the device name into
it, and passes a pointer to the entire structure as the optval for
setsockopt. This only works because ifr_name happens to be the first
element in the ifreq data structure. What actually gets passed to
setsockopt looks like
"wlp3s0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\255nzHmU\0\0"...
and is 40 bytes long.
We could change it so we pass ifr.ifr_name, but this is wasteful too,
since device is already in the format we want.
Attached is a patch that uses device directly, and passes IFNAMSIZ as
optlen.
Thanks,
khm
diff --git a/src/dhcp-common.c b/src/dhcp-common.c
index eae9ae3..8e128fa 100644
--- a/src/dhcp-common.c
+++ b/src/dhcp-common.c
@@ -485,11 +485,8 @@ char *whichdevice(void)
void bindtodevice(char *device, int fd)
{
- struct ifreq ifr;
-
- strcpy(ifr.ifr_name, device);
/* only allowed by root. */
- if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) == -1 &&
+ if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, device, IFNAMSIZ) == -1 &&
errno != EPERM)
die(_("failed to set SO_BINDTODEVICE on DHCP socket: %s"), NULL, EC_BADNET);
}
_______________________________________________
Dnsmasq-discuss mailing list
http://lists.thekelleys.org.uk/mailman/listinfo/dnsmasq-discuss
Loading...