Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp6066485ybv; Wed, 12 Feb 2020 05:27:44 -0800 (PST) X-Google-Smtp-Source: APXvYqyw8upMonwVnz4sOUS2Hn+AvKf5vSjZrWoar3Xlm0HUIgpmnY9tdulq5cIB1yIuA9xnCD3d X-Received: by 2002:a9d:7410:: with SMTP id n16mr9542480otk.23.1581514064342; Wed, 12 Feb 2020 05:27:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581514064; cv=none; d=google.com; s=arc-20160816; b=anT95HDm1Up74odEBmw9qMQ0w6IsgWF0c5caZHUdJcM8oRFUTDiwAwEplVNja4mEYU CuhaDYxQLbWBzHiwxeG6w7cds9H0wW5ya8mcAFH9D+D31Ni6NHqF7eJeQgArTV/P+J/g aZAxJvR5e+lILGJ5tKQMtfI2QihnuGMR9J8JgNcvPQ5URVNdlohtWUy8btTYZcDiq2fj xviSlo8OzQ3bM6sOCU3E7qkOrmYOoEtPuzNFNWvCAvGtGNr2ZXiVH8sF+4glflKYw55W dkEWxLg9OkcS91jvSct0jlPTwiYYVXderyf32r1OyPL3O3/VPdIxiMH5bVzOJoNs9XIP 36/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=ZtXXlREU2cjAd+/HomCRqQSbaeZi4fws+du2r06WTMw=; b=HQjNN4I2MiVd4NP8Ok9nEpDvqJ5H4hONtUhTXyuDWBroAG2XEOpRPKDJFCQv6bWm5j YQRnybSSQB3XVcBKFk/nyKl5NcJGFm6Mgo7BT1O7SG8TphTcJ3f+/6frTDmK7er3lmh3 QQba5MEV5K1irUPy5ypu8bnYzL/Y2VsOTRq0rwQum3SmHUZy01eiOaKQqNtWHMKbDnRg VbaRdBpKG/MLnjIM43VTfnY0HlXoVBGH4FAIA+/ZbP202DWpnVg3OAKF5Z74JgAZI6jc rKzQadSdxEc8l6D5W66vdHovAlksYD2gkh/CsEIr/9qqmRb1nQ1ejLBfHTiUjuEm10VO yfTQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t10si190030otp.310.2020.02.12.05.27.31; Wed, 12 Feb 2020 05:27:44 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727790AbgBLN11 (ORCPT + 99 others); Wed, 12 Feb 2020 08:27:27 -0500 Received: from www62.your-server.de ([213.133.104.62]:57368 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725887AbgBLN11 (ORCPT ); Wed, 12 Feb 2020 08:27:27 -0500 Received: from sslproxy03.your-server.de ([88.198.220.132]) by www62.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1j1s2v-0004et-Je; Wed, 12 Feb 2020 14:27:18 +0100 Received: from [2001:1620:665:0:5795:5b0a:e5d5:5944] (helo=linux-3.fritz.box) by sslproxy03.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j1s2u-000X31-Sa; Wed, 12 Feb 2020 14:27:16 +0100 Subject: Re: BPF LSM and fexit [was: [PATCH bpf-next v3 04/10] bpf: lsm: Add mutable hooks list for the BPF LSM] To: Alexei Starovoitov Cc: 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 , =?UTF-8?Q?Micka=c3=abl_Sala=c3=bcn?= , Florent Revest , Brendan Jackman , "Serge E. Hallyn" , Mauro Carvalho Chehab , "David S. Miller" , Greg Kroah-Hartman , Kernel Team References: <20200211124334.GA96694@google.com> <20200211175825.szxaqaepqfbd2wmg@ast-mbp> <20200211190943.sysdbz2zuz5666nq@ast-mbp> <20200211201039.om6xqoscfle7bguz@ast-mbp> <20200211213819.j4ltrjjkuywihpnv@ast-mbp> <1cd10710-a81b-8f9b-696d-aa40b0a67225@iogearbox.net> <20200212024542.gdsafhvqykucdp4h@ast-mbp> From: Daniel Borkmann Message-ID: Date: Wed, 12 Feb 2020 14:27:15 +0100 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: <20200212024542.gdsafhvqykucdp4h@ast-mbp> 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.102.1/25721/Wed Feb 12 06:24:38 2020) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.. Thanks, Daniel