blob: 07283e1dfad24f97f900f2422b112000593b30d9 [file] [log] [blame]
Paul Moore96cb8e32006-08-03 16:48:59 -07001/*
2 * NetLabel Unlabeled Support
3 *
4 * This file defines functions for dealing with unlabeled packets for the
5 * NetLabel system. The NetLabel system manages static and dynamic label
6 * mappings for network protocols such as CIPSO and RIPSO.
7 *
8 * Author: Paul Moore <paul.moore@hp.com>
9 *
10 */
11
12/*
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <linux/types.h>
32#include <linux/rcupdate.h>
33#include <linux/list.h>
34#include <linux/spinlock.h>
35#include <linux/socket.h>
36#include <linux/string.h>
37#include <linux/skbuff.h>
38#include <net/sock.h>
39#include <net/netlink.h>
40#include <net/genetlink.h>
41
42#include <net/netlabel.h>
43#include <asm/bug.h>
44
45#include "netlabel_user.h"
46#include "netlabel_domainhash.h"
47#include "netlabel_unlabeled.h"
48
49/* Accept unlabeled packets flag */
Paul Moorecd287862006-11-17 17:38:44 -050050static DEFINE_SPINLOCK(netlabel_unlabel_acceptflg_lock);
51static u8 netlabel_unlabel_acceptflg = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -070052
53/* NetLabel Generic NETLINK CIPSOv4 family */
54static struct genl_family netlbl_unlabel_gnl_family = {
55 .id = GENL_ID_GENERATE,
56 .hdrsize = 0,
57 .name = NETLBL_NLTYPE_UNLABELED_NAME,
58 .version = NETLBL_PROTO_VERSION,
Paul Moorefd385852006-09-25 15:56:37 -070059 .maxattr = NLBL_UNLABEL_A_MAX,
Paul Moore96cb8e32006-08-03 16:48:59 -070060};
61
Paul Moorefd385852006-09-25 15:56:37 -070062/* NetLabel Netlink attribute policy */
63static struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
64 [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
65};
Paul Moore96cb8e32006-08-03 16:48:59 -070066
67/*
Paul Moore32f50cd2006-09-28 14:51:47 -070068 * Helper Functions
69 */
70
71/**
72 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
73 * @value: desired value
Paul Moore95d4e6b2006-09-29 17:05:05 -070074 * @audit_info: NetLabel audit information
Paul Moore32f50cd2006-09-28 14:51:47 -070075 *
76 * Description:
77 * Set the value of the unlabeled accept flag to @value.
78 *
79 */
Paul Moore95d4e6b2006-09-29 17:05:05 -070080static void netlbl_unlabel_acceptflg_set(u8 value,
81 struct netlbl_audit *audit_info)
Paul Moore32f50cd2006-09-28 14:51:47 -070082{
Paul Moore95d4e6b2006-09-29 17:05:05 -070083 struct audit_buffer *audit_buf;
84 u8 old_val;
85
Paul Moorecd287862006-11-17 17:38:44 -050086 rcu_read_lock();
87 old_val = netlabel_unlabel_acceptflg;
88 spin_lock(&netlabel_unlabel_acceptflg_lock);
89 netlabel_unlabel_acceptflg = value;
90 spin_unlock(&netlabel_unlabel_acceptflg_lock);
91 rcu_read_unlock();
Paul Moore95d4e6b2006-09-29 17:05:05 -070092
93 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
94 audit_info);
95 audit_log_format(audit_buf, " unlbl_accept=%u old=%u", value, old_val);
96 audit_log_end(audit_buf);
Paul Moore32f50cd2006-09-28 14:51:47 -070097}
98
99/*
Paul Moore96cb8e32006-08-03 16:48:59 -0700100 * NetLabel Command Handlers
101 */
102
103/**
104 * netlbl_unlabel_accept - Handle an ACCEPT message
105 * @skb: the NETLINK buffer
106 * @info: the Generic NETLINK info block
107 *
108 * Description:
109 * Process a user generated ACCEPT message and set the accept flag accordingly.
110 * Returns zero on success, negative values on failure.
111 *
112 */
113static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
114{
Paul Moorefd385852006-09-25 15:56:37 -0700115 u8 value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700116 struct netlbl_audit audit_info;
Paul Moore96cb8e32006-08-03 16:48:59 -0700117
Paul Moorefd385852006-09-25 15:56:37 -0700118 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
119 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
Paul Moore96cb8e32006-08-03 16:48:59 -0700120 if (value == 1 || value == 0) {
Paul Moore95d4e6b2006-09-29 17:05:05 -0700121 netlbl_netlink_auditinfo(skb, &audit_info);
122 netlbl_unlabel_acceptflg_set(value, &audit_info);
Paul Moore32f50cd2006-09-28 14:51:47 -0700123 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700124 }
125 }
126
Paul Moore32f50cd2006-09-28 14:51:47 -0700127 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700128}
129
130/**
131 * netlbl_unlabel_list - Handle a LIST message
132 * @skb: the NETLINK buffer
133 * @info: the Generic NETLINK info block
134 *
135 * Description:
136 * Process a user generated LIST message and respond with the current status.
137 * Returns zero on success, negative values on failure.
138 *
139 */
140static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
141{
Paul Moorefd385852006-09-25 15:56:37 -0700142 int ret_val = -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700143 struct sk_buff *ans_skb;
Paul Moorefd385852006-09-25 15:56:37 -0700144 void *data;
Paul Moore96cb8e32006-08-03 16:48:59 -0700145
Thomas Graf339bf982006-11-10 14:10:15 -0800146 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moore96cb8e32006-08-03 16:48:59 -0700147 if (ans_skb == NULL)
148 goto list_failure;
Thomas Graf17c157c2006-11-14 19:46:02 -0800149 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
150 0, NLBL_UNLABEL_C_LIST);
Paul Moorefd385852006-09-25 15:56:37 -0700151 if (data == NULL) {
152 ret_val = -ENOMEM;
Paul Moore96cb8e32006-08-03 16:48:59 -0700153 goto list_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700154 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700155
Paul Moorecd287862006-11-17 17:38:44 -0500156 rcu_read_lock();
Paul Moorefd385852006-09-25 15:56:37 -0700157 ret_val = nla_put_u8(ans_skb,
158 NLBL_UNLABEL_A_ACPTFLG,
Paul Moorecd287862006-11-17 17:38:44 -0500159 netlabel_unlabel_acceptflg);
160 rcu_read_unlock();
Paul Moore96cb8e32006-08-03 16:48:59 -0700161 if (ret_val != 0)
162 goto list_failure;
163
Paul Moorefd385852006-09-25 15:56:37 -0700164 genlmsg_end(ans_skb, data);
165
Thomas Graf81878d22006-11-14 19:45:27 -0800166 ret_val = genlmsg_reply(ans_skb, info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700167 if (ret_val != 0)
168 goto list_failure;
Paul Moore96cb8e32006-08-03 16:48:59 -0700169 return 0;
170
171list_failure:
Paul Moorefd385852006-09-25 15:56:37 -0700172 kfree(ans_skb);
Paul Moore96cb8e32006-08-03 16:48:59 -0700173 return ret_val;
174}
175
176
177/*
178 * NetLabel Generic NETLINK Command Definitions
179 */
180
181static struct genl_ops netlbl_unlabel_genl_c_accept = {
182 .cmd = NLBL_UNLABEL_C_ACCEPT,
Paul Moorefd385852006-09-25 15:56:37 -0700183 .flags = GENL_ADMIN_PERM,
184 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -0700185 .doit = netlbl_unlabel_accept,
186 .dumpit = NULL,
187};
188
189static struct genl_ops netlbl_unlabel_genl_c_list = {
190 .cmd = NLBL_UNLABEL_C_LIST,
191 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700192 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -0700193 .doit = netlbl_unlabel_list,
194 .dumpit = NULL,
195};
196
197
198/*
199 * NetLabel Generic NETLINK Protocol Functions
200 */
201
202/**
203 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
204 *
205 * Description:
206 * Register the unlabeled packet NetLabel component with the Generic NETLINK
207 * mechanism. Returns zero on success, negative values on failure.
208 *
209 */
210int netlbl_unlabel_genl_init(void)
211{
212 int ret_val;
213
214 ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
215 if (ret_val != 0)
216 return ret_val;
217
218 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
219 &netlbl_unlabel_genl_c_accept);
220 if (ret_val != 0)
221 return ret_val;
222
223 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
224 &netlbl_unlabel_genl_c_list);
225 if (ret_val != 0)
226 return ret_val;
227
228 return 0;
229}
230
231/*
232 * NetLabel KAPI Hooks
233 */
234
235/**
236 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
237 * @secattr: the security attributes
238 *
239 * Description:
240 * Determine the security attributes, if any, for an unlabled packet and return
241 * them in @secattr. Returns zero on success and negative values on failure.
242 *
243 */
244int netlbl_unlabel_getattr(struct netlbl_lsm_secattr *secattr)
245{
Paul Moorecd287862006-11-17 17:38:44 -0500246 int ret_val;
Paul Moore96cb8e32006-08-03 16:48:59 -0700247
Paul Moorecd287862006-11-17 17:38:44 -0500248 rcu_read_lock();
249 if (netlabel_unlabel_acceptflg == 1) {
250 netlbl_secattr_init(secattr);
251 ret_val = 0;
252 } else
253 ret_val = -ENOMSG;
254 rcu_read_unlock();
255
256 return ret_val;
Paul Moore96cb8e32006-08-03 16:48:59 -0700257}
258
259/**
260 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
261 *
262 * Description:
263 * Set the default NetLabel configuration to allow incoming unlabeled packets
264 * and to send unlabeled network traffic by default.
265 *
266 */
267int netlbl_unlabel_defconf(void)
268{
269 int ret_val;
270 struct netlbl_dom_map *entry;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700271 struct netlbl_audit audit_info;
Paul Moore32f50cd2006-09-28 14:51:47 -0700272
Paul Moore95d4e6b2006-09-29 17:05:05 -0700273 /* Only the kernel is allowed to call this function and the only time
274 * it is called is at bootup before the audit subsystem is reporting
275 * messages so don't worry to much about these values. */
276 security_task_getsecid(current, &audit_info.secid);
277 audit_info.loginuid = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700278
279 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
280 if (entry == NULL)
281 return -ENOMEM;
282 entry->type = NETLBL_NLTYPE_UNLABELED;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700283 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700284 if (ret_val != 0)
285 return ret_val;
286
Paul Moore95d4e6b2006-09-29 17:05:05 -0700287 netlbl_unlabel_acceptflg_set(1, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700288
289 return 0;
290}