Received: by 2002:ac8:6216:0:b0:41c:c224:f26f with SMTP id ks22csp77410qtb; Thu, 2 Nov 2023 12:02:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFpeObgEFSoXezU+9YgvXon2MM1aAM7XRgkKe4lhbge6D8B1OIEAnc9M9gWq4TmIKztsgr/ X-Received: by 2002:a05:6a20:3d0b:b0:16b:c22c:f99c with SMTP id y11-20020a056a203d0b00b0016bc22cf99cmr25545783pzi.32.1698951758664; Thu, 02 Nov 2023 12:02:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698951758; cv=none; d=google.com; s=arc-20160816; b=mBkeCGe9xmSx1peHiPq7PDlSFAKbV76BIS2mZMsYFMa6A+3oAa1rHT7KhOQ1wWUcI3 zgtVljfW1NYu3VqkCenbKokadc/4lAeLCphCbP+eIKfuFqY7KYBUTCuCXQL1l48msVK7 OZMhiY76syHMMtkfk4Tvugz3rHamIIx3Cpt/d5hk2NiphL3jY0VLUK7QpmMg4se3wgcn AkX26doc/c2qYJQkISRLXjaYlIG/Xdd9ArM9cQwt3L9ePwwgkVZa1IbdTz0eW9CQF8W4 E/MSmvUQw/g+Xsnk2w1sNf15wgXMHgnQmnBWGkI8SG3zVntjhKmed5lhzFlGjfeaKO/0 W7Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=b1R9Rk0WygxMRqJq8TtBQ2hK9DFJHW6itqPQ8m1un1Q=; fh=pj5uIYBUhMHYtS8RaaHKfZc9t2R00gkw8e203/cbxys=; b=koklatSNS8olnP2CLhKNLlO5GvV4DMBmp4NJDHCzhq4zaUvT/qvor2pDyVnocactgQ OMuphBkAcNNe+AR6OUNPtDWOKjoCYNGYyybqJPuhW376c1yQI+eVr6Z8smG1+GfbLEb8 YJBuwFtPeyAAtD6HTibwHWc13PD1P/XsJR12p12dvRAKb/SfXyg9ElJ+UT3Bvalq6g/k EBb2f2hfwa14MwT2lq7o+osZK4vq/SijUtLDQ7MK5Kk8frVLW7MGH+siR0kTUQGCK3Ka GpbV5iXrcM7QhjDKMArY7xmNLyyokHASi2QPb+ifAJOdXtM8zXP9p8P2WzTSb//Qv4NS g2kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=FoDh8yGL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id r20-20020a6560d4000000b0059b9e4e53d5si124666pgv.218.2023.11.02.12.02.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 12:02:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=FoDh8yGL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 865298062913; Thu, 2 Nov 2023 12:00:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377358AbjKBTAm (ORCPT + 99 others); Thu, 2 Nov 2023 15:00:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234446AbjKBTAi (ORCPT ); Thu, 2 Nov 2023 15:00:38 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FEB7186 for ; Thu, 2 Nov 2023 12:00:34 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-5b87150242cso1043151a12.0 for ; Thu, 02 Nov 2023 12:00:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698951634; x=1699556434; 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=b1R9Rk0WygxMRqJq8TtBQ2hK9DFJHW6itqPQ8m1un1Q=; b=FoDh8yGLa2tBK3Bn+1BOvZ5abo/KwHU2/PkPuCh82hqmCqjzrG3d350ubBL8bpikQS smaY7CgszrKDkBeLDNZzqnSNLYg6WwpTz3ZBu1dAqv+POEHAtjacywCgG7sGVhmf1xLq TKeKa9rExWV9pff30ipJqb+xp6pPtiZK1NvT05D0GWa6FlyOGCZsqrCMgtxqAK9tagDo 50mBXfKloKGIPRDRx0v0AqK0yovtbMiJ+l4oy8eAKcRXzwhTZmSCd4EG4L9Xno1suJ2L X/vvCj7bE+mMXSMrDEqVV7ytGowYT8Dy1NUuxVKfyLyV7ABMalfZFAb1E3uU1bD6zwDb I8xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698951634; x=1699556434; 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=b1R9Rk0WygxMRqJq8TtBQ2hK9DFJHW6itqPQ8m1un1Q=; b=nZv83zlgKTNdXaeYowbRIX1Y0cK2cpyhjqAGZgEUJEzV1iCzrVV0X3MT4ULlfHBqiN qrN9u4uboSWa+LFWpRERxW87E83xDLoXSpIfyFbWmgbvTULcf+/vsUPUQUKRrnv+t9Zi Th8zO7wd28Dyn/v/HMFPqI/M89Io5FAj7wae0FRmIvAuom5bqbM+/ZwFq6L3C3xUbkFh yGUwIoeHZ1WYJc2JBzyL7+lPwFU4Efjg47iy56lQCenfbzfpQoigt3L2M6RN9UW4q6V9 RIzhUe8w8yZxuHBmuX5/KZW0rvtPOpG+gmJt3hGj9+cNuYGQQqKTk4qbK9NL/Zv15Cxp 5Oag== X-Gm-Message-State: AOJu0YyTK6kRnd3Baz6bFo8X+J3mMVmmk1ZSCGXQYOeL3+6X4Gv4AFmH zUmR0Ht0um3YXLUEUF9pjeQ+jiuTfZlkIQ== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a63:755c:0:b0:564:8375:d240 with SMTP id f28-20020a63755c000000b005648375d240mr337944pgn.12.1698951633813; Thu, 02 Nov 2023 12:00:33 -0700 (PDT) Date: Thu, 2 Nov 2023 18:59:03 +0000 In-Reply-To: <20231102185934.773885-1-cmllamas@google.com> Mime-Version: 1.0 References: <20231102185934.773885-1-cmllamas@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231102185934.773885-3-cmllamas@google.com> Subject: [PATCH 02/21] binder: fix use-after-free in shinker's callback From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan , Andrew Morton , Vlastimil Babka , "Kirill A. Shutemov" , Matthew Wilcox , Michal Hocko Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, stable@vger.kernel.org, Liam Howlett , Minchan Kim Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 02 Nov 2023 12:00:51 -0700 (PDT) The mmap read lock is used during the shrinker's callback, which means that using alloc->vma pointer isn't safe as it can race with munmap(). As of commit dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in munmap") the mmap lock is downgraded after the vma has been isolated. I was able to reproduce this issue by manually adding some delays and triggering page reclaiming through the shrinker's debug sysfs. The following KASAN report confirms the UAF: ================================================================== BUG: KASAN: slab-use-after-free in zap_page_range_single+0x470/0x4b8 Read of size 8 at addr ffff356ed50e50f0 by task bash/478 CPU: 1 PID: 478 Comm: bash Not tainted 6.6.0-rc5-00055-g1c8b86a3799f-dirty #70 Hardware name: linux,dummy-virt (DT) Call trace: zap_page_range_single+0x470/0x4b8 binder_alloc_free_page+0x608/0xadc __list_lru_walk_one+0x130/0x3b0 list_lru_walk_node+0xc4/0x22c binder_shrink_scan+0x108/0x1dc shrinker_debugfs_scan_write+0x2b4/0x500 full_proxy_write+0xd4/0x140 vfs_write+0x1ac/0x758 ksys_write+0xf0/0x1dc __arm64_sys_write+0x6c/0x9c Allocated by task 492: kmem_cache_alloc+0x130/0x368 vm_area_alloc+0x2c/0x190 mmap_region+0x258/0x18bc do_mmap+0x694/0xa60 vm_mmap_pgoff+0x170/0x29c ksys_mmap_pgoff+0x290/0x3a0 __arm64_sys_mmap+0xcc/0x144 Freed by task 491: kmem_cache_free+0x17c/0x3c8 vm_area_free_rcu_cb+0x74/0x98 rcu_core+0xa38/0x26d4 rcu_core_si+0x10/0x1c __do_softirq+0x2fc/0xd24 Last potentially related work creation: __call_rcu_common.constprop.0+0x6c/0xba0 call_rcu+0x10/0x1c vm_area_free+0x18/0x24 remove_vma+0xe4/0x118 do_vmi_align_munmap.isra.0+0x718/0xb5c do_vmi_munmap+0xdc/0x1fc __vm_munmap+0x10c/0x278 __arm64_sys_munmap+0x58/0x7c Fix this issue by performing instead a vma_lookup() which will fail to find the vma that was isolated before the mmap lock downgrade. Note that this option has better performance than upgrading to a mmap write lock which would increase contention. Plus, mmap_write_trylock() has been recently removed anyway. Fixes: dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in munmap") Cc: stable@vger.kernel.org Cc: Liam Howlett Cc: Minchan Kim Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index e3db8297095a..c4d60d81221b 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -1005,7 +1005,9 @@ enum lru_status binder_alloc_free_page(struct list_head *item, goto err_mmget; if (!mmap_read_trylock(mm)) goto err_mmap_read_lock_failed; - vma = binder_alloc_get_vma(alloc); + vma = vma_lookup(mm, page_addr); + if (vma && vma != binder_alloc_get_vma(alloc)) + goto err_invalid_vma; list_lru_isolate(lru, item); spin_unlock(lock); @@ -1031,6 +1033,8 @@ enum lru_status binder_alloc_free_page(struct list_head *item, mutex_unlock(&alloc->mutex); return LRU_REMOVED_RETRY; +err_invalid_vma: + mmap_read_unlock(mm); err_mmap_read_lock_failed: mmput_async(mm); err_mmget: -- 2.42.0.869.gea05f2083d-goog