Skip to contentSkip to conversation

The problem with TUN/TAP devices

DISCUSSION Paul Moore 5 participants 15 messages Jun 30, 2009 – Jul 2, 2009 SELinux security module

lore
Share & tools
lore

Paul Moore

author
#0 lore raw

Unfortunately we have a problem with the network access controls and TUN/TAP devices. The basic issue is that packets entering the stack via a TUN device, e.g. QEMU/KVM guest instance operating with a bridged network configuration, do not have a fully initialized sock associated with them. I say "fully initialized" because the basic initialization has been done (memory allocated, initial values set to SECINITSID_UNLABELED, etc.) but the last step where we assign the sock a label/SID never happens. Why? Because the TUN driver code only calls sk_alloc() and nothing else in the TUN code paths finish the SELinux sock setup.

Okay, so what? Well, the problem is that the SELinux IP postrouting code treats the packet's sock label (the one that is still set as unlabeled_t in the TUN case) as the originating peer label; in short it looks like packets sent from your QEMU/KVM instance are unlabeled_t instead of my_guest_t:s3. Needless to say this is not ideal.

So how do we fix it? Well, there are a two options that I can think of right now (feel free to add to the list):

1. Set the sock's label/SID in sk_alloc()
2. Introduce a new hook to set the label/SID of a sock and call it from tun_set_iff()

The problem with #2 is that it introduces a new (basically TUN specific) hook to do something silly. Important, but still kinda silly. The problem with #1 is that we currently set the sock's label/SID in selinux_socket_post_create() and match it with the inode's label/SID which has the potential to get ugly (I haven't verified all of those cases yet). However, there may be an alternative, call it #1a, where set label the sock in sk_alloc() and then use the sock's label to set the inode's label in socket_post_create(); this should solve the potential ugliness.

Thoughts?

There is also a somewhat related issue involving persistent TUN/TAP devices but I'd like to resolve this before getting deeper into that problem.

Signature · 8 lines
paul moore
linux @ hp


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#1 lore raw
Quoted 16 lines · Paul Moore

So how do we fix it? Well, there are a two options that I can think of right now (feel free to add to the list):

1. Set the sock's label/SID in sk_alloc()
2. Introduce a new hook to set the label/SID of a sock and call it from tun_set_iff()

The problem with #2 is that it introduces a new (basically TUN specific) hook to do something silly. Important, but still kinda silly. The problem with #1 is that we currently set the sock's label/SID in selinux_socket_post_create() and match it with the inode's label/SID which has the potential to get ugly (I haven't verified all of those cases yet). However, there may be an alternative, call it #1a, where set label the sock in sk_alloc() and then use the sock's label to set the inode's label in socket_post_create(); this should solve the potential ugliness.

Quoted 1 line

Thoughts?

I'm not sure, but we probably need to include the netdev list in the discussion.

Signature · 7 lines
James Morris
<[email protected]>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#3 lore raw
In reply to Paul Moore
Quoted 10 lines · Paul Moore

Unfortunately we have a problem with the network access controls and TUN/TAP devices. The basic issue is that packets entering the stack via a TUN device, e.g. QEMU/KVM guest instance operating with a bridged network configuration, do not have a fully initialized sock associated with them. I say "fully initialized" because the basic initialization has been done (memory allocated, initial values set to SECINITSID_UNLABELED, etc.) but the last step where we assign the sock a label/SID never happens. Why? Because the TUN driver code only calls sk_alloc() and nothing else in the TUN code paths finish the SELinux sock setup.

So what should it be calling and why is the fact that it isn't not a bug in the TUN driver?

Quoted 22 lines

Okay, so what? Well, the problem is that the SELinux IP postrouting code treats the packet's sock label (the one that is still set as unlabeled_t in the TUN case) as the originating peer label; in short it looks like packets sent from your QEMU/KVM instance are unlabeled_t instead of my_guest_t:s3. Needless to say this is not ideal.

So how do we fix it? Well, there are a two options that I can think of right now (feel free to add to the list):

1. Set the sock's label/SID in sk_alloc()
2. Introduce a new hook to set the label/SID of a sock and call it from tun_set_iff()

The problem with #2 is that it introduces a new (basically TUN specific) hook to do something silly. Important, but still kinda silly. The problem with #1 is that we currently set the sock's label/SID in selinux_socket_post_create() and match it with the inode's label/SID which has the potential to get ugly (I haven't verified all of those cases yet). However, there may be an alternative, call it #1a, where set label the sock in sk_alloc() and then use the sock's label to set the inode's label in socket_post_create(); this should solve the potential ugliness.

Quoted 3 lines

Thoughts?

As this would appear to be a flaw in the TUN driver, get the TUN developers to fix their broken driver. I certainly dislike a special purpose LSM hook for this.

Do you see this as a problem for all users of labeled networking, or is it isolated to SELinux?

Quoted 4 lines

There is also a somewhat related issue involving persistent TUN/TAP devices but I'd like to resolve this before getting deeper into that problem.

Oh joy.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#5 lore raw
In reply to Paul Moore
Quoted 16 lines · Paul Moore

Unfortunately we have a problem with the network access controls and TUN/TAP devices. The basic issue is that packets entering the stack via a TUN device, e.g. QEMU/KVM guest instance operating with a bridged network configuration, do not have a fully initialized sock associated with them. I say "fully initialized" because the basic initialization has been done (memory allocated, initial values set to SECINITSID_UNLABELED, etc.) but the last step where we assign the sock a label/SID never happens. Why? Because the TUN driver code only calls sk_alloc() and nothing else in the TUN code paths finish the SELinux sock setup.

Okay, so what? Well, the problem is that the SELinux IP postrouting code treats the packet's sock label (the one that is still set as unlabeled_t in the TUN case) as the originating peer label; in short it looks like packets sent from your QEMU/KVM instance are unlabeled_t instead of my_guest_t:s3. Needless to say this is not ideal.

Quoted 4 lines

So how do we fix it? Well, there are a two options that I can think of right now (feel free to add to the list):

1. Set the sock's label/SID in sk_alloc()

This seems to be the right choice; not sure why we didn't do it that way in the first place (maybe we did in the original LSM patches, but it fell out when the original networking hooks were rejected).

Quoted 11 lines

2. Introduce a new hook to set the label/SID of a sock and call it from tun_set_iff()

The problem with #2 is that it introduces a new (basically TUN specific) hook to do something silly. Important, but still kinda silly. The problem with #1 is that we currently set the sock's label/SID in selinux_socket_post_create() and match it with the inode's label/SID which has the potential to get ugly (I haven't verified all of those cases yet). However, there may be an alternative, call it #1a, where set label the sock in sk_alloc() and then use the sock's label to set the inode's label in socket_post_create(); this should solve the potential ugliness.

Wouldn't it be a bug if they didn't match? So I'd add the sk_alloc() hook, set the label/SID for the sock there, and remove the setting of the sock label/SID from post_create. And then just add a BUG_ON to post_create to assert that the inode SID should be the same as the sock SID and if they don't match something has gone wrong.

Quoted 6 lines

Thoughts?

There is also a somewhat related issue involving persistent TUN/TAP devices but I'd like to resolve this before getting deeper into that problem.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#6 lore raw
Quoted 8 lines · Stephen Smalley

On Tue, 2009-06-30 at 17:34 -0400, Paul Moore wrote:

Wouldn't it be a bug if they didn't match?  So I'd add the sk_alloc() hook, set the label/SID for the sock there, and remove the setting of the sock label/SID from post_create.  And then just add a BUG_ON to post_create to assert that the inode SID should be the same as the sock SID and if they don't match something has gone wrong.

I've got a system all set up to test anything you want/have/need.... Maybe this afternoon I'll even give this suggestion a try just to see what happens, the networking hooks are ummmm, special?

-Eric

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#7 lore raw
Quoted 10 lines · Eric Paris
Quoted 8 lines · Stephen Smalley

On Tue, 2009-06-30 at 17:34 -0400, Paul Moore wrote:

Wouldn't it be a bug if they didn't match? So I'd add the sk_alloc() hook, set the label/SID for the sock there, and remove the setting of the sock label/SID from post_create. And then just add a BUG_ON to post_create to assert that the inode SID should be the same as the sock SID and if they don't match something has gone wrong.

Quoted 3 lines

I've got a system all set up to test anything you want/have/need.... Maybe this afternoon I'll even give this suggestion a try just to see what happens, the networking hooks are ummmm, special?

Wait - we already have a sk_alloc_security hook. And IIRC, we don't set the SID/label there because that can happen when a new connection sock is created, and thus outside of process context.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#2 lore raw
In reply to James Morris
Quoted 18 lines · James Morris
Quoted 17 lines · Paul Moore

So how do we fix it? Well, there are a two options that I can think of right now (feel free to add to the list):

1. Set the sock's label/SID in sk_alloc()
2. Introduce a new hook to set the label/SID of a sock and call it from tun_set_iff()

The problem with #2 is that it introduces a new (basically TUN specific) hook to do something silly. Important, but still kinda silly. The problem with #1 is that we currently set the sock's label/SID in selinux_socket_post_create() and match it with the inode's label/SID which has the potential to get ugly (I haven't verified all of those cases yet). However, there may be an alternative, call it #1a, where set label the sock in sk_alloc() and then use the sock's label to set the inode's label in socket_post_create(); this should solve the potential ugliness.

Quoted 4 lines
Quoted 1 line

Thoughts?

I'm not sure, but we probably need to include the netdev list in the discussion.

Yep, I was just hoping to have a little discussion here first to make sure we at least agreed on what should be done from a security point of view ...

Signature · 8 lines
paul moore
linux @ hp


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#4 lore raw
In reply to Casey Schaufler
Quoted 14 lines · Casey Schaufler
Quoted 10 lines · Paul Moore

Unfortunately we have a problem with the network access controls and TUN/TAP devices. The basic issue is that packets entering the stack via a TUN device, e.g. QEMU/KVM guest instance operating with a bridged network configuration, do not have a fully initialized sock associated with them. I say "fully initialized" because the basic initialization has been done (memory allocated, initial values set to SECINITSID_UNLABELED, etc.) but the last step where we assign the sock a label/SID never happens. Why? Because the TUN driver code only calls sk_alloc() and nothing else in the TUN code paths finish the SELinux sock setup.

So what should it be calling and why is the fact that it isn't not a bug in the TUN driver?

...

Quoted 3 lines

As this would appear to be a flaw in the TUN driver, get the TUN developers to fix their broken driver. I certainly dislike a special purpose LSM hook for this.

I do too.

Quoted 2 lines

Do you see this as a problem for all users of labeled networking, or is it isolated to SELinux?

The issue first came up with respect to SELinux so that is where I've been looking/thinking. While there is a potential for Smack to be affected I doubt that will be he case since Smack doesn't really care about stuff at the postrouting level; although Smack may want to enforce some level of access control but I need to go re-remember how Smack does outbound access control. TOMOYO should be unaffected since they don't mess with labels.

Signature · 8 lines
paul moore
linux @ hp


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#8 lore raw
In reply to Stephen Smalley
Quoted 10 lines · Stephen Smalley
Quoted 9 lines · Eric Paris
Quoted 7 lines · Stephen Smalley

On Tue, 2009-06-30 at 17:34 -0400, Paul Moore wrote:

Wouldn't it be a bug if they didn't match? So I'd add the sk_alloc() hook, set the label/SID for the sock there, and remove the setting of the sock label/SID from post_create. And then just add a BUG_ON to post_create to assert that the inode SID should be the same as the sock SID and if they don't match something has gone wrong.

Quoted 5 lines
Quoted 3 lines

I've got a system all set up to test anything you want/have/need.... Maybe this afternoon I'll even give this suggestion a try just to see what happens, the networking hooks are ummmm, special?

Wait - we already have a sk_alloc_security hook.

Yep, this is where we would set the sock's label/SID.

Quoted 2 lines

And IIRC, we don't set the SID/label there because that can happen when a new connection sock is created, and thus outside of process context.

This gets back to my original comments about "ugliness". New incoming connection sockets should get labeled correctly before use through the inet_conn_request() and inet_csk_clone() hooks. While admittedly, there is the potential for the sock to be labeled incorrectly immediately after allocation, but this is not very different from what we have today (sock is initially labeled with unlabeled_t).

I'll put something together and post an RFC this afternoon, the patch should be relatively small.

Signature · 8 lines
paul moore
linux @ hp


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#9 lore raw
Quoted 22 lines · Paul Moore
Quoted 10 lines · Stephen Smalley
Quoted 9 lines · Eric Paris
Quoted 7 lines · Stephen Smalley

On Tue, 2009-06-30 at 17:34 -0400, Paul Moore wrote:

Wouldn't it be a bug if they didn't match? So I'd add the sk_alloc() hook, set the label/SID for the sock there, and remove the setting of the sock label/SID from post_create. And then just add a BUG_ON to post_create to assert that the inode SID should be the same as the sock SID and if they don't match something has gone wrong.

Quoted 5 lines
Quoted 3 lines

I've got a system all set up to test anything you want/have/need.... Maybe this afternoon I'll even give this suggestion a try just to see what happens, the networking hooks are ummmm, special?

Wait - we already have a sk_alloc_security hook.

Yep, this is where we would set the sock's label/SID.

Quoted 2 lines

And IIRC, we don't set the SID/label there because that can happen when a new connection sock is created, and thus outside of process context.

Quoted 6 lines

This gets back to my original comments about "ugliness". New incoming connection sockets should get labeled correctly before use through the inet_conn_request() and inet_csk_clone() hooks. While admittedly, there is the potential for the sock to be labeled incorrectly immediately after allocation, but this is not very different from what we have today (sock is initially labeled with unlabeled_t).

unlabeled_t is easily noticeable in denials (as in this case) and correctly reflects the state of the socket - it isn't yet labeled. Setting the sid from the task sid of some random task that happens to be referenced by current when the new connection is initiated seems a little more prone to danger if it happens to not get set by the other hooks later.

Quoted 3 lines

I'll put something together and post an RFC this afternoon, the patch should be relatively small.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#10 lore raw
Quoted 4 lines · Stephen Smalley
Quoted 3 lines · Paul Moore
Quoted 2 lines · Stephen Smalley
Quoted 1 line · Eric Paris

On Wed, Jul 1, 2009 at 9:44 AM, Stephen Smalley<[email protected]>

Quoted 27 lines · wrote:
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines · Paul Moore

Wouldn't it be a bug if they didn't match? So I'd add the sk_alloc() hook, set the label/SID for the sock there, and remove the setting of the sock label/SID from post_create. And then just add a BUG_ON to post_create to assert that the inode SID should be the same as the sock SID and if they don't match something has gone wrong.

Quoted 5 lines
Quoted 3 lines

I've got a system all set up to test anything you want/have/need.... Maybe this afternoon I'll even give this suggestion a try just to see what happens, the networking hooks are ummmm, special?

Wait - we already have a sk_alloc_security hook.

Yep, this is where we would set the sock's label/SID.

Quoted 2 lines

And IIRC, we don't set the SID/label there because that can happen when a new connection sock is created, and thus outside of process context.

Quoted 6 lines

This gets back to my original comments about "ugliness". New incoming connection sockets should get labeled correctly before use through the inet_conn_request() and inet_csk_clone() hooks. While admittedly, there is the potential for the sock to be labeled incorrectly immediately after allocation, but this is not very different from what we have today (sock is initially labeled with unlabeled_t).

Quoted 6 lines

unlabeled_t is easily noticeable in denials (as in this case) and correctly reflects the state of the socket - it isn't yet labeled. Setting the sid from the task sid of some random task that happens to be referenced by current when the new connection is initiated seems a little more prone to danger if it happens to not get set by the other hooks later.

Well, if we can't do it in sk_alloc() then I think we are stuck with a new hook; which just seems wrong.

Signature · 8 lines
paul moore
linux @ hp


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#11 lore raw
Quoted 2 lines · Paul Moore

Well, if we can't do it in sk_alloc() then I think we are stuck with a new hook; which just seems wrong.

Why isn't the TUN driver calling the same code as other socket creating code?

- James

Signature · 7 lines
James Morris <[email protected]>


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#12 lore raw
Quoted 6 lines · James Morris
Quoted 2 lines · Paul Moore

Well, if we can't do it in sk_alloc() then I think we are stuck with a new hook; which just seems wrong.

Why isn't the TUN driver calling the same code as other socket creating code?

The other socket creating code handles the final setup/initialization in the security_socket_post_create() hook which operates on sockets not socks.

Signature · 8 lines
paul moore
linux @ hp


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#13 lore raw
Quoted 10 lines · Paul Moore
Quoted 6 lines · James Morris
Quoted 2 lines · Paul Moore

Well, if we can't do it in sk_alloc() then I think we are stuck with a new hook; which just seems wrong.

Why isn't the TUN driver calling the same code as other socket creating code?

The other socket creating code handles the final setup/initialization in the security_socket_post_create() hook which operates on sockets not socks.

I wonder if passing a flag might be better than the prot argument, which allows the caller to indicate what kind of initialization it's doing, rather than what will be seen as another protocol layering violation (i.e. the security model poking around to find out what kind of protocol & changing its behaviour).

- James

Signature · 7 lines
James Morris
<[email protected]>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#14 lore raw
Quoted 13 lines · James Morris
Quoted 11 lines · Paul Moore
Quoted 6 lines · James Morris
Quoted 2 lines · Paul Moore

Well, if we can't do it in sk_alloc() then I think we are stuck with a new hook; which just seems wrong.

Why isn't the TUN driver calling the same code as other socket creating code?

The other socket creating code handles the final setup/initialization in the security_socket_post_create() hook which operates on sockets not socks.

Quoted 5 lines

I wonder if passing a flag might be better than the prot argument, which allows the caller to indicate what kind of initialization it's doing, rather than what will be seen as another protocol layering violation (i.e. the security model poking around to find out what kind of protocol & changing its behaviour).

Good point. I'm going to be reworking the solution a bit, but if I still need to do something like this I'll go the flag route.

Signature · 8 lines
paul moore
linux @ hp


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.