Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp1018871lqo; Fri, 17 May 2024 08:22:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUWgU7V3pNeK8FrYLozkfm1ew1AawvP56dmzYs3WFArFaPdZzpH2xGV6AxswTt8I6MpjwEomFe+uui+IWRtv4A/vGIp+T0pqLg6axxybQ== X-Google-Smtp-Source: AGHT+IG7RHdMF/TOuMdBFBPCb96GjeoQtVLrkxV9/31XJPli+1uqlvJ7bbAXOTvZYJV2gmjJRKnG X-Received: by 2002:a17:906:3e41:b0:a59:bd5b:a0f3 with SMTP id a640c23a62f3a-a5a2d5d4509mr1415407266b.38.1715959368512; Fri, 17 May 2024 08:22:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715959368; cv=pass; d=google.com; s=arc-20160816; b=lNX/Ws7mwvofUQiaLLcU3HfIosrB7J0o1lX4W2IDdEdyH2Lx4lXN1EognLtwqfde/d pKQCvYnYzZ5UOtxSJvwWY3++nInMxtGiMSNkkWsN8wyLu8vf2HbnYj+YP7/Yt/IkBbv7 GxJT44LoF2+wvBIORDJhZrETe1HgtGZEaf95NsbpTZUGOjQGdK0nDDjBdA5OAYrHdrHr QdhFEVVpj/1xx+fsnthxNKECU5cUcyJKQi1MRNyWEAQTmfLwKTssnPbaoXazUGFCY3LW FiAL8x+gNVCToMbdLQFM1SL/zEedMo7YJquT+Pmpr4VswJQhirC5rpnZUGn9YHZE0G/n JVGg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=a0X1IdGrvgRbIDZDbKlklVPOFj9du4fG+oJBScJlx9I=; fh=sPfctXuE4H3Lm9AT7gSlBwumFKea97F+s/0C0j3d9mU=; b=AppjU+yEycXl1Ze68/gufOBlWkTDDZh4pv6y5zmEaRr7jHEDwQZZ/Ji1bkX07NwIeJ Xo8tU4bXIWJQKIR9XrxADo03ffDfrAs4UY+6qq8DWcIY92v9ObceJjeY+QoVGAh3wP0+ O0r/rze7e60xA+fDMY29Zbzl68VRNbeWH+wtMqgOfuhMAaNYGkcst9oJX53pZLY4OTQM LXY9s7R830uC/6gzrTlptKVRwGET0BQYB9uZg98KXjzHXkuSUAWhxLJANa32yA/2aC1n zFm7UNi0ojWO+ymi0WWS8SO55M10k9ZU1ucPsrZ2hUbFxV40T4WayGg1WWd21udAtNyJ aR6Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SS9A74wE; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-182289-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182289-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17ba5619si1017442966b.613.2024.05.17.08.22.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 May 2024 08:22:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-182289-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SS9A74wE; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-182289-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182289-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 1AAA01F22F0F for ; Fri, 17 May 2024 15:22:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8A83012D1EF; Fri, 17 May 2024 15:22:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SS9A74wE" Received: from mail-vk1-f171.google.com (mail-vk1-f171.google.com [209.85.221.171]) (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 0E1C96A8DE for ; Fri, 17 May 2024 15:22:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715959361; cv=none; b=etfI438lch256RsBWir4lOvUnmzJUHn4Wj8LlJf7Zn7MglBmN13PFLPjHNZCSGdbdX9zxkYdU2iTNotiQajxQai7oKq3KauxdYKAOl81OqfNKQs2tS+I8wPx0QiA+T4nMqqzqslKAh8bbx5cvdmsvPZzJyGHIT0mRTCtpkez7OU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715959361; c=relaxed/simple; bh=SoNjM+fZ7CTvIUU9FKqh2EWjcEbzvsg7sGSSi3DdsV0=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=LEtfHSGskeXSZnP2QrO70MVE/vR7VKt9vjIl2yGofA9rpgYQOq2bp29Gjn/QeFpmGNN8AbGjmND4ydQCSRpzYyE9jGw7C3fnW3r2dnTM2/YkXTm7BaLWtYRAZMDYtowDrhd2+EgmYQMeDlZlF/AtAE8J9EUHxJcNwP5LOqcBqWU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SS9A74wE; arc=none smtp.client-ip=209.85.221.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-vk1-f171.google.com with SMTP id 71dfb90a1353d-4df2a816455so254147e0c.3 for ; Fri, 17 May 2024 08:22:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715959359; x=1716564159; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=a0X1IdGrvgRbIDZDbKlklVPOFj9du4fG+oJBScJlx9I=; b=SS9A74wE6LHPNSRyFk6q77cTsqUE8hdfUeoiWp63dln8EqOYkAeDslONOwds9H93DA oJnV+RPME8knDHLPyOv7VCBCbjlMIB9+JCEorPj2qNflVw6Q3GP8AwvLDsyLOfN72aG4 LCC6Mb1ZT/02JkrhPmNH8FXmlE2wMmTYby2mufeCIHPzdi2lgxW7ANVLRvhTS8zaUNei tEMupoxtfq98Ti7bsxtJb/HVLwTiDC8Nt0GmeZp9GGTWRvEYhiv2j92LO+sDKQLOWUqo Z/GtKij0O+5C3Lo8h1jTLsMsCuBiwzEXSS7KTHgkD5ABWSRTMZ1LN9R4qsqAG0Qj8Fu7 c3lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715959359; x=1716564159; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=a0X1IdGrvgRbIDZDbKlklVPOFj9du4fG+oJBScJlx9I=; b=LrQISJ9G/4TQUawvn+Vm8XgnFjm2LEB4sJg2b33RwvxSIZIauUfPLuNJcdQnQTy880 45KLeqhH8NJ6WGu0Z3D9lpd1u2Si8EvFfqEuwhJzUBczFQx2ln43Vqc4PHA4N3YZWxFL B7IlrMP5GfYoodQubtATBjNmsYIxed83rAEZUweKMh6aYkJrH1Mplam8Lmy+q3RhgmmQ A4aYl7qyjmn4n2ZvT3DXg51mybY9nGf38zz9RHBlJt8c2CLD6EXe+REG3nVM/GiZUEZt je16DFvyntzEZORcgx3cRBKtxXwEFUxXlpT7KG25fnJR6433LyTS0PGtwXq499cTotT8 rrkg== X-Forwarded-Encrypted: i=1; AJvYcCVww9qHipMNpfTbquH6x7FvjnAc/2aAVdZg7bX0skmudWjaCiJ9Csoshh/q19Z9rQ4x8bntoBQVEZBv9GAqNDGSjnwm7BsN0cfbs+3p X-Gm-Message-State: AOJu0Yzre8CemHD3qpW96JHyfP8sjcYMO7+miu+QlWIcjgAO79akEPqu sGvT8oIimaC2yBmVbubcmAIvMvdYKRimT6xKKQBQYaVW3Skf5rLmHq5InScs0C0HWVpLukO9tYJ gfWZgAgBpDI291cdLOrWaMI6owV3i5bi2WWtS X-Received: by 2002:a05:6122:98a:b0:4d3:362f:f9c1 with SMTP id 71dfb90a1353d-4df88359107mr21578723e0c.13.1715959357364; Fri, 17 May 2024 08:22:37 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240517130118.759301-1-andrey.konovalov@linux.dev> In-Reply-To: <20240517130118.759301-1-andrey.konovalov@linux.dev> From: Marco Elver Date: Fri, 17 May 2024 17:21:58 +0200 Message-ID: Subject: Re: [PATCH] kasan, fortify: properly rename memintrinsics To: andrey.konovalov@linux.dev Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, Erhard Furtner , Nico Pache , Daniel Axtens , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" On Fri, 17 May 2024 at 15:01, wrote: > > From: Andrey Konovalov > > After commit 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() > functions") and the follow-up fixes, with CONFIG_FORTIFY_SOURCE enabled, > even though the compiler instruments meminstrinsics by generating calls > to __asan/__hwasan_ prefixed functions, FORTIFY_SOURCE still uses > uninstrumented memset/memmove/memcpy as the underlying functions. > > As a result, KASAN cannot detect bad accesses in memset/memmove/memcpy. > This also makes KASAN tests corrupt kernel memory and cause crashes. > > To fix this, use __asan_/__hwasan_memset/memmove/memcpy as the underlying > functions whenever appropriate. Do this only for the instrumented code > (as indicated by __SANITIZE_ADDRESS__). > > Reported-by: Erhard Furtner > Reported-by: Nico Pache > Closes: https://lore.kernel.org/all/20240501144156.17e65021@outsider.home/ > Fixes: 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions") > Fixes: 51287dcb00cc ("kasan: emit different calls for instrumentable memintrinsics") > Fixes: 36be5cba99f6 ("kasan: treat meminstrinsic as builtins in uninstrumented files") > Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver This is getting rather complex, but I don't see a better way either. > --- > include/linux/fortify-string.h | 22 ++++++++++++++++++---- > 1 file changed, 18 insertions(+), 4 deletions(-) > > diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h > index 85fc0e6f0f7f..bac010cfc42f 100644 > --- a/include/linux/fortify-string.h > +++ b/include/linux/fortify-string.h > @@ -75,17 +75,30 @@ void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning(" > __ret; \ > }) > > -#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) > +#if defined(__SANITIZE_ADDRESS__) > + > +#if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY) > +extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset); > +extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove); > +extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy); > +#elif defined(CONFIG_KASAN_GENERIC) > +extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(__asan_memset); > +extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(__asan_memmove); > +extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(__asan_memcpy); > +#else /* CONFIG_KASAN_SW_TAGS */ > +extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(__hwasan_memset); > +extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(__hwasan_memmove); > +extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(__hwasan_memcpy); > +#endif > + > extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr); > extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp); > -extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy); > -extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove); > -extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset); > extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat); > extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy); > extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen); > extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat); > extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy); > + > #else > > #if defined(__SANITIZE_MEMORY__) > @@ -110,6 +123,7 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) > #define __underlying_strlen __builtin_strlen > #define __underlying_strncat __builtin_strncat > #define __underlying_strncpy __builtin_strncpy > + > #endif > > /** > -- > 2.25.1 >