Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1467191

Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault

From Linus Torvalds <torvalds@linux-foundation.org>
Newsgroups linux.kernel
Subject Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault
Date 2016-08-21 20:00 +0200
Message-ID <s8Fcl-3qf-11@gated-at.bofh.it> (permalink)
References (4 earlier) <s8o1P-140-3@gated-at.bofh.it> <s8oEy-1xs-1@gated-at.bofh.it> <s8p7z-1If-1@gated-at.bofh.it> <s8t1w-4d5-1@gated-at.bofh.it> <s8uJX-5jE-7@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Sat, Aug 20, 2016 at 11:42 PM, Al Viro <viro@zeniv.linux.org.uk> wrote:
>
> It's not exactly setjmp/longjmp; what I had in mind was along the lines of

That ends up having all the exact same issues as setjmp, and generally
you *do* want the compiler to know about it.

For example, let's say that you have something like

    if (start())
        return -EFAULT;

    ... do things that can fault and trigger an exception ..

    stop();
    return 0;

then it doesn't matter that "start" clobbers all memory and registers,
if it returns twice the code generation by a compiler that doesn't
know about the magical setjmp-like behavior can trigger bugs.

For example, the most common case is that lots of compilers try to
share the final return-point - sometimes because of instrumentation,
sometimes just because there's a big stack frame and the return is a
lot of pop instructions and stack undo code.

And *particularly* if your magical 'start()' function has an inline
asm that clobbers memory and registers, the compiler will have to
spill state to stack around it - but part of the spill might be the
return value that it had in a register.

So the compiler might end up generating code like this:

         movl $-EFAULT,8(%rbp)    # retval
         call start
         testl %eax,%eax
         jne return_point;

         ...

         movl $0,8(%rbp)    # retval

         ....

    return_point:
        .. pop-pop-pop-whatever ..
        movl 4(%rbp),%eax
        .. more stack frame cleanup ..
        ret

and notice how if "start()" returns a second time - even if it
restored all registers including the stack pointer - the function
might return the wrong error value if the exception that caused
longjmp happened after the code that had updated the return.

There are lots of other ways a setjmp() point is special. Some
compilers might push/pop values just temporarily around a call, so you
might have sequences like

     pushq %rdx
     call fn
     popq %rdx

where the compiler wanted to save register %rdx around the call (I've
never actually seen gcc generate that code, the exact same thing may
happen with just random register spills).

Again, that fails completely in the presence of a function that
returns twice - even it the stack pointer itself gets reset, the stack
*contents* that the code pops the saved value of %rdx might have been
re-used for something else (and for a register spill, the frame slot
might have been re-used). So now you're restoring garbage.

So the interface you propose is in fact *exactly* the same as setjmp,
and we'd need to make sure that the compiler knows that.

                       Linus

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Vineet Gupta <Vineet.Gupta1@synopsys.com> - 2016-08-19 21:20 +0200
  Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Al Viro <viro@ZenIV.linux.org.uk> - 2016-08-19 23:30 +0200
    Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Linus Torvalds <torvalds@linux-foundation.org> - 2016-08-20 00:10 +0200
      Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Linus Torvalds <torvalds@linux-foundation.org> - 2016-08-20 00:20 +0200
        Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Linus Torvalds <torvalds@linux-foundation.org> - 2016-08-21 01:40 +0200
          Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Al Viro <viro@ZenIV.linux.org.uk> - 2016-08-21 02:20 +0200
            Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Linus Torvalds <torvalds@linux-foundation.org> - 2016-08-21 02:50 +0200
              Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Linus Torvalds <torvalds@linux-foundation.org> - 2016-08-21 03:10 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault "H. Peter Anvin" <hpa@zytor.com> - 2016-08-21 03:10 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Al Viro <viro@ZenIV.linux.org.uk> - 2016-08-21 03:50 +0200
              Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Jakub Jelinek <jakub@redhat.com> - 2016-08-21 07:00 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Al Viro <viro@ZenIV.linux.org.uk> - 2016-08-21 08:50 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Linus Torvalds <torvalds@linux-foundation.org> - 2016-08-21 20:00 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Linus Torvalds <torvalds@linux-foundation.org> - 2016-08-23 00:30 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault "H. Peter Anvin" <hpa@zytor.com> - 2016-08-23 01:20 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Linus Torvalds <torvalds@linux-foundation.org> - 2016-08-23 01:50 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of  fault David Miller <davem@davemloft.net> - 2016-08-23 02:00 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault "H. Peter Anvin" <hpa@zytor.com> - 2016-08-23 02:10 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault "H. Peter Anvin" <hpa@zytor.com> - 2016-08-23 02:00 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault Al Viro <viro@ZenIV.linux.org.uk> - 2016-08-23 02:20 +0200
                Re: [PATCH] ARC: uaccess: get_user to zero out dest in cause of fault "H. Peter Anvin" <hpa@zytor.com> - 2016-08-23 01:20 +0200

csiph-web