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