]>
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> | |
23 | #include <net/netlabel.h> | |
24 | #include <net/cipso_ipv4.h> | |
25 | #include <linux/seq_file.h> | |
26 | #include <linux/ctype.h> | |
4bc87e62 | 27 | #include <linux/audit.h> |
e114e473 CS |
28 | #include "smack.h" |
29 | ||
30 | /* | |
31 | * smackfs pseudo filesystem. | |
32 | */ | |
33 | ||
34 | enum smk_inos { | |
35 | SMK_ROOT_INO = 2, | |
36 | SMK_LOAD = 3, /* load policy */ | |
37 | SMK_CIPSO = 4, /* load label -> CIPSO mapping */ | |
38 | SMK_DOI = 5, /* CIPSO DOI */ | |
39 | SMK_DIRECT = 6, /* CIPSO level indicating direct label */ | |
40 | SMK_AMBIENT = 7, /* internet ambient label */ | |
41 | SMK_NLTYPE = 8, /* label scheme to use by default */ | |
15446235 | 42 | SMK_ONLYCAP = 9, /* the only "capable" label */ |
e114e473 CS |
43 | }; |
44 | ||
45 | /* | |
46 | * List locks | |
47 | */ | |
48 | static DEFINE_MUTEX(smack_list_lock); | |
49 | static DEFINE_MUTEX(smack_cipso_lock); | |
4bc87e62 | 50 | static DEFINE_MUTEX(smack_ambient_lock); |
e114e473 CS |
51 | |
52 | /* | |
53 | * This is the "ambient" label for network traffic. | |
54 | * If it isn't somehow marked, use this. | |
55 | * It can be reset via smackfs/ambient | |
56 | */ | |
57 | char *smack_net_ambient = smack_known_floor.smk_known; | |
58 | ||
59 | /* | |
60 | * This is the default packet marking scheme for network traffic. | |
61 | * It can be reset via smackfs/nltype | |
62 | */ | |
63 | int smack_net_nltype = NETLBL_NLTYPE_CIPSOV4; | |
64 | ||
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 | ||
e114e473 CS |
82 | static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT; |
83 | struct smk_list_entry *smack_list; | |
84 | ||
85 | #define SEQ_READ_FINISHED 1 | |
86 | ||
e114e473 CS |
87 | /* |
88 | * Values for parsing cipso rules | |
89 | * SMK_DIGITLEN: Length of a digit field in a rule. | |
b500ce8d AD |
90 | * SMK_CIPSOMIN: Minimum possible cipso rule length. |
91 | * SMK_CIPSOMAX: Maximum possible cipso rule length. | |
e114e473 CS |
92 | */ |
93 | #define SMK_DIGITLEN 4 | |
b500ce8d AD |
94 | #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN) |
95 | #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN) | |
96 | ||
97 | /* | |
98 | * Values for parsing MAC rules | |
99 | * SMK_ACCESS: Maximum possible combination of access permissions | |
100 | * SMK_ACCESSLEN: Maximum length for a rule access field | |
101 | * SMK_LOADLEN: Smack rule length | |
102 | */ | |
103 | #define SMK_ACCESS "rwxa" | |
104 | #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1) | |
105 | #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN) | |
106 | ||
e114e473 CS |
107 | |
108 | /* | |
109 | * Seq_file read operations for /smack/load | |
110 | */ | |
111 | ||
112 | static void *load_seq_start(struct seq_file *s, loff_t *pos) | |
113 | { | |
114 | if (*pos == SEQ_READ_FINISHED) | |
115 | return NULL; | |
116 | ||
117 | return smack_list; | |
118 | } | |
119 | ||
120 | static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
121 | { | |
122 | struct smk_list_entry *skp = ((struct smk_list_entry *) v)->smk_next; | |
123 | ||
124 | if (skp == NULL) | |
125 | *pos = SEQ_READ_FINISHED; | |
126 | ||
127 | return skp; | |
128 | } | |
129 | ||
130 | static int load_seq_show(struct seq_file *s, void *v) | |
131 | { | |
132 | struct smk_list_entry *slp = (struct smk_list_entry *) v; | |
133 | struct smack_rule *srp = &slp->smk_rule; | |
134 | ||
135 | seq_printf(s, "%s %s", (char *)srp->smk_subject, | |
136 | (char *)srp->smk_object); | |
137 | ||
138 | seq_putc(s, ' '); | |
139 | ||
140 | if (srp->smk_access & MAY_READ) | |
141 | seq_putc(s, 'r'); | |
142 | if (srp->smk_access & MAY_WRITE) | |
143 | seq_putc(s, 'w'); | |
144 | if (srp->smk_access & MAY_EXEC) | |
145 | seq_putc(s, 'x'); | |
146 | if (srp->smk_access & MAY_APPEND) | |
147 | seq_putc(s, 'a'); | |
148 | if (srp->smk_access == 0) | |
149 | seq_putc(s, '-'); | |
150 | ||
151 | seq_putc(s, '\n'); | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | static void load_seq_stop(struct seq_file *s, void *v) | |
157 | { | |
158 | /* No-op */ | |
159 | } | |
160 | ||
161 | static struct seq_operations load_seq_ops = { | |
162 | .start = load_seq_start, | |
163 | .next = load_seq_next, | |
164 | .show = load_seq_show, | |
165 | .stop = load_seq_stop, | |
166 | }; | |
167 | ||
168 | /** | |
169 | * smk_open_load - open() for /smack/load | |
170 | * @inode: inode structure representing file | |
171 | * @file: "load" file pointer | |
172 | * | |
173 | * For reading, use load_seq_* seq_file reading operations. | |
174 | */ | |
175 | static int smk_open_load(struct inode *inode, struct file *file) | |
176 | { | |
cb622bbb | 177 | return seq_open(file, &load_seq_ops); |
e114e473 CS |
178 | } |
179 | ||
180 | /** | |
181 | * smk_set_access - add a rule to the rule list | |
182 | * @srp: the new rule to add | |
183 | * | |
184 | * Looks through the current subject/object/access list for | |
185 | * the subject/object pair and replaces the access that was | |
186 | * there. If the pair isn't found add it with the specified | |
187 | * access. | |
81ea714b SL |
188 | * |
189 | * Returns 0 if nothing goes wrong or -ENOMEM if it fails | |
190 | * during the allocation of the new pair to add. | |
e114e473 | 191 | */ |
81ea714b | 192 | static int smk_set_access(struct smack_rule *srp) |
e114e473 CS |
193 | { |
194 | struct smk_list_entry *sp; | |
195 | struct smk_list_entry *newp; | |
81ea714b | 196 | int ret = 0; |
e114e473 CS |
197 | |
198 | mutex_lock(&smack_list_lock); | |
199 | ||
200 | for (sp = smack_list; sp != NULL; sp = sp->smk_next) | |
201 | if (sp->smk_rule.smk_subject == srp->smk_subject && | |
202 | sp->smk_rule.smk_object == srp->smk_object) { | |
203 | sp->smk_rule.smk_access = srp->smk_access; | |
204 | break; | |
205 | } | |
206 | ||
207 | if (sp == NULL) { | |
208 | newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL); | |
81ea714b SL |
209 | if (newp == NULL) { |
210 | ret = -ENOMEM; | |
211 | goto out; | |
212 | } | |
213 | ||
e114e473 CS |
214 | newp->smk_rule = *srp; |
215 | newp->smk_next = smack_list; | |
216 | smack_list = newp; | |
217 | } | |
218 | ||
81ea714b | 219 | out: |
e114e473 CS |
220 | mutex_unlock(&smack_list_lock); |
221 | ||
81ea714b | 222 | return ret; |
e114e473 CS |
223 | } |
224 | ||
225 | /** | |
226 | * smk_write_load - write() for /smack/load | |
227 | * @filp: file pointer, not actually used | |
228 | * @buf: where to get the data from | |
229 | * @count: bytes sent | |
230 | * @ppos: where to start - must be 0 | |
231 | * | |
232 | * Get one smack access rule from above. | |
233 | * The format is exactly: | |
234 | * char subject[SMK_LABELLEN] | |
235 | * char object[SMK_LABELLEN] | |
b500ce8d | 236 | * char access[SMK_ACCESSLEN] |
e114e473 | 237 | * |
b500ce8d | 238 | * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes. |
e114e473 | 239 | */ |
e114e473 CS |
240 | static ssize_t smk_write_load(struct file *file, const char __user *buf, |
241 | size_t count, loff_t *ppos) | |
242 | { | |
243 | struct smack_rule rule; | |
244 | char *data; | |
245 | int rc = -EINVAL; | |
246 | ||
247 | /* | |
248 | * Must have privilege. | |
249 | * No partial writes. | |
250 | * Enough data must be present. | |
251 | */ | |
252 | if (!capable(CAP_MAC_ADMIN)) | |
253 | return -EPERM; | |
254 | if (*ppos != 0) | |
255 | return -EINVAL; | |
b500ce8d | 256 | if (count != SMK_LOADLEN) |
e114e473 CS |
257 | return -EINVAL; |
258 | ||
259 | data = kzalloc(count, GFP_KERNEL); | |
260 | if (data == NULL) | |
261 | return -ENOMEM; | |
262 | ||
263 | if (copy_from_user(data, buf, count) != 0) { | |
264 | rc = -EFAULT; | |
265 | goto out; | |
266 | } | |
267 | ||
268 | rule.smk_subject = smk_import(data, 0); | |
269 | if (rule.smk_subject == NULL) | |
270 | goto out; | |
271 | ||
272 | rule.smk_object = smk_import(data + SMK_LABELLEN, 0); | |
273 | if (rule.smk_object == NULL) | |
274 | goto out; | |
275 | ||
276 | rule.smk_access = 0; | |
277 | ||
278 | switch (data[SMK_LABELLEN + SMK_LABELLEN]) { | |
279 | case '-': | |
280 | break; | |
281 | case 'r': | |
282 | case 'R': | |
283 | rule.smk_access |= MAY_READ; | |
284 | break; | |
285 | default: | |
286 | goto out; | |
287 | } | |
288 | ||
289 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) { | |
290 | case '-': | |
291 | break; | |
292 | case 'w': | |
293 | case 'W': | |
294 | rule.smk_access |= MAY_WRITE; | |
295 | break; | |
296 | default: | |
297 | goto out; | |
298 | } | |
299 | ||
300 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) { | |
301 | case '-': | |
302 | break; | |
303 | case 'x': | |
304 | case 'X': | |
305 | rule.smk_access |= MAY_EXEC; | |
306 | break; | |
307 | default: | |
308 | goto out; | |
309 | } | |
310 | ||
311 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) { | |
312 | case '-': | |
313 | break; | |
314 | case 'a': | |
315 | case 'A': | |
316 | rule.smk_access |= MAY_READ; | |
317 | break; | |
318 | default: | |
319 | goto out; | |
320 | } | |
321 | ||
81ea714b SL |
322 | rc = smk_set_access(&rule); |
323 | ||
324 | if (!rc) | |
325 | rc = count; | |
e114e473 CS |
326 | |
327 | out: | |
328 | kfree(data); | |
329 | return rc; | |
330 | } | |
331 | ||
332 | static const struct file_operations smk_load_ops = { | |
333 | .open = smk_open_load, | |
334 | .read = seq_read, | |
335 | .llseek = seq_lseek, | |
336 | .write = smk_write_load, | |
cb622bbb | 337 | .release = seq_release, |
e114e473 CS |
338 | }; |
339 | ||
340 | /** | |
341 | * smk_cipso_doi - initialize the CIPSO domain | |
342 | */ | |
30aa4faf | 343 | static void smk_cipso_doi(void) |
e114e473 CS |
344 | { |
345 | int rc; | |
346 | struct cipso_v4_doi *doip; | |
347 | struct netlbl_audit audit_info; | |
348 | ||
4bc87e62 | 349 | audit_info.loginuid = audit_get_loginuid(current); |
2532386f | 350 | audit_info.sessionid = audit_get_sessionid(current); |
86a264ab | 351 | audit_info.secid = smack_to_secid(current_security()); |
4bc87e62 | 352 | |
e114e473 CS |
353 | rc = netlbl_cfg_map_del(NULL, &audit_info); |
354 | if (rc != 0) | |
355 | printk(KERN_WARNING "%s:%d remove rc = %d\n", | |
356 | __func__, __LINE__, rc); | |
357 | ||
358 | doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL); | |
359 | if (doip == NULL) | |
360 | panic("smack: Failed to initialize cipso DOI.\n"); | |
361 | doip->map.std = NULL; | |
362 | doip->doi = smk_cipso_doi_value; | |
363 | doip->type = CIPSO_V4_MAP_PASS; | |
364 | doip->tags[0] = CIPSO_V4_TAG_RBITMAP; | |
365 | for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++) | |
366 | doip->tags[rc] = CIPSO_V4_TAG_INVALID; | |
367 | ||
368 | rc = netlbl_cfg_cipsov4_add_map(doip, NULL, &audit_info); | |
b1edeb10 | 369 | if (rc != 0) { |
e114e473 CS |
370 | printk(KERN_WARNING "%s:%d add rc = %d\n", |
371 | __func__, __LINE__, rc); | |
b1edeb10 PM |
372 | kfree(doip); |
373 | } | |
e114e473 CS |
374 | } |
375 | ||
4bc87e62 CS |
376 | /** |
377 | * smk_unlbl_ambient - initialize the unlabeled domain | |
378 | */ | |
30aa4faf | 379 | static void smk_unlbl_ambient(char *oldambient) |
4bc87e62 CS |
380 | { |
381 | int rc; | |
382 | struct netlbl_audit audit_info; | |
383 | ||
384 | audit_info.loginuid = audit_get_loginuid(current); | |
2532386f | 385 | audit_info.sessionid = audit_get_sessionid(current); |
86a264ab | 386 | audit_info.secid = smack_to_secid(current_security()); |
4bc87e62 CS |
387 | |
388 | if (oldambient != NULL) { | |
389 | rc = netlbl_cfg_map_del(oldambient, &audit_info); | |
390 | if (rc != 0) | |
391 | printk(KERN_WARNING "%s:%d remove rc = %d\n", | |
392 | __func__, __LINE__, rc); | |
393 | } | |
394 | ||
395 | rc = netlbl_cfg_unlbl_add_map(smack_net_ambient, &audit_info); | |
396 | if (rc != 0) | |
397 | printk(KERN_WARNING "%s:%d add rc = %d\n", | |
398 | __func__, __LINE__, rc); | |
399 | } | |
400 | ||
e114e473 CS |
401 | /* |
402 | * Seq_file read operations for /smack/cipso | |
403 | */ | |
404 | ||
405 | static void *cipso_seq_start(struct seq_file *s, loff_t *pos) | |
406 | { | |
407 | if (*pos == SEQ_READ_FINISHED) | |
408 | return NULL; | |
409 | ||
410 | return smack_known; | |
411 | } | |
412 | ||
413 | static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
414 | { | |
415 | struct smack_known *skp = ((struct smack_known *) v)->smk_next; | |
416 | ||
417 | /* | |
418 | * Omit labels with no associated cipso value | |
419 | */ | |
420 | while (skp != NULL && !skp->smk_cipso) | |
421 | skp = skp->smk_next; | |
422 | ||
423 | if (skp == NULL) | |
424 | *pos = SEQ_READ_FINISHED; | |
425 | ||
426 | return skp; | |
427 | } | |
428 | ||
429 | /* | |
430 | * Print cipso labels in format: | |
431 | * label level[/cat[,cat]] | |
432 | */ | |
433 | static int cipso_seq_show(struct seq_file *s, void *v) | |
434 | { | |
435 | struct smack_known *skp = (struct smack_known *) v; | |
436 | struct smack_cipso *scp = skp->smk_cipso; | |
437 | char *cbp; | |
438 | char sep = '/'; | |
439 | int cat = 1; | |
440 | int i; | |
441 | unsigned char m; | |
442 | ||
443 | if (scp == NULL) | |
444 | return 0; | |
445 | ||
446 | seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level); | |
447 | ||
448 | cbp = scp->smk_catset; | |
449 | for (i = 0; i < SMK_LABELLEN; i++) | |
450 | for (m = 0x80; m != 0; m >>= 1) { | |
451 | if (m & cbp[i]) { | |
452 | seq_printf(s, "%c%d", sep, cat); | |
453 | sep = ','; | |
454 | } | |
455 | cat++; | |
456 | } | |
457 | ||
458 | seq_putc(s, '\n'); | |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
463 | static void cipso_seq_stop(struct seq_file *s, void *v) | |
464 | { | |
465 | /* No-op */ | |
466 | } | |
467 | ||
468 | static struct seq_operations cipso_seq_ops = { | |
469 | .start = cipso_seq_start, | |
470 | .stop = cipso_seq_stop, | |
471 | .next = cipso_seq_next, | |
472 | .show = cipso_seq_show, | |
473 | }; | |
474 | ||
475 | /** | |
476 | * smk_open_cipso - open() for /smack/cipso | |
477 | * @inode: inode structure representing file | |
478 | * @file: "cipso" file pointer | |
479 | * | |
480 | * Connect our cipso_seq_* operations with /smack/cipso | |
481 | * file_operations | |
482 | */ | |
483 | static int smk_open_cipso(struct inode *inode, struct file *file) | |
484 | { | |
485 | return seq_open(file, &cipso_seq_ops); | |
486 | } | |
487 | ||
488 | /** | |
489 | * smk_write_cipso - write() for /smack/cipso | |
490 | * @filp: file pointer, not actually used | |
491 | * @buf: where to get the data from | |
492 | * @count: bytes sent | |
493 | * @ppos: where to start | |
494 | * | |
495 | * Accepts only one cipso rule per write call. | |
496 | * Returns number of bytes written or error code, as appropriate | |
497 | */ | |
498 | static ssize_t smk_write_cipso(struct file *file, const char __user *buf, | |
499 | size_t count, loff_t *ppos) | |
500 | { | |
501 | struct smack_known *skp; | |
502 | struct smack_cipso *scp = NULL; | |
503 | char mapcatset[SMK_LABELLEN]; | |
504 | int maplevel; | |
505 | int cat; | |
506 | int catlen; | |
507 | ssize_t rc = -EINVAL; | |
508 | char *data = NULL; | |
509 | char *rule; | |
510 | int ret; | |
511 | int i; | |
512 | ||
513 | /* | |
514 | * Must have privilege. | |
515 | * No partial writes. | |
516 | * Enough data must be present. | |
517 | */ | |
518 | if (!capable(CAP_MAC_ADMIN)) | |
519 | return -EPERM; | |
520 | if (*ppos != 0) | |
521 | return -EINVAL; | |
b500ce8d | 522 | if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX) |
e114e473 CS |
523 | return -EINVAL; |
524 | ||
525 | data = kzalloc(count + 1, GFP_KERNEL); | |
526 | if (data == NULL) | |
527 | return -ENOMEM; | |
528 | ||
529 | if (copy_from_user(data, buf, count) != 0) { | |
530 | rc = -EFAULT; | |
531 | goto unlockedout; | |
532 | } | |
533 | ||
534 | data[count] = '\0'; | |
535 | rule = data; | |
536 | /* | |
537 | * Only allow one writer at a time. Writes should be | |
538 | * quite rare and small in any case. | |
539 | */ | |
540 | mutex_lock(&smack_cipso_lock); | |
541 | ||
542 | skp = smk_import_entry(rule, 0); | |
543 | if (skp == NULL) | |
544 | goto out; | |
545 | ||
546 | rule += SMK_LABELLEN;; | |
547 | ret = sscanf(rule, "%d", &maplevel); | |
548 | if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL) | |
549 | goto out; | |
550 | ||
551 | rule += SMK_DIGITLEN; | |
552 | ret = sscanf(rule, "%d", &catlen); | |
553 | if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM) | |
554 | goto out; | |
555 | ||
b500ce8d | 556 | if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN)) |
e114e473 CS |
557 | goto out; |
558 | ||
559 | memset(mapcatset, 0, sizeof(mapcatset)); | |
560 | ||
561 | for (i = 0; i < catlen; i++) { | |
562 | rule += SMK_DIGITLEN; | |
563 | ret = sscanf(rule, "%d", &cat); | |
564 | if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL) | |
565 | goto out; | |
566 | ||
567 | smack_catset_bit(cat, mapcatset); | |
568 | } | |
569 | ||
570 | if (skp->smk_cipso == NULL) { | |
571 | scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL); | |
572 | if (scp == NULL) { | |
573 | rc = -ENOMEM; | |
574 | goto out; | |
575 | } | |
576 | } | |
577 | ||
578 | spin_lock_bh(&skp->smk_cipsolock); | |
579 | ||
580 | if (scp == NULL) | |
581 | scp = skp->smk_cipso; | |
582 | else | |
583 | skp->smk_cipso = scp; | |
584 | ||
585 | scp->smk_level = maplevel; | |
586 | memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset)); | |
587 | ||
588 | spin_unlock_bh(&skp->smk_cipsolock); | |
589 | ||
590 | rc = count; | |
591 | out: | |
592 | mutex_unlock(&smack_cipso_lock); | |
593 | unlockedout: | |
594 | kfree(data); | |
595 | return rc; | |
596 | } | |
597 | ||
598 | static const struct file_operations smk_cipso_ops = { | |
599 | .open = smk_open_cipso, | |
600 | .read = seq_read, | |
601 | .llseek = seq_lseek, | |
602 | .write = smk_write_cipso, | |
603 | .release = seq_release, | |
604 | }; | |
605 | ||
606 | /** | |
607 | * smk_read_doi - read() for /smack/doi | |
608 | * @filp: file pointer, not actually used | |
609 | * @buf: where to put the result | |
610 | * @count: maximum to send along | |
611 | * @ppos: where to start | |
612 | * | |
613 | * Returns number of bytes read or error code, as appropriate | |
614 | */ | |
615 | static ssize_t smk_read_doi(struct file *filp, char __user *buf, | |
616 | size_t count, loff_t *ppos) | |
617 | { | |
618 | char temp[80]; | |
619 | ssize_t rc; | |
620 | ||
621 | if (*ppos != 0) | |
622 | return 0; | |
623 | ||
624 | sprintf(temp, "%d", smk_cipso_doi_value); | |
625 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
626 | ||
627 | return rc; | |
628 | } | |
629 | ||
630 | /** | |
631 | * smk_write_doi - write() for /smack/doi | |
632 | * @filp: file pointer, not actually used | |
633 | * @buf: where to get the data from | |
634 | * @count: bytes sent | |
635 | * @ppos: where to start | |
636 | * | |
637 | * Returns number of bytes written or error code, as appropriate | |
638 | */ | |
639 | static ssize_t smk_write_doi(struct file *file, const char __user *buf, | |
640 | size_t count, loff_t *ppos) | |
641 | { | |
642 | char temp[80]; | |
643 | int i; | |
644 | ||
645 | if (!capable(CAP_MAC_ADMIN)) | |
646 | return -EPERM; | |
647 | ||
648 | if (count >= sizeof(temp) || count == 0) | |
649 | return -EINVAL; | |
650 | ||
651 | if (copy_from_user(temp, buf, count) != 0) | |
652 | return -EFAULT; | |
653 | ||
654 | temp[count] = '\0'; | |
655 | ||
656 | if (sscanf(temp, "%d", &i) != 1) | |
657 | return -EINVAL; | |
658 | ||
659 | smk_cipso_doi_value = i; | |
660 | ||
661 | smk_cipso_doi(); | |
662 | ||
663 | return count; | |
664 | } | |
665 | ||
666 | static const struct file_operations smk_doi_ops = { | |
667 | .read = smk_read_doi, | |
668 | .write = smk_write_doi, | |
669 | }; | |
670 | ||
671 | /** | |
672 | * smk_read_direct - read() for /smack/direct | |
673 | * @filp: file pointer, not actually used | |
674 | * @buf: where to put the result | |
675 | * @count: maximum to send along | |
676 | * @ppos: where to start | |
677 | * | |
678 | * Returns number of bytes read or error code, as appropriate | |
679 | */ | |
680 | static ssize_t smk_read_direct(struct file *filp, char __user *buf, | |
681 | size_t count, loff_t *ppos) | |
682 | { | |
683 | char temp[80]; | |
684 | ssize_t rc; | |
685 | ||
686 | if (*ppos != 0) | |
687 | return 0; | |
688 | ||
689 | sprintf(temp, "%d", smack_cipso_direct); | |
690 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
691 | ||
692 | return rc; | |
693 | } | |
694 | ||
695 | /** | |
696 | * smk_write_direct - write() for /smack/direct | |
697 | * @filp: file pointer, not actually used | |
698 | * @buf: where to get the data from | |
699 | * @count: bytes sent | |
700 | * @ppos: where to start | |
701 | * | |
702 | * Returns number of bytes written or error code, as appropriate | |
703 | */ | |
704 | static ssize_t smk_write_direct(struct file *file, const char __user *buf, | |
705 | size_t count, loff_t *ppos) | |
706 | { | |
707 | char temp[80]; | |
708 | int i; | |
709 | ||
710 | if (!capable(CAP_MAC_ADMIN)) | |
711 | return -EPERM; | |
712 | ||
713 | if (count >= sizeof(temp) || count == 0) | |
714 | return -EINVAL; | |
715 | ||
716 | if (copy_from_user(temp, buf, count) != 0) | |
717 | return -EFAULT; | |
718 | ||
719 | temp[count] = '\0'; | |
720 | ||
721 | if (sscanf(temp, "%d", &i) != 1) | |
722 | return -EINVAL; | |
723 | ||
724 | smack_cipso_direct = i; | |
725 | ||
726 | return count; | |
727 | } | |
728 | ||
729 | static const struct file_operations smk_direct_ops = { | |
730 | .read = smk_read_direct, | |
731 | .write = smk_write_direct, | |
732 | }; | |
733 | ||
734 | /** | |
735 | * smk_read_ambient - read() for /smack/ambient | |
736 | * @filp: file pointer, not actually used | |
737 | * @buf: where to put the result | |
738 | * @cn: maximum to send along | |
739 | * @ppos: where to start | |
740 | * | |
741 | * Returns number of bytes read or error code, as appropriate | |
742 | */ | |
743 | static ssize_t smk_read_ambient(struct file *filp, char __user *buf, | |
744 | size_t cn, loff_t *ppos) | |
745 | { | |
746 | ssize_t rc; | |
e114e473 CS |
747 | int asize; |
748 | ||
749 | if (*ppos != 0) | |
750 | return 0; | |
751 | /* | |
752 | * Being careful to avoid a problem in the case where | |
753 | * smack_net_ambient gets changed in midstream. | |
e114e473 | 754 | */ |
4bc87e62 | 755 | mutex_lock(&smack_ambient_lock); |
e114e473 | 756 | |
4bc87e62 CS |
757 | asize = strlen(smack_net_ambient) + 1; |
758 | ||
759 | if (cn >= asize) | |
760 | rc = simple_read_from_buffer(buf, cn, ppos, | |
761 | smack_net_ambient, asize); | |
762 | else | |
763 | rc = -EINVAL; | |
e114e473 | 764 | |
4bc87e62 | 765 | mutex_unlock(&smack_ambient_lock); |
e114e473 CS |
766 | |
767 | return rc; | |
768 | } | |
769 | ||
770 | /** | |
771 | * smk_write_ambient - write() for /smack/ambient | |
772 | * @filp: file pointer, not actually used | |
773 | * @buf: where to get the data from | |
774 | * @count: bytes sent | |
775 | * @ppos: where to start | |
776 | * | |
777 | * Returns number of bytes written or error code, as appropriate | |
778 | */ | |
779 | static ssize_t smk_write_ambient(struct file *file, const char __user *buf, | |
780 | size_t count, loff_t *ppos) | |
781 | { | |
782 | char in[SMK_LABELLEN]; | |
4bc87e62 | 783 | char *oldambient; |
e114e473 CS |
784 | char *smack; |
785 | ||
786 | if (!capable(CAP_MAC_ADMIN)) | |
787 | return -EPERM; | |
788 | ||
789 | if (count >= SMK_LABELLEN) | |
790 | return -EINVAL; | |
791 | ||
792 | if (copy_from_user(in, buf, count) != 0) | |
793 | return -EFAULT; | |
794 | ||
795 | smack = smk_import(in, count); | |
796 | if (smack == NULL) | |
797 | return -EINVAL; | |
798 | ||
4bc87e62 CS |
799 | mutex_lock(&smack_ambient_lock); |
800 | ||
801 | oldambient = smack_net_ambient; | |
e114e473 | 802 | smack_net_ambient = smack; |
4bc87e62 CS |
803 | smk_unlbl_ambient(oldambient); |
804 | ||
805 | mutex_unlock(&smack_ambient_lock); | |
e114e473 CS |
806 | |
807 | return count; | |
808 | } | |
809 | ||
810 | static const struct file_operations smk_ambient_ops = { | |
811 | .read = smk_read_ambient, | |
812 | .write = smk_write_ambient, | |
813 | }; | |
814 | ||
15446235 CS |
815 | /** |
816 | * smk_read_onlycap - read() for /smack/onlycap | |
817 | * @filp: file pointer, not actually used | |
818 | * @buf: where to put the result | |
819 | * @cn: maximum to send along | |
820 | * @ppos: where to start | |
821 | * | |
822 | * Returns number of bytes read or error code, as appropriate | |
823 | */ | |
824 | static ssize_t smk_read_onlycap(struct file *filp, char __user *buf, | |
825 | size_t cn, loff_t *ppos) | |
826 | { | |
827 | char *smack = ""; | |
828 | ssize_t rc = -EINVAL; | |
829 | int asize; | |
830 | ||
831 | if (*ppos != 0) | |
832 | return 0; | |
833 | ||
834 | if (smack_onlycap != NULL) | |
835 | smack = smack_onlycap; | |
836 | ||
837 | asize = strlen(smack) + 1; | |
838 | ||
839 | if (cn >= asize) | |
840 | rc = simple_read_from_buffer(buf, cn, ppos, smack, asize); | |
841 | ||
842 | return rc; | |
843 | } | |
844 | ||
845 | /** | |
846 | * smk_write_onlycap - write() for /smack/onlycap | |
847 | * @filp: file pointer, not actually used | |
848 | * @buf: where to get the data from | |
849 | * @count: bytes sent | |
850 | * @ppos: where to start | |
851 | * | |
852 | * Returns number of bytes written or error code, as appropriate | |
853 | */ | |
854 | static ssize_t smk_write_onlycap(struct file *file, const char __user *buf, | |
855 | size_t count, loff_t *ppos) | |
856 | { | |
857 | char in[SMK_LABELLEN]; | |
b6dff3ec | 858 | char *sp = current->cred->security; |
15446235 CS |
859 | |
860 | if (!capable(CAP_MAC_ADMIN)) | |
861 | return -EPERM; | |
862 | ||
863 | /* | |
864 | * This can be done using smk_access() but is done | |
865 | * explicitly for clarity. The smk_access() implementation | |
866 | * would use smk_access(smack_onlycap, MAY_WRITE) | |
867 | */ | |
868 | if (smack_onlycap != NULL && smack_onlycap != sp) | |
869 | return -EPERM; | |
870 | ||
871 | if (count >= SMK_LABELLEN) | |
872 | return -EINVAL; | |
873 | ||
874 | if (copy_from_user(in, buf, count) != 0) | |
875 | return -EFAULT; | |
876 | ||
877 | /* | |
878 | * Should the null string be passed in unset the onlycap value. | |
879 | * This seems like something to be careful with as usually | |
880 | * smk_import only expects to return NULL for errors. It | |
881 | * is usually the case that a nullstring or "\n" would be | |
882 | * bad to pass to smk_import but in fact this is useful here. | |
883 | */ | |
884 | smack_onlycap = smk_import(in, count); | |
885 | ||
886 | return count; | |
887 | } | |
888 | ||
889 | static const struct file_operations smk_onlycap_ops = { | |
890 | .read = smk_read_onlycap, | |
891 | .write = smk_write_onlycap, | |
892 | }; | |
893 | ||
e114e473 CS |
894 | struct option_names { |
895 | int o_number; | |
896 | char *o_name; | |
897 | char *o_alias; | |
898 | }; | |
899 | ||
900 | static struct option_names netlbl_choices[] = { | |
901 | { NETLBL_NLTYPE_RIPSO, | |
902 | NETLBL_NLTYPE_RIPSO_NAME, "ripso" }, | |
903 | { NETLBL_NLTYPE_CIPSOV4, | |
904 | NETLBL_NLTYPE_CIPSOV4_NAME, "cipsov4" }, | |
905 | { NETLBL_NLTYPE_CIPSOV4, | |
906 | NETLBL_NLTYPE_CIPSOV4_NAME, "cipso" }, | |
907 | { NETLBL_NLTYPE_CIPSOV6, | |
908 | NETLBL_NLTYPE_CIPSOV6_NAME, "cipsov6" }, | |
909 | { NETLBL_NLTYPE_UNLABELED, | |
910 | NETLBL_NLTYPE_UNLABELED_NAME, "unlabeled" }, | |
911 | }; | |
912 | ||
913 | /** | |
914 | * smk_read_nltype - read() for /smack/nltype | |
915 | * @filp: file pointer, not actually used | |
916 | * @buf: where to put the result | |
917 | * @count: maximum to send along | |
918 | * @ppos: where to start | |
919 | * | |
920 | * Returns number of bytes read or error code, as appropriate | |
921 | */ | |
922 | static ssize_t smk_read_nltype(struct file *filp, char __user *buf, | |
923 | size_t count, loff_t *ppos) | |
924 | { | |
925 | char bound[40]; | |
926 | ssize_t rc; | |
927 | int i; | |
928 | ||
929 | if (count < SMK_LABELLEN) | |
930 | return -EINVAL; | |
931 | ||
932 | if (*ppos != 0) | |
933 | return 0; | |
934 | ||
935 | sprintf(bound, "unknown"); | |
936 | ||
937 | for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++) | |
938 | if (smack_net_nltype == netlbl_choices[i].o_number) { | |
939 | sprintf(bound, "%s", netlbl_choices[i].o_name); | |
940 | break; | |
941 | } | |
942 | ||
943 | rc = simple_read_from_buffer(buf, count, ppos, bound, strlen(bound)); | |
944 | ||
945 | return rc; | |
946 | } | |
947 | ||
948 | /** | |
949 | * smk_write_nltype - write() for /smack/nltype | |
950 | * @filp: file pointer, not actually used | |
951 | * @buf: where to get the data from | |
952 | * @count: bytes sent | |
953 | * @ppos: where to start | |
954 | * | |
955 | * Returns number of bytes written or error code, as appropriate | |
956 | */ | |
957 | static ssize_t smk_write_nltype(struct file *file, const char __user *buf, | |
958 | size_t count, loff_t *ppos) | |
959 | { | |
960 | char bound[40]; | |
961 | char *cp; | |
962 | int i; | |
963 | ||
964 | if (!capable(CAP_MAC_ADMIN)) | |
965 | return -EPERM; | |
966 | ||
967 | if (count >= 40) | |
968 | return -EINVAL; | |
969 | ||
970 | if (copy_from_user(bound, buf, count) != 0) | |
971 | return -EFAULT; | |
972 | ||
973 | bound[count] = '\0'; | |
974 | cp = strchr(bound, ' '); | |
975 | if (cp != NULL) | |
976 | *cp = '\0'; | |
977 | cp = strchr(bound, '\n'); | |
978 | if (cp != NULL) | |
979 | *cp = '\0'; | |
980 | ||
981 | for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++) | |
982 | if (strcmp(bound, netlbl_choices[i].o_name) == 0 || | |
983 | strcmp(bound, netlbl_choices[i].o_alias) == 0) { | |
984 | smack_net_nltype = netlbl_choices[i].o_number; | |
985 | return count; | |
986 | } | |
987 | /* | |
988 | * Not a valid choice. | |
989 | */ | |
990 | return -EINVAL; | |
991 | } | |
992 | ||
993 | static const struct file_operations smk_nltype_ops = { | |
994 | .read = smk_read_nltype, | |
995 | .write = smk_write_nltype, | |
996 | }; | |
997 | ||
998 | /** | |
999 | * smk_fill_super - fill the /smackfs superblock | |
1000 | * @sb: the empty superblock | |
1001 | * @data: unused | |
1002 | * @silent: unused | |
1003 | * | |
1004 | * Fill in the well known entries for /smack | |
1005 | * | |
1006 | * Returns 0 on success, an error code on failure | |
1007 | */ | |
1008 | static int smk_fill_super(struct super_block *sb, void *data, int silent) | |
1009 | { | |
1010 | int rc; | |
1011 | struct inode *root_inode; | |
1012 | ||
1013 | static struct tree_descr smack_files[] = { | |
1014 | [SMK_LOAD] = | |
1015 | {"load", &smk_load_ops, S_IRUGO|S_IWUSR}, | |
1016 | [SMK_CIPSO] = | |
1017 | {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR}, | |
1018 | [SMK_DOI] = | |
1019 | {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR}, | |
1020 | [SMK_DIRECT] = | |
1021 | {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR}, | |
1022 | [SMK_AMBIENT] = | |
1023 | {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR}, | |
1024 | [SMK_NLTYPE] = | |
1025 | {"nltype", &smk_nltype_ops, S_IRUGO|S_IWUSR}, | |
15446235 CS |
1026 | [SMK_ONLYCAP] = |
1027 | {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR}, | |
e114e473 CS |
1028 | /* last one */ {""} |
1029 | }; | |
1030 | ||
1031 | rc = simple_fill_super(sb, SMACK_MAGIC, smack_files); | |
1032 | if (rc != 0) { | |
1033 | printk(KERN_ERR "%s failed %d while creating inodes\n", | |
1034 | __func__, rc); | |
1035 | return rc; | |
1036 | } | |
1037 | ||
1038 | root_inode = sb->s_root->d_inode; | |
1039 | root_inode->i_security = new_inode_smack(smack_known_floor.smk_known); | |
1040 | ||
1041 | return 0; | |
1042 | } | |
1043 | ||
1044 | /** | |
1045 | * smk_get_sb - get the smackfs superblock | |
1046 | * @fs_type: passed along without comment | |
1047 | * @flags: passed along without comment | |
1048 | * @dev_name: passed along without comment | |
1049 | * @data: passed along without comment | |
1050 | * @mnt: passed along without comment | |
1051 | * | |
1052 | * Just passes everything along. | |
1053 | * | |
1054 | * Returns what the lower level code does. | |
1055 | */ | |
1056 | static int smk_get_sb(struct file_system_type *fs_type, | |
1057 | int flags, const char *dev_name, void *data, | |
1058 | struct vfsmount *mnt) | |
1059 | { | |
1060 | return get_sb_single(fs_type, flags, data, smk_fill_super, mnt); | |
1061 | } | |
1062 | ||
1063 | static struct file_system_type smk_fs_type = { | |
1064 | .name = "smackfs", | |
1065 | .get_sb = smk_get_sb, | |
1066 | .kill_sb = kill_litter_super, | |
1067 | }; | |
1068 | ||
1069 | static struct vfsmount *smackfs_mount; | |
1070 | ||
1071 | /** | |
1072 | * init_smk_fs - get the smackfs superblock | |
1073 | * | |
1074 | * register the smackfs | |
1075 | * | |
076c54c5 AD |
1076 | * Do not register smackfs if Smack wasn't enabled |
1077 | * on boot. We can not put this method normally under the | |
1078 | * smack_init() code path since the security subsystem get | |
1079 | * initialized before the vfs caches. | |
1080 | * | |
1081 | * Returns true if we were not chosen on boot or if | |
1082 | * we were chosen and filesystem registration succeeded. | |
e114e473 CS |
1083 | */ |
1084 | static int __init init_smk_fs(void) | |
1085 | { | |
1086 | int err; | |
1087 | ||
076c54c5 AD |
1088 | if (!security_module_enable(&smack_ops)) |
1089 | return 0; | |
1090 | ||
e114e473 CS |
1091 | err = register_filesystem(&smk_fs_type); |
1092 | if (!err) { | |
1093 | smackfs_mount = kern_mount(&smk_fs_type); | |
1094 | if (IS_ERR(smackfs_mount)) { | |
1095 | printk(KERN_ERR "smackfs: could not mount!\n"); | |
1096 | err = PTR_ERR(smackfs_mount); | |
1097 | smackfs_mount = NULL; | |
1098 | } | |
1099 | } | |
1100 | ||
e114e473 | 1101 | smk_cipso_doi(); |
4bc87e62 | 1102 | smk_unlbl_ambient(NULL); |
e114e473 CS |
1103 | |
1104 | return err; | |
1105 | } | |
1106 | ||
1107 | __initcall(init_smk_fs); |