From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3274AC7EE22 for ; Mon, 8 May 2023 21:52:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233959AbjEHVwt (ORCPT ); Mon, 8 May 2023 17:52:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233098AbjEHVws (ORCPT ); Mon, 8 May 2023 17:52:48 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 235A8422A; Mon, 8 May 2023 14:52:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=eRQGnZvNHAr7ZLmQ2c/n160+Xuj4wnfU8/X1ZrpT+os=; b=bzbCx8ugyvY+QOYywH7H37/+g2 L3UlTqZW4KEM6EjuOdAfAU9Clp7mjXnEOgUUQ52/5zLYoeSyG1mVamDlE4ZmxQBbkk5oAaHlr/YQD jcVjXjqh+lvf1A7HdoLOIt11lp6coTQeTsfl1lZMJ/tXzABh2bohq0wr4Br3GWKXwkFc01y1X0zOX aSvz7xjzo1gA1P96FZqIRKqmVblm40fIVzM11Fa8VoE3+27G7tS5PDYsfR7f8tATYEglIgJx40eVL 463E8gCJm4q5Nu8exndie2IYIGB8uVSEC/4SfWlYim5/J0N69aTSVni9d0c76uX+XYfhBHa0bwmYQ NaCs/FLw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pw8ma-00EXAU-Mn; Mon, 08 May 2023 21:52:36 +0000 Date: Mon, 8 May 2023 22:52:36 +0100 From: Matthew Wilcox To: Pasha Tatashin Cc: Ruihan Li , syzbot+fcf1a817ceb50935ce99@syzkaller.appspotmail.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, linux-usb@vger.kernel.org, syzkaller-bugs@googlegroups.com Subject: Re: usbdev_mmap causes type confusion in page_table_check Message-ID: References: <000000000000258e5e05fae79fc1@google.com> <20230507135844.1231056-1-lrh2000@pku.edu.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org On Mon, May 08, 2023 at 02:48:59PM -0700, Pasha Tatashin wrote: > On Mon, May 8, 2023 at 2:36 PM Matthew Wilcox wrote: > > > > On Mon, May 08, 2023 at 05:27:10PM -0400, Pasha Tatashin wrote: > > > > static void page_table_check_set(struct mm_struct *mm, unsigned long addr, > > > > unsigned long pfn, unsigned long pgcnt, > > > > bool rw) > > > > { > > > > // ... > > > > anon = PageAnon(page); > > > > for (i = 0; i < pgcnt; i++) { > > > > // ... > > > > if (anon) { > > > > BUG_ON(atomic_read(&ptc->file_map_count)); > > > > BUG_ON(atomic_inc_return(&ptc->anon_map_count) > 1 && rw); > > > > } else { > > > > BUG_ON(atomic_read(&ptc->anon_map_count)); > > > > BUG_ON(atomic_inc_return(&ptc->file_map_count) < 0); > > > > } > > > > // ... > > > > } > > > > // ... > > > > } > > > > > > > > This call to PageAnon is invalid for slab pages because slab reuses the bits > > > > in struct page/folio to store its internal states, and the anonymity bit only > > > > exists in struct page/folio. As a result, the counters are incorrectly updated > > > > and checked in page_table_check_set and page_table_check_clear, leading to the > > > > bug being raised. > > > > > > We should change anon boolean to be: > > > > > > anon = !PageSlab(page) && PageAnon(page); > > > > No. Slab pages are not elegible for mapping into userspace. That's > > Sure, I can add BUG_ON(PageSlab(page)); to page_table_check_set. > > > all. There should be a BUG() for that. And I do mean BUG(), not > > "return error to user". Something has gone horribly wrong, and it's > > time to crash. > > It is just too easy to make slab available via remap_pfn_range(), but > I do not think we want to add BUG() into the remap function, otherwise > we will break devices such as /dev/mem. Slab pages can't be mmaped. Really, no matter what interface you're using. page->_mapcount is necessarily incremented by mapping to userspace, and slab uses that space for its own purposes (and has for decades). It's similar for page tables and other allocations that use PageType.