blob: e40dc48737ffa9d6fbae49a30a255bde5c894a99 [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 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200341 * Returns 0 on success, appropriate error code on failure.
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300342 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700343static int smk_fill_rule(const char *subject, const char *object,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100344 const char *access1, const char *access2,
345 struct smack_parsed_rule *rule, int import,
346 int len)
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300347{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700348 const char *cp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300349 struct smack_known *skp;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300350
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300351 if (import) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700352 rule->smk_subject = smk_import_entry(subject, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200353 if (IS_ERR(rule->smk_subject))
354 return PTR_ERR(rule->smk_subject);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300355
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200356 rule->smk_object = smk_import_entry(object, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200357 if (IS_ERR(rule->smk_object))
358 return PTR_ERR(rule->smk_object);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300359 } else {
Casey Schaufler35187212012-06-18 19:01:36 -0700360 cp = smk_parse_smack(subject, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200361 if (IS_ERR(cp))
362 return PTR_ERR(cp);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700363 skp = smk_find_entry(cp);
364 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300365 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200366 return -ENOENT;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700367 rule->smk_subject = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300368
Casey Schaufler35187212012-06-18 19:01:36 -0700369 cp = smk_parse_smack(object, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200370 if (IS_ERR(cp))
371 return PTR_ERR(cp);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700372 skp = smk_find_entry(cp);
373 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300374 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200375 return -ENOENT;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200376 rule->smk_object = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300377 }
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300378
Rafal Krypae05b6f92013-01-10 19:42:00 +0100379 rule->smk_access1 = smk_perm_from_str(access1);
380 if (access2)
381 rule->smk_access2 = smk_perm_from_str(access2);
382 else
383 rule->smk_access2 = ~rule->smk_access1;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300384
Casey Schaufler35187212012-06-18 19:01:36 -0700385 return 0;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300386}
387
388/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700389 * smk_parse_rule - parse Smack rule from load string
390 * @data: string to be parsed whose size is SMK_LOADLEN
391 * @rule: Smack rule
392 * @import: if non-zero, import labels
393 *
394 * Returns 0 on success, -1 on errors.
395 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100396static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
397 int import)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700398{
399 int rc;
400
401 rc = smk_fill_rule(data, data + SMK_LABELLEN,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100402 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
403 import, SMK_LABELLEN);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700404 return rc;
405}
406
407/**
408 * smk_parse_long_rule - parse Smack rule from rule string
409 * @data: string to be parsed, null terminated
Rafal Krypae05b6f92013-01-10 19:42:00 +0100410 * @rule: Will be filled with Smack parsed rule
Casey Schauflerf7112e62012-05-06 15:22:02 -0700411 * @import: if non-zero, import labels
Rafal Krypa10289b02013-08-09 11:47:07 +0200412 * @tokens: numer of substrings expected in data
Casey Schauflerf7112e62012-05-06 15:22:02 -0700413 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200414 * Returns number of processed bytes on success, -ERRNO on failure.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700415 */
Rafal Krypa10289b02013-08-09 11:47:07 +0200416static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
417 int import, int tokens)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700418{
Rafal Krypa10289b02013-08-09 11:47:07 +0200419 ssize_t cnt = 0;
420 char *tok[4];
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200421 int rc;
Rafal Krypa10289b02013-08-09 11:47:07 +0200422 int i;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700423
Rafal Krypa10289b02013-08-09 11:47:07 +0200424 /*
425 * Parsing the rule in-place, filling all white-spaces with '\0'
426 */
427 for (i = 0; i < tokens; ++i) {
428 while (isspace(data[cnt]))
429 data[cnt++] = '\0';
Alan Cox3b9fc372012-07-26 14:47:11 -0700430
Rafal Krypa10289b02013-08-09 11:47:07 +0200431 if (data[cnt] == '\0')
432 /* Unexpected end of data */
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200433 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700434
Rafal Krypa10289b02013-08-09 11:47:07 +0200435 tok[i] = data + cnt;
436
437 while (data[cnt] && !isspace(data[cnt]))
438 ++cnt;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100439 }
Rafal Krypa10289b02013-08-09 11:47:07 +0200440 while (isspace(data[cnt]))
441 data[cnt++] = '\0';
Casey Schauflerf7112e62012-05-06 15:22:02 -0700442
Rafal Krypa10289b02013-08-09 11:47:07 +0200443 while (i < 4)
444 tok[i++] = NULL;
445
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200446 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
447 return rc == 0 ? cnt : rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700448}
449
450#define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
451#define SMK_LONG_FMT 1 /* Variable long label format */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100452#define SMK_CHANGE_FMT 2 /* Rule modification format */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700453/**
454 * smk_write_rules_list - write() for any /smack rule file
Randy Dunlap251a2a92009-02-18 11:42:33 -0800455 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800456 * @buf: where to get the data from
457 * @count: bytes sent
458 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800459 * @rule_list: the list of rules to write to
460 * @rule_lock: lock for the rule list
Rafal Krypae05b6f92013-01-10 19:42:00 +0100461 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
Casey Schauflere114e472008-02-04 22:29:50 -0800462 *
463 * Get one smack access rule from above.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700464 * The format for SMK_LONG_FMT is:
465 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
466 * The format for SMK_FIXED24_FMT is exactly:
467 * "subject object rwxat"
Rafal Krypae05b6f92013-01-10 19:42:00 +0100468 * The format for SMK_CHANGE_FMT is:
469 * "subject<whitespace>object<whitespace>
470 * acc_enable<whitespace>acc_disable[<whitespace>...]"
Casey Schauflere114e472008-02-04 22:29:50 -0800471 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700472static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
473 size_t count, loff_t *ppos,
474 struct list_head *rule_list,
475 struct mutex *rule_lock, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800476{
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200477 struct smack_parsed_rule rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800478 char *data;
Rafal Krypa10289b02013-08-09 11:47:07 +0200479 int rc;
480 int trunc = 0;
481 int tokens;
482 ssize_t cnt = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800483
484 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800485 * No partial writes.
486 * Enough data must be present.
487 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200488 if (*ppos != 0)
489 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -0800490
Casey Schauflerf7112e62012-05-06 15:22:02 -0700491 if (format == SMK_FIXED24_FMT) {
492 /*
493 * Minor hack for backward compatibility
494 */
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700495 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700496 return -EINVAL;
Rafal Krypa10289b02013-08-09 11:47:07 +0200497 } else {
498 if (count >= PAGE_SIZE) {
499 count = PAGE_SIZE - 1;
500 trunc = 1;
501 }
502 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700503
Rafal Krypa10289b02013-08-09 11:47:07 +0200504 data = kmalloc(count + 1, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -0800505 if (data == NULL)
506 return -ENOMEM;
507
508 if (copy_from_user(data, buf, count) != 0) {
509 rc = -EFAULT;
510 goto out;
511 }
512
Rafal Krypa10289b02013-08-09 11:47:07 +0200513 /*
514 * In case of parsing only part of user buf,
515 * avoid having partial rule at the data buffer
516 */
517 if (trunc) {
518 while (count > 0 && (data[count - 1] != '\n'))
519 --count;
520 if (count == 0) {
521 rc = -EINVAL;
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200522 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200523 }
524 }
525
526 data[count] = '\0';
527 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
528 while (cnt < count) {
529 if (format == SMK_FIXED24_FMT) {
530 rc = smk_parse_rule(data, &rule, 1);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200531 if (rc < 0)
Rafal Krypa10289b02013-08-09 11:47:07 +0200532 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200533 cnt = count;
534 } else {
535 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200536 if (rc < 0)
537 goto out;
538 if (rc == 0) {
Rafal Krypa10289b02013-08-09 11:47:07 +0200539 rc = -EINVAL;
540 goto out;
541 }
542 cnt += rc;
543 }
544
545 if (rule_list == NULL)
546 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
547 &rule.smk_subject->smk_rules_lock, 1);
548 else
549 rc = smk_set_access(&rule, rule_list, rule_lock, 0);
550
551 if (rc)
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200552 goto out;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700553 }
554
Rafal Krypa10289b02013-08-09 11:47:07 +0200555 rc = cnt;
Casey Schauflere114e472008-02-04 22:29:50 -0800556out:
557 kfree(data);
558 return rc;
559}
560
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800561/*
Casey Schaufler40809562011-11-10 15:02:22 -0800562 * Core logic for smackfs seq list operations.
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800563 */
564
Casey Schaufler40809562011-11-10 15:02:22 -0800565static void *smk_seq_start(struct seq_file *s, loff_t *pos,
566 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800567{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700568 struct list_head *list;
Rafal Krypa01fa8472015-05-21 18:24:31 +0200569 int i = *pos;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700570
Rafal Krypa01fa8472015-05-21 18:24:31 +0200571 rcu_read_lock();
572 for (list = rcu_dereference(list_next_rcu(head));
573 list != head;
574 list = rcu_dereference(list_next_rcu(list))) {
575 if (i-- == 0)
576 return list;
577 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700578
Rafal Krypa01fa8472015-05-21 18:24:31 +0200579 return NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800580}
581
Casey Schaufler40809562011-11-10 15:02:22 -0800582static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
583 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800584{
585 struct list_head *list = v;
586
Rafal Krypa01fa8472015-05-21 18:24:31 +0200587 ++*pos;
588 list = rcu_dereference(list_next_rcu(list));
589
590 return (list == head) ? NULL : list;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800591}
592
Casey Schaufler40809562011-11-10 15:02:22 -0800593static void smk_seq_stop(struct seq_file *s, void *v)
594{
Rafal Krypa01fa8472015-05-21 18:24:31 +0200595 rcu_read_unlock();
Casey Schaufler40809562011-11-10 15:02:22 -0800596}
597
Casey Schauflerf7112e62012-05-06 15:22:02 -0700598static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
Casey Schaufler40809562011-11-10 15:02:22 -0800599{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700600 /*
601 * Don't show any rules with label names too long for
602 * interface file (/smack/load or /smack/load2)
603 * because you should expect to be able to write
604 * anything you read back.
605 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700606 if (strlen(srp->smk_subject->smk_known) >= max ||
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200607 strlen(srp->smk_object->smk_known) >= max)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700608 return;
Casey Schaufler40809562011-11-10 15:02:22 -0800609
Rafal Krypa65ee7f42012-07-09 19:36:34 +0200610 if (srp->smk_access == 0)
611 return;
612
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200613 seq_printf(s, "%s %s",
614 srp->smk_subject->smk_known,
615 srp->smk_object->smk_known);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800616
617 seq_putc(s, ' ');
618
619 if (srp->smk_access & MAY_READ)
620 seq_putc(s, 'r');
621 if (srp->smk_access & MAY_WRITE)
622 seq_putc(s, 'w');
623 if (srp->smk_access & MAY_EXEC)
624 seq_putc(s, 'x');
625 if (srp->smk_access & MAY_APPEND)
626 seq_putc(s, 'a');
627 if (srp->smk_access & MAY_TRANSMUTE)
628 seq_putc(s, 't');
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700629 if (srp->smk_access & MAY_LOCK)
630 seq_putc(s, 'l');
Casey Schauflerd166c802014-08-27 14:51:27 -0700631 if (srp->smk_access & MAY_BRINGUP)
632 seq_putc(s, 'b');
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800633
634 seq_putc(s, '\n');
Casey Schauflerf7112e62012-05-06 15:22:02 -0700635}
636
637/*
638 * Seq_file read operations for /smack/load
639 */
640
641static void *load2_seq_start(struct seq_file *s, loff_t *pos)
642{
643 return smk_seq_start(s, pos, &smack_rule_list);
644}
645
646static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
647{
648 return smk_seq_next(s, v, pos, &smack_rule_list);
649}
650
651static int load_seq_show(struct seq_file *s, void *v)
652{
653 struct list_head *list = v;
654 struct smack_master_list *smlp =
Rafal Krypa01fa8472015-05-21 18:24:31 +0200655 list_entry_rcu(list, struct smack_master_list, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700656
657 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800658
659 return 0;
660}
661
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800662static const struct seq_operations load_seq_ops = {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700663 .start = load2_seq_start,
664 .next = load2_seq_next,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800665 .show = load_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800666 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800667};
668
669/**
670 * smk_open_load - open() for /smack/load
671 * @inode: inode structure representing file
672 * @file: "load" file pointer
673 *
674 * For reading, use load_seq_* seq_file reading operations.
675 */
676static int smk_open_load(struct inode *inode, struct file *file)
677{
678 return seq_open(file, &load_seq_ops);
679}
680
681/**
682 * smk_write_load - write() for /smack/load
683 * @file: file pointer, not actually used
684 * @buf: where to get the data from
685 * @count: bytes sent
686 * @ppos: where to start - must be 0
687 *
688 */
689static ssize_t smk_write_load(struct file *file, const char __user *buf,
690 size_t count, loff_t *ppos)
691{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800692 /*
693 * Must have privilege.
694 * No partial writes.
695 * Enough data must be present.
696 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700697 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800698 return -EPERM;
699
Casey Schauflerf7112e62012-05-06 15:22:02 -0700700 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
701 SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800702}
703
Casey Schauflere114e472008-02-04 22:29:50 -0800704static const struct file_operations smk_load_ops = {
705 .open = smk_open_load,
706 .read = seq_read,
707 .llseek = seq_lseek,
708 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700709 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800710};
711
712/**
713 * smk_cipso_doi - initialize the CIPSO domain
714 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700715static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800716{
717 int rc;
718 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500719 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800720
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500721 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800722
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500723 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800724 if (rc != 0)
725 printk(KERN_WARNING "%s:%d remove rc = %d\n",
726 __func__, __LINE__, rc);
727
728 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
729 if (doip == NULL)
730 panic("smack: Failed to initialize cipso DOI.\n");
731 doip->map.std = NULL;
732 doip->doi = smk_cipso_doi_value;
733 doip->type = CIPSO_V4_MAP_PASS;
734 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
735 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
736 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
737
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500738 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400739 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500740 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800741 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400742 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500743 return;
744 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500745 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500746 if (rc != 0) {
747 printk(KERN_WARNING "%s:%d map add rc = %d\n",
748 __func__, __LINE__, rc);
749 kfree(doip);
750 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400751 }
Casey Schauflere114e472008-02-04 22:29:50 -0800752}
753
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800754/**
755 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800756 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800757 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700758static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800759{
760 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500761 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800762
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500763 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800764
765 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500766 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800767 if (rc != 0)
768 printk(KERN_WARNING "%s:%d remove rc = %d\n",
769 __func__, __LINE__, rc);
770 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700771 if (smack_net_ambient == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700772 smack_net_ambient = &smack_known_floor;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800773
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700774 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500775 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800776 if (rc != 0)
777 printk(KERN_WARNING "%s:%d add rc = %d\n",
778 __func__, __LINE__, rc);
779}
780
Casey Schauflere114e472008-02-04 22:29:50 -0800781/*
782 * Seq_file read operations for /smack/cipso
783 */
784
785static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
786{
Casey Schaufler40809562011-11-10 15:02:22 -0800787 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800788}
789
790static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
791{
Casey Schaufler40809562011-11-10 15:02:22 -0800792 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800793}
794
795/*
796 * Print cipso labels in format:
797 * label level[/cat[,cat]]
798 */
799static int cipso_seq_show(struct seq_file *s, void *v)
800{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100801 struct list_head *list = v;
802 struct smack_known *skp =
Rafal Krypa01fa8472015-05-21 18:24:31 +0200803 list_entry_rcu(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400804 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800805 char sep = '/';
Casey Schauflere114e472008-02-04 22:29:50 -0800806 int i;
Casey Schauflere114e472008-02-04 22:29:50 -0800807
Casey Schauflerf7112e62012-05-06 15:22:02 -0700808 /*
809 * Don't show a label that could not have been set using
810 * /smack/cipso. This is in support of the notion that
811 * anything read from /smack/cipso ought to be writeable
812 * to /smack/cipso.
813 *
814 * /smack/cipso2 should be used instead.
815 */
816 if (strlen(skp->smk_known) >= SMK_LABELLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800817 return 0;
818
Casey Schauflerf7112e62012-05-06 15:22:02 -0700819 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
Casey Schauflere114e472008-02-04 22:29:50 -0800820
Paul Moore4fbe63d2014-08-01 11:17:37 -0400821 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
822 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700823 seq_printf(s, "%c%d", sep, i);
824 sep = ',';
825 }
Casey Schauflere114e472008-02-04 22:29:50 -0800826
827 seq_putc(s, '\n');
828
829 return 0;
830}
831
James Morris88e9d342009-09-22 16:43:43 -0700832static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800833 .start = cipso_seq_start,
Casey Schauflere114e472008-02-04 22:29:50 -0800834 .next = cipso_seq_next,
835 .show = cipso_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800836 .stop = smk_seq_stop,
Casey Schauflere114e472008-02-04 22:29:50 -0800837};
838
839/**
840 * smk_open_cipso - open() for /smack/cipso
841 * @inode: inode structure representing file
842 * @file: "cipso" file pointer
843 *
844 * Connect our cipso_seq_* operations with /smack/cipso
845 * file_operations
846 */
847static int smk_open_cipso(struct inode *inode, struct file *file)
848{
849 return seq_open(file, &cipso_seq_ops);
850}
851
852/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700853 * smk_set_cipso - do the work for write() for cipso and cipso2
Randy Dunlap251a2a92009-02-18 11:42:33 -0800854 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800855 * @buf: where to get the data from
856 * @count: bytes sent
857 * @ppos: where to start
Casey Schauflerf7112e62012-05-06 15:22:02 -0700858 * @format: /smack/cipso or /smack/cipso2
Casey Schauflere114e472008-02-04 22:29:50 -0800859 *
860 * Accepts only one cipso rule per write call.
861 * Returns number of bytes written or error code, as appropriate
862 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700863static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
864 size_t count, loff_t *ppos, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800865{
866 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700867 struct netlbl_lsm_secattr ncats;
868 char mapcatset[SMK_CIPSOLEN];
Casey Schauflere114e472008-02-04 22:29:50 -0800869 int maplevel;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700870 unsigned int cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800871 int catlen;
872 ssize_t rc = -EINVAL;
873 char *data = NULL;
874 char *rule;
875 int ret;
876 int i;
877
878 /*
879 * Must have privilege.
880 * No partial writes.
881 * Enough data must be present.
882 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700883 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -0800884 return -EPERM;
885 if (*ppos != 0)
886 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700887 if (format == SMK_FIXED24_FMT &&
888 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
Casey Schauflere114e472008-02-04 22:29:50 -0800889 return -EINVAL;
890
891 data = kzalloc(count + 1, GFP_KERNEL);
892 if (data == NULL)
893 return -ENOMEM;
894
895 if (copy_from_user(data, buf, count) != 0) {
896 rc = -EFAULT;
897 goto unlockedout;
898 }
899
900 data[count] = '\0';
901 rule = data;
902 /*
903 * Only allow one writer at a time. Writes should be
904 * quite rare and small in any case.
905 */
906 mutex_lock(&smack_cipso_lock);
907
908 skp = smk_import_entry(rule, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200909 if (IS_ERR(skp)) {
910 rc = PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800911 goto out;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200912 }
Casey Schauflere114e472008-02-04 22:29:50 -0800913
Casey Schauflerf7112e62012-05-06 15:22:02 -0700914 if (format == SMK_FIXED24_FMT)
915 rule += SMK_LABELLEN;
916 else
Passion,Zhao0fcfee62013-06-03 11:42:24 +0800917 rule += strlen(skp->smk_known) + 1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700918
Casey Schauflere114e472008-02-04 22:29:50 -0800919 ret = sscanf(rule, "%d", &maplevel);
920 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
921 goto out;
922
923 rule += SMK_DIGITLEN;
924 ret = sscanf(rule, "%d", &catlen);
925 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
926 goto out;
927
Casey Schauflerf7112e62012-05-06 15:22:02 -0700928 if (format == SMK_FIXED24_FMT &&
929 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800930 goto out;
931
932 memset(mapcatset, 0, sizeof(mapcatset));
933
934 for (i = 0; i < catlen; i++) {
935 rule += SMK_DIGITLEN;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700936 ret = sscanf(rule, "%u", &cat);
Casey Schaufler677264e2013-06-28 13:47:07 -0700937 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
Casey Schauflere114e472008-02-04 22:29:50 -0800938 goto out;
939
940 smack_catset_bit(cat, mapcatset);
941 }
942
Casey Schauflerf7112e62012-05-06 15:22:02 -0700943 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
944 if (rc >= 0) {
Paul Moore4fbe63d2014-08-01 11:17:37 -0400945 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700946 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
947 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
948 rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -0800949 }
950
Casey Schauflere114e472008-02-04 22:29:50 -0800951out:
952 mutex_unlock(&smack_cipso_lock);
953unlockedout:
954 kfree(data);
955 return rc;
956}
957
Casey Schauflerf7112e62012-05-06 15:22:02 -0700958/**
959 * smk_write_cipso - write() for /smack/cipso
960 * @file: file pointer, not actually used
961 * @buf: where to get the data from
962 * @count: bytes sent
963 * @ppos: where to start
964 *
965 * Accepts only one cipso rule per write call.
966 * Returns number of bytes written or error code, as appropriate
967 */
968static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
969 size_t count, loff_t *ppos)
970{
971 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
972}
973
Casey Schauflere114e472008-02-04 22:29:50 -0800974static const struct file_operations smk_cipso_ops = {
975 .open = smk_open_cipso,
976 .read = seq_read,
977 .llseek = seq_lseek,
978 .write = smk_write_cipso,
979 .release = seq_release,
980};
981
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500982/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700983 * Seq_file read operations for /smack/cipso2
984 */
985
986/*
987 * Print cipso labels in format:
988 * label level[/cat[,cat]]
989 */
990static int cipso2_seq_show(struct seq_file *s, void *v)
991{
992 struct list_head *list = v;
993 struct smack_known *skp =
Rafal Krypa01fa8472015-05-21 18:24:31 +0200994 list_entry_rcu(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400995 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700996 char sep = '/';
997 int i;
998
999 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
1000
Paul Moore4fbe63d2014-08-01 11:17:37 -04001001 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
1002 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001003 seq_printf(s, "%c%d", sep, i);
1004 sep = ',';
1005 }
1006
1007 seq_putc(s, '\n');
1008
1009 return 0;
1010}
1011
1012static const struct seq_operations cipso2_seq_ops = {
1013 .start = cipso_seq_start,
1014 .next = cipso_seq_next,
1015 .show = cipso2_seq_show,
1016 .stop = smk_seq_stop,
1017};
1018
1019/**
1020 * smk_open_cipso2 - open() for /smack/cipso2
1021 * @inode: inode structure representing file
1022 * @file: "cipso2" file pointer
1023 *
1024 * Connect our cipso_seq_* operations with /smack/cipso2
1025 * file_operations
1026 */
1027static int smk_open_cipso2(struct inode *inode, struct file *file)
1028{
1029 return seq_open(file, &cipso2_seq_ops);
1030}
1031
1032/**
1033 * smk_write_cipso2 - write() for /smack/cipso2
1034 * @file: file pointer, not actually used
1035 * @buf: where to get the data from
1036 * @count: bytes sent
1037 * @ppos: where to start
1038 *
1039 * Accepts only one cipso rule per write call.
1040 * Returns number of bytes written or error code, as appropriate
1041 */
1042static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1043 size_t count, loff_t *ppos)
1044{
1045 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1046}
1047
1048static const struct file_operations smk_cipso2_ops = {
1049 .open = smk_open_cipso2,
1050 .read = seq_read,
1051 .llseek = seq_lseek,
1052 .write = smk_write_cipso2,
1053 .release = seq_release,
1054};
1055
1056/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001057 * Seq_file read operations for /smack/netlabel
1058 */
1059
1060static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1061{
Casey Schaufler40809562011-11-10 15:02:22 -08001062 return smk_seq_start(s, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001063}
1064
1065static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1066{
Casey Schaufler40809562011-11-10 15:02:22 -08001067 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001068}
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001069#define BEBITS (sizeof(__be32) * 8)
1070
1071/*
1072 * Print host/label pairs
1073 */
1074static int netlbladdr_seq_show(struct seq_file *s, void *v)
1075{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001076 struct list_head *list = v;
1077 struct smk_netlbladdr *skp =
Rafal Krypa01fa8472015-05-21 18:24:31 +02001078 list_entry_rcu(list, struct smk_netlbladdr, list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001079 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001080 int maskn;
1081 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001082
etienne113a0e42009-03-04 07:33:51 +01001083 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001084
1085 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001086 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label->smk_known);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001087
1088 return 0;
1089}
1090
James Morris88e9d342009-09-22 16:43:43 -07001091static const struct seq_operations netlbladdr_seq_ops = {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001092 .start = netlbladdr_seq_start,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001093 .next = netlbladdr_seq_next,
1094 .show = netlbladdr_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001095 .stop = smk_seq_stop,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001096};
1097
1098/**
1099 * smk_open_netlbladdr - open() for /smack/netlabel
1100 * @inode: inode structure representing file
1101 * @file: "netlabel" file pointer
1102 *
1103 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1104 * file_operations
1105 */
1106static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1107{
1108 return seq_open(file, &netlbladdr_seq_ops);
1109}
1110
1111/**
etienne113a0e42009-03-04 07:33:51 +01001112 * smk_netlbladdr_insert
1113 * @new : netlabel to insert
1114 *
1115 * This helper insert netlabel in the smack_netlbladdrs list
1116 * sorted by netmask length (longest to smallest)
Etienne Basset7198e2e2009-03-24 20:53:24 +01001117 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1118 *
etienne113a0e42009-03-04 07:33:51 +01001119 */
1120static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1121{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001122 struct smk_netlbladdr *m, *m_next;
etienne113a0e42009-03-04 07:33:51 +01001123
Etienne Basset7198e2e2009-03-24 20:53:24 +01001124 if (list_empty(&smk_netlbladdr_list)) {
1125 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001126 return;
1127 }
1128
Jiri Pirko05725f72009-04-14 20:17:16 +02001129 m = list_entry_rcu(smk_netlbladdr_list.next,
1130 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001131
etienne113a0e42009-03-04 07:33:51 +01001132 /* the comparison '>' is a bit hacky, but works */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001133 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1134 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001135 return;
1136 }
Etienne Basset7198e2e2009-03-24 20:53:24 +01001137
1138 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1139 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1140 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001141 return;
1142 }
Jiri Pirko05725f72009-04-14 20:17:16 +02001143 m_next = list_entry_rcu(m->list.next,
1144 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001145 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1146 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001147 return;
1148 }
1149 }
1150}
1151
1152
1153/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001154 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -08001155 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001156 * @buf: where to get the data from
1157 * @count: bytes sent
1158 * @ppos: where to start
1159 *
1160 * Accepts only one netlbladdr per write call.
1161 * Returns number of bytes written or error code, as appropriate
1162 */
1163static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1164 size_t count, loff_t *ppos)
1165{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001166 struct smk_netlbladdr *snp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001167 struct sockaddr_in newname;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001168 char *smack;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001169 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001170 char *data;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001171 char *host = (char *)&newname.sin_addr.s_addr;
1172 int rc;
1173 struct netlbl_audit audit_info;
1174 struct in_addr mask;
1175 unsigned int m;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001176 int found;
etienne113a0e42009-03-04 07:33:51 +01001177 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001178 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +01001179 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001180
1181 /*
1182 * Must have privilege.
1183 * No partial writes.
1184 * Enough data must be present.
1185 * "<addr/mask, as a.b.c.d/e><space><label>"
1186 * "<addr, as a.b.c.d><space><label>"
1187 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001188 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001189 return -EPERM;
1190 if (*ppos != 0)
1191 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001192 if (count < SMK_NETLBLADDRMIN)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001193 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001194
1195 data = kzalloc(count + 1, GFP_KERNEL);
1196 if (data == NULL)
1197 return -ENOMEM;
1198
1199 if (copy_from_user(data, buf, count) != 0) {
1200 rc = -EFAULT;
1201 goto free_data_out;
1202 }
1203
1204 smack = kzalloc(count + 1, GFP_KERNEL);
1205 if (smack == NULL) {
1206 rc = -ENOMEM;
1207 goto free_data_out;
1208 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001209
1210 data[count] = '\0';
1211
Toralf Försterec554fa2014-04-27 19:33:34 +02001212 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001213 &host[0], &host[1], &host[2], &host[3], &m, smack);
1214 if (rc != 6) {
1215 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1216 &host[0], &host[1], &host[2], &host[3], smack);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001217 if (rc != 5) {
1218 rc = -EINVAL;
1219 goto free_out;
1220 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001221 m = BEBITS;
1222 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07001223 if (m > BEBITS) {
1224 rc = -EINVAL;
1225 goto free_out;
1226 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001227
Casey Schauflerf7112e62012-05-06 15:22:02 -07001228 /*
1229 * If smack begins with '-', it is an option, don't import it
1230 */
Etienne Basset43031542009-03-27 17:11:01 -04001231 if (smack[0] != '-') {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001232 skp = smk_import_entry(smack, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001233 if (IS_ERR(skp)) {
1234 rc = PTR_ERR(skp);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001235 goto free_out;
1236 }
Etienne Basset43031542009-03-27 17:11:01 -04001237 } else {
1238 /* check known options */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001239 if (strcmp(smack, smack_cipso_option.smk_known) == 0)
1240 skp = &smack_cipso_option;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001241 else {
1242 rc = -EINVAL;
1243 goto free_out;
1244 }
Etienne Basset43031542009-03-27 17:11:01 -04001245 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001246
etienne113a0e42009-03-04 07:33:51 +01001247 for (temp_mask = 0; m > 0; m--) {
1248 temp_mask |= mask_bits;
1249 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001250 }
etienne113a0e42009-03-04 07:33:51 +01001251 mask.s_addr = cpu_to_be32(temp_mask);
1252
1253 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001254 /*
1255 * Only allow one writer at a time. Writes should be
1256 * quite rare and small in any case.
1257 */
1258 mutex_lock(&smk_netlbladdr_lock);
1259
1260 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001261 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001262 found = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001263 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list) {
1264 if (snp->smk_host.sin_addr.s_addr == nsa &&
1265 snp->smk_mask.s_addr == mask.s_addr) {
Etienne Basset7198e2e2009-03-24 20:53:24 +01001266 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001267 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001268 }
1269 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001270 smk_netlabel_audit_set(&audit_info);
1271
Etienne Basset7198e2e2009-03-24 20:53:24 +01001272 if (found == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001273 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1274 if (snp == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001275 rc = -ENOMEM;
1276 else {
1277 rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001278 snp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1279 snp->smk_mask.s_addr = mask.s_addr;
1280 snp->smk_label = skp;
1281 smk_netlbladdr_insert(snp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001282 }
1283 } else {
Etienne Basset43031542009-03-27 17:11:01 -04001284 /* we delete the unlabeled entry, only if the previous label
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001285 * wasn't the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001286 if (snp->smk_label != &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04001287 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001288 &snp->smk_host.sin_addr, &snp->smk_mask,
Etienne Basset43031542009-03-27 17:11:01 -04001289 PF_INET, &audit_info);
1290 else
1291 rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001292 snp->smk_label = skp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001293 }
1294
1295 /*
1296 * Now tell netlabel about the single label nature of
1297 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -04001298 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001299 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001300 if (rc == 0 && skp != &smack_cipso_option)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001301 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001302 &snp->smk_host.sin_addr, &snp->smk_mask, PF_INET,
1303 snp->smk_label->smk_secid, &audit_info);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001304
1305 if (rc == 0)
1306 rc = count;
1307
1308 mutex_unlock(&smk_netlbladdr_lock);
1309
Casey Schauflerf7112e62012-05-06 15:22:02 -07001310free_out:
1311 kfree(smack);
1312free_data_out:
1313 kfree(data);
1314
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001315 return rc;
1316}
1317
1318static const struct file_operations smk_netlbladdr_ops = {
1319 .open = smk_open_netlbladdr,
1320 .read = seq_read,
1321 .llseek = seq_lseek,
1322 .write = smk_write_netlbladdr,
1323 .release = seq_release,
1324};
1325
Casey Schauflere114e472008-02-04 22:29:50 -08001326/**
1327 * smk_read_doi - read() for /smack/doi
1328 * @filp: file pointer, not actually used
1329 * @buf: where to put the result
1330 * @count: maximum to send along
1331 * @ppos: where to start
1332 *
1333 * Returns number of bytes read or error code, as appropriate
1334 */
1335static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1336 size_t count, loff_t *ppos)
1337{
1338 char temp[80];
1339 ssize_t rc;
1340
1341 if (*ppos != 0)
1342 return 0;
1343
1344 sprintf(temp, "%d", smk_cipso_doi_value);
1345 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1346
1347 return rc;
1348}
1349
1350/**
1351 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001352 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001353 * @buf: where to get the data from
1354 * @count: bytes sent
1355 * @ppos: where to start
1356 *
1357 * Returns number of bytes written or error code, as appropriate
1358 */
1359static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1360 size_t count, loff_t *ppos)
1361{
1362 char temp[80];
1363 int i;
1364
Casey Schaufler1880eff2012-06-05 15:28:30 -07001365 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001366 return -EPERM;
1367
1368 if (count >= sizeof(temp) || count == 0)
1369 return -EINVAL;
1370
1371 if (copy_from_user(temp, buf, count) != 0)
1372 return -EFAULT;
1373
1374 temp[count] = '\0';
1375
1376 if (sscanf(temp, "%d", &i) != 1)
1377 return -EINVAL;
1378
1379 smk_cipso_doi_value = i;
1380
1381 smk_cipso_doi();
1382
1383 return count;
1384}
1385
1386static const struct file_operations smk_doi_ops = {
1387 .read = smk_read_doi,
1388 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001389 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001390};
1391
1392/**
1393 * smk_read_direct - read() for /smack/direct
1394 * @filp: file pointer, not actually used
1395 * @buf: where to put the result
1396 * @count: maximum to send along
1397 * @ppos: where to start
1398 *
1399 * Returns number of bytes read or error code, as appropriate
1400 */
1401static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1402 size_t count, loff_t *ppos)
1403{
1404 char temp[80];
1405 ssize_t rc;
1406
1407 if (*ppos != 0)
1408 return 0;
1409
1410 sprintf(temp, "%d", smack_cipso_direct);
1411 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1412
1413 return rc;
1414}
1415
1416/**
1417 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001418 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001419 * @buf: where to get the data from
1420 * @count: bytes sent
1421 * @ppos: where to start
1422 *
1423 * Returns number of bytes written or error code, as appropriate
1424 */
1425static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1426 size_t count, loff_t *ppos)
1427{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001428 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001429 char temp[80];
1430 int i;
1431
Casey Schaufler1880eff2012-06-05 15:28:30 -07001432 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001433 return -EPERM;
1434
1435 if (count >= sizeof(temp) || count == 0)
1436 return -EINVAL;
1437
1438 if (copy_from_user(temp, buf, count) != 0)
1439 return -EFAULT;
1440
1441 temp[count] = '\0';
1442
1443 if (sscanf(temp, "%d", &i) != 1)
1444 return -EINVAL;
1445
Casey Schauflerf7112e62012-05-06 15:22:02 -07001446 /*
1447 * Don't do anything if the value hasn't actually changed.
1448 * If it is changing reset the level on entries that were
1449 * set up to be direct when they were created.
1450 */
1451 if (smack_cipso_direct != i) {
1452 mutex_lock(&smack_known_lock);
1453 list_for_each_entry_rcu(skp, &smack_known_list, list)
1454 if (skp->smk_netlabel.attr.mls.lvl ==
1455 smack_cipso_direct)
1456 skp->smk_netlabel.attr.mls.lvl = i;
1457 smack_cipso_direct = i;
1458 mutex_unlock(&smack_known_lock);
1459 }
Casey Schauflere114e472008-02-04 22:29:50 -08001460
1461 return count;
1462}
1463
1464static const struct file_operations smk_direct_ops = {
1465 .read = smk_read_direct,
1466 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001467 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001468};
1469
1470/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001471 * smk_read_mapped - read() for /smack/mapped
1472 * @filp: file pointer, not actually used
1473 * @buf: where to put the result
1474 * @count: maximum to send along
1475 * @ppos: where to start
1476 *
1477 * Returns number of bytes read or error code, as appropriate
1478 */
1479static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1480 size_t count, loff_t *ppos)
1481{
1482 char temp[80];
1483 ssize_t rc;
1484
1485 if (*ppos != 0)
1486 return 0;
1487
1488 sprintf(temp, "%d", smack_cipso_mapped);
1489 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1490
1491 return rc;
1492}
1493
1494/**
1495 * smk_write_mapped - write() for /smack/mapped
1496 * @file: file pointer, not actually used
1497 * @buf: where to get the data from
1498 * @count: bytes sent
1499 * @ppos: where to start
1500 *
1501 * Returns number of bytes written or error code, as appropriate
1502 */
1503static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1504 size_t count, loff_t *ppos)
1505{
1506 struct smack_known *skp;
1507 char temp[80];
1508 int i;
1509
Casey Schaufler1880eff2012-06-05 15:28:30 -07001510 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001511 return -EPERM;
1512
1513 if (count >= sizeof(temp) || count == 0)
1514 return -EINVAL;
1515
1516 if (copy_from_user(temp, buf, count) != 0)
1517 return -EFAULT;
1518
1519 temp[count] = '\0';
1520
1521 if (sscanf(temp, "%d", &i) != 1)
1522 return -EINVAL;
1523
1524 /*
1525 * Don't do anything if the value hasn't actually changed.
1526 * If it is changing reset the level on entries that were
1527 * set up to be mapped when they were created.
1528 */
1529 if (smack_cipso_mapped != i) {
1530 mutex_lock(&smack_known_lock);
1531 list_for_each_entry_rcu(skp, &smack_known_list, list)
1532 if (skp->smk_netlabel.attr.mls.lvl ==
1533 smack_cipso_mapped)
1534 skp->smk_netlabel.attr.mls.lvl = i;
1535 smack_cipso_mapped = i;
1536 mutex_unlock(&smack_known_lock);
1537 }
1538
1539 return count;
1540}
1541
1542static const struct file_operations smk_mapped_ops = {
1543 .read = smk_read_mapped,
1544 .write = smk_write_mapped,
1545 .llseek = default_llseek,
1546};
1547
1548/**
Casey Schauflere114e472008-02-04 22:29:50 -08001549 * smk_read_ambient - read() for /smack/ambient
1550 * @filp: file pointer, not actually used
1551 * @buf: where to put the result
1552 * @cn: maximum to send along
1553 * @ppos: where to start
1554 *
1555 * Returns number of bytes read or error code, as appropriate
1556 */
1557static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1558 size_t cn, loff_t *ppos)
1559{
1560 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001561 int asize;
1562
1563 if (*ppos != 0)
1564 return 0;
1565 /*
1566 * Being careful to avoid a problem in the case where
1567 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001568 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001569 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001570
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001571 asize = strlen(smack_net_ambient->smk_known) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001572
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001573 if (cn >= asize)
1574 rc = simple_read_from_buffer(buf, cn, ppos,
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001575 smack_net_ambient->smk_known,
1576 asize);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001577 else
1578 rc = -EINVAL;
1579
1580 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001581
1582 return rc;
1583}
1584
1585/**
1586 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001587 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001588 * @buf: where to get the data from
1589 * @count: bytes sent
1590 * @ppos: where to start
1591 *
1592 * Returns number of bytes written or error code, as appropriate
1593 */
1594static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1595 size_t count, loff_t *ppos)
1596{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001597 struct smack_known *skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001598 char *oldambient;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001599 char *data;
1600 int rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -08001601
Casey Schaufler1880eff2012-06-05 15:28:30 -07001602 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001603 return -EPERM;
1604
Casey Schauflerf7112e62012-05-06 15:22:02 -07001605 data = kzalloc(count + 1, GFP_KERNEL);
1606 if (data == NULL)
1607 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001608
Casey Schauflerf7112e62012-05-06 15:22:02 -07001609 if (copy_from_user(data, buf, count) != 0) {
1610 rc = -EFAULT;
1611 goto out;
1612 }
Casey Schauflere114e472008-02-04 22:29:50 -08001613
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001614 skp = smk_import_entry(data, count);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001615 if (IS_ERR(skp)) {
1616 rc = PTR_ERR(skp);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001617 goto out;
1618 }
Casey Schauflere114e472008-02-04 22:29:50 -08001619
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001620 mutex_lock(&smack_ambient_lock);
1621
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001622 oldambient = smack_net_ambient->smk_known;
1623 smack_net_ambient = skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001624 smk_unlbl_ambient(oldambient);
1625
1626 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001627
Casey Schauflerf7112e62012-05-06 15:22:02 -07001628out:
1629 kfree(data);
1630 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001631}
1632
1633static const struct file_operations smk_ambient_ops = {
1634 .read = smk_read_ambient,
1635 .write = smk_write_ambient,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001636 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001637};
1638
Casey Schaufler15446232008-07-30 15:37:11 -07001639/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08001640 * smk_read_onlycap - read() for smackfs/onlycap
Casey Schaufler15446232008-07-30 15:37:11 -07001641 * @filp: file pointer, not actually used
1642 * @buf: where to put the result
1643 * @cn: maximum to send along
1644 * @ppos: where to start
1645 *
1646 * Returns number of bytes read or error code, as appropriate
1647 */
1648static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1649 size_t cn, loff_t *ppos)
1650{
1651 char *smack = "";
1652 ssize_t rc = -EINVAL;
1653 int asize;
1654
1655 if (*ppos != 0)
1656 return 0;
1657
1658 if (smack_onlycap != NULL)
Casey Schaufler00f84f32013-12-23 11:07:10 -08001659 smack = smack_onlycap->smk_known;
Casey Schaufler15446232008-07-30 15:37:11 -07001660
1661 asize = strlen(smack) + 1;
1662
1663 if (cn >= asize)
1664 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1665
1666 return rc;
1667}
1668
1669/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08001670 * smk_write_onlycap - write() for smackfs/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001671 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001672 * @buf: where to get the data from
1673 * @count: bytes sent
1674 * @ppos: where to start
1675 *
1676 * Returns number of bytes written or error code, as appropriate
1677 */
1678static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1679 size_t count, loff_t *ppos)
1680{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001681 char *data;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001682 struct smack_known *skp = smk_of_task(current->cred->security);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001683 int rc = count;
Casey Schaufler15446232008-07-30 15:37:11 -07001684
Casey Schaufler1880eff2012-06-05 15:28:30 -07001685 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler15446232008-07-30 15:37:11 -07001686 return -EPERM;
1687
1688 /*
1689 * This can be done using smk_access() but is done
1690 * explicitly for clarity. The smk_access() implementation
1691 * would use smk_access(smack_onlycap, MAY_WRITE)
1692 */
Casey Schaufler00f84f32013-12-23 11:07:10 -08001693 if (smack_onlycap != NULL && smack_onlycap != skp)
Casey Schaufler15446232008-07-30 15:37:11 -07001694 return -EPERM;
1695
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001696 data = kzalloc(count + 1, GFP_KERNEL);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001697 if (data == NULL)
1698 return -ENOMEM;
Casey Schaufler15446232008-07-30 15:37:11 -07001699
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001700 if (copy_from_user(data, buf, count) != 0) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001701 rc = -EFAULT;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001702 goto freeout;
1703 }
Casey Schaufler15446232008-07-30 15:37:11 -07001704
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001705 /*
1706 * Clear the smack_onlycap on invalid label errors. This means
1707 * that we can pass a null string to unset the onlycap value.
1708 *
1709 * Importing will also reject a label beginning with '-',
1710 * so "-usecapabilities" will also work.
1711 *
1712 * But do so only on invalid label, not on system errors.
1713 */
1714 skp = smk_import_entry(data, count);
1715 if (PTR_ERR(skp) == -EINVAL)
1716 skp = NULL;
1717 else if (IS_ERR(skp)) {
1718 rc = PTR_ERR(skp);
1719 goto freeout;
1720 }
1721
1722 smack_onlycap = skp;
1723
1724freeout:
Casey Schauflerf7112e62012-05-06 15:22:02 -07001725 kfree(data);
1726 return rc;
Casey Schaufler15446232008-07-30 15:37:11 -07001727}
1728
1729static const struct file_operations smk_onlycap_ops = {
1730 .read = smk_read_onlycap,
1731 .write = smk_write_onlycap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001732 .llseek = default_llseek,
Casey Schaufler15446232008-07-30 15:37:11 -07001733};
1734
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07001735#ifdef CONFIG_SECURITY_SMACK_BRINGUP
1736/**
1737 * smk_read_unconfined - read() for smackfs/unconfined
1738 * @filp: file pointer, not actually used
1739 * @buf: where to put the result
1740 * @cn: maximum to send along
1741 * @ppos: where to start
1742 *
1743 * Returns number of bytes read or error code, as appropriate
1744 */
1745static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
1746 size_t cn, loff_t *ppos)
1747{
1748 char *smack = "";
1749 ssize_t rc = -EINVAL;
1750 int asize;
1751
1752 if (*ppos != 0)
1753 return 0;
1754
1755 if (smack_unconfined != NULL)
1756 smack = smack_unconfined->smk_known;
1757
1758 asize = strlen(smack) + 1;
1759
1760 if (cn >= asize)
1761 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1762
1763 return rc;
1764}
1765
1766/**
1767 * smk_write_unconfined - write() for smackfs/unconfined
1768 * @file: file pointer, not actually used
1769 * @buf: where to get the data from
1770 * @count: bytes sent
1771 * @ppos: where to start
1772 *
1773 * Returns number of bytes written or error code, as appropriate
1774 */
1775static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
1776 size_t count, loff_t *ppos)
1777{
1778 char *data;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001779 struct smack_known *skp;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07001780 int rc = count;
1781
1782 if (!smack_privileged(CAP_MAC_ADMIN))
1783 return -EPERM;
1784
1785 data = kzalloc(count + 1, GFP_KERNEL);
1786 if (data == NULL)
1787 return -ENOMEM;
1788
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001789 if (copy_from_user(data, buf, count) != 0) {
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07001790 rc = -EFAULT;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001791 goto freeout;
1792 }
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07001793
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001794 /*
1795 * Clear the smack_unconfined on invalid label errors. This means
1796 * that we can pass a null string to unset the unconfined value.
1797 *
1798 * Importing will also reject a label beginning with '-',
1799 * so "-confine" will also work.
1800 *
1801 * But do so only on invalid label, not on system errors.
1802 */
1803 skp = smk_import_entry(data, count);
1804 if (PTR_ERR(skp) == -EINVAL)
1805 skp = NULL;
1806 else if (IS_ERR(skp)) {
1807 rc = PTR_ERR(skp);
1808 goto freeout;
1809 }
1810
1811 smack_unconfined = skp;
1812
1813freeout:
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07001814 kfree(data);
1815 return rc;
1816}
1817
1818static const struct file_operations smk_unconfined_ops = {
1819 .read = smk_read_unconfined,
1820 .write = smk_write_unconfined,
1821 .llseek = default_llseek,
1822};
1823#endif /* CONFIG_SECURITY_SMACK_BRINGUP */
1824
Casey Schauflere114e472008-02-04 22:29:50 -08001825/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001826 * smk_read_logging - read() for /smack/logging
1827 * @filp: file pointer, not actually used
1828 * @buf: where to put the result
1829 * @cn: maximum to send along
1830 * @ppos: where to start
1831 *
1832 * Returns number of bytes read or error code, as appropriate
1833 */
1834static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1835 size_t count, loff_t *ppos)
1836{
1837 char temp[32];
1838 ssize_t rc;
1839
1840 if (*ppos != 0)
1841 return 0;
1842
1843 sprintf(temp, "%d\n", log_policy);
1844 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1845 return rc;
1846}
1847
1848/**
1849 * smk_write_logging - write() for /smack/logging
1850 * @file: file pointer, not actually used
1851 * @buf: where to get the data from
1852 * @count: bytes sent
1853 * @ppos: where to start
1854 *
1855 * Returns number of bytes written or error code, as appropriate
1856 */
1857static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1858 size_t count, loff_t *ppos)
1859{
1860 char temp[32];
1861 int i;
1862
Casey Schaufler1880eff2012-06-05 15:28:30 -07001863 if (!smack_privileged(CAP_MAC_ADMIN))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001864 return -EPERM;
1865
1866 if (count >= sizeof(temp) || count == 0)
1867 return -EINVAL;
1868
1869 if (copy_from_user(temp, buf, count) != 0)
1870 return -EFAULT;
1871
1872 temp[count] = '\0';
1873
1874 if (sscanf(temp, "%d", &i) != 1)
1875 return -EINVAL;
1876 if (i < 0 || i > 3)
1877 return -EINVAL;
1878 log_policy = i;
1879 return count;
1880}
1881
1882
1883
1884static const struct file_operations smk_logging_ops = {
1885 .read = smk_read_logging,
1886 .write = smk_write_logging,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001887 .llseek = default_llseek,
Etienne Bassetecfcc532009-04-08 20:40:06 +02001888};
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001889
1890/*
1891 * Seq_file read operations for /smack/load-self
1892 */
1893
1894static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1895{
1896 struct task_smack *tsp = current_security();
1897
Casey Schaufler40809562011-11-10 15:02:22 -08001898 return smk_seq_start(s, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001899}
1900
1901static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1902{
1903 struct task_smack *tsp = current_security();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001904
Casey Schaufler40809562011-11-10 15:02:22 -08001905 return smk_seq_next(s, v, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001906}
1907
1908static int load_self_seq_show(struct seq_file *s, void *v)
1909{
1910 struct list_head *list = v;
1911 struct smack_rule *srp =
Rafal Krypa01fa8472015-05-21 18:24:31 +02001912 list_entry_rcu(list, struct smack_rule, list);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001913
Casey Schauflerf7112e62012-05-06 15:22:02 -07001914 smk_rule_show(s, srp, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001915
1916 return 0;
1917}
1918
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001919static const struct seq_operations load_self_seq_ops = {
1920 .start = load_self_seq_start,
1921 .next = load_self_seq_next,
1922 .show = load_self_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001923 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001924};
1925
1926
1927/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001928 * smk_open_load_self - open() for /smack/load-self2
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001929 * @inode: inode structure representing file
1930 * @file: "load" file pointer
1931 *
1932 * For reading, use load_seq_* seq_file reading operations.
1933 */
1934static int smk_open_load_self(struct inode *inode, struct file *file)
1935{
1936 return seq_open(file, &load_self_seq_ops);
1937}
1938
1939/**
1940 * smk_write_load_self - write() for /smack/load-self
1941 * @file: file pointer, not actually used
1942 * @buf: where to get the data from
1943 * @count: bytes sent
1944 * @ppos: where to start - must be 0
1945 *
1946 */
1947static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1948 size_t count, loff_t *ppos)
1949{
1950 struct task_smack *tsp = current_security();
1951
Casey Schauflerf7112e62012-05-06 15:22:02 -07001952 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1953 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001954}
1955
1956static const struct file_operations smk_load_self_ops = {
1957 .open = smk_open_load_self,
1958 .read = seq_read,
1959 .llseek = seq_lseek,
1960 .write = smk_write_load_self,
1961 .release = seq_release,
1962};
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001963
1964/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001965 * smk_user_access - handle access check transaction
1966 * @file: file pointer
1967 * @buf: data from user space
1968 * @count: bytes sent
1969 * @ppos: where to start - must be 0
1970 */
1971static ssize_t smk_user_access(struct file *file, const char __user *buf,
1972 size_t count, loff_t *ppos, int format)
1973{
Rafal Krypae05b6f92013-01-10 19:42:00 +01001974 struct smack_parsed_rule rule;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001975 char *data;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001976 int res;
1977
1978 data = simple_transaction_get(file, buf, count);
1979 if (IS_ERR(data))
1980 return PTR_ERR(data);
1981
1982 if (format == SMK_FIXED24_FMT) {
1983 if (count < SMK_LOADLEN)
1984 return -EINVAL;
1985 res = smk_parse_rule(data, &rule, 0);
1986 } else {
1987 /*
Rafal Krypa10289b02013-08-09 11:47:07 +02001988 * simple_transaction_get() returns null-terminated data
Casey Schauflerf7112e62012-05-06 15:22:02 -07001989 */
Rafal Krypa10289b02013-08-09 11:47:07 +02001990 res = smk_parse_long_rule(data, &rule, 0, 3);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001991 }
1992
Jarkko Sakkinen398ce072013-11-28 19:16:46 +02001993 if (res >= 0)
1994 res = smk_access(rule.smk_subject, rule.smk_object,
1995 rule.smk_access1, NULL);
1996 else if (res != -ENOENT)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001997 return res;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001998
Casey Schauflerd166c802014-08-27 14:51:27 -07001999 /*
2000 * smk_access() can return a value > 0 in the "bringup" case.
2001 */
2002 data[0] = res >= 0 ? '1' : '0';
Casey Schauflerf7112e62012-05-06 15:22:02 -07002003 data[1] = '\0';
2004
2005 simple_transaction_set(file, 2);
2006
2007 if (format == SMK_FIXED24_FMT)
2008 return SMK_LOADLEN;
2009 return count;
2010}
2011
2012/**
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002013 * smk_write_access - handle access check transaction
2014 * @file: file pointer
2015 * @buf: data from user space
2016 * @count: bytes sent
2017 * @ppos: where to start - must be 0
2018 */
2019static ssize_t smk_write_access(struct file *file, const char __user *buf,
2020 size_t count, loff_t *ppos)
2021{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002022 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002023}
2024
2025static const struct file_operations smk_access_ops = {
2026 .write = smk_write_access,
2027 .read = simple_transaction_read,
2028 .release = simple_transaction_release,
2029 .llseek = generic_file_llseek,
2030};
2031
Casey Schauflerf7112e62012-05-06 15:22:02 -07002032
2033/*
2034 * Seq_file read operations for /smack/load2
2035 */
2036
2037static int load2_seq_show(struct seq_file *s, void *v)
2038{
2039 struct list_head *list = v;
2040 struct smack_master_list *smlp =
Rafal Krypa01fa8472015-05-21 18:24:31 +02002041 list_entry_rcu(list, struct smack_master_list, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002042
2043 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
2044
2045 return 0;
2046}
2047
2048static const struct seq_operations load2_seq_ops = {
2049 .start = load2_seq_start,
2050 .next = load2_seq_next,
2051 .show = load2_seq_show,
2052 .stop = smk_seq_stop,
2053};
2054
2055/**
2056 * smk_open_load2 - open() for /smack/load2
2057 * @inode: inode structure representing file
2058 * @file: "load2" file pointer
2059 *
2060 * For reading, use load2_seq_* seq_file reading operations.
2061 */
2062static int smk_open_load2(struct inode *inode, struct file *file)
2063{
2064 return seq_open(file, &load2_seq_ops);
2065}
2066
2067/**
2068 * smk_write_load2 - write() for /smack/load2
2069 * @file: file pointer, not actually used
2070 * @buf: where to get the data from
2071 * @count: bytes sent
2072 * @ppos: where to start - must be 0
2073 *
2074 */
2075static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2076 size_t count, loff_t *ppos)
2077{
2078 /*
2079 * Must have privilege.
2080 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07002081 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07002082 return -EPERM;
2083
2084 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2085 SMK_LONG_FMT);
2086}
2087
2088static const struct file_operations smk_load2_ops = {
2089 .open = smk_open_load2,
2090 .read = seq_read,
2091 .llseek = seq_lseek,
2092 .write = smk_write_load2,
2093 .release = seq_release,
2094};
2095
2096/*
2097 * Seq_file read operations for /smack/load-self2
2098 */
2099
2100static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2101{
2102 struct task_smack *tsp = current_security();
2103
2104 return smk_seq_start(s, pos, &tsp->smk_rules);
2105}
2106
2107static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2108{
2109 struct task_smack *tsp = current_security();
2110
2111 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2112}
2113
2114static int load_self2_seq_show(struct seq_file *s, void *v)
2115{
2116 struct list_head *list = v;
2117 struct smack_rule *srp =
Rafal Krypa01fa8472015-05-21 18:24:31 +02002118 list_entry_rcu(list, struct smack_rule, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002119
2120 smk_rule_show(s, srp, SMK_LONGLABEL);
2121
2122 return 0;
2123}
2124
2125static const struct seq_operations load_self2_seq_ops = {
2126 .start = load_self2_seq_start,
2127 .next = load_self2_seq_next,
2128 .show = load_self2_seq_show,
2129 .stop = smk_seq_stop,
2130};
2131
2132/**
2133 * smk_open_load_self2 - open() for /smack/load-self2
2134 * @inode: inode structure representing file
2135 * @file: "load" file pointer
2136 *
2137 * For reading, use load_seq_* seq_file reading operations.
2138 */
2139static int smk_open_load_self2(struct inode *inode, struct file *file)
2140{
2141 return seq_open(file, &load_self2_seq_ops);
2142}
2143
2144/**
2145 * smk_write_load_self2 - write() for /smack/load-self2
2146 * @file: file pointer, not actually used
2147 * @buf: where to get the data from
2148 * @count: bytes sent
2149 * @ppos: where to start - must be 0
2150 *
2151 */
2152static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2153 size_t count, loff_t *ppos)
2154{
2155 struct task_smack *tsp = current_security();
2156
2157 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2158 &tsp->smk_rules_lock, SMK_LONG_FMT);
2159}
2160
2161static const struct file_operations smk_load_self2_ops = {
2162 .open = smk_open_load_self2,
2163 .read = seq_read,
2164 .llseek = seq_lseek,
2165 .write = smk_write_load_self2,
2166 .release = seq_release,
2167};
2168
2169/**
2170 * smk_write_access2 - handle access check transaction
2171 * @file: file pointer
2172 * @buf: data from user space
2173 * @count: bytes sent
2174 * @ppos: where to start - must be 0
2175 */
2176static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2177 size_t count, loff_t *ppos)
2178{
2179 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2180}
2181
2182static const struct file_operations smk_access2_ops = {
2183 .write = smk_write_access2,
2184 .read = simple_transaction_read,
2185 .release = simple_transaction_release,
2186 .llseek = generic_file_llseek,
2187};
2188
Etienne Bassetecfcc532009-04-08 20:40:06 +02002189/**
Rafal Krypa449543b2012-07-11 17:49:30 +02002190 * smk_write_revoke_subj - write() for /smack/revoke-subject
2191 * @file: file pointer
2192 * @buf: data from user space
2193 * @count: bytes sent
2194 * @ppos: where to start - must be 0
2195 */
2196static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2197 size_t count, loff_t *ppos)
2198{
2199 char *data = NULL;
2200 const char *cp = NULL;
2201 struct smack_known *skp;
2202 struct smack_rule *sp;
2203 struct list_head *rule_list;
2204 struct mutex *rule_lock;
2205 int rc = count;
2206
2207 if (*ppos != 0)
2208 return -EINVAL;
2209
2210 if (!smack_privileged(CAP_MAC_ADMIN))
2211 return -EPERM;
2212
2213 if (count == 0 || count > SMK_LONGLABEL)
2214 return -EINVAL;
2215
2216 data = kzalloc(count, GFP_KERNEL);
2217 if (data == NULL)
2218 return -ENOMEM;
2219
2220 if (copy_from_user(data, buf, count) != 0) {
2221 rc = -EFAULT;
2222 goto free_out;
2223 }
2224
2225 cp = smk_parse_smack(data, count);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002226 if (IS_ERR(cp)) {
2227 rc = PTR_ERR(cp);
Rafal Krypa449543b2012-07-11 17:49:30 +02002228 goto free_out;
2229 }
2230
2231 skp = smk_find_entry(cp);
Rafal Krypad15d9fa2012-11-27 16:28:11 +01002232 if (skp == NULL)
Rafal Krypa449543b2012-07-11 17:49:30 +02002233 goto free_out;
Rafal Krypa449543b2012-07-11 17:49:30 +02002234
2235 rule_list = &skp->smk_rules;
2236 rule_lock = &skp->smk_rules_lock;
2237
2238 mutex_lock(rule_lock);
2239
2240 list_for_each_entry_rcu(sp, rule_list, list)
2241 sp->smk_access = 0;
2242
2243 mutex_unlock(rule_lock);
2244
2245free_out:
2246 kfree(data);
2247 kfree(cp);
2248 return rc;
2249}
2250
2251static const struct file_operations smk_revoke_subj_ops = {
2252 .write = smk_write_revoke_subj,
2253 .read = simple_transaction_read,
2254 .release = simple_transaction_release,
2255 .llseek = generic_file_llseek,
2256};
2257
Casey Schauflere9307232012-11-01 18:14:32 -07002258static struct kset *smackfs_kset;
2259/**
2260 * smk_init_sysfs - initialize /sys/fs/smackfs
2261 *
2262 */
2263static int smk_init_sysfs(void)
2264{
2265 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2266 if (!smackfs_kset)
2267 return -ENOMEM;
2268 return 0;
2269}
2270
Rafal Krypa449543b2012-07-11 17:49:30 +02002271/**
Rafal Krypae05b6f92013-01-10 19:42:00 +01002272 * smk_write_change_rule - write() for /smack/change-rule
2273 * @file: file pointer
2274 * @buf: data from user space
2275 * @count: bytes sent
2276 * @ppos: where to start - must be 0
2277 */
2278static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2279 size_t count, loff_t *ppos)
2280{
2281 /*
2282 * Must have privilege.
2283 */
Casey Schaufler4afde482013-12-19 13:23:26 -08002284 if (!smack_privileged(CAP_MAC_ADMIN))
Rafal Krypae05b6f92013-01-10 19:42:00 +01002285 return -EPERM;
2286
2287 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2288 SMK_CHANGE_FMT);
2289}
2290
2291static const struct file_operations smk_change_rule_ops = {
2292 .write = smk_write_change_rule,
2293 .read = simple_transaction_read,
2294 .release = simple_transaction_release,
2295 .llseek = generic_file_llseek,
2296};
2297
2298/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002299 * smk_read_syslog - read() for smackfs/syslog
2300 * @filp: file pointer, not actually used
2301 * @buf: where to put the result
2302 * @cn: maximum to send along
2303 * @ppos: where to start
2304 *
2305 * Returns number of bytes read or error code, as appropriate
2306 */
2307static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2308 size_t cn, loff_t *ppos)
2309{
2310 struct smack_known *skp;
2311 ssize_t rc = -EINVAL;
2312 int asize;
2313
2314 if (*ppos != 0)
2315 return 0;
2316
2317 if (smack_syslog_label == NULL)
2318 skp = &smack_known_star;
2319 else
2320 skp = smack_syslog_label;
2321
2322 asize = strlen(skp->smk_known) + 1;
2323
2324 if (cn >= asize)
2325 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2326 asize);
2327
2328 return rc;
2329}
2330
2331/**
2332 * smk_write_syslog - write() for smackfs/syslog
2333 * @file: file pointer, not actually used
2334 * @buf: where to get the data from
2335 * @count: bytes sent
2336 * @ppos: where to start
2337 *
2338 * Returns number of bytes written or error code, as appropriate
2339 */
2340static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2341 size_t count, loff_t *ppos)
2342{
2343 char *data;
2344 struct smack_known *skp;
2345 int rc = count;
2346
2347 if (!smack_privileged(CAP_MAC_ADMIN))
2348 return -EPERM;
2349
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04002350 data = kzalloc(count + 1, GFP_KERNEL);
Casey Schaufler00f84f32013-12-23 11:07:10 -08002351 if (data == NULL)
2352 return -ENOMEM;
2353
2354 if (copy_from_user(data, buf, count) != 0)
2355 rc = -EFAULT;
2356 else {
2357 skp = smk_import_entry(data, count);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002358 if (IS_ERR(skp))
2359 rc = PTR_ERR(skp);
Casey Schaufler00f84f32013-12-23 11:07:10 -08002360 else
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002361 smack_syslog_label = skp;
Casey Schaufler00f84f32013-12-23 11:07:10 -08002362 }
2363
2364 kfree(data);
2365 return rc;
2366}
2367
2368static const struct file_operations smk_syslog_ops = {
2369 .read = smk_read_syslog,
2370 .write = smk_write_syslog,
2371 .llseek = default_llseek,
2372};
2373
2374
2375/**
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002376 * smk_read_ptrace - read() for /smack/ptrace
2377 * @filp: file pointer, not actually used
2378 * @buf: where to put the result
2379 * @count: maximum to send along
2380 * @ppos: where to start
2381 *
2382 * Returns number of bytes read or error code, as appropriate
2383 */
2384static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2385 size_t count, loff_t *ppos)
2386{
2387 char temp[32];
2388 ssize_t rc;
2389
2390 if (*ppos != 0)
2391 return 0;
2392
2393 sprintf(temp, "%d\n", smack_ptrace_rule);
2394 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2395 return rc;
2396}
2397
2398/**
2399 * smk_write_ptrace - write() for /smack/ptrace
2400 * @file: file pointer
2401 * @buf: data from user space
2402 * @count: bytes sent
2403 * @ppos: where to start - must be 0
2404 */
2405static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2406 size_t count, loff_t *ppos)
2407{
2408 char temp[32];
2409 int i;
2410
2411 if (!smack_privileged(CAP_MAC_ADMIN))
2412 return -EPERM;
2413
2414 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2415 return -EINVAL;
2416
2417 if (copy_from_user(temp, buf, count) != 0)
2418 return -EFAULT;
2419
2420 temp[count] = '\0';
2421
2422 if (sscanf(temp, "%d", &i) != 1)
2423 return -EINVAL;
2424 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2425 return -EINVAL;
2426 smack_ptrace_rule = i;
2427
2428 return count;
2429}
2430
2431static const struct file_operations smk_ptrace_ops = {
2432 .write = smk_write_ptrace,
2433 .read = smk_read_ptrace,
2434 .llseek = default_llseek,
2435};
2436
2437/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002438 * smk_fill_super - fill the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002439 * @sb: the empty superblock
2440 * @data: unused
2441 * @silent: unused
2442 *
Casey Schaufler00f84f32013-12-23 11:07:10 -08002443 * Fill in the well known entries for the smack filesystem
Casey Schauflere114e472008-02-04 22:29:50 -08002444 *
2445 * Returns 0 on success, an error code on failure
2446 */
2447static int smk_fill_super(struct super_block *sb, void *data, int silent)
2448{
2449 int rc;
2450 struct inode *root_inode;
2451
2452 static struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002453 [SMK_LOAD] = {
2454 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2455 [SMK_CIPSO] = {
2456 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2457 [SMK_DOI] = {
2458 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2459 [SMK_DIRECT] = {
2460 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2461 [SMK_AMBIENT] = {
2462 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2463 [SMK_NETLBLADDR] = {
2464 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2465 [SMK_ONLYCAP] = {
2466 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2467 [SMK_LOGGING] = {
2468 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2469 [SMK_LOAD_SELF] = {
2470 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002471 [SMK_ACCESSES] = {
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03002472 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
Casey Schauflerf7112e62012-05-06 15:22:02 -07002473 [SMK_MAPPED] = {
2474 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2475 [SMK_LOAD2] = {
2476 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2477 [SMK_LOAD_SELF2] = {
2478 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2479 [SMK_ACCESS2] = {
2480 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2481 [SMK_CIPSO2] = {
2482 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
Rafal Krypa449543b2012-07-11 17:49:30 +02002483 [SMK_REVOKE_SUBJ] = {
2484 "revoke-subject", &smk_revoke_subj_ops,
2485 S_IRUGO|S_IWUSR},
Rafal Krypae05b6f92013-01-10 19:42:00 +01002486 [SMK_CHANGE_RULE] = {
2487 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
Casey Schaufler00f84f32013-12-23 11:07:10 -08002488 [SMK_SYSLOG] = {
2489 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002490 [SMK_PTRACE] = {
2491 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002492#ifdef CONFIG_SECURITY_SMACK_BRINGUP
2493 [SMK_UNCONFINED] = {
2494 "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2495#endif
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002496 /* last one */
2497 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08002498 };
2499
2500 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2501 if (rc != 0) {
2502 printk(KERN_ERR "%s failed %d while creating inodes\n",
2503 __func__, rc);
2504 return rc;
2505 }
2506
David Howellsce0b16d2015-02-19 10:47:02 +00002507 root_inode = d_inode(sb->s_root);
Casey Schauflere114e472008-02-04 22:29:50 -08002508
2509 return 0;
2510}
2511
2512/**
Al Virofc14f2f2010-07-25 01:48:30 +04002513 * smk_mount - get the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002514 * @fs_type: passed along without comment
2515 * @flags: passed along without comment
2516 * @dev_name: passed along without comment
2517 * @data: passed along without comment
Casey Schauflere114e472008-02-04 22:29:50 -08002518 *
2519 * Just passes everything along.
2520 *
2521 * Returns what the lower level code does.
2522 */
Al Virofc14f2f2010-07-25 01:48:30 +04002523static struct dentry *smk_mount(struct file_system_type *fs_type,
2524 int flags, const char *dev_name, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -08002525{
Al Virofc14f2f2010-07-25 01:48:30 +04002526 return mount_single(fs_type, flags, data, smk_fill_super);
Casey Schauflere114e472008-02-04 22:29:50 -08002527}
2528
2529static struct file_system_type smk_fs_type = {
2530 .name = "smackfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002531 .mount = smk_mount,
Casey Schauflere114e472008-02-04 22:29:50 -08002532 .kill_sb = kill_litter_super,
2533};
2534
2535static struct vfsmount *smackfs_mount;
2536
Casey Schauflerf7112e62012-05-06 15:22:02 -07002537static int __init smk_preset_netlabel(struct smack_known *skp)
2538{
2539 skp->smk_netlabel.domain = skp->smk_known;
2540 skp->smk_netlabel.flags =
2541 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2542 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2543 &skp->smk_netlabel, strlen(skp->smk_known));
2544}
2545
Casey Schauflere114e472008-02-04 22:29:50 -08002546/**
2547 * init_smk_fs - get the smackfs superblock
2548 *
2549 * register the smackfs
2550 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002551 * Do not register smackfs if Smack wasn't enabled
2552 * on boot. We can not put this method normally under the
2553 * smack_init() code path since the security subsystem get
2554 * initialized before the vfs caches.
2555 *
2556 * Returns true if we were not chosen on boot or if
2557 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08002558 */
2559static int __init init_smk_fs(void)
2560{
2561 int err;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002562 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002563
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002564 if (!security_module_enable("smack"))
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002565 return 0;
2566
Casey Schauflere9307232012-11-01 18:14:32 -07002567 err = smk_init_sysfs();
2568 if (err)
2569 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2570
Casey Schauflere114e472008-02-04 22:29:50 -08002571 err = register_filesystem(&smk_fs_type);
2572 if (!err) {
2573 smackfs_mount = kern_mount(&smk_fs_type);
2574 if (IS_ERR(smackfs_mount)) {
2575 printk(KERN_ERR "smackfs: could not mount!\n");
2576 err = PTR_ERR(smackfs_mount);
2577 smackfs_mount = NULL;
2578 }
2579 }
2580
Casey Schauflere114e472008-02-04 22:29:50 -08002581 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002582 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08002583
Casey Schauflerf7112e62012-05-06 15:22:02 -07002584 rc = smk_preset_netlabel(&smack_known_floor);
2585 if (err == 0 && rc < 0)
2586 err = rc;
2587 rc = smk_preset_netlabel(&smack_known_hat);
2588 if (err == 0 && rc < 0)
2589 err = rc;
2590 rc = smk_preset_netlabel(&smack_known_huh);
2591 if (err == 0 && rc < 0)
2592 err = rc;
2593 rc = smk_preset_netlabel(&smack_known_invalid);
2594 if (err == 0 && rc < 0)
2595 err = rc;
2596 rc = smk_preset_netlabel(&smack_known_star);
2597 if (err == 0 && rc < 0)
2598 err = rc;
2599 rc = smk_preset_netlabel(&smack_known_web);
2600 if (err == 0 && rc < 0)
2601 err = rc;
2602
Casey Schauflere114e472008-02-04 22:29:50 -08002603 return err;
2604}
2605
2606__initcall(init_smk_fs);