From c740bbe30affb7b94c6cca5f65a4e9047a1119ec Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Mon, 29 Jun 2020 09:39:22 +0200 Subject: swrap: let swrap_sendmsg_before_unix() create a copy of msg_tmp.msg_control With fd-passing we'll have to modify the content of it. Signed-off-by: Stefan Metzmacher Reviewed-by: Anoop C S Reviewed-by: Andreas Schneider --- src/socket_wrapper.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c index a0eef9d..fa18a09 100644 --- a/src/socket_wrapper.c +++ b/src/socket_wrapper.c @@ -5151,13 +5151,56 @@ static int swrap_sendmsg_filter_cmsg_sol_socket(const struct cmsghdr *cmsg, static int swrap_sendmsg_before_unix(const struct msghdr *_msg_in, struct msghdr *msg_tmp) { +#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL + struct msghdr *msg_in = discard_const_p(struct msghdr, _msg_in); + struct cmsghdr *cmsg = NULL; + uint8_t *cm_data = NULL; + size_t cm_data_space = 0; + int rc = -1; + + *msg_tmp = *msg_in; + + /* Nothing to do */ + if (msg_in->msg_controllen == 0 || msg_in->msg_control == NULL) { + return 0; + } + + for (cmsg = CMSG_FIRSTHDR(msg_in); + cmsg != NULL; + cmsg = CMSG_NXTHDR(msg_in, cmsg)) { + switch (cmsg->cmsg_level) { + default: + rc = swrap_sendmsg_copy_cmsg(cmsg, + &cm_data, + &cm_data_space); + break; + } + if (rc < 0) { + int saved_errno = errno; + SAFE_FREE(cm_data); + errno = saved_errno; + return rc; + } + } + + msg_tmp->msg_controllen = cm_data_space; + msg_tmp->msg_control = cm_data; + + return 0; +#else /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */ *msg_tmp = *_msg_in; return 0; +#endif /* ! HAVE_STRUCT_MSGHDR_MSG_CONTROL */ } static ssize_t swrap_sendmsg_after_unix(struct msghdr *msg_tmp, ssize_t ret) { +#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL + int saved_errno = errno; + SAFE_FREE(msg_tmp->msg_control); + errno = saved_errno; +#endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */ return ret; } -- cgit