Skip to contentSkip to conversation

xenforeignmemory: work around bug in older privcmd

PATCH In review Paul Durrant 5 participants 7 messages Aug 24, 2018 – Aug 28, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xenforeignmemory: work around bug in older privcmd

Versions of linux privcmd prior to commit dc9eab6fd94d ("return -ENOTTY for unimplemented IOCTLs") will return -EINVAL rather than the conventional -ENOTTY for unimplemented codes. This breaks the error path in libxenforeignmemory resource mapping, which only translates ENOTTY into EOPNOTSUPP to inform callers of the need to use an alternative (legacy) mechanism.

This patch adds a new 'unimplemented' [1] ioctl code into the local privcmd header which is then used to probe for the appropriate errno to translate in the resource mapping error path

[1] this is a code that has, so far, never been used in any version of privcmd and will be added to future versions of the header in the linux source, to make sure it stays unimplemented.

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

---
 tools/include/xen-sys/Linux/privcmd.h |  2 ++
 tools/libs/foreignmemory/linux.c      | 19 ++++++++++++++++++-
 tools/libs/foreignmemory/private.h    |  1 +
 3 files changed, 21 insertions(+), 1 deletion(-)
tools/include/xen-sys/Linux/privcmd.h +2 -0
index 9531b728f9..bc60e8fd55 100644
@@ -114,5 +114,7 @@ typedef struct privcmd_mmap_resource {
 	_IOC(_IOC_NONE, 'P', 6, sizeof(domid_t))
 #define IOCTL_PRIVCMD_MMAP_RESOURCE				\
 	_IOC(_IOC_NONE, 'P', 7, sizeof(privcmd_mmap_resource_t))
+#define IOCTL_PRIVCMD_UNIMPLEMENTED				\
+	_IOC(_IOC_NONE, 'P', 0xFF, 0)
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
tools/libs/foreignmemory/linux.c +18 -1
index 3686cf41e0..0368aa09f4 100644
@@ -53,6 +53,23 @@ int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
         return -1;
     }
 
+    /*
+     * Older versions of privcmd return -EINVAL for unimplemented ioctls
+     * so we need to probe for the errno to use rather than just using
+     * the conventional ENOTTY.
+     */
+    if ( ioctl(fd, IOCTL_PRIVCMD_UNIMPLEMENTED, NULL) >= 0 )
+    {
+        xtl_log(fmem->logger, XTL_ERROR, -1, "xenforeignmemory",
+                "privcmd ioctl should not be implemented");
+        return -1;
+    }
+    else
+    {
+        fmem->unimpl_errno = errno;
+        errno = 0;
+    }
+
     fmem->fd = fd;
     return 0;
 }
@@ -307,7 +324,7 @@ int osdep_xenforeignmemory_map_resource(
     {
         int saved_errno;
 
-        if ( errno != ENOTTY && errno != EOPNOTSUPP )
+        if ( errno != fmem->unimpl_errno && errno != EOPNOTSUPP )
             PERROR("ioctl failed");
         else
             errno = EOPNOTSUPP;
tools/libs/foreignmemory/private.h +1 -0
index b06ce12583..8f1bf081ed 100644
@@ -23,6 +23,7 @@ struct xenforeignmemory_handle {
     unsigned flags;
     int fd;
     Xentoolcore__Active_Handle tc_ah;
+    int unimpl_errno;
 };
 
 int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem);
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Paul Durrant

Versions of linux privcmd prior to commit dc9eab6fd94d ("return -ENOTTY for unimplemented IOCTLs") will return -EINVAL rather than the conventional -ENOTTY for unimplemented codes. This breaks the error path in libxenforeignmemory resource mapping, which only translates ENOTTY into EOPNOTSUPP to inform callers of the need to use an alternative (legacy) mechanism.

This patch adds a new 'unimplemented' [1] ioctl code into the local privcmd header which is then used to probe for the appropriate errno to translate in the resource mapping error path

Quoted 6 lines

[1] this is a code that has, so far, never been used in any version of privcmd and will be added to future versions of the header in the linux source, to make sure it stays unimplemented.

---

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v1 1/1
#2 lore raw
In reply to Paul Durrant

Paul Durrant writes ("[PATCH] xenforeignmemory: work around bug in older privcmd"):

Quoted 7 lines

Versions of linux privcmd prior to commit dc9eab6fd94d ("return -ENOTTY for unimplemented IOCTLs") will return -EINVAL rather than the conventional -ENOTTY for unimplemented codes. This breaks the error path in libxenforeignmemory resource mapping, which only translates ENOTTY into EOPNOTSUPP to inform callers of the need to use an alternative (legacy) mechanism.

Quoted 7 lines

This patch adds a new 'unimplemented' [1] ioctl code into the local privcmd header which is then used to probe for the appropriate errno to translate in the resource mapping error path

[1] this is a code that has, so far, never been used in any version of privcmd and will be added to future versions of the header in the linux source, to make sure it stays unimplemented.

How irritating! Thanks for fixing it.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
In reply to Paul Durrant
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 24.08.18 at 14:16, <[email protected]> wrote:

Versions of linux privcmd prior to commit dc9eab6fd94d ("return -ENOTTY for unimplemented IOCTLs") will return -EINVAL rather than the conventional -ENOTTY for unimplemented codes. This breaks the error path in libxenforeignmemory resource mapping, which only translates ENOTTY into EOPNOTSUPP to inform callers of the need to use an alternative (legacy) mechanism.

Quoted 7 lines

This patch adds a new 'unimplemented' [1] ioctl code into the local privcmd header which is then used to probe for the appropriate errno to translate in the resource mapping error path

[1] this is a code that has, so far, never been used in any version of privcmd and will be added to future versions of the header in the linux source, to make sure it stays unimplemented.

Shouldn't this addition happen before the Xen tools side change goes in?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 12 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 24.08.18 at 14:16, <[email protected]> wrote:

Versions of linux privcmd prior to commit dc9eab6fd94d ("return -ENOTTY for unimplemented IOCTLs") will return -EINVAL rather than the conventional -ENOTTY for unimplemented codes. This breaks the error path in libxenforeignmemory resource mapping, which only translates ENOTTY into EOPNOTSUPP to inform callers of the need to use an alternative (legacy) mechanism.

This patch adds a new 'unimplemented' [1] ioctl code into the local privcmd header which is then used to probe for the appropriate errno to translate in the resource mapping error path

Quoted 6 lines
Quoted 3 lines

[1] this is a code that has, so far, never been used in any version of privcmd and will be added to future versions of the header in the linux source, to make sure it stays unimplemented.

Shouldn't this addition happen before the Xen tools side change goes in?

It doesn't really make any difference from a practical PoV. Applying this patch right away helps unblocking various Linux branches.

Wei.

Quoted 4 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
In reply to Paul Durrant
Quoted 7 lines · Paul Durrant
tools/libs/foreignmemory/linux.c +0 -0
index 3686cf41e0..0368aa09f4 100644
@@ -53,6 +53,23 @@ int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
         return -1;
     }

tools/libs/foreignmemory/linux.c int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem) · line 64

Quoted 10 lines
tools/libs/foreignmemory/linux.c +9 -0
 
+    /*
+     * Older versions of privcmd return -EINVAL for unimplemented ioctls
+     * so we need to probe for the errno to use rather than just using
+     * the conventional ENOTTY.
+     */
+    if ( ioctl(fd, IOCTL_PRIVCMD_UNIMPLEMENTED, NULL) >= 0 )
+    {
+        xtl_log(fmem->logger, XTL_ERROR, -1, "xenforeignmemory",
+                "privcmd ioctl should not be implemented");

This error path leaks fd.

~Andrew

tools/libs/foreignmemory/linux.c int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem) · line 75

Quoted 12 lines
tools/libs/foreignmemory/linux.c +8 -0
+        return -1;
+    }
+    else
+    {
+        fmem->unimpl_errno = errno;
+        errno = 0;
+    }
+
     fmem->fd = fd;
     return 0;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Paul Durrant
tools/libs/foreignmemory/linux.c +0 -0
index 3686cf41e0..0368aa09f4 100644
@@ -53,6 +53,23 @@ int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
         return -1;
     }
 
Quoted 11 lines

tools/libs/foreignmemory/linux.c int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem) · line 64

Quoted 9 lines
tools/libs/foreignmemory/linux.c +9 -0
+    /*
+     * Older versions of privcmd return -EINVAL for unimplemented ioctls
+     * so we need to probe for the errno to use rather than just using
+     * the conventional ENOTTY.
+     */
+    if ( ioctl(fd, IOCTL_PRIVCMD_UNIMPLEMENTED, NULL) >= 0 )
+    {
+        xtl_log(fmem->logger, XTL_ERROR, -1, "xenforeignmemory",
+                "privcmd ioctl should not be implemented");

This error path leaks fd.

Patch sent.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel