blob: fd8d1eb437006337ec3fb4824dc8ba3042aa07fb [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>
Casey Schaufler6d3dc072008-12-31 12:54:12 -050023#include <net/net_namespace.h>
Casey Schauflere114e472008-02-04 22:29:50 -080024#include <net/netlabel.h>
25#include <net/cipso_ipv4.h>
26#include <linux/seq_file.h>
27#include <linux/ctype.h>
Casey Schaufler4bc87e62008-02-15 15:24:25 -080028#include <linux/audit.h>
Casey Schauflere114e472008-02-04 22:29:50 -080029#include "smack.h"
30
31/*
32 * smackfs pseudo filesystem.
33 */
34
35enum smk_inos {
36 SMK_ROOT_INO = 2,
37 SMK_LOAD = 3, /* load policy */
38 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
39 SMK_DOI = 5, /* CIPSO DOI */
40 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
41 SMK_AMBIENT = 7, /* internet ambient label */
Casey Schaufler6d3dc072008-12-31 12:54:12 -050042 SMK_NETLBLADDR = 8, /* single label hosts */
Casey Schaufler15446232008-07-30 15:37:11 -070043 SMK_ONLYCAP = 9, /* the only "capable" label */
Casey Schauflere114e472008-02-04 22:29:50 -080044};
45
46/*
47 * List locks
48 */
49static DEFINE_MUTEX(smack_list_lock);
50static DEFINE_MUTEX(smack_cipso_lock);
Casey Schaufler4bc87e62008-02-15 15:24:25 -080051static DEFINE_MUTEX(smack_ambient_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -050052static DEFINE_MUTEX(smk_netlbladdr_lock);
Casey Schauflere114e472008-02-04 22:29:50 -080053
54/*
55 * This is the "ambient" label for network traffic.
56 * If it isn't somehow marked, use this.
57 * It can be reset via smackfs/ambient
58 */
59char *smack_net_ambient = smack_known_floor.smk_known;
60
61/*
Casey Schauflere114e472008-02-04 22:29:50 -080062 * This is the level in a CIPSO header that indicates a
63 * smack label is contained directly in the category set.
64 * It can be reset via smackfs/direct
65 */
66int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
67
Casey Schaufler15446232008-07-30 15:37:11 -070068/*
69 * Unless a process is running with this label even
70 * having CAP_MAC_OVERRIDE isn't enough to grant
71 * privilege to violate MAC policy. If no label is
72 * designated (the NULL case) capabilities apply to
73 * everyone. It is expected that the hat (^) label
74 * will be used if any label is used.
75 */
76char *smack_onlycap;
77
Casey Schaufler6d3dc072008-12-31 12:54:12 -050078/*
79 * Certain IP addresses may be designated as single label hosts.
80 * Packets are sent there unlabeled, but only from tasks that
81 * can write to the specified label.
82 */
83struct smk_netlbladdr *smack_netlbladdrs;
84
Casey Schauflere114e472008-02-04 22:29:50 -080085static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
86struct smk_list_entry *smack_list;
87
88#define SEQ_READ_FINISHED 1
89
90/*
Casey Schauflere114e472008-02-04 22:29:50 -080091 * Values for parsing cipso rules
92 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -070093 * SMK_CIPSOMIN: Minimum possible cipso rule length.
94 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -080095 */
96#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -070097#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
98#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
99
100/*
101 * Values for parsing MAC rules
102 * SMK_ACCESS: Maximum possible combination of access permissions
103 * SMK_ACCESSLEN: Maximum length for a rule access field
104 * SMK_LOADLEN: Smack rule length
105 */
106#define SMK_ACCESS "rwxa"
107#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
108#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
109
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500110/**
111 * smk_netlabel_audit_set - fill a netlbl_audit struct
112 * @nap: structure to fill
113 */
114static void smk_netlabel_audit_set(struct netlbl_audit *nap)
115{
116 nap->loginuid = audit_get_loginuid(current);
117 nap->sessionid = audit_get_sessionid(current);
118 nap->secid = smack_to_secid(current_security());
119}
120
121/*
122 * Values for parsing single label host rules
123 * "1.2.3.4 X"
124 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
125 */
126#define SMK_NETLBLADDRMIN 9
127#define SMK_NETLBLADDRMAX 42
Casey Schauflere114e472008-02-04 22:29:50 -0800128
129/*
130 * Seq_file read operations for /smack/load
131 */
132
133static void *load_seq_start(struct seq_file *s, loff_t *pos)
134{
135 if (*pos == SEQ_READ_FINISHED)
136 return NULL;
137
138 return smack_list;
139}
140
141static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
142{
143 struct smk_list_entry *skp = ((struct smk_list_entry *) v)->smk_next;
144
145 if (skp == NULL)
146 *pos = SEQ_READ_FINISHED;
147
148 return skp;
149}
150
151static int load_seq_show(struct seq_file *s, void *v)
152{
153 struct smk_list_entry *slp = (struct smk_list_entry *) v;
154 struct smack_rule *srp = &slp->smk_rule;
155
156 seq_printf(s, "%s %s", (char *)srp->smk_subject,
157 (char *)srp->smk_object);
158
159 seq_putc(s, ' ');
160
161 if (srp->smk_access & MAY_READ)
162 seq_putc(s, 'r');
163 if (srp->smk_access & MAY_WRITE)
164 seq_putc(s, 'w');
165 if (srp->smk_access & MAY_EXEC)
166 seq_putc(s, 'x');
167 if (srp->smk_access & MAY_APPEND)
168 seq_putc(s, 'a');
169 if (srp->smk_access == 0)
170 seq_putc(s, '-');
171
172 seq_putc(s, '\n');
173
174 return 0;
175}
176
177static void load_seq_stop(struct seq_file *s, void *v)
178{
179 /* No-op */
180}
181
182static struct seq_operations load_seq_ops = {
183 .start = load_seq_start,
184 .next = load_seq_next,
185 .show = load_seq_show,
186 .stop = load_seq_stop,
187};
188
189/**
190 * smk_open_load - open() for /smack/load
191 * @inode: inode structure representing file
192 * @file: "load" file pointer
193 *
194 * For reading, use load_seq_* seq_file reading operations.
195 */
196static int smk_open_load(struct inode *inode, struct file *file)
197{
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700198 return seq_open(file, &load_seq_ops);
Casey Schauflere114e472008-02-04 22:29:50 -0800199}
200
201/**
202 * smk_set_access - add a rule to the rule list
203 * @srp: the new rule to add
204 *
205 * Looks through the current subject/object/access list for
206 * the subject/object pair and replaces the access that was
207 * there. If the pair isn't found add it with the specified
208 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300209 *
210 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
211 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800212 */
Sergio Luis81ea7142008-12-22 01:16:15 -0300213static int smk_set_access(struct smack_rule *srp)
Casey Schauflere114e472008-02-04 22:29:50 -0800214{
215 struct smk_list_entry *sp;
216 struct smk_list_entry *newp;
Sergio Luis81ea7142008-12-22 01:16:15 -0300217 int ret = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800218
219 mutex_lock(&smack_list_lock);
220
221 for (sp = smack_list; sp != NULL; sp = sp->smk_next)
222 if (sp->smk_rule.smk_subject == srp->smk_subject &&
223 sp->smk_rule.smk_object == srp->smk_object) {
224 sp->smk_rule.smk_access = srp->smk_access;
225 break;
226 }
227
228 if (sp == NULL) {
229 newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL);
Sergio Luis81ea7142008-12-22 01:16:15 -0300230 if (newp == NULL) {
231 ret = -ENOMEM;
232 goto out;
233 }
234
Casey Schauflere114e472008-02-04 22:29:50 -0800235 newp->smk_rule = *srp;
236 newp->smk_next = smack_list;
237 smack_list = newp;
238 }
239
Sergio Luis81ea7142008-12-22 01:16:15 -0300240out:
Casey Schauflere114e472008-02-04 22:29:50 -0800241 mutex_unlock(&smack_list_lock);
242
Sergio Luis81ea7142008-12-22 01:16:15 -0300243 return ret;
Casey Schauflere114e472008-02-04 22:29:50 -0800244}
245
246/**
247 * smk_write_load - write() for /smack/load
Randy Dunlap251a2a92009-02-18 11:42:33 -0800248 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800249 * @buf: where to get the data from
250 * @count: bytes sent
251 * @ppos: where to start - must be 0
252 *
253 * Get one smack access rule from above.
254 * The format is exactly:
255 * char subject[SMK_LABELLEN]
256 * char object[SMK_LABELLEN]
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700257 * char access[SMK_ACCESSLEN]
Casey Schauflere114e472008-02-04 22:29:50 -0800258 *
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700259 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
Casey Schauflere114e472008-02-04 22:29:50 -0800260 */
Casey Schauflere114e472008-02-04 22:29:50 -0800261static ssize_t smk_write_load(struct file *file, const char __user *buf,
262 size_t count, loff_t *ppos)
263{
264 struct smack_rule rule;
265 char *data;
266 int rc = -EINVAL;
267
268 /*
269 * Must have privilege.
270 * No partial writes.
271 * Enough data must be present.
272 */
273 if (!capable(CAP_MAC_ADMIN))
274 return -EPERM;
275 if (*ppos != 0)
276 return -EINVAL;
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700277 if (count != SMK_LOADLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800278 return -EINVAL;
279
280 data = kzalloc(count, GFP_KERNEL);
281 if (data == NULL)
282 return -ENOMEM;
283
284 if (copy_from_user(data, buf, count) != 0) {
285 rc = -EFAULT;
286 goto out;
287 }
288
289 rule.smk_subject = smk_import(data, 0);
290 if (rule.smk_subject == NULL)
291 goto out;
292
293 rule.smk_object = smk_import(data + SMK_LABELLEN, 0);
294 if (rule.smk_object == NULL)
295 goto out;
296
297 rule.smk_access = 0;
298
299 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
300 case '-':
301 break;
302 case 'r':
303 case 'R':
304 rule.smk_access |= MAY_READ;
305 break;
306 default:
307 goto out;
308 }
309
310 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
311 case '-':
312 break;
313 case 'w':
314 case 'W':
315 rule.smk_access |= MAY_WRITE;
316 break;
317 default:
318 goto out;
319 }
320
321 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
322 case '-':
323 break;
324 case 'x':
325 case 'X':
326 rule.smk_access |= MAY_EXEC;
327 break;
328 default:
329 goto out;
330 }
331
332 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
333 case '-':
334 break;
335 case 'a':
336 case 'A':
Casey Schaufler152a6492009-01-27 19:56:30 -0800337 rule.smk_access |= MAY_APPEND;
Casey Schauflere114e472008-02-04 22:29:50 -0800338 break;
339 default:
340 goto out;
341 }
342
Sergio Luis81ea7142008-12-22 01:16:15 -0300343 rc = smk_set_access(&rule);
344
345 if (!rc)
346 rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -0800347
348out:
349 kfree(data);
350 return rc;
351}
352
353static const struct file_operations smk_load_ops = {
354 .open = smk_open_load,
355 .read = seq_read,
356 .llseek = seq_lseek,
357 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700358 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800359};
360
361/**
362 * smk_cipso_doi - initialize the CIPSO domain
363 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700364static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800365{
366 int rc;
367 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500368 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800369
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500370 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800371
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500372 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800373 if (rc != 0)
374 printk(KERN_WARNING "%s:%d remove rc = %d\n",
375 __func__, __LINE__, rc);
376
377 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
378 if (doip == NULL)
379 panic("smack: Failed to initialize cipso DOI.\n");
380 doip->map.std = NULL;
381 doip->doi = smk_cipso_doi_value;
382 doip->type = CIPSO_V4_MAP_PASS;
383 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
384 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
385 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
386
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500387 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400388 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500389 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800390 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400391 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500392 return;
393 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500394 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500395 if (rc != 0) {
396 printk(KERN_WARNING "%s:%d map add rc = %d\n",
397 __func__, __LINE__, rc);
398 kfree(doip);
399 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400400 }
Casey Schauflere114e472008-02-04 22:29:50 -0800401}
402
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800403/**
404 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800405 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800406 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700407static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800408{
409 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500410 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800411
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500412 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800413
414 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500415 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800416 if (rc != 0)
417 printk(KERN_WARNING "%s:%d remove rc = %d\n",
418 __func__, __LINE__, rc);
419 }
420
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500421 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
422 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800423 if (rc != 0)
424 printk(KERN_WARNING "%s:%d add rc = %d\n",
425 __func__, __LINE__, rc);
426}
427
Casey Schauflere114e472008-02-04 22:29:50 -0800428/*
429 * Seq_file read operations for /smack/cipso
430 */
431
432static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
433{
434 if (*pos == SEQ_READ_FINISHED)
435 return NULL;
436
437 return smack_known;
438}
439
440static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
441{
442 struct smack_known *skp = ((struct smack_known *) v)->smk_next;
443
444 /*
445 * Omit labels with no associated cipso value
446 */
447 while (skp != NULL && !skp->smk_cipso)
448 skp = skp->smk_next;
449
450 if (skp == NULL)
451 *pos = SEQ_READ_FINISHED;
452
453 return skp;
454}
455
456/*
457 * Print cipso labels in format:
458 * label level[/cat[,cat]]
459 */
460static int cipso_seq_show(struct seq_file *s, void *v)
461{
462 struct smack_known *skp = (struct smack_known *) v;
463 struct smack_cipso *scp = skp->smk_cipso;
464 char *cbp;
465 char sep = '/';
466 int cat = 1;
467 int i;
468 unsigned char m;
469
470 if (scp == NULL)
471 return 0;
472
473 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
474
475 cbp = scp->smk_catset;
476 for (i = 0; i < SMK_LABELLEN; i++)
477 for (m = 0x80; m != 0; m >>= 1) {
478 if (m & cbp[i]) {
479 seq_printf(s, "%c%d", sep, cat);
480 sep = ',';
481 }
482 cat++;
483 }
484
485 seq_putc(s, '\n');
486
487 return 0;
488}
489
490static void cipso_seq_stop(struct seq_file *s, void *v)
491{
492 /* No-op */
493}
494
495static struct seq_operations cipso_seq_ops = {
496 .start = cipso_seq_start,
497 .stop = cipso_seq_stop,
498 .next = cipso_seq_next,
499 .show = cipso_seq_show,
500};
501
502/**
503 * smk_open_cipso - open() for /smack/cipso
504 * @inode: inode structure representing file
505 * @file: "cipso" file pointer
506 *
507 * Connect our cipso_seq_* operations with /smack/cipso
508 * file_operations
509 */
510static int smk_open_cipso(struct inode *inode, struct file *file)
511{
512 return seq_open(file, &cipso_seq_ops);
513}
514
515/**
516 * smk_write_cipso - write() for /smack/cipso
Randy Dunlap251a2a92009-02-18 11:42:33 -0800517 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800518 * @buf: where to get the data from
519 * @count: bytes sent
520 * @ppos: where to start
521 *
522 * Accepts only one cipso rule per write call.
523 * Returns number of bytes written or error code, as appropriate
524 */
525static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
526 size_t count, loff_t *ppos)
527{
528 struct smack_known *skp;
529 struct smack_cipso *scp = NULL;
530 char mapcatset[SMK_LABELLEN];
531 int maplevel;
532 int cat;
533 int catlen;
534 ssize_t rc = -EINVAL;
535 char *data = NULL;
536 char *rule;
537 int ret;
538 int i;
539
540 /*
541 * Must have privilege.
542 * No partial writes.
543 * Enough data must be present.
544 */
545 if (!capable(CAP_MAC_ADMIN))
546 return -EPERM;
547 if (*ppos != 0)
548 return -EINVAL;
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700549 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
Casey Schauflere114e472008-02-04 22:29:50 -0800550 return -EINVAL;
551
552 data = kzalloc(count + 1, GFP_KERNEL);
553 if (data == NULL)
554 return -ENOMEM;
555
556 if (copy_from_user(data, buf, count) != 0) {
557 rc = -EFAULT;
558 goto unlockedout;
559 }
560
561 data[count] = '\0';
562 rule = data;
563 /*
564 * Only allow one writer at a time. Writes should be
565 * quite rare and small in any case.
566 */
567 mutex_lock(&smack_cipso_lock);
568
569 skp = smk_import_entry(rule, 0);
570 if (skp == NULL)
571 goto out;
572
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800573 rule += SMK_LABELLEN;
Casey Schauflere114e472008-02-04 22:29:50 -0800574 ret = sscanf(rule, "%d", &maplevel);
575 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
576 goto out;
577
578 rule += SMK_DIGITLEN;
579 ret = sscanf(rule, "%d", &catlen);
580 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
581 goto out;
582
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700583 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800584 goto out;
585
586 memset(mapcatset, 0, sizeof(mapcatset));
587
588 for (i = 0; i < catlen; i++) {
589 rule += SMK_DIGITLEN;
590 ret = sscanf(rule, "%d", &cat);
591 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
592 goto out;
593
594 smack_catset_bit(cat, mapcatset);
595 }
596
597 if (skp->smk_cipso == NULL) {
598 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
599 if (scp == NULL) {
600 rc = -ENOMEM;
601 goto out;
602 }
603 }
604
605 spin_lock_bh(&skp->smk_cipsolock);
606
607 if (scp == NULL)
608 scp = skp->smk_cipso;
609 else
610 skp->smk_cipso = scp;
611
612 scp->smk_level = maplevel;
613 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
614
615 spin_unlock_bh(&skp->smk_cipsolock);
616
617 rc = count;
618out:
619 mutex_unlock(&smack_cipso_lock);
620unlockedout:
621 kfree(data);
622 return rc;
623}
624
625static const struct file_operations smk_cipso_ops = {
626 .open = smk_open_cipso,
627 .read = seq_read,
628 .llseek = seq_lseek,
629 .write = smk_write_cipso,
630 .release = seq_release,
631};
632
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500633/*
634 * Seq_file read operations for /smack/netlabel
635 */
636
637static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
638{
639 if (*pos == SEQ_READ_FINISHED)
640 return NULL;
641
642 return smack_netlbladdrs;
643}
644
645static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
646{
647 struct smk_netlbladdr *skp = ((struct smk_netlbladdr *) v)->smk_next;
648
649 if (skp == NULL)
650 *pos = SEQ_READ_FINISHED;
651
652 return skp;
653}
654/*
655#define BEMASK 0x80000000
656*/
657#define BEMASK 0x00000001
658#define BEBITS (sizeof(__be32) * 8)
659
660/*
661 * Print host/label pairs
662 */
663static int netlbladdr_seq_show(struct seq_file *s, void *v)
664{
665 struct smk_netlbladdr *skp = (struct smk_netlbladdr *) v;
666 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
667 __be32 bebits;
668 int maskn = 0;
669
670 for (bebits = BEMASK; bebits != 0; maskn++, bebits <<= 1)
671 if ((skp->smk_mask.s_addr & bebits) == 0)
672 break;
673
674 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
675 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
676
677 return 0;
678}
679
680static void netlbladdr_seq_stop(struct seq_file *s, void *v)
681{
682 /* No-op */
683}
684
685static struct seq_operations netlbladdr_seq_ops = {
686 .start = netlbladdr_seq_start,
687 .stop = netlbladdr_seq_stop,
688 .next = netlbladdr_seq_next,
689 .show = netlbladdr_seq_show,
690};
691
692/**
693 * smk_open_netlbladdr - open() for /smack/netlabel
694 * @inode: inode structure representing file
695 * @file: "netlabel" file pointer
696 *
697 * Connect our netlbladdr_seq_* operations with /smack/netlabel
698 * file_operations
699 */
700static int smk_open_netlbladdr(struct inode *inode, struct file *file)
701{
702 return seq_open(file, &netlbladdr_seq_ops);
703}
704
705/**
706 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -0800707 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500708 * @buf: where to get the data from
709 * @count: bytes sent
710 * @ppos: where to start
711 *
712 * Accepts only one netlbladdr per write call.
713 * Returns number of bytes written or error code, as appropriate
714 */
715static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
716 size_t count, loff_t *ppos)
717{
718 struct smk_netlbladdr *skp;
719 struct sockaddr_in newname;
720 char smack[SMK_LABELLEN];
721 char *sp;
722 char data[SMK_NETLBLADDRMAX];
723 char *host = (char *)&newname.sin_addr.s_addr;
724 int rc;
725 struct netlbl_audit audit_info;
726 struct in_addr mask;
727 unsigned int m;
728 __be32 bebits = BEMASK;
729 __be32 nsa;
730
731 /*
732 * Must have privilege.
733 * No partial writes.
734 * Enough data must be present.
735 * "<addr/mask, as a.b.c.d/e><space><label>"
736 * "<addr, as a.b.c.d><space><label>"
737 */
738 if (!capable(CAP_MAC_ADMIN))
739 return -EPERM;
740 if (*ppos != 0)
741 return -EINVAL;
742 if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
743 return -EINVAL;
744 if (copy_from_user(data, buf, count) != 0)
745 return -EFAULT;
746
747 data[count] = '\0';
748
749 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
750 &host[0], &host[1], &host[2], &host[3], &m, smack);
751 if (rc != 6) {
752 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
753 &host[0], &host[1], &host[2], &host[3], smack);
754 if (rc != 5)
755 return -EINVAL;
756 m = BEBITS;
757 }
758 if (m > BEBITS)
759 return -EINVAL;
760
761 sp = smk_import(smack, 0);
762 if (sp == NULL)
763 return -EINVAL;
764
765 for (mask.s_addr = 0; m > 0; m--) {
766 mask.s_addr |= bebits;
767 bebits <<= 1;
768 }
769 /*
770 * Only allow one writer at a time. Writes should be
771 * quite rare and small in any case.
772 */
773 mutex_lock(&smk_netlbladdr_lock);
774
775 nsa = newname.sin_addr.s_addr;
776 for (skp = smack_netlbladdrs; skp != NULL; skp = skp->smk_next)
777 if (skp->smk_host.sin_addr.s_addr == nsa &&
778 skp->smk_mask.s_addr == mask.s_addr)
779 break;
780
781 smk_netlabel_audit_set(&audit_info);
782
783 if (skp == NULL) {
784 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
785 if (skp == NULL)
786 rc = -ENOMEM;
787 else {
788 rc = 0;
789 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
790 skp->smk_mask.s_addr = mask.s_addr;
791 skp->smk_next = smack_netlbladdrs;
792 skp->smk_label = sp;
793 smack_netlbladdrs = skp;
794 }
795 } else {
796 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
797 &skp->smk_host.sin_addr, &skp->smk_mask,
798 PF_INET, &audit_info);
799 skp->smk_label = sp;
800 }
801
802 /*
803 * Now tell netlabel about the single label nature of
804 * this host so that incoming packets get labeled.
805 */
806
807 if (rc == 0)
808 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
809 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
810 smack_to_secid(skp->smk_label), &audit_info);
811
812 if (rc == 0)
813 rc = count;
814
815 mutex_unlock(&smk_netlbladdr_lock);
816
817 return rc;
818}
819
820static const struct file_operations smk_netlbladdr_ops = {
821 .open = smk_open_netlbladdr,
822 .read = seq_read,
823 .llseek = seq_lseek,
824 .write = smk_write_netlbladdr,
825 .release = seq_release,
826};
827
Casey Schauflere114e472008-02-04 22:29:50 -0800828/**
829 * smk_read_doi - read() for /smack/doi
830 * @filp: file pointer, not actually used
831 * @buf: where to put the result
832 * @count: maximum to send along
833 * @ppos: where to start
834 *
835 * Returns number of bytes read or error code, as appropriate
836 */
837static ssize_t smk_read_doi(struct file *filp, char __user *buf,
838 size_t count, loff_t *ppos)
839{
840 char temp[80];
841 ssize_t rc;
842
843 if (*ppos != 0)
844 return 0;
845
846 sprintf(temp, "%d", smk_cipso_doi_value);
847 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
848
849 return rc;
850}
851
852/**
853 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -0800854 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800855 * @buf: where to get the data from
856 * @count: bytes sent
857 * @ppos: where to start
858 *
859 * Returns number of bytes written or error code, as appropriate
860 */
861static ssize_t smk_write_doi(struct file *file, const char __user *buf,
862 size_t count, loff_t *ppos)
863{
864 char temp[80];
865 int i;
866
867 if (!capable(CAP_MAC_ADMIN))
868 return -EPERM;
869
870 if (count >= sizeof(temp) || count == 0)
871 return -EINVAL;
872
873 if (copy_from_user(temp, buf, count) != 0)
874 return -EFAULT;
875
876 temp[count] = '\0';
877
878 if (sscanf(temp, "%d", &i) != 1)
879 return -EINVAL;
880
881 smk_cipso_doi_value = i;
882
883 smk_cipso_doi();
884
885 return count;
886}
887
888static const struct file_operations smk_doi_ops = {
889 .read = smk_read_doi,
890 .write = smk_write_doi,
891};
892
893/**
894 * smk_read_direct - read() for /smack/direct
895 * @filp: file pointer, not actually used
896 * @buf: where to put the result
897 * @count: maximum to send along
898 * @ppos: where to start
899 *
900 * Returns number of bytes read or error code, as appropriate
901 */
902static ssize_t smk_read_direct(struct file *filp, char __user *buf,
903 size_t count, loff_t *ppos)
904{
905 char temp[80];
906 ssize_t rc;
907
908 if (*ppos != 0)
909 return 0;
910
911 sprintf(temp, "%d", smack_cipso_direct);
912 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
913
914 return rc;
915}
916
917/**
918 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -0800919 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800920 * @buf: where to get the data from
921 * @count: bytes sent
922 * @ppos: where to start
923 *
924 * Returns number of bytes written or error code, as appropriate
925 */
926static ssize_t smk_write_direct(struct file *file, const char __user *buf,
927 size_t count, loff_t *ppos)
928{
929 char temp[80];
930 int i;
931
932 if (!capable(CAP_MAC_ADMIN))
933 return -EPERM;
934
935 if (count >= sizeof(temp) || count == 0)
936 return -EINVAL;
937
938 if (copy_from_user(temp, buf, count) != 0)
939 return -EFAULT;
940
941 temp[count] = '\0';
942
943 if (sscanf(temp, "%d", &i) != 1)
944 return -EINVAL;
945
946 smack_cipso_direct = i;
947
948 return count;
949}
950
951static const struct file_operations smk_direct_ops = {
952 .read = smk_read_direct,
953 .write = smk_write_direct,
954};
955
956/**
957 * smk_read_ambient - read() for /smack/ambient
958 * @filp: file pointer, not actually used
959 * @buf: where to put the result
960 * @cn: maximum to send along
961 * @ppos: where to start
962 *
963 * Returns number of bytes read or error code, as appropriate
964 */
965static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
966 size_t cn, loff_t *ppos)
967{
968 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800969 int asize;
970
971 if (*ppos != 0)
972 return 0;
973 /*
974 * Being careful to avoid a problem in the case where
975 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -0800976 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800977 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800978
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800979 asize = strlen(smack_net_ambient) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800980
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800981 if (cn >= asize)
982 rc = simple_read_from_buffer(buf, cn, ppos,
983 smack_net_ambient, asize);
984 else
985 rc = -EINVAL;
986
987 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800988
989 return rc;
990}
991
992/**
993 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -0800994 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800995 * @buf: where to get the data from
996 * @count: bytes sent
997 * @ppos: where to start
998 *
999 * Returns number of bytes written or error code, as appropriate
1000 */
1001static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1002 size_t count, loff_t *ppos)
1003{
1004 char in[SMK_LABELLEN];
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001005 char *oldambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001006 char *smack;
1007
1008 if (!capable(CAP_MAC_ADMIN))
1009 return -EPERM;
1010
1011 if (count >= SMK_LABELLEN)
1012 return -EINVAL;
1013
1014 if (copy_from_user(in, buf, count) != 0)
1015 return -EFAULT;
1016
1017 smack = smk_import(in, count);
1018 if (smack == NULL)
1019 return -EINVAL;
1020
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001021 mutex_lock(&smack_ambient_lock);
1022
1023 oldambient = smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001024 smack_net_ambient = smack;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001025 smk_unlbl_ambient(oldambient);
1026
1027 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001028
1029 return count;
1030}
1031
1032static const struct file_operations smk_ambient_ops = {
1033 .read = smk_read_ambient,
1034 .write = smk_write_ambient,
1035};
1036
Casey Schaufler15446232008-07-30 15:37:11 -07001037/**
1038 * smk_read_onlycap - read() for /smack/onlycap
1039 * @filp: file pointer, not actually used
1040 * @buf: where to put the result
1041 * @cn: maximum to send along
1042 * @ppos: where to start
1043 *
1044 * Returns number of bytes read or error code, as appropriate
1045 */
1046static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1047 size_t cn, loff_t *ppos)
1048{
1049 char *smack = "";
1050 ssize_t rc = -EINVAL;
1051 int asize;
1052
1053 if (*ppos != 0)
1054 return 0;
1055
1056 if (smack_onlycap != NULL)
1057 smack = smack_onlycap;
1058
1059 asize = strlen(smack) + 1;
1060
1061 if (cn >= asize)
1062 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1063
1064 return rc;
1065}
1066
1067/**
1068 * smk_write_onlycap - write() for /smack/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001069 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001070 * @buf: where to get the data from
1071 * @count: bytes sent
1072 * @ppos: where to start
1073 *
1074 * Returns number of bytes written or error code, as appropriate
1075 */
1076static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1077 size_t count, loff_t *ppos)
1078{
1079 char in[SMK_LABELLEN];
David Howellsb6dff3e2008-11-14 10:39:16 +11001080 char *sp = current->cred->security;
Casey Schaufler15446232008-07-30 15:37:11 -07001081
1082 if (!capable(CAP_MAC_ADMIN))
1083 return -EPERM;
1084
1085 /*
1086 * This can be done using smk_access() but is done
1087 * explicitly for clarity. The smk_access() implementation
1088 * would use smk_access(smack_onlycap, MAY_WRITE)
1089 */
1090 if (smack_onlycap != NULL && smack_onlycap != sp)
1091 return -EPERM;
1092
1093 if (count >= SMK_LABELLEN)
1094 return -EINVAL;
1095
1096 if (copy_from_user(in, buf, count) != 0)
1097 return -EFAULT;
1098
1099 /*
1100 * Should the null string be passed in unset the onlycap value.
1101 * This seems like something to be careful with as usually
1102 * smk_import only expects to return NULL for errors. It
1103 * is usually the case that a nullstring or "\n" would be
1104 * bad to pass to smk_import but in fact this is useful here.
1105 */
1106 smack_onlycap = smk_import(in, count);
1107
1108 return count;
1109}
1110
1111static const struct file_operations smk_onlycap_ops = {
1112 .read = smk_read_onlycap,
1113 .write = smk_write_onlycap,
1114};
1115
Casey Schauflere114e472008-02-04 22:29:50 -08001116/**
1117 * smk_fill_super - fill the /smackfs superblock
1118 * @sb: the empty superblock
1119 * @data: unused
1120 * @silent: unused
1121 *
1122 * Fill in the well known entries for /smack
1123 *
1124 * Returns 0 on success, an error code on failure
1125 */
1126static int smk_fill_super(struct super_block *sb, void *data, int silent)
1127{
1128 int rc;
1129 struct inode *root_inode;
1130
1131 static struct tree_descr smack_files[] = {
1132 [SMK_LOAD] =
1133 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
1134 [SMK_CIPSO] =
1135 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1136 [SMK_DOI] =
1137 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1138 [SMK_DIRECT] =
1139 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1140 [SMK_AMBIENT] =
1141 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001142 [SMK_NETLBLADDR] =
1143 {"netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
Casey Schaufler15446232008-07-30 15:37:11 -07001144 [SMK_ONLYCAP] =
1145 {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
Casey Schauflere114e472008-02-04 22:29:50 -08001146 /* last one */ {""}
1147 };
1148
1149 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1150 if (rc != 0) {
1151 printk(KERN_ERR "%s failed %d while creating inodes\n",
1152 __func__, rc);
1153 return rc;
1154 }
1155
1156 root_inode = sb->s_root->d_inode;
1157 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1158
1159 return 0;
1160}
1161
1162/**
1163 * smk_get_sb - get the smackfs superblock
1164 * @fs_type: passed along without comment
1165 * @flags: passed along without comment
1166 * @dev_name: passed along without comment
1167 * @data: passed along without comment
1168 * @mnt: passed along without comment
1169 *
1170 * Just passes everything along.
1171 *
1172 * Returns what the lower level code does.
1173 */
1174static int smk_get_sb(struct file_system_type *fs_type,
1175 int flags, const char *dev_name, void *data,
1176 struct vfsmount *mnt)
1177{
1178 return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
1179}
1180
1181static struct file_system_type smk_fs_type = {
1182 .name = "smackfs",
1183 .get_sb = smk_get_sb,
1184 .kill_sb = kill_litter_super,
1185};
1186
1187static struct vfsmount *smackfs_mount;
1188
1189/**
1190 * init_smk_fs - get the smackfs superblock
1191 *
1192 * register the smackfs
1193 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02001194 * Do not register smackfs if Smack wasn't enabled
1195 * on boot. We can not put this method normally under the
1196 * smack_init() code path since the security subsystem get
1197 * initialized before the vfs caches.
1198 *
1199 * Returns true if we were not chosen on boot or if
1200 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08001201 */
1202static int __init init_smk_fs(void)
1203{
1204 int err;
1205
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02001206 if (!security_module_enable(&smack_ops))
1207 return 0;
1208
Casey Schauflere114e472008-02-04 22:29:50 -08001209 err = register_filesystem(&smk_fs_type);
1210 if (!err) {
1211 smackfs_mount = kern_mount(&smk_fs_type);
1212 if (IS_ERR(smackfs_mount)) {
1213 printk(KERN_ERR "smackfs: could not mount!\n");
1214 err = PTR_ERR(smackfs_mount);
1215 smackfs_mount = NULL;
1216 }
1217 }
1218
Casey Schauflere114e472008-02-04 22:29:50 -08001219 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001220 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001221
1222 return err;
1223}
1224
1225__initcall(init_smk_fs);