blob: ef0d8712d318fe626d1eace6d2467d96acfb1df4 [file] [log] [blame]
Thomas Gleixnera10e7632019-05-31 01:09:32 -07001// SPDX-License-Identifier: GPL-2.0-only
Casey Schauflere114e472008-02-04 22:29:50 -08002/*
3 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
4 *
Casey Schauflere114e472008-02-04 22:29:50 -08005 * Authors:
6 * Casey Schaufler <casey@schaufler-ca.com>
7 * Ahmed S. Darwish <darwish.07@gmail.com>
8 *
9 * Special thanks to the authors of selinuxfs.
10 *
11 * Karl MacMillan <kmacmillan@tresys.com>
12 * James Morris <jmorris@redhat.com>
Casey Schauflere114e472008-02-04 22:29:50 -080013 */
14
15#include <linux/kernel.h>
16#include <linux/vmalloc.h>
17#include <linux/security.h>
18#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Casey Schaufler6d3dc072008-12-31 12:54:12 -050020#include <net/net_namespace.h>
Casey Schauflere114e472008-02-04 22:29:50 -080021#include <net/cipso_ipv4.h>
22#include <linux/seq_file.h>
23#include <linux/ctype.h>
Casey Schaufler4bc87e62008-02-15 15:24:25 -080024#include <linux/audit.h>
Casey Schaufler958d2c22013-04-02 11:41:18 -070025#include <linux/magic.h>
Casey Schauflere114e472008-02-04 22:29:50 -080026#include "smack.h"
27
Casey Schaufler21abb1e2015-07-22 14:25:31 -070028#define BEBITS (sizeof(__be32) * 8)
Casey Schauflere114e472008-02-04 22:29:50 -080029/*
30 * smackfs pseudo filesystem.
31 */
32
33enum smk_inos {
34 SMK_ROOT_INO = 2,
35 SMK_LOAD = 3, /* load policy */
36 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
37 SMK_DOI = 5, /* CIPSO DOI */
38 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
39 SMK_AMBIENT = 7, /* internet ambient label */
Casey Schaufler21abb1e2015-07-22 14:25:31 -070040 SMK_NET4ADDR = 8, /* single label hosts */
Casey Schaufler15446232008-07-30 15:37:11 -070041 SMK_ONLYCAP = 9, /* the only "capable" label */
Etienne Bassetecfcc532009-04-08 20:40:06 +020042 SMK_LOGGING = 10, /* logging */
Casey Schaufler7898e1f2011-01-17 08:05:27 -080043 SMK_LOAD_SELF = 11, /* task specific rules */
Jarkko Sakkinen828716c2011-09-08 10:12:01 +030044 SMK_ACCESSES = 12, /* access policy */
Casey Schauflerf7112e62012-05-06 15:22:02 -070045 SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
46 SMK_LOAD2 = 14, /* load policy with long labels */
47 SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
48 SMK_ACCESS2 = 16, /* make an access check with long labels */
49 SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
Rafal Krypa449543b2012-07-11 17:49:30 +020050 SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
Rafal Krypae05b6f92013-01-10 19:42:00 +010051 SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
Casey Schaufler00f84f32013-12-23 11:07:10 -080052 SMK_SYSLOG = 20, /* change syslog label) */
Lukasz Pawelczyk66867812014-03-11 17:07:06 +010053 SMK_PTRACE = 21, /* set ptrace rule */
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070054#ifdef CONFIG_SECURITY_SMACK_BRINGUP
55 SMK_UNCONFINED = 22, /* define an unconfined label */
56#endif
Casey Schaufler21abb1e2015-07-22 14:25:31 -070057#if IS_ENABLED(CONFIG_IPV6)
58 SMK_NET6ADDR = 23, /* single label IPv6 hosts */
59#endif /* CONFIG_IPV6 */
Zbigniew Jasinski38416e52015-10-19 18:23:53 +020060 SMK_RELABEL_SELF = 24, /* relabel possible without CAP_MAC_ADMIN */
Casey Schauflere114e472008-02-04 22:29:50 -080061};
62
63/*
64 * List locks
65 */
Casey Schauflere114e472008-02-04 22:29:50 -080066static DEFINE_MUTEX(smack_cipso_lock);
Casey Schaufler4bc87e62008-02-15 15:24:25 -080067static DEFINE_MUTEX(smack_ambient_lock);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070068static DEFINE_MUTEX(smk_net4addr_lock);
69#if IS_ENABLED(CONFIG_IPV6)
70static DEFINE_MUTEX(smk_net6addr_lock);
71#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -080072
73/*
74 * This is the "ambient" label for network traffic.
75 * If it isn't somehow marked, use this.
76 * It can be reset via smackfs/ambient
77 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -070078struct smack_known *smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -080079
80/*
Casey Schauflere114e472008-02-04 22:29:50 -080081 * This is the level in a CIPSO header that indicates a
82 * smack label is contained directly in the category set.
83 * It can be reset via smackfs/direct
84 */
85int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
86
Casey Schaufler15446232008-07-30 15:37:11 -070087/*
Casey Schauflerf7112e62012-05-06 15:22:02 -070088 * This is the level in a CIPSO header that indicates a
89 * secid is contained directly in the category set.
90 * It can be reset via smackfs/mapped
91 */
92int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
93
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070094#ifdef CONFIG_SECURITY_SMACK_BRINGUP
95/*
96 * Allow one label to be unconfined. This is for
97 * debugging and application bring-up purposes only.
98 * It is bad and wrong, but everyone seems to expect
99 * to have it.
100 */
101struct smack_known *smack_unconfined;
102#endif
103
Casey Schaufler00f84f32013-12-23 11:07:10 -0800104/*
105 * If this value is set restrict syslog use to the label specified.
106 * It can be reset via smackfs/syslog
107 */
108struct smack_known *smack_syslog_label;
Casey Schaufler15446232008-07-30 15:37:11 -0700109
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500110/*
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100111 * Ptrace current rule
112 * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
113 * SMACK_PTRACE_EXACT labels must match, but can be overriden with
114 * CAP_SYS_PTRACE
115 * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
116 */
117int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
118
119/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500120 * Certain IP addresses may be designated as single label hosts.
121 * Packets are sent there unlabeled, but only from tasks that
122 * can write to the specified label.
123 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100124
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700125LIST_HEAD(smk_net4addr_list);
126#if IS_ENABLED(CONFIG_IPV6)
127LIST_HEAD(smk_net6addr_list);
128#endif /* CONFIG_IPV6 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700129
130/*
131 * Rule lists are maintained for each label.
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700132 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100133struct smack_parsed_rule {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700134 struct smack_known *smk_subject;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200135 struct smack_known *smk_object;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100136 int smk_access1;
137 int smk_access2;
138};
139
Casey Schauflere114e472008-02-04 22:29:50 -0800140static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
Casey Schauflere114e472008-02-04 22:29:50 -0800141
Casey Schauflere114e472008-02-04 22:29:50 -0800142/*
Casey Schauflere114e472008-02-04 22:29:50 -0800143 * Values for parsing cipso rules
144 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700145 * SMK_CIPSOMIN: Minimum possible cipso rule length.
146 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -0800147 */
148#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700149#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
150#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
151
152/*
153 * Values for parsing MAC rules
154 * SMK_ACCESS: Maximum possible combination of access permissions
155 * SMK_ACCESSLEN: Maximum length for a rule access field
156 * SMK_LOADLEN: Smack rule length
157 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200158#define SMK_OACCESS "rwxa"
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700159#define SMK_ACCESS "rwxatl"
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200160#define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
161#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
162#define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
163#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700164
Casey Schauflerf7112e62012-05-06 15:22:02 -0700165/*
166 * Stricly for CIPSO level manipulation.
167 * Set the category bit number in a smack label sized buffer.
168 */
169static inline void smack_catset_bit(unsigned int cat, char *catsetp)
170{
171 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
172 return;
173
174 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
175}
176
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500177/**
178 * smk_netlabel_audit_set - fill a netlbl_audit struct
179 * @nap: structure to fill
180 */
181static void smk_netlabel_audit_set(struct netlbl_audit *nap)
182{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700183 struct smack_known *skp = smk_of_current();
184
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500185 nap->loginuid = audit_get_loginuid(current);
186 nap->sessionid = audit_get_sessionid(current);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700187 nap->secid = skp->smk_secid;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500188}
189
190/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700191 * Value for parsing single label host rules
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500192 * "1.2.3.4 X"
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500193 */
194#define SMK_NETLBLADDRMIN 9
Casey Schauflere114e472008-02-04 22:29:50 -0800195
Casey Schauflere114e472008-02-04 22:29:50 -0800196/**
Rafal Krypae05b6f92013-01-10 19:42:00 +0100197 * smk_set_access - add a rule to the rule list or replace an old rule
198 * @srp: the rule to add or replace
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800199 * @rule_list: the list of rules
200 * @rule_lock: the rule list lock
Casey Schauflere114e472008-02-04 22:29:50 -0800201 *
202 * Looks through the current subject/object/access list for
203 * the subject/object pair and replaces the access that was
204 * there. If the pair isn't found add it with the specified
205 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300206 *
207 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
208 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800209 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100210static int smk_set_access(struct smack_parsed_rule *srp,
211 struct list_head *rule_list,
Vishal Goel460d95a2019-03-07 16:55:24 +0530212 struct mutex *rule_lock)
Casey Schauflere114e472008-02-04 22:29:50 -0800213{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100214 struct smack_rule *sp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800215 int found = 0;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100216 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800217
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800218 mutex_lock(rule_lock);
219
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700220 /*
221 * Because the object label is less likely to match
222 * than the subject label check it first
223 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800224 list_for_each_entry_rcu(sp, rule_list, list) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700225 if (sp->smk_object == srp->smk_object &&
226 sp->smk_subject == srp->smk_subject) {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100227 found = 1;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100228 sp->smk_access |= srp->smk_access1;
229 sp->smk_access &= ~srp->smk_access2;
Casey Schauflere114e472008-02-04 22:29:50 -0800230 break;
231 }
Casey Schauflere114e472008-02-04 22:29:50 -0800232 }
233
Rafal Krypae05b6f92013-01-10 19:42:00 +0100234 if (found == 0) {
Casey Schaufler4e328b02019-04-02 11:37:12 -0700235 sp = kmem_cache_zalloc(smack_rule_cache, GFP_KERNEL);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100236 if (sp == NULL) {
237 rc = -ENOMEM;
238 goto out;
239 }
240
241 sp->smk_subject = srp->smk_subject;
242 sp->smk_object = srp->smk_object;
243 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
244
245 list_add_rcu(&sp->list, rule_list);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100246 }
247
248out:
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800249 mutex_unlock(rule_lock);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100250 return rc;
251}
Casey Schauflere114e472008-02-04 22:29:50 -0800252
Rafal Krypae05b6f92013-01-10 19:42:00 +0100253/**
254 * smk_perm_from_str - parse smack accesses from a text string
255 * @string: a text string that contains a Smack accesses code
256 *
257 * Returns an integer with respective bits set for specified accesses.
258 */
259static int smk_perm_from_str(const char *string)
260{
261 int perm = 0;
262 const char *cp;
263
264 for (cp = string; ; cp++)
265 switch (*cp) {
266 case '-':
267 break;
268 case 'r':
269 case 'R':
270 perm |= MAY_READ;
271 break;
272 case 'w':
273 case 'W':
274 perm |= MAY_WRITE;
275 break;
276 case 'x':
277 case 'X':
278 perm |= MAY_EXEC;
279 break;
280 case 'a':
281 case 'A':
282 perm |= MAY_APPEND;
283 break;
284 case 't':
285 case 'T':
286 perm |= MAY_TRANSMUTE;
287 break;
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700288 case 'l':
289 case 'L':
290 perm |= MAY_LOCK;
291 break;
Casey Schauflerd166c802014-08-27 14:51:27 -0700292 case 'b':
293 case 'B':
294 perm |= MAY_BRINGUP;
295 break;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100296 default:
297 return perm;
298 }
Casey Schauflere114e472008-02-04 22:29:50 -0800299}
300
301/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700302 * smk_fill_rule - Fill Smack rule from strings
303 * @subject: subject label string
304 * @object: object label string
Rafal Krypae05b6f92013-01-10 19:42:00 +0100305 * @access1: access string
306 * @access2: string with permissions to be removed
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300307 * @rule: Smack rule
308 * @import: if non-zero, import labels
Casey Schaufler35187212012-06-18 19:01:36 -0700309 * @len: label length limit
Casey Schauflerf7112e62012-05-06 15:22:02 -0700310 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200311 * Returns 0 on success, appropriate error code on failure.
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300312 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700313static int smk_fill_rule(const char *subject, const char *object,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100314 const char *access1, const char *access2,
315 struct smack_parsed_rule *rule, int import,
316 int len)
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300317{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700318 const char *cp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300319 struct smack_known *skp;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300320
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300321 if (import) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700322 rule->smk_subject = smk_import_entry(subject, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200323 if (IS_ERR(rule->smk_subject))
324 return PTR_ERR(rule->smk_subject);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300325
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200326 rule->smk_object = smk_import_entry(object, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200327 if (IS_ERR(rule->smk_object))
328 return PTR_ERR(rule->smk_object);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300329 } else {
Casey Schaufler35187212012-06-18 19:01:36 -0700330 cp = smk_parse_smack(subject, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200331 if (IS_ERR(cp))
332 return PTR_ERR(cp);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700333 skp = smk_find_entry(cp);
334 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300335 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200336 return -ENOENT;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700337 rule->smk_subject = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300338
Casey Schaufler35187212012-06-18 19:01:36 -0700339 cp = smk_parse_smack(object, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200340 if (IS_ERR(cp))
341 return PTR_ERR(cp);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700342 skp = smk_find_entry(cp);
343 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300344 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200345 return -ENOENT;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200346 rule->smk_object = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300347 }
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300348
Rafal Krypae05b6f92013-01-10 19:42:00 +0100349 rule->smk_access1 = smk_perm_from_str(access1);
350 if (access2)
351 rule->smk_access2 = smk_perm_from_str(access2);
352 else
353 rule->smk_access2 = ~rule->smk_access1;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300354
Casey Schaufler35187212012-06-18 19:01:36 -0700355 return 0;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300356}
357
358/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700359 * smk_parse_rule - parse Smack rule from load string
360 * @data: string to be parsed whose size is SMK_LOADLEN
361 * @rule: Smack rule
362 * @import: if non-zero, import labels
363 *
364 * Returns 0 on success, -1 on errors.
365 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100366static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
367 int import)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700368{
369 int rc;
370
371 rc = smk_fill_rule(data, data + SMK_LABELLEN,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100372 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
373 import, SMK_LABELLEN);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700374 return rc;
375}
376
377/**
378 * smk_parse_long_rule - parse Smack rule from rule string
379 * @data: string to be parsed, null terminated
Rafal Krypae05b6f92013-01-10 19:42:00 +0100380 * @rule: Will be filled with Smack parsed rule
Casey Schauflerf7112e62012-05-06 15:22:02 -0700381 * @import: if non-zero, import labels
Rafal Krypa10289b02013-08-09 11:47:07 +0200382 * @tokens: numer of substrings expected in data
Casey Schauflerf7112e62012-05-06 15:22:02 -0700383 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200384 * Returns number of processed bytes on success, -ERRNO on failure.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700385 */
Rafal Krypa10289b02013-08-09 11:47:07 +0200386static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
387 int import, int tokens)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700388{
Rafal Krypa10289b02013-08-09 11:47:07 +0200389 ssize_t cnt = 0;
390 char *tok[4];
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200391 int rc;
Rafal Krypa10289b02013-08-09 11:47:07 +0200392 int i;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700393
Rafal Krypa10289b02013-08-09 11:47:07 +0200394 /*
395 * Parsing the rule in-place, filling all white-spaces with '\0'
396 */
397 for (i = 0; i < tokens; ++i) {
398 while (isspace(data[cnt]))
399 data[cnt++] = '\0';
Alan Cox3b9fc372012-07-26 14:47:11 -0700400
Rafal Krypa10289b02013-08-09 11:47:07 +0200401 if (data[cnt] == '\0')
402 /* Unexpected end of data */
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200403 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700404
Rafal Krypa10289b02013-08-09 11:47:07 +0200405 tok[i] = data + cnt;
406
407 while (data[cnt] && !isspace(data[cnt]))
408 ++cnt;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100409 }
Rafal Krypa10289b02013-08-09 11:47:07 +0200410 while (isspace(data[cnt]))
411 data[cnt++] = '\0';
Casey Schauflerf7112e62012-05-06 15:22:02 -0700412
Rafal Krypa10289b02013-08-09 11:47:07 +0200413 while (i < 4)
414 tok[i++] = NULL;
415
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200416 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
417 return rc == 0 ? cnt : rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700418}
419
420#define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
421#define SMK_LONG_FMT 1 /* Variable long label format */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100422#define SMK_CHANGE_FMT 2 /* Rule modification format */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700423/**
424 * smk_write_rules_list - write() for any /smack rule file
Randy Dunlap251a2a92009-02-18 11:42:33 -0800425 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800426 * @buf: where to get the data from
427 * @count: bytes sent
428 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800429 * @rule_list: the list of rules to write to
430 * @rule_lock: lock for the rule list
Rafal Krypae05b6f92013-01-10 19:42:00 +0100431 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
Casey Schauflere114e472008-02-04 22:29:50 -0800432 *
433 * Get one smack access rule from above.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700434 * The format for SMK_LONG_FMT is:
435 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
436 * The format for SMK_FIXED24_FMT is exactly:
437 * "subject object rwxat"
Rafal Krypae05b6f92013-01-10 19:42:00 +0100438 * The format for SMK_CHANGE_FMT is:
439 * "subject<whitespace>object<whitespace>
440 * acc_enable<whitespace>acc_disable[<whitespace>...]"
Casey Schauflere114e472008-02-04 22:29:50 -0800441 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700442static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
443 size_t count, loff_t *ppos,
444 struct list_head *rule_list,
445 struct mutex *rule_lock, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800446{
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200447 struct smack_parsed_rule rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800448 char *data;
Rafal Krypa10289b02013-08-09 11:47:07 +0200449 int rc;
450 int trunc = 0;
451 int tokens;
452 ssize_t cnt = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800453
454 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800455 * No partial writes.
456 * Enough data must be present.
457 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200458 if (*ppos != 0)
459 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -0800460
Casey Schauflerf7112e62012-05-06 15:22:02 -0700461 if (format == SMK_FIXED24_FMT) {
462 /*
463 * Minor hack for backward compatibility
464 */
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700465 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700466 return -EINVAL;
Rafal Krypa10289b02013-08-09 11:47:07 +0200467 } else {
468 if (count >= PAGE_SIZE) {
469 count = PAGE_SIZE - 1;
470 trunc = 1;
471 }
472 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700473
Al Viro16e5c1f2015-12-24 00:06:05 -0500474 data = memdup_user_nul(buf, count);
475 if (IS_ERR(data))
476 return PTR_ERR(data);
Casey Schauflere114e472008-02-04 22:29:50 -0800477
Rafal Krypa10289b02013-08-09 11:47:07 +0200478 /*
479 * In case of parsing only part of user buf,
480 * avoid having partial rule at the data buffer
481 */
482 if (trunc) {
483 while (count > 0 && (data[count - 1] != '\n'))
484 --count;
485 if (count == 0) {
486 rc = -EINVAL;
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200487 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200488 }
489 }
490
491 data[count] = '\0';
492 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
493 while (cnt < count) {
494 if (format == SMK_FIXED24_FMT) {
495 rc = smk_parse_rule(data, &rule, 1);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200496 if (rc < 0)
Rafal Krypa10289b02013-08-09 11:47:07 +0200497 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200498 cnt = count;
499 } else {
500 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200501 if (rc < 0)
502 goto out;
503 if (rc == 0) {
Rafal Krypa10289b02013-08-09 11:47:07 +0200504 rc = -EINVAL;
505 goto out;
506 }
507 cnt += rc;
508 }
509
510 if (rule_list == NULL)
511 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
Vishal Goel460d95a2019-03-07 16:55:24 +0530512 &rule.smk_subject->smk_rules_lock);
Rafal Krypa10289b02013-08-09 11:47:07 +0200513 else
Vishal Goel460d95a2019-03-07 16:55:24 +0530514 rc = smk_set_access(&rule, rule_list, rule_lock);
Rafal Krypa10289b02013-08-09 11:47:07 +0200515
516 if (rc)
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200517 goto out;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700518 }
519
Rafal Krypa10289b02013-08-09 11:47:07 +0200520 rc = cnt;
Casey Schauflere114e472008-02-04 22:29:50 -0800521out:
522 kfree(data);
523 return rc;
524}
525
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800526/*
Casey Schaufler40809562011-11-10 15:02:22 -0800527 * Core logic for smackfs seq list operations.
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800528 */
529
Casey Schaufler40809562011-11-10 15:02:22 -0800530static void *smk_seq_start(struct seq_file *s, loff_t *pos,
531 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800532{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700533 struct list_head *list;
Rafal Krypa01fa8472015-05-21 18:24:31 +0200534 int i = *pos;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700535
Rafal Krypa01fa8472015-05-21 18:24:31 +0200536 rcu_read_lock();
537 for (list = rcu_dereference(list_next_rcu(head));
538 list != head;
539 list = rcu_dereference(list_next_rcu(list))) {
540 if (i-- == 0)
541 return list;
542 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700543
Rafal Krypa01fa8472015-05-21 18:24:31 +0200544 return NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800545}
546
Casey Schaufler40809562011-11-10 15:02:22 -0800547static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
548 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800549{
550 struct list_head *list = v;
551
Rafal Krypa01fa8472015-05-21 18:24:31 +0200552 ++*pos;
553 list = rcu_dereference(list_next_rcu(list));
554
555 return (list == head) ? NULL : list;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800556}
557
Casey Schaufler40809562011-11-10 15:02:22 -0800558static void smk_seq_stop(struct seq_file *s, void *v)
559{
Rafal Krypa01fa8472015-05-21 18:24:31 +0200560 rcu_read_unlock();
Casey Schaufler40809562011-11-10 15:02:22 -0800561}
562
Casey Schauflerf7112e62012-05-06 15:22:02 -0700563static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
Casey Schaufler40809562011-11-10 15:02:22 -0800564{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700565 /*
566 * Don't show any rules with label names too long for
567 * interface file (/smack/load or /smack/load2)
568 * because you should expect to be able to write
569 * anything you read back.
570 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700571 if (strlen(srp->smk_subject->smk_known) >= max ||
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200572 strlen(srp->smk_object->smk_known) >= max)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700573 return;
Casey Schaufler40809562011-11-10 15:02:22 -0800574
Rafal Krypa65ee7f42012-07-09 19:36:34 +0200575 if (srp->smk_access == 0)
576 return;
577
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200578 seq_printf(s, "%s %s",
579 srp->smk_subject->smk_known,
580 srp->smk_object->smk_known);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800581
582 seq_putc(s, ' ');
583
584 if (srp->smk_access & MAY_READ)
585 seq_putc(s, 'r');
586 if (srp->smk_access & MAY_WRITE)
587 seq_putc(s, 'w');
588 if (srp->smk_access & MAY_EXEC)
589 seq_putc(s, 'x');
590 if (srp->smk_access & MAY_APPEND)
591 seq_putc(s, 'a');
592 if (srp->smk_access & MAY_TRANSMUTE)
593 seq_putc(s, 't');
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700594 if (srp->smk_access & MAY_LOCK)
595 seq_putc(s, 'l');
Casey Schauflerd166c802014-08-27 14:51:27 -0700596 if (srp->smk_access & MAY_BRINGUP)
597 seq_putc(s, 'b');
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800598
599 seq_putc(s, '\n');
Casey Schauflerf7112e62012-05-06 15:22:02 -0700600}
601
602/*
603 * Seq_file read operations for /smack/load
604 */
605
606static void *load2_seq_start(struct seq_file *s, loff_t *pos)
607{
Vishal Goel460d95a2019-03-07 16:55:24 +0530608 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700609}
610
611static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
612{
Vishal Goel460d95a2019-03-07 16:55:24 +0530613 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700614}
615
616static int load_seq_show(struct seq_file *s, void *v)
617{
618 struct list_head *list = v;
Vishal Goel460d95a2019-03-07 16:55:24 +0530619 struct smack_rule *srp;
620 struct smack_known *skp =
621 list_entry_rcu(list, struct smack_known, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700622
Vishal Goel460d95a2019-03-07 16:55:24 +0530623 list_for_each_entry_rcu(srp, &skp->smk_rules, list)
624 smk_rule_show(s, srp, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800625
626 return 0;
627}
628
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800629static const struct seq_operations load_seq_ops = {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700630 .start = load2_seq_start,
631 .next = load2_seq_next,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800632 .show = load_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800633 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800634};
635
636/**
637 * smk_open_load - open() for /smack/load
638 * @inode: inode structure representing file
639 * @file: "load" file pointer
640 *
641 * For reading, use load_seq_* seq_file reading operations.
642 */
643static int smk_open_load(struct inode *inode, struct file *file)
644{
645 return seq_open(file, &load_seq_ops);
646}
647
648/**
649 * smk_write_load - write() for /smack/load
650 * @file: file pointer, not actually used
651 * @buf: where to get the data from
652 * @count: bytes sent
653 * @ppos: where to start - must be 0
654 *
655 */
656static ssize_t smk_write_load(struct file *file, const char __user *buf,
657 size_t count, loff_t *ppos)
658{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800659 /*
660 * Must have privilege.
661 * No partial writes.
662 * Enough data must be present.
663 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700664 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800665 return -EPERM;
666
Casey Schauflerf7112e62012-05-06 15:22:02 -0700667 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
668 SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800669}
670
Casey Schauflere114e472008-02-04 22:29:50 -0800671static const struct file_operations smk_load_ops = {
672 .open = smk_open_load,
673 .read = seq_read,
674 .llseek = seq_lseek,
675 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700676 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800677};
678
679/**
680 * smk_cipso_doi - initialize the CIPSO domain
681 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700682static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800683{
684 int rc;
685 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500686 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800687
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500688 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800689
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500690 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800691 if (rc != 0)
692 printk(KERN_WARNING "%s:%d remove rc = %d\n",
693 __func__, __LINE__, rc);
694
695 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
696 if (doip == NULL)
697 panic("smack: Failed to initialize cipso DOI.\n");
698 doip->map.std = NULL;
699 doip->doi = smk_cipso_doi_value;
700 doip->type = CIPSO_V4_MAP_PASS;
701 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
702 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
703 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
704
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500705 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400706 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500707 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800708 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400709 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500710 return;
711 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500712 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500713 if (rc != 0) {
714 printk(KERN_WARNING "%s:%d map add rc = %d\n",
715 __func__, __LINE__, rc);
716 kfree(doip);
717 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400718 }
Casey Schauflere114e472008-02-04 22:29:50 -0800719}
720
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800721/**
722 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800723 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800724 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700725static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800726{
727 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500728 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800729
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500730 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800731
732 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500733 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800734 if (rc != 0)
735 printk(KERN_WARNING "%s:%d remove rc = %d\n",
736 __func__, __LINE__, rc);
737 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700738 if (smack_net_ambient == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700739 smack_net_ambient = &smack_known_floor;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800740
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700741 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500742 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800743 if (rc != 0)
744 printk(KERN_WARNING "%s:%d add rc = %d\n",
745 __func__, __LINE__, rc);
746}
747
Casey Schauflere114e472008-02-04 22:29:50 -0800748/*
749 * Seq_file read operations for /smack/cipso
750 */
751
752static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
753{
Casey Schaufler40809562011-11-10 15:02:22 -0800754 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800755}
756
757static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
758{
Casey Schaufler40809562011-11-10 15:02:22 -0800759 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800760}
761
762/*
763 * Print cipso labels in format:
764 * label level[/cat[,cat]]
765 */
766static int cipso_seq_show(struct seq_file *s, void *v)
767{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100768 struct list_head *list = v;
769 struct smack_known *skp =
Rafal Krypa01fa8472015-05-21 18:24:31 +0200770 list_entry_rcu(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400771 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800772 char sep = '/';
Casey Schauflere114e472008-02-04 22:29:50 -0800773 int i;
Casey Schauflere114e472008-02-04 22:29:50 -0800774
Casey Schauflerf7112e62012-05-06 15:22:02 -0700775 /*
776 * Don't show a label that could not have been set using
777 * /smack/cipso. This is in support of the notion that
778 * anything read from /smack/cipso ought to be writeable
779 * to /smack/cipso.
780 *
781 * /smack/cipso2 should be used instead.
782 */
783 if (strlen(skp->smk_known) >= SMK_LABELLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800784 return 0;
785
Casey Schauflerf7112e62012-05-06 15:22:02 -0700786 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
Casey Schauflere114e472008-02-04 22:29:50 -0800787
Paul Moore4fbe63d2014-08-01 11:17:37 -0400788 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
789 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700790 seq_printf(s, "%c%d", sep, i);
791 sep = ',';
792 }
Casey Schauflere114e472008-02-04 22:29:50 -0800793
794 seq_putc(s, '\n');
795
796 return 0;
797}
798
James Morris88e9d342009-09-22 16:43:43 -0700799static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800800 .start = cipso_seq_start,
Casey Schauflere114e472008-02-04 22:29:50 -0800801 .next = cipso_seq_next,
802 .show = cipso_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800803 .stop = smk_seq_stop,
Casey Schauflere114e472008-02-04 22:29:50 -0800804};
805
806/**
807 * smk_open_cipso - open() for /smack/cipso
808 * @inode: inode structure representing file
809 * @file: "cipso" file pointer
810 *
811 * Connect our cipso_seq_* operations with /smack/cipso
812 * file_operations
813 */
814static int smk_open_cipso(struct inode *inode, struct file *file)
815{
816 return seq_open(file, &cipso_seq_ops);
817}
818
819/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700820 * smk_set_cipso - do the work for write() for cipso and cipso2
Randy Dunlap251a2a92009-02-18 11:42:33 -0800821 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800822 * @buf: where to get the data from
823 * @count: bytes sent
824 * @ppos: where to start
Casey Schauflerf7112e62012-05-06 15:22:02 -0700825 * @format: /smack/cipso or /smack/cipso2
Casey Schauflere114e472008-02-04 22:29:50 -0800826 *
827 * Accepts only one cipso rule per write call.
828 * Returns number of bytes written or error code, as appropriate
829 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700830static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
831 size_t count, loff_t *ppos, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800832{
833 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700834 struct netlbl_lsm_secattr ncats;
835 char mapcatset[SMK_CIPSOLEN];
Casey Schauflere114e472008-02-04 22:29:50 -0800836 int maplevel;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700837 unsigned int cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800838 int catlen;
839 ssize_t rc = -EINVAL;
840 char *data = NULL;
841 char *rule;
842 int ret;
843 int i;
844
845 /*
846 * Must have privilege.
847 * No partial writes.
848 * Enough data must be present.
849 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700850 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -0800851 return -EPERM;
852 if (*ppos != 0)
853 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700854 if (format == SMK_FIXED24_FMT &&
855 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
Casey Schauflere114e472008-02-04 22:29:50 -0800856 return -EINVAL;
857
Al Viro16e5c1f2015-12-24 00:06:05 -0500858 data = memdup_user_nul(buf, count);
859 if (IS_ERR(data))
860 return PTR_ERR(data);
Casey Schauflere114e472008-02-04 22:29:50 -0800861
Casey Schauflere114e472008-02-04 22:29:50 -0800862 rule = data;
863 /*
864 * Only allow one writer at a time. Writes should be
865 * quite rare and small in any case.
866 */
867 mutex_lock(&smack_cipso_lock);
868
869 skp = smk_import_entry(rule, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200870 if (IS_ERR(skp)) {
871 rc = PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800872 goto out;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200873 }
Casey Schauflere114e472008-02-04 22:29:50 -0800874
Casey Schauflerf7112e62012-05-06 15:22:02 -0700875 if (format == SMK_FIXED24_FMT)
876 rule += SMK_LABELLEN;
877 else
Passion,Zhao0fcfee62013-06-03 11:42:24 +0800878 rule += strlen(skp->smk_known) + 1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700879
Casey Schauflere114e472008-02-04 22:29:50 -0800880 ret = sscanf(rule, "%d", &maplevel);
881 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
882 goto out;
883
884 rule += SMK_DIGITLEN;
885 ret = sscanf(rule, "%d", &catlen);
886 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
887 goto out;
888
Casey Schauflerf7112e62012-05-06 15:22:02 -0700889 if (format == SMK_FIXED24_FMT &&
890 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800891 goto out;
892
893 memset(mapcatset, 0, sizeof(mapcatset));
894
895 for (i = 0; i < catlen; i++) {
896 rule += SMK_DIGITLEN;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700897 ret = sscanf(rule, "%u", &cat);
Casey Schaufler677264e2013-06-28 13:47:07 -0700898 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
Casey Schauflere114e472008-02-04 22:29:50 -0800899 goto out;
900
901 smack_catset_bit(cat, mapcatset);
902 }
903
Casey Schauflerf7112e62012-05-06 15:22:02 -0700904 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
905 if (rc >= 0) {
Paul Moore4fbe63d2014-08-01 11:17:37 -0400906 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700907 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
908 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
909 rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -0800910 }
911
Casey Schauflere114e472008-02-04 22:29:50 -0800912out:
913 mutex_unlock(&smack_cipso_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800914 kfree(data);
915 return rc;
916}
917
Casey Schauflerf7112e62012-05-06 15:22:02 -0700918/**
919 * smk_write_cipso - write() for /smack/cipso
920 * @file: file pointer, not actually used
921 * @buf: where to get the data from
922 * @count: bytes sent
923 * @ppos: where to start
924 *
925 * Accepts only one cipso rule per write call.
926 * Returns number of bytes written or error code, as appropriate
927 */
928static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
929 size_t count, loff_t *ppos)
930{
931 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
932}
933
Casey Schauflere114e472008-02-04 22:29:50 -0800934static const struct file_operations smk_cipso_ops = {
935 .open = smk_open_cipso,
936 .read = seq_read,
937 .llseek = seq_lseek,
938 .write = smk_write_cipso,
939 .release = seq_release,
940};
941
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500942/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700943 * Seq_file read operations for /smack/cipso2
944 */
945
946/*
947 * Print cipso labels in format:
948 * label level[/cat[,cat]]
949 */
950static int cipso2_seq_show(struct seq_file *s, void *v)
951{
952 struct list_head *list = v;
953 struct smack_known *skp =
Rafal Krypa01fa8472015-05-21 18:24:31 +0200954 list_entry_rcu(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400955 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700956 char sep = '/';
957 int i;
958
959 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
960
Paul Moore4fbe63d2014-08-01 11:17:37 -0400961 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
962 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700963 seq_printf(s, "%c%d", sep, i);
964 sep = ',';
965 }
966
967 seq_putc(s, '\n');
968
969 return 0;
970}
971
972static const struct seq_operations cipso2_seq_ops = {
973 .start = cipso_seq_start,
974 .next = cipso_seq_next,
975 .show = cipso2_seq_show,
976 .stop = smk_seq_stop,
977};
978
979/**
980 * smk_open_cipso2 - open() for /smack/cipso2
981 * @inode: inode structure representing file
982 * @file: "cipso2" file pointer
983 *
984 * Connect our cipso_seq_* operations with /smack/cipso2
985 * file_operations
986 */
987static int smk_open_cipso2(struct inode *inode, struct file *file)
988{
989 return seq_open(file, &cipso2_seq_ops);
990}
991
992/**
993 * smk_write_cipso2 - write() for /smack/cipso2
994 * @file: file pointer, not actually used
995 * @buf: where to get the data from
996 * @count: bytes sent
997 * @ppos: where to start
998 *
999 * Accepts only one cipso rule per write call.
1000 * Returns number of bytes written or error code, as appropriate
1001 */
1002static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1003 size_t count, loff_t *ppos)
1004{
1005 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1006}
1007
1008static const struct file_operations smk_cipso2_ops = {
1009 .open = smk_open_cipso2,
1010 .read = seq_read,
1011 .llseek = seq_lseek,
1012 .write = smk_write_cipso2,
1013 .release = seq_release,
1014};
1015
1016/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001017 * Seq_file read operations for /smack/netlabel
1018 */
1019
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001020static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001021{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001022 return smk_seq_start(s, pos, &smk_net4addr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001023}
1024
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001025static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001026{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001027 return smk_seq_next(s, v, pos, &smk_net4addr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001028}
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001029
1030/*
1031 * Print host/label pairs
1032 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001033static int net4addr_seq_show(struct seq_file *s, void *v)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001034{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001035 struct list_head *list = v;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001036 struct smk_net4addr *skp =
1037 list_entry_rcu(list, struct smk_net4addr, list);
1038 char *kp = SMACK_CIPSO_OPTION;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001039
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001040 if (skp->smk_label != NULL)
1041 kp = skp->smk_label->smk_known;
1042 seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
1043 skp->smk_masks, kp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001044
1045 return 0;
1046}
1047
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001048static const struct seq_operations net4addr_seq_ops = {
1049 .start = net4addr_seq_start,
1050 .next = net4addr_seq_next,
1051 .show = net4addr_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001052 .stop = smk_seq_stop,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001053};
1054
1055/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001056 * smk_open_net4addr - open() for /smack/netlabel
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001057 * @inode: inode structure representing file
1058 * @file: "netlabel" file pointer
1059 *
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001060 * Connect our net4addr_seq_* operations with /smack/netlabel
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001061 * file_operations
1062 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001063static int smk_open_net4addr(struct inode *inode, struct file *file)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001064{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001065 return seq_open(file, &net4addr_seq_ops);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001066}
1067
1068/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001069 * smk_net4addr_insert
etienne113a0e42009-03-04 07:33:51 +01001070 * @new : netlabel to insert
1071 *
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001072 * This helper insert netlabel in the smack_net4addrs list
etienne113a0e42009-03-04 07:33:51 +01001073 * sorted by netmask length (longest to smallest)
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001074 * locked by &smk_net4addr_lock in smk_write_net4addr
Etienne Basset7198e2e2009-03-24 20:53:24 +01001075 *
etienne113a0e42009-03-04 07:33:51 +01001076 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001077static void smk_net4addr_insert(struct smk_net4addr *new)
etienne113a0e42009-03-04 07:33:51 +01001078{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001079 struct smk_net4addr *m;
1080 struct smk_net4addr *m_next;
etienne113a0e42009-03-04 07:33:51 +01001081
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001082 if (list_empty(&smk_net4addr_list)) {
1083 list_add_rcu(&new->list, &smk_net4addr_list);
etienne113a0e42009-03-04 07:33:51 +01001084 return;
1085 }
1086
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001087 m = list_entry_rcu(smk_net4addr_list.next,
1088 struct smk_net4addr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001089
etienne113a0e42009-03-04 07:33:51 +01001090 /* the comparison '>' is a bit hacky, but works */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001091 if (new->smk_masks > m->smk_masks) {
1092 list_add_rcu(&new->list, &smk_net4addr_list);
etienne113a0e42009-03-04 07:33:51 +01001093 return;
1094 }
Etienne Basset7198e2e2009-03-24 20:53:24 +01001095
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001096 list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
1097 if (list_is_last(&m->list, &smk_net4addr_list)) {
Etienne Basset7198e2e2009-03-24 20:53:24 +01001098 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001099 return;
1100 }
Jiri Pirko05725f72009-04-14 20:17:16 +02001101 m_next = list_entry_rcu(m->list.next,
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001102 struct smk_net4addr, list);
1103 if (new->smk_masks > m_next->smk_masks) {
Etienne Basset7198e2e2009-03-24 20:53:24 +01001104 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001105 return;
1106 }
1107 }
1108}
1109
1110
1111/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001112 * smk_write_net4addr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -08001113 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001114 * @buf: where to get the data from
1115 * @count: bytes sent
1116 * @ppos: where to start
1117 *
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001118 * Accepts only one net4addr per write call.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001119 * Returns number of bytes written or error code, as appropriate
1120 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001121static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001122 size_t count, loff_t *ppos)
1123{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001124 struct smk_net4addr *snp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001125 struct sockaddr_in newname;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001126 char *smack;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001127 struct smack_known *skp = NULL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001128 char *data;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001129 char *host = (char *)&newname.sin_addr.s_addr;
1130 int rc;
1131 struct netlbl_audit audit_info;
1132 struct in_addr mask;
1133 unsigned int m;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001134 unsigned int masks;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001135 int found;
etienne113a0e42009-03-04 07:33:51 +01001136 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001137 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +01001138 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001139
1140 /*
1141 * Must have privilege.
1142 * No partial writes.
1143 * Enough data must be present.
1144 * "<addr/mask, as a.b.c.d/e><space><label>"
1145 * "<addr, as a.b.c.d><space><label>"
1146 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001147 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001148 return -EPERM;
1149 if (*ppos != 0)
1150 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001151 if (count < SMK_NETLBLADDRMIN)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001152 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001153
Al Viro16e5c1f2015-12-24 00:06:05 -05001154 data = memdup_user_nul(buf, count);
1155 if (IS_ERR(data))
1156 return PTR_ERR(data);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001157
1158 smack = kzalloc(count + 1, GFP_KERNEL);
1159 if (smack == NULL) {
1160 rc = -ENOMEM;
1161 goto free_data_out;
1162 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001163
Toralf Försterec554fa2014-04-27 19:33:34 +02001164 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001165 &host[0], &host[1], &host[2], &host[3], &masks, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001166 if (rc != 6) {
1167 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1168 &host[0], &host[1], &host[2], &host[3], smack);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001169 if (rc != 5) {
1170 rc = -EINVAL;
1171 goto free_out;
1172 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001173 m = BEBITS;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001174 masks = 32;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001175 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001176 if (masks > BEBITS) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001177 rc = -EINVAL;
1178 goto free_out;
1179 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001180
Casey Schauflerf7112e62012-05-06 15:22:02 -07001181 /*
1182 * If smack begins with '-', it is an option, don't import it
1183 */
Etienne Basset43031542009-03-27 17:11:01 -04001184 if (smack[0] != '-') {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001185 skp = smk_import_entry(smack, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001186 if (IS_ERR(skp)) {
1187 rc = PTR_ERR(skp);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001188 goto free_out;
1189 }
Etienne Basset43031542009-03-27 17:11:01 -04001190 } else {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001191 /*
1192 * Only the -CIPSO option is supported for IPv4
1193 */
1194 if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001195 rc = -EINVAL;
1196 goto free_out;
1197 }
Etienne Basset43031542009-03-27 17:11:01 -04001198 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001199
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001200 for (m = masks, temp_mask = 0; m > 0; m--) {
etienne113a0e42009-03-04 07:33:51 +01001201 temp_mask |= mask_bits;
1202 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001203 }
etienne113a0e42009-03-04 07:33:51 +01001204 mask.s_addr = cpu_to_be32(temp_mask);
1205
1206 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001207 /*
1208 * Only allow one writer at a time. Writes should be
1209 * quite rare and small in any case.
1210 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001211 mutex_lock(&smk_net4addr_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001212
1213 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001214 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001215 found = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001216 list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
1217 if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
Etienne Basset7198e2e2009-03-24 20:53:24 +01001218 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001219 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001220 }
1221 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001222 smk_netlabel_audit_set(&audit_info);
1223
Etienne Basset7198e2e2009-03-24 20:53:24 +01001224 if (found == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001225 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1226 if (snp == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001227 rc = -ENOMEM;
1228 else {
1229 rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001230 snp->smk_host.s_addr = newname.sin_addr.s_addr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001231 snp->smk_mask.s_addr = mask.s_addr;
1232 snp->smk_label = skp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001233 snp->smk_masks = masks;
1234 smk_net4addr_insert(snp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001235 }
1236 } else {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001237 /*
1238 * Delete the unlabeled entry, only if the previous label
1239 * wasn't the special CIPSO option
1240 */
1241 if (snp->smk_label != NULL)
Etienne Basset43031542009-03-27 17:11:01 -04001242 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001243 &snp->smk_host, &snp->smk_mask,
Etienne Basset43031542009-03-27 17:11:01 -04001244 PF_INET, &audit_info);
1245 else
1246 rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001247 snp->smk_label = skp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001248 }
1249
1250 /*
1251 * Now tell netlabel about the single label nature of
1252 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -04001253 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001254 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001255 if (rc == 0 && skp != NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001256 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001257 &snp->smk_host, &snp->smk_mask, PF_INET,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001258 snp->smk_label->smk_secid, &audit_info);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001259
1260 if (rc == 0)
1261 rc = count;
1262
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001263 mutex_unlock(&smk_net4addr_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001264
Casey Schauflerf7112e62012-05-06 15:22:02 -07001265free_out:
1266 kfree(smack);
1267free_data_out:
1268 kfree(data);
1269
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001270 return rc;
1271}
1272
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001273static const struct file_operations smk_net4addr_ops = {
1274 .open = smk_open_net4addr,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001275 .read = seq_read,
1276 .llseek = seq_lseek,
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001277 .write = smk_write_net4addr,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001278 .release = seq_release,
1279};
1280
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001281#if IS_ENABLED(CONFIG_IPV6)
1282/*
1283 * Seq_file read operations for /smack/netlabel6
1284 */
1285
1286static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
1287{
1288 return smk_seq_start(s, pos, &smk_net6addr_list);
1289}
1290
1291static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1292{
1293 return smk_seq_next(s, v, pos, &smk_net6addr_list);
1294}
1295
1296/*
1297 * Print host/label pairs
1298 */
1299static int net6addr_seq_show(struct seq_file *s, void *v)
1300{
1301 struct list_head *list = v;
1302 struct smk_net6addr *skp =
1303 list_entry(list, struct smk_net6addr, list);
1304
1305 if (skp->smk_label != NULL)
1306 seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
1307 skp->smk_label->smk_known);
1308
1309 return 0;
1310}
1311
1312static const struct seq_operations net6addr_seq_ops = {
1313 .start = net6addr_seq_start,
1314 .next = net6addr_seq_next,
1315 .show = net6addr_seq_show,
1316 .stop = smk_seq_stop,
1317};
1318
1319/**
1320 * smk_open_net6addr - open() for /smack/netlabel
1321 * @inode: inode structure representing file
1322 * @file: "netlabel" file pointer
1323 *
1324 * Connect our net6addr_seq_* operations with /smack/netlabel
1325 * file_operations
1326 */
1327static int smk_open_net6addr(struct inode *inode, struct file *file)
1328{
1329 return seq_open(file, &net6addr_seq_ops);
1330}
1331
1332/**
1333 * smk_net6addr_insert
1334 * @new : entry to insert
1335 *
1336 * This inserts an entry in the smack_net6addrs list
1337 * sorted by netmask length (longest to smallest)
1338 * locked by &smk_net6addr_lock in smk_write_net6addr
1339 *
1340 */
1341static void smk_net6addr_insert(struct smk_net6addr *new)
1342{
1343 struct smk_net6addr *m_next;
1344 struct smk_net6addr *m;
1345
1346 if (list_empty(&smk_net6addr_list)) {
1347 list_add_rcu(&new->list, &smk_net6addr_list);
1348 return;
1349 }
1350
1351 m = list_entry_rcu(smk_net6addr_list.next,
1352 struct smk_net6addr, list);
1353
1354 if (new->smk_masks > m->smk_masks) {
1355 list_add_rcu(&new->list, &smk_net6addr_list);
1356 return;
1357 }
1358
1359 list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
1360 if (list_is_last(&m->list, &smk_net6addr_list)) {
1361 list_add_rcu(&new->list, &m->list);
1362 return;
1363 }
1364 m_next = list_entry_rcu(m->list.next,
1365 struct smk_net6addr, list);
1366 if (new->smk_masks > m_next->smk_masks) {
1367 list_add_rcu(&new->list, &m->list);
1368 return;
1369 }
1370 }
1371}
1372
1373
1374/**
1375 * smk_write_net6addr - write() for /smack/netlabel
1376 * @file: file pointer, not actually used
1377 * @buf: where to get the data from
1378 * @count: bytes sent
1379 * @ppos: where to start
1380 *
1381 * Accepts only one net6addr per write call.
1382 * Returns number of bytes written or error code, as appropriate
1383 */
1384static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
1385 size_t count, loff_t *ppos)
1386{
1387 struct smk_net6addr *snp;
1388 struct in6_addr newname;
1389 struct in6_addr fullmask;
1390 struct smack_known *skp = NULL;
1391 char *smack;
1392 char *data;
1393 int rc = 0;
1394 int found = 0;
1395 int i;
1396 unsigned int scanned[8];
1397 unsigned int m;
1398 unsigned int mask = 128;
1399
1400 /*
1401 * Must have privilege.
1402 * No partial writes.
1403 * Enough data must be present.
1404 * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
1405 * "<addr, as a:b:c:d:e:f:g:h><space><label>"
1406 */
1407 if (!smack_privileged(CAP_MAC_ADMIN))
1408 return -EPERM;
1409 if (*ppos != 0)
1410 return -EINVAL;
1411 if (count < SMK_NETLBLADDRMIN)
1412 return -EINVAL;
1413
Al Viro16e5c1f2015-12-24 00:06:05 -05001414 data = memdup_user_nul(buf, count);
1415 if (IS_ERR(data))
1416 return PTR_ERR(data);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001417
1418 smack = kzalloc(count + 1, GFP_KERNEL);
1419 if (smack == NULL) {
1420 rc = -ENOMEM;
1421 goto free_data_out;
1422 }
1423
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001424 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
1425 &scanned[0], &scanned[1], &scanned[2], &scanned[3],
1426 &scanned[4], &scanned[5], &scanned[6], &scanned[7],
1427 &mask, smack);
1428 if (i != 10) {
1429 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
1430 &scanned[0], &scanned[1], &scanned[2],
1431 &scanned[3], &scanned[4], &scanned[5],
1432 &scanned[6], &scanned[7], smack);
1433 if (i != 9) {
1434 rc = -EINVAL;
1435 goto free_out;
1436 }
1437 }
1438 if (mask > 128) {
1439 rc = -EINVAL;
1440 goto free_out;
1441 }
1442 for (i = 0; i < 8; i++) {
1443 if (scanned[i] > 0xffff) {
1444 rc = -EINVAL;
1445 goto free_out;
1446 }
1447 newname.s6_addr16[i] = htons(scanned[i]);
1448 }
1449
1450 /*
1451 * If smack begins with '-', it is an option, don't import it
1452 */
1453 if (smack[0] != '-') {
1454 skp = smk_import_entry(smack, 0);
Lukasz Pawelczyk5f2bfe22015-08-25 12:39:46 +02001455 if (IS_ERR(skp)) {
1456 rc = PTR_ERR(skp);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001457 goto free_out;
1458 }
1459 } else {
1460 /*
1461 * Only -DELETE is supported for IPv6
1462 */
1463 if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
1464 rc = -EINVAL;
1465 goto free_out;
1466 }
1467 }
1468
1469 for (i = 0, m = mask; i < 8; i++) {
1470 if (m >= 16) {
1471 fullmask.s6_addr16[i] = 0xffff;
1472 m -= 16;
1473 } else if (m > 0) {
1474 fullmask.s6_addr16[i] = (1 << m) - 1;
1475 m = 0;
1476 } else
1477 fullmask.s6_addr16[i] = 0;
1478 newname.s6_addr16[i] &= fullmask.s6_addr16[i];
1479 }
1480
1481 /*
1482 * Only allow one writer at a time. Writes should be
1483 * quite rare and small in any case.
1484 */
1485 mutex_lock(&smk_net6addr_lock);
1486 /*
1487 * Try to find the prefix in the list
1488 */
1489 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
1490 if (mask != snp->smk_masks)
1491 continue;
1492 for (found = 1, i = 0; i < 8; i++) {
1493 if (newname.s6_addr16[i] !=
1494 snp->smk_host.s6_addr16[i]) {
1495 found = 0;
1496 break;
1497 }
1498 }
1499 if (found == 1)
1500 break;
1501 }
1502 if (found == 0) {
1503 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1504 if (snp == NULL)
1505 rc = -ENOMEM;
1506 else {
1507 snp->smk_host = newname;
1508 snp->smk_mask = fullmask;
1509 snp->smk_masks = mask;
1510 snp->smk_label = skp;
1511 smk_net6addr_insert(snp);
1512 }
1513 } else {
1514 snp->smk_label = skp;
1515 }
1516
1517 if (rc == 0)
1518 rc = count;
1519
1520 mutex_unlock(&smk_net6addr_lock);
1521
1522free_out:
1523 kfree(smack);
1524free_data_out:
1525 kfree(data);
1526
1527 return rc;
1528}
1529
1530static const struct file_operations smk_net6addr_ops = {
1531 .open = smk_open_net6addr,
1532 .read = seq_read,
1533 .llseek = seq_lseek,
1534 .write = smk_write_net6addr,
1535 .release = seq_release,
1536};
1537#endif /* CONFIG_IPV6 */
1538
Casey Schauflere114e472008-02-04 22:29:50 -08001539/**
1540 * smk_read_doi - read() for /smack/doi
1541 * @filp: file pointer, not actually used
1542 * @buf: where to put the result
1543 * @count: maximum to send along
1544 * @ppos: where to start
1545 *
1546 * Returns number of bytes read or error code, as appropriate
1547 */
1548static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1549 size_t count, loff_t *ppos)
1550{
1551 char temp[80];
1552 ssize_t rc;
1553
1554 if (*ppos != 0)
1555 return 0;
1556
1557 sprintf(temp, "%d", smk_cipso_doi_value);
1558 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1559
1560 return rc;
1561}
1562
1563/**
1564 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001565 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001566 * @buf: where to get the data from
1567 * @count: bytes sent
1568 * @ppos: where to start
1569 *
1570 * Returns number of bytes written or error code, as appropriate
1571 */
1572static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1573 size_t count, loff_t *ppos)
1574{
1575 char temp[80];
1576 int i;
1577
Casey Schaufler1880eff2012-06-05 15:28:30 -07001578 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001579 return -EPERM;
1580
1581 if (count >= sizeof(temp) || count == 0)
1582 return -EINVAL;
1583
1584 if (copy_from_user(temp, buf, count) != 0)
1585 return -EFAULT;
1586
1587 temp[count] = '\0';
1588
1589 if (sscanf(temp, "%d", &i) != 1)
1590 return -EINVAL;
1591
1592 smk_cipso_doi_value = i;
1593
1594 smk_cipso_doi();
1595
1596 return count;
1597}
1598
1599static const struct file_operations smk_doi_ops = {
1600 .read = smk_read_doi,
1601 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001602 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001603};
1604
1605/**
1606 * smk_read_direct - read() for /smack/direct
1607 * @filp: file pointer, not actually used
1608 * @buf: where to put the result
1609 * @count: maximum to send along
1610 * @ppos: where to start
1611 *
1612 * Returns number of bytes read or error code, as appropriate
1613 */
1614static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1615 size_t count, loff_t *ppos)
1616{
1617 char temp[80];
1618 ssize_t rc;
1619
1620 if (*ppos != 0)
1621 return 0;
1622
1623 sprintf(temp, "%d", smack_cipso_direct);
1624 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1625
1626 return rc;
1627}
1628
1629/**
1630 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001631 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001632 * @buf: where to get the data from
1633 * @count: bytes sent
1634 * @ppos: where to start
1635 *
1636 * Returns number of bytes written or error code, as appropriate
1637 */
1638static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1639 size_t count, loff_t *ppos)
1640{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001641 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001642 char temp[80];
1643 int i;
1644
Casey Schaufler1880eff2012-06-05 15:28:30 -07001645 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001646 return -EPERM;
1647
1648 if (count >= sizeof(temp) || count == 0)
1649 return -EINVAL;
1650
1651 if (copy_from_user(temp, buf, count) != 0)
1652 return -EFAULT;
1653
1654 temp[count] = '\0';
1655
1656 if (sscanf(temp, "%d", &i) != 1)
1657 return -EINVAL;
1658
Casey Schauflerf7112e62012-05-06 15:22:02 -07001659 /*
1660 * Don't do anything if the value hasn't actually changed.
1661 * If it is changing reset the level on entries that were
1662 * set up to be direct when they were created.
1663 */
1664 if (smack_cipso_direct != i) {
1665 mutex_lock(&smack_known_lock);
1666 list_for_each_entry_rcu(skp, &smack_known_list, list)
1667 if (skp->smk_netlabel.attr.mls.lvl ==
1668 smack_cipso_direct)
1669 skp->smk_netlabel.attr.mls.lvl = i;
1670 smack_cipso_direct = i;
1671 mutex_unlock(&smack_known_lock);
1672 }
Casey Schauflere114e472008-02-04 22:29:50 -08001673
1674 return count;
1675}
1676
1677static const struct file_operations smk_direct_ops = {
1678 .read = smk_read_direct,
1679 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001680 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001681};
1682
1683/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001684 * smk_read_mapped - read() for /smack/mapped
1685 * @filp: file pointer, not actually used
1686 * @buf: where to put the result
1687 * @count: maximum to send along
1688 * @ppos: where to start
1689 *
1690 * Returns number of bytes read or error code, as appropriate
1691 */
1692static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1693 size_t count, loff_t *ppos)
1694{
1695 char temp[80];
1696 ssize_t rc;
1697
1698 if (*ppos != 0)
1699 return 0;
1700
1701 sprintf(temp, "%d", smack_cipso_mapped);
1702 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1703
1704 return rc;
1705}
1706
1707/**
1708 * smk_write_mapped - write() for /smack/mapped
1709 * @file: file pointer, not actually used
1710 * @buf: where to get the data from
1711 * @count: bytes sent
1712 * @ppos: where to start
1713 *
1714 * Returns number of bytes written or error code, as appropriate
1715 */
1716static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1717 size_t count, loff_t *ppos)
1718{
1719 struct smack_known *skp;
1720 char temp[80];
1721 int i;
1722
Casey Schaufler1880eff2012-06-05 15:28:30 -07001723 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001724 return -EPERM;
1725
1726 if (count >= sizeof(temp) || count == 0)
1727 return -EINVAL;
1728
1729 if (copy_from_user(temp, buf, count) != 0)
1730 return -EFAULT;
1731
1732 temp[count] = '\0';
1733
1734 if (sscanf(temp, "%d", &i) != 1)
1735 return -EINVAL;
1736
1737 /*
1738 * Don't do anything if the value hasn't actually changed.
1739 * If it is changing reset the level on entries that were
1740 * set up to be mapped when they were created.
1741 */
1742 if (smack_cipso_mapped != i) {
1743 mutex_lock(&smack_known_lock);
1744 list_for_each_entry_rcu(skp, &smack_known_list, list)
1745 if (skp->smk_netlabel.attr.mls.lvl ==
1746 smack_cipso_mapped)
1747 skp->smk_netlabel.attr.mls.lvl = i;
1748 smack_cipso_mapped = i;
1749 mutex_unlock(&smack_known_lock);
1750 }
1751
1752 return count;
1753}
1754
1755static const struct file_operations smk_mapped_ops = {
1756 .read = smk_read_mapped,
1757 .write = smk_write_mapped,
1758 .llseek = default_llseek,
1759};
1760
1761/**
Casey Schauflere114e472008-02-04 22:29:50 -08001762 * smk_read_ambient - read() for /smack/ambient
1763 * @filp: file pointer, not actually used
1764 * @buf: where to put the result
1765 * @cn: maximum to send along
1766 * @ppos: where to start
1767 *
1768 * Returns number of bytes read or error code, as appropriate
1769 */
1770static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1771 size_t cn, loff_t *ppos)
1772{
1773 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001774 int asize;
1775
1776 if (*ppos != 0)
1777 return 0;
1778 /*
1779 * Being careful to avoid a problem in the case where
1780 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001781 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001782 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001783
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001784 asize = strlen(smack_net_ambient->smk_known) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001785
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001786 if (cn >= asize)
1787 rc = simple_read_from_buffer(buf, cn, ppos,
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001788 smack_net_ambient->smk_known,
1789 asize);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001790 else
1791 rc = -EINVAL;
1792
1793 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001794
1795 return rc;
1796}
1797
1798/**
1799 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001800 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001801 * @buf: where to get the data from
1802 * @count: bytes sent
1803 * @ppos: where to start
1804 *
1805 * Returns number of bytes written or error code, as appropriate
1806 */
1807static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1808 size_t count, loff_t *ppos)
1809{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001810 struct smack_known *skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001811 char *oldambient;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001812 char *data;
1813 int rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -08001814
Casey Schaufler1880eff2012-06-05 15:28:30 -07001815 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001816 return -EPERM;
1817
Al Viro16e5c1f2015-12-24 00:06:05 -05001818 data = memdup_user_nul(buf, count);
1819 if (IS_ERR(data))
1820 return PTR_ERR(data);
Casey Schauflere114e472008-02-04 22:29:50 -08001821
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001822 skp = smk_import_entry(data, count);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001823 if (IS_ERR(skp)) {
1824 rc = PTR_ERR(skp);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001825 goto out;
1826 }
Casey Schauflere114e472008-02-04 22:29:50 -08001827
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001828 mutex_lock(&smack_ambient_lock);
1829
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001830 oldambient = smack_net_ambient->smk_known;
1831 smack_net_ambient = skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001832 smk_unlbl_ambient(oldambient);
1833
1834 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001835
Casey Schauflerf7112e62012-05-06 15:22:02 -07001836out:
1837 kfree(data);
1838 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001839}
1840
1841static const struct file_operations smk_ambient_ops = {
1842 .read = smk_read_ambient,
1843 .write = smk_write_ambient,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001844 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001845};
1846
Rafal Krypac0d77c82015-06-02 11:23:48 +02001847/*
1848 * Seq_file operations for /smack/onlycap
Casey Schaufler15446232008-07-30 15:37:11 -07001849 */
Rafal Krypac0d77c82015-06-02 11:23:48 +02001850static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
Casey Schaufler15446232008-07-30 15:37:11 -07001851{
Rafal Krypac0d77c82015-06-02 11:23:48 +02001852 return smk_seq_start(s, pos, &smack_onlycap_list);
1853}
Casey Schaufler15446232008-07-30 15:37:11 -07001854
Rafal Krypac0d77c82015-06-02 11:23:48 +02001855static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
1856{
1857 return smk_seq_next(s, v, pos, &smack_onlycap_list);
1858}
Casey Schaufler15446232008-07-30 15:37:11 -07001859
Rafal Krypac0d77c82015-06-02 11:23:48 +02001860static int onlycap_seq_show(struct seq_file *s, void *v)
1861{
1862 struct list_head *list = v;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001863 struct smack_known_list_elem *sklep =
1864 list_entry_rcu(list, struct smack_known_list_elem, list);
Casey Schaufler15446232008-07-30 15:37:11 -07001865
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001866 seq_puts(s, sklep->smk_label->smk_known);
Rafal Krypac0d77c82015-06-02 11:23:48 +02001867 seq_putc(s, ' ');
Casey Schaufler15446232008-07-30 15:37:11 -07001868
Rafal Krypac0d77c82015-06-02 11:23:48 +02001869 return 0;
1870}
Casey Schaufler15446232008-07-30 15:37:11 -07001871
Rafal Krypac0d77c82015-06-02 11:23:48 +02001872static const struct seq_operations onlycap_seq_ops = {
1873 .start = onlycap_seq_start,
1874 .next = onlycap_seq_next,
1875 .show = onlycap_seq_show,
1876 .stop = smk_seq_stop,
1877};
1878
1879static int smk_open_onlycap(struct inode *inode, struct file *file)
1880{
1881 return seq_open(file, &onlycap_seq_ops);
1882}
1883
1884/**
1885 * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
1886 * The caller must hold appropriate mutex to prevent concurrent modifications
1887 * to the public list.
1888 * Private list is assumed to be not accessible to other threads yet.
1889 *
1890 * @public: public list
1891 * @private: private list
1892 */
1893static void smk_list_swap_rcu(struct list_head *public,
1894 struct list_head *private)
1895{
1896 struct list_head *first, *last;
1897
1898 if (list_empty(public)) {
1899 list_splice_init_rcu(private, public, synchronize_rcu);
1900 } else {
1901 /* Remember public list before replacing it */
1902 first = public->next;
1903 last = public->prev;
1904
1905 /* Publish private list in place of public in RCU-safe way */
1906 private->prev->next = public;
1907 private->next->prev = public;
1908 rcu_assign_pointer(public->next, private->next);
1909 public->prev = private->prev;
1910
1911 synchronize_rcu();
1912
1913 /* When all readers are done with the old public list,
1914 * attach it in place of private */
1915 private->next = first;
1916 private->prev = last;
1917 first->prev = private;
1918 last->next = private;
1919 }
Casey Schaufler15446232008-07-30 15:37:11 -07001920}
1921
1922/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001923 * smk_parse_label_list - parse list of Smack labels, separated by spaces
1924 *
1925 * @data: the string to parse
1926 * @private: destination list
1927 *
1928 * Returns zero on success or error code, as appropriate
1929 */
1930static int smk_parse_label_list(char *data, struct list_head *list)
1931{
1932 char *tok;
1933 struct smack_known *skp;
1934 struct smack_known_list_elem *sklep;
1935
1936 while ((tok = strsep(&data, " ")) != NULL) {
1937 if (!*tok)
1938 continue;
1939
1940 skp = smk_import_entry(tok, 0);
1941 if (IS_ERR(skp))
1942 return PTR_ERR(skp);
1943
1944 sklep = kzalloc(sizeof(*sklep), GFP_KERNEL);
1945 if (sklep == NULL)
1946 return -ENOMEM;
1947
1948 sklep->smk_label = skp;
1949 list_add(&sklep->list, list);
1950 }
1951
1952 return 0;
1953}
1954
1955/**
1956 * smk_destroy_label_list - destroy a list of smack_known_list_elem
1957 * @head: header pointer of the list to destroy
1958 */
1959void smk_destroy_label_list(struct list_head *list)
1960{
1961 struct smack_known_list_elem *sklep;
1962 struct smack_known_list_elem *sklep2;
1963
1964 list_for_each_entry_safe(sklep, sklep2, list, list)
1965 kfree(sklep);
1966
1967 INIT_LIST_HEAD(list);
1968}
1969
1970/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08001971 * smk_write_onlycap - write() for smackfs/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001972 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001973 * @buf: where to get the data from
1974 * @count: bytes sent
1975 * @ppos: where to start
1976 *
1977 * Returns number of bytes written or error code, as appropriate
1978 */
1979static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1980 size_t count, loff_t *ppos)
1981{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001982 char *data;
Rafal Krypac0d77c82015-06-02 11:23:48 +02001983 LIST_HEAD(list_tmp);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001984 int rc;
Casey Schaufler15446232008-07-30 15:37:11 -07001985
Casey Schaufler1880eff2012-06-05 15:28:30 -07001986 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler15446232008-07-30 15:37:11 -07001987 return -EPERM;
1988
Al Viro16e5c1f2015-12-24 00:06:05 -05001989 data = memdup_user_nul(buf, count);
1990 if (IS_ERR(data))
1991 return PTR_ERR(data);
Casey Schaufler15446232008-07-30 15:37:11 -07001992
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001993 rc = smk_parse_label_list(data, &list_tmp);
Rafal Krypac0d77c82015-06-02 11:23:48 +02001994 kfree(data);
1995
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001996 /*
1997 * Clear the smack_onlycap on invalid label errors. This means
1998 * that we can pass a null string to unset the onlycap value.
1999 *
2000 * Importing will also reject a label beginning with '-',
2001 * so "-usecapabilities" will also work.
2002 *
2003 * But do so only on invalid label, not on system errors.
Rafal Krypac0d77c82015-06-02 11:23:48 +02002004 * The invalid label must be first to count as clearing attempt.
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002005 */
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002006 if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
Rafal Krypac0d77c82015-06-02 11:23:48 +02002007 mutex_lock(&smack_onlycap_lock);
2008 smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
2009 mutex_unlock(&smack_onlycap_lock);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002010 rc = count;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002011 }
2012
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002013 smk_destroy_label_list(&list_tmp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002014
Casey Schauflerf7112e62012-05-06 15:22:02 -07002015 return rc;
Casey Schaufler15446232008-07-30 15:37:11 -07002016}
2017
2018static const struct file_operations smk_onlycap_ops = {
Rafal Krypac0d77c82015-06-02 11:23:48 +02002019 .open = smk_open_onlycap,
2020 .read = seq_read,
Casey Schaufler15446232008-07-30 15:37:11 -07002021 .write = smk_write_onlycap,
Rafal Krypac0d77c82015-06-02 11:23:48 +02002022 .llseek = seq_lseek,
2023 .release = seq_release,
Casey Schaufler15446232008-07-30 15:37:11 -07002024};
2025
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002026#ifdef CONFIG_SECURITY_SMACK_BRINGUP
2027/**
2028 * smk_read_unconfined - read() for smackfs/unconfined
2029 * @filp: file pointer, not actually used
2030 * @buf: where to put the result
2031 * @cn: maximum to send along
2032 * @ppos: where to start
2033 *
2034 * Returns number of bytes read or error code, as appropriate
2035 */
2036static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
2037 size_t cn, loff_t *ppos)
2038{
2039 char *smack = "";
2040 ssize_t rc = -EINVAL;
2041 int asize;
2042
2043 if (*ppos != 0)
2044 return 0;
2045
2046 if (smack_unconfined != NULL)
2047 smack = smack_unconfined->smk_known;
2048
2049 asize = strlen(smack) + 1;
2050
2051 if (cn >= asize)
2052 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
2053
2054 return rc;
2055}
2056
2057/**
2058 * smk_write_unconfined - write() for smackfs/unconfined
2059 * @file: file pointer, not actually used
2060 * @buf: where to get the data from
2061 * @count: bytes sent
2062 * @ppos: where to start
2063 *
2064 * Returns number of bytes written or error code, as appropriate
2065 */
2066static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
2067 size_t count, loff_t *ppos)
2068{
2069 char *data;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002070 struct smack_known *skp;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002071 int rc = count;
2072
2073 if (!smack_privileged(CAP_MAC_ADMIN))
2074 return -EPERM;
2075
Al Viro16e5c1f2015-12-24 00:06:05 -05002076 data = memdup_user_nul(buf, count);
2077 if (IS_ERR(data))
2078 return PTR_ERR(data);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002079
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002080 /*
2081 * Clear the smack_unconfined on invalid label errors. This means
2082 * that we can pass a null string to unset the unconfined value.
2083 *
2084 * Importing will also reject a label beginning with '-',
2085 * so "-confine" will also work.
2086 *
2087 * But do so only on invalid label, not on system errors.
2088 */
2089 skp = smk_import_entry(data, count);
2090 if (PTR_ERR(skp) == -EINVAL)
2091 skp = NULL;
2092 else if (IS_ERR(skp)) {
2093 rc = PTR_ERR(skp);
2094 goto freeout;
2095 }
2096
2097 smack_unconfined = skp;
2098
2099freeout:
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002100 kfree(data);
2101 return rc;
2102}
2103
2104static const struct file_operations smk_unconfined_ops = {
2105 .read = smk_read_unconfined,
2106 .write = smk_write_unconfined,
2107 .llseek = default_llseek,
2108};
2109#endif /* CONFIG_SECURITY_SMACK_BRINGUP */
2110
Casey Schauflere114e472008-02-04 22:29:50 -08002111/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002112 * smk_read_logging - read() for /smack/logging
2113 * @filp: file pointer, not actually used
2114 * @buf: where to put the result
2115 * @cn: maximum to send along
2116 * @ppos: where to start
2117 *
2118 * Returns number of bytes read or error code, as appropriate
2119 */
2120static ssize_t smk_read_logging(struct file *filp, char __user *buf,
2121 size_t count, loff_t *ppos)
2122{
2123 char temp[32];
2124 ssize_t rc;
2125
2126 if (*ppos != 0)
2127 return 0;
2128
2129 sprintf(temp, "%d\n", log_policy);
2130 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2131 return rc;
2132}
2133
2134/**
2135 * smk_write_logging - write() for /smack/logging
2136 * @file: file pointer, not actually used
2137 * @buf: where to get the data from
2138 * @count: bytes sent
2139 * @ppos: where to start
2140 *
2141 * Returns number of bytes written or error code, as appropriate
2142 */
2143static ssize_t smk_write_logging(struct file *file, const char __user *buf,
2144 size_t count, loff_t *ppos)
2145{
2146 char temp[32];
2147 int i;
2148
Casey Schaufler1880eff2012-06-05 15:28:30 -07002149 if (!smack_privileged(CAP_MAC_ADMIN))
Etienne Bassetecfcc532009-04-08 20:40:06 +02002150 return -EPERM;
2151
2152 if (count >= sizeof(temp) || count == 0)
2153 return -EINVAL;
2154
2155 if (copy_from_user(temp, buf, count) != 0)
2156 return -EFAULT;
2157
2158 temp[count] = '\0';
2159
2160 if (sscanf(temp, "%d", &i) != 1)
2161 return -EINVAL;
2162 if (i < 0 || i > 3)
2163 return -EINVAL;
2164 log_policy = i;
2165 return count;
2166}
2167
2168
2169
2170static const struct file_operations smk_logging_ops = {
2171 .read = smk_read_logging,
2172 .write = smk_write_logging,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002173 .llseek = default_llseek,
Etienne Bassetecfcc532009-04-08 20:40:06 +02002174};
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002175
2176/*
2177 * Seq_file read operations for /smack/load-self
2178 */
2179
2180static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
2181{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002182 struct task_smack *tsp = smack_cred(current_cred());
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002183
Casey Schaufler40809562011-11-10 15:02:22 -08002184 return smk_seq_start(s, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002185}
2186
2187static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2188{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002189 struct task_smack *tsp = smack_cred(current_cred());
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002190
Casey Schaufler40809562011-11-10 15:02:22 -08002191 return smk_seq_next(s, v, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002192}
2193
2194static int load_self_seq_show(struct seq_file *s, void *v)
2195{
2196 struct list_head *list = v;
2197 struct smack_rule *srp =
Rafal Krypa01fa8472015-05-21 18:24:31 +02002198 list_entry_rcu(list, struct smack_rule, list);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002199
Casey Schauflerf7112e62012-05-06 15:22:02 -07002200 smk_rule_show(s, srp, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002201
2202 return 0;
2203}
2204
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002205static const struct seq_operations load_self_seq_ops = {
2206 .start = load_self_seq_start,
2207 .next = load_self_seq_next,
2208 .show = load_self_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08002209 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002210};
2211
2212
2213/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07002214 * smk_open_load_self - open() for /smack/load-self2
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002215 * @inode: inode structure representing file
2216 * @file: "load" file pointer
2217 *
2218 * For reading, use load_seq_* seq_file reading operations.
2219 */
2220static int smk_open_load_self(struct inode *inode, struct file *file)
2221{
2222 return seq_open(file, &load_self_seq_ops);
2223}
2224
2225/**
2226 * smk_write_load_self - write() for /smack/load-self
2227 * @file: file pointer, not actually used
2228 * @buf: where to get the data from
2229 * @count: bytes sent
2230 * @ppos: where to start - must be 0
2231 *
2232 */
2233static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
2234 size_t count, loff_t *ppos)
2235{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002236 struct task_smack *tsp = smack_cred(current_cred());
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002237
Casey Schauflerf7112e62012-05-06 15:22:02 -07002238 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2239 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002240}
2241
2242static const struct file_operations smk_load_self_ops = {
2243 .open = smk_open_load_self,
2244 .read = seq_read,
2245 .llseek = seq_lseek,
2246 .write = smk_write_load_self,
2247 .release = seq_release,
2248};
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002249
2250/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07002251 * smk_user_access - handle access check transaction
2252 * @file: file pointer
2253 * @buf: data from user space
2254 * @count: bytes sent
2255 * @ppos: where to start - must be 0
2256 */
2257static ssize_t smk_user_access(struct file *file, const char __user *buf,
2258 size_t count, loff_t *ppos, int format)
2259{
Rafal Krypae05b6f92013-01-10 19:42:00 +01002260 struct smack_parsed_rule rule;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002261 char *data;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002262 int res;
2263
2264 data = simple_transaction_get(file, buf, count);
2265 if (IS_ERR(data))
2266 return PTR_ERR(data);
2267
2268 if (format == SMK_FIXED24_FMT) {
2269 if (count < SMK_LOADLEN)
2270 return -EINVAL;
2271 res = smk_parse_rule(data, &rule, 0);
2272 } else {
2273 /*
Rafal Krypa10289b02013-08-09 11:47:07 +02002274 * simple_transaction_get() returns null-terminated data
Casey Schauflerf7112e62012-05-06 15:22:02 -07002275 */
Rafal Krypa10289b02013-08-09 11:47:07 +02002276 res = smk_parse_long_rule(data, &rule, 0, 3);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002277 }
2278
Jarkko Sakkinen398ce072013-11-28 19:16:46 +02002279 if (res >= 0)
2280 res = smk_access(rule.smk_subject, rule.smk_object,
2281 rule.smk_access1, NULL);
2282 else if (res != -ENOENT)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002283 return res;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002284
Casey Schauflerd166c802014-08-27 14:51:27 -07002285 /*
2286 * smk_access() can return a value > 0 in the "bringup" case.
2287 */
2288 data[0] = res >= 0 ? '1' : '0';
Casey Schauflerf7112e62012-05-06 15:22:02 -07002289 data[1] = '\0';
2290
2291 simple_transaction_set(file, 2);
2292
2293 if (format == SMK_FIXED24_FMT)
2294 return SMK_LOADLEN;
2295 return count;
2296}
2297
2298/**
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002299 * smk_write_access - handle access check transaction
2300 * @file: file pointer
2301 * @buf: data from user space
2302 * @count: bytes sent
2303 * @ppos: where to start - must be 0
2304 */
2305static ssize_t smk_write_access(struct file *file, const char __user *buf,
2306 size_t count, loff_t *ppos)
2307{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002308 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002309}
2310
2311static const struct file_operations smk_access_ops = {
2312 .write = smk_write_access,
2313 .read = simple_transaction_read,
2314 .release = simple_transaction_release,
2315 .llseek = generic_file_llseek,
2316};
2317
Casey Schauflerf7112e62012-05-06 15:22:02 -07002318
2319/*
2320 * Seq_file read operations for /smack/load2
2321 */
2322
2323static int load2_seq_show(struct seq_file *s, void *v)
2324{
2325 struct list_head *list = v;
Vishal Goel460d95a2019-03-07 16:55:24 +05302326 struct smack_rule *srp;
2327 struct smack_known *skp =
2328 list_entry_rcu(list, struct smack_known, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002329
Vishal Goel460d95a2019-03-07 16:55:24 +05302330 list_for_each_entry_rcu(srp, &skp->smk_rules, list)
2331 smk_rule_show(s, srp, SMK_LONGLABEL);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002332
2333 return 0;
2334}
2335
2336static const struct seq_operations load2_seq_ops = {
2337 .start = load2_seq_start,
2338 .next = load2_seq_next,
2339 .show = load2_seq_show,
2340 .stop = smk_seq_stop,
2341};
2342
2343/**
2344 * smk_open_load2 - open() for /smack/load2
2345 * @inode: inode structure representing file
2346 * @file: "load2" file pointer
2347 *
2348 * For reading, use load2_seq_* seq_file reading operations.
2349 */
2350static int smk_open_load2(struct inode *inode, struct file *file)
2351{
2352 return seq_open(file, &load2_seq_ops);
2353}
2354
2355/**
2356 * smk_write_load2 - write() for /smack/load2
2357 * @file: file pointer, not actually used
2358 * @buf: where to get the data from
2359 * @count: bytes sent
2360 * @ppos: where to start - must be 0
2361 *
2362 */
2363static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2364 size_t count, loff_t *ppos)
2365{
2366 /*
2367 * Must have privilege.
2368 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07002369 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07002370 return -EPERM;
2371
2372 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2373 SMK_LONG_FMT);
2374}
2375
2376static const struct file_operations smk_load2_ops = {
2377 .open = smk_open_load2,
2378 .read = seq_read,
2379 .llseek = seq_lseek,
2380 .write = smk_write_load2,
2381 .release = seq_release,
2382};
2383
2384/*
2385 * Seq_file read operations for /smack/load-self2
2386 */
2387
2388static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2389{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002390 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerf7112e62012-05-06 15:22:02 -07002391
2392 return smk_seq_start(s, pos, &tsp->smk_rules);
2393}
2394
2395static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2396{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002397 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerf7112e62012-05-06 15:22:02 -07002398
2399 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2400}
2401
2402static int load_self2_seq_show(struct seq_file *s, void *v)
2403{
2404 struct list_head *list = v;
2405 struct smack_rule *srp =
Rafal Krypa01fa8472015-05-21 18:24:31 +02002406 list_entry_rcu(list, struct smack_rule, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002407
2408 smk_rule_show(s, srp, SMK_LONGLABEL);
2409
2410 return 0;
2411}
2412
2413static const struct seq_operations load_self2_seq_ops = {
2414 .start = load_self2_seq_start,
2415 .next = load_self2_seq_next,
2416 .show = load_self2_seq_show,
2417 .stop = smk_seq_stop,
2418};
2419
2420/**
2421 * smk_open_load_self2 - open() for /smack/load-self2
2422 * @inode: inode structure representing file
2423 * @file: "load" file pointer
2424 *
2425 * For reading, use load_seq_* seq_file reading operations.
2426 */
2427static int smk_open_load_self2(struct inode *inode, struct file *file)
2428{
2429 return seq_open(file, &load_self2_seq_ops);
2430}
2431
2432/**
2433 * smk_write_load_self2 - write() for /smack/load-self2
2434 * @file: file pointer, not actually used
2435 * @buf: where to get the data from
2436 * @count: bytes sent
2437 * @ppos: where to start - must be 0
2438 *
2439 */
2440static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2441 size_t count, loff_t *ppos)
2442{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002443 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerf7112e62012-05-06 15:22:02 -07002444
2445 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2446 &tsp->smk_rules_lock, SMK_LONG_FMT);
2447}
2448
2449static const struct file_operations smk_load_self2_ops = {
2450 .open = smk_open_load_self2,
2451 .read = seq_read,
2452 .llseek = seq_lseek,
2453 .write = smk_write_load_self2,
2454 .release = seq_release,
2455};
2456
2457/**
2458 * smk_write_access2 - handle access check transaction
2459 * @file: file pointer
2460 * @buf: data from user space
2461 * @count: bytes sent
2462 * @ppos: where to start - must be 0
2463 */
2464static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2465 size_t count, loff_t *ppos)
2466{
2467 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2468}
2469
2470static const struct file_operations smk_access2_ops = {
2471 .write = smk_write_access2,
2472 .read = simple_transaction_read,
2473 .release = simple_transaction_release,
2474 .llseek = generic_file_llseek,
2475};
2476
Etienne Bassetecfcc532009-04-08 20:40:06 +02002477/**
Rafal Krypa449543b2012-07-11 17:49:30 +02002478 * smk_write_revoke_subj - write() for /smack/revoke-subject
2479 * @file: file pointer
2480 * @buf: data from user space
2481 * @count: bytes sent
2482 * @ppos: where to start - must be 0
2483 */
2484static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2485 size_t count, loff_t *ppos)
2486{
Dan Carpenter54302092015-06-11 11:51:16 +03002487 char *data;
2488 const char *cp;
Rafal Krypa449543b2012-07-11 17:49:30 +02002489 struct smack_known *skp;
2490 struct smack_rule *sp;
2491 struct list_head *rule_list;
2492 struct mutex *rule_lock;
2493 int rc = count;
2494
2495 if (*ppos != 0)
2496 return -EINVAL;
2497
2498 if (!smack_privileged(CAP_MAC_ADMIN))
2499 return -EPERM;
2500
2501 if (count == 0 || count > SMK_LONGLABEL)
2502 return -EINVAL;
2503
Markus Elfring63e24c42016-08-21 20:17:36 +02002504 data = memdup_user(buf, count);
2505 if (IS_ERR(data))
2506 return PTR_ERR(data);
Rafal Krypa449543b2012-07-11 17:49:30 +02002507
2508 cp = smk_parse_smack(data, count);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002509 if (IS_ERR(cp)) {
2510 rc = PTR_ERR(cp);
Dan Carpenter54302092015-06-11 11:51:16 +03002511 goto out_data;
Rafal Krypa449543b2012-07-11 17:49:30 +02002512 }
2513
2514 skp = smk_find_entry(cp);
Rafal Krypad15d9fa2012-11-27 16:28:11 +01002515 if (skp == NULL)
Dan Carpenter54302092015-06-11 11:51:16 +03002516 goto out_cp;
Rafal Krypa449543b2012-07-11 17:49:30 +02002517
2518 rule_list = &skp->smk_rules;
2519 rule_lock = &skp->smk_rules_lock;
2520
2521 mutex_lock(rule_lock);
2522
2523 list_for_each_entry_rcu(sp, rule_list, list)
2524 sp->smk_access = 0;
2525
2526 mutex_unlock(rule_lock);
2527
Dan Carpenter54302092015-06-11 11:51:16 +03002528out_cp:
Rafal Krypa449543b2012-07-11 17:49:30 +02002529 kfree(cp);
Dan Carpenter54302092015-06-11 11:51:16 +03002530out_data:
2531 kfree(data);
2532
Rafal Krypa449543b2012-07-11 17:49:30 +02002533 return rc;
2534}
2535
2536static const struct file_operations smk_revoke_subj_ops = {
2537 .write = smk_write_revoke_subj,
2538 .read = simple_transaction_read,
2539 .release = simple_transaction_release,
2540 .llseek = generic_file_llseek,
2541};
2542
Casey Schauflere93072372012-11-01 18:14:32 -07002543/**
2544 * smk_init_sysfs - initialize /sys/fs/smackfs
2545 *
2546 */
2547static int smk_init_sysfs(void)
2548{
kbuild test robotca70d272015-06-24 07:41:07 +08002549 return sysfs_create_mount_point(fs_kobj, "smackfs");
Casey Schauflere93072372012-11-01 18:14:32 -07002550}
2551
Rafal Krypa449543b2012-07-11 17:49:30 +02002552/**
Rafal Krypae05b6f92013-01-10 19:42:00 +01002553 * smk_write_change_rule - write() for /smack/change-rule
2554 * @file: file pointer
2555 * @buf: data from user space
2556 * @count: bytes sent
2557 * @ppos: where to start - must be 0
2558 */
2559static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2560 size_t count, loff_t *ppos)
2561{
2562 /*
2563 * Must have privilege.
2564 */
Casey Schaufler4afde482013-12-19 13:23:26 -08002565 if (!smack_privileged(CAP_MAC_ADMIN))
Rafal Krypae05b6f92013-01-10 19:42:00 +01002566 return -EPERM;
2567
2568 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2569 SMK_CHANGE_FMT);
2570}
2571
2572static const struct file_operations smk_change_rule_ops = {
2573 .write = smk_write_change_rule,
2574 .read = simple_transaction_read,
2575 .release = simple_transaction_release,
2576 .llseek = generic_file_llseek,
2577};
2578
2579/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002580 * smk_read_syslog - read() for smackfs/syslog
2581 * @filp: file pointer, not actually used
2582 * @buf: where to put the result
2583 * @cn: maximum to send along
2584 * @ppos: where to start
2585 *
2586 * Returns number of bytes read or error code, as appropriate
2587 */
2588static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2589 size_t cn, loff_t *ppos)
2590{
2591 struct smack_known *skp;
2592 ssize_t rc = -EINVAL;
2593 int asize;
2594
2595 if (*ppos != 0)
2596 return 0;
2597
2598 if (smack_syslog_label == NULL)
2599 skp = &smack_known_star;
2600 else
2601 skp = smack_syslog_label;
2602
2603 asize = strlen(skp->smk_known) + 1;
2604
2605 if (cn >= asize)
2606 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2607 asize);
2608
2609 return rc;
2610}
2611
2612/**
2613 * smk_write_syslog - write() for smackfs/syslog
2614 * @file: file pointer, not actually used
2615 * @buf: where to get the data from
2616 * @count: bytes sent
2617 * @ppos: where to start
2618 *
2619 * Returns number of bytes written or error code, as appropriate
2620 */
2621static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2622 size_t count, loff_t *ppos)
2623{
2624 char *data;
2625 struct smack_known *skp;
2626 int rc = count;
2627
2628 if (!smack_privileged(CAP_MAC_ADMIN))
2629 return -EPERM;
2630
Al Viro16e5c1f2015-12-24 00:06:05 -05002631 data = memdup_user_nul(buf, count);
2632 if (IS_ERR(data))
2633 return PTR_ERR(data);
Casey Schaufler00f84f32013-12-23 11:07:10 -08002634
Al Viro16e5c1f2015-12-24 00:06:05 -05002635 skp = smk_import_entry(data, count);
2636 if (IS_ERR(skp))
2637 rc = PTR_ERR(skp);
2638 else
2639 smack_syslog_label = skp;
Casey Schaufler00f84f32013-12-23 11:07:10 -08002640
2641 kfree(data);
2642 return rc;
2643}
2644
2645static const struct file_operations smk_syslog_ops = {
2646 .read = smk_read_syslog,
2647 .write = smk_write_syslog,
2648 .llseek = default_llseek,
2649};
2650
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002651/*
2652 * Seq_file read operations for /smack/relabel-self
2653 */
2654
2655static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
2656{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002657 struct task_smack *tsp = smack_cred(current_cred());
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002658
2659 return smk_seq_start(s, pos, &tsp->smk_relabel);
2660}
2661
2662static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2663{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002664 struct task_smack *tsp = smack_cred(current_cred());
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002665
2666 return smk_seq_next(s, v, pos, &tsp->smk_relabel);
2667}
2668
2669static int relabel_self_seq_show(struct seq_file *s, void *v)
2670{
2671 struct list_head *list = v;
2672 struct smack_known_list_elem *sklep =
2673 list_entry(list, struct smack_known_list_elem, list);
2674
2675 seq_puts(s, sklep->smk_label->smk_known);
2676 seq_putc(s, ' ');
2677
2678 return 0;
2679}
2680
2681static const struct seq_operations relabel_self_seq_ops = {
2682 .start = relabel_self_seq_start,
2683 .next = relabel_self_seq_next,
2684 .show = relabel_self_seq_show,
2685 .stop = smk_seq_stop,
2686};
2687
2688/**
2689 * smk_open_relabel_self - open() for /smack/relabel-self
2690 * @inode: inode structure representing file
2691 * @file: "relabel-self" file pointer
2692 *
2693 * Connect our relabel_self_seq_* operations with /smack/relabel-self
2694 * file_operations
2695 */
2696static int smk_open_relabel_self(struct inode *inode, struct file *file)
2697{
2698 return seq_open(file, &relabel_self_seq_ops);
2699}
2700
2701/**
2702 * smk_write_relabel_self - write() for /smack/relabel-self
2703 * @file: file pointer, not actually used
2704 * @buf: where to get the data from
2705 * @count: bytes sent
2706 * @ppos: where to start - must be 0
2707 *
2708 */
2709static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
2710 size_t count, loff_t *ppos)
2711{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002712 struct task_smack *tsp = smack_cred(current_cred());
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002713 char *data;
2714 int rc;
2715 LIST_HEAD(list_tmp);
2716
2717 /*
2718 * Must have privilege.
2719 */
2720 if (!smack_privileged(CAP_MAC_ADMIN))
2721 return -EPERM;
2722
2723 /*
2724 * Enough data must be present.
2725 */
2726 if (*ppos != 0)
2727 return -EINVAL;
2728
Al Viro16e5c1f2015-12-24 00:06:05 -05002729 data = memdup_user_nul(buf, count);
2730 if (IS_ERR(data))
2731 return PTR_ERR(data);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002732
2733 rc = smk_parse_label_list(data, &list_tmp);
2734 kfree(data);
2735
2736 if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2737 smk_destroy_label_list(&tsp->smk_relabel);
2738 list_splice(&list_tmp, &tsp->smk_relabel);
2739 return count;
2740 }
2741
2742 smk_destroy_label_list(&list_tmp);
2743 return rc;
2744}
2745
2746static const struct file_operations smk_relabel_self_ops = {
2747 .open = smk_open_relabel_self,
2748 .read = seq_read,
2749 .llseek = seq_lseek,
2750 .write = smk_write_relabel_self,
2751 .release = seq_release,
2752};
Casey Schaufler00f84f32013-12-23 11:07:10 -08002753
2754/**
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002755 * smk_read_ptrace - read() for /smack/ptrace
2756 * @filp: file pointer, not actually used
2757 * @buf: where to put the result
2758 * @count: maximum to send along
2759 * @ppos: where to start
2760 *
2761 * Returns number of bytes read or error code, as appropriate
2762 */
2763static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2764 size_t count, loff_t *ppos)
2765{
2766 char temp[32];
2767 ssize_t rc;
2768
2769 if (*ppos != 0)
2770 return 0;
2771
2772 sprintf(temp, "%d\n", smack_ptrace_rule);
2773 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2774 return rc;
2775}
2776
2777/**
2778 * smk_write_ptrace - write() for /smack/ptrace
2779 * @file: file pointer
2780 * @buf: data from user space
2781 * @count: bytes sent
2782 * @ppos: where to start - must be 0
2783 */
2784static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2785 size_t count, loff_t *ppos)
2786{
2787 char temp[32];
2788 int i;
2789
2790 if (!smack_privileged(CAP_MAC_ADMIN))
2791 return -EPERM;
2792
2793 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2794 return -EINVAL;
2795
2796 if (copy_from_user(temp, buf, count) != 0)
2797 return -EFAULT;
2798
2799 temp[count] = '\0';
2800
2801 if (sscanf(temp, "%d", &i) != 1)
2802 return -EINVAL;
2803 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2804 return -EINVAL;
2805 smack_ptrace_rule = i;
2806
2807 return count;
2808}
2809
2810static const struct file_operations smk_ptrace_ops = {
2811 .write = smk_write_ptrace,
2812 .read = smk_read_ptrace,
2813 .llseek = default_llseek,
2814};
2815
2816/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002817 * smk_fill_super - fill the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002818 * @sb: the empty superblock
2819 * @data: unused
2820 * @silent: unused
2821 *
Casey Schaufler00f84f32013-12-23 11:07:10 -08002822 * Fill in the well known entries for the smack filesystem
Casey Schauflere114e472008-02-04 22:29:50 -08002823 *
2824 * Returns 0 on success, an error code on failure
2825 */
2826static int smk_fill_super(struct super_block *sb, void *data, int silent)
2827{
2828 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002829
Eric Biggerscda37122017-03-25 21:15:37 -07002830 static const struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002831 [SMK_LOAD] = {
2832 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2833 [SMK_CIPSO] = {
2834 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2835 [SMK_DOI] = {
2836 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2837 [SMK_DIRECT] = {
2838 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2839 [SMK_AMBIENT] = {
2840 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002841 [SMK_NET4ADDR] = {
2842 "netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002843 [SMK_ONLYCAP] = {
2844 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2845 [SMK_LOGGING] = {
2846 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2847 [SMK_LOAD_SELF] = {
2848 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002849 [SMK_ACCESSES] = {
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03002850 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
Casey Schauflerf7112e62012-05-06 15:22:02 -07002851 [SMK_MAPPED] = {
2852 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2853 [SMK_LOAD2] = {
2854 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2855 [SMK_LOAD_SELF2] = {
2856 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2857 [SMK_ACCESS2] = {
2858 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2859 [SMK_CIPSO2] = {
2860 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
Rafal Krypa449543b2012-07-11 17:49:30 +02002861 [SMK_REVOKE_SUBJ] = {
2862 "revoke-subject", &smk_revoke_subj_ops,
2863 S_IRUGO|S_IWUSR},
Rafal Krypae05b6f92013-01-10 19:42:00 +01002864 [SMK_CHANGE_RULE] = {
2865 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
Casey Schaufler00f84f32013-12-23 11:07:10 -08002866 [SMK_SYSLOG] = {
2867 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002868 [SMK_PTRACE] = {
2869 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002870#ifdef CONFIG_SECURITY_SMACK_BRINGUP
2871 [SMK_UNCONFINED] = {
2872 "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2873#endif
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002874#if IS_ENABLED(CONFIG_IPV6)
2875 [SMK_NET6ADDR] = {
2876 "ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
2877#endif /* CONFIG_IPV6 */
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002878 [SMK_RELABEL_SELF] = {
2879 "relabel-self", &smk_relabel_self_ops,
2880 S_IRUGO|S_IWUGO},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002881 /* last one */
2882 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08002883 };
2884
2885 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2886 if (rc != 0) {
2887 printk(KERN_ERR "%s failed %d while creating inodes\n",
2888 __func__, rc);
2889 return rc;
2890 }
2891
Casey Schauflere114e472008-02-04 22:29:50 -08002892 return 0;
2893}
2894
2895/**
Al Virofc14f2f2010-07-25 01:48:30 +04002896 * smk_mount - get the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002897 * @fs_type: passed along without comment
2898 * @flags: passed along without comment
2899 * @dev_name: passed along without comment
2900 * @data: passed along without comment
Casey Schauflere114e472008-02-04 22:29:50 -08002901 *
2902 * Just passes everything along.
2903 *
2904 * Returns what the lower level code does.
2905 */
Al Virofc14f2f2010-07-25 01:48:30 +04002906static struct dentry *smk_mount(struct file_system_type *fs_type,
2907 int flags, const char *dev_name, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -08002908{
Al Virofc14f2f2010-07-25 01:48:30 +04002909 return mount_single(fs_type, flags, data, smk_fill_super);
Casey Schauflere114e472008-02-04 22:29:50 -08002910}
2911
2912static struct file_system_type smk_fs_type = {
2913 .name = "smackfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002914 .mount = smk_mount,
Casey Schauflere114e472008-02-04 22:29:50 -08002915 .kill_sb = kill_litter_super,
2916};
2917
2918static struct vfsmount *smackfs_mount;
2919
Casey Schauflerf7112e62012-05-06 15:22:02 -07002920static int __init smk_preset_netlabel(struct smack_known *skp)
2921{
2922 skp->smk_netlabel.domain = skp->smk_known;
2923 skp->smk_netlabel.flags =
2924 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2925 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2926 &skp->smk_netlabel, strlen(skp->smk_known));
2927}
2928
Casey Schauflere114e472008-02-04 22:29:50 -08002929/**
2930 * init_smk_fs - get the smackfs superblock
2931 *
2932 * register the smackfs
2933 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002934 * Do not register smackfs if Smack wasn't enabled
2935 * on boot. We can not put this method normally under the
2936 * smack_init() code path since the security subsystem get
2937 * initialized before the vfs caches.
2938 *
2939 * Returns true if we were not chosen on boot or if
2940 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08002941 */
2942static int __init init_smk_fs(void)
2943{
2944 int err;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002945 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002946
José Bollod21b7b02015-10-02 15:15:56 +02002947 if (smack_enabled == 0)
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002948 return 0;
2949
Casey Schauflere93072372012-11-01 18:14:32 -07002950 err = smk_init_sysfs();
2951 if (err)
2952 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2953
Casey Schauflere114e472008-02-04 22:29:50 -08002954 err = register_filesystem(&smk_fs_type);
2955 if (!err) {
2956 smackfs_mount = kern_mount(&smk_fs_type);
2957 if (IS_ERR(smackfs_mount)) {
2958 printk(KERN_ERR "smackfs: could not mount!\n");
2959 err = PTR_ERR(smackfs_mount);
2960 smackfs_mount = NULL;
2961 }
2962 }
2963
Casey Schauflere114e472008-02-04 22:29:50 -08002964 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002965 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08002966
Casey Schauflerf7112e62012-05-06 15:22:02 -07002967 rc = smk_preset_netlabel(&smack_known_floor);
2968 if (err == 0 && rc < 0)
2969 err = rc;
2970 rc = smk_preset_netlabel(&smack_known_hat);
2971 if (err == 0 && rc < 0)
2972 err = rc;
2973 rc = smk_preset_netlabel(&smack_known_huh);
2974 if (err == 0 && rc < 0)
2975 err = rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002976 rc = smk_preset_netlabel(&smack_known_star);
2977 if (err == 0 && rc < 0)
2978 err = rc;
2979 rc = smk_preset_netlabel(&smack_known_web);
2980 if (err == 0 && rc < 0)
2981 err = rc;
2982
Casey Schauflere114e472008-02-04 22:29:50 -08002983 return err;
2984}
2985
2986__initcall(init_smk_fs);