DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: dev@dpdk.org
Cc: Maxime Coquelin <maxime.coquelin@redhat.com>,
	Yuanhan Liu <yliu@fridaylinux.org>,
	Stefan Hajnoczi <stefanha@redhat.com>
Subject: [dpdk-dev] [PATCH 4/8] vhost: clear out unused SCM_RIGHTS file descriptors
Date: Mon,  5 Feb 2018 12:16:38 +0000	[thread overview]
Message-ID: <20180205121642.26428-5-stefanha@redhat.com> (raw)
In-Reply-To: <20180205121642.26428-1-stefanha@redhat.com>

The number of file descriptors received is not stored by vhost_user.c.
vhost_user_set_mem_table() assumes that memory.nregions matches the
number of file descriptors received, but nothing guarantees this:

  for (i = 0; i < memory.nregions; i++)
      close(pmsg->fds[i]);

Another questionable code snippet is:

  case VHOST_USER_SET_LOG_FD:
      close(msg.fds[0]);

If not enough file descriptors were received then fds[] contains
uninitialized data from the stack (see read_fd_message()).  This might
cause non-vhost file descriptors to be closed if the uninitialized data
happens to match.

Refactoring vhost_user.c to pass around and check the number of file
descriptors everywhere would make the code more complex.  It is simpler
for read_fd_message() to set unused elements in fds[] to -1.  This way
close(-1) is called and no harm is done.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 lib/librte_vhost/socket.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c
index 6e3857e7a..4e3f8abb9 100644
--- a/lib/librte_vhost/socket.c
+++ b/lib/librte_vhost/socket.c
@@ -96,6 +96,7 @@ read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
 	size_t fdsize = fd_num * sizeof(int);
 	char control[CMSG_SPACE(fdsize)];
 	struct cmsghdr *cmsg;
+	int got_fds = 0;
 	int ret;
 
 	memset(&msgh, 0, sizeof(msgh));
@@ -122,11 +123,16 @@ read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
 		cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
 		if ((cmsg->cmsg_level == SOL_SOCKET) &&
 			(cmsg->cmsg_type == SCM_RIGHTS)) {
-			memcpy(fds, CMSG_DATA(cmsg), fdsize);
+			got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+			memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
 			break;
 		}
 	}
 
+	/* Clear out unused file descriptors */
+	while (got_fds < fd_num)
+		fds[got_fds++] = -1;
+
 	return ret;
 }
 
-- 
2.14.3

  parent reply	other threads:[~2018-02-05 12:17 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-05 12:16 [dpdk-dev] [PATCH 0/8] vhost: input validation enhancements Stefan Hajnoczi
2018-02-05 12:16 ` [dpdk-dev] [PATCH 1/8] vhost: add security model documentation to vhost_user.c Stefan Hajnoczi
2018-02-06 13:26   ` Kovacevic, Marko
     [not found]     ` <20180206142235.GB13343@stefanha-x1.localdomain>
2018-02-06 15:39       ` Kovacevic, Marko
2018-02-07 16:10       ` Mcnamara, John
2018-02-07 16:18         ` Maxime Coquelin
2018-02-07 17:23           ` Mcnamara, John
2018-02-05 12:16 ` [dpdk-dev] [PATCH 2/8] vhost: avoid enum fields in VhostUserMsg Stefan Hajnoczi
2018-02-06  9:47   ` Maxime Coquelin
2018-02-05 12:16 ` [dpdk-dev] [PATCH 3/8] vhost: validate untrusted memory.nregions field Stefan Hajnoczi
2018-02-05 12:16 ` Stefan Hajnoczi [this message]
2018-02-05 12:16 ` [dpdk-dev] [PATCH 5/8] vhost: reject invalid log base mmap_offset values Stefan Hajnoczi
2018-02-05 12:16 ` [dpdk-dev] [PATCH 6/8] vhost: fix msg->payload union typo in vhost_user_set_vring_addr() Stefan Hajnoczi
2018-02-05 12:16 ` [dpdk-dev] [PATCH 7/8] vhost: validate virtqueue size Stefan Hajnoczi
2018-02-05 12:16 ` [dpdk-dev] [PATCH 8/8] vhost: check for memory_size + mmap_offset overflow Stefan Hajnoczi
2018-02-06  9:32 ` [dpdk-dev] [PATCH 0/8] vhost: input validation enhancements Maxime Coquelin
2018-02-06 10:01 ` Maxime Coquelin
2018-02-19 13:52 ` Maxime Coquelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180205121642.26428-5-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=dev@dpdk.org \
    --cc=maxime.coquelin@redhat.com \
    --cc=yliu@fridaylinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).