skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit b2eac2dedd62224df07148a9e42eae0197902c00
parent e48c16f37bac55820c605f82c07f04436a55f415
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Sun, 23 Apr 2017 00:00:10 +0000

 Align ancillary buffers on struct cmsghdr and int boundaries

Diffstat:
MINSTALL | 2+-
Msrc/libunixonacid/unixmessage_receive.c | 10++++++++--
Msrc/libunixonacid/unixmessage_sender_flush.c | 13++++++++++---
Msrc/sysdeps/output-types.c | 2++
Msrc/sysdeps/tryancilautoclose.c | 14++++++++++----
5 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/INSTALL b/INSTALL @@ -119,7 +119,7 @@ HOST-ar, etc.) are accessible via your PATH environment variable * give the correct --host option * for --prefix, --bindir and other paths, always use the run-time path * to install the cross-compiled package in a staging directory /your/stage, - perform "make install DESTDIR=/your/staging/directory" + perform "make install DESTDIR=/your/stage" * The slashpackage convention diff --git a/src/libunixonacid/unixmessage_receive.c b/src/libunixonacid/unixmessage_receive.c @@ -14,6 +14,12 @@ #include <skalibs/stralloc.h> #include <skalibs/unixmessage.h> +union aligner_u +{ + struct cmsghdr cmsghdr ; + int i ; +} ; + static int const awesomeflags = #ifdef SKALIBS_HASMSGDONTWAIT MSG_DONTWAIT @@ -36,7 +42,7 @@ static int const awesomeflags = static int unixmessage_receiver_fill (unixmessage_receiver_t *b) { - char ancilbuf[CMSG_SPACE(b->auxb.a - 1)] ; + union aligner_u ancilbuf[1 + CMSG_SPACE(b->auxb.a - 1) / sizeof(union aligner_u)] ; struct iovec iov[2] ; struct msghdr msghdr = { @@ -46,7 +52,7 @@ static int unixmessage_receiver_fill (unixmessage_receiver_t *b) .msg_iovlen = 2, .msg_flags = 0, .msg_control = b->fds_ok & 1 ? ancilbuf : 0, - .msg_controllen = b->fds_ok & 1 ? sizeof(ancilbuf) : 0 + .msg_controllen = b->fds_ok & 1 ? CMSG_SPACE(b->auxb.a - 1) : 0 } ; ssize_t r = -1 ; if (cbuffer_isfull(&b->mainb) || ((b->fds_ok & 1) && cbuffer_isfull(&b->auxb))) diff --git a/src/libunixonacid/unixmessage_sender_flush.c b/src/libunixonacid/unixmessage_sender_flush.c @@ -21,6 +21,13 @@ #define MSG_NOSIGNAL 0 #endif +union aligner_u +{ + struct cmsghdr cmsghdr ; + int i ; +} ; + + /* XXX: sendmsg/recvmsg is badly, badly specified. XXX: We assume ancillary data is attached to the first byte. @@ -68,7 +75,7 @@ int unixmessage_sender_flush (unixmessage_sender_t *b) { .iov_base = pack, .iov_len = 6 }, { .iov_base = b->data.s + offsets[b->head].left, .iov_len = len } } ; - char ancilbuf[CMSG_SPACE(nfds * sizeof(int))] ; + union aligner_u ancilbuf[1 + CMSG_SPACE(nfds * sizeof(int)) / sizeof(union aligner_u)] ; struct msghdr hdr = { .msg_name = 0, @@ -76,7 +83,7 @@ int unixmessage_sender_flush (unixmessage_sender_t *b) .msg_iov = v, .msg_iovlen = 2, .msg_control = nfds ? ancilbuf : 0, - .msg_controllen = nfds ? sizeof(ancilbuf) : 0 + .msg_controllen = nfds ? CMSG_SPACE(nfds * sizeof(int)) : 0 } ; uint32_pack_big(pack, (uint32_t)len) ; uint16_pack_big(pack + 4, (uint16_t)nfds) ; @@ -84,7 +91,7 @@ int unixmessage_sender_flush (unixmessage_sender_t *b) { struct cmsghdr *cp = CMSG_FIRSTHDR(&hdr) ; size_t i = 0 ; - memset(ancilbuf, 0, sizeof(ancilbuf)) ; + memset(hdr.msg_control, 0, hdr.msg_controllen) ; cp->cmsg_level = SOL_SOCKET ; cp->cmsg_type = SCM_RIGHTS ; cp->cmsg_len = CMSG_LEN(nfds * sizeof(int)) ; diff --git a/src/sysdeps/output-types.c b/src/sysdeps/output-types.c @@ -1,3 +1,5 @@ +/* ISC license. */ + #include <sys/types.h> #include <stdio.h> diff --git a/src/sysdeps/tryancilautoclose.c b/src/sysdeps/tryancilautoclose.c @@ -17,9 +17,15 @@ #include <sys/socket.h> #include <sys/uio.h> +union aligner_u +{ + struct cmsghdr cmsghdr ; + int i ; +} ; + static int ancil_send_fd (int sock, int fd) { - char ancilbuf[CMSG_SPACE(sizeof(int))] ; + union aligner_u ancilbuf[1 + CMSG_SPACE(sizeof(int)) / sizeof(union aligner_u)] ; char s[8] = "blahblah" ; struct iovec v = { .iov_base = s, .iov_len = 8 } ; struct msghdr msghdr = @@ -30,7 +36,7 @@ static int ancil_send_fd (int sock, int fd) .msg_iovlen = 1, .msg_flags = 0, .msg_control = ancilbuf, - .msg_controllen = sizeof(ancilbuf) + .msg_controllen = CMSG_SPACE(sizeof(int)) } ; struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr) ; cmsg->cmsg_len = CMSG_LEN(sizeof(int)) ; @@ -43,7 +49,7 @@ static int ancil_send_fd (int sock, int fd) static int ancil_recv_fd (int sock) { - char ancilbuf[CMSG_SPACE(sizeof(int))] ; + union aligner_u ancilbuf[1 + CMSG_SPACE(sizeof(int)) / sizeof(union aligner_u)] ; char s[8] ; struct iovec v = { .iov_base = s, .iov_len = 8 } ; struct msghdr msghdr = @@ -54,7 +60,7 @@ static int ancil_recv_fd (int sock) .msg_iovlen = 1, .msg_flags = 0, .msg_control = ancilbuf, - .msg_controllen = sizeof(ancilbuf) + .msg_controllen = CMSG_SPACE(sizeof(int)) } ; struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msghdr) ; cmsg->cmsg_len = msghdr.msg_controllen ;