Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp731180lqd; Wed, 24 Apr 2024 15:32:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUBo+MGZ3Bdv12sDnVLw2jUtVK7mNeOXhHws+LYJgJqvVs7mxyy8G2L0VMTcdPeRndL2uATBftiv5GIKzPOsao88OcEC9QNR7bDmFTBWw== X-Google-Smtp-Source: AGHT+IG95HH0vBVLJ4PIL5w0JaSPfjwJAGVBQePgFXVvDjKxN/DZfXTASrDysWIG+1/HBGfsK6Pu X-Received: by 2002:ac8:5984:0:b0:43a:387:a005 with SMTP id e4-20020ac85984000000b0043a0387a005mr4216798qte.57.1713997941202; Wed, 24 Apr 2024 15:32:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713997941; cv=pass; d=google.com; s=arc-20160816; b=Kc1aofNaZGBeQRLF3DBG9NygMggK22nYKekk3xBL7F1eQ4OzgM9s61BQraJqI0w1ig G0rAqw7OMtxSEAhyS//eWWiAYt4WsRrsTjgD573/DYGNwEGztnzAh3hFJnOn9dINnHeg KwL6L7Bk/DZLbBJdKtbbdwoz7FcW3B6b9MAcnKcFBG7AuYPRfBbEoU+B/gJ/o+RunCAQ bVxr21rFYro1w4Ti3a/yy8zE/MGwUqAJFEEVp6IX/v/2sfSTknLPnD9mgvck9lYVSQyE M5Lm8zLR6scmEutjzn2Yc/KsNmpt6EDjM0hgXeRAGvO0VbRg2ZGQUWJydcgnidyPIyfF sgKg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=W5XW2EeZa2xzGai521vbraJwVDegbnZlLcrpCRyHG1Q=; fh=r5ILp/2lkxMHorAQIQxBk20BxpcHa8lYzyOlSqRvmF4=; b=RiH5C19lGcDZzkqgj0ujDN/6L2XeHe3l8DsCeqLBhQWSmIvxH1/BYrlVu8ymJU4lnD DqNRCcyTVbadohyWkMvdeQZnh02s2VT5WhXmuq16+Xp7TA9ErCs/Uceq6n8TUQnGkiZu 5BgFPzrzalsuJTAM+PNuWuve9w84w5piJRrKXsKRarOPpoL+J2K7ojIhzO7NGX8zHcGR kHlk6JM+oiNsfNZrr/SCrtVKNMYSn+gQSz+Tio1rpBsvBpFkie0W5uhze5WhSmL1YVx1 2Eyd5esIrv/V94Qa+kLgXMJJdGJvUh/ZE9ZVYFKklSlUIQCwZbDM+ykQW8pht8IM3P5I vPzw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Kx+gIttH; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-157787-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157787-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id yf13-20020a05620a3bcd00b0078d699ba683si15677145qkn.635.2024.04.24.15.32.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 15:32:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-157787-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Kx+gIttH; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-157787-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-157787-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D02A91C22448 for ; Wed, 24 Apr 2024 22:32:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9733C15ECEE; Wed, 24 Apr 2024 22:32:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Kx+gIttH" Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C11A23BE for ; Wed, 24 Apr 2024 22:32:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713997933; cv=none; b=Ai38brRmrnsy9HjJNSjyNzpUcTj37Vwr1OPvAxcy9esFhtRDUEFA/zeMY3a5MB87sXwU0DQBZX8ibq1WqL9htDJzNF7iOekgXcEmts16Tjlfyga4Pt76PsHh2lfgSAiPGoQut6fc0VkUh2AgDJvXOmINmPw0joQQXTfEhPwPOjs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713997933; c=relaxed/simple; bh=uvZEBkA/MPyDoHJ5/C9wqvZpYhJJlinnsxmUnLpv1is=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=g34USzUlHnHerV0Bff2oSYU8lXkI7iLzz01Dz97e0Lqj9sFW8nctcw3Xtp4UOWQD6QrbdGfVtdiH1eaotdBy5IpMrPW3xtxTkuv7vkwAz/vKzZG6JqWEQgfRTHA2vUfNlgzW0oYu8g1lWyVIa/kFQMgxStikuqAv1VUUkIh2i68= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Kx+gIttH; arc=none smtp.client-ip=209.85.210.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6ed691fb83eso365591b3a.1 for ; Wed, 24 Apr 2024 15:32:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1713997931; x=1714602731; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=W5XW2EeZa2xzGai521vbraJwVDegbnZlLcrpCRyHG1Q=; b=Kx+gIttHeoiuuNGI10tI2jPgH1H8bQ7BfIHaBcOV8UOYyeTtHrs8UQMluz7MU8wM1F ZvgJCsx7aSoDHM/eTD9ildmKsGHa/e6MBoQd7JRT28WJMVM6YcmX6d4Rav84A1cw9uxJ Bhfh6uqIyrvSLAbvl8S8KKLnlVyNOgdQQHu1g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713997931; x=1714602731; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=W5XW2EeZa2xzGai521vbraJwVDegbnZlLcrpCRyHG1Q=; b=pO4bY/uU7MY+Sxj4XXazNPtUlfFEEgaVG3G/j519XJx0g+gwJ7JVg4luJYbtVpNQKp I/OWB8ek4KVK5ny2EJaoHPhYH11axkEYe2jIo1g2i9NDwUbz9z1CN1Ex997KoSDrI1n9 9X/SzJLJMuptQoV8dYjNn6EaZ9IozjjnSCLDUWaWI4qQgEuZ/2c1X91vnY04A/6qCAks 06AI+RaFXcmjv57Xa9zrGcl/z5J6QDgRNwfxpmTXiKrv7bZrH5sj8aT/A43e05UuSCHx veVhK4/h/p1Psaqh8+FOxqBThm88PXeH8lOIRtFlBQpTKXZyILz7DJC6wHxslHLeUWn7 97YA== X-Forwarded-Encrypted: i=1; AJvYcCVMECEfGEk/Z4eKHVvNdYGnfrWpKIHjLXsrUx82+qOiO0vxAHAraSErFXoAqEecuqK7HyLf79NNtPImOkoHSi07l4aCVW9tkdZA7XQk X-Gm-Message-State: AOJu0YwtUEkZlsMa5rOwQZjgCbbDXiuWPk+OtcathTa3OkuwudK2SkcO imnZKeRxtlPQu88W8GebnevKC3fiPawbEvvVAwYgRh8TpTTy/TcpqkVnpZ8peA== X-Received: by 2002:a05:6a20:de89:b0:1a7:870a:86eb with SMTP id la9-20020a056a20de8900b001a7870a86ebmr3750218pzb.15.1713997931478; Wed, 24 Apr 2024 15:32:11 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id u12-20020a056a00098c00b006f09d5807ebsm11488861pfg.82.2024.04.24.15.32.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 15:32:10 -0700 (PDT) Date: Wed, 24 Apr 2024 15:32:10 -0700 From: Kees Cook To: Nathan Chancellor Cc: Marco Elver , Andrey Konovalov , Andrey Ryabinin , Nick Desaulniers , Bill Wendling , Justin Stitt , llvm@lists.linux.dev, kasan-dev@googlegroups.com, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ubsan: Avoid i386 UBSAN handler crashes with Clang Message-ID: <202404241530.A26FA3CC2@keescook> References: <20240424162942.work.341-kees@kernel.org> <20240424192652.GA3341665@dev-arch.thelio-3990X> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240424192652.GA3341665@dev-arch.thelio-3990X> On Wed, Apr 24, 2024 at 12:26:52PM -0700, Nathan Chancellor wrote: > Hi Kees, > > On Wed, Apr 24, 2024 at 09:29:43AM -0700, Kees Cook wrote: > > When generating Runtime Calls, Clang doesn't respect the -mregparm=3 > > option used on i386. Hopefully this will be fixed correctly in Clang 19: > > https://github.com/llvm/llvm-project/pull/89707 > > but we need to fix this for earlier Clang versions today. Force the > > calling convention to use non-register arguments. > > > > Reported-by: ernsteiswuerfel > > FWIW, I think this can be > > Reported-by: Erhard Furtner > > since it has been used in the kernel before, the reporter is well known > :) Ah! Okay, thanks. I wasn't able to find an associated email address. :) > > > Closes: https://github.com/KSPP/linux/issues/350 > > Signed-off-by: Kees Cook > > --- > > Cc: Marco Elver > > Cc: Andrey Konovalov > > Cc: Andrey Ryabinin > > Cc: Nathan Chancellor > > Cc: Nick Desaulniers > > Cc: Bill Wendling > > Cc: Justin Stitt > > Cc: llvm@lists.linux.dev > > Cc: kasan-dev@googlegroups.com > > Cc: linux-hardening@vger.kernel.org > > --- > > lib/ubsan.h | 41 +++++++++++++++++++++++++++-------------- > > 1 file changed, 27 insertions(+), 14 deletions(-) > > > > diff --git a/lib/ubsan.h b/lib/ubsan.h > > index 50ef50811b7c..978828f6099d 100644 > > --- a/lib/ubsan.h > > +++ b/lib/ubsan.h > > @@ -124,19 +124,32 @@ typedef s64 s_max; > > typedef u64 u_max; > > #endif > > > > -void __ubsan_handle_add_overflow(void *data, void *lhs, void *rhs); > > -void __ubsan_handle_sub_overflow(void *data, void *lhs, void *rhs); > > -void __ubsan_handle_mul_overflow(void *data, void *lhs, void *rhs); > > -void __ubsan_handle_negate_overflow(void *_data, void *old_val); > > -void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs); > > -void __ubsan_handle_type_mismatch(struct type_mismatch_data *data, void *ptr); > > -void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr); > > -void __ubsan_handle_out_of_bounds(void *_data, void *index); > > -void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs); > > -void __ubsan_handle_builtin_unreachable(void *_data); > > -void __ubsan_handle_load_invalid_value(void *_data, void *val); > > -void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr, > > - unsigned long align, > > - unsigned long offset); > > +/* > > + * When generating Runtime Calls, Clang doesn't respect the -mregparm=3 > > + * option used on i386. Hopefully this will be fixed correctly in Clang 19: > > + * https://github.com/llvm/llvm-project/pull/89707 > > + * but we need to fix this for earlier Clang versions today. Force the > > It may be better to link to the tracking issue upstream instead of the > pull request just in case someone comes up with an alternative fix (not > that I think your change is wrong or anything but it seems like that > happens every so often). > > I also get leary of the version information in the comment, even though > I don't doubt this will be fixed in clang 19. > > > + * calling convention to use non-register arguments. > > + */ > > +#if defined(__clang__) && defined(CONFIG_X86_32) > > While __clang__ is what causes CONFIG_CC_IS_CLANG to get set and there > is some existing use of it throughout the kernel, I think > CONFIG_CC_IS_CLANG makes it easier to audit the workarounds that we > have, plus this will be presumably covered to > > CONFIG_CLANG_VERSION < 190000 Yeah, that seems much cleaner. I will adjust it... > > when the fix actually lands. This file is not expected to be used > outside of the kernel, right? That is the only thing I could think of > where this distinction would actually matter. > > > +# define ubsan_linkage asmlinkage > > Heh, clever... > > > +#else > > +# define ubsan_linkage /**/ > > Why is this defined as a comment rather than just nothing? I dunno; this is a coding style glitch of mine. :P I will drop it. Thanks for the review! -Kees > > > +#endif > > + > > +void ubsan_linkage __ubsan_handle_add_overflow(void *data, void *lhs, void *rhs); > > +void ubsan_linkage __ubsan_handle_sub_overflow(void *data, void *lhs, void *rhs); > > +void ubsan_linkage __ubsan_handle_mul_overflow(void *data, void *lhs, void *rhs); > > +void ubsan_linkage __ubsan_handle_negate_overflow(void *_data, void *old_val); > > +void ubsan_linkage __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs); > > +void ubsan_linkage __ubsan_handle_type_mismatch(struct type_mismatch_data *data, void *ptr); > > +void ubsan_linkage __ubsan_handle_type_mismatch_v1(void *_data, void *ptr); > > +void ubsan_linkage __ubsan_handle_out_of_bounds(void *_data, void *index); > > +void ubsan_linkage __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs); > > +void ubsan_linkage __ubsan_handle_builtin_unreachable(void *_data); > > +void ubsan_linkage __ubsan_handle_load_invalid_value(void *_data, void *val); > > +void ubsan_linkage __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr, > > + unsigned long align, > > + unsigned long offset); > > > > #endif > > -- > > 2.34.1 > > > > -- Kees Cook