Skip to content

Commit 8128e30

Browse files
committed
utils: Ensure that the buffer for struct cmsghdr is suitably-aligned
A char array on the stack is not guaranteed to have any particular alignment. Resolves: #637 Signed-off-by: Simon McVittie <[email protected]>
1 parent a05614e commit 8128e30

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

utils.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,13 +758,16 @@ send_pid_on_socket (int sockfd)
758758
struct msghdr msg = {};
759759
struct iovec iov = { buf, sizeof (buf) };
760760
const ssize_t control_len_snd = CMSG_SPACE(sizeof(struct ucred));
761-
char control_buf_snd[control_len_snd];
761+
union {
762+
char buf[control_len_snd];
763+
struct cmsghdr align;
764+
} control_buf_snd;
762765
struct cmsghdr *cmsg;
763766
struct ucred cred;
764767

765768
msg.msg_iov = &iov;
766769
msg.msg_iovlen = 1;
767-
msg.msg_control = control_buf_snd;
770+
msg.msg_control = control_buf_snd.buf;
768771
msg.msg_controllen = control_len_snd;
769772

770773
cmsg = CMSG_FIRSTHDR(&msg);
@@ -800,12 +803,15 @@ read_pid_from_socket (int sockfd)
800803
struct msghdr msg = {};
801804
struct iovec iov = { recv_buf, sizeof (recv_buf) };
802805
const ssize_t control_len_rcv = CMSG_SPACE(sizeof(struct ucred));
803-
char control_buf_rcv[control_len_rcv];
806+
union {
807+
char buf[control_len_rcv];
808+
struct cmsghdr align;
809+
} control_buf_rcv;
804810
struct cmsghdr* cmsg;
805811

806812
msg.msg_iov = &iov;
807813
msg.msg_iovlen = 1;
808-
msg.msg_control = control_buf_rcv;
814+
msg.msg_control = control_buf_rcv.buf;
809815
msg.msg_controllen = control_len_rcv;
810816

811817
if (TEMP_FAILURE_RETRY (recvmsg (sockfd, &msg, 0)) < 0)

0 commit comments

Comments
 (0)