blob: 53a08b85bda4837936339f7158474994af8a7ba6 [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/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 Schaufler958d2c22013-04-02 11:41:18 -070029#include <linux/magic.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 Schauflerf7112e62012-05-06 15:22:02 -070048 SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
49 SMK_LOAD2 = 14, /* load policy with long labels */
50 SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
51 SMK_ACCESS2 = 16, /* make an access check with long labels */
52 SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
Rafal Krypa449543b2012-07-11 17:49:30 +020053 SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
Rafal Krypae05b6f92013-01-10 19:42:00 +010054 SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
Casey Schauflere114e472008-02-04 22:29:50 -080055};
56
57/*
58 * List locks
59 */
Casey Schauflere114e472008-02-04 22:29:50 -080060static DEFINE_MUTEX(smack_cipso_lock);
Casey Schaufler4bc87e62008-02-15 15:24:25 -080061static DEFINE_MUTEX(smack_ambient_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -050062static DEFINE_MUTEX(smk_netlbladdr_lock);
Casey Schauflere114e472008-02-04 22:29:50 -080063
64/*
65 * This is the "ambient" label for network traffic.
66 * If it isn't somehow marked, use this.
67 * It can be reset via smackfs/ambient
68 */
Casey Schauflerf7112e62012-05-06 15:22:02 -070069char *smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -080070
71/*
Casey Schauflere114e472008-02-04 22:29:50 -080072 * This is the level in a CIPSO header that indicates a
73 * smack label is contained directly in the category set.
74 * It can be reset via smackfs/direct
75 */
76int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
77
Casey Schaufler15446232008-07-30 15:37:11 -070078/*
Casey Schauflerf7112e62012-05-06 15:22:02 -070079 * This is the level in a CIPSO header that indicates a
80 * secid is contained directly in the category set.
81 * It can be reset via smackfs/mapped
82 */
83int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
84
85/*
Casey Schaufler15446232008-07-30 15:37:11 -070086 * Unless a process is running with this label even
87 * having CAP_MAC_OVERRIDE isn't enough to grant
88 * privilege to violate MAC policy. If no label is
89 * designated (the NULL case) capabilities apply to
90 * everyone. It is expected that the hat (^) label
91 * will be used if any label is used.
92 */
93char *smack_onlycap;
94
Casey Schaufler6d3dc072008-12-31 12:54:12 -050095/*
96 * Certain IP addresses may be designated as single label hosts.
97 * Packets are sent there unlabeled, but only from tasks that
98 * can write to the specified label.
99 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100100
101LIST_HEAD(smk_netlbladdr_list);
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700102
103/*
104 * Rule lists are maintained for each label.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700105 * This master list is just for reading /smack/load and /smack/load2.
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700106 */
107struct smack_master_list {
108 struct list_head list;
109 struct smack_rule *smk_rule;
110};
111
Etienne Basset7198e2e2009-03-24 20:53:24 +0100112LIST_HEAD(smack_rule_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500113
Rafal Krypae05b6f92013-01-10 19:42:00 +0100114struct smack_parsed_rule {
115 char *smk_subject;
116 char *smk_object;
117 int smk_access1;
118 int smk_access2;
119};
120
Casey Schauflere114e472008-02-04 22:29:50 -0800121static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
Casey Schauflere114e472008-02-04 22:29:50 -0800122
Etienne Basset43031542009-03-27 17:11:01 -0400123const char *smack_cipso_option = SMACK_CIPSO_OPTION;
124
Casey Schauflere114e472008-02-04 22:29:50 -0800125/*
Casey Schauflere114e472008-02-04 22:29:50 -0800126 * Values for parsing cipso rules
127 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700128 * SMK_CIPSOMIN: Minimum possible cipso rule length.
129 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -0800130 */
131#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700132#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
133#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
134
135/*
136 * Values for parsing MAC rules
137 * SMK_ACCESS: Maximum possible combination of access permissions
138 * SMK_ACCESSLEN: Maximum length for a rule access field
139 * SMK_LOADLEN: Smack rule length
140 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200141#define SMK_OACCESS "rwxa"
142#define SMK_ACCESS "rwxat"
143#define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
144#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
145#define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
146#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700147
Casey Schauflerf7112e62012-05-06 15:22:02 -0700148/*
149 * Stricly for CIPSO level manipulation.
150 * Set the category bit number in a smack label sized buffer.
151 */
152static inline void smack_catset_bit(unsigned int cat, char *catsetp)
153{
154 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
155 return;
156
157 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
158}
159
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500160/**
161 * smk_netlabel_audit_set - fill a netlbl_audit struct
162 * @nap: structure to fill
163 */
164static void smk_netlabel_audit_set(struct netlbl_audit *nap)
165{
166 nap->loginuid = audit_get_loginuid(current);
167 nap->sessionid = audit_get_sessionid(current);
Casey Schaufler676dac42010-12-02 06:43:39 -0800168 nap->secid = smack_to_secid(smk_of_current());
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500169}
170
171/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700172 * Value for parsing single label host rules
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500173 * "1.2.3.4 X"
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500174 */
175#define SMK_NETLBLADDRMIN 9
Casey Schauflere114e472008-02-04 22:29:50 -0800176
Casey Schauflere114e472008-02-04 22:29:50 -0800177/**
Rafal Krypae05b6f92013-01-10 19:42:00 +0100178 * smk_set_access - add a rule to the rule list or replace an old rule
179 * @srp: the rule to add or replace
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800180 * @rule_list: the list of rules
181 * @rule_lock: the rule list lock
Rafal Krypae05b6f92013-01-10 19:42:00 +0100182 * @global: if non-zero, indicates a global rule
Casey Schauflere114e472008-02-04 22:29:50 -0800183 *
184 * Looks through the current subject/object/access list for
185 * the subject/object pair and replaces the access that was
186 * there. If the pair isn't found add it with the specified
187 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300188 *
189 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
190 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800191 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100192static int smk_set_access(struct smack_parsed_rule *srp,
193 struct list_head *rule_list,
194 struct mutex *rule_lock, int global)
Casey Schauflere114e472008-02-04 22:29:50 -0800195{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100196 struct smack_rule *sp;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100197 struct smack_master_list *smlp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800198 int found = 0;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100199 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800200
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800201 mutex_lock(rule_lock);
202
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700203 /*
204 * Because the object label is less likely to match
205 * than the subject label check it first
206 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800207 list_for_each_entry_rcu(sp, rule_list, list) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700208 if (sp->smk_object == srp->smk_object &&
209 sp->smk_subject == srp->smk_subject) {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100210 found = 1;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100211 sp->smk_access |= srp->smk_access1;
212 sp->smk_access &= ~srp->smk_access2;
Casey Schauflere114e472008-02-04 22:29:50 -0800213 break;
214 }
Casey Schauflere114e472008-02-04 22:29:50 -0800215 }
216
Rafal Krypae05b6f92013-01-10 19:42:00 +0100217 if (found == 0) {
218 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
219 if (sp == NULL) {
220 rc = -ENOMEM;
221 goto out;
222 }
223
224 sp->smk_subject = srp->smk_subject;
225 sp->smk_object = srp->smk_object;
226 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
227
228 list_add_rcu(&sp->list, rule_list);
229 /*
230 * If this is a global as opposed to self and a new rule
231 * it needs to get added for reporting.
232 */
233 if (global) {
234 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
235 if (smlp != NULL) {
236 smlp->smk_rule = sp;
237 list_add_rcu(&smlp->list, &smack_rule_list);
238 } else
239 rc = -ENOMEM;
240 }
241 }
242
243out:
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800244 mutex_unlock(rule_lock);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100245 return rc;
246}
Casey Schauflere114e472008-02-04 22:29:50 -0800247
Rafal Krypae05b6f92013-01-10 19:42:00 +0100248/**
249 * smk_perm_from_str - parse smack accesses from a text string
250 * @string: a text string that contains a Smack accesses code
251 *
252 * Returns an integer with respective bits set for specified accesses.
253 */
254static int smk_perm_from_str(const char *string)
255{
256 int perm = 0;
257 const char *cp;
258
259 for (cp = string; ; cp++)
260 switch (*cp) {
261 case '-':
262 break;
263 case 'r':
264 case 'R':
265 perm |= MAY_READ;
266 break;
267 case 'w':
268 case 'W':
269 perm |= MAY_WRITE;
270 break;
271 case 'x':
272 case 'X':
273 perm |= MAY_EXEC;
274 break;
275 case 'a':
276 case 'A':
277 perm |= MAY_APPEND;
278 break;
279 case 't':
280 case 'T':
281 perm |= MAY_TRANSMUTE;
282 break;
283 default:
284 return perm;
285 }
Casey Schauflere114e472008-02-04 22:29:50 -0800286}
287
288/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700289 * smk_fill_rule - Fill Smack rule from strings
290 * @subject: subject label string
291 * @object: object label string
Rafal Krypae05b6f92013-01-10 19:42:00 +0100292 * @access1: access string
293 * @access2: string with permissions to be removed
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300294 * @rule: Smack rule
295 * @import: if non-zero, import labels
Casey Schaufler35187212012-06-18 19:01:36 -0700296 * @len: label length limit
Casey Schauflerf7112e62012-05-06 15:22:02 -0700297 *
298 * Returns 0 on success, -1 on failure
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300299 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700300static int smk_fill_rule(const char *subject, const char *object,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100301 const char *access1, const char *access2,
302 struct smack_parsed_rule *rule, int import,
303 int len)
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300304{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700305 const char *cp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300306 struct smack_known *skp;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300307
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300308 if (import) {
Casey Schaufler35187212012-06-18 19:01:36 -0700309 rule->smk_subject = smk_import(subject, len);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300310 if (rule->smk_subject == NULL)
311 return -1;
312
Casey Schaufler35187212012-06-18 19:01:36 -0700313 rule->smk_object = smk_import(object, len);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300314 if (rule->smk_object == NULL)
315 return -1;
316 } else {
Casey Schaufler35187212012-06-18 19:01:36 -0700317 cp = smk_parse_smack(subject, len);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700318 if (cp == NULL)
319 return -1;
320 skp = smk_find_entry(cp);
321 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300322 if (skp == NULL)
323 return -1;
324 rule->smk_subject = skp->smk_known;
325
Casey Schaufler35187212012-06-18 19:01:36 -0700326 cp = smk_parse_smack(object, len);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700327 if (cp == NULL)
328 return -1;
329 skp = smk_find_entry(cp);
330 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300331 if (skp == NULL)
332 return -1;
333 rule->smk_object = skp->smk_known;
334 }
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300335
Rafal Krypae05b6f92013-01-10 19:42:00 +0100336 rule->smk_access1 = smk_perm_from_str(access1);
337 if (access2)
338 rule->smk_access2 = smk_perm_from_str(access2);
339 else
340 rule->smk_access2 = ~rule->smk_access1;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300341
Casey Schaufler35187212012-06-18 19:01:36 -0700342 return 0;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300343}
344
345/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700346 * smk_parse_rule - parse Smack rule from load string
347 * @data: string to be parsed whose size is SMK_LOADLEN
348 * @rule: Smack rule
349 * @import: if non-zero, import labels
350 *
351 * Returns 0 on success, -1 on errors.
352 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100353static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
354 int import)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700355{
356 int rc;
357
358 rc = smk_fill_rule(data, data + SMK_LABELLEN,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100359 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
360 import, SMK_LABELLEN);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700361 return rc;
362}
363
364/**
365 * smk_parse_long_rule - parse Smack rule from rule string
366 * @data: string to be parsed, null terminated
Rafal Krypae05b6f92013-01-10 19:42:00 +0100367 * @rule: Will be filled with Smack parsed rule
Casey Schauflerf7112e62012-05-06 15:22:02 -0700368 * @import: if non-zero, import labels
Rafal Krypae05b6f92013-01-10 19:42:00 +0100369 * @change: if non-zero, data is from /smack/change-rule
Casey Schauflerf7112e62012-05-06 15:22:02 -0700370 *
371 * Returns 0 on success, -1 on failure
372 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100373static int smk_parse_long_rule(const char *data, struct smack_parsed_rule *rule,
374 int import, int change)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700375{
376 char *subject;
377 char *object;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100378 char *access1;
379 char *access2;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700380 int datalen;
381 int rc = -1;
382
Alan Cox3b9fc372012-07-26 14:47:11 -0700383 /* This is inefficient */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700384 datalen = strlen(data);
Alan Cox3b9fc372012-07-26 14:47:11 -0700385
386 /* Our first element can be 64 + \0 with no spaces */
387 subject = kzalloc(datalen + 1, GFP_KERNEL);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700388 if (subject == NULL)
389 return -1;
390 object = kzalloc(datalen, GFP_KERNEL);
391 if (object == NULL)
392 goto free_out_s;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100393 access1 = kzalloc(datalen, GFP_KERNEL);
394 if (access1 == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700395 goto free_out_o;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100396 access2 = kzalloc(datalen, GFP_KERNEL);
397 if (access2 == NULL)
398 goto free_out_a;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700399
Rafal Krypae05b6f92013-01-10 19:42:00 +0100400 if (change) {
401 if (sscanf(data, "%s %s %s %s",
402 subject, object, access1, access2) == 4)
403 rc = smk_fill_rule(subject, object, access1, access2,
404 rule, import, 0);
405 } else {
406 if (sscanf(data, "%s %s %s", subject, object, access1) == 3)
407 rc = smk_fill_rule(subject, object, access1, NULL,
408 rule, import, 0);
409 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700410
Rafal Krypae05b6f92013-01-10 19:42:00 +0100411 kfree(access2);
412free_out_a:
413 kfree(access1);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700414free_out_o:
415 kfree(object);
416free_out_s:
417 kfree(subject);
418 return rc;
419}
420
421#define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
422#define SMK_LONG_FMT 1 /* Variable long label format */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100423#define SMK_CHANGE_FMT 2 /* Rule modification format */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700424/**
425 * smk_write_rules_list - write() for any /smack rule file
Randy Dunlap251a2a92009-02-18 11:42:33 -0800426 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800427 * @buf: where to get the data from
428 * @count: bytes sent
429 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800430 * @rule_list: the list of rules to write to
431 * @rule_lock: lock for the rule list
Rafal Krypae05b6f92013-01-10 19:42:00 +0100432 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
Casey Schauflere114e472008-02-04 22:29:50 -0800433 *
434 * Get one smack access rule from above.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700435 * The format for SMK_LONG_FMT is:
436 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
437 * The format for SMK_FIXED24_FMT is exactly:
438 * "subject object rwxat"
Rafal Krypae05b6f92013-01-10 19:42:00 +0100439 * The format for SMK_CHANGE_FMT is:
440 * "subject<whitespace>object<whitespace>
441 * acc_enable<whitespace>acc_disable[<whitespace>...]"
Casey Schauflere114e472008-02-04 22:29:50 -0800442 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700443static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
444 size_t count, loff_t *ppos,
445 struct list_head *rule_list,
446 struct mutex *rule_lock, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800447{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700448 struct smack_known *skp;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100449 struct smack_parsed_rule *rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800450 char *data;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700451 int datalen;
Casey Schauflere114e472008-02-04 22:29:50 -0800452 int rc = -EINVAL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700453 int load = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800454
455 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800456 * No partial writes.
457 * Enough data must be present.
458 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200459 if (*ppos != 0)
460 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -0800461
Casey Schauflerf7112e62012-05-06 15:22:02 -0700462 if (format == SMK_FIXED24_FMT) {
463 /*
464 * Minor hack for backward compatibility
465 */
466 if (count != SMK_OLOADLEN && count != SMK_LOADLEN)
467 return -EINVAL;
468 datalen = SMK_LOADLEN;
469 } else
470 datalen = count + 1;
471
472 data = kzalloc(datalen, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -0800473 if (data == NULL)
474 return -ENOMEM;
475
476 if (copy_from_user(data, buf, count) != 0) {
477 rc = -EFAULT;
478 goto out;
479 }
480
Etienne Basset7198e2e2009-03-24 20:53:24 +0100481 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
482 if (rule == NULL) {
483 rc = -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800484 goto out;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100485 }
Casey Schauflere114e472008-02-04 22:29:50 -0800486
Casey Schauflerf7112e62012-05-06 15:22:02 -0700487 if (format == SMK_LONG_FMT) {
488 /*
489 * Be sure the data string is terminated.
490 */
491 data[count] = '\0';
Rafal Krypae05b6f92013-01-10 19:42:00 +0100492 if (smk_parse_long_rule(data, rule, 1, 0))
493 goto out_free_rule;
494 } else if (format == SMK_CHANGE_FMT) {
495 data[count] = '\0';
496 if (smk_parse_long_rule(data, rule, 1, 1))
Casey Schauflerf7112e62012-05-06 15:22:02 -0700497 goto out_free_rule;
498 } else {
499 /*
500 * More on the minor hack for backward compatibility
501 */
502 if (count == (SMK_OLOADLEN))
503 data[SMK_OLOADLEN] = '-';
504 if (smk_parse_rule(data, rule, 1))
505 goto out_free_rule;
506 }
507
Casey Schauflere114e472008-02-04 22:29:50 -0800508
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700509 if (rule_list == NULL) {
510 load = 1;
511 skp = smk_find_entry(rule->smk_subject);
512 rule_list = &skp->smk_rules;
513 rule_lock = &skp->smk_rules_lock;
514 }
515
Rafal Krypae05b6f92013-01-10 19:42:00 +0100516 rc = smk_set_access(rule, rule_list, rule_lock, load);
517 if (rc == 0) {
518 rc = count;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800519 goto out;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700520 }
Casey Schauflere114e472008-02-04 22:29:50 -0800521
Etienne Basset7198e2e2009-03-24 20:53:24 +0100522out_free_rule:
523 kfree(rule);
Casey Schauflere114e472008-02-04 22:29:50 -0800524out:
525 kfree(data);
526 return rc;
527}
528
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800529/*
Casey Schaufler40809562011-11-10 15:02:22 -0800530 * Core logic for smackfs seq list operations.
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800531 */
532
Casey Schaufler40809562011-11-10 15:02:22 -0800533static void *smk_seq_start(struct seq_file *s, loff_t *pos,
534 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800535{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700536 struct list_head *list;
537
538 /*
539 * This is 0 the first time through.
540 */
541 if (s->index == 0)
Casey Schaufler40809562011-11-10 15:02:22 -0800542 s->private = head;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700543
544 if (s->private == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800545 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700546
547 list = s->private;
548 if (list_empty(list))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800549 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700550
551 if (s->index == 0)
552 return list->next;
553 return list;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800554}
555
Casey Schaufler40809562011-11-10 15:02:22 -0800556static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
557 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800558{
559 struct list_head *list = v;
560
Casey Schaufler40809562011-11-10 15:02:22 -0800561 if (list_is_last(list, head)) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700562 s->private = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800563 return NULL;
564 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700565 s->private = list->next;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800566 return list->next;
567}
568
Casey Schaufler40809562011-11-10 15:02:22 -0800569static void smk_seq_stop(struct seq_file *s, void *v)
570{
571 /* No-op */
572}
573
Casey Schauflerf7112e62012-05-06 15:22:02 -0700574static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
Casey Schaufler40809562011-11-10 15:02:22 -0800575{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700576 /*
577 * Don't show any rules with label names too long for
578 * interface file (/smack/load or /smack/load2)
579 * because you should expect to be able to write
580 * anything you read back.
581 */
582 if (strlen(srp->smk_subject) >= max || strlen(srp->smk_object) >= max)
583 return;
Casey Schaufler40809562011-11-10 15:02:22 -0800584
Rafal Krypa65ee7f42012-07-09 19:36:34 +0200585 if (srp->smk_access == 0)
586 return;
587
Casey Schauflerf7112e62012-05-06 15:22:02 -0700588 seq_printf(s, "%s %s", srp->smk_subject, srp->smk_object);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800589
590 seq_putc(s, ' ');
591
592 if (srp->smk_access & MAY_READ)
593 seq_putc(s, 'r');
594 if (srp->smk_access & MAY_WRITE)
595 seq_putc(s, 'w');
596 if (srp->smk_access & MAY_EXEC)
597 seq_putc(s, 'x');
598 if (srp->smk_access & MAY_APPEND)
599 seq_putc(s, 'a');
600 if (srp->smk_access & MAY_TRANSMUTE)
601 seq_putc(s, 't');
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800602
603 seq_putc(s, '\n');
Casey Schauflerf7112e62012-05-06 15:22:02 -0700604}
605
606/*
607 * Seq_file read operations for /smack/load
608 */
609
610static void *load2_seq_start(struct seq_file *s, loff_t *pos)
611{
612 return smk_seq_start(s, pos, &smack_rule_list);
613}
614
615static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
616{
617 return smk_seq_next(s, v, pos, &smack_rule_list);
618}
619
620static int load_seq_show(struct seq_file *s, void *v)
621{
622 struct list_head *list = v;
623 struct smack_master_list *smlp =
624 list_entry(list, struct smack_master_list, list);
625
626 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800627
628 return 0;
629}
630
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800631static const struct seq_operations load_seq_ops = {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700632 .start = load2_seq_start,
633 .next = load2_seq_next,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800634 .show = load_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800635 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800636};
637
638/**
639 * smk_open_load - open() for /smack/load
640 * @inode: inode structure representing file
641 * @file: "load" file pointer
642 *
643 * For reading, use load_seq_* seq_file reading operations.
644 */
645static int smk_open_load(struct inode *inode, struct file *file)
646{
647 return seq_open(file, &load_seq_ops);
648}
649
650/**
651 * smk_write_load - write() for /smack/load
652 * @file: file pointer, not actually used
653 * @buf: where to get the data from
654 * @count: bytes sent
655 * @ppos: where to start - must be 0
656 *
657 */
658static ssize_t smk_write_load(struct file *file, const char __user *buf,
659 size_t count, loff_t *ppos)
660{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800661 /*
662 * Must have privilege.
663 * No partial writes.
664 * Enough data must be present.
665 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700666 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800667 return -EPERM;
668
Casey Schauflerf7112e62012-05-06 15:22:02 -0700669 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
670 SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800671}
672
Casey Schauflere114e472008-02-04 22:29:50 -0800673static const struct file_operations smk_load_ops = {
674 .open = smk_open_load,
675 .read = seq_read,
676 .llseek = seq_lseek,
677 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700678 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800679};
680
681/**
682 * smk_cipso_doi - initialize the CIPSO domain
683 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700684static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800685{
686 int rc;
687 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500688 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800689
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500690 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800691
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500692 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800693 if (rc != 0)
694 printk(KERN_WARNING "%s:%d remove rc = %d\n",
695 __func__, __LINE__, rc);
696
697 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
698 if (doip == NULL)
699 panic("smack: Failed to initialize cipso DOI.\n");
700 doip->map.std = NULL;
701 doip->doi = smk_cipso_doi_value;
702 doip->type = CIPSO_V4_MAP_PASS;
703 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
704 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
705 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
706
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500707 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400708 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500709 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800710 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400711 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500712 return;
713 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500714 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500715 if (rc != 0) {
716 printk(KERN_WARNING "%s:%d map add rc = %d\n",
717 __func__, __LINE__, rc);
718 kfree(doip);
719 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400720 }
Casey Schauflere114e472008-02-04 22:29:50 -0800721}
722
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800723/**
724 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800725 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800726 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700727static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800728{
729 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500730 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800731
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500732 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800733
734 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500735 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800736 if (rc != 0)
737 printk(KERN_WARNING "%s:%d remove rc = %d\n",
738 __func__, __LINE__, rc);
739 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700740 if (smack_net_ambient == NULL)
741 smack_net_ambient = smack_known_floor.smk_known;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800742
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500743 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
744 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800745 if (rc != 0)
746 printk(KERN_WARNING "%s:%d add rc = %d\n",
747 __func__, __LINE__, rc);
748}
749
Casey Schauflere114e472008-02-04 22:29:50 -0800750/*
751 * Seq_file read operations for /smack/cipso
752 */
753
754static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
755{
Casey Schaufler40809562011-11-10 15:02:22 -0800756 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800757}
758
759static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
760{
Casey Schaufler40809562011-11-10 15:02:22 -0800761 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800762}
763
764/*
765 * Print cipso labels in format:
766 * label level[/cat[,cat]]
767 */
768static int cipso_seq_show(struct seq_file *s, void *v)
769{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100770 struct list_head *list = v;
771 struct smack_known *skp =
772 list_entry(list, struct smack_known, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700773 struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800774 char sep = '/';
Casey Schauflere114e472008-02-04 22:29:50 -0800775 int i;
Casey Schauflere114e472008-02-04 22:29:50 -0800776
Casey Schauflerf7112e62012-05-06 15:22:02 -0700777 /*
778 * Don't show a label that could not have been set using
779 * /smack/cipso. This is in support of the notion that
780 * anything read from /smack/cipso ought to be writeable
781 * to /smack/cipso.
782 *
783 * /smack/cipso2 should be used instead.
784 */
785 if (strlen(skp->smk_known) >= SMK_LABELLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800786 return 0;
787
Casey Schauflerf7112e62012-05-06 15:22:02 -0700788 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
Casey Schauflere114e472008-02-04 22:29:50 -0800789
Casey Schauflerf7112e62012-05-06 15:22:02 -0700790 for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
791 i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
792 seq_printf(s, "%c%d", sep, i);
793 sep = ',';
794 }
Casey Schauflere114e472008-02-04 22:29:50 -0800795
796 seq_putc(s, '\n');
797
798 return 0;
799}
800
James Morris88e9d342009-09-22 16:43:43 -0700801static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800802 .start = cipso_seq_start,
Casey Schauflere114e472008-02-04 22:29:50 -0800803 .next = cipso_seq_next,
804 .show = cipso_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800805 .stop = smk_seq_stop,
Casey Schauflere114e472008-02-04 22:29:50 -0800806};
807
808/**
809 * smk_open_cipso - open() for /smack/cipso
810 * @inode: inode structure representing file
811 * @file: "cipso" file pointer
812 *
813 * Connect our cipso_seq_* operations with /smack/cipso
814 * file_operations
815 */
816static int smk_open_cipso(struct inode *inode, struct file *file)
817{
818 return seq_open(file, &cipso_seq_ops);
819}
820
821/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700822 * smk_set_cipso - do the work for write() for cipso and cipso2
Randy Dunlap251a2a92009-02-18 11:42:33 -0800823 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800824 * @buf: where to get the data from
825 * @count: bytes sent
826 * @ppos: where to start
Casey Schauflerf7112e62012-05-06 15:22:02 -0700827 * @format: /smack/cipso or /smack/cipso2
Casey Schauflere114e472008-02-04 22:29:50 -0800828 *
829 * Accepts only one cipso rule per write call.
830 * Returns number of bytes written or error code, as appropriate
831 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700832static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
833 size_t count, loff_t *ppos, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800834{
835 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700836 struct netlbl_lsm_secattr ncats;
837 char mapcatset[SMK_CIPSOLEN];
Casey Schauflere114e472008-02-04 22:29:50 -0800838 int maplevel;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700839 unsigned int cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800840 int catlen;
841 ssize_t rc = -EINVAL;
842 char *data = NULL;
843 char *rule;
844 int ret;
845 int i;
846
847 /*
848 * Must have privilege.
849 * No partial writes.
850 * Enough data must be present.
851 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700852 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -0800853 return -EPERM;
854 if (*ppos != 0)
855 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700856 if (format == SMK_FIXED24_FMT &&
857 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
Casey Schauflere114e472008-02-04 22:29:50 -0800858 return -EINVAL;
859
860 data = kzalloc(count + 1, GFP_KERNEL);
861 if (data == NULL)
862 return -ENOMEM;
863
864 if (copy_from_user(data, buf, count) != 0) {
865 rc = -EFAULT;
866 goto unlockedout;
867 }
868
869 data[count] = '\0';
870 rule = data;
871 /*
872 * Only allow one writer at a time. Writes should be
873 * quite rare and small in any case.
874 */
875 mutex_lock(&smack_cipso_lock);
876
877 skp = smk_import_entry(rule, 0);
878 if (skp == NULL)
879 goto out;
880
Casey Schauflerf7112e62012-05-06 15:22:02 -0700881 if (format == SMK_FIXED24_FMT)
882 rule += SMK_LABELLEN;
883 else
884 rule += strlen(skp->smk_known);
885
Casey Schauflere114e472008-02-04 22:29:50 -0800886 ret = sscanf(rule, "%d", &maplevel);
887 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
888 goto out;
889
890 rule += SMK_DIGITLEN;
891 ret = sscanf(rule, "%d", &catlen);
892 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
893 goto out;
894
Casey Schauflerf7112e62012-05-06 15:22:02 -0700895 if (format == SMK_FIXED24_FMT &&
896 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800897 goto out;
898
899 memset(mapcatset, 0, sizeof(mapcatset));
900
901 for (i = 0; i < catlen; i++) {
902 rule += SMK_DIGITLEN;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700903 ret = sscanf(rule, "%u", &cat);
Casey Schauflere114e472008-02-04 22:29:50 -0800904 if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
905 goto out;
906
907 smack_catset_bit(cat, mapcatset);
908 }
909
Casey Schauflerf7112e62012-05-06 15:22:02 -0700910 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
911 if (rc >= 0) {
912 netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat);
913 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
914 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
915 rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -0800916 }
917
Casey Schauflere114e472008-02-04 22:29:50 -0800918out:
919 mutex_unlock(&smack_cipso_lock);
920unlockedout:
921 kfree(data);
922 return rc;
923}
924
Casey Schauflerf7112e62012-05-06 15:22:02 -0700925/**
926 * smk_write_cipso - write() for /smack/cipso
927 * @file: file pointer, not actually used
928 * @buf: where to get the data from
929 * @count: bytes sent
930 * @ppos: where to start
931 *
932 * Accepts only one cipso rule per write call.
933 * Returns number of bytes written or error code, as appropriate
934 */
935static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
936 size_t count, loff_t *ppos)
937{
938 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
939}
940
Casey Schauflere114e472008-02-04 22:29:50 -0800941static const struct file_operations smk_cipso_ops = {
942 .open = smk_open_cipso,
943 .read = seq_read,
944 .llseek = seq_lseek,
945 .write = smk_write_cipso,
946 .release = seq_release,
947};
948
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500949/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700950 * Seq_file read operations for /smack/cipso2
951 */
952
953/*
954 * Print cipso labels in format:
955 * label level[/cat[,cat]]
956 */
957static int cipso2_seq_show(struct seq_file *s, void *v)
958{
959 struct list_head *list = v;
960 struct smack_known *skp =
961 list_entry(list, struct smack_known, list);
962 struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
963 char sep = '/';
964 int i;
965
966 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
967
968 for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
969 i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
970 seq_printf(s, "%c%d", sep, i);
971 sep = ',';
972 }
973
974 seq_putc(s, '\n');
975
976 return 0;
977}
978
979static const struct seq_operations cipso2_seq_ops = {
980 .start = cipso_seq_start,
981 .next = cipso_seq_next,
982 .show = cipso2_seq_show,
983 .stop = smk_seq_stop,
984};
985
986/**
987 * smk_open_cipso2 - open() for /smack/cipso2
988 * @inode: inode structure representing file
989 * @file: "cipso2" file pointer
990 *
991 * Connect our cipso_seq_* operations with /smack/cipso2
992 * file_operations
993 */
994static int smk_open_cipso2(struct inode *inode, struct file *file)
995{
996 return seq_open(file, &cipso2_seq_ops);
997}
998
999/**
1000 * smk_write_cipso2 - write() for /smack/cipso2
1001 * @file: file pointer, not actually used
1002 * @buf: where to get the data from
1003 * @count: bytes sent
1004 * @ppos: where to start
1005 *
1006 * Accepts only one cipso rule per write call.
1007 * Returns number of bytes written or error code, as appropriate
1008 */
1009static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1010 size_t count, loff_t *ppos)
1011{
1012 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1013}
1014
1015static const struct file_operations smk_cipso2_ops = {
1016 .open = smk_open_cipso2,
1017 .read = seq_read,
1018 .llseek = seq_lseek,
1019 .write = smk_write_cipso2,
1020 .release = seq_release,
1021};
1022
1023/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001024 * Seq_file read operations for /smack/netlabel
1025 */
1026
1027static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1028{
Casey Schaufler40809562011-11-10 15:02:22 -08001029 return smk_seq_start(s, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001030}
1031
1032static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1033{
Casey Schaufler40809562011-11-10 15:02:22 -08001034 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001035}
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001036#define BEBITS (sizeof(__be32) * 8)
1037
1038/*
1039 * Print host/label pairs
1040 */
1041static int netlbladdr_seq_show(struct seq_file *s, void *v)
1042{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001043 struct list_head *list = v;
1044 struct smk_netlbladdr *skp =
1045 list_entry(list, struct smk_netlbladdr, list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001046 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001047 int maskn;
1048 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001049
etienne113a0e42009-03-04 07:33:51 +01001050 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001051
1052 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
1053 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
1054
1055 return 0;
1056}
1057
James Morris88e9d342009-09-22 16:43:43 -07001058static const struct seq_operations netlbladdr_seq_ops = {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001059 .start = netlbladdr_seq_start,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001060 .next = netlbladdr_seq_next,
1061 .show = netlbladdr_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001062 .stop = smk_seq_stop,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001063};
1064
1065/**
1066 * smk_open_netlbladdr - open() for /smack/netlabel
1067 * @inode: inode structure representing file
1068 * @file: "netlabel" file pointer
1069 *
1070 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1071 * file_operations
1072 */
1073static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1074{
1075 return seq_open(file, &netlbladdr_seq_ops);
1076}
1077
1078/**
etienne113a0e42009-03-04 07:33:51 +01001079 * smk_netlbladdr_insert
1080 * @new : netlabel to insert
1081 *
1082 * This helper insert netlabel in the smack_netlbladdrs list
1083 * sorted by netmask length (longest to smallest)
Etienne Basset7198e2e2009-03-24 20:53:24 +01001084 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1085 *
etienne113a0e42009-03-04 07:33:51 +01001086 */
1087static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1088{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001089 struct smk_netlbladdr *m, *m_next;
etienne113a0e42009-03-04 07:33:51 +01001090
Etienne Basset7198e2e2009-03-24 20:53:24 +01001091 if (list_empty(&smk_netlbladdr_list)) {
1092 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001093 return;
1094 }
1095
Jiri Pirko05725f72009-04-14 20:17:16 +02001096 m = list_entry_rcu(smk_netlbladdr_list.next,
1097 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001098
etienne113a0e42009-03-04 07:33:51 +01001099 /* the comparison '>' is a bit hacky, but works */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001100 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1101 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001102 return;
1103 }
Etienne Basset7198e2e2009-03-24 20:53:24 +01001104
1105 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1106 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1107 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001108 return;
1109 }
Jiri Pirko05725f72009-04-14 20:17:16 +02001110 m_next = list_entry_rcu(m->list.next,
1111 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001112 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1113 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001114 return;
1115 }
1116 }
1117}
1118
1119
1120/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001121 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -08001122 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001123 * @buf: where to get the data from
1124 * @count: bytes sent
1125 * @ppos: where to start
1126 *
1127 * Accepts only one netlbladdr per write call.
1128 * Returns number of bytes written or error code, as appropriate
1129 */
1130static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1131 size_t count, loff_t *ppos)
1132{
1133 struct smk_netlbladdr *skp;
1134 struct sockaddr_in newname;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001135 char *smack;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001136 char *sp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001137 char *data;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001138 char *host = (char *)&newname.sin_addr.s_addr;
1139 int rc;
1140 struct netlbl_audit audit_info;
1141 struct in_addr mask;
1142 unsigned int m;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001143 int found;
etienne113a0e42009-03-04 07:33:51 +01001144 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001145 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +01001146 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001147
1148 /*
1149 * Must have privilege.
1150 * No partial writes.
1151 * Enough data must be present.
1152 * "<addr/mask, as a.b.c.d/e><space><label>"
1153 * "<addr, as a.b.c.d><space><label>"
1154 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001155 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001156 return -EPERM;
1157 if (*ppos != 0)
1158 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001159 if (count < SMK_NETLBLADDRMIN)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001160 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001161
1162 data = kzalloc(count + 1, GFP_KERNEL);
1163 if (data == NULL)
1164 return -ENOMEM;
1165
1166 if (copy_from_user(data, buf, count) != 0) {
1167 rc = -EFAULT;
1168 goto free_data_out;
1169 }
1170
1171 smack = kzalloc(count + 1, GFP_KERNEL);
1172 if (smack == NULL) {
1173 rc = -ENOMEM;
1174 goto free_data_out;
1175 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001176
1177 data[count] = '\0';
1178
1179 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
1180 &host[0], &host[1], &host[2], &host[3], &m, smack);
1181 if (rc != 6) {
1182 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1183 &host[0], &host[1], &host[2], &host[3], smack);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001184 if (rc != 5) {
1185 rc = -EINVAL;
1186 goto free_out;
1187 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001188 m = BEBITS;
1189 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07001190 if (m > BEBITS) {
1191 rc = -EINVAL;
1192 goto free_out;
1193 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001194
Casey Schauflerf7112e62012-05-06 15:22:02 -07001195 /*
1196 * If smack begins with '-', it is an option, don't import it
1197 */
Etienne Basset43031542009-03-27 17:11:01 -04001198 if (smack[0] != '-') {
1199 sp = smk_import(smack, 0);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001200 if (sp == NULL) {
1201 rc = -EINVAL;
1202 goto free_out;
1203 }
Etienne Basset43031542009-03-27 17:11:01 -04001204 } else {
1205 /* check known options */
1206 if (strcmp(smack, smack_cipso_option) == 0)
1207 sp = (char *)smack_cipso_option;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001208 else {
1209 rc = -EINVAL;
1210 goto free_out;
1211 }
Etienne Basset43031542009-03-27 17:11:01 -04001212 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001213
etienne113a0e42009-03-04 07:33:51 +01001214 for (temp_mask = 0; m > 0; m--) {
1215 temp_mask |= mask_bits;
1216 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001217 }
etienne113a0e42009-03-04 07:33:51 +01001218 mask.s_addr = cpu_to_be32(temp_mask);
1219
1220 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001221 /*
1222 * Only allow one writer at a time. Writes should be
1223 * quite rare and small in any case.
1224 */
1225 mutex_lock(&smk_netlbladdr_lock);
1226
1227 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001228 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001229 found = 0;
1230 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001231 if (skp->smk_host.sin_addr.s_addr == nsa &&
Etienne Basset7198e2e2009-03-24 20:53:24 +01001232 skp->smk_mask.s_addr == mask.s_addr) {
1233 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001234 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001235 }
1236 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001237 smk_netlabel_audit_set(&audit_info);
1238
Etienne Basset7198e2e2009-03-24 20:53:24 +01001239 if (found == 0) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001240 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
1241 if (skp == NULL)
1242 rc = -ENOMEM;
1243 else {
1244 rc = 0;
1245 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1246 skp->smk_mask.s_addr = mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001247 skp->smk_label = sp;
etienne113a0e42009-03-04 07:33:51 +01001248 smk_netlbladdr_insert(skp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001249 }
1250 } else {
Etienne Basset43031542009-03-27 17:11:01 -04001251 /* we delete the unlabeled entry, only if the previous label
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001252 * wasn't the special CIPSO option */
Etienne Basset43031542009-03-27 17:11:01 -04001253 if (skp->smk_label != smack_cipso_option)
1254 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1255 &skp->smk_host.sin_addr, &skp->smk_mask,
1256 PF_INET, &audit_info);
1257 else
1258 rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001259 skp->smk_label = sp;
1260 }
1261
1262 /*
1263 * Now tell netlabel about the single label nature of
1264 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -04001265 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001266 */
Etienne Basset43031542009-03-27 17:11:01 -04001267 if (rc == 0 && sp != smack_cipso_option)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001268 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1269 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
1270 smack_to_secid(skp->smk_label), &audit_info);
1271
1272 if (rc == 0)
1273 rc = count;
1274
1275 mutex_unlock(&smk_netlbladdr_lock);
1276
Casey Schauflerf7112e62012-05-06 15:22:02 -07001277free_out:
1278 kfree(smack);
1279free_data_out:
1280 kfree(data);
1281
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001282 return rc;
1283}
1284
1285static const struct file_operations smk_netlbladdr_ops = {
1286 .open = smk_open_netlbladdr,
1287 .read = seq_read,
1288 .llseek = seq_lseek,
1289 .write = smk_write_netlbladdr,
1290 .release = seq_release,
1291};
1292
Casey Schauflere114e472008-02-04 22:29:50 -08001293/**
1294 * smk_read_doi - read() for /smack/doi
1295 * @filp: file pointer, not actually used
1296 * @buf: where to put the result
1297 * @count: maximum to send along
1298 * @ppos: where to start
1299 *
1300 * Returns number of bytes read or error code, as appropriate
1301 */
1302static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1303 size_t count, loff_t *ppos)
1304{
1305 char temp[80];
1306 ssize_t rc;
1307
1308 if (*ppos != 0)
1309 return 0;
1310
1311 sprintf(temp, "%d", smk_cipso_doi_value);
1312 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1313
1314 return rc;
1315}
1316
1317/**
1318 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001319 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001320 * @buf: where to get the data from
1321 * @count: bytes sent
1322 * @ppos: where to start
1323 *
1324 * Returns number of bytes written or error code, as appropriate
1325 */
1326static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1327 size_t count, loff_t *ppos)
1328{
1329 char temp[80];
1330 int i;
1331
Casey Schaufler1880eff2012-06-05 15:28:30 -07001332 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001333 return -EPERM;
1334
1335 if (count >= sizeof(temp) || count == 0)
1336 return -EINVAL;
1337
1338 if (copy_from_user(temp, buf, count) != 0)
1339 return -EFAULT;
1340
1341 temp[count] = '\0';
1342
1343 if (sscanf(temp, "%d", &i) != 1)
1344 return -EINVAL;
1345
1346 smk_cipso_doi_value = i;
1347
1348 smk_cipso_doi();
1349
1350 return count;
1351}
1352
1353static const struct file_operations smk_doi_ops = {
1354 .read = smk_read_doi,
1355 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001356 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001357};
1358
1359/**
1360 * smk_read_direct - read() for /smack/direct
1361 * @filp: file pointer, not actually used
1362 * @buf: where to put the result
1363 * @count: maximum to send along
1364 * @ppos: where to start
1365 *
1366 * Returns number of bytes read or error code, as appropriate
1367 */
1368static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1369 size_t count, loff_t *ppos)
1370{
1371 char temp[80];
1372 ssize_t rc;
1373
1374 if (*ppos != 0)
1375 return 0;
1376
1377 sprintf(temp, "%d", smack_cipso_direct);
1378 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1379
1380 return rc;
1381}
1382
1383/**
1384 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001385 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001386 * @buf: where to get the data from
1387 * @count: bytes sent
1388 * @ppos: where to start
1389 *
1390 * Returns number of bytes written or error code, as appropriate
1391 */
1392static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1393 size_t count, loff_t *ppos)
1394{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001395 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001396 char temp[80];
1397 int i;
1398
Casey Schaufler1880eff2012-06-05 15:28:30 -07001399 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001400 return -EPERM;
1401
1402 if (count >= sizeof(temp) || count == 0)
1403 return -EINVAL;
1404
1405 if (copy_from_user(temp, buf, count) != 0)
1406 return -EFAULT;
1407
1408 temp[count] = '\0';
1409
1410 if (sscanf(temp, "%d", &i) != 1)
1411 return -EINVAL;
1412
Casey Schauflerf7112e62012-05-06 15:22:02 -07001413 /*
1414 * Don't do anything if the value hasn't actually changed.
1415 * If it is changing reset the level on entries that were
1416 * set up to be direct when they were created.
1417 */
1418 if (smack_cipso_direct != i) {
1419 mutex_lock(&smack_known_lock);
1420 list_for_each_entry_rcu(skp, &smack_known_list, list)
1421 if (skp->smk_netlabel.attr.mls.lvl ==
1422 smack_cipso_direct)
1423 skp->smk_netlabel.attr.mls.lvl = i;
1424 smack_cipso_direct = i;
1425 mutex_unlock(&smack_known_lock);
1426 }
Casey Schauflere114e472008-02-04 22:29:50 -08001427
1428 return count;
1429}
1430
1431static const struct file_operations smk_direct_ops = {
1432 .read = smk_read_direct,
1433 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001434 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001435};
1436
1437/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001438 * smk_read_mapped - read() for /smack/mapped
1439 * @filp: file pointer, not actually used
1440 * @buf: where to put the result
1441 * @count: maximum to send along
1442 * @ppos: where to start
1443 *
1444 * Returns number of bytes read or error code, as appropriate
1445 */
1446static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1447 size_t count, loff_t *ppos)
1448{
1449 char temp[80];
1450 ssize_t rc;
1451
1452 if (*ppos != 0)
1453 return 0;
1454
1455 sprintf(temp, "%d", smack_cipso_mapped);
1456 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1457
1458 return rc;
1459}
1460
1461/**
1462 * smk_write_mapped - write() for /smack/mapped
1463 * @file: file pointer, not actually used
1464 * @buf: where to get the data from
1465 * @count: bytes sent
1466 * @ppos: where to start
1467 *
1468 * Returns number of bytes written or error code, as appropriate
1469 */
1470static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1471 size_t count, loff_t *ppos)
1472{
1473 struct smack_known *skp;
1474 char temp[80];
1475 int i;
1476
Casey Schaufler1880eff2012-06-05 15:28:30 -07001477 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001478 return -EPERM;
1479
1480 if (count >= sizeof(temp) || count == 0)
1481 return -EINVAL;
1482
1483 if (copy_from_user(temp, buf, count) != 0)
1484 return -EFAULT;
1485
1486 temp[count] = '\0';
1487
1488 if (sscanf(temp, "%d", &i) != 1)
1489 return -EINVAL;
1490
1491 /*
1492 * Don't do anything if the value hasn't actually changed.
1493 * If it is changing reset the level on entries that were
1494 * set up to be mapped when they were created.
1495 */
1496 if (smack_cipso_mapped != i) {
1497 mutex_lock(&smack_known_lock);
1498 list_for_each_entry_rcu(skp, &smack_known_list, list)
1499 if (skp->smk_netlabel.attr.mls.lvl ==
1500 smack_cipso_mapped)
1501 skp->smk_netlabel.attr.mls.lvl = i;
1502 smack_cipso_mapped = i;
1503 mutex_unlock(&smack_known_lock);
1504 }
1505
1506 return count;
1507}
1508
1509static const struct file_operations smk_mapped_ops = {
1510 .read = smk_read_mapped,
1511 .write = smk_write_mapped,
1512 .llseek = default_llseek,
1513};
1514
1515/**
Casey Schauflere114e472008-02-04 22:29:50 -08001516 * smk_read_ambient - read() for /smack/ambient
1517 * @filp: file pointer, not actually used
1518 * @buf: where to put the result
1519 * @cn: maximum to send along
1520 * @ppos: where to start
1521 *
1522 * Returns number of bytes read or error code, as appropriate
1523 */
1524static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1525 size_t cn, loff_t *ppos)
1526{
1527 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001528 int asize;
1529
1530 if (*ppos != 0)
1531 return 0;
1532 /*
1533 * Being careful to avoid a problem in the case where
1534 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001535 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001536 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001537
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001538 asize = strlen(smack_net_ambient) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001539
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001540 if (cn >= asize)
1541 rc = simple_read_from_buffer(buf, cn, ppos,
1542 smack_net_ambient, asize);
1543 else
1544 rc = -EINVAL;
1545
1546 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001547
1548 return rc;
1549}
1550
1551/**
1552 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001553 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001554 * @buf: where to get the data from
1555 * @count: bytes sent
1556 * @ppos: where to start
1557 *
1558 * Returns number of bytes written or error code, as appropriate
1559 */
1560static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1561 size_t count, loff_t *ppos)
1562{
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001563 char *oldambient;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001564 char *smack = NULL;
1565 char *data;
1566 int rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -08001567
Casey Schaufler1880eff2012-06-05 15:28:30 -07001568 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001569 return -EPERM;
1570
Casey Schauflerf7112e62012-05-06 15:22:02 -07001571 data = kzalloc(count + 1, GFP_KERNEL);
1572 if (data == NULL)
1573 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001574
Casey Schauflerf7112e62012-05-06 15:22:02 -07001575 if (copy_from_user(data, buf, count) != 0) {
1576 rc = -EFAULT;
1577 goto out;
1578 }
Casey Schauflere114e472008-02-04 22:29:50 -08001579
Casey Schauflerf7112e62012-05-06 15:22:02 -07001580 smack = smk_import(data, count);
1581 if (smack == NULL) {
1582 rc = -EINVAL;
1583 goto out;
1584 }
Casey Schauflere114e472008-02-04 22:29:50 -08001585
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001586 mutex_lock(&smack_ambient_lock);
1587
1588 oldambient = smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08001589 smack_net_ambient = smack;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001590 smk_unlbl_ambient(oldambient);
1591
1592 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001593
Casey Schauflerf7112e62012-05-06 15:22:02 -07001594out:
1595 kfree(data);
1596 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001597}
1598
1599static const struct file_operations smk_ambient_ops = {
1600 .read = smk_read_ambient,
1601 .write = smk_write_ambient,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001602 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001603};
1604
Casey Schaufler15446232008-07-30 15:37:11 -07001605/**
1606 * smk_read_onlycap - read() for /smack/onlycap
1607 * @filp: file pointer, not actually used
1608 * @buf: where to put the result
1609 * @cn: maximum to send along
1610 * @ppos: where to start
1611 *
1612 * Returns number of bytes read or error code, as appropriate
1613 */
1614static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1615 size_t cn, loff_t *ppos)
1616{
1617 char *smack = "";
1618 ssize_t rc = -EINVAL;
1619 int asize;
1620
1621 if (*ppos != 0)
1622 return 0;
1623
1624 if (smack_onlycap != NULL)
1625 smack = smack_onlycap;
1626
1627 asize = strlen(smack) + 1;
1628
1629 if (cn >= asize)
1630 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1631
1632 return rc;
1633}
1634
1635/**
1636 * smk_write_onlycap - write() for /smack/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001637 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001638 * @buf: where to get the data from
1639 * @count: bytes sent
1640 * @ppos: where to start
1641 *
1642 * Returns number of bytes written or error code, as appropriate
1643 */
1644static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1645 size_t count, loff_t *ppos)
1646{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001647 char *data;
Casey Schaufler676dac42010-12-02 06:43:39 -08001648 char *sp = smk_of_task(current->cred->security);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001649 int rc = count;
Casey Schaufler15446232008-07-30 15:37:11 -07001650
Casey Schaufler1880eff2012-06-05 15:28:30 -07001651 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler15446232008-07-30 15:37:11 -07001652 return -EPERM;
1653
1654 /*
1655 * This can be done using smk_access() but is done
1656 * explicitly for clarity. The smk_access() implementation
1657 * would use smk_access(smack_onlycap, MAY_WRITE)
1658 */
1659 if (smack_onlycap != NULL && smack_onlycap != sp)
1660 return -EPERM;
1661
Casey Schauflerf7112e62012-05-06 15:22:02 -07001662 data = kzalloc(count, GFP_KERNEL);
1663 if (data == NULL)
1664 return -ENOMEM;
Casey Schaufler15446232008-07-30 15:37:11 -07001665
1666 /*
1667 * Should the null string be passed in unset the onlycap value.
1668 * This seems like something to be careful with as usually
1669 * smk_import only expects to return NULL for errors. It
1670 * is usually the case that a nullstring or "\n" would be
1671 * bad to pass to smk_import but in fact this is useful here.
Casey Schauflerf7112e62012-05-06 15:22:02 -07001672 *
1673 * smk_import will also reject a label beginning with '-',
1674 * so "-usecapabilities" will also work.
Casey Schaufler15446232008-07-30 15:37:11 -07001675 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07001676 if (copy_from_user(data, buf, count) != 0)
1677 rc = -EFAULT;
1678 else
1679 smack_onlycap = smk_import(data, count);
Casey Schaufler15446232008-07-30 15:37:11 -07001680
Casey Schauflerf7112e62012-05-06 15:22:02 -07001681 kfree(data);
1682 return rc;
Casey Schaufler15446232008-07-30 15:37:11 -07001683}
1684
1685static const struct file_operations smk_onlycap_ops = {
1686 .read = smk_read_onlycap,
1687 .write = smk_write_onlycap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001688 .llseek = default_llseek,
Casey Schaufler15446232008-07-30 15:37:11 -07001689};
1690
Casey Schauflere114e472008-02-04 22:29:50 -08001691/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001692 * smk_read_logging - read() for /smack/logging
1693 * @filp: file pointer, not actually used
1694 * @buf: where to put the result
1695 * @cn: maximum to send along
1696 * @ppos: where to start
1697 *
1698 * Returns number of bytes read or error code, as appropriate
1699 */
1700static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1701 size_t count, loff_t *ppos)
1702{
1703 char temp[32];
1704 ssize_t rc;
1705
1706 if (*ppos != 0)
1707 return 0;
1708
1709 sprintf(temp, "%d\n", log_policy);
1710 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1711 return rc;
1712}
1713
1714/**
1715 * smk_write_logging - write() for /smack/logging
1716 * @file: file pointer, not actually used
1717 * @buf: where to get the data from
1718 * @count: bytes sent
1719 * @ppos: where to start
1720 *
1721 * Returns number of bytes written or error code, as appropriate
1722 */
1723static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1724 size_t count, loff_t *ppos)
1725{
1726 char temp[32];
1727 int i;
1728
Casey Schaufler1880eff2012-06-05 15:28:30 -07001729 if (!smack_privileged(CAP_MAC_ADMIN))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001730 return -EPERM;
1731
1732 if (count >= sizeof(temp) || count == 0)
1733 return -EINVAL;
1734
1735 if (copy_from_user(temp, buf, count) != 0)
1736 return -EFAULT;
1737
1738 temp[count] = '\0';
1739
1740 if (sscanf(temp, "%d", &i) != 1)
1741 return -EINVAL;
1742 if (i < 0 || i > 3)
1743 return -EINVAL;
1744 log_policy = i;
1745 return count;
1746}
1747
1748
1749
1750static const struct file_operations smk_logging_ops = {
1751 .read = smk_read_logging,
1752 .write = smk_write_logging,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001753 .llseek = default_llseek,
Etienne Bassetecfcc532009-04-08 20:40:06 +02001754};
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001755
1756/*
1757 * Seq_file read operations for /smack/load-self
1758 */
1759
1760static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1761{
1762 struct task_smack *tsp = current_security();
1763
Casey Schaufler40809562011-11-10 15:02:22 -08001764 return smk_seq_start(s, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001765}
1766
1767static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1768{
1769 struct task_smack *tsp = current_security();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001770
Casey Schaufler40809562011-11-10 15:02:22 -08001771 return smk_seq_next(s, v, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001772}
1773
1774static int load_self_seq_show(struct seq_file *s, void *v)
1775{
1776 struct list_head *list = v;
1777 struct smack_rule *srp =
1778 list_entry(list, struct smack_rule, list);
1779
Casey Schauflerf7112e62012-05-06 15:22:02 -07001780 smk_rule_show(s, srp, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001781
1782 return 0;
1783}
1784
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001785static const struct seq_operations load_self_seq_ops = {
1786 .start = load_self_seq_start,
1787 .next = load_self_seq_next,
1788 .show = load_self_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001789 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001790};
1791
1792
1793/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001794 * smk_open_load_self - open() for /smack/load-self2
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001795 * @inode: inode structure representing file
1796 * @file: "load" file pointer
1797 *
1798 * For reading, use load_seq_* seq_file reading operations.
1799 */
1800static int smk_open_load_self(struct inode *inode, struct file *file)
1801{
1802 return seq_open(file, &load_self_seq_ops);
1803}
1804
1805/**
1806 * smk_write_load_self - write() for /smack/load-self
1807 * @file: file pointer, not actually used
1808 * @buf: where to get the data from
1809 * @count: bytes sent
1810 * @ppos: where to start - must be 0
1811 *
1812 */
1813static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1814 size_t count, loff_t *ppos)
1815{
1816 struct task_smack *tsp = current_security();
1817
Casey Schauflerf7112e62012-05-06 15:22:02 -07001818 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1819 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001820}
1821
1822static const struct file_operations smk_load_self_ops = {
1823 .open = smk_open_load_self,
1824 .read = seq_read,
1825 .llseek = seq_lseek,
1826 .write = smk_write_load_self,
1827 .release = seq_release,
1828};
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001829
1830/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001831 * smk_user_access - handle access check transaction
1832 * @file: file pointer
1833 * @buf: data from user space
1834 * @count: bytes sent
1835 * @ppos: where to start - must be 0
1836 */
1837static ssize_t smk_user_access(struct file *file, const char __user *buf,
1838 size_t count, loff_t *ppos, int format)
1839{
Rafal Krypae05b6f92013-01-10 19:42:00 +01001840 struct smack_parsed_rule rule;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001841 char *data;
1842 char *cod;
1843 int res;
1844
1845 data = simple_transaction_get(file, buf, count);
1846 if (IS_ERR(data))
1847 return PTR_ERR(data);
1848
1849 if (format == SMK_FIXED24_FMT) {
1850 if (count < SMK_LOADLEN)
1851 return -EINVAL;
1852 res = smk_parse_rule(data, &rule, 0);
1853 } else {
1854 /*
1855 * Copy the data to make sure the string is terminated.
1856 */
1857 cod = kzalloc(count + 1, GFP_KERNEL);
1858 if (cod == NULL)
1859 return -ENOMEM;
1860 memcpy(cod, data, count);
1861 cod[count] = '\0';
Rafal Krypae05b6f92013-01-10 19:42:00 +01001862 res = smk_parse_long_rule(cod, &rule, 0, 0);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001863 kfree(cod);
1864 }
1865
1866 if (res)
1867 return -EINVAL;
1868
Rafal Krypae05b6f92013-01-10 19:42:00 +01001869 res = smk_access(rule.smk_subject, rule.smk_object, rule.smk_access1,
Casey Schauflerf7112e62012-05-06 15:22:02 -07001870 NULL);
1871 data[0] = res == 0 ? '1' : '0';
1872 data[1] = '\0';
1873
1874 simple_transaction_set(file, 2);
1875
1876 if (format == SMK_FIXED24_FMT)
1877 return SMK_LOADLEN;
1878 return count;
1879}
1880
1881/**
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001882 * smk_write_access - handle access check transaction
1883 * @file: file pointer
1884 * @buf: data from user space
1885 * @count: bytes sent
1886 * @ppos: where to start - must be 0
1887 */
1888static ssize_t smk_write_access(struct file *file, const char __user *buf,
1889 size_t count, loff_t *ppos)
1890{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001891 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001892}
1893
1894static const struct file_operations smk_access_ops = {
1895 .write = smk_write_access,
1896 .read = simple_transaction_read,
1897 .release = simple_transaction_release,
1898 .llseek = generic_file_llseek,
1899};
1900
Casey Schauflerf7112e62012-05-06 15:22:02 -07001901
1902/*
1903 * Seq_file read operations for /smack/load2
1904 */
1905
1906static int load2_seq_show(struct seq_file *s, void *v)
1907{
1908 struct list_head *list = v;
1909 struct smack_master_list *smlp =
1910 list_entry(list, struct smack_master_list, list);
1911
1912 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
1913
1914 return 0;
1915}
1916
1917static const struct seq_operations load2_seq_ops = {
1918 .start = load2_seq_start,
1919 .next = load2_seq_next,
1920 .show = load2_seq_show,
1921 .stop = smk_seq_stop,
1922};
1923
1924/**
1925 * smk_open_load2 - open() for /smack/load2
1926 * @inode: inode structure representing file
1927 * @file: "load2" file pointer
1928 *
1929 * For reading, use load2_seq_* seq_file reading operations.
1930 */
1931static int smk_open_load2(struct inode *inode, struct file *file)
1932{
1933 return seq_open(file, &load2_seq_ops);
1934}
1935
1936/**
1937 * smk_write_load2 - write() for /smack/load2
1938 * @file: file pointer, not actually used
1939 * @buf: where to get the data from
1940 * @count: bytes sent
1941 * @ppos: where to start - must be 0
1942 *
1943 */
1944static ssize_t smk_write_load2(struct file *file, const char __user *buf,
1945 size_t count, loff_t *ppos)
1946{
1947 /*
1948 * Must have privilege.
1949 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001950 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001951 return -EPERM;
1952
1953 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
1954 SMK_LONG_FMT);
1955}
1956
1957static const struct file_operations smk_load2_ops = {
1958 .open = smk_open_load2,
1959 .read = seq_read,
1960 .llseek = seq_lseek,
1961 .write = smk_write_load2,
1962 .release = seq_release,
1963};
1964
1965/*
1966 * Seq_file read operations for /smack/load-self2
1967 */
1968
1969static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
1970{
1971 struct task_smack *tsp = current_security();
1972
1973 return smk_seq_start(s, pos, &tsp->smk_rules);
1974}
1975
1976static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
1977{
1978 struct task_smack *tsp = current_security();
1979
1980 return smk_seq_next(s, v, pos, &tsp->smk_rules);
1981}
1982
1983static int load_self2_seq_show(struct seq_file *s, void *v)
1984{
1985 struct list_head *list = v;
1986 struct smack_rule *srp =
1987 list_entry(list, struct smack_rule, list);
1988
1989 smk_rule_show(s, srp, SMK_LONGLABEL);
1990
1991 return 0;
1992}
1993
1994static const struct seq_operations load_self2_seq_ops = {
1995 .start = load_self2_seq_start,
1996 .next = load_self2_seq_next,
1997 .show = load_self2_seq_show,
1998 .stop = smk_seq_stop,
1999};
2000
2001/**
2002 * smk_open_load_self2 - open() for /smack/load-self2
2003 * @inode: inode structure representing file
2004 * @file: "load" file pointer
2005 *
2006 * For reading, use load_seq_* seq_file reading operations.
2007 */
2008static int smk_open_load_self2(struct inode *inode, struct file *file)
2009{
2010 return seq_open(file, &load_self2_seq_ops);
2011}
2012
2013/**
2014 * smk_write_load_self2 - write() for /smack/load-self2
2015 * @file: file pointer, not actually used
2016 * @buf: where to get the data from
2017 * @count: bytes sent
2018 * @ppos: where to start - must be 0
2019 *
2020 */
2021static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2022 size_t count, loff_t *ppos)
2023{
2024 struct task_smack *tsp = current_security();
2025
2026 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2027 &tsp->smk_rules_lock, SMK_LONG_FMT);
2028}
2029
2030static const struct file_operations smk_load_self2_ops = {
2031 .open = smk_open_load_self2,
2032 .read = seq_read,
2033 .llseek = seq_lseek,
2034 .write = smk_write_load_self2,
2035 .release = seq_release,
2036};
2037
2038/**
2039 * smk_write_access2 - handle access check transaction
2040 * @file: file pointer
2041 * @buf: data from user space
2042 * @count: bytes sent
2043 * @ppos: where to start - must be 0
2044 */
2045static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2046 size_t count, loff_t *ppos)
2047{
2048 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2049}
2050
2051static const struct file_operations smk_access2_ops = {
2052 .write = smk_write_access2,
2053 .read = simple_transaction_read,
2054 .release = simple_transaction_release,
2055 .llseek = generic_file_llseek,
2056};
2057
Etienne Bassetecfcc532009-04-08 20:40:06 +02002058/**
Rafal Krypa449543b2012-07-11 17:49:30 +02002059 * smk_write_revoke_subj - write() for /smack/revoke-subject
2060 * @file: file pointer
2061 * @buf: data from user space
2062 * @count: bytes sent
2063 * @ppos: where to start - must be 0
2064 */
2065static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2066 size_t count, loff_t *ppos)
2067{
2068 char *data = NULL;
2069 const char *cp = NULL;
2070 struct smack_known *skp;
2071 struct smack_rule *sp;
2072 struct list_head *rule_list;
2073 struct mutex *rule_lock;
2074 int rc = count;
2075
2076 if (*ppos != 0)
2077 return -EINVAL;
2078
2079 if (!smack_privileged(CAP_MAC_ADMIN))
2080 return -EPERM;
2081
2082 if (count == 0 || count > SMK_LONGLABEL)
2083 return -EINVAL;
2084
2085 data = kzalloc(count, GFP_KERNEL);
2086 if (data == NULL)
2087 return -ENOMEM;
2088
2089 if (copy_from_user(data, buf, count) != 0) {
2090 rc = -EFAULT;
2091 goto free_out;
2092 }
2093
2094 cp = smk_parse_smack(data, count);
2095 if (cp == NULL) {
2096 rc = -EINVAL;
2097 goto free_out;
2098 }
2099
2100 skp = smk_find_entry(cp);
Rafal Krypad15d9fad2012-11-27 16:28:11 +01002101 if (skp == NULL)
Rafal Krypa449543b2012-07-11 17:49:30 +02002102 goto free_out;
Rafal Krypa449543b2012-07-11 17:49:30 +02002103
2104 rule_list = &skp->smk_rules;
2105 rule_lock = &skp->smk_rules_lock;
2106
2107 mutex_lock(rule_lock);
2108
2109 list_for_each_entry_rcu(sp, rule_list, list)
2110 sp->smk_access = 0;
2111
2112 mutex_unlock(rule_lock);
2113
2114free_out:
2115 kfree(data);
2116 kfree(cp);
2117 return rc;
2118}
2119
2120static const struct file_operations smk_revoke_subj_ops = {
2121 .write = smk_write_revoke_subj,
2122 .read = simple_transaction_read,
2123 .release = simple_transaction_release,
2124 .llseek = generic_file_llseek,
2125};
2126
Casey Schauflere9307232012-11-01 18:14:32 -07002127static struct kset *smackfs_kset;
2128/**
2129 * smk_init_sysfs - initialize /sys/fs/smackfs
2130 *
2131 */
2132static int smk_init_sysfs(void)
2133{
2134 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2135 if (!smackfs_kset)
2136 return -ENOMEM;
2137 return 0;
2138}
2139
Rafal Krypa449543b2012-07-11 17:49:30 +02002140/**
Rafal Krypae05b6f92013-01-10 19:42:00 +01002141 * smk_write_change_rule - write() for /smack/change-rule
2142 * @file: file pointer
2143 * @buf: data from user space
2144 * @count: bytes sent
2145 * @ppos: where to start - must be 0
2146 */
2147static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2148 size_t count, loff_t *ppos)
2149{
2150 /*
2151 * Must have privilege.
2152 */
2153 if (!capable(CAP_MAC_ADMIN))
2154 return -EPERM;
2155
2156 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2157 SMK_CHANGE_FMT);
2158}
2159
2160static const struct file_operations smk_change_rule_ops = {
2161 .write = smk_write_change_rule,
2162 .read = simple_transaction_read,
2163 .release = simple_transaction_release,
2164 .llseek = generic_file_llseek,
2165};
2166
2167/**
Casey Schauflere114e472008-02-04 22:29:50 -08002168 * smk_fill_super - fill the /smackfs superblock
2169 * @sb: the empty superblock
2170 * @data: unused
2171 * @silent: unused
2172 *
2173 * Fill in the well known entries for /smack
2174 *
2175 * Returns 0 on success, an error code on failure
2176 */
2177static int smk_fill_super(struct super_block *sb, void *data, int silent)
2178{
2179 int rc;
2180 struct inode *root_inode;
2181
2182 static struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002183 [SMK_LOAD] = {
2184 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2185 [SMK_CIPSO] = {
2186 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2187 [SMK_DOI] = {
2188 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2189 [SMK_DIRECT] = {
2190 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2191 [SMK_AMBIENT] = {
2192 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2193 [SMK_NETLBLADDR] = {
2194 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2195 [SMK_ONLYCAP] = {
2196 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2197 [SMK_LOGGING] = {
2198 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2199 [SMK_LOAD_SELF] = {
2200 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002201 [SMK_ACCESSES] = {
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03002202 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
Casey Schauflerf7112e62012-05-06 15:22:02 -07002203 [SMK_MAPPED] = {
2204 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2205 [SMK_LOAD2] = {
2206 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2207 [SMK_LOAD_SELF2] = {
2208 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2209 [SMK_ACCESS2] = {
2210 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2211 [SMK_CIPSO2] = {
2212 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
Rafal Krypa449543b2012-07-11 17:49:30 +02002213 [SMK_REVOKE_SUBJ] = {
2214 "revoke-subject", &smk_revoke_subj_ops,
2215 S_IRUGO|S_IWUSR},
Rafal Krypae05b6f92013-01-10 19:42:00 +01002216 [SMK_CHANGE_RULE] = {
2217 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002218 /* last one */
2219 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08002220 };
2221
2222 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2223 if (rc != 0) {
2224 printk(KERN_ERR "%s failed %d while creating inodes\n",
2225 __func__, rc);
2226 return rc;
2227 }
2228
2229 root_inode = sb->s_root->d_inode;
Casey Schauflere114e472008-02-04 22:29:50 -08002230
2231 return 0;
2232}
2233
2234/**
Al Virofc14f2f2010-07-25 01:48:30 +04002235 * smk_mount - get the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002236 * @fs_type: passed along without comment
2237 * @flags: passed along without comment
2238 * @dev_name: passed along without comment
2239 * @data: passed along without comment
Casey Schauflere114e472008-02-04 22:29:50 -08002240 *
2241 * Just passes everything along.
2242 *
2243 * Returns what the lower level code does.
2244 */
Al Virofc14f2f2010-07-25 01:48:30 +04002245static struct dentry *smk_mount(struct file_system_type *fs_type,
2246 int flags, const char *dev_name, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -08002247{
Al Virofc14f2f2010-07-25 01:48:30 +04002248 return mount_single(fs_type, flags, data, smk_fill_super);
Casey Schauflere114e472008-02-04 22:29:50 -08002249}
2250
2251static struct file_system_type smk_fs_type = {
2252 .name = "smackfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002253 .mount = smk_mount,
Casey Schauflere114e472008-02-04 22:29:50 -08002254 .kill_sb = kill_litter_super,
2255};
2256
2257static struct vfsmount *smackfs_mount;
2258
Casey Schauflerf7112e62012-05-06 15:22:02 -07002259static int __init smk_preset_netlabel(struct smack_known *skp)
2260{
2261 skp->smk_netlabel.domain = skp->smk_known;
2262 skp->smk_netlabel.flags =
2263 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2264 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2265 &skp->smk_netlabel, strlen(skp->smk_known));
2266}
2267
Casey Schauflere114e472008-02-04 22:29:50 -08002268/**
2269 * init_smk_fs - get the smackfs superblock
2270 *
2271 * register the smackfs
2272 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002273 * Do not register smackfs if Smack wasn't enabled
2274 * on boot. We can not put this method normally under the
2275 * smack_init() code path since the security subsystem get
2276 * initialized before the vfs caches.
2277 *
2278 * Returns true if we were not chosen on boot or if
2279 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08002280 */
2281static int __init init_smk_fs(void)
2282{
2283 int err;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002284 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002285
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002286 if (!security_module_enable(&smack_ops))
2287 return 0;
2288
Casey Schauflere9307232012-11-01 18:14:32 -07002289 err = smk_init_sysfs();
2290 if (err)
2291 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2292
Casey Schauflere114e472008-02-04 22:29:50 -08002293 err = register_filesystem(&smk_fs_type);
2294 if (!err) {
2295 smackfs_mount = kern_mount(&smk_fs_type);
2296 if (IS_ERR(smackfs_mount)) {
2297 printk(KERN_ERR "smackfs: could not mount!\n");
2298 err = PTR_ERR(smackfs_mount);
2299 smackfs_mount = NULL;
2300 }
2301 }
2302
Casey Schauflere114e472008-02-04 22:29:50 -08002303 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002304 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08002305
Casey Schauflerf7112e62012-05-06 15:22:02 -07002306 rc = smk_preset_netlabel(&smack_known_floor);
2307 if (err == 0 && rc < 0)
2308 err = rc;
2309 rc = smk_preset_netlabel(&smack_known_hat);
2310 if (err == 0 && rc < 0)
2311 err = rc;
2312 rc = smk_preset_netlabel(&smack_known_huh);
2313 if (err == 0 && rc < 0)
2314 err = rc;
2315 rc = smk_preset_netlabel(&smack_known_invalid);
2316 if (err == 0 && rc < 0)
2317 err = rc;
2318 rc = smk_preset_netlabel(&smack_known_star);
2319 if (err == 0 && rc < 0)
2320 err = rc;
2321 rc = smk_preset_netlabel(&smack_known_web);
2322 if (err == 0 && rc < 0)
2323 err = rc;
2324
Casey Schauflere114e472008-02-04 22:29:50 -08002325 return err;
2326}
2327
2328__initcall(init_smk_fs);