Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4766356pxb; Tue, 5 Oct 2021 09:52:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxwIoj7tuyCRTKLe80OBI2OZDBpowYQm2XyPy0Y25FLYvPnF4cde0xmXBFJNLhhVechbpce X-Received: by 2002:a17:906:2294:: with SMTP id p20mr17555937eja.224.1633452760802; Tue, 05 Oct 2021 09:52:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633452760; cv=none; d=google.com; s=arc-20160816; b=ttL35BiOvRnxnykKWMaIaT4T/ojgD/NhZ9YPv/PNmv/F3BqpDRSfBKj8LS0FgrY1Ry wj9f+kkPeF1FWNEXU/VZoJf3k65yOP3O2aSbyti8pnVntFOT1saeVs3RgCL5+UQ5DN3F w6fpkh9KIDUUWsfUoKblWfw+dV8zrVjSNFI5xGRr6qJJJmwLtrO/7Huj6WEKSpeozVhn eXmOYygb9jK0tFaSUXkCgSsMbDxPO5RudXFurxiXmDRrg9oIk2pB25OwGkYh1fnI0rXm d/LPZdBv9KWn+dXUT7ItjrlMhcL3NF0ETDOo4dzDl1IjMNT4Eq0watu35GqrdlOXx8JZ QTlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=qjakKZPtbZWA5oTWQgTBXy+CXYr6mwaPBB/ZlgtkOp0=; b=jEyfnF+XiIsbEERhlHut4UEcgPGKq5la1i+Q9AiRPFMHgAO6Jl8kBtAJXngHa+Yvuk AK4XZxVkUBly+9Qcvevgcl1PWQc7qjJFeuB+uDaXhHNf5kN3lzE5MvULhEgCvBbTDRcM yED0FWIOw94Z4VXdE4lyroF4Vpf0MAgAAtiW5NPz5luIEsparNWwrfT1tVehcTdJ4oO1 eiGJePBYGj9/hcK3MhR5df/fA8dMw2RWbn+SqzzR2+4nLmZwtYe6UlNhpM8CxTo9369S GwBp6gST0oEZ3wDkGwEGYezjaP2f4kuCN39LHTnWM5j+PgW0wnsEKYd6F+f23Ph1YGFU SbxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=cO+svndx; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h4si27126574edt.426.2021.10.05.09.52.16; Tue, 05 Oct 2021 09:52:40 -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=@google.com header.s=20210112 header.b=cO+svndx; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235361AbhJEQvL (ORCPT + 99 others); Tue, 5 Oct 2021 12:51:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236337AbhJEQvK (ORCPT ); Tue, 5 Oct 2021 12:51:10 -0400 Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74B13C061755 for ; Tue, 5 Oct 2021 09:49:19 -0700 (PDT) Received: by mail-lf1-x12c.google.com with SMTP id m3so87616149lfu.2 for ; Tue, 05 Oct 2021 09:49:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qjakKZPtbZWA5oTWQgTBXy+CXYr6mwaPBB/ZlgtkOp0=; b=cO+svndxQ62I/bJgFPgZstCjGW3FddPDTeMaWLwXyTcw92L2tHgA1Zg/os0PWmWTxT LHPwAES1BxasUSzhwFICCkAx6hJwBuPVMjL+Iu3LgvUutigQfC4EkppgKmCRUBRYPnN9 dg7e1bUuq8Syj2ftQuQlk2Ym045RRJSJTh+g6eVbj+bvDGwz4pl4xyMru2bUyef2RUCh s291lAgPlI6gQ/JP1Va1JMWQoa0N25G+GydSJmEzA8xCSEYPKoX67fK4o6dy31hBUJfF q5AuAofDODmeqwouv//YEFoPwfed75wGKUSLecs/eY7fciE3EA7WlkT3F/io0dF+I6ij EDkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qjakKZPtbZWA5oTWQgTBXy+CXYr6mwaPBB/ZlgtkOp0=; b=7/b0oThGU72nT3MNT5RW441/q2ej8Bcn9ghbXZnm3TvSRtWBl1M1CtcGUcUStUGp3Z hBiC0LgKbAGdfuPxUuUxphSj8/bNyBpqFq2WUEbNUPrNZTE08tIm+s5bcR2il+yA+9hx dkUQ2fLPvtOrYu9K3TRWo5jGpcslrwDQL6tI03fKMr16yJWbRT8Tp+C7dwTVk8Z2BrOC B0qEDlW3s2eDpNkVP92Wlfr1/8NFcYuZ4lg8fnwzN1Riw7DZYhD3r3PpFVms+LKYsxa6 MCRe5MwjCwwKcElTPm3Do+aiAXYLdRJoLbRf31keLLmiOHJn6WYjHuS1C9jdEcsj5KUo RvLA== X-Gm-Message-State: AOAM530i6KuNdFOMxu5KYHsYVlA39keKO4xxkiTkg17DNNnUns1Innfz KVvVtRBFpK5/JoBgCKHrab95Ff3w7GjnuWVAyC70EQ== X-Received: by 2002:a05:6512:2290:: with SMTP id f16mr4526054lfu.668.1633452557160; Tue, 05 Oct 2021 09:49:17 -0700 (PDT) MIME-Version: 1.0 References: <20211001175521.3853257-1-tkjos@google.com> <6bd2de29-b46a-1d24-4c73-9e4e0f3f6eea@schaufler-ca.com> In-Reply-To: From: Todd Kjos Date: Tue, 5 Oct 2021 09:49:05 -0700 Message-ID: Subject: Re: [PATCH v2] binder: use cred instead of task for selinux checks To: Stephen Smalley Cc: Jann Horn , Casey Schaufler , Greg Kroah-Hartman , arve@android.com, tkjos@android.com, maco@android.com, christian@brauner.io, James Morris , "Serge E. Hallyn" , Paul Moore , Eric Paris , Kees Cook , Jeffrey Vander Stoep , Mimi Zohar , LSM List , SElinux list , devel@driverdev.osuosl.org, linux-kernel , "Joel Fernandes (Google)" , "Cc: Android Kernel" , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 5, 2021 at 8:21 AM Stephen Smalley wrote: > > On Mon, Oct 4, 2021 at 8:27 PM Jann Horn wrote: > > > > On Tue, Oct 5, 2021 at 1:38 AM Casey Schaufler wrote: > > > On 10/4/2021 3:28 PM, Jann Horn wrote: > > > > On Mon, Oct 4, 2021 at 6:19 PM Casey Schaufler wrote: > > > >> On 10/1/2021 3:58 PM, Jann Horn wrote: > > > >>> On Fri, Oct 1, 2021 at 10:10 PM Casey Schaufler wrote: > > > >>>> On 10/1/2021 12:50 PM, Jann Horn wrote: > > > >>>>> On Fri, Oct 1, 2021 at 9:36 PM Jann Horn wrote: > > > >>>>>> On Fri, Oct 1, 2021 at 8:46 PM Casey Schaufler wrote: > > > >>>>>>> On 10/1/2021 10:55 AM, Todd Kjos wrote: > > > >>>>>>>> Save the struct cred associated with a binder process > > > >>>>>>>> at initial open to avoid potential race conditions > > > >>>>>>>> when converting to a security ID. > > > >>>>>>>> > > > >>>>>>>> Since binder was integrated with selinux, it has passed > > > >>>>>>>> 'struct task_struct' associated with the binder_proc > > > >>>>>>>> to represent the source and target of transactions. > > > >>>>>>>> The conversion of task to SID was then done in the hook > > > >>>>>>>> implementations. It turns out that there are race conditions > > > >>>>>>>> which can result in an incorrect security context being used. > > > >>>>>>> In the LSM stacking patch set I've been posting for a while > > > >>>>>>> (on version 29 now) I use information from the task structure > > > >>>>>>> to ensure that the security information passed via the binder > > > >>>>>>> interface is agreeable to both sides. Passing the cred will > > > >>>>>>> make it impossible to do this check. The task information > > > >>>>>>> required is not appropriate to have in the cred. > > > >>>>>> Why not? Why can't you put the security identity of the task into the creds? > > > >>>>> Ah, I get it now, you're concerned about different processes wanting > > > >>>>> to see security contexts formatted differently (e.g. printing the > > > >>>>> SELinux label vs printing the AppArmor label), right? > > > >>>> That is correct. > > > >>>> > > > >>>>> But still, I don't think you can pull that information from the > > > >>>>> receiving task. Maybe the easiest solution would be to also store that > > > >>>>> in the creds? Or you'd have to manually grab that information when > > > >>>>> /dev/binder is opened. > > > >>>> I'm storing the information in the task security blob because that's > > > >>>> the appropriate scope. Today the LSM hook is given both task_struct's. > > > >>> Which is wrong, because you have no idea who the semantic "recipient > > > >>> task" is - any task that has a mapping of the binder fd can > > > >>> effectively receive transactions from it. > > > >>> > > > >>> (And the current "sender task" is also wrong, because binder looks at > > > >>> the task that opened the binder device, not the task currently > > > >>> performing the action.) > > > >> I'm confused. Are you saying that the existing binder code is > > > >> completely broken? Are you saying that neither "task" is correct? > > > > Yeah, basically > > > > > > Well, hot biscuits and gravy! > > > > > > > - but luckily the actual impact this has is limited by > > > > the transitions that SELinux permits. If domain1 has no way to > > > > transition to domain2, then it can't abuse this bug to pretend to be > > > > domain2. I do have a reproducer that lets Android's "shell" domain > > > > send a binder transaction that appears to come from "runas", but > > > > luckily "runas" has no interesting privileges with regards to binder, > > > > so that's not exploitable. > > > > > > You're counting on the peculiarities of the SELinux policy you're > > > assuming is used to mask the fact that the hook isn't really doing > > > what it is supposed to? Ouch. > > > > I'm not saying I like the current situation - I do think that this > > needs to change. I'm just saying it probably isn't *exploitable*, and > > exploitability often hinges on these little circumstantial details. > > > > > >> How does passing the creds from the wrong tasks "fix" the problem? > > > > This patch is not passing the creds from the "wrong" tasks at all. It > > > > relies on the basic idea that when a security context opens a > > > > resource, and then hands that resource to another context for > > > > read/write operations, then you can effectively treat this as a > > > > delegation of privileges from the original opener, and perform access > > > > checks against the credentials using which the resource was opened. > > > > > > OK. I can understand that without endorsing it. > > > > > > > In particular, we already have those semantics in the core kernel for > > > > ->read() and ->write() VFS operations - they are *not allowed* to look > > > > at the credentials of the caller, and if they want to make security > > > > checks, they have to instead check against file->f_cred, which are the > > > > credentials using which the file was originally opened. (Yes, some > > > > places still get that wrong.) Passing a file descriptor to another > > > > task is a delegation of access, and the other task can then call > > > > syscalls like read() / write() / mmap() on the file descriptor without > > > > needing to have any access to the underlying file. > > > > > > A mechanism sufficiently entrenched. > > > > It's not just "entrenched", it is a fundamental requirement for being > > able to use file descriptor passing with syscalls like write(). If > > task A gives a file descriptor to task B, then task B must be able to > > write() to that FD without having to worry that the FD actually refers > > to some sort of special file that interprets the written data as some > > type of command, or something like that, and that this leads to task B > > unknowingly passing through access checks. > > > > > > You can't really attribute binder transactions to specific tasks that > > > > are actually involved in the specific transaction, neither on the > > > > sending side nor on the receiving side, because binder is built around > > > > passing data through memory mappings. Memory mappings can be accessed > > > > by multiple tasks, and even a task that does not currently have it > > > > mapped could e.g. map it at a later time. And on top of that you have > > > > the problem that the receiving task might also go through privileged > > > > execve() transitions. > > > > > > OK. I'm curious now as to why the task_struct was being passed to the > > > hook in the first place. > > > > Probably because that's what most other LSM hooks looked like and the > > authors/reviewers of the patch didn't realize that this model doesn't > > really work for binder? FWIW, these hooks were added in commit > > 79af73079d75 ("Add security hooks to binder and implement the hooks > > for SELinux."). The commit message also just talks about "processes". > > Note that in the same code path (binder_transaction), sender_euid is > set from proc->tsk and security_ctx is based on proc->tsk. If we are > changing the hooks to operate on the opener cred, then presumably we > should be doing that for sender_euid and replace the > security_task_getsecid_obj() call with security_cred_getsecid()? Stephan, do you want that to be included in this patch? Or should I follow this up with another patch for the sender_euid case? > > NB Mandatory Access Control doesn't allow uncontrolled delegation, > hence typically checks against the subject credential either at > delegation/transfer or use or both. That's true in other places too, > e.g. file_permission, socket_sendmsg. > > -- > To unsubscribe from this group and stop receiving emails from it, send an email to kernel-team+unsubscribe@android.com. >