All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
@ 2014-08-22 11:19 Alexander Graf
  2014-08-22 11:33 ` Peter Maydell
  0 siblings, 1 reply; 9+ messages in thread
From: Alexander Graf @ 2014-08-22 11:19 UTC (permalink / raw)
  To: Riku Voipio; +Cc: qemu-devel, afaerber

We check whether the passed in counter value is negative on all calls
that involve g_posix_timers. However, we AND the value down to 16 bits
right before the check, so they can never be negative.

Simplify all the checks and remove the useless negativity check.

Signed-off-by: Alexander Graf <agraf@suse.de>
---
 linux-user/syscall.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index f6c887f..bb68dd4 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -9509,7 +9509,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
          * struct itimerspec * old_value */
         arg1 &= 0xffff;
-        if (arg3 == 0 || arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
+        if (arg3 == 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
             ret = -TARGET_EINVAL;
         } else {
             timer_t htimer = g_posix_timers[arg1];
@@ -9531,7 +9531,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         arg1 &= 0xffff;
         if (!arg2) {
             return -TARGET_EFAULT;
-        } else if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
+        } else if (arg1 >= ARRAY_SIZE(g_posix_timers)) {
             ret = -TARGET_EINVAL;
         } else {
             timer_t htimer = g_posix_timers[arg1];
@@ -9551,7 +9551,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     {
         /* args: timer_t timerid */
         arg1 &= 0xffff;
-        if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
+        if (arg1 >= ARRAY_SIZE(g_posix_timers)) {
             ret = -TARGET_EINVAL;
         } else {
             timer_t htimer = g_posix_timers[arg1];
@@ -9566,7 +9566,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     {
         /* args: timer_t timerid */
         arg1 &= 0xffff;
-        if (arg1 < 0 || arg1 >= ARRAY_SIZE(g_posix_timers)) {
+        if (arg1 >= ARRAY_SIZE(g_posix_timers)) {
             ret = -TARGET_EINVAL;
         } else {
             timer_t htimer = g_posix_timers[arg1];
-- 
1.7.12.4

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
  2014-08-22 11:19 [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range Alexander Graf
@ 2014-08-22 11:33 ` Peter Maydell
  2014-08-22 11:36   ` Andreas Färber
  2014-08-22 11:42   ` Alexander Graf
  0 siblings, 2 replies; 9+ messages in thread
From: Peter Maydell @ 2014-08-22 11:33 UTC (permalink / raw)
  To: Alexander Graf; +Cc: Riku Voipio, QEMU Developers, Andreas Färber

On 22 August 2014 12:19, Alexander Graf <agraf@suse.de> wrote:
> We check whether the passed in counter value is negative on all calls
> that involve g_posix_timers. However, we AND the value down to 16 bits
> right before the check, so they can never be negative.

...but why exactly are we doing that AND with 0xffff ?? It seems
unlikely that the kernel really allows random garbage in the top
half of the timer ID arguments, so maybe we should drop the
mask and keep the <0 bounds checks?

-- PMM

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
  2014-08-22 11:33 ` Peter Maydell
@ 2014-08-22 11:36   ` Andreas Färber
  2014-08-22 11:45     ` Peter Maydell
  2014-08-22 11:42   ` Alexander Graf
  1 sibling, 1 reply; 9+ messages in thread
From: Andreas Färber @ 2014-08-22 11:36 UTC (permalink / raw)
  To: Peter Maydell, Alexander Graf; +Cc: Riku Voipio, QEMU Developers

Am 22.08.2014 13:33, schrieb Peter Maydell:
> On 22 August 2014 12:19, Alexander Graf <agraf@suse.de> wrote:
>> We check whether the passed in counter value is negative on all calls
>> that involve g_posix_timers. However, we AND the value down to 16 bits
>> right before the check, so they can never be negative.
> 
> ...but why exactly are we doing that AND with 0xffff ?? It seems
> unlikely that the kernel really allows random garbage in the top
> half of the timer ID arguments, so maybe we should drop the
> mask and keep the <0 bounds checks?

Or maybe just use a local int16_t variable? I.e., should 0xffff match
the <0 check there or not?

Andreas

-- 
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
  2014-08-22 11:33 ` Peter Maydell
  2014-08-22 11:36   ` Andreas Färber
@ 2014-08-22 11:42   ` Alexander Graf
  2014-08-22 11:44     ` Peter Maydell
  1 sibling, 1 reply; 9+ messages in thread
From: Alexander Graf @ 2014-08-22 11:42 UTC (permalink / raw)
  To: Peter Maydell; +Cc: Riku Voipio, QEMU Developers, Andreas Färber



On 22.08.14 13:33, Peter Maydell wrote:
> On 22 August 2014 12:19, Alexander Graf <agraf@suse.de> wrote:
>> We check whether the passed in counter value is negative on all calls
>> that involve g_posix_timers. However, we AND the value down to 16 bits
>> right before the check, so they can never be negative.
> 
> ...but why exactly are we doing that AND with 0xffff ?? It seems
> unlikely that the kernel really allows random garbage in the top
> half of the timer ID arguments, so maybe we should drop the
> mask and keep the <0 bounds checks?

Or we drop the AND and and the <0 check and treat arg1 as unsigned ;).


Alex

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
  2014-08-22 11:42   ` Alexander Graf
@ 2014-08-22 11:44     ` Peter Maydell
  2014-08-22 11:45       ` Alexander Graf
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Maydell @ 2014-08-22 11:44 UTC (permalink / raw)
  To: Alexander Graf; +Cc: Riku Voipio, QEMU Developers, Andreas Färber

On 22 August 2014 12:42, Alexander Graf <agraf@suse.de> wrote:
> On 22.08.14 13:33, Peter Maydell wrote:
>> On 22 August 2014 12:19, Alexander Graf <agraf@suse.de> wrote:
>>> We check whether the passed in counter value is negative on all calls
>>> that involve g_posix_timers. However, we AND the value down to 16 bits
>>> right before the check, so they can never be negative.
>>
>> ...but why exactly are we doing that AND with 0xffff ?? It seems
>> unlikely that the kernel really allows random garbage in the top
>> half of the timer ID arguments, so maybe we should drop the
>> mask and keep the <0 bounds checks?
>
> Or we drop the AND and and the <0 check and treat arg1 as unsigned ;).

That probably just requires equally many changes to
code that is currently correct because the arg* are
signed but would need changes if they became unsigned.

-- PMM

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
  2014-08-22 11:36   ` Andreas Färber
@ 2014-08-22 11:45     ` Peter Maydell
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Maydell @ 2014-08-22 11:45 UTC (permalink / raw)
  To: Andreas Färber; +Cc: Riku Voipio, Alexander Graf, QEMU Developers

On 22 August 2014 12:36, Andreas Färber <afaerber@suse.de> wrote:
> Am 22.08.2014 13:33, schrieb Peter Maydell:
>> On 22 August 2014 12:19, Alexander Graf <agraf@suse.de> wrote:
>>> We check whether the passed in counter value is negative on all calls
>>> that involve g_posix_timers. However, we AND the value down to 16 bits
>>> right before the check, so they can never be negative.
>>
>> ...but why exactly are we doing that AND with 0xffff ?? It seems
>> unlikely that the kernel really allows random garbage in the top
>> half of the timer ID arguments, so maybe we should drop the
>> mask and keep the <0 bounds checks?
>
> Or maybe just use a local int16_t variable? I.e., should 0xffff match
> the <0 check there or not?

The kernel seems to use 'int' for the timer id type, which suggests
that this mask is just wrong.

-- PMM

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
  2014-08-22 11:44     ` Peter Maydell
@ 2014-08-22 11:45       ` Alexander Graf
  2014-08-22 11:49         ` Peter Maydell
  0 siblings, 1 reply; 9+ messages in thread
From: Alexander Graf @ 2014-08-22 11:45 UTC (permalink / raw)
  To: Peter Maydell; +Cc: Riku Voipio, QEMU Developers, Andreas Färber



On 22.08.14 13:44, Peter Maydell wrote:
> On 22 August 2014 12:42, Alexander Graf <agraf@suse.de> wrote:
>> On 22.08.14 13:33, Peter Maydell wrote:
>>> On 22 August 2014 12:19, Alexander Graf <agraf@suse.de> wrote:
>>>> We check whether the passed in counter value is negative on all calls
>>>> that involve g_posix_timers. However, we AND the value down to 16 bits
>>>> right before the check, so they can never be negative.
>>>
>>> ...but why exactly are we doing that AND with 0xffff ?? It seems
>>> unlikely that the kernel really allows random garbage in the top
>>> half of the timer ID arguments, so maybe we should drop the
>>> mask and keep the <0 bounds checks?
>>
>> Or we drop the AND and and the <0 check and treat arg1 as unsigned ;).
> 
> That probably just requires equally many changes to
> code that is currently correct because the arg* are
> signed but would need changes if they became unsigned.

Well, I do have a downstream patch that makes them unsigned, so I'd
rather like to make the code as stable to that as I can ;).


Alex

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
  2014-08-22 11:45       ` Alexander Graf
@ 2014-08-22 11:49         ` Peter Maydell
  2014-08-22 11:52           ` Alexander Graf
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Maydell @ 2014-08-22 11:49 UTC (permalink / raw)
  To: Alexander Graf; +Cc: Riku Voipio, QEMU Developers, Andreas Färber

On 22 August 2014 12:45, Alexander Graf <agraf@suse.de> wrote:
> On 22.08.14 13:44, Peter Maydell wrote:
>> On 22 August 2014 12:42, Alexander Graf <agraf@suse.de> wrote:
>>> Or we drop the AND and and the <0 check and treat arg1 as unsigned ;).
>>
>> That probably just requires equally many changes to
>> code that is currently correct because the arg* are
>> signed but would need changes if they became unsigned.
>
> Well, I do have a downstream patch that makes them unsigned, so I'd
> rather like to make the code as stable to that as I can ;).

Yeah, I know. When I was looking through your patch tree
I saw that one and my reaction was "why on earth did
you do that?"...

-- PMM

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range
  2014-08-22 11:49         ` Peter Maydell
@ 2014-08-22 11:52           ` Alexander Graf
  0 siblings, 0 replies; 9+ messages in thread
From: Alexander Graf @ 2014-08-22 11:52 UTC (permalink / raw)
  To: Peter Maydell; +Cc: Riku Voipio, QEMU Developers, Andreas Färber



On 22.08.14 13:49, Peter Maydell wrote:
> On 22 August 2014 12:45, Alexander Graf <agraf@suse.de> wrote:
>> On 22.08.14 13:44, Peter Maydell wrote:
>>> On 22 August 2014 12:42, Alexander Graf <agraf@suse.de> wrote:
>>>> Or we drop the AND and and the <0 check and treat arg1 as unsigned ;).
>>>
>>> That probably just requires equally many changes to
>>> code that is currently correct because the arg* are
>>> signed but would need changes if they became unsigned.
>>
>> Well, I do have a downstream patch that makes them unsigned, so I'd
>> rather like to make the code as stable to that as I can ;).
> 
> Yeah, I know. When I was looking through your patch tree
> I saw that one and my reaction was "why on earth did
> you do that?"...

I don't fully remember all the glorious details either - and there's a
good reason I never pushed it upstream :). It seemed to make the code
more robust though.

Maybe we'll just ditch it again sooner or later. Or push it upstream and
make unsigned the default (which IMHO is a lot more sane, you get way
less unwanted side effects).


Alex

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-08-22 11:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-22 11:19 [Qemu-devel] [PATCH] linux-user: Simplify boundary checks on g_posix_timers range Alexander Graf
2014-08-22 11:33 ` Peter Maydell
2014-08-22 11:36   ` Andreas Färber
2014-08-22 11:45     ` Peter Maydell
2014-08-22 11:42   ` Alexander Graf
2014-08-22 11:44     ` Peter Maydell
2014-08-22 11:45       ` Alexander Graf
2014-08-22 11:49         ` Peter Maydell
2014-08-22 11:52           ` Alexander Graf

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.