virtio: force spec specified alignment on types
authorMichael S. Tsirkin <mst@redhat.com>
Mon, 6 Apr 2020 12:42:55 +0000 (08:42 -0400)
committerMichael S. Tsirkin <mst@redhat.com>
Tue, 2 Jun 2020 06:45:13 +0000 (02:45 -0400)
The ring element addresses are passed between components with different
alignments assumptions. Thus, if guest/userspace selects a pointer and
host then gets and dereferences it, we might need to decrease the
compiler-selected alignment to prevent compiler on the host from
assuming pointer is aligned.

This actually triggers on ARM with -mabi=apcs-gnu - which is a
deprecated configuration, but it seems safer to handle this
generally.

Note that userspace that allocates the memory is actually OK and does
not need to be fixed, but userspace that gets it from guest or another
process does need to be fixed. The later doesn't generally talk to the
kernel so while it might be buggy it's not talking to the kernel in the
buggy way - it's just using the header in the buggy way - so fixing
header and asking userspace to recompile is the best we can do.

I verified that the produced kernel binary on x86 is exactly identical
before and after the change.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
drivers/vhost/vhost.c
drivers/vhost/vhost.h
drivers/vhost/vringh.c
include/linux/vringh.h
include/uapi/linux/virtio_ring.h

index 21a59b598ed87312685f60185a72374ee2f3fc9a..96d9871fa0cb50607914d121859c0127a3220d8a 100644 (file)
@@ -1244,9 +1244,9 @@ static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
 }
 
 static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
-                        struct vring_desc __user *desc,
-                        struct vring_avail __user *avail,
-                        struct vring_used __user *used)
+                        vring_desc_t __user *desc,
+                        vring_avail_t __user *avail,
+                        vring_used_t __user *used)
 
 {
        return access_ok(desc, vhost_get_desc_size(vq, num)) &&
@@ -2301,7 +2301,7 @@ static int __vhost_add_used_n(struct vhost_virtqueue *vq,
                            struct vring_used_elem *heads,
                            unsigned count)
 {
-       struct vring_used_elem __user *used;
+       vring_used_elem_t __user *used;
        u16 old, new;
        int start;
 
index f8403bd46b85dd5d0183baee2ef650cdd37e0fa6..60cab4c782296f085ab9beb4597e2f59739a7833 100644 (file)
@@ -67,9 +67,9 @@ struct vhost_virtqueue {
        /* The actual ring of buffers. */
        struct mutex mutex;
        unsigned int num;
-       struct vring_desc __user *desc;
-       struct vring_avail __user *avail;
-       struct vring_used __user *used;
+       vring_desc_t __user *desc;
+       vring_avail_t __user *avail;
+       vring_used_t __user *used;
        const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
        struct file *kick;
        struct eventfd_ctx *call_ctx;
index ba8e0d6cfd9748dfdd39fa28865287a578f4e960..e059a9a47cdf1df0e76850b35b57819241bebc4e 100644 (file)
@@ -620,9 +620,9 @@ static inline int xfer_to_user(const struct vringh *vrh,
  */
 int vringh_init_user(struct vringh *vrh, u64 features,
                     unsigned int num, bool weak_barriers,
-                    struct vring_desc __user *desc,
-                    struct vring_avail __user *avail,
-                    struct vring_used __user *used)
+                    vring_desc_t __user *desc,
+                    vring_avail_t __user *avail,
+                    vring_used_t __user *used)
 {
        /* Sane power of 2 please! */
        if (!num || num > 0xffff || (num & (num - 1))) {
index 9e2763d7c15912e0a77f7ca2e90e2ae836347c08..59bd50f99291badb2903d9734d09bc960ff2fa7e 100644 (file)
@@ -105,9 +105,9 @@ struct vringh_kiov {
 /* Helpers for userspace vrings. */
 int vringh_init_user(struct vringh *vrh, u64 features,
                     unsigned int num, bool weak_barriers,
-                    struct vring_desc __user *desc,
-                    struct vring_avail __user *avail,
-                    struct vring_used __user *used);
+                    vring_desc_t __user *desc,
+                    vring_avail_t __user *avail,
+                    vring_used_t __user *used);
 
 static inline void vringh_iov_init(struct vringh_iov *iov,
                                   struct iovec *iovec, unsigned num)
index 9223c3a5c46ae8c817e49ba874ea6ae41134df7b..476d3e5c0fe7024201b7de17a074388fbb8237f6 100644 (file)
  * at the end of the used ring. Guest should ignore the used->flags field. */
 #define VIRTIO_RING_F_EVENT_IDX                29
 
+/* Alignment requirements for vring elements.
+ * When using pre-virtio 1.0 layout, these fall out naturally.
+ */
+#define VRING_AVAIL_ALIGN_SIZE 2
+#define VRING_USED_ALIGN_SIZE 4
+#define VRING_DESC_ALIGN_SIZE 16
+
 /* Virtio ring descriptors: 16 bytes.  These can chain together via "next". */
 struct vring_desc {
        /* Address (guest-physical). */
@@ -112,29 +119,46 @@ struct vring_used_elem {
        __virtio32 len;
 };
 
+typedef struct vring_used_elem __attribute__((aligned(VRING_USED_ALIGN_SIZE)))
+       vring_used_elem_t;
+
 struct vring_used {
        __virtio16 flags;
        __virtio16 idx;
-       struct vring_used_elem ring[];
+       vring_used_elem_t ring[];
 };
 
+/*
+ * The ring element addresses are passed between components with different
+ * alignments assumptions. Thus, we might need to decrease the compiler-selected
+ * alignment, and so must use a typedef to make sure the aligned attribute
+ * actually takes hold:
+ *
+ * https://gcc.gnu.org/onlinedocs//gcc/Common-Type-Attributes.html#Common-Type-Attributes
+ *
+ * When used on a struct, or struct member, the aligned attribute can only
+ * increase the alignment; in order to decrease it, the packed attribute must
+ * be specified as well. When used as part of a typedef, the aligned attribute
+ * can both increase and decrease alignment, and specifying the packed
+ * attribute generates a warning.
+ */
+typedef struct vring_desc __attribute__((aligned(VRING_DESC_ALIGN_SIZE)))
+       vring_desc_t;
+typedef struct vring_avail __attribute__((aligned(VRING_AVAIL_ALIGN_SIZE)))
+       vring_avail_t;
+typedef struct vring_used __attribute__((aligned(VRING_USED_ALIGN_SIZE)))
+       vring_used_t;
+
 struct vring {
        unsigned int num;
 
-       struct vring_desc *desc;
+       vring_desc_t *desc;
 
-       struct vring_avail *avail;
+       vring_avail_t *avail;
 
-       struct vring_used *used;
+       vring_used_t *used;
 };
 
-/* Alignment requirements for vring elements.
- * When using pre-virtio 1.0 layout, these fall out naturally.
- */
-#define VRING_AVAIL_ALIGN_SIZE 2
-#define VRING_USED_ALIGN_SIZE 4
-#define VRING_DESC_ALIGN_SIZE 16
-
 #ifndef VIRTIO_RING_NO_LEGACY
 
 /* The standard layout for the ring is a continuous chunk of memory which looks