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>
40 * selinux_netlbl_sidlookup_cached - Cache a SID lookup
42 * @secattr: the NetLabel security attributes
46 * Query the SELinux security server to lookup the correct SID for the given
47 * security attributes. If the query is successful, cache the result to speed
48 * up future lookups. Returns zero on success, negative values on failure.
51 static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb,
52 struct netlbl_lsm_secattr *secattr,
57 rc = security_netlbl_secattr_to_sid(secattr, sid);
59 (secattr->flags & NETLBL_SECATTR_CACHEABLE) &&
60 (secattr->flags & NETLBL_SECATTR_CACHE))
61 netlbl_cache_add(skb, secattr);
67 * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism
68 * @sk: the socket to label
71 * Attempt to label a socket using the NetLabel mechanism. Returns zero values
72 * on success, negative values on failure.
75 static int selinux_netlbl_sock_setsid(struct sock *sk)
78 struct sk_security_struct *sksec = sk->sk_security;
79 struct netlbl_lsm_secattr secattr;
81 if (sksec->nlbl_state != NLBL_REQUIRE)
84 netlbl_secattr_init(&secattr);
86 rc = security_netlbl_sid_to_secattr(sksec->sid, &secattr);
88 goto sock_setsid_return;
89 rc = netlbl_sock_setattr(sk, &secattr);
91 sksec->nlbl_state = NLBL_LABELED;
94 netlbl_secattr_destroy(&secattr);
99 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
102 * Invalidate the NetLabel security attribute mapping cache.
105 void selinux_netlbl_cache_invalidate(void)
107 netlbl_cache_invalidate();
111 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
112 * @ssec: the sk_security_struct
113 * @family: the socket family
116 * Called when the NetLabel state of a sk_security_struct needs to be reset.
117 * The caller is responsibile for all the NetLabel sk_security_struct locking.
120 void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
123 if (family == PF_INET)
124 ssec->nlbl_state = NLBL_REQUIRE;
126 ssec->nlbl_state = NLBL_UNSET;
130 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
132 * @family: protocol family
133 * @type: NetLabel labeling protocol type
137 * Call the NetLabel mechanism to get the security attributes of the given
138 * packet and use those attributes to determine the correct context/SID to
139 * assign to the packet. Returns zero on success, negative values on failure.
142 int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
148 struct netlbl_lsm_secattr secattr;
150 if (!netlbl_enabled()) {
155 netlbl_secattr_init(&secattr);
156 rc = netlbl_skbuff_getattr(skb, family, &secattr);
157 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
158 rc = selinux_netlbl_sidlookup_cached(skb, &secattr, sid);
161 *type = secattr.type;
162 netlbl_secattr_destroy(&secattr);
168 * selinux_netlbl_sock_graft - Netlabel the new socket
169 * @sk: the new connection
170 * @sock: the new socket
173 * The connection represented by @sk is being grafted onto @sock so set the
174 * socket's NetLabel to match the SID of @sk.
177 void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
179 /* Try to set the NetLabel on the socket to save time later, if we fail
180 * here we will pick up the pieces in later calls to
181 * selinux_netlbl_inode_permission(). */
182 selinux_netlbl_sock_setsid(sk);
186 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
187 * @sock: the socket to label
190 * Attempt to label a socket using the NetLabel mechanism using the given
191 * SID. Returns zero values on success, negative values on failure.
194 int selinux_netlbl_socket_post_create(struct socket *sock)
196 return selinux_netlbl_sock_setsid(sock->sk);
200 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
201 * @inode: the file descriptor's inode
202 * @mask: the permission mask
205 * Looks at a file's inode and if it is marked as a socket protected by
206 * NetLabel then verify that the socket has been labeled, if not try to label
207 * the socket now with the inode's SID. Returns zero on success, negative
211 int selinux_netlbl_inode_permission(struct inode *inode, int mask)
216 struct sk_security_struct *sksec;
218 if (!S_ISSOCK(inode->i_mode) ||
219 ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
222 sock = SOCKET_I(inode);
224 sksec = sk->sk_security;
225 if (sksec->nlbl_state != NLBL_REQUIRE)
229 bh_lock_sock_nested(sk);
230 if (likely(sksec->nlbl_state == NLBL_REQUIRE))
231 rc = selinux_netlbl_sock_setsid(sk);
241 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
242 * @sksec: the sock's sk_security_struct
244 * @family: protocol family
245 * @ad: the audit data
248 * Fetch the NetLabel security attributes from @skb and perform an access check
249 * against the receiving socket. Returns zero on success, negative values on
253 int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
256 struct avc_audit_data *ad)
261 struct netlbl_lsm_secattr secattr;
263 if (!netlbl_enabled())
266 netlbl_secattr_init(&secattr);
267 rc = netlbl_skbuff_getattr(skb, family, &secattr);
268 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
269 rc = selinux_netlbl_sidlookup_cached(skb, &secattr, &nlbl_sid);
271 nlbl_sid = SECINITSID_UNLABELED;
272 netlbl_secattr_destroy(&secattr);
276 switch (sksec->sclass) {
277 case SECCLASS_UDP_SOCKET:
278 perm = UDP_SOCKET__RECVFROM;
280 case SECCLASS_TCP_SOCKET:
281 perm = TCP_SOCKET__RECVFROM;
284 perm = RAWIP_SOCKET__RECVFROM;
287 rc = avc_has_perm(sksec->sid, nlbl_sid, sksec->sclass, perm, ad);
291 if (nlbl_sid != SECINITSID_UNLABELED)
292 netlbl_skbuff_err(skb, rc);
297 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
299 * @level: the socket level or protocol
300 * @optname: the socket option name
303 * Check the setsockopt() call and if the user is trying to replace the IP
304 * options on a socket and a NetLabel is in place for the socket deny the
305 * access; otherwise allow the access. Returns zero when the access is
306 * allowed, -EACCES when denied, and other negative values on error.
309 int selinux_netlbl_socket_setsockopt(struct socket *sock,
314 struct sock *sk = sock->sk;
315 struct sk_security_struct *sksec = sk->sk_security;
316 struct netlbl_lsm_secattr secattr;
318 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
319 sksec->nlbl_state == NLBL_LABELED) {
320 netlbl_secattr_init(&secattr);
322 rc = netlbl_sock_getattr(sk, &secattr);
324 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
326 netlbl_secattr_destroy(&secattr);