Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp747873ybp; Wed, 9 Oct 2019 03:41:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqzoDGQL//4vHlK4qsJTiXqZP17WMzMVptnwjiDOevTKEP9XRYS7D8u36ZJiLuYm2xcwTmqq X-Received: by 2002:a17:906:1c87:: with SMTP id g7mr2120882ejh.189.1570617697382; Wed, 09 Oct 2019 03:41:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570617697; cv=none; d=google.com; s=arc-20160816; b=hi8nGu0gDzPmzVCMgdKRIwAFvNE5smpjt9l+VluytWHyQdqIMf3YbQpbeYRZl+sGv/ Avyf+par6NX6ttNIttkTbg7EVDBsPGH/MmF3Ha0alNiw5ylPwm2GE9vEiJvMiP6SKOem W0+OnM8Vs0wzN/9EfG/myd1b9rgfnwFbzwT1u96S7c8PunCJLlHrPxeY80mdB318PPpF xU+r9ialhzzZeq4sPiOLhjf9CCbgwm7AH6u+h/B7QUmOT60Aho3p2sN7lHPTIKs25Al8 /gqRfvDDExUSxDMn7WK9fg1Vi5vGaT7VzrvztBjFGpIWD9TkNQ8uTXEcu3vBDcOWweF8 c7dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=NLsahbsmFHFMR8gJ/AZUXYD7ga1mqO96240abQpZgQA=; b=bb+oHXxXK+3wA2k8jZ1nrB+l/3jy2DU/P1S6yJ2vJYM/UtuoBZl2lVqGP3iYuhsvER AfvtrMOaOXP3iNAX2A9vUzdHvulaHUYE7aoQ2CPufn7Vnoi5j+/uIo9V3dG/PvvNPH7j dGtzDJTmDfk5By82/y+cJQxh9PpQCvZ4LNpf3K3vtjlOpRpDux7STJOfEoznCvK6MBcm Sh/Sst+T4kes4r1D9EWFgDY/V+W3Rc01cHtjQFtuhVojfCk2q5gDE9Vn7hu3I9dONhKT kqhdp40vcadjDe/WrxQO2sUC6DMv24rbpFx4JpyU2Vrdpb71sRexG8Fqj0s8m45+bwc0 0aOQ== ARC-Authentication-Results: i=1; mx.google.com; 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 u3si939124edy.48.2019.10.09.03.41.14; Wed, 09 Oct 2019 03:41:37 -0700 (PDT) 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; 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 S1730606AbfJIKkT (ORCPT + 99 others); Wed, 9 Oct 2019 06:40:19 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:57539 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726579AbfJIKkT (ORCPT ); Wed, 9 Oct 2019 06:40:19 -0400 Received: from [79.140.115.128] (helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iI9O9-0003Mv-7o; Wed, 09 Oct 2019 10:40:13 +0000 Date: Wed, 9 Oct 2019 12:40:12 +0200 From: Christian Brauner To: Joel Fernandes , Todd Kjos Cc: jannh@google.com, arve@android.com, christian@brauner.io, devel@driverdev.osuosl.org, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, maco@android.com, tkjos@google.com, Todd Kjos , Hridya Valsaraju Subject: Re: [PATCH] binder: prevent UAF read in print_binder_transaction_log_entry() Message-ID: <20191009104011.rzfdvq7otkkj533m@wittgenstein> References: <20191008130159.10161-1-christian.brauner@ubuntu.com> <20191008180516.GB143258@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20191008180516.GB143258@google.com> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 08, 2019 at 02:05:16PM -0400, Joel Fernandes wrote: > On Tue, Oct 08, 2019 at 03:01:59PM +0200, Christian Brauner wrote: > > When a binder transaction is initiated on a binder device coming from a > > binderfs instance, a pointer to the name of the binder device is stashed > > in the binder_transaction_log_entry's context_name member. Later on it > > is used to print the name in print_binder_transaction_log_entry(). By > > the time print_binder_transaction_log_entry() accesses context_name > > binderfs_evict_inode() might have already freed the associated memory > > thereby causing a UAF. Do the simple thing and prevent this by copying > > the name of the binder device instead of stashing a pointer to it. > > > > Reported-by: Jann Horn > > Fixes: 03e2e07e3814 ("binder: Make transaction_log available in binderfs") > > Link: https://lore.kernel.org/r/CAG48ez14Q0-F8LqsvcNbyR2o6gPW8SHXsm4u5jmD9MpsteM2Tw@mail.gmail.com > > Cc: Joel Fernandes > > Cc: Todd Kjos > > Cc: Hridya Valsaraju > > Signed-off-by: Christian Brauner > > --- > > drivers/android/binder.c | 4 +++- > > drivers/android/binder_internal.h | 2 +- > > 2 files changed, 4 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/android/binder.c b/drivers/android/binder.c > > index c0a491277aca..5b9ac2122e89 100644 > > --- a/drivers/android/binder.c > > +++ b/drivers/android/binder.c > > @@ -57,6 +57,7 @@ > > #include > > #include > > #include > > +#include > > #include > > #include > > #include > > @@ -66,6 +67,7 @@ > > #include > > > > #include > > +#include > > > > #include > > > > @@ -2876,7 +2878,7 @@ static void binder_transaction(struct binder_proc *proc, > > e->target_handle = tr->target.handle; > > e->data_size = tr->data_size; > > e->offsets_size = tr->offsets_size; > > - e->context_name = proc->context->name; > > + strscpy(e->context_name, proc->context->name, BINDERFS_MAX_NAME); > > Strictly speaking, proc-context->name can also be initialized for !BINDERFS > so the BINDERFS in the MAX_NAME macro is misleading. So probably there should > be a BINDER_MAX_NAME (and associated checks for whether non BINDERFS names > fit within the MAX. I know but I don't think it's worth special-casing non-binderfs devices. First, non-binderfs devices can only be created through a KCONFIG option determined at compile time. For stock Android the names are the same for all vendors afaik. Second, BINDERFS_MAX_NAME is set to the maximum path name component length that nearly all filesystems support (256 chars). If you exceed that then you run afoul of a bunch of other assumptions already and will cause trouble. Third, even if there is someone crazy and uses more than 256 chars for a non-binderfs device at KCONFIG time strscpy will do the right thing and truncate and you'd see a truncated binder device name. This doesn't seem to be a big deal for a debugfs interface. Fourth, the check for non-binderfs devices technically has nothing to do with this patch. This patch should really just do the minimal thing and fix the UAF. Which it does. Fifth, I already tried to push for validation of non-binderfs binder devices a while back when I wrote binderfs and was told that it's not needed. Hrydia tried the same and we decided the same thing. So you get to be the next person to send a patch. :) > > > if (reply) { > > > binder_inner_proc_lock(proc); > > diff --git a/drivers/android/binder_internal.h b/drivers/android/binder_internal.h > > index bd47f7f72075..ae991097d14d 100644 > > --- a/drivers/android/binder_internal.h > > +++ b/drivers/android/binder_internal.h > > @@ -130,7 +130,7 @@ struct binder_transaction_log_entry { > > int return_error_line; > > uint32_t return_error; > > uint32_t return_error_param; > > - const char *context_name; > > + char context_name[BINDERFS_MAX_NAME + 1]; > > Same comment here, context_name can be used for non-BINDERFS transactions as > well such as default binder devices. See above. > > One more thought, this can be made dependent on CONFIG_BINDERFS since regular > binder devices cannot be unregistered AFAICS and as Jann said, the problem is > BINDERFS specific. That way we avoid the memcpy for _every_ transaction. > These can be thundering when Android starts up. Unless Todd sees this as a real performance problem I'm weary to introduce additional checking and record a pointer for non-binderfs and a memcpy() for binderfs devices. :) > > (I secretly wish C strings could be refcounted to avoid exactly this issue, > that should not be hard to develop but I am not sure if it is worth it for > this problem :) - For one, it will avoid having to do the strcpy for _every_ > transaction). > > Other than these nits, please add my tag on whichever is the final solution: > > Reviewed-by: Joel Fernandes (Google) Thanks for the review, Joel. :) Christian