Skip to contentSkip to conversation

linux-user: minor epoll_wait fixes

PATCH In review Peter Maydell 2 participants 5 messages Jul 18, 2016 – Oct 5, 2016

lore
Share & tools
lore
#0 lore raw

cover linux-user: minor epoll_wait fixes

This patchset fixes a couple of minor problems in epoll_wait and epoll_pwait:
* we weren't checking an argument for sanity, so we would fail EFAULT rather than EINVAL for cases like negative lengths (thus failing an LTP testcase)
* we were using alloca() to allocate an array whose length is set by the guest

Peter Maydell (2):
linux-user: Check for bad event numbers in epoll_wait
linux-user: Don't use alloca() for epoll_wait's epoll event array

 linux-user/syscall.c      | 22 ++++++++++++++++++----
 linux-user/syscall_defs.h |  3 +++
 2 files changed, 21 insertions(+), 4 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/2 linux-user: Check for bad event numbers in epoll_wait

The kernel checks that the maxevents parameter to epoll_wait is non-negative and not larger than EP_MAX_EVENTS. Add this check to our implementation, so that:
* we fail these cases EINVAL rather than EFAULT
* we don't pass negative or overflowing values to the lock_user() size calculation

---
This fixes an LTP epoll_wait03 test case that checks a negative max_events argument.

---
 linux-user/syscall.c      | 5 +++++
 linux-user/syscall_defs.h | 3 +++
 2 files changed, 8 insertions(+)
linux-user/syscall.c +5 -0
index fe72abe..3552295 100644
@@ -11024,6 +11024,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         int maxevents = arg3;
         int timeout = arg4;
 
+        if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
+            ret = -TARGET_EINVAL;
+            break;
+        }
+
         target_ep = lock_user(VERIFY_WRITE, arg2,
                               maxevents * sizeof(struct target_epoll_event), 1);
         if (!target_ep) {
linux-user/syscall_defs.h +3 -0
index d9dea0e..dbf6a38 100644
@@ -2585,6 +2585,9 @@ struct target_epoll_event {
     abi_uint events;
     target_epoll_data_t data;
 } TARGET_EPOLL_PACKED;
+
+#define TARGET_EP_MAX_EVENTS (INT_MAX / sizeof(struct target_epoll_event))
+
 #endif
 struct target_rlimit64 {
     uint64_t rlim_cur;
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/2 linux-user: Don't use alloca() for epoll_wait's epoll event array

In reply to Peter Maydell

The epoll event array which epoll_wait() allocates has a size determined by the guest which could potentially be quite large. Use g_try_new() rather than alloca() so that we can fail more cleanly if the guest hands us an oversize value. (ENOMEM is not a documented return value for epoll_wait() but in practice some kernel configurations can return it -- see for instance sys_oabi_epoll_wait() on ARM.)

This rearrangement includes fixing a bug where we were incorrectly passing a negative length to unlock_user() in the error-exit codepath.

---
 linux-user/syscall.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
linux-user/syscall.c +13 -4
index 3552295..721d7b1 100644
@@ -11035,7 +11035,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             goto efault;
         }
 
-        ep = alloca(maxevents * sizeof(struct epoll_event));
+        ep = g_try_new(struct epoll_event, maxevents);
+        if (!ep) {
+            unlock_user(target_ep, arg2, 0);
+            ret = -TARGET_ENOMEM;
+            break;
+        }
 
         switch (num) {
 #if defined(TARGET_NR_epoll_pwait)
@@ -11053,8 +11058,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 target_set = lock_user(VERIFY_READ, arg5,
                                        sizeof(target_sigset_t), 1);
                 if (!target_set) {
-                    unlock_user(target_ep, arg2, 0);
-                    goto efault;
+                    ret = -TARGET_EFAULT;
+                    break;
                 }
                 target_to_host_sigset(set, target_set);
                 unlock_user(target_set, arg5, 0);
@@ -11082,8 +11087,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 target_ep[i].events = tswap32(ep[i].events);
                 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
             }
+            unlock_user(target_ep, arg2,
+                        ret * sizeof(struct target_epoll_event));
+        } else {
+            unlock_user(target_ep, arg2, 0);
         }
-        unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
+        g_free(ep);
         break;
     }
 #endif
Signature · 1 line
1.9.1
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: Don't use alloca() for epoll_wait's epoll event array

Ping? It looks like patch 1/2 of this series got into the recent linux-user pullreq, but this one (2/2) didn't. Do you want a resend as a standalone patch?

thanks
-- PMM

Quoted 62 lines · Peter Maydell

The epoll event array which epoll_wait() allocates has a size determined by the guest which could potentially be quite large. Use g_try_new() rather than alloca() so that we can fail more cleanly if the guest hands us an oversize value. (ENOMEM is not a documented return value for epoll_wait() but in practice some kernel configurations can return it -- see for instance sys_oabi_epoll_wait() on ARM.)

This rearrangement includes fixing a bug where we were incorrectly passing a negative length to unlock_user() in the error-exit codepath.

---
 linux-user/syscall.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
linux-user/syscall.c +13 -5
index 3552295..721d7b1 100644
@@ -11035,7 +11035,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             goto efault;
         }
 
-        ep = alloca(maxevents * sizeof(struct epoll_event));
+        ep = g_try_new(struct epoll_event, maxevents);
+        if (!ep) {
+            unlock_user(target_ep, arg2, 0);
+            ret = -TARGET_ENOMEM;
+            break;
+        }
 
         switch (num) {
 #if defined(TARGET_NR_epoll_pwait)
@@ -11053,8 +11058,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 target_set = lock_user(VERIFY_READ, arg5,
                                        sizeof(target_sigset_t), 1);
                 if (!target_set) {
-                    unlock_user(target_ep, arg2, 0);
-                    goto efault;
+                    ret = -TARGET_EFAULT;
+                    break;
                 }
                 target_to_host_sigset(set, target_set);
                 unlock_user(target_set, arg5, 0);
@@ -11082,8 +11087,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 target_ep[i].events = tswap32(ep[i].events);
                 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
             }
+            unlock_user(target_ep, arg2,
+                        ret * sizeof(struct target_epoll_event));
+        } else {
+            unlock_user(target_ep, arg2, 0);
         }
-        unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
+        g_free(ep);
         break;
     }
 #endif
--

1.9.1

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] linux-user: Don't use alloca() for epoll_wait's epoll event array

Quoted 3 lines · Peter Maydell

Ping? It looks like patch 1/2 of this series got into the recent linux-user pullreq, but this one (2/2) didn't. Do you want a resend as a standalone patch?

No need, I've pulled it from patchwork, for some reason this didn't get to my mailbox...

Quoted 63 lines · Peter Maydell
Quoted 62 lines

The epoll event array which epoll_wait() allocates has a size determined by the guest which could potentially be quite large. Use g_try_new() rather than alloca() so that we can fail more cleanly if the guest hands us an oversize value. (ENOMEM is not a documented return value for epoll_wait() but in practice some kernel configurations can return it -- see for instance sys_oabi_epoll_wait() on ARM.)

This rearrangement includes fixing a bug where we were incorrectly passing a negative length to unlock_user() in the error-exit codepath.

---
 linux-user/syscall.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
linux-user/syscall.c +13 -5
index 3552295..721d7b1 100644
@@ -11035,7 +11035,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             goto efault;
         }
 
-        ep = alloca(maxevents * sizeof(struct epoll_event));
+        ep = g_try_new(struct epoll_event, maxevents);
+        if (!ep) {
+            unlock_user(target_ep, arg2, 0);
+            ret = -TARGET_ENOMEM;
+            break;
+        }
 
         switch (num) {
 #if defined(TARGET_NR_epoll_pwait)
@@ -11053,8 +11058,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 target_set = lock_user(VERIFY_READ, arg5,
                                        sizeof(target_sigset_t), 1);
                 if (!target_set) {
-                    unlock_user(target_ep, arg2, 0);
-                    goto efault;
+                    ret = -TARGET_EFAULT;
+                    break;
                 }
                 target_to_host_sigset(set, target_set);
                 unlock_user(target_set, arg5, 0);
@@ -11082,8 +11087,12 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 target_ep[i].events = tswap32(ep[i].events);
                 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
             }
+            unlock_user(target_ep, arg2,
+                        ret * sizeof(struct target_epoll_event));
+        } else {
+            unlock_user(target_ep, arg2, 0);
         }
-        unlock_user(target_ep, arg2, ret * sizeof(struct target_epoll_event));
+        g_free(ep);
         break;
     }
 #endif
--

1.9.1