blob: f4bcbf12a4c945125e55987813ba5a701449b8c6 [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"
37
38/**
Paul Mooreba6ff9f2007-06-07 18:37:15 -070039 * selinux_netlbl_sock_setsid - Label a socket using the NetLabel mechanism
40 * @sk: the socket to label
Paul Moore5778eab2007-02-28 15:14:22 -050041 * @sid: the SID to use
42 *
43 * Description:
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.
48 *
49 */
Paul Mooreba6ff9f2007-06-07 18:37:15 -070050static int selinux_netlbl_sock_setsid(struct sock *sk, u32 sid)
Paul Moore5778eab2007-02-28 15:14:22 -050051{
52 int rc;
Paul Mooreba6ff9f2007-06-07 18:37:15 -070053 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -050054 struct netlbl_lsm_secattr secattr;
55
Paul Moore45c950e2008-01-22 09:31:00 +110056 netlbl_secattr_init(&secattr);
57
Paul Moore5778eab2007-02-28 15:14:22 -050058 rc = security_netlbl_sid_to_secattr(sid, &secattr);
59 if (rc != 0)
Paul Moore45c950e2008-01-22 09:31:00 +110060 goto sock_setsid_return;
Paul Mooreba6ff9f2007-06-07 18:37:15 -070061 rc = netlbl_sock_setattr(sk, &secattr);
Paul Moore5778eab2007-02-28 15:14:22 -050062 if (rc == 0) {
63 spin_lock_bh(&sksec->nlbl_lock);
64 sksec->nlbl_state = NLBL_LABELED;
65 spin_unlock_bh(&sksec->nlbl_lock);
66 }
67
Paul Moore45c950e2008-01-22 09:31:00 +110068sock_setsid_return:
69 netlbl_secattr_destroy(&secattr);
Paul Moore5778eab2007-02-28 15:14:22 -050070 return rc;
71}
72
73/**
74 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
75 *
76 * Description:
77 * Invalidate the NetLabel security attribute mapping cache.
78 *
79 */
80void selinux_netlbl_cache_invalidate(void)
81{
82 netlbl_cache_invalidate();
83}
84
85/**
86 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
87 * @ssec: the sk_security_struct
88 * @family: the socket family
89 *
90 * Description:
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.
93 *
94 */
95void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
96 int family)
97{
98 if (family == PF_INET)
99 ssec->nlbl_state = NLBL_REQUIRE;
100 else
101 ssec->nlbl_state = NLBL_UNSET;
102}
103
104/**
105 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
106 * @ssec: the sk_security_struct
107 * @family: the socket family
108 *
109 * Description:
110 * Called when a new sk_security_struct is allocated to initialize the NetLabel
111 * fields.
112 *
113 */
114void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
115 int family)
116{
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);
120}
121
122/**
123 * selinux_netlbl_sk_security_clone - Copy the NetLabel fields
124 * @ssec: the original sk_security_struct
125 * @newssec: the cloned sk_security_struct
126 *
127 * Description:
128 * Clone the NetLabel specific sk_security_struct fields from @ssec to
129 * @newssec.
130 *
131 */
132void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
133 struct sk_security_struct *newssec)
134{
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 */
138 rcu_read_lock();
139 selinux_netlbl_sk_security_reset(newssec, ssec->sk->sk_family);
140 newssec->sclass = ssec->sclass;
141 rcu_read_unlock();
142}
143
144/**
145 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
146 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -0500147 * @family: protocol family
Paul Moore5778eab2007-02-28 15:14:22 -0500148 * @base_sid: the SELinux SID to use as a context for MLS only attributes
149 * @sid: the SID
150 *
151 * Description:
152 * Call the NetLabel mechanism to get the security attributes of the given
153 * packet and use those attributes to determine the correct context/SID to
154 * assign to the packet. Returns zero on success, negative values on failure.
155 *
156 */
Paul Moore75e22912008-01-29 08:38:04 -0500157int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
158 u16 family,
159 u32 base_sid,
160 u32 *sid)
Paul Moore5778eab2007-02-28 15:14:22 -0500161{
162 int rc;
163 struct netlbl_lsm_secattr secattr;
164
Paul Moore23bcdc12007-07-18 12:28:45 -0400165 if (!netlbl_enabled()) {
166 *sid = SECSID_NULL;
167 return 0;
168 }
169
Paul Moore5778eab2007-02-28 15:14:22 -0500170 netlbl_secattr_init(&secattr);
Paul Moore75e22912008-01-29 08:38:04 -0500171 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Paul Moore9534f712007-07-30 16:33:26 -0400172 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) {
Paul Mooref36158c2007-07-18 12:28:46 -0400173 rc = security_netlbl_secattr_to_sid(&secattr, base_sid, sid);
Paul Moore9534f712007-07-30 16:33:26 -0400174 if (rc == 0 &&
175 (secattr.flags & NETLBL_SECATTR_CACHEABLE) &&
176 (secattr.flags & NETLBL_SECATTR_CACHE))
177 netlbl_cache_add(skb, &secattr);
178 } else
Paul Moore5778eab2007-02-28 15:14:22 -0500179 *sid = SECSID_NULL;
180 netlbl_secattr_destroy(&secattr);
181
182 return rc;
183}
184
185/**
186 * selinux_netlbl_sock_graft - Netlabel the new socket
187 * @sk: the new connection
188 * @sock: the new socket
189 *
190 * Description:
191 * The connection represented by @sk is being grafted onto @sock so set the
192 * socket's NetLabel to match the SID of @sk.
193 *
194 */
195void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
196{
197 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
198 struct sk_security_struct *sksec = sk->sk_security;
199 struct netlbl_lsm_secattr secattr;
200 u32 nlbl_peer_sid;
201
202 sksec->sclass = isec->sclass;
203
204 rcu_read_lock();
205
206 if (sksec->nlbl_state != NLBL_REQUIRE) {
207 rcu_read_unlock();
208 return;
209 }
210
211 netlbl_secattr_init(&secattr);
212 if (netlbl_sock_getattr(sk, &secattr) == 0 &&
213 secattr.flags != NETLBL_SECATTR_NONE &&
214 security_netlbl_secattr_to_sid(&secattr,
Paul Mooref36158c2007-07-18 12:28:46 -0400215 SECINITSID_NETMSG,
Paul Moore5778eab2007-02-28 15:14:22 -0500216 &nlbl_peer_sid) == 0)
217 sksec->peer_sid = nlbl_peer_sid;
218 netlbl_secattr_destroy(&secattr);
219
220 /* Try to set the NetLabel on the socket to save time later, if we fail
221 * here we will pick up the pieces in later calls to
222 * selinux_netlbl_inode_permission(). */
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700223 selinux_netlbl_sock_setsid(sk, sksec->sid);
Paul Moore5778eab2007-02-28 15:14:22 -0500224
225 rcu_read_unlock();
226}
227
228/**
229 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
230 * @sock: the socket to label
231 *
232 * Description:
233 * Attempt to label a socket using the NetLabel mechanism using the given
234 * SID. Returns zero values on success, negative values on failure.
235 *
236 */
237int selinux_netlbl_socket_post_create(struct socket *sock)
238{
239 int rc = 0;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700240 struct sock *sk = sock->sk;
Paul Moore5778eab2007-02-28 15:14:22 -0500241 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700242 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -0500243
244 sksec->sclass = isec->sclass;
245
246 rcu_read_lock();
247 if (sksec->nlbl_state == NLBL_REQUIRE)
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700248 rc = selinux_netlbl_sock_setsid(sk, sksec->sid);
Paul Moore5778eab2007-02-28 15:14:22 -0500249 rcu_read_unlock();
250
251 return rc;
252}
253
254/**
255 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
256 * @inode: the file descriptor's inode
257 * @mask: the permission mask
258 *
259 * Description:
260 * Looks at a file's inode and if it is marked as a socket protected by
261 * NetLabel then verify that the socket has been labeled, if not try to label
262 * the socket now with the inode's SID. Returns zero on success, negative
263 * values on failure.
264 *
265 */
266int selinux_netlbl_inode_permission(struct inode *inode, int mask)
267{
268 int rc;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700269 struct sock *sk;
Paul Moore5778eab2007-02-28 15:14:22 -0500270 struct socket *sock;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700271 struct sk_security_struct *sksec;
Paul Moore5778eab2007-02-28 15:14:22 -0500272
273 if (!S_ISSOCK(inode->i_mode) ||
274 ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
275 return 0;
276 sock = SOCKET_I(inode);
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700277 sk = sock->sk;
278 sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -0500279
280 rcu_read_lock();
281 if (sksec->nlbl_state != NLBL_REQUIRE) {
282 rcu_read_unlock();
283 return 0;
284 }
285 local_bh_disable();
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700286 bh_lock_sock_nested(sk);
287 rc = selinux_netlbl_sock_setsid(sk, sksec->sid);
288 bh_unlock_sock(sk);
Paul Moore5778eab2007-02-28 15:14:22 -0500289 local_bh_enable();
290 rcu_read_unlock();
291
292 return rc;
293}
294
295/**
296 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
297 * @sksec: the sock's sk_security_struct
298 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -0500299 * @family: protocol family
Paul Moore5778eab2007-02-28 15:14:22 -0500300 * @ad: the audit data
301 *
302 * Description:
303 * Fetch the NetLabel security attributes from @skb and perform an access check
304 * against the receiving socket. Returns zero on success, negative values on
305 * error.
306 *
307 */
308int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
309 struct sk_buff *skb,
Paul Moore75e22912008-01-29 08:38:04 -0500310 u16 family,
Paul Moore5778eab2007-02-28 15:14:22 -0500311 struct avc_audit_data *ad)
312{
313 int rc;
Paul Mooref36158c2007-07-18 12:28:46 -0400314 u32 nlbl_sid;
315 u32 perm;
316 struct netlbl_lsm_secattr secattr;
Paul Moore5778eab2007-02-28 15:14:22 -0500317
Paul Moore23bcdc12007-07-18 12:28:45 -0400318 if (!netlbl_enabled())
319 return 0;
320
Paul Mooref36158c2007-07-18 12:28:46 -0400321 netlbl_secattr_init(&secattr);
Paul Moore75e22912008-01-29 08:38:04 -0500322 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Paul Moore9534f712007-07-30 16:33:26 -0400323 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE) {
Paul Mooref36158c2007-07-18 12:28:46 -0400324 rc = security_netlbl_secattr_to_sid(&secattr,
325 SECINITSID_NETMSG,
326 &nlbl_sid);
Paul Moore9534f712007-07-30 16:33:26 -0400327 if (rc == 0 &&
328 (secattr.flags & NETLBL_SECATTR_CACHEABLE) &&
329 (secattr.flags & NETLBL_SECATTR_CACHE))
330 netlbl_cache_add(skb, &secattr);
331 } else
Paul Mooref36158c2007-07-18 12:28:46 -0400332 nlbl_sid = SECINITSID_UNLABELED;
333 netlbl_secattr_destroy(&secattr);
Paul Moore5778eab2007-02-28 15:14:22 -0500334 if (rc != 0)
335 return rc;
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700336
Paul Moore5778eab2007-02-28 15:14:22 -0500337 switch (sksec->sclass) {
338 case SECCLASS_UDP_SOCKET:
Paul Mooref36158c2007-07-18 12:28:46 -0400339 perm = UDP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500340 break;
341 case SECCLASS_TCP_SOCKET:
Paul Mooref36158c2007-07-18 12:28:46 -0400342 perm = TCP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500343 break;
344 default:
Paul Mooref36158c2007-07-18 12:28:46 -0400345 perm = RAWIP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500346 }
347
Paul Mooref36158c2007-07-18 12:28:46 -0400348 rc = avc_has_perm(sksec->sid, nlbl_sid, sksec->sclass, perm, ad);
Paul Moore5778eab2007-02-28 15:14:22 -0500349 if (rc == 0)
350 return 0;
351
Paul Mooref36158c2007-07-18 12:28:46 -0400352 if (nlbl_sid != SECINITSID_UNLABELED)
353 netlbl_skbuff_err(skb, rc);
Paul Moore5778eab2007-02-28 15:14:22 -0500354 return rc;
355}
356
357/**
358 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
359 * @sock: the socket
360 * @level: the socket level or protocol
361 * @optname: the socket option name
362 *
363 * Description:
364 * Check the setsockopt() call and if the user is trying to replace the IP
365 * options on a socket and a NetLabel is in place for the socket deny the
366 * access; otherwise allow the access. Returns zero when the access is
367 * allowed, -EACCES when denied, and other negative values on error.
368 *
369 */
370int selinux_netlbl_socket_setsockopt(struct socket *sock,
371 int level,
372 int optname)
373{
374 int rc = 0;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700375 struct sock *sk = sock->sk;
376 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -0500377 struct netlbl_lsm_secattr secattr;
378
379 rcu_read_lock();
380 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
381 sksec->nlbl_state == NLBL_LABELED) {
382 netlbl_secattr_init(&secattr);
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700383 lock_sock(sk);
384 rc = netlbl_sock_getattr(sk, &secattr);
385 release_sock(sk);
Paul Moore5778eab2007-02-28 15:14:22 -0500386 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
387 rc = -EACCES;
388 netlbl_secattr_destroy(&secattr);
389 }
390 rcu_read_unlock();
391
392 return rc;
393}