Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp2616648ybe; Sat, 14 Sep 2019 19:45:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqwAaCmtuG+Rcmm60ZGeX/4JeH5cGS6vLahKF09pCkj1zucnpuvmFR9QeP5vCbJJpJjGOrMD X-Received: by 2002:a50:f045:: with SMTP id u5mr11353414edl.297.1568515534385; Sat, 14 Sep 2019 19:45:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568515534; cv=none; d=google.com; s=arc-20160816; b=pyhQ7TnPvvGmUZhvsKm/PTq+H+EGGLMod5mDDnKxfE27esd2gfAKece52t30PG56wX baWT28SmvFQygRDjGZHtDZeHRfCsfX89jDDHoIdd+kfK87yPg8hytUUsiInlAMUgNFOh sRGdsbwq4Bv2oMXCGnTb5U3aGHK2uUBUk4Es39+mUF3AwKKsk1Krc2FX1T3qR3KFvv/r jgz+rBDaNVEweJBokQZFF5+4LtGT90oH5oixef4AdKbtjlkuOYMrnFplLpuiU8Gvm2R3 UoAHDqLg18EbPsDmKJ9opJlbWiykYJwCfmdNRhqLL6rN/EAWwFa1sXbYZOZf6H+vsYRb wyKA== 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=6+RN58I3uX4e+kxMi088PxHU4GRRAfQ1h2bRXfJjpgg=; b=uG8qa+3YWDBjSd8CoGEoVM0vz7B3HHNd0JnCr1KamfhLq5EZUzQKR+j1wHStPDtnzb eGx6MWJcxRnt7Zu7LJHyQIqF2eFJ3d5ykJZCCg0U9jzun37fflHXrisickLBnszOuYzM 4s9f8ae0s/nQeFT5zkMf8WWAPcxf5EKz+npGlu+hJVGr6qIV5i8jdnHjFXrxO003Iupo IEf2WHdLvL+ptesZpyL3YuKlnkzYoBx/lnKwAXYd9oweFhVrXTQvf85BT9KNPuMZQx2E 1FTuWurcnHgKKYDMtVODh53hOLaFeCCAIOMkYJ5jn+rySNM5D7u84ZIZC8239QXCM+Pu lWHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="fpye/1uO"; 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 u8si206488edx.162.2019.09.14.19.45.09; Sat, 14 Sep 2019 19:45:34 -0700 (PDT) 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="fpye/1uO"; 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 S1727068AbfIOBkP (ORCPT + 99 others); Sat, 14 Sep 2019 21:40:15 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:45130 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726562AbfIOBkO (ORCPT ); Sat, 14 Sep 2019 21:40:14 -0400 Received: by mail-ed1-f65.google.com with SMTP id h33so1304055edh.12 for ; Sat, 14 Sep 2019 18:40:12 -0700 (PDT) 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=6+RN58I3uX4e+kxMi088PxHU4GRRAfQ1h2bRXfJjpgg=; b=fpye/1uOdA27CI+WiVSAHY2FFj0ygzyL96vbWVl2S0f00HcwfRnuO9dwtmh4CWpGmh bn3/omY1ogZSmhi4eBKZTcohOx8sxu+ykLP8fnFJLIUewNLErExGvR3+kDs7yXubDKHS 6OUFmTvFK8vyYZU2AaDF+x2URUFpcweSxpwrA= 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=6+RN58I3uX4e+kxMi088PxHU4GRRAfQ1h2bRXfJjpgg=; b=aZ/+V2oJ/6wi/A37tuouH4GQTVwj8RpRU1nh7AbqWyg071d8ijYhwYDTH7kcqwta7W hT9+3D90qXzdpzg4x9/pbJ+isamMvClZoXvkFB408jVYC1Asi3f8yUp4G7ns7ACkPliM NbiaxjQuLb//eKo4DUEulgzO7ZFSrEk2m/10q3L0lC9r7Sr3cJWQ/DqOPzklHrSPJwWg ZMAHOpjq/v+0nfnBwsrflwmlntEmPjBoPSN0iry2jciNuphJlHTUuSkTFSlWTfk+7QYv S8BE7aHBsx9pVVw2YlH1yRA4eSRwaw6YNkB+krTMk4dmM44BwTxIz/0Xj8Ma/yA9wQsx LJrg== X-Gm-Message-State: APjAAAUJNs+McMnLzk1qfGnDx6puIXgQSXayzKv++0MCaV8aoH9GtGQO go/YPqAjDI1mEsBwO9gszPjitQ== X-Received: by 2002:a17:907:423e:: with SMTP id oi22mr34778743ejb.311.1568511611420; Sat, 14 Sep 2019 18:40:11 -0700 (PDT) Received: from chromium.org (77-56-209-237.dclient.hispeed.ch. [77.56.209.237]) by smtp.gmail.com with ESMTPSA id 60sm6175120edg.10.2019.09.14.18.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2019 18:40:10 -0700 (PDT) From: KP Singh X-Google-Original-From: KP Singh Date: Sun, 15 Sep 2019 03:40:08 +0200 To: Yonghong Song Cc: "linux-kernel@vger.kernel.org" , "bpf@vger.kernel.org" , "linux-security-module@vger.kernel.org" , Alexei Starovoitov , Daniel Borkmann , James Morris , Kees Cook , Thomas Garnier , Michael Halcrow , Paul Turner , Brendan Gregg , Jann Horn , Matthew Garrett , Christian Brauner , =?iso-8859-1?Q?Micka=EBl_Sala=FCn?= , Florent Revest , Martin Lau , Song Liu , "Serge E. Hallyn" , Mauro Carvalho Chehab , "David S. Miller" , Greg Kroah-Hartman , Nicolas Ferre , Stanislav Fomichev , Quentin Monnet , Andrey Ignatov , Joe Stringer Subject: Re: [RFC v1 14/14] krsi: Pin arg pages only when needed Message-ID: <20190915014008.GA19558@chromium.org> References: <20190910115527.5235-1-kpsingh@chromium.org> <20190910115527.5235-15-kpsingh@chromium.org> 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 15-Sep 00:33, Yonghong Song wrote: > > > On 9/10/19 12:55 PM, KP Singh wrote: > > From: KP Singh > > > > Adds a callback which is called when a new program is attached > > to a hook. The callback registered by the process_exection hook > > checks if a program that has calls to a helper that requires pages to > > be pinned (eg. krsi_get_env_var). > > > > Signed-off-by: KP Singh > > --- > > include/linux/krsi.h | 1 + > > security/krsi/include/hooks.h | 5 ++- > > security/krsi/include/krsi_init.h | 7 ++++ > > security/krsi/krsi.c | 62 ++++++++++++++++++++++++++++--- > > security/krsi/ops.c | 10 ++++- > > 5 files changed, 77 insertions(+), 8 deletions(-) > > > > diff --git a/include/linux/krsi.h b/include/linux/krsi.h > > index c7d1790d0c1f..e443d0309764 100644 > > --- a/include/linux/krsi.h > > +++ b/include/linux/krsi.h > > @@ -7,6 +7,7 @@ > > > > #ifdef CONFIG_SECURITY_KRSI > > int krsi_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog); > > +extern const struct bpf_func_proto krsi_get_env_var_proto; > > #else > > static inline int krsi_prog_attach(const union bpf_attr *attr, > > struct bpf_prog *prog) > > diff --git a/security/krsi/include/hooks.h b/security/krsi/include/hooks.h > > index e070c452b5de..38293125ff99 100644 > > --- a/security/krsi/include/hooks.h > > +++ b/security/krsi/include/hooks.h > > @@ -8,7 +8,7 @@ > > * > > * Format: > > * > > - * KRSI_HOOK_INIT(TYPE, NAME, LSM_HOOK, KRSI_HOOK_FN) > > + * KRSI_HOOK_INIT(TYPE, NAME, LSM_HOOK, KRSI_HOOK_FN, CALLBACK) > > * > > * KRSI adds one layer of indirection between the name of the hook and the name > > * it exposes to the userspace in Security FS to prevent the userspace from > > @@ -18,4 +18,5 @@ > > KRSI_HOOK_INIT(PROCESS_EXECUTION, > > process_execution, > > bprm_check_security, > > - krsi_process_execution) > > + krsi_process_execution, > > + krsi_process_execution_cb) > > diff --git a/security/krsi/include/krsi_init.h b/security/krsi/include/krsi_init.h > > index 6152847c3b08..99801d5b273a 100644 > > --- a/security/krsi/include/krsi_init.h > > +++ b/security/krsi/include/krsi_init.h > > @@ -31,6 +31,8 @@ struct krsi_ctx { > > }; > > }; > > > > +typedef int (*krsi_prog_attach_t) (struct bpf_prog_array *); > > + > > /* > > * The LSM creates one file per hook. > > * > > @@ -61,6 +63,11 @@ struct krsi_hook { > > * The eBPF programs that are attached to this hook. > > */ > > struct bpf_prog_array __rcu *progs; > > + /* > > + * The attach callback is called before a new program is attached > > + * to the hook and is passed the updated bpf_prog_array as an argument. > > + */ > > + krsi_prog_attach_t attach_callback; > > }; > > > > extern struct krsi_hook krsi_hooks_list[]; > > diff --git a/security/krsi/krsi.c b/security/krsi/krsi.c > > index 00a7150c1b22..a4443d7aa150 100644 > > --- a/security/krsi/krsi.c > > +++ b/security/krsi/krsi.c > > @@ -5,15 +5,65 @@ > > #include > > #include > > #include > > +#include > > #include > > > > #include "krsi_init.h" > > > > +/* > > + * need_arg_pages is only updated in bprm_check_security_cb > > + * when a mutex on krsi_hook for bprm_check_security is already > > + * held. need_arg_pages avoids pinning pages when no program > > + * that needs them is attached to the hook. > > + */ > > +static bool need_arg_pages; > > + > > +/* > > + * Checks if the instruction is a BPF_CALL to an eBPF helper located > > + * at the given address. > > + */ > > +static inline bool bpf_is_call_to_func(struct bpf_insn *insn, > > + void *func_addr) > > +{ > > + u8 opcode = BPF_OP(insn->code); > > + > > + if (opcode != BPF_CALL) > > + return false; > > + > > + if (insn->src_reg == BPF_PSEUDO_CALL) > > + return false; > > + > > + /* > > + * The BPF verifier updates the value of insn->imm from the > > + * enum bpf_func_id to the offset of the address of helper > > + * from the __bpf_call_base. > > + */ > > + return __bpf_call_base + insn->imm == func_addr; > > In how many cases, krsi program does not have krsi_get_env_var() helper? It depends, if the user does not choose to use log environment variables or use the the value as a part of their MAC policy, the pinning of the pages is not needed. Also, the pinning is needed since eBPF helpers cannot run a non-atomic context. It would not be needed if "sleepable eBPF" becomes a thing. - KP > > > +} > > + > > +static int krsi_process_execution_cb(struct bpf_prog_array *array) > > +{ > > + struct bpf_prog_array_item *item = array->items; > > + struct bpf_prog *p; > > + const struct bpf_func_proto *proto = &krsi_get_env_var_proto; > > + int i; > > + > > + while ((p = READ_ONCE(item->prog))) { > > + for (i = 0; i < p->len; i++) { > > + if (bpf_is_call_to_func(&p->insnsi[i], proto->func)) > > + need_arg_pages = true; > > + } > > + item++; > > + } > > + return 0; > > +} > > + > > struct krsi_hook krsi_hooks_list[] = { > > - #define KRSI_HOOK_INIT(TYPE, NAME, H, I) \ > > + #define KRSI_HOOK_INIT(TYPE, NAME, H, I, CB) \ > > [TYPE] = { \ > > .h_type = TYPE, \ > > .name = #NAME, \ > > + .attach_callback = CB, \ > > }, > > #include "hooks.h" > > #undef KRSI_HOOK_INIT > > @@ -75,9 +125,11 @@ static int krsi_process_execution(struct linux_binprm *bprm) > > .bprm = bprm, > > }; > > > > - ret = pin_arg_pages(&ctx.bprm_ctx); > > - if (ret < 0) > > - goto out_arg_pages; > > + if (READ_ONCE(need_arg_pages)) { > > + ret = pin_arg_pages(&ctx.bprm_ctx); > > + if (ret < 0) > > + goto out_arg_pages; > > + } > > > > ret = krsi_run_progs(PROCESS_EXECUTION, &ctx); > > kfree(ctx.bprm_ctx.arg_pages); > > @@ -87,7 +139,7 @@ static int krsi_process_execution(struct linux_binprm *bprm) > > } > > > > static struct security_hook_list krsi_hooks[] __lsm_ro_after_init = { > > - #define KRSI_HOOK_INIT(T, N, HOOK, IMPL) LSM_HOOK_INIT(HOOK, IMPL), > > + #define KRSI_HOOK_INIT(T, N, HOOK, IMPL, CB) LSM_HOOK_INIT(HOOK, IMPL), > > #include "hooks.h" > > #undef KRSI_HOOK_INIT > > }; > > diff --git a/security/krsi/ops.c b/security/krsi/ops.c > > index 1db94dfaac15..2de682371eff 100644 > > --- a/security/krsi/ops.c > > +++ b/security/krsi/ops.c > > @@ -139,6 +139,14 @@ int krsi_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog) > > goto unlock; > > } > > > > + if (h->attach_callback) { > > + ret = h->attach_callback(new_array); > > + if (ret < 0) { > > + bpf_prog_array_free(new_array); > > + goto unlock; > > + } > > + } > > + > > rcu_assign_pointer(h->progs, new_array); > > bpf_prog_array_free(old_array); > > > > @@ -278,7 +286,7 @@ BPF_CALL_5(krsi_get_env_var, struct krsi_ctx *, ctx, char *, name, u32, n_size, > > return get_env_var(ctx, name, dest, n_size, size); > > } > > > > -static const struct bpf_func_proto krsi_get_env_var_proto = { > > +const struct bpf_func_proto krsi_get_env_var_proto = { > > .func = krsi_get_env_var, > > .gpl_only = true, > > .ret_type = RET_INTEGER, > >