Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp511102pxj; Fri, 28 May 2021 08:55:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyb4GUf2PNRzSxOUVROD8SdPGilIFlez3KlzZxLVT1tN/8bv3Je9OsnwinyE9cYEdEZsac9 X-Received: by 2002:a02:a10f:: with SMTP id f15mr9257896jag.124.1622217300159; Fri, 28 May 2021 08:55:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622217300; cv=none; d=google.com; s=arc-20160816; b=yy52LXSdzbmiLSvBWUmFqBwcldKrbUhh5mIh4UdDNN5wXA5U7JwTFq/jVua1LwNG3e 3Zu40zm1QXdsPU0CMQt+iZ8HCe9hZkbx3DsffYI0+sjJ4gEuoGWL3CC9rh9vylJAMAIj lNQKUpKFfsj03g0WLYr7/uljgwZ4oYqQZW2jg4ZpMe8ZnhjjfEtb5H1Wbh+bDhcgQt8n FbteAf+88jT7jVO6xgyxGBz0BdPEcQyt0ptoRJ/l8ESb9FKPVXpYGIQUyzUFMVRAcpoU p37vsd3VyPlXhgfTHM3Sxkwnta/XQwvxJR6uq2QX/zhVLPs4dsCHpweYgZTt85mN05Tl MWhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=n+zZMHKIDtvyIuqsZX9YxNWH9Q0khwm3wL8EZyv+mXI=; b=07WuUaqd5I8nLbWmeLcf0iZM2YgRs00aFu7Dg7aDok9vrM56oPmSE7rJMwRMv0qGDx 6IAJqvqWoIXh440fPyScyQ4nyL6igV0gAu9NxcqCQENMb3ey+fJttNmbFLC9kEVPS/NS KVEBKb15HUWDonVpFA+i+GeSaE61pWH55dYl44nHzYzvXsFw8wmRtN0R1qQV4uUDtdim CVLnht4a26A9TKbOq8dZW19UcnNJ27IALUXRXThUAhK8v0rtt6eWNB6zIsCKmKvE3cgd R7KK7wjlOKfnJnv2JWOIf0kdw3b/3MU/FioQYOEO5tV5NfmbkjI+auMMI/z5yxWuIJBz xcWw== ARC-Authentication-Results: i=1; mx.google.com; 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 f6si6710703ila.51.2021.05.28.08.54.42; Fri, 28 May 2021 08:55:00 -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; 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 S236497AbhE1Opb (ORCPT + 99 others); Fri, 28 May 2021 10:45:31 -0400 Received: from www62.your-server.de ([213.133.104.62]:40950 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236309AbhE1OpX (ORCPT ); Fri, 28 May 2021 10:45:23 -0400 Received: from sslproxy06.your-server.de ([78.46.172.3]) by www62.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1lmdLp-0006Az-6G; Fri, 28 May 2021 16:20:37 +0200 Received: from [85.7.101.30] (helo=linux.home) by sslproxy06.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lmdLo-000B6x-Rs; Fri, 28 May 2021 16:20:36 +0200 Subject: Re: [PATCH v2] lockdown,selinux: avoid bogus SELinux lockdown permission checks To: Ondrej Mosnacek Cc: Paul Moore , Linux Security Module list , James Morris , Steven Rostedt , Ingo Molnar , Stephen Smalley , SElinux list , linuxppc-dev@lists.ozlabs.org, Linux FS Devel , bpf , network dev , Linux kernel mailing list , Casey Schaufler , Jiri Olsa , andrii.nakryiko@gmail.com References: <20210517092006.803332-1-omosnace@redhat.com> <01135120-8bf7-df2e-cff0-1d73f1f841c3@iogearbox.net> <4fee8c12-194f-3f85-e28b-f7f24ab03c91@iogearbox.net> From: Daniel Borkmann Message-ID: <17eaebd3-6389-8c80-38ed-dada9d087266@iogearbox.net> Date: Fri, 28 May 2021 16:20:36 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.2/26184/Fri May 28 13:05:50 2021) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/28/21 3:42 PM, Ondrej Mosnacek wrote: > (I'm off work today and plan to reply also to Paul's comments next > week, but for now let me at least share a couple quick thoughts on > Daniel's patch.) > > On Fri, May 28, 2021 at 11:56 AM Daniel Borkmann wrote: >> On 5/28/21 9:09 AM, Daniel Borkmann wrote: >>> On 5/28/21 3:37 AM, Paul Moore wrote: >>>> On Mon, May 17, 2021 at 5:22 AM Ondrej Mosnacek wrote: >>>>> >>>>> Commit 59438b46471a ("security,lockdown,selinux: implement SELinux >>>>> lockdown") added an implementation of the locked_down LSM hook to >>>>> SELinux, with the aim to restrict which domains are allowed to perform >>>>> operations that would breach lockdown. >>>>> >>>>> However, in several places the security_locked_down() hook is called in >>>>> situations where the current task isn't doing any action that would >>>>> directly breach lockdown, leading to SELinux checks that are basically >>>>> bogus. >>>>> >>>>> Since in most of these situations converting the callers such that >>>>> security_locked_down() is called in a context where the current task >>>>> would be meaningful for SELinux is impossible or very non-trivial (and >>>>> could lead to TOCTOU issues for the classic Lockdown LSM >>>>> implementation), fix this by modifying the hook to accept a struct cred >>>>> pointer as argument, where NULL will be interpreted as a request for a >>>>> "global", task-independent lockdown decision only. Then modify SELinux >>>>> to ignore calls with cred == NULL. >>>> >>>> I'm not overly excited about skipping the access check when cred is >>>> NULL. Based on the description and the little bit that I've dug into >>>> thus far it looks like using SECINITSID_KERNEL as the subject would be >>>> much more appropriate. *Something* (the kernel in most of the >>>> relevant cases it looks like) is requesting that a potentially >>>> sensitive disclosure be made, and ignoring it seems like the wrong >>>> thing to do. Leaving the access control intact also provides a nice >>>> avenue to audit these requests should users want to do that. >>> >>> I think the rationale/workaround for ignoring calls with cred == NULL (or the previous >>> patch with the unimplemented hook) from Ondrej was two-fold, at least speaking for his >>> seen tracing cases: >>> >>> i) The audit events that are triggered due to calls to security_locked_down() >>> can OOM kill a machine, see below details [0]. >>> >>> ii) It seems to be causing a deadlock via slow_avc_audit() -> audit_log_end() >>> when presumingly trying to wake up kauditd [1]. > > Actually, I wasn't aware of the deadlock... But calling an LSM hook > [that is backed by a SELinux access check] from within a BPF helper is > calling for all kinds of trouble, so I'm not surprised :) Fully agree, it's just waiting to blow up in unpredictable ways.. :/ >> Ondrej / Paul / Jiri: at least for the BPF tracing case specifically (I haven't looked >> at the rest but it's also kind of independent), the attached fix should address both >> reported issues, please take a look & test. > > Thanks, I like this solution, although there are a few gotchas: > > 1. This patch creates a slight "regression" in that if someone flips > the Lockdown LSM into lockdown mode on runtime, existing (already > loaded) BPF programs will still be able to call the > confidentiality-breaching helpers, while before the lockdown would > apply also to them. Personally, I don't think it's a big deal (and I > bet there are other existing cases where some handle kept from before > lockdown could leak data), but I wanted to mention it in case someone > thinks the opposite. Yes, right, though this is nothing new either in the sense that there are plenty of other cases with security_locked_down() that operate this way e.g. take the open_kcore() for /proc/kcore access or the module_sig_check() for mod signatures just to pick some random ones, same approach where the enforcement is happen at open/load time. > 2. IIUC. when a BPF program is rejected due to lockdown/SELinux, the > kernel will return -EINVAL to userspace (looking at > check_helper_call() in kernel/bpf/verifier.c; didn't have time to look > at other callers...). It would be nicer if the error code from the > security_locked_down() call would be passed through the call chain and > eventually returned to the caller. It should be relatively > straightforward to convert bpf_base_func_proto() to return a PTR_ERR() > instead of NULL on error, but it looks like this would result in quite > a big patch updating all the callers (and callers of callers, etc.) > with a not-so-small chance of missing some NULL check and introducing > a bug... I guess we could live with EINVAL-on-denied in stable kernels > and only have the error path refactoring in -next; I'm not sure... Right, it would return a verifier log entry with reporting to the user that the prog is attempting to use an unavailable/unknown helper function. We do have similar return NULL with bpf_capable() and perfmon_capable() checks. Potentially, we could do PTR_ERR() in future where we tell if it failed due to missing CAPs, due to lockdown or just due to helper not compiled in.. > 3. This is a bit of a shot-in-the-dark, but I suppose there might be > some BPF programs that would be able to do something useful also when > the read_kernel helpers return an error, yet the kernel will now > outright refuse to load them (when the lockdown hook returns nonzero). > I have no idea if such BPF programs realistically exist in practice, > but perhaps it would be worth returning some dummy > always-error-returning helper function instead of NULL from > bpf_base_func_proto() when security_locked_down() returns an error. > That would also resolve (2.), basically. (Then there is the question > of what error code to use (because Lockdown LSM uses -EPERM, while > SELinux -EACCESS), but I think always returning -EPERM from such stub > helpers would be a viable choice.) It would actually be harder to debug. Returning NULL at verification time, libbpf, for example, would have a chance to probe for this. See the feature_probes[] in libbpf's kernel_supports(), so it could provide a meaningful warning to the user that the tracing functionality is unavailable on the system. With returning an error from the helper, libbpf cannot check it.. theoretically, it could but significantly more cumbersome given it needs to attach the probe somewhere, trigger it, read out the helper result and pass it back to libbpf user space.. not really feasible. Overall, moving into func_proto and returning NULL is the much better approach (and in line with the CAP check enforcement). Thanks, Daniel