From: Lakshmi Ramasubramanian <nramas@linux.microsoft.com>
To: Ondrej Mosnacek <omosnace@redhat.com>
Cc: SElinux list <selinux@vger.kernel.org>,
Paul Moore <paul@paul-moore.com>,
Stephen Smalley <stephen.smalley.work@gmail.com>,
rcu@vger.kernel.org, "Paul E . McKenney" <paulmck@kernel.org>
Subject: Re: [RFC PATCH 3/3] selinux: track policy lifetime with refcount
Date: Mon, 7 Sep 2020 07:03:23 -0700 [thread overview]
Message-ID: <41bc40c8-e2b5-8535-5d72-ebe66e2ffb5d@linux.microsoft.com> (raw)
In-Reply-To: <CAFqZXNs+eQL7H54BbbeO-Ra4RtQKwQN6gBPAniMmUHmr4RGyBw@mail.gmail.com>
On 9/7/20 12:47 AM, Ondrej Mosnacek wrote:
> On Sat, Sep 5, 2020 at 11:33 PM Lakshmi Ramasubramanian
> <nramas@linux.microsoft.com> wrote:
>> On 8/25/20 8:20 AM, Ondrej Mosnacek wrote:
>>
>> Hi Ondrej,
>>
>> I am just trying understand the expected behavior w.r.t the usage of
>> rcu_dereference_protected() for accessing SELinux policy. Could you
>> please clarify?
>>
>>> Instead of holding the RCU read lock the whole time while accessing the
>>> policy, add a simple refcount mechanism to track its lifetime. After
>>> this, the RCU read lock is held only for a brief time when fetching the
>>> policy pointer and incrementing the refcount. The policy struct is then
>>> guaranteed to stay alive until the refcount is decremented.
>>>
>>> Freeing of the policy remains the responsibility of the task that does
>>> the policy reload. In case the refcount drops to zero in a different
>>> task, the policy load task is notified via a completion.
>>>
>>> The advantage of this change is that the operations that access the
>>> policy can now do sleeping allocations, since they don't need to hold
>>> the RCU read lock anymore. This patch so far only leverages this in
>>> security_read_policy() for the vmalloc_user() allocation (although this
>>> function is always called under fsi->mutex and could just access the
>>> policy pointer directly). The conversion of affected GFP_ATOMIC
>>> allocations to GFP_KERNEL is left for a later patch, since auditing
>>> which code paths may still need GFP_ATOMIC is not very easy.
>>>
>>> Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
>>> ---
>>> security/selinux/ss/services.c | 286 ++++++++++++++++-----------------
>>> security/selinux/ss/services.h | 6 +
>>> 2 files changed, 147 insertions(+), 145 deletions(-)
>>
>> int security_read_policy(struct selinux_state *state,
>> void **data, size_t *len)
>> {
>> struct selinux_policy *policy;
>>
>> policy = rcu_dereference_protected(
>> state->policy,
>> lockdep_is_held(&state->policy_mutex));
>> if (!policy)
>> return -EINVAL;
>> ...
>> }
>>
>> If "policy_mutex" is not held by the caller of security_read_policy() I
>> was expecting the above rcu_dereference_protected() call to return NULL,
>
> No, that's not how rcu_dereference_protected() works. You should only
> call it when you know for sure that you are in a section that is
> mutually exclusive with anything that might change the pointer. The
> check argument is only used for sanity checking that this is indeed
> true, but other than triggering a warning when RCU debugging is
> enabled the result of the check is ignored.
>
> If the returned pointer is NULL, it just means that the pointer hasn't
> been assigned yet, i.e. that no policy has been loaded yet (this was
> checked using selinux_initialized() before, but when we're under
> policy_mutex, checking the pointer for NULL is possible and simpler).
>
> BTW, you should've replied to [1], which is the merged patch that
> introduced the code you are referencing :)
Thanks for clarifying Ondrej.
-lakshmi
prev parent reply other threads:[~2020-09-07 17:18 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-25 15:20 [RFC PATCH 0/3] selinux: RCU conversion follow-ups Ondrej Mosnacek
2020-08-25 15:20 ` [RFC PATCH 1/3] selinux: simplify away security_policydb_len() Ondrej Mosnacek
2020-08-25 16:02 ` Stephen Smalley
2020-08-25 16:48 ` Ondrej Mosnacek
2020-08-25 15:20 ` [RFC PATCH 2/3] selinux: remove the 'initialized' flag from selinux_state Ondrej Mosnacek
2020-08-25 16:06 ` Stephen Smalley
2020-08-25 17:20 ` Ondrej Mosnacek
2020-08-25 17:46 ` Stephen Smalley
2020-08-25 15:20 ` [RFC PATCH 3/3] selinux: track policy lifetime with refcount Ondrej Mosnacek
2020-08-25 16:45 ` Stephen Smalley
2020-08-25 17:30 ` Ondrej Mosnacek
2020-08-25 17:50 ` Paul E. McKenney
2020-08-25 18:51 ` peter enderborg
2020-09-05 21:33 ` Lakshmi Ramasubramanian
2020-09-07 7:47 ` Ondrej Mosnacek
2020-09-07 14:03 ` Lakshmi Ramasubramanian [this message]
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=41bc40c8-e2b5-8535-5d72-ebe66e2ffb5d@linux.microsoft.com \
--to=nramas@linux.microsoft.com \
--cc=omosnace@redhat.com \
--cc=paul@paul-moore.com \
--cc=paulmck@kernel.org \
--cc=rcu@vger.kernel.org \
--cc=selinux@vger.kernel.org \
--cc=stephen.smalley.work@gmail.com \
/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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox