DPDK patches and discussions
 help / color / mirror / Atom feed
From: Maxime Coquelin <maxime.coquelin@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>, dev@dpdk.org
Cc: Yuanhan Liu <yliu@fridaylinux.org>
Subject: Re: [dpdk-dev] [PATCH 2/8] vhost: avoid enum fields in VhostUserMsg
Date: Tue, 6 Feb 2018 10:47:31 +0100	[thread overview]
Message-ID: <b1bc5236-f20f-587b-c12d-9b66b87b17cc@redhat.com> (raw)
In-Reply-To: <20180205121642.26428-3-stefanha@redhat.com>



On 02/05/2018 01:16 PM, Stefan Hajnoczi wrote:
> The VhostUserMsg struct binary representation must match the vhost-user
> protocol specification since this struct is read from and written to the
> socket.
> 
> The VhostUserMsg.request union contains enum fields.  Enum binary
> representation is implementation-defined according to the C standard and
> it is unportable to make assumptions about the representation:
> 
>    6.7.2.2 Enumeration specifiers
>    ...
>    Each enumerated type shall be compatible with char, a signed integer
>    type, or an unsigned integer type. The choice of type is
>    implementation-defined, but shall be capable of representing the
>    values of all the members of the enumeration.
> 
> Additionally, librte_vhost relies on the enum type being unsigned when
> validating untrusted inputs:
> 
>    if (ret <= 0 || msg.request.master >= VHOST_USER_MAX) {
> 
> If msg.request.master is signed then negative values pass this check!
> 
> Even if we assume gcc on x86_64 (SysV amd64 ABI) and don't care about
> portability, the actual enum constants still affect the final type.  For
> example, if we add a negative constant then its type changes to signed
> int:
> 
>    typedef enum VhostUserRequest {
>        ...
>        VHOST_USER_INVALID = -1,
>    };
> 
> This is very fragile and it's unlikely that anyone changing the code
> would remember this.  A security hole can be introduced accidentally.
> 
> This patch switches VhostUserMsg.request fields to uint32_t to avoid the
> portability and potential security issues.
> 
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>   lib/librte_vhost/vhost_user.h | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/librte_vhost/vhost_user.h b/lib/librte_vhost/vhost_user.h
> index d4bd604b9..0fafbe6e0 100644
> --- a/lib/librte_vhost/vhost_user.h
> +++ b/lib/librte_vhost/vhost_user.h
> @@ -81,8 +81,8 @@ typedef struct VhostUserLog {
>   
>   typedef struct VhostUserMsg {
>   	union {
> -		VhostUserRequest master;
> -		VhostUserSlaveRequest slave;
> +		uint32_t master; /* a VhostUserRequest value */
> +		uint32_t slave;  /* a VhostUserSlaveRequest value*/
>   	} request;
>   
>   #define VHOST_USER_VERSION_MASK     0x3
> 

Maybe we could simplify to:

typedef struct VhostUserMsg {
  	uint32_t request; /* a VhostUserRequest or VhostUserSlaveRequest value */
...

Also, it seems QEMU's vhost-user master implementation uses an enum for
the request in its VhostUserMsg struct. Should it be fixed too?

Thanks,
Maxime

  reply	other threads:[~2018-02-06  9:47 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 [this message]
2018-02-05 12:16 ` [dpdk-dev] [PATCH 3/8] vhost: validate untrusted memory.nregions field Stefan Hajnoczi
2018-02-05 12:16 ` [dpdk-dev] [PATCH 4/8] vhost: clear out unused SCM_RIGHTS file descriptors Stefan Hajnoczi
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=b1bc5236-f20f-587b-c12d-9b66b87b17cc@redhat.com \
    --to=maxime.coquelin@redhat.com \
    --cc=dev@dpdk.org \
    --cc=stefanha@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).