From nobody Tue Dec 16 23:44:45 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 97E2C8460 for ; Thu, 3 Apr 2025 00:08:39 +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=1743638921; cv=none; b=XHBbW67AlbR6jMa3vwHDYjMNq2kJZQKDl1LMVpkbxiYPHoFM3I46tDUFltq9s0uTxLApbq+WQyCSKFUeZGi0VLIpI4KFuxODMf7wwg7wSOVFL09rjkk7bCNuojLVKvMPD5ca+SqrjSYnxMAt1Lv+74nsSZ3UJU1BHhDJunqJ3CM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743638921; c=relaxed/simple; bh=A4qOF8g/Lp0fNN70XKVNtD9pF/sKUksq0VFQkLxYtOA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YR+D7PGCCczOGk8iUPlxoR5eUk5ZQElHLNwBJchD0ttmUjINKyKExVmre33NrDgrIR8Tu3DaACtIvOxBZdGyfAQTHDtmlhDm0iOEdeBK6l9KU9dQIhfgTN0VGt/Say4IChiG7img0Qgtf+IKhxBF4h8koaYlLvmjF1k5k2Enmqc= 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=Eem3MP+g; 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="Eem3MP+g" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff605a7a43so495658a91.3 for ; Wed, 02 Apr 2025 17:08:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743638919; x=1744243719; 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=Eem3MP+g4EtVriA43tI3xztih2frs8m0SDRAT+ey1GJCq69R56WsoDNa7zniFZMVvy KTWeByMWvOGbbcc/exEJD6vt5bEyvx+8l0TyGdt9/q+2jT1wPzqbPFj7d2oRPAUHs3De gilDD7Lv+QRNJnXKCVQSV94JHof+SU2XzOUWNxrzRVOty4X7PvMGUgX4KrzQS7rmXfFj poBBJRSPDN/EaDlPEKUYbrUAkM7eWdtKhVyg8avnPA5PnkR4lTMGW2EAG6qfZQiRrk2f CTcTTbP4jlFTycgG+uT2L4x0msLPH39IbNqgV2YUK5MDiWochrxuJfxa7hDFFKBgT08Q urQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743638919; x=1744243719; 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=ipx20+MohwojacOu6V/nztXAqz+UqlZo4+ifuRWwfVPeTYmEgv3hmjjjm40BmQ9s7V fX0eD4Uo7Ym/H1cNPRoKUgAnLvnCOPtWMQ8nHDtW6tubFjq2Bd+H7maBJ2c/4rthwTjc THVm/If4E4b8DUc+kPdmzBtsuLqQ1y8nDgYrdpBkrXWynUjLQiJwCBGZjizTtIdjym5V u4lhYYprQcM+Z5ajtRHLPNi3ATX5QrWMO2ZlWVkpzAoy/n0pc6NZ7EQTqjGzhy6ns/QD gSEsHhv1sYu5S5zN5nLFxzICVJ0igcrtEklB8UAZr9knTM9hHGXdO/cirlFAciM0tXcH yjcQ== X-Forwarded-Encrypted: i=1; AJvYcCVfZjEyWMfWMQwePBHmoB8+mCPGTFtdwEkDQxj/JIHJ96qKQ8nThvZKA7KxirWbyCNkbjJeTRNXGa8Rjnk=@vger.kernel.org X-Gm-Message-State: AOJu0YzYG2l7N4pIx9odY8+9/fVl4APU1YKSI3TqpAvzjXtbKQm1/BuS 9gO9j2H6OAfGoKfDPWiyIzZyq4eOe+fdtPDRjD11GKWzsLdgKOWptGK6rRwBDLBPvg== X-Google-Smtp-Source: AGHT+IEn+KDt633hxPIy/Xv0o5YX2H1GlYC0LI3Wy6iiMYKkwRX/0697A1IGauH6w3mSOoDfNBnB/3U= X-Received: from pjbsu16.prod.google.com ([2002:a17:90b:5350:b0:2ea:9d23:79a0]) (user=pcc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5241:b0:2ff:58a4:9db5 with SMTP id 98e67ed59e1d1-3056ef46f92mr5028152a91.30.1743638918950; Wed, 02 Apr 2025 17:08:38 -0700 (PDT) Date: Wed, 2 Apr 2025 17:06:59 -0700 In-Reply-To: <20250403000703.2584581-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: <20250403000703.2584581-1-pcc@google.com> X-Mailer: git-send-email 2.49.0.472.ge94155a9ec-goog Message-ID: <20250403000703.2584581-2-pcc@google.com> Subject: [PATCH v5 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 Reviewed-by: Catalin Marinas --- 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 Tue Dec 16 23:44:45 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 AB464EAFA for ; Thu, 3 Apr 2025 00:08:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743638923; cv=none; b=l8Wgj3PoxnUlITpb76HG6WFWH7KFLW59Lk8vROqHH1oMtwa0LzlJraXdlR/x1sXrLxrakslip6e7Ks0RIHoh3NOZn6oswpzofcdr/LkCeVfDuE48GXg1eW1JQ6xjiUPD7NgcqQRNvNVxuW085zRyT3NDfPfZQFhZ1XvESUHxLBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743638923; c=relaxed/simple; bh=SEXoMgfO871mcJmIm6KSXpbtvdsiD+EaJJqUWAfrQ38=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jSc5QTu6pPMC8AfbJh7wwbRFAq/E1LxLsv/vRk/sQnVhUy2o+F79uNwckyU9oNq1WPwKlqyUVA4KkTXH4r3frh4tm8yO104MZCr4BjJTXakv8ANQevKwITWgSmSGDC130CpU4a+BAstiE+WEhzXOjfa2E4ZpCkZ56ZlyG2b6+L8= 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=r1RYD+5/; arc=none smtp.client-ip=209.85.216.74 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="r1RYD+5/" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ff55176edcso271512a91.1 for ; Wed, 02 Apr 2025 17:08:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743638921; x=1744243721; 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=NyoF5i3nxCUfNVJkAVl8GCLqKKAkd0DOEReeKsHnmjw=; b=r1RYD+5/EOTB26KkaCiqFXm/gJHwFDfSeWt8ZVgicOm249ixyGNyfZNLF/Sm4ZfjgL K/dP39BbVPOq0j+UtVdqeXyjvbk4edgV3QwLWD01iK1kcJv3MmGO8RgKMN+8nIVcHHbq 5RjsDecbyHK6aAfav4rWdnCKeOWGRGDqcTxKrNX82hCtxNjDGQcYEFCed1XjXrqXNSzh akZXSOlvlhIOIFig78Oh/uT5pp5D6pRpWai2ywbMTYNZJeT2qCT7TfZbmRhODR6kt1Df hWF+sZ8ETYjJ54sYlLMTa3IB6i6FB9VlVRWT4liAt/og60yUAu/1+sY+haZbl5nQSNiC SaiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743638921; x=1744243721; 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=NyoF5i3nxCUfNVJkAVl8GCLqKKAkd0DOEReeKsHnmjw=; b=td7aPSNW2B/BgKD+lpIgjUPD8huyNfzOMZHQh1GSn6QL1kGvNiXngnqjM8tyjSQCY0 x10VtwmCTD6nTO6zWBygFy8PVloBe7374yw12+VLsCR2GmBkUZXQgJ7TJhiPnObDQbA9 j9IA38VcReiiHMqqbYIytQhRHqXzG+sgP1M4mGnTs+jlhUoGVyMpvZ2Otqfn7CG4ErO7 7djotWKwnpq6x+oUtYkYHyEV+H8in+TeZC4z2Tl7Cf2KkdP6C9PzTUSsOpfwB8P5Qmtz AtZ+QM/NEOL00cbHN9WFoCGL4Moh3aQruh5hCrvIXr1VsJNvPschX76fcYB8ZPCtsfrz Bdxw== X-Forwarded-Encrypted: i=1; AJvYcCVCczYZFx46SyvIy0ShXE9nhfzbHsKCt9Wcenm4tl1dcpQAu/TcxNaw+5ih/ZFIKla3ChfzhLUmeX/3cyo=@vger.kernel.org X-Gm-Message-State: AOJu0Yxs/OHY+2MZa2Z55ybjFKDt7MgGNK4Qptn1fq1+ccRNFjHtI0+S 6/w+MTrWjS3T/f7Zds6Oh0s5VvPGEozy4LaLngvuSShlbLAUcqPk0cqfsI7S3e8Alw== X-Google-Smtp-Source: AGHT+IF9PPSWYluV28LHjIAQYtoCIsrCyo2nNzgTlCfOaTtLczxhcR0/cQHFz16AjBy9tsQ09yZBi4s= X-Received: from pja11.prod.google.com ([2002:a17:90b:548b:b0:2fc:3022:36b8]) (user=pcc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:dfc7:b0:2ee:b875:6d30 with SMTP id 98e67ed59e1d1-30531fa4dd6mr27878978a91.9.1743638920951; Wed, 02 Apr 2025 17:08:40 -0700 (PDT) Date: Wed, 2 Apr 2025 17:07:00 -0700 In-Reply-To: <20250403000703.2584581-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: <20250403000703.2584581-1-pcc@google.com> X-Mailer: git-send-email 2.49.0.472.ge94155a9ec-goog Message-ID: <20250403000703.2584581-3-pcc@google.com> Subject: [PATCH v5 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 Reviewed-by: Catalin Marinas --- v5: - add test for unreadable first byte of strscpy() source v4: - clarify commit message - improve comment v3: - simplify test case v2: - rebased - fixed test failure mm/kasan/kasan_test_c.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/mm/kasan/kasan_test_c.c b/mm/kasan/kasan_test_c.c index 59d673400085f..e8d33af634b03 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,25 @@ 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)); + + /* strscpy should fail if the first byte is unreadable. */ + KUNIT_EXPECT_KASAN_FAIL(test, strscpy(ptr, src + KASAN_GRANULE_SIZE, + KASAN_GRANULE_SIZE)); + + kfree(src); kfree(ptr); =20 /* --=20 2.49.0.472.ge94155a9ec-goog