socket options ... again
I was looking at some policy and just realized that it's pretty liberal in handing out the socket setopt permission to domains; for some reason I was thought this was a rather rare thing. This concerns me slightly because this is how NetLabel attaches security attributes to outgoing packets.
As a result I'm starting to look at how to best protect the NetLabel security attributes in the socket's IP options. It is pretty straight forward to hook into the selinux_socket_setsockopt() function and watch for the IPPROTO_IP/IP_OPTIONS combination but my question to all of you is what is the "right" thing to do in such a case? I can think of the following options (all of which are fairly easy to implement):
1. If NetLabel is compiled into the kernel deny the application the right to set the socket's IP options. This is pretty heavy handed but it would preserve the security attributes; although I have no idea the problems it might cause in the application space.
2. If the socket has a NetLabel attached to it simply deny the access, otherwise allow it to proceed. This has the problem in that it could allow a "bad" application to write it's own "evil" security attributes to the socket's IP option, although if there is no NetLabel attached to the socket (i.e. NetLabel was told to ignore this domain) this may be okay.
3. If the application sets it's own security attributes, clear the socket's option field and replace it with the correct NetLabel option even if the NetLabel configuration for that domain is to leave the packets unlabeled. The problem here is that we would be allowing the setsockopt() syscall to proceed without error then we would end up resetting the options on the socket (this would happen on the first write to the socket).
While option #1 is the easiest I don't really like the idea of simply denying applications the ability to set IP options (although I question how common this really is). Option #3 is perhaps the most compatible, although it could lead to strange behavior with socket options changing underneath the application. Which leads me to option #2, it does have the drawback I mentioned above, but then again there is nothing stopping applications from doing this currently.
Thoughts on these options? Can anyone else think of a better solution?
Signature · 7 lines
paul moore linux security @ 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.
Quoted 30 lines · Paul Moore
I was looking at some policy and just realized that it's pretty liberal in handing out the socket setopt permission to domains; for some reason I was thought this was a rather rare thing. This concerns me slightly because this is how NetLabel attaches security attributes to outgoing packets.
As a result I'm starting to look at how to best protect the NetLabel security attributes in the socket's IP options. It is pretty straight forward to hook into the
selinux_socket_setsockopt()function and watch for the IPPROTO_IP/IP_OPTIONS combination but my question to all of you is what is the "right" thing to do in such a case? I can think of the following options (all of which are fairly easy to implement):1. If NetLabel is compiled into the kernel deny the application the right to set the socket's IP options. This is pretty heavy handed but it would preserve the security attributes; although I have no idea the problems it might cause in the application space.
2. If the socket has a NetLabel attached to it simply deny the access, otherwise allow it to proceed. This has the problem in that it could allow a "bad" application to write it's own "evil" security attributes to the socket's IP option, although if there is no NetLabel attached to the socket (i.e. NetLabel was told to ignore this domain) this may be okay.
3. If the application sets it's own security attributes, clear the socket's option field and replace it with the correct NetLabel option even if the NetLabel configuration for that domain is to leave the packets unlabeled. The problem here is that we would be allowing the
setsockopt()syscall to proceed without error then we would end up resetting the options on the socket (this would happen on the first write to the socket).
Quoted 8 lines
While option #1 is the easiest I don't really like the idea of simply denying applications the ability to set IP options (although I question how common this really is). Option #3 is perhaps the most compatible, although it could lead to strange behavior with socket options changing underneath the application. Which leads me to option #2, it does have the drawback I mentioned above, but then again there is nothing stopping applications from doing this currently.
Thoughts on these options? Can anyone else think of a better solution?
ip_options_compile() checks CAP_NET_RAW for IPOPT_SEC, IPOPT_SID, and any unknown options. Is there a reason to not do likewise for setting IPOPT_CIPSO from userspace (which you need to distinguish in some manner from your internal setting)?
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.
Quoted 8 lines · Stephen Smalley
Quoted 1 line · Paul Moore
Thoughts on these options? Can anyone else think of a better solution?
ip_options_compile()checksCAP_NET_RAWforIPOPT_SEC,IPOPT_SID, and any unknown options. Is there a reason to not do likewise for settingIPOPT_CIPSOfrom userspace (which you need to distinguish in some manner from your internal setting)?
Ah ha ... thank you for the lightbulb moment.
Adding CAP_NET_RAW in ip_options_compile() would solve the problem in a manner which makes the most sense from an application point of view. However, it does present a problem in that potentially even applications without CAP_NET_RAW will need to have a CIPSO options set by the kernel, as you noted in the comment about distinguishing between NetLabel and applications.
Right now the CIPSO code only makes use of ip_options_compile() in two places (that I can think of): the first is when a packet enters the system via ip_rcv_options() in ip_rcv_finish(), the second when a socket is created by an application and cipso_v4_socket_setattr() is called. I don't believe CAP_NET_RAW should be an issue in the first case (please correct me if I'm wrong) but it would cause problems in the second case. Considering the blank slate of a new socket and the very simple nature of the CIPSO code in ip_options_compile() it may be the best option is to simply handle the ip_options_compile() tasks directly in cipso_v4_socket_setattr().
Does this sound reasonable?
Signature · 7 lines
paul moore linux security @ 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.
Quoted 28 lines · Paul Moore
Quoted 8 lines · Stephen Smalley
Quoted 1 line · Paul Moore
Thoughts on these options? Can anyone else think of a better solution?
ip_options_compile()checksCAP_NET_RAWforIPOPT_SEC,IPOPT_SID, and any unknown options. Is there a reason to not do likewise for settingIPOPT_CIPSOfrom userspace (which you need to distinguish in some manner from your internal setting)?Ah ha ... thank you for the lightbulb moment.
Adding
CAP_NET_RAWinip_options_compile()would solve the problem in a manner which makes the most sense from an application point of view. However, it does present a problem in that potentially even applications withoutCAP_NET_RAWwill need to have a CIPSO options set by the kernel, as you noted in the comment about distinguishing between NetLabel and applications.Right now the CIPSO code only makes use of
ip_options_compile()in two places (that I can think of): the first is when a packet enters the system viaip_rcv_options()inip_rcv_finish(), the second when a socket is created by an application andcipso_v4_socket_setattr()is called. I don't believeCAP_NET_RAWshould be an issue in the first case (please correct me if I'm wrong) but it would cause problems in the second case. Considering the blank slate of a new socket and the very simple nature of the CIPSO code inip_options_compile()it may be the best option is to simply handle theip_options_compile()tasks directly incipso_v4_socket_setattr().
Quoted 1 line
Does this sound reasonable?
To clarify, the existing capable checks in ip_options_compile() are only applied on the output processing (they check for a NULL skb), never on the input side.
It seems like your internal use of ip_options_compile() from cipso is overkill, since it then just calls back into your cipso code for the CIPSO option, right? So just handling it directly within the cipso code makes sense to me.
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.
Quoted 32 lines · Stephen Smalley
Quoted 30 lines · Paul Moore
Quoted 9 lines · Stephen Smalley
Quoted 1 line · Paul Moore
Thoughts on these options? Can anyone else think of a better solution?
ip_options_compile()checksCAP_NET_RAWforIPOPT_SEC,IPOPT_SID, and any unknown options. Is there a reason to not do likewise for settingIPOPT_CIPSOfrom userspace (which you need to distinguish in some manner from your internal setting)?Ah ha ... thank you for the lightbulb moment.
Adding
CAP_NET_RAWinip_options_compile()would solve the problem in a manner which makes the most sense from an application point of view. However, it does present a problem in that potentially even applications withoutCAP_NET_RAWwill need to have a CIPSO options set by the kernel, as you noted in the comment about distinguishing between NetLabel and applications.Right now the CIPSO code only makes use of
ip_options_compile()in two places (that I can think of): the first is when a packet enters the system viaip_rcv_options()inip_rcv_finish(), the second when a socket is created by an application andcipso_v4_socket_setattr()is called. I don't believeCAP_NET_RAWshould be an issue in the first case (please correct me if I'm wrong) but it would cause problems in the second case. Considering the blank slate of a new socket and the very simple nature of the CIPSO code inip_options_compile()it may be the best option is to simply handle theip_options_compile()tasks directly incipso_v4_socket_setattr().
Quoted 5 lines
Quoted 1 line
Does this sound reasonable?
To clarify, the existing capable checks in
ip_options_compile()are only applied on the output processing (they check for a NULL skb), never on the input side.
Yep.
Quoted 4 lines
It seems like your internal use of
ip_options_compile()from cipso is overkill, since it then just calls back into your cipso code for the CIPSO option, right? So just handling it directly within the cipso code makes sense to me.
Yes, you are right there is a lot of unnecessary stuff there; its a bit of a holdover from the initial code which used ip_options_get(). The original thought was that since it wasn't really a critical path it was probably better to reuse what was there even if it was slower.
I'm working on a patch now which I should have out in a few hours.
Signature · 7 lines
paul moore linux security @ 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.
Quoted 39 lines · Stephen Smalley
Quoted 37 lines · Paul Moore
I was looking at some policy and just realized that it's pretty liberal in handing out the socket setopt permission to domains; for some reason I was thought this was a rather rare thing. This concerns me slightly because this is how NetLabel attaches security attributes to outgoing packets.
As a result I'm starting to look at how to best protect the NetLabel security attributes in the socket's IP options. It is pretty straight forward to hook into the
selinux_socket_setsockopt()function and watch for the IPPROTO_IP/IP_OPTIONS combination but my question to all of you is what is the "right" thing to do in such a case? I can think of the following options (all of which are fairly easy to implement):1. If NetLabel is compiled into the kernel deny the application the right to set the socket's IP options. This is pretty heavy handed but it would preserve the security attributes; although I have no idea the problems it might cause in the application space.
2. If the socket has a NetLabel attached to it simply deny the access, otherwise allow it to proceed. This has the problem in that it could allow a "bad" application to write it's own "evil" security attributes to the socket's IP option, although if there is no NetLabel attached to the socket (i.e. NetLabel was told to ignore this domain) this may be okay.
3. If the application sets it's own security attributes, clear the socket's option field and replace it with the correct NetLabel option even if the NetLabel configuration for that domain is to leave the packets unlabeled. The problem here is that we would be allowing the
setsockopt()syscall to proceed without error then we would end up resetting the options on the socket (this would happen on the first write to the socket).While option #1 is the easiest I don't really like the idea of simply denying applications the ability to set IP options (although I question how common this really is). Option #3 is perhaps the most compatible, although it could lead to strange behavior with socket options changing underneath the application. Which leads me to option #2, it does have the drawback I mentioned above, but then again there is nothing stopping applications from doing this currently.
Quoted 6 lines
Quoted 1 line
Thoughts on these options? Can anyone else think of a better solution?
ip_options_compile()checksCAP_NET_RAWforIPOPT_SEC,IPOPT_SID, and any unknown options. Is there a reason to not do likewise for settingIPOPT_CIPSOfrom userspace (which you need to distinguish in some manner from your internal setting)?
Ungh. I really wanted to believe it was that simple, but upon further inspection it looks like simply adding CAP_NET_RAW only prevents applications from setting their own CIPSO option it does not prevent users from replacing a CIPSO option with another option like source routing. With this in mind I think adding the CAP_NET_RAW check and going with option #2 (see above) is the best choice ... thoughts?
Signature · 7 lines
paul moore linux security @ 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.
RFC 1/1 protect NetLabel options from setsockopt (was: socket options ... again)
Quoted 8 lines · Paul Moore
Ungh. I really wanted to believe it was that simple, but upon further inspection it looks like simply adding
CAP_NET_RAWonly prevents applications from setting their own CIPSO option it does not prevent users from replacing a CIPSO option with another option like source routing. With this in mind I think adding theCAP_NET_RAWcheck and going with option #2 (see above) is the best choice ... thoughts?
Below is a quick patch which adds both the CAP_NET_RAW check and option #2 check (see the rest of the thread for details). You will also notice some slight locking changes in selinux_netlbl_inode_permission(); I believe these are necessary to prevent a race condition with the checks in selinux_netlbl_socket_setsockopt().
I haven't had much chance to test this code yet (all I know is it compiles and boots) but I won't be in a position where I can do much testing this Friday or over the weekend so I thought it might be a good idea to at least throw this out as a RFC patch to let people comment. If there are no objections and nothing pops up during testing I'll post this for inclusion early next week.
(My apologies for the whitespace issues in the diff below but I wasn't sure how to post a patch w/o losing the threading any other way)
net/ipv4/cipso_ipv4.c +3 -4
@@ -1307,7 +1307,8 @@ int cipso_v4_socket_setattr(const struct/* We can't use ip_options_get() directly because it makes a call to * ip_options_get_alloc() which allocates memory with GFP_KERNEL and -* we can't block here. */ +* we won't always have CAP_NET_RAW even though we _always_ want to +* set the IPOPT_CIPSO option. */ opt_len = (buf_len +3 ) & ~3 ; opt =kzalloc (sizeof (*opt) + opt_len,GFP_ATOMIC );if (opt ==NULL ) {
@@ -1317,11 +1318,9 @@ int cipso_v4_socket_setattr(const structmemcpy (opt->__data, buf, buf_len); opt->optlen = opt_len; opt->is_data =1 ; + opt->cipso =sizeof (struct iphdr );kfree (buf); buf =NULL ; - ret_val =ip_options_compile (opt,NULL ); -if (ret_val !=0 ) - goto socket_setattr_failure; sk_inet =inet_sk (sk);if (sk_inet->is_icsk) {
net/ipv4/ip_options.c +1 -1
@@ -443,7 +443,7 @@ int ip_options_compile(struct ip_options opt->router_alert = optptr - iph;break ;case IPOPT_CIPSO : -if (opt->cipso) { +if ((!skb && !capable (CAP_NET_RAW )) || opt->cipso) { pp_ptr = optptr; goto error; }
security/selinux/hooks.c +7 -1
@@ -3313,7 +3313,13 @@ static int selinux_socket_getpeername(ststatic int selinux_socket_setsockopt (struct socket *sock,int level,int optname) { -return socket_has_perm (current, sock,SOCKET__SETOPT ); +int err; + + err =socket_has_perm (current, sock,SOCKET__SETOPT ); +if (err) +return err; + +return selinux_netlbl_socket_setsockopt (sock, level, optname); } static int selinux_socket_getsockopt(struct socket *sock, int level,
security/selinux/include/selinux_netlabel.h +10 -0
@@ -53,6 +53,9 @@ void selinux_netlbl_sk_security_init(strvoid selinux_netlbl_sk_clone_security (struct sk_security_struct *ssec,struct sk_security_struct *newssec);int selinux_netlbl_inode_permission (struct inode *inode,int mask); +int selinux_netlbl_socket_setsockopt (struct socket *sock, +int level, +int optname);#else static inline void selinux_netlbl_cache_invalidate (void ) {
@@ -114,6 +117,13 @@ static inline int selinux_netlbl_inode_p {return 0 ; } + +static inline int selinux_netlbl_socket_setsockopt (struct socket *sock, +int level, +int optname) +{ +return 0 ; +}#endif /* CONFIG_NETLABEL */ #endif
security/selinux/ss/services.c +40 -6
64 lines not on this page · raw message
Signature · 7 lines
paul moore linux security @ 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.