linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jann Horn <jannh@google.com>
To: "Mickaël Salaün" <mic@digikod.net>
Cc: "kernel list" <linux-kernel@vger.kernel.org>,
	"Al Viro" <viro@zeniv.linux.org.uk>,
	"Andy Lutomirski" <luto@amacapital.net>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Casey Schaufler" <casey@schaufler-ca.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"James Morris" <jmorris@namei.org>, "Jann Horn" <jann@thejh.net>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Kees Cook" <keescook@chromium.org>,
	"Michael Kerrisk" <mtk.manpages@gmail.com>,
	"Mickaël Salaün" <mickael.salaun@ssi.gouv.fr>,
	"Serge E . Hallyn" <serge@hallyn.com>,
	"Shuah Khan" <shuah@kernel.org>,
	"Vincent Dagonneau" <vincent.dagonneau@ssi.gouv.fr>,
	"Kernel Hardening" <kernel-hardening@lists.openwall.com>,
	"Linux API" <linux-api@vger.kernel.org>,
	linux-arch <linux-arch@vger.kernel.org>,
	linux-doc@vger.kernel.org,
	linux-fsdevel <linux-fsdevel@vger.kernel.org>,
	"open list:KERNEL SELFTEST FRAMEWORK"
	<linux-kselftest@vger.kernel.org>,
	linux-security-module <linux-security-module@vger.kernel.org>,
	"the arch/x86 maintainers" <x86@kernel.org>
Subject: Re: [RFC PATCH v14 01/10] landlock: Add object and rule management
Date: Wed, 26 Feb 2020 21:24:04 +0100	[thread overview]
Message-ID: <CAG48ez33WjzAee9h_Nfxi6vbnjognsKziv=whi_7ocT36DCXcg@mail.gmail.com> (raw)
In-Reply-To: <67465638-e22c-5d1a-df37-862b31d999a1@digikod.net>

On Wed, Feb 26, 2020 at 4:32 PM Mickaël Salaün <mic@digikod.net> wrote:
> On 25/02/2020 21:49, Jann Horn wrote:
> > On Mon, Feb 24, 2020 at 5:05 PM Mickaël Salaün <mic@digikod.net> wrote:
> >> A Landlock object enables to identify a kernel object (e.g. an inode).
> >> A Landlock rule is a set of access rights allowed on an object.  Rules
> >> are grouped in rulesets that may be tied to a set of processes (i.e.
> >> subjects) to enforce a scoped access-control (i.e. a domain).
> >>
> >> Because Landlock's goal is to empower any process (especially
> >> unprivileged ones) to sandbox themselves, we can't rely on a system-wide
> >> object identification such as file extended attributes.  Indeed, we need
> >> innocuous, composable and modular access-controls.
> >>
> >> The main challenge with this constraints is to identify kernel objects
> >> while this identification is useful (i.e. when a security policy makes
> >> use of this object).  But this identification data should be freed once
> >> no policy is using it.  This ephemeral tagging should not and may not be
> >> written in the filesystem.  We then need to manage the lifetime of a
> >> rule according to the lifetime of its object.  To avoid a global lock,
> >> this implementation make use of RCU and counters to safely reference
> >> objects.
> >>
> >> A following commit uses this generic object management for inodes.
[...]
> >> +config SECURITY_LANDLOCK
> >> +       bool "Landlock support"
> >> +       depends on SECURITY
> >> +       default n
> >
> > (I think "default n" is implicit?)
>
> It seems that most (all?) Kconfig are written like this.

See e.g. <https://lore.kernel.org/lkml/c187bb77-e804-93bd-64db-9418be58f191@infradead.org/>.

[...]
> >> +       return object;
> >> +}
> >> +
> >> +struct landlock_object *landlock_get_object(struct landlock_object *object)
> >> +       __acquires(object->usage)
> >> +{
> >> +       __acquire(object->usage);
> >> +       /*
> >> +        * If @object->usage equal 0, then it will be ignored by writers, and
> >> +        * underlying_object->object may be replaced, but this is not an issue
> >> +        * for release_object().
> >> +        */
> >> +       if (object && refcount_inc_not_zero(&object->usage)) {
> >> +               /*
> >> +                * It should not be possible to get a reference to an object if
> >> +                * its underlying object is being terminated (e.g. with
> >> +                * landlock_release_object()), because an object is only
> >> +                * modifiable through such underlying object.  This is not the
> >> +                * case with landlock_get_object_cleaner().
> >> +                */
> >> +               WARN_ON_ONCE(!READ_ONCE(object->underlying_object));
> >> +               return object;
> >> +       }
> >> +       return NULL;
> >> +}
> >> +
> >> +static struct landlock_object *get_object_cleaner(
> >> +               struct landlock_object *object)
> >> +       __acquires(object->cleaners)
> >> +{
> >> +       __acquire(object->cleaners);
> >> +       if (object && refcount_inc_not_zero(&object->cleaners))
> >> +               return object;
> >> +       return NULL;
> >> +}
> >
> > I don't get this whole "cleaners" thing. Can you give a quick
> > description of why this is necessary, and what benefits it has over a
> > standard refcounting+RCU scheme? I don't immediately see anything that
> > requires this.
>
> This indeed needs more documentation here. Here is a comment I'll add to
> get_object_cleaner():
>
> This enables to safely get a reference to an object to potentially free
> it if it is not already being freed by a concurrent thread.

"get a reference to an object to potentially free it" just sounds all
wrong to me. You free an object when you're *dropping* a reference to
it. Your refcounting scheme doesn't fit my mental models of how normal
refcounting works at all...

[...]
> >> +/*
> >> + * Putting an object is easy when the object is being terminated, but it is
> >> + * much more tricky when the reason is that there is no more rule tied to this
> >> + * object.  Indeed, new rules could be added at the same time.
> >> + */
> >> +void landlock_put_object(struct landlock_object *object)
> >> +       __releases(object->usage)
> >> +{
> >> +       struct landlock_object *object_cleaner;
> >> +
> >> +       __release(object->usage);
> >> +       might_sleep();
> >> +       if (!object)
> >> +               return;
> >> +       /*
> >> +        * Guards against concurrent termination to be able to terminate
> >> +        * @object if it is empty and not referenced by another rule-appender
> >> +        * other than the underlying object.
> >> +        */
> >> +       object_cleaner = get_object_cleaner(object);
[...]
> >> +       /*
> >> +        * Decrements @object->usage and if it reach zero, also decrement
> >> +        * @object->cleaners.  If both reach zero, then release and free
> >> +        * @object.
> >> +        */
> >> +       if (refcount_dec_and_test(&object->usage)) {
> >> +               struct landlock_rule *rule_walker, *rule_walker2;
> >> +
> >> +               spin_lock(&object->lock);
> >> +               /*
> >> +                * Disables all the rules tied to @object when it is forbidden
> >> +                * to add new rule but still allowed to remove them with
> >> +                * landlock_put_rule().  This is crucial to be able to safely
> >> +                * free a rule according to landlock_rule_is_disabled().
> >> +                */
> >> +               list_for_each_entry_safe(rule_walker, rule_walker2,
> >> +                               &object->rules, list)
> >> +                       list_del_rcu(&rule_walker->list);

So... rules don't take references on the landlock_objects they use?
Instead, the landlock_object knows which rules use it, and when the
landlock_object goes away, it nukes all the rules associated with
itself?

That seems terrible to me - AFAICS it means that if some random
process decides to install a landlock rule that uses inode X, and then
that process dies together with all its landlock rules, the inode
still stays pinned in kernel memory as long as the superblock is
mounted. In other words, it's a resource leak. (And if I'm not missing
something in patch 5, that applies even if the inode has been
unlinked?)

Can you please refactor your refcounting as follows?

 - A rule takes a reference on each landlock_object it uses.
 - A landlock_object takes a reference on the underlying object (just like now).
 - The underlying object *DOES NOT* take a reference on the
landlock_object (unlike now); the reference from the underlying object
to the landlock_object has weak pointer semantics.
 - When a landlock_object's refcount drops to zero (iow no rules use
it anymore), it is freed.

That might also help get rid of the awkward ->cleaners thing?

> >> +               /*
> >> +                * Releases @object if it is not already released (e.g. with
> >> +                * landlock_release_object()).
> >> +                */
> >> +               release_object(object);
> >> +               /*
> >> +                * Unbalances the @object->cleaners counter to reflect the
> >> +                * underlying object release.
> >> +                */
> >> +               __acquire(object->cleaners);
> >> +               put_object_free(object);
> >> +       }
> >> +       put_object_cleaner(object_cleaner);
> >> +}
[...]
> >> +static inline bool landlock_rule_is_disabled(
> >> +               struct landlock_rule *rule)
> >> +{
> >> +       /*
> >> +        * Disabling (i.e. unlinking) a landlock_rule is a one-way operation.
> >> +        * It is not possible to re-enable such a rule, then there is no need
> >> +        * for smp_load_acquire().
> >> +        *
> >> +        * LIST_POISON2 is set by list_del() and list_del_rcu().
> >> +        */
> >> +       return !rule || READ_ONCE(rule->list.prev) == LIST_POISON2;
> >
> > You're not allowed to do this, the comment above list_del() states:
> >
> >  * Note: list_empty() on entry does not return true after this, the entry is
> >  * in an undefined state.
>
> list_del() checks READ_ONCE(head->next) == head, but
> landlock_rule_is_disabled() checks READ_ONCE(rule->list.prev) ==
> LIST_POISON2.
> The comment about LIST_POISON2 is right but may be misleading. There is
> no use of list_empty() with a landlock_rule->list, only
> landlock_object->rules. The only list_del() is in landlock_put_rule()
> when there is a guarantee that there is no other reference to it, hence
> no possible use of landlock_rule_is_disabled() with this rule. I could
> replace it with a call to list_del_rcu() to make it more consistent.
>
> >
> > If you want to be able to test whether the element is on a list
> > afterwards, use stuff like list_del_init().
>
> There is no need to re-initialize the list but using list_del_init() and
> list_empty() could work too. However, there is no list_del_init_rcu()
> helper. Moreover, resetting the list's pointer with LIST_POISON2 might
> help to detect bugs.

Either way, you are currently using the list_head API in a way that
goes against what the header documents. If you want to rely on
list_del() bringing the object into a specific state, then you can't
leave the comment above list_del() as-is that says that it puts the
object in an undefined state; and this kind of check should probably
be done in a helper in list.h instead of open-coding the check for
LIST_POISON2.

  reply	other threads:[~2020-02-26 20:24 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-24 16:02 [RFC PATCH v14 00/10] Landlock LSM Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 01/10] landlock: Add object and rule management Mickaël Salaün
2020-02-25 20:49   ` Jann Horn
2020-02-26 15:31     ` Mickaël Salaün
2020-02-26 20:24       ` Jann Horn [this message]
2020-02-27 16:46         ` Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 02/10] landlock: Add ruleset and domain management Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 03/10] landlock: Set up the security framework and manage credentials Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 04/10] landlock: Add ptrace restrictions Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 05/10] fs,landlock: Support filesystem access-control Mickaël Salaün
2020-02-26 20:29   ` Jann Horn
2020-02-27 16:50     ` Mickaël Salaün
2020-02-27 16:51       ` Jann Horn
2020-02-24 16:02 ` [RFC PATCH v14 06/10] landlock: Add syscall implementation Mickaël Salaün
2020-03-17 16:47   ` Al Viro
2020-03-17 17:51     ` Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 07/10] arch: Wire up landlock() syscall Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 08/10] selftests/landlock: Add initial tests Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 09/10] samples/landlock: Add a sandbox manager example Mickaël Salaün
2020-02-24 16:02 ` [RFC PATCH v14 10/10] landlock: Add user and kernel documentation Mickaël Salaün
2020-02-29 17:23   ` Randy Dunlap
2020-03-02 10:03     ` Mickaël Salaün
2020-02-25 18:49 ` [RFC PATCH v14 00/10] Landlock LSM J Freyensee
2020-02-26 15:34   ` Mickaël Salaün
     [not found] ` <20200227042002.3032-1-hdanton@sina.com>
2020-02-27 17:01   ` [RFC PATCH v14 01/10] landlock: Add object and rule management Mickaël Salaün
2020-03-09 23:44 ` [RFC PATCH v14 00/10] Landlock LSM Jann Horn
2020-03-11 23:38   ` Mickaël Salaün
2020-03-17 16:19     ` Jann Horn
2020-03-17 17:50       ` Mickaël Salaün
2020-03-17 19:45         ` Jann Horn
2020-03-18 12:06           ` Mickaël Salaün
2020-03-18 23:33             ` Jann Horn
2020-03-19 16:58               ` Mickaël Salaün
2020-03-19 21:17                 ` Jann Horn
2020-03-30 18:26                   ` Mickaël Salaün

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAG48ez33WjzAee9h_Nfxi6vbnjognsKziv=whi_7ocT36DCXcg@mail.gmail.com' \
    --to=jannh@google.com \
    --cc=arnd@arndb.de \
    --cc=casey@schaufler-ca.com \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=jann@thejh.net \
    --cc=jmorris@namei.org \
    --cc=keescook@chromium.org \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mic@digikod.net \
    --cc=mickael.salaun@ssi.gouv.fr \
    --cc=mtk.manpages@gmail.com \
    --cc=serge@hallyn.com \
    --cc=shuah@kernel.org \
    --cc=vincent.dagonneau@ssi.gouv.fr \
    --cc=viro@zeniv.linux.org.uk \
    --cc=x86@kernel.org \
    --subject='Re: [RFC PATCH v14 01/10] landlock: Add object and rule management' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
on how to clone and mirror all data and code used for this inbox