Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2031419rwd; Mon, 15 May 2023 06:29:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5bPYRZ4eiPAPL73fUOk0WzBb+E7UJiHlBdD+zbWk8KsZPhnBQTyDIGP50f/a/fYj+xGz8O X-Received: by 2002:a17:90a:e60e:b0:24e:1ca3:7279 with SMTP id j14-20020a17090ae60e00b0024e1ca37279mr33636633pjy.38.1684157362254; Mon, 15 May 2023 06:29:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684157362; cv=none; d=google.com; s=arc-20160816; b=aisUxJDpx41FPHf2i8dZ5Xg2ZY4kzdGNCB/foQAnMQXcQ1c79mSkMdQKoG7vzJgY4j dTwvVHCcv2gKnot1MAtwQj0fK7Oe13+aCVLnRUVLxQaq6BAD8/51SdFqIbAuNWZ1jWQe kUja/V1u1c8Iw/ElCJGt/IqDWBSJLN+swXq/MCmeoo+08FWikXJXYaiLkkVjemKTGMW/ zYLzUYvV3lFzgmC3XBVAhix5p9H9uqAMnoGobZHTy/2GJt9N+IMLflRWi+dTPy900kEr eXtRxs1JgM4dp3X8SU5e9Z9b1FwQYRwx+FsE8obdp/3vGDm5rhSL7XVLKIGG9yq2B6nP pfow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=L+SQ0nmfGSPRi8glqTzv3fHgUPypuBnrsBZKLf4on5Y=; b=SadJGuXcWkPgeF0okUcMVbXjt7nvAQHUI5E6N2sWjUL0c2Qz8n21aeyTpDdRQqzcJ5 Yq8wKCryIVLgMndWVPU7QvGDT8GZJknEcgYH84NPHiWxlzBXdz2MWIWCQld3lk/IsSZj OL5KHbdSMOVPlYsrj/JnahhX1UWNn71NDLftVxQRDY00HEiout+b+agmsJlsIJKtIbl7 oEE8H8Fx4ALQ2HJwzYzt7y7qrfyGZlR7ZtUdxhFWFMtvoAbL8su35IOwRii4QoZD8m1L uRvHlEWDIYHmumwUS+l9no09fQZOAiavBpOvhsqT7Vi1IR7oyqVXce7V/SOhOTjJi0ql 0LvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@pku.edu.cn header.s=dkim header.b=ox9S7sNS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=pku.edu.cn Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv1-20020a632e01000000b0051b6ee8bdd0si15595288pgb.687.2023.05.15.06.29.06; Mon, 15 May 2023 06:29:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@pku.edu.cn header.s=dkim header.b=ox9S7sNS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=pku.edu.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242250AbjEONLH (ORCPT + 99 others); Mon, 15 May 2023 09:11:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242254AbjEONKr (ORCPT ); Mon, 15 May 2023 09:10:47 -0400 Received: from pku.edu.cn (mx19.pku.edu.cn [162.105.129.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BF4B32686; Mon, 15 May 2023 06:10:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pku.edu.cn; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-Id:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=L+SQ0nmfGSPRi8glqTzv3fHgUPypuBnrsB ZKLf4on5Y=; b=ox9S7sNSOR+qIM/NofYQxW7bDUidbSTXFsfF5W//szZbyOtCdS Zx5RZyiJbOaHg5CnaOa7fFpoekGi458S3qVhX4jk9l9h2KWAPGvkA57DX64e6HkD QZsoMnu23gTA4M6u1BC6GpPdd7iAD65zNx3q2817kRszYNDYcsmlLlyt4= Received: from localhost.localdomain (unknown [10.7.98.243]) by front02 (Coremail) with SMTP id 54FpogAnLDgqL2JkVboyFA--.10053S6; Mon, 15 May 2023 21:10:08 +0800 (CST) From: Ruihan Li To: linux-mm@kvack.org, linux-usb@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Pasha Tatashin , David Hildenbrand , Matthew Wilcox , Andrew Morton , Christoph Hellwig , Alan Stern , Greg Kroah-Hartman , Ruihan Li , syzbot+fcf1a817ceb50935ce99@syzkaller.appspotmail.com, stable@vger.kernel.org Subject: [PATCH v2 4/4] mm: page_table_check: Ensure user pages are not slab pages Date: Mon, 15 May 2023 21:09:58 +0800 Message-Id: <20230515130958.32471-5-lrh2000@pku.edu.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230515130958.32471-1-lrh2000@pku.edu.cn> References: <20230515130958.32471-1-lrh2000@pku.edu.cn> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: 54FpogAnLDgqL2JkVboyFA--.10053S6 X-Coremail-Antispam: 1UD129KBjvJXoWxurWrCw4UAFyDWF48ZrW3Awb_yoW5Wr4Dpa 95u3W0krW5Ka4akw1kZ3ZayryrJFZ8G3yUCry7J3Wjv3ZxtFy0vF1jkr9ay3s8KrW7CFy5 AFZ8tr1j9rWDZ3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBa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INE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The current uses of PageAnon in page table check functions can lead to type confusion bugs between struct page and slab [1], if slab pages are accidentally mapped into the user space. This is because slab reuses the bits in struct page to store its internal states, which renders PageAnon ineffective on slab pages. Since slab pages are not expected to be mapped into the user space, this patch adds BUG_ON(PageSlab(page)) checks to make sure that slab pages are not inadvertently mapped. Otherwise, there must be some bugs in the kernel. Reported-by: syzbot+fcf1a817ceb50935ce99@syzkaller.appspotmail.com Closes: https://lore.kernel.org/lkml/000000000000258e5e05fae79fc1@google.com/ [1] Fixes: df4e817b7108 ("mm: page table check") Cc: # 5.17 Signed-off-by: Ruihan Li --- include/linux/page-flags.h | 6 ++++++ mm/page_table_check.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 1c68d67b8..92a2063a0 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -617,6 +617,12 @@ PAGEFLAG_FALSE(VmemmapSelfHosted, vmemmap_self_hosted) * Please note that, confusingly, "page_mapping" refers to the inode * address_space which maps the page from disk; whereas "page_mapped" * refers to user virtual address space into which the page is mapped. + * + * For slab pages, since slab reuses the bits in struct page to store its + * internal states, the page->mapping does not exist as such, nor do these + * flags below. So in order to avoid testing non-existent bits, please + * make sure that PageSlab(page) actually evaluates to false before calling + * the following functions (e.g., PageAnon). See mm/slab.h. */ #define PAGE_MAPPING_ANON 0x1 #define PAGE_MAPPING_MOVABLE 0x2 diff --git a/mm/page_table_check.c b/mm/page_table_check.c index 25d8610c0..f2baf97d5 100644 --- a/mm/page_table_check.c +++ b/mm/page_table_check.c @@ -71,6 +71,8 @@ static void page_table_check_clear(struct mm_struct *mm, unsigned long addr, page = pfn_to_page(pfn); page_ext = page_ext_get(page); + + BUG_ON(PageSlab(page)); anon = PageAnon(page); for (i = 0; i < pgcnt; i++) { @@ -107,6 +109,8 @@ static void page_table_check_set(struct mm_struct *mm, unsigned long addr, page = pfn_to_page(pfn); page_ext = page_ext_get(page); + + BUG_ON(PageSlab(page)); anon = PageAnon(page); for (i = 0; i < pgcnt; i++) { @@ -133,6 +137,8 @@ void __page_table_check_zero(struct page *page, unsigned int order) struct page_ext *page_ext; unsigned long i; + BUG_ON(PageSlab(page)); + page_ext = page_ext_get(page); BUG_ON(!page_ext); for (i = 0; i < (1ul << order); i++) { -- 2.40.1