blob: 6aceef518a41fe7cd095bf8f860afe210abeb6ce [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Casey Schaufler6d3dc072008-12-31 12:54:12 -050024#include <net/net_namespace.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <net/netlabel.h>
26#include <net/cipso_ipv4.h>
27#include <linux/seq_file.h>
28#include <linux/ctype.h>
Casey Schaufler4bc87e62008-02-15 15:24:25 -080029#include <linux/audit.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030#include "smack.h"
31
32/*
33 * smackfs pseudo filesystem.
34 */
35
36enum smk_inos {
37 SMK_ROOT_INO = 2,
38 SMK_LOAD = 3, /* load policy */
39 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
40 SMK_DOI = 5, /* CIPSO DOI */
41 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
42 SMK_AMBIENT = 7, /* internet ambient label */
Casey Schaufler6d3dc072008-12-31 12:54:12 -050043 SMK_NETLBLADDR = 8, /* single label hosts */
Casey Schaufler15446232008-07-30 15:37:11 -070044 SMK_ONLYCAP = 9, /* the only "capable" label */
Etienne Bassetecfcc532009-04-08 20:40:06 +020045 SMK_LOGGING = 10, /* logging */
Casey Schaufler7898e1f2011-01-17 08:05:27 -080046 SMK_LOAD_SELF = 11, /* task specific rules */
Jarkko Sakkinen828716c2011-09-08 10:12:01 +030047 SMK_ACCESSES = 12, /* access policy */
Casey Schauflere114e472008-02-04 22:29:50 -080048};
49
50/*
51 * List locks
52 */
53static DEFINE_MUTEX(smack_list_lock);
54static DEFINE_MUTEX(smack_cipso_lock);
Casey Schaufler4bc87e62008-02-15 15:24:25 -080055static DEFINE_MUTEX(smack_ambient_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -050056static DEFINE_MUTEX(smk_netlbladdr_lock);
Casey Schauflere114e472008-02-04 22:29:50 -080057
58/*
59 * This is the "ambient" label for network traffic.
60 * If it isn't somehow marked, use this.
61 * It can be reset via smackfs/ambient
62 */
63char *smack_net_ambient = smack_known_floor.smk_known;
64
65/*
Casey Schauflere114e472008-02-04 22:29:50 -080066 * 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 Schaufler6d3dc072008-12-31 12:54:12 -050082/*
83 * Certain IP addresses may be designated as single label hosts.
84 * Packets are sent there unlabeled, but only from tasks that
85 * can write to the specified label.
86 */
Etienne Basset7198e2e2009-03-24 20:53:24 +010087
88LIST_HEAD(smk_netlbladdr_list);
Casey Schaufler272cd7a2011-09-20 12:24:36 -070089
90/*
91 * Rule lists are maintained for each label.
92 * This master list is just for reading /smack/load.
93 */
94struct smack_master_list {
95 struct list_head list;
96 struct smack_rule *smk_rule;
97};
98
Etienne Basset7198e2e2009-03-24 20:53:24 +010099LIST_HEAD(smack_rule_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500100
Casey Schauflere114e472008-02-04 22:29:50 -0800101static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
Casey Schauflere114e472008-02-04 22:29:50 -0800102
Etienne Basset43031542009-03-27 17:11:01 -0400103const char *smack_cipso_option = SMACK_CIPSO_OPTION;
104
105
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700106#define SEQ_READ_FINISHED ((loff_t)-1)
Casey Schauflere114e472008-02-04 22:29:50 -0800107
108/*
Casey Schauflere114e472008-02-04 22:29:50 -0800109 * Values for parsing cipso rules
110 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700111 * SMK_CIPSOMIN: Minimum possible cipso rule length.
112 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -0800113 */
114#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700115#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
116#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
117
118/*
119 * Values for parsing MAC rules
120 * SMK_ACCESS: Maximum possible combination of access permissions
121 * SMK_ACCESSLEN: Maximum length for a rule access field
122 * SMK_LOADLEN: Smack rule length
123 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200124#define SMK_OACCESS "rwxa"
125#define SMK_ACCESS "rwxat"
126#define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
127#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
128#define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
129#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700130
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500131/**
132 * smk_netlabel_audit_set - fill a netlbl_audit struct
133 * @nap: structure to fill
134 */
135static void smk_netlabel_audit_set(struct netlbl_audit *nap)
136{
137 nap->loginuid = audit_get_loginuid(current);
138 nap->sessionid = audit_get_sessionid(current);
Casey Schaufler676dac42010-12-02 06:43:39 -0800139 nap->secid = smack_to_secid(smk_of_current());
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500140}
141
142/*
143 * Values for parsing single label host rules
144 * "1.2.3.4 X"
145 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
146 */
147#define SMK_NETLBLADDRMIN 9
148#define SMK_NETLBLADDRMAX 42
Casey Schauflere114e472008-02-04 22:29:50 -0800149
Casey Schauflere114e472008-02-04 22:29:50 -0800150/**
151 * smk_set_access - add a rule to the rule list
152 * @srp: the new rule to add
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800153 * @rule_list: the list of rules
154 * @rule_lock: the rule list lock
Casey Schauflere114e472008-02-04 22:29:50 -0800155 *
156 * Looks through the current subject/object/access list for
157 * the subject/object pair and replaces the access that was
158 * there. If the pair isn't found add it with the specified
159 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300160 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800161 * Returns 1 if a rule was found to exist already, 0 if it is new
Sergio Luis81ea7142008-12-22 01:16:15 -0300162 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
163 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800164 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800165static int smk_set_access(struct smack_rule *srp, struct list_head *rule_list,
166 struct mutex *rule_lock)
Casey Schauflere114e472008-02-04 22:29:50 -0800167{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100168 struct smack_rule *sp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800169 int found = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800170
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800171 mutex_lock(rule_lock);
172
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700173 /*
174 * Because the object label is less likely to match
175 * than the subject label check it first
176 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800177 list_for_each_entry_rcu(sp, rule_list, list) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700178 if (sp->smk_object == srp->smk_object &&
179 sp->smk_subject == srp->smk_subject) {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100180 found = 1;
181 sp->smk_access = srp->smk_access;
Casey Schauflere114e472008-02-04 22:29:50 -0800182 break;
183 }
Casey Schauflere114e472008-02-04 22:29:50 -0800184 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100185 if (found == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800186 list_add_rcu(&srp->list, rule_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800187
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800188 mutex_unlock(rule_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800189
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800190 return found;
Casey Schauflere114e472008-02-04 22:29:50 -0800191}
192
193/**
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300194 * smk_parse_rule - parse Smack rule from load string
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300195 * @data: string to be parsed whose size is SMK_LOADLEN
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300196 * @rule: Smack rule
197 * @import: if non-zero, import labels
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300198 */
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300199static int smk_parse_rule(const char *data, struct smack_rule *rule, int import)
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300200{
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300201 char smack[SMK_LABELLEN];
202 struct smack_known *skp;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300203
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300204 if (import) {
205 rule->smk_subject = smk_import(data, 0);
206 if (rule->smk_subject == NULL)
207 return -1;
208
209 rule->smk_object = smk_import(data + SMK_LABELLEN, 0);
210 if (rule->smk_object == NULL)
211 return -1;
212 } else {
213 smk_parse_smack(data, 0, smack);
214 skp = smk_find_entry(smack);
215 if (skp == NULL)
216 return -1;
217 rule->smk_subject = skp->smk_known;
218
219 smk_parse_smack(data + SMK_LABELLEN, 0, smack);
220 skp = smk_find_entry(smack);
221 if (skp == NULL)
222 return -1;
223 rule->smk_object = skp->smk_known;
224 }
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300225
226 rule->smk_access = 0;
227
228 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
229 case '-':
230 break;
231 case 'r':
232 case 'R':
233 rule->smk_access |= MAY_READ;
234 break;
235 default:
236 return -1;
237 }
238
239 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
240 case '-':
241 break;
242 case 'w':
243 case 'W':
244 rule->smk_access |= MAY_WRITE;
245 break;
246 default:
247 return -1;
248 }
249
250 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
251 case '-':
252 break;
253 case 'x':
254 case 'X':
255 rule->smk_access |= MAY_EXEC;
256 break;
257 default:
258 return -1;
259 }
260
261 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
262 case '-':
263 break;
264 case 'a':
265 case 'A':
266 rule->smk_access |= MAY_APPEND;
267 break;
268 default:
269 return -1;
270 }
271
272 switch (data[SMK_LABELLEN + SMK_LABELLEN + 4]) {
273 case '-':
274 break;
275 case 't':
276 case 'T':
277 rule->smk_access |= MAY_TRANSMUTE;
278 break;
279 default:
280 return -1;
281 }
282
283 return 0;
284}
285
286/**
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800287 * smk_write_load_list - write() for any /smack/load
Randy Dunlap251a2a92009-02-18 11:42:33 -0800288 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800289 * @buf: where to get the data from
290 * @count: bytes sent
291 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800292 * @rule_list: the list of rules to write to
293 * @rule_lock: lock for the rule list
Casey Schauflere114e472008-02-04 22:29:50 -0800294 *
295 * Get one smack access rule from above.
296 * The format is exactly:
297 * char subject[SMK_LABELLEN]
298 * char object[SMK_LABELLEN]
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700299 * char access[SMK_ACCESSLEN]
Casey Schauflere114e472008-02-04 22:29:50 -0800300 *
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700301 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
Casey Schauflere114e472008-02-04 22:29:50 -0800302 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800303static ssize_t smk_write_load_list(struct file *file, const char __user *buf,
304 size_t count, loff_t *ppos,
305 struct list_head *rule_list,
306 struct mutex *rule_lock)
Casey Schauflere114e472008-02-04 22:29:50 -0800307{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700308 struct smack_master_list *smlp;
309 struct smack_known *skp;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100310 struct smack_rule *rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800311 char *data;
312 int rc = -EINVAL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700313 int load = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800314
315 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800316 * No partial writes.
317 * Enough data must be present.
318 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200319 if (*ppos != 0)
320 return -EINVAL;
321 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300322 * Minor hack for backward compatibility
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200323 */
324 if (count < (SMK_OLOADLEN) || count > SMK_LOADLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800325 return -EINVAL;
326
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200327 data = kzalloc(SMK_LOADLEN, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -0800328 if (data == NULL)
329 return -ENOMEM;
330
331 if (copy_from_user(data, buf, count) != 0) {
332 rc = -EFAULT;
333 goto out;
334 }
335
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200336 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300337 * More on the minor hack for backward compatibility
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200338 */
339 if (count == (SMK_OLOADLEN))
340 data[SMK_OLOADLEN] = '-';
341
Etienne Basset7198e2e2009-03-24 20:53:24 +0100342 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
343 if (rule == NULL) {
344 rc = -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800345 goto out;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100346 }
Casey Schauflere114e472008-02-04 22:29:50 -0800347
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300348 if (smk_parse_rule(data, rule, 1))
Etienne Basset7198e2e2009-03-24 20:53:24 +0100349 goto out_free_rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800350
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700351 if (rule_list == NULL) {
352 load = 1;
353 skp = smk_find_entry(rule->smk_subject);
354 rule_list = &skp->smk_rules;
355 rule_lock = &skp->smk_rules_lock;
356 }
357
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800358 rc = count;
359 /*
360 * smk_set_access returns true if there was already a rule
361 * for the subject/object pair, and false if it was new.
362 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700363 if (!smk_set_access(rule, rule_list, rule_lock)) {
364 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
365 if (smlp != NULL) {
366 smlp->smk_rule = rule;
367 list_add_rcu(&smlp->list, &smack_rule_list);
368 } else
369 rc = -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800370 goto out;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700371 }
Casey Schauflere114e472008-02-04 22:29:50 -0800372
Etienne Basset7198e2e2009-03-24 20:53:24 +0100373out_free_rule:
374 kfree(rule);
Casey Schauflere114e472008-02-04 22:29:50 -0800375out:
376 kfree(data);
377 return rc;
378}
379
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800380
381/*
382 * Seq_file read operations for /smack/load
383 */
384
385static void *load_seq_start(struct seq_file *s, loff_t *pos)
386{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700387 struct list_head *list;
388
389 /*
390 * This is 0 the first time through.
391 */
392 if (s->index == 0)
393 s->private = &smack_rule_list;
394
395 if (s->private == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800396 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700397
398 list = s->private;
399 if (list_empty(list))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800400 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700401
402 if (s->index == 0)
403 return list->next;
404 return list;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800405}
406
407static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
408{
409 struct list_head *list = v;
410
411 if (list_is_last(list, &smack_rule_list)) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700412 s->private = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800413 return NULL;
414 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700415 s->private = list->next;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800416 return list->next;
417}
418
419static int load_seq_show(struct seq_file *s, void *v)
420{
421 struct list_head *list = v;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700422 struct smack_master_list *smlp =
423 list_entry(list, struct smack_master_list, list);
424 struct smack_rule *srp = smlp->smk_rule;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800425
426 seq_printf(s, "%s %s", (char *)srp->smk_subject,
427 (char *)srp->smk_object);
428
429 seq_putc(s, ' ');
430
431 if (srp->smk_access & MAY_READ)
432 seq_putc(s, 'r');
433 if (srp->smk_access & MAY_WRITE)
434 seq_putc(s, 'w');
435 if (srp->smk_access & MAY_EXEC)
436 seq_putc(s, 'x');
437 if (srp->smk_access & MAY_APPEND)
438 seq_putc(s, 'a');
439 if (srp->smk_access & MAY_TRANSMUTE)
440 seq_putc(s, 't');
441 if (srp->smk_access == 0)
442 seq_putc(s, '-');
443
444 seq_putc(s, '\n');
445
446 return 0;
447}
448
449static void load_seq_stop(struct seq_file *s, void *v)
450{
451 /* No-op */
452}
453
454static const struct seq_operations load_seq_ops = {
455 .start = load_seq_start,
456 .next = load_seq_next,
457 .show = load_seq_show,
458 .stop = load_seq_stop,
459};
460
461/**
462 * smk_open_load - open() for /smack/load
463 * @inode: inode structure representing file
464 * @file: "load" file pointer
465 *
466 * For reading, use load_seq_* seq_file reading operations.
467 */
468static int smk_open_load(struct inode *inode, struct file *file)
469{
470 return seq_open(file, &load_seq_ops);
471}
472
473/**
474 * smk_write_load - write() for /smack/load
475 * @file: file pointer, not actually used
476 * @buf: where to get the data from
477 * @count: bytes sent
478 * @ppos: where to start - must be 0
479 *
480 */
481static ssize_t smk_write_load(struct file *file, const char __user *buf,
482 size_t count, loff_t *ppos)
483{
484
485 /*
486 * Must have privilege.
487 * No partial writes.
488 * Enough data must be present.
489 */
490 if (!capable(CAP_MAC_ADMIN))
491 return -EPERM;
492
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700493 return smk_write_load_list(file, buf, count, ppos, NULL, NULL);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800494}
495
Casey Schauflere114e472008-02-04 22:29:50 -0800496static const struct file_operations smk_load_ops = {
497 .open = smk_open_load,
498 .read = seq_read,
499 .llseek = seq_lseek,
500 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700501 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800502};
503
504/**
505 * smk_cipso_doi - initialize the CIPSO domain
506 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700507static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800508{
509 int rc;
510 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500511 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800512
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500513 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800514
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500515 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800516 if (rc != 0)
517 printk(KERN_WARNING "%s:%d remove rc = %d\n",
518 __func__, __LINE__, rc);
519
520 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
521 if (doip == NULL)
522 panic("smack: Failed to initialize cipso DOI.\n");
523 doip->map.std = NULL;
524 doip->doi = smk_cipso_doi_value;
525 doip->type = CIPSO_V4_MAP_PASS;
526 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
527 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
528 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
529
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500530 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400531 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500532 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800533 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400534 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500535 return;
536 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500537 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500538 if (rc != 0) {
539 printk(KERN_WARNING "%s:%d map add rc = %d\n",
540 __func__, __LINE__, rc);
541 kfree(doip);
542 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400543 }
Casey Schauflere114e472008-02-04 22:29:50 -0800544}
545
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800546/**
547 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800548 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800549 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700550static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800551{
552 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500553 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800554
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500555 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800556
557 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500558 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800559 if (rc != 0)
560 printk(KERN_WARNING "%s:%d remove rc = %d\n",
561 __func__, __LINE__, rc);
562 }
563
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500564 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
565 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800566 if (rc != 0)
567 printk(KERN_WARNING "%s:%d add rc = %d\n",
568 __func__, __LINE__, rc);
569}
570
Casey Schauflere114e472008-02-04 22:29:50 -0800571/*
572 * Seq_file read operations for /smack/cipso
573 */
574
575static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
576{
577 if (*pos == SEQ_READ_FINISHED)
578 return NULL;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100579 if (list_empty(&smack_known_list))
580 return NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800581
Etienne Basset7198e2e2009-03-24 20:53:24 +0100582 return smack_known_list.next;
Casey Schauflere114e472008-02-04 22:29:50 -0800583}
584
585static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
586{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100587 struct list_head *list = v;
Casey Schauflere114e472008-02-04 22:29:50 -0800588
589 /*
Etienne Basset7198e2e2009-03-24 20:53:24 +0100590 * labels with no associated cipso value wont be printed
591 * in cipso_seq_show
Casey Schauflere114e472008-02-04 22:29:50 -0800592 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100593 if (list_is_last(list, &smack_known_list)) {
Casey Schauflere114e472008-02-04 22:29:50 -0800594 *pos = SEQ_READ_FINISHED;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100595 return NULL;
596 }
Casey Schauflere114e472008-02-04 22:29:50 -0800597
Etienne Basset7198e2e2009-03-24 20:53:24 +0100598 return list->next;
Casey Schauflere114e472008-02-04 22:29:50 -0800599}
600
601/*
602 * Print cipso labels in format:
603 * label level[/cat[,cat]]
604 */
605static int cipso_seq_show(struct seq_file *s, void *v)
606{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100607 struct list_head *list = v;
608 struct smack_known *skp =
609 list_entry(list, struct smack_known, list);
Casey Schauflere114e472008-02-04 22:29:50 -0800610 struct smack_cipso *scp = skp->smk_cipso;
611 char *cbp;
612 char sep = '/';
613 int cat = 1;
614 int i;
615 unsigned char m;
616
617 if (scp == NULL)
618 return 0;
619
620 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
621
622 cbp = scp->smk_catset;
623 for (i = 0; i < SMK_LABELLEN; i++)
624 for (m = 0x80; m != 0; m >>= 1) {
625 if (m & cbp[i]) {
626 seq_printf(s, "%c%d", sep, cat);
627 sep = ',';
628 }
629 cat++;
630 }
631
632 seq_putc(s, '\n');
633
634 return 0;
635}
636
637static void cipso_seq_stop(struct seq_file *s, void *v)
638{
639 /* No-op */
640}
641
James Morris88e9d342009-09-22 16:43:43 -0700642static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800643 .start = cipso_seq_start,
644 .stop = cipso_seq_stop,
645 .next = cipso_seq_next,
646 .show = cipso_seq_show,
647};
648
649/**
650 * smk_open_cipso - open() for /smack/cipso
651 * @inode: inode structure representing file
652 * @file: "cipso" file pointer
653 *
654 * Connect our cipso_seq_* operations with /smack/cipso
655 * file_operations
656 */
657static int smk_open_cipso(struct inode *inode, struct file *file)
658{
659 return seq_open(file, &cipso_seq_ops);
660}
661
662/**
663 * smk_write_cipso - write() for /smack/cipso
Randy Dunlap251a2a92009-02-18 11:42:33 -0800664 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800665 * @buf: where to get the data from
666 * @count: bytes sent
667 * @ppos: where to start
668 *
669 * Accepts only one cipso rule per write call.
670 * Returns number of bytes written or error code, as appropriate
671 */
672static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
673 size_t count, loff_t *ppos)
674{
675 struct smack_known *skp;
676 struct smack_cipso *scp = NULL;
677 char mapcatset[SMK_LABELLEN];
678 int maplevel;
679 int cat;
680 int catlen;
681 ssize_t rc = -EINVAL;
682 char *data = NULL;
683 char *rule;
684 int ret;
685 int i;
686
687 /*
688 * Must have privilege.
689 * No partial writes.
690 * Enough data must be present.
691 */
692 if (!capable(CAP_MAC_ADMIN))
693 return -EPERM;
694 if (*ppos != 0)
695 return -EINVAL;
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700696 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
Casey Schauflere114e472008-02-04 22:29:50 -0800697 return -EINVAL;
698
699 data = kzalloc(count + 1, GFP_KERNEL);
700 if (data == NULL)
701 return -ENOMEM;
702
703 if (copy_from_user(data, buf, count) != 0) {
704 rc = -EFAULT;
705 goto unlockedout;
706 }
707
Etienne Basset43031542009-03-27 17:11:01 -0400708 /* labels cannot begin with a '-' */
709 if (data[0] == '-') {
710 rc = -EINVAL;
711 goto unlockedout;
712 }
Casey Schauflere114e472008-02-04 22:29:50 -0800713 data[count] = '\0';
714 rule = data;
715 /*
716 * Only allow one writer at a time. Writes should be
717 * quite rare and small in any case.
718 */
719 mutex_lock(&smack_cipso_lock);
720
721 skp = smk_import_entry(rule, 0);
722 if (skp == NULL)
723 goto out;
724
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800725 rule += SMK_LABELLEN;
Casey Schauflere114e472008-02-04 22:29:50 -0800726 ret = sscanf(rule, "%d", &maplevel);
727 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
728 goto out;
729
730 rule += SMK_DIGITLEN;
731 ret = sscanf(rule, "%d", &catlen);
732 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
733 goto out;
734
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700735 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800736 goto out;
737
738 memset(mapcatset, 0, sizeof(mapcatset));
739
740 for (i = 0; i < catlen; i++) {
741 rule += SMK_DIGITLEN;
742 ret = sscanf(rule, "%d", &cat);
743 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
744 goto out;
745
746 smack_catset_bit(cat, mapcatset);
747 }
748
749 if (skp->smk_cipso == NULL) {
750 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
751 if (scp == NULL) {
752 rc = -ENOMEM;
753 goto out;
754 }
755 }
756
757 spin_lock_bh(&skp->smk_cipsolock);
758
759 if (scp == NULL)
760 scp = skp->smk_cipso;
761 else
762 skp->smk_cipso = scp;
763
764 scp->smk_level = maplevel;
765 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
766
767 spin_unlock_bh(&skp->smk_cipsolock);
768
769 rc = count;
770out:
771 mutex_unlock(&smack_cipso_lock);
772unlockedout:
773 kfree(data);
774 return rc;
775}
776
777static const struct file_operations smk_cipso_ops = {
778 .open = smk_open_cipso,
779 .read = seq_read,
780 .llseek = seq_lseek,
781 .write = smk_write_cipso,
782 .release = seq_release,
783};
784
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500785/*
786 * Seq_file read operations for /smack/netlabel
787 */
788
789static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
790{
791 if (*pos == SEQ_READ_FINISHED)
792 return NULL;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100793 if (list_empty(&smk_netlbladdr_list))
794 return NULL;
795 return smk_netlbladdr_list.next;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500796}
797
798static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
799{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100800 struct list_head *list = v;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500801
Etienne Basset7198e2e2009-03-24 20:53:24 +0100802 if (list_is_last(list, &smk_netlbladdr_list)) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500803 *pos = SEQ_READ_FINISHED;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100804 return NULL;
805 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500806
Etienne Basset7198e2e2009-03-24 20:53:24 +0100807 return list->next;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500808}
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500809#define BEBITS (sizeof(__be32) * 8)
810
811/*
812 * Print host/label pairs
813 */
814static int netlbladdr_seq_show(struct seq_file *s, void *v)
815{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100816 struct list_head *list = v;
817 struct smk_netlbladdr *skp =
818 list_entry(list, struct smk_netlbladdr, list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500819 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +0100820 int maskn;
821 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500822
etienne113a0e42009-03-04 07:33:51 +0100823 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500824
825 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
826 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
827
828 return 0;
829}
830
831static void netlbladdr_seq_stop(struct seq_file *s, void *v)
832{
833 /* No-op */
834}
835
James Morris88e9d342009-09-22 16:43:43 -0700836static const struct seq_operations netlbladdr_seq_ops = {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500837 .start = netlbladdr_seq_start,
838 .stop = netlbladdr_seq_stop,
839 .next = netlbladdr_seq_next,
840 .show = netlbladdr_seq_show,
841};
842
843/**
844 * smk_open_netlbladdr - open() for /smack/netlabel
845 * @inode: inode structure representing file
846 * @file: "netlabel" file pointer
847 *
848 * Connect our netlbladdr_seq_* operations with /smack/netlabel
849 * file_operations
850 */
851static int smk_open_netlbladdr(struct inode *inode, struct file *file)
852{
853 return seq_open(file, &netlbladdr_seq_ops);
854}
855
856/**
etienne113a0e42009-03-04 07:33:51 +0100857 * smk_netlbladdr_insert
858 * @new : netlabel to insert
859 *
860 * This helper insert netlabel in the smack_netlbladdrs list
861 * sorted by netmask length (longest to smallest)
Etienne Basset7198e2e2009-03-24 20:53:24 +0100862 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
863 *
etienne113a0e42009-03-04 07:33:51 +0100864 */
865static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
866{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100867 struct smk_netlbladdr *m, *m_next;
etienne113a0e42009-03-04 07:33:51 +0100868
Etienne Basset7198e2e2009-03-24 20:53:24 +0100869 if (list_empty(&smk_netlbladdr_list)) {
870 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +0100871 return;
872 }
873
Jiri Pirko05725f72009-04-14 20:17:16 +0200874 m = list_entry_rcu(smk_netlbladdr_list.next,
875 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +0100876
etienne113a0e42009-03-04 07:33:51 +0100877 /* the comparison '>' is a bit hacky, but works */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100878 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
879 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +0100880 return;
881 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100882
883 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
884 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
885 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +0100886 return;
887 }
Jiri Pirko05725f72009-04-14 20:17:16 +0200888 m_next = list_entry_rcu(m->list.next,
889 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +0100890 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
891 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +0100892 return;
893 }
894 }
895}
896
897
898/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500899 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -0800900 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500901 * @buf: where to get the data from
902 * @count: bytes sent
903 * @ppos: where to start
904 *
905 * Accepts only one netlbladdr per write call.
906 * Returns number of bytes written or error code, as appropriate
907 */
908static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
909 size_t count, loff_t *ppos)
910{
911 struct smk_netlbladdr *skp;
912 struct sockaddr_in newname;
913 char smack[SMK_LABELLEN];
914 char *sp;
Roel Kluin6470c072009-05-21 18:42:54 +0200915 char data[SMK_NETLBLADDRMAX + 1];
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500916 char *host = (char *)&newname.sin_addr.s_addr;
917 int rc;
918 struct netlbl_audit audit_info;
919 struct in_addr mask;
920 unsigned int m;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100921 int found;
etienne113a0e42009-03-04 07:33:51 +0100922 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500923 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +0100924 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500925
926 /*
927 * Must have privilege.
928 * No partial writes.
929 * Enough data must be present.
930 * "<addr/mask, as a.b.c.d/e><space><label>"
931 * "<addr, as a.b.c.d><space><label>"
932 */
933 if (!capable(CAP_MAC_ADMIN))
934 return -EPERM;
935 if (*ppos != 0)
936 return -EINVAL;
937 if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
938 return -EINVAL;
939 if (copy_from_user(data, buf, count) != 0)
940 return -EFAULT;
941
942 data[count] = '\0';
943
944 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
945 &host[0], &host[1], &host[2], &host[3], &m, smack);
946 if (rc != 6) {
947 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
948 &host[0], &host[1], &host[2], &host[3], smack);
949 if (rc != 5)
950 return -EINVAL;
951 m = BEBITS;
952 }
953 if (m > BEBITS)
954 return -EINVAL;
955
Etienne Basset43031542009-03-27 17:11:01 -0400956 /* if smack begins with '-', its an option, don't import it */
957 if (smack[0] != '-') {
958 sp = smk_import(smack, 0);
959 if (sp == NULL)
960 return -EINVAL;
961 } else {
962 /* check known options */
963 if (strcmp(smack, smack_cipso_option) == 0)
964 sp = (char *)smack_cipso_option;
965 else
966 return -EINVAL;
967 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500968
etienne113a0e42009-03-04 07:33:51 +0100969 for (temp_mask = 0; m > 0; m--) {
970 temp_mask |= mask_bits;
971 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500972 }
etienne113a0e42009-03-04 07:33:51 +0100973 mask.s_addr = cpu_to_be32(temp_mask);
974
975 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500976 /*
977 * Only allow one writer at a time. Writes should be
978 * quite rare and small in any case.
979 */
980 mutex_lock(&smk_netlbladdr_lock);
981
982 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +0100983 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100984 found = 0;
985 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500986 if (skp->smk_host.sin_addr.s_addr == nsa &&
Etienne Basset7198e2e2009-03-24 20:53:24 +0100987 skp->smk_mask.s_addr == mask.s_addr) {
988 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500989 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100990 }
991 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500992 smk_netlabel_audit_set(&audit_info);
993
Etienne Basset7198e2e2009-03-24 20:53:24 +0100994 if (found == 0) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500995 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
996 if (skp == NULL)
997 rc = -ENOMEM;
998 else {
999 rc = 0;
1000 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1001 skp->smk_mask.s_addr = mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001002 skp->smk_label = sp;
etienne113a0e42009-03-04 07:33:51 +01001003 smk_netlbladdr_insert(skp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001004 }
1005 } else {
Etienne Basset43031542009-03-27 17:11:01 -04001006 /* we delete the unlabeled entry, only if the previous label
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001007 * wasn't the special CIPSO option */
Etienne Basset43031542009-03-27 17:11:01 -04001008 if (skp->smk_label != smack_cipso_option)
1009 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1010 &skp->smk_host.sin_addr, &skp->smk_mask,
1011 PF_INET, &audit_info);
1012 else
1013 rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001014 skp->smk_label = sp;
1015 }
1016
1017 /*
1018 * Now tell netlabel about the single label nature of
1019 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -04001020 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001021 */
Etienne Basset43031542009-03-27 17:11:01 -04001022 if (rc == 0 && sp != smack_cipso_option)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001023 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1024 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
1025 smack_to_secid(skp->smk_label), &audit_info);
1026
1027 if (rc == 0)
1028 rc = count;
1029
1030 mutex_unlock(&smk_netlbladdr_lock);
1031
1032 return rc;
1033}
1034
1035static const struct file_operations smk_netlbladdr_ops = {
1036 .open = smk_open_netlbladdr,
1037 .read = seq_read,
1038 .llseek = seq_lseek,
1039 .write = smk_write_netlbladdr,
1040 .release = seq_release,
1041};
1042
Casey Schauflere114e472008-02-04 22:29:50 -08001043/**
1044 * smk_read_doi - read() for /smack/doi
1045 * @filp: file pointer, not actually used
1046 * @buf: where to put the result
1047 * @count: maximum to send along
1048 * @ppos: where to start
1049 *
1050 * Returns number of bytes read or error code, as appropriate
1051 */
1052static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1053 size_t count, loff_t *ppos)
1054{
1055 char temp[80];
1056 ssize_t rc;
1057
1058 if (*ppos != 0)
1059 return 0;
1060
1061 sprintf(temp, "%d", smk_cipso_doi_value);
1062 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1063
1064 return rc;
1065}
1066
1067/**
1068 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001069 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001070 * @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_doi(struct file *file, const char __user *buf,
1077 size_t count, loff_t *ppos)
1078{
1079 char temp[80];
1080 int i;
1081
1082 if (!capable(CAP_MAC_ADMIN))
1083 return -EPERM;
1084
1085 if (count >= sizeof(temp) || count == 0)
1086 return -EINVAL;
1087
1088 if (copy_from_user(temp, buf, count) != 0)
1089 return -EFAULT;
1090
1091 temp[count] = '\0';
1092
1093 if (sscanf(temp, "%d", &i) != 1)
1094 return -EINVAL;
1095
1096 smk_cipso_doi_value = i;
1097
1098 smk_cipso_doi();
1099
1100 return count;
1101}
1102
1103static const struct file_operations smk_doi_ops = {
1104 .read = smk_read_doi,
1105 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001106 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001107};
1108
1109/**
1110 * smk_read_direct - read() for /smack/direct
1111 * @filp: file pointer, not actually used
1112 * @buf: where to put the result
1113 * @count: maximum to send along
1114 * @ppos: where to start
1115 *
1116 * Returns number of bytes read or error code, as appropriate
1117 */
1118static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1119 size_t count, loff_t *ppos)
1120{
1121 char temp[80];
1122 ssize_t rc;
1123
1124 if (*ppos != 0)
1125 return 0;
1126
1127 sprintf(temp, "%d", smack_cipso_direct);
1128 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1129
1130 return rc;
1131}
1132
1133/**
1134 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001135 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001136 * @buf: where to get the data from
1137 * @count: bytes sent
1138 * @ppos: where to start
1139 *
1140 * Returns number of bytes written or error code, as appropriate
1141 */
1142static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1143 size_t count, loff_t *ppos)
1144{
1145 char temp[80];
1146 int i;
1147
1148 if (!capable(CAP_MAC_ADMIN))
1149 return -EPERM;
1150
1151 if (count >= sizeof(temp) || count == 0)
1152 return -EINVAL;
1153
1154 if (copy_from_user(temp, buf, count) != 0)
1155 return -EFAULT;
1156
1157 temp[count] = '\0';
1158
1159 if (sscanf(temp, "%d", &i) != 1)
1160 return -EINVAL;
1161
1162 smack_cipso_direct = i;
1163
1164 return count;
1165}
1166
1167static const struct file_operations smk_direct_ops = {
1168 .read = smk_read_direct,
1169 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001170 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001171};
1172
1173/**
1174 * smk_read_ambient - read() for /smack/ambient
1175 * @filp: file pointer, not actually used
1176 * @buf: where to put the result
1177 * @cn: maximum to send along
1178 * @ppos: where to start
1179 *
1180 * Returns number of bytes read or error code, as appropriate
1181 */
1182static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1183 size_t cn, loff_t *ppos)
1184{
1185 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001186 int asize;
1187
1188 if (*ppos != 0)
1189 return 0;
1190 /*
1191 * Being careful to avoid a problem in the case where
1192 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001193 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001194 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001195
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001196 asize = strlen(smack_net_ambient) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001197
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001198 if (cn >= asize)
1199 rc = simple_read_from_buffer(buf, cn, ppos,
1200 smack_net_ambient, asize);
1201 else
1202 rc = -EINVAL;
1203
1204 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001205
1206 return rc;
1207}
1208
1209/**
1210 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001211 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001212 * @buf: where to get the data from
1213 * @count: bytes sent
1214 * @ppos: where to start
1215 *
1216 * Returns number of bytes written or error code, as appropriate
1217 */
1218static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1219 size_t count, loff_t *ppos)
1220{
1221 char in[SMK_LABELLEN];
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001222 char *oldambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001223 char *smack;
1224
1225 if (!capable(CAP_MAC_ADMIN))
1226 return -EPERM;
1227
1228 if (count >= SMK_LABELLEN)
1229 return -EINVAL;
1230
1231 if (copy_from_user(in, buf, count) != 0)
1232 return -EFAULT;
1233
1234 smack = smk_import(in, count);
1235 if (smack == NULL)
1236 return -EINVAL;
1237
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001238 mutex_lock(&smack_ambient_lock);
1239
1240 oldambient = smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001241 smack_net_ambient = smack;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001242 smk_unlbl_ambient(oldambient);
1243
1244 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001245
1246 return count;
1247}
1248
1249static const struct file_operations smk_ambient_ops = {
1250 .read = smk_read_ambient,
1251 .write = smk_write_ambient,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001252 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001253};
1254
Casey Schaufler15446232008-07-30 15:37:11 -07001255/**
1256 * smk_read_onlycap - read() for /smack/onlycap
1257 * @filp: file pointer, not actually used
1258 * @buf: where to put the result
1259 * @cn: maximum to send along
1260 * @ppos: where to start
1261 *
1262 * Returns number of bytes read or error code, as appropriate
1263 */
1264static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1265 size_t cn, loff_t *ppos)
1266{
1267 char *smack = "";
1268 ssize_t rc = -EINVAL;
1269 int asize;
1270
1271 if (*ppos != 0)
1272 return 0;
1273
1274 if (smack_onlycap != NULL)
1275 smack = smack_onlycap;
1276
1277 asize = strlen(smack) + 1;
1278
1279 if (cn >= asize)
1280 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1281
1282 return rc;
1283}
1284
1285/**
1286 * smk_write_onlycap - write() for /smack/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001287 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001288 * @buf: where to get the data from
1289 * @count: bytes sent
1290 * @ppos: where to start
1291 *
1292 * Returns number of bytes written or error code, as appropriate
1293 */
1294static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1295 size_t count, loff_t *ppos)
1296{
1297 char in[SMK_LABELLEN];
Casey Schaufler676dac42010-12-02 06:43:39 -08001298 char *sp = smk_of_task(current->cred->security);
Casey Schaufler15446232008-07-30 15:37:11 -07001299
1300 if (!capable(CAP_MAC_ADMIN))
1301 return -EPERM;
1302
1303 /*
1304 * This can be done using smk_access() but is done
1305 * explicitly for clarity. The smk_access() implementation
1306 * would use smk_access(smack_onlycap, MAY_WRITE)
1307 */
1308 if (smack_onlycap != NULL && smack_onlycap != sp)
1309 return -EPERM;
1310
1311 if (count >= SMK_LABELLEN)
1312 return -EINVAL;
1313
1314 if (copy_from_user(in, buf, count) != 0)
1315 return -EFAULT;
1316
1317 /*
1318 * Should the null string be passed in unset the onlycap value.
1319 * This seems like something to be careful with as usually
1320 * smk_import only expects to return NULL for errors. It
1321 * is usually the case that a nullstring or "\n" would be
1322 * bad to pass to smk_import but in fact this is useful here.
1323 */
1324 smack_onlycap = smk_import(in, count);
1325
1326 return count;
1327}
1328
1329static const struct file_operations smk_onlycap_ops = {
1330 .read = smk_read_onlycap,
1331 .write = smk_write_onlycap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001332 .llseek = default_llseek,
Casey Schaufler15446232008-07-30 15:37:11 -07001333};
1334
Casey Schauflere114e472008-02-04 22:29:50 -08001335/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001336 * smk_read_logging - read() for /smack/logging
1337 * @filp: file pointer, not actually used
1338 * @buf: where to put the result
1339 * @cn: maximum to send along
1340 * @ppos: where to start
1341 *
1342 * Returns number of bytes read or error code, as appropriate
1343 */
1344static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1345 size_t count, loff_t *ppos)
1346{
1347 char temp[32];
1348 ssize_t rc;
1349
1350 if (*ppos != 0)
1351 return 0;
1352
1353 sprintf(temp, "%d\n", log_policy);
1354 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1355 return rc;
1356}
1357
1358/**
1359 * smk_write_logging - write() for /smack/logging
1360 * @file: file pointer, not actually used
1361 * @buf: where to get the data from
1362 * @count: bytes sent
1363 * @ppos: where to start
1364 *
1365 * Returns number of bytes written or error code, as appropriate
1366 */
1367static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1368 size_t count, loff_t *ppos)
1369{
1370 char temp[32];
1371 int i;
1372
1373 if (!capable(CAP_MAC_ADMIN))
1374 return -EPERM;
1375
1376 if (count >= sizeof(temp) || count == 0)
1377 return -EINVAL;
1378
1379 if (copy_from_user(temp, buf, count) != 0)
1380 return -EFAULT;
1381
1382 temp[count] = '\0';
1383
1384 if (sscanf(temp, "%d", &i) != 1)
1385 return -EINVAL;
1386 if (i < 0 || i > 3)
1387 return -EINVAL;
1388 log_policy = i;
1389 return count;
1390}
1391
1392
1393
1394static const struct file_operations smk_logging_ops = {
1395 .read = smk_read_logging,
1396 .write = smk_write_logging,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001397 .llseek = default_llseek,
Etienne Bassetecfcc532009-04-08 20:40:06 +02001398};
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001399
1400/*
1401 * Seq_file read operations for /smack/load-self
1402 */
1403
1404static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1405{
1406 struct task_smack *tsp = current_security();
1407
1408 if (*pos == SEQ_READ_FINISHED)
1409 return NULL;
1410 if (list_empty(&tsp->smk_rules))
1411 return NULL;
1412 return tsp->smk_rules.next;
1413}
1414
1415static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1416{
1417 struct task_smack *tsp = current_security();
1418 struct list_head *list = v;
1419
1420 if (list_is_last(list, &tsp->smk_rules)) {
1421 *pos = SEQ_READ_FINISHED;
1422 return NULL;
1423 }
1424 return list->next;
1425}
1426
1427static int load_self_seq_show(struct seq_file *s, void *v)
1428{
1429 struct list_head *list = v;
1430 struct smack_rule *srp =
1431 list_entry(list, struct smack_rule, list);
1432
1433 seq_printf(s, "%s %s", (char *)srp->smk_subject,
1434 (char *)srp->smk_object);
1435
1436 seq_putc(s, ' ');
1437
1438 if (srp->smk_access & MAY_READ)
1439 seq_putc(s, 'r');
1440 if (srp->smk_access & MAY_WRITE)
1441 seq_putc(s, 'w');
1442 if (srp->smk_access & MAY_EXEC)
1443 seq_putc(s, 'x');
1444 if (srp->smk_access & MAY_APPEND)
1445 seq_putc(s, 'a');
1446 if (srp->smk_access & MAY_TRANSMUTE)
1447 seq_putc(s, 't');
1448 if (srp->smk_access == 0)
1449 seq_putc(s, '-');
1450
1451 seq_putc(s, '\n');
1452
1453 return 0;
1454}
1455
1456static void load_self_seq_stop(struct seq_file *s, void *v)
1457{
1458 /* No-op */
1459}
1460
1461static const struct seq_operations load_self_seq_ops = {
1462 .start = load_self_seq_start,
1463 .next = load_self_seq_next,
1464 .show = load_self_seq_show,
1465 .stop = load_self_seq_stop,
1466};
1467
1468
1469/**
1470 * smk_open_load_self - open() for /smack/load-self
1471 * @inode: inode structure representing file
1472 * @file: "load" file pointer
1473 *
1474 * For reading, use load_seq_* seq_file reading operations.
1475 */
1476static int smk_open_load_self(struct inode *inode, struct file *file)
1477{
1478 return seq_open(file, &load_self_seq_ops);
1479}
1480
1481/**
1482 * smk_write_load_self - write() for /smack/load-self
1483 * @file: file pointer, not actually used
1484 * @buf: where to get the data from
1485 * @count: bytes sent
1486 * @ppos: where to start - must be 0
1487 *
1488 */
1489static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1490 size_t count, loff_t *ppos)
1491{
1492 struct task_smack *tsp = current_security();
1493
1494 return smk_write_load_list(file, buf, count, ppos, &tsp->smk_rules,
1495 &tsp->smk_rules_lock);
1496}
1497
1498static const struct file_operations smk_load_self_ops = {
1499 .open = smk_open_load_self,
1500 .read = seq_read,
1501 .llseek = seq_lseek,
1502 .write = smk_write_load_self,
1503 .release = seq_release,
1504};
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001505
1506/**
1507 * smk_write_access - handle access check transaction
1508 * @file: file pointer
1509 * @buf: data from user space
1510 * @count: bytes sent
1511 * @ppos: where to start - must be 0
1512 */
1513static ssize_t smk_write_access(struct file *file, const char __user *buf,
1514 size_t count, loff_t *ppos)
1515{
1516 struct smack_rule rule;
1517 char *data;
Jarkko Sakkinenf8859d92011-10-10 14:29:28 +03001518 int res;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001519
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001520 data = simple_transaction_get(file, buf, count);
1521 if (IS_ERR(data))
1522 return PTR_ERR(data);
1523
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03001524 if (count < SMK_LOADLEN || smk_parse_rule(data, &rule, 0))
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001525 return -EINVAL;
1526
Jarkko Sakkinenf8859d92011-10-10 14:29:28 +03001527 res = smk_access(rule.smk_subject, rule.smk_object, rule.smk_access,
1528 NULL);
1529 data[0] = res == 0 ? '1' : '0';
1530 data[1] = '\0';
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001531
Jarkko Sakkinend86b2b62011-10-18 14:34:28 +03001532 simple_transaction_set(file, 2);
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001533 return SMK_LOADLEN;
1534}
1535
1536static const struct file_operations smk_access_ops = {
1537 .write = smk_write_access,
1538 .read = simple_transaction_read,
1539 .release = simple_transaction_release,
1540 .llseek = generic_file_llseek,
1541};
1542
Etienne Bassetecfcc532009-04-08 20:40:06 +02001543/**
Casey Schauflere114e472008-02-04 22:29:50 -08001544 * smk_fill_super - fill the /smackfs superblock
1545 * @sb: the empty superblock
1546 * @data: unused
1547 * @silent: unused
1548 *
1549 * Fill in the well known entries for /smack
1550 *
1551 * Returns 0 on success, an error code on failure
1552 */
1553static int smk_fill_super(struct super_block *sb, void *data, int silent)
1554{
1555 int rc;
1556 struct inode *root_inode;
1557
1558 static struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001559 [SMK_LOAD] = {
1560 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
1561 [SMK_CIPSO] = {
1562 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1563 [SMK_DOI] = {
1564 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1565 [SMK_DIRECT] = {
1566 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1567 [SMK_AMBIENT] = {
1568 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
1569 [SMK_NETLBLADDR] = {
1570 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
1571 [SMK_ONLYCAP] = {
1572 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
1573 [SMK_LOGGING] = {
1574 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
1575 [SMK_LOAD_SELF] = {
1576 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001577 [SMK_ACCESSES] = {
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03001578 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001579 /* last one */
1580 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08001581 };
1582
1583 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1584 if (rc != 0) {
1585 printk(KERN_ERR "%s failed %d while creating inodes\n",
1586 __func__, rc);
1587 return rc;
1588 }
1589
1590 root_inode = sb->s_root->d_inode;
1591 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1592
1593 return 0;
1594}
1595
1596/**
Al Virofc14f2f2010-07-25 01:48:30 +04001597 * smk_mount - get the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08001598 * @fs_type: passed along without comment
1599 * @flags: passed along without comment
1600 * @dev_name: passed along without comment
1601 * @data: passed along without comment
Casey Schauflere114e472008-02-04 22:29:50 -08001602 *
1603 * Just passes everything along.
1604 *
1605 * Returns what the lower level code does.
1606 */
Al Virofc14f2f2010-07-25 01:48:30 +04001607static struct dentry *smk_mount(struct file_system_type *fs_type,
1608 int flags, const char *dev_name, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -08001609{
Al Virofc14f2f2010-07-25 01:48:30 +04001610 return mount_single(fs_type, flags, data, smk_fill_super);
Casey Schauflere114e472008-02-04 22:29:50 -08001611}
1612
1613static struct file_system_type smk_fs_type = {
1614 .name = "smackfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001615 .mount = smk_mount,
Casey Schauflere114e472008-02-04 22:29:50 -08001616 .kill_sb = kill_litter_super,
1617};
1618
1619static struct vfsmount *smackfs_mount;
1620
1621/**
1622 * init_smk_fs - get the smackfs superblock
1623 *
1624 * register the smackfs
1625 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02001626 * Do not register smackfs if Smack wasn't enabled
1627 * on boot. We can not put this method normally under the
1628 * smack_init() code path since the security subsystem get
1629 * initialized before the vfs caches.
1630 *
1631 * Returns true if we were not chosen on boot or if
1632 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08001633 */
1634static int __init init_smk_fs(void)
1635{
1636 int err;
1637
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02001638 if (!security_module_enable(&smack_ops))
1639 return 0;
1640
Casey Schauflere114e472008-02-04 22:29:50 -08001641 err = register_filesystem(&smk_fs_type);
1642 if (!err) {
1643 smackfs_mount = kern_mount(&smk_fs_type);
1644 if (IS_ERR(smackfs_mount)) {
1645 printk(KERN_ERR "smackfs: could not mount!\n");
1646 err = PTR_ERR(smackfs_mount);
1647 smackfs_mount = NULL;
1648 }
1649 }
1650
Casey Schauflere114e472008-02-04 22:29:50 -08001651 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001652 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001653
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001654 mutex_init(&smack_known_floor.smk_rules_lock);
1655 mutex_init(&smack_known_hat.smk_rules_lock);
1656 mutex_init(&smack_known_huh.smk_rules_lock);
1657 mutex_init(&smack_known_invalid.smk_rules_lock);
1658 mutex_init(&smack_known_star.smk_rules_lock);
1659 mutex_init(&smack_known_web.smk_rules_lock);
1660
1661 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
1662 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
1663 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
1664 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
1665 INIT_LIST_HEAD(&smack_known_star.smk_rules);
1666 INIT_LIST_HEAD(&smack_known_web.smk_rules);
1667
Casey Schauflere114e472008-02-04 22:29:50 -08001668 return err;
1669}
1670
1671__initcall(init_smk_fs);