Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp6214922ybv; Wed, 12 Feb 2020 08:05:29 -0800 (PST) X-Google-Smtp-Source: APXvYqzsV3olrUlv70Easy+FUX3jsOjRIVleZp95hK+vXo/H4JS6ImaECQnzCl6AytBxtoNAwbws X-Received: by 2002:aca:ab53:: with SMTP id u80mr6487409oie.94.1581523528897; Wed, 12 Feb 2020 08:05:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581523528; cv=none; d=google.com; s=arc-20160816; b=VnlcbITwfj9DToX7bOb9kHB8oXw5N8ndDg/Xa1Rhqllyhun1Kg8PNpUUcBkWVk/VxQ vYpiafrix0wmI9hlozMIE8JiNLPBHqMiebcL12kioVx2ynRRhq0MkoAhATLAPBCNqq/h 9VyQcW6iPfp+8wEP8+3r9Wu3L8ckdG7AolzVr/AbtwndhQUImT2sl/GtKgB9FoZG99vY 77/+mNygfpG+GvzQpqn/xVIbB+JM8qFqMwh5FEhQgdgzutNWchCQxetq3Cih93uR95t7 cIodgoiaRU2jfnmpgYZSC5xVHCF4QINhc+DnNat+ok/c3zjBCXlfaJBSqHa+ZWSmYiGu 5GEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:date:from:dkim-signature; bh=KiqSA/av2ZNWBE7nJHGVa6/1LBWU3I7cZAwdWxWzGmE=; b=QwLTfMCA38H2f60guwbSzP6z9ba/44xtR7gbyVAZ7ayAFL8Nxd9bglPr3RJ9PMKj/e 18X85UQvPBuziyMzxNeICPFW08UKDMtvogTXbepR/gQ2hw9hZ4+OdvatH5jRCBOSmFh4 zGfUc0o5Y09lr4KaTfVRlkQW0RLYJdGt0+j2Z7eTTSyQSyv7LwvRBn2b1B8w2fS8wlrm wQ9Qn+qz/3VFlUVmSAo9KH4T+h7Ng4cHhVj8yeWOqzBLFLwCX1aEbKB6xa1d5P64BcbI 5jiUwPuk4LlY6SMhzQq1wuZ2IPeVyjlEr4ZJ+mqB8euwnxFJORn8djYdOcf/yfqRXJdC yJRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GANCPNe9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id d22si399077oti.316.2020.02.12.08.05.06; Wed, 12 Feb 2020 08:05:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GANCPNe9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1728757AbgBLQEc (ORCPT + 99 others); Wed, 12 Feb 2020 11:04:32 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:41630 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728698AbgBLQEb (ORCPT ); Wed, 12 Feb 2020 11:04:31 -0500 Received: by mail-wr1-f65.google.com with SMTP id c9so3032180wrw.8 for ; Wed, 12 Feb 2020 08:04:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=KiqSA/av2ZNWBE7nJHGVa6/1LBWU3I7cZAwdWxWzGmE=; b=GANCPNe9XjsbmeyJHI+SqNuFhFhrU5no91yuB/KbQIxyRrRL2oqnecs84I6PI/QFGs 0tOu2a8IbgyAQNajlvF4iGbdZkTzztqH1wn352I4W+zIv3zTlm73NfMid12H8TlJlTz/ q4VKWsBQ4W3eQD87vL2C3NiCwHrFZGuhegr5o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=KiqSA/av2ZNWBE7nJHGVa6/1LBWU3I7cZAwdWxWzGmE=; b=WKKA9bjkwmR1f2zo6m2SADvWFpUZ3Nj9leSjQZMy1BD2XH8rA0uZMU4BVecEfIse8P IXFWUd5YaYOKOYOGX6ZQWtfFtlP2E8GlWj2ONpMtKQVZzmmYrW8RqnmEVt57/HdZLpSU PHb41j7C2qhyNk+IKfLsOK+IPa/O8+D0lCGegGr+uqN8Wr+p5wjEs060oI+txJxJQb+m 1KxYTx1lhulq8AQDlaSH09BsPX5ArbUlBbtVdiXV4TpDC8yhvfY52Yeyffj5rqizIKSa MN32ZbN1B28V1dRkgUbp8QPxBh8Yyac0vRT8kLwzMa+bL4BDY3NzC+v2EiA7ZTzRZG1P tEBg== X-Gm-Message-State: APjAAAWU6B8iCXXah3SCzFLAwsCxUdlJW7o1tvv94E2I+TAvJcWb4G4X oe+FZR2QH1yR9cbUq5LLgWAN3A== X-Received: by 2002:a5d:4b88:: with SMTP id b8mr16010468wrt.343.1581523470287; Wed, 12 Feb 2020 08:04:30 -0800 (PST) Received: from google.com ([2a00:79e0:42:204:8a21:ba0c:bb42:75ec]) by smtp.gmail.com with ESMTPSA id b21sm1346559wmd.37.2020.02.12.08.04.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Feb 2020 08:04:29 -0800 (PST) From: KP Singh X-Google-Original-From: KP Singh Date: Wed, 12 Feb 2020 17:04:27 +0100 To: Daniel Borkmann Cc: Alexei Starovoitov , Jann Horn , KP Singh , kernel list , bpf , linux-security-module , Brendan Jackman , Florent Revest , Thomas Garnier , Alexei Starovoitov , James Morris , Kees Cook , Thomas Garnier , Michael Halcrow , Paul Turner , Brendan Gregg , Matthew Garrett , Christian Brauner , =?iso-8859-1?Q?Micka=EBl_Sala=FCn?= , Florent Revest , Brendan Jackman , "Serge E. Hallyn" , Mauro Carvalho Chehab , "David S. Miller" , Greg Kroah-Hartman , Kernel Team Subject: Re: BPF LSM and fexit [was: [PATCH bpf-next v3 04/10] bpf: lsm: Add mutable hooks list for the BPF LSM] Message-ID: <20200212160427.GA259057@google.com> References: <20200211190943.sysdbz2zuz5666nq@ast-mbp> <20200211201039.om6xqoscfle7bguz@ast-mbp> <20200211213819.j4ltrjjkuywihpnv@ast-mbp> <1cd10710-a81b-8f9b-696d-aa40b0a67225@iogearbox.net> <20200212024542.gdsafhvqykucdp4h@ast-mbp> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12-Feb 14:27, Daniel Borkmann wrote: > On 2/12/20 3:45 AM, Alexei Starovoitov wrote: > > On Wed, Feb 12, 2020 at 01:09:07AM +0100, Daniel Borkmann wrote: > > > > > > Another approach could be to have a special nop inside call_int_hook() > > > macro which would then get patched to avoid these situations. Somewhat > > > similar like static keys where it could be defined anywhere in text but > > > with updating of call_int_hook()'s RC for the verdict. > > > > Sounds nice in theory. I couldn't quite picture how that would look > > in the code, so I hacked: > > diff --git a/security/security.c b/security/security.c > > index 565bc9b67276..ce4bc1e5e26c 100644 > > --- a/security/security.c > > +++ b/security/security.c > > @@ -28,6 +28,7 @@ > > #include > > #include > > #include > > +#include > > > > #define MAX_LSM_EVM_XATTR 2 > > > > @@ -678,12 +679,26 @@ static void __init lsm_early_task(struct task_struct *task) > > * This is a hook that returns a value. > > */ > > > > +#define LSM_HOOK_NAME(FUNC) \ > > + DEFINE_STATIC_KEY_FALSE(bpf_lsm_key_##FUNC); > > +#include > > +#undef LSM_HOOK_NAME > > +__diag_push(); > > +__diag_ignore(GCC, 8, "-Wstrict-prototypes", ""); > > +#define LSM_HOOK_NAME(FUNC) \ > > + int bpf_lsm_call_##FUNC() {return 0;} > > +#include > > +#undef LSM_HOOK_NAME > > +__diag_pop(); > > + > > #define call_void_hook(FUNC, ...) \ > > do { \ > > struct security_hook_list *P; \ > > \ > > hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \ > > P->hook.FUNC(__VA_ARGS__); \ > > + if (static_branch_unlikely(&bpf_lsm_key_##FUNC)) \ > > + (void)bpf_lsm_call_##FUNC(__VA_ARGS__); \ > > } while (0) > > > > #define call_int_hook(FUNC, IRC, ...) ({ \ > > @@ -696,6 +711,8 @@ static void __init lsm_early_task(struct task_struct *task) > > if (RC != 0) \ > > break; \ > > } \ > > + if (RC == IRC && static_branch_unlikely(&bpf_lsm_key_##FUNC)) \ > > + RC = bpf_lsm_call_##FUNC(__VA_ARGS__); \ > > Nit: the `RC == IRC` test could be moved behind the static_branch_unlikely() so > that it would be bypassed when not enabled. > > > } while (0); \ > > RC; \ > > }) > > > > The assembly looks good from correctness and performance points. > > union security_list_options can be split into lsm_hook_names.h too > > to avoid __diag_ignore. Is that what you have in mind? > > I don't see how one can improve call_int_hook() macro without > > full refactoring of linux/lsm_hooks.h > > imo static_key doesn't have to be there in the first set. We can add this > > optimization later. > > Yes, like the above diff looks good, and then we'd dynamically attach the program > at bpf_lsm_call_##FUNC()'s fexit hook for a direct jump, so all the security_blah() > internals could stay as-is which then might also address Jann's concerns wrt > concrete annotation as well as potential locking changes inside security_blah(). > Agree that patching out via static key could be optional but since you were talking > about avoiding indirect jumps.. I like this approach as well. Will give it a go and update the patches. Thanks a lot for your inputs! - KP > > Thanks, > Daniel