Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4407033pxa; Mon, 10 Aug 2020 08:23:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxg2nlq8lR7weNCbD2x5B/zpUM+9DaNzsS418+28yJucbMC+sIqgdRgmZeBJCCIEtiuPQ9b X-Received: by 2002:aa7:d758:: with SMTP id a24mr14994265eds.379.1597072991010; Mon, 10 Aug 2020 08:23:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597072991; cv=none; d=google.com; s=arc-20160816; b=CgrP9iOWt3Np1wsMli1ohVznqLDb00BBj92RLM5Xa+rUaw37GvgvL17iqZEDueNbF0 SGgULy35hUF0EPxLO6YuUxX7hgUrjE9jRwSaAiaE/pdN/+ZS0p503anj6zZR6ks2wR/W 1DG6AdebYUzaAcH6ehKOi63NtLShzp1LoLFz7Hc1YjSvJse9eDFzz1SW4tRd9+xgc596 lGQu1LS+y7pvI+3dYKRlslA/CBPzT5x+MEMpEnD7BxDT6JPQDWl9bdh7RuNgoA7bah+x Jy/dfkotYaUBI6smgB8+1kr4liDBG1vtYdfTZ8b4yV+aHW6GewWPH6GvBmgDvEWvySpe SQeg== 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=Z+QmzipWONmMRZyzjO/BlijNfvHy/p9DMLCOM1ySmy0=; b=qCqPnb3n15mK6xXi5f9AgTwSA7uBFm72kqwqUUTu2CnF5sJnKfGJb2ZkQpiA9mffDA 5bo0lwqVP0tMa2cgYv6MkT1ITi+Pf+Lpl2am6jR3WCK2LsWfR0MBLSCc/IcxtR4gIkdd 7KnmFbDlof043f6axXdYoBKOhfwZNii1gWq8XtftLufZxSPixAaAxxlja/RCR5rbHva8 EL8xt7ZQAczYY7mV4K/54n06gd5FtIeY68DDLA3Kf9nlQlVm8nGFf04LGOVtJS+xQLRY 3yzGcNwfg3S+fUJ39UrWFCoHG1/jn4EXpJpNMzXoCLGw43hAlgkYPFCn55OBT2+PqjYf cWsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2C1lahIh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z12si10739742edx.530.2020.08.10.08.22.47; Mon, 10 Aug 2020 08:23:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2C1lahIh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728052AbgHJPUr (ORCPT + 99 others); Mon, 10 Aug 2020 11:20:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:51252 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728040AbgHJPUm (ORCPT ); Mon, 10 Aug 2020 11:20:42 -0400 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 598D92075F; Mon, 10 Aug 2020 15:20:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597072841; bh=t9H3WW3HSu9TxPPbh+BJneKjwG8jK5KJoQmUWWF9c+Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2C1lahIhXdb8KMuVh8YH4nEv2sq953DNSFVb1kI6wVPIkGRf5ufjeF+fJRpxc+Gd4 SBgEevB+ykudsLrwV3XSB98mOG91W6rJEkEqTErGUPmKm4YrE1ErcEcGP7BhkxcfS+ d/8LAQU4sNh6mnzbGCXRlMHwzxeEHMPPZwz7CQkA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Todd Kjos , Jann Horn , Martijn Coenen Subject: [PATCH 5.8 19/38] binder: Prevent context manager from incrementing ref 0 Date: Mon, 10 Aug 2020 17:19:09 +0200 Message-Id: <20200810151804.840050319@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200810151803.920113428@linuxfoundation.org> References: <20200810151803.920113428@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 4b836a1426cb0f1ef2a6e211d7e553221594f8fc upstream. Binder is designed such that a binder_proc never has references to itself. If this rule is violated, memory corruption can occur when a process sends a transaction to itself; see e.g. . There is a remaining edgecase through which such a transaction-to-self can still occur from the context of a task with BINDER_SET_CONTEXT_MGR access: - task A opens /dev/binder twice, creating binder_proc instances P1 and P2 - P1 becomes context manager - P2 calls ACQUIRE on the magic handle 0, allocating index 0 in its handle table - P1 dies (by closing the /dev/binder fd and waiting a bit) - P2 becomes context manager - P2 calls ACQUIRE on the magic handle 0, allocating index 1 in its handle table [this triggers a warning: "binder: 1974:1974 tried to acquire reference to desc 0, got 1 instead"] - task B opens /dev/binder once, creating binder_proc instance P3 - P3 calls P2 (via magic handle 0) with (void*)1 as argument (two-way transaction) - P2 receives the handle and uses it to call P3 (two-way transaction) - P3 calls P2 (via magic handle 0) (two-way transaction) - P2 calls P2 (via handle 1) (two-way transaction) And then, if P2 does *NOT* accept the incoming transaction work, but instead closes the binder fd, we get a crash. Solve it by preventing the context manager from using ACQUIRE on ref 0. There shouldn't be any legitimate reason for the context manager to do that. Additionally, print a warning if someone manages to find another way to trigger a transaction-to-self bug in the future. Cc: stable@vger.kernel.org Fixes: 457b9a6f09f0 ("Staging: android: add binder driver") Acked-by: Todd Kjos Signed-off-by: Jann Horn Reviewed-by: Martijn Coenen Link: https://lore.kernel.org/r/20200727120424.1627555-1-jannh@google.com Signed-off-by: Greg Kroah-Hartman --- drivers/android/binder.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -2982,6 +2982,12 @@ static void binder_transaction(struct bi goto err_dead_binder; } e->to_node = target_node->debug_id; + if (WARN_ON(proc == target_proc)) { + return_error = BR_FAILED_REPLY; + return_error_param = -EINVAL; + return_error_line = __LINE__; + goto err_invalid_target_handle; + } if (security_binder_transaction(proc->tsk, target_proc->tsk) < 0) { return_error = BR_FAILED_REPLY; @@ -3635,10 +3641,17 @@ static int binder_thread_write(struct bi struct binder_node *ctx_mgr_node; mutex_lock(&context->context_mgr_node_lock); ctx_mgr_node = context->binder_context_mgr_node; - if (ctx_mgr_node) + if (ctx_mgr_node) { + if (ctx_mgr_node->proc == proc) { + binder_user_error("%d:%d context manager tried to acquire desc 0\n", + proc->pid, thread->pid); + mutex_unlock(&context->context_mgr_node_lock); + return -EINVAL; + } ret = binder_inc_ref_for_node( proc, ctx_mgr_node, strong, NULL, &rdata); + } mutex_unlock(&context->context_mgr_node_lock); } if (ret)