Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3079930rwe; Mon, 29 Aug 2022 05:36:41 -0700 (PDT) X-Google-Smtp-Source: AA6agR7VeYAygMrSbgWHqjaNB5+BeaaBfZ/pXOZyEXBJMn3FuK5ajnMbs4XaV/v9XXbcNiXZcEv7 X-Received: by 2002:a17:90b:3511:b0:1f4:e0cd:1e04 with SMTP id ls17-20020a17090b351100b001f4e0cd1e04mr18646397pjb.154.1661776601446; Mon, 29 Aug 2022 05:36:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661776601; cv=none; d=google.com; s=arc-20160816; b=GS7O+IH/YRfJWY7zrU+PdmOiDtzIDVEbvD7iNCyEQi8MfM0419zsF4E6eJTGhgHyMm iGMhLS3/TUpUId+VxN7B2LHZO7p5achexTQV5BSH4dj8rtRJYde9cgLB8qrA5z/JvqaR P7WkCNAGNVIFIAF1SD/EZ/pghb6mRXPsaBwuAVA5Oj2JJA58lUptzPk/Wz9K36SPf3j7 TLptMRVIne0nm8aagF5N9y6HrN8RplrI2e92J22+uDyDoMIi2HXhWVDIzbMS56z9Nxj/ 5cpiKyGbP2MyOM63A53UYodnsJWYurxKnWCQUWHSuqzzvb311Oz/UjbqNr0A9lyhwFc7 WjtA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=z2W6gwAQ0qmF6uIuuW9oTTrpXMrocDUguuk72xl4L6k=; b=gkvLpgU0sOXOiGm9LcOssx93RBSr7ehf0Am3Og3+ku4X0aLGTjaW8rvCGI4aZVss6g C89EykI7slUp9W8gmCbP8NFrCk5Q9xoFOhDhhbtKDG8QBdl4zmcoKbAWXHiRItcRcM0P 0GELv59pMDowZA5dVy9zA00OUztsIShEQq6sYGZ59VwfnuD0uDQpQQ/CD8OzsBHyelG+ Sjpbj0HCHUsizSkSlg5EwKf9tG+JeaVl1zp0/X4X4P14MJOHcgLAfxP5WUvzY600AzHP b/+167+KuLzHziszbWl7dDLeiSMUyneiGG+TinSjvZpgX1gI3EYc26GUubRUQ9u/bDSC p4YQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=p08yESJL; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f13-20020a056a00228d00b005360fd17809si472641pfe.63.2022.08.29.05.36.30; Mon, 29 Aug 2022 05:36:41 -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 header.i=@linuxfoundation.org header.s=korg header.b=p08yESJL; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229919AbiH2MWL (ORCPT + 99 others); Mon, 29 Aug 2022 08:22:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231390AbiH2MVp (ORCPT ); Mon, 29 Aug 2022 08:21:45 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C433E61; Mon, 29 Aug 2022 05:05:56 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id AE754B80F03; Mon, 29 Aug 2022 11:18:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0DA5C43141; Mon, 29 Aug 2022 11:18:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661771912; bh=lbbo5zIz7LBHyIKQSGFYcidE8T1e9xUIFuGqFczWtjQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p08yESJLcUm4icj97KaFF7AHIXwlyVUThHDGbb8Nd7y6anR4qHHLeDeDAfhj2NqQf MHl7UCajA2Uf0xaRwbOm02Vq06xHakP2EeSz2I1TNsKrfwhqoV+gaXzl+WddHdlCuA TIk4IpFFSjX6tzncU+BoLQ9JOLwONSCZW3qGhXiU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Liam R. Howlett" , Ondrej Mosnacek , syzbot+a7b60a176ec13cafb793@syzkaller.appspotmail.com, Carlos Llamas , Minchan Kim , "Christian Brauner (Microsoft)" , Hridya Valsaraju , Joel Fernandes , Martijn Coenen , Suren Baghdasaryan , Todd Kjos , "Matthew Wilcox (Oracle)" , =?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= , Andrew Morton Subject: [PATCH 5.19 138/158] binder_alloc: add missing mmap_lock calls when using the VMA Date: Mon, 29 Aug 2022 12:59:48 +0200 Message-Id: <20220829105814.857786586@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829105808.828227973@linuxfoundation.org> References: <20220829105808.828227973@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Liam Howlett commit 44e602b4e52f70f04620bbbf4fe46ecb40170bde upstream. Take the mmap_read_lock() when using the VMA in binder_alloc_print_pages() and when checking for a VMA in binder_alloc_new_buf_locked(). It is worth noting binder_alloc_new_buf_locked() drops the VMA read lock after it verifies a VMA exists, but may be taken again deeper in the call stack, if necessary. Link: https://lkml.kernel.org/r/20220810160209.1630707-1-Liam.Howlett@oracle.com Fixes: a43cfc87caaf (android: binder: stop saving a pointer to the VMA) Signed-off-by: Liam R. Howlett Reported-by: Ondrej Mosnacek Reported-by: Acked-by: Carlos Llamas Tested-by: Ondrej Mosnacek Cc: Minchan Kim Cc: Christian Brauner (Microsoft) Cc: Greg Kroah-Hartman Cc: Hridya Valsaraju Cc: Joel Fernandes Cc: Martijn Coenen Cc: Suren Baghdasaryan Cc: Todd Kjos Cc: Matthew Wilcox (Oracle) Cc: "Arve Hjønnevåg" Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- drivers/android/binder_alloc.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -395,12 +395,15 @@ static struct binder_buffer *binder_allo size_t size, data_offsets_size; int ret; + mmap_read_lock(alloc->vma_vm_mm); if (!binder_alloc_get_vma(alloc)) { + mmap_read_unlock(alloc->vma_vm_mm); binder_alloc_debug(BINDER_DEBUG_USER_ERROR, "%d: binder_alloc_buf, no vma\n", alloc->pid); return ERR_PTR(-ESRCH); } + mmap_read_unlock(alloc->vma_vm_mm); data_offsets_size = ALIGN(data_size, sizeof(void *)) + ALIGN(offsets_size, sizeof(void *)); @@ -922,17 +925,25 @@ void binder_alloc_print_pages(struct seq * Make sure the binder_alloc is fully initialized, otherwise we might * read inconsistent state. */ - if (binder_alloc_get_vma(alloc) != NULL) { - for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) { - page = &alloc->pages[i]; - if (!page->page_ptr) - free++; - else if (list_empty(&page->lru)) - active++; - else - lru++; - } + + mmap_read_lock(alloc->vma_vm_mm); + if (binder_alloc_get_vma(alloc) == NULL) { + mmap_read_unlock(alloc->vma_vm_mm); + goto uninitialized; + } + + mmap_read_unlock(alloc->vma_vm_mm); + for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) { + page = &alloc->pages[i]; + if (!page->page_ptr) + free++; + else if (list_empty(&page->lru)) + active++; + else + lru++; } + +uninitialized: mutex_unlock(&alloc->mutex); seq_printf(m, " pages: %d:%d:%d\n", active, lru, free); seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);