blob: 4aa12c8d3c63f8a08c2cd6933e955cb41a36c072 [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 Schaufler00f84f32013-12-23 11:07:10 -080055 SMK_SYSLOG = 20, /* change syslog label) */
Lukasz Pawelczyk66867812014-03-11 17:07:06 +010056 SMK_PTRACE = 21, /* set ptrace rule */
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070057#ifdef CONFIG_SECURITY_SMACK_BRINGUP
58 SMK_UNCONFINED = 22, /* define an unconfined label */
59#endif
Casey Schauflere114e472008-02-04 22:29:50 -080060};
61
62/*
63 * List locks
64 */
Casey Schauflere114e472008-02-04 22:29:50 -080065static DEFINE_MUTEX(smack_cipso_lock);
Casey Schaufler4bc87e62008-02-15 15:24:25 -080066static DEFINE_MUTEX(smack_ambient_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -050067static DEFINE_MUTEX(smk_netlbladdr_lock);
Casey Schauflere114e472008-02-04 22:29:50 -080068
69/*
70 * This is the "ambient" label for network traffic.
71 * If it isn't somehow marked, use this.
72 * It can be reset via smackfs/ambient
73 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -070074struct smack_known *smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -080075
76/*
Casey Schauflere114e472008-02-04 22:29:50 -080077 * This is the level in a CIPSO header that indicates a
78 * smack label is contained directly in the category set.
79 * It can be reset via smackfs/direct
80 */
81int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
82
Casey Schaufler15446232008-07-30 15:37:11 -070083/*
Casey Schauflerf7112e62012-05-06 15:22:02 -070084 * This is the level in a CIPSO header that indicates a
85 * secid is contained directly in the category set.
86 * It can be reset via smackfs/mapped
87 */
88int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
89
90/*
Casey Schaufler15446232008-07-30 15:37:11 -070091 * Unless a process is running with this label even
92 * having CAP_MAC_OVERRIDE isn't enough to grant
93 * privilege to violate MAC policy. If no label is
94 * designated (the NULL case) capabilities apply to
95 * everyone. It is expected that the hat (^) label
96 * will be used if any label is used.
97 */
Casey Schaufler00f84f32013-12-23 11:07:10 -080098struct smack_known *smack_onlycap;
99
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700100#ifdef CONFIG_SECURITY_SMACK_BRINGUP
101/*
102 * Allow one label to be unconfined. This is for
103 * debugging and application bring-up purposes only.
104 * It is bad and wrong, but everyone seems to expect
105 * to have it.
106 */
107struct smack_known *smack_unconfined;
108#endif
109
Casey Schaufler00f84f32013-12-23 11:07:10 -0800110/*
111 * If this value is set restrict syslog use to the label specified.
112 * It can be reset via smackfs/syslog
113 */
114struct smack_known *smack_syslog_label;
Casey Schaufler15446232008-07-30 15:37:11 -0700115
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500116/*
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100117 * Ptrace current rule
118 * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
119 * SMACK_PTRACE_EXACT labels must match, but can be overriden with
120 * CAP_SYS_PTRACE
121 * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
122 */
123int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
124
125/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500126 * Certain IP addresses may be designated as single label hosts.
127 * Packets are sent there unlabeled, but only from tasks that
128 * can write to the specified label.
129 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100130
131LIST_HEAD(smk_netlbladdr_list);
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700132
133/*
134 * Rule lists are maintained for each label.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700135 * This master list is just for reading /smack/load and /smack/load2.
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700136 */
137struct smack_master_list {
138 struct list_head list;
139 struct smack_rule *smk_rule;
140};
141
Etienne Basset7198e2e2009-03-24 20:53:24 +0100142LIST_HEAD(smack_rule_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500143
Rafal Krypae05b6f92013-01-10 19:42:00 +0100144struct smack_parsed_rule {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700145 struct smack_known *smk_subject;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200146 struct smack_known *smk_object;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100147 int smk_access1;
148 int smk_access2;
149};
150
Casey Schauflere114e472008-02-04 22:29:50 -0800151static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
Casey Schauflere114e472008-02-04 22:29:50 -0800152
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200153struct smack_known smack_cipso_option = {
154 .smk_known = SMACK_CIPSO_OPTION,
155 .smk_secid = 0,
156};
Etienne Basset43031542009-03-27 17:11:01 -0400157
Casey Schauflere114e472008-02-04 22:29:50 -0800158/*
Casey Schauflere114e472008-02-04 22:29:50 -0800159 * Values for parsing cipso rules
160 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700161 * SMK_CIPSOMIN: Minimum possible cipso rule length.
162 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -0800163 */
164#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700165#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
166#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
167
168/*
169 * Values for parsing MAC rules
170 * SMK_ACCESS: Maximum possible combination of access permissions
171 * SMK_ACCESSLEN: Maximum length for a rule access field
172 * SMK_LOADLEN: Smack rule length
173 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200174#define SMK_OACCESS "rwxa"
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700175#define SMK_ACCESS "rwxatl"
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200176#define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
177#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
178#define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
179#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700180
Casey Schauflerf7112e62012-05-06 15:22:02 -0700181/*
182 * Stricly for CIPSO level manipulation.
183 * Set the category bit number in a smack label sized buffer.
184 */
185static inline void smack_catset_bit(unsigned int cat, char *catsetp)
186{
187 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
188 return;
189
190 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
191}
192
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500193/**
194 * smk_netlabel_audit_set - fill a netlbl_audit struct
195 * @nap: structure to fill
196 */
197static void smk_netlabel_audit_set(struct netlbl_audit *nap)
198{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700199 struct smack_known *skp = smk_of_current();
200
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500201 nap->loginuid = audit_get_loginuid(current);
202 nap->sessionid = audit_get_sessionid(current);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700203 nap->secid = skp->smk_secid;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500204}
205
206/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700207 * Value for parsing single label host rules
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500208 * "1.2.3.4 X"
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500209 */
210#define SMK_NETLBLADDRMIN 9
Casey Schauflere114e472008-02-04 22:29:50 -0800211
Casey Schauflere114e472008-02-04 22:29:50 -0800212/**
Rafal Krypae05b6f92013-01-10 19:42:00 +0100213 * smk_set_access - add a rule to the rule list or replace an old rule
214 * @srp: the rule to add or replace
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800215 * @rule_list: the list of rules
216 * @rule_lock: the rule list lock
Rafal Krypae05b6f92013-01-10 19:42:00 +0100217 * @global: if non-zero, indicates a global rule
Casey Schauflere114e472008-02-04 22:29:50 -0800218 *
219 * Looks through the current subject/object/access list for
220 * the subject/object pair and replaces the access that was
221 * there. If the pair isn't found add it with the specified
222 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300223 *
224 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
225 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800226 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100227static int smk_set_access(struct smack_parsed_rule *srp,
228 struct list_head *rule_list,
229 struct mutex *rule_lock, int global)
Casey Schauflere114e472008-02-04 22:29:50 -0800230{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100231 struct smack_rule *sp;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100232 struct smack_master_list *smlp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800233 int found = 0;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100234 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800235
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800236 mutex_lock(rule_lock);
237
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700238 /*
239 * Because the object label is less likely to match
240 * than the subject label check it first
241 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800242 list_for_each_entry_rcu(sp, rule_list, list) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700243 if (sp->smk_object == srp->smk_object &&
244 sp->smk_subject == srp->smk_subject) {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100245 found = 1;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100246 sp->smk_access |= srp->smk_access1;
247 sp->smk_access &= ~srp->smk_access2;
Casey Schauflere114e472008-02-04 22:29:50 -0800248 break;
249 }
Casey Schauflere114e472008-02-04 22:29:50 -0800250 }
251
Rafal Krypae05b6f92013-01-10 19:42:00 +0100252 if (found == 0) {
253 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
254 if (sp == NULL) {
255 rc = -ENOMEM;
256 goto out;
257 }
258
259 sp->smk_subject = srp->smk_subject;
260 sp->smk_object = srp->smk_object;
261 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
262
263 list_add_rcu(&sp->list, rule_list);
264 /*
265 * If this is a global as opposed to self and a new rule
266 * it needs to get added for reporting.
267 */
268 if (global) {
269 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
270 if (smlp != NULL) {
271 smlp->smk_rule = sp;
272 list_add_rcu(&smlp->list, &smack_rule_list);
273 } else
274 rc = -ENOMEM;
275 }
276 }
277
278out:
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800279 mutex_unlock(rule_lock);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100280 return rc;
281}
Casey Schauflere114e472008-02-04 22:29:50 -0800282
Rafal Krypae05b6f92013-01-10 19:42:00 +0100283/**
284 * smk_perm_from_str - parse smack accesses from a text string
285 * @string: a text string that contains a Smack accesses code
286 *
287 * Returns an integer with respective bits set for specified accesses.
288 */
289static int smk_perm_from_str(const char *string)
290{
291 int perm = 0;
292 const char *cp;
293
294 for (cp = string; ; cp++)
295 switch (*cp) {
296 case '-':
297 break;
298 case 'r':
299 case 'R':
300 perm |= MAY_READ;
301 break;
302 case 'w':
303 case 'W':
304 perm |= MAY_WRITE;
305 break;
306 case 'x':
307 case 'X':
308 perm |= MAY_EXEC;
309 break;
310 case 'a':
311 case 'A':
312 perm |= MAY_APPEND;
313 break;
314 case 't':
315 case 'T':
316 perm |= MAY_TRANSMUTE;
317 break;
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700318 case 'l':
319 case 'L':
320 perm |= MAY_LOCK;
321 break;
Casey Schauflerd166c802014-08-27 14:51:27 -0700322 case 'b':
323 case 'B':
324 perm |= MAY_BRINGUP;
325 break;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100326 default:
327 return perm;
328 }
Casey Schauflere114e472008-02-04 22:29:50 -0800329}
330
331/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700332 * smk_fill_rule - Fill Smack rule from strings
333 * @subject: subject label string
334 * @object: object label string
Rafal Krypae05b6f92013-01-10 19:42:00 +0100335 * @access1: access string
336 * @access2: string with permissions to be removed
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300337 * @rule: Smack rule
338 * @import: if non-zero, import labels
Casey Schaufler35187212012-06-18 19:01:36 -0700339 * @len: label length limit
Casey Schauflerf7112e62012-05-06 15:22:02 -0700340 *
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200341 * Returns 0 on success, -EINVAL on failure and -ENOENT when either subject
342 * or object is missing.
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300343 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700344static int smk_fill_rule(const char *subject, const char *object,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100345 const char *access1, const char *access2,
346 struct smack_parsed_rule *rule, int import,
347 int len)
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300348{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700349 const char *cp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300350 struct smack_known *skp;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300351
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300352 if (import) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700353 rule->smk_subject = smk_import_entry(subject, len);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300354 if (rule->smk_subject == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200355 return -EINVAL;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300356
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200357 rule->smk_object = smk_import_entry(object, len);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300358 if (rule->smk_object == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200359 return -EINVAL;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300360 } else {
Casey Schaufler35187212012-06-18 19:01:36 -0700361 cp = smk_parse_smack(subject, len);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700362 if (cp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200363 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700364 skp = smk_find_entry(cp);
365 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300366 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200367 return -ENOENT;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700368 rule->smk_subject = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300369
Casey Schaufler35187212012-06-18 19:01:36 -0700370 cp = smk_parse_smack(object, len);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700371 if (cp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200372 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700373 skp = smk_find_entry(cp);
374 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300375 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200376 return -ENOENT;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200377 rule->smk_object = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300378 }
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300379
Rafal Krypae05b6f92013-01-10 19:42:00 +0100380 rule->smk_access1 = smk_perm_from_str(access1);
381 if (access2)
382 rule->smk_access2 = smk_perm_from_str(access2);
383 else
384 rule->smk_access2 = ~rule->smk_access1;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300385
Casey Schaufler35187212012-06-18 19:01:36 -0700386 return 0;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300387}
388
389/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700390 * smk_parse_rule - parse Smack rule from load string
391 * @data: string to be parsed whose size is SMK_LOADLEN
392 * @rule: Smack rule
393 * @import: if non-zero, import labels
394 *
395 * Returns 0 on success, -1 on errors.
396 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100397static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
398 int import)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700399{
400 int rc;
401
402 rc = smk_fill_rule(data, data + SMK_LABELLEN,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100403 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
404 import, SMK_LABELLEN);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700405 return rc;
406}
407
408/**
409 * smk_parse_long_rule - parse Smack rule from rule string
410 * @data: string to be parsed, null terminated
Rafal Krypae05b6f92013-01-10 19:42:00 +0100411 * @rule: Will be filled with Smack parsed rule
Casey Schauflerf7112e62012-05-06 15:22:02 -0700412 * @import: if non-zero, import labels
Rafal Krypa10289b02013-08-09 11:47:07 +0200413 * @tokens: numer of substrings expected in data
Casey Schauflerf7112e62012-05-06 15:22:02 -0700414 *
Rafal Krypa10289b02013-08-09 11:47:07 +0200415 * Returns number of processed bytes on success, -1 on failure.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700416 */
Rafal Krypa10289b02013-08-09 11:47:07 +0200417static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
418 int import, int tokens)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700419{
Rafal Krypa10289b02013-08-09 11:47:07 +0200420 ssize_t cnt = 0;
421 char *tok[4];
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200422 int rc;
Rafal Krypa10289b02013-08-09 11:47:07 +0200423 int i;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700424
Rafal Krypa10289b02013-08-09 11:47:07 +0200425 /*
426 * Parsing the rule in-place, filling all white-spaces with '\0'
427 */
428 for (i = 0; i < tokens; ++i) {
429 while (isspace(data[cnt]))
430 data[cnt++] = '\0';
Alan Cox3b9fc372012-07-26 14:47:11 -0700431
Rafal Krypa10289b02013-08-09 11:47:07 +0200432 if (data[cnt] == '\0')
433 /* Unexpected end of data */
434 return -1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700435
Rafal Krypa10289b02013-08-09 11:47:07 +0200436 tok[i] = data + cnt;
437
438 while (data[cnt] && !isspace(data[cnt]))
439 ++cnt;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100440 }
Rafal Krypa10289b02013-08-09 11:47:07 +0200441 while (isspace(data[cnt]))
442 data[cnt++] = '\0';
Casey Schauflerf7112e62012-05-06 15:22:02 -0700443
Rafal Krypa10289b02013-08-09 11:47:07 +0200444 while (i < 4)
445 tok[i++] = NULL;
446
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200447 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
448 return rc == 0 ? cnt : rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700449}
450
451#define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
452#define SMK_LONG_FMT 1 /* Variable long label format */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100453#define SMK_CHANGE_FMT 2 /* Rule modification format */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700454/**
455 * smk_write_rules_list - write() for any /smack rule file
Randy Dunlap251a2a92009-02-18 11:42:33 -0800456 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800457 * @buf: where to get the data from
458 * @count: bytes sent
459 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800460 * @rule_list: the list of rules to write to
461 * @rule_lock: lock for the rule list
Rafal Krypae05b6f92013-01-10 19:42:00 +0100462 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
Casey Schauflere114e472008-02-04 22:29:50 -0800463 *
464 * Get one smack access rule from above.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700465 * The format for SMK_LONG_FMT is:
466 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
467 * The format for SMK_FIXED24_FMT is exactly:
468 * "subject object rwxat"
Rafal Krypae05b6f92013-01-10 19:42:00 +0100469 * The format for SMK_CHANGE_FMT is:
470 * "subject<whitespace>object<whitespace>
471 * acc_enable<whitespace>acc_disable[<whitespace>...]"
Casey Schauflere114e472008-02-04 22:29:50 -0800472 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700473static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
474 size_t count, loff_t *ppos,
475 struct list_head *rule_list,
476 struct mutex *rule_lock, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800477{
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200478 struct smack_parsed_rule rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800479 char *data;
Rafal Krypa10289b02013-08-09 11:47:07 +0200480 int rc;
481 int trunc = 0;
482 int tokens;
483 ssize_t cnt = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800484
485 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800486 * No partial writes.
487 * Enough data must be present.
488 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200489 if (*ppos != 0)
490 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -0800491
Casey Schauflerf7112e62012-05-06 15:22:02 -0700492 if (format == SMK_FIXED24_FMT) {
493 /*
494 * Minor hack for backward compatibility
495 */
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700496 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700497 return -EINVAL;
Rafal Krypa10289b02013-08-09 11:47:07 +0200498 } else {
499 if (count >= PAGE_SIZE) {
500 count = PAGE_SIZE - 1;
501 trunc = 1;
502 }
503 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700504
Rafal Krypa10289b02013-08-09 11:47:07 +0200505 data = kmalloc(count + 1, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -0800506 if (data == NULL)
507 return -ENOMEM;
508
509 if (copy_from_user(data, buf, count) != 0) {
510 rc = -EFAULT;
511 goto out;
512 }
513
Rafal Krypa10289b02013-08-09 11:47:07 +0200514 /*
515 * In case of parsing only part of user buf,
516 * avoid having partial rule at the data buffer
517 */
518 if (trunc) {
519 while (count > 0 && (data[count - 1] != '\n'))
520 --count;
521 if (count == 0) {
522 rc = -EINVAL;
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200523 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200524 }
525 }
526
527 data[count] = '\0';
528 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
529 while (cnt < count) {
530 if (format == SMK_FIXED24_FMT) {
531 rc = smk_parse_rule(data, &rule, 1);
532 if (rc != 0) {
533 rc = -EINVAL;
534 goto out;
535 }
536 cnt = count;
537 } else {
538 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
539 if (rc <= 0) {
540 rc = -EINVAL;
541 goto out;
542 }
543 cnt += rc;
544 }
545
546 if (rule_list == NULL)
547 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
548 &rule.smk_subject->smk_rules_lock, 1);
549 else
550 rc = smk_set_access(&rule, rule_list, rule_lock, 0);
551
552 if (rc)
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200553 goto out;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700554 }
555
Rafal Krypa10289b02013-08-09 11:47:07 +0200556 rc = cnt;
Casey Schauflere114e472008-02-04 22:29:50 -0800557out:
558 kfree(data);
559 return rc;
560}
561
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800562/*
Casey Schaufler40809562011-11-10 15:02:22 -0800563 * Core logic for smackfs seq list operations.
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800564 */
565
Casey Schaufler40809562011-11-10 15:02:22 -0800566static void *smk_seq_start(struct seq_file *s, loff_t *pos,
567 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800568{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700569 struct list_head *list;
570
571 /*
572 * This is 0 the first time through.
573 */
574 if (s->index == 0)
Casey Schaufler40809562011-11-10 15:02:22 -0800575 s->private = head;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700576
577 if (s->private == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800578 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700579
580 list = s->private;
581 if (list_empty(list))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800582 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700583
584 if (s->index == 0)
585 return list->next;
586 return list;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800587}
588
Casey Schaufler40809562011-11-10 15:02:22 -0800589static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
590 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800591{
592 struct list_head *list = v;
593
Casey Schaufler40809562011-11-10 15:02:22 -0800594 if (list_is_last(list, head)) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700595 s->private = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800596 return NULL;
597 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700598 s->private = list->next;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800599 return list->next;
600}
601
Casey Schaufler40809562011-11-10 15:02:22 -0800602static void smk_seq_stop(struct seq_file *s, void *v)
603{
604 /* No-op */
605}
606
Casey Schauflerf7112e62012-05-06 15:22:02 -0700607static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
Casey Schaufler40809562011-11-10 15:02:22 -0800608{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700609 /*
610 * Don't show any rules with label names too long for
611 * interface file (/smack/load or /smack/load2)
612 * because you should expect to be able to write
613 * anything you read back.
614 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700615 if (strlen(srp->smk_subject->smk_known) >= max ||
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200616 strlen(srp->smk_object->smk_known) >= max)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700617 return;
Casey Schaufler40809562011-11-10 15:02:22 -0800618
Rafal Krypa65ee7f42012-07-09 19:36:34 +0200619 if (srp->smk_access == 0)
620 return;
621
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200622 seq_printf(s, "%s %s",
623 srp->smk_subject->smk_known,
624 srp->smk_object->smk_known);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800625
626 seq_putc(s, ' ');
627
628 if (srp->smk_access & MAY_READ)
629 seq_putc(s, 'r');
630 if (srp->smk_access & MAY_WRITE)
631 seq_putc(s, 'w');
632 if (srp->smk_access & MAY_EXEC)
633 seq_putc(s, 'x');
634 if (srp->smk_access & MAY_APPEND)
635 seq_putc(s, 'a');
636 if (srp->smk_access & MAY_TRANSMUTE)
637 seq_putc(s, 't');
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700638 if (srp->smk_access & MAY_LOCK)
639 seq_putc(s, 'l');
Casey Schauflerd166c802014-08-27 14:51:27 -0700640 if (srp->smk_access & MAY_BRINGUP)
641 seq_putc(s, 'b');
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800642
643 seq_putc(s, '\n');
Casey Schauflerf7112e62012-05-06 15:22:02 -0700644}
645
646/*
647 * Seq_file read operations for /smack/load
648 */
649
650static void *load2_seq_start(struct seq_file *s, loff_t *pos)
651{
652 return smk_seq_start(s, pos, &smack_rule_list);
653}
654
655static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
656{
657 return smk_seq_next(s, v, pos, &smack_rule_list);
658}
659
660static int load_seq_show(struct seq_file *s, void *v)
661{
662 struct list_head *list = v;
663 struct smack_master_list *smlp =
664 list_entry(list, struct smack_master_list, list);
665
666 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800667
668 return 0;
669}
670
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800671static const struct seq_operations load_seq_ops = {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700672 .start = load2_seq_start,
673 .next = load2_seq_next,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800674 .show = load_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800675 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800676};
677
678/**
679 * smk_open_load - open() for /smack/load
680 * @inode: inode structure representing file
681 * @file: "load" file pointer
682 *
683 * For reading, use load_seq_* seq_file reading operations.
684 */
685static int smk_open_load(struct inode *inode, struct file *file)
686{
687 return seq_open(file, &load_seq_ops);
688}
689
690/**
691 * smk_write_load - write() for /smack/load
692 * @file: file pointer, not actually used
693 * @buf: where to get the data from
694 * @count: bytes sent
695 * @ppos: where to start - must be 0
696 *
697 */
698static ssize_t smk_write_load(struct file *file, const char __user *buf,
699 size_t count, loff_t *ppos)
700{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800701 /*
702 * Must have privilege.
703 * No partial writes.
704 * Enough data must be present.
705 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700706 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800707 return -EPERM;
708
Casey Schauflerf7112e62012-05-06 15:22:02 -0700709 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
710 SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800711}
712
Casey Schauflere114e472008-02-04 22:29:50 -0800713static const struct file_operations smk_load_ops = {
714 .open = smk_open_load,
715 .read = seq_read,
716 .llseek = seq_lseek,
717 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700718 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800719};
720
721/**
722 * smk_cipso_doi - initialize the CIPSO domain
723 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700724static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800725{
726 int rc;
727 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500728 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800729
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500730 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800731
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500732 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800733 if (rc != 0)
734 printk(KERN_WARNING "%s:%d remove rc = %d\n",
735 __func__, __LINE__, rc);
736
737 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
738 if (doip == NULL)
739 panic("smack: Failed to initialize cipso DOI.\n");
740 doip->map.std = NULL;
741 doip->doi = smk_cipso_doi_value;
742 doip->type = CIPSO_V4_MAP_PASS;
743 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
744 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
745 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
746
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500747 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400748 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500749 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800750 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400751 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500752 return;
753 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500754 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500755 if (rc != 0) {
756 printk(KERN_WARNING "%s:%d map add rc = %d\n",
757 __func__, __LINE__, rc);
758 kfree(doip);
759 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400760 }
Casey Schauflere114e472008-02-04 22:29:50 -0800761}
762
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800763/**
764 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800765 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800766 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700767static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800768{
769 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500770 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800771
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500772 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800773
774 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500775 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800776 if (rc != 0)
777 printk(KERN_WARNING "%s:%d remove rc = %d\n",
778 __func__, __LINE__, rc);
779 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700780 if (smack_net_ambient == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700781 smack_net_ambient = &smack_known_floor;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800782
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700783 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500784 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800785 if (rc != 0)
786 printk(KERN_WARNING "%s:%d add rc = %d\n",
787 __func__, __LINE__, rc);
788}
789
Casey Schauflere114e472008-02-04 22:29:50 -0800790/*
791 * Seq_file read operations for /smack/cipso
792 */
793
794static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
795{
Casey Schaufler40809562011-11-10 15:02:22 -0800796 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800797}
798
799static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
800{
Casey Schaufler40809562011-11-10 15:02:22 -0800801 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800802}
803
804/*
805 * Print cipso labels in format:
806 * label level[/cat[,cat]]
807 */
808static int cipso_seq_show(struct seq_file *s, void *v)
809{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100810 struct list_head *list = v;
811 struct smack_known *skp =
812 list_entry(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400813 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800814 char sep = '/';
Casey Schauflere114e472008-02-04 22:29:50 -0800815 int i;
Casey Schauflere114e472008-02-04 22:29:50 -0800816
Casey Schauflerf7112e62012-05-06 15:22:02 -0700817 /*
818 * Don't show a label that could not have been set using
819 * /smack/cipso. This is in support of the notion that
820 * anything read from /smack/cipso ought to be writeable
821 * to /smack/cipso.
822 *
823 * /smack/cipso2 should be used instead.
824 */
825 if (strlen(skp->smk_known) >= SMK_LABELLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800826 return 0;
827
Casey Schauflerf7112e62012-05-06 15:22:02 -0700828 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
Casey Schauflere114e472008-02-04 22:29:50 -0800829
Paul Moore4fbe63d2014-08-01 11:17:37 -0400830 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
831 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700832 seq_printf(s, "%c%d", sep, i);
833 sep = ',';
834 }
Casey Schauflere114e472008-02-04 22:29:50 -0800835
836 seq_putc(s, '\n');
837
838 return 0;
839}
840
James Morris88e9d342009-09-22 16:43:43 -0700841static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800842 .start = cipso_seq_start,
Casey Schauflere114e472008-02-04 22:29:50 -0800843 .next = cipso_seq_next,
844 .show = cipso_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800845 .stop = smk_seq_stop,
Casey Schauflere114e472008-02-04 22:29:50 -0800846};
847
848/**
849 * smk_open_cipso - open() for /smack/cipso
850 * @inode: inode structure representing file
851 * @file: "cipso" file pointer
852 *
853 * Connect our cipso_seq_* operations with /smack/cipso
854 * file_operations
855 */
856static int smk_open_cipso(struct inode *inode, struct file *file)
857{
858 return seq_open(file, &cipso_seq_ops);
859}
860
861/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700862 * smk_set_cipso - do the work for write() for cipso and cipso2
Randy Dunlap251a2a92009-02-18 11:42:33 -0800863 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800864 * @buf: where to get the data from
865 * @count: bytes sent
866 * @ppos: where to start
Casey Schauflerf7112e62012-05-06 15:22:02 -0700867 * @format: /smack/cipso or /smack/cipso2
Casey Schauflere114e472008-02-04 22:29:50 -0800868 *
869 * Accepts only one cipso rule per write call.
870 * Returns number of bytes written or error code, as appropriate
871 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700872static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
873 size_t count, loff_t *ppos, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800874{
875 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700876 struct netlbl_lsm_secattr ncats;
877 char mapcatset[SMK_CIPSOLEN];
Casey Schauflere114e472008-02-04 22:29:50 -0800878 int maplevel;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700879 unsigned int cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800880 int catlen;
881 ssize_t rc = -EINVAL;
882 char *data = NULL;
883 char *rule;
884 int ret;
885 int i;
886
887 /*
888 * Must have privilege.
889 * No partial writes.
890 * Enough data must be present.
891 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700892 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -0800893 return -EPERM;
894 if (*ppos != 0)
895 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700896 if (format == SMK_FIXED24_FMT &&
897 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
Casey Schauflere114e472008-02-04 22:29:50 -0800898 return -EINVAL;
899
900 data = kzalloc(count + 1, GFP_KERNEL);
901 if (data == NULL)
902 return -ENOMEM;
903
904 if (copy_from_user(data, buf, count) != 0) {
905 rc = -EFAULT;
906 goto unlockedout;
907 }
908
909 data[count] = '\0';
910 rule = data;
911 /*
912 * Only allow one writer at a time. Writes should be
913 * quite rare and small in any case.
914 */
915 mutex_lock(&smack_cipso_lock);
916
917 skp = smk_import_entry(rule, 0);
918 if (skp == NULL)
919 goto out;
920
Casey Schauflerf7112e62012-05-06 15:22:02 -0700921 if (format == SMK_FIXED24_FMT)
922 rule += SMK_LABELLEN;
923 else
Passion,Zhao0fcfee62013-06-03 11:42:24 +0800924 rule += strlen(skp->smk_known) + 1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700925
Casey Schauflere114e472008-02-04 22:29:50 -0800926 ret = sscanf(rule, "%d", &maplevel);
927 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
928 goto out;
929
930 rule += SMK_DIGITLEN;
931 ret = sscanf(rule, "%d", &catlen);
932 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
933 goto out;
934
Casey Schauflerf7112e62012-05-06 15:22:02 -0700935 if (format == SMK_FIXED24_FMT &&
936 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800937 goto out;
938
939 memset(mapcatset, 0, sizeof(mapcatset));
940
941 for (i = 0; i < catlen; i++) {
942 rule += SMK_DIGITLEN;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700943 ret = sscanf(rule, "%u", &cat);
Casey Schaufler677264e2013-06-28 13:47:07 -0700944 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
Casey Schauflere114e472008-02-04 22:29:50 -0800945 goto out;
946
947 smack_catset_bit(cat, mapcatset);
948 }
949
Casey Schauflerf7112e62012-05-06 15:22:02 -0700950 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
951 if (rc >= 0) {
Paul Moore4fbe63d2014-08-01 11:17:37 -0400952 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700953 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
954 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
955 rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -0800956 }
957
Casey Schauflere114e472008-02-04 22:29:50 -0800958out:
959 mutex_unlock(&smack_cipso_lock);
960unlockedout:
961 kfree(data);
962 return rc;
963}
964
Casey Schauflerf7112e62012-05-06 15:22:02 -0700965/**
966 * smk_write_cipso - write() for /smack/cipso
967 * @file: file pointer, not actually used
968 * @buf: where to get the data from
969 * @count: bytes sent
970 * @ppos: where to start
971 *
972 * Accepts only one cipso rule per write call.
973 * Returns number of bytes written or error code, as appropriate
974 */
975static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
976 size_t count, loff_t *ppos)
977{
978 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
979}
980
Casey Schauflere114e472008-02-04 22:29:50 -0800981static const struct file_operations smk_cipso_ops = {
982 .open = smk_open_cipso,
983 .read = seq_read,
984 .llseek = seq_lseek,
985 .write = smk_write_cipso,
986 .release = seq_release,
987};
988
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500989/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700990 * Seq_file read operations for /smack/cipso2
991 */
992
993/*
994 * Print cipso labels in format:
995 * label level[/cat[,cat]]
996 */
997static int cipso2_seq_show(struct seq_file *s, void *v)
998{
999 struct list_head *list = v;
1000 struct smack_known *skp =
1001 list_entry(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -04001002 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001003 char sep = '/';
1004 int i;
1005
1006 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
1007
Paul Moore4fbe63d2014-08-01 11:17:37 -04001008 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
1009 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001010 seq_printf(s, "%c%d", sep, i);
1011 sep = ',';
1012 }
1013
1014 seq_putc(s, '\n');
1015
1016 return 0;
1017}
1018
1019static const struct seq_operations cipso2_seq_ops = {
1020 .start = cipso_seq_start,
1021 .next = cipso_seq_next,
1022 .show = cipso2_seq_show,
1023 .stop = smk_seq_stop,
1024};
1025
1026/**
1027 * smk_open_cipso2 - open() for /smack/cipso2
1028 * @inode: inode structure representing file
1029 * @file: "cipso2" file pointer
1030 *
1031 * Connect our cipso_seq_* operations with /smack/cipso2
1032 * file_operations
1033 */
1034static int smk_open_cipso2(struct inode *inode, struct file *file)
1035{
1036 return seq_open(file, &cipso2_seq_ops);
1037}
1038
1039/**
1040 * smk_write_cipso2 - write() for /smack/cipso2
1041 * @file: file pointer, not actually used
1042 * @buf: where to get the data from
1043 * @count: bytes sent
1044 * @ppos: where to start
1045 *
1046 * Accepts only one cipso rule per write call.
1047 * Returns number of bytes written or error code, as appropriate
1048 */
1049static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1050 size_t count, loff_t *ppos)
1051{
1052 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1053}
1054
1055static const struct file_operations smk_cipso2_ops = {
1056 .open = smk_open_cipso2,
1057 .read = seq_read,
1058 .llseek = seq_lseek,
1059 .write = smk_write_cipso2,
1060 .release = seq_release,
1061};
1062
1063/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001064 * Seq_file read operations for /smack/netlabel
1065 */
1066
1067static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1068{
Casey Schaufler40809562011-11-10 15:02:22 -08001069 return smk_seq_start(s, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001070}
1071
1072static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1073{
Casey Schaufler40809562011-11-10 15:02:22 -08001074 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001075}
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001076#define BEBITS (sizeof(__be32) * 8)
1077
1078/*
1079 * Print host/label pairs
1080 */
1081static int netlbladdr_seq_show(struct seq_file *s, void *v)
1082{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001083 struct list_head *list = v;
1084 struct smk_netlbladdr *skp =
1085 list_entry(list, struct smk_netlbladdr, list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001086 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001087 int maskn;
1088 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001089
etienne113a0e42009-03-04 07:33:51 +01001090 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001091
1092 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001093 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label->smk_known);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001094
1095 return 0;
1096}
1097
James Morris88e9d342009-09-22 16:43:43 -07001098static const struct seq_operations netlbladdr_seq_ops = {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001099 .start = netlbladdr_seq_start,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001100 .next = netlbladdr_seq_next,
1101 .show = netlbladdr_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001102 .stop = smk_seq_stop,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001103};
1104
1105/**
1106 * smk_open_netlbladdr - open() for /smack/netlabel
1107 * @inode: inode structure representing file
1108 * @file: "netlabel" file pointer
1109 *
1110 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1111 * file_operations
1112 */
1113static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1114{
1115 return seq_open(file, &netlbladdr_seq_ops);
1116}
1117
1118/**
etienne113a0e42009-03-04 07:33:51 +01001119 * smk_netlbladdr_insert
1120 * @new : netlabel to insert
1121 *
1122 * This helper insert netlabel in the smack_netlbladdrs list
1123 * sorted by netmask length (longest to smallest)
Etienne Basset7198e2e2009-03-24 20:53:24 +01001124 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1125 *
etienne113a0e42009-03-04 07:33:51 +01001126 */
1127static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1128{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001129 struct smk_netlbladdr *m, *m_next;
etienne113a0e42009-03-04 07:33:51 +01001130
Etienne Basset7198e2e2009-03-24 20:53:24 +01001131 if (list_empty(&smk_netlbladdr_list)) {
1132 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001133 return;
1134 }
1135
Jiri Pirko05725f72009-04-14 20:17:16 +02001136 m = list_entry_rcu(smk_netlbladdr_list.next,
1137 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001138
etienne113a0e42009-03-04 07:33:51 +01001139 /* the comparison '>' is a bit hacky, but works */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001140 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1141 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001142 return;
1143 }
Etienne Basset7198e2e2009-03-24 20:53:24 +01001144
1145 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1146 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1147 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001148 return;
1149 }
Jiri Pirko05725f72009-04-14 20:17:16 +02001150 m_next = list_entry_rcu(m->list.next,
1151 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001152 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1153 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001154 return;
1155 }
1156 }
1157}
1158
1159
1160/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001161 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -08001162 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001163 * @buf: where to get the data from
1164 * @count: bytes sent
1165 * @ppos: where to start
1166 *
1167 * Accepts only one netlbladdr per write call.
1168 * Returns number of bytes written or error code, as appropriate
1169 */
1170static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1171 size_t count, loff_t *ppos)
1172{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001173 struct smk_netlbladdr *snp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001174 struct sockaddr_in newname;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001175 char *smack;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001176 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001177 char *data;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001178 char *host = (char *)&newname.sin_addr.s_addr;
1179 int rc;
1180 struct netlbl_audit audit_info;
1181 struct in_addr mask;
1182 unsigned int m;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001183 int found;
etienne113a0e42009-03-04 07:33:51 +01001184 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001185 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +01001186 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001187
1188 /*
1189 * Must have privilege.
1190 * No partial writes.
1191 * Enough data must be present.
1192 * "<addr/mask, as a.b.c.d/e><space><label>"
1193 * "<addr, as a.b.c.d><space><label>"
1194 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001195 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001196 return -EPERM;
1197 if (*ppos != 0)
1198 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001199 if (count < SMK_NETLBLADDRMIN)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001200 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001201
1202 data = kzalloc(count + 1, GFP_KERNEL);
1203 if (data == NULL)
1204 return -ENOMEM;
1205
1206 if (copy_from_user(data, buf, count) != 0) {
1207 rc = -EFAULT;
1208 goto free_data_out;
1209 }
1210
1211 smack = kzalloc(count + 1, GFP_KERNEL);
1212 if (smack == NULL) {
1213 rc = -ENOMEM;
1214 goto free_data_out;
1215 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001216
1217 data[count] = '\0';
1218
Toralf Försterec554fa2014-04-27 19:33:34 +02001219 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001220 &host[0], &host[1], &host[2], &host[3], &m, smack);
1221 if (rc != 6) {
1222 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1223 &host[0], &host[1], &host[2], &host[3], smack);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001224 if (rc != 5) {
1225 rc = -EINVAL;
1226 goto free_out;
1227 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001228 m = BEBITS;
1229 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07001230 if (m > BEBITS) {
1231 rc = -EINVAL;
1232 goto free_out;
1233 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001234
Casey Schauflerf7112e62012-05-06 15:22:02 -07001235 /*
1236 * If smack begins with '-', it is an option, don't import it
1237 */
Etienne Basset43031542009-03-27 17:11:01 -04001238 if (smack[0] != '-') {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001239 skp = smk_import_entry(smack, 0);
1240 if (skp == NULL) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001241 rc = -EINVAL;
1242 goto free_out;
1243 }
Etienne Basset43031542009-03-27 17:11:01 -04001244 } else {
1245 /* check known options */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001246 if (strcmp(smack, smack_cipso_option.smk_known) == 0)
1247 skp = &smack_cipso_option;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001248 else {
1249 rc = -EINVAL;
1250 goto free_out;
1251 }
Etienne Basset43031542009-03-27 17:11:01 -04001252 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001253
etienne113a0e42009-03-04 07:33:51 +01001254 for (temp_mask = 0; m > 0; m--) {
1255 temp_mask |= mask_bits;
1256 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001257 }
etienne113a0e42009-03-04 07:33:51 +01001258 mask.s_addr = cpu_to_be32(temp_mask);
1259
1260 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001261 /*
1262 * Only allow one writer at a time. Writes should be
1263 * quite rare and small in any case.
1264 */
1265 mutex_lock(&smk_netlbladdr_lock);
1266
1267 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001268 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001269 found = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001270 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list) {
1271 if (snp->smk_host.sin_addr.s_addr == nsa &&
1272 snp->smk_mask.s_addr == mask.s_addr) {
Etienne Basset7198e2e2009-03-24 20:53:24 +01001273 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001274 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001275 }
1276 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001277 smk_netlabel_audit_set(&audit_info);
1278
Etienne Basset7198e2e2009-03-24 20:53:24 +01001279 if (found == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001280 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1281 if (snp == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001282 rc = -ENOMEM;
1283 else {
1284 rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001285 snp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1286 snp->smk_mask.s_addr = mask.s_addr;
1287 snp->smk_label = skp;
1288 smk_netlbladdr_insert(snp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001289 }
1290 } else {
Etienne Basset43031542009-03-27 17:11:01 -04001291 /* we delete the unlabeled entry, only if the previous label
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001292 * wasn't the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001293 if (snp->smk_label != &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04001294 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001295 &snp->smk_host.sin_addr, &snp->smk_mask,
Etienne Basset43031542009-03-27 17:11:01 -04001296 PF_INET, &audit_info);
1297 else
1298 rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001299 snp->smk_label = skp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001300 }
1301
1302 /*
1303 * Now tell netlabel about the single label nature of
1304 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -04001305 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001306 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001307 if (rc == 0 && skp != &smack_cipso_option)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001308 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001309 &snp->smk_host.sin_addr, &snp->smk_mask, PF_INET,
1310 snp->smk_label->smk_secid, &audit_info);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001311
1312 if (rc == 0)
1313 rc = count;
1314
1315 mutex_unlock(&smk_netlbladdr_lock);
1316
Casey Schauflerf7112e62012-05-06 15:22:02 -07001317free_out:
1318 kfree(smack);
1319free_data_out:
1320 kfree(data);
1321
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001322 return rc;
1323}
1324
1325static const struct file_operations smk_netlbladdr_ops = {
1326 .open = smk_open_netlbladdr,
1327 .read = seq_read,
1328 .llseek = seq_lseek,
1329 .write = smk_write_netlbladdr,
1330 .release = seq_release,
1331};
1332
Casey Schauflere114e472008-02-04 22:29:50 -08001333/**
1334 * smk_read_doi - read() for /smack/doi
1335 * @filp: file pointer, not actually used
1336 * @buf: where to put the result
1337 * @count: maximum to send along
1338 * @ppos: where to start
1339 *
1340 * Returns number of bytes read or error code, as appropriate
1341 */
1342static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1343 size_t count, loff_t *ppos)
1344{
1345 char temp[80];
1346 ssize_t rc;
1347
1348 if (*ppos != 0)
1349 return 0;
1350
1351 sprintf(temp, "%d", smk_cipso_doi_value);
1352 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1353
1354 return rc;
1355}
1356
1357/**
1358 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001359 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001360 * @buf: where to get the data from
1361 * @count: bytes sent
1362 * @ppos: where to start
1363 *
1364 * Returns number of bytes written or error code, as appropriate
1365 */
1366static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1367 size_t count, loff_t *ppos)
1368{
1369 char temp[80];
1370 int i;
1371
Casey Schaufler1880eff2012-06-05 15:28:30 -07001372 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001373 return -EPERM;
1374
1375 if (count >= sizeof(temp) || count == 0)
1376 return -EINVAL;
1377
1378 if (copy_from_user(temp, buf, count) != 0)
1379 return -EFAULT;
1380
1381 temp[count] = '\0';
1382
1383 if (sscanf(temp, "%d", &i) != 1)
1384 return -EINVAL;
1385
1386 smk_cipso_doi_value = i;
1387
1388 smk_cipso_doi();
1389
1390 return count;
1391}
1392
1393static const struct file_operations smk_doi_ops = {
1394 .read = smk_read_doi,
1395 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001396 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001397};
1398
1399/**
1400 * smk_read_direct - read() for /smack/direct
1401 * @filp: file pointer, not actually used
1402 * @buf: where to put the result
1403 * @count: maximum to send along
1404 * @ppos: where to start
1405 *
1406 * Returns number of bytes read or error code, as appropriate
1407 */
1408static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1409 size_t count, loff_t *ppos)
1410{
1411 char temp[80];
1412 ssize_t rc;
1413
1414 if (*ppos != 0)
1415 return 0;
1416
1417 sprintf(temp, "%d", smack_cipso_direct);
1418 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1419
1420 return rc;
1421}
1422
1423/**
1424 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001425 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001426 * @buf: where to get the data from
1427 * @count: bytes sent
1428 * @ppos: where to start
1429 *
1430 * Returns number of bytes written or error code, as appropriate
1431 */
1432static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1433 size_t count, loff_t *ppos)
1434{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001435 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001436 char temp[80];
1437 int i;
1438
Casey Schaufler1880eff2012-06-05 15:28:30 -07001439 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001440 return -EPERM;
1441
1442 if (count >= sizeof(temp) || count == 0)
1443 return -EINVAL;
1444
1445 if (copy_from_user(temp, buf, count) != 0)
1446 return -EFAULT;
1447
1448 temp[count] = '\0';
1449
1450 if (sscanf(temp, "%d", &i) != 1)
1451 return -EINVAL;
1452
Casey Schauflerf7112e62012-05-06 15:22:02 -07001453 /*
1454 * Don't do anything if the value hasn't actually changed.
1455 * If it is changing reset the level on entries that were
1456 * set up to be direct when they were created.
1457 */
1458 if (smack_cipso_direct != i) {
1459 mutex_lock(&smack_known_lock);
1460 list_for_each_entry_rcu(skp, &smack_known_list, list)
1461 if (skp->smk_netlabel.attr.mls.lvl ==
1462 smack_cipso_direct)
1463 skp->smk_netlabel.attr.mls.lvl = i;
1464 smack_cipso_direct = i;
1465 mutex_unlock(&smack_known_lock);
1466 }
Casey Schauflere114e472008-02-04 22:29:50 -08001467
1468 return count;
1469}
1470
1471static const struct file_operations smk_direct_ops = {
1472 .read = smk_read_direct,
1473 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001474 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001475};
1476
1477/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001478 * smk_read_mapped - read() for /smack/mapped
1479 * @filp: file pointer, not actually used
1480 * @buf: where to put the result
1481 * @count: maximum to send along
1482 * @ppos: where to start
1483 *
1484 * Returns number of bytes read or error code, as appropriate
1485 */
1486static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1487 size_t count, loff_t *ppos)
1488{
1489 char temp[80];
1490 ssize_t rc;
1491
1492 if (*ppos != 0)
1493 return 0;
1494
1495 sprintf(temp, "%d", smack_cipso_mapped);
1496 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1497
1498 return rc;
1499}
1500
1501/**
1502 * smk_write_mapped - write() for /smack/mapped
1503 * @file: file pointer, not actually used
1504 * @buf: where to get the data from
1505 * @count: bytes sent
1506 * @ppos: where to start
1507 *
1508 * Returns number of bytes written or error code, as appropriate
1509 */
1510static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1511 size_t count, loff_t *ppos)
1512{
1513 struct smack_known *skp;
1514 char temp[80];
1515 int i;
1516
Casey Schaufler1880eff2012-06-05 15:28:30 -07001517 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001518 return -EPERM;
1519
1520 if (count >= sizeof(temp) || count == 0)
1521 return -EINVAL;
1522
1523 if (copy_from_user(temp, buf, count) != 0)
1524 return -EFAULT;
1525
1526 temp[count] = '\0';
1527
1528 if (sscanf(temp, "%d", &i) != 1)
1529 return -EINVAL;
1530
1531 /*
1532 * Don't do anything if the value hasn't actually changed.
1533 * If it is changing reset the level on entries that were
1534 * set up to be mapped when they were created.
1535 */
1536 if (smack_cipso_mapped != i) {
1537 mutex_lock(&smack_known_lock);
1538 list_for_each_entry_rcu(skp, &smack_known_list, list)
1539 if (skp->smk_netlabel.attr.mls.lvl ==
1540 smack_cipso_mapped)
1541 skp->smk_netlabel.attr.mls.lvl = i;
1542 smack_cipso_mapped = i;
1543 mutex_unlock(&smack_known_lock);
1544 }
1545
1546 return count;
1547}
1548
1549static const struct file_operations smk_mapped_ops = {
1550 .read = smk_read_mapped,
1551 .write = smk_write_mapped,
1552 .llseek = default_llseek,
1553};
1554
1555/**
Casey Schauflere114e472008-02-04 22:29:50 -08001556 * smk_read_ambient - read() for /smack/ambient
1557 * @filp: file pointer, not actually used
1558 * @buf: where to put the result
1559 * @cn: maximum to send along
1560 * @ppos: where to start
1561 *
1562 * Returns number of bytes read or error code, as appropriate
1563 */
1564static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1565 size_t cn, loff_t *ppos)
1566{
1567 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001568 int asize;
1569
1570 if (*ppos != 0)
1571 return 0;
1572 /*
1573 * Being careful to avoid a problem in the case where
1574 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001575 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001576 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001577
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001578 asize = strlen(smack_net_ambient->smk_known) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001579
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001580 if (cn >= asize)
1581 rc = simple_read_from_buffer(buf, cn, ppos,
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001582 smack_net_ambient->smk_known,
1583 asize);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001584 else
1585 rc = -EINVAL;
1586
1587 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001588
1589 return rc;
1590}
1591
1592/**
1593 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001594 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001595 * @buf: where to get the data from
1596 * @count: bytes sent
1597 * @ppos: where to start
1598 *
1599 * Returns number of bytes written or error code, as appropriate
1600 */
1601static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1602 size_t count, loff_t *ppos)
1603{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001604 struct smack_known *skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001605 char *oldambient;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001606 char *data;
1607 int rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -08001608
Casey Schaufler1880eff2012-06-05 15:28:30 -07001609 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001610 return -EPERM;
1611
Casey Schauflerf7112e62012-05-06 15:22:02 -07001612 data = kzalloc(count + 1, GFP_KERNEL);
1613 if (data == NULL)
1614 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001615
Casey Schauflerf7112e62012-05-06 15:22:02 -07001616 if (copy_from_user(data, buf, count) != 0) {
1617 rc = -EFAULT;
1618 goto out;
1619 }
Casey Schauflere114e472008-02-04 22:29:50 -08001620
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001621 skp = smk_import_entry(data, count);
1622 if (skp == NULL) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001623 rc = -EINVAL;
1624 goto out;
1625 }
Casey Schauflere114e472008-02-04 22:29:50 -08001626
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001627 mutex_lock(&smack_ambient_lock);
1628
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001629 oldambient = smack_net_ambient->smk_known;
1630 smack_net_ambient = skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001631 smk_unlbl_ambient(oldambient);
1632
1633 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001634
Casey Schauflerf7112e62012-05-06 15:22:02 -07001635out:
1636 kfree(data);
1637 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001638}
1639
1640static const struct file_operations smk_ambient_ops = {
1641 .read = smk_read_ambient,
1642 .write = smk_write_ambient,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001643 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001644};
1645
Casey Schaufler15446232008-07-30 15:37:11 -07001646/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08001647 * smk_read_onlycap - read() for smackfs/onlycap
Casey Schaufler15446232008-07-30 15:37:11 -07001648 * @filp: file pointer, not actually used
1649 * @buf: where to put the result
1650 * @cn: maximum to send along
1651 * @ppos: where to start
1652 *
1653 * Returns number of bytes read or error code, as appropriate
1654 */
1655static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1656 size_t cn, loff_t *ppos)
1657{
1658 char *smack = "";
1659 ssize_t rc = -EINVAL;
1660 int asize;
1661
1662 if (*ppos != 0)
1663 return 0;
1664
1665 if (smack_onlycap != NULL)
Casey Schaufler00f84f32013-12-23 11:07:10 -08001666 smack = smack_onlycap->smk_known;
Casey Schaufler15446232008-07-30 15:37:11 -07001667
1668 asize = strlen(smack) + 1;
1669
1670 if (cn >= asize)
1671 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1672
1673 return rc;
1674}
1675
1676/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08001677 * smk_write_onlycap - write() for smackfs/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001678 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001679 * @buf: where to get the data from
1680 * @count: bytes sent
1681 * @ppos: where to start
1682 *
1683 * Returns number of bytes written or error code, as appropriate
1684 */
1685static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1686 size_t count, loff_t *ppos)
1687{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001688 char *data;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001689 struct smack_known *skp = smk_of_task(current->cred->security);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001690 int rc = count;
Casey Schaufler15446232008-07-30 15:37:11 -07001691
Casey Schaufler1880eff2012-06-05 15:28:30 -07001692 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler15446232008-07-30 15:37:11 -07001693 return -EPERM;
1694
1695 /*
1696 * This can be done using smk_access() but is done
1697 * explicitly for clarity. The smk_access() implementation
1698 * would use smk_access(smack_onlycap, MAY_WRITE)
1699 */
Casey Schaufler00f84f32013-12-23 11:07:10 -08001700 if (smack_onlycap != NULL && smack_onlycap != skp)
Casey Schaufler15446232008-07-30 15:37:11 -07001701 return -EPERM;
1702
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001703 data = kzalloc(count + 1, GFP_KERNEL);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001704 if (data == NULL)
1705 return -ENOMEM;
Casey Schaufler15446232008-07-30 15:37:11 -07001706
1707 /*
1708 * Should the null string be passed in unset the onlycap value.
1709 * This seems like something to be careful with as usually
1710 * smk_import only expects to return NULL for errors. It
1711 * is usually the case that a nullstring or "\n" would be
1712 * bad to pass to smk_import but in fact this is useful here.
Casey Schauflerf7112e62012-05-06 15:22:02 -07001713 *
1714 * smk_import will also reject a label beginning with '-',
1715 * so "-usecapabilities" will also work.
Casey Schaufler15446232008-07-30 15:37:11 -07001716 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07001717 if (copy_from_user(data, buf, count) != 0)
1718 rc = -EFAULT;
1719 else
Casey Schaufler00f84f32013-12-23 11:07:10 -08001720 smack_onlycap = smk_import_entry(data, count);
Casey Schaufler15446232008-07-30 15:37:11 -07001721
Casey Schauflerf7112e62012-05-06 15:22:02 -07001722 kfree(data);
1723 return rc;
Casey Schaufler15446232008-07-30 15:37:11 -07001724}
1725
1726static const struct file_operations smk_onlycap_ops = {
1727 .read = smk_read_onlycap,
1728 .write = smk_write_onlycap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001729 .llseek = default_llseek,
Casey Schaufler15446232008-07-30 15:37:11 -07001730};
1731
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07001732#ifdef CONFIG_SECURITY_SMACK_BRINGUP
1733/**
1734 * smk_read_unconfined - read() for smackfs/unconfined
1735 * @filp: file pointer, not actually used
1736 * @buf: where to put the result
1737 * @cn: maximum to send along
1738 * @ppos: where to start
1739 *
1740 * Returns number of bytes read or error code, as appropriate
1741 */
1742static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
1743 size_t cn, loff_t *ppos)
1744{
1745 char *smack = "";
1746 ssize_t rc = -EINVAL;
1747 int asize;
1748
1749 if (*ppos != 0)
1750 return 0;
1751
1752 if (smack_unconfined != NULL)
1753 smack = smack_unconfined->smk_known;
1754
1755 asize = strlen(smack) + 1;
1756
1757 if (cn >= asize)
1758 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1759
1760 return rc;
1761}
1762
1763/**
1764 * smk_write_unconfined - write() for smackfs/unconfined
1765 * @file: file pointer, not actually used
1766 * @buf: where to get the data from
1767 * @count: bytes sent
1768 * @ppos: where to start
1769 *
1770 * Returns number of bytes written or error code, as appropriate
1771 */
1772static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
1773 size_t count, loff_t *ppos)
1774{
1775 char *data;
1776 int rc = count;
1777
1778 if (!smack_privileged(CAP_MAC_ADMIN))
1779 return -EPERM;
1780
1781 data = kzalloc(count + 1, GFP_KERNEL);
1782 if (data == NULL)
1783 return -ENOMEM;
1784
1785 /*
1786 * Should the null string be passed in unset the unconfined value.
1787 * This seems like something to be careful with as usually
1788 * smk_import only expects to return NULL for errors. It
1789 * is usually the case that a nullstring or "\n" would be
1790 * bad to pass to smk_import but in fact this is useful here.
1791 *
1792 * smk_import will also reject a label beginning with '-',
1793 * so "-confine" will also work.
1794 */
1795 if (copy_from_user(data, buf, count) != 0)
1796 rc = -EFAULT;
1797 else
1798 smack_unconfined = smk_import_entry(data, count);
1799
1800 kfree(data);
1801 return rc;
1802}
1803
1804static const struct file_operations smk_unconfined_ops = {
1805 .read = smk_read_unconfined,
1806 .write = smk_write_unconfined,
1807 .llseek = default_llseek,
1808};
1809#endif /* CONFIG_SECURITY_SMACK_BRINGUP */
1810
Casey Schauflere114e472008-02-04 22:29:50 -08001811/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001812 * smk_read_logging - read() for /smack/logging
1813 * @filp: file pointer, not actually used
1814 * @buf: where to put the result
1815 * @cn: maximum to send along
1816 * @ppos: where to start
1817 *
1818 * Returns number of bytes read or error code, as appropriate
1819 */
1820static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1821 size_t count, loff_t *ppos)
1822{
1823 char temp[32];
1824 ssize_t rc;
1825
1826 if (*ppos != 0)
1827 return 0;
1828
1829 sprintf(temp, "%d\n", log_policy);
1830 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1831 return rc;
1832}
1833
1834/**
1835 * smk_write_logging - write() for /smack/logging
1836 * @file: file pointer, not actually used
1837 * @buf: where to get the data from
1838 * @count: bytes sent
1839 * @ppos: where to start
1840 *
1841 * Returns number of bytes written or error code, as appropriate
1842 */
1843static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1844 size_t count, loff_t *ppos)
1845{
1846 char temp[32];
1847 int i;
1848
Casey Schaufler1880eff2012-06-05 15:28:30 -07001849 if (!smack_privileged(CAP_MAC_ADMIN))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001850 return -EPERM;
1851
1852 if (count >= sizeof(temp) || count == 0)
1853 return -EINVAL;
1854
1855 if (copy_from_user(temp, buf, count) != 0)
1856 return -EFAULT;
1857
1858 temp[count] = '\0';
1859
1860 if (sscanf(temp, "%d", &i) != 1)
1861 return -EINVAL;
1862 if (i < 0 || i > 3)
1863 return -EINVAL;
1864 log_policy = i;
1865 return count;
1866}
1867
1868
1869
1870static const struct file_operations smk_logging_ops = {
1871 .read = smk_read_logging,
1872 .write = smk_write_logging,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001873 .llseek = default_llseek,
Etienne Bassetecfcc532009-04-08 20:40:06 +02001874};
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001875
1876/*
1877 * Seq_file read operations for /smack/load-self
1878 */
1879
1880static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1881{
1882 struct task_smack *tsp = current_security();
1883
Casey Schaufler40809562011-11-10 15:02:22 -08001884 return smk_seq_start(s, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001885}
1886
1887static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1888{
1889 struct task_smack *tsp = current_security();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001890
Casey Schaufler40809562011-11-10 15:02:22 -08001891 return smk_seq_next(s, v, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001892}
1893
1894static int load_self_seq_show(struct seq_file *s, void *v)
1895{
1896 struct list_head *list = v;
1897 struct smack_rule *srp =
1898 list_entry(list, struct smack_rule, list);
1899
Casey Schauflerf7112e62012-05-06 15:22:02 -07001900 smk_rule_show(s, srp, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001901
1902 return 0;
1903}
1904
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001905static const struct seq_operations load_self_seq_ops = {
1906 .start = load_self_seq_start,
1907 .next = load_self_seq_next,
1908 .show = load_self_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001909 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001910};
1911
1912
1913/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001914 * smk_open_load_self - open() for /smack/load-self2
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001915 * @inode: inode structure representing file
1916 * @file: "load" file pointer
1917 *
1918 * For reading, use load_seq_* seq_file reading operations.
1919 */
1920static int smk_open_load_self(struct inode *inode, struct file *file)
1921{
1922 return seq_open(file, &load_self_seq_ops);
1923}
1924
1925/**
1926 * smk_write_load_self - write() for /smack/load-self
1927 * @file: file pointer, not actually used
1928 * @buf: where to get the data from
1929 * @count: bytes sent
1930 * @ppos: where to start - must be 0
1931 *
1932 */
1933static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1934 size_t count, loff_t *ppos)
1935{
1936 struct task_smack *tsp = current_security();
1937
Casey Schauflerf7112e62012-05-06 15:22:02 -07001938 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1939 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001940}
1941
1942static const struct file_operations smk_load_self_ops = {
1943 .open = smk_open_load_self,
1944 .read = seq_read,
1945 .llseek = seq_lseek,
1946 .write = smk_write_load_self,
1947 .release = seq_release,
1948};
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001949
1950/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001951 * smk_user_access - handle access check transaction
1952 * @file: file pointer
1953 * @buf: data from user space
1954 * @count: bytes sent
1955 * @ppos: where to start - must be 0
1956 */
1957static ssize_t smk_user_access(struct file *file, const char __user *buf,
1958 size_t count, loff_t *ppos, int format)
1959{
Rafal Krypae05b6f92013-01-10 19:42:00 +01001960 struct smack_parsed_rule rule;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001961 char *data;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001962 int res;
1963
1964 data = simple_transaction_get(file, buf, count);
1965 if (IS_ERR(data))
1966 return PTR_ERR(data);
1967
1968 if (format == SMK_FIXED24_FMT) {
1969 if (count < SMK_LOADLEN)
1970 return -EINVAL;
1971 res = smk_parse_rule(data, &rule, 0);
1972 } else {
1973 /*
Rafal Krypa10289b02013-08-09 11:47:07 +02001974 * simple_transaction_get() returns null-terminated data
Casey Schauflerf7112e62012-05-06 15:22:02 -07001975 */
Rafal Krypa10289b02013-08-09 11:47:07 +02001976 res = smk_parse_long_rule(data, &rule, 0, 3);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001977 }
1978
Jarkko Sakkinen398ce072013-11-28 19:16:46 +02001979 if (res >= 0)
1980 res = smk_access(rule.smk_subject, rule.smk_object,
1981 rule.smk_access1, NULL);
1982 else if (res != -ENOENT)
Casey Schauflerf7112e62012-05-06 15:22:02 -07001983 return -EINVAL;
1984
Casey Schauflerd166c802014-08-27 14:51:27 -07001985 /*
1986 * smk_access() can return a value > 0 in the "bringup" case.
1987 */
1988 data[0] = res >= 0 ? '1' : '0';
Casey Schauflerf7112e62012-05-06 15:22:02 -07001989 data[1] = '\0';
1990
1991 simple_transaction_set(file, 2);
1992
1993 if (format == SMK_FIXED24_FMT)
1994 return SMK_LOADLEN;
1995 return count;
1996}
1997
1998/**
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001999 * smk_write_access - handle access check transaction
2000 * @file: file pointer
2001 * @buf: data from user space
2002 * @count: bytes sent
2003 * @ppos: where to start - must be 0
2004 */
2005static ssize_t smk_write_access(struct file *file, const char __user *buf,
2006 size_t count, loff_t *ppos)
2007{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002008 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002009}
2010
2011static const struct file_operations smk_access_ops = {
2012 .write = smk_write_access,
2013 .read = simple_transaction_read,
2014 .release = simple_transaction_release,
2015 .llseek = generic_file_llseek,
2016};
2017
Casey Schauflerf7112e62012-05-06 15:22:02 -07002018
2019/*
2020 * Seq_file read operations for /smack/load2
2021 */
2022
2023static int load2_seq_show(struct seq_file *s, void *v)
2024{
2025 struct list_head *list = v;
2026 struct smack_master_list *smlp =
2027 list_entry(list, struct smack_master_list, list);
2028
2029 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
2030
2031 return 0;
2032}
2033
2034static const struct seq_operations load2_seq_ops = {
2035 .start = load2_seq_start,
2036 .next = load2_seq_next,
2037 .show = load2_seq_show,
2038 .stop = smk_seq_stop,
2039};
2040
2041/**
2042 * smk_open_load2 - open() for /smack/load2
2043 * @inode: inode structure representing file
2044 * @file: "load2" file pointer
2045 *
2046 * For reading, use load2_seq_* seq_file reading operations.
2047 */
2048static int smk_open_load2(struct inode *inode, struct file *file)
2049{
2050 return seq_open(file, &load2_seq_ops);
2051}
2052
2053/**
2054 * smk_write_load2 - write() for /smack/load2
2055 * @file: file pointer, not actually used
2056 * @buf: where to get the data from
2057 * @count: bytes sent
2058 * @ppos: where to start - must be 0
2059 *
2060 */
2061static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2062 size_t count, loff_t *ppos)
2063{
2064 /*
2065 * Must have privilege.
2066 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07002067 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07002068 return -EPERM;
2069
2070 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2071 SMK_LONG_FMT);
2072}
2073
2074static const struct file_operations smk_load2_ops = {
2075 .open = smk_open_load2,
2076 .read = seq_read,
2077 .llseek = seq_lseek,
2078 .write = smk_write_load2,
2079 .release = seq_release,
2080};
2081
2082/*
2083 * Seq_file read operations for /smack/load-self2
2084 */
2085
2086static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2087{
2088 struct task_smack *tsp = current_security();
2089
2090 return smk_seq_start(s, pos, &tsp->smk_rules);
2091}
2092
2093static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2094{
2095 struct task_smack *tsp = current_security();
2096
2097 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2098}
2099
2100static int load_self2_seq_show(struct seq_file *s, void *v)
2101{
2102 struct list_head *list = v;
2103 struct smack_rule *srp =
2104 list_entry(list, struct smack_rule, list);
2105
2106 smk_rule_show(s, srp, SMK_LONGLABEL);
2107
2108 return 0;
2109}
2110
2111static const struct seq_operations load_self2_seq_ops = {
2112 .start = load_self2_seq_start,
2113 .next = load_self2_seq_next,
2114 .show = load_self2_seq_show,
2115 .stop = smk_seq_stop,
2116};
2117
2118/**
2119 * smk_open_load_self2 - open() for /smack/load-self2
2120 * @inode: inode structure representing file
2121 * @file: "load" file pointer
2122 *
2123 * For reading, use load_seq_* seq_file reading operations.
2124 */
2125static int smk_open_load_self2(struct inode *inode, struct file *file)
2126{
2127 return seq_open(file, &load_self2_seq_ops);
2128}
2129
2130/**
2131 * smk_write_load_self2 - write() for /smack/load-self2
2132 * @file: file pointer, not actually used
2133 * @buf: where to get the data from
2134 * @count: bytes sent
2135 * @ppos: where to start - must be 0
2136 *
2137 */
2138static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2139 size_t count, loff_t *ppos)
2140{
2141 struct task_smack *tsp = current_security();
2142
2143 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2144 &tsp->smk_rules_lock, SMK_LONG_FMT);
2145}
2146
2147static const struct file_operations smk_load_self2_ops = {
2148 .open = smk_open_load_self2,
2149 .read = seq_read,
2150 .llseek = seq_lseek,
2151 .write = smk_write_load_self2,
2152 .release = seq_release,
2153};
2154
2155/**
2156 * smk_write_access2 - handle access check transaction
2157 * @file: file pointer
2158 * @buf: data from user space
2159 * @count: bytes sent
2160 * @ppos: where to start - must be 0
2161 */
2162static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2163 size_t count, loff_t *ppos)
2164{
2165 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2166}
2167
2168static const struct file_operations smk_access2_ops = {
2169 .write = smk_write_access2,
2170 .read = simple_transaction_read,
2171 .release = simple_transaction_release,
2172 .llseek = generic_file_llseek,
2173};
2174
Etienne Bassetecfcc532009-04-08 20:40:06 +02002175/**
Rafal Krypa449543b2012-07-11 17:49:30 +02002176 * smk_write_revoke_subj - write() for /smack/revoke-subject
2177 * @file: file pointer
2178 * @buf: data from user space
2179 * @count: bytes sent
2180 * @ppos: where to start - must be 0
2181 */
2182static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2183 size_t count, loff_t *ppos)
2184{
2185 char *data = NULL;
2186 const char *cp = NULL;
2187 struct smack_known *skp;
2188 struct smack_rule *sp;
2189 struct list_head *rule_list;
2190 struct mutex *rule_lock;
2191 int rc = count;
2192
2193 if (*ppos != 0)
2194 return -EINVAL;
2195
2196 if (!smack_privileged(CAP_MAC_ADMIN))
2197 return -EPERM;
2198
2199 if (count == 0 || count > SMK_LONGLABEL)
2200 return -EINVAL;
2201
2202 data = kzalloc(count, GFP_KERNEL);
2203 if (data == NULL)
2204 return -ENOMEM;
2205
2206 if (copy_from_user(data, buf, count) != 0) {
2207 rc = -EFAULT;
2208 goto free_out;
2209 }
2210
2211 cp = smk_parse_smack(data, count);
2212 if (cp == NULL) {
2213 rc = -EINVAL;
2214 goto free_out;
2215 }
2216
2217 skp = smk_find_entry(cp);
Rafal Krypad15d9fad2012-11-27 16:28:11 +01002218 if (skp == NULL)
Rafal Krypa449543b2012-07-11 17:49:30 +02002219 goto free_out;
Rafal Krypa449543b2012-07-11 17:49:30 +02002220
2221 rule_list = &skp->smk_rules;
2222 rule_lock = &skp->smk_rules_lock;
2223
2224 mutex_lock(rule_lock);
2225
2226 list_for_each_entry_rcu(sp, rule_list, list)
2227 sp->smk_access = 0;
2228
2229 mutex_unlock(rule_lock);
2230
2231free_out:
2232 kfree(data);
2233 kfree(cp);
2234 return rc;
2235}
2236
2237static const struct file_operations smk_revoke_subj_ops = {
2238 .write = smk_write_revoke_subj,
2239 .read = simple_transaction_read,
2240 .release = simple_transaction_release,
2241 .llseek = generic_file_llseek,
2242};
2243
Casey Schauflere9307232012-11-01 18:14:32 -07002244static struct kset *smackfs_kset;
2245/**
2246 * smk_init_sysfs - initialize /sys/fs/smackfs
2247 *
2248 */
2249static int smk_init_sysfs(void)
2250{
2251 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2252 if (!smackfs_kset)
2253 return -ENOMEM;
2254 return 0;
2255}
2256
Rafal Krypa449543b2012-07-11 17:49:30 +02002257/**
Rafal Krypae05b6f92013-01-10 19:42:00 +01002258 * smk_write_change_rule - write() for /smack/change-rule
2259 * @file: file pointer
2260 * @buf: data from user space
2261 * @count: bytes sent
2262 * @ppos: where to start - must be 0
2263 */
2264static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2265 size_t count, loff_t *ppos)
2266{
2267 /*
2268 * Must have privilege.
2269 */
Casey Schaufler4afde482013-12-19 13:23:26 -08002270 if (!smack_privileged(CAP_MAC_ADMIN))
Rafal Krypae05b6f92013-01-10 19:42:00 +01002271 return -EPERM;
2272
2273 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2274 SMK_CHANGE_FMT);
2275}
2276
2277static const struct file_operations smk_change_rule_ops = {
2278 .write = smk_write_change_rule,
2279 .read = simple_transaction_read,
2280 .release = simple_transaction_release,
2281 .llseek = generic_file_llseek,
2282};
2283
2284/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002285 * smk_read_syslog - read() for smackfs/syslog
2286 * @filp: file pointer, not actually used
2287 * @buf: where to put the result
2288 * @cn: maximum to send along
2289 * @ppos: where to start
2290 *
2291 * Returns number of bytes read or error code, as appropriate
2292 */
2293static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2294 size_t cn, loff_t *ppos)
2295{
2296 struct smack_known *skp;
2297 ssize_t rc = -EINVAL;
2298 int asize;
2299
2300 if (*ppos != 0)
2301 return 0;
2302
2303 if (smack_syslog_label == NULL)
2304 skp = &smack_known_star;
2305 else
2306 skp = smack_syslog_label;
2307
2308 asize = strlen(skp->smk_known) + 1;
2309
2310 if (cn >= asize)
2311 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2312 asize);
2313
2314 return rc;
2315}
2316
2317/**
2318 * smk_write_syslog - write() for smackfs/syslog
2319 * @file: file pointer, not actually used
2320 * @buf: where to get the data from
2321 * @count: bytes sent
2322 * @ppos: where to start
2323 *
2324 * Returns number of bytes written or error code, as appropriate
2325 */
2326static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2327 size_t count, loff_t *ppos)
2328{
2329 char *data;
2330 struct smack_known *skp;
2331 int rc = count;
2332
2333 if (!smack_privileged(CAP_MAC_ADMIN))
2334 return -EPERM;
2335
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04002336 data = kzalloc(count + 1, GFP_KERNEL);
Casey Schaufler00f84f32013-12-23 11:07:10 -08002337 if (data == NULL)
2338 return -ENOMEM;
2339
2340 if (copy_from_user(data, buf, count) != 0)
2341 rc = -EFAULT;
2342 else {
2343 skp = smk_import_entry(data, count);
2344 if (skp == NULL)
2345 rc = -EINVAL;
2346 else
2347 smack_syslog_label = smk_import_entry(data, count);
2348 }
2349
2350 kfree(data);
2351 return rc;
2352}
2353
2354static const struct file_operations smk_syslog_ops = {
2355 .read = smk_read_syslog,
2356 .write = smk_write_syslog,
2357 .llseek = default_llseek,
2358};
2359
2360
2361/**
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002362 * smk_read_ptrace - read() for /smack/ptrace
2363 * @filp: file pointer, not actually used
2364 * @buf: where to put the result
2365 * @count: maximum to send along
2366 * @ppos: where to start
2367 *
2368 * Returns number of bytes read or error code, as appropriate
2369 */
2370static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2371 size_t count, loff_t *ppos)
2372{
2373 char temp[32];
2374 ssize_t rc;
2375
2376 if (*ppos != 0)
2377 return 0;
2378
2379 sprintf(temp, "%d\n", smack_ptrace_rule);
2380 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2381 return rc;
2382}
2383
2384/**
2385 * smk_write_ptrace - write() for /smack/ptrace
2386 * @file: file pointer
2387 * @buf: data from user space
2388 * @count: bytes sent
2389 * @ppos: where to start - must be 0
2390 */
2391static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2392 size_t count, loff_t *ppos)
2393{
2394 char temp[32];
2395 int i;
2396
2397 if (!smack_privileged(CAP_MAC_ADMIN))
2398 return -EPERM;
2399
2400 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2401 return -EINVAL;
2402
2403 if (copy_from_user(temp, buf, count) != 0)
2404 return -EFAULT;
2405
2406 temp[count] = '\0';
2407
2408 if (sscanf(temp, "%d", &i) != 1)
2409 return -EINVAL;
2410 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2411 return -EINVAL;
2412 smack_ptrace_rule = i;
2413
2414 return count;
2415}
2416
2417static const struct file_operations smk_ptrace_ops = {
2418 .write = smk_write_ptrace,
2419 .read = smk_read_ptrace,
2420 .llseek = default_llseek,
2421};
2422
2423/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002424 * smk_fill_super - fill the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002425 * @sb: the empty superblock
2426 * @data: unused
2427 * @silent: unused
2428 *
Casey Schaufler00f84f32013-12-23 11:07:10 -08002429 * Fill in the well known entries for the smack filesystem
Casey Schauflere114e472008-02-04 22:29:50 -08002430 *
2431 * Returns 0 on success, an error code on failure
2432 */
2433static int smk_fill_super(struct super_block *sb, void *data, int silent)
2434{
2435 int rc;
2436 struct inode *root_inode;
2437
2438 static struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002439 [SMK_LOAD] = {
2440 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2441 [SMK_CIPSO] = {
2442 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2443 [SMK_DOI] = {
2444 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2445 [SMK_DIRECT] = {
2446 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2447 [SMK_AMBIENT] = {
2448 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2449 [SMK_NETLBLADDR] = {
2450 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2451 [SMK_ONLYCAP] = {
2452 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2453 [SMK_LOGGING] = {
2454 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2455 [SMK_LOAD_SELF] = {
2456 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002457 [SMK_ACCESSES] = {
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03002458 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
Casey Schauflerf7112e62012-05-06 15:22:02 -07002459 [SMK_MAPPED] = {
2460 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2461 [SMK_LOAD2] = {
2462 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2463 [SMK_LOAD_SELF2] = {
2464 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2465 [SMK_ACCESS2] = {
2466 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2467 [SMK_CIPSO2] = {
2468 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
Rafal Krypa449543b2012-07-11 17:49:30 +02002469 [SMK_REVOKE_SUBJ] = {
2470 "revoke-subject", &smk_revoke_subj_ops,
2471 S_IRUGO|S_IWUSR},
Rafal Krypae05b6f92013-01-10 19:42:00 +01002472 [SMK_CHANGE_RULE] = {
2473 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
Casey Schaufler00f84f32013-12-23 11:07:10 -08002474 [SMK_SYSLOG] = {
2475 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002476 [SMK_PTRACE] = {
2477 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002478#ifdef CONFIG_SECURITY_SMACK_BRINGUP
2479 [SMK_UNCONFINED] = {
2480 "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2481#endif
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002482 /* last one */
2483 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08002484 };
2485
2486 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2487 if (rc != 0) {
2488 printk(KERN_ERR "%s failed %d while creating inodes\n",
2489 __func__, rc);
2490 return rc;
2491 }
2492
David Howellsce0b16d2015-02-19 10:47:02 +00002493 root_inode = d_inode(sb->s_root);
Casey Schauflere114e472008-02-04 22:29:50 -08002494
2495 return 0;
2496}
2497
2498/**
Al Virofc14f2f2010-07-25 01:48:30 +04002499 * smk_mount - get the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002500 * @fs_type: passed along without comment
2501 * @flags: passed along without comment
2502 * @dev_name: passed along without comment
2503 * @data: passed along without comment
Casey Schauflere114e472008-02-04 22:29:50 -08002504 *
2505 * Just passes everything along.
2506 *
2507 * Returns what the lower level code does.
2508 */
Al Virofc14f2f2010-07-25 01:48:30 +04002509static struct dentry *smk_mount(struct file_system_type *fs_type,
2510 int flags, const char *dev_name, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -08002511{
Al Virofc14f2f2010-07-25 01:48:30 +04002512 return mount_single(fs_type, flags, data, smk_fill_super);
Casey Schauflere114e472008-02-04 22:29:50 -08002513}
2514
2515static struct file_system_type smk_fs_type = {
2516 .name = "smackfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002517 .mount = smk_mount,
Casey Schauflere114e472008-02-04 22:29:50 -08002518 .kill_sb = kill_litter_super,
2519};
2520
2521static struct vfsmount *smackfs_mount;
2522
Casey Schauflerf7112e62012-05-06 15:22:02 -07002523static int __init smk_preset_netlabel(struct smack_known *skp)
2524{
2525 skp->smk_netlabel.domain = skp->smk_known;
2526 skp->smk_netlabel.flags =
2527 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2528 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2529 &skp->smk_netlabel, strlen(skp->smk_known));
2530}
2531
Casey Schauflere114e472008-02-04 22:29:50 -08002532/**
2533 * init_smk_fs - get the smackfs superblock
2534 *
2535 * register the smackfs
2536 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002537 * Do not register smackfs if Smack wasn't enabled
2538 * on boot. We can not put this method normally under the
2539 * smack_init() code path since the security subsystem get
2540 * initialized before the vfs caches.
2541 *
2542 * Returns true if we were not chosen on boot or if
2543 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08002544 */
2545static int __init init_smk_fs(void)
2546{
2547 int err;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002548 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002549
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002550 if (!security_module_enable("smack"))
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002551 return 0;
2552
Casey Schauflere9307232012-11-01 18:14:32 -07002553 err = smk_init_sysfs();
2554 if (err)
2555 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2556
Casey Schauflere114e472008-02-04 22:29:50 -08002557 err = register_filesystem(&smk_fs_type);
2558 if (!err) {
2559 smackfs_mount = kern_mount(&smk_fs_type);
2560 if (IS_ERR(smackfs_mount)) {
2561 printk(KERN_ERR "smackfs: could not mount!\n");
2562 err = PTR_ERR(smackfs_mount);
2563 smackfs_mount = NULL;
2564 }
2565 }
2566
Casey Schauflere114e472008-02-04 22:29:50 -08002567 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002568 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08002569
Casey Schauflerf7112e62012-05-06 15:22:02 -07002570 rc = smk_preset_netlabel(&smack_known_floor);
2571 if (err == 0 && rc < 0)
2572 err = rc;
2573 rc = smk_preset_netlabel(&smack_known_hat);
2574 if (err == 0 && rc < 0)
2575 err = rc;
2576 rc = smk_preset_netlabel(&smack_known_huh);
2577 if (err == 0 && rc < 0)
2578 err = rc;
2579 rc = smk_preset_netlabel(&smack_known_invalid);
2580 if (err == 0 && rc < 0)
2581 err = rc;
2582 rc = smk_preset_netlabel(&smack_known_star);
2583 if (err == 0 && rc < 0)
2584 err = rc;
2585 rc = smk_preset_netlabel(&smack_known_web);
2586 if (err == 0 && rc < 0)
2587 err = rc;
2588
Casey Schauflere114e472008-02-04 22:29:50 -08002589 return err;
2590}
2591
2592__initcall(init_smk_fs);