Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2273852ybt; Sun, 21 Jun 2020 14:56:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyHNhKNeN9/mozNxtO5IXOWqxXBHVo0tpP7WE2kFOIHB+UTXFXCETuz3hySJcEI0Y+P9JG3 X-Received: by 2002:a17:906:7813:: with SMTP id u19mr9391344ejm.471.1592776613902; Sun, 21 Jun 2020 14:56:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592776613; cv=none; d=google.com; s=arc-20160816; b=k2GhqYsxtDSuffaf12CROwExHZ4MeuXRp5+pvO75QbWUVgB1GhYKCjrzdVOHy6Z6nw beIYUAtBwUljvK9f5lhK6ZUkduHzqQwpKhwOb8Y7FRbleTsNSyfJGvYOR0j+irDxpDN6 pdHB2cd/owG6hI/LuJN17tkniEiomWzblWpYngnBVNuE/eEggMTtBYCbPIlU5eqiD046 kHvRC0i8mcxxPu0J8Sp8IscZHjFgxFWxZ7ssqmFi4fL1jbTAXuCK8bc1itbey4hCKVZT pwfUr23dnO9bZNPMKSkMJ/9XCAwZ5A/YNbpnjD6Qm6xTJkBHnb0D07lB3ozpckqiAi1L VHEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=ClfrnpZFSOZxRBbJuLVUEpU0s3ZL/yim5HOOV5Ogv1E=; b=C4EBwHW6UiFexW5OR9n3CcFh2xtJkXgt9YW9Pum84T/hQbWkrjZqVXR0aswKThqGmO rZ9mWhPXMoP0Kp4P5pbLjY/m8ib7k8m4OIePHzODzSwRDQRpSVMTk0OtGy9w6AsDZjCg pdsvfy9IdxEfGzpzHpvc+m8anrjtuJ/DhIOXmdB+uMVxaS+AS51QzqJgMeu1Umb5pTO/ yiz6ovRlADIURJOUQMP/DrUd5YtAUKOpnMDaN/KIODyCa9uD+o6WIvuLzKnbRdiX7hTo WdP4q5W6ioOHYLTD25SwghUgy11M6dbQWI1YdAXXZryrqx76XS3Es5n6NsXKGKJjZU4W xG6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UmqmDfcG; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b30si1937631edj.204.2020.06.21.14.56.29; Sun, 21 Jun 2020 14:56:53 -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=@chromium.org header.s=google header.b=UmqmDfcG; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726424AbgFUVyt (ORCPT + 99 others); Sun, 21 Jun 2020 17:54:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726279AbgFUVys (ORCPT ); Sun, 21 Jun 2020 17:54:48 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFF0CC061795 for ; Sun, 21 Jun 2020 14:54:47 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id y20so13936868wmi.2 for ; Sun, 21 Jun 2020 14:54:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ClfrnpZFSOZxRBbJuLVUEpU0s3ZL/yim5HOOV5Ogv1E=; b=UmqmDfcGFPh0Va7QuZnr3DCpZHHCoGEUeNMvxE49O43zZI4ukzRSqoeZrNg1fNozX5 KdlJ8y9VbJysFjkWTnhLtGwdSVlO36dC1xFYD6+RdImYoj+1Qxt0KAq93hxwXLzWrWCJ L1WmyTunnn1F2IwLdNdDFJ1EuD1aioWwa3N84= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ClfrnpZFSOZxRBbJuLVUEpU0s3ZL/yim5HOOV5Ogv1E=; b=N9ZNBWQg6mbKAuwwJFDCS9wr6fNN9VeTXMHjtFSDywBVjSwAJsULS4rKeJ0mR5E56L s4bnIDkYJsHfeRn7zpuDezk/fhHr3h/jyzX7hOtyU0NJeNOsgg1rfBICtJNtFk0omGzd w3L0tJ1rFwKsdQ4MkbZrcdpQsqMABEqfimA8ryHyK+DFTvY8vr1PXb9N8vyVOssCEFKm 3WJnPNOW01yTylASKnzt74M3jIEsJaEJjBOoymmEMEs+41LER8YoGUQSgFTHsZzbKzXE Io/MHoVix8tz3CwmfkkrosiwJ6ywn/mYX5I6rdHCP9PbPePV/wfYyhaLZi1iKG0tTD+T wWOw== X-Gm-Message-State: AOAM533iWucqjdzONXDMElI9kPtq7HZz+cToCHwaxQKS6lwq54b3Zb13 WS7GQ58cnx46DG5l9B0itCy+8QkE4PsQcU0u0aeyTw== X-Received: by 2002:a7b:cd96:: with SMTP id y22mr15443921wmj.56.1592776486432; Sun, 21 Jun 2020 14:54:46 -0700 (PDT) MIME-Version: 1.0 References: <20200520125616.193765-1-kpsingh@chromium.org> In-Reply-To: From: KP Singh Date: Sun, 21 Jun 2020 23:54:35 +0200 Message-ID: Subject: Re: [PATCH bpf] security: Fix hook iteration for secid_to_secctx To: Ondrej Mosnacek Cc: Linux kernel mailing list , bpf , Linux Security Module list , Alexei Starovoitov , Daniel Borkmann , James Morris , Anders Roxell , Casey Schaufler , Peter Zijlstra , Josh Poimboeuf Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 19, 2020 at 4:17 PM Ondrej Mosnacek wrote: > > On Fri, Jun 19, 2020 at 3:13 PM KP Singh wrote: > > Hi, > > > > On Fri, Jun 19, 2020 at 2:49 PM Ondrej Mosnacek wrote: > > > > > > On Wed, May 20, 2020 at 2:56 PM KP Singh wrote: > > > > From: KP Singh > > > > > > > > secid_to_secctx is not stackable, and since the BPF LSM registers this > > > > hook by default, the call_int_hook logic is not suitable which > > > > "bails-on-fail" and casues issues when other LSMs register this hook and > > > > eventually breaks Audit. > > > > > > > > In order to fix this, directly iterate over the security hooks instead > > > > of using call_int_hook as suggested in: > > > > > > > > https: //lore.kernel.org/bpf/9d0eb6c6-803a-ff3a-5603-9ad6d9edfc00@schaufler-ca.com/#t > > > > > > > > Fixes: 98e828a0650f ("security: Refactor declaration of LSM hooks") > > > > Fixes: 625236ba3832 ("security: Fix the default value of secid_to_secctx hook" > > > > Reported-by: Alexei Starovoitov > > > > Signed-off-by: KP Singh > > > [...] > > > > > > Sorry for being late to the party, but doesn't this (and the > > > associated default return value patch) just paper over a bigger > > > problem? What if I have only the BPF LSM enabled and I attach a BPF > > > program to this hook that just returns 0? Doesn't that allow anything > > > privileged enough to do this to force the kernel to try and send > > > memory from uninitialized pointers to userspace and/or copy such > > > memory around and/or free uninitialized pointers? > > > > > > Why on earth does the BPF LSM directly expose *all* of the hooks, even > > > those that are not being used for any security decisions (and are > > > "useful" in this context only for borking the kernel...)? Feel free to > > > prove me wrong, but this lazy approach of "let's just take all the > > > hooks as they are and stick BPF programs to them" doesn't seem like a > > > > The plan was definitely to not hook everywhere but only call the hooks > > that do have a BPF program registered. This was one of the versions > > we proposed in the initial patches where the call to the BPF LSM was > > guarded by a static key with it being enabled only when there's a > > BPF program attached to the hook. > > > > https://lore.kernel.org/bpf/20200220175250.10795-5-kpsingh@chromium.org/ > > > > However, this special-cased BPF in the LSM framework, and, was met > > with opposition. Our plan is to still achieve this, but we want to do this > > with DEFINE_STATIC_CALL patches: > > > > https://lore.kernel.org/lkml/cover.1547073843.git.jpoimboe@redhat.com > > > > Using these, only can we enable the call into the hook based on whether > > a program is attached, we can also eliminate the indirect call overhead which > > currently affects the "slow" way which was decided in the discussion: > > > > https://lore.kernel.org/bpf/202002241136.C4F9F7DFF@keescook/ > > Perhaps you are misunderstanding me... I don't have a problem with BPF > LSM registering callbacks for all the hooks. My point is about what > you can trigger once you attach programs to certain hooks. All the > above seem to be just optimizations/implementation details that do not > affect the problem I'm pointing to. > The immediate concern was to fix the issue caused by the default callback (bpf_lsm_secid_to_secctx) which affected even the users who were not deliberately attaching a BPF program to the hook. We can probably restrict attachment of BPF programs to be fexit trampolines instead of fmod_ret by using some of the work that is being done for BTF ID whitelists for the d_path helper. (fexit trampolines cannot change the return value of the default callback). https://lore.kernel.org/bpf/20200616100512.2168860-9-jolsa@kernel.org/ With some of the optimization work which should remove this default callback, we can also prevent the non-deliberate errors (the ones that occur even when a BPF program is not attached to the LSM hook). IMHO, these are more important to fix. - KP > > > > > good choice... IMHO you should either limit the set of hooks that can > > > be attached to only those that aren't used to return back values via > > > > I am not sure if limiting the hooks is required here once we have > > the ability to call into BPF only when a program is attached. If the > > the user provides a BPF program, deliberately returns 0 (or any > > other value) then it is working as intended. Even if we limit this in the > > bpf LSM, deliberate privileged users can still achieve this with > > other means. > > The point is that for this particular hook (secid_to_secctx) and a > couple others, the consequences of having control over the return > value are more serious than with other hooks. For most hooks, the > implementation usually just returns 0 (OK), -EACCESS (access denied) > or -E... (error) and the caller either continues as normal or handles > the error. But here if you return 0, you signal that you have > initialized the pointer and size to valid values. So suddenly the BPF > prog doesn't just control allow/deny decisions, but can now easily > trigger kernel panic. And when you look at the semantics of the hook, > you will realize that it doesn't really make sense to implement it via > BPF, since it can never populate the output values and the only > meaningful implementation would be to just return -EOPNOTSUPP. > > Maybe I have it all wrong, but isn't the whole point of BPF programs > to provide a tight sandbox where you can only implement pure input -> > output functions + read/modify some internal state? Is it really > "working as intended" if you can crash the kernel by attaching a > simple BPF program to a certain hook? I mean yes, you can make the > system pretty much unusable already using the classic hooks by simply > returning -EACCESS for everything, but IMO that's quite different from > causing the kernel to do an invalid memory access. > > -- > Ondrej Mosnacek > Software Engineer, Platform Security - SELinux kernel > Red Hat, Inc. >