Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4076840pxb; Mon, 4 Oct 2021 16:57:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyAeCL1B0NEpo0mx+81UgbmxfCenv76e0RF4TnphPL6HGB2P2VWbaEbILpHddAydeJiJo6C X-Received: by 2002:a65:62d1:: with SMTP id m17mr13188371pgv.370.1633391829014; Mon, 04 Oct 2021 16:57:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633391829; cv=none; d=google.com; s=arc-20160816; b=PTqPig3NazuG6IOG2nQpE/t4lqm6KDcD+Va06S1HQqTuIUom7vIR81VRjp07/dxkNC Ssbl4n515GaD7k1HeTqS3vEqU5zfSMyg3c3FDKt2ycPnID7jeqyVCSaleikDBEs+aHLi fNzc6XPYoKtzp/Hq+iowjSW/KDJCH9/o3v0oTAan5MuBa8PP0+0B1Fdxgt/h7KkX2Q7a zX31aJwa9dmryjjRDWQGiP4e0h4FvczSlQF17k7ncqXD1LYDnMbmrK9r7Ajg8KRhkTmZ F0tkY7iCjGwSNXSr2yRqDMmCKYdBiAt37douzKwCXM90/PMNQdJNklcmxglpxcidghRC 2ycw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=nZeXrjzRtK3UU5hiJ+Ec3aHGmGIKwTyAOQoux0JGTGQ=; b=FDGxTFfpL4vNNULJmiMl+/SESnkIcsoK05vL4CyOYINB6hsTUrdsKsFJet3U0dTi/2 PL9sd+Kmwr791wwvVNjhNcRQ47GdqYu5DSEGDlmUvYDEdtdL7kiU6XsKzPOYsEl68qsL +DhDv5USpbF92r9FM8IGTwd4N8fCHLvxgG9u3AER0Q4kfCb09cAZVCbX0SKvAGPuKyio akcvjDNGxC+gTKbDE9LB54p+95/juAtaXSdoPRzxmyacojVHnOscoGOPjaktxBgOD9FD 1t+lrIoGdtpE5YggWRJ/SK679XLSVMyfacveonfwOthIi/Xq2P3ABk/3H6Q6mhQGd+Cb Bg5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@yahoo.com header.s=s2048 header.b=mDS0KZFI; 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 m16si30117520pgu.423.2021.10.04.16.56.56; Mon, 04 Oct 2021 16:57:09 -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=@yahoo.com header.s=s2048 header.b=mDS0KZFI; 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 S235190AbhJDXjx (ORCPT + 99 others); Mon, 4 Oct 2021 19:39:53 -0400 Received: from sonic307-16.consmr.mail.ne1.yahoo.com ([66.163.190.39]:34458 "EHLO sonic307-16.consmr.mail.ne1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234888AbhJDXjx (ORCPT ); Mon, 4 Oct 2021 19:39:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1633390683; bh=nZeXrjzRtK3UU5hiJ+Ec3aHGmGIKwTyAOQoux0JGTGQ=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From:Subject:Reply-To; b=mDS0KZFIB2ewEkW8nmIKOIeqUPqAcqG+O3eiUw8pYW/YjYfenUW9/OZVmAHRzyS1CLVia86wgqm0ejEePkeDrjvvl7dQwbINnbnyzJECEKXO9KNCqcPnF8ju28VFncZ8aIgCy9PXcgs3vXRFrMSYoGGeBzzNL32CW/qHKiDESbDvyIp4Ed4oMkGjX9nojIWguulUMvgzJ1GX+WAbVwSIAPZNFyPrxvmMpJHppht2jt/FdApWsfbPkvBkr2b+tFik0zLTpZKH1EKjjzoWnPJoFgnb+lfpq2HeRrKRLfa7t+5mo5MPj6lgnHPz+iYI5yjUSM9oiN6d1Mesp8sjEHY17Q== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1633390683; bh=0oa3TICGV+ypV8PZK9n/khhIyB2XNCAUnVDhvOLy/rW=; h=X-Sonic-MF:Subject:To:From:Date:From:Subject; b=lg5FdxIVN3+BYNu0dF6mO9GlV/xaf9wsOzASLQKoaKQx6rtQydaeTfnK4fWv5GnojdeuTRjIdo4K3i9JsNygpka45aHx5HGbzg5W4t076Z/CGZMt6JIFqGKFSKa5QFI+mzYr1QIv4/hiQxdx5kxvX18td5C1NKOjbdipqfl/rg8RdvVQYgPD6GO1PNR0g8D6tewOKOExL25B5SxupNGlU+t08f81JZpLqvyJpx88X0n3xIvZeQLAlz2qqKLt4O4ZSaF4I0e+tn2GunB9yvaZ2biRiEY4H9qyjMnEdIJUQI3pwcfan6fki5cwIdrOo1KvehvI1Tmee/btn6+SUiqq2g== X-YMail-OSG: xAyAB6MVM1kQjGFYeXYrFp9Y_h5jTAjOFz_uCnegniJoAQCswNBymxULtyBZIpZ eDpIzi7i9v3wxt_N9kIJMIzjdL2m09QFvlEs_ITUXH6LPqXKWP9SmgpE218xzYzeUdak65M.R6DB se5OGlDWh0Tb6KTA41J7NUzZcJXlieHLCwZ0t2lE8jFSspkZ26V4tpicv.bbb36jgTiE1UmXFJMd fTy7GzZvNcgRPduoO8aiRoHajCfhRxvYrqJuNSG_Do2TIlAYX03w4kDOJEY2RfUPG8BtojPJSa3g 0NKqXwZAEmy4jxSgOgAVaNfVx9kWL8Ka.u7cf83xBQrnzlgSRBPWFOoSOYf_WrOK_8_a2TJsgtq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anhYwGQeyhoKS s9R4iTRahPqKWdR9IrB_k6o.WBUJB1v5sIbU7apYbBTetVaVHmt5HWoFJ5nLkcOx54gVHli12FN_ noEZLCSqf1_d.ML8nlESiuxOj1W372r8kGybvLY44XkYlY7hG4N07PMsZLha8bvSLkc98YfbjmOF ZQj5n1kcGR9seZz9MeacX54_jbmSKSPElAcDE1C1Lwb9vL1C0VH28tjII3Xsy5K7FqHAMsJmbFYi gHCtXolDXrqL51PVmIy2Ven7rgSYzuWZ_mTvWmpT5jJO9pwkYLgfLCNHnqWD1l5DqMbqGQAQbPeD Dl2vOypFRITj9EBgJxSY.8Uulel14QUjBDPoJi_vld1_iycH9SgXURyVCCVyKotbPb.T7p7D4n22 5KYAOelaqhPPFOXc5l1cevf.yJVBEVxK5hzEub7F8c7NbgcnTTEldpdVGpsXDVwWYjuz2WblNaBh suDgLt3b5HzB6R0s8NRnXfKUwOo1ibojpkatpWwEVSsa7rtJRhujYkVZnDCLCdmaE4scNaojyKxm _zVM88UKcFx1_YKy7lRgOtcgYAkRjW9E3Qd1sVrOQwo21JgrbuSoggwpAR1u8aIy7cVSUSHL9SUj WVkUV4JS6yfNfBPAZNGE6xWHhAbkEdb5a5C3pEqD.tirAD4YPNFx7LXPftpF9NtMVngRiOOXySc7 YWaJTrGOmltPMOZAtLY_DEGInXaBHX3dt.XqcD3jXdC79meF3QH04yfgjiwFkAdEt5_hgKHrHZ3Y YQEdu5a3it9_WQ0NsAZU.2sAah928oyAODcYQmoMCEf5VnWyA8iKphQyFfnAFLqD3lZ8o_WLx2_C hXaGIPoakqlz_IbicejT8s_8vUbiMT6VlaoUN6sAI2JiEg7H5Izfre_xDZhwB3eNIlIAIfw8KTpR p0l.gzD0- X-Sonic-MF: Received: from sonic.gate.mail.ne1.yahoo.com by sonic307.consmr.mail.ne1.yahoo.com with HTTP; Mon, 4 Oct 2021 23:38:03 +0000 Received: by kubenode513.mail-prod1.omega.ne1.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID 6d7bcf93567d1f2ff56e26bd47a6512e; Mon, 04 Oct 2021 23:37:59 +0000 (UTC) Subject: Re: [PATCH v2] binder: use cred instead of task for selinux checks To: Jann Horn Cc: Todd Kjos , gregkh@linuxfoundation.org, arve@android.com, tkjos@android.com, maco@android.com, christian@brauner.io, jmorris@namei.org, serge@hallyn.com, paul@paul-moore.com, stephen.smalley.work@gmail.com, eparis@parisplace.org, keescook@chromium.org, jeffv@google.com, zohar@linux.ibm.com, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, joel@joelfernandes.org, kernel-team@android.com, stable@vger.kernel.org, Casey Schaufler References: <20211001175521.3853257-1-tkjos@google.com> From: Casey Schaufler Message-ID: <6bd2de29-b46a-1d24-4c73-9e4e0f3f6eea@schaufler-ca.com> Date: Mon, 4 Oct 2021 16:37:58 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Mailer: WebService/1.1.19076 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.yahoo Content-Length: 6605 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 wantin= g >>>>> 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 t= hat >>>>> 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. >> 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. > 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. And about where you are getting the cred from if not a task. >>>> It's easy to compare to make sure the tasks are compatible. >>> It would be, if you actually had a pair of tasks that accurately >>> represent the sender and the recipient. >>> >>>> Adding the >>>> information to the cred would be yet another case where the scope of= >>>> security information is wrong. >>> Can you elaborate on why you think that? >> The information identifies how the task is going to display >> the security "context". It isn't used in access checks. > But it is data that AFAICS needs to be preserved in the same places > where the creds need to be preserved, and it is also related to > security labels, so isn't "struct cred" a logical place to stuff it > anyway? I am probably the only person on the planet who dislikes shared creds. One of the things that made me happiest when I switched from UNIX development to Linux was that it didn't have shared creds and all the associated management. Oh well. Yes, it could go in the cred. But that raises another question. Where are the creds coming from? Is it even rational to make access decisions based on them? You've explained how SELinux ends up with an Uncle Bob, but that's doesn't leave me confident that another security module would be able to come up with something sensible.=20 At this point I'm really looking for something that I can put in the change log explaining why creds work and task_structs don't.