blob: 1c89ade186b651bd7898821bbc3b9199da871305 [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 Schaufler2f823ff2013-05-22 18:43:03 -070069struct smack_known *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 {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700115 struct smack_known *smk_subject;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100116 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"
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700142#define SMK_ACCESS "rwxatl"
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200143#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{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700166 struct smack_known *skp = smk_of_current();
167
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500168 nap->loginuid = audit_get_loginuid(current);
169 nap->sessionid = audit_get_sessionid(current);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700170 nap->secid = skp->smk_secid;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500171}
172
173/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700174 * Value for parsing single label host rules
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500175 * "1.2.3.4 X"
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500176 */
177#define SMK_NETLBLADDRMIN 9
Casey Schauflere114e472008-02-04 22:29:50 -0800178
Casey Schauflere114e472008-02-04 22:29:50 -0800179/**
Rafal Krypae05b6f92013-01-10 19:42:00 +0100180 * smk_set_access - add a rule to the rule list or replace an old rule
181 * @srp: the rule to add or replace
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800182 * @rule_list: the list of rules
183 * @rule_lock: the rule list lock
Rafal Krypae05b6f92013-01-10 19:42:00 +0100184 * @global: if non-zero, indicates a global rule
Casey Schauflere114e472008-02-04 22:29:50 -0800185 *
186 * Looks through the current subject/object/access list for
187 * the subject/object pair and replaces the access that was
188 * there. If the pair isn't found add it with the specified
189 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300190 *
191 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
192 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800193 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100194static int smk_set_access(struct smack_parsed_rule *srp,
195 struct list_head *rule_list,
196 struct mutex *rule_lock, int global)
Casey Schauflere114e472008-02-04 22:29:50 -0800197{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100198 struct smack_rule *sp;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100199 struct smack_master_list *smlp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800200 int found = 0;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100201 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800202
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800203 mutex_lock(rule_lock);
204
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700205 /*
206 * Because the object label is less likely to match
207 * than the subject label check it first
208 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800209 list_for_each_entry_rcu(sp, rule_list, list) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700210 if (sp->smk_object == srp->smk_object &&
211 sp->smk_subject == srp->smk_subject) {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100212 found = 1;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100213 sp->smk_access |= srp->smk_access1;
214 sp->smk_access &= ~srp->smk_access2;
Casey Schauflere114e472008-02-04 22:29:50 -0800215 break;
216 }
Casey Schauflere114e472008-02-04 22:29:50 -0800217 }
218
Rafal Krypae05b6f92013-01-10 19:42:00 +0100219 if (found == 0) {
220 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
221 if (sp == NULL) {
222 rc = -ENOMEM;
223 goto out;
224 }
225
226 sp->smk_subject = srp->smk_subject;
227 sp->smk_object = srp->smk_object;
228 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
229
230 list_add_rcu(&sp->list, rule_list);
231 /*
232 * If this is a global as opposed to self and a new rule
233 * it needs to get added for reporting.
234 */
235 if (global) {
236 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
237 if (smlp != NULL) {
238 smlp->smk_rule = sp;
239 list_add_rcu(&smlp->list, &smack_rule_list);
240 } else
241 rc = -ENOMEM;
242 }
243 }
244
245out:
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800246 mutex_unlock(rule_lock);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100247 return rc;
248}
Casey Schauflere114e472008-02-04 22:29:50 -0800249
Rafal Krypae05b6f92013-01-10 19:42:00 +0100250/**
251 * smk_perm_from_str - parse smack accesses from a text string
252 * @string: a text string that contains a Smack accesses code
253 *
254 * Returns an integer with respective bits set for specified accesses.
255 */
256static int smk_perm_from_str(const char *string)
257{
258 int perm = 0;
259 const char *cp;
260
261 for (cp = string; ; cp++)
262 switch (*cp) {
263 case '-':
264 break;
265 case 'r':
266 case 'R':
267 perm |= MAY_READ;
268 break;
269 case 'w':
270 case 'W':
271 perm |= MAY_WRITE;
272 break;
273 case 'x':
274 case 'X':
275 perm |= MAY_EXEC;
276 break;
277 case 'a':
278 case 'A':
279 perm |= MAY_APPEND;
280 break;
281 case 't':
282 case 'T':
283 perm |= MAY_TRANSMUTE;
284 break;
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700285 case 'l':
286 case 'L':
287 perm |= MAY_LOCK;
288 break;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100289 default:
290 return perm;
291 }
Casey Schauflere114e472008-02-04 22:29:50 -0800292}
293
294/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700295 * smk_fill_rule - Fill Smack rule from strings
296 * @subject: subject label string
297 * @object: object label string
Rafal Krypae05b6f92013-01-10 19:42:00 +0100298 * @access1: access string
299 * @access2: string with permissions to be removed
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300300 * @rule: Smack rule
301 * @import: if non-zero, import labels
Casey Schaufler35187212012-06-18 19:01:36 -0700302 * @len: label length limit
Casey Schauflerf7112e62012-05-06 15:22:02 -0700303 *
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200304 * Returns 0 on success, -EINVAL on failure and -ENOENT when either subject
305 * or object is missing.
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300306 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700307static int smk_fill_rule(const char *subject, const char *object,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100308 const char *access1, const char *access2,
309 struct smack_parsed_rule *rule, int import,
310 int len)
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300311{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700312 const char *cp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300313 struct smack_known *skp;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300314
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300315 if (import) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700316 rule->smk_subject = smk_import_entry(subject, len);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300317 if (rule->smk_subject == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200318 return -EINVAL;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300319
Casey Schaufler35187212012-06-18 19:01:36 -0700320 rule->smk_object = smk_import(object, len);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300321 if (rule->smk_object == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200322 return -EINVAL;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300323 } else {
Casey Schaufler35187212012-06-18 19:01:36 -0700324 cp = smk_parse_smack(subject, len);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700325 if (cp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200326 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700327 skp = smk_find_entry(cp);
328 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300329 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200330 return -ENOENT;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700331 rule->smk_subject = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300332
Casey Schaufler35187212012-06-18 19:01:36 -0700333 cp = smk_parse_smack(object, len);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700334 if (cp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200335 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700336 skp = smk_find_entry(cp);
337 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300338 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200339 return -ENOENT;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300340 rule->smk_object = skp->smk_known;
341 }
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300342
Rafal Krypae05b6f92013-01-10 19:42:00 +0100343 rule->smk_access1 = smk_perm_from_str(access1);
344 if (access2)
345 rule->smk_access2 = smk_perm_from_str(access2);
346 else
347 rule->smk_access2 = ~rule->smk_access1;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300348
Casey Schaufler35187212012-06-18 19:01:36 -0700349 return 0;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300350}
351
352/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700353 * smk_parse_rule - parse Smack rule from load string
354 * @data: string to be parsed whose size is SMK_LOADLEN
355 * @rule: Smack rule
356 * @import: if non-zero, import labels
357 *
358 * Returns 0 on success, -1 on errors.
359 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100360static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
361 int import)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700362{
363 int rc;
364
365 rc = smk_fill_rule(data, data + SMK_LABELLEN,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100366 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
367 import, SMK_LABELLEN);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700368 return rc;
369}
370
371/**
372 * smk_parse_long_rule - parse Smack rule from rule string
373 * @data: string to be parsed, null terminated
Rafal Krypae05b6f92013-01-10 19:42:00 +0100374 * @rule: Will be filled with Smack parsed rule
Casey Schauflerf7112e62012-05-06 15:22:02 -0700375 * @import: if non-zero, import labels
Rafal Krypa10289b02013-08-09 11:47:07 +0200376 * @tokens: numer of substrings expected in data
Casey Schauflerf7112e62012-05-06 15:22:02 -0700377 *
Rafal Krypa10289b02013-08-09 11:47:07 +0200378 * Returns number of processed bytes on success, -1 on failure.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700379 */
Rafal Krypa10289b02013-08-09 11:47:07 +0200380static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
381 int import, int tokens)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700382{
Rafal Krypa10289b02013-08-09 11:47:07 +0200383 ssize_t cnt = 0;
384 char *tok[4];
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200385 int rc;
Rafal Krypa10289b02013-08-09 11:47:07 +0200386 int i;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700387
Rafal Krypa10289b02013-08-09 11:47:07 +0200388 /*
389 * Parsing the rule in-place, filling all white-spaces with '\0'
390 */
391 for (i = 0; i < tokens; ++i) {
392 while (isspace(data[cnt]))
393 data[cnt++] = '\0';
Alan Cox3b9fc372012-07-26 14:47:11 -0700394
Rafal Krypa10289b02013-08-09 11:47:07 +0200395 if (data[cnt] == '\0')
396 /* Unexpected end of data */
397 return -1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700398
Rafal Krypa10289b02013-08-09 11:47:07 +0200399 tok[i] = data + cnt;
400
401 while (data[cnt] && !isspace(data[cnt]))
402 ++cnt;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100403 }
Rafal Krypa10289b02013-08-09 11:47:07 +0200404 while (isspace(data[cnt]))
405 data[cnt++] = '\0';
Casey Schauflerf7112e62012-05-06 15:22:02 -0700406
Rafal Krypa10289b02013-08-09 11:47:07 +0200407 while (i < 4)
408 tok[i++] = NULL;
409
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200410 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
411 return rc == 0 ? cnt : rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700412}
413
414#define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
415#define SMK_LONG_FMT 1 /* Variable long label format */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100416#define SMK_CHANGE_FMT 2 /* Rule modification format */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700417/**
418 * smk_write_rules_list - write() for any /smack rule file
Randy Dunlap251a2a92009-02-18 11:42:33 -0800419 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800420 * @buf: where to get the data from
421 * @count: bytes sent
422 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800423 * @rule_list: the list of rules to write to
424 * @rule_lock: lock for the rule list
Rafal Krypae05b6f92013-01-10 19:42:00 +0100425 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
Casey Schauflere114e472008-02-04 22:29:50 -0800426 *
427 * Get one smack access rule from above.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700428 * The format for SMK_LONG_FMT is:
429 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
430 * The format for SMK_FIXED24_FMT is exactly:
431 * "subject object rwxat"
Rafal Krypae05b6f92013-01-10 19:42:00 +0100432 * The format for SMK_CHANGE_FMT is:
433 * "subject<whitespace>object<whitespace>
434 * acc_enable<whitespace>acc_disable[<whitespace>...]"
Casey Schauflere114e472008-02-04 22:29:50 -0800435 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700436static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
437 size_t count, loff_t *ppos,
438 struct list_head *rule_list,
439 struct mutex *rule_lock, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800440{
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200441 struct smack_parsed_rule rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800442 char *data;
Rafal Krypa10289b02013-08-09 11:47:07 +0200443 int rc;
444 int trunc = 0;
445 int tokens;
446 ssize_t cnt = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800447
448 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800449 * No partial writes.
450 * Enough data must be present.
451 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200452 if (*ppos != 0)
453 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -0800454
Casey Schauflerf7112e62012-05-06 15:22:02 -0700455 if (format == SMK_FIXED24_FMT) {
456 /*
457 * Minor hack for backward compatibility
458 */
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700459 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700460 return -EINVAL;
Rafal Krypa10289b02013-08-09 11:47:07 +0200461 } else {
462 if (count >= PAGE_SIZE) {
463 count = PAGE_SIZE - 1;
464 trunc = 1;
465 }
466 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700467
Rafal Krypa10289b02013-08-09 11:47:07 +0200468 data = kmalloc(count + 1, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -0800469 if (data == NULL)
470 return -ENOMEM;
471
472 if (copy_from_user(data, buf, count) != 0) {
473 rc = -EFAULT;
474 goto out;
475 }
476
Rafal Krypa10289b02013-08-09 11:47:07 +0200477 /*
478 * In case of parsing only part of user buf,
479 * avoid having partial rule at the data buffer
480 */
481 if (trunc) {
482 while (count > 0 && (data[count - 1] != '\n'))
483 --count;
484 if (count == 0) {
485 rc = -EINVAL;
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200486 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200487 }
488 }
489
490 data[count] = '\0';
491 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
492 while (cnt < count) {
493 if (format == SMK_FIXED24_FMT) {
494 rc = smk_parse_rule(data, &rule, 1);
495 if (rc != 0) {
496 rc = -EINVAL;
497 goto out;
498 }
499 cnt = count;
500 } else {
501 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
502 if (rc <= 0) {
503 rc = -EINVAL;
504 goto out;
505 }
506 cnt += rc;
507 }
508
509 if (rule_list == NULL)
510 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
511 &rule.smk_subject->smk_rules_lock, 1);
512 else
513 rc = smk_set_access(&rule, rule_list, rule_lock, 0);
514
515 if (rc)
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200516 goto out;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700517 }
518
Rafal Krypa10289b02013-08-09 11:47:07 +0200519 rc = cnt;
Casey Schauflere114e472008-02-04 22:29:50 -0800520out:
521 kfree(data);
522 return rc;
523}
524
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800525/*
Casey Schaufler40809562011-11-10 15:02:22 -0800526 * Core logic for smackfs seq list operations.
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800527 */
528
Casey Schaufler40809562011-11-10 15:02:22 -0800529static void *smk_seq_start(struct seq_file *s, loff_t *pos,
530 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800531{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700532 struct list_head *list;
533
534 /*
535 * This is 0 the first time through.
536 */
537 if (s->index == 0)
Casey Schaufler40809562011-11-10 15:02:22 -0800538 s->private = head;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700539
540 if (s->private == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800541 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700542
543 list = s->private;
544 if (list_empty(list))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800545 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700546
547 if (s->index == 0)
548 return list->next;
549 return list;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800550}
551
Casey Schaufler40809562011-11-10 15:02:22 -0800552static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
553 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800554{
555 struct list_head *list = v;
556
Casey Schaufler40809562011-11-10 15:02:22 -0800557 if (list_is_last(list, head)) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700558 s->private = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800559 return NULL;
560 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700561 s->private = list->next;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800562 return list->next;
563}
564
Casey Schaufler40809562011-11-10 15:02:22 -0800565static void smk_seq_stop(struct seq_file *s, void *v)
566{
567 /* No-op */
568}
569
Casey Schauflerf7112e62012-05-06 15:22:02 -0700570static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
Casey Schaufler40809562011-11-10 15:02:22 -0800571{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700572 /*
573 * Don't show any rules with label names too long for
574 * interface file (/smack/load or /smack/load2)
575 * because you should expect to be able to write
576 * anything you read back.
577 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700578 if (strlen(srp->smk_subject->smk_known) >= max ||
579 strlen(srp->smk_object) >= max)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700580 return;
Casey Schaufler40809562011-11-10 15:02:22 -0800581
Rafal Krypa65ee7f42012-07-09 19:36:34 +0200582 if (srp->smk_access == 0)
583 return;
584
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700585 seq_printf(s, "%s %s", srp->smk_subject->smk_known, srp->smk_object);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800586
587 seq_putc(s, ' ');
588
589 if (srp->smk_access & MAY_READ)
590 seq_putc(s, 'r');
591 if (srp->smk_access & MAY_WRITE)
592 seq_putc(s, 'w');
593 if (srp->smk_access & MAY_EXEC)
594 seq_putc(s, 'x');
595 if (srp->smk_access & MAY_APPEND)
596 seq_putc(s, 'a');
597 if (srp->smk_access & MAY_TRANSMUTE)
598 seq_putc(s, 't');
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700599 if (srp->smk_access & MAY_LOCK)
600 seq_putc(s, 'l');
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800601
602 seq_putc(s, '\n');
Casey Schauflerf7112e62012-05-06 15:22:02 -0700603}
604
605/*
606 * Seq_file read operations for /smack/load
607 */
608
609static void *load2_seq_start(struct seq_file *s, loff_t *pos)
610{
611 return smk_seq_start(s, pos, &smack_rule_list);
612}
613
614static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
615{
616 return smk_seq_next(s, v, pos, &smack_rule_list);
617}
618
619static int load_seq_show(struct seq_file *s, void *v)
620{
621 struct list_head *list = v;
622 struct smack_master_list *smlp =
623 list_entry(list, struct smack_master_list, list);
624
625 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800626
627 return 0;
628}
629
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800630static const struct seq_operations load_seq_ops = {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700631 .start = load2_seq_start,
632 .next = load2_seq_next,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800633 .show = load_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800634 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800635};
636
637/**
638 * smk_open_load - open() for /smack/load
639 * @inode: inode structure representing file
640 * @file: "load" file pointer
641 *
642 * For reading, use load_seq_* seq_file reading operations.
643 */
644static int smk_open_load(struct inode *inode, struct file *file)
645{
646 return seq_open(file, &load_seq_ops);
647}
648
649/**
650 * smk_write_load - write() for /smack/load
651 * @file: file pointer, not actually used
652 * @buf: where to get the data from
653 * @count: bytes sent
654 * @ppos: where to start - must be 0
655 *
656 */
657static ssize_t smk_write_load(struct file *file, const char __user *buf,
658 size_t count, loff_t *ppos)
659{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800660 /*
661 * Must have privilege.
662 * No partial writes.
663 * Enough data must be present.
664 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700665 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800666 return -EPERM;
667
Casey Schauflerf7112e62012-05-06 15:22:02 -0700668 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
669 SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800670}
671
Casey Schauflere114e472008-02-04 22:29:50 -0800672static const struct file_operations smk_load_ops = {
673 .open = smk_open_load,
674 .read = seq_read,
675 .llseek = seq_lseek,
676 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700677 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800678};
679
680/**
681 * smk_cipso_doi - initialize the CIPSO domain
682 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700683static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800684{
685 int rc;
686 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500687 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800688
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500689 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800690
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500691 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800692 if (rc != 0)
693 printk(KERN_WARNING "%s:%d remove rc = %d\n",
694 __func__, __LINE__, rc);
695
696 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
697 if (doip == NULL)
698 panic("smack: Failed to initialize cipso DOI.\n");
699 doip->map.std = NULL;
700 doip->doi = smk_cipso_doi_value;
701 doip->type = CIPSO_V4_MAP_PASS;
702 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
703 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
704 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
705
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500706 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400707 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500708 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800709 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400710 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500711 return;
712 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500713 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500714 if (rc != 0) {
715 printk(KERN_WARNING "%s:%d map add rc = %d\n",
716 __func__, __LINE__, rc);
717 kfree(doip);
718 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400719 }
Casey Schauflere114e472008-02-04 22:29:50 -0800720}
721
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800722/**
723 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800724 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800725 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700726static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800727{
728 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500729 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800730
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500731 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800732
733 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500734 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800735 if (rc != 0)
736 printk(KERN_WARNING "%s:%d remove rc = %d\n",
737 __func__, __LINE__, rc);
738 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700739 if (smack_net_ambient == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700740 smack_net_ambient = &smack_known_floor;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800741
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700742 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500743 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800744 if (rc != 0)
745 printk(KERN_WARNING "%s:%d add rc = %d\n",
746 __func__, __LINE__, rc);
747}
748
Casey Schauflere114e472008-02-04 22:29:50 -0800749/*
750 * Seq_file read operations for /smack/cipso
751 */
752
753static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
754{
Casey Schaufler40809562011-11-10 15:02:22 -0800755 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800756}
757
758static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
759{
Casey Schaufler40809562011-11-10 15:02:22 -0800760 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800761}
762
763/*
764 * Print cipso labels in format:
765 * label level[/cat[,cat]]
766 */
767static int cipso_seq_show(struct seq_file *s, void *v)
768{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100769 struct list_head *list = v;
770 struct smack_known *skp =
771 list_entry(list, struct smack_known, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700772 struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800773 char sep = '/';
Casey Schauflere114e472008-02-04 22:29:50 -0800774 int i;
Casey Schauflere114e472008-02-04 22:29:50 -0800775
Casey Schauflerf7112e62012-05-06 15:22:02 -0700776 /*
777 * Don't show a label that could not have been set using
778 * /smack/cipso. This is in support of the notion that
779 * anything read from /smack/cipso ought to be writeable
780 * to /smack/cipso.
781 *
782 * /smack/cipso2 should be used instead.
783 */
784 if (strlen(skp->smk_known) >= SMK_LABELLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800785 return 0;
786
Casey Schauflerf7112e62012-05-06 15:22:02 -0700787 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
Casey Schauflere114e472008-02-04 22:29:50 -0800788
Casey Schauflerf7112e62012-05-06 15:22:02 -0700789 for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
790 i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
791 seq_printf(s, "%c%d", sep, i);
792 sep = ',';
793 }
Casey Schauflere114e472008-02-04 22:29:50 -0800794
795 seq_putc(s, '\n');
796
797 return 0;
798}
799
James Morris88e9d342009-09-22 16:43:43 -0700800static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800801 .start = cipso_seq_start,
Casey Schauflere114e472008-02-04 22:29:50 -0800802 .next = cipso_seq_next,
803 .show = cipso_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800804 .stop = smk_seq_stop,
Casey Schauflere114e472008-02-04 22:29:50 -0800805};
806
807/**
808 * smk_open_cipso - open() for /smack/cipso
809 * @inode: inode structure representing file
810 * @file: "cipso" file pointer
811 *
812 * Connect our cipso_seq_* operations with /smack/cipso
813 * file_operations
814 */
815static int smk_open_cipso(struct inode *inode, struct file *file)
816{
817 return seq_open(file, &cipso_seq_ops);
818}
819
820/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700821 * smk_set_cipso - do the work for write() for cipso and cipso2
Randy Dunlap251a2a92009-02-18 11:42:33 -0800822 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800823 * @buf: where to get the data from
824 * @count: bytes sent
825 * @ppos: where to start
Casey Schauflerf7112e62012-05-06 15:22:02 -0700826 * @format: /smack/cipso or /smack/cipso2
Casey Schauflere114e472008-02-04 22:29:50 -0800827 *
828 * Accepts only one cipso rule per write call.
829 * Returns number of bytes written or error code, as appropriate
830 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700831static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
832 size_t count, loff_t *ppos, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800833{
834 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700835 struct netlbl_lsm_secattr ncats;
836 char mapcatset[SMK_CIPSOLEN];
Casey Schauflere114e472008-02-04 22:29:50 -0800837 int maplevel;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700838 unsigned int cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800839 int catlen;
840 ssize_t rc = -EINVAL;
841 char *data = NULL;
842 char *rule;
843 int ret;
844 int i;
845
846 /*
847 * Must have privilege.
848 * No partial writes.
849 * Enough data must be present.
850 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700851 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -0800852 return -EPERM;
853 if (*ppos != 0)
854 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700855 if (format == SMK_FIXED24_FMT &&
856 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
Casey Schauflere114e472008-02-04 22:29:50 -0800857 return -EINVAL;
858
859 data = kzalloc(count + 1, GFP_KERNEL);
860 if (data == NULL)
861 return -ENOMEM;
862
863 if (copy_from_user(data, buf, count) != 0) {
864 rc = -EFAULT;
865 goto unlockedout;
866 }
867
868 data[count] = '\0';
869 rule = data;
870 /*
871 * Only allow one writer at a time. Writes should be
872 * quite rare and small in any case.
873 */
874 mutex_lock(&smack_cipso_lock);
875
876 skp = smk_import_entry(rule, 0);
877 if (skp == NULL)
878 goto out;
879
Casey Schauflerf7112e62012-05-06 15:22:02 -0700880 if (format == SMK_FIXED24_FMT)
881 rule += SMK_LABELLEN;
882 else
Passion,Zhao0fcfee62013-06-03 11:42:24 +0800883 rule += strlen(skp->smk_known) + 1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700884
Casey Schauflere114e472008-02-04 22:29:50 -0800885 ret = sscanf(rule, "%d", &maplevel);
886 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
887 goto out;
888
889 rule += SMK_DIGITLEN;
890 ret = sscanf(rule, "%d", &catlen);
891 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
892 goto out;
893
Casey Schauflerf7112e62012-05-06 15:22:02 -0700894 if (format == SMK_FIXED24_FMT &&
895 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800896 goto out;
897
898 memset(mapcatset, 0, sizeof(mapcatset));
899
900 for (i = 0; i < catlen; i++) {
901 rule += SMK_DIGITLEN;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700902 ret = sscanf(rule, "%u", &cat);
Casey Schaufler677264e2013-06-28 13:47:07 -0700903 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
Casey Schauflere114e472008-02-04 22:29:50 -0800904 goto out;
905
906 smack_catset_bit(cat, mapcatset);
907 }
908
Casey Schauflerf7112e62012-05-06 15:22:02 -0700909 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
910 if (rc >= 0) {
911 netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat);
912 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
913 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
914 rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -0800915 }
916
Casey Schauflere114e472008-02-04 22:29:50 -0800917out:
918 mutex_unlock(&smack_cipso_lock);
919unlockedout:
920 kfree(data);
921 return rc;
922}
923
Casey Schauflerf7112e62012-05-06 15:22:02 -0700924/**
925 * smk_write_cipso - write() for /smack/cipso
926 * @file: file pointer, not actually used
927 * @buf: where to get the data from
928 * @count: bytes sent
929 * @ppos: where to start
930 *
931 * Accepts only one cipso rule per write call.
932 * Returns number of bytes written or error code, as appropriate
933 */
934static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
935 size_t count, loff_t *ppos)
936{
937 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
938}
939
Casey Schauflere114e472008-02-04 22:29:50 -0800940static const struct file_operations smk_cipso_ops = {
941 .open = smk_open_cipso,
942 .read = seq_read,
943 .llseek = seq_lseek,
944 .write = smk_write_cipso,
945 .release = seq_release,
946};
947
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500948/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700949 * Seq_file read operations for /smack/cipso2
950 */
951
952/*
953 * Print cipso labels in format:
954 * label level[/cat[,cat]]
955 */
956static int cipso2_seq_show(struct seq_file *s, void *v)
957{
958 struct list_head *list = v;
959 struct smack_known *skp =
960 list_entry(list, struct smack_known, list);
961 struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
962 char sep = '/';
963 int i;
964
965 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
966
967 for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
968 i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
969 seq_printf(s, "%c%d", sep, i);
970 sep = ',';
971 }
972
973 seq_putc(s, '\n');
974
975 return 0;
976}
977
978static const struct seq_operations cipso2_seq_ops = {
979 .start = cipso_seq_start,
980 .next = cipso_seq_next,
981 .show = cipso2_seq_show,
982 .stop = smk_seq_stop,
983};
984
985/**
986 * smk_open_cipso2 - open() for /smack/cipso2
987 * @inode: inode structure representing file
988 * @file: "cipso2" file pointer
989 *
990 * Connect our cipso_seq_* operations with /smack/cipso2
991 * file_operations
992 */
993static int smk_open_cipso2(struct inode *inode, struct file *file)
994{
995 return seq_open(file, &cipso2_seq_ops);
996}
997
998/**
999 * smk_write_cipso2 - write() for /smack/cipso2
1000 * @file: file pointer, not actually used
1001 * @buf: where to get the data from
1002 * @count: bytes sent
1003 * @ppos: where to start
1004 *
1005 * Accepts only one cipso rule per write call.
1006 * Returns number of bytes written or error code, as appropriate
1007 */
1008static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1009 size_t count, loff_t *ppos)
1010{
1011 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1012}
1013
1014static const struct file_operations smk_cipso2_ops = {
1015 .open = smk_open_cipso2,
1016 .read = seq_read,
1017 .llseek = seq_lseek,
1018 .write = smk_write_cipso2,
1019 .release = seq_release,
1020};
1021
1022/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001023 * Seq_file read operations for /smack/netlabel
1024 */
1025
1026static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1027{
Casey Schaufler40809562011-11-10 15:02:22 -08001028 return smk_seq_start(s, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001029}
1030
1031static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1032{
Casey Schaufler40809562011-11-10 15:02:22 -08001033 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001034}
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001035#define BEBITS (sizeof(__be32) * 8)
1036
1037/*
1038 * Print host/label pairs
1039 */
1040static int netlbladdr_seq_show(struct seq_file *s, void *v)
1041{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001042 struct list_head *list = v;
1043 struct smk_netlbladdr *skp =
1044 list_entry(list, struct smk_netlbladdr, list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001045 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001046 int maskn;
1047 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001048
etienne113a0e42009-03-04 07:33:51 +01001049 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001050
1051 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
1052 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
1053
1054 return 0;
1055}
1056
James Morris88e9d342009-09-22 16:43:43 -07001057static const struct seq_operations netlbladdr_seq_ops = {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001058 .start = netlbladdr_seq_start,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001059 .next = netlbladdr_seq_next,
1060 .show = netlbladdr_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001061 .stop = smk_seq_stop,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001062};
1063
1064/**
1065 * smk_open_netlbladdr - open() for /smack/netlabel
1066 * @inode: inode structure representing file
1067 * @file: "netlabel" file pointer
1068 *
1069 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1070 * file_operations
1071 */
1072static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1073{
1074 return seq_open(file, &netlbladdr_seq_ops);
1075}
1076
1077/**
etienne113a0e42009-03-04 07:33:51 +01001078 * smk_netlbladdr_insert
1079 * @new : netlabel to insert
1080 *
1081 * This helper insert netlabel in the smack_netlbladdrs list
1082 * sorted by netmask length (longest to smallest)
Etienne Basset7198e2e2009-03-24 20:53:24 +01001083 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1084 *
etienne113a0e42009-03-04 07:33:51 +01001085 */
1086static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1087{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001088 struct smk_netlbladdr *m, *m_next;
etienne113a0e42009-03-04 07:33:51 +01001089
Etienne Basset7198e2e2009-03-24 20:53:24 +01001090 if (list_empty(&smk_netlbladdr_list)) {
1091 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001092 return;
1093 }
1094
Jiri Pirko05725f72009-04-14 20:17:16 +02001095 m = list_entry_rcu(smk_netlbladdr_list.next,
1096 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001097
etienne113a0e42009-03-04 07:33:51 +01001098 /* the comparison '>' is a bit hacky, but works */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001099 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1100 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001101 return;
1102 }
Etienne Basset7198e2e2009-03-24 20:53:24 +01001103
1104 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1105 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1106 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001107 return;
1108 }
Jiri Pirko05725f72009-04-14 20:17:16 +02001109 m_next = list_entry_rcu(m->list.next,
1110 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001111 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1112 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001113 return;
1114 }
1115 }
1116}
1117
1118
1119/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001120 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -08001121 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001122 * @buf: where to get the data from
1123 * @count: bytes sent
1124 * @ppos: where to start
1125 *
1126 * Accepts only one netlbladdr per write call.
1127 * Returns number of bytes written or error code, as appropriate
1128 */
1129static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1130 size_t count, loff_t *ppos)
1131{
1132 struct smk_netlbladdr *skp;
1133 struct sockaddr_in newname;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001134 char *smack;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001135 char *sp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001136 char *data;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001137 char *host = (char *)&newname.sin_addr.s_addr;
1138 int rc;
1139 struct netlbl_audit audit_info;
1140 struct in_addr mask;
1141 unsigned int m;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001142 int found;
etienne113a0e42009-03-04 07:33:51 +01001143 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001144 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +01001145 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001146
1147 /*
1148 * Must have privilege.
1149 * No partial writes.
1150 * Enough data must be present.
1151 * "<addr/mask, as a.b.c.d/e><space><label>"
1152 * "<addr, as a.b.c.d><space><label>"
1153 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001154 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001155 return -EPERM;
1156 if (*ppos != 0)
1157 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001158 if (count < SMK_NETLBLADDRMIN)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001159 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001160
1161 data = kzalloc(count + 1, GFP_KERNEL);
1162 if (data == NULL)
1163 return -ENOMEM;
1164
1165 if (copy_from_user(data, buf, count) != 0) {
1166 rc = -EFAULT;
1167 goto free_data_out;
1168 }
1169
1170 smack = kzalloc(count + 1, GFP_KERNEL);
1171 if (smack == NULL) {
1172 rc = -ENOMEM;
1173 goto free_data_out;
1174 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001175
1176 data[count] = '\0';
1177
1178 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
1179 &host[0], &host[1], &host[2], &host[3], &m, smack);
1180 if (rc != 6) {
1181 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1182 &host[0], &host[1], &host[2], &host[3], smack);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001183 if (rc != 5) {
1184 rc = -EINVAL;
1185 goto free_out;
1186 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001187 m = BEBITS;
1188 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07001189 if (m > BEBITS) {
1190 rc = -EINVAL;
1191 goto free_out;
1192 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001193
Casey Schauflerf7112e62012-05-06 15:22:02 -07001194 /*
1195 * If smack begins with '-', it is an option, don't import it
1196 */
Etienne Basset43031542009-03-27 17:11:01 -04001197 if (smack[0] != '-') {
1198 sp = smk_import(smack, 0);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001199 if (sp == NULL) {
1200 rc = -EINVAL;
1201 goto free_out;
1202 }
Etienne Basset43031542009-03-27 17:11:01 -04001203 } else {
1204 /* check known options */
1205 if (strcmp(smack, smack_cipso_option) == 0)
1206 sp = (char *)smack_cipso_option;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001207 else {
1208 rc = -EINVAL;
1209 goto free_out;
1210 }
Etienne Basset43031542009-03-27 17:11:01 -04001211 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001212
etienne113a0e42009-03-04 07:33:51 +01001213 for (temp_mask = 0; m > 0; m--) {
1214 temp_mask |= mask_bits;
1215 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001216 }
etienne113a0e42009-03-04 07:33:51 +01001217 mask.s_addr = cpu_to_be32(temp_mask);
1218
1219 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001220 /*
1221 * Only allow one writer at a time. Writes should be
1222 * quite rare and small in any case.
1223 */
1224 mutex_lock(&smk_netlbladdr_lock);
1225
1226 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001227 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001228 found = 0;
1229 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001230 if (skp->smk_host.sin_addr.s_addr == nsa &&
Etienne Basset7198e2e2009-03-24 20:53:24 +01001231 skp->smk_mask.s_addr == mask.s_addr) {
1232 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001233 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001234 }
1235 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001236 smk_netlabel_audit_set(&audit_info);
1237
Etienne Basset7198e2e2009-03-24 20:53:24 +01001238 if (found == 0) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001239 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
1240 if (skp == NULL)
1241 rc = -ENOMEM;
1242 else {
1243 rc = 0;
1244 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1245 skp->smk_mask.s_addr = mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001246 skp->smk_label = sp;
etienne113a0e42009-03-04 07:33:51 +01001247 smk_netlbladdr_insert(skp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001248 }
1249 } else {
Etienne Basset43031542009-03-27 17:11:01 -04001250 /* we delete the unlabeled entry, only if the previous label
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001251 * wasn't the special CIPSO option */
Etienne Basset43031542009-03-27 17:11:01 -04001252 if (skp->smk_label != smack_cipso_option)
1253 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1254 &skp->smk_host.sin_addr, &skp->smk_mask,
1255 PF_INET, &audit_info);
1256 else
1257 rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001258 skp->smk_label = sp;
1259 }
1260
1261 /*
1262 * Now tell netlabel about the single label nature of
1263 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -04001264 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001265 */
Etienne Basset43031542009-03-27 17:11:01 -04001266 if (rc == 0 && sp != smack_cipso_option)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001267 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1268 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
1269 smack_to_secid(skp->smk_label), &audit_info);
1270
1271 if (rc == 0)
1272 rc = count;
1273
1274 mutex_unlock(&smk_netlbladdr_lock);
1275
Casey Schauflerf7112e62012-05-06 15:22:02 -07001276free_out:
1277 kfree(smack);
1278free_data_out:
1279 kfree(data);
1280
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001281 return rc;
1282}
1283
1284static const struct file_operations smk_netlbladdr_ops = {
1285 .open = smk_open_netlbladdr,
1286 .read = seq_read,
1287 .llseek = seq_lseek,
1288 .write = smk_write_netlbladdr,
1289 .release = seq_release,
1290};
1291
Casey Schauflere114e472008-02-04 22:29:50 -08001292/**
1293 * smk_read_doi - read() for /smack/doi
1294 * @filp: file pointer, not actually used
1295 * @buf: where to put the result
1296 * @count: maximum to send along
1297 * @ppos: where to start
1298 *
1299 * Returns number of bytes read or error code, as appropriate
1300 */
1301static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1302 size_t count, loff_t *ppos)
1303{
1304 char temp[80];
1305 ssize_t rc;
1306
1307 if (*ppos != 0)
1308 return 0;
1309
1310 sprintf(temp, "%d", smk_cipso_doi_value);
1311 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1312
1313 return rc;
1314}
1315
1316/**
1317 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001318 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001319 * @buf: where to get the data from
1320 * @count: bytes sent
1321 * @ppos: where to start
1322 *
1323 * Returns number of bytes written or error code, as appropriate
1324 */
1325static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1326 size_t count, loff_t *ppos)
1327{
1328 char temp[80];
1329 int i;
1330
Casey Schaufler1880eff2012-06-05 15:28:30 -07001331 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001332 return -EPERM;
1333
1334 if (count >= sizeof(temp) || count == 0)
1335 return -EINVAL;
1336
1337 if (copy_from_user(temp, buf, count) != 0)
1338 return -EFAULT;
1339
1340 temp[count] = '\0';
1341
1342 if (sscanf(temp, "%d", &i) != 1)
1343 return -EINVAL;
1344
1345 smk_cipso_doi_value = i;
1346
1347 smk_cipso_doi();
1348
1349 return count;
1350}
1351
1352static const struct file_operations smk_doi_ops = {
1353 .read = smk_read_doi,
1354 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001355 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001356};
1357
1358/**
1359 * smk_read_direct - read() for /smack/direct
1360 * @filp: file pointer, not actually used
1361 * @buf: where to put the result
1362 * @count: maximum to send along
1363 * @ppos: where to start
1364 *
1365 * Returns number of bytes read or error code, as appropriate
1366 */
1367static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1368 size_t count, loff_t *ppos)
1369{
1370 char temp[80];
1371 ssize_t rc;
1372
1373 if (*ppos != 0)
1374 return 0;
1375
1376 sprintf(temp, "%d", smack_cipso_direct);
1377 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1378
1379 return rc;
1380}
1381
1382/**
1383 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001384 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001385 * @buf: where to get the data from
1386 * @count: bytes sent
1387 * @ppos: where to start
1388 *
1389 * Returns number of bytes written or error code, as appropriate
1390 */
1391static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1392 size_t count, loff_t *ppos)
1393{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001394 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001395 char temp[80];
1396 int i;
1397
Casey Schaufler1880eff2012-06-05 15:28:30 -07001398 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001399 return -EPERM;
1400
1401 if (count >= sizeof(temp) || count == 0)
1402 return -EINVAL;
1403
1404 if (copy_from_user(temp, buf, count) != 0)
1405 return -EFAULT;
1406
1407 temp[count] = '\0';
1408
1409 if (sscanf(temp, "%d", &i) != 1)
1410 return -EINVAL;
1411
Casey Schauflerf7112e62012-05-06 15:22:02 -07001412 /*
1413 * Don't do anything if the value hasn't actually changed.
1414 * If it is changing reset the level on entries that were
1415 * set up to be direct when they were created.
1416 */
1417 if (smack_cipso_direct != i) {
1418 mutex_lock(&smack_known_lock);
1419 list_for_each_entry_rcu(skp, &smack_known_list, list)
1420 if (skp->smk_netlabel.attr.mls.lvl ==
1421 smack_cipso_direct)
1422 skp->smk_netlabel.attr.mls.lvl = i;
1423 smack_cipso_direct = i;
1424 mutex_unlock(&smack_known_lock);
1425 }
Casey Schauflere114e472008-02-04 22:29:50 -08001426
1427 return count;
1428}
1429
1430static const struct file_operations smk_direct_ops = {
1431 .read = smk_read_direct,
1432 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001433 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001434};
1435
1436/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001437 * smk_read_mapped - read() for /smack/mapped
1438 * @filp: file pointer, not actually used
1439 * @buf: where to put the result
1440 * @count: maximum to send along
1441 * @ppos: where to start
1442 *
1443 * Returns number of bytes read or error code, as appropriate
1444 */
1445static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1446 size_t count, loff_t *ppos)
1447{
1448 char temp[80];
1449 ssize_t rc;
1450
1451 if (*ppos != 0)
1452 return 0;
1453
1454 sprintf(temp, "%d", smack_cipso_mapped);
1455 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1456
1457 return rc;
1458}
1459
1460/**
1461 * smk_write_mapped - write() for /smack/mapped
1462 * @file: file pointer, not actually used
1463 * @buf: where to get the data from
1464 * @count: bytes sent
1465 * @ppos: where to start
1466 *
1467 * Returns number of bytes written or error code, as appropriate
1468 */
1469static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1470 size_t count, loff_t *ppos)
1471{
1472 struct smack_known *skp;
1473 char temp[80];
1474 int i;
1475
Casey Schaufler1880eff2012-06-05 15:28:30 -07001476 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001477 return -EPERM;
1478
1479 if (count >= sizeof(temp) || count == 0)
1480 return -EINVAL;
1481
1482 if (copy_from_user(temp, buf, count) != 0)
1483 return -EFAULT;
1484
1485 temp[count] = '\0';
1486
1487 if (sscanf(temp, "%d", &i) != 1)
1488 return -EINVAL;
1489
1490 /*
1491 * Don't do anything if the value hasn't actually changed.
1492 * If it is changing reset the level on entries that were
1493 * set up to be mapped when they were created.
1494 */
1495 if (smack_cipso_mapped != i) {
1496 mutex_lock(&smack_known_lock);
1497 list_for_each_entry_rcu(skp, &smack_known_list, list)
1498 if (skp->smk_netlabel.attr.mls.lvl ==
1499 smack_cipso_mapped)
1500 skp->smk_netlabel.attr.mls.lvl = i;
1501 smack_cipso_mapped = i;
1502 mutex_unlock(&smack_known_lock);
1503 }
1504
1505 return count;
1506}
1507
1508static const struct file_operations smk_mapped_ops = {
1509 .read = smk_read_mapped,
1510 .write = smk_write_mapped,
1511 .llseek = default_llseek,
1512};
1513
1514/**
Casey Schauflere114e472008-02-04 22:29:50 -08001515 * smk_read_ambient - read() for /smack/ambient
1516 * @filp: file pointer, not actually used
1517 * @buf: where to put the result
1518 * @cn: maximum to send along
1519 * @ppos: where to start
1520 *
1521 * Returns number of bytes read or error code, as appropriate
1522 */
1523static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1524 size_t cn, loff_t *ppos)
1525{
1526 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001527 int asize;
1528
1529 if (*ppos != 0)
1530 return 0;
1531 /*
1532 * Being careful to avoid a problem in the case where
1533 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001534 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001535 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001536
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001537 asize = strlen(smack_net_ambient->smk_known) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001538
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001539 if (cn >= asize)
1540 rc = simple_read_from_buffer(buf, cn, ppos,
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001541 smack_net_ambient->smk_known,
1542 asize);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001543 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 Schaufler2f823ff2013-05-22 18:43:03 -07001563 struct smack_known *skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001564 char *oldambient;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001565 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 Schaufler2f823ff2013-05-22 18:43:03 -07001580 skp = smk_import_entry(data, count);
1581 if (skp == NULL) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001582 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
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001588 oldambient = smack_net_ambient->smk_known;
1589 smack_net_ambient = skp;
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 Schaufler2f823ff2013-05-22 18:43:03 -07001648 struct smack_known *skp = 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 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001659 if (smack_onlycap != NULL && smack_onlycap != skp->smk_known)
Casey Schaufler15446232008-07-30 15:37:11 -07001660 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;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001842 int res;
1843
1844 data = simple_transaction_get(file, buf, count);
1845 if (IS_ERR(data))
1846 return PTR_ERR(data);
1847
1848 if (format == SMK_FIXED24_FMT) {
1849 if (count < SMK_LOADLEN)
1850 return -EINVAL;
1851 res = smk_parse_rule(data, &rule, 0);
1852 } else {
1853 /*
Rafal Krypa10289b02013-08-09 11:47:07 +02001854 * simple_transaction_get() returns null-terminated data
Casey Schauflerf7112e62012-05-06 15:22:02 -07001855 */
Rafal Krypa10289b02013-08-09 11:47:07 +02001856 res = smk_parse_long_rule(data, &rule, 0, 3);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001857 }
1858
Jarkko Sakkinen398ce072013-11-28 19:16:46 +02001859 if (res >= 0)
1860 res = smk_access(rule.smk_subject, rule.smk_object,
1861 rule.smk_access1, NULL);
1862 else if (res != -ENOENT)
Casey Schauflerf7112e62012-05-06 15:22:02 -07001863 return -EINVAL;
1864
Casey Schauflerf7112e62012-05-06 15:22:02 -07001865 data[0] = res == 0 ? '1' : '0';
1866 data[1] = '\0';
1867
1868 simple_transaction_set(file, 2);
1869
1870 if (format == SMK_FIXED24_FMT)
1871 return SMK_LOADLEN;
1872 return count;
1873}
1874
1875/**
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001876 * smk_write_access - handle access check transaction
1877 * @file: file pointer
1878 * @buf: data from user space
1879 * @count: bytes sent
1880 * @ppos: where to start - must be 0
1881 */
1882static ssize_t smk_write_access(struct file *file, const char __user *buf,
1883 size_t count, loff_t *ppos)
1884{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001885 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001886}
1887
1888static const struct file_operations smk_access_ops = {
1889 .write = smk_write_access,
1890 .read = simple_transaction_read,
1891 .release = simple_transaction_release,
1892 .llseek = generic_file_llseek,
1893};
1894
Casey Schauflerf7112e62012-05-06 15:22:02 -07001895
1896/*
1897 * Seq_file read operations for /smack/load2
1898 */
1899
1900static int load2_seq_show(struct seq_file *s, void *v)
1901{
1902 struct list_head *list = v;
1903 struct smack_master_list *smlp =
1904 list_entry(list, struct smack_master_list, list);
1905
1906 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
1907
1908 return 0;
1909}
1910
1911static const struct seq_operations load2_seq_ops = {
1912 .start = load2_seq_start,
1913 .next = load2_seq_next,
1914 .show = load2_seq_show,
1915 .stop = smk_seq_stop,
1916};
1917
1918/**
1919 * smk_open_load2 - open() for /smack/load2
1920 * @inode: inode structure representing file
1921 * @file: "load2" file pointer
1922 *
1923 * For reading, use load2_seq_* seq_file reading operations.
1924 */
1925static int smk_open_load2(struct inode *inode, struct file *file)
1926{
1927 return seq_open(file, &load2_seq_ops);
1928}
1929
1930/**
1931 * smk_write_load2 - write() for /smack/load2
1932 * @file: file pointer, not actually used
1933 * @buf: where to get the data from
1934 * @count: bytes sent
1935 * @ppos: where to start - must be 0
1936 *
1937 */
1938static ssize_t smk_write_load2(struct file *file, const char __user *buf,
1939 size_t count, loff_t *ppos)
1940{
1941 /*
1942 * Must have privilege.
1943 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001944 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001945 return -EPERM;
1946
1947 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
1948 SMK_LONG_FMT);
1949}
1950
1951static const struct file_operations smk_load2_ops = {
1952 .open = smk_open_load2,
1953 .read = seq_read,
1954 .llseek = seq_lseek,
1955 .write = smk_write_load2,
1956 .release = seq_release,
1957};
1958
1959/*
1960 * Seq_file read operations for /smack/load-self2
1961 */
1962
1963static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
1964{
1965 struct task_smack *tsp = current_security();
1966
1967 return smk_seq_start(s, pos, &tsp->smk_rules);
1968}
1969
1970static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
1971{
1972 struct task_smack *tsp = current_security();
1973
1974 return smk_seq_next(s, v, pos, &tsp->smk_rules);
1975}
1976
1977static int load_self2_seq_show(struct seq_file *s, void *v)
1978{
1979 struct list_head *list = v;
1980 struct smack_rule *srp =
1981 list_entry(list, struct smack_rule, list);
1982
1983 smk_rule_show(s, srp, SMK_LONGLABEL);
1984
1985 return 0;
1986}
1987
1988static const struct seq_operations load_self2_seq_ops = {
1989 .start = load_self2_seq_start,
1990 .next = load_self2_seq_next,
1991 .show = load_self2_seq_show,
1992 .stop = smk_seq_stop,
1993};
1994
1995/**
1996 * smk_open_load_self2 - open() for /smack/load-self2
1997 * @inode: inode structure representing file
1998 * @file: "load" file pointer
1999 *
2000 * For reading, use load_seq_* seq_file reading operations.
2001 */
2002static int smk_open_load_self2(struct inode *inode, struct file *file)
2003{
2004 return seq_open(file, &load_self2_seq_ops);
2005}
2006
2007/**
2008 * smk_write_load_self2 - write() for /smack/load-self2
2009 * @file: file pointer, not actually used
2010 * @buf: where to get the data from
2011 * @count: bytes sent
2012 * @ppos: where to start - must be 0
2013 *
2014 */
2015static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2016 size_t count, loff_t *ppos)
2017{
2018 struct task_smack *tsp = current_security();
2019
2020 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2021 &tsp->smk_rules_lock, SMK_LONG_FMT);
2022}
2023
2024static const struct file_operations smk_load_self2_ops = {
2025 .open = smk_open_load_self2,
2026 .read = seq_read,
2027 .llseek = seq_lseek,
2028 .write = smk_write_load_self2,
2029 .release = seq_release,
2030};
2031
2032/**
2033 * smk_write_access2 - handle access check transaction
2034 * @file: file pointer
2035 * @buf: data from user space
2036 * @count: bytes sent
2037 * @ppos: where to start - must be 0
2038 */
2039static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2040 size_t count, loff_t *ppos)
2041{
2042 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2043}
2044
2045static const struct file_operations smk_access2_ops = {
2046 .write = smk_write_access2,
2047 .read = simple_transaction_read,
2048 .release = simple_transaction_release,
2049 .llseek = generic_file_llseek,
2050};
2051
Etienne Bassetecfcc532009-04-08 20:40:06 +02002052/**
Rafal Krypa449543b2012-07-11 17:49:30 +02002053 * smk_write_revoke_subj - write() for /smack/revoke-subject
2054 * @file: file pointer
2055 * @buf: data from user space
2056 * @count: bytes sent
2057 * @ppos: where to start - must be 0
2058 */
2059static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2060 size_t count, loff_t *ppos)
2061{
2062 char *data = NULL;
2063 const char *cp = NULL;
2064 struct smack_known *skp;
2065 struct smack_rule *sp;
2066 struct list_head *rule_list;
2067 struct mutex *rule_lock;
2068 int rc = count;
2069
2070 if (*ppos != 0)
2071 return -EINVAL;
2072
2073 if (!smack_privileged(CAP_MAC_ADMIN))
2074 return -EPERM;
2075
2076 if (count == 0 || count > SMK_LONGLABEL)
2077 return -EINVAL;
2078
2079 data = kzalloc(count, GFP_KERNEL);
2080 if (data == NULL)
2081 return -ENOMEM;
2082
2083 if (copy_from_user(data, buf, count) != 0) {
2084 rc = -EFAULT;
2085 goto free_out;
2086 }
2087
2088 cp = smk_parse_smack(data, count);
2089 if (cp == NULL) {
2090 rc = -EINVAL;
2091 goto free_out;
2092 }
2093
2094 skp = smk_find_entry(cp);
Rafal Krypad15d9fa2012-11-27 16:28:11 +01002095 if (skp == NULL)
Rafal Krypa449543b2012-07-11 17:49:30 +02002096 goto free_out;
Rafal Krypa449543b2012-07-11 17:49:30 +02002097
2098 rule_list = &skp->smk_rules;
2099 rule_lock = &skp->smk_rules_lock;
2100
2101 mutex_lock(rule_lock);
2102
2103 list_for_each_entry_rcu(sp, rule_list, list)
2104 sp->smk_access = 0;
2105
2106 mutex_unlock(rule_lock);
2107
2108free_out:
2109 kfree(data);
2110 kfree(cp);
2111 return rc;
2112}
2113
2114static const struct file_operations smk_revoke_subj_ops = {
2115 .write = smk_write_revoke_subj,
2116 .read = simple_transaction_read,
2117 .release = simple_transaction_release,
2118 .llseek = generic_file_llseek,
2119};
2120
Casey Schauflere9307232012-11-01 18:14:32 -07002121static struct kset *smackfs_kset;
2122/**
2123 * smk_init_sysfs - initialize /sys/fs/smackfs
2124 *
2125 */
2126static int smk_init_sysfs(void)
2127{
2128 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2129 if (!smackfs_kset)
2130 return -ENOMEM;
2131 return 0;
2132}
2133
Rafal Krypa449543b2012-07-11 17:49:30 +02002134/**
Rafal Krypae05b6f92013-01-10 19:42:00 +01002135 * smk_write_change_rule - write() for /smack/change-rule
2136 * @file: file pointer
2137 * @buf: data from user space
2138 * @count: bytes sent
2139 * @ppos: where to start - must be 0
2140 */
2141static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2142 size_t count, loff_t *ppos)
2143{
2144 /*
2145 * Must have privilege.
2146 */
2147 if (!capable(CAP_MAC_ADMIN))
2148 return -EPERM;
2149
2150 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2151 SMK_CHANGE_FMT);
2152}
2153
2154static const struct file_operations smk_change_rule_ops = {
2155 .write = smk_write_change_rule,
2156 .read = simple_transaction_read,
2157 .release = simple_transaction_release,
2158 .llseek = generic_file_llseek,
2159};
2160
2161/**
Casey Schauflere114e472008-02-04 22:29:50 -08002162 * smk_fill_super - fill the /smackfs superblock
2163 * @sb: the empty superblock
2164 * @data: unused
2165 * @silent: unused
2166 *
2167 * Fill in the well known entries for /smack
2168 *
2169 * Returns 0 on success, an error code on failure
2170 */
2171static int smk_fill_super(struct super_block *sb, void *data, int silent)
2172{
2173 int rc;
2174 struct inode *root_inode;
2175
2176 static struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002177 [SMK_LOAD] = {
2178 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2179 [SMK_CIPSO] = {
2180 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2181 [SMK_DOI] = {
2182 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2183 [SMK_DIRECT] = {
2184 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2185 [SMK_AMBIENT] = {
2186 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2187 [SMK_NETLBLADDR] = {
2188 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2189 [SMK_ONLYCAP] = {
2190 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2191 [SMK_LOGGING] = {
2192 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2193 [SMK_LOAD_SELF] = {
2194 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002195 [SMK_ACCESSES] = {
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03002196 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
Casey Schauflerf7112e62012-05-06 15:22:02 -07002197 [SMK_MAPPED] = {
2198 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2199 [SMK_LOAD2] = {
2200 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2201 [SMK_LOAD_SELF2] = {
2202 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2203 [SMK_ACCESS2] = {
2204 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2205 [SMK_CIPSO2] = {
2206 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
Rafal Krypa449543b2012-07-11 17:49:30 +02002207 [SMK_REVOKE_SUBJ] = {
2208 "revoke-subject", &smk_revoke_subj_ops,
2209 S_IRUGO|S_IWUSR},
Rafal Krypae05b6f92013-01-10 19:42:00 +01002210 [SMK_CHANGE_RULE] = {
2211 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002212 /* last one */
2213 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08002214 };
2215
2216 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2217 if (rc != 0) {
2218 printk(KERN_ERR "%s failed %d while creating inodes\n",
2219 __func__, rc);
2220 return rc;
2221 }
2222
2223 root_inode = sb->s_root->d_inode;
Casey Schauflere114e472008-02-04 22:29:50 -08002224
2225 return 0;
2226}
2227
2228/**
Al Virofc14f2f2010-07-25 01:48:30 +04002229 * smk_mount - get the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002230 * @fs_type: passed along without comment
2231 * @flags: passed along without comment
2232 * @dev_name: passed along without comment
2233 * @data: passed along without comment
Casey Schauflere114e472008-02-04 22:29:50 -08002234 *
2235 * Just passes everything along.
2236 *
2237 * Returns what the lower level code does.
2238 */
Al Virofc14f2f2010-07-25 01:48:30 +04002239static struct dentry *smk_mount(struct file_system_type *fs_type,
2240 int flags, const char *dev_name, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -08002241{
Al Virofc14f2f2010-07-25 01:48:30 +04002242 return mount_single(fs_type, flags, data, smk_fill_super);
Casey Schauflere114e472008-02-04 22:29:50 -08002243}
2244
2245static struct file_system_type smk_fs_type = {
2246 .name = "smackfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002247 .mount = smk_mount,
Casey Schauflere114e472008-02-04 22:29:50 -08002248 .kill_sb = kill_litter_super,
2249};
2250
2251static struct vfsmount *smackfs_mount;
2252
Casey Schauflerf7112e62012-05-06 15:22:02 -07002253static int __init smk_preset_netlabel(struct smack_known *skp)
2254{
2255 skp->smk_netlabel.domain = skp->smk_known;
2256 skp->smk_netlabel.flags =
2257 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2258 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2259 &skp->smk_netlabel, strlen(skp->smk_known));
2260}
2261
Casey Schauflere114e472008-02-04 22:29:50 -08002262/**
2263 * init_smk_fs - get the smackfs superblock
2264 *
2265 * register the smackfs
2266 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002267 * Do not register smackfs if Smack wasn't enabled
2268 * on boot. We can not put this method normally under the
2269 * smack_init() code path since the security subsystem get
2270 * initialized before the vfs caches.
2271 *
2272 * Returns true if we were not chosen on boot or if
2273 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08002274 */
2275static int __init init_smk_fs(void)
2276{
2277 int err;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002278 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002279
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002280 if (!security_module_enable(&smack_ops))
2281 return 0;
2282
Casey Schauflere9307232012-11-01 18:14:32 -07002283 err = smk_init_sysfs();
2284 if (err)
2285 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2286
Casey Schauflere114e472008-02-04 22:29:50 -08002287 err = register_filesystem(&smk_fs_type);
2288 if (!err) {
2289 smackfs_mount = kern_mount(&smk_fs_type);
2290 if (IS_ERR(smackfs_mount)) {
2291 printk(KERN_ERR "smackfs: could not mount!\n");
2292 err = PTR_ERR(smackfs_mount);
2293 smackfs_mount = NULL;
2294 }
2295 }
2296
Casey Schauflere114e472008-02-04 22:29:50 -08002297 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002298 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08002299
Casey Schauflerf7112e62012-05-06 15:22:02 -07002300 rc = smk_preset_netlabel(&smack_known_floor);
2301 if (err == 0 && rc < 0)
2302 err = rc;
2303 rc = smk_preset_netlabel(&smack_known_hat);
2304 if (err == 0 && rc < 0)
2305 err = rc;
2306 rc = smk_preset_netlabel(&smack_known_huh);
2307 if (err == 0 && rc < 0)
2308 err = rc;
2309 rc = smk_preset_netlabel(&smack_known_invalid);
2310 if (err == 0 && rc < 0)
2311 err = rc;
2312 rc = smk_preset_netlabel(&smack_known_star);
2313 if (err == 0 && rc < 0)
2314 err = rc;
2315 rc = smk_preset_netlabel(&smack_known_web);
2316 if (err == 0 && rc < 0)
2317 err = rc;
2318
Casey Schauflere114e472008-02-04 22:29:50 -08002319 return err;
2320}
2321
2322__initcall(init_smk_fs);