Re: [PATCH 5/6] objtool: Add UACCESS validation

Andy Lutomirski
2019-02-25T15:53:19Z

On Mon, Feb 25, 2019 at 4:53 AM Peter Zijlstra <peterz@infradead.org> wrote:
>
> It is important that UACCESS regions are as small as possible;
> furthermore the UACCESS state is not scheduled, so doing anything that
> might directly call into the scheduler will cause random code to be
> ran with UACCESS enabled.
>
> Teach objtool too track UACCESS state and warn about any CALL made
> while UACCESS is enabled. This very much includes the __fentry__()
> tracing calls and __preempt_schedule() calls.
>
> Note that exceptions _do_ save/restore the UACCESS state, and therefore
> they can drive preemption. This also means that all exception handlers
> must have an otherwise dedundant UACCESS disable instruction;
> therefore ignore this warning for !STT_FUNC code (exception handlers
> are not normal functions).
>
> It also provides a UACCESS_SAFE() annotation which allows explicit
> annotation. This is meant to be used for future things like:
> unsafe_copy_{to,from}_user().
>
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
> include/linux/frame.h | 30 +++++++++++-
> tools/objtool/arch.h | 4 +
> tools/objtool/arch/x86/decode.c | 14 +++++
> tools/objtool/check.c | 100 ++++++++++++++++++++++++++++++++++++----
> tools/objtool/check.h | 2
> tools/objtool/elf.h | 1
> 6 files changed, 138 insertions(+), 13 deletions(-)
>
> --- a/include/linux/frame.h
> +++ b/include/linux/frame.h
> @@ -28,10 +28,38 @@ asm (".pushsection .discard.nonstd_frame
> ".byte 0\n\t"
> ".popsection\n\t");
>
> +/*
> + * This macro marks functions as UACCESS-safe, that is, it is safe to call from an
> + * UACCESS enabled region (typically user_access_begin() /
> + * user_access_end()).
> + *
> + * These functions in turn will only call UACCESS-safe functions themselves (which
> + * precludes tracing, including __fentry__ and scheduling, including
> + * preempt_enable).
> + *
> + * UACCESS-safe functions will obviously also not change UACCESS themselves.
> + */
> +#define UACCESS_SAFE(func) \
> + asm (".pushsection .discard.uaccess_safe_strtab, \"S\", @3\n\t" \
> + "999: .ascii \"" #func "\"\n\t" \
> + " .byte 0\n\t" \
> + ".popsection\n\t" \
> + ".pushsection .discard.uaccess_safe\n\t" \
> + ".long 999b - .\n\t" \
> + ".popsection")

Minor nit: using big numbers like 999: like this always bugs me. It
relies on there not being a macro nested inside or outside this that
uses the same number. My general preference is to do something like
.Ldescription_\@ instead.

Otherwise this looks conceptually good :)