Skip to contentSkip to conversation

linux-user: Fix posix timer implementation

PATCH Superseded Alexander Graf 2 participants 5 messages Nov 10, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover linux-user: Fix posix timer implementation

Tom recently realized that I accidently broke the posix timer functions. With his simple test program he was able to show that we indeed do break posix timers since my last commit touching them:

http://csgraf.de/agraf/timer.c

These two patches fix it up properly. With these I can successfully run his test case in any endian combination.

Please apply for 2.2.

Alex

Alexander Graf (2):
linux-user: Fix timer creation tswap
linux-user: Properly handle timer magic offset

 linux-user/syscall.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
Signature · 1 line
1.7.12.4
#1 lore raw

PATCH 1/2 linux-user: Fix timer creation tswap

The timer pointer field we're writing a new timer ID into is of field abi_ulong which means we need to swab 64bits on 64bit ABI targets.

Change the tswap from 32 to l to give us correct foreign-endian results.

---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +1 -1
index a175cc1..f3e22c8 100644
@@ -9604,7 +9604,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 if (!lock_user_struct(VERIFY_WRITE, ptarget_timer, arg3, 1)) {
                     goto efault;
                 }
-                ptarget_timer->ptr = tswap32(0xcafe0000 | timer_index);
+                ptarget_timer->ptr = tswapl(0xcafe0000 | timer_index);
                 unlock_user_struct(ptarget_timer, arg3, 1);
             }
         }
Signature · 1 line
1.7.12.4
#2 lore raw

PATCH 2/2 linux-user: Properly handle timer magic offset

In reply to Alexander Graf

When creating a timer handle, we give the timer id a special magic offset of 0xcafe0000. However, we never mask that offset out of the timer id before we start using it to dereference our timer array. So we always end up aborting timer operations because the timer id is out of bounds.

This was not an issue before my patch e52a99f756e ("linux-user: Simplify timerid checks on g_posix_timers range") because before we would blindly mask anything above the first 16 bits.

This patch is superior to the plain masking in that it also adds validity checks against the timer id to ensure we're always dealing with an actual timer id created by QEMU.

---
 linux-user/syscall.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
linux-user/syscall.c +14 -1
index f3e22c8..78896dc 100644
@@ -9573,6 +9573,9 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     }
 #endif
 
+#define TIMER_MAGIC 0xcafe0000
+#define TIMER_MAGIC_MASK 0xffff0000
+
 #ifdef TARGET_NR_timer_create
     case TARGET_NR_timer_create:
     {
@@ -9604,7 +9607,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 if (!lock_user_struct(VERIFY_WRITE, ptarget_timer, arg3, 1)) {
                     goto efault;
                 }
-                ptarget_timer->ptr = tswapl(0xcafe0000 | timer_index);
+                ptarget_timer->ptr = tswapl(TIMER_MAGIC | timer_index);
                 unlock_user_struct(ptarget_timer, arg3, 1);
             }
         }
@@ -9619,6 +9622,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
          * struct itimerspec * old_value */
         target_ulong timerid = arg1;
 
+        /* Convert QEMU provided timer ID back to internal 16bit index format */
+        if ((timerid & TIMER_MAGIC_MASK) == TIMER_MAGIC) {
+            timerid &= 0xffff;
+        }
+
         if (arg3 == 0 || timerid >= ARRAY_SIZE(g_posix_timers)) {
             ret = -TARGET_EINVAL;
         } else {
@@ -9640,6 +9648,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         /* args: timer_t timerid, struct itimerspec *curr_value */
         target_ulong timerid = arg1;
 
+        /* Convert QEMU provided timer ID back to internal 16bit index format */
+        if ((timerid & TIMER_MAGIC_MASK) == TIMER_MAGIC) {
+            timerid &= 0xffff;
+        }
+
         if (!arg2) {
             return -TARGET_EFAULT;
         } else if (timerid >= ARRAY_SIZE(g_posix_timers)) {
Signature · 1 line
1.7.12.4
#3 lore raw

Re: [Qemu-devel] [PATCH 2.2 2/2] linux-user: Properly handle timer magic offset

Quoted 5 lines · Alexander Graf

When creating a timer handle, we give the timer id a special magic offset of 0xcafe0000. However, we never mask that offset out of the timer id before we start using it to dereference our timer array. So we always end up aborting timer operations because the timer id is out of bounds.

Quoted 7 lines

This was not an issue before my patch e52a99f756e ("linux-user: Simplify timerid checks on g_posix_timers range") because before we would blindly mask anything above the first 16 bits.

This patch is superior to the plain masking in that it also adds validity checks against the timer id to ensure we're always dealing with an actual timer id created by QEMU.

The commit message says this...

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 9628

Quoted 8 lines
linux-user/syscall.c +4 -0
@@ -9619,6 +9622,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
          * struct itimerspec * old_value */
         target_ulong timerid = arg1;
 
+        /* Convert QEMU provided timer ID back to internal 16bit index format */
+        if ((timerid & TIMER_MAGIC_MASK) == TIMER_MAGIC) {
+            timerid &= 0xffff;
+        }

...but the code doesn't actually fail EINVAL in the case that the magic value doesn't match, so if you just pass in a small integer for the timerid that will work even though the magic is wrong. (You can't actually make it overflow the array this way, obviously.)

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 9655

Quoted 13 lines
linux-user/syscall.c +6 -0
+
         if (arg3 == 0 || timerid >= ARRAY_SIZE(g_posix_timers)) {
             ret = -TARGET_EINVAL;
         } else {
@@ -9640,6 +9648,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         /* args: timer_t timerid, struct itimerspec *curr_value */
         target_ulong timerid = arg1;
 
+        /* Convert QEMU provided timer ID back to internal 16bit index format */
+        if ((timerid & TIMER_MAGIC_MASK) == TIMER_MAGIC) {
+            timerid &= 0xffff;
+        }
+

Same here.

thanks
- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH 2.2 2/2] linux-user: Properly handle timer magic offset

Quoted 20 lines · Peter Maydell
Quoted 5 lines · Alexander Graf

When creating a timer handle, we give the timer id a special magic offset of 0xcafe0000. However, we never mask that offset out of the timer id before we start using it to dereference our timer array. So we always end up aborting timer operations because the timer id is out of bounds.

Quoted 7 lines

This was not an issue before my patch e52a99f756e ("linux-user: Simplify timerid checks on g_posix_timers range") because before we would blindly mask anything above the first 16 bits.

This patch is superior to the plain masking in that it also adds validity checks against the timer id to ensure we're always dealing with an actual timer id created by QEMU.

The commit message says this...

Quoted 4 lines
linux-user/syscall.c +0 -0
@@ -9619,6 +9622,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
          * struct itimerspec * old_value */
         target_ulong timerid = arg1;
 
Quoted 9 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 9628

Quoted 4 lines
linux-user/syscall.c +4 -0
+        /* Convert QEMU provided timer ID back to internal 16bit index format */
+        if ((timerid & TIMER_MAGIC_MASK) == TIMER_MAGIC) {
+            timerid &= 0xffff;
+        }

...but the code doesn't actually fail EINVAL in the case that the magic value doesn't match, so if you just pass in a small integer for the timerid that will work even though the magic is wrong. (You can't actually make it overflow the array this way, obviously.)

Well, it's not exactly horribly obvious, but...

Quoted 3 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 9630

Quoted 3 lines
linux-user/syscall.c +1 -0
+
         if (arg3 == 0 || timerid >= ARRAY_SIZE(g_posix_timers)) {

... this check will catch cases where the magic is not masked out ;). So the only case we don't catch is when a user passes a number < 32 as timer id - that will work still and IMHO it's desirable to maintain that behavior as Linux (some times) treats timer ids as u16.

Alex

Quoted 17 lines

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num, abi_long arg1, · line 9655

Quoted 11 lines
linux-user/syscall.c +5 -0
             ret = -TARGET_EINVAL;
         } else {
@@ -9640,6 +9648,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         /* args: timer_t timerid, struct itimerspec *curr_value */
         target_ulong timerid = arg1;
 
+        /* Convert QEMU provided timer ID back to internal 16bit index format */
+        if ((timerid & TIMER_MAGIC_MASK) == TIMER_MAGIC) {
+            timerid &= 0xffff;
+        }
+

Same here.

thanks
- PMM