Skip to content

Commit

Permalink
utils: Ensure that the buffer for struct cmsghdr is suitably-aligned
Browse files Browse the repository at this point in the history
A char array on the stack is not guaranteed to have any particular
alignment.

Resolves: #637
Signed-off-by: Simon McVittie <smcv@collabora.com>
  • Loading branch information
smcv committed Oct 16, 2024
1 parent a05614e commit 8128e30
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,13 +758,16 @@ send_pid_on_socket (int sockfd)
struct msghdr msg = {};
struct iovec iov = { buf, sizeof (buf) };
const ssize_t control_len_snd = CMSG_SPACE(sizeof(struct ucred));
char control_buf_snd[control_len_snd];
union {
char buf[control_len_snd];
struct cmsghdr align;
} control_buf_snd;
struct cmsghdr *cmsg;
struct ucred cred;

msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control_buf_snd;
msg.msg_control = control_buf_snd.buf;
msg.msg_controllen = control_len_snd;

cmsg = CMSG_FIRSTHDR(&msg);
Expand Down Expand Up @@ -800,12 +803,15 @@ read_pid_from_socket (int sockfd)
struct msghdr msg = {};
struct iovec iov = { recv_buf, sizeof (recv_buf) };
const ssize_t control_len_rcv = CMSG_SPACE(sizeof(struct ucred));
char control_buf_rcv[control_len_rcv];
union {
char buf[control_len_rcv];
struct cmsghdr align;
} control_buf_rcv;
struct cmsghdr* cmsg;

msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control_buf_rcv;
msg.msg_control = control_buf_rcv.buf;
msg.msg_controllen = control_len_rcv;

if (TEMP_FAILURE_RETRY (recvmsg (sockfd, &msg, 0)) < 0)
Expand Down

0 comments on commit 8128e30

Please sign in to comment.