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