Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp817003ybl; Wed, 11 Dec 2019 07:58:47 -0800 (PST) X-Google-Smtp-Source: APXvYqyPHoD83ZS7OoXTDRT9tFufPLhpfRuexOedUuvXDeKQt5Pv2EJqOdc2TqJMg5/ZsyLEEc+L X-Received: by 2002:a9d:2028:: with SMTP id n37mr2920406ota.127.1576079927860; Wed, 11 Dec 2019 07:58:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576079927; cv=none; d=google.com; s=arc-20160816; b=sh/5AYs33RO8yVq1Fri1CEpMJlwzLTk9kDDvyVtQS6NvzT161aedG5wQ63Yx1mGsAL 5nMbvwy0PvuTz5b6AupuQQqAB4f+5Iiz+/kawMR39JQfzk/a7fgsIFEqrSoLb7/GlDGi XNhqMScAev1k3nJq5bf+nXEKqbf2hoAsI77FAb7sLzZtwKsigR9Qvev5umPx/aybgrt+ sbQAqWrSfbRFwWYIvw3kt9Wht853WDw35HwnCMhJnpSrhOTC7cmth8OM1YxT0ix4iEzR H7htnjENeUmubZK3vqfJjHTRo9ilf4jOUVIN0gqjna2Sez7iuRodsFujXygsooIP+/Uz kX6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ig3XOAE/R5PinuP1E0Jf01FVI09j2XdldrrvnJC7G50=; b=xjFpMa1WH8pAb0gD+crcQSw8su4Ng2Rnq3zAyw1lYoccshtPNePH2G7iy6jzrMfc91 R24KF+WmUju4G8oC75vHkcGx3fB9K3JVX35GdGWc2bJxV2Jja0/twmZi4z4Tar4ebwLB SY+WtO4audXOzpwQohS+H/RTBw++mjqLVOWluesBdsubvfd1Zc4tEc3UekCaCbULmdnE bmAkCol8c15gk229EZYupp/usiEqkm3K9BUuN6JBshfP6YIV9QQLsjMQ1NirhYbeDUso s/yD09hA5hw0FE+s9akybBojuQtk95zTMv0nkKAbWBpe1sAsmbvlCEgQXZdR55DXtlz0 caGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mBZ4bkcv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d16si1281352otc.210.2019.12.11.07.58.35; Wed, 11 Dec 2019 07:58:47 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mBZ4bkcv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732019AbfLKP40 (ORCPT + 99 others); Wed, 11 Dec 2019 10:56:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:41662 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731100AbfLKPP1 (ORCPT ); Wed, 11 Dec 2019 10:15:27 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0313C20663; Wed, 11 Dec 2019 15:15:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576077326; bh=c3vBMZp+lpXGhPTzAWD4guEpdgOH6VJJjvfEyhqNDmI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mBZ4bkcvH4fNjtpRXSuaMIawHG510FRYMNu54ltUxOCZKkHE/jjXCoHy9Ez6jG9WR THlDajn7fSuq6aVNlaEW7LoMdUA24PknO3s2tkV6y51obxkx1Ez9j6r9x+yXq3SfQV w9XVDx597rBO1MaapQp7rWys7SYUPNLISi9Wl2pQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Christian Brauner Subject: [PATCH 5.3 103/105] binder: Fix race between mmap() and binder_alloc_print_pages() Date: Wed, 11 Dec 2019 16:06:32 +0100 Message-Id: <20191211150305.663665117@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191211150221.153659747@linuxfoundation.org> References: <20191211150221.153659747@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jann Horn commit 8eb52a1ee37aafd9b796713aa0b3ab9cbc455be3 upstream. binder_alloc_print_pages() iterates over alloc->pages[0..alloc->buffer_size-1] under alloc->mutex. binder_alloc_mmap_handler() writes alloc->pages and alloc->buffer_size without holding that lock, and even writes them before the last bailout point. Unfortunately we can't take the alloc->mutex in the ->mmap() handler because mmap_sem can be taken while alloc->mutex is held. So instead, we have to locklessly check whether the binder_alloc has been fully initialized with binder_alloc_get_vma(), like in binder_alloc_new_buf_locked(). Fixes: 8ef4665aa129 ("android: binder: Add page usage in binder stats") Cc: stable@vger.kernel.org Signed-off-by: Jann Horn Acked-by: Christian Brauner Link: https://lore.kernel.org/r/20191018205631.248274-1-jannh@google.com Signed-off-by: Greg Kroah-Hartman --- drivers/android/binder_alloc.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -841,14 +841,20 @@ void binder_alloc_print_pages(struct seq int free = 0; mutex_lock(&alloc->mutex); - 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++; + /* + * 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++; + } } mutex_unlock(&alloc->mutex); seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);