Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1135391pxb; Wed, 15 Sep 2021 23:59:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxzwzipI1zZDcfaoQkFBq94svUIbw57cQYkuqZPUadYfUbEOTMUcEq68l5kOPGUyxisSD0A X-Received: by 2002:a50:becb:: with SMTP id e11mr3906747edk.161.1631775544251; Wed, 15 Sep 2021 23:59:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631775544; cv=none; d=google.com; s=arc-20160816; b=wFVSHICp6FNNXJvdspKl6Rw2N329wZo0hPUQCQ/KTSpemB+qaq5qmoBrHCaq2nA/jE sMy9h9x4pQxqa2NSjlyvr56UdaKgGIbbg7EMVQCv0HK/cu+N8/xW46B1XowboWYwNHUa syn85BvfuM/hPtxWANZrjnfmHV7Kr+sqSu/+Yz7Mrlc2aVrfmCWQWccp6cF0hsGIx97B 1uRgJJNnZNBY7j0861YfOiIyUtrG9YFLeMzp9riT3uF6WFhwEgwcIXzvNPE686kCA721 TYohOe6AWx8FvIX+ixYnojbSNPxe/ZaPi1LTUYKm8/lhjCPNxNbU53G/UpIyyDvl89W3 6TbQ== 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=8RB13z4WWFvpQhPAtaErfMzrxHDfvjm55tbgAkFGFds=; b=EEtjx8uDoiQhUEIRWg6RxQmxR0I7Pl+9mRbZZZP6s8SgmCq3qA03BK4XfSKqhwyini JGVqYfIQqTpDM7sr5Zzd0EV1diOT1axIUTfsz2KgkH3WGkq85xZW9dPSIev9zMo7gt9z 7yIw4sIguEiHOUaLJucLkdIWGRLcFqupsc1+uE6aMc9AbKUxA6+GzwBSe9zIav/3lAgZ R3Dkfnp1rMAPCXcmgunuHjii8xf6fsSGlVlarF7iwN8+4wXVJOEkrd95BeWJna6b6zu6 FQyoDCDD6qoNAZiJ/TGVqYMGSP1BovWuoQ6dR5u46+y5W7BzWYNrHAfTgSz1R8qGS0jF e1aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZBAJKGHe; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ak17si2888907ejc.44.2021.09.15.23.58.40; Wed, 15 Sep 2021 23:59:04 -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=@redhat.com header.s=mimecast20190719 header.b=ZBAJKGHe; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234640AbhIPG6j (ORCPT + 99 others); Thu, 16 Sep 2021 02:58:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:39436 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232254AbhIPG6g (ORCPT ); Thu, 16 Sep 2021 02:58:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631775436; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=8RB13z4WWFvpQhPAtaErfMzrxHDfvjm55tbgAkFGFds=; b=ZBAJKGHeTQAAls0kn8k6LPMvXMKAHXMry7i+6tnxK7bO8TShzF/vDBg3Pro5mnPDaKfoso 2P/voGX3guUEqajPOHXVAaEOZpiXkPe6KJpAyuZy7iVISrGu0cKMJrIdKQA6fUsZz7PZny hcW8Cyp1cns8fuUiUG7QHIfUf9e1W90= Received: from mail-yb1-f200.google.com (mail-yb1-f200.google.com [209.85.219.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-405-ZHX6SxgnPaGMcXbQ-iJuLQ-1; Thu, 16 Sep 2021 02:57:15 -0400 X-MC-Unique: ZHX6SxgnPaGMcXbQ-iJuLQ-1 Received: by mail-yb1-f200.google.com with SMTP id a23-20020a25ae17000000b005ad73346312so11276143ybj.18 for ; Wed, 15 Sep 2021 23:57:15 -0700 (PDT) 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=8RB13z4WWFvpQhPAtaErfMzrxHDfvjm55tbgAkFGFds=; b=4PUyuqozwo7O+F1OHtk7xK/jkJcmiIW/KjboyEUoobUvlixBL/RMJz57zOoAxw7NLR taplo6aEsc61jtvOmZXLMi2x1/OV13Fo/S32fPsFHiEjIP8QOvHnVmeyRcyBnvm350ro p8bwe1//fm/DgWQ0UvKpK7YBB0+0O9kzeGpIDoGWx4NEPMmyF7NtcGNrgrckH3g018bq 4cnDDMryiT4emYA+H13FSwv9C06dqsNVknunLTGcSwAKAGTwwCJvueLpm/lVDe+GTzuw d9rQVR5qSrsY/q9I1azu+eb2rQU0FWXjrn9p71gCMex7L3YwU0BYe+5uORDjNNxCPkss B8NQ== X-Gm-Message-State: AOAM533bmqSxMDY7ViQ+yEU4d7KPjxI4zx2dAfyehvuFT/djfe9FOs8e bU8bZXXohjhyrNK+UQhjIHBrxhTOq2zqVBTIiczQf+qYXaa27V8gluuXSx24AO+FRXu7y3qJNL4 jXE4qu0Ta8qgMH+ZN+sWlI5P5pYJ7dMBSIN7oKPXU X-Received: by 2002:a25:bb8b:: with SMTP id y11mr5374072ybg.384.1631775434453; Wed, 15 Sep 2021 23:57:14 -0700 (PDT) X-Received: by 2002:a25:bb8b:: with SMTP id y11mr5374044ybg.384.1631775434181; Wed, 15 Sep 2021 23:57:14 -0700 (PDT) MIME-Version: 1.0 References: <20210913140229.24797-1-omosnace@redhat.com> In-Reply-To: From: Ondrej Mosnacek Date: Thu, 16 Sep 2021 08:57:02 +0200 Message-ID: Subject: Re: [PATCH v4] lockdown,selinux: fix wrong subject in some SELinux lockdown checks To: Paul Moore Cc: Linux Security Module list , James Morris , Steven Rostedt , Ingo Molnar , Steffen Klassert , Herbert Xu , "David S . Miller" , Stephen Smalley , SElinux list , linuxppc-dev , X86 ML , Linux ACPI , linux-cxl@vger.kernel.org, linux-efi , Linux FS Devel , Linux PCI , Linux-pm mailing list , linux-serial@vger.kernel.org, bpf , network dev , Kexec Mailing List , Linux kernel mailing list , Casey Schaufler , Dan Williams Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 16, 2021 at 4:59 AM Paul Moore wrote: > On Mon, Sep 13, 2021 at 5:05 PM Paul Moore wrote: > > > > On Mon, Sep 13, 2021 at 10:02 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. > > > > > > To fix this, add an explicit struct cred pointer argument to > > > security_lockdown() and define NULL as a special value to pass instead > > > of current_cred() in such situations. LSMs that take the subject > > > credentials into account can then fall back to some default or ignore > > > such calls altogether. In the SELinux lockdown hook implementation, use > > > SECINITSID_KERNEL in case the cred argument is NULL. > > > > > > Most of the callers are updated to pass current_cred() as the cred > > > pointer, thus maintaining the same behavior. The following callers are > > > modified to pass NULL as the cred pointer instead: > > > 1. arch/powerpc/xmon/xmon.c > > > Seems to be some interactive debugging facility. It appears that > > > the lockdown hook is called from interrupt context here, so it > > > should be more appropriate to request a global lockdown decision. > > > 2. fs/tracefs/inode.c:tracefs_create_file() > > > Here the call is used to prevent creating new tracefs entries when > > > the kernel is locked down. Assumes that locking down is one-way - > > > i.e. if the hook returns non-zero once, it will never return zero > > > again, thus no point in creating these files. Also, the hook is > > > often called by a module's init function when it is loaded by > > > userspace, where it doesn't make much sense to do a check against > > > the current task's creds, since the task itself doesn't actually > > > use the tracing functionality (i.e. doesn't breach lockdown), just > > > indirectly makes some new tracepoints available to whoever is > > > authorized to use them. > > > 3. net/xfrm/xfrm_user.c:copy_to_user_*() > > > Here a cryptographic secret is redacted based on the value returned > > > from the hook. There are two possible actions that may lead here: > > > a) A netlink message XFRM_MSG_GETSA with NLM_F_DUMP set - here the > > > task context is relevant, since the dumped data is sent back to > > > the current task. > > > b) When adding/deleting/updating an SA via XFRM_MSG_xxxSA, the > > > dumped SA is broadcasted to tasks subscribed to XFRM events - > > > here the current task context is not relevant as it doesn't > > > represent the tasks that could potentially see the secret. > > > It doesn't seem worth it to try to keep using the current task's > > > context in the a) case, since the eventual data leak can be > > > circumvented anyway via b), plus there is no way for the task to > > > indicate that it doesn't care about the actual key value, so the > > > check could generate a lot of "false alert" denials with SELinux. > > > Thus, let's pass NULL instead of current_cred() here faute de > > > mieux. > > > > > > Improvements-suggested-by: Casey Schaufler > > > Improvements-suggested-by: Paul Moore > > > Fixes: 59438b46471a ("security,lockdown,selinux: implement SELinux lockdown") > > > Acked-by: Dan Williams [cxl] > > > Acked-by: Steffen Klassert [xfrm] > > > Signed-off-by: Ondrej Mosnacek > > > --- > > > > > > v4: > > > - rebase on top of TODO > > > - fix rebase conflicts: > > > * drivers/cxl/pci.c > > > - trivial: the lockdown reason was corrected in mainline > > > * kernel/bpf/helpers.c, kernel/trace/bpf_trace.c > > > - trivial: LOCKDOWN_BPF_READ was renamed to LOCKDOWN_BPF_READ_KERNEL > > > in mainline > > > * kernel/power/hibernate.c > > > - trivial: !secretmem_active() was added to the condition in > > > hibernation_available() > > > - cover new security_locked_down() call in kernel/bpf/helpers.c > > > (LOCKDOWN_BPF_WRITE_USER in BPF_FUNC_probe_write_user case) > > > > > > v3: https://lore.kernel.org/lkml/20210616085118.1141101-1-omosnace@redhat.com/ > > > - add the cred argument to security_locked_down() and adapt all callers > > > - keep using current_cred() in BPF, as the hook calls have been shifted > > > to program load time (commit ff40e51043af ("bpf, lockdown, audit: Fix > > > buggy SELinux lockdown permission checks")) > > > - in SELinux, don't ignore hook calls where cred == NULL, but use > > > SECINITSID_KERNEL as the subject instead > > > - update explanations in the commit message > > > > > > v2: https://lore.kernel.org/lkml/20210517092006.803332-1-omosnace@redhat.com/ > > > - change to a single hook based on suggestions by Casey Schaufler > > > > > > v1: https://lore.kernel.org/lkml/20210507114048.138933-1-omosnace@redhat.com/ > > > > The changes between v3 and v4 all seem sane to me, but I'm going to > > let this sit for a few days in hopes that we can collect a few more > > Reviewed-bys and ACKs. If I don't see any objections I'll merge it > > mid-week(ish) into selinux/stable-5.15 and plan on sending it to Linus > > after it goes through a build/test cycle. > > Time's up, I just merged this into selinux/stable-5.15 and I'll send > this to Linus once it passes testing. Thanks! -- Ondrej Mosnacek Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.