Commit | Line | Data |
---|---|---|
e114e473 CS |
1 | /* |
2 | * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, version 2. | |
7 | * | |
8 | * Authors: | |
9 | * Casey Schaufler <casey@schaufler-ca.com> | |
10 | * Ahmed S. Darwish <darwish.07@gmail.com> | |
11 | * | |
12 | * Special thanks to the authors of selinuxfs. | |
13 | * | |
14 | * Karl MacMillan <kmacmillan@tresys.com> | |
15 | * James Morris <jmorris@redhat.com> | |
16 | * | |
17 | */ | |
18 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/vmalloc.h> | |
21 | #include <linux/security.h> | |
22 | #include <linux/mutex.h> | |
5a0e3ad6 | 23 | #include <linux/slab.h> |
6d3dc07c | 24 | #include <net/net_namespace.h> |
e114e473 CS |
25 | #include <net/netlabel.h> |
26 | #include <net/cipso_ipv4.h> | |
27 | #include <linux/seq_file.h> | |
28 | #include <linux/ctype.h> | |
4bc87e62 | 29 | #include <linux/audit.h> |
e114e473 CS |
30 | #include "smack.h" |
31 | ||
32 | /* | |
33 | * smackfs pseudo filesystem. | |
34 | */ | |
35 | ||
36 | enum smk_inos { | |
37 | SMK_ROOT_INO = 2, | |
38 | SMK_LOAD = 3, /* load policy */ | |
39 | SMK_CIPSO = 4, /* load label -> CIPSO mapping */ | |
40 | SMK_DOI = 5, /* CIPSO DOI */ | |
41 | SMK_DIRECT = 6, /* CIPSO level indicating direct label */ | |
42 | SMK_AMBIENT = 7, /* internet ambient label */ | |
6d3dc07c | 43 | SMK_NETLBLADDR = 8, /* single label hosts */ |
15446235 | 44 | SMK_ONLYCAP = 9, /* the only "capable" label */ |
ecfcc53f | 45 | SMK_LOGGING = 10, /* logging */ |
7898e1f8 | 46 | SMK_LOAD_SELF = 11, /* task specific rules */ |
828716c2 | 47 | SMK_ACCESSES = 12, /* access policy */ |
e114e473 CS |
48 | }; |
49 | ||
50 | /* | |
51 | * List locks | |
52 | */ | |
53 | static DEFINE_MUTEX(smack_list_lock); | |
54 | static DEFINE_MUTEX(smack_cipso_lock); | |
4bc87e62 | 55 | static DEFINE_MUTEX(smack_ambient_lock); |
6d3dc07c | 56 | static DEFINE_MUTEX(smk_netlbladdr_lock); |
e114e473 CS |
57 | |
58 | /* | |
59 | * This is the "ambient" label for network traffic. | |
60 | * If it isn't somehow marked, use this. | |
61 | * It can be reset via smackfs/ambient | |
62 | */ | |
63 | char *smack_net_ambient = smack_known_floor.smk_known; | |
64 | ||
e114e473 CS |
65 | /* |
66 | * This is the level in a CIPSO header that indicates a | |
67 | * smack label is contained directly in the category set. | |
68 | * It can be reset via smackfs/direct | |
69 | */ | |
70 | int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT; | |
71 | ||
15446235 CS |
72 | /* |
73 | * Unless a process is running with this label even | |
74 | * having CAP_MAC_OVERRIDE isn't enough to grant | |
75 | * privilege to violate MAC policy. If no label is | |
76 | * designated (the NULL case) capabilities apply to | |
77 | * everyone. It is expected that the hat (^) label | |
78 | * will be used if any label is used. | |
79 | */ | |
80 | char *smack_onlycap; | |
81 | ||
6d3dc07c CS |
82 | /* |
83 | * Certain IP addresses may be designated as single label hosts. | |
84 | * Packets are sent there unlabeled, but only from tasks that | |
85 | * can write to the specified label. | |
86 | */ | |
7198e2ee EB |
87 | |
88 | LIST_HEAD(smk_netlbladdr_list); | |
89 | LIST_HEAD(smack_rule_list); | |
6d3dc07c | 90 | |
e114e473 | 91 | static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT; |
e114e473 | 92 | |
4303154e EB |
93 | const char *smack_cipso_option = SMACK_CIPSO_OPTION; |
94 | ||
95 | ||
e114e473 CS |
96 | #define SEQ_READ_FINISHED 1 |
97 | ||
e114e473 CS |
98 | /* |
99 | * Values for parsing cipso rules | |
100 | * SMK_DIGITLEN: Length of a digit field in a rule. | |
b500ce8d AD |
101 | * SMK_CIPSOMIN: Minimum possible cipso rule length. |
102 | * SMK_CIPSOMAX: Maximum possible cipso rule length. | |
e114e473 CS |
103 | */ |
104 | #define SMK_DIGITLEN 4 | |
b500ce8d AD |
105 | #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN) |
106 | #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN) | |
107 | ||
108 | /* | |
109 | * Values for parsing MAC rules | |
110 | * SMK_ACCESS: Maximum possible combination of access permissions | |
111 | * SMK_ACCESSLEN: Maximum length for a rule access field | |
112 | * SMK_LOADLEN: Smack rule length | |
113 | */ | |
5c6d1125 JS |
114 | #define SMK_OACCESS "rwxa" |
115 | #define SMK_ACCESS "rwxat" | |
116 | #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1) | |
117 | #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1) | |
118 | #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN) | |
119 | #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN) | |
b500ce8d | 120 | |
6d3dc07c CS |
121 | /** |
122 | * smk_netlabel_audit_set - fill a netlbl_audit struct | |
123 | * @nap: structure to fill | |
124 | */ | |
125 | static void smk_netlabel_audit_set(struct netlbl_audit *nap) | |
126 | { | |
127 | nap->loginuid = audit_get_loginuid(current); | |
128 | nap->sessionid = audit_get_sessionid(current); | |
676dac4b | 129 | nap->secid = smack_to_secid(smk_of_current()); |
6d3dc07c CS |
130 | } |
131 | ||
132 | /* | |
133 | * Values for parsing single label host rules | |
134 | * "1.2.3.4 X" | |
135 | * "192.168.138.129/32 abcdefghijklmnopqrstuvw" | |
136 | */ | |
137 | #define SMK_NETLBLADDRMIN 9 | |
138 | #define SMK_NETLBLADDRMAX 42 | |
e114e473 | 139 | |
e114e473 CS |
140 | /** |
141 | * smk_set_access - add a rule to the rule list | |
142 | * @srp: the new rule to add | |
7898e1f8 CS |
143 | * @rule_list: the list of rules |
144 | * @rule_lock: the rule list lock | |
e114e473 CS |
145 | * |
146 | * Looks through the current subject/object/access list for | |
147 | * the subject/object pair and replaces the access that was | |
148 | * there. If the pair isn't found add it with the specified | |
149 | * access. | |
81ea714b | 150 | * |
7898e1f8 | 151 | * Returns 1 if a rule was found to exist already, 0 if it is new |
81ea714b SL |
152 | * Returns 0 if nothing goes wrong or -ENOMEM if it fails |
153 | * during the allocation of the new pair to add. | |
e114e473 | 154 | */ |
7898e1f8 CS |
155 | static int smk_set_access(struct smack_rule *srp, struct list_head *rule_list, |
156 | struct mutex *rule_lock) | |
e114e473 | 157 | { |
7198e2ee | 158 | struct smack_rule *sp; |
7898e1f8 | 159 | int found = 0; |
e114e473 | 160 | |
7898e1f8 CS |
161 | mutex_lock(rule_lock); |
162 | ||
163 | list_for_each_entry_rcu(sp, rule_list, list) { | |
7198e2ee EB |
164 | if (sp->smk_subject == srp->smk_subject && |
165 | sp->smk_object == srp->smk_object) { | |
166 | found = 1; | |
167 | sp->smk_access = srp->smk_access; | |
e114e473 CS |
168 | break; |
169 | } | |
e114e473 | 170 | } |
7198e2ee | 171 | if (found == 0) |
7898e1f8 | 172 | list_add_rcu(&srp->list, rule_list); |
e114e473 | 173 | |
7898e1f8 | 174 | mutex_unlock(rule_lock); |
e114e473 | 175 | |
7898e1f8 | 176 | return found; |
e114e473 CS |
177 | } |
178 | ||
179 | /** | |
828716c2 JS |
180 | * smk_parse_rule - parse subject, object and access type |
181 | * @data: string to be parsed whose size is SMK_LOADLEN | |
182 | * @rule: parsed entities are stored in here | |
e114e473 | 183 | */ |
828716c2 | 184 | static int smk_parse_rule(const char *data, struct smack_rule *rule) |
e114e473 | 185 | { |
7198e2ee EB |
186 | rule->smk_subject = smk_import(data, 0); |
187 | if (rule->smk_subject == NULL) | |
828716c2 | 188 | return -1; |
e114e473 | 189 | |
7198e2ee EB |
190 | rule->smk_object = smk_import(data + SMK_LABELLEN, 0); |
191 | if (rule->smk_object == NULL) | |
828716c2 | 192 | return -1; |
7198e2ee EB |
193 | |
194 | rule->smk_access = 0; | |
e114e473 CS |
195 | |
196 | switch (data[SMK_LABELLEN + SMK_LABELLEN]) { | |
197 | case '-': | |
198 | break; | |
199 | case 'r': | |
200 | case 'R': | |
7198e2ee | 201 | rule->smk_access |= MAY_READ; |
e114e473 CS |
202 | break; |
203 | default: | |
828716c2 | 204 | return -1; |
e114e473 CS |
205 | } |
206 | ||
207 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) { | |
208 | case '-': | |
209 | break; | |
210 | case 'w': | |
211 | case 'W': | |
7198e2ee | 212 | rule->smk_access |= MAY_WRITE; |
e114e473 CS |
213 | break; |
214 | default: | |
828716c2 | 215 | return -1; |
e114e473 CS |
216 | } |
217 | ||
218 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) { | |
219 | case '-': | |
220 | break; | |
221 | case 'x': | |
222 | case 'X': | |
7198e2ee | 223 | rule->smk_access |= MAY_EXEC; |
e114e473 CS |
224 | break; |
225 | default: | |
828716c2 | 226 | return -1; |
e114e473 CS |
227 | } |
228 | ||
229 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) { | |
230 | case '-': | |
231 | break; | |
232 | case 'a': | |
233 | case 'A': | |
7198e2ee | 234 | rule->smk_access |= MAY_APPEND; |
e114e473 CS |
235 | break; |
236 | default: | |
828716c2 | 237 | return -1; |
e114e473 CS |
238 | } |
239 | ||
5c6d1125 JS |
240 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 4]) { |
241 | case '-': | |
242 | break; | |
243 | case 't': | |
244 | case 'T': | |
245 | rule->smk_access |= MAY_TRANSMUTE; | |
246 | break; | |
247 | default: | |
828716c2 | 248 | return -1; |
5c6d1125 JS |
249 | } |
250 | ||
828716c2 JS |
251 | return 0; |
252 | } | |
253 | ||
254 | /** | |
255 | * smk_write_load_list - write() for any /smack/load | |
256 | * @file: file pointer, not actually used | |
257 | * @buf: where to get the data from | |
258 | * @count: bytes sent | |
259 | * @ppos: where to start - must be 0 | |
260 | * @rule_list: the list of rules to write to | |
261 | * @rule_lock: lock for the rule list | |
262 | * | |
263 | * Get one smack access rule from above. | |
264 | * The format is exactly: | |
265 | * char subject[SMK_LABELLEN] | |
266 | * char object[SMK_LABELLEN] | |
267 | * char access[SMK_ACCESSLEN] | |
268 | * | |
269 | * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes. | |
270 | */ | |
271 | static ssize_t smk_write_load_list(struct file *file, const char __user *buf, | |
272 | size_t count, loff_t *ppos, | |
273 | struct list_head *rule_list, | |
274 | struct mutex *rule_lock) | |
275 | { | |
276 | struct smack_rule *rule; | |
277 | char *data; | |
278 | int rc = -EINVAL; | |
279 | ||
280 | /* | |
281 | * No partial writes. | |
282 | * Enough data must be present. | |
283 | */ | |
284 | if (*ppos != 0) | |
285 | return -EINVAL; | |
286 | /* | |
287 | * Minor hack for backward compatibility | |
288 | */ | |
289 | if (count < (SMK_OLOADLEN) || count > SMK_LOADLEN) | |
290 | return -EINVAL; | |
291 | ||
292 | data = kzalloc(SMK_LOADLEN, GFP_KERNEL); | |
293 | if (data == NULL) | |
294 | return -ENOMEM; | |
295 | ||
296 | if (copy_from_user(data, buf, count) != 0) { | |
297 | rc = -EFAULT; | |
298 | goto out; | |
299 | } | |
300 | ||
301 | /* | |
302 | * More on the minor hack for backward compatibility | |
303 | */ | |
304 | if (count == (SMK_OLOADLEN)) | |
305 | data[SMK_OLOADLEN] = '-'; | |
306 | ||
307 | rule = kzalloc(sizeof(*rule), GFP_KERNEL); | |
308 | if (rule == NULL) { | |
309 | rc = -ENOMEM; | |
310 | goto out; | |
311 | } | |
312 | ||
313 | if (smk_parse_rule(data, rule)) | |
314 | goto out_free_rule; | |
315 | ||
7898e1f8 CS |
316 | rc = count; |
317 | /* | |
318 | * smk_set_access returns true if there was already a rule | |
319 | * for the subject/object pair, and false if it was new. | |
320 | */ | |
321 | if (!smk_set_access(rule, rule_list, rule_lock)) | |
322 | goto out; | |
e114e473 | 323 | |
7198e2ee EB |
324 | out_free_rule: |
325 | kfree(rule); | |
e114e473 CS |
326 | out: |
327 | kfree(data); | |
328 | return rc; | |
329 | } | |
330 | ||
7898e1f8 CS |
331 | |
332 | /* | |
333 | * Seq_file read operations for /smack/load | |
334 | */ | |
335 | ||
336 | static void *load_seq_start(struct seq_file *s, loff_t *pos) | |
337 | { | |
338 | if (*pos == SEQ_READ_FINISHED) | |
339 | return NULL; | |
340 | if (list_empty(&smack_rule_list)) | |
341 | return NULL; | |
342 | return smack_rule_list.next; | |
343 | } | |
344 | ||
345 | static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
346 | { | |
347 | struct list_head *list = v; | |
348 | ||
349 | if (list_is_last(list, &smack_rule_list)) { | |
350 | *pos = SEQ_READ_FINISHED; | |
351 | return NULL; | |
352 | } | |
353 | return list->next; | |
354 | } | |
355 | ||
356 | static int load_seq_show(struct seq_file *s, void *v) | |
357 | { | |
358 | struct list_head *list = v; | |
359 | struct smack_rule *srp = | |
360 | list_entry(list, struct smack_rule, list); | |
361 | ||
362 | seq_printf(s, "%s %s", (char *)srp->smk_subject, | |
363 | (char *)srp->smk_object); | |
364 | ||
365 | seq_putc(s, ' '); | |
366 | ||
367 | if (srp->smk_access & MAY_READ) | |
368 | seq_putc(s, 'r'); | |
369 | if (srp->smk_access & MAY_WRITE) | |
370 | seq_putc(s, 'w'); | |
371 | if (srp->smk_access & MAY_EXEC) | |
372 | seq_putc(s, 'x'); | |
373 | if (srp->smk_access & MAY_APPEND) | |
374 | seq_putc(s, 'a'); | |
375 | if (srp->smk_access & MAY_TRANSMUTE) | |
376 | seq_putc(s, 't'); | |
377 | if (srp->smk_access == 0) | |
378 | seq_putc(s, '-'); | |
379 | ||
380 | seq_putc(s, '\n'); | |
381 | ||
382 | return 0; | |
383 | } | |
384 | ||
385 | static void load_seq_stop(struct seq_file *s, void *v) | |
386 | { | |
387 | /* No-op */ | |
388 | } | |
389 | ||
390 | static const struct seq_operations load_seq_ops = { | |
391 | .start = load_seq_start, | |
392 | .next = load_seq_next, | |
393 | .show = load_seq_show, | |
394 | .stop = load_seq_stop, | |
395 | }; | |
396 | ||
397 | /** | |
398 | * smk_open_load - open() for /smack/load | |
399 | * @inode: inode structure representing file | |
400 | * @file: "load" file pointer | |
401 | * | |
402 | * For reading, use load_seq_* seq_file reading operations. | |
403 | */ | |
404 | static int smk_open_load(struct inode *inode, struct file *file) | |
405 | { | |
406 | return seq_open(file, &load_seq_ops); | |
407 | } | |
408 | ||
409 | /** | |
410 | * smk_write_load - write() for /smack/load | |
411 | * @file: file pointer, not actually used | |
412 | * @buf: where to get the data from | |
413 | * @count: bytes sent | |
414 | * @ppos: where to start - must be 0 | |
415 | * | |
416 | */ | |
417 | static ssize_t smk_write_load(struct file *file, const char __user *buf, | |
418 | size_t count, loff_t *ppos) | |
419 | { | |
420 | ||
421 | /* | |
422 | * Must have privilege. | |
423 | * No partial writes. | |
424 | * Enough data must be present. | |
425 | */ | |
426 | if (!capable(CAP_MAC_ADMIN)) | |
427 | return -EPERM; | |
428 | ||
429 | return smk_write_load_list(file, buf, count, ppos, &smack_rule_list, | |
430 | &smack_list_lock); | |
431 | } | |
432 | ||
e114e473 CS |
433 | static const struct file_operations smk_load_ops = { |
434 | .open = smk_open_load, | |
435 | .read = seq_read, | |
436 | .llseek = seq_lseek, | |
437 | .write = smk_write_load, | |
cb622bbb | 438 | .release = seq_release, |
e114e473 CS |
439 | }; |
440 | ||
441 | /** | |
442 | * smk_cipso_doi - initialize the CIPSO domain | |
443 | */ | |
30aa4faf | 444 | static void smk_cipso_doi(void) |
e114e473 CS |
445 | { |
446 | int rc; | |
447 | struct cipso_v4_doi *doip; | |
6d3dc07c | 448 | struct netlbl_audit nai; |
e114e473 | 449 | |
6d3dc07c | 450 | smk_netlabel_audit_set(&nai); |
4bc87e62 | 451 | |
6d3dc07c | 452 | rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai); |
e114e473 CS |
453 | if (rc != 0) |
454 | printk(KERN_WARNING "%s:%d remove rc = %d\n", | |
455 | __func__, __LINE__, rc); | |
456 | ||
457 | doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL); | |
458 | if (doip == NULL) | |
459 | panic("smack: Failed to initialize cipso DOI.\n"); | |
460 | doip->map.std = NULL; | |
461 | doip->doi = smk_cipso_doi_value; | |
462 | doip->type = CIPSO_V4_MAP_PASS; | |
463 | doip->tags[0] = CIPSO_V4_TAG_RBITMAP; | |
464 | for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++) | |
465 | doip->tags[rc] = CIPSO_V4_TAG_INVALID; | |
466 | ||
6d3dc07c | 467 | rc = netlbl_cfg_cipsov4_add(doip, &nai); |
b1edeb10 | 468 | if (rc != 0) { |
6c2e8ac0 PM |
469 | printk(KERN_WARNING "%s:%d cipso add rc = %d\n", |
470 | __func__, __LINE__, rc); | |
471 | kfree(doip); | |
472 | return; | |
473 | } | |
6d3dc07c | 474 | rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai); |
6c2e8ac0 PM |
475 | if (rc != 0) { |
476 | printk(KERN_WARNING "%s:%d map add rc = %d\n", | |
e114e473 | 477 | __func__, __LINE__, rc); |
b1edeb10 | 478 | kfree(doip); |
6c2e8ac0 | 479 | return; |
b1edeb10 | 480 | } |
e114e473 CS |
481 | } |
482 | ||
4bc87e62 CS |
483 | /** |
484 | * smk_unlbl_ambient - initialize the unlabeled domain | |
251a2a95 | 485 | * @oldambient: previous domain string |
4bc87e62 | 486 | */ |
30aa4faf | 487 | static void smk_unlbl_ambient(char *oldambient) |
4bc87e62 CS |
488 | { |
489 | int rc; | |
6d3dc07c | 490 | struct netlbl_audit nai; |
4bc87e62 | 491 | |
6d3dc07c | 492 | smk_netlabel_audit_set(&nai); |
4bc87e62 CS |
493 | |
494 | if (oldambient != NULL) { | |
6d3dc07c | 495 | rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai); |
4bc87e62 CS |
496 | if (rc != 0) |
497 | printk(KERN_WARNING "%s:%d remove rc = %d\n", | |
498 | __func__, __LINE__, rc); | |
499 | } | |
500 | ||
6d3dc07c CS |
501 | rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET, |
502 | NULL, NULL, &nai); | |
4bc87e62 CS |
503 | if (rc != 0) |
504 | printk(KERN_WARNING "%s:%d add rc = %d\n", | |
505 | __func__, __LINE__, rc); | |
506 | } | |
507 | ||
e114e473 CS |
508 | /* |
509 | * Seq_file read operations for /smack/cipso | |
510 | */ | |
511 | ||
512 | static void *cipso_seq_start(struct seq_file *s, loff_t *pos) | |
513 | { | |
514 | if (*pos == SEQ_READ_FINISHED) | |
515 | return NULL; | |
7198e2ee EB |
516 | if (list_empty(&smack_known_list)) |
517 | return NULL; | |
e114e473 | 518 | |
7198e2ee | 519 | return smack_known_list.next; |
e114e473 CS |
520 | } |
521 | ||
522 | static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
523 | { | |
7198e2ee | 524 | struct list_head *list = v; |
e114e473 CS |
525 | |
526 | /* | |
7198e2ee EB |
527 | * labels with no associated cipso value wont be printed |
528 | * in cipso_seq_show | |
e114e473 | 529 | */ |
7198e2ee | 530 | if (list_is_last(list, &smack_known_list)) { |
e114e473 | 531 | *pos = SEQ_READ_FINISHED; |
7198e2ee EB |
532 | return NULL; |
533 | } | |
e114e473 | 534 | |
7198e2ee | 535 | return list->next; |
e114e473 CS |
536 | } |
537 | ||
538 | /* | |
539 | * Print cipso labels in format: | |
540 | * label level[/cat[,cat]] | |
541 | */ | |
542 | static int cipso_seq_show(struct seq_file *s, void *v) | |
543 | { | |
7198e2ee EB |
544 | struct list_head *list = v; |
545 | struct smack_known *skp = | |
546 | list_entry(list, struct smack_known, list); | |
e114e473 CS |
547 | struct smack_cipso *scp = skp->smk_cipso; |
548 | char *cbp; | |
549 | char sep = '/'; | |
550 | int cat = 1; | |
551 | int i; | |
552 | unsigned char m; | |
553 | ||
554 | if (scp == NULL) | |
555 | return 0; | |
556 | ||
557 | seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level); | |
558 | ||
559 | cbp = scp->smk_catset; | |
560 | for (i = 0; i < SMK_LABELLEN; i++) | |
561 | for (m = 0x80; m != 0; m >>= 1) { | |
562 | if (m & cbp[i]) { | |
563 | seq_printf(s, "%c%d", sep, cat); | |
564 | sep = ','; | |
565 | } | |
566 | cat++; | |
567 | } | |
568 | ||
569 | seq_putc(s, '\n'); | |
570 | ||
571 | return 0; | |
572 | } | |
573 | ||
574 | static void cipso_seq_stop(struct seq_file *s, void *v) | |
575 | { | |
576 | /* No-op */ | |
577 | } | |
578 | ||
88e9d34c | 579 | static const struct seq_operations cipso_seq_ops = { |
e114e473 CS |
580 | .start = cipso_seq_start, |
581 | .stop = cipso_seq_stop, | |
582 | .next = cipso_seq_next, | |
583 | .show = cipso_seq_show, | |
584 | }; | |
585 | ||
586 | /** | |
587 | * smk_open_cipso - open() for /smack/cipso | |
588 | * @inode: inode structure representing file | |
589 | * @file: "cipso" file pointer | |
590 | * | |
591 | * Connect our cipso_seq_* operations with /smack/cipso | |
592 | * file_operations | |
593 | */ | |
594 | static int smk_open_cipso(struct inode *inode, struct file *file) | |
595 | { | |
596 | return seq_open(file, &cipso_seq_ops); | |
597 | } | |
598 | ||
599 | /** | |
600 | * smk_write_cipso - write() for /smack/cipso | |
251a2a95 | 601 | * @file: file pointer, not actually used |
e114e473 CS |
602 | * @buf: where to get the data from |
603 | * @count: bytes sent | |
604 | * @ppos: where to start | |
605 | * | |
606 | * Accepts only one cipso rule per write call. | |
607 | * Returns number of bytes written or error code, as appropriate | |
608 | */ | |
609 | static ssize_t smk_write_cipso(struct file *file, const char __user *buf, | |
610 | size_t count, loff_t *ppos) | |
611 | { | |
612 | struct smack_known *skp; | |
613 | struct smack_cipso *scp = NULL; | |
614 | char mapcatset[SMK_LABELLEN]; | |
615 | int maplevel; | |
616 | int cat; | |
617 | int catlen; | |
618 | ssize_t rc = -EINVAL; | |
619 | char *data = NULL; | |
620 | char *rule; | |
621 | int ret; | |
622 | int i; | |
623 | ||
624 | /* | |
625 | * Must have privilege. | |
626 | * No partial writes. | |
627 | * Enough data must be present. | |
628 | */ | |
629 | if (!capable(CAP_MAC_ADMIN)) | |
630 | return -EPERM; | |
631 | if (*ppos != 0) | |
632 | return -EINVAL; | |
b500ce8d | 633 | if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX) |
e114e473 CS |
634 | return -EINVAL; |
635 | ||
636 | data = kzalloc(count + 1, GFP_KERNEL); | |
637 | if (data == NULL) | |
638 | return -ENOMEM; | |
639 | ||
640 | if (copy_from_user(data, buf, count) != 0) { | |
641 | rc = -EFAULT; | |
642 | goto unlockedout; | |
643 | } | |
644 | ||
4303154e EB |
645 | /* labels cannot begin with a '-' */ |
646 | if (data[0] == '-') { | |
647 | rc = -EINVAL; | |
648 | goto unlockedout; | |
649 | } | |
e114e473 CS |
650 | data[count] = '\0'; |
651 | rule = data; | |
652 | /* | |
653 | * Only allow one writer at a time. Writes should be | |
654 | * quite rare and small in any case. | |
655 | */ | |
656 | mutex_lock(&smack_cipso_lock); | |
657 | ||
658 | skp = smk_import_entry(rule, 0); | |
659 | if (skp == NULL) | |
660 | goto out; | |
661 | ||
c19a28e1 | 662 | rule += SMK_LABELLEN; |
e114e473 CS |
663 | ret = sscanf(rule, "%d", &maplevel); |
664 | if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL) | |
665 | goto out; | |
666 | ||
667 | rule += SMK_DIGITLEN; | |
668 | ret = sscanf(rule, "%d", &catlen); | |
669 | if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM) | |
670 | goto out; | |
671 | ||
b500ce8d | 672 | if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN)) |
e114e473 CS |
673 | goto out; |
674 | ||
675 | memset(mapcatset, 0, sizeof(mapcatset)); | |
676 | ||
677 | for (i = 0; i < catlen; i++) { | |
678 | rule += SMK_DIGITLEN; | |
679 | ret = sscanf(rule, "%d", &cat); | |
680 | if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL) | |
681 | goto out; | |
682 | ||
683 | smack_catset_bit(cat, mapcatset); | |
684 | } | |
685 | ||
686 | if (skp->smk_cipso == NULL) { | |
687 | scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL); | |
688 | if (scp == NULL) { | |
689 | rc = -ENOMEM; | |
690 | goto out; | |
691 | } | |
692 | } | |
693 | ||
694 | spin_lock_bh(&skp->smk_cipsolock); | |
695 | ||
696 | if (scp == NULL) | |
697 | scp = skp->smk_cipso; | |
698 | else | |
699 | skp->smk_cipso = scp; | |
700 | ||
701 | scp->smk_level = maplevel; | |
702 | memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset)); | |
703 | ||
704 | spin_unlock_bh(&skp->smk_cipsolock); | |
705 | ||
706 | rc = count; | |
707 | out: | |
708 | mutex_unlock(&smack_cipso_lock); | |
709 | unlockedout: | |
710 | kfree(data); | |
711 | return rc; | |
712 | } | |
713 | ||
714 | static const struct file_operations smk_cipso_ops = { | |
715 | .open = smk_open_cipso, | |
716 | .read = seq_read, | |
717 | .llseek = seq_lseek, | |
718 | .write = smk_write_cipso, | |
719 | .release = seq_release, | |
720 | }; | |
721 | ||
6d3dc07c CS |
722 | /* |
723 | * Seq_file read operations for /smack/netlabel | |
724 | */ | |
725 | ||
726 | static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos) | |
727 | { | |
728 | if (*pos == SEQ_READ_FINISHED) | |
729 | return NULL; | |
7198e2ee EB |
730 | if (list_empty(&smk_netlbladdr_list)) |
731 | return NULL; | |
732 | return smk_netlbladdr_list.next; | |
6d3dc07c CS |
733 | } |
734 | ||
735 | static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
736 | { | |
7198e2ee | 737 | struct list_head *list = v; |
6d3dc07c | 738 | |
7198e2ee | 739 | if (list_is_last(list, &smk_netlbladdr_list)) { |
6d3dc07c | 740 | *pos = SEQ_READ_FINISHED; |
7198e2ee EB |
741 | return NULL; |
742 | } | |
6d3dc07c | 743 | |
7198e2ee | 744 | return list->next; |
6d3dc07c | 745 | } |
6d3dc07c CS |
746 | #define BEBITS (sizeof(__be32) * 8) |
747 | ||
748 | /* | |
749 | * Print host/label pairs | |
750 | */ | |
751 | static int netlbladdr_seq_show(struct seq_file *s, void *v) | |
752 | { | |
7198e2ee EB |
753 | struct list_head *list = v; |
754 | struct smk_netlbladdr *skp = | |
755 | list_entry(list, struct smk_netlbladdr, list); | |
6d3dc07c | 756 | unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr; |
113a0e45 | 757 | int maskn; |
758 | u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr); | |
6d3dc07c | 759 | |
113a0e45 | 760 | for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++); |
6d3dc07c CS |
761 | |
762 | seq_printf(s, "%u.%u.%u.%u/%d %s\n", | |
763 | hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label); | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
768 | static void netlbladdr_seq_stop(struct seq_file *s, void *v) | |
769 | { | |
770 | /* No-op */ | |
771 | } | |
772 | ||
88e9d34c | 773 | static const struct seq_operations netlbladdr_seq_ops = { |
6d3dc07c CS |
774 | .start = netlbladdr_seq_start, |
775 | .stop = netlbladdr_seq_stop, | |
776 | .next = netlbladdr_seq_next, | |
777 | .show = netlbladdr_seq_show, | |
778 | }; | |
779 | ||
780 | /** | |
781 | * smk_open_netlbladdr - open() for /smack/netlabel | |
782 | * @inode: inode structure representing file | |
783 | * @file: "netlabel" file pointer | |
784 | * | |
785 | * Connect our netlbladdr_seq_* operations with /smack/netlabel | |
786 | * file_operations | |
787 | */ | |
788 | static int smk_open_netlbladdr(struct inode *inode, struct file *file) | |
789 | { | |
790 | return seq_open(file, &netlbladdr_seq_ops); | |
791 | } | |
792 | ||
113a0e45 | 793 | /** |
794 | * smk_netlbladdr_insert | |
795 | * @new : netlabel to insert | |
796 | * | |
797 | * This helper insert netlabel in the smack_netlbladdrs list | |
798 | * sorted by netmask length (longest to smallest) | |
7198e2ee EB |
799 | * locked by &smk_netlbladdr_lock in smk_write_netlbladdr |
800 | * | |
113a0e45 | 801 | */ |
802 | static void smk_netlbladdr_insert(struct smk_netlbladdr *new) | |
803 | { | |
7198e2ee | 804 | struct smk_netlbladdr *m, *m_next; |
113a0e45 | 805 | |
7198e2ee EB |
806 | if (list_empty(&smk_netlbladdr_list)) { |
807 | list_add_rcu(&new->list, &smk_netlbladdr_list); | |
113a0e45 | 808 | return; |
809 | } | |
810 | ||
05725f7e JP |
811 | m = list_entry_rcu(smk_netlbladdr_list.next, |
812 | struct smk_netlbladdr, list); | |
7198e2ee | 813 | |
113a0e45 | 814 | /* the comparison '>' is a bit hacky, but works */ |
7198e2ee EB |
815 | if (new->smk_mask.s_addr > m->smk_mask.s_addr) { |
816 | list_add_rcu(&new->list, &smk_netlbladdr_list); | |
113a0e45 | 817 | return; |
818 | } | |
7198e2ee EB |
819 | |
820 | list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) { | |
821 | if (list_is_last(&m->list, &smk_netlbladdr_list)) { | |
822 | list_add_rcu(&new->list, &m->list); | |
113a0e45 | 823 | return; |
824 | } | |
05725f7e JP |
825 | m_next = list_entry_rcu(m->list.next, |
826 | struct smk_netlbladdr, list); | |
7198e2ee EB |
827 | if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) { |
828 | list_add_rcu(&new->list, &m->list); | |
113a0e45 | 829 | return; |
830 | } | |
831 | } | |
832 | } | |
833 | ||
834 | ||
6d3dc07c CS |
835 | /** |
836 | * smk_write_netlbladdr - write() for /smack/netlabel | |
251a2a95 | 837 | * @file: file pointer, not actually used |
6d3dc07c CS |
838 | * @buf: where to get the data from |
839 | * @count: bytes sent | |
840 | * @ppos: where to start | |
841 | * | |
842 | * Accepts only one netlbladdr per write call. | |
843 | * Returns number of bytes written or error code, as appropriate | |
844 | */ | |
845 | static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf, | |
846 | size_t count, loff_t *ppos) | |
847 | { | |
848 | struct smk_netlbladdr *skp; | |
849 | struct sockaddr_in newname; | |
850 | char smack[SMK_LABELLEN]; | |
851 | char *sp; | |
6470c077 | 852 | char data[SMK_NETLBLADDRMAX + 1]; |
6d3dc07c CS |
853 | char *host = (char *)&newname.sin_addr.s_addr; |
854 | int rc; | |
855 | struct netlbl_audit audit_info; | |
856 | struct in_addr mask; | |
857 | unsigned int m; | |
7198e2ee | 858 | int found; |
113a0e45 | 859 | u32 mask_bits = (1<<31); |
6d3dc07c | 860 | __be32 nsa; |
113a0e45 | 861 | u32 temp_mask; |
6d3dc07c CS |
862 | |
863 | /* | |
864 | * Must have privilege. | |
865 | * No partial writes. | |
866 | * Enough data must be present. | |
867 | * "<addr/mask, as a.b.c.d/e><space><label>" | |
868 | * "<addr, as a.b.c.d><space><label>" | |
869 | */ | |
870 | if (!capable(CAP_MAC_ADMIN)) | |
871 | return -EPERM; | |
872 | if (*ppos != 0) | |
873 | return -EINVAL; | |
874 | if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX) | |
875 | return -EINVAL; | |
876 | if (copy_from_user(data, buf, count) != 0) | |
877 | return -EFAULT; | |
878 | ||
879 | data[count] = '\0'; | |
880 | ||
881 | rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s", | |
882 | &host[0], &host[1], &host[2], &host[3], &m, smack); | |
883 | if (rc != 6) { | |
884 | rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s", | |
885 | &host[0], &host[1], &host[2], &host[3], smack); | |
886 | if (rc != 5) | |
887 | return -EINVAL; | |
888 | m = BEBITS; | |
889 | } | |
890 | if (m > BEBITS) | |
891 | return -EINVAL; | |
892 | ||
4303154e EB |
893 | /* if smack begins with '-', its an option, don't import it */ |
894 | if (smack[0] != '-') { | |
895 | sp = smk_import(smack, 0); | |
896 | if (sp == NULL) | |
897 | return -EINVAL; | |
898 | } else { | |
899 | /* check known options */ | |
900 | if (strcmp(smack, smack_cipso_option) == 0) | |
901 | sp = (char *)smack_cipso_option; | |
902 | else | |
903 | return -EINVAL; | |
904 | } | |
6d3dc07c | 905 | |
113a0e45 | 906 | for (temp_mask = 0; m > 0; m--) { |
907 | temp_mask |= mask_bits; | |
908 | mask_bits >>= 1; | |
6d3dc07c | 909 | } |
113a0e45 | 910 | mask.s_addr = cpu_to_be32(temp_mask); |
911 | ||
912 | newname.sin_addr.s_addr &= mask.s_addr; | |
6d3dc07c CS |
913 | /* |
914 | * Only allow one writer at a time. Writes should be | |
915 | * quite rare and small in any case. | |
916 | */ | |
917 | mutex_lock(&smk_netlbladdr_lock); | |
918 | ||
919 | nsa = newname.sin_addr.s_addr; | |
113a0e45 | 920 | /* try to find if the prefix is already in the list */ |
7198e2ee EB |
921 | found = 0; |
922 | list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) { | |
6d3dc07c | 923 | if (skp->smk_host.sin_addr.s_addr == nsa && |
7198e2ee EB |
924 | skp->smk_mask.s_addr == mask.s_addr) { |
925 | found = 1; | |
6d3dc07c | 926 | break; |
7198e2ee EB |
927 | } |
928 | } | |
6d3dc07c CS |
929 | smk_netlabel_audit_set(&audit_info); |
930 | ||
7198e2ee | 931 | if (found == 0) { |
6d3dc07c CS |
932 | skp = kzalloc(sizeof(*skp), GFP_KERNEL); |
933 | if (skp == NULL) | |
934 | rc = -ENOMEM; | |
935 | else { | |
936 | rc = 0; | |
937 | skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr; | |
938 | skp->smk_mask.s_addr = mask.s_addr; | |
6d3dc07c | 939 | skp->smk_label = sp; |
113a0e45 | 940 | smk_netlbladdr_insert(skp); |
6d3dc07c CS |
941 | } |
942 | } else { | |
4303154e | 943 | /* we delete the unlabeled entry, only if the previous label |
25985edc | 944 | * wasn't the special CIPSO option */ |
4303154e EB |
945 | if (skp->smk_label != smack_cipso_option) |
946 | rc = netlbl_cfg_unlbl_static_del(&init_net, NULL, | |
947 | &skp->smk_host.sin_addr, &skp->smk_mask, | |
948 | PF_INET, &audit_info); | |
949 | else | |
950 | rc = 0; | |
6d3dc07c CS |
951 | skp->smk_label = sp; |
952 | } | |
953 | ||
954 | /* | |
955 | * Now tell netlabel about the single label nature of | |
956 | * this host so that incoming packets get labeled. | |
4303154e | 957 | * but only if we didn't get the special CIPSO option |
6d3dc07c | 958 | */ |
4303154e | 959 | if (rc == 0 && sp != smack_cipso_option) |
6d3dc07c CS |
960 | rc = netlbl_cfg_unlbl_static_add(&init_net, NULL, |
961 | &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET, | |
962 | smack_to_secid(skp->smk_label), &audit_info); | |
963 | ||
964 | if (rc == 0) | |
965 | rc = count; | |
966 | ||
967 | mutex_unlock(&smk_netlbladdr_lock); | |
968 | ||
969 | return rc; | |
970 | } | |
971 | ||
972 | static const struct file_operations smk_netlbladdr_ops = { | |
973 | .open = smk_open_netlbladdr, | |
974 | .read = seq_read, | |
975 | .llseek = seq_lseek, | |
976 | .write = smk_write_netlbladdr, | |
977 | .release = seq_release, | |
978 | }; | |
979 | ||
e114e473 CS |
980 | /** |
981 | * smk_read_doi - read() for /smack/doi | |
982 | * @filp: file pointer, not actually used | |
983 | * @buf: where to put the result | |
984 | * @count: maximum to send along | |
985 | * @ppos: where to start | |
986 | * | |
987 | * Returns number of bytes read or error code, as appropriate | |
988 | */ | |
989 | static ssize_t smk_read_doi(struct file *filp, char __user *buf, | |
990 | size_t count, loff_t *ppos) | |
991 | { | |
992 | char temp[80]; | |
993 | ssize_t rc; | |
994 | ||
995 | if (*ppos != 0) | |
996 | return 0; | |
997 | ||
998 | sprintf(temp, "%d", smk_cipso_doi_value); | |
999 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
1000 | ||
1001 | return rc; | |
1002 | } | |
1003 | ||
1004 | /** | |
1005 | * smk_write_doi - write() for /smack/doi | |
251a2a95 | 1006 | * @file: file pointer, not actually used |
e114e473 CS |
1007 | * @buf: where to get the data from |
1008 | * @count: bytes sent | |
1009 | * @ppos: where to start | |
1010 | * | |
1011 | * Returns number of bytes written or error code, as appropriate | |
1012 | */ | |
1013 | static ssize_t smk_write_doi(struct file *file, const char __user *buf, | |
1014 | size_t count, loff_t *ppos) | |
1015 | { | |
1016 | char temp[80]; | |
1017 | int i; | |
1018 | ||
1019 | if (!capable(CAP_MAC_ADMIN)) | |
1020 | return -EPERM; | |
1021 | ||
1022 | if (count >= sizeof(temp) || count == 0) | |
1023 | return -EINVAL; | |
1024 | ||
1025 | if (copy_from_user(temp, buf, count) != 0) | |
1026 | return -EFAULT; | |
1027 | ||
1028 | temp[count] = '\0'; | |
1029 | ||
1030 | if (sscanf(temp, "%d", &i) != 1) | |
1031 | return -EINVAL; | |
1032 | ||
1033 | smk_cipso_doi_value = i; | |
1034 | ||
1035 | smk_cipso_doi(); | |
1036 | ||
1037 | return count; | |
1038 | } | |
1039 | ||
1040 | static const struct file_operations smk_doi_ops = { | |
1041 | .read = smk_read_doi, | |
1042 | .write = smk_write_doi, | |
6038f373 | 1043 | .llseek = default_llseek, |
e114e473 CS |
1044 | }; |
1045 | ||
1046 | /** | |
1047 | * smk_read_direct - read() for /smack/direct | |
1048 | * @filp: file pointer, not actually used | |
1049 | * @buf: where to put the result | |
1050 | * @count: maximum to send along | |
1051 | * @ppos: where to start | |
1052 | * | |
1053 | * Returns number of bytes read or error code, as appropriate | |
1054 | */ | |
1055 | static ssize_t smk_read_direct(struct file *filp, char __user *buf, | |
1056 | size_t count, loff_t *ppos) | |
1057 | { | |
1058 | char temp[80]; | |
1059 | ssize_t rc; | |
1060 | ||
1061 | if (*ppos != 0) | |
1062 | return 0; | |
1063 | ||
1064 | sprintf(temp, "%d", smack_cipso_direct); | |
1065 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
1066 | ||
1067 | return rc; | |
1068 | } | |
1069 | ||
1070 | /** | |
1071 | * smk_write_direct - write() for /smack/direct | |
251a2a95 | 1072 | * @file: file pointer, not actually used |
e114e473 CS |
1073 | * @buf: where to get the data from |
1074 | * @count: bytes sent | |
1075 | * @ppos: where to start | |
1076 | * | |
1077 | * Returns number of bytes written or error code, as appropriate | |
1078 | */ | |
1079 | static ssize_t smk_write_direct(struct file *file, const char __user *buf, | |
1080 | size_t count, loff_t *ppos) | |
1081 | { | |
1082 | char temp[80]; | |
1083 | int i; | |
1084 | ||
1085 | if (!capable(CAP_MAC_ADMIN)) | |
1086 | return -EPERM; | |
1087 | ||
1088 | if (count >= sizeof(temp) || count == 0) | |
1089 | return -EINVAL; | |
1090 | ||
1091 | if (copy_from_user(temp, buf, count) != 0) | |
1092 | return -EFAULT; | |
1093 | ||
1094 | temp[count] = '\0'; | |
1095 | ||
1096 | if (sscanf(temp, "%d", &i) != 1) | |
1097 | return -EINVAL; | |
1098 | ||
1099 | smack_cipso_direct = i; | |
1100 | ||
1101 | return count; | |
1102 | } | |
1103 | ||
1104 | static const struct file_operations smk_direct_ops = { | |
1105 | .read = smk_read_direct, | |
1106 | .write = smk_write_direct, | |
6038f373 | 1107 | .llseek = default_llseek, |
e114e473 CS |
1108 | }; |
1109 | ||
1110 | /** | |
1111 | * smk_read_ambient - read() for /smack/ambient | |
1112 | * @filp: file pointer, not actually used | |
1113 | * @buf: where to put the result | |
1114 | * @cn: maximum to send along | |
1115 | * @ppos: where to start | |
1116 | * | |
1117 | * Returns number of bytes read or error code, as appropriate | |
1118 | */ | |
1119 | static ssize_t smk_read_ambient(struct file *filp, char __user *buf, | |
1120 | size_t cn, loff_t *ppos) | |
1121 | { | |
1122 | ssize_t rc; | |
e114e473 CS |
1123 | int asize; |
1124 | ||
1125 | if (*ppos != 0) | |
1126 | return 0; | |
1127 | /* | |
1128 | * Being careful to avoid a problem in the case where | |
1129 | * smack_net_ambient gets changed in midstream. | |
e114e473 | 1130 | */ |
4bc87e62 | 1131 | mutex_lock(&smack_ambient_lock); |
e114e473 | 1132 | |
4bc87e62 CS |
1133 | asize = strlen(smack_net_ambient) + 1; |
1134 | ||
1135 | if (cn >= asize) | |
1136 | rc = simple_read_from_buffer(buf, cn, ppos, | |
1137 | smack_net_ambient, asize); | |
1138 | else | |
1139 | rc = -EINVAL; | |
e114e473 | 1140 | |
4bc87e62 | 1141 | mutex_unlock(&smack_ambient_lock); |
e114e473 CS |
1142 | |
1143 | return rc; | |
1144 | } | |
1145 | ||
1146 | /** | |
1147 | * smk_write_ambient - write() for /smack/ambient | |
251a2a95 | 1148 | * @file: file pointer, not actually used |
e114e473 CS |
1149 | * @buf: where to get the data from |
1150 | * @count: bytes sent | |
1151 | * @ppos: where to start | |
1152 | * | |
1153 | * Returns number of bytes written or error code, as appropriate | |
1154 | */ | |
1155 | static ssize_t smk_write_ambient(struct file *file, const char __user *buf, | |
1156 | size_t count, loff_t *ppos) | |
1157 | { | |
1158 | char in[SMK_LABELLEN]; | |
4bc87e62 | 1159 | char *oldambient; |
e114e473 CS |
1160 | char *smack; |
1161 | ||
1162 | if (!capable(CAP_MAC_ADMIN)) | |
1163 | return -EPERM; | |
1164 | ||
1165 | if (count >= SMK_LABELLEN) | |
1166 | return -EINVAL; | |
1167 | ||
1168 | if (copy_from_user(in, buf, count) != 0) | |
1169 | return -EFAULT; | |
1170 | ||
1171 | smack = smk_import(in, count); | |
1172 | if (smack == NULL) | |
1173 | return -EINVAL; | |
1174 | ||
4bc87e62 CS |
1175 | mutex_lock(&smack_ambient_lock); |
1176 | ||
1177 | oldambient = smack_net_ambient; | |
e114e473 | 1178 | smack_net_ambient = smack; |
4bc87e62 CS |
1179 | smk_unlbl_ambient(oldambient); |
1180 | ||
1181 | mutex_unlock(&smack_ambient_lock); | |
e114e473 CS |
1182 | |
1183 | return count; | |
1184 | } | |
1185 | ||
1186 | static const struct file_operations smk_ambient_ops = { | |
1187 | .read = smk_read_ambient, | |
1188 | .write = smk_write_ambient, | |
6038f373 | 1189 | .llseek = default_llseek, |
e114e473 CS |
1190 | }; |
1191 | ||
15446235 CS |
1192 | /** |
1193 | * smk_read_onlycap - read() for /smack/onlycap | |
1194 | * @filp: file pointer, not actually used | |
1195 | * @buf: where to put the result | |
1196 | * @cn: maximum to send along | |
1197 | * @ppos: where to start | |
1198 | * | |
1199 | * Returns number of bytes read or error code, as appropriate | |
1200 | */ | |
1201 | static ssize_t smk_read_onlycap(struct file *filp, char __user *buf, | |
1202 | size_t cn, loff_t *ppos) | |
1203 | { | |
1204 | char *smack = ""; | |
1205 | ssize_t rc = -EINVAL; | |
1206 | int asize; | |
1207 | ||
1208 | if (*ppos != 0) | |
1209 | return 0; | |
1210 | ||
1211 | if (smack_onlycap != NULL) | |
1212 | smack = smack_onlycap; | |
1213 | ||
1214 | asize = strlen(smack) + 1; | |
1215 | ||
1216 | if (cn >= asize) | |
1217 | rc = simple_read_from_buffer(buf, cn, ppos, smack, asize); | |
1218 | ||
1219 | return rc; | |
1220 | } | |
1221 | ||
1222 | /** | |
1223 | * smk_write_onlycap - write() for /smack/onlycap | |
251a2a95 | 1224 | * @file: file pointer, not actually used |
15446235 CS |
1225 | * @buf: where to get the data from |
1226 | * @count: bytes sent | |
1227 | * @ppos: where to start | |
1228 | * | |
1229 | * Returns number of bytes written or error code, as appropriate | |
1230 | */ | |
1231 | static ssize_t smk_write_onlycap(struct file *file, const char __user *buf, | |
1232 | size_t count, loff_t *ppos) | |
1233 | { | |
1234 | char in[SMK_LABELLEN]; | |
676dac4b | 1235 | char *sp = smk_of_task(current->cred->security); |
15446235 CS |
1236 | |
1237 | if (!capable(CAP_MAC_ADMIN)) | |
1238 | return -EPERM; | |
1239 | ||
1240 | /* | |
1241 | * This can be done using smk_access() but is done | |
1242 | * explicitly for clarity. The smk_access() implementation | |
1243 | * would use smk_access(smack_onlycap, MAY_WRITE) | |
1244 | */ | |
1245 | if (smack_onlycap != NULL && smack_onlycap != sp) | |
1246 | return -EPERM; | |
1247 | ||
1248 | if (count >= SMK_LABELLEN) | |
1249 | return -EINVAL; | |
1250 | ||
1251 | if (copy_from_user(in, buf, count) != 0) | |
1252 | return -EFAULT; | |
1253 | ||
1254 | /* | |
1255 | * Should the null string be passed in unset the onlycap value. | |
1256 | * This seems like something to be careful with as usually | |
1257 | * smk_import only expects to return NULL for errors. It | |
1258 | * is usually the case that a nullstring or "\n" would be | |
1259 | * bad to pass to smk_import but in fact this is useful here. | |
1260 | */ | |
1261 | smack_onlycap = smk_import(in, count); | |
1262 | ||
1263 | return count; | |
1264 | } | |
1265 | ||
1266 | static const struct file_operations smk_onlycap_ops = { | |
1267 | .read = smk_read_onlycap, | |
1268 | .write = smk_write_onlycap, | |
6038f373 | 1269 | .llseek = default_llseek, |
15446235 CS |
1270 | }; |
1271 | ||
ecfcc53f EB |
1272 | /** |
1273 | * smk_read_logging - read() for /smack/logging | |
1274 | * @filp: file pointer, not actually used | |
1275 | * @buf: where to put the result | |
1276 | * @cn: maximum to send along | |
1277 | * @ppos: where to start | |
1278 | * | |
1279 | * Returns number of bytes read or error code, as appropriate | |
1280 | */ | |
1281 | static ssize_t smk_read_logging(struct file *filp, char __user *buf, | |
1282 | size_t count, loff_t *ppos) | |
1283 | { | |
1284 | char temp[32]; | |
1285 | ssize_t rc; | |
1286 | ||
1287 | if (*ppos != 0) | |
1288 | return 0; | |
1289 | ||
1290 | sprintf(temp, "%d\n", log_policy); | |
1291 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
1292 | return rc; | |
1293 | } | |
1294 | ||
1295 | /** | |
1296 | * smk_write_logging - write() for /smack/logging | |
1297 | * @file: file pointer, not actually used | |
1298 | * @buf: where to get the data from | |
1299 | * @count: bytes sent | |
1300 | * @ppos: where to start | |
1301 | * | |
1302 | * Returns number of bytes written or error code, as appropriate | |
1303 | */ | |
1304 | static ssize_t smk_write_logging(struct file *file, const char __user *buf, | |
1305 | size_t count, loff_t *ppos) | |
1306 | { | |
1307 | char temp[32]; | |
1308 | int i; | |
1309 | ||
1310 | if (!capable(CAP_MAC_ADMIN)) | |
1311 | return -EPERM; | |
1312 | ||
1313 | if (count >= sizeof(temp) || count == 0) | |
1314 | return -EINVAL; | |
1315 | ||
1316 | if (copy_from_user(temp, buf, count) != 0) | |
1317 | return -EFAULT; | |
1318 | ||
1319 | temp[count] = '\0'; | |
1320 | ||
1321 | if (sscanf(temp, "%d", &i) != 1) | |
1322 | return -EINVAL; | |
1323 | if (i < 0 || i > 3) | |
1324 | return -EINVAL; | |
1325 | log_policy = i; | |
1326 | return count; | |
1327 | } | |
1328 | ||
1329 | ||
1330 | ||
1331 | static const struct file_operations smk_logging_ops = { | |
1332 | .read = smk_read_logging, | |
1333 | .write = smk_write_logging, | |
6038f373 | 1334 | .llseek = default_llseek, |
ecfcc53f | 1335 | }; |
7898e1f8 CS |
1336 | |
1337 | /* | |
1338 | * Seq_file read operations for /smack/load-self | |
1339 | */ | |
1340 | ||
1341 | static void *load_self_seq_start(struct seq_file *s, loff_t *pos) | |
1342 | { | |
1343 | struct task_smack *tsp = current_security(); | |
1344 | ||
1345 | if (*pos == SEQ_READ_FINISHED) | |
1346 | return NULL; | |
1347 | if (list_empty(&tsp->smk_rules)) | |
1348 | return NULL; | |
1349 | return tsp->smk_rules.next; | |
1350 | } | |
1351 | ||
1352 | static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
1353 | { | |
1354 | struct task_smack *tsp = current_security(); | |
1355 | struct list_head *list = v; | |
1356 | ||
1357 | if (list_is_last(list, &tsp->smk_rules)) { | |
1358 | *pos = SEQ_READ_FINISHED; | |
1359 | return NULL; | |
1360 | } | |
1361 | return list->next; | |
1362 | } | |
1363 | ||
1364 | static int load_self_seq_show(struct seq_file *s, void *v) | |
1365 | { | |
1366 | struct list_head *list = v; | |
1367 | struct smack_rule *srp = | |
1368 | list_entry(list, struct smack_rule, list); | |
1369 | ||
1370 | seq_printf(s, "%s %s", (char *)srp->smk_subject, | |
1371 | (char *)srp->smk_object); | |
1372 | ||
1373 | seq_putc(s, ' '); | |
1374 | ||
1375 | if (srp->smk_access & MAY_READ) | |
1376 | seq_putc(s, 'r'); | |
1377 | if (srp->smk_access & MAY_WRITE) | |
1378 | seq_putc(s, 'w'); | |
1379 | if (srp->smk_access & MAY_EXEC) | |
1380 | seq_putc(s, 'x'); | |
1381 | if (srp->smk_access & MAY_APPEND) | |
1382 | seq_putc(s, 'a'); | |
1383 | if (srp->smk_access & MAY_TRANSMUTE) | |
1384 | seq_putc(s, 't'); | |
1385 | if (srp->smk_access == 0) | |
1386 | seq_putc(s, '-'); | |
1387 | ||
1388 | seq_putc(s, '\n'); | |
1389 | ||
1390 | return 0; | |
1391 | } | |
1392 | ||
1393 | static void load_self_seq_stop(struct seq_file *s, void *v) | |
1394 | { | |
1395 | /* No-op */ | |
1396 | } | |
1397 | ||
1398 | static const struct seq_operations load_self_seq_ops = { | |
1399 | .start = load_self_seq_start, | |
1400 | .next = load_self_seq_next, | |
1401 | .show = load_self_seq_show, | |
1402 | .stop = load_self_seq_stop, | |
1403 | }; | |
1404 | ||
1405 | ||
1406 | /** | |
1407 | * smk_open_load_self - open() for /smack/load-self | |
1408 | * @inode: inode structure representing file | |
1409 | * @file: "load" file pointer | |
1410 | * | |
1411 | * For reading, use load_seq_* seq_file reading operations. | |
1412 | */ | |
1413 | static int smk_open_load_self(struct inode *inode, struct file *file) | |
1414 | { | |
1415 | return seq_open(file, &load_self_seq_ops); | |
1416 | } | |
1417 | ||
1418 | /** | |
1419 | * smk_write_load_self - write() for /smack/load-self | |
1420 | * @file: file pointer, not actually used | |
1421 | * @buf: where to get the data from | |
1422 | * @count: bytes sent | |
1423 | * @ppos: where to start - must be 0 | |
1424 | * | |
1425 | */ | |
1426 | static ssize_t smk_write_load_self(struct file *file, const char __user *buf, | |
1427 | size_t count, loff_t *ppos) | |
1428 | { | |
1429 | struct task_smack *tsp = current_security(); | |
1430 | ||
1431 | return smk_write_load_list(file, buf, count, ppos, &tsp->smk_rules, | |
1432 | &tsp->smk_rules_lock); | |
1433 | } | |
1434 | ||
1435 | static const struct file_operations smk_load_self_ops = { | |
1436 | .open = smk_open_load_self, | |
1437 | .read = seq_read, | |
1438 | .llseek = seq_lseek, | |
1439 | .write = smk_write_load_self, | |
1440 | .release = seq_release, | |
1441 | }; | |
828716c2 JS |
1442 | |
1443 | /** | |
1444 | * smk_write_access - handle access check transaction | |
1445 | * @file: file pointer | |
1446 | * @buf: data from user space | |
1447 | * @count: bytes sent | |
1448 | * @ppos: where to start - must be 0 | |
1449 | */ | |
1450 | static ssize_t smk_write_access(struct file *file, const char __user *buf, | |
1451 | size_t count, loff_t *ppos) | |
1452 | { | |
1453 | struct smack_rule rule; | |
1454 | char *data; | |
1455 | ||
1456 | if (!capable(CAP_MAC_ADMIN)) | |
1457 | return -EPERM; | |
1458 | ||
1459 | data = simple_transaction_get(file, buf, count); | |
1460 | if (IS_ERR(data)) | |
1461 | return PTR_ERR(data); | |
1462 | ||
1463 | if (count < SMK_LOADLEN || smk_parse_rule(data, &rule)) | |
1464 | return -EINVAL; | |
1465 | ||
1466 | data[0] = smk_access(rule.smk_subject, rule.smk_object, | |
1467 | rule.smk_access, NULL) == 0; | |
1468 | ||
1469 | simple_transaction_set(file, 1); | |
1470 | return SMK_LOADLEN; | |
1471 | } | |
1472 | ||
1473 | static const struct file_operations smk_access_ops = { | |
1474 | .write = smk_write_access, | |
1475 | .read = simple_transaction_read, | |
1476 | .release = simple_transaction_release, | |
1477 | .llseek = generic_file_llseek, | |
1478 | }; | |
1479 | ||
e114e473 CS |
1480 | /** |
1481 | * smk_fill_super - fill the /smackfs superblock | |
1482 | * @sb: the empty superblock | |
1483 | * @data: unused | |
1484 | * @silent: unused | |
1485 | * | |
1486 | * Fill in the well known entries for /smack | |
1487 | * | |
1488 | * Returns 0 on success, an error code on failure | |
1489 | */ | |
1490 | static int smk_fill_super(struct super_block *sb, void *data, int silent) | |
1491 | { | |
1492 | int rc; | |
1493 | struct inode *root_inode; | |
1494 | ||
1495 | static struct tree_descr smack_files[] = { | |
7898e1f8 CS |
1496 | [SMK_LOAD] = { |
1497 | "load", &smk_load_ops, S_IRUGO|S_IWUSR}, | |
1498 | [SMK_CIPSO] = { | |
1499 | "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR}, | |
1500 | [SMK_DOI] = { | |
1501 | "doi", &smk_doi_ops, S_IRUGO|S_IWUSR}, | |
1502 | [SMK_DIRECT] = { | |
1503 | "direct", &smk_direct_ops, S_IRUGO|S_IWUSR}, | |
1504 | [SMK_AMBIENT] = { | |
1505 | "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR}, | |
1506 | [SMK_NETLBLADDR] = { | |
1507 | "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR}, | |
1508 | [SMK_ONLYCAP] = { | |
1509 | "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR}, | |
1510 | [SMK_LOGGING] = { | |
1511 | "logging", &smk_logging_ops, S_IRUGO|S_IWUSR}, | |
1512 | [SMK_LOAD_SELF] = { | |
1513 | "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO}, | |
828716c2 JS |
1514 | [SMK_ACCESSES] = { |
1515 | "access", &smk_access_ops, S_IRUGO|S_IWUSR}, | |
7898e1f8 CS |
1516 | /* last one */ |
1517 | {""} | |
e114e473 CS |
1518 | }; |
1519 | ||
1520 | rc = simple_fill_super(sb, SMACK_MAGIC, smack_files); | |
1521 | if (rc != 0) { | |
1522 | printk(KERN_ERR "%s failed %d while creating inodes\n", | |
1523 | __func__, rc); | |
1524 | return rc; | |
1525 | } | |
1526 | ||
1527 | root_inode = sb->s_root->d_inode; | |
1528 | root_inode->i_security = new_inode_smack(smack_known_floor.smk_known); | |
1529 | ||
1530 | return 0; | |
1531 | } | |
1532 | ||
1533 | /** | |
fc14f2fe | 1534 | * smk_mount - get the smackfs superblock |
e114e473 CS |
1535 | * @fs_type: passed along without comment |
1536 | * @flags: passed along without comment | |
1537 | * @dev_name: passed along without comment | |
1538 | * @data: passed along without comment | |
e114e473 CS |
1539 | * |
1540 | * Just passes everything along. | |
1541 | * | |
1542 | * Returns what the lower level code does. | |
1543 | */ | |
fc14f2fe AV |
1544 | static struct dentry *smk_mount(struct file_system_type *fs_type, |
1545 | int flags, const char *dev_name, void *data) | |
e114e473 | 1546 | { |
fc14f2fe | 1547 | return mount_single(fs_type, flags, data, smk_fill_super); |
e114e473 CS |
1548 | } |
1549 | ||
1550 | static struct file_system_type smk_fs_type = { | |
1551 | .name = "smackfs", | |
fc14f2fe | 1552 | .mount = smk_mount, |
e114e473 CS |
1553 | .kill_sb = kill_litter_super, |
1554 | }; | |
1555 | ||
1556 | static struct vfsmount *smackfs_mount; | |
1557 | ||
1558 | /** | |
1559 | * init_smk_fs - get the smackfs superblock | |
1560 | * | |
1561 | * register the smackfs | |
1562 | * | |
076c54c5 AD |
1563 | * Do not register smackfs if Smack wasn't enabled |
1564 | * on boot. We can not put this method normally under the | |
1565 | * smack_init() code path since the security subsystem get | |
1566 | * initialized before the vfs caches. | |
1567 | * | |
1568 | * Returns true if we were not chosen on boot or if | |
1569 | * we were chosen and filesystem registration succeeded. | |
e114e473 CS |
1570 | */ |
1571 | static int __init init_smk_fs(void) | |
1572 | { | |
1573 | int err; | |
1574 | ||
076c54c5 AD |
1575 | if (!security_module_enable(&smack_ops)) |
1576 | return 0; | |
1577 | ||
e114e473 CS |
1578 | err = register_filesystem(&smk_fs_type); |
1579 | if (!err) { | |
1580 | smackfs_mount = kern_mount(&smk_fs_type); | |
1581 | if (IS_ERR(smackfs_mount)) { | |
1582 | printk(KERN_ERR "smackfs: could not mount!\n"); | |
1583 | err = PTR_ERR(smackfs_mount); | |
1584 | smackfs_mount = NULL; | |
1585 | } | |
1586 | } | |
1587 | ||
e114e473 | 1588 | smk_cipso_doi(); |
4bc87e62 | 1589 | smk_unlbl_ambient(NULL); |
e114e473 CS |
1590 | |
1591 | return err; | |
1592 | } | |
1593 | ||
1594 | __initcall(init_smk_fs); |