Commit | Line | Data |
---|---|---|
96cb8e33 PM |
1 | /* |
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> | |
96cb8e33 PM |
32 | #include <linux/list.h> |
33 | #include <linux/spinlock.h> | |
34 | #include <linux/socket.h> | |
35 | #include <linux/string.h> | |
36 | #include <linux/skbuff.h> | |
de64688f | 37 | #include <linux/audit.h> |
96cb8e33 PM |
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 */ | |
cd28786d | 50 | static u8 netlabel_unlabel_acceptflg = 0; |
96cb8e33 PM |
51 | |
52 | /* NetLabel Generic NETLINK CIPSOv4 family */ | |
53 | static struct genl_family netlbl_unlabel_gnl_family = { | |
54 | .id = GENL_ID_GENERATE, | |
55 | .hdrsize = 0, | |
56 | .name = NETLBL_NLTYPE_UNLABELED_NAME, | |
57 | .version = NETLBL_PROTO_VERSION, | |
fd385855 | 58 | .maxattr = NLBL_UNLABEL_A_MAX, |
96cb8e33 PM |
59 | }; |
60 | ||
fd385855 | 61 | /* NetLabel Netlink attribute policy */ |
ef7c79ed | 62 | static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = { |
fd385855 PM |
63 | [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 }, |
64 | }; | |
96cb8e33 | 65 | |
32f50cde PM |
66 | /* |
67 | * Helper Functions | |
68 | */ | |
69 | ||
70 | /** | |
71 | * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag | |
72 | * @value: desired value | |
95d4e6be | 73 | * @audit_info: NetLabel audit information |
32f50cde PM |
74 | * |
75 | * Description: | |
76 | * Set the value of the unlabeled accept flag to @value. | |
77 | * | |
78 | */ | |
95d4e6be PM |
79 | static void netlbl_unlabel_acceptflg_set(u8 value, |
80 | struct netlbl_audit *audit_info) | |
32f50cde | 81 | { |
95d4e6be PM |
82 | struct audit_buffer *audit_buf; |
83 | u8 old_val; | |
84 | ||
4be2700f | 85 | old_val = netlabel_unlabel_acceptflg; |
cd28786d | 86 | netlabel_unlabel_acceptflg = value; |
95d4e6be PM |
87 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW, |
88 | audit_info); | |
de64688f PM |
89 | if (audit_buf != NULL) { |
90 | audit_log_format(audit_buf, | |
91 | " unlbl_accept=%u old=%u", value, old_val); | |
92 | audit_log_end(audit_buf); | |
93 | } | |
32f50cde PM |
94 | } |
95 | ||
96cb8e33 PM |
96 | /* |
97 | * NetLabel Command Handlers | |
98 | */ | |
99 | ||
100 | /** | |
101 | * netlbl_unlabel_accept - Handle an ACCEPT message | |
102 | * @skb: the NETLINK buffer | |
103 | * @info: the Generic NETLINK info block | |
104 | * | |
105 | * Description: | |
106 | * Process a user generated ACCEPT message and set the accept flag accordingly. | |
107 | * Returns zero on success, negative values on failure. | |
108 | * | |
109 | */ | |
110 | static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info) | |
111 | { | |
fd385855 | 112 | u8 value; |
95d4e6be | 113 | struct netlbl_audit audit_info; |
96cb8e33 | 114 | |
fd385855 PM |
115 | if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) { |
116 | value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]); | |
96cb8e33 | 117 | if (value == 1 || value == 0) { |
95d4e6be PM |
118 | netlbl_netlink_auditinfo(skb, &audit_info); |
119 | netlbl_unlabel_acceptflg_set(value, &audit_info); | |
32f50cde | 120 | return 0; |
96cb8e33 PM |
121 | } |
122 | } | |
123 | ||
32f50cde | 124 | return -EINVAL; |
96cb8e33 PM |
125 | } |
126 | ||
127 | /** | |
128 | * netlbl_unlabel_list - Handle a LIST message | |
129 | * @skb: the NETLINK buffer | |
130 | * @info: the Generic NETLINK info block | |
131 | * | |
132 | * Description: | |
133 | * Process a user generated LIST message and respond with the current status. | |
134 | * Returns zero on success, negative values on failure. | |
135 | * | |
136 | */ | |
137 | static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info) | |
138 | { | |
fd385855 | 139 | int ret_val = -EINVAL; |
96cb8e33 | 140 | struct sk_buff *ans_skb; |
fd385855 | 141 | void *data; |
96cb8e33 | 142 | |
339bf98f | 143 | ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
96cb8e33 PM |
144 | if (ans_skb == NULL) |
145 | goto list_failure; | |
17c157c8 TG |
146 | data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family, |
147 | 0, NLBL_UNLABEL_C_LIST); | |
fd385855 PM |
148 | if (data == NULL) { |
149 | ret_val = -ENOMEM; | |
96cb8e33 | 150 | goto list_failure; |
fd385855 | 151 | } |
96cb8e33 | 152 | |
fd385855 PM |
153 | ret_val = nla_put_u8(ans_skb, |
154 | NLBL_UNLABEL_A_ACPTFLG, | |
cd28786d | 155 | netlabel_unlabel_acceptflg); |
96cb8e33 PM |
156 | if (ret_val != 0) |
157 | goto list_failure; | |
158 | ||
fd385855 PM |
159 | genlmsg_end(ans_skb, data); |
160 | ||
81878d27 | 161 | ret_val = genlmsg_reply(ans_skb, info); |
96cb8e33 PM |
162 | if (ret_val != 0) |
163 | goto list_failure; | |
96cb8e33 PM |
164 | return 0; |
165 | ||
166 | list_failure: | |
b08d5840 | 167 | kfree_skb(ans_skb); |
96cb8e33 PM |
168 | return ret_val; |
169 | } | |
170 | ||
171 | ||
172 | /* | |
173 | * NetLabel Generic NETLINK Command Definitions | |
174 | */ | |
175 | ||
176 | static struct genl_ops netlbl_unlabel_genl_c_accept = { | |
177 | .cmd = NLBL_UNLABEL_C_ACCEPT, | |
fd385855 PM |
178 | .flags = GENL_ADMIN_PERM, |
179 | .policy = netlbl_unlabel_genl_policy, | |
96cb8e33 PM |
180 | .doit = netlbl_unlabel_accept, |
181 | .dumpit = NULL, | |
182 | }; | |
183 | ||
184 | static struct genl_ops netlbl_unlabel_genl_c_list = { | |
185 | .cmd = NLBL_UNLABEL_C_LIST, | |
186 | .flags = 0, | |
fd385855 | 187 | .policy = netlbl_unlabel_genl_policy, |
96cb8e33 PM |
188 | .doit = netlbl_unlabel_list, |
189 | .dumpit = NULL, | |
190 | }; | |
191 | ||
192 | ||
193 | /* | |
194 | * NetLabel Generic NETLINK Protocol Functions | |
195 | */ | |
196 | ||
197 | /** | |
198 | * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component | |
199 | * | |
200 | * Description: | |
201 | * Register the unlabeled packet NetLabel component with the Generic NETLINK | |
202 | * mechanism. Returns zero on success, negative values on failure. | |
203 | * | |
204 | */ | |
205 | int netlbl_unlabel_genl_init(void) | |
206 | { | |
207 | int ret_val; | |
208 | ||
209 | ret_val = genl_register_family(&netlbl_unlabel_gnl_family); | |
210 | if (ret_val != 0) | |
211 | return ret_val; | |
212 | ||
213 | ret_val = genl_register_ops(&netlbl_unlabel_gnl_family, | |
214 | &netlbl_unlabel_genl_c_accept); | |
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_list); | |
220 | if (ret_val != 0) | |
221 | return ret_val; | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
226 | /* | |
227 | * NetLabel KAPI Hooks | |
228 | */ | |
229 | ||
230 | /** | |
231 | * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet | |
232 | * @secattr: the security attributes | |
233 | * | |
234 | * Description: | |
235 | * Determine the security attributes, if any, for an unlabled packet and return | |
236 | * them in @secattr. Returns zero on success and negative values on failure. | |
237 | * | |
238 | */ | |
239 | int netlbl_unlabel_getattr(struct netlbl_lsm_secattr *secattr) | |
240 | { | |
c783f1ce PM |
241 | if (netlabel_unlabel_acceptflg == 0) |
242 | return -ENOMSG; | |
243 | netlbl_secattr_init(secattr); | |
244 | return 0; | |
96cb8e33 PM |
245 | } |
246 | ||
247 | /** | |
248 | * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets | |
249 | * | |
250 | * Description: | |
251 | * Set the default NetLabel configuration to allow incoming unlabeled packets | |
252 | * and to send unlabeled network traffic by default. | |
253 | * | |
254 | */ | |
255 | int netlbl_unlabel_defconf(void) | |
256 | { | |
257 | int ret_val; | |
258 | struct netlbl_dom_map *entry; | |
95d4e6be | 259 | struct netlbl_audit audit_info; |
32f50cde | 260 | |
95d4e6be PM |
261 | /* Only the kernel is allowed to call this function and the only time |
262 | * it is called is at bootup before the audit subsystem is reporting | |
263 | * messages so don't worry to much about these values. */ | |
264 | security_task_getsecid(current, &audit_info.secid); | |
265 | audit_info.loginuid = 0; | |
96cb8e33 PM |
266 | |
267 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); | |
268 | if (entry == NULL) | |
269 | return -ENOMEM; | |
270 | entry->type = NETLBL_NLTYPE_UNLABELED; | |
95d4e6be | 271 | ret_val = netlbl_domhsh_add_default(entry, &audit_info); |
96cb8e33 PM |
272 | if (ret_val != 0) |
273 | return ret_val; | |
274 | ||
95d4e6be | 275 | netlbl_unlabel_acceptflg_set(1, &audit_info); |
96cb8e33 PM |
276 | |
277 | return 0; | |
278 | } |