From nobody Wed Dec 17 08:56:13 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 B35332063D7 for ; Tue, 18 Mar 2025 21:41:33 +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=1742334095; cv=none; b=D8Vgv6vvbeGKht7UPMuczdxsbascO/lYAaOWQTQpoFVWoL3+0q8M6rJJHxQP3GxxnKb0qmbxpwqvDLAHBLH9RBxE1TW5D1/mVHPbnEnrg9PIvSfLOkZnv1Ca70OKbRUzYCiZ1N8pdgihnwi50jjV/0/ushKnPbiDfnGkNRyJ7oE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742334095; c=relaxed/simple; bh=48tNNYVxNbJIhapHHVTjKQnmA5ByjfhX/glfPRAKIhc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=POuY45mg3/Lo6ELbq1hld07kvSeodtVYdKOo0LzhDBCrxlW6ue97LzPImzGcbjSwBEKlsGdr0ZGo6tOHOeb/33tVbin6KVR2gKP5A80IY7ufJ6Q7UJI7fzrjIprnaBPKvdupfFZTuCr/PDQnqvRPAcymqFys3WtINQ36lh9SBxw= 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=E+0JuCPu; 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="E+0JuCPu" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-225ab228a37so86775365ad.2 for ; Tue, 18 Mar 2025 14:41:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742334093; x=1742938893; 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=eTUn5qDH83alwoR8172wGnmaTD/nkQcDeotToQ/FQtU=; b=E+0JuCPuXCTRQpxwuJCoPOuxF5kCEkC/7dh9RpTVY1vd36GHCre9gUgN5wIA20WDmh v9ah15eJ4BiIeGZ/MXE7wXesI/NjKWY2vivmuKO18oZfYhqC+iLAqXljJrQ2AJnVipNN TppDy357MnlZBUkqmlLwfdx6yobFatF7Dnubot0usBH9Cve4k+UtMD6hAPS8NtBw2dkk CO0+agWZuIznwDGLeO0reg59qWzpmwpQTpJZa79dQhJwP34sJ3fDc2F78LJIJX+uTNKW 7/Z94xujnNdvrEmTR4RF3eg7QrL4B+KX3qL+XyFShFhCfyF4M07YhPjuDbpE4sI243jD A4FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742334093; x=1742938893; 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=eTUn5qDH83alwoR8172wGnmaTD/nkQcDeotToQ/FQtU=; b=v0ISjchNqBBwz1LoiXqVTInua8p+hGOpT1iRwy7XIzPUOk9EE7jo4XeleKnvjhq/AA PvEzqtET2qo1yl+VrxYFDo/3Ch09iVPWVw7E4u4NxGFOLpekouD4+czPP/FWPlqgs2ld YkdFqSob2zX/hhmSP/XqSqStg9OgABN+fMCYDiZN7xwh3I9CBg43ZBwGxMDLkmvxa3A4 TKP0RuDeEDjMY9CQwbt8RHYBIhux8ja1Ko99qnaWv2qWwcYsA139VCBxGBA5YGeJpl6X 8SZsgtXy6H/FnqHK6i3n9bbXGL2zXNKMrxpzwoip+1zpqeD7LLCEPHt4SHw63ZS17/OR dxKg== X-Forwarded-Encrypted: i=1; AJvYcCWz4k2RbvI7vOwYD5Ujh1BgVar/av35x+C8L+OM3lCqO+r+gsxupoINtR1U1Bf6gA+xR7jbQBlWzlkYsI0=@vger.kernel.org X-Gm-Message-State: AOJu0YwTy19QX6RW569LqALA9Rwq+EwuErwRsWeqXVBJrMvGTVwBziVS a1uGbS/WtOtlL2kP9U3xTHPC3/EHoF2tAj43y+L9r+N1BkMXlerBl9YasYT/44HN2w== X-Google-Smtp-Source: AGHT+IG/QGxbxUqv2YDIjQQv/YN/p4GQJhFMy09dmHyQiNHhErBBFph57AE76+yZ/pqecPFGYqZrens= X-Received: from plsu3.prod.google.com ([2002:a17:902:bf43:b0:223:f487:afc6]) (user=pcc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2412:b0:223:3ef1:a30a with SMTP id d9443c01a7336-22649a57c52mr3056545ad.45.1742334093068; Tue, 18 Mar 2025 14:41:33 -0700 (PDT) Date: Tue, 18 Mar 2025 14:40:32 -0700 In-Reply-To: <20250318214035.481950-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: <20250318214035.481950-1-pcc@google.com> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog Message-ID: <20250318214035.481950-2-pcc@google.com> Subject: [PATCH v2 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.395.g12beb8f557-goog From nobody Wed Dec 17 08:56:13 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 9F6442066F1 for ; Tue, 18 Mar 2025 21:41:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742334097; cv=none; b=QekAy5gDeDKhp6lRadY8A/fElcIEgZU252k9d0z+t6AnZ+Oj9pqjOB7dX+A6iRTOyKEzCYcQlvb5bAXPaUamEsN1KDt9jXWxKCVZKR7gAcXHCmlJa8gkZ4Jsbz7FwOsfU8QxvsQJiezQJi0Q/YllfN0KEHakFUQgDNvUSchjwCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742334097; c=relaxed/simple; bh=2JVg6jNN+EXcNb7mKuWKPPuyH/BrxNNvi7msA7FPZpA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZlSvqQethK+sMfvL/7wV+TVr5nTsbAjlXmQQq+eriJ+yR210fxBHFtmao3xtUw4IP8xPR8JAQj0lwO3XCIAWSa7kWCDM/2j4s5M+R6ykl+tTGsPRZimwZx2BEWO9sEj2R+jrLiQk9Jp/bZaLfsMGR+XhiOzopehycM9vLudbeG4= 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=Ir5ZZQK6; arc=none smtp.client-ip=209.85.216.73 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="Ir5ZZQK6" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff605a7a43so9806300a91.3 for ; Tue, 18 Mar 2025 14:41:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742334095; x=1742938895; 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=+3v041miGofNddzBnUn4Cs5yNorfDPWogz1+D6wVoyA=; b=Ir5ZZQK62/5S4SYS82c6xIwYfeod1V+mSobHilPnykNkBIAPcaEG0e3y3ksPC7qAWF CmJuKyiBLOGrR1lyqB0aXtVwLyiSuiz9yvO7DhepJxRRo97a8mV97w0MuzkBFwG9T2fM tIUdK01ClzzM2HX/DJD8GvK1tzPXzPxu1BL6e02+bFJSq02vy6UROINqI5CMLEG/ztoz RMmhWZUbjqq8FmMqIGKENKXOR9JfOAkbMLzR4UO2Tu2eI6ah+qQ+uQcA3ADRWeAisN44 TbcSVOk+ke4N/zJAWS8eHS8yQFTCbqx4XUSwHtp4r/jBp8G4jvE7XYObetw/elotG9Dn c5Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742334095; x=1742938895; 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=+3v041miGofNddzBnUn4Cs5yNorfDPWogz1+D6wVoyA=; b=HH+7n6Ys0q7hkX8KBOcstG3XmcNBmW571Z05FgpwOCaOo/tirrznG8HStfXB0CNw7Y 7uqKMYqhSdXkPoeEdoHvUFep1WjJ3CV+JTEirvdVbGJ6pFRRZZG6cmynhyZYVPu0EYfp +QjWubhqYcITv98Vi2jtXb8kqTnJfByG8LnqCNNpMfne3mEsfRNuxQHV0TWon0XeHF56 LF/rdfk/UbSCVM4509KjWOv6pQnM0FJzaHcxiWxsCcWKWl19EN7lqNDTXJ3In2V+ro7h MwKhI704SlDxyrEb1/N/cNnOuIp3LNnnZBlCbeAzne+pc8SfhMN+QxPSHRtZsMaPrBaS 1LKg== X-Forwarded-Encrypted: i=1; AJvYcCVUB8wZvDnDEoCt2s+CluGepnfjK27gPeJUcoT84rEe/cngQCvVtsTkKccjWE2rGjTvj93AyadUdKvDi1E=@vger.kernel.org X-Gm-Message-State: AOJu0YzWdpY1zlhWrpupZWAniUG/2Wd4TkpkgvwiCEqVlAtqiSKn66gy JtC3pc7P4aGal+nN3cn9f1dwdS92cta8rQTFXZwQxz658QZDBzgCEQQRdViY8rDbIQ== X-Google-Smtp-Source: AGHT+IFLY1EmXxbPh0DHXmzLlTkBArwizsU3t94Vw7f4cPApT4gnOzyKca7K7uUKbY1tEhJcornmRlo= X-Received: from pjp6.prod.google.com ([2002:a17:90b:55c6:b0:2ef:78ff:bc3b]) (user=pcc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:38d2:b0:2ff:6f8a:3a13 with SMTP id 98e67ed59e1d1-301be1f8f6emr353522a91.25.1742334094908; Tue, 18 Mar 2025 14:41:34 -0700 (PDT) Date: Tue, 18 Mar 2025 14:40:33 -0700 In-Reply-To: <20250318214035.481950-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: <20250318214035.481950-1-pcc@google.com> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog Message-ID: <20250318214035.481950-3-pcc@google.com> Subject: [PATCH v2 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 triggers an MTE fault. Implement a KASAN KUnit test that triggers the issue with the current 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 Link: https://linux-review.googlesource.com/id/If88e396b9e7c058c1a4b5a25227= 4120e77b1898a --- v2: - rebased - fixed test failure mm/kasan/kasan_test_c.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index 59d673400085f..c4bb3ee497b54 100644 --- a/mm/kasan/kasan_test_c.c +++ b/mm/kasan/kasan_test_c.c @@ -1570,7 +1570,9 @@ static void kasan_memcmp(struct kunit *test) static void kasan_strings(struct kunit *test) { char *ptr; - size_t size =3D 24; + char *src, *src2; + u8 tag; + size_t size =3D 2 * KASAN_GRANULE_SIZE; =20 /* * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT. @@ -1581,6 +1583,33 @@ 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(size, GFP_KERNEL | __GFP_ZERO); + strscpy(src, "f0cacc1a00000000f0cacc1a00000000", size); + + tag =3D get_tag(src); + + src2 =3D src + KASAN_GRANULE_SIZE; + + /* + * Shorten string and poison the granule after it so that the unaligned + * read in strscpy() triggers a tag mismatch. + */ + src[KASAN_GRANULE_SIZE - 1] =3D '\0'; + kasan_poison(src2, KASAN_GRANULE_SIZE, tag + 1, false); + + /* + * 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, size)); + + /* Undo operations above. */ + src[KASAN_GRANULE_SIZE - 1] =3D '0'; + kasan_poison(src2, KASAN_GRANULE_SIZE, tag, false); + + kfree(src); kfree(ptr); =20 /* --=20 2.49.0.395.g12beb8f557-goog