From nobody Fri Dec 19 18:29:54 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 E70CC29A1 for ; Sat, 29 Mar 2025 00:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743206634; cv=none; b=g8PifufrNiLKPf14y2ZDSdG61MRndKPhrAu1UoSh4zTWsIUApRHJ9MNW21apHjBWmlZqXeC+lDyo/wEvyd+QHdH/oil3glgktF0eCeMEgHW48aGtjwqS92O1SiI3TTHRXW8oXgiunYYyO8mS84XQbGYWO8Z/JVYKYWEAFYMWJM4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743206634; c=relaxed/simple; bh=A4qOF8g/Lp0fNN70XKVNtD9pF/sKUksq0VFQkLxYtOA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BMUZyAVyaBhvuskJ6CjFwqu5SeACTrryykQVt3KXknO9acPf25JaHFl2bxrPaCG+BDjrebMu5dPtkYhLA7pcHeAjlSaOW8vgiP9d/K4YBuWbXJPFofu6gvqdkZObODJStxpn5tOS6Y6cl3YSo0ReESmkIB43ChUCqJMaDbjo4Xo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--pcc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CqM6trdQ; arc=none smtp.client-ip=209.85.214.201 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=flex--pcc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CqM6trdQ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-22410b910b0so39666675ad.2 for ; Fri, 28 Mar 2025 17:03:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743206631; x=1743811431; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mEgmoeCU7ZoBbD/6q07yzTwvuV9KDxvtvsClWJ9QuF8=; b=CqM6trdQnGyRuGojGezqW97vHKKPBba0YFXnxkJYUjR5WqFZ+srqQE/JxKuUcrK1zP epGyEB5E/IvmhgFwRBI2siYtJ+9TpAc0LvKD3TUdQMB1lsRs0o/H5vMU9RvMw05WVbFP PDBPYUTokovsDljKlToIbpQl5uCL/XAmwpkxibZ9JZi8Vristc4qGncD3ffkFnQt7/Gg 6Gj/ge9MlbXnOw62liHAG6yavJcuJEQ8ek3eitT2Ym/4+fNkLctPutd3WvTpeGAZVmGX A/P++a8ym79yEDclEgWtyjaD7XOF84i3KRdjStdb21hrbdrD8gdxxxMnFwMG6UEiZ4Kh ucww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743206631; x=1743811431; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mEgmoeCU7ZoBbD/6q07yzTwvuV9KDxvtvsClWJ9QuF8=; b=JcedzlF2UwNa+mPGArq0DJ3g8kQhJKblrChkw4ATcHQZTltyVIDr0xS53JWX5huLm0 2zKBt5mggAzM03jY9KiAKHyLLkqe2ZHlfGmJ8YPmdCgep4ZAe6/DeLDfMck79k8TQjXS J+lO/pHrDRftuUxmDsIYauijuRf6lwGHqVKp0iVBNCnE55tv+cjBngtoSknj1vvKGz7f M3ez7oDXrouDgl3cOzIy8KS36eBh890rjLsThtEyVxj7THgP0yWt7bAAOjdBO/eqZVqg JZX4iIqbnjRcx/a70Z4vpEkAIOTIWNRaHDe8y2dl3MHccp4136UkFNHAmbbisPX4+DIZ zaLQ== X-Forwarded-Encrypted: i=1; AJvYcCXDmJduYOGVF/BDx5AVYL3yeNsSvymlf2JKlxMyVc02dKLNMzm3iRyMlPpJ1nvZICC0EYO9kErQjuYgSoY=@vger.kernel.org X-Gm-Message-State: AOJu0Yzzb4LBl5KqIw8qWFD/o9+DNLfFfogDcTAkVWSmQ0CfNtLDgjDV eqOEUHzNT209vQ+hi16L0STcT2bCW8CR3uaqzpjhzQrTHOWI1XQT48LT8Ym9lH2UCw== X-Google-Smtp-Source: AGHT+IHjR6hGtSOVpUkc0nyNRot3w3fhTtOD52YS62CSenulpOThQdeL5LjrShYtAkO27skye9Y/6dw= X-Received: from pfbmd6.prod.google.com ([2002:a05:6a00:7706:b0:739:4935:6146]) (user=pcc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e887:b0:223:f408:c3f8 with SMTP id d9443c01a7336-2292f960348mr16354725ad.14.1743206631260; Fri, 28 Mar 2025 17:03:51 -0700 (PDT) Date: Fri, 28 Mar 2025 17:03:36 -0700 In-Reply-To: <20250329000338.1031289-1-pcc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250329000338.1031289-1-pcc@google.com> X-Mailer: git-send-email 2.49.0.472.ge94155a9ec-goog Message-ID: <20250329000338.1031289-2-pcc@google.com> Subject: [PATCH v4 1/2] string: Add load_unaligned_zeropad() code path to sized_strscpy() From: Peter Collingbourne To: Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Kees Cook , Andy Shevchenko , Andrey Konovalov , Catalin Marinas , Mark Rutland Cc: Peter Collingbourne , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-arm-kernel@lists.infradead.org, stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The call to read_word_at_a_time() in sized_strscpy() is problematic with MTE because it may trigger a tag check fault when reading across a tag granule (16 bytes) boundary. To make this code MTE compatible, let's start using load_unaligned_zeropad() on architectures where it is available (i.e. architectures that define CONFIG_DCACHE_WORD_ACCESS). Because load_unaligned_zeropad() takes care of page boundaries as well as tag granule boundaries, also disable the code preventing crossing page boundaries when using load_unaligned_zeropad(). Signed-off-by: Peter Collingbourne Link: https://linux-review.googlesource.com/id/If4b22e43b5a4ca49726b4bf98ad= a827fdf755548 Fixes: 94ab5b61ee16 ("kasan, arm64: enable CONFIG_KASAN_HW_TAGS") Cc: stable@vger.kernel.org --- v2: - new approach lib/string.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/string.c b/lib/string.c index eb4486ed40d25..b632c71df1a50 100644 --- a/lib/string.c +++ b/lib/string.c @@ -119,6 +119,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size= _t count) if (count =3D=3D 0 || WARN_ON_ONCE(count > INT_MAX)) return -E2BIG; =20 +#ifndef CONFIG_DCACHE_WORD_ACCESS #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS /* * If src is unaligned, don't cross a page boundary, @@ -133,12 +134,14 @@ ssize_t sized_strscpy(char *dest, const char *src, si= ze_t count) /* If src or dest is unaligned, don't do word-at-a-time. */ if (((long) dest | (long) src) & (sizeof(long) - 1)) max =3D 0; +#endif #endif =20 /* - * read_word_at_a_time() below may read uninitialized bytes after the - * trailing zero and use them in comparisons. Disable this optimization - * under KMSAN to prevent false positive reports. + * load_unaligned_zeropad() or read_word_at_a_time() below may read + * uninitialized bytes after the trailing zero and use them in + * comparisons. Disable this optimization under KMSAN to prevent + * false positive reports. */ if (IS_ENABLED(CONFIG_KMSAN)) max =3D 0; @@ -146,7 +149,11 @@ ssize_t sized_strscpy(char *dest, const char *src, siz= e_t count) while (max >=3D sizeof(unsigned long)) { unsigned long c, data; =20 +#ifdef CONFIG_DCACHE_WORD_ACCESS + c =3D load_unaligned_zeropad(src+res); +#else c =3D read_word_at_a_time(src+res); +#endif if (has_zero(c, &data, &constants)) { data =3D prep_zero_mask(c, data, &constants); data =3D create_zero_mask(data); --=20 2.49.0.472.ge94155a9ec-goog From nobody Fri Dec 19 18:29:54 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 E4C171D555 for ; Sat, 29 Mar 2025 00:03:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743206635; cv=none; b=CEY8wUnoHwiJ9+0WQPDinlEHR2FA9LaAK7T6cIWyEfIoJr2LYricwqVSBZuhYEq6ZVHiIvZr+o6y7ZLvWD4BzKX03AHn+6fo7dhwT1pwha3klYKu4+nFjRyZTlHVJKtRCvN2OWbdJbzzbzkaKCVT1j1szkhyPageiZlnThn5V+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743206635; c=relaxed/simple; bh=0GH03YEIRBHpcTs5EQTf5tRjGOZhfj5ycrE7WqRiMdo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=duOnLrHc2PeDn7ZAIXa8kehSEj37MJDgE6IxyepmbNdlZnnC7i/T027Z4bXAI/t4qZmnlF4EGG1Hdrb9uWXSHNoP947Ym0VYDi79kmrfZIgtreM6q5ct3v4tF+D+XkwFfmuAwk/PuF8gsI6Qm9xzI9Qp1+FTwXQVWmUIfiz18nc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--pcc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uRxFww+t; arc=none smtp.client-ip=209.85.214.202 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=flex--pcc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uRxFww+t" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2240a96112fso79188185ad.2 for ; Fri, 28 Mar 2025 17:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743206633; x=1743811433; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tGMonBDCtHbGxccj+//vlpjOsUsbEFJEDLrw6ctd8aw=; b=uRxFww+tlYNsYYB/x9abq3jT5BxzIRaJUYOfFllDJWB4ChJ2ynmwPT58DCh+oCPC0f /AGtzjTIL16uwwjjac9W8WKj80GqUMTR5U1Xa/JL30HNwCzN5Po2ReqW2JpYsah1BT2X bV8C99W9UuKAtbUgsJC7arwnbsTmvOGe756H+JQRtw4tjfrCt1x266jtMuPNkHFGiHbl 7x6Vbij036Kgwn5V/vxuKXks11Dp5feiddn8rEQm0WKaHjof3tqdLL06kTtbUii6avWh 3W+hnXJbZuIKCGnefvmF6gOIlJDLYDlTeb2dvCXoJUYH1p8xRfAGuBdTe6rV/sosUQWE KUbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743206633; x=1743811433; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tGMonBDCtHbGxccj+//vlpjOsUsbEFJEDLrw6ctd8aw=; b=amfq51ERtzOpLqT2VtSVcJQ9Nsy8skLHPO9UY0/depHuqbLOm9Ll1LmBeVdU2XiGtq ivjQT927wcaSOBl8Ms0Svz3h3FPZgf+SeWXBJS+vKozhy8SrzgF5YkRB490H20GNxgPg 59rWujk1yp4y5FSVVp5+jtekg/sAp1Q4PIcCLYNpqlhT5m0X0yJE+Abuv0MeMIo6Dfk2 1VDAshQgqVM0sjgCiz+jHRLqeV5mithGnaRTNgBSTGZInoOR7TRVTr473L9OeQEhTJ13 bgg25IUWuVRc3oNJBlnjXYP8ag9wse1aqmuakwReuyDrvtt+FxXY9rV9OHuy12u0DR1N 84QA== X-Forwarded-Encrypted: i=1; AJvYcCXXA9vUq1ZcWZwnNziHz4wj6xRTh1DgoG+fcwH2moZq+Kd+LW+fmL//4fIIGD76w4Ks2PNFMUziBs4/e2Q=@vger.kernel.org X-Gm-Message-State: AOJu0YwUB+tkCyDnQqwL3jIUW2fjBR/FW68itjp3Twyxd7twI5dYfoDo ztCkGDaAR8bcd38L67UzxYYmMFU5Noa6OdfXyN/9SRm9LMQg+A1p99bwTv7uhgo3EQ== X-Google-Smtp-Source: AGHT+IG1OCkWK6ACc2gjb+axSi3OGauLJByZEq2XzaKVmUmtuGmaBnSVPaf+lB7MT0QcfgqXjpACp1o= X-Received: from plrc9.prod.google.com ([2002:a17:902:aa49:b0:229:2f8a:aac5]) (user=pcc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:eb8d:b0:21f:564:80a4 with SMTP id d9443c01a7336-2292f9e5139mr16353845ad.33.1743206633220; Fri, 28 Mar 2025 17:03:53 -0700 (PDT) Date: Fri, 28 Mar 2025 17:03:37 -0700 In-Reply-To: <20250329000338.1031289-1-pcc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250329000338.1031289-1-pcc@google.com> X-Mailer: git-send-email 2.49.0.472.ge94155a9ec-goog Message-ID: <20250329000338.1031289-3-pcc@google.com> Subject: [PATCH v4 2/2] kasan: Add strscpy() test to trigger tag fault on arm64 From: Peter Collingbourne To: Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Kees Cook , Andy Shevchenko , Andrey Konovalov , Catalin Marinas , Mark Rutland Cc: Vincenzo Frascino , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Will Deacon , Peter Collingbourne Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vincenzo Frascino When we invoke strscpy() with a maximum size of N bytes, it assumes that: - It can always read N bytes from the source. - It always write N bytes (zero-padded) to the destination. On aarch64 with Memory Tagging Extension enabled if we pass an N that is bigger then the source buffer, it would previously trigger an MTE fault. Implement a KASAN KUnit test that triggers the issue with the previous implementation of read_word_at_a_time() on aarch64 with MTE enabled. Cc: Will Deacon Signed-off-by: Vincenzo Frascino Signed-off-by: Catalin Marinas Co-developed-by: Peter Collingbourne Signed-off-by: Peter Collingbourne Reviewed-by: Andrey Konovalov Link: https://linux-review.googlesource.com/id/If88e396b9e7c058c1a4b5a25227= 4120e77b1898a --- v4: - clarify commit message - improve comment v3: - simplify test case v2: - rebased - fixed test failure mm/kasan/kasan_test_c.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index 59d673400085f..655356df71fe6 100644 --- a/mm/kasan/kasan_test_c.c +++ b/mm/kasan/kasan_test_c.c @@ -1570,6 +1570,7 @@ static void kasan_memcmp(struct kunit *test) static void kasan_strings(struct kunit *test) { char *ptr; + char *src; size_t size =3D 24; =20 /* @@ -1581,6 +1582,21 @@ static void kasan_strings(struct kunit *test) ptr =3D kmalloc(size, GFP_KERNEL | __GFP_ZERO); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); =20 + src =3D kmalloc(KASAN_GRANULE_SIZE, GFP_KERNEL | __GFP_ZERO); + strscpy(src, "f0cacc1a0000000", KASAN_GRANULE_SIZE); + + /* + * Make sure that strscpy() does not trigger KASAN if it overreads into + * poisoned memory. + * + * The expected size does not include the terminator '\0' + * so it is (KASAN_GRANULE_SIZE - 2) =3D=3D + * KASAN_GRANULE_SIZE - ("initial removed character" + "\0"). + */ + KUNIT_EXPECT_EQ(test, KASAN_GRANULE_SIZE - 2, + strscpy(ptr, src + 1, KASAN_GRANULE_SIZE)); + + kfree(src); kfree(ptr); =20 /* --=20 2.49.0.472.ge94155a9ec-goog