2 * SELinux NetLabel Support
4 * This file provides the necessary glue to tie NetLabel into the SELinux
7 * Author: Paul Moore <paul.moore@hp.com>
12 * (c) Copyright Hewlett-Packard Development Company, L.P., 2007
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
22 * the GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <linux/spinlock.h>
31 #include <linux/rcupdate.h>
33 #include <net/netlabel.h>
39 * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism
40 * @sk: the socket to label
41 * @sid: the SID to use
44 * Attempt to label a socket using the NetLabel mechanism using the given
45 * SID. Returns zero values on success, negative values on failure. The
46 * caller is responsibile for calling rcu_read_lock() before calling this
47 * this function and rcu_read_unlock() after this function returns.
50 static int selinux_netlbl_sock_setsid(struct sock *sk, u32 sid)
53 struct sk_security_struct *sksec = sk->sk_security;
54 struct netlbl_lsm_secattr secattr;
56 netlbl_secattr_init(&secattr);
58 rc = security_netlbl_sid_to_secattr(sid, &secattr);
60 goto sock_setsid_return;
61 rc = netlbl_sock_setattr(sk, &secattr);
63 spin_lock_bh(&sksec->nlbl_lock);
64 sksec->nlbl_state = NLBL_LABELED;
65 spin_unlock_bh(&sksec->nlbl_lock);
69 netlbl_secattr_destroy(&secattr);
74 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
77 * Invalidate the NetLabel security attribute mapping cache.
80 void selinux_netlbl_cache_invalidate(void)
82 netlbl_cache_invalidate();
86 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
87 * @ssec: the sk_security_struct
88 * @family: the socket family
91 * Called when the NetLabel state of a sk_security_struct needs to be reset.
92 * The caller is responsibile for all the NetLabel sk_security_struct locking.
95 void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
98 if (family == PF_INET)
99 ssec->nlbl_state = NLBL_REQUIRE;
101 ssec->nlbl_state = NLBL_UNSET;
105 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
106 * @ssec: the sk_security_struct
107 * @family: the socket family
110 * Called when a new sk_security_struct is allocated to initialize the NetLabel
114 void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
117 /* No locking needed, we are the only one who has access to ssec */
118 selinux_netlbl_sk_security_reset(ssec, family);
119 spin_lock_init(&ssec->nlbl_lock);
123 * selinux_netlbl_sk_security_clone - Copy the NetLabel fields
124 * @ssec: the original sk_security_struct
125 * @newssec: the cloned sk_security_struct
128 * Clone the NetLabel specific sk_security_struct fields from @ssec to
132 void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
133 struct sk_security_struct *newssec)
135 /* We don't need to take newssec->nlbl_lock because we are the only
136 * thread with access to newssec, but we do need to take the RCU read
137 * lock as other threads could have access to ssec */
139 selinux_netlbl_sk_security_reset(newssec, ssec->sk->sk_family);
140 newssec->sclass = ssec->sclass;
145 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
147 * @base_sid: the SELinux SID to use as a context for MLS only attributes
151 * Call the NetLabel mechanism to get the security attributes of the given
152 * packet and use those attributes to determine the correct context/SID to
153 * assign to the packet. Returns zero on success, negative values on failure.
156 int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, u32 base_sid, u32 *sid)
159 struct netlbl_lsm_secattr secattr;
161 if (!netlbl_enabled()) {
166 netlbl_secattr_init(&secattr);
167 rc = netlbl_skbuff_getattr(skb, &secattr);
168 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) {
169 rc = security_netlbl_secattr_to_sid(&secattr, base_sid, sid);
171 (secattr.flags & NETLBL_SECATTR_CACHEABLE) &&
172 (secattr.flags & NETLBL_SECATTR_CACHE))
173 netlbl_cache_add(skb, &secattr);
176 netlbl_secattr_destroy(&secattr);
182 * selinux_netlbl_sock_graft - Netlabel the new socket
183 * @sk: the new connection
184 * @sock: the new socket
187 * The connection represented by @sk is being grafted onto @sock so set the
188 * socket's NetLabel to match the SID of @sk.
191 void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
193 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
194 struct sk_security_struct *sksec = sk->sk_security;
195 struct netlbl_lsm_secattr secattr;
198 sksec->sclass = isec->sclass;
202 if (sksec->nlbl_state != NLBL_REQUIRE) {
207 netlbl_secattr_init(&secattr);
208 if (netlbl_sock_getattr(sk, &secattr) == 0 &&
209 secattr.flags != NETLBL_SECATTR_NONE &&
210 security_netlbl_secattr_to_sid(&secattr,
212 &nlbl_peer_sid) == 0)
213 sksec->peer_sid = nlbl_peer_sid;
214 netlbl_secattr_destroy(&secattr);
216 /* Try to set the NetLabel on the socket to save time later, if we fail
217 * here we will pick up the pieces in later calls to
218 * selinux_netlbl_inode_permission(). */
219 selinux_netlbl_sock_setsid(sk, sksec->sid);
225 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
226 * @sock: the socket to label
229 * Attempt to label a socket using the NetLabel mechanism using the given
230 * SID. Returns zero values on success, negative values on failure.
233 int selinux_netlbl_socket_post_create(struct socket *sock)
236 struct sock *sk = sock->sk;
237 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
238 struct sk_security_struct *sksec = sk->sk_security;
240 sksec->sclass = isec->sclass;
243 if (sksec->nlbl_state == NLBL_REQUIRE)
244 rc = selinux_netlbl_sock_setsid(sk, sksec->sid);
251 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
252 * @inode: the file descriptor's inode
253 * @mask: the permission mask
256 * Looks at a file's inode and if it is marked as a socket protected by
257 * NetLabel then verify that the socket has been labeled, if not try to label
258 * the socket now with the inode's SID. Returns zero on success, negative
262 int selinux_netlbl_inode_permission(struct inode *inode, int mask)
267 struct sk_security_struct *sksec;
269 if (!S_ISSOCK(inode->i_mode) ||
270 ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
272 sock = SOCKET_I(inode);
274 sksec = sk->sk_security;
277 if (sksec->nlbl_state != NLBL_REQUIRE) {
282 bh_lock_sock_nested(sk);
283 rc = selinux_netlbl_sock_setsid(sk, sksec->sid);
292 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
293 * @sksec: the sock's sk_security_struct
295 * @ad: the audit data
298 * Fetch the NetLabel security attributes from @skb and perform an access check
299 * against the receiving socket. Returns zero on success, negative values on
303 int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
305 struct avc_audit_data *ad)
310 struct netlbl_lsm_secattr secattr;
312 if (!netlbl_enabled())
315 netlbl_secattr_init(&secattr);
316 rc = netlbl_skbuff_getattr(skb, &secattr);
317 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) {
318 rc = security_netlbl_secattr_to_sid(&secattr,
322 (secattr.flags & NETLBL_SECATTR_CACHEABLE) &&
323 (secattr.flags & NETLBL_SECATTR_CACHE))
324 netlbl_cache_add(skb, &secattr);
326 nlbl_sid = SECINITSID_UNLABELED;
327 netlbl_secattr_destroy(&secattr);
331 switch (sksec->sclass) {
332 case SECCLASS_UDP_SOCKET:
333 perm = UDP_SOCKET__RECVFROM;
335 case SECCLASS_TCP_SOCKET:
336 perm = TCP_SOCKET__RECVFROM;
339 perm = RAWIP_SOCKET__RECVFROM;
342 rc = avc_has_perm(sksec->sid, nlbl_sid, sksec->sclass, perm, ad);
346 if (nlbl_sid != SECINITSID_UNLABELED)
347 netlbl_skbuff_err(skb, rc);
352 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
354 * @level: the socket level or protocol
355 * @optname: the socket option name
358 * Check the setsockopt() call and if the user is trying to replace the IP
359 * options on a socket and a NetLabel is in place for the socket deny the
360 * access; otherwise allow the access. Returns zero when the access is
361 * allowed, -EACCES when denied, and other negative values on error.
364 int selinux_netlbl_socket_setsockopt(struct socket *sock,
369 struct sock *sk = sock->sk;
370 struct sk_security_struct *sksec = sk->sk_security;
371 struct netlbl_lsm_secattr secattr;
374 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
375 sksec->nlbl_state == NLBL_LABELED) {
376 netlbl_secattr_init(&secattr);
378 rc = netlbl_sock_getattr(sk, &secattr);
380 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
382 netlbl_secattr_destroy(&secattr);