blob: ed9155b29c1a9bf6eebedec3311a0971dc596342 [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
56 rc = security_netlbl_sid_to_secattr(sid, &secattr);
57 if (rc != 0)
58 return rc;
59
Paul Mooreba6ff9f2007-06-07 18:37:15 -070060 rc = netlbl_sock_setattr(sk, &secattr);
Paul Moore5778eab2007-02-28 15:14:22 -050061 if (rc == 0) {
62 spin_lock_bh(&sksec->nlbl_lock);
63 sksec->nlbl_state = NLBL_LABELED;
64 spin_unlock_bh(&sksec->nlbl_lock);
65 }
66
67 return rc;
68}
69
70/**
71 * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
72 *
73 * Description:
74 * Invalidate the NetLabel security attribute mapping cache.
75 *
76 */
77void selinux_netlbl_cache_invalidate(void)
78{
79 netlbl_cache_invalidate();
80}
81
82/**
83 * selinux_netlbl_sk_security_reset - Reset the NetLabel fields
84 * @ssec: the sk_security_struct
85 * @family: the socket family
86 *
87 * Description:
88 * Called when the NetLabel state of a sk_security_struct needs to be reset.
89 * The caller is responsibile for all the NetLabel sk_security_struct locking.
90 *
91 */
92void selinux_netlbl_sk_security_reset(struct sk_security_struct *ssec,
93 int family)
94{
95 if (family == PF_INET)
96 ssec->nlbl_state = NLBL_REQUIRE;
97 else
98 ssec->nlbl_state = NLBL_UNSET;
99}
100
101/**
102 * selinux_netlbl_sk_security_init - Setup the NetLabel fields
103 * @ssec: the sk_security_struct
104 * @family: the socket family
105 *
106 * Description:
107 * Called when a new sk_security_struct is allocated to initialize the NetLabel
108 * fields.
109 *
110 */
111void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
112 int family)
113{
114 /* No locking needed, we are the only one who has access to ssec */
115 selinux_netlbl_sk_security_reset(ssec, family);
116 spin_lock_init(&ssec->nlbl_lock);
117}
118
119/**
120 * selinux_netlbl_sk_security_clone - Copy the NetLabel fields
121 * @ssec: the original sk_security_struct
122 * @newssec: the cloned sk_security_struct
123 *
124 * Description:
125 * Clone the NetLabel specific sk_security_struct fields from @ssec to
126 * @newssec.
127 *
128 */
129void selinux_netlbl_sk_security_clone(struct sk_security_struct *ssec,
130 struct sk_security_struct *newssec)
131{
132 /* We don't need to take newssec->nlbl_lock because we are the only
133 * thread with access to newssec, but we do need to take the RCU read
134 * lock as other threads could have access to ssec */
135 rcu_read_lock();
136 selinux_netlbl_sk_security_reset(newssec, ssec->sk->sk_family);
137 newssec->sclass = ssec->sclass;
138 rcu_read_unlock();
139}
140
141/**
142 * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
143 * @skb: the packet
144 * @base_sid: the SELinux SID to use as a context for MLS only attributes
145 * @sid: the SID
146 *
147 * Description:
148 * Call the NetLabel mechanism to get the security attributes of the given
149 * packet and use those attributes to determine the correct context/SID to
150 * assign to the packet. Returns zero on success, negative values on failure.
151 *
152 */
153int selinux_netlbl_skbuff_getsid(struct sk_buff *skb, u32 base_sid, u32 *sid)
154{
155 int rc;
156 struct netlbl_lsm_secattr secattr;
157
Paul Moore23bcdc12007-07-18 12:28:45 -0400158 if (!netlbl_enabled()) {
159 *sid = SECSID_NULL;
160 return 0;
161 }
162
Paul Moore5778eab2007-02-28 15:14:22 -0500163 netlbl_secattr_init(&secattr);
164 rc = netlbl_skbuff_getattr(skb, &secattr);
165 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700166 rc = security_netlbl_secattr_to_sid(&secattr,
167 base_sid,
168 sid);
Paul Moore5778eab2007-02-28 15:14:22 -0500169 else
170 *sid = SECSID_NULL;
171 netlbl_secattr_destroy(&secattr);
172
173 return rc;
174}
175
176/**
177 * selinux_netlbl_sock_graft - Netlabel the new socket
178 * @sk: the new connection
179 * @sock: the new socket
180 *
181 * Description:
182 * The connection represented by @sk is being grafted onto @sock so set the
183 * socket's NetLabel to match the SID of @sk.
184 *
185 */
186void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
187{
188 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
189 struct sk_security_struct *sksec = sk->sk_security;
190 struct netlbl_lsm_secattr secattr;
191 u32 nlbl_peer_sid;
192
193 sksec->sclass = isec->sclass;
194
195 rcu_read_lock();
196
197 if (sksec->nlbl_state != NLBL_REQUIRE) {
198 rcu_read_unlock();
199 return;
200 }
201
202 netlbl_secattr_init(&secattr);
203 if (netlbl_sock_getattr(sk, &secattr) == 0 &&
204 secattr.flags != NETLBL_SECATTR_NONE &&
205 security_netlbl_secattr_to_sid(&secattr,
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700206 SECINITSID_UNLABELED,
Paul Moore5778eab2007-02-28 15:14:22 -0500207 &nlbl_peer_sid) == 0)
208 sksec->peer_sid = nlbl_peer_sid;
209 netlbl_secattr_destroy(&secattr);
210
211 /* Try to set the NetLabel on the socket to save time later, if we fail
212 * here we will pick up the pieces in later calls to
213 * selinux_netlbl_inode_permission(). */
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700214 selinux_netlbl_sock_setsid(sk, sksec->sid);
Paul Moore5778eab2007-02-28 15:14:22 -0500215
216 rcu_read_unlock();
217}
218
219/**
220 * selinux_netlbl_socket_post_create - Label a socket using NetLabel
221 * @sock: the socket to label
222 *
223 * Description:
224 * Attempt to label a socket using the NetLabel mechanism using the given
225 * SID. Returns zero values on success, negative values on failure.
226 *
227 */
228int selinux_netlbl_socket_post_create(struct socket *sock)
229{
230 int rc = 0;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700231 struct sock *sk = sock->sk;
Paul Moore5778eab2007-02-28 15:14:22 -0500232 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700233 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -0500234
235 sksec->sclass = isec->sclass;
236
237 rcu_read_lock();
238 if (sksec->nlbl_state == NLBL_REQUIRE)
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700239 rc = selinux_netlbl_sock_setsid(sk, sksec->sid);
Paul Moore5778eab2007-02-28 15:14:22 -0500240 rcu_read_unlock();
241
242 return rc;
243}
244
245/**
246 * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
247 * @inode: the file descriptor's inode
248 * @mask: the permission mask
249 *
250 * Description:
251 * Looks at a file's inode and if it is marked as a socket protected by
252 * NetLabel then verify that the socket has been labeled, if not try to label
253 * the socket now with the inode's SID. Returns zero on success, negative
254 * values on failure.
255 *
256 */
257int selinux_netlbl_inode_permission(struct inode *inode, int mask)
258{
259 int rc;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700260 struct sock *sk;
Paul Moore5778eab2007-02-28 15:14:22 -0500261 struct socket *sock;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700262 struct sk_security_struct *sksec;
Paul Moore5778eab2007-02-28 15:14:22 -0500263
264 if (!S_ISSOCK(inode->i_mode) ||
265 ((mask & (MAY_WRITE | MAY_APPEND)) == 0))
266 return 0;
267 sock = SOCKET_I(inode);
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700268 sk = sock->sk;
269 sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -0500270
271 rcu_read_lock();
272 if (sksec->nlbl_state != NLBL_REQUIRE) {
273 rcu_read_unlock();
274 return 0;
275 }
276 local_bh_disable();
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700277 bh_lock_sock_nested(sk);
278 rc = selinux_netlbl_sock_setsid(sk, sksec->sid);
279 bh_unlock_sock(sk);
Paul Moore5778eab2007-02-28 15:14:22 -0500280 local_bh_enable();
281 rcu_read_unlock();
282
283 return rc;
284}
285
286/**
287 * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
288 * @sksec: the sock's sk_security_struct
289 * @skb: the packet
290 * @ad: the audit data
291 *
292 * Description:
293 * Fetch the NetLabel security attributes from @skb and perform an access check
294 * against the receiving socket. Returns zero on success, negative values on
295 * error.
296 *
297 */
298int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
299 struct sk_buff *skb,
300 struct avc_audit_data *ad)
301{
302 int rc;
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700303 u32 netlbl_sid;
304 u32 recv_perm;
Paul Moore5778eab2007-02-28 15:14:22 -0500305
Paul Moore23bcdc12007-07-18 12:28:45 -0400306 if (!netlbl_enabled())
307 return 0;
308
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700309 rc = selinux_netlbl_skbuff_getsid(skb,
310 SECINITSID_UNLABELED,
311 &netlbl_sid);
Paul Moore5778eab2007-02-28 15:14:22 -0500312 if (rc != 0)
313 return rc;
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700314
315 if (netlbl_sid == SECSID_NULL)
316 return 0;
Paul Moore5778eab2007-02-28 15:14:22 -0500317
318 switch (sksec->sclass) {
319 case SECCLASS_UDP_SOCKET:
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700320 recv_perm = UDP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500321 break;
322 case SECCLASS_TCP_SOCKET:
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700323 recv_perm = TCP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500324 break;
325 default:
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700326 recv_perm = RAWIP_SOCKET__RECVFROM;
Paul Moore5778eab2007-02-28 15:14:22 -0500327 }
328
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700329 rc = avc_has_perm(sksec->sid,
330 netlbl_sid,
331 sksec->sclass,
332 recv_perm,
333 ad);
Paul Moore5778eab2007-02-28 15:14:22 -0500334 if (rc == 0)
335 return 0;
336
Linus Torvalds8d9107e2007-07-13 16:53:18 -0700337 netlbl_skbuff_err(skb, rc);
Paul Moore5778eab2007-02-28 15:14:22 -0500338 return rc;
339}
340
341/**
342 * selinux_netlbl_socket_setsockopt - Do not allow users to remove a NetLabel
343 * @sock: the socket
344 * @level: the socket level or protocol
345 * @optname: the socket option name
346 *
347 * Description:
348 * Check the setsockopt() call and if the user is trying to replace the IP
349 * options on a socket and a NetLabel is in place for the socket deny the
350 * access; otherwise allow the access. Returns zero when the access is
351 * allowed, -EACCES when denied, and other negative values on error.
352 *
353 */
354int selinux_netlbl_socket_setsockopt(struct socket *sock,
355 int level,
356 int optname)
357{
358 int rc = 0;
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700359 struct sock *sk = sock->sk;
360 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore5778eab2007-02-28 15:14:22 -0500361 struct netlbl_lsm_secattr secattr;
362
363 rcu_read_lock();
364 if (level == IPPROTO_IP && optname == IP_OPTIONS &&
365 sksec->nlbl_state == NLBL_LABELED) {
366 netlbl_secattr_init(&secattr);
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700367 lock_sock(sk);
368 rc = netlbl_sock_getattr(sk, &secattr);
369 release_sock(sk);
Paul Moore5778eab2007-02-28 15:14:22 -0500370 if (rc == 0 && secattr.flags != NETLBL_SECATTR_NONE)
371 rc = -EACCES;
372 netlbl_secattr_destroy(&secattr);
373 }
374 rcu_read_unlock();
375
376 return rc;
377}