From patchwork Tue Oct 15 01:33:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ritesh Harjani (IBM)" X-Patchwork-Id: 13835648 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8650CFC505 for ; Tue, 15 Oct 2024 01:34:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 692426B008C; Mon, 14 Oct 2024 21:34:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6405D6B0092; Mon, 14 Oct 2024 21:34:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E1F86B0093; Mon, 14 Oct 2024 21:34:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 3217D6B008C for ; Mon, 14 Oct 2024 21:34:00 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 739691403F1 for ; Tue, 15 Oct 2024 01:33:51 +0000 (UTC) X-FDA: 82674115230.22.2184453 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf03.hostedemail.com (Postfix) with ESMTP id AB7B220005 for ; Tue, 15 Oct 2024 01:33:54 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=c26xhBQy; spf=pass (imf03.hostedemail.com: domain of ritesh.list@gmail.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=ritesh.list@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728955895; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2IOurK7ENej5lHHiP4d73lr8yQG82cdevQPBO00m0fo=; b=LvGdRw+zDNer+rYrAUzDvAhc38F8TGb5Wicz8mkmsTDomf+r4f48bkNt0Vg+FxmFo7ScuI IcE2btoXmDV65yzEVfMgnpuZOsuYSv4Ja4aa0NLbWSUzd2JegAyAwGU3n7D5uaTo3uupYH fumJAkFokJ0xLvTc3/iH/RUF8HSSRWs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728955895; a=rsa-sha256; cv=none; b=CzBWm9ivqUYtx9PgxApAFos2uWKv4YxIlKcb/GRz2nLjmPGAZDVKEEss1/5FvBWIsxcLyU /R0O4DvMmSgwQdBHN//6uwAPgcbxpChjuhcmUr1lrcop8wIzq7Yae5YdhJSBCLEVifwi5e 6hTJU/q/xlN1OxxvRRTe7YtBD3PzgXU= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=c26xhBQy; spf=pass (imf03.hostedemail.com: domain of ritesh.list@gmail.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=ritesh.list@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-71e52582cf8so1557224b3a.2 for ; Mon, 14 Oct 2024 18:33:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728956037; x=1729560837; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2IOurK7ENej5lHHiP4d73lr8yQG82cdevQPBO00m0fo=; b=c26xhBQyz90hX7+y9Z4CCHe/tIuDXay6GzmsaPB3KqqObibqwwL3rGBwauJQHuBbcf l+1Mot5yLH/1QejnrW2Ek64C5b5t99lpG+ldnkwJkuaGN6OjFfZjpqkz4Hl1eCDSyStA gIk+7wfFO2wASA2OKYAcn59Y4i70yXanzmGaJXu0rVa6ZjRYZt3cm0lFWn+TxurNQci7 ws6fIa5geAvcp4b3lqK/1N6pTbo4BdyRw5kDeZHFOBGpf9g43/FntbYoYUI5rTvabN5C gNm0mEEyed1afhD5SHyp7NaepIem5tpFvxdCfC8T3rBrBecj6E23GBjjpqliY196GLpW HhxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728956037; x=1729560837; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2IOurK7ENej5lHHiP4d73lr8yQG82cdevQPBO00m0fo=; b=Ge7nhpcWivp26RrRuDFfoLmUmN4tqj7/vxgtDbQGekzmUzpqhFfxWy6qY0jCKgjb8b +2czfoJycO8wgvu+o8f+ud0DlkFqdPJWa9SdRNixQpnpIurTdiwnaVmgo3wBqHjmkiRf x8G1lj2moCN5W2VVnHQVnA6+C98Qj1VTvVrQHxkCDHf91vWGCudmh5lvcE21LCbFrwDL /JC6bwrqSzrmwFhqMmPkv+eiOHaN3xJw7V/FYvfkN8KZF2IEouV6C3tYxULna0oVIcdi RpeJwiRzFqasW8sdLssUjxg97wT730gGghoN7soVrkphjXtNWgo/cOGP+3YJA1ob/XMs zfwg== X-Forwarded-Encrypted: i=1; AJvYcCU4eBuBR8i7r7NKq7ALjf+dkogR5y7tZ3zaBUYKDavEOZa2GI3sEAD8e0uB0mB5N72raQjdzkbcTw==@kvack.org X-Gm-Message-State: AOJu0Yx4W7NAsn6+W9fGGzwoiT6IpRwhRfSVoOHx385vXa+byK9+TE6F X6Gzotyc1XV5HMgASaeS/90RG41g3rlGYZ1EK4eCr/B2YoWZi509 X-Google-Smtp-Source: AGHT+IE+s7PAXjF3lV5jJ2AuCaaz/0o7aAW2i8sfMGGFqoeg6aQJcAwijtM1PaQhSx1fjTwLZ36lFg== X-Received: by 2002:a05:6a00:174b:b0:71e:148c:4611 with SMTP id d2e1a72fcca58-71e4c13a1d0mr15707237b3a.6.1728956036726; Mon, 14 Oct 2024 18:33:56 -0700 (PDT) Received: from dw-tp.. ([171.76.80.151]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71e77508562sm189349b3a.186.2024.10.14.18.33.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2024 18:33:56 -0700 (PDT) From: "Ritesh Harjani (IBM)" To: linuxppc-dev@lists.ozlabs.org Cc: kasan-dev@googlegroups.com, linux-mm@kvack.org, Marco Elver , Alexander Potapenko , Heiko Carstens , Michael Ellerman , Nicholas Piggin , Madhavan Srinivasan , Christophe Leroy , Hari Bathini , "Aneesh Kumar K . V" , Donet Tom , Pavithra Prakash , LKML , "Ritesh Harjani (IBM)" , Disha Goel Subject: [RFC RESEND v2 02/13] powerpc: mm: Fix kfence page fault reporting Date: Tue, 15 Oct 2024 07:03:25 +0530 Message-ID: <6bf523aa03e72d701d24aca49b51864331eed2d5.1728954719.git.ritesh.list@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: 7coh8sn31wwiadu8yci3awhpi4fmp7cu X-Rspamd-Queue-Id: AB7B220005 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728956034-140870 X-HE-Meta: U2FsdGVkX1/5FIVpuPCdV9MXkwQTmqYto+OsHRjnl+wTyuvCWaln9eE38mNuCg9keTJS7s33iFHS1OsVCTshbtPYL6SeRzOZGFBsHgluM1vbSjHfIzAe4J1oNWufbdrZXY3MX/HhUqsWs1dIrZH1BfYdgCfYJCjL3TgDHqSjJZMYprM/9JpbB/F2JlhjODsMMi91e+ktEJyDXILCviYszznXpeIqNgzwD81zZGFK21MPKO1K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2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: copy_from_kernel_nofault() can be called when doing read of /proc/kcore. /proc/kcore can have some unmapped kfence objects which when read via copy_from_kernel_nofault() can cause page faults. Since *_nofault() functions define their own fixup table for handling fault, use that instead of asking kfence to handle such faults. Hence we search the exception tables for the nip which generated the fault. If there is an entry then we let the fixup table handler handle the page fault by returning an error from within ___do_page_fault(). This can be easily triggered if someone tries to do dd from /proc/kcore. dd if=/proc/kcore of=/dev/null bs=1M =============================== BUG: KFENCE: invalid read in copy_from_kernel_nofault+0xb0/0x1c8 Invalid read at 0x000000004f749d2e: copy_from_kernel_nofault+0xb0/0x1c8 0xc0000000057f7950 read_kcore_iter+0x41c/0x9ac proc_reg_read_iter+0xe4/0x16c vfs_read+0x2e4/0x3b0 ksys_read+0x88/0x154 system_call_exception+0x124/0x340 system_call_common+0x160/0x2c4 BUG: KFENCE: use-after-free read in copy_from_kernel_nofault+0xb0/0x1c8 Use-after-free read at 0x000000008fbb08ad (in kfence-#0): copy_from_kernel_nofault+0xb0/0x1c8 0xc0000000057f7950 read_kcore_iter+0x41c/0x9ac proc_reg_read_iter+0xe4/0x16c vfs_read+0x2e4/0x3b0 ksys_read+0x88/0x154 system_call_exception+0x124/0x340 system_call_common+0x160/0x2c4 Guessing the fix should go back to when we first got kfence on PPC32. Fixes: 90cbac0e995d ("powerpc: Enable KFENCE for PPC32") Reported-by: Disha Goel Signed-off-by: Ritesh Harjani (IBM) --- arch/powerpc/mm/fault.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c index 81c77ddce2e3..fa825198f29f 100644 --- a/arch/powerpc/mm/fault.c +++ b/arch/powerpc/mm/fault.c @@ -439,9 +439,17 @@ static int ___do_page_fault(struct pt_regs *regs, unsigned long address, /* * The kernel should never take an execute fault nor should it * take a page fault to a kernel address or a page fault to a user - * address outside of dedicated places + * address outside of dedicated places. + * + * Rather than kfence reporting false negatives, let the fixup table + * handler handle the page fault by returning SIGSEGV, if the fault + * has come from functions like copy_from_kernel_nofault(). */ if (unlikely(!is_user && bad_kernel_fault(regs, error_code, address, is_write))) { + + if (search_exception_tables(instruction_pointer(regs))) + return SIGSEGV; + if (kfence_handle_page_fault(address, is_write, regs)) return 0;