Skip to contentSkip to conversation

linux-user: rt_sigprocmask, check read perms first

PATCH Superseded Patrick Venture 2 participants 6 messages Jan 6, 2022 – Jan 26, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: rt_sigprocmask, check read perms first

From: Shu-Chun Weng <[email protected]>

Linux kernel does it this way (checks read permission before validating how) and the latest version of ABSL's AddressIsReadable() depends on this behavior.

c.f. https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147

---
 linux-user/syscall.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
linux-user/syscall.c +5 -5
index ce9d64896c..3070d31f34 100644
@@ -9491,6 +9491,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
             }
 
             if (arg2) {
+                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
+                    return -TARGET_EFAULT;
+                target_to_host_sigset(&set, p);
+                unlock_user(p, arg2, 0);
+                set_ptr = &set;
                 switch(how) {
                 case TARGET_SIG_BLOCK:
                     how = SIG_BLOCK;
@@ -9504,11 +9509,6 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                 default:
                     return -TARGET_EINVAL;
                 }
-                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
-                    return -TARGET_EFAULT;
-                target_to_host_sigset(&set, p);
-                unlock_user(p, arg2, 0);
-                set_ptr = &set;
             } else {
                 how = 0;
                 set_ptr = NULL;
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
#1 lore raw

Le 06/01/2022 à 23:00, Patrick Venture a écrit :

Quoted 31 lines

From: Shu-Chun Weng <[email protected]>

Linux kernel does it this way (checks read permission before validating how) and the latest version of ABSL's AddressIsReadable() depends on this behavior.

c.f. https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147

---
  linux-user/syscall.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
linux-user/syscall.c +5 -0
index ce9d64896c..3070d31f34 100644
@@ -9491,6 +9491,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
              }
 
              if (arg2) {
+                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
+                    return -TARGET_EFAULT;
+                target_to_host_sigset(&set, p);
+                unlock_user(p, arg2, 0);
+                set_ptr = &set;
                  switch(how) {
                  case TARGET_SIG_BLOCK:
                      how = SIG_BLOCK;
@@ -9504,11 +9509,6 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                  default:

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 9514

Quoted 10 lines
linux-user/syscall.c +0 -5
                      return -TARGET_EINVAL;
                  }
-                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
-                    return -TARGET_EFAULT;
-                target_to_host_sigset(&set, p);
-                unlock_user(p, arg2, 0);
-                set_ptr = &set;
              } else {
                  how = 0;
                  set_ptr = NULL;

I know it's only code move but generally we also update the style to pass scripts/checkpatch.pl successfully.

Could you also update TARGET_NR_sigprocmask in the same way as it seems the kernel behaves like this too in this case?

Thanks,
Laurent

#2 lore raw
Quoted 49 lines · Laurent Vivier
Quoted 3 lines · Patrick Venture

From: Shu-Chun Weng <[email protected]>

Linux kernel does it this way (checks read permission before validating

how)

Quoted 4 lines

and the latest version of ABSL's AddressIsReadable() depends on this behavior.

c.f.

https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int

Quoted 11 lines
---
  linux-user/syscall.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
linux-user/syscall.c +0 -0
index ce9d64896c..3070d31f34 100644
@@ -9491,6 +9491,11 @@ static abi_long do_syscall1(void *cpu_env, int

num, abi_long arg1,

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,

Quoted 4 lines
linux-user/syscall.c +1 -0
              }
 
              if (arg2) {
+                if (!(p = lock_user(VERIFY_READ, arg2,

sizeof(target_sigset_t), 1)))

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int

Quoted 8 lines
linux-user/syscall.c +4 -0
+                    return -TARGET_EFAULT;
+                target_to_host_sigset(&set, p);
+                unlock_user(p, arg2, 0);
+                set_ptr = &set;
                  switch(how) {
                  case TARGET_SIG_BLOCK:
                      how = SIG_BLOCK;
@@ -9504,11 +9509,6 @@ static abi_long do_syscall1(void *cpu_env, int

num, abi_long arg1,

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,

Quoted 4 lines
linux-user/syscall.c +0 -1
                  default:
                      return -TARGET_EINVAL;
                  }
-                if (!(p = lock_user(VERIFY_READ, arg2,

sizeof(target_sigset_t), 1)))

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 9514

Quoted 7 lines
linux-user/syscall.c +0 -4
-                    return -TARGET_EFAULT;
-                target_to_host_sigset(&set, p);
-                unlock_user(p, arg2, 0);
-                set_ptr = &set;
              } else {
                  how = 0;
                  set_ptr = NULL;
Quoted 4 lines

I know it's only code move but generally we also update the style to pass scripts/checkpatch.pl
successfully.

That is a reasonable request, however, can I just send a follow-on patch? I didn't write this one and I honestly don't know much about it, but I don't mind doing the cleanup

Quoted 5 lines

Could you also update TARGET_NR_sigprocmask in the same way as it seems the kernel behaves like this
too in this case?

I can take a look. I would prefer then to also prefetch the style fixup in a preceding patch. I don't recall seeing whether qemu supports clang-format.

Quoted 4 lines

Thanks,
Laurent

Patrick

#3 lore raw

Hi Patrick,

Le 11/01/2022 à 21:14, Patrick Venture a écrit :

Quoted 57 lines · On Sat, Jan 8, 2022 at 10:16 AM Laurent Vivier <[email protected]…
    Le 06/01/2022 à 23:00, Patrick Venture a écrit :
     > From: Shu-Chun Weng <[email protected] <mailto:[email protected]>>
     >
     > Linux kernel does it this way (checks read permission before validating `how`)
     > and the latest version of ABSL's `AddressIsReadable()` depends on this
     > behavior.
     >
     > c.f.
    https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147
    <https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147>
     > Reviewed-by: Patrick Venture <[email protected] <mailto:[email protected]>>
     > Signed-off-by: Shu-Chun Weng <[email protected] <mailto:[email protected]>>
     > ---
     >   linux-user/syscall.c | 10 +++++-----
     >   1 file changed, 5 insertions(+), 5 deletions(-)
     >
     > diff --git a/linux-user/syscall.c b/linux-user/syscall.c
     > index ce9d64896c..3070d31f34 100644
     > --- a/linux-user/syscall.c
     > +++ b/linux-user/syscall.c
     > @@ -9491,6 +9491,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
     >               }
     >
     >               if (arg2) {
     > +                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
     > +                    return -TARGET_EFAULT;
     > +                target_to_host_sigset(&set, p);
     > +                unlock_user(p, arg2, 0);
     > +                set_ptr = &set;
     >                   switch(how) {
     >                   case TARGET_SIG_BLOCK:
     >                       how = SIG_BLOCK;
     > @@ -9504,11 +9509,6 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
     >                   default:
     >                       return -TARGET_EINVAL;
     >                   }
     > -                if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
     > -                    return -TARGET_EFAULT;
     > -                target_to_host_sigset(&set, p);
     > -                unlock_user(p, arg2, 0);
     > -                set_ptr = &set;
     >               } else {
     >                   how = 0;
     >                   set_ptr = NULL;

    I know it's only code move but generally we also update the style to pass scripts/checkpatch.pl
    <http://checkpatch.pl>
    successfully.

That is a reasonable request, however, can I just send a follow-on patch?  I didn't write this one and I honestly don't know much about it, but I don't mind doing the cleanup

Quoted 7 lines

Could you also update TARGET_NR_sigprocmask in the same way as it seems the kernel behaves like this
too in this case?

I can take a look.  I would prefer then to also prefetch the style fixup in a preceding patch. I don't recall seeing whether qemu supports clang-format.

There is no problem. You can keep this patch unmodified, and add patches to fix the problems.

I only ask to have all the patches in one series.

Thanks,
Laurent

#4 lore raw
Quoted 83 lines · Laurent Vivier

Hi Patrick,

Quoted 3 lines · Patrick Venture

On Sat, Jan 8, 2022 at 10:16 AM Laurent Vivier <[email protected]

Quoted 5 lines · <mailto:[email protected]>> wrote:

Le 06/01/2022 à 23:00, Patrick Venture a écrit :
> From: Shu-Chun Weng <[email protected] <mailto:[email protected]>> >
> Linux kernel does it this way (checks read permission before

validating how)

Quoted 1 line

> and the latest version of ABSL's AddressIsReadable() depends on

this

Quoted 4 lines
     > behavior.
     >
     > c.f.

https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147

Quoted 1 line
    <

https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147

Quoted 2 lines

> Reviewed-by: Patrick Venture <[email protected] <mailto:

[email protected]>>

Quoted 1 line

> Signed-off-by: Shu-Chun Weng <[email protected] <mailto:

[email protected]>>

Quoted 9 lines
     > ---
     >   linux-user/syscall.c | 10 +++++-----
     >   1 file changed, 5 insertions(+), 5 deletions(-)
     >
     > diff --git a/linux-user/syscall.c b/linux-user/syscall.c
     > index ce9d64896c..3070d31f34 100644
     > --- a/linux-user/syscall.c
     > +++ b/linux-user/syscall.c
     > @@ -9491,6 +9491,11 @@ static abi_long do_syscall1(void *cpu_env,

int num, abi_long arg1,

Quoted 4 lines
     >               }
     >
     >               if (arg2) {
     > +                if (!(p = lock_user(VERIFY_READ, arg2,

sizeof(target_sigset_t), 1)))

Quoted 8 lines
     > +                    return -TARGET_EFAULT;
     > +                target_to_host_sigset(&set, p);
     > +                unlock_user(p, arg2, 0);
     > +                set_ptr = &set;
     >                   switch(how) {
     >                   case TARGET_SIG_BLOCK:
     >                       how = SIG_BLOCK;
     > @@ -9504,11 +9509,6 @@ static abi_long do_syscall1(void *cpu_env,

int num, abi_long arg1,

Quoted 4 lines
     >                   default:
     >                       return -TARGET_EINVAL;
     >                   }
     > -                if (!(p = lock_user(VERIFY_READ, arg2,

sizeof(target_sigset_t), 1)))

Quoted 9 lines
     > -                    return -TARGET_EFAULT;
     > -                target_to_host_sigset(&set, p);
     > -                unlock_user(p, arg2, 0);
     > -                set_ptr = &set;
     >               } else {
     >                   how = 0;
     >                   set_ptr = NULL;

    I know it's only code move but generally we also update the style to

pass scripts/checkpatch.pl

Quoted 5 lines
    <http://checkpatch.pl>
    successfully.

That is a reasonable request, however, can I just send a follow-on

patch? I didn't write this one

Quoted 1 line

and I honestly don't know much about it, but I don't mind doing the

cleanup

Quoted 3 lines

Could you also update TARGET_NR_sigprocmask in the same way as it

seems the kernel behaves like

Quoted 5 lines

this
too in this case?

I can take a look. I would prefer then to also prefetch the style fixup

Quoted 9 lines

in a preceding patch. I

Quoted 2 lines

don't recall seeing whether qemu supports clang-format.

There is no problem. You can keep this patch unmodified, and add patches to fix the problems.

I only ask to have all the patches in one series.

Will take a swing at this for v2.

Quoted 5 lines

Thanks,
Laurent

#5 lore raw
Quoted 91 lines · Patrick Venture
Quoted 87 lines · Laurent Vivier

Hi Patrick,

Quoted 3 lines · Patrick Venture

On Sat, Jan 8, 2022 at 10:16 AM Laurent Vivier <[email protected]

Quoted 5 lines · <mailto:[email protected]>> wrote:

Le 06/01/2022 à 23:00, Patrick Venture a écrit :
> From: Shu-Chun Weng <[email protected] <mailto:[email protected]>> >
> Linux kernel does it this way (checks read permission before

validating how)

Quoted 1 line

> and the latest version of ABSL's AddressIsReadable() depends

on this

Quoted 4 lines
     > behavior.
     >
     > c.f.

https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147

Quoted 1 line
    <

https://github.com/torvalds/linux/blob/9539ba4308ad5bdca6cb41c7b73cbb9f796dcdd7/kernel/signal.c#L3147

Quoted 2 lines

> Reviewed-by: Patrick Venture <[email protected] <mailto:

[email protected]>>

Quoted 1 line

> Signed-off-by: Shu-Chun Weng <[email protected] <mailto:

[email protected]>>

Quoted 9 lines
     > ---
     >   linux-user/syscall.c | 10 +++++-----
     >   1 file changed, 5 insertions(+), 5 deletions(-)
     >
     > diff --git a/linux-user/syscall.c b/linux-user/syscall.c
     > index ce9d64896c..3070d31f34 100644
     > --- a/linux-user/syscall.c
     > +++ b/linux-user/syscall.c
     > @@ -9491,6 +9491,11 @@ static abi_long do_syscall1(void

*cpu_env, int num, abi_long arg1,

Quoted 4 lines
     >               }
     >
     >               if (arg2) {
     > +                if (!(p = lock_user(VERIFY_READ, arg2,

sizeof(target_sigset_t), 1)))

Quoted 8 lines
     > +                    return -TARGET_EFAULT;
     > +                target_to_host_sigset(&set, p);
     > +                unlock_user(p, arg2, 0);
     > +                set_ptr = &set;
     >                   switch(how) {
     >                   case TARGET_SIG_BLOCK:
     >                       how = SIG_BLOCK;
     > @@ -9504,11 +9509,6 @@ static abi_long do_syscall1(void

*cpu_env, int num, abi_long arg1,

Quoted 4 lines
     >                   default:
     >                       return -TARGET_EINVAL;
     >                   }
     > -                if (!(p = lock_user(VERIFY_READ, arg2,

sizeof(target_sigset_t), 1)))

Quoted 9 lines
     > -                    return -TARGET_EFAULT;
     > -                target_to_host_sigset(&set, p);
     > -                unlock_user(p, arg2, 0);
     > -                set_ptr = &set;
     >               } else {
     >                   how = 0;
     >                   set_ptr = NULL;

    I know it's only code move but generally we also update the style

to pass scripts/checkpatch.pl

Quoted 5 lines
    <http://checkpatch.pl>
    successfully.

That is a reasonable request, however, can I just send a follow-on

patch? I didn't write this one

Quoted 1 line

and I honestly don't know much about it, but I don't mind doing the

cleanup

Quoted 3 lines

Could you also update TARGET_NR_sigprocmask in the same way as it

seems the kernel behaves like

Quoted 5 lines

this
too in this case?

I can take a look. I would prefer then to also prefetch the style

fixup in a preceding patch. I

Quoted 2 lines

don't recall seeing whether qemu supports clang-format.

Quoted 8 lines
Quoted 5 lines

There is no problem. You can keep this patch unmodified, and add patches to fix the problems.

I only ask to have all the patches in one series.

Will take a swing at this for v2.

Laurent,
I spent some time today going over the patches and digging into what they're actually doing and I think I can make this two patches, both with the style changes squashed and will send in a v2 today.

Thanks

Quoted 7 lines
Quoted 7 lines

Thanks,
Laurent