]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - security/smack/smackfs.c
Merge branch 'for-4.1/sensor-hub' into for-linus
[mirror_ubuntu-artful-kernel.git] / security / smack / smackfs.c
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 <linux/slab.h>
24 #include <net/net_namespace.h>
25 #include <net/cipso_ipv4.h>
26 #include <linux/seq_file.h>
27 #include <linux/ctype.h>
28 #include <linux/audit.h>
29 #include <linux/magic.h>
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 */
43 SMK_NETLBLADDR = 8, /* single label hosts */
44 SMK_ONLYCAP = 9, /* the only "capable" label */
45 SMK_LOGGING = 10, /* logging */
46 SMK_LOAD_SELF = 11, /* task specific rules */
47 SMK_ACCESSES = 12, /* access policy */
48 SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
49 SMK_LOAD2 = 14, /* load policy with long labels */
50 SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
51 SMK_ACCESS2 = 16, /* make an access check with long labels */
52 SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
53 SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
54 SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
55 SMK_SYSLOG = 20, /* change syslog label) */
56 SMK_PTRACE = 21, /* set ptrace rule */
57 };
58
59 /*
60 * List locks
61 */
62 static DEFINE_MUTEX(smack_cipso_lock);
63 static DEFINE_MUTEX(smack_ambient_lock);
64 static DEFINE_MUTEX(smack_syslog_lock);
65 static DEFINE_MUTEX(smk_netlbladdr_lock);
66
67 /*
68 * This is the "ambient" label for network traffic.
69 * If it isn't somehow marked, use this.
70 * It can be reset via smackfs/ambient
71 */
72 struct smack_known *smack_net_ambient;
73
74 /*
75 * This is the level in a CIPSO header that indicates a
76 * smack label is contained directly in the category set.
77 * It can be reset via smackfs/direct
78 */
79 int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
80
81 /*
82 * This is the level in a CIPSO header that indicates a
83 * secid is contained directly in the category set.
84 * It can be reset via smackfs/mapped
85 */
86 int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
87
88 /*
89 * Unless a process is running with this label even
90 * having CAP_MAC_OVERRIDE isn't enough to grant
91 * privilege to violate MAC policy. If no label is
92 * designated (the NULL case) capabilities apply to
93 * everyone. It is expected that the hat (^) label
94 * will be used if any label is used.
95 */
96 struct smack_known *smack_onlycap;
97
98 /*
99 * If this value is set restrict syslog use to the label specified.
100 * It can be reset via smackfs/syslog
101 */
102 struct smack_known *smack_syslog_label;
103
104 /*
105 * Ptrace current rule
106 * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
107 * SMACK_PTRACE_EXACT labels must match, but can be overriden with
108 * CAP_SYS_PTRACE
109 * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
110 */
111 int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
112
113 /*
114 * Certain IP addresses may be designated as single label hosts.
115 * Packets are sent there unlabeled, but only from tasks that
116 * can write to the specified label.
117 */
118
119 LIST_HEAD(smk_netlbladdr_list);
120
121 /*
122 * Rule lists are maintained for each label.
123 * This master list is just for reading /smack/load and /smack/load2.
124 */
125 struct smack_master_list {
126 struct list_head list;
127 struct smack_rule *smk_rule;
128 };
129
130 LIST_HEAD(smack_rule_list);
131
132 struct smack_parsed_rule {
133 struct smack_known *smk_subject;
134 struct smack_known *smk_object;
135 int smk_access1;
136 int smk_access2;
137 };
138
139 static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
140
141 struct smack_known smack_cipso_option = {
142 .smk_known = SMACK_CIPSO_OPTION,
143 .smk_secid = 0,
144 };
145
146 /*
147 * Values for parsing cipso rules
148 * SMK_DIGITLEN: Length of a digit field in a rule.
149 * SMK_CIPSOMIN: Minimum possible cipso rule length.
150 * SMK_CIPSOMAX: Maximum possible cipso rule length.
151 */
152 #define SMK_DIGITLEN 4
153 #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
154 #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
155
156 /*
157 * Values for parsing MAC rules
158 * SMK_ACCESS: Maximum possible combination of access permissions
159 * SMK_ACCESSLEN: Maximum length for a rule access field
160 * SMK_LOADLEN: Smack rule length
161 */
162 #define SMK_OACCESS "rwxa"
163 #define SMK_ACCESS "rwxatl"
164 #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
165 #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
166 #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
167 #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
168
169 /*
170 * Stricly for CIPSO level manipulation.
171 * Set the category bit number in a smack label sized buffer.
172 */
173 static inline void smack_catset_bit(unsigned int cat, char *catsetp)
174 {
175 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
176 return;
177
178 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
179 }
180
181 /**
182 * smk_netlabel_audit_set - fill a netlbl_audit struct
183 * @nap: structure to fill
184 */
185 static void smk_netlabel_audit_set(struct netlbl_audit *nap)
186 {
187 struct smack_known *skp = smk_of_current();
188
189 nap->loginuid = audit_get_loginuid(current);
190 nap->sessionid = audit_get_sessionid(current);
191 nap->secid = skp->smk_secid;
192 }
193
194 /*
195 * Value for parsing single label host rules
196 * "1.2.3.4 X"
197 */
198 #define SMK_NETLBLADDRMIN 9
199
200 /**
201 * smk_set_access - add a rule to the rule list or replace an old rule
202 * @srp: the rule to add or replace
203 * @rule_list: the list of rules
204 * @rule_lock: the rule list lock
205 * @global: if non-zero, indicates a global rule
206 *
207 * Looks through the current subject/object/access list for
208 * the subject/object pair and replaces the access that was
209 * there. If the pair isn't found add it with the specified
210 * access.
211 *
212 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
213 * during the allocation of the new pair to add.
214 */
215 static int smk_set_access(struct smack_parsed_rule *srp,
216 struct list_head *rule_list,
217 struct mutex *rule_lock, int global)
218 {
219 struct smack_rule *sp;
220 struct smack_master_list *smlp;
221 int found = 0;
222 int rc = 0;
223
224 mutex_lock(rule_lock);
225
226 /*
227 * Because the object label is less likely to match
228 * than the subject label check it first
229 */
230 list_for_each_entry_rcu(sp, rule_list, list) {
231 if (sp->smk_object == srp->smk_object &&
232 sp->smk_subject == srp->smk_subject) {
233 found = 1;
234 sp->smk_access |= srp->smk_access1;
235 sp->smk_access &= ~srp->smk_access2;
236 break;
237 }
238 }
239
240 if (found == 0) {
241 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
242 if (sp == NULL) {
243 rc = -ENOMEM;
244 goto out;
245 }
246
247 sp->smk_subject = srp->smk_subject;
248 sp->smk_object = srp->smk_object;
249 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
250
251 list_add_rcu(&sp->list, rule_list);
252 /*
253 * If this is a global as opposed to self and a new rule
254 * it needs to get added for reporting.
255 */
256 if (global) {
257 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
258 if (smlp != NULL) {
259 smlp->smk_rule = sp;
260 list_add_rcu(&smlp->list, &smack_rule_list);
261 } else
262 rc = -ENOMEM;
263 }
264 }
265
266 out:
267 mutex_unlock(rule_lock);
268 return rc;
269 }
270
271 /**
272 * smk_perm_from_str - parse smack accesses from a text string
273 * @string: a text string that contains a Smack accesses code
274 *
275 * Returns an integer with respective bits set for specified accesses.
276 */
277 static int smk_perm_from_str(const char *string)
278 {
279 int perm = 0;
280 const char *cp;
281
282 for (cp = string; ; cp++)
283 switch (*cp) {
284 case '-':
285 break;
286 case 'r':
287 case 'R':
288 perm |= MAY_READ;
289 break;
290 case 'w':
291 case 'W':
292 perm |= MAY_WRITE;
293 break;
294 case 'x':
295 case 'X':
296 perm |= MAY_EXEC;
297 break;
298 case 'a':
299 case 'A':
300 perm |= MAY_APPEND;
301 break;
302 case 't':
303 case 'T':
304 perm |= MAY_TRANSMUTE;
305 break;
306 case 'l':
307 case 'L':
308 perm |= MAY_LOCK;
309 break;
310 case 'b':
311 case 'B':
312 perm |= MAY_BRINGUP;
313 break;
314 default:
315 return perm;
316 }
317 }
318
319 /**
320 * smk_fill_rule - Fill Smack rule from strings
321 * @subject: subject label string
322 * @object: object label string
323 * @access1: access string
324 * @access2: string with permissions to be removed
325 * @rule: Smack rule
326 * @import: if non-zero, import labels
327 * @len: label length limit
328 *
329 * Returns 0 on success, -EINVAL on failure and -ENOENT when either subject
330 * or object is missing.
331 */
332 static int smk_fill_rule(const char *subject, const char *object,
333 const char *access1, const char *access2,
334 struct smack_parsed_rule *rule, int import,
335 int len)
336 {
337 const char *cp;
338 struct smack_known *skp;
339
340 if (import) {
341 rule->smk_subject = smk_import_entry(subject, len);
342 if (rule->smk_subject == NULL)
343 return -EINVAL;
344
345 rule->smk_object = smk_import_entry(object, len);
346 if (rule->smk_object == NULL)
347 return -EINVAL;
348 } else {
349 cp = smk_parse_smack(subject, len);
350 if (cp == NULL)
351 return -EINVAL;
352 skp = smk_find_entry(cp);
353 kfree(cp);
354 if (skp == NULL)
355 return -ENOENT;
356 rule->smk_subject = skp;
357
358 cp = smk_parse_smack(object, len);
359 if (cp == NULL)
360 return -EINVAL;
361 skp = smk_find_entry(cp);
362 kfree(cp);
363 if (skp == NULL)
364 return -ENOENT;
365 rule->smk_object = skp;
366 }
367
368 rule->smk_access1 = smk_perm_from_str(access1);
369 if (access2)
370 rule->smk_access2 = smk_perm_from_str(access2);
371 else
372 rule->smk_access2 = ~rule->smk_access1;
373
374 return 0;
375 }
376
377 /**
378 * smk_parse_rule - parse Smack rule from load string
379 * @data: string to be parsed whose size is SMK_LOADLEN
380 * @rule: Smack rule
381 * @import: if non-zero, import labels
382 *
383 * Returns 0 on success, -1 on errors.
384 */
385 static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
386 int import)
387 {
388 int rc;
389
390 rc = smk_fill_rule(data, data + SMK_LABELLEN,
391 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
392 import, SMK_LABELLEN);
393 return rc;
394 }
395
396 /**
397 * smk_parse_long_rule - parse Smack rule from rule string
398 * @data: string to be parsed, null terminated
399 * @rule: Will be filled with Smack parsed rule
400 * @import: if non-zero, import labels
401 * @tokens: numer of substrings expected in data
402 *
403 * Returns number of processed bytes on success, -1 on failure.
404 */
405 static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
406 int import, int tokens)
407 {
408 ssize_t cnt = 0;
409 char *tok[4];
410 int rc;
411 int i;
412
413 /*
414 * Parsing the rule in-place, filling all white-spaces with '\0'
415 */
416 for (i = 0; i < tokens; ++i) {
417 while (isspace(data[cnt]))
418 data[cnt++] = '\0';
419
420 if (data[cnt] == '\0')
421 /* Unexpected end of data */
422 return -1;
423
424 tok[i] = data + cnt;
425
426 while (data[cnt] && !isspace(data[cnt]))
427 ++cnt;
428 }
429 while (isspace(data[cnt]))
430 data[cnt++] = '\0';
431
432 while (i < 4)
433 tok[i++] = NULL;
434
435 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
436 return rc == 0 ? cnt : rc;
437 }
438
439 #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
440 #define SMK_LONG_FMT 1 /* Variable long label format */
441 #define SMK_CHANGE_FMT 2 /* Rule modification format */
442 /**
443 * smk_write_rules_list - write() for any /smack rule file
444 * @file: file pointer, not actually used
445 * @buf: where to get the data from
446 * @count: bytes sent
447 * @ppos: where to start - must be 0
448 * @rule_list: the list of rules to write to
449 * @rule_lock: lock for the rule list
450 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
451 *
452 * Get one smack access rule from above.
453 * The format for SMK_LONG_FMT is:
454 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
455 * The format for SMK_FIXED24_FMT is exactly:
456 * "subject object rwxat"
457 * The format for SMK_CHANGE_FMT is:
458 * "subject<whitespace>object<whitespace>
459 * acc_enable<whitespace>acc_disable[<whitespace>...]"
460 */
461 static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
462 size_t count, loff_t *ppos,
463 struct list_head *rule_list,
464 struct mutex *rule_lock, int format)
465 {
466 struct smack_parsed_rule rule;
467 char *data;
468 int rc;
469 int trunc = 0;
470 int tokens;
471 ssize_t cnt = 0;
472
473 /*
474 * No partial writes.
475 * Enough data must be present.
476 */
477 if (*ppos != 0)
478 return -EINVAL;
479
480 if (format == SMK_FIXED24_FMT) {
481 /*
482 * Minor hack for backward compatibility
483 */
484 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
485 return -EINVAL;
486 } else {
487 if (count >= PAGE_SIZE) {
488 count = PAGE_SIZE - 1;
489 trunc = 1;
490 }
491 }
492
493 data = kmalloc(count + 1, GFP_KERNEL);
494 if (data == NULL)
495 return -ENOMEM;
496
497 if (copy_from_user(data, buf, count) != 0) {
498 rc = -EFAULT;
499 goto out;
500 }
501
502 /*
503 * In case of parsing only part of user buf,
504 * avoid having partial rule at the data buffer
505 */
506 if (trunc) {
507 while (count > 0 && (data[count - 1] != '\n'))
508 --count;
509 if (count == 0) {
510 rc = -EINVAL;
511 goto out;
512 }
513 }
514
515 data[count] = '\0';
516 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
517 while (cnt < count) {
518 if (format == SMK_FIXED24_FMT) {
519 rc = smk_parse_rule(data, &rule, 1);
520 if (rc != 0) {
521 rc = -EINVAL;
522 goto out;
523 }
524 cnt = count;
525 } else {
526 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
527 if (rc <= 0) {
528 rc = -EINVAL;
529 goto out;
530 }
531 cnt += rc;
532 }
533
534 if (rule_list == NULL)
535 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
536 &rule.smk_subject->smk_rules_lock, 1);
537 else
538 rc = smk_set_access(&rule, rule_list, rule_lock, 0);
539
540 if (rc)
541 goto out;
542 }
543
544 rc = cnt;
545 out:
546 kfree(data);
547 return rc;
548 }
549
550 /*
551 * Core logic for smackfs seq list operations.
552 */
553
554 static void *smk_seq_start(struct seq_file *s, loff_t *pos,
555 struct list_head *head)
556 {
557 struct list_head *list;
558
559 /*
560 * This is 0 the first time through.
561 */
562 if (s->index == 0)
563 s->private = head;
564
565 if (s->private == NULL)
566 return NULL;
567
568 list = s->private;
569 if (list_empty(list))
570 return NULL;
571
572 if (s->index == 0)
573 return list->next;
574 return list;
575 }
576
577 static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
578 struct list_head *head)
579 {
580 struct list_head *list = v;
581
582 if (list_is_last(list, head)) {
583 s->private = NULL;
584 return NULL;
585 }
586 s->private = list->next;
587 return list->next;
588 }
589
590 static void smk_seq_stop(struct seq_file *s, void *v)
591 {
592 /* No-op */
593 }
594
595 static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
596 {
597 /*
598 * Don't show any rules with label names too long for
599 * interface file (/smack/load or /smack/load2)
600 * because you should expect to be able to write
601 * anything you read back.
602 */
603 if (strlen(srp->smk_subject->smk_known) >= max ||
604 strlen(srp->smk_object->smk_known) >= max)
605 return;
606
607 if (srp->smk_access == 0)
608 return;
609
610 seq_printf(s, "%s %s",
611 srp->smk_subject->smk_known,
612 srp->smk_object->smk_known);
613
614 seq_putc(s, ' ');
615
616 if (srp->smk_access & MAY_READ)
617 seq_putc(s, 'r');
618 if (srp->smk_access & MAY_WRITE)
619 seq_putc(s, 'w');
620 if (srp->smk_access & MAY_EXEC)
621 seq_putc(s, 'x');
622 if (srp->smk_access & MAY_APPEND)
623 seq_putc(s, 'a');
624 if (srp->smk_access & MAY_TRANSMUTE)
625 seq_putc(s, 't');
626 if (srp->smk_access & MAY_LOCK)
627 seq_putc(s, 'l');
628 if (srp->smk_access & MAY_BRINGUP)
629 seq_putc(s, 'b');
630
631 seq_putc(s, '\n');
632 }
633
634 /*
635 * Seq_file read operations for /smack/load
636 */
637
638 static void *load2_seq_start(struct seq_file *s, loff_t *pos)
639 {
640 return smk_seq_start(s, pos, &smack_rule_list);
641 }
642
643 static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
644 {
645 return smk_seq_next(s, v, pos, &smack_rule_list);
646 }
647
648 static int load_seq_show(struct seq_file *s, void *v)
649 {
650 struct list_head *list = v;
651 struct smack_master_list *smlp =
652 list_entry(list, struct smack_master_list, list);
653
654 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
655
656 return 0;
657 }
658
659 static const struct seq_operations load_seq_ops = {
660 .start = load2_seq_start,
661 .next = load2_seq_next,
662 .show = load_seq_show,
663 .stop = smk_seq_stop,
664 };
665
666 /**
667 * smk_open_load - open() for /smack/load
668 * @inode: inode structure representing file
669 * @file: "load" file pointer
670 *
671 * For reading, use load_seq_* seq_file reading operations.
672 */
673 static int smk_open_load(struct inode *inode, struct file *file)
674 {
675 return seq_open(file, &load_seq_ops);
676 }
677
678 /**
679 * smk_write_load - write() for /smack/load
680 * @file: file pointer, not actually used
681 * @buf: where to get the data from
682 * @count: bytes sent
683 * @ppos: where to start - must be 0
684 *
685 */
686 static ssize_t smk_write_load(struct file *file, const char __user *buf,
687 size_t count, loff_t *ppos)
688 {
689 /*
690 * Must have privilege.
691 * No partial writes.
692 * Enough data must be present.
693 */
694 if (!smack_privileged(CAP_MAC_ADMIN))
695 return -EPERM;
696
697 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
698 SMK_FIXED24_FMT);
699 }
700
701 static const struct file_operations smk_load_ops = {
702 .open = smk_open_load,
703 .read = seq_read,
704 .llseek = seq_lseek,
705 .write = smk_write_load,
706 .release = seq_release,
707 };
708
709 /**
710 * smk_cipso_doi - initialize the CIPSO domain
711 */
712 static void smk_cipso_doi(void)
713 {
714 int rc;
715 struct cipso_v4_doi *doip;
716 struct netlbl_audit nai;
717
718 smk_netlabel_audit_set(&nai);
719
720 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
721 if (rc != 0)
722 printk(KERN_WARNING "%s:%d remove rc = %d\n",
723 __func__, __LINE__, rc);
724
725 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
726 if (doip == NULL)
727 panic("smack: Failed to initialize cipso DOI.\n");
728 doip->map.std = NULL;
729 doip->doi = smk_cipso_doi_value;
730 doip->type = CIPSO_V4_MAP_PASS;
731 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
732 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
733 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
734
735 rc = netlbl_cfg_cipsov4_add(doip, &nai);
736 if (rc != 0) {
737 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
738 __func__, __LINE__, rc);
739 kfree(doip);
740 return;
741 }
742 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
743 if (rc != 0) {
744 printk(KERN_WARNING "%s:%d map add rc = %d\n",
745 __func__, __LINE__, rc);
746 kfree(doip);
747 return;
748 }
749 }
750
751 /**
752 * smk_unlbl_ambient - initialize the unlabeled domain
753 * @oldambient: previous domain string
754 */
755 static void smk_unlbl_ambient(char *oldambient)
756 {
757 int rc;
758 struct netlbl_audit nai;
759
760 smk_netlabel_audit_set(&nai);
761
762 if (oldambient != NULL) {
763 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
764 if (rc != 0)
765 printk(KERN_WARNING "%s:%d remove rc = %d\n",
766 __func__, __LINE__, rc);
767 }
768 if (smack_net_ambient == NULL)
769 smack_net_ambient = &smack_known_floor;
770
771 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
772 NULL, NULL, &nai);
773 if (rc != 0)
774 printk(KERN_WARNING "%s:%d add rc = %d\n",
775 __func__, __LINE__, rc);
776 }
777
778 /*
779 * Seq_file read operations for /smack/cipso
780 */
781
782 static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
783 {
784 return smk_seq_start(s, pos, &smack_known_list);
785 }
786
787 static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
788 {
789 return smk_seq_next(s, v, pos, &smack_known_list);
790 }
791
792 /*
793 * Print cipso labels in format:
794 * label level[/cat[,cat]]
795 */
796 static int cipso_seq_show(struct seq_file *s, void *v)
797 {
798 struct list_head *list = v;
799 struct smack_known *skp =
800 list_entry(list, struct smack_known, list);
801 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
802 char sep = '/';
803 int i;
804
805 /*
806 * Don't show a label that could not have been set using
807 * /smack/cipso. This is in support of the notion that
808 * anything read from /smack/cipso ought to be writeable
809 * to /smack/cipso.
810 *
811 * /smack/cipso2 should be used instead.
812 */
813 if (strlen(skp->smk_known) >= SMK_LABELLEN)
814 return 0;
815
816 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
817
818 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
819 i = netlbl_catmap_walk(cmp, i + 1)) {
820 seq_printf(s, "%c%d", sep, i);
821 sep = ',';
822 }
823
824 seq_putc(s, '\n');
825
826 return 0;
827 }
828
829 static const struct seq_operations cipso_seq_ops = {
830 .start = cipso_seq_start,
831 .next = cipso_seq_next,
832 .show = cipso_seq_show,
833 .stop = smk_seq_stop,
834 };
835
836 /**
837 * smk_open_cipso - open() for /smack/cipso
838 * @inode: inode structure representing file
839 * @file: "cipso" file pointer
840 *
841 * Connect our cipso_seq_* operations with /smack/cipso
842 * file_operations
843 */
844 static int smk_open_cipso(struct inode *inode, struct file *file)
845 {
846 return seq_open(file, &cipso_seq_ops);
847 }
848
849 /**
850 * smk_set_cipso - do the work for write() for cipso and cipso2
851 * @file: file pointer, not actually used
852 * @buf: where to get the data from
853 * @count: bytes sent
854 * @ppos: where to start
855 * @format: /smack/cipso or /smack/cipso2
856 *
857 * Accepts only one cipso rule per write call.
858 * Returns number of bytes written or error code, as appropriate
859 */
860 static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
861 size_t count, loff_t *ppos, int format)
862 {
863 struct smack_known *skp;
864 struct netlbl_lsm_secattr ncats;
865 char mapcatset[SMK_CIPSOLEN];
866 int maplevel;
867 unsigned int cat;
868 int catlen;
869 ssize_t rc = -EINVAL;
870 char *data = NULL;
871 char *rule;
872 int ret;
873 int i;
874
875 /*
876 * Must have privilege.
877 * No partial writes.
878 * Enough data must be present.
879 */
880 if (!smack_privileged(CAP_MAC_ADMIN))
881 return -EPERM;
882 if (*ppos != 0)
883 return -EINVAL;
884 if (format == SMK_FIXED24_FMT &&
885 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
886 return -EINVAL;
887
888 data = kzalloc(count + 1, GFP_KERNEL);
889 if (data == NULL)
890 return -ENOMEM;
891
892 if (copy_from_user(data, buf, count) != 0) {
893 rc = -EFAULT;
894 goto unlockedout;
895 }
896
897 data[count] = '\0';
898 rule = data;
899 /*
900 * Only allow one writer at a time. Writes should be
901 * quite rare and small in any case.
902 */
903 mutex_lock(&smack_cipso_lock);
904
905 skp = smk_import_entry(rule, 0);
906 if (skp == NULL)
907 goto out;
908
909 if (format == SMK_FIXED24_FMT)
910 rule += SMK_LABELLEN;
911 else
912 rule += strlen(skp->smk_known) + 1;
913
914 ret = sscanf(rule, "%d", &maplevel);
915 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
916 goto out;
917
918 rule += SMK_DIGITLEN;
919 ret = sscanf(rule, "%d", &catlen);
920 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
921 goto out;
922
923 if (format == SMK_FIXED24_FMT &&
924 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
925 goto out;
926
927 memset(mapcatset, 0, sizeof(mapcatset));
928
929 for (i = 0; i < catlen; i++) {
930 rule += SMK_DIGITLEN;
931 ret = sscanf(rule, "%u", &cat);
932 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
933 goto out;
934
935 smack_catset_bit(cat, mapcatset);
936 }
937
938 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
939 if (rc >= 0) {
940 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
941 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
942 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
943 rc = count;
944 }
945
946 out:
947 mutex_unlock(&smack_cipso_lock);
948 unlockedout:
949 kfree(data);
950 return rc;
951 }
952
953 /**
954 * smk_write_cipso - write() for /smack/cipso
955 * @file: file pointer, not actually used
956 * @buf: where to get the data from
957 * @count: bytes sent
958 * @ppos: where to start
959 *
960 * Accepts only one cipso rule per write call.
961 * Returns number of bytes written or error code, as appropriate
962 */
963 static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
964 size_t count, loff_t *ppos)
965 {
966 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
967 }
968
969 static const struct file_operations smk_cipso_ops = {
970 .open = smk_open_cipso,
971 .read = seq_read,
972 .llseek = seq_lseek,
973 .write = smk_write_cipso,
974 .release = seq_release,
975 };
976
977 /*
978 * Seq_file read operations for /smack/cipso2
979 */
980
981 /*
982 * Print cipso labels in format:
983 * label level[/cat[,cat]]
984 */
985 static int cipso2_seq_show(struct seq_file *s, void *v)
986 {
987 struct list_head *list = v;
988 struct smack_known *skp =
989 list_entry(list, struct smack_known, list);
990 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
991 char sep = '/';
992 int i;
993
994 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
995
996 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
997 i = netlbl_catmap_walk(cmp, i + 1)) {
998 seq_printf(s, "%c%d", sep, i);
999 sep = ',';
1000 }
1001
1002 seq_putc(s, '\n');
1003
1004 return 0;
1005 }
1006
1007 static const struct seq_operations cipso2_seq_ops = {
1008 .start = cipso_seq_start,
1009 .next = cipso_seq_next,
1010 .show = cipso2_seq_show,
1011 .stop = smk_seq_stop,
1012 };
1013
1014 /**
1015 * smk_open_cipso2 - open() for /smack/cipso2
1016 * @inode: inode structure representing file
1017 * @file: "cipso2" file pointer
1018 *
1019 * Connect our cipso_seq_* operations with /smack/cipso2
1020 * file_operations
1021 */
1022 static int smk_open_cipso2(struct inode *inode, struct file *file)
1023 {
1024 return seq_open(file, &cipso2_seq_ops);
1025 }
1026
1027 /**
1028 * smk_write_cipso2 - write() for /smack/cipso2
1029 * @file: file pointer, not actually used
1030 * @buf: where to get the data from
1031 * @count: bytes sent
1032 * @ppos: where to start
1033 *
1034 * Accepts only one cipso rule per write call.
1035 * Returns number of bytes written or error code, as appropriate
1036 */
1037 static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1038 size_t count, loff_t *ppos)
1039 {
1040 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1041 }
1042
1043 static const struct file_operations smk_cipso2_ops = {
1044 .open = smk_open_cipso2,
1045 .read = seq_read,
1046 .llseek = seq_lseek,
1047 .write = smk_write_cipso2,
1048 .release = seq_release,
1049 };
1050
1051 /*
1052 * Seq_file read operations for /smack/netlabel
1053 */
1054
1055 static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1056 {
1057 return smk_seq_start(s, pos, &smk_netlbladdr_list);
1058 }
1059
1060 static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1061 {
1062 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
1063 }
1064 #define BEBITS (sizeof(__be32) * 8)
1065
1066 /*
1067 * Print host/label pairs
1068 */
1069 static int netlbladdr_seq_show(struct seq_file *s, void *v)
1070 {
1071 struct list_head *list = v;
1072 struct smk_netlbladdr *skp =
1073 list_entry(list, struct smk_netlbladdr, list);
1074 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
1075 int maskn;
1076 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
1077
1078 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
1079
1080 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
1081 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label->smk_known);
1082
1083 return 0;
1084 }
1085
1086 static const struct seq_operations netlbladdr_seq_ops = {
1087 .start = netlbladdr_seq_start,
1088 .next = netlbladdr_seq_next,
1089 .show = netlbladdr_seq_show,
1090 .stop = smk_seq_stop,
1091 };
1092
1093 /**
1094 * smk_open_netlbladdr - open() for /smack/netlabel
1095 * @inode: inode structure representing file
1096 * @file: "netlabel" file pointer
1097 *
1098 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1099 * file_operations
1100 */
1101 static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1102 {
1103 return seq_open(file, &netlbladdr_seq_ops);
1104 }
1105
1106 /**
1107 * smk_netlbladdr_insert
1108 * @new : netlabel to insert
1109 *
1110 * This helper insert netlabel in the smack_netlbladdrs list
1111 * sorted by netmask length (longest to smallest)
1112 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1113 *
1114 */
1115 static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1116 {
1117 struct smk_netlbladdr *m, *m_next;
1118
1119 if (list_empty(&smk_netlbladdr_list)) {
1120 list_add_rcu(&new->list, &smk_netlbladdr_list);
1121 return;
1122 }
1123
1124 m = list_entry_rcu(smk_netlbladdr_list.next,
1125 struct smk_netlbladdr, list);
1126
1127 /* the comparison '>' is a bit hacky, but works */
1128 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1129 list_add_rcu(&new->list, &smk_netlbladdr_list);
1130 return;
1131 }
1132
1133 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1134 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1135 list_add_rcu(&new->list, &m->list);
1136 return;
1137 }
1138 m_next = list_entry_rcu(m->list.next,
1139 struct smk_netlbladdr, list);
1140 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1141 list_add_rcu(&new->list, &m->list);
1142 return;
1143 }
1144 }
1145 }
1146
1147
1148 /**
1149 * smk_write_netlbladdr - write() for /smack/netlabel
1150 * @file: file pointer, not actually used
1151 * @buf: where to get the data from
1152 * @count: bytes sent
1153 * @ppos: where to start
1154 *
1155 * Accepts only one netlbladdr per write call.
1156 * Returns number of bytes written or error code, as appropriate
1157 */
1158 static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1159 size_t count, loff_t *ppos)
1160 {
1161 struct smk_netlbladdr *snp;
1162 struct sockaddr_in newname;
1163 char *smack;
1164 struct smack_known *skp;
1165 char *data;
1166 char *host = (char *)&newname.sin_addr.s_addr;
1167 int rc;
1168 struct netlbl_audit audit_info;
1169 struct in_addr mask;
1170 unsigned int m;
1171 int found;
1172 u32 mask_bits = (1<<31);
1173 __be32 nsa;
1174 u32 temp_mask;
1175
1176 /*
1177 * Must have privilege.
1178 * No partial writes.
1179 * Enough data must be present.
1180 * "<addr/mask, as a.b.c.d/e><space><label>"
1181 * "<addr, as a.b.c.d><space><label>"
1182 */
1183 if (!smack_privileged(CAP_MAC_ADMIN))
1184 return -EPERM;
1185 if (*ppos != 0)
1186 return -EINVAL;
1187 if (count < SMK_NETLBLADDRMIN)
1188 return -EINVAL;
1189
1190 data = kzalloc(count + 1, GFP_KERNEL);
1191 if (data == NULL)
1192 return -ENOMEM;
1193
1194 if (copy_from_user(data, buf, count) != 0) {
1195 rc = -EFAULT;
1196 goto free_data_out;
1197 }
1198
1199 smack = kzalloc(count + 1, GFP_KERNEL);
1200 if (smack == NULL) {
1201 rc = -ENOMEM;
1202 goto free_data_out;
1203 }
1204
1205 data[count] = '\0';
1206
1207 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
1208 &host[0], &host[1], &host[2], &host[3], &m, smack);
1209 if (rc != 6) {
1210 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1211 &host[0], &host[1], &host[2], &host[3], smack);
1212 if (rc != 5) {
1213 rc = -EINVAL;
1214 goto free_out;
1215 }
1216 m = BEBITS;
1217 }
1218 if (m > BEBITS) {
1219 rc = -EINVAL;
1220 goto free_out;
1221 }
1222
1223 /*
1224 * If smack begins with '-', it is an option, don't import it
1225 */
1226 if (smack[0] != '-') {
1227 skp = smk_import_entry(smack, 0);
1228 if (skp == NULL) {
1229 rc = -EINVAL;
1230 goto free_out;
1231 }
1232 } else {
1233 /* check known options */
1234 if (strcmp(smack, smack_cipso_option.smk_known) == 0)
1235 skp = &smack_cipso_option;
1236 else {
1237 rc = -EINVAL;
1238 goto free_out;
1239 }
1240 }
1241
1242 for (temp_mask = 0; m > 0; m--) {
1243 temp_mask |= mask_bits;
1244 mask_bits >>= 1;
1245 }
1246 mask.s_addr = cpu_to_be32(temp_mask);
1247
1248 newname.sin_addr.s_addr &= mask.s_addr;
1249 /*
1250 * Only allow one writer at a time. Writes should be
1251 * quite rare and small in any case.
1252 */
1253 mutex_lock(&smk_netlbladdr_lock);
1254
1255 nsa = newname.sin_addr.s_addr;
1256 /* try to find if the prefix is already in the list */
1257 found = 0;
1258 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list) {
1259 if (snp->smk_host.sin_addr.s_addr == nsa &&
1260 snp->smk_mask.s_addr == mask.s_addr) {
1261 found = 1;
1262 break;
1263 }
1264 }
1265 smk_netlabel_audit_set(&audit_info);
1266
1267 if (found == 0) {
1268 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1269 if (snp == NULL)
1270 rc = -ENOMEM;
1271 else {
1272 rc = 0;
1273 snp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1274 snp->smk_mask.s_addr = mask.s_addr;
1275 snp->smk_label = skp;
1276 smk_netlbladdr_insert(snp);
1277 }
1278 } else {
1279 /* we delete the unlabeled entry, only if the previous label
1280 * wasn't the special CIPSO option */
1281 if (snp->smk_label != &smack_cipso_option)
1282 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1283 &snp->smk_host.sin_addr, &snp->smk_mask,
1284 PF_INET, &audit_info);
1285 else
1286 rc = 0;
1287 snp->smk_label = skp;
1288 }
1289
1290 /*
1291 * Now tell netlabel about the single label nature of
1292 * this host so that incoming packets get labeled.
1293 * but only if we didn't get the special CIPSO option
1294 */
1295 if (rc == 0 && skp != &smack_cipso_option)
1296 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1297 &snp->smk_host.sin_addr, &snp->smk_mask, PF_INET,
1298 snp->smk_label->smk_secid, &audit_info);
1299
1300 if (rc == 0)
1301 rc = count;
1302
1303 mutex_unlock(&smk_netlbladdr_lock);
1304
1305 free_out:
1306 kfree(smack);
1307 free_data_out:
1308 kfree(data);
1309
1310 return rc;
1311 }
1312
1313 static const struct file_operations smk_netlbladdr_ops = {
1314 .open = smk_open_netlbladdr,
1315 .read = seq_read,
1316 .llseek = seq_lseek,
1317 .write = smk_write_netlbladdr,
1318 .release = seq_release,
1319 };
1320
1321 /**
1322 * smk_read_doi - read() for /smack/doi
1323 * @filp: file pointer, not actually used
1324 * @buf: where to put the result
1325 * @count: maximum to send along
1326 * @ppos: where to start
1327 *
1328 * Returns number of bytes read or error code, as appropriate
1329 */
1330 static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1331 size_t count, loff_t *ppos)
1332 {
1333 char temp[80];
1334 ssize_t rc;
1335
1336 if (*ppos != 0)
1337 return 0;
1338
1339 sprintf(temp, "%d", smk_cipso_doi_value);
1340 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1341
1342 return rc;
1343 }
1344
1345 /**
1346 * smk_write_doi - write() for /smack/doi
1347 * @file: file pointer, not actually used
1348 * @buf: where to get the data from
1349 * @count: bytes sent
1350 * @ppos: where to start
1351 *
1352 * Returns number of bytes written or error code, as appropriate
1353 */
1354 static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1355 size_t count, loff_t *ppos)
1356 {
1357 char temp[80];
1358 int i;
1359
1360 if (!smack_privileged(CAP_MAC_ADMIN))
1361 return -EPERM;
1362
1363 if (count >= sizeof(temp) || count == 0)
1364 return -EINVAL;
1365
1366 if (copy_from_user(temp, buf, count) != 0)
1367 return -EFAULT;
1368
1369 temp[count] = '\0';
1370
1371 if (sscanf(temp, "%d", &i) != 1)
1372 return -EINVAL;
1373
1374 smk_cipso_doi_value = i;
1375
1376 smk_cipso_doi();
1377
1378 return count;
1379 }
1380
1381 static const struct file_operations smk_doi_ops = {
1382 .read = smk_read_doi,
1383 .write = smk_write_doi,
1384 .llseek = default_llseek,
1385 };
1386
1387 /**
1388 * smk_read_direct - read() for /smack/direct
1389 * @filp: file pointer, not actually used
1390 * @buf: where to put the result
1391 * @count: maximum to send along
1392 * @ppos: where to start
1393 *
1394 * Returns number of bytes read or error code, as appropriate
1395 */
1396 static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1397 size_t count, loff_t *ppos)
1398 {
1399 char temp[80];
1400 ssize_t rc;
1401
1402 if (*ppos != 0)
1403 return 0;
1404
1405 sprintf(temp, "%d", smack_cipso_direct);
1406 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1407
1408 return rc;
1409 }
1410
1411 /**
1412 * smk_write_direct - write() for /smack/direct
1413 * @file: file pointer, not actually used
1414 * @buf: where to get the data from
1415 * @count: bytes sent
1416 * @ppos: where to start
1417 *
1418 * Returns number of bytes written or error code, as appropriate
1419 */
1420 static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1421 size_t count, loff_t *ppos)
1422 {
1423 struct smack_known *skp;
1424 char temp[80];
1425 int i;
1426
1427 if (!smack_privileged(CAP_MAC_ADMIN))
1428 return -EPERM;
1429
1430 if (count >= sizeof(temp) || count == 0)
1431 return -EINVAL;
1432
1433 if (copy_from_user(temp, buf, count) != 0)
1434 return -EFAULT;
1435
1436 temp[count] = '\0';
1437
1438 if (sscanf(temp, "%d", &i) != 1)
1439 return -EINVAL;
1440
1441 /*
1442 * Don't do anything if the value hasn't actually changed.
1443 * If it is changing reset the level on entries that were
1444 * set up to be direct when they were created.
1445 */
1446 if (smack_cipso_direct != i) {
1447 mutex_lock(&smack_known_lock);
1448 list_for_each_entry_rcu(skp, &smack_known_list, list)
1449 if (skp->smk_netlabel.attr.mls.lvl ==
1450 smack_cipso_direct)
1451 skp->smk_netlabel.attr.mls.lvl = i;
1452 smack_cipso_direct = i;
1453 mutex_unlock(&smack_known_lock);
1454 }
1455
1456 return count;
1457 }
1458
1459 static const struct file_operations smk_direct_ops = {
1460 .read = smk_read_direct,
1461 .write = smk_write_direct,
1462 .llseek = default_llseek,
1463 };
1464
1465 /**
1466 * smk_read_mapped - read() for /smack/mapped
1467 * @filp: file pointer, not actually used
1468 * @buf: where to put the result
1469 * @count: maximum to send along
1470 * @ppos: where to start
1471 *
1472 * Returns number of bytes read or error code, as appropriate
1473 */
1474 static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1475 size_t count, loff_t *ppos)
1476 {
1477 char temp[80];
1478 ssize_t rc;
1479
1480 if (*ppos != 0)
1481 return 0;
1482
1483 sprintf(temp, "%d", smack_cipso_mapped);
1484 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1485
1486 return rc;
1487 }
1488
1489 /**
1490 * smk_write_mapped - write() for /smack/mapped
1491 * @file: file pointer, not actually used
1492 * @buf: where to get the data from
1493 * @count: bytes sent
1494 * @ppos: where to start
1495 *
1496 * Returns number of bytes written or error code, as appropriate
1497 */
1498 static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1499 size_t count, loff_t *ppos)
1500 {
1501 struct smack_known *skp;
1502 char temp[80];
1503 int i;
1504
1505 if (!smack_privileged(CAP_MAC_ADMIN))
1506 return -EPERM;
1507
1508 if (count >= sizeof(temp) || count == 0)
1509 return -EINVAL;
1510
1511 if (copy_from_user(temp, buf, count) != 0)
1512 return -EFAULT;
1513
1514 temp[count] = '\0';
1515
1516 if (sscanf(temp, "%d", &i) != 1)
1517 return -EINVAL;
1518
1519 /*
1520 * Don't do anything if the value hasn't actually changed.
1521 * If it is changing reset the level on entries that were
1522 * set up to be mapped when they were created.
1523 */
1524 if (smack_cipso_mapped != i) {
1525 mutex_lock(&smack_known_lock);
1526 list_for_each_entry_rcu(skp, &smack_known_list, list)
1527 if (skp->smk_netlabel.attr.mls.lvl ==
1528 smack_cipso_mapped)
1529 skp->smk_netlabel.attr.mls.lvl = i;
1530 smack_cipso_mapped = i;
1531 mutex_unlock(&smack_known_lock);
1532 }
1533
1534 return count;
1535 }
1536
1537 static const struct file_operations smk_mapped_ops = {
1538 .read = smk_read_mapped,
1539 .write = smk_write_mapped,
1540 .llseek = default_llseek,
1541 };
1542
1543 /**
1544 * smk_read_ambient - read() for /smack/ambient
1545 * @filp: file pointer, not actually used
1546 * @buf: where to put the result
1547 * @cn: maximum to send along
1548 * @ppos: where to start
1549 *
1550 * Returns number of bytes read or error code, as appropriate
1551 */
1552 static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1553 size_t cn, loff_t *ppos)
1554 {
1555 ssize_t rc;
1556 int asize;
1557
1558 if (*ppos != 0)
1559 return 0;
1560 /*
1561 * Being careful to avoid a problem in the case where
1562 * smack_net_ambient gets changed in midstream.
1563 */
1564 mutex_lock(&smack_ambient_lock);
1565
1566 asize = strlen(smack_net_ambient->smk_known) + 1;
1567
1568 if (cn >= asize)
1569 rc = simple_read_from_buffer(buf, cn, ppos,
1570 smack_net_ambient->smk_known,
1571 asize);
1572 else
1573 rc = -EINVAL;
1574
1575 mutex_unlock(&smack_ambient_lock);
1576
1577 return rc;
1578 }
1579
1580 /**
1581 * smk_write_ambient - write() for /smack/ambient
1582 * @file: file pointer, not actually used
1583 * @buf: where to get the data from
1584 * @count: bytes sent
1585 * @ppos: where to start
1586 *
1587 * Returns number of bytes written or error code, as appropriate
1588 */
1589 static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1590 size_t count, loff_t *ppos)
1591 {
1592 struct smack_known *skp;
1593 char *oldambient;
1594 char *data;
1595 int rc = count;
1596
1597 if (!smack_privileged(CAP_MAC_ADMIN))
1598 return -EPERM;
1599
1600 data = kzalloc(count + 1, GFP_KERNEL);
1601 if (data == NULL)
1602 return -ENOMEM;
1603
1604 if (copy_from_user(data, buf, count) != 0) {
1605 rc = -EFAULT;
1606 goto out;
1607 }
1608
1609 skp = smk_import_entry(data, count);
1610 if (skp == NULL) {
1611 rc = -EINVAL;
1612 goto out;
1613 }
1614
1615 mutex_lock(&smack_ambient_lock);
1616
1617 oldambient = smack_net_ambient->smk_known;
1618 smack_net_ambient = skp;
1619 smk_unlbl_ambient(oldambient);
1620
1621 mutex_unlock(&smack_ambient_lock);
1622
1623 out:
1624 kfree(data);
1625 return rc;
1626 }
1627
1628 static const struct file_operations smk_ambient_ops = {
1629 .read = smk_read_ambient,
1630 .write = smk_write_ambient,
1631 .llseek = default_llseek,
1632 };
1633
1634 /**
1635 * smk_read_onlycap - read() for smackfs/onlycap
1636 * @filp: file pointer, not actually used
1637 * @buf: where to put the result
1638 * @cn: maximum to send along
1639 * @ppos: where to start
1640 *
1641 * Returns number of bytes read or error code, as appropriate
1642 */
1643 static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1644 size_t cn, loff_t *ppos)
1645 {
1646 char *smack = "";
1647 ssize_t rc = -EINVAL;
1648 int asize;
1649
1650 if (*ppos != 0)
1651 return 0;
1652
1653 if (smack_onlycap != NULL)
1654 smack = smack_onlycap->smk_known;
1655
1656 asize = strlen(smack) + 1;
1657
1658 if (cn >= asize)
1659 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1660
1661 return rc;
1662 }
1663
1664 /**
1665 * smk_write_onlycap - write() for smackfs/onlycap
1666 * @file: file pointer, not actually used
1667 * @buf: where to get the data from
1668 * @count: bytes sent
1669 * @ppos: where to start
1670 *
1671 * Returns number of bytes written or error code, as appropriate
1672 */
1673 static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1674 size_t count, loff_t *ppos)
1675 {
1676 char *data;
1677 struct smack_known *skp = smk_of_task(current->cred->security);
1678 int rc = count;
1679
1680 if (!smack_privileged(CAP_MAC_ADMIN))
1681 return -EPERM;
1682
1683 /*
1684 * This can be done using smk_access() but is done
1685 * explicitly for clarity. The smk_access() implementation
1686 * would use smk_access(smack_onlycap, MAY_WRITE)
1687 */
1688 if (smack_onlycap != NULL && smack_onlycap != skp)
1689 return -EPERM;
1690
1691 data = kzalloc(count + 1, GFP_KERNEL);
1692 if (data == NULL)
1693 return -ENOMEM;
1694
1695 /*
1696 * Should the null string be passed in unset the onlycap value.
1697 * This seems like something to be careful with as usually
1698 * smk_import only expects to return NULL for errors. It
1699 * is usually the case that a nullstring or "\n" would be
1700 * bad to pass to smk_import but in fact this is useful here.
1701 *
1702 * smk_import will also reject a label beginning with '-',
1703 * so "-usecapabilities" will also work.
1704 */
1705 if (copy_from_user(data, buf, count) != 0)
1706 rc = -EFAULT;
1707 else
1708 smack_onlycap = smk_import_entry(data, count);
1709
1710 kfree(data);
1711 return rc;
1712 }
1713
1714 static const struct file_operations smk_onlycap_ops = {
1715 .read = smk_read_onlycap,
1716 .write = smk_write_onlycap,
1717 .llseek = default_llseek,
1718 };
1719
1720 /**
1721 * smk_read_logging - read() for /smack/logging
1722 * @filp: file pointer, not actually used
1723 * @buf: where to put the result
1724 * @cn: maximum to send along
1725 * @ppos: where to start
1726 *
1727 * Returns number of bytes read or error code, as appropriate
1728 */
1729 static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1730 size_t count, loff_t *ppos)
1731 {
1732 char temp[32];
1733 ssize_t rc;
1734
1735 if (*ppos != 0)
1736 return 0;
1737
1738 sprintf(temp, "%d\n", log_policy);
1739 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1740 return rc;
1741 }
1742
1743 /**
1744 * smk_write_logging - write() for /smack/logging
1745 * @file: file pointer, not actually used
1746 * @buf: where to get the data from
1747 * @count: bytes sent
1748 * @ppos: where to start
1749 *
1750 * Returns number of bytes written or error code, as appropriate
1751 */
1752 static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1753 size_t count, loff_t *ppos)
1754 {
1755 char temp[32];
1756 int i;
1757
1758 if (!smack_privileged(CAP_MAC_ADMIN))
1759 return -EPERM;
1760
1761 if (count >= sizeof(temp) || count == 0)
1762 return -EINVAL;
1763
1764 if (copy_from_user(temp, buf, count) != 0)
1765 return -EFAULT;
1766
1767 temp[count] = '\0';
1768
1769 if (sscanf(temp, "%d", &i) != 1)
1770 return -EINVAL;
1771 if (i < 0 || i > 3)
1772 return -EINVAL;
1773 log_policy = i;
1774 return count;
1775 }
1776
1777
1778
1779 static const struct file_operations smk_logging_ops = {
1780 .read = smk_read_logging,
1781 .write = smk_write_logging,
1782 .llseek = default_llseek,
1783 };
1784
1785 /*
1786 * Seq_file read operations for /smack/load-self
1787 */
1788
1789 static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1790 {
1791 struct task_smack *tsp = current_security();
1792
1793 return smk_seq_start(s, pos, &tsp->smk_rules);
1794 }
1795
1796 static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1797 {
1798 struct task_smack *tsp = current_security();
1799
1800 return smk_seq_next(s, v, pos, &tsp->smk_rules);
1801 }
1802
1803 static int load_self_seq_show(struct seq_file *s, void *v)
1804 {
1805 struct list_head *list = v;
1806 struct smack_rule *srp =
1807 list_entry(list, struct smack_rule, list);
1808
1809 smk_rule_show(s, srp, SMK_LABELLEN);
1810
1811 return 0;
1812 }
1813
1814 static const struct seq_operations load_self_seq_ops = {
1815 .start = load_self_seq_start,
1816 .next = load_self_seq_next,
1817 .show = load_self_seq_show,
1818 .stop = smk_seq_stop,
1819 };
1820
1821
1822 /**
1823 * smk_open_load_self - open() for /smack/load-self2
1824 * @inode: inode structure representing file
1825 * @file: "load" file pointer
1826 *
1827 * For reading, use load_seq_* seq_file reading operations.
1828 */
1829 static int smk_open_load_self(struct inode *inode, struct file *file)
1830 {
1831 return seq_open(file, &load_self_seq_ops);
1832 }
1833
1834 /**
1835 * smk_write_load_self - write() for /smack/load-self
1836 * @file: file pointer, not actually used
1837 * @buf: where to get the data from
1838 * @count: bytes sent
1839 * @ppos: where to start - must be 0
1840 *
1841 */
1842 static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1843 size_t count, loff_t *ppos)
1844 {
1845 struct task_smack *tsp = current_security();
1846
1847 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1848 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
1849 }
1850
1851 static const struct file_operations smk_load_self_ops = {
1852 .open = smk_open_load_self,
1853 .read = seq_read,
1854 .llseek = seq_lseek,
1855 .write = smk_write_load_self,
1856 .release = seq_release,
1857 };
1858
1859 /**
1860 * smk_user_access - handle access check transaction
1861 * @file: file pointer
1862 * @buf: data from user space
1863 * @count: bytes sent
1864 * @ppos: where to start - must be 0
1865 */
1866 static ssize_t smk_user_access(struct file *file, const char __user *buf,
1867 size_t count, loff_t *ppos, int format)
1868 {
1869 struct smack_parsed_rule rule;
1870 char *data;
1871 int res;
1872
1873 data = simple_transaction_get(file, buf, count);
1874 if (IS_ERR(data))
1875 return PTR_ERR(data);
1876
1877 if (format == SMK_FIXED24_FMT) {
1878 if (count < SMK_LOADLEN)
1879 return -EINVAL;
1880 res = smk_parse_rule(data, &rule, 0);
1881 } else {
1882 /*
1883 * simple_transaction_get() returns null-terminated data
1884 */
1885 res = smk_parse_long_rule(data, &rule, 0, 3);
1886 }
1887
1888 if (res >= 0)
1889 res = smk_access(rule.smk_subject, rule.smk_object,
1890 rule.smk_access1, NULL);
1891 else if (res != -ENOENT)
1892 return -EINVAL;
1893
1894 /*
1895 * smk_access() can return a value > 0 in the "bringup" case.
1896 */
1897 data[0] = res >= 0 ? '1' : '0';
1898 data[1] = '\0';
1899
1900 simple_transaction_set(file, 2);
1901
1902 if (format == SMK_FIXED24_FMT)
1903 return SMK_LOADLEN;
1904 return count;
1905 }
1906
1907 /**
1908 * smk_write_access - handle access check transaction
1909 * @file: file pointer
1910 * @buf: data from user space
1911 * @count: bytes sent
1912 * @ppos: where to start - must be 0
1913 */
1914 static ssize_t smk_write_access(struct file *file, const char __user *buf,
1915 size_t count, loff_t *ppos)
1916 {
1917 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
1918 }
1919
1920 static const struct file_operations smk_access_ops = {
1921 .write = smk_write_access,
1922 .read = simple_transaction_read,
1923 .release = simple_transaction_release,
1924 .llseek = generic_file_llseek,
1925 };
1926
1927
1928 /*
1929 * Seq_file read operations for /smack/load2
1930 */
1931
1932 static int load2_seq_show(struct seq_file *s, void *v)
1933 {
1934 struct list_head *list = v;
1935 struct smack_master_list *smlp =
1936 list_entry(list, struct smack_master_list, list);
1937
1938 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
1939
1940 return 0;
1941 }
1942
1943 static const struct seq_operations load2_seq_ops = {
1944 .start = load2_seq_start,
1945 .next = load2_seq_next,
1946 .show = load2_seq_show,
1947 .stop = smk_seq_stop,
1948 };
1949
1950 /**
1951 * smk_open_load2 - open() for /smack/load2
1952 * @inode: inode structure representing file
1953 * @file: "load2" file pointer
1954 *
1955 * For reading, use load2_seq_* seq_file reading operations.
1956 */
1957 static int smk_open_load2(struct inode *inode, struct file *file)
1958 {
1959 return seq_open(file, &load2_seq_ops);
1960 }
1961
1962 /**
1963 * smk_write_load2 - write() for /smack/load2
1964 * @file: file pointer, not actually used
1965 * @buf: where to get the data from
1966 * @count: bytes sent
1967 * @ppos: where to start - must be 0
1968 *
1969 */
1970 static ssize_t smk_write_load2(struct file *file, const char __user *buf,
1971 size_t count, loff_t *ppos)
1972 {
1973 /*
1974 * Must have privilege.
1975 */
1976 if (!smack_privileged(CAP_MAC_ADMIN))
1977 return -EPERM;
1978
1979 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
1980 SMK_LONG_FMT);
1981 }
1982
1983 static const struct file_operations smk_load2_ops = {
1984 .open = smk_open_load2,
1985 .read = seq_read,
1986 .llseek = seq_lseek,
1987 .write = smk_write_load2,
1988 .release = seq_release,
1989 };
1990
1991 /*
1992 * Seq_file read operations for /smack/load-self2
1993 */
1994
1995 static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
1996 {
1997 struct task_smack *tsp = current_security();
1998
1999 return smk_seq_start(s, pos, &tsp->smk_rules);
2000 }
2001
2002 static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2003 {
2004 struct task_smack *tsp = current_security();
2005
2006 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2007 }
2008
2009 static int load_self2_seq_show(struct seq_file *s, void *v)
2010 {
2011 struct list_head *list = v;
2012 struct smack_rule *srp =
2013 list_entry(list, struct smack_rule, list);
2014
2015 smk_rule_show(s, srp, SMK_LONGLABEL);
2016
2017 return 0;
2018 }
2019
2020 static const struct seq_operations load_self2_seq_ops = {
2021 .start = load_self2_seq_start,
2022 .next = load_self2_seq_next,
2023 .show = load_self2_seq_show,
2024 .stop = smk_seq_stop,
2025 };
2026
2027 /**
2028 * smk_open_load_self2 - open() for /smack/load-self2
2029 * @inode: inode structure representing file
2030 * @file: "load" file pointer
2031 *
2032 * For reading, use load_seq_* seq_file reading operations.
2033 */
2034 static int smk_open_load_self2(struct inode *inode, struct file *file)
2035 {
2036 return seq_open(file, &load_self2_seq_ops);
2037 }
2038
2039 /**
2040 * smk_write_load_self2 - write() for /smack/load-self2
2041 * @file: file pointer, not actually used
2042 * @buf: where to get the data from
2043 * @count: bytes sent
2044 * @ppos: where to start - must be 0
2045 *
2046 */
2047 static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2048 size_t count, loff_t *ppos)
2049 {
2050 struct task_smack *tsp = current_security();
2051
2052 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2053 &tsp->smk_rules_lock, SMK_LONG_FMT);
2054 }
2055
2056 static const struct file_operations smk_load_self2_ops = {
2057 .open = smk_open_load_self2,
2058 .read = seq_read,
2059 .llseek = seq_lseek,
2060 .write = smk_write_load_self2,
2061 .release = seq_release,
2062 };
2063
2064 /**
2065 * smk_write_access2 - handle access check transaction
2066 * @file: file pointer
2067 * @buf: data from user space
2068 * @count: bytes sent
2069 * @ppos: where to start - must be 0
2070 */
2071 static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2072 size_t count, loff_t *ppos)
2073 {
2074 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2075 }
2076
2077 static const struct file_operations smk_access2_ops = {
2078 .write = smk_write_access2,
2079 .read = simple_transaction_read,
2080 .release = simple_transaction_release,
2081 .llseek = generic_file_llseek,
2082 };
2083
2084 /**
2085 * smk_write_revoke_subj - write() for /smack/revoke-subject
2086 * @file: file pointer
2087 * @buf: data from user space
2088 * @count: bytes sent
2089 * @ppos: where to start - must be 0
2090 */
2091 static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2092 size_t count, loff_t *ppos)
2093 {
2094 char *data = NULL;
2095 const char *cp = NULL;
2096 struct smack_known *skp;
2097 struct smack_rule *sp;
2098 struct list_head *rule_list;
2099 struct mutex *rule_lock;
2100 int rc = count;
2101
2102 if (*ppos != 0)
2103 return -EINVAL;
2104
2105 if (!smack_privileged(CAP_MAC_ADMIN))
2106 return -EPERM;
2107
2108 if (count == 0 || count > SMK_LONGLABEL)
2109 return -EINVAL;
2110
2111 data = kzalloc(count, GFP_KERNEL);
2112 if (data == NULL)
2113 return -ENOMEM;
2114
2115 if (copy_from_user(data, buf, count) != 0) {
2116 rc = -EFAULT;
2117 goto free_out;
2118 }
2119
2120 cp = smk_parse_smack(data, count);
2121 if (cp == NULL) {
2122 rc = -EINVAL;
2123 goto free_out;
2124 }
2125
2126 skp = smk_find_entry(cp);
2127 if (skp == NULL)
2128 goto free_out;
2129
2130 rule_list = &skp->smk_rules;
2131 rule_lock = &skp->smk_rules_lock;
2132
2133 mutex_lock(rule_lock);
2134
2135 list_for_each_entry_rcu(sp, rule_list, list)
2136 sp->smk_access = 0;
2137
2138 mutex_unlock(rule_lock);
2139
2140 free_out:
2141 kfree(data);
2142 kfree(cp);
2143 return rc;
2144 }
2145
2146 static const struct file_operations smk_revoke_subj_ops = {
2147 .write = smk_write_revoke_subj,
2148 .read = simple_transaction_read,
2149 .release = simple_transaction_release,
2150 .llseek = generic_file_llseek,
2151 };
2152
2153 static struct kset *smackfs_kset;
2154 /**
2155 * smk_init_sysfs - initialize /sys/fs/smackfs
2156 *
2157 */
2158 static int smk_init_sysfs(void)
2159 {
2160 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2161 if (!smackfs_kset)
2162 return -ENOMEM;
2163 return 0;
2164 }
2165
2166 /**
2167 * smk_write_change_rule - write() for /smack/change-rule
2168 * @file: file pointer
2169 * @buf: data from user space
2170 * @count: bytes sent
2171 * @ppos: where to start - must be 0
2172 */
2173 static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2174 size_t count, loff_t *ppos)
2175 {
2176 /*
2177 * Must have privilege.
2178 */
2179 if (!smack_privileged(CAP_MAC_ADMIN))
2180 return -EPERM;
2181
2182 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2183 SMK_CHANGE_FMT);
2184 }
2185
2186 static const struct file_operations smk_change_rule_ops = {
2187 .write = smk_write_change_rule,
2188 .read = simple_transaction_read,
2189 .release = simple_transaction_release,
2190 .llseek = generic_file_llseek,
2191 };
2192
2193 /**
2194 * smk_read_syslog - read() for smackfs/syslog
2195 * @filp: file pointer, not actually used
2196 * @buf: where to put the result
2197 * @cn: maximum to send along
2198 * @ppos: where to start
2199 *
2200 * Returns number of bytes read or error code, as appropriate
2201 */
2202 static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2203 size_t cn, loff_t *ppos)
2204 {
2205 struct smack_known *skp;
2206 ssize_t rc = -EINVAL;
2207 int asize;
2208
2209 if (*ppos != 0)
2210 return 0;
2211
2212 if (smack_syslog_label == NULL)
2213 skp = &smack_known_star;
2214 else
2215 skp = smack_syslog_label;
2216
2217 asize = strlen(skp->smk_known) + 1;
2218
2219 if (cn >= asize)
2220 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2221 asize);
2222
2223 return rc;
2224 }
2225
2226 /**
2227 * smk_write_syslog - write() for smackfs/syslog
2228 * @file: file pointer, not actually used
2229 * @buf: where to get the data from
2230 * @count: bytes sent
2231 * @ppos: where to start
2232 *
2233 * Returns number of bytes written or error code, as appropriate
2234 */
2235 static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2236 size_t count, loff_t *ppos)
2237 {
2238 char *data;
2239 struct smack_known *skp;
2240 int rc = count;
2241
2242 if (!smack_privileged(CAP_MAC_ADMIN))
2243 return -EPERM;
2244
2245 data = kzalloc(count + 1, GFP_KERNEL);
2246 if (data == NULL)
2247 return -ENOMEM;
2248
2249 if (copy_from_user(data, buf, count) != 0)
2250 rc = -EFAULT;
2251 else {
2252 skp = smk_import_entry(data, count);
2253 if (skp == NULL)
2254 rc = -EINVAL;
2255 else
2256 smack_syslog_label = smk_import_entry(data, count);
2257 }
2258
2259 kfree(data);
2260 return rc;
2261 }
2262
2263 static const struct file_operations smk_syslog_ops = {
2264 .read = smk_read_syslog,
2265 .write = smk_write_syslog,
2266 .llseek = default_llseek,
2267 };
2268
2269
2270 /**
2271 * smk_read_ptrace - read() for /smack/ptrace
2272 * @filp: file pointer, not actually used
2273 * @buf: where to put the result
2274 * @count: maximum to send along
2275 * @ppos: where to start
2276 *
2277 * Returns number of bytes read or error code, as appropriate
2278 */
2279 static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2280 size_t count, loff_t *ppos)
2281 {
2282 char temp[32];
2283 ssize_t rc;
2284
2285 if (*ppos != 0)
2286 return 0;
2287
2288 sprintf(temp, "%d\n", smack_ptrace_rule);
2289 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2290 return rc;
2291 }
2292
2293 /**
2294 * smk_write_ptrace - write() for /smack/ptrace
2295 * @file: file pointer
2296 * @buf: data from user space
2297 * @count: bytes sent
2298 * @ppos: where to start - must be 0
2299 */
2300 static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2301 size_t count, loff_t *ppos)
2302 {
2303 char temp[32];
2304 int i;
2305
2306 if (!smack_privileged(CAP_MAC_ADMIN))
2307 return -EPERM;
2308
2309 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2310 return -EINVAL;
2311
2312 if (copy_from_user(temp, buf, count) != 0)
2313 return -EFAULT;
2314
2315 temp[count] = '\0';
2316
2317 if (sscanf(temp, "%d", &i) != 1)
2318 return -EINVAL;
2319 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2320 return -EINVAL;
2321 smack_ptrace_rule = i;
2322
2323 return count;
2324 }
2325
2326 static const struct file_operations smk_ptrace_ops = {
2327 .write = smk_write_ptrace,
2328 .read = smk_read_ptrace,
2329 .llseek = default_llseek,
2330 };
2331
2332 /**
2333 * smk_fill_super - fill the smackfs superblock
2334 * @sb: the empty superblock
2335 * @data: unused
2336 * @silent: unused
2337 *
2338 * Fill in the well known entries for the smack filesystem
2339 *
2340 * Returns 0 on success, an error code on failure
2341 */
2342 static int smk_fill_super(struct super_block *sb, void *data, int silent)
2343 {
2344 int rc;
2345 struct inode *root_inode;
2346
2347 static struct tree_descr smack_files[] = {
2348 [SMK_LOAD] = {
2349 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2350 [SMK_CIPSO] = {
2351 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2352 [SMK_DOI] = {
2353 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2354 [SMK_DIRECT] = {
2355 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2356 [SMK_AMBIENT] = {
2357 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2358 [SMK_NETLBLADDR] = {
2359 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2360 [SMK_ONLYCAP] = {
2361 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2362 [SMK_LOGGING] = {
2363 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2364 [SMK_LOAD_SELF] = {
2365 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
2366 [SMK_ACCESSES] = {
2367 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
2368 [SMK_MAPPED] = {
2369 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2370 [SMK_LOAD2] = {
2371 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2372 [SMK_LOAD_SELF2] = {
2373 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2374 [SMK_ACCESS2] = {
2375 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2376 [SMK_CIPSO2] = {
2377 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
2378 [SMK_REVOKE_SUBJ] = {
2379 "revoke-subject", &smk_revoke_subj_ops,
2380 S_IRUGO|S_IWUSR},
2381 [SMK_CHANGE_RULE] = {
2382 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
2383 [SMK_SYSLOG] = {
2384 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
2385 [SMK_PTRACE] = {
2386 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
2387 /* last one */
2388 {""}
2389 };
2390
2391 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2392 if (rc != 0) {
2393 printk(KERN_ERR "%s failed %d while creating inodes\n",
2394 __func__, rc);
2395 return rc;
2396 }
2397
2398 root_inode = sb->s_root->d_inode;
2399
2400 return 0;
2401 }
2402
2403 /**
2404 * smk_mount - get the smackfs superblock
2405 * @fs_type: passed along without comment
2406 * @flags: passed along without comment
2407 * @dev_name: passed along without comment
2408 * @data: passed along without comment
2409 *
2410 * Just passes everything along.
2411 *
2412 * Returns what the lower level code does.
2413 */
2414 static struct dentry *smk_mount(struct file_system_type *fs_type,
2415 int flags, const char *dev_name, void *data)
2416 {
2417 return mount_single(fs_type, flags, data, smk_fill_super);
2418 }
2419
2420 static struct file_system_type smk_fs_type = {
2421 .name = "smackfs",
2422 .mount = smk_mount,
2423 .kill_sb = kill_litter_super,
2424 };
2425
2426 static struct vfsmount *smackfs_mount;
2427
2428 static int __init smk_preset_netlabel(struct smack_known *skp)
2429 {
2430 skp->smk_netlabel.domain = skp->smk_known;
2431 skp->smk_netlabel.flags =
2432 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2433 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2434 &skp->smk_netlabel, strlen(skp->smk_known));
2435 }
2436
2437 /**
2438 * init_smk_fs - get the smackfs superblock
2439 *
2440 * register the smackfs
2441 *
2442 * Do not register smackfs if Smack wasn't enabled
2443 * on boot. We can not put this method normally under the
2444 * smack_init() code path since the security subsystem get
2445 * initialized before the vfs caches.
2446 *
2447 * Returns true if we were not chosen on boot or if
2448 * we were chosen and filesystem registration succeeded.
2449 */
2450 static int __init init_smk_fs(void)
2451 {
2452 int err;
2453 int rc;
2454
2455 if (!security_module_enable(&smack_ops))
2456 return 0;
2457
2458 err = smk_init_sysfs();
2459 if (err)
2460 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2461
2462 err = register_filesystem(&smk_fs_type);
2463 if (!err) {
2464 smackfs_mount = kern_mount(&smk_fs_type);
2465 if (IS_ERR(smackfs_mount)) {
2466 printk(KERN_ERR "smackfs: could not mount!\n");
2467 err = PTR_ERR(smackfs_mount);
2468 smackfs_mount = NULL;
2469 }
2470 }
2471
2472 smk_cipso_doi();
2473 smk_unlbl_ambient(NULL);
2474
2475 rc = smk_preset_netlabel(&smack_known_floor);
2476 if (err == 0 && rc < 0)
2477 err = rc;
2478 rc = smk_preset_netlabel(&smack_known_hat);
2479 if (err == 0 && rc < 0)
2480 err = rc;
2481 rc = smk_preset_netlabel(&smack_known_huh);
2482 if (err == 0 && rc < 0)
2483 err = rc;
2484 rc = smk_preset_netlabel(&smack_known_invalid);
2485 if (err == 0 && rc < 0)
2486 err = rc;
2487 rc = smk_preset_netlabel(&smack_known_star);
2488 if (err == 0 && rc < 0)
2489 err = rc;
2490 rc = smk_preset_netlabel(&smack_known_web);
2491 if (err == 0 && rc < 0)
2492 err = rc;
2493
2494 return err;
2495 }
2496
2497 __initcall(init_smk_fs);