vhost: improve truncated messages handling

Message ID 20230127135731.22499-1-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers
Series vhost: improve truncated messages handling |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-broadcom-Performance success Performance Testing PASS
ci/intel-Testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/github-robot: build success github build: passed
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-abi-testing success Testing PASS

Commit Message

Maxime Coquelin Jan. 27, 2023, 1:57 p.m. UTC
  This patch improves truncated messages logging to ease
debugging.

First, it differentiates between message's buffer
truncation and control data truncation. Indeed, MSG_CTRUNC
can happen even if enough room was provided, in case LSM
detects access rights issue.

Then, it does not return directly in case of truncation,
but returns normally to let a chance to display request
type in Vhost-user protocol layer logs.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 lib/vhost/socket.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
  

Comments

Maxime Coquelin Jan. 27, 2023, 4:30 p.m. UTC | #1
On 1/27/23 14:57, Maxime Coquelin wrote:
> This patch improves truncated messages logging to ease
> debugging.
> 
> First, it differentiates between message's buffer
> truncation and control data truncation. Indeed, MSG_CTRUNC
> can happen even if enough room was provided, in case LSM
> detects access rights issue.
> 
> Then, it does not return directly in case of truncation,
> but returns normally to let a chance to display request
> type in Vhost-user protocol layer logs.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>   lib/vhost/socket.c | 8 +++++---
>   1 file changed, 5 insertions(+), 3 deletions(-)

Discussing with David, there was/is a possible FDs leak when
truncation happens, a V2 is coming to handle these properly.

> diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
> index 863a6f6d52..669c322e12 100644
> --- a/lib/vhost/socket.c
> +++ b/lib/vhost/socket.c
> @@ -129,10 +129,12 @@ read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int m
>   		return ret;
>   	}
>   
> -	if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
> +	if (msgh.msg_flags & MSG_TRUNC)
>   		VHOST_LOG_CONFIG(ifname, ERR, "truncated msg (fd %d)\n", sockfd);
> -		return -1;
> -	}
> +
> +	/* MSG_CTRUNC may be caused by LSM misconfiguration */
> +	if (msgh.msg_flags & MSG_CTRUNC)
> +		VHOST_LOG_CONFIG(ifname, ERR, "truncated control data (fd %d)\n", sockfd);
>   
>   	for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
>   		cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
  

Patch

diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c
index 863a6f6d52..669c322e12 100644
--- a/lib/vhost/socket.c
+++ b/lib/vhost/socket.c
@@ -129,10 +129,12 @@  read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int m
 		return ret;
 	}
 
-	if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
+	if (msgh.msg_flags & MSG_TRUNC)
 		VHOST_LOG_CONFIG(ifname, ERR, "truncated msg (fd %d)\n", sockfd);
-		return -1;
-	}
+
+	/* MSG_CTRUNC may be caused by LSM misconfiguration */
+	if (msgh.msg_flags & MSG_CTRUNC)
+		VHOST_LOG_CONFIG(ifname, ERR, "truncated control data (fd %d)\n", sockfd);
 
 	for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
 		cmsg = CMSG_NXTHDR(&msgh, cmsg)) {