Re: [PATCH] ubsan: mark ubsan_type_mismatch_common inline

From: Peter Zijlstra
Date: Mon Jun 17 2019 - 10:07:10 EST

On Mon, Jun 17, 2019 at 02:31:09PM +0200, Arnd Bergmann wrote:
> objtool points out a condition that it does not like:
> lib/ubsan.o: warning: objtool: __ubsan_handle_type_mismatch()+0x4a: call to stackleak_track_stack() with UACCESS enabled
> lib/ubsan.o: warning: objtool: __ubsan_handle_type_mismatch_v1()+0x4a: call to stackleak_track_stack() with UACCESS enabled
> I guess this is related to the call ubsan_type_mismatch_common()
> not being inline before it calls user_access_restore(), though
> I don't fully understand why that is a problem.

The rules are that when AC is set, one is not allowed to CALL schedule,
because scheduling does not save/restore AC. Preemption, through the
exceptions is fine, because the exceptions do save/restore AC.

And while most functions do not appear to call into schedule, function
trace ensures that every single call does in fact call into schedule.
Therefore any CALL (with AC set) is invalid.

> Marking the function inline shuts up the warning and might be
> the right thing to do. The patch that caused this is marked
> for stable backports, so this one should probably be backported
> as well.

This appears to be a 'fun' interaction between different checkers. What
happens is that __ubsan_handle_type_mismatch*() calls into
stackleak_track_stack() because it has an on-stack variable. It does
this before calling ubsan_type_mismatch_common().

ubsan_type_mismatch_common() does user_access_save/restore which
saves/restores AC and allows 'normal' code to be ran.

With the proposed __always_inline, the code generation changes such that
we run user_access_save() _before_ stackleack_track_stack() (for,
afaict, undefined raisins), and the warning goes away.

Maybe we should disable stackleak when building ubsan instead? We
already disable stack-protector when building ubsan.

> Fixes: 42440c1f9911 ("lib/ubsan: add type mismatch handler for new GCC/Clang")

I don't think this is quite right, because back then there wasn't any
uaccess validation.

> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Arnd Bergmann <arnd@xxxxxxxx>
> ---
> lib/ubsan.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
> diff --git a/lib/ubsan.c b/lib/ubsan.c
> index ecc179338094..3d8836f0fc5c 100644
> --- a/lib/ubsan.c
> +++ b/lib/ubsan.c
> @@ -309,7 +309,7 @@ static void handle_object_size_mismatch(struct type_mismatch_data_common *data,
> ubsan_epilogue(&flags);
> }
> -static void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
> +static __always_inline void ubsan_type_mismatch_common(struct type_mismatch_data_common *data,
> unsigned long ptr)
> {
> unsigned long flags = user_access_save();