diff mbox

[0/1] EVM/IMA xattr hardening and unprivileged user ns mounts

Message ID 20160901132250.GB145647@ubuntu-hedt (mailing list archive)
State New, archived
Headers show

Commit Message

Seth Forshee Sept. 1, 2016, 1:22 p.m. UTC
On Tue, Aug 02, 2016 at 02:26:29PM -0500, Eric W. Biederman wrote:
> Mimi Zohar <zohar@linux.vnet.ibm.com> writes:
> 
> > On Di, 2016-08-02 at 09:09 -0500, Eric W. Biederman wrote:
> >> Mimi Zohar <zohar@linux.vnet.ibm.com> writes:
> >> 
> >> > On Mo, 2016-08-01 at 15:38 -0500, Eric W. Biederman wrote:
> >> >> Mimi Zohar <zohar@linux.vnet.ibm.com> writes:
> >> >> 
> >> >> > On Mo, 2016-08-01 at 08:19 -0500, Seth Forshee wrote:
> >> >> >> I was taking a look at IMA and EVM in the context of allowing mounts in
> >> >> >> user namespaces from users with only capabilities in that namespace
> >> >> >> (i.e. no global capabilities). As a result of that I found just a
> >> >> >> handful of places where the size of an xattr or the values it contains
> >> >> >> lack sufficient validation to protect against malicious xattrs. The
> >> >> >> fixes for these are worthwhile on their own, so I'm sending them in the
> >> >> >> following patch.
> >> >> >> 
> >> >> >> More generally though I'd like to request some guidance on how IMA and
> >> >> >> EVM should handle xattrs in mounts from globally-unprivileged users. For
> >> >> >> context, the intended use case for this is allowing fuse mounts in user
> >> >> >> namespace containers, and possibly mounts with a limited set of other
> >> >> >> filesystems in the future. All that would actually be needed to mount
> >> >> >> though are private user and mount namespaces.
> >> >> >> 
> >> >> >> Based on what I see, I suspect that we can treat the user ns mounts like
> >> >> >> any other mount. I don't see that this would cause harm as long as the
> >> >> >> filesystem can't trigger bugs in the xattr parsing, and if the system is
> >> >> >> configured for IMA enforcment this policy should also be applied to
> >> >> >> these mounts.
> >> >> >
> >> >> > "security.evm" is written as a result of one of the protected security
> >> >> > xattrs being written.   From your question, I assume that in this use
> >> >> > case scenario there are two concerns:
> >> >> > - parsing improper security.evm xattrs
> >> >> > - updating the protected security xattrs, causing the EVM xattr to
> >> >> > "bless" the change.
> >> >> >
> >> >> > The first issue you're addressing with the patch.  As for the second
> >> >> > issue,  as long as root in the namespace can not write security.xattrs,
> >> >> > there shouldn't be a problem.
> >> >> 
> >> >> Assume that it is allowed that root in a namespace will be allowed to
> >> >> write security xattrs for a filesystem it has mounted.  Certainly for
> >> >> the capability xattr this makes sense.
> >> >
> >> > Yeah, I remember.  Having one capability xattr sounded interesting.
> >> > Have the capability patches been upstreamed?   To label files in a
> >> > container, root in the namespace will need to be able to write an IMA
> >> > xattr.  Whether it is possible to use a single xattr, is yet to be
> >> > determined.
> >
> >> For the most part yes.  As part of this merge window Linus
> >> merged the changes to store s_user_ns on a super block and the code to
> >> use that.
> >
> > Great!  I'll take a look.
> >
> >> I have kept back the code to relax the permission checks in the obvious
> >> cases.  There is little immediate gain from that code and I noticed a
> >> few places where s_user_ns is not being handled properly.  Primarily
> >> IMA and EVM (so this disucssion).
> >
> > Ok
> >
> >> >> If understand you correctly is that the key used to sign the HMAC in
> >> >> security.evm is machine local.  As such I expect the proper semantics
> >> >> here will be to not update the security.evm xattrs on write of a
> >> >> filesystem with s_user_ns != &init_user_ns.
> >> >
> >> > The EVM xattr is verified before it can be changed, to prevent making an
> >> > existing invalid hmac, valid.  If the namespace has the ability to
> >> > change any of the metadata included in the hmac, the EVM  hmac will need
> >> > to be updated with the file metadata, otherwise nobody will be able to
> >> > access the file.
> >> 
> >> To be clear with evm enabled and security.evm attributes present if
> >> the hmac is wrong access to the file is denied?
> >
> > Yes, if the file is included in the IMA policy and the EVM
> > hmac/signature verification fails, then access to the file is denied.
> 
> Makes sense.
> 
> >> What happens in the case of new files and in the case of file without a
> >> security.evm xattr?
> >
> > New files are a special case.  The first EVM protected xattr* written
> > will cause the security.evm xattr to be written, without first
> > validating the existing xattr. 
> >
> > *evm_config_xattrnames[] contains the list of protected xattrs.
> 
> Thank you.
> 
> >> >> On read I am not certain what the right thing to do is, ignore the
> >> >> attribute or do what happens on validation failure.
> >> >
> >> >> > Any changes made offline/elsewhere, would
> >> >> > result in the security.evm HMAC not verifying, causing a denial of
> >> >> > service.  Unless of course the EVM HMAC key has somehow been
> >> >> > leaked/exfiltrated from the kernel.
> >> >> 
> >> >> One scenario where this is likely to come up, and the kind of scenario
> >> >> we are planning for is a filesystem on a usb stick is transported from
> >> >> one machine to another.  One the second system it is mounted directly
> >> >> (ideally) or through fuse running a copy of it's filesystem driver in
> >> >> userspace to guard against filesystem implementation bugs.
> >> >> 
> >> >> So if that filesystem has security.evm xattrs if it appears things will
> >> >> work very poorly.
> >> >
> >> > It could work for files with EVM signatures, obviously not with an HMAC.
> >> 
> >> Fair.
> >> 
> >> What policy would you recommend for (evm and ima) dealing with
> >> filesystems that are mounted by a user namespace root and thus have
> >> sb->s_user_ns != &init_user_ns?
> >
> > I think as long as it can not escape the mount namespace, allowing it to
> > write both the EVM and IMA xattrs should be fine.  In terms of reading
> > the xattrs, real root would need to be able to differentiate the xattrs'
> > origin.
> 
> It is a little more subtle than that, but essentially it is true that
> a filesystem will not propogate into a mount namespace owned by root in
> more privileged user namespace.
> 
> The piece of orgin that is retained in the kernel for a filesystem is
> sb->s_user_ns.  So the information is there.  It is not currently
> exported explicitly to userspace, but who owns the mount namespace a
> filesystem is in, is a big clue.
> 
> But yes any reader knows which filesystem an xattr is coming from so
> this should not be a point of confusion.
> 
> >> Like Seth I am not much of an expert in the semantics of IMA and EVM.
> >> I just know that ima calcualtes a hash of a file and does something with
> >> it.  EVM caculcates a hash of other xattrs and the classic file
> >> attributes.
> >
> > EVM protects file metadata.  For immutable files, such as ELF
> > executables, the security.evm should contain a file signature.  For
> > mutable files, security.evm should contain an hmac.
> 
> You know I am not certain what the distinction between a file signature
> and a hmac is in this context.

I've been reading back through all of this, and I'm not sure any
conclusion was reached.

For my part, I'm uneasy about writing out hmacs to mounts from a
non-root user. So I'll make a proposal - let's not read or write EVM or
IMA xattrs for filesystems from non-init user namespace, essentially
behaving as though they don't support xattrs. Something like the
(untested) patch below.

Thanks,
Seth


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Mimi Zohar Sept. 6, 2016, 8 p.m. UTC | #1
On Thu, 2016-09-01 at 08:22 -0500, Seth Forshee wrote:

> I've been reading back through all of this, and I'm not sure any
> conclusion was reached.

> For my part, I'm uneasy about writing out hmacs to mounts from a
> non-root user. So I'll make a proposal - let's not read or write EVM or
> IMA xattrs for filesystems from non-init user namespace, essentially
> behaving as though they don't support xattrs. Something like the
> (untested) patch below.

This really doesn't sound like the right long term solution.  The
kernel, as well as root in the namespace, should write the EVM and IMA
security xattrs, as long as their usage is limited to the uid/gid
associated with that user_ns namespace.

In terms of the USB stick scenario, instead of security.evm containing
HMACs, they would need to be replaced with signatures, before using it
on another system.  Refer to the ima-evm-utils package for writing out
security.evm signatures.

Mimi

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Seth Forshee Sept. 6, 2016, 8:52 p.m. UTC | #2
On Tue, Sep 06, 2016 at 04:00:32PM -0400, Mimi Zohar wrote:
> On Thu, 2016-09-01 at 08:22 -0500, Seth Forshee wrote:
> 
> > I've been reading back through all of this, and I'm not sure any
> > conclusion was reached.
> 
> > For my part, I'm uneasy about writing out hmacs to mounts from a
> > non-root user. So I'll make a proposal - let's not read or write EVM or
> > IMA xattrs for filesystems from non-init user namespace, essentially
> > behaving as though they don't support xattrs. Something like the
> > (untested) patch below.
> 
> This really doesn't sound like the right long term solution.

I'm not proposing this for a long term solution. I just haven't seen
that anyone has a real use case in mind yet for using the integrity
subsystem with these mounts and that it might be better to wait and
decide exactly how it should behave until someone does. In the mean time
I'm just trying to ensure there won't be vulnerabilities to exploit.

> The kernel, as well as root in the namespace, should write the EVM and
> IMA security xattrs, as long as their usage is limited to the uid/gid
> associated with that user_ns namespace.

But keep in mind that the uid mapping doesn't get written out to disk.
If I create a file owned by uid 0 in my namespace (let's say that maps
to uid 1000 in init_user_ns) then uid 0 is what will be written to the
inode on disk.

Based on my understanding of what you said previously, this means that I
could create a file in my filesystem as root in my user ns and write out
a security.capability xattr, and the kernel would then write out an EVM
xattr with a valid signature. If I can then manage to get that file
mounted in init_user_ns the kernel will be owned by real root and the
kernel will see a valid signature for the capability xattr.

Granted, this scenario is problematic even without EVM, but it seems
like specifically the kind of thing someone might use EVM to protect
against.

Thanks,
Seth

> In terms of the USB stick scenario, instead of security.evm containing
> HMACs, they would need to be replaced with signatures, before using it
> on another system.  Refer to the ima-evm-utils package for writing out
> security.evm signatures.
> 
> Mimi
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Mimi Zohar Sept. 6, 2016, 9:28 p.m. UTC | #3
On Tue, 2016-09-06 at 15:52 -0500, Seth Forshee wrote:
> On Tue, Sep 06, 2016 at 04:00:32PM -0400, Mimi Zohar wrote:
> > On Thu, 2016-09-01 at 08:22 -0500, Seth Forshee wrote:
> > 
> > > I've been reading back through all of this, and I'm not sure any
> > > conclusion was reached.
> > 
> > > For my part, I'm uneasy about writing out hmacs to mounts from a
> > > non-root user. So I'll make a proposal - let's not read or write EVM or
> > > IMA xattrs for filesystems from non-init user namespace, essentially
> > > behaving as though they don't support xattrs. Something like the
> > > (untested) patch below.
> > 
> > This really doesn't sound like the right long term solution.
> 
> I'm not proposing this for a long term solution. I just haven't seen
> that anyone has a real use case in mind yet for using the integrity
> subsystem with these mounts and that it might be better to wait and
> decide exactly how it should behave until someone does. In the mean time
> I'm just trying to ensure there won't be vulnerabilities to exploit.

The main use case will be installing/updating packages within a
container and writing the corresponding file signatures.  Unfortunately,
we're not there yet.

> > The kernel, as well as root in the namespace, should write the EVM and
> > IMA security xattrs, as long as their usage is limited to the uid/gid
> > associated with that user_ns namespace.
> 
> But keep in mind that the uid mapping doesn't get written out to disk.
> If I create a file owned by uid 0 in my namespace (let's say that maps
> to uid 1000 in init_user_ns) then uid 0 is what will be written to the
> inode on disk.

oh!

> Based on my understanding of what you said previously, this means that I
> could create a file in my filesystem as root in my user ns and write out
> a security.capability xattr, and the kernel would then write out an EVM
> xattr with a valid signature. If I can then manage to get that file
> mounted in init_user_ns the kernel will be owned by real root and the
> kernel will see a valid signature for the capability xattr.

I see.

> Granted, this scenario is problematic even without EVM, but it seems
> like specifically the kind of thing someone might use EVM to protect
> against.

Agreed.

Mimi

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Seth Forshee Sept. 7, 2016, 12:27 p.m. UTC | #4
On Tue, Sep 06, 2016 at 05:28:49PM -0400, Mimi Zohar wrote:
> On Tue, 2016-09-06 at 15:52 -0500, Seth Forshee wrote:
> > On Tue, Sep 06, 2016 at 04:00:32PM -0400, Mimi Zohar wrote:
> > > On Thu, 2016-09-01 at 08:22 -0500, Seth Forshee wrote:
> > > 
> > > > I've been reading back through all of this, and I'm not sure any
> > > > conclusion was reached.
> > > 
> > > > For my part, I'm uneasy about writing out hmacs to mounts from a
> > > > non-root user. So I'll make a proposal - let's not read or write EVM or
> > > > IMA xattrs for filesystems from non-init user namespace, essentially
> > > > behaving as though they don't support xattrs. Something like the
> > > > (untested) patch below.
> > > 
> > > This really doesn't sound like the right long term solution.
> > 
> > I'm not proposing this for a long term solution. I just haven't seen
> > that anyone has a real use case in mind yet for using the integrity
> > subsystem with these mounts and that it might be better to wait and
> > decide exactly how it should behave until someone does. In the mean time
> > I'm just trying to ensure there won't be vulnerabilities to exploit.
> 
> The main use case will be installing/updating packages within a
> container and writing the corresponding file signatures.  Unfortunately,
> we're not there yet.

Okay. If the distro is providing those signatures then I don't see a
problem with allowing them to be written. What concerns me is if there's
a scenario where the kernel would calculate and write out a signature or
hmac that would make the kernel trust the file in other contexts.

So when s_user_ns != &init_user_ns it sounds like we could allow the
kernel to read and verify signatures and write out signatures provided
by userspace, but not calculate new/updated signatures for files. Does
that sound reasonable?

Thanks,
Seth
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Mimi Zohar Sept. 7, 2016, 3:20 p.m. UTC | #5
On Wed, 2016-09-07 at 07:27 -0500, Seth Forshee wrote:
> On Tue, Sep 06, 2016 at 05:28:49PM -0400, Mimi Zohar wrote:
> > On Tue, 2016-09-06 at 15:52 -0500, Seth Forshee wrote:
> > > On Tue, Sep 06, 2016 at 04:00:32PM -0400, Mimi Zohar wrote:
> > > > On Thu, 2016-09-01 at 08:22 -0500, Seth Forshee wrote:
> > > > 
> > > > > I've been reading back through all of this, and I'm not sure any
> > > > > conclusion was reached.
> > > > 
> > > > > For my part, I'm uneasy about writing out hmacs to mounts from a
> > > > > non-root user. So I'll make a proposal - let's not read or write EVM or
> > > > > IMA xattrs for filesystems from non-init user namespace, essentially
> > > > > behaving as though they don't support xattrs. Something like the
> > > > > (untested) patch below.
> > > > 
> > > > This really doesn't sound like the right long term solution.
> > > 
> > > I'm not proposing this for a long term solution. I just haven't seen
> > > that anyone has a real use case in mind yet for using the integrity
> > > subsystem with these mounts and that it might be better to wait and
> > > decide exactly how it should behave until someone does. In the mean time
> > > I'm just trying to ensure there won't be vulnerabilities to exploit.
> > 
> > The main use case will be installing/updating packages within a
> > container and writing the corresponding file signatures.  Unfortunately,
> > we're not there yet.
> 
> Okay. If the distro is providing those signatures then I don't see a
> problem with allowing them to be written. What concerns me is if there's
> a scenario where the kernel would calculate and write out a signature or
> hmac that would make the kernel trust the file in other contexts.

There's really no way of differentiating the source of the file
signatures.  We either allow root in the namespace to write them or not.

> So when s_user_ns != &init_user_ns it sounds like we could allow the
> kernel to read and verify signatures and write out signatures provided
> by userspace, but not calculate new/updated signatures for files. Does
> that sound reasonable?

Not really.  At least initially, perhaps we should be differentiating
between EVM and IMA xattrs, allowing only IMA xattrs to be read/written
and returning EOPNOTSUPP, as you suggested, for EVM.  Having just IMA
xattrs, without EVM xattrs, would then work in the namespace, but not
outside of it (assuming EVM is enabled).

Mimi

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Seth Forshee Sept. 7, 2016, 4:01 p.m. UTC | #6
On Wed, Sep 07, 2016 at 11:20:19AM -0400, Mimi Zohar wrote:
> On Wed, 2016-09-07 at 07:27 -0500, Seth Forshee wrote:
> > On Tue, Sep 06, 2016 at 05:28:49PM -0400, Mimi Zohar wrote:
> > > On Tue, 2016-09-06 at 15:52 -0500, Seth Forshee wrote:
> > > > On Tue, Sep 06, 2016 at 04:00:32PM -0400, Mimi Zohar wrote:
> > > > > On Thu, 2016-09-01 at 08:22 -0500, Seth Forshee wrote:
> > > > > 
> > > > > > I've been reading back through all of this, and I'm not sure any
> > > > > > conclusion was reached.
> > > > > 
> > > > > > For my part, I'm uneasy about writing out hmacs to mounts from a
> > > > > > non-root user. So I'll make a proposal - let's not read or write EVM or
> > > > > > IMA xattrs for filesystems from non-init user namespace, essentially
> > > > > > behaving as though they don't support xattrs. Something like the
> > > > > > (untested) patch below.
> > > > > 
> > > > > This really doesn't sound like the right long term solution.
> > > > 
> > > > I'm not proposing this for a long term solution. I just haven't seen
> > > > that anyone has a real use case in mind yet for using the integrity
> > > > subsystem with these mounts and that it might be better to wait and
> > > > decide exactly how it should behave until someone does. In the mean time
> > > > I'm just trying to ensure there won't be vulnerabilities to exploit.
> > > 
> > > The main use case will be installing/updating packages within a
> > > container and writing the corresponding file signatures.  Unfortunately,
> > > we're not there yet.
> > 
> > Okay. If the distro is providing those signatures then I don't see a
> > problem with allowing them to be written. What concerns me is if there's
> > a scenario where the kernel would calculate and write out a signature or
> > hmac that would make the kernel trust the file in other contexts.
> 
> There's really no way of differentiating the source of the file
> signatures.  We either allow root in the namespace to write them or not.

I must be misunderstanding something then.

My impression was that for EVM we'd be dealing with some kind of keyed
signatures, not just hashes. In that case a user without the correct key
could write the EVM xattrs, then if the signature was correct the kernel
would permit access to the file, otherwise it would not. I.e. if the
user was installing distro packages which shipped with correct
signatures, the user could write those and the kernel would validate
them as correct. But if the user tried to write out their own files with
security xattrs they could not generate valid signatures, and the kernel
would deny access to those files.

Is something about that incorrect?

> > So when s_user_ns != &init_user_ns it sounds like we could allow the
> > kernel to read and verify signatures and write out signatures provided
> > by userspace, but not calculate new/updated signatures for files. Does
> > that sound reasonable?
> 
> Not really.  At least initially, perhaps we should be differentiating
> between EVM and IMA xattrs, allowing only IMA xattrs to be read/written
> and returning EOPNOTSUPP, as you suggested, for EVM.  Having just IMA
> xattrs, without EVM xattrs, would then work in the namespace, but not
> outside of it (assuming EVM is enabled).

That's okay with me assuming it doesn't weaken security for systems with
EVM enabled. We're already effectively ignoring LSM security xattrs and
scoping capability xattrs to s_user_ns, so I think we should be okay.

Thanks,
Seth
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Mimi Zohar Sept. 7, 2016, 5:06 p.m. UTC | #7
On Wed, 2016-09-07 at 11:01 -0500, Seth Forshee wrote:
> On Wed, Sep 07, 2016 at 11:20:19AM -0400, Mimi Zohar wrote:
> > On Wed, 2016-09-07 at 07:27 -0500, Seth Forshee wrote:
> > > On Tue, Sep 06, 2016 at 05:28:49PM -0400, Mimi Zohar wrote:
> > > > On Tue, 2016-09-06 at 15:52 -0500, Seth Forshee wrote:
> > > > > On Tue, Sep 06, 2016 at 04:00:32PM -0400, Mimi Zohar wrote:
> > > > > > On Thu, 2016-09-01 at 08:22 -0500, Seth Forshee wrote:
> > > > > > 
> > > > > > > I've been reading back through all of this, and I'm not sure any
> > > > > > > conclusion was reached.
> > > > > > 
> > > > > > > For my part, I'm uneasy about writing out hmacs to mounts from a
> > > > > > > non-root user. So I'll make a proposal - let's not read or write EVM or
> > > > > > > IMA xattrs for filesystems from non-init user namespace, essentially
> > > > > > > behaving as though they don't support xattrs. Something like the
> > > > > > > (untested) patch below.
> > > > > > 
> > > > > > This really doesn't sound like the right long term solution.
> > > > > 
> > > > > I'm not proposing this for a long term solution. I just haven't seen
> > > > > that anyone has a real use case in mind yet for using the integrity
> > > > > subsystem with these mounts and that it might be better to wait and
> > > > > decide exactly how it should behave until someone does. In the mean time
> > > > > I'm just trying to ensure there won't be vulnerabilities to exploit.
> > > > 
> > > > The main use case will be installing/updating packages within a
> > > > container and writing the corresponding file signatures.  Unfortunately,
> > > > we're not there yet.
> > > 
> > > Okay. If the distro is providing those signatures then I don't see a
> > > problem with allowing them to be written. What concerns me is if there's
> > > a scenario where the kernel would calculate and write out a signature or
> > > hmac that would make the kernel trust the file in other contexts.
> > 
> > There's really no way of differentiating the source of the file
> > signatures.  We either allow root in the namespace to write them or not.
> 
> I must be misunderstanding something then.

By file signature, I meant file data signature, which refers to IMA (not
EVM).  

> My impression was that for EVM we'd be dealing with some kind of keyed
> signatures, not just hashes. In that case a user without the correct key
> could write the EVM xattrs, then if the signature was correct the kernel
> would permit access to the file, otherwise it would not.

Right, except the EVM signature is not portable, as it includes the
inode in the signature.  Our work for including file signatures in
packages is limited to the IMA file data signatures.  A new portable EVM
format will be need for including EVM signatures in packages.

>  I.e. if the
> user was installing distro packages which shipped with correct
> signatures, the user could write those and the kernel would validate
> them as correct. But if the user tried to write out their own files with
> security xattrs they could not generate valid signatures, and the kernel
> would deny access to those files.

> Is something about that incorrect?

That sounds right.  Userspace can write EVM signatures, using the
ima-evm-utils package.  The asymmetric public key used for verifying the
signature, would need to be loaded onto the EVM keyring.

> > > So when s_user_ns != &init_user_ns it sounds like we could allow the
> > > kernel to read and verify signatures and write out signatures provided
> > > by userspace, but not calculate new/updated signatures for files. Does
> > > that sound reasonable?
> > 
> > Not really.  At least initially, perhaps we should be differentiating
> > between EVM and IMA xattrs, allowing only IMA xattrs to be read/written
> > and returning EOPNOTSUPP, as you suggested, for EVM.  Having just IMA
> > xattrs, without EVM xattrs, would then work in the namespace, but not
> > outside of it (assuming EVM is enabled).
> 
> That's okay with me assuming it doesn't weaken security for systems with
> EVM enabled. We're already effectively ignoring LSM security xattrs and
> scoping capability xattrs to s_user_ns, so I think we should be okay.

The worst, which would be bad, is preventing access to a file, but it
wouldn't make an invalid EVM signature/hmac valid.

Mimi

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Seth Forshee Sept. 7, 2016, 6:34 p.m. UTC | #8
On Wed, Sep 07, 2016 at 01:06:10PM -0400, Mimi Zohar wrote:
> On Wed, 2016-09-07 at 11:01 -0500, Seth Forshee wrote:
> > On Wed, Sep 07, 2016 at 11:20:19AM -0400, Mimi Zohar wrote:
> > > On Wed, 2016-09-07 at 07:27 -0500, Seth Forshee wrote:
> > > > On Tue, Sep 06, 2016 at 05:28:49PM -0400, Mimi Zohar wrote:
> > > > > On Tue, 2016-09-06 at 15:52 -0500, Seth Forshee wrote:
> > > > > > On Tue, Sep 06, 2016 at 04:00:32PM -0400, Mimi Zohar wrote:
> > > > > > > On Thu, 2016-09-01 at 08:22 -0500, Seth Forshee wrote:
> > > > > > > 
> > > > > > > > I've been reading back through all of this, and I'm not sure any
> > > > > > > > conclusion was reached.
> > > > > > > 
> > > > > > > > For my part, I'm uneasy about writing out hmacs to mounts from a
> > > > > > > > non-root user. So I'll make a proposal - let's not read or write EVM or
> > > > > > > > IMA xattrs for filesystems from non-init user namespace, essentially
> > > > > > > > behaving as though they don't support xattrs. Something like the
> > > > > > > > (untested) patch below.
> > > > > > > 
> > > > > > > This really doesn't sound like the right long term solution.
> > > > > > 
> > > > > > I'm not proposing this for a long term solution. I just haven't seen
> > > > > > that anyone has a real use case in mind yet for using the integrity
> > > > > > subsystem with these mounts and that it might be better to wait and
> > > > > > decide exactly how it should behave until someone does. In the mean time
> > > > > > I'm just trying to ensure there won't be vulnerabilities to exploit.
> > > > > 
> > > > > The main use case will be installing/updating packages within a
> > > > > container and writing the corresponding file signatures.  Unfortunately,
> > > > > we're not there yet.
> > > > 
> > > > Okay. If the distro is providing those signatures then I don't see a
> > > > problem with allowing them to be written. What concerns me is if there's
> > > > a scenario where the kernel would calculate and write out a signature or
> > > > hmac that would make the kernel trust the file in other contexts.
> > > 
> > > There's really no way of differentiating the source of the file
> > > signatures.  We either allow root in the namespace to write them or not.
> > 
> > I must be misunderstanding something then.
> 
> By file signature, I meant file data signature, which refers to IMA (not
> EVM).  

Okay. It does get a little confusing.

> > My impression was that for EVM we'd be dealing with some kind of keyed
> > signatures, not just hashes. In that case a user without the correct key
> > could write the EVM xattrs, then if the signature was correct the kernel
> > would permit access to the file, otherwise it would not.
> 
> Right, except the EVM signature is not portable, as it includes the
> inode in the signature.  Our work for including file signatures in
> packages is limited to the IMA file data signatures.  A new portable EVM
> format will be need for including EVM signatures in packages.
> 
> >  I.e. if the
> > user was installing distro packages which shipped with correct
> > signatures, the user could write those and the kernel would validate
> > them as correct. But if the user tried to write out their own files with
> > security xattrs they could not generate valid signatures, and the kernel
> > would deny access to those files.
> 
> > Is something about that incorrect?
> 
> That sounds right.  Userspace can write EVM signatures, using the
> ima-evm-utils package.  The asymmetric public key used for verifying the
> signature, would need to be loaded onto the EVM keyring.

Got it. Sounds like there currently wouldn't be any benefit to letting
ns-root write out EVM xattrs in that case. Of course we can't prevent
mounting a filesystem which already contains the xattrs, but we don't
have to make use of them.

> > > > So when s_user_ns != &init_user_ns it sounds like we could allow the
> > > > kernel to read and verify signatures and write out signatures provided
> > > > by userspace, but not calculate new/updated signatures for files. Does
> > > > that sound reasonable?
> > > 
> > > Not really.  At least initially, perhaps we should be differentiating
> > > between EVM and IMA xattrs, allowing only IMA xattrs to be read/written
> > > and returning EOPNOTSUPP, as you suggested, for EVM.  Having just IMA
> > > xattrs, without EVM xattrs, would then work in the namespace, but not
> > > outside of it (assuming EVM is enabled).
> > 
> > That's okay with me assuming it doesn't weaken security for systems with
> > EVM enabled. We're already effectively ignoring LSM security xattrs and
> > scoping capability xattrs to s_user_ns, so I think we should be okay.
> 
> The worst, which would be bad, is preventing access to a file, but it
> wouldn't make an invalid EVM signature/hmac valid.

Yeah, I've been taking another look at the code today and saw that was
the case. The only time I see the kernel writing out a new signature is
for IMA xattrs which are only digests and not signatures. So that seems
okay, and some of my concerns were unfounded.

I'll work up a patch then to deny reading and writing of EVM xattrs when
s_user_ns != &init_user_ns.

Thanks,
Seth
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
index 11c1d30bd705..5a1738524fbb 100644
--- a/security/integrity/evm/evm_crypto.c
+++ b/security/integrity/evm/evm_crypto.c
@@ -182,7 +182,7 @@  static int evm_calc_hmac_or_hash(struct dentry *dentry,
 	int error;
 	int size;
 
-	if (!inode->i_op->getxattr)
+	if (inode->i_sb->s_user_ns != &init_user_ns || !inode->i_op->getxattr)
 		return -EOPNOTSUPP;
 	desc = init_desc(type);
 	if (IS_ERR(desc))
diff --git a/security/integrity/evm/evm_main.c b/security/integrity/evm/evm_main.c
index 35ab453ce861..3d16e4c523c4 100644
--- a/security/integrity/evm/evm_main.c
+++ b/security/integrity/evm/evm_main.c
@@ -78,7 +78,7 @@  static int evm_find_protected_xattrs(struct dentry *dentry)
 	int error;
 	int count = 0;
 
-	if (!inode->i_op->getxattr)
+	if (inode->i_sb->s_user_ns != &init_user_ns || !inode->i_op->getxattr)
 		return -EOPNOTSUPP;
 
 	for (xattr = evm_config_xattrnames; *xattr != NULL; xattr++) {
@@ -118,6 +118,9 @@  static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	int rc, xattr_len;
 
+	if (d_backing_inode(dentry)->i_sb->s_user_ns != &init_user_ns)
+		return INTEGRITY_UNKNOWN;
+
 	if (iint && iint->evm_status == INTEGRITY_PASS)
 		return iint->evm_status;
 
diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c
index a13fc6809554..7f6915a4f660 100644
--- a/security/integrity/ima/ima_appraise.c
+++ b/security/integrity/ima/ima_appraise.c
@@ -50,6 +50,9 @@  static int ima_fix_xattr(struct dentry *dentry,
 	int rc, offset;
 	u8 algo = iint->ima_hash->algo;
 
+	if (d_backing_inode(dentry)->i_sb->s_user_ns != &init_user_ns)
+		return -EOPNOTSUPP;
+
 	if (algo <= HASH_ALGO_SHA1) {
 		offset = 1;
 		iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
@@ -170,7 +173,7 @@  int ima_read_xattr(struct dentry *dentry,
 {
 	struct inode *inode = d_backing_inode(dentry);
 
-	if (!inode->i_op->getxattr)
+	if (inode->i_sb->s_user_ns != &init_user_ns || !inode->i_op->getxattr)
 		return 0;
 
 	return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
@@ -198,7 +201,7 @@  int ima_appraise_measurement(enum ima_hooks func,
 	enum integrity_status status = INTEGRITY_UNKNOWN;
 	int rc = xattr_len, hash_start = 0;
 
-	if (!inode->i_op->getxattr)
+	if (inode->i_sb->s_user_ns != &init_user_ns || !inode->i_op->getxattr)
 		return INTEGRITY_UNKNOWN;
 
 	if (rc <= 0) {