blob: f4c28eeba1b18d2c64fdfd3b4679900026953052 [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);
89LIST_HEAD(smack_rule_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -050090
Casey Schauflere114e472008-02-04 22:29:50 -080091static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
Casey Schauflere114e472008-02-04 22:29:50 -080092
Etienne Basset43031542009-03-27 17:11:01 -040093const char *smack_cipso_option = SMACK_CIPSO_OPTION;
94
95
Casey Schauflere114e472008-02-04 22:29:50 -080096#define SEQ_READ_FINISHED 1
97
98/*
Casey Schauflere114e472008-02-04 22:29:50 -080099 * Values for parsing cipso rules
100 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700101 * SMK_CIPSOMIN: Minimum possible cipso rule length.
102 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -0800103 */
104#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700105#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
106#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
107
108/*
109 * Values for parsing MAC rules
110 * SMK_ACCESS: Maximum possible combination of access permissions
111 * SMK_ACCESSLEN: Maximum length for a rule access field
112 * SMK_LOADLEN: Smack rule length
113 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200114#define SMK_OACCESS "rwxa"
115#define SMK_ACCESS "rwxat"
116#define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
117#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
118#define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
119#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700120
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500121/**
122 * smk_netlabel_audit_set - fill a netlbl_audit struct
123 * @nap: structure to fill
124 */
125static void smk_netlabel_audit_set(struct netlbl_audit *nap)
126{
127 nap->loginuid = audit_get_loginuid(current);
128 nap->sessionid = audit_get_sessionid(current);
Casey Schaufler676dac42010-12-02 06:43:39 -0800129 nap->secid = smack_to_secid(smk_of_current());
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500130}
131
132/*
133 * Values for parsing single label host rules
134 * "1.2.3.4 X"
135 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
136 */
137#define SMK_NETLBLADDRMIN 9
138#define SMK_NETLBLADDRMAX 42
Casey Schauflere114e472008-02-04 22:29:50 -0800139
Casey Schauflere114e472008-02-04 22:29:50 -0800140/**
141 * smk_set_access - add a rule to the rule list
142 * @srp: the new rule to add
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800143 * @rule_list: the list of rules
144 * @rule_lock: the rule list lock
Casey Schauflere114e472008-02-04 22:29:50 -0800145 *
146 * Looks through the current subject/object/access list for
147 * the subject/object pair and replaces the access that was
148 * there. If the pair isn't found add it with the specified
149 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300150 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800151 * Returns 1 if a rule was found to exist already, 0 if it is new
Sergio Luis81ea7142008-12-22 01:16:15 -0300152 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
153 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800154 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800155static int smk_set_access(struct smack_rule *srp, struct list_head *rule_list,
156 struct mutex *rule_lock)
Casey Schauflere114e472008-02-04 22:29:50 -0800157{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100158 struct smack_rule *sp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800159 int found = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800160
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800161 mutex_lock(rule_lock);
162
163 list_for_each_entry_rcu(sp, rule_list, list) {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100164 if (sp->smk_subject == srp->smk_subject &&
165 sp->smk_object == srp->smk_object) {
166 found = 1;
167 sp->smk_access = srp->smk_access;
Casey Schauflere114e472008-02-04 22:29:50 -0800168 break;
169 }
Casey Schauflere114e472008-02-04 22:29:50 -0800170 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100171 if (found == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800172 list_add_rcu(&srp->list, rule_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800173
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800174 mutex_unlock(rule_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800175
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800176 return found;
Casey Schauflere114e472008-02-04 22:29:50 -0800177}
178
179/**
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300180 * smk_parse_rule - parse subject, object and access type
181 * @data: string to be parsed whose size is SMK_LOADLEN
182 * @rule: parsed entities are stored in here
183 */
184static int smk_parse_rule(const char *data, struct smack_rule *rule)
185{
186 rule->smk_subject = smk_import(data, 0);
187 if (rule->smk_subject == NULL)
188 return -1;
189
190 rule->smk_object = smk_import(data + SMK_LABELLEN, 0);
191 if (rule->smk_object == NULL)
192 return -1;
193
194 rule->smk_access = 0;
195
196 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
197 case '-':
198 break;
199 case 'r':
200 case 'R':
201 rule->smk_access |= MAY_READ;
202 break;
203 default:
204 return -1;
205 }
206
207 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
208 case '-':
209 break;
210 case 'w':
211 case 'W':
212 rule->smk_access |= MAY_WRITE;
213 break;
214 default:
215 return -1;
216 }
217
218 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
219 case '-':
220 break;
221 case 'x':
222 case 'X':
223 rule->smk_access |= MAY_EXEC;
224 break;
225 default:
226 return -1;
227 }
228
229 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
230 case '-':
231 break;
232 case 'a':
233 case 'A':
234 rule->smk_access |= MAY_APPEND;
235 break;
236 default:
237 return -1;
238 }
239
240 switch (data[SMK_LABELLEN + SMK_LABELLEN + 4]) {
241 case '-':
242 break;
243 case 't':
244 case 'T':
245 rule->smk_access |= MAY_TRANSMUTE;
246 break;
247 default:
248 return -1;
249 }
250
251 return 0;
252}
253
254/**
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800255 * smk_write_load_list - write() for any /smack/load
Randy Dunlap251a2a92009-02-18 11:42:33 -0800256 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800257 * @buf: where to get the data from
258 * @count: bytes sent
259 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800260 * @rule_list: the list of rules to write to
261 * @rule_lock: lock for the rule list
Casey Schauflere114e472008-02-04 22:29:50 -0800262 *
263 * Get one smack access rule from above.
264 * The format is exactly:
265 * char subject[SMK_LABELLEN]
266 * char object[SMK_LABELLEN]
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700267 * char access[SMK_ACCESSLEN]
Casey Schauflere114e472008-02-04 22:29:50 -0800268 *
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700269 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
Casey Schauflere114e472008-02-04 22:29:50 -0800270 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800271static ssize_t smk_write_load_list(struct file *file, const char __user *buf,
272 size_t count, loff_t *ppos,
273 struct list_head *rule_list,
274 struct mutex *rule_lock)
Casey Schauflere114e472008-02-04 22:29:50 -0800275{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100276 struct smack_rule *rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800277 char *data;
278 int rc = -EINVAL;
279
280 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800281 * No partial writes.
282 * Enough data must be present.
283 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200284 if (*ppos != 0)
285 return -EINVAL;
286 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300287 * Minor hack for backward compatibility
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200288 */
289 if (count < (SMK_OLOADLEN) || count > SMK_LOADLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800290 return -EINVAL;
291
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200292 data = kzalloc(SMK_LOADLEN, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -0800293 if (data == NULL)
294 return -ENOMEM;
295
296 if (copy_from_user(data, buf, count) != 0) {
297 rc = -EFAULT;
298 goto out;
299 }
300
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200301 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300302 * More on the minor hack for backward compatibility
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200303 */
304 if (count == (SMK_OLOADLEN))
305 data[SMK_OLOADLEN] = '-';
306
Etienne Basset7198e2e2009-03-24 20:53:24 +0100307 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
308 if (rule == NULL) {
309 rc = -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800310 goto out;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100311 }
Casey Schauflere114e472008-02-04 22:29:50 -0800312
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300313 if (smk_parse_rule(data, rule))
Etienne Basset7198e2e2009-03-24 20:53:24 +0100314 goto out_free_rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800315
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800316 rc = count;
317 /*
318 * smk_set_access returns true if there was already a rule
319 * for the subject/object pair, and false if it was new.
320 */
321 if (!smk_set_access(rule, rule_list, rule_lock))
322 goto out;
Casey Schauflere114e472008-02-04 22:29:50 -0800323
Etienne Basset7198e2e2009-03-24 20:53:24 +0100324out_free_rule:
325 kfree(rule);
Casey Schauflere114e472008-02-04 22:29:50 -0800326out:
327 kfree(data);
328 return rc;
329}
330
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800331
332/*
333 * Seq_file read operations for /smack/load
334 */
335
336static void *load_seq_start(struct seq_file *s, loff_t *pos)
337{
338 if (*pos == SEQ_READ_FINISHED)
339 return NULL;
340 if (list_empty(&smack_rule_list))
341 return NULL;
342 return smack_rule_list.next;
343}
344
345static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
346{
347 struct list_head *list = v;
348
349 if (list_is_last(list, &smack_rule_list)) {
350 *pos = SEQ_READ_FINISHED;
351 return NULL;
352 }
353 return list->next;
354}
355
356static int load_seq_show(struct seq_file *s, void *v)
357{
358 struct list_head *list = v;
359 struct smack_rule *srp =
360 list_entry(list, struct smack_rule, list);
361
362 seq_printf(s, "%s %s", (char *)srp->smk_subject,
363 (char *)srp->smk_object);
364
365 seq_putc(s, ' ');
366
367 if (srp->smk_access & MAY_READ)
368 seq_putc(s, 'r');
369 if (srp->smk_access & MAY_WRITE)
370 seq_putc(s, 'w');
371 if (srp->smk_access & MAY_EXEC)
372 seq_putc(s, 'x');
373 if (srp->smk_access & MAY_APPEND)
374 seq_putc(s, 'a');
375 if (srp->smk_access & MAY_TRANSMUTE)
376 seq_putc(s, 't');
377 if (srp->smk_access == 0)
378 seq_putc(s, '-');
379
380 seq_putc(s, '\n');
381
382 return 0;
383}
384
385static void load_seq_stop(struct seq_file *s, void *v)
386{
387 /* No-op */
388}
389
390static const struct seq_operations load_seq_ops = {
391 .start = load_seq_start,
392 .next = load_seq_next,
393 .show = load_seq_show,
394 .stop = load_seq_stop,
395};
396
397/**
398 * smk_open_load - open() for /smack/load
399 * @inode: inode structure representing file
400 * @file: "load" file pointer
401 *
402 * For reading, use load_seq_* seq_file reading operations.
403 */
404static int smk_open_load(struct inode *inode, struct file *file)
405{
406 return seq_open(file, &load_seq_ops);
407}
408
409/**
410 * smk_write_load - write() for /smack/load
411 * @file: file pointer, not actually used
412 * @buf: where to get the data from
413 * @count: bytes sent
414 * @ppos: where to start - must be 0
415 *
416 */
417static ssize_t smk_write_load(struct file *file, const char __user *buf,
418 size_t count, loff_t *ppos)
419{
420
421 /*
422 * Must have privilege.
423 * No partial writes.
424 * Enough data must be present.
425 */
426 if (!capable(CAP_MAC_ADMIN))
427 return -EPERM;
428
429 return smk_write_load_list(file, buf, count, ppos, &smack_rule_list,
430 &smack_list_lock);
431}
432
Casey Schauflere114e472008-02-04 22:29:50 -0800433static const struct file_operations smk_load_ops = {
434 .open = smk_open_load,
435 .read = seq_read,
436 .llseek = seq_lseek,
437 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700438 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800439};
440
441/**
442 * smk_cipso_doi - initialize the CIPSO domain
443 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700444static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800445{
446 int rc;
447 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500448 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800449
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500450 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800451
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500452 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800453 if (rc != 0)
454 printk(KERN_WARNING "%s:%d remove rc = %d\n",
455 __func__, __LINE__, rc);
456
457 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
458 if (doip == NULL)
459 panic("smack: Failed to initialize cipso DOI.\n");
460 doip->map.std = NULL;
461 doip->doi = smk_cipso_doi_value;
462 doip->type = CIPSO_V4_MAP_PASS;
463 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
464 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
465 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
466
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500467 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400468 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500469 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800470 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400471 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500472 return;
473 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500474 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500475 if (rc != 0) {
476 printk(KERN_WARNING "%s:%d map add rc = %d\n",
477 __func__, __LINE__, rc);
478 kfree(doip);
479 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400480 }
Casey Schauflere114e472008-02-04 22:29:50 -0800481}
482
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800483/**
484 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800485 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800486 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700487static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800488{
489 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500490 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800491
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500492 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800493
494 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500495 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800496 if (rc != 0)
497 printk(KERN_WARNING "%s:%d remove rc = %d\n",
498 __func__, __LINE__, rc);
499 }
500
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500501 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
502 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800503 if (rc != 0)
504 printk(KERN_WARNING "%s:%d add rc = %d\n",
505 __func__, __LINE__, rc);
506}
507
Casey Schauflere114e472008-02-04 22:29:50 -0800508/*
509 * Seq_file read operations for /smack/cipso
510 */
511
512static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
513{
514 if (*pos == SEQ_READ_FINISHED)
515 return NULL;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100516 if (list_empty(&smack_known_list))
517 return NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800518
Etienne Basset7198e2e2009-03-24 20:53:24 +0100519 return smack_known_list.next;
Casey Schauflere114e472008-02-04 22:29:50 -0800520}
521
522static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
523{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100524 struct list_head *list = v;
Casey Schauflere114e472008-02-04 22:29:50 -0800525
526 /*
Etienne Basset7198e2e2009-03-24 20:53:24 +0100527 * labels with no associated cipso value wont be printed
528 * in cipso_seq_show
Casey Schauflere114e472008-02-04 22:29:50 -0800529 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100530 if (list_is_last(list, &smack_known_list)) {
Casey Schauflere114e472008-02-04 22:29:50 -0800531 *pos = SEQ_READ_FINISHED;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100532 return NULL;
533 }
Casey Schauflere114e472008-02-04 22:29:50 -0800534
Etienne Basset7198e2e2009-03-24 20:53:24 +0100535 return list->next;
Casey Schauflere114e472008-02-04 22:29:50 -0800536}
537
538/*
539 * Print cipso labels in format:
540 * label level[/cat[,cat]]
541 */
542static int cipso_seq_show(struct seq_file *s, void *v)
543{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100544 struct list_head *list = v;
545 struct smack_known *skp =
546 list_entry(list, struct smack_known, list);
Casey Schauflere114e472008-02-04 22:29:50 -0800547 struct smack_cipso *scp = skp->smk_cipso;
548 char *cbp;
549 char sep = '/';
550 int cat = 1;
551 int i;
552 unsigned char m;
553
554 if (scp == NULL)
555 return 0;
556
557 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
558
559 cbp = scp->smk_catset;
560 for (i = 0; i < SMK_LABELLEN; i++)
561 for (m = 0x80; m != 0; m >>= 1) {
562 if (m & cbp[i]) {
563 seq_printf(s, "%c%d", sep, cat);
564 sep = ',';
565 }
566 cat++;
567 }
568
569 seq_putc(s, '\n');
570
571 return 0;
572}
573
574static void cipso_seq_stop(struct seq_file *s, void *v)
575{
576 /* No-op */
577}
578
James Morris88e9d342009-09-22 16:43:43 -0700579static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800580 .start = cipso_seq_start,
581 .stop = cipso_seq_stop,
582 .next = cipso_seq_next,
583 .show = cipso_seq_show,
584};
585
586/**
587 * smk_open_cipso - open() for /smack/cipso
588 * @inode: inode structure representing file
589 * @file: "cipso" file pointer
590 *
591 * Connect our cipso_seq_* operations with /smack/cipso
592 * file_operations
593 */
594static int smk_open_cipso(struct inode *inode, struct file *file)
595{
596 return seq_open(file, &cipso_seq_ops);
597}
598
599/**
600 * smk_write_cipso - write() for /smack/cipso
Randy Dunlap251a2a92009-02-18 11:42:33 -0800601 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800602 * @buf: where to get the data from
603 * @count: bytes sent
604 * @ppos: where to start
605 *
606 * Accepts only one cipso rule per write call.
607 * Returns number of bytes written or error code, as appropriate
608 */
609static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
610 size_t count, loff_t *ppos)
611{
612 struct smack_known *skp;
613 struct smack_cipso *scp = NULL;
614 char mapcatset[SMK_LABELLEN];
615 int maplevel;
616 int cat;
617 int catlen;
618 ssize_t rc = -EINVAL;
619 char *data = NULL;
620 char *rule;
621 int ret;
622 int i;
623
624 /*
625 * Must have privilege.
626 * No partial writes.
627 * Enough data must be present.
628 */
629 if (!capable(CAP_MAC_ADMIN))
630 return -EPERM;
631 if (*ppos != 0)
632 return -EINVAL;
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700633 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
Casey Schauflere114e472008-02-04 22:29:50 -0800634 return -EINVAL;
635
636 data = kzalloc(count + 1, GFP_KERNEL);
637 if (data == NULL)
638 return -ENOMEM;
639
640 if (copy_from_user(data, buf, count) != 0) {
641 rc = -EFAULT;
642 goto unlockedout;
643 }
644
Etienne Basset43031542009-03-27 17:11:01 -0400645 /* labels cannot begin with a '-' */
646 if (data[0] == '-') {
647 rc = -EINVAL;
648 goto unlockedout;
649 }
Casey Schauflere114e472008-02-04 22:29:50 -0800650 data[count] = '\0';
651 rule = data;
652 /*
653 * Only allow one writer at a time. Writes should be
654 * quite rare and small in any case.
655 */
656 mutex_lock(&smack_cipso_lock);
657
658 skp = smk_import_entry(rule, 0);
659 if (skp == NULL)
660 goto out;
661
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800662 rule += SMK_LABELLEN;
Casey Schauflere114e472008-02-04 22:29:50 -0800663 ret = sscanf(rule, "%d", &maplevel);
664 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
665 goto out;
666
667 rule += SMK_DIGITLEN;
668 ret = sscanf(rule, "%d", &catlen);
669 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
670 goto out;
671
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700672 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800673 goto out;
674
675 memset(mapcatset, 0, sizeof(mapcatset));
676
677 for (i = 0; i < catlen; i++) {
678 rule += SMK_DIGITLEN;
679 ret = sscanf(rule, "%d", &cat);
680 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
681 goto out;
682
683 smack_catset_bit(cat, mapcatset);
684 }
685
686 if (skp->smk_cipso == NULL) {
687 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
688 if (scp == NULL) {
689 rc = -ENOMEM;
690 goto out;
691 }
692 }
693
694 spin_lock_bh(&skp->smk_cipsolock);
695
696 if (scp == NULL)
697 scp = skp->smk_cipso;
698 else
699 skp->smk_cipso = scp;
700
701 scp->smk_level = maplevel;
702 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
703
704 spin_unlock_bh(&skp->smk_cipsolock);
705
706 rc = count;
707out:
708 mutex_unlock(&smack_cipso_lock);
709unlockedout:
710 kfree(data);
711 return rc;
712}
713
714static const struct file_operations smk_cipso_ops = {
715 .open = smk_open_cipso,
716 .read = seq_read,
717 .llseek = seq_lseek,
718 .write = smk_write_cipso,
719 .release = seq_release,
720};
721
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500722/*
723 * Seq_file read operations for /smack/netlabel
724 */
725
726static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
727{
728 if (*pos == SEQ_READ_FINISHED)
729 return NULL;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100730 if (list_empty(&smk_netlbladdr_list))
731 return NULL;
732 return smk_netlbladdr_list.next;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500733}
734
735static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
736{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100737 struct list_head *list = v;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500738
Etienne Basset7198e2e2009-03-24 20:53:24 +0100739 if (list_is_last(list, &smk_netlbladdr_list)) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500740 *pos = SEQ_READ_FINISHED;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100741 return NULL;
742 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500743
Etienne Basset7198e2e2009-03-24 20:53:24 +0100744 return list->next;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500745}
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500746#define BEBITS (sizeof(__be32) * 8)
747
748/*
749 * Print host/label pairs
750 */
751static int netlbladdr_seq_show(struct seq_file *s, void *v)
752{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100753 struct list_head *list = v;
754 struct smk_netlbladdr *skp =
755 list_entry(list, struct smk_netlbladdr, list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500756 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +0100757 int maskn;
758 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500759
etienne113a0e42009-03-04 07:33:51 +0100760 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500761
762 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
763 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
764
765 return 0;
766}
767
768static void netlbladdr_seq_stop(struct seq_file *s, void *v)
769{
770 /* No-op */
771}
772
James Morris88e9d342009-09-22 16:43:43 -0700773static const struct seq_operations netlbladdr_seq_ops = {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500774 .start = netlbladdr_seq_start,
775 .stop = netlbladdr_seq_stop,
776 .next = netlbladdr_seq_next,
777 .show = netlbladdr_seq_show,
778};
779
780/**
781 * smk_open_netlbladdr - open() for /smack/netlabel
782 * @inode: inode structure representing file
783 * @file: "netlabel" file pointer
784 *
785 * Connect our netlbladdr_seq_* operations with /smack/netlabel
786 * file_operations
787 */
788static int smk_open_netlbladdr(struct inode *inode, struct file *file)
789{
790 return seq_open(file, &netlbladdr_seq_ops);
791}
792
793/**
etienne113a0e42009-03-04 07:33:51 +0100794 * smk_netlbladdr_insert
795 * @new : netlabel to insert
796 *
797 * This helper insert netlabel in the smack_netlbladdrs list
798 * sorted by netmask length (longest to smallest)
Etienne Basset7198e2e2009-03-24 20:53:24 +0100799 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
800 *
etienne113a0e42009-03-04 07:33:51 +0100801 */
802static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
803{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100804 struct smk_netlbladdr *m, *m_next;
etienne113a0e42009-03-04 07:33:51 +0100805
Etienne Basset7198e2e2009-03-24 20:53:24 +0100806 if (list_empty(&smk_netlbladdr_list)) {
807 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +0100808 return;
809 }
810
Jiri Pirko05725f72009-04-14 20:17:16 +0200811 m = list_entry_rcu(smk_netlbladdr_list.next,
812 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +0100813
etienne113a0e42009-03-04 07:33:51 +0100814 /* the comparison '>' is a bit hacky, but works */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100815 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
816 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +0100817 return;
818 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100819
820 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
821 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
822 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +0100823 return;
824 }
Jiri Pirko05725f72009-04-14 20:17:16 +0200825 m_next = list_entry_rcu(m->list.next,
826 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +0100827 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
828 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +0100829 return;
830 }
831 }
832}
833
834
835/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500836 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -0800837 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500838 * @buf: where to get the data from
839 * @count: bytes sent
840 * @ppos: where to start
841 *
842 * Accepts only one netlbladdr per write call.
843 * Returns number of bytes written or error code, as appropriate
844 */
845static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
846 size_t count, loff_t *ppos)
847{
848 struct smk_netlbladdr *skp;
849 struct sockaddr_in newname;
850 char smack[SMK_LABELLEN];
851 char *sp;
Roel Kluin6470c072009-05-21 18:42:54 +0200852 char data[SMK_NETLBLADDRMAX + 1];
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500853 char *host = (char *)&newname.sin_addr.s_addr;
854 int rc;
855 struct netlbl_audit audit_info;
856 struct in_addr mask;
857 unsigned int m;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100858 int found;
etienne113a0e42009-03-04 07:33:51 +0100859 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500860 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +0100861 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500862
863 /*
864 * Must have privilege.
865 * No partial writes.
866 * Enough data must be present.
867 * "<addr/mask, as a.b.c.d/e><space><label>"
868 * "<addr, as a.b.c.d><space><label>"
869 */
870 if (!capable(CAP_MAC_ADMIN))
871 return -EPERM;
872 if (*ppos != 0)
873 return -EINVAL;
874 if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
875 return -EINVAL;
876 if (copy_from_user(data, buf, count) != 0)
877 return -EFAULT;
878
879 data[count] = '\0';
880
881 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
882 &host[0], &host[1], &host[2], &host[3], &m, smack);
883 if (rc != 6) {
884 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
885 &host[0], &host[1], &host[2], &host[3], smack);
886 if (rc != 5)
887 return -EINVAL;
888 m = BEBITS;
889 }
890 if (m > BEBITS)
891 return -EINVAL;
892
Etienne Basset43031542009-03-27 17:11:01 -0400893 /* if smack begins with '-', its an option, don't import it */
894 if (smack[0] != '-') {
895 sp = smk_import(smack, 0);
896 if (sp == NULL)
897 return -EINVAL;
898 } else {
899 /* check known options */
900 if (strcmp(smack, smack_cipso_option) == 0)
901 sp = (char *)smack_cipso_option;
902 else
903 return -EINVAL;
904 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500905
etienne113a0e42009-03-04 07:33:51 +0100906 for (temp_mask = 0; m > 0; m--) {
907 temp_mask |= mask_bits;
908 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500909 }
etienne113a0e42009-03-04 07:33:51 +0100910 mask.s_addr = cpu_to_be32(temp_mask);
911
912 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500913 /*
914 * Only allow one writer at a time. Writes should be
915 * quite rare and small in any case.
916 */
917 mutex_lock(&smk_netlbladdr_lock);
918
919 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +0100920 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100921 found = 0;
922 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500923 if (skp->smk_host.sin_addr.s_addr == nsa &&
Etienne Basset7198e2e2009-03-24 20:53:24 +0100924 skp->smk_mask.s_addr == mask.s_addr) {
925 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500926 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100927 }
928 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500929 smk_netlabel_audit_set(&audit_info);
930
Etienne Basset7198e2e2009-03-24 20:53:24 +0100931 if (found == 0) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500932 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
933 if (skp == NULL)
934 rc = -ENOMEM;
935 else {
936 rc = 0;
937 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
938 skp->smk_mask.s_addr = mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500939 skp->smk_label = sp;
etienne113a0e42009-03-04 07:33:51 +0100940 smk_netlbladdr_insert(skp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500941 }
942 } else {
Etienne Basset43031542009-03-27 17:11:01 -0400943 /* we delete the unlabeled entry, only if the previous label
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300944 * wasn't the special CIPSO option */
Etienne Basset43031542009-03-27 17:11:01 -0400945 if (skp->smk_label != smack_cipso_option)
946 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
947 &skp->smk_host.sin_addr, &skp->smk_mask,
948 PF_INET, &audit_info);
949 else
950 rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500951 skp->smk_label = sp;
952 }
953
954 /*
955 * Now tell netlabel about the single label nature of
956 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -0400957 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500958 */
Etienne Basset43031542009-03-27 17:11:01 -0400959 if (rc == 0 && sp != smack_cipso_option)
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500960 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
961 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
962 smack_to_secid(skp->smk_label), &audit_info);
963
964 if (rc == 0)
965 rc = count;
966
967 mutex_unlock(&smk_netlbladdr_lock);
968
969 return rc;
970}
971
972static const struct file_operations smk_netlbladdr_ops = {
973 .open = smk_open_netlbladdr,
974 .read = seq_read,
975 .llseek = seq_lseek,
976 .write = smk_write_netlbladdr,
977 .release = seq_release,
978};
979
Casey Schauflere114e472008-02-04 22:29:50 -0800980/**
981 * smk_read_doi - read() for /smack/doi
982 * @filp: file pointer, not actually used
983 * @buf: where to put the result
984 * @count: maximum to send along
985 * @ppos: where to start
986 *
987 * Returns number of bytes read or error code, as appropriate
988 */
989static ssize_t smk_read_doi(struct file *filp, char __user *buf,
990 size_t count, loff_t *ppos)
991{
992 char temp[80];
993 ssize_t rc;
994
995 if (*ppos != 0)
996 return 0;
997
998 sprintf(temp, "%d", smk_cipso_doi_value);
999 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1000
1001 return rc;
1002}
1003
1004/**
1005 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001006 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001007 * @buf: where to get the data from
1008 * @count: bytes sent
1009 * @ppos: where to start
1010 *
1011 * Returns number of bytes written or error code, as appropriate
1012 */
1013static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1014 size_t count, loff_t *ppos)
1015{
1016 char temp[80];
1017 int i;
1018
1019 if (!capable(CAP_MAC_ADMIN))
1020 return -EPERM;
1021
1022 if (count >= sizeof(temp) || count == 0)
1023 return -EINVAL;
1024
1025 if (copy_from_user(temp, buf, count) != 0)
1026 return -EFAULT;
1027
1028 temp[count] = '\0';
1029
1030 if (sscanf(temp, "%d", &i) != 1)
1031 return -EINVAL;
1032
1033 smk_cipso_doi_value = i;
1034
1035 smk_cipso_doi();
1036
1037 return count;
1038}
1039
1040static const struct file_operations smk_doi_ops = {
1041 .read = smk_read_doi,
1042 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001043 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001044};
1045
1046/**
1047 * smk_read_direct - read() for /smack/direct
1048 * @filp: file pointer, not actually used
1049 * @buf: where to put the result
1050 * @count: maximum to send along
1051 * @ppos: where to start
1052 *
1053 * Returns number of bytes read or error code, as appropriate
1054 */
1055static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1056 size_t count, loff_t *ppos)
1057{
1058 char temp[80];
1059 ssize_t rc;
1060
1061 if (*ppos != 0)
1062 return 0;
1063
1064 sprintf(temp, "%d", smack_cipso_direct);
1065 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1066
1067 return rc;
1068}
1069
1070/**
1071 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001072 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001073 * @buf: where to get the data from
1074 * @count: bytes sent
1075 * @ppos: where to start
1076 *
1077 * Returns number of bytes written or error code, as appropriate
1078 */
1079static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1080 size_t count, loff_t *ppos)
1081{
1082 char temp[80];
1083 int i;
1084
1085 if (!capable(CAP_MAC_ADMIN))
1086 return -EPERM;
1087
1088 if (count >= sizeof(temp) || count == 0)
1089 return -EINVAL;
1090
1091 if (copy_from_user(temp, buf, count) != 0)
1092 return -EFAULT;
1093
1094 temp[count] = '\0';
1095
1096 if (sscanf(temp, "%d", &i) != 1)
1097 return -EINVAL;
1098
1099 smack_cipso_direct = i;
1100
1101 return count;
1102}
1103
1104static const struct file_operations smk_direct_ops = {
1105 .read = smk_read_direct,
1106 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001107 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001108};
1109
1110/**
1111 * smk_read_ambient - read() for /smack/ambient
1112 * @filp: file pointer, not actually used
1113 * @buf: where to put the result
1114 * @cn: maximum to send along
1115 * @ppos: where to start
1116 *
1117 * Returns number of bytes read or error code, as appropriate
1118 */
1119static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1120 size_t cn, loff_t *ppos)
1121{
1122 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001123 int asize;
1124
1125 if (*ppos != 0)
1126 return 0;
1127 /*
1128 * Being careful to avoid a problem in the case where
1129 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001130 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001131 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001132
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001133 asize = strlen(smack_net_ambient) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001134
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001135 if (cn >= asize)
1136 rc = simple_read_from_buffer(buf, cn, ppos,
1137 smack_net_ambient, asize);
1138 else
1139 rc = -EINVAL;
1140
1141 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001142
1143 return rc;
1144}
1145
1146/**
1147 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001148 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001149 * @buf: where to get the data from
1150 * @count: bytes sent
1151 * @ppos: where to start
1152 *
1153 * Returns number of bytes written or error code, as appropriate
1154 */
1155static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1156 size_t count, loff_t *ppos)
1157{
1158 char in[SMK_LABELLEN];
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001159 char *oldambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001160 char *smack;
1161
1162 if (!capable(CAP_MAC_ADMIN))
1163 return -EPERM;
1164
1165 if (count >= SMK_LABELLEN)
1166 return -EINVAL;
1167
1168 if (copy_from_user(in, buf, count) != 0)
1169 return -EFAULT;
1170
1171 smack = smk_import(in, count);
1172 if (smack == NULL)
1173 return -EINVAL;
1174
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001175 mutex_lock(&smack_ambient_lock);
1176
1177 oldambient = smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001178 smack_net_ambient = smack;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001179 smk_unlbl_ambient(oldambient);
1180
1181 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001182
1183 return count;
1184}
1185
1186static const struct file_operations smk_ambient_ops = {
1187 .read = smk_read_ambient,
1188 .write = smk_write_ambient,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001189 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001190};
1191
Casey Schaufler15446232008-07-30 15:37:11 -07001192/**
1193 * smk_read_onlycap - read() for /smack/onlycap
1194 * @filp: file pointer, not actually used
1195 * @buf: where to put the result
1196 * @cn: maximum to send along
1197 * @ppos: where to start
1198 *
1199 * Returns number of bytes read or error code, as appropriate
1200 */
1201static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1202 size_t cn, loff_t *ppos)
1203{
1204 char *smack = "";
1205 ssize_t rc = -EINVAL;
1206 int asize;
1207
1208 if (*ppos != 0)
1209 return 0;
1210
1211 if (smack_onlycap != NULL)
1212 smack = smack_onlycap;
1213
1214 asize = strlen(smack) + 1;
1215
1216 if (cn >= asize)
1217 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1218
1219 return rc;
1220}
1221
1222/**
1223 * smk_write_onlycap - write() for /smack/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001224 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001225 * @buf: where to get the data from
1226 * @count: bytes sent
1227 * @ppos: where to start
1228 *
1229 * Returns number of bytes written or error code, as appropriate
1230 */
1231static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1232 size_t count, loff_t *ppos)
1233{
1234 char in[SMK_LABELLEN];
Casey Schaufler676dac42010-12-02 06:43:39 -08001235 char *sp = smk_of_task(current->cred->security);
Casey Schaufler15446232008-07-30 15:37:11 -07001236
1237 if (!capable(CAP_MAC_ADMIN))
1238 return -EPERM;
1239
1240 /*
1241 * This can be done using smk_access() but is done
1242 * explicitly for clarity. The smk_access() implementation
1243 * would use smk_access(smack_onlycap, MAY_WRITE)
1244 */
1245 if (smack_onlycap != NULL && smack_onlycap != sp)
1246 return -EPERM;
1247
1248 if (count >= SMK_LABELLEN)
1249 return -EINVAL;
1250
1251 if (copy_from_user(in, buf, count) != 0)
1252 return -EFAULT;
1253
1254 /*
1255 * Should the null string be passed in unset the onlycap value.
1256 * This seems like something to be careful with as usually
1257 * smk_import only expects to return NULL for errors. It
1258 * is usually the case that a nullstring or "\n" would be
1259 * bad to pass to smk_import but in fact this is useful here.
1260 */
1261 smack_onlycap = smk_import(in, count);
1262
1263 return count;
1264}
1265
1266static const struct file_operations smk_onlycap_ops = {
1267 .read = smk_read_onlycap,
1268 .write = smk_write_onlycap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001269 .llseek = default_llseek,
Casey Schaufler15446232008-07-30 15:37:11 -07001270};
1271
Casey Schauflere114e472008-02-04 22:29:50 -08001272/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001273 * smk_read_logging - read() for /smack/logging
1274 * @filp: file pointer, not actually used
1275 * @buf: where to put the result
1276 * @cn: maximum to send along
1277 * @ppos: where to start
1278 *
1279 * Returns number of bytes read or error code, as appropriate
1280 */
1281static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1282 size_t count, loff_t *ppos)
1283{
1284 char temp[32];
1285 ssize_t rc;
1286
1287 if (*ppos != 0)
1288 return 0;
1289
1290 sprintf(temp, "%d\n", log_policy);
1291 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1292 return rc;
1293}
1294
1295/**
1296 * smk_write_logging - write() for /smack/logging
1297 * @file: file pointer, not actually used
1298 * @buf: where to get the data from
1299 * @count: bytes sent
1300 * @ppos: where to start
1301 *
1302 * Returns number of bytes written or error code, as appropriate
1303 */
1304static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1305 size_t count, loff_t *ppos)
1306{
1307 char temp[32];
1308 int i;
1309
1310 if (!capable(CAP_MAC_ADMIN))
1311 return -EPERM;
1312
1313 if (count >= sizeof(temp) || count == 0)
1314 return -EINVAL;
1315
1316 if (copy_from_user(temp, buf, count) != 0)
1317 return -EFAULT;
1318
1319 temp[count] = '\0';
1320
1321 if (sscanf(temp, "%d", &i) != 1)
1322 return -EINVAL;
1323 if (i < 0 || i > 3)
1324 return -EINVAL;
1325 log_policy = i;
1326 return count;
1327}
1328
1329
1330
1331static const struct file_operations smk_logging_ops = {
1332 .read = smk_read_logging,
1333 .write = smk_write_logging,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001334 .llseek = default_llseek,
Etienne Bassetecfcc532009-04-08 20:40:06 +02001335};
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001336
1337/*
1338 * Seq_file read operations for /smack/load-self
1339 */
1340
1341static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1342{
1343 struct task_smack *tsp = current_security();
1344
1345 if (*pos == SEQ_READ_FINISHED)
1346 return NULL;
1347 if (list_empty(&tsp->smk_rules))
1348 return NULL;
1349 return tsp->smk_rules.next;
1350}
1351
1352static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1353{
1354 struct task_smack *tsp = current_security();
1355 struct list_head *list = v;
1356
1357 if (list_is_last(list, &tsp->smk_rules)) {
1358 *pos = SEQ_READ_FINISHED;
1359 return NULL;
1360 }
1361 return list->next;
1362}
1363
1364static int load_self_seq_show(struct seq_file *s, void *v)
1365{
1366 struct list_head *list = v;
1367 struct smack_rule *srp =
1368 list_entry(list, struct smack_rule, list);
1369
1370 seq_printf(s, "%s %s", (char *)srp->smk_subject,
1371 (char *)srp->smk_object);
1372
1373 seq_putc(s, ' ');
1374
1375 if (srp->smk_access & MAY_READ)
1376 seq_putc(s, 'r');
1377 if (srp->smk_access & MAY_WRITE)
1378 seq_putc(s, 'w');
1379 if (srp->smk_access & MAY_EXEC)
1380 seq_putc(s, 'x');
1381 if (srp->smk_access & MAY_APPEND)
1382 seq_putc(s, 'a');
1383 if (srp->smk_access & MAY_TRANSMUTE)
1384 seq_putc(s, 't');
1385 if (srp->smk_access == 0)
1386 seq_putc(s, '-');
1387
1388 seq_putc(s, '\n');
1389
1390 return 0;
1391}
1392
1393static void load_self_seq_stop(struct seq_file *s, void *v)
1394{
1395 /* No-op */
1396}
1397
1398static const struct seq_operations load_self_seq_ops = {
1399 .start = load_self_seq_start,
1400 .next = load_self_seq_next,
1401 .show = load_self_seq_show,
1402 .stop = load_self_seq_stop,
1403};
1404
1405
1406/**
1407 * smk_open_load_self - open() for /smack/load-self
1408 * @inode: inode structure representing file
1409 * @file: "load" file pointer
1410 *
1411 * For reading, use load_seq_* seq_file reading operations.
1412 */
1413static int smk_open_load_self(struct inode *inode, struct file *file)
1414{
1415 return seq_open(file, &load_self_seq_ops);
1416}
1417
1418/**
1419 * smk_write_load_self - write() for /smack/load-self
1420 * @file: file pointer, not actually used
1421 * @buf: where to get the data from
1422 * @count: bytes sent
1423 * @ppos: where to start - must be 0
1424 *
1425 */
1426static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1427 size_t count, loff_t *ppos)
1428{
1429 struct task_smack *tsp = current_security();
1430
1431 return smk_write_load_list(file, buf, count, ppos, &tsp->smk_rules,
1432 &tsp->smk_rules_lock);
1433}
1434
1435static const struct file_operations smk_load_self_ops = {
1436 .open = smk_open_load_self,
1437 .read = seq_read,
1438 .llseek = seq_lseek,
1439 .write = smk_write_load_self,
1440 .release = seq_release,
1441};
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001442
1443/**
1444 * smk_write_access - handle access check transaction
1445 * @file: file pointer
1446 * @buf: data from user space
1447 * @count: bytes sent
1448 * @ppos: where to start - must be 0
1449 */
1450static ssize_t smk_write_access(struct file *file, const char __user *buf,
1451 size_t count, loff_t *ppos)
1452{
1453 struct smack_rule rule;
1454 char *data;
1455
1456 if (!capable(CAP_MAC_ADMIN))
1457 return -EPERM;
1458
1459 data = simple_transaction_get(file, buf, count);
1460 if (IS_ERR(data))
1461 return PTR_ERR(data);
1462
1463 if (count < SMK_LOADLEN || smk_parse_rule(data, &rule))
1464 return -EINVAL;
1465
1466 data[0] = smk_access(rule.smk_subject, rule.smk_object,
1467 rule.smk_access, NULL) == 0;
1468
1469 simple_transaction_set(file, 1);
1470 return SMK_LOADLEN;
1471}
1472
1473static const struct file_operations smk_access_ops = {
1474 .write = smk_write_access,
1475 .read = simple_transaction_read,
1476 .release = simple_transaction_release,
1477 .llseek = generic_file_llseek,
1478};
1479
Etienne Bassetecfcc532009-04-08 20:40:06 +02001480/**
Casey Schauflere114e472008-02-04 22:29:50 -08001481 * smk_fill_super - fill the /smackfs superblock
1482 * @sb: the empty superblock
1483 * @data: unused
1484 * @silent: unused
1485 *
1486 * Fill in the well known entries for /smack
1487 *
1488 * Returns 0 on success, an error code on failure
1489 */
1490static int smk_fill_super(struct super_block *sb, void *data, int silent)
1491{
1492 int rc;
1493 struct inode *root_inode;
1494
1495 static struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001496 [SMK_LOAD] = {
1497 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
1498 [SMK_CIPSO] = {
1499 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1500 [SMK_DOI] = {
1501 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1502 [SMK_DIRECT] = {
1503 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1504 [SMK_AMBIENT] = {
1505 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
1506 [SMK_NETLBLADDR] = {
1507 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
1508 [SMK_ONLYCAP] = {
1509 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
1510 [SMK_LOGGING] = {
1511 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
1512 [SMK_LOAD_SELF] = {
1513 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001514 [SMK_ACCESSES] = {
1515 "access", &smk_access_ops, S_IRUGO|S_IWUSR},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001516 /* last one */
1517 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08001518 };
1519
1520 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1521 if (rc != 0) {
1522 printk(KERN_ERR "%s failed %d while creating inodes\n",
1523 __func__, rc);
1524 return rc;
1525 }
1526
1527 root_inode = sb->s_root->d_inode;
1528 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1529
1530 return 0;
1531}
1532
1533/**
Al Virofc14f2f2010-07-25 01:48:30 +04001534 * smk_mount - get the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08001535 * @fs_type: passed along without comment
1536 * @flags: passed along without comment
1537 * @dev_name: passed along without comment
1538 * @data: passed along without comment
Casey Schauflere114e472008-02-04 22:29:50 -08001539 *
1540 * Just passes everything along.
1541 *
1542 * Returns what the lower level code does.
1543 */
Al Virofc14f2f2010-07-25 01:48:30 +04001544static struct dentry *smk_mount(struct file_system_type *fs_type,
1545 int flags, const char *dev_name, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -08001546{
Al Virofc14f2f2010-07-25 01:48:30 +04001547 return mount_single(fs_type, flags, data, smk_fill_super);
Casey Schauflere114e472008-02-04 22:29:50 -08001548}
1549
1550static struct file_system_type smk_fs_type = {
1551 .name = "smackfs",
Al Virofc14f2f2010-07-25 01:48:30 +04001552 .mount = smk_mount,
Casey Schauflere114e472008-02-04 22:29:50 -08001553 .kill_sb = kill_litter_super,
1554};
1555
1556static struct vfsmount *smackfs_mount;
1557
1558/**
1559 * init_smk_fs - get the smackfs superblock
1560 *
1561 * register the smackfs
1562 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02001563 * Do not register smackfs if Smack wasn't enabled
1564 * on boot. We can not put this method normally under the
1565 * smack_init() code path since the security subsystem get
1566 * initialized before the vfs caches.
1567 *
1568 * Returns true if we were not chosen on boot or if
1569 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08001570 */
1571static int __init init_smk_fs(void)
1572{
1573 int err;
1574
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02001575 if (!security_module_enable(&smack_ops))
1576 return 0;
1577
Casey Schauflere114e472008-02-04 22:29:50 -08001578 err = register_filesystem(&smk_fs_type);
1579 if (!err) {
1580 smackfs_mount = kern_mount(&smk_fs_type);
1581 if (IS_ERR(smackfs_mount)) {
1582 printk(KERN_ERR "smackfs: could not mount!\n");
1583 err = PTR_ERR(smackfs_mount);
1584 smackfs_mount = NULL;
1585 }
1586 }
1587
Casey Schauflere114e472008-02-04 22:29:50 -08001588 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001589 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001590
1591 return err;
1592}
1593
1594__initcall(init_smk_fs);