[2/2] x86/mm/iommu/sva: Do not allow to set FORCE_TAGGED_SVA bit from outside

Message ID 20230403111020.3136-3-kirill.shutemov@linux.intel.com
State New
Headers
Series Couple of trivial fixes for LAM vs. SVA interaction |

Commit Message

Kirill A. Shutemov April 3, 2023, 11:10 a.m. UTC
  arch_prctl(ARCH_FORCE_TAGGED_SVA) overrides the default and allows LAM
and SVA to co-exist in the process. It is expected by called by the
process when it knows what it is doing.

arch_prctl() operates on the current process, but the same code is
reachable from ptrace where it can be called on arbitrary task.

Make it strict and only allow to set MM_CONTEXT_FORCE_TAGGED_SVA for the
current process.

Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Fixes: 23e5d9ec2bab ("x86/mm/iommu/sva: Make LAM and SVA mutually exclusive")
Suggested-by: Dmitry Vyukov <dvyukov@google.com>
---
 arch/x86/kernel/process_64.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Dmitry Vyukov April 3, 2023, 1:55 p.m. UTC | #1
On Mon, 3 Apr 2023 at 13:10, Kirill A. Shutemov
<kirill.shutemov@linux.intel.com> wrote:
>
> arch_prctl(ARCH_FORCE_TAGGED_SVA) overrides the default and allows LAM
> and SVA to co-exist in the process. It is expected by called by the
> process when it knows what it is doing.
>
> arch_prctl() operates on the current process, but the same code is
> reachable from ptrace where it can be called on arbitrary task.
>
> Make it strict and only allow to set MM_CONTEXT_FORCE_TAGGED_SVA for the
> current process.
>
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> Fixes: 23e5d9ec2bab ("x86/mm/iommu/sva: Make LAM and SVA mutually exclusive")
> Suggested-by: Dmitry Vyukov <dvyukov@google.com>
> ---
>  arch/x86/kernel/process_64.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
> index c7dfd727c9ec..cefac2d3a9f6 100644
> --- a/arch/x86/kernel/process_64.c
> +++ b/arch/x86/kernel/process_64.c
> @@ -885,6 +885,8 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
>         case ARCH_ENABLE_TAGGED_ADDR:
>                 return prctl_enable_tagged_addr(task->mm, arg2);
>         case ARCH_FORCE_TAGGED_SVA:
> +               if (current != task)
> +                       return -EINVAL;

prctl_enable_tagged_addr() checks "task->mm != current->mm".
Should we check the same here for consistency? Or also change the
check in prctl_enable_tagged_addr().

arch_prctl() can only do task==current, so I guess "current != task"
is a more reasonable check for prctl_enable_tagged_addr() as well.



>                 set_bit(MM_CONTEXT_FORCE_TAGGED_SVA, &task->mm->context.flags);
>                 return 0;
>         case ARCH_GET_MAX_TAG_BITS:
> --
> 2.39.2
>
  
Kirill A. Shutemov April 3, 2023, 2:31 p.m. UTC | #2
On Mon, Apr 03, 2023 at 03:55:09PM +0200, Dmitry Vyukov wrote:
> On Mon, 3 Apr 2023 at 13:10, Kirill A. Shutemov
> <kirill.shutemov@linux.intel.com> wrote:
> >
> > arch_prctl(ARCH_FORCE_TAGGED_SVA) overrides the default and allows LAM
> > and SVA to co-exist in the process. It is expected by called by the
> > process when it knows what it is doing.
> >
> > arch_prctl() operates on the current process, but the same code is
> > reachable from ptrace where it can be called on arbitrary task.
> >
> > Make it strict and only allow to set MM_CONTEXT_FORCE_TAGGED_SVA for the
> > current process.
> >
> > Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> > Fixes: 23e5d9ec2bab ("x86/mm/iommu/sva: Make LAM and SVA mutually exclusive")
> > Suggested-by: Dmitry Vyukov <dvyukov@google.com>
> > ---
> >  arch/x86/kernel/process_64.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
> > index c7dfd727c9ec..cefac2d3a9f6 100644
> > --- a/arch/x86/kernel/process_64.c
> > +++ b/arch/x86/kernel/process_64.c
> > @@ -885,6 +885,8 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
> >         case ARCH_ENABLE_TAGGED_ADDR:
> >                 return prctl_enable_tagged_addr(task->mm, arg2);
> >         case ARCH_FORCE_TAGGED_SVA:
> > +               if (current != task)
> > +                       return -EINVAL;
> 
> prctl_enable_tagged_addr() checks "task->mm != current->mm".
> Should we check the same here for consistency? Or also change the
> check in prctl_enable_tagged_addr().
> 
> arch_prctl() can only do task==current, so I guess "current != task"
> is a more reasonable check for prctl_enable_tagged_addr() as well.

As of now, prctl_enable_tagged_addr() doesn't have the task on hands. It
gets mm as input, so it cannot check the task directly. But functionally
it is the same check.

I would prefer to keep it this way. Unless anyone feels strongly about it.
  
Dmitry Vyukov April 3, 2023, 2:46 p.m. UTC | #3
On Mon, 3 Apr 2023 at 16:31, Kirill A. Shutemov <kirill@shutemov.name> wrote:
>
> On Mon, Apr 03, 2023 at 03:55:09PM +0200, Dmitry Vyukov wrote:
> > On Mon, 3 Apr 2023 at 13:10, Kirill A. Shutemov
> > <kirill.shutemov@linux.intel.com> wrote:
> > >
> > > arch_prctl(ARCH_FORCE_TAGGED_SVA) overrides the default and allows LAM
> > > and SVA to co-exist in the process. It is expected by called by the
> > > process when it knows what it is doing.
> > >
> > > arch_prctl() operates on the current process, but the same code is
> > > reachable from ptrace where it can be called on arbitrary task.
> > >
> > > Make it strict and only allow to set MM_CONTEXT_FORCE_TAGGED_SVA for the
> > > current process.
> > >
> > > Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> > > Fixes: 23e5d9ec2bab ("x86/mm/iommu/sva: Make LAM and SVA mutually exclusive")
> > > Suggested-by: Dmitry Vyukov <dvyukov@google.com>
> > > ---
> > >  arch/x86/kernel/process_64.c | 2 ++
> > >  1 file changed, 2 insertions(+)
> > >
> > > diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
> > > index c7dfd727c9ec..cefac2d3a9f6 100644
> > > --- a/arch/x86/kernel/process_64.c
> > > +++ b/arch/x86/kernel/process_64.c
> > > @@ -885,6 +885,8 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
> > >         case ARCH_ENABLE_TAGGED_ADDR:
> > >                 return prctl_enable_tagged_addr(task->mm, arg2);
> > >         case ARCH_FORCE_TAGGED_SVA:
> > > +               if (current != task)
> > > +                       return -EINVAL;
> >
> > prctl_enable_tagged_addr() checks "task->mm != current->mm".
> > Should we check the same here for consistency? Or also change the
> > check in prctl_enable_tagged_addr().
> >
> > arch_prctl() can only do task==current, so I guess "current != task"
> > is a more reasonable check for prctl_enable_tagged_addr() as well.
>
> As of now, prctl_enable_tagged_addr() doesn't have the task on hands. It
> gets mm as input, so it cannot check the task directly. But functionally
> it is the same check.
>
> I would prefer to keep it this way. Unless anyone feels strongly about it.

Fine with me.

Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
  

Patch

diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index c7dfd727c9ec..cefac2d3a9f6 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -885,6 +885,8 @@  long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
 	case ARCH_ENABLE_TAGGED_ADDR:
 		return prctl_enable_tagged_addr(task->mm, arg2);
 	case ARCH_FORCE_TAGGED_SVA:
+		if (current != task)
+			return -EINVAL;
 		set_bit(MM_CONTEXT_FORCE_TAGGED_SVA, &task->mm->context.flags);
 		return 0;
 	case ARCH_GET_MAX_TAG_BITS: