All of lore.kernel.org
 help / color / mirror / Atom feed
From: Julien Grall <julien.grall@linaro.org>
To: xen-devel@lists.xenproject.org
Cc: Keir Fraser <keir@xen.org>,
	ian.campbell@citrix.com, tim@xen.org,
	Julien Grall <julien.grall@linaro.org>,
	Ian Jackson <ian.jackson@eu.citrix.com>,
	stefano.stabellini@citrix.com, Jan Beulich <jbeulich@suse.com>,
	Daniel De Graaf <dgdegra@tycho.nsa.gov>
Subject: [PATCH v3 04/24] xen: guestcopy: Provide an helper to safely copy string from guest
Date: Tue, 13 Jan 2015 14:25:13 +0000	[thread overview]
Message-ID: <1421159133-31526-5-git-send-email-julien.grall@linaro.org> (raw)
In-Reply-To: <1421159133-31526-1-git-send-email-julien.grall@linaro.org>

Flask code already provides an helper to copy a string from guest. In a later
patch, the new DT hypercalls will need a similar function.

To avoid code duplication, copy the flask helper (flask_copying_string) to
common code:
    - Rename into safe_copy_string_from_guest
    - Add comment to explain the extra +1
    - Return directly the buffer and use the macros provided by
    xen/err.h to return an error code if necessary.

Signed-off-by: Julien Grall <julien.grall@linaro.org>
Cc: Daniel De Graaf <dgdegra@tycho.nsa.gov>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Keir Fraser <keir@xen.org>

---
    Changes in v3:
        - Use macros of xen/err.h to return either the buffer or an
        error code
        - Reuse size_t instead of unsigned long
        - Update comment and commit message

    Changes in v2:
        - Rename copy_string_from_guest into safe_copy_string_from_guest
        - Update commit message and comment in the code
---
 xen/common/Makefile            |  1 +
 xen/common/guestcopy.c         | 30 +++++++++++++++++++++++++++++
 xen/include/xen/guest_access.h |  5 +++++
 xen/xsm/flask/flask_op.c       | 43 ++++++++++--------------------------------
 4 files changed, 46 insertions(+), 33 deletions(-)
 create mode 100644 xen/common/guestcopy.c

diff --git a/xen/common/Makefile b/xen/common/Makefile
index 9ce75bb..3da774a 100644
--- a/xen/common/Makefile
+++ b/xen/common/Makefile
@@ -10,6 +10,7 @@ obj-y += event_2l.o
 obj-y += event_channel.o
 obj-y += event_fifo.o
 obj-y += grant_table.o
+obj-y += guestcopy.o
 obj-y += irq.o
 obj-y += kernel.o
 obj-y += keyhandler.o
diff --git a/xen/common/guestcopy.c b/xen/common/guestcopy.c
new file mode 100644
index 0000000..d974f5c
--- /dev/null
+++ b/xen/common/guestcopy.c
@@ -0,0 +1,30 @@
+#include <xen/config.h>
+#include <xen/lib.h>
+#include <xen/guest_access.h>
+#include <xen/err.h>
+
+/* The function copies a string from the guest and adds a NUL to
+ * make sure the string is correctly terminated.
+ */
+void *safe_copy_string_from_guest(XEN_GUEST_HANDLE(char) u_buf,
+                                  size_t size, size_t max_size)
+{
+    char *tmp;
+
+    if ( size > max_size )
+        return ERR_PTR(-ENOENT);
+
+    /* Add an extra +1 to append \0 */
+    tmp = xmalloc_array(char, size + 1);
+    if ( !tmp )
+        return ERR_PTR(-ENOMEM);
+
+    if ( copy_from_guest(tmp, u_buf, size) )
+    {
+        xfree(tmp);
+        return ERR_PTR(-EFAULT);
+    }
+    tmp[size] = 0;
+
+    return tmp;
+}
diff --git a/xen/include/xen/guest_access.h b/xen/include/xen/guest_access.h
index 373454e..55645e6 100644
--- a/xen/include/xen/guest_access.h
+++ b/xen/include/xen/guest_access.h
@@ -8,6 +8,8 @@
 #define __XEN_GUEST_ACCESS_H__
 
 #include <asm/guest_access.h>
+#include <xen/types.h>
+#include <public/xen.h>
 
 #define copy_to_guest(hnd, ptr, nr)                     \
     copy_to_guest_offset(hnd, 0, ptr, nr)
@@ -27,4 +29,7 @@
 #define __clear_guest(hnd, nr)                          \
     __clear_guest_offset(hnd, 0, nr)
 
+void *safe_copy_string_from_guest(XEN_GUEST_HANDLE(char) u_buf,
+                                 size_t size, size_t max_size);
+
 #endif /* __XEN_GUEST_ACCESS_H__ */
diff --git a/xen/xsm/flask/flask_op.c b/xen/xsm/flask/flask_op.c
index 7743aac..b14d306 100644
--- a/xen/xsm/flask/flask_op.c
+++ b/xen/xsm/flask/flask_op.c
@@ -12,6 +12,7 @@
 #include <xen/event.h>
 #include <xsm/xsm.h>
 #include <xen/guest_access.h>
+#include <xen/err.h>
 
 #include <public/xsm/flask_op.h>
 
@@ -76,29 +77,6 @@ static int domain_has_security(struct domain *d, u32 perms)
                         perms, NULL);
 }
 
-static int flask_copyin_string(XEN_GUEST_HANDLE(char) u_buf, char **buf,
-                               size_t size, size_t max_size)
-{
-    char *tmp;
-
-    if ( size > max_size )
-        return -ENOENT;
-
-    tmp = xmalloc_array(char, size + 1);
-    if ( !tmp )
-        return -ENOMEM;
-
-    if ( copy_from_guest(tmp, u_buf, size) )
-    {
-        xfree(tmp);
-        return -EFAULT;
-    }
-    tmp[size] = 0;
-
-    *buf = tmp;
-    return 0;
-}
-
 #endif /* COMPAT */
 
 static int flask_security_user(struct xen_flask_userlist *arg)
@@ -112,9 +90,9 @@ static int flask_security_user(struct xen_flask_userlist *arg)
     if ( rv )
         return rv;
 
-    rv = flask_copyin_string(arg->u.user, &user, arg->size, PAGE_SIZE);
-    if ( rv )
-        return rv;
+    user = safe_copy_string_from_guest(arg->u.user, arg->size, PAGE_SIZE);
+    if ( IS_ERR(user) )
+        return PTR_ERR(user);
 
     rv = security_get_user_sids(arg->start_sid, user, &sids, &nsids);
     if ( rv < 0 )
@@ -227,9 +205,9 @@ static int flask_security_context(struct xen_flask_sid_context *arg)
     if ( rv )
         return rv;
 
-    rv = flask_copyin_string(arg->context, &buf, arg->size, PAGE_SIZE);
-    if ( rv )
-        return rv;
+    buf = safe_copy_string_from_guest(arg->context, arg->size, PAGE_SIZE);
+    if ( IS_ERR(buf) )
+        return PTR_ERR(buf);
 
     rv = security_context_to_sid(buf, arg->size, &arg->sid);
     if ( rv < 0 )
@@ -319,14 +297,13 @@ static int flask_security_setavc_threshold(struct xen_flask_setavc_threshold *ar
 static int flask_security_resolve_bool(struct xen_flask_boolean *arg)
 {
     char *name;
-    int rv;
 
     if ( arg->bool_id != -1 )
         return 0;
 
-    rv = flask_copyin_string(arg->name, &name, arg->size, bool_maxstr);
-    if ( rv )
-        return rv;
+    name = safe_copy_string_from_guest(arg->name, arg->size, bool_maxstr);
+    if ( IS_ERR(name) )
+        return PTR_ERR(name);
 
     arg->bool_id = security_find_bool(name);
     arg->size = 0;
-- 
2.1.4

  parent reply	other threads:[~2015-01-13 14:26 UTC|newest]

Thread overview: 251+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-13 14:25 [PATCH v3 00/24] xen/arm: Add support for non-pci passthrough Julien Grall
2015-01-13 14:25 ` [PATCH v3 01/24] xen: Extend DOMCTL createdomain to support arch configuration Julien Grall
2015-01-13 15:08   ` Daniel De Graaf
2015-01-19 16:46   ` Jan Beulich
2015-01-20 12:40     ` Julien Grall
2015-01-28 15:50   ` Stefano Stabellini
2015-02-20 15:15   ` Ian Campbell
2015-02-20 16:09     ` Julien Grall
2015-02-20 16:37       ` Jan Beulich
2015-02-23 15:09       ` Ian Campbell
2015-02-20 16:35     ` Jan Beulich
2015-02-23 15:48     ` Andrew Cooper
2015-02-23 16:00       ` Ian Campbell
2015-02-23 21:48         ` Julien Grall
2015-02-24 10:06           ` Ian Campbell
2015-01-13 14:25 ` [PATCH v3 02/24] xen/arm: Divide GIC initialization in 2 parts Julien Grall
2015-01-28 16:09   ` Stefano Stabellini
2015-02-20 15:19     ` Ian Campbell
2015-02-20 15:19   ` Ian Campbell
2015-01-13 14:25 ` [PATCH v3 03/24] xen/dts: Allow only IRQ translation that are mapped to main GIC Julien Grall
2015-01-28 16:11   ` Stefano Stabellini
2015-02-20 15:20   ` Ian Campbell
2015-01-13 14:25 ` Julien Grall [this message]
2015-01-13 15:10   ` [PATCH v3 04/24] xen: guestcopy: Provide an helper to safely copy string from guest Daniel De Graaf
2015-01-19 16:51   ` Jan Beulich
2015-01-20 12:45     ` Julien Grall
2015-01-20 13:16       ` Jan Beulich
2015-02-20 15:22   ` Ian Campbell
2015-01-13 14:25 ` [PATCH v3 05/24] xen/arm: vgic: Introduce a function to initialize pending_irq Julien Grall
2015-02-20 15:24   ` Ian Campbell
2015-01-13 14:25 ` [PATCH v3 06/24] xen/arm: Map disabled device in DOM0 Julien Grall
2015-01-28 16:18   ` Stefano Stabellini
2015-01-28 16:23     ` Julien Grall
2015-01-29 11:41       ` Stefano Stabellini
2015-02-20 15:27   ` Ian Campbell
2015-01-13 14:25 ` [PATCH v3 07/24] xen/arm: Introduce xen, passthrough property Julien Grall
2015-01-28 16:36   ` Stefano Stabellini
2015-01-28 16:53     ` Julien Grall
2015-01-29 11:43       ` Stefano Stabellini
2015-02-20 15:38   ` Ian Campbell
2015-02-20 17:01     ` Julien Grall
2015-02-23 15:12       ` Ian Campbell
2015-02-23 15:43         ` Julien Grall
2015-02-20 15:42   ` Ian Campbell
2015-02-20 17:03     ` Julien Grall
2015-02-23 15:15       ` Ian Campbell
2015-02-23 15:44         ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 08/24] xen/arm: Allow virq != irq Julien Grall
2015-01-28 16:47   ` Stefano Stabellini
2015-01-28 16:56     ` Julien Grall
2015-01-28 17:00       ` Julien Grall
2015-01-29 11:50       ` Stefano Stabellini
2015-02-20 15:52   ` Ian Campbell
2015-02-20 17:09     ` Julien Grall
2015-02-27 14:33       ` Julien Grall
2015-02-27 14:44         ` Ian Campbell
2015-02-27 15:55           ` Julien Grall
2015-02-27 14:25     ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 09/24] xen/arm: route_irq_to_guest: Check validity of the IRQ Julien Grall
2015-01-28 17:55   ` Stefano Stabellini
2015-01-28 18:05     ` Julien Grall
2015-01-29 11:52       ` Stefano Stabellini
2015-02-20 16:00   ` Ian Campbell
2015-02-20 17:21     ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 10/24] xen/arm: gic: Add sanity checks gic_route_irq_to_guest Julien Grall
2015-01-28 18:15   ` Stefano Stabellini
2015-02-20 16:07   ` Ian Campbell
2015-02-20 17:28     ` Julien Grall
2015-02-23 15:20       ` Ian Campbell
2015-02-23 15:47         ` Julien Grall
2015-02-23 15:52           ` Ian Campbell
2015-02-23 15:54             ` Julien Grall
2015-02-23 16:04               ` Ian Campbell
2015-01-13 14:25 ` [PATCH v3 11/24] xen/arm: Let the toolstack configure the number of SPIs Julien Grall
2015-01-28 18:26   ` Stefano Stabellini
2015-01-28 18:58     ` Julien Grall
2015-01-29 12:01       ` Stefano Stabellini
2015-01-29 12:14         ` Julien Grall
2015-02-20 16:08     ` Ian Campbell
2015-02-20 17:29       ` Julien Grall
2015-02-23 15:22         ` Ian Campbell
2015-02-23 15:48           ` Julien Grall
2015-02-20 16:23   ` Ian Campbell
2015-02-20 17:31     ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 12/24] xen/arm: Release IRQ routed to a domain when it's destroying Julien Grall
2015-01-28 18:38   ` Stefano Stabellini
2015-02-20 16:31   ` Ian Campbell
2015-02-20 17:41     ` Julien Grall
2015-02-23 15:25       ` Ian Campbell
2015-02-23 15:49         ` Julien Grall
2015-03-03 12:50       ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 13/24] xen/arm: Implement hypercall PHYSDEVOP_{, un}map_pirq Julien Grall
2015-01-19 16:54   ` Jan Beulich
2015-01-20 14:01     ` Julien Grall
2015-01-28 18:52   ` Stefano Stabellini
2015-01-28 19:04     ` Julien Grall
2015-01-29 12:17       ` Stefano Stabellini
2015-01-29 12:26         ` Julien Grall
2015-01-29 12:33           ` Stefano Stabellini
2015-02-20 16:53             ` Ian Campbell
2015-02-23  9:33               ` Jan Beulich
2015-02-23 15:28                 ` Ian Campbell
2015-02-23 15:53                   ` Julien Grall
2015-02-23 16:04                     ` Ian Campbell
2015-02-23 16:11                       ` Julien Grall
2015-02-23 16:34                         ` Ian Campbell
2015-02-23 21:54                           ` Julien Grall
2015-02-23 16:22                     ` Jan Beulich
2015-02-23 15:51               ` Julien Grall
2015-02-23 16:02                 ` Ian Campbell
2015-03-04 14:37                   ` Julien Grall
2015-03-04 14:55                     ` Jan Beulich
2015-03-04 14:56                       ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 14/24] xen/dts: Use unsigned int for MMIO and IRQ index Julien Grall
2015-02-20 16:55   ` Ian Campbell
2015-02-23 15:57     ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 15/24] xen/dts: Provide an helper to get a DT node from a path provided by a guest Julien Grall
2015-02-20 16:56   ` Ian Campbell
2015-02-20 17:43     ` Julien Grall
2015-02-23 15:29       ` Ian Campbell
2015-02-23 15:30   ` Ian Campbell
2015-02-23 16:01     ` Julien Grall
2015-02-23 16:27       ` Ian Campbell
2015-03-10 13:58         ` Julien Grall
2015-03-11 12:34           ` Ian Campbell
2015-03-19 14:53             ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 16/24] xen/passthrough: Introduce iommu_construct Julien Grall
2015-01-19 17:02   ` Jan Beulich
2015-01-20 14:28     ` Julien Grall
2015-01-20 16:40       ` Jan Beulich
2015-01-20 17:11         ` Julien Grall
2015-01-21 10:23           ` Jan Beulich
2015-01-21 10:37             ` Julien Grall
2015-01-21 10:48               ` Jan Beulich
2015-01-21 12:13                 ` Julien Grall
2015-01-21 14:13                   ` Jan Beulich
2015-01-21 14:22                     ` Julien Grall
2015-01-21 14:29                       ` Jan Beulich
2015-02-20 16:58   ` Ian Campbell
2015-02-20 17:45     ` Julien Grall
2015-02-23 15:31       ` Ian Campbell
2015-02-23 16:04         ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 17/24] xen/passthrough: arm: release earlier the DT devices assigned to a guest Julien Grall
2015-01-20  9:19   ` Jan Beulich
2015-01-20 14:30     ` Julien Grall
2015-01-20 16:42       ` Jan Beulich
2015-01-28 18:59   ` Stefano Stabellini
2015-02-20 17:03   ` Ian Campbell
2015-02-20 17:46     ` Julien Grall
2015-02-23  9:37       ` Jan Beulich
2015-02-23 15:33         ` Ian Campbell
2015-02-23 16:22           ` Jan Beulich
2015-01-13 14:25 ` [PATCH v3 18/24] xen/passthrough: iommu_deassign_device_dt: By default reassign device to nobody Julien Grall
2015-01-20  9:21   ` Jan Beulich
2015-01-20 14:31     ` Julien Grall
2015-01-29 10:20   ` Stefano Stabellini
2015-02-20 17:04   ` Ian Campbell
2015-02-23  9:40     ` Jan Beulich
2015-02-23 10:10       ` Julien Grall
2015-02-23 10:20         ` Jan Beulich
2015-02-23 15:39           ` Ian Campbell
2015-02-23 16:24             ` Jan Beulich
2015-02-23 16:35               ` Ian Campbell
2015-02-23 16:37             ` Julien Grall
2015-02-23 16:47               ` Jan Beulich
2015-02-23 16:54                 ` Julien Grall
2015-02-23 10:10     ` Julien Grall
2015-02-23 15:34       ` Ian Campbell
2015-03-10 15:29         ` Julien Grall
2015-03-11 12:35           ` Ian Campbell
2015-03-19 15:07             ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 19/24] xen/iommu: arm: Wire iommu DOMCTL for ARM Julien Grall
2015-01-20  9:22   ` Jan Beulich
2015-01-20 14:32     ` Julien Grall
2015-02-20 17:06   ` Ian Campbell
2015-01-13 14:25 ` [PATCH v3 20/24] xen/passthrough: Extend XEN_DOMCTL_assign_device to support DT device Julien Grall
2015-01-20  9:34   ` Jan Beulich
2015-01-20 14:37     ` Julien Grall
2015-01-20 16:43       ` Jan Beulich
2015-01-29 10:29     ` Stefano Stabellini
2015-01-29 10:37       ` Jan Beulich
2015-01-29 11:40       ` Julien Grall
2015-01-29 10:29   ` Stefano Stabellini
2015-01-29 11:45     ` Julien Grall
2015-01-29 12:09       ` Stefano Stabellini
2015-01-29 13:09         ` Julien Grall
2015-01-29 15:03           ` Stefano Stabellini
2015-01-29 15:14             ` Julien Grall
2015-02-20 17:17   ` Ian Campbell
2015-02-23 16:25     ` Daniel De Graaf
2015-03-10 16:52       ` Julien Grall
2015-03-10 22:45         ` Daniel De Graaf
2015-03-10 23:07           ` Julien Grall
2015-03-10 23:39             ` Daniel De Graaf
2015-03-11 12:30               ` Julien Grall
2015-03-11  8:53           ` Jan Beulich
2015-03-11 12:15             ` Julien Grall
2015-03-11 12:23               ` Ian Campbell
2015-03-11 12:35                 ` Julien Grall
2015-03-11 12:45               ` Jan Beulich
2015-03-10 16:33     ` Julien Grall
2015-03-11 12:37       ` Ian Campbell
2015-03-11 13:50         ` Julien Grall
2015-03-11 13:55           ` Ian Campbell
2015-03-11 14:03             ` Jan Beulich
2015-03-11 14:11               ` Julien Grall
2015-03-13 16:47           ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 21/24] tools/(lib)xl: Add partial device tree support for ARM Julien Grall
2015-01-29 11:03   ` Stefano Stabellini
2015-01-29 12:02     ` Julien Grall
2015-01-29 12:26       ` Stefano Stabellini
2015-02-23 11:31     ` Ian Campbell
2015-02-23 11:46   ` Ian Campbell
2015-02-23 17:06     ` Julien Grall
2015-02-23 17:22       ` Ian Campbell
2015-03-17 13:32         ` Julien Grall
2015-02-23 12:03   ` Ian Jackson
2015-02-23 12:44     ` Ian Jackson
2015-02-23 18:43     ` Julien Grall
2015-02-23 19:12       ` Ian Jackson
2015-01-13 14:25 ` [PATCH v3 22/24] tools/libxl: arm: Use an higher value for the GIC phandle Julien Grall
2015-01-29 11:07   ` Stefano Stabellini
2015-01-29 12:05     ` Julien Grall
2015-01-29 12:28       ` Stefano Stabellini
2015-01-29 13:48         ` Julien Grall
2015-01-29 15:04           ` Stefano Stabellini
2015-01-29 15:12             ` Julien Grall
2015-02-23 14:36           ` Ian Campbell
2015-02-23 22:02             ` Julien Grall
2015-02-24 10:08               ` Ian Campbell
2015-03-18 14:14                 ` Julien Grall
2015-01-13 14:25 ` [PATCH v3 23/24] libxl: Add support for non-PCI passthrough Julien Grall
2015-01-29 11:12   ` Stefano Stabellini
2015-01-29 12:08     ` Julien Grall
2015-01-29 12:31       ` Stefano Stabellini
2015-01-29 13:51         ` Julien Grall
2015-02-23 14:41           ` Ian Campbell
2015-02-23 14:42   ` Ian Campbell
2015-01-13 14:25 ` [PATCH v3 24/24] xl: Add new option dtdev Julien Grall
2015-01-29 11:18   ` Stefano Stabellini
2015-01-29 12:09     ` Julien Grall
2015-01-29 12:32       ` Stefano Stabellini
2015-01-29 13:51         ` Julien Grall
2015-01-29 15:06           ` Stefano Stabellini
2015-02-23 14:44             ` Ian Campbell
2015-02-23 14:45   ` Ian Campbell
2015-02-23 22:03     ` Julien Grall
2015-02-24 10:07       ` Ian Campbell
2015-01-13 17:56 ` [PATCH v3 00/24] xen/arm: Add support for non-pci passthrough Julien Grall
2015-02-20 17:18 ` Ian Campbell
2015-02-20 17:47   ` Julien Grall

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=1421159133-31526-5-git-send-email-julien.grall@linaro.org \
    --to=julien.grall@linaro.org \
    --cc=dgdegra@tycho.nsa.gov \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=keir@xen.org \
    --cc=stefano.stabellini@citrix.com \
    --cc=tim@xen.org \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.