From: Roy Marples Date: Wed, 8 Jan 2020 20:13:20 +0000 (+0000) Subject: ioctl: The POSIX signature differs from BSD and glibc X-Git-Tag: v9.0.0~131 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa070df4e9ac982c9097fdfdbfde16ed3bd693b4;p=thirdparty%2Fdhcpcd.git ioctl: The POSIX signature differs from BSD and glibc BSD and glibc have the signature for request as unsigned long. musl and Solaris have a signed int. As such, we need to detect this at compile time and adjust the signature of our internal ioctl functions to match. To keep the onwire format the same, memcpy the request to the unsigned long request and back again, thus preserving the signedness. --- diff --git a/configure b/configure index 0cb27ce1..263afbf7 100755 --- a/configure +++ b/configure @@ -549,7 +549,7 @@ if [ "$PRIVSEP" = yes ]; then fi done fi - : ${PRIVSEP_USER:= _dhcpcd} + : ${PRIVSEP_USER:=_dhcpcd} echo "CPPFLAGS+= -DPRIVSEP" >>$CONFIG_MK echo "#ifndef PRIVSEP_USER" >>$CONFIG_H @@ -737,6 +737,27 @@ fi rm -f _clock_gettime.c _clock_gettime $abort && exit 1 +printf "Testing ioctl request type ... " +cat <_ioctl.c +#include +int main(void) { + unsigned long req = 0; + return ioctl(3, req, &req); +} +EOF +if $XCC _ioctl.c -o _ioctl 2>&3; then + IOCTL_REQ="unsigned long" +else + IOCTL_REQ="int" +fi +echo "$IOCTL_REQ" +# Our default is unsigned long +# We can still define it, but it makes the code path slightly bigger +if [ "$IOCTL_REQ" != "unsigned long" ]; then + echo "#define IOCTL_REQUEST_TYPE $IOCTL_REQ" >>$CONFIG_H +fi +rm -f _ioctl.c _ioctl + printf "Testing for inet_ntoa ... " cat <_inet_ntoa.c #include diff --git a/src/if.c b/src/if.c index 26324286..a96ac5c1 100644 --- a/src/if.c +++ b/src/if.c @@ -133,7 +133,7 @@ if_closesockets(struct dhcpcd_ctx *ctx) } int -if_ioctl(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len) +if_ioctl(struct dhcpcd_ctx *ctx, ioctl_request_t req, void *data, size_t len) { #ifdef PRIVSEP diff --git a/src/if.h b/src/if.h index 8165a498..e371e970 100644 --- a/src/if.h +++ b/src/if.h @@ -62,6 +62,16 @@ #endif #include "config.h" + +/* POSIX defines ioctl request as an int, which Solaris and musl use. + * Everyone else use an unsigned long, which happens to be the bigger one + * so we use that in our on wire API. */ +#ifdef IOCTL_REQUEST_TYPE +typedef IOCTL_REQUEST_TYPE ioctl_request_t; +#else +typedef unsigned long ioctl_request_t; +#endif + #include "dhcpcd.h" #include "ipv4.h" #include "ipv6.h" @@ -110,7 +120,7 @@ int if_getifaddrs(struct ifaddrs **); int if_getsubnet(struct dhcpcd_ctx *, const char *, int, void *, size_t); #endif -int if_ioctl(struct dhcpcd_ctx *, unsigned long, void *, size_t); +int if_ioctl(struct dhcpcd_ctx *, ioctl_request_t, void *, size_t); int if_getflags(struct interface *ifp); int if_setflag(struct interface *ifp, short flag); #define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING)) diff --git a/src/privsep-bpf.c b/src/privsep-bpf.c index 0985bd2e..bb6ee083 100644 --- a/src/privsep-bpf.c +++ b/src/privsep-bpf.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include diff --git a/src/privsep-inet.c b/src/privsep-inet.c index 2d5e30db..01761eb2 100644 --- a/src/privsep-inet.c +++ b/src/privsep-inet.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include diff --git a/src/privsep-root.c b/src/privsep-root.c index dd60a4ff..47363624 100644 --- a/src/privsep-root.c +++ b/src/privsep-root.c @@ -46,6 +46,8 @@ #include "privsep.h" #include "script.h" +__CTASSERT(sizeof(ioctl_request_t) <= sizeof(unsigned long)); + struct psr_error { ssize_t psr_result; @@ -130,7 +132,16 @@ ps_root_doioctl(unsigned long req, void *data, size_t len) s = socket(PF_INET, SOCK_DGRAM, 0); if (s != -1) +#ifdef IOCTL_REQUEST_TYPE + { + ioctl_request_t reqt; + + memcpy(&reqt, &req, sizeof(reqt)); + err = ioctl(s, reqt, data, len); + } +#else err = ioctl(s, req, data, len); +#endif else err = -1; if (s != -1) @@ -410,9 +421,18 @@ ps_root_script(const struct interface *ifp, const void *data, size_t len) } ssize_t -ps_root_ioctl(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len) +ps_root_ioctl(struct dhcpcd_ctx *ctx, ioctl_request_t req, void *data, + size_t len) { +#ifdef IOCTL_REQUEST_TYPE + unsigned long ulreq = 0; + + memcpy(&ulreq, &req, sizeof(req)); + if (ps_sendcmd(ctx, ctx->ps_root_fd, PS_IOCTL, ulreq, data, len) == -1) + return -1; +#else if (ps_sendcmd(ctx, ctx->ps_root_fd, PS_IOCTL, req, data, len) == -1) return -1; +#endif return ps_root_readerror(ctx); } diff --git a/src/privsep-root.h b/src/privsep-root.h index 4f6ac955..57862626 100644 --- a/src/privsep-root.h +++ b/src/privsep-root.h @@ -29,11 +29,13 @@ #ifndef PRIVSEP_ROOT_H #define PRIVSEP_ROOT_H +#include "if.h" + pid_t ps_root_start(struct dhcpcd_ctx *ctx); int ps_root_stop(struct dhcpcd_ctx *ctx); ssize_t ps_root_readerror(struct dhcpcd_ctx *); -ssize_t ps_root_ioctl(struct dhcpcd_ctx *, unsigned long, void *, size_t); +ssize_t ps_root_ioctl(struct dhcpcd_ctx *, ioctl_request_t, void *, size_t); ssize_t ps_root_os(struct ps_msghdr *, struct msghdr *); #if defined(BSD) || defined(__sun) ssize_t ps_root_route(struct dhcpcd_ctx *, void *, size_t);