blob: b9ce5fcf3432d570f3a280b0970406f4a3eb7a45 [file] [log] [blame]
Paul Moore5778eab2007-02-28 15:14:22 -05001/*
2 * SELinux NetLabel Support
3 *
4 * This file provides the necessary glue to tie NetLabel into the SELinux
5 * subsystem.
6 *
7 * Author: Paul Moore <paul.moore@hp.com>
8 *
9 */
10
11/*
12 * (c) Copyright Hewlett-Packard Development Company, L.P., 2007
13 *
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.
18 *
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.
23 *
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
27 *
28 */
29
30#include <linux/spinlock.h>
31#include <linux/rcupdate.h>
32#include <net/sock.h>
33#include <net/netlabel.h>
34
35#include "objsec.h"
36#include "security.h"
Adrian Bunkd4ee4232008-02-27 23:20:42 +020037#include "netlabel.h"
Paul Moore5778eab2007-02-28 15:14:22 -050038
39/**
Paul Moore5dbe1eb2008-01-29 08:44:18 -050040 * selinux_netlbl_sidlookup_cached - Cache a SID lookup
41 * @skb: the packet
42 * @secattr: the NetLabel security attributes
43 * @sid: the SID
44 *
45 * Description:
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.
49 *
50 */
51static int selinux_netlbl_sidlookup_cached(struct sk_buff *skb,
52 struct netlbl_lsm_secattr *secattr,
53 u32 *sid)
54{
55 int rc;
56
57 rc = security_netlbl_secattr_to_sid(secattr, sid);
58 if (rc == 0 &&
59 (secattr->flags & NETLBL_SECATTR_CACHEABLE) &&
60 (secattr->flags & NETLBL_SECATTR_CACHE))
61 netlbl_cache_add(skb, secattr);
62
63 return rc;
64}
65
66/**
Paul Mooreba6ff9f2007-06-07 18:37:15 -070067 * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism
68 * @sk: the socket to label
Paul Moore5778eab2007-02-28 15:14:22 -050069 *
70 * Description:
Paul Mooreaccc6092008-10-10 10:16:29 -040071 * Attempt to label a socket using the NetLabel mechanism. Returns zero values
72 * on success, negative values on failure.
Paul Moore5778eab2007-02-28 15:14:22 -050073 *
74 */
Paul Mooreaccc6092008-10-10 10:16:29 -040075static int selinux_netlbl_sock_setsid(struct sock *sk)
Paul Moore5778eab2007-02-28 15:14:22 -050076{
77 int rc;
Paul Mooreba6ff9f2007-06-07 18:37:15 -070078 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -050079 struct netlbl_lsm_secattr secattr;
80
Paul Mooreaccc6092008-10-10 10:16:29 -040081 if (sksec->nlbl_state != NLBL_REQUIRE)
82 return 0;
83
Paul Moore45c950e2008-01-22 09:31:00 +110084 netlbl_secattr_init(&secattr);
85
Paul Mooreaccc6092008-10-10 10:16:29 -040086 rc = security_netlbl_sid_to_secattr(sksec->sid, &secattr);
Paul Moore5778eab2007-02-28 15:14:22 -050087 if (rc != 0)
Paul Moore45c950e2008-01-22 09:31:00 +110088 goto sock_setsid_return;
Paul Mooreba6ff9f2007-06-07 18:37:15 -070089 rc = netlbl_sock_setattr(sk, &secattr);
Paul Mooref74af6e2008-02-25 11:40:33 -050090 if (rc == 0)
Paul Moore5778eab2007-02-28 15:14:22 -050091 sksec->nlbl_state = NLBL_LABELED;
Paul Moore5778eab2007-02-28 15:14:22 -050092
Paul Moore45c950e2008-01-22 09:31:00 +110093sock_setsid_return:
94 netlbl_secattr_destroy(&secattr);
Paul Moore5778eab2007-02-28 15:14:22 -050095 return rc;
96}
97
98/**
99 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
100 *
101 * Description:
102 * Invalidate the NetLabel security attribute mapping cache.
103 *
104 */
105void selinux_netlbl_cache_invalidate(void)
106{
107 netlbl_cache_invalidate();
108}
109
110/**
111 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
112 * @ssec: the sk_security_struct
113 * @family: the socket family
114 *
115 * Description:
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.
118 *
119 */
120void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
121 int family)
122{
Eric Parisa6aaafe2008-04-18 17:38:23 -0400123 if (family == PF_INET)
Paul Moore5778eab2007-02-28 15:14:22 -0500124 ssec->nlbl_state = NLBL_REQUIRE;
125 else
126 ssec->nlbl_state = NLBL_UNSET;
127}
128
129/**
Paul Moore5778eab2007-02-28 15:14:22 -0500130 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
131 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -0500132 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -0500133 * @type: NetLabel labeling protocol type
Paul Moore5778eab2007-02-28 15:14:22 -0500134 * @sid: the SID
135 *
136 * Description:
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.
140 *
141 */
Paul Moore75e22912008-01-29 08:38:04 -0500142int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
143 u16 family,
Paul Moore220deb92008-01-29 08:38:23 -0500144 u32 *type,
Paul Moore75e22912008-01-29 08:38:04 -0500145 u32 *sid)
Paul Moore5778eab2007-02-28 15:14:22 -0500146{
147 int rc;
148 struct netlbl_lsm_secattr secattr;
149
Paul Moore23bcdc12007-07-18 12:28:45 -0400150 if (!netlbl_enabled()) {
151 *sid = SECSID_NULL;
152 return 0;
153 }
154
Paul Moore5778eab2007-02-28 15:14:22 -0500155 netlbl_secattr_init(&secattr);
Paul Moore75e22912008-01-29 08:38:04 -0500156 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Paul Moore5dbe1eb2008-01-29 08:44:18 -0500157 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
158 rc = selinux_netlbl_sidlookup_cached(skb, &secattr, sid);
159 else
Paul Moore5778eab2007-02-28 15:14:22 -0500160 *sid = SECSID_NULL;
Paul Moore220deb92008-01-29 08:38:23 -0500161 *type = secattr.type;
Paul Moore5778eab2007-02-28 15:14:22 -0500162 netlbl_secattr_destroy(&secattr);
163
164 return rc;
165}
166
167/**
168 * selinux_netlbl_sock_graft - Netlabel the new socket
169 * @sk: the new connection
170 * @sock: the new socket
171 *
172 * Description:
173 * The connection represented by @sk is being grafted onto @sock so set the
174 * socket's NetLabel to match the SID of @sk.
175 *
176 */
177void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
178{
Paul Moore5778eab2007-02-28 15:14:22 -0500179 /* 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(). */
Paul Mooreaccc6092008-10-10 10:16:29 -0400182 selinux_netlbl_sock_setsid(sk);
Paul Moore5778eab2007-02-28 15:14:22 -0500183}
184
185/**
186 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
187 * @sock: the socket to label
188 *
189 * Description:
190 * Attempt to label a socket using the NetLabel mechanism using the given
191 * SID. Returns zero values on success, negative values on failure.
192 *
193 */
194int selinux_netlbl_socket_post_create(struct socket *sock)
195{
Paul Mooreaccc6092008-10-10 10:16:29 -0400196 return selinux_netlbl_sock_setsid(sock->sk);
Paul Moore5778eab2007-02-28 15:14:22 -0500197}
198
199/**
200 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
201 * @inode: the file descriptor's inode
202 * @mask: the permission mask
203 *
204 * Description:
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
208 * values on failure.
209 *
210 */
211int selinux_netlbl_inode_permission(struct inode *inode, int mask)
212{
213 int rc;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700214 struct sock *sk;
Paul Moore5778eab2007-02-28 15:14:22 -0500215 struct socket *sock;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700216 struct sk_security_struct *sksec;
Paul Moore5778eab2007-02-28 15:14:22 -0500217
218 if (!S_ISSOCK(inode->i_mode) ||
219 ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
220 return 0;
Paul Mooref74af6e2008-02-25 11:40:33 -0500221
Paul Moore5778eab2007-02-28 15:14:22 -0500222 sock = SOCKET_I(inode);
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700223 sk = sock->sk;
224 sksec = sk->sk_security;
Paul Mooref74af6e2008-02-25 11:40:33 -0500225 if (sksec->nlbl_state != NLBL_REQUIRE)
Paul Moore5778eab2007-02-28 15:14:22 -0500226 return 0;
Paul Mooref74af6e2008-02-25 11:40:33 -0500227
Paul Moore5778eab2007-02-28 15:14:22 -0500228 local_bh_disable();
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700229 bh_lock_sock_nested(sk);
Paul Mooref74af6e2008-02-25 11:40:33 -0500230 if (likely(sksec->nlbl_state == NLBL_REQUIRE))
Paul Mooreaccc6092008-10-10 10:16:29 -0400231 rc = selinux_netlbl_sock_setsid(sk);
Paul Mooref74af6e2008-02-25 11:40:33 -0500232 else
233 rc = 0;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700234 bh_unlock_sock(sk);
Paul Moore5778eab2007-02-28 15:14:22 -0500235 local_bh_enable();
Paul Moore5778eab2007-02-28 15:14:22 -0500236
237 return rc;
238}
239
240/**
241 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
242 * @sksec: the sock's sk_security_struct
243 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -0500244 * @family: protocol family
Paul Moore5778eab2007-02-28 15:14:22 -0500245 * @ad: the audit data
246 *
247 * Description:
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
250 * error.
251 *
252 */
253int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
254 struct sk_buff *skb,
Paul Moore75e22912008-01-29 08:38:04 -0500255 u16 family,
Paul Moore5778eab2007-02-28 15:14:22 -0500256 struct avc_audit_data *ad)
257{
258 int rc;
Paul Mooref36158c2007-07-18 12:28:46 -0400259 u32 nlbl_sid;
260 u32 perm;
261 struct netlbl_lsm_secattr secattr;
Paul Moore5778eab2007-02-28 15:14:22 -0500262
Paul Moore23bcdc12007-07-18 12:28:45 -0400263 if (!netlbl_enabled())
264 return 0;
265
Paul Mooref36158c2007-07-18 12:28:46 -0400266 netlbl_secattr_init(&secattr);
Paul Moore75e22912008-01-29 08:38:04 -0500267 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Paul Moore5dbe1eb2008-01-29 08:44:18 -0500268 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
269 rc = selinux_netlbl_sidlookup_cached(skb, &secattr, &nlbl_sid);
270 else
Paul Mooref36158c2007-07-18 12:28:46 -0400271 nlbl_sid = SECINITSID_UNLABELED;
272 netlbl_secattr_destroy(&secattr);
Paul Moore5778eab2007-02-28 15:14:22 -0500273 if (rc != 0)
274 return rc;
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700275
Paul Moore5778eab2007-02-28 15:14:22 -0500276 switch (sksec->sclass) {
277 case SECCLASS_UDP_SOCKET:
Paul Mooref36158c2007-07-18 12:28:46 -0400278 perm = UDP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500279 break;
280 case SECCLASS_TCP_SOCKET:
Paul Mooref36158c2007-07-18 12:28:46 -0400281 perm = TCP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500282 break;
283 default:
Paul Mooref36158c2007-07-18 12:28:46 -0400284 perm = RAWIP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500285 }
286
Paul Mooref36158c2007-07-18 12:28:46 -0400287 rc = avc_has_perm(sksec->sid, nlbl_sid, sksec->sclass, perm, ad);
Paul Moore5778eab2007-02-28 15:14:22 -0500288 if (rc == 0)
289 return 0;
290
Paul Mooref36158c2007-07-18 12:28:46 -0400291 if (nlbl_sid != SECINITSID_UNLABELED)
292 netlbl_skbuff_err(skb, rc);
Paul Moore5778eab2007-02-28 15:14:22 -0500293 return rc;
294}
295
296/**
297 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
298 * @sock: the socket
299 * @level: the socket level or protocol
300 * @optname: the socket option name
301 *
302 * Description:
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.
307 *
308 */
309int selinux_netlbl_socket_setsockopt(struct socket *sock,
310 int level,
311 int optname)
312{
313 int rc = 0;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700314 struct sock *sk = sock->sk;
315 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -0500316 struct netlbl_lsm_secattr secattr;
317
Paul Moore5778eab2007-02-28 15:14:22 -0500318 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
319 sksec->nlbl_state == NLBL_LABELED) {
320 netlbl_secattr_init(&secattr);
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700321 lock_sock(sk);
322 rc = netlbl_sock_getattr(sk, &secattr);
323 release_sock(sk);
Paul Moore5778eab2007-02-28 15:14:22 -0500324 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
325 rc = -EACCES;
326 netlbl_secattr_destroy(&secattr);
327 }
Paul Moore5778eab2007-02-28 15:14:22 -0500328
329 return rc;
330}