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 X-Spam-Level: X-Spam-Status: No, score=-11.3 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BFC1C433DF for ; Fri, 10 Jul 2020 10:27:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D9BC72076A for ; Fri, 10 Jul 2020 10:27:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NT6hn6KG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726942AbgGJK1h (ORCPT ); Fri, 10 Jul 2020 06:27:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726369AbgGJK1h (ORCPT ); Fri, 10 Jul 2020 06:27:37 -0400 Received: from mail-lj1-x244.google.com (mail-lj1-x244.google.com [IPv6:2a00:1450:4864:20::244]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8F85C08C5CE for ; Fri, 10 Jul 2020 03:27:36 -0700 (PDT) Received: by mail-lj1-x244.google.com with SMTP id e4so5861739ljn.4 for ; Fri, 10 Jul 2020 03:27:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=irAYaXay27AD2lSUWXkja3Lk/12VaS/aPk24zD+y1jA=; b=NT6hn6KGBeSQNYoJ4TSGOgqeZXB5x8xlaW3d75qpNeg6Y8S8odq5NytrN3uXcansjX R0wU3gTNmnz8FvK3Xg1hyZj/sgS0qA5XQ0TCT9LQ8HC/FvTbv4wOAlDmXUeoOB/eAD5G ComUdluQ8Bv9ZUBNdjeD4l9ho5eoD9jAlcwZkH4XNt/r42KU8uiqb/TkhmhBuUoodSGA JQTeh+H3b09tnx0BdzjWCrq02e0kDiKyc5EISTsdRP6DrpF2DT8ZXA6IuOwG+DG8LwmW 4Y/40sJnOCpYyM0snl68oDWdwCQPJa2lt6rbsuF2paDlX8AOYHdSuOwyfcM42WNNTaUN vAdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=irAYaXay27AD2lSUWXkja3Lk/12VaS/aPk24zD+y1jA=; b=LRc4m+1ljeLok+jl2RAqsDuQTcqIc+ilS6gaYqrfKwEo4eNBGwbMmtRn9aMLdb0upG mct97oNe5Yj4/AVLZFbVR+NeMEwQFJVIKN75x5IkBumZPWzK0G/a3oSunZ2qC5Es3H2A /QT2ajfILfOL0P9hSWLR+TsickFoBNFKaF7gelFojOBL2RXr2tBEhCHkFxVAnExVbpS3 hqUdGGORTIJWRHorZw8MdveVehd4X8/UHpHPyTA675s55Zsgaf6Cm65R9Y7yQ22lskpo wBj4f5AkVSL9Q8Qm7f31375G88RAsIVrCSZfw3NAjsgKuAmTd2/re/LQvCv28XgrsQSL C6xw== X-Gm-Message-State: AOAM532vKo0WJWyQ4kNvLhuIdD86xXpJxMENZ+T8bc1wWHvQvUejeed6 IimOpT6Xgb83x+tVAF9oVsfu2Kc/OIUxIE3Pioyhog== X-Google-Smtp-Source: ABdhPJyvjlEfU5AEHqHy1TpQ6u3w5UGbNJbg/vKjfQjSlvZ99aPTn9iHVT52rgNOqMSP3WBHmgzMbme69Qni0E5tUhk= X-Received: by 2002:a05:651c:21c:: with SMTP id y28mr28230968ljn.139.1594376854785; Fri, 10 Jul 2020 03:27:34 -0700 (PDT) MIME-Version: 1.0 References: <20200709223948.1051613-1-jannh@google.com> <20200710065418.GA1143105@kroah.com> In-Reply-To: <20200710065418.GA1143105@kroah.com> From: Jann Horn Date: Fri, 10 Jul 2020 12:27:08 +0200 Message-ID: Subject: Re: [PATCH resend] binder: Prevent context manager from incrementing ref 0 To: Greg Kroah-Hartman Cc: =?UTF-8?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , "open list:ANDROID DRIVERS" , Mattias Nissler , kernel list Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 10, 2020 at 8:54 AM Greg Kroah-Hartman wrote: > On Fri, Jul 10, 2020 at 12:39:48AM +0200, Jann Horn wrote: > > 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") > > Signed-off-by: Jann Horn > > Acked-by: Todd Kjos > > --- > > sending again because I forgot to CC LKML the first time... sorry about > > the spam. > > This spits out a bunch of warnings when built, how did it work on your > end? ... by creating the patch file before fixing the warnings. Sigh. I'll send the proper patch as v2... 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 X-Spam-Level: X-Spam-Status: No, score=-3.5 required=3.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB113C433DF for ; Fri, 10 Jul 2020 10:28:17 +0000 (UTC) Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9799E2076A for ; Fri, 10 Jul 2020 10:28:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="NT6hn6KG" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9799E2076A Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=driverdev-devel-bounces@linuxdriverproject.org Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id C850B873F1; Fri, 10 Jul 2020 10:27:40 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id d22lac-4c3jd; Fri, 10 Jul 2020 10:27:39 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by fraxinus.osuosl.org (Postfix) with ESMTP id 19BF987281; Fri, 10 Jul 2020 10:27:39 +0000 (UTC) Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by ash.osuosl.org (Postfix) with ESMTP id 885B71BF322 for ; Fri, 10 Jul 2020 10:27:38 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 5E13A20022 for ; Fri, 10 Jul 2020 10:27:38 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id y3pCI71oPzMI for ; Fri, 10 Jul 2020 10:27:37 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-lj1-f193.google.com (mail-lj1-f193.google.com [209.85.208.193]) by silver.osuosl.org (Postfix) with ESMTPS id 339C820017 for ; Fri, 10 Jul 2020 10:27:37 +0000 (UTC) Received: by mail-lj1-f193.google.com with SMTP id h22so5834393lji.9 for ; Fri, 10 Jul 2020 03:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=irAYaXay27AD2lSUWXkja3Lk/12VaS/aPk24zD+y1jA=; b=NT6hn6KGBeSQNYoJ4TSGOgqeZXB5x8xlaW3d75qpNeg6Y8S8odq5NytrN3uXcansjX R0wU3gTNmnz8FvK3Xg1hyZj/sgS0qA5XQ0TCT9LQ8HC/FvTbv4wOAlDmXUeoOB/eAD5G ComUdluQ8Bv9ZUBNdjeD4l9ho5eoD9jAlcwZkH4XNt/r42KU8uiqb/TkhmhBuUoodSGA JQTeh+H3b09tnx0BdzjWCrq02e0kDiKyc5EISTsdRP6DrpF2DT8ZXA6IuOwG+DG8LwmW 4Y/40sJnOCpYyM0snl68oDWdwCQPJa2lt6rbsuF2paDlX8AOYHdSuOwyfcM42WNNTaUN vAdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=irAYaXay27AD2lSUWXkja3Lk/12VaS/aPk24zD+y1jA=; b=QCeAqLP6sV4SyjxNNFjEMxKV2f7Pywe9uKWoUcFfwyrIrxZ1eOqJFMLSudPnITIBZI ropIe6klW7DcRVmmIVlqkIrSDIREXRiR1FNgzf+s+sB7q64g4reuQaCFdOyedK98uyFa UYH5HIp5KmULZhDCEDNHX4XQy/1MzDawA240u/YTHpYA9xb9+slS7nPxpXRD4z+dDXM+ VFboCyMQHjdxb7YQae3xcug/lEqQKbgCkVu2q1HSw/Cn3aw80Ml0h14J853oqH0klpz1 sFg1rE0NedIsVMKRo6borhLPT1xe9gIjipFMOIuXG6TOn7Pii7nq31vSadWExVNuJIWK +0AA== X-Gm-Message-State: AOAM5305PJEqPoT36zUMgEjEg7lEFLRGPB6VQBFeV873HyKZQC9x2Rft NXVXRYThF2ad7o+9USqjx/LCO4UW8mRsyLiNTokRIg== X-Google-Smtp-Source: ABdhPJyvjlEfU5AEHqHy1TpQ6u3w5UGbNJbg/vKjfQjSlvZ99aPTn9iHVT52rgNOqMSP3WBHmgzMbme69Qni0E5tUhk= X-Received: by 2002:a05:651c:21c:: with SMTP id y28mr28230968ljn.139.1594376854785; Fri, 10 Jul 2020 03:27:34 -0700 (PDT) MIME-Version: 1.0 References: <20200709223948.1051613-1-jannh@google.com> <20200710065418.GA1143105@kroah.com> In-Reply-To: <20200710065418.GA1143105@kroah.com> From: Jann Horn Date: Fri, 10 Jul 2020 12:27:08 +0200 Message-ID: Subject: Re: [PATCH resend] binder: Prevent context manager from incrementing ref 0 To: Greg Kroah-Hartman X-BeenThere: driverdev-devel@linuxdriverproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Driver Project Developer List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "open list:ANDROID DRIVERS" , Todd Kjos , Mattias Nissler , kernel list , =?UTF-8?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , Joel Fernandes , Martijn Coenen , Christian Brauner Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: driverdev-devel-bounces@linuxdriverproject.org Sender: "devel" On Fri, Jul 10, 2020 at 8:54 AM Greg Kroah-Hartman wrote: > On Fri, Jul 10, 2020 at 12:39:48AM +0200, Jann Horn wrote: > > 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") > > Signed-off-by: Jann Horn > > Acked-by: Todd Kjos > > --- > > sending again because I forgot to CC LKML the first time... sorry about > > the spam. > > This spits out a bunch of warnings when built, how did it work on your > end? ... by creating the patch file before fixing the warnings. Sigh. I'll send the proper patch as v2... _______________________________________________ devel mailing list devel@linuxdriverproject.org http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel