blob: 856c8a2875231f2a6d64075bb114f99e0c356628 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
7 *
8 * Authors:
9 * Casey Schaufler <casey@schaufler-ca.com>
10 * Ahmed S. Darwish <darwish.07@gmail.com>
11 *
12 * Special thanks to the authors of selinuxfs.
13 *
14 * Karl MacMillan <kmacmillan@tresys.com>
15 * James Morris <jmorris@redhat.com>
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/vmalloc.h>
21#include <linux/security.h>
22#include <linux/mutex.h>
Casey Schaufler6d3dc072008-12-31 12:54:12 -050023#include <net/net_namespace.h>
Casey Schauflere114e472008-02-04 22:29:50 -080024#include <net/netlabel.h>
25#include <net/cipso_ipv4.h>
26#include <linux/seq_file.h>
27#include <linux/ctype.h>
Casey Schaufler4bc87e62008-02-15 15:24:25 -080028#include <linux/audit.h>
Casey Schauflere114e472008-02-04 22:29:50 -080029#include "smack.h"
30
31/*
32 * smackfs pseudo filesystem.
33 */
34
35enum smk_inos {
36 SMK_ROOT_INO = 2,
37 SMK_LOAD = 3, /* load policy */
38 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
39 SMK_DOI = 5, /* CIPSO DOI */
40 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
41 SMK_AMBIENT = 7, /* internet ambient label */
Casey Schaufler6d3dc072008-12-31 12:54:12 -050042 SMK_NETLBLADDR = 8, /* single label hosts */
Casey Schaufler15446232008-07-30 15:37:11 -070043 SMK_ONLYCAP = 9, /* the only "capable" label */
Casey Schauflere114e472008-02-04 22:29:50 -080044};
45
46/*
47 * List locks
48 */
49static DEFINE_MUTEX(smack_list_lock);
50static DEFINE_MUTEX(smack_cipso_lock);
Casey Schaufler4bc87e62008-02-15 15:24:25 -080051static DEFINE_MUTEX(smack_ambient_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -050052static DEFINE_MUTEX(smk_netlbladdr_lock);
Casey Schauflere114e472008-02-04 22:29:50 -080053
54/*
55 * This is the "ambient" label for network traffic.
56 * If it isn't somehow marked, use this.
57 * It can be reset via smackfs/ambient
58 */
59char *smack_net_ambient = smack_known_floor.smk_known;
60
61/*
Casey Schauflere114e472008-02-04 22:29:50 -080062 * This is the level in a CIPSO header that indicates a
63 * smack label is contained directly in the category set.
64 * It can be reset via smackfs/direct
65 */
66int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
67
Casey Schaufler15446232008-07-30 15:37:11 -070068/*
69 * Unless a process is running with this label even
70 * having CAP_MAC_OVERRIDE isn't enough to grant
71 * privilege to violate MAC policy. If no label is
72 * designated (the NULL case) capabilities apply to
73 * everyone. It is expected that the hat (^) label
74 * will be used if any label is used.
75 */
76char *smack_onlycap;
77
Casey Schaufler6d3dc072008-12-31 12:54:12 -050078/*
79 * Certain IP addresses may be designated as single label hosts.
80 * Packets are sent there unlabeled, but only from tasks that
81 * can write to the specified label.
82 */
Etienne Basset7198e2e2009-03-24 20:53:24 +010083
84LIST_HEAD(smk_netlbladdr_list);
85LIST_HEAD(smack_rule_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -050086
Casey Schauflere114e472008-02-04 22:29:50 -080087static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
Casey Schauflere114e472008-02-04 22:29:50 -080088
89#define SEQ_READ_FINISHED 1
90
91/*
Casey Schauflere114e472008-02-04 22:29:50 -080092 * Values for parsing cipso rules
93 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -070094 * SMK_CIPSOMIN: Minimum possible cipso rule length.
95 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -080096 */
97#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -070098#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
99#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
100
101/*
102 * Values for parsing MAC rules
103 * SMK_ACCESS: Maximum possible combination of access permissions
104 * SMK_ACCESSLEN: Maximum length for a rule access field
105 * SMK_LOADLEN: Smack rule length
106 */
107#define SMK_ACCESS "rwxa"
108#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
109#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
110
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500111/**
112 * smk_netlabel_audit_set - fill a netlbl_audit struct
113 * @nap: structure to fill
114 */
115static void smk_netlabel_audit_set(struct netlbl_audit *nap)
116{
117 nap->loginuid = audit_get_loginuid(current);
118 nap->sessionid = audit_get_sessionid(current);
119 nap->secid = smack_to_secid(current_security());
120}
121
122/*
123 * Values for parsing single label host rules
124 * "1.2.3.4 X"
125 * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
126 */
127#define SMK_NETLBLADDRMIN 9
128#define SMK_NETLBLADDRMAX 42
Casey Schauflere114e472008-02-04 22:29:50 -0800129
130/*
131 * Seq_file read operations for /smack/load
132 */
133
134static void *load_seq_start(struct seq_file *s, loff_t *pos)
135{
136 if (*pos == SEQ_READ_FINISHED)
137 return NULL;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100138 if (list_empty(&smack_rule_list))
139 return NULL;
140 return smack_rule_list.next;
Casey Schauflere114e472008-02-04 22:29:50 -0800141}
142
143static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
144{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100145 struct list_head *list = v;
Casey Schauflere114e472008-02-04 22:29:50 -0800146
Etienne Basset7198e2e2009-03-24 20:53:24 +0100147 if (list_is_last(list, &smack_rule_list)) {
Casey Schauflere114e472008-02-04 22:29:50 -0800148 *pos = SEQ_READ_FINISHED;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100149 return NULL;
150 }
151 return list->next;
Casey Schauflere114e472008-02-04 22:29:50 -0800152}
153
154static int load_seq_show(struct seq_file *s, void *v)
155{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100156 struct list_head *list = v;
157 struct smack_rule *srp =
158 list_entry(list, struct smack_rule, list);
Casey Schauflere114e472008-02-04 22:29:50 -0800159
160 seq_printf(s, "%s %s", (char *)srp->smk_subject,
161 (char *)srp->smk_object);
162
163 seq_putc(s, ' ');
164
165 if (srp->smk_access & MAY_READ)
166 seq_putc(s, 'r');
167 if (srp->smk_access & MAY_WRITE)
168 seq_putc(s, 'w');
169 if (srp->smk_access & MAY_EXEC)
170 seq_putc(s, 'x');
171 if (srp->smk_access & MAY_APPEND)
172 seq_putc(s, 'a');
173 if (srp->smk_access == 0)
174 seq_putc(s, '-');
175
176 seq_putc(s, '\n');
177
178 return 0;
179}
180
181static void load_seq_stop(struct seq_file *s, void *v)
182{
183 /* No-op */
184}
185
186static struct seq_operations load_seq_ops = {
187 .start = load_seq_start,
188 .next = load_seq_next,
189 .show = load_seq_show,
190 .stop = load_seq_stop,
191};
192
193/**
194 * smk_open_load - open() for /smack/load
195 * @inode: inode structure representing file
196 * @file: "load" file pointer
197 *
198 * For reading, use load_seq_* seq_file reading operations.
199 */
200static int smk_open_load(struct inode *inode, struct file *file)
201{
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700202 return seq_open(file, &load_seq_ops);
Casey Schauflere114e472008-02-04 22:29:50 -0800203}
204
205/**
206 * smk_set_access - add a rule to the rule list
207 * @srp: the new rule to add
208 *
209 * Looks through the current subject/object/access list for
210 * the subject/object pair and replaces the access that was
211 * there. If the pair isn't found add it with the specified
212 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300213 *
214 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
215 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800216 */
Sergio Luis81ea7142008-12-22 01:16:15 -0300217static int smk_set_access(struct smack_rule *srp)
Casey Schauflere114e472008-02-04 22:29:50 -0800218{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100219 struct smack_rule *sp;
Sergio Luis81ea7142008-12-22 01:16:15 -0300220 int ret = 0;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100221 int found;
Casey Schauflere114e472008-02-04 22:29:50 -0800222 mutex_lock(&smack_list_lock);
223
Etienne Basset7198e2e2009-03-24 20:53:24 +0100224 found = 0;
225 list_for_each_entry_rcu(sp, &smack_rule_list, list) {
226 if (sp->smk_subject == srp->smk_subject &&
227 sp->smk_object == srp->smk_object) {
228 found = 1;
229 sp->smk_access = srp->smk_access;
Casey Schauflere114e472008-02-04 22:29:50 -0800230 break;
231 }
Casey Schauflere114e472008-02-04 22:29:50 -0800232 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100233 if (found == 0)
234 list_add_rcu(&srp->list, &smack_rule_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800235
236 mutex_unlock(&smack_list_lock);
237
Sergio Luis81ea7142008-12-22 01:16:15 -0300238 return ret;
Casey Schauflere114e472008-02-04 22:29:50 -0800239}
240
241/**
242 * smk_write_load - write() for /smack/load
Randy Dunlap251a2a92009-02-18 11:42:33 -0800243 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800244 * @buf: where to get the data from
245 * @count: bytes sent
246 * @ppos: where to start - must be 0
247 *
248 * Get one smack access rule from above.
249 * The format is exactly:
250 * char subject[SMK_LABELLEN]
251 * char object[SMK_LABELLEN]
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700252 * char access[SMK_ACCESSLEN]
Casey Schauflere114e472008-02-04 22:29:50 -0800253 *
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700254 * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
Casey Schauflere114e472008-02-04 22:29:50 -0800255 */
Casey Schauflere114e472008-02-04 22:29:50 -0800256static ssize_t smk_write_load(struct file *file, const char __user *buf,
257 size_t count, loff_t *ppos)
258{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100259 struct smack_rule *rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800260 char *data;
261 int rc = -EINVAL;
262
263 /*
264 * Must have privilege.
265 * No partial writes.
266 * Enough data must be present.
267 */
268 if (!capable(CAP_MAC_ADMIN))
269 return -EPERM;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100270
271 if (*ppos != 0 || count != SMK_LOADLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800272 return -EINVAL;
273
274 data = kzalloc(count, GFP_KERNEL);
275 if (data == NULL)
276 return -ENOMEM;
277
278 if (copy_from_user(data, buf, count) != 0) {
279 rc = -EFAULT;
280 goto out;
281 }
282
Etienne Basset7198e2e2009-03-24 20:53:24 +0100283 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
284 if (rule == NULL) {
285 rc = -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800286 goto out;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100287 }
Casey Schauflere114e472008-02-04 22:29:50 -0800288
Etienne Basset7198e2e2009-03-24 20:53:24 +0100289 rule->smk_subject = smk_import(data, 0);
290 if (rule->smk_subject == NULL)
291 goto out_free_rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800292
Etienne Basset7198e2e2009-03-24 20:53:24 +0100293 rule->smk_object = smk_import(data + SMK_LABELLEN, 0);
294 if (rule->smk_object == NULL)
295 goto out_free_rule;
296
297 rule->smk_access = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800298
299 switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
300 case '-':
301 break;
302 case 'r':
303 case 'R':
Etienne Basset7198e2e2009-03-24 20:53:24 +0100304 rule->smk_access |= MAY_READ;
Casey Schauflere114e472008-02-04 22:29:50 -0800305 break;
306 default:
Etienne Basset7198e2e2009-03-24 20:53:24 +0100307 goto out_free_rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800308 }
309
310 switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
311 case '-':
312 break;
313 case 'w':
314 case 'W':
Etienne Basset7198e2e2009-03-24 20:53:24 +0100315 rule->smk_access |= MAY_WRITE;
Casey Schauflere114e472008-02-04 22:29:50 -0800316 break;
317 default:
Etienne Basset7198e2e2009-03-24 20:53:24 +0100318 goto out_free_rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800319 }
320
321 switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
322 case '-':
323 break;
324 case 'x':
325 case 'X':
Etienne Basset7198e2e2009-03-24 20:53:24 +0100326 rule->smk_access |= MAY_EXEC;
Casey Schauflere114e472008-02-04 22:29:50 -0800327 break;
328 default:
Etienne Basset7198e2e2009-03-24 20:53:24 +0100329 goto out_free_rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800330 }
331
332 switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
333 case '-':
334 break;
335 case 'a':
336 case 'A':
Etienne Basset7198e2e2009-03-24 20:53:24 +0100337 rule->smk_access |= MAY_APPEND;
Casey Schauflere114e472008-02-04 22:29:50 -0800338 break;
339 default:
Etienne Basset7198e2e2009-03-24 20:53:24 +0100340 goto out_free_rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800341 }
342
Etienne Basset7198e2e2009-03-24 20:53:24 +0100343 rc = smk_set_access(rule);
Sergio Luis81ea7142008-12-22 01:16:15 -0300344
345 if (!rc)
346 rc = count;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100347 goto out;
Casey Schauflere114e472008-02-04 22:29:50 -0800348
Etienne Basset7198e2e2009-03-24 20:53:24 +0100349out_free_rule:
350 kfree(rule);
Casey Schauflere114e472008-02-04 22:29:50 -0800351out:
352 kfree(data);
353 return rc;
354}
355
356static const struct file_operations smk_load_ops = {
357 .open = smk_open_load,
358 .read = seq_read,
359 .llseek = seq_lseek,
360 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700361 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800362};
363
364/**
365 * smk_cipso_doi - initialize the CIPSO domain
366 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700367static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800368{
369 int rc;
370 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500371 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800372
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500373 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800374
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500375 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800376 if (rc != 0)
377 printk(KERN_WARNING "%s:%d remove rc = %d\n",
378 __func__, __LINE__, rc);
379
380 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
381 if (doip == NULL)
382 panic("smack: Failed to initialize cipso DOI.\n");
383 doip->map.std = NULL;
384 doip->doi = smk_cipso_doi_value;
385 doip->type = CIPSO_V4_MAP_PASS;
386 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
387 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
388 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
389
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500390 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400391 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500392 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800393 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400394 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500395 return;
396 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500397 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500398 if (rc != 0) {
399 printk(KERN_WARNING "%s:%d map add rc = %d\n",
400 __func__, __LINE__, rc);
401 kfree(doip);
402 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400403 }
Casey Schauflere114e472008-02-04 22:29:50 -0800404}
405
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800406/**
407 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800408 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800409 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700410static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800411{
412 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500413 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800414
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500415 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800416
417 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500418 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800419 if (rc != 0)
420 printk(KERN_WARNING "%s:%d remove rc = %d\n",
421 __func__, __LINE__, rc);
422 }
423
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500424 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
425 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800426 if (rc != 0)
427 printk(KERN_WARNING "%s:%d add rc = %d\n",
428 __func__, __LINE__, rc);
429}
430
Casey Schauflere114e472008-02-04 22:29:50 -0800431/*
432 * Seq_file read operations for /smack/cipso
433 */
434
435static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
436{
437 if (*pos == SEQ_READ_FINISHED)
438 return NULL;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100439 if (list_empty(&smack_known_list))
440 return NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800441
Etienne Basset7198e2e2009-03-24 20:53:24 +0100442 return smack_known_list.next;
Casey Schauflere114e472008-02-04 22:29:50 -0800443}
444
445static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
446{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100447 struct list_head *list = v;
Casey Schauflere114e472008-02-04 22:29:50 -0800448
449 /*
Etienne Basset7198e2e2009-03-24 20:53:24 +0100450 * labels with no associated cipso value wont be printed
451 * in cipso_seq_show
Casey Schauflere114e472008-02-04 22:29:50 -0800452 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100453 if (list_is_last(list, &smack_known_list)) {
Casey Schauflere114e472008-02-04 22:29:50 -0800454 *pos = SEQ_READ_FINISHED;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100455 return NULL;
456 }
Casey Schauflere114e472008-02-04 22:29:50 -0800457
Etienne Basset7198e2e2009-03-24 20:53:24 +0100458 return list->next;
Casey Schauflere114e472008-02-04 22:29:50 -0800459}
460
461/*
462 * Print cipso labels in format:
463 * label level[/cat[,cat]]
464 */
465static int cipso_seq_show(struct seq_file *s, void *v)
466{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100467 struct list_head *list = v;
468 struct smack_known *skp =
469 list_entry(list, struct smack_known, list);
Casey Schauflere114e472008-02-04 22:29:50 -0800470 struct smack_cipso *scp = skp->smk_cipso;
471 char *cbp;
472 char sep = '/';
473 int cat = 1;
474 int i;
475 unsigned char m;
476
477 if (scp == NULL)
478 return 0;
479
480 seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
481
482 cbp = scp->smk_catset;
483 for (i = 0; i < SMK_LABELLEN; i++)
484 for (m = 0x80; m != 0; m >>= 1) {
485 if (m & cbp[i]) {
486 seq_printf(s, "%c%d", sep, cat);
487 sep = ',';
488 }
489 cat++;
490 }
491
492 seq_putc(s, '\n');
493
494 return 0;
495}
496
497static void cipso_seq_stop(struct seq_file *s, void *v)
498{
499 /* No-op */
500}
501
502static struct seq_operations cipso_seq_ops = {
503 .start = cipso_seq_start,
504 .stop = cipso_seq_stop,
505 .next = cipso_seq_next,
506 .show = cipso_seq_show,
507};
508
509/**
510 * smk_open_cipso - open() for /smack/cipso
511 * @inode: inode structure representing file
512 * @file: "cipso" file pointer
513 *
514 * Connect our cipso_seq_* operations with /smack/cipso
515 * file_operations
516 */
517static int smk_open_cipso(struct inode *inode, struct file *file)
518{
519 return seq_open(file, &cipso_seq_ops);
520}
521
522/**
523 * smk_write_cipso - write() for /smack/cipso
Randy Dunlap251a2a92009-02-18 11:42:33 -0800524 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800525 * @buf: where to get the data from
526 * @count: bytes sent
527 * @ppos: where to start
528 *
529 * Accepts only one cipso rule per write call.
530 * Returns number of bytes written or error code, as appropriate
531 */
532static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
533 size_t count, loff_t *ppos)
534{
535 struct smack_known *skp;
536 struct smack_cipso *scp = NULL;
537 char mapcatset[SMK_LABELLEN];
538 int maplevel;
539 int cat;
540 int catlen;
541 ssize_t rc = -EINVAL;
542 char *data = NULL;
543 char *rule;
544 int ret;
545 int i;
546
547 /*
548 * Must have privilege.
549 * No partial writes.
550 * Enough data must be present.
551 */
552 if (!capable(CAP_MAC_ADMIN))
553 return -EPERM;
554 if (*ppos != 0)
555 return -EINVAL;
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700556 if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
Casey Schauflere114e472008-02-04 22:29:50 -0800557 return -EINVAL;
558
559 data = kzalloc(count + 1, GFP_KERNEL);
560 if (data == NULL)
561 return -ENOMEM;
562
563 if (copy_from_user(data, buf, count) != 0) {
564 rc = -EFAULT;
565 goto unlockedout;
566 }
567
568 data[count] = '\0';
569 rule = data;
570 /*
571 * Only allow one writer at a time. Writes should be
572 * quite rare and small in any case.
573 */
574 mutex_lock(&smack_cipso_lock);
575
576 skp = smk_import_entry(rule, 0);
577 if (skp == NULL)
578 goto out;
579
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800580 rule += SMK_LABELLEN;
Casey Schauflere114e472008-02-04 22:29:50 -0800581 ret = sscanf(rule, "%d", &maplevel);
582 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
583 goto out;
584
585 rule += SMK_DIGITLEN;
586 ret = sscanf(rule, "%d", &catlen);
587 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
588 goto out;
589
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700590 if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800591 goto out;
592
593 memset(mapcatset, 0, sizeof(mapcatset));
594
595 for (i = 0; i < catlen; i++) {
596 rule += SMK_DIGITLEN;
597 ret = sscanf(rule, "%d", &cat);
598 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
599 goto out;
600
601 smack_catset_bit(cat, mapcatset);
602 }
603
604 if (skp->smk_cipso == NULL) {
605 scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
606 if (scp == NULL) {
607 rc = -ENOMEM;
608 goto out;
609 }
610 }
611
612 spin_lock_bh(&skp->smk_cipsolock);
613
614 if (scp == NULL)
615 scp = skp->smk_cipso;
616 else
617 skp->smk_cipso = scp;
618
619 scp->smk_level = maplevel;
620 memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
621
622 spin_unlock_bh(&skp->smk_cipsolock);
623
624 rc = count;
625out:
626 mutex_unlock(&smack_cipso_lock);
627unlockedout:
628 kfree(data);
629 return rc;
630}
631
632static const struct file_operations smk_cipso_ops = {
633 .open = smk_open_cipso,
634 .read = seq_read,
635 .llseek = seq_lseek,
636 .write = smk_write_cipso,
637 .release = seq_release,
638};
639
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500640/*
641 * Seq_file read operations for /smack/netlabel
642 */
643
644static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
645{
646 if (*pos == SEQ_READ_FINISHED)
647 return NULL;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100648 if (list_empty(&smk_netlbladdr_list))
649 return NULL;
650 return smk_netlbladdr_list.next;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500651}
652
653static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
654{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100655 struct list_head *list = v;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500656
Etienne Basset7198e2e2009-03-24 20:53:24 +0100657 if (list_is_last(list, &smk_netlbladdr_list)) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500658 *pos = SEQ_READ_FINISHED;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100659 return NULL;
660 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500661
Etienne Basset7198e2e2009-03-24 20:53:24 +0100662 return list->next;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500663}
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500664#define BEBITS (sizeof(__be32) * 8)
665
666/*
667 * Print host/label pairs
668 */
669static int netlbladdr_seq_show(struct seq_file *s, void *v)
670{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100671 struct list_head *list = v;
672 struct smk_netlbladdr *skp =
673 list_entry(list, struct smk_netlbladdr, list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500674 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +0100675 int maskn;
676 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500677
etienne113a0e42009-03-04 07:33:51 +0100678 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500679
680 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
681 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
682
683 return 0;
684}
685
686static void netlbladdr_seq_stop(struct seq_file *s, void *v)
687{
688 /* No-op */
689}
690
691static struct seq_operations netlbladdr_seq_ops = {
692 .start = netlbladdr_seq_start,
693 .stop = netlbladdr_seq_stop,
694 .next = netlbladdr_seq_next,
695 .show = netlbladdr_seq_show,
696};
697
698/**
699 * smk_open_netlbladdr - open() for /smack/netlabel
700 * @inode: inode structure representing file
701 * @file: "netlabel" file pointer
702 *
703 * Connect our netlbladdr_seq_* operations with /smack/netlabel
704 * file_operations
705 */
706static int smk_open_netlbladdr(struct inode *inode, struct file *file)
707{
708 return seq_open(file, &netlbladdr_seq_ops);
709}
710
711/**
etienne113a0e42009-03-04 07:33:51 +0100712 * smk_netlbladdr_insert
713 * @new : netlabel to insert
714 *
715 * This helper insert netlabel in the smack_netlbladdrs list
716 * sorted by netmask length (longest to smallest)
Etienne Basset7198e2e2009-03-24 20:53:24 +0100717 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
718 *
etienne113a0e42009-03-04 07:33:51 +0100719 */
720static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
721{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100722 struct smk_netlbladdr *m, *m_next;
etienne113a0e42009-03-04 07:33:51 +0100723
Etienne Basset7198e2e2009-03-24 20:53:24 +0100724 if (list_empty(&smk_netlbladdr_list)) {
725 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +0100726 return;
727 }
728
Etienne Basset7198e2e2009-03-24 20:53:24 +0100729 m = list_entry(rcu_dereference(smk_netlbladdr_list.next),
730 struct smk_netlbladdr, list);
731
etienne113a0e42009-03-04 07:33:51 +0100732 /* the comparison '>' is a bit hacky, but works */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100733 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
734 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +0100735 return;
736 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100737
738 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
739 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
740 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +0100741 return;
742 }
Etienne Basset7198e2e2009-03-24 20:53:24 +0100743 m_next = list_entry(rcu_dereference(m->list.next),
744 struct smk_netlbladdr, list);
745 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
746 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +0100747 return;
748 }
749 }
750}
751
752
753/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500754 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -0800755 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500756 * @buf: where to get the data from
757 * @count: bytes sent
758 * @ppos: where to start
759 *
760 * Accepts only one netlbladdr per write call.
761 * Returns number of bytes written or error code, as appropriate
762 */
763static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
764 size_t count, loff_t *ppos)
765{
766 struct smk_netlbladdr *skp;
767 struct sockaddr_in newname;
768 char smack[SMK_LABELLEN];
769 char *sp;
770 char data[SMK_NETLBLADDRMAX];
771 char *host = (char *)&newname.sin_addr.s_addr;
772 int rc;
773 struct netlbl_audit audit_info;
774 struct in_addr mask;
775 unsigned int m;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100776 int found;
etienne113a0e42009-03-04 07:33:51 +0100777 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500778 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +0100779 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500780
781 /*
782 * Must have privilege.
783 * No partial writes.
784 * Enough data must be present.
785 * "<addr/mask, as a.b.c.d/e><space><label>"
786 * "<addr, as a.b.c.d><space><label>"
787 */
788 if (!capable(CAP_MAC_ADMIN))
789 return -EPERM;
790 if (*ppos != 0)
791 return -EINVAL;
792 if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
793 return -EINVAL;
794 if (copy_from_user(data, buf, count) != 0)
795 return -EFAULT;
796
797 data[count] = '\0';
798
799 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
800 &host[0], &host[1], &host[2], &host[3], &m, smack);
801 if (rc != 6) {
802 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
803 &host[0], &host[1], &host[2], &host[3], smack);
804 if (rc != 5)
805 return -EINVAL;
806 m = BEBITS;
807 }
808 if (m > BEBITS)
809 return -EINVAL;
810
811 sp = smk_import(smack, 0);
812 if (sp == NULL)
813 return -EINVAL;
814
etienne113a0e42009-03-04 07:33:51 +0100815 for (temp_mask = 0; m > 0; m--) {
816 temp_mask |= mask_bits;
817 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500818 }
etienne113a0e42009-03-04 07:33:51 +0100819 mask.s_addr = cpu_to_be32(temp_mask);
820
821 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500822 /*
823 * Only allow one writer at a time. Writes should be
824 * quite rare and small in any case.
825 */
826 mutex_lock(&smk_netlbladdr_lock);
827
828 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +0100829 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100830 found = 0;
831 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500832 if (skp->smk_host.sin_addr.s_addr == nsa &&
Etienne Basset7198e2e2009-03-24 20:53:24 +0100833 skp->smk_mask.s_addr == mask.s_addr) {
834 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500835 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100836 }
837 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500838 smk_netlabel_audit_set(&audit_info);
839
Etienne Basset7198e2e2009-03-24 20:53:24 +0100840 if (found == 0) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500841 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
842 if (skp == NULL)
843 rc = -ENOMEM;
844 else {
845 rc = 0;
846 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
847 skp->smk_mask.s_addr = mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500848 skp->smk_label = sp;
etienne113a0e42009-03-04 07:33:51 +0100849 smk_netlbladdr_insert(skp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500850 }
851 } else {
852 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
853 &skp->smk_host.sin_addr, &skp->smk_mask,
854 PF_INET, &audit_info);
855 skp->smk_label = sp;
856 }
857
858 /*
859 * Now tell netlabel about the single label nature of
860 * this host so that incoming packets get labeled.
861 */
862
863 if (rc == 0)
864 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
865 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
866 smack_to_secid(skp->smk_label), &audit_info);
867
868 if (rc == 0)
869 rc = count;
870
871 mutex_unlock(&smk_netlbladdr_lock);
872
873 return rc;
874}
875
876static const struct file_operations smk_netlbladdr_ops = {
877 .open = smk_open_netlbladdr,
878 .read = seq_read,
879 .llseek = seq_lseek,
880 .write = smk_write_netlbladdr,
881 .release = seq_release,
882};
883
Casey Schauflere114e472008-02-04 22:29:50 -0800884/**
885 * smk_read_doi - read() for /smack/doi
886 * @filp: file pointer, not actually used
887 * @buf: where to put the result
888 * @count: maximum to send along
889 * @ppos: where to start
890 *
891 * Returns number of bytes read or error code, as appropriate
892 */
893static ssize_t smk_read_doi(struct file *filp, char __user *buf,
894 size_t count, loff_t *ppos)
895{
896 char temp[80];
897 ssize_t rc;
898
899 if (*ppos != 0)
900 return 0;
901
902 sprintf(temp, "%d", smk_cipso_doi_value);
903 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
904
905 return rc;
906}
907
908/**
909 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -0800910 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800911 * @buf: where to get the data from
912 * @count: bytes sent
913 * @ppos: where to start
914 *
915 * Returns number of bytes written or error code, as appropriate
916 */
917static ssize_t smk_write_doi(struct file *file, const char __user *buf,
918 size_t count, loff_t *ppos)
919{
920 char temp[80];
921 int i;
922
923 if (!capable(CAP_MAC_ADMIN))
924 return -EPERM;
925
926 if (count >= sizeof(temp) || count == 0)
927 return -EINVAL;
928
929 if (copy_from_user(temp, buf, count) != 0)
930 return -EFAULT;
931
932 temp[count] = '\0';
933
934 if (sscanf(temp, "%d", &i) != 1)
935 return -EINVAL;
936
937 smk_cipso_doi_value = i;
938
939 smk_cipso_doi();
940
941 return count;
942}
943
944static const struct file_operations smk_doi_ops = {
945 .read = smk_read_doi,
946 .write = smk_write_doi,
947};
948
949/**
950 * smk_read_direct - read() for /smack/direct
951 * @filp: file pointer, not actually used
952 * @buf: where to put the result
953 * @count: maximum to send along
954 * @ppos: where to start
955 *
956 * Returns number of bytes read or error code, as appropriate
957 */
958static ssize_t smk_read_direct(struct file *filp, char __user *buf,
959 size_t count, loff_t *ppos)
960{
961 char temp[80];
962 ssize_t rc;
963
964 if (*ppos != 0)
965 return 0;
966
967 sprintf(temp, "%d", smack_cipso_direct);
968 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
969
970 return rc;
971}
972
973/**
974 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -0800975 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800976 * @buf: where to get the data from
977 * @count: bytes sent
978 * @ppos: where to start
979 *
980 * Returns number of bytes written or error code, as appropriate
981 */
982static ssize_t smk_write_direct(struct file *file, const char __user *buf,
983 size_t count, loff_t *ppos)
984{
985 char temp[80];
986 int i;
987
988 if (!capable(CAP_MAC_ADMIN))
989 return -EPERM;
990
991 if (count >= sizeof(temp) || count == 0)
992 return -EINVAL;
993
994 if (copy_from_user(temp, buf, count) != 0)
995 return -EFAULT;
996
997 temp[count] = '\0';
998
999 if (sscanf(temp, "%d", &i) != 1)
1000 return -EINVAL;
1001
1002 smack_cipso_direct = i;
1003
1004 return count;
1005}
1006
1007static const struct file_operations smk_direct_ops = {
1008 .read = smk_read_direct,
1009 .write = smk_write_direct,
1010};
1011
1012/**
1013 * smk_read_ambient - read() for /smack/ambient
1014 * @filp: file pointer, not actually used
1015 * @buf: where to put the result
1016 * @cn: maximum to send along
1017 * @ppos: where to start
1018 *
1019 * Returns number of bytes read or error code, as appropriate
1020 */
1021static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1022 size_t cn, loff_t *ppos)
1023{
1024 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001025 int asize;
1026
1027 if (*ppos != 0)
1028 return 0;
1029 /*
1030 * Being careful to avoid a problem in the case where
1031 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001032 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001033 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001034
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001035 asize = strlen(smack_net_ambient) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001036
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001037 if (cn >= asize)
1038 rc = simple_read_from_buffer(buf, cn, ppos,
1039 smack_net_ambient, asize);
1040 else
1041 rc = -EINVAL;
1042
1043 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001044
1045 return rc;
1046}
1047
1048/**
1049 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001050 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001051 * @buf: where to get the data from
1052 * @count: bytes sent
1053 * @ppos: where to start
1054 *
1055 * Returns number of bytes written or error code, as appropriate
1056 */
1057static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1058 size_t count, loff_t *ppos)
1059{
1060 char in[SMK_LABELLEN];
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001061 char *oldambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001062 char *smack;
1063
1064 if (!capable(CAP_MAC_ADMIN))
1065 return -EPERM;
1066
1067 if (count >= SMK_LABELLEN)
1068 return -EINVAL;
1069
1070 if (copy_from_user(in, buf, count) != 0)
1071 return -EFAULT;
1072
1073 smack = smk_import(in, count);
1074 if (smack == NULL)
1075 return -EINVAL;
1076
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001077 mutex_lock(&smack_ambient_lock);
1078
1079 oldambient = smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001080 smack_net_ambient = smack;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001081 smk_unlbl_ambient(oldambient);
1082
1083 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001084
1085 return count;
1086}
1087
1088static const struct file_operations smk_ambient_ops = {
1089 .read = smk_read_ambient,
1090 .write = smk_write_ambient,
1091};
1092
Casey Schaufler15446232008-07-30 15:37:11 -07001093/**
1094 * smk_read_onlycap - read() for /smack/onlycap
1095 * @filp: file pointer, not actually used
1096 * @buf: where to put the result
1097 * @cn: maximum to send along
1098 * @ppos: where to start
1099 *
1100 * Returns number of bytes read or error code, as appropriate
1101 */
1102static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1103 size_t cn, loff_t *ppos)
1104{
1105 char *smack = "";
1106 ssize_t rc = -EINVAL;
1107 int asize;
1108
1109 if (*ppos != 0)
1110 return 0;
1111
1112 if (smack_onlycap != NULL)
1113 smack = smack_onlycap;
1114
1115 asize = strlen(smack) + 1;
1116
1117 if (cn >= asize)
1118 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1119
1120 return rc;
1121}
1122
1123/**
1124 * smk_write_onlycap - write() for /smack/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001125 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001126 * @buf: where to get the data from
1127 * @count: bytes sent
1128 * @ppos: where to start
1129 *
1130 * Returns number of bytes written or error code, as appropriate
1131 */
1132static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1133 size_t count, loff_t *ppos)
1134{
1135 char in[SMK_LABELLEN];
David Howellsb6dff3e2008-11-14 10:39:16 +11001136 char *sp = current->cred->security;
Casey Schaufler15446232008-07-30 15:37:11 -07001137
1138 if (!capable(CAP_MAC_ADMIN))
1139 return -EPERM;
1140
1141 /*
1142 * This can be done using smk_access() but is done
1143 * explicitly for clarity. The smk_access() implementation
1144 * would use smk_access(smack_onlycap, MAY_WRITE)
1145 */
1146 if (smack_onlycap != NULL && smack_onlycap != sp)
1147 return -EPERM;
1148
1149 if (count >= SMK_LABELLEN)
1150 return -EINVAL;
1151
1152 if (copy_from_user(in, buf, count) != 0)
1153 return -EFAULT;
1154
1155 /*
1156 * Should the null string be passed in unset the onlycap value.
1157 * This seems like something to be careful with as usually
1158 * smk_import only expects to return NULL for errors. It
1159 * is usually the case that a nullstring or "\n" would be
1160 * bad to pass to smk_import but in fact this is useful here.
1161 */
1162 smack_onlycap = smk_import(in, count);
1163
1164 return count;
1165}
1166
1167static const struct file_operations smk_onlycap_ops = {
1168 .read = smk_read_onlycap,
1169 .write = smk_write_onlycap,
1170};
1171
Casey Schauflere114e472008-02-04 22:29:50 -08001172/**
1173 * smk_fill_super - fill the /smackfs superblock
1174 * @sb: the empty superblock
1175 * @data: unused
1176 * @silent: unused
1177 *
1178 * Fill in the well known entries for /smack
1179 *
1180 * Returns 0 on success, an error code on failure
1181 */
1182static int smk_fill_super(struct super_block *sb, void *data, int silent)
1183{
1184 int rc;
1185 struct inode *root_inode;
1186
1187 static struct tree_descr smack_files[] = {
1188 [SMK_LOAD] =
1189 {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
1190 [SMK_CIPSO] =
1191 {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
1192 [SMK_DOI] =
1193 {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
1194 [SMK_DIRECT] =
1195 {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
1196 [SMK_AMBIENT] =
1197 {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001198 [SMK_NETLBLADDR] =
1199 {"netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
Casey Schaufler15446232008-07-30 15:37:11 -07001200 [SMK_ONLYCAP] =
1201 {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
Casey Schauflere114e472008-02-04 22:29:50 -08001202 /* last one */ {""}
1203 };
1204
1205 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
1206 if (rc != 0) {
1207 printk(KERN_ERR "%s failed %d while creating inodes\n",
1208 __func__, rc);
1209 return rc;
1210 }
1211
1212 root_inode = sb->s_root->d_inode;
1213 root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
1214
1215 return 0;
1216}
1217
1218/**
1219 * smk_get_sb - get the smackfs superblock
1220 * @fs_type: passed along without comment
1221 * @flags: passed along without comment
1222 * @dev_name: passed along without comment
1223 * @data: passed along without comment
1224 * @mnt: passed along without comment
1225 *
1226 * Just passes everything along.
1227 *
1228 * Returns what the lower level code does.
1229 */
1230static int smk_get_sb(struct file_system_type *fs_type,
1231 int flags, const char *dev_name, void *data,
1232 struct vfsmount *mnt)
1233{
1234 return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
1235}
1236
1237static struct file_system_type smk_fs_type = {
1238 .name = "smackfs",
1239 .get_sb = smk_get_sb,
1240 .kill_sb = kill_litter_super,
1241};
1242
1243static struct vfsmount *smackfs_mount;
1244
1245/**
1246 * init_smk_fs - get the smackfs superblock
1247 *
1248 * register the smackfs
1249 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02001250 * Do not register smackfs if Smack wasn't enabled
1251 * on boot. We can not put this method normally under the
1252 * smack_init() code path since the security subsystem get
1253 * initialized before the vfs caches.
1254 *
1255 * Returns true if we were not chosen on boot or if
1256 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08001257 */
1258static int __init init_smk_fs(void)
1259{
1260 int err;
1261
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02001262 if (!security_module_enable(&smack_ops))
1263 return 0;
1264
Casey Schauflere114e472008-02-04 22:29:50 -08001265 err = register_filesystem(&smk_fs_type);
1266 if (!err) {
1267 smackfs_mount = kern_mount(&smk_fs_type);
1268 if (IS_ERR(smackfs_mount)) {
1269 printk(KERN_ERR "smackfs: could not mount!\n");
1270 err = PTR_ERR(smackfs_mount);
1271 smackfs_mount = NULL;
1272 }
1273 }
1274
Casey Schauflere114e472008-02-04 22:29:50 -08001275 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001276 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001277
1278 return err;
1279}
1280
1281__initcall(init_smk_fs);