Linux Integrity Measurement development
 help / color / mirror / Atom feed
From: Mimi Zohar <zohar@linux.ibm.com>
To: "Guozihua (Scott)" <guozihua@huawei.com>,
	Paul Moore <paul@paul-moore.com>
Cc: "linux-integrity@vger.kernel.org"
	<linux-integrity@vger.kernel.org>,
	selinux@vger.kernel.org,
	"xiujianfeng@huawei.com" <xiujianfeng@huawei.com>,
	luhuaxin <luhuaxin1@huawei.com>
Subject: Re: Race conditioned discovered between ima_match_rules and ima_update_lsm_update_rules
Date: Wed, 17 Aug 2022 15:26:45 -0400	[thread overview]
Message-ID: <1309f1ee6fafe75f9f25b2d936171c0c0d2a5fd1.camel@linux.ibm.com> (raw)
In-Reply-To: <283a9142-f9e5-24b9-808c-f980343acaa7@huawei.com>

On Wed, 2022-08-17 at 15:20 +0800, Guozihua (Scott) wrote:
> On 2022/8/17 15:17, Guozihua (Scott) wrote:
> > On 2022/8/16 6:23, Paul Moore wrote:
> >> On Sun, Aug 14, 2022 at 2:30 PM Mimi Zohar <zohar@linux.ibm.com> wrote:
> >>>
> >>> Hi Scott, Paul,
> >>>
> >>> On Tue, 2022-08-09 at 12:24 -0400, Paul Moore wrote:
> >>>> On Sun, Aug 7, 2022 at 11:19 PM Guozihua (Scott) 
> >>>> <guozihua@huawei.com> wrote:
> >>>>>
> >>>>> On 2022/8/8 11:02, Guozihua (Scott) wrote:
> >>>>>> Hi Community,
> >>>>>>
> >>>>>> Recently we discovered a race condition while updating SELinux policy
> >>>>>> with IMA lsm rule enabled. Which would lead to extra files being 
> >>>>>> measured.
> >>>>>>
> >>>>>> While SELinux policy is updated, the IDs for object types and such 
> >>>>>> would
> >>>>>> be changed, and ima_lsm_update_rules would be called.
> >>>>>>
> >>>>>> There are no lock applied in ima_lsm_update_rules. If user accesses a
> >>>>>> file during this time, ima_match_rules will be matching rules 
> >>>>>> based on
> >>>>>> old SELinux au_seqno resulting in selinux_audit_rule_match returning
> >>>>>> -ESTALE.
> >>>>>>
> >>>>>> However, in ima_match_rules, this error number is not handled, 
> >>>>>> causing
> >>>>>> IMA to think the LSM rule is also a match, leading to measuring extra
> >>>>>> files.
> >>>>
> >>>> ...
> >>>>
> >>>>>> Is this the intended behavior? Or is it a good idea to add a lock for
> >>>>>> LSM rules during update?
> >>>>
> >>>> I'm not the IMA expert here, but a lot of effort has been into the
> >>>> SELinux code to enable lockless/RCU SELinux policy access and I
> >>>> *really* don't want to have to backtrack on that.
> >>>
> >>> IMA initially updated it's reference to the SELinux label ids lazily.
> >>> More recently IMA refreshes the LSM label ids based on
> >>> register_blocking_lsm_notifier().  As a result of commit 9ad6e9cb39c6
> >>> ("selinux: fix race between old and new sidtab"), -ESTALE is now being
> >>> returned.
> >>
> >> To be clear, are you seeing this only started happening after commit
> >> 9ad6e9cb39c6?  If that is the case, I would suggest a retry loop
> >> around ima_filter_rule_match() when -ESTALE is returned.  I believe
> >> that should resolve the problem, if not please let us know.
> > 
> > Hi Mimi and Paul
> > 
> > It seems that selinux_audit_rule_match has been returning -ESTALE for a 
> > very long time. It dates back to 376bd9cb357ec.
> > 
> > IMA used to have a retry mechanism, but it was removed by b16942455193 
> > ("ima: use the lsm policy update notifier"). Maybe we should consider 
> > bring it back or just add a lock in ima_lsm_update_rules().
> > 
> > FYI, once ima received the notification, it starts updating all it's lsm 
> > rules one-by-one. During this time, calling ima_match_rules on any rule 
> > that is not yet updated would return -ESTALE.
> 
> I mean a retry might still be needed in ima_match_rules(), but not the 
> ima_lsm_update_rules().

Ok.  So eventually the LSM label ids are properly updated.  Did adding
a retry loop around ima_filter_rule_match(), as Paul suggested, resolve
the problem?

thanks,

Mimi


  reply	other threads:[~2022-08-17 19:27 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-08  3:02 Race conditioned discovered between ima_match_rules and ima_update_lsm_update_rules Guozihua (Scott)
2022-08-08  3:19 ` Guozihua (Scott)
2022-08-09 16:24   ` Paul Moore
2022-08-14 18:30     ` Mimi Zohar
2022-08-15 22:23       ` Paul Moore
2022-08-17  7:17         ` Guozihua (Scott)
2022-08-17  7:20           ` Guozihua (Scott)
2022-08-17 19:26             ` Mimi Zohar [this message]
2022-08-17 20:49               ` Paul Moore
2022-08-18  1:12                 ` Guozihua (Scott)

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=1309f1ee6fafe75f9f25b2d936171c0c0d2a5fd1.camel@linux.ibm.com \
    --to=zohar@linux.ibm.com \
    --cc=guozihua@huawei.com \
    --cc=linux-integrity@vger.kernel.org \
    --cc=luhuaxin1@huawei.com \
    --cc=paul@paul-moore.com \
    --cc=selinux@vger.kernel.org \
    --cc=xiujianfeng@huawei.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