blob: f2fb563af43176045eedd9d5b2d49ffc8a9d4977 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
7 *
8 * Authors:
9 * Casey Schaufler <casey@schaufler-ca.com>
10 * Ahmed S. Darwish <darwish.07@gmail.com>
11 *
12 * Special thanks to the authors of selinuxfs.
13 *
14 * Karl MacMillan <kmacmillan@tresys.com>
15 * James Morris <jmorris@redhat.com>
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/vmalloc.h>
21#include <linux/security.h>
22#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Casey Schaufler6d3dc072008-12-31 12:54:12 -050024#include <net/net_namespace.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <net/cipso_ipv4.h>
26#include <linux/seq_file.h>
27#include <linux/ctype.h>
Casey Schaufler4bc87e62008-02-15 15:24:25 -080028#include <linux/audit.h>
Casey Schaufler958d2c22013-04-02 11:41:18 -070029#include <linux/magic.h>
David Howells5afdd0f2019-03-25 16:38:31 +000030#include <linux/fs_context.h>
Casey Schauflere114e472008-02-04 22:29:50 -080031#include "smack.h"
32
Casey Schaufler21abb1e2015-07-22 14:25:31 -070033#define BEBITS (sizeof(__be32) * 8)
Casey Schauflere114e472008-02-04 22:29:50 -080034/*
35 * smackfs pseudo filesystem.
36 */
37
38enum smk_inos {
39 SMK_ROOT_INO = 2,
40 SMK_LOAD = 3, /* load policy */
41 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
42 SMK_DOI = 5, /* CIPSO DOI */
43 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
44 SMK_AMBIENT = 7, /* internet ambient label */
Casey Schaufler21abb1e2015-07-22 14:25:31 -070045 SMK_NET4ADDR = 8, /* single label hosts */
Casey Schaufler15446232008-07-30 15:37:11 -070046 SMK_ONLYCAP = 9, /* the only "capable" label */
Etienne Bassetecfcc532009-04-08 20:40:06 +020047 SMK_LOGGING = 10, /* logging */
Casey Schaufler7898e1f2011-01-17 08:05:27 -080048 SMK_LOAD_SELF = 11, /* task specific rules */
Jarkko Sakkinen828716c2011-09-08 10:12:01 +030049 SMK_ACCESSES = 12, /* access policy */
Casey Schauflerf7112e62012-05-06 15:22:02 -070050 SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
51 SMK_LOAD2 = 14, /* load policy with long labels */
52 SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
53 SMK_ACCESS2 = 16, /* make an access check with long labels */
54 SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
Rafal Krypa449543b2012-07-11 17:49:30 +020055 SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
Rafal Krypae05b6f92013-01-10 19:42:00 +010056 SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
Casey Schaufler00f84f32013-12-23 11:07:10 -080057 SMK_SYSLOG = 20, /* change syslog label) */
Lukasz Pawelczyk66867812014-03-11 17:07:06 +010058 SMK_PTRACE = 21, /* set ptrace rule */
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070059#ifdef CONFIG_SECURITY_SMACK_BRINGUP
60 SMK_UNCONFINED = 22, /* define an unconfined label */
61#endif
Casey Schaufler21abb1e2015-07-22 14:25:31 -070062#if IS_ENABLED(CONFIG_IPV6)
63 SMK_NET6ADDR = 23, /* single label IPv6 hosts */
64#endif /* CONFIG_IPV6 */
Zbigniew Jasinski38416e52015-10-19 18:23:53 +020065 SMK_RELABEL_SELF = 24, /* relabel possible without CAP_MAC_ADMIN */
Casey Schauflere114e472008-02-04 22:29:50 -080066};
67
68/*
69 * List locks
70 */
Casey Schauflere114e472008-02-04 22:29:50 -080071static DEFINE_MUTEX(smack_cipso_lock);
Casey Schaufler4bc87e62008-02-15 15:24:25 -080072static DEFINE_MUTEX(smack_ambient_lock);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070073static DEFINE_MUTEX(smk_net4addr_lock);
74#if IS_ENABLED(CONFIG_IPV6)
75static DEFINE_MUTEX(smk_net6addr_lock);
76#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -080077
78/*
79 * This is the "ambient" label for network traffic.
80 * If it isn't somehow marked, use this.
81 * It can be reset via smackfs/ambient
82 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -070083struct smack_known *smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -080084
85/*
Casey Schauflere114e472008-02-04 22:29:50 -080086 * This is the level in a CIPSO header that indicates a
87 * smack label is contained directly in the category set.
88 * It can be reset via smackfs/direct
89 */
90int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
91
Casey Schaufler15446232008-07-30 15:37:11 -070092/*
Casey Schauflerf7112e62012-05-06 15:22:02 -070093 * This is the level in a CIPSO header that indicates a
94 * secid is contained directly in the category set.
95 * It can be reset via smackfs/mapped
96 */
97int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
98
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070099#ifdef CONFIG_SECURITY_SMACK_BRINGUP
100/*
101 * Allow one label to be unconfined. This is for
102 * debugging and application bring-up purposes only.
103 * It is bad and wrong, but everyone seems to expect
104 * to have it.
105 */
106struct smack_known *smack_unconfined;
107#endif
108
Casey Schaufler00f84f32013-12-23 11:07:10 -0800109/*
110 * If this value is set restrict syslog use to the label specified.
111 * It can be reset via smackfs/syslog
112 */
113struct smack_known *smack_syslog_label;
Casey Schaufler15446232008-07-30 15:37:11 -0700114
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500115/*
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100116 * Ptrace current rule
117 * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
118 * SMACK_PTRACE_EXACT labels must match, but can be overriden with
119 * CAP_SYS_PTRACE
120 * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
121 */
122int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
123
124/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500125 * Certain IP addresses may be designated as single label hosts.
126 * Packets are sent there unlabeled, but only from tasks that
127 * can write to the specified label.
128 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100129
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700130LIST_HEAD(smk_net4addr_list);
131#if IS_ENABLED(CONFIG_IPV6)
132LIST_HEAD(smk_net6addr_list);
133#endif /* CONFIG_IPV6 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700134
135/*
136 * Rule lists are maintained for each label.
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700137 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100138struct smack_parsed_rule {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700139 struct smack_known *smk_subject;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200140 struct smack_known *smk_object;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100141 int smk_access1;
142 int smk_access2;
143};
144
Casey Schauflere114e472008-02-04 22:29:50 -0800145static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
Casey Schauflere114e472008-02-04 22:29:50 -0800146
Casey Schauflere114e472008-02-04 22:29:50 -0800147/*
Casey Schauflere114e472008-02-04 22:29:50 -0800148 * Values for parsing cipso rules
149 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700150 * SMK_CIPSOMIN: Minimum possible cipso rule length.
151 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -0800152 */
153#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700154#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
155#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
156
157/*
158 * Values for parsing MAC rules
159 * SMK_ACCESS: Maximum possible combination of access permissions
160 * SMK_ACCESSLEN: Maximum length for a rule access field
161 * SMK_LOADLEN: Smack rule length
162 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200163#define SMK_OACCESS "rwxa"
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700164#define SMK_ACCESS "rwxatl"
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200165#define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
166#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
167#define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
168#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700169
Casey Schauflerf7112e62012-05-06 15:22:02 -0700170/*
171 * Stricly for CIPSO level manipulation.
172 * Set the category bit number in a smack label sized buffer.
173 */
174static inline void smack_catset_bit(unsigned int cat, char *catsetp)
175{
176 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
177 return;
178
179 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
180}
181
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500182/**
183 * smk_netlabel_audit_set - fill a netlbl_audit struct
184 * @nap: structure to fill
185 */
186static void smk_netlabel_audit_set(struct netlbl_audit *nap)
187{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700188 struct smack_known *skp = smk_of_current();
189
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500190 nap->loginuid = audit_get_loginuid(current);
191 nap->sessionid = audit_get_sessionid(current);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700192 nap->secid = skp->smk_secid;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500193}
194
195/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700196 * Value for parsing single label host rules
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500197 * "1.2.3.4 X"
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500198 */
199#define SMK_NETLBLADDRMIN 9
Casey Schauflere114e472008-02-04 22:29:50 -0800200
Casey Schauflere114e472008-02-04 22:29:50 -0800201/**
Rafal Krypae05b6f92013-01-10 19:42:00 +0100202 * smk_set_access - add a rule to the rule list or replace an old rule
203 * @srp: the rule to add or replace
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800204 * @rule_list: the list of rules
205 * @rule_lock: the rule list lock
Casey Schauflere114e472008-02-04 22:29:50 -0800206 *
207 * Looks through the current subject/object/access list for
208 * the subject/object pair and replaces the access that was
209 * there. If the pair isn't found add it with the specified
210 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300211 *
212 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
213 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800214 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100215static int smk_set_access(struct smack_parsed_rule *srp,
216 struct list_head *rule_list,
Vishal Goel460d95a2019-03-07 16:55:24 +0530217 struct mutex *rule_lock)
Casey Schauflere114e472008-02-04 22:29:50 -0800218{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100219 struct smack_rule *sp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800220 int found = 0;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100221 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800222
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800223 mutex_lock(rule_lock);
224
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700225 /*
226 * Because the object label is less likely to match
227 * than the subject label check it first
228 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800229 list_for_each_entry_rcu(sp, rule_list, list) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700230 if (sp->smk_object == srp->smk_object &&
231 sp->smk_subject == srp->smk_subject) {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100232 found = 1;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100233 sp->smk_access |= srp->smk_access1;
234 sp->smk_access &= ~srp->smk_access2;
Casey Schauflere114e472008-02-04 22:29:50 -0800235 break;
236 }
Casey Schauflere114e472008-02-04 22:29:50 -0800237 }
238
Rafal Krypae05b6f92013-01-10 19:42:00 +0100239 if (found == 0) {
Casey Schaufler4e328b02019-04-02 11:37:12 -0700240 sp = kmem_cache_zalloc(smack_rule_cache, GFP_KERNEL);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100241 if (sp == NULL) {
242 rc = -ENOMEM;
243 goto out;
244 }
245
246 sp->smk_subject = srp->smk_subject;
247 sp->smk_object = srp->smk_object;
248 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
249
250 list_add_rcu(&sp->list, rule_list);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100251 }
252
253out:
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800254 mutex_unlock(rule_lock);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100255 return rc;
256}
Casey Schauflere114e472008-02-04 22:29:50 -0800257
Rafal Krypae05b6f92013-01-10 19:42:00 +0100258/**
259 * smk_perm_from_str - parse smack accesses from a text string
260 * @string: a text string that contains a Smack accesses code
261 *
262 * Returns an integer with respective bits set for specified accesses.
263 */
264static int smk_perm_from_str(const char *string)
265{
266 int perm = 0;
267 const char *cp;
268
269 for (cp = string; ; cp++)
270 switch (*cp) {
271 case '-':
272 break;
273 case 'r':
274 case 'R':
275 perm |= MAY_READ;
276 break;
277 case 'w':
278 case 'W':
279 perm |= MAY_WRITE;
280 break;
281 case 'x':
282 case 'X':
283 perm |= MAY_EXEC;
284 break;
285 case 'a':
286 case 'A':
287 perm |= MAY_APPEND;
288 break;
289 case 't':
290 case 'T':
291 perm |= MAY_TRANSMUTE;
292 break;
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700293 case 'l':
294 case 'L':
295 perm |= MAY_LOCK;
296 break;
Casey Schauflerd166c802014-08-27 14:51:27 -0700297 case 'b':
298 case 'B':
299 perm |= MAY_BRINGUP;
300 break;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100301 default:
302 return perm;
303 }
Casey Schauflere114e472008-02-04 22:29:50 -0800304}
305
306/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700307 * smk_fill_rule - Fill Smack rule from strings
308 * @subject: subject label string
309 * @object: object label string
Rafal Krypae05b6f92013-01-10 19:42:00 +0100310 * @access1: access string
311 * @access2: string with permissions to be removed
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300312 * @rule: Smack rule
313 * @import: if non-zero, import labels
Casey Schaufler35187212012-06-18 19:01:36 -0700314 * @len: label length limit
Casey Schauflerf7112e62012-05-06 15:22:02 -0700315 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200316 * Returns 0 on success, appropriate error code on failure.
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300317 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700318static int smk_fill_rule(const char *subject, const char *object,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100319 const char *access1, const char *access2,
320 struct smack_parsed_rule *rule, int import,
321 int len)
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300322{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700323 const char *cp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300324 struct smack_known *skp;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300325
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300326 if (import) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700327 rule->smk_subject = smk_import_entry(subject, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200328 if (IS_ERR(rule->smk_subject))
329 return PTR_ERR(rule->smk_subject);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300330
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200331 rule->smk_object = smk_import_entry(object, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200332 if (IS_ERR(rule->smk_object))
333 return PTR_ERR(rule->smk_object);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300334 } else {
Casey Schaufler35187212012-06-18 19:01:36 -0700335 cp = smk_parse_smack(subject, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200336 if (IS_ERR(cp))
337 return PTR_ERR(cp);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700338 skp = smk_find_entry(cp);
339 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300340 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200341 return -ENOENT;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700342 rule->smk_subject = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300343
Casey Schaufler35187212012-06-18 19:01:36 -0700344 cp = smk_parse_smack(object, len);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200345 if (IS_ERR(cp))
346 return PTR_ERR(cp);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700347 skp = smk_find_entry(cp);
348 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300349 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200350 return -ENOENT;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200351 rule->smk_object = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300352 }
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300353
Rafal Krypae05b6f92013-01-10 19:42:00 +0100354 rule->smk_access1 = smk_perm_from_str(access1);
355 if (access2)
356 rule->smk_access2 = smk_perm_from_str(access2);
357 else
358 rule->smk_access2 = ~rule->smk_access1;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300359
Casey Schaufler35187212012-06-18 19:01:36 -0700360 return 0;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300361}
362
363/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700364 * smk_parse_rule - parse Smack rule from load string
365 * @data: string to be parsed whose size is SMK_LOADLEN
366 * @rule: Smack rule
367 * @import: if non-zero, import labels
368 *
369 * Returns 0 on success, -1 on errors.
370 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100371static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
372 int import)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700373{
374 int rc;
375
376 rc = smk_fill_rule(data, data + SMK_LABELLEN,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100377 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
378 import, SMK_LABELLEN);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700379 return rc;
380}
381
382/**
383 * smk_parse_long_rule - parse Smack rule from rule string
384 * @data: string to be parsed, null terminated
Rafal Krypae05b6f92013-01-10 19:42:00 +0100385 * @rule: Will be filled with Smack parsed rule
Casey Schauflerf7112e62012-05-06 15:22:02 -0700386 * @import: if non-zero, import labels
Rafal Krypa10289b02013-08-09 11:47:07 +0200387 * @tokens: numer of substrings expected in data
Casey Schauflerf7112e62012-05-06 15:22:02 -0700388 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200389 * Returns number of processed bytes on success, -ERRNO on failure.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700390 */
Rafal Krypa10289b02013-08-09 11:47:07 +0200391static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
392 int import, int tokens)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700393{
Rafal Krypa10289b02013-08-09 11:47:07 +0200394 ssize_t cnt = 0;
395 char *tok[4];
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200396 int rc;
Rafal Krypa10289b02013-08-09 11:47:07 +0200397 int i;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700398
Rafal Krypa10289b02013-08-09 11:47:07 +0200399 /*
400 * Parsing the rule in-place, filling all white-spaces with '\0'
401 */
402 for (i = 0; i < tokens; ++i) {
403 while (isspace(data[cnt]))
404 data[cnt++] = '\0';
Alan Cox3b9fc372012-07-26 14:47:11 -0700405
Rafal Krypa10289b02013-08-09 11:47:07 +0200406 if (data[cnt] == '\0')
407 /* Unexpected end of data */
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200408 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700409
Rafal Krypa10289b02013-08-09 11:47:07 +0200410 tok[i] = data + cnt;
411
412 while (data[cnt] && !isspace(data[cnt]))
413 ++cnt;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100414 }
Rafal Krypa10289b02013-08-09 11:47:07 +0200415 while (isspace(data[cnt]))
416 data[cnt++] = '\0';
Casey Schauflerf7112e62012-05-06 15:22:02 -0700417
Rafal Krypa10289b02013-08-09 11:47:07 +0200418 while (i < 4)
419 tok[i++] = NULL;
420
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200421 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
422 return rc == 0 ? cnt : rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700423}
424
425#define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
426#define SMK_LONG_FMT 1 /* Variable long label format */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100427#define SMK_CHANGE_FMT 2 /* Rule modification format */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700428/**
429 * smk_write_rules_list - write() for any /smack rule file
Randy Dunlap251a2a92009-02-18 11:42:33 -0800430 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800431 * @buf: where to get the data from
432 * @count: bytes sent
433 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800434 * @rule_list: the list of rules to write to
435 * @rule_lock: lock for the rule list
Rafal Krypae05b6f92013-01-10 19:42:00 +0100436 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
Casey Schauflere114e472008-02-04 22:29:50 -0800437 *
438 * Get one smack access rule from above.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700439 * The format for SMK_LONG_FMT is:
440 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
441 * The format for SMK_FIXED24_FMT is exactly:
442 * "subject object rwxat"
Rafal Krypae05b6f92013-01-10 19:42:00 +0100443 * The format for SMK_CHANGE_FMT is:
444 * "subject<whitespace>object<whitespace>
445 * acc_enable<whitespace>acc_disable[<whitespace>...]"
Casey Schauflere114e472008-02-04 22:29:50 -0800446 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700447static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
448 size_t count, loff_t *ppos,
449 struct list_head *rule_list,
450 struct mutex *rule_lock, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800451{
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200452 struct smack_parsed_rule rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800453 char *data;
Rafal Krypa10289b02013-08-09 11:47:07 +0200454 int rc;
455 int trunc = 0;
456 int tokens;
457 ssize_t cnt = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800458
459 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800460 * No partial writes.
461 * Enough data must be present.
462 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200463 if (*ppos != 0)
464 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -0800465
Casey Schauflerf7112e62012-05-06 15:22:02 -0700466 if (format == SMK_FIXED24_FMT) {
467 /*
468 * Minor hack for backward compatibility
469 */
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700470 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700471 return -EINVAL;
Rafal Krypa10289b02013-08-09 11:47:07 +0200472 } else {
473 if (count >= PAGE_SIZE) {
474 count = PAGE_SIZE - 1;
475 trunc = 1;
476 }
477 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700478
Al Viro16e5c1f2015-12-24 00:06:05 -0500479 data = memdup_user_nul(buf, count);
480 if (IS_ERR(data))
481 return PTR_ERR(data);
Casey Schauflere114e472008-02-04 22:29:50 -0800482
Rafal Krypa10289b02013-08-09 11:47:07 +0200483 /*
484 * In case of parsing only part of user buf,
485 * avoid having partial rule at the data buffer
486 */
487 if (trunc) {
488 while (count > 0 && (data[count - 1] != '\n'))
489 --count;
490 if (count == 0) {
491 rc = -EINVAL;
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200492 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200493 }
494 }
495
496 data[count] = '\0';
497 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
498 while (cnt < count) {
499 if (format == SMK_FIXED24_FMT) {
500 rc = smk_parse_rule(data, &rule, 1);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200501 if (rc < 0)
Rafal Krypa10289b02013-08-09 11:47:07 +0200502 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200503 cnt = count;
504 } else {
505 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200506 if (rc < 0)
507 goto out;
508 if (rc == 0) {
Rafal Krypa10289b02013-08-09 11:47:07 +0200509 rc = -EINVAL;
510 goto out;
511 }
512 cnt += rc;
513 }
514
515 if (rule_list == NULL)
516 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
Vishal Goel460d95a2019-03-07 16:55:24 +0530517 &rule.smk_subject->smk_rules_lock);
Rafal Krypa10289b02013-08-09 11:47:07 +0200518 else
Vishal Goel460d95a2019-03-07 16:55:24 +0530519 rc = smk_set_access(&rule, rule_list, rule_lock);
Rafal Krypa10289b02013-08-09 11:47:07 +0200520
521 if (rc)
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200522 goto out;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700523 }
524
Rafal Krypa10289b02013-08-09 11:47:07 +0200525 rc = cnt;
Casey Schauflere114e472008-02-04 22:29:50 -0800526out:
527 kfree(data);
528 return rc;
529}
530
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800531/*
Casey Schaufler40809562011-11-10 15:02:22 -0800532 * Core logic for smackfs seq list operations.
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800533 */
534
Casey Schaufler40809562011-11-10 15:02:22 -0800535static void *smk_seq_start(struct seq_file *s, loff_t *pos,
536 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800537{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700538 struct list_head *list;
Rafal Krypa01fa8472015-05-21 18:24:31 +0200539 int i = *pos;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700540
Rafal Krypa01fa8472015-05-21 18:24:31 +0200541 rcu_read_lock();
542 for (list = rcu_dereference(list_next_rcu(head));
543 list != head;
544 list = rcu_dereference(list_next_rcu(list))) {
545 if (i-- == 0)
546 return list;
547 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700548
Rafal Krypa01fa8472015-05-21 18:24:31 +0200549 return NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800550}
551
Casey Schaufler40809562011-11-10 15:02:22 -0800552static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
553 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800554{
555 struct list_head *list = v;
556
Rafal Krypa01fa8472015-05-21 18:24:31 +0200557 ++*pos;
558 list = rcu_dereference(list_next_rcu(list));
559
560 return (list == head) ? NULL : list;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800561}
562
Casey Schaufler40809562011-11-10 15:02:22 -0800563static void smk_seq_stop(struct seq_file *s, void *v)
564{
Rafal Krypa01fa8472015-05-21 18:24:31 +0200565 rcu_read_unlock();
Casey Schaufler40809562011-11-10 15:02:22 -0800566}
567
Casey Schauflerf7112e62012-05-06 15:22:02 -0700568static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
Casey Schaufler40809562011-11-10 15:02:22 -0800569{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700570 /*
571 * Don't show any rules with label names too long for
572 * interface file (/smack/load or /smack/load2)
573 * because you should expect to be able to write
574 * anything you read back.
575 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700576 if (strlen(srp->smk_subject->smk_known) >= max ||
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200577 strlen(srp->smk_object->smk_known) >= max)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700578 return;
Casey Schaufler40809562011-11-10 15:02:22 -0800579
Rafal Krypa65ee7f42012-07-09 19:36:34 +0200580 if (srp->smk_access == 0)
581 return;
582
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200583 seq_printf(s, "%s %s",
584 srp->smk_subject->smk_known,
585 srp->smk_object->smk_known);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800586
587 seq_putc(s, ' ');
588
589 if (srp->smk_access & MAY_READ)
590 seq_putc(s, 'r');
591 if (srp->smk_access & MAY_WRITE)
592 seq_putc(s, 'w');
593 if (srp->smk_access & MAY_EXEC)
594 seq_putc(s, 'x');
595 if (srp->smk_access & MAY_APPEND)
596 seq_putc(s, 'a');
597 if (srp->smk_access & MAY_TRANSMUTE)
598 seq_putc(s, 't');
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700599 if (srp->smk_access & MAY_LOCK)
600 seq_putc(s, 'l');
Casey Schauflerd166c802014-08-27 14:51:27 -0700601 if (srp->smk_access & MAY_BRINGUP)
602 seq_putc(s, 'b');
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800603
604 seq_putc(s, '\n');
Casey Schauflerf7112e62012-05-06 15:22:02 -0700605}
606
607/*
608 * Seq_file read operations for /smack/load
609 */
610
611static void *load2_seq_start(struct seq_file *s, loff_t *pos)
612{
Vishal Goel460d95a2019-03-07 16:55:24 +0530613 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700614}
615
616static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
617{
Vishal Goel460d95a2019-03-07 16:55:24 +0530618 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700619}
620
621static int load_seq_show(struct seq_file *s, void *v)
622{
623 struct list_head *list = v;
Vishal Goel460d95a2019-03-07 16:55:24 +0530624 struct smack_rule *srp;
625 struct smack_known *skp =
626 list_entry_rcu(list, struct smack_known, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700627
Vishal Goel460d95a2019-03-07 16:55:24 +0530628 list_for_each_entry_rcu(srp, &skp->smk_rules, list)
629 smk_rule_show(s, srp, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800630
631 return 0;
632}
633
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800634static const struct seq_operations load_seq_ops = {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700635 .start = load2_seq_start,
636 .next = load2_seq_next,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800637 .show = load_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800638 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800639};
640
641/**
642 * smk_open_load - open() for /smack/load
643 * @inode: inode structure representing file
644 * @file: "load" file pointer
645 *
646 * For reading, use load_seq_* seq_file reading operations.
647 */
648static int smk_open_load(struct inode *inode, struct file *file)
649{
650 return seq_open(file, &load_seq_ops);
651}
652
653/**
654 * smk_write_load - write() for /smack/load
655 * @file: file pointer, not actually used
656 * @buf: where to get the data from
657 * @count: bytes sent
658 * @ppos: where to start - must be 0
659 *
660 */
661static ssize_t smk_write_load(struct file *file, const char __user *buf,
662 size_t count, loff_t *ppos)
663{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800664 /*
665 * Must have privilege.
666 * No partial writes.
667 * Enough data must be present.
668 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700669 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800670 return -EPERM;
671
Casey Schauflerf7112e62012-05-06 15:22:02 -0700672 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
673 SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800674}
675
Casey Schauflere114e472008-02-04 22:29:50 -0800676static const struct file_operations smk_load_ops = {
677 .open = smk_open_load,
678 .read = seq_read,
679 .llseek = seq_lseek,
680 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700681 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800682};
683
684/**
685 * smk_cipso_doi - initialize the CIPSO domain
686 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700687static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800688{
689 int rc;
690 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500691 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800692
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500693 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800694
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500695 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800696 if (rc != 0)
697 printk(KERN_WARNING "%s:%d remove rc = %d\n",
698 __func__, __LINE__, rc);
699
700 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
701 if (doip == NULL)
702 panic("smack: Failed to initialize cipso DOI.\n");
703 doip->map.std = NULL;
704 doip->doi = smk_cipso_doi_value;
705 doip->type = CIPSO_V4_MAP_PASS;
706 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
707 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
708 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
709
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500710 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400711 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500712 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800713 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400714 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500715 return;
716 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500717 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500718 if (rc != 0) {
719 printk(KERN_WARNING "%s:%d map add rc = %d\n",
720 __func__, __LINE__, rc);
721 kfree(doip);
722 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400723 }
Casey Schauflere114e472008-02-04 22:29:50 -0800724}
725
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800726/**
727 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800728 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800729 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700730static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800731{
732 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500733 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800734
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500735 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800736
737 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500738 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800739 if (rc != 0)
740 printk(KERN_WARNING "%s:%d remove rc = %d\n",
741 __func__, __LINE__, rc);
742 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700743 if (smack_net_ambient == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700744 smack_net_ambient = &smack_known_floor;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800745
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700746 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500747 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800748 if (rc != 0)
749 printk(KERN_WARNING "%s:%d add rc = %d\n",
750 __func__, __LINE__, rc);
751}
752
Casey Schauflere114e472008-02-04 22:29:50 -0800753/*
754 * Seq_file read operations for /smack/cipso
755 */
756
757static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
758{
Casey Schaufler40809562011-11-10 15:02:22 -0800759 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800760}
761
762static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
763{
Casey Schaufler40809562011-11-10 15:02:22 -0800764 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800765}
766
767/*
768 * Print cipso labels in format:
769 * label level[/cat[,cat]]
770 */
771static int cipso_seq_show(struct seq_file *s, void *v)
772{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100773 struct list_head *list = v;
774 struct smack_known *skp =
Rafal Krypa01fa8472015-05-21 18:24:31 +0200775 list_entry_rcu(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400776 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800777 char sep = '/';
Casey Schauflere114e472008-02-04 22:29:50 -0800778 int i;
Casey Schauflere114e472008-02-04 22:29:50 -0800779
Casey Schauflerf7112e62012-05-06 15:22:02 -0700780 /*
781 * Don't show a label that could not have been set using
782 * /smack/cipso. This is in support of the notion that
783 * anything read from /smack/cipso ought to be writeable
784 * to /smack/cipso.
785 *
786 * /smack/cipso2 should be used instead.
787 */
788 if (strlen(skp->smk_known) >= SMK_LABELLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800789 return 0;
790
Casey Schauflerf7112e62012-05-06 15:22:02 -0700791 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
Casey Schauflere114e472008-02-04 22:29:50 -0800792
Paul Moore4fbe63d2014-08-01 11:17:37 -0400793 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
794 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700795 seq_printf(s, "%c%d", sep, i);
796 sep = ',';
797 }
Casey Schauflere114e472008-02-04 22:29:50 -0800798
799 seq_putc(s, '\n');
800
801 return 0;
802}
803
James Morris88e9d342009-09-22 16:43:43 -0700804static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800805 .start = cipso_seq_start,
Casey Schauflere114e472008-02-04 22:29:50 -0800806 .next = cipso_seq_next,
807 .show = cipso_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800808 .stop = smk_seq_stop,
Casey Schauflere114e472008-02-04 22:29:50 -0800809};
810
811/**
812 * smk_open_cipso - open() for /smack/cipso
813 * @inode: inode structure representing file
814 * @file: "cipso" file pointer
815 *
816 * Connect our cipso_seq_* operations with /smack/cipso
817 * file_operations
818 */
819static int smk_open_cipso(struct inode *inode, struct file *file)
820{
821 return seq_open(file, &cipso_seq_ops);
822}
823
824/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700825 * smk_set_cipso - do the work for write() for cipso and cipso2
Randy Dunlap251a2a92009-02-18 11:42:33 -0800826 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800827 * @buf: where to get the data from
828 * @count: bytes sent
829 * @ppos: where to start
Casey Schauflerf7112e62012-05-06 15:22:02 -0700830 * @format: /smack/cipso or /smack/cipso2
Casey Schauflere114e472008-02-04 22:29:50 -0800831 *
832 * Accepts only one cipso rule per write call.
833 * Returns number of bytes written or error code, as appropriate
834 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700835static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
836 size_t count, loff_t *ppos, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800837{
838 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700839 struct netlbl_lsm_secattr ncats;
840 char mapcatset[SMK_CIPSOLEN];
Casey Schauflere114e472008-02-04 22:29:50 -0800841 int maplevel;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700842 unsigned int cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800843 int catlen;
844 ssize_t rc = -EINVAL;
845 char *data = NULL;
846 char *rule;
847 int ret;
848 int i;
849
850 /*
851 * Must have privilege.
852 * No partial writes.
853 * Enough data must be present.
854 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700855 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -0800856 return -EPERM;
857 if (*ppos != 0)
858 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700859 if (format == SMK_FIXED24_FMT &&
860 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
Casey Schauflere114e472008-02-04 22:29:50 -0800861 return -EINVAL;
862
Al Viro16e5c1f2015-12-24 00:06:05 -0500863 data = memdup_user_nul(buf, count);
864 if (IS_ERR(data))
865 return PTR_ERR(data);
Casey Schauflere114e472008-02-04 22:29:50 -0800866
Casey Schauflere114e472008-02-04 22:29:50 -0800867 rule = data;
868 /*
869 * Only allow one writer at a time. Writes should be
870 * quite rare and small in any case.
871 */
872 mutex_lock(&smack_cipso_lock);
873
874 skp = smk_import_entry(rule, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200875 if (IS_ERR(skp)) {
876 rc = PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800877 goto out;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200878 }
Casey Schauflere114e472008-02-04 22:29:50 -0800879
Casey Schauflerf7112e62012-05-06 15:22:02 -0700880 if (format == SMK_FIXED24_FMT)
881 rule += SMK_LABELLEN;
882 else
Passion,Zhao0fcfee62013-06-03 11:42:24 +0800883 rule += strlen(skp->smk_known) + 1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700884
Casey Schauflere114e472008-02-04 22:29:50 -0800885 ret = sscanf(rule, "%d", &maplevel);
886 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
887 goto out;
888
889 rule += SMK_DIGITLEN;
890 ret = sscanf(rule, "%d", &catlen);
891 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
892 goto out;
893
Casey Schauflerf7112e62012-05-06 15:22:02 -0700894 if (format == SMK_FIXED24_FMT &&
895 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800896 goto out;
897
898 memset(mapcatset, 0, sizeof(mapcatset));
899
900 for (i = 0; i < catlen; i++) {
901 rule += SMK_DIGITLEN;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700902 ret = sscanf(rule, "%u", &cat);
Casey Schaufler677264e2013-06-28 13:47:07 -0700903 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
Casey Schauflere114e472008-02-04 22:29:50 -0800904 goto out;
905
906 smack_catset_bit(cat, mapcatset);
907 }
908
Casey Schauflerf7112e62012-05-06 15:22:02 -0700909 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
910 if (rc >= 0) {
Paul Moore4fbe63d2014-08-01 11:17:37 -0400911 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700912 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
913 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
914 rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -0800915 }
916
Casey Schauflere114e472008-02-04 22:29:50 -0800917out:
918 mutex_unlock(&smack_cipso_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800919 kfree(data);
920 return rc;
921}
922
Casey Schauflerf7112e62012-05-06 15:22:02 -0700923/**
924 * smk_write_cipso - write() for /smack/cipso
925 * @file: file pointer, not actually used
926 * @buf: where to get the data from
927 * @count: bytes sent
928 * @ppos: where to start
929 *
930 * Accepts only one cipso rule per write call.
931 * Returns number of bytes written or error code, as appropriate
932 */
933static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
934 size_t count, loff_t *ppos)
935{
936 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
937}
938
Casey Schauflere114e472008-02-04 22:29:50 -0800939static const struct file_operations smk_cipso_ops = {
940 .open = smk_open_cipso,
941 .read = seq_read,
942 .llseek = seq_lseek,
943 .write = smk_write_cipso,
944 .release = seq_release,
945};
946
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500947/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700948 * Seq_file read operations for /smack/cipso2
949 */
950
951/*
952 * Print cipso labels in format:
953 * label level[/cat[,cat]]
954 */
955static int cipso2_seq_show(struct seq_file *s, void *v)
956{
957 struct list_head *list = v;
958 struct smack_known *skp =
Rafal Krypa01fa8472015-05-21 18:24:31 +0200959 list_entry_rcu(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400960 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700961 char sep = '/';
962 int i;
963
964 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
965
Paul Moore4fbe63d2014-08-01 11:17:37 -0400966 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
967 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700968 seq_printf(s, "%c%d", sep, i);
969 sep = ',';
970 }
971
972 seq_putc(s, '\n');
973
974 return 0;
975}
976
977static const struct seq_operations cipso2_seq_ops = {
978 .start = cipso_seq_start,
979 .next = cipso_seq_next,
980 .show = cipso2_seq_show,
981 .stop = smk_seq_stop,
982};
983
984/**
985 * smk_open_cipso2 - open() for /smack/cipso2
986 * @inode: inode structure representing file
987 * @file: "cipso2" file pointer
988 *
989 * Connect our cipso_seq_* operations with /smack/cipso2
990 * file_operations
991 */
992static int smk_open_cipso2(struct inode *inode, struct file *file)
993{
994 return seq_open(file, &cipso2_seq_ops);
995}
996
997/**
998 * smk_write_cipso2 - write() for /smack/cipso2
999 * @file: file pointer, not actually used
1000 * @buf: where to get the data from
1001 * @count: bytes sent
1002 * @ppos: where to start
1003 *
1004 * Accepts only one cipso rule per write call.
1005 * Returns number of bytes written or error code, as appropriate
1006 */
1007static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1008 size_t count, loff_t *ppos)
1009{
1010 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1011}
1012
1013static const struct file_operations smk_cipso2_ops = {
1014 .open = smk_open_cipso2,
1015 .read = seq_read,
1016 .llseek = seq_lseek,
1017 .write = smk_write_cipso2,
1018 .release = seq_release,
1019};
1020
1021/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001022 * Seq_file read operations for /smack/netlabel
1023 */
1024
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001025static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001026{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001027 return smk_seq_start(s, pos, &smk_net4addr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001028}
1029
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001030static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001031{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001032 return smk_seq_next(s, v, pos, &smk_net4addr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001033}
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001034
1035/*
1036 * Print host/label pairs
1037 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001038static int net4addr_seq_show(struct seq_file *s, void *v)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001039{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001040 struct list_head *list = v;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001041 struct smk_net4addr *skp =
1042 list_entry_rcu(list, struct smk_net4addr, list);
1043 char *kp = SMACK_CIPSO_OPTION;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001044
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001045 if (skp->smk_label != NULL)
1046 kp = skp->smk_label->smk_known;
1047 seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
1048 skp->smk_masks, kp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001049
1050 return 0;
1051}
1052
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001053static const struct seq_operations net4addr_seq_ops = {
1054 .start = net4addr_seq_start,
1055 .next = net4addr_seq_next,
1056 .show = net4addr_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001057 .stop = smk_seq_stop,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001058};
1059
1060/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001061 * smk_open_net4addr - open() for /smack/netlabel
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001062 * @inode: inode structure representing file
1063 * @file: "netlabel" file pointer
1064 *
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001065 * Connect our net4addr_seq_* operations with /smack/netlabel
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001066 * file_operations
1067 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001068static int smk_open_net4addr(struct inode *inode, struct file *file)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001069{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001070 return seq_open(file, &net4addr_seq_ops);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001071}
1072
1073/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001074 * smk_net4addr_insert
etienne113a0e42009-03-04 07:33:51 +01001075 * @new : netlabel to insert
1076 *
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001077 * This helper insert netlabel in the smack_net4addrs list
etienne113a0e42009-03-04 07:33:51 +01001078 * sorted by netmask length (longest to smallest)
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001079 * locked by &smk_net4addr_lock in smk_write_net4addr
Etienne Basset7198e2e2009-03-24 20:53:24 +01001080 *
etienne113a0e42009-03-04 07:33:51 +01001081 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001082static void smk_net4addr_insert(struct smk_net4addr *new)
etienne113a0e42009-03-04 07:33:51 +01001083{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001084 struct smk_net4addr *m;
1085 struct smk_net4addr *m_next;
etienne113a0e42009-03-04 07:33:51 +01001086
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001087 if (list_empty(&smk_net4addr_list)) {
1088 list_add_rcu(&new->list, &smk_net4addr_list);
etienne113a0e42009-03-04 07:33:51 +01001089 return;
1090 }
1091
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001092 m = list_entry_rcu(smk_net4addr_list.next,
1093 struct smk_net4addr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001094
etienne113a0e42009-03-04 07:33:51 +01001095 /* the comparison '>' is a bit hacky, but works */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001096 if (new->smk_masks > m->smk_masks) {
1097 list_add_rcu(&new->list, &smk_net4addr_list);
etienne113a0e42009-03-04 07:33:51 +01001098 return;
1099 }
Etienne Basset7198e2e2009-03-24 20:53:24 +01001100
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001101 list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
1102 if (list_is_last(&m->list, &smk_net4addr_list)) {
Etienne Basset7198e2e2009-03-24 20:53:24 +01001103 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001104 return;
1105 }
Jiri Pirko05725f72009-04-14 20:17:16 +02001106 m_next = list_entry_rcu(m->list.next,
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001107 struct smk_net4addr, list);
1108 if (new->smk_masks > m_next->smk_masks) {
Etienne Basset7198e2e2009-03-24 20:53:24 +01001109 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001110 return;
1111 }
1112 }
1113}
1114
1115
1116/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001117 * smk_write_net4addr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -08001118 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001119 * @buf: where to get the data from
1120 * @count: bytes sent
1121 * @ppos: where to start
1122 *
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001123 * Accepts only one net4addr per write call.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001124 * Returns number of bytes written or error code, as appropriate
1125 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001126static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001127 size_t count, loff_t *ppos)
1128{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001129 struct smk_net4addr *snp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001130 struct sockaddr_in newname;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001131 char *smack;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001132 struct smack_known *skp = NULL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001133 char *data;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001134 char *host = (char *)&newname.sin_addr.s_addr;
1135 int rc;
1136 struct netlbl_audit audit_info;
1137 struct in_addr mask;
1138 unsigned int m;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001139 unsigned int masks;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001140 int found;
etienne113a0e42009-03-04 07:33:51 +01001141 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001142 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +01001143 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001144
1145 /*
1146 * Must have privilege.
1147 * No partial writes.
1148 * Enough data must be present.
1149 * "<addr/mask, as a.b.c.d/e><space><label>"
1150 * "<addr, as a.b.c.d><space><label>"
1151 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001152 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001153 return -EPERM;
1154 if (*ppos != 0)
1155 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001156 if (count < SMK_NETLBLADDRMIN)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001157 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001158
Al Viro16e5c1f2015-12-24 00:06:05 -05001159 data = memdup_user_nul(buf, count);
1160 if (IS_ERR(data))
1161 return PTR_ERR(data);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001162
1163 smack = kzalloc(count + 1, GFP_KERNEL);
1164 if (smack == NULL) {
1165 rc = -ENOMEM;
1166 goto free_data_out;
1167 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001168
Toralf Försterec554fa2014-04-27 19:33:34 +02001169 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001170 &host[0], &host[1], &host[2], &host[3], &masks, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001171 if (rc != 6) {
1172 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1173 &host[0], &host[1], &host[2], &host[3], smack);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001174 if (rc != 5) {
1175 rc = -EINVAL;
1176 goto free_out;
1177 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001178 m = BEBITS;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001179 masks = 32;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001180 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001181 if (masks > BEBITS) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001182 rc = -EINVAL;
1183 goto free_out;
1184 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001185
Casey Schauflerf7112e62012-05-06 15:22:02 -07001186 /*
1187 * If smack begins with '-', it is an option, don't import it
1188 */
Etienne Basset43031542009-03-27 17:11:01 -04001189 if (smack[0] != '-') {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001190 skp = smk_import_entry(smack, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001191 if (IS_ERR(skp)) {
1192 rc = PTR_ERR(skp);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001193 goto free_out;
1194 }
Etienne Basset43031542009-03-27 17:11:01 -04001195 } else {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001196 /*
1197 * Only the -CIPSO option is supported for IPv4
1198 */
1199 if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001200 rc = -EINVAL;
1201 goto free_out;
1202 }
Etienne Basset43031542009-03-27 17:11:01 -04001203 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001204
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001205 for (m = masks, temp_mask = 0; m > 0; m--) {
etienne113a0e42009-03-04 07:33:51 +01001206 temp_mask |= mask_bits;
1207 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001208 }
etienne113a0e42009-03-04 07:33:51 +01001209 mask.s_addr = cpu_to_be32(temp_mask);
1210
1211 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001212 /*
1213 * Only allow one writer at a time. Writes should be
1214 * quite rare and small in any case.
1215 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001216 mutex_lock(&smk_net4addr_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001217
1218 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001219 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001220 found = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001221 list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
1222 if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
Etienne Basset7198e2e2009-03-24 20:53:24 +01001223 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001224 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001225 }
1226 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001227 smk_netlabel_audit_set(&audit_info);
1228
Etienne Basset7198e2e2009-03-24 20:53:24 +01001229 if (found == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001230 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1231 if (snp == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001232 rc = -ENOMEM;
1233 else {
1234 rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001235 snp->smk_host.s_addr = newname.sin_addr.s_addr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001236 snp->smk_mask.s_addr = mask.s_addr;
1237 snp->smk_label = skp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001238 snp->smk_masks = masks;
1239 smk_net4addr_insert(snp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001240 }
1241 } else {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001242 /*
1243 * Delete the unlabeled entry, only if the previous label
1244 * wasn't the special CIPSO option
1245 */
1246 if (snp->smk_label != NULL)
Etienne Basset43031542009-03-27 17:11:01 -04001247 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001248 &snp->smk_host, &snp->smk_mask,
Etienne Basset43031542009-03-27 17:11:01 -04001249 PF_INET, &audit_info);
1250 else
1251 rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001252 snp->smk_label = skp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001253 }
1254
1255 /*
1256 * Now tell netlabel about the single label nature of
1257 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -04001258 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001259 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001260 if (rc == 0 && skp != NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001261 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001262 &snp->smk_host, &snp->smk_mask, PF_INET,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001263 snp->smk_label->smk_secid, &audit_info);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001264
1265 if (rc == 0)
1266 rc = count;
1267
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001268 mutex_unlock(&smk_net4addr_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001269
Casey Schauflerf7112e62012-05-06 15:22:02 -07001270free_out:
1271 kfree(smack);
1272free_data_out:
1273 kfree(data);
1274
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001275 return rc;
1276}
1277
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001278static const struct file_operations smk_net4addr_ops = {
1279 .open = smk_open_net4addr,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001280 .read = seq_read,
1281 .llseek = seq_lseek,
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001282 .write = smk_write_net4addr,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001283 .release = seq_release,
1284};
1285
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001286#if IS_ENABLED(CONFIG_IPV6)
1287/*
1288 * Seq_file read operations for /smack/netlabel6
1289 */
1290
1291static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
1292{
1293 return smk_seq_start(s, pos, &smk_net6addr_list);
1294}
1295
1296static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1297{
1298 return smk_seq_next(s, v, pos, &smk_net6addr_list);
1299}
1300
1301/*
1302 * Print host/label pairs
1303 */
1304static int net6addr_seq_show(struct seq_file *s, void *v)
1305{
1306 struct list_head *list = v;
1307 struct smk_net6addr *skp =
1308 list_entry(list, struct smk_net6addr, list);
1309
1310 if (skp->smk_label != NULL)
1311 seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
1312 skp->smk_label->smk_known);
1313
1314 return 0;
1315}
1316
1317static const struct seq_operations net6addr_seq_ops = {
1318 .start = net6addr_seq_start,
1319 .next = net6addr_seq_next,
1320 .show = net6addr_seq_show,
1321 .stop = smk_seq_stop,
1322};
1323
1324/**
1325 * smk_open_net6addr - open() for /smack/netlabel
1326 * @inode: inode structure representing file
1327 * @file: "netlabel" file pointer
1328 *
1329 * Connect our net6addr_seq_* operations with /smack/netlabel
1330 * file_operations
1331 */
1332static int smk_open_net6addr(struct inode *inode, struct file *file)
1333{
1334 return seq_open(file, &net6addr_seq_ops);
1335}
1336
1337/**
1338 * smk_net6addr_insert
1339 * @new : entry to insert
1340 *
1341 * This inserts an entry in the smack_net6addrs list
1342 * sorted by netmask length (longest to smallest)
1343 * locked by &smk_net6addr_lock in smk_write_net6addr
1344 *
1345 */
1346static void smk_net6addr_insert(struct smk_net6addr *new)
1347{
1348 struct smk_net6addr *m_next;
1349 struct smk_net6addr *m;
1350
1351 if (list_empty(&smk_net6addr_list)) {
1352 list_add_rcu(&new->list, &smk_net6addr_list);
1353 return;
1354 }
1355
1356 m = list_entry_rcu(smk_net6addr_list.next,
1357 struct smk_net6addr, list);
1358
1359 if (new->smk_masks > m->smk_masks) {
1360 list_add_rcu(&new->list, &smk_net6addr_list);
1361 return;
1362 }
1363
1364 list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
1365 if (list_is_last(&m->list, &smk_net6addr_list)) {
1366 list_add_rcu(&new->list, &m->list);
1367 return;
1368 }
1369 m_next = list_entry_rcu(m->list.next,
1370 struct smk_net6addr, list);
1371 if (new->smk_masks > m_next->smk_masks) {
1372 list_add_rcu(&new->list, &m->list);
1373 return;
1374 }
1375 }
1376}
1377
1378
1379/**
1380 * smk_write_net6addr - write() for /smack/netlabel
1381 * @file: file pointer, not actually used
1382 * @buf: where to get the data from
1383 * @count: bytes sent
1384 * @ppos: where to start
1385 *
1386 * Accepts only one net6addr per write call.
1387 * Returns number of bytes written or error code, as appropriate
1388 */
1389static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
1390 size_t count, loff_t *ppos)
1391{
1392 struct smk_net6addr *snp;
1393 struct in6_addr newname;
1394 struct in6_addr fullmask;
1395 struct smack_known *skp = NULL;
1396 char *smack;
1397 char *data;
1398 int rc = 0;
1399 int found = 0;
1400 int i;
1401 unsigned int scanned[8];
1402 unsigned int m;
1403 unsigned int mask = 128;
1404
1405 /*
1406 * Must have privilege.
1407 * No partial writes.
1408 * Enough data must be present.
1409 * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
1410 * "<addr, as a:b:c:d:e:f:g:h><space><label>"
1411 */
1412 if (!smack_privileged(CAP_MAC_ADMIN))
1413 return -EPERM;
1414 if (*ppos != 0)
1415 return -EINVAL;
1416 if (count < SMK_NETLBLADDRMIN)
1417 return -EINVAL;
1418
Al Viro16e5c1f2015-12-24 00:06:05 -05001419 data = memdup_user_nul(buf, count);
1420 if (IS_ERR(data))
1421 return PTR_ERR(data);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001422
1423 smack = kzalloc(count + 1, GFP_KERNEL);
1424 if (smack == NULL) {
1425 rc = -ENOMEM;
1426 goto free_data_out;
1427 }
1428
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001429 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
1430 &scanned[0], &scanned[1], &scanned[2], &scanned[3],
1431 &scanned[4], &scanned[5], &scanned[6], &scanned[7],
1432 &mask, smack);
1433 if (i != 10) {
1434 i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
1435 &scanned[0], &scanned[1], &scanned[2],
1436 &scanned[3], &scanned[4], &scanned[5],
1437 &scanned[6], &scanned[7], smack);
1438 if (i != 9) {
1439 rc = -EINVAL;
1440 goto free_out;
1441 }
1442 }
1443 if (mask > 128) {
1444 rc = -EINVAL;
1445 goto free_out;
1446 }
1447 for (i = 0; i < 8; i++) {
1448 if (scanned[i] > 0xffff) {
1449 rc = -EINVAL;
1450 goto free_out;
1451 }
1452 newname.s6_addr16[i] = htons(scanned[i]);
1453 }
1454
1455 /*
1456 * If smack begins with '-', it is an option, don't import it
1457 */
1458 if (smack[0] != '-') {
1459 skp = smk_import_entry(smack, 0);
Lukasz Pawelczyk5f2bfe22015-08-25 12:39:46 +02001460 if (IS_ERR(skp)) {
1461 rc = PTR_ERR(skp);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07001462 goto free_out;
1463 }
1464 } else {
1465 /*
1466 * Only -DELETE is supported for IPv6
1467 */
1468 if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
1469 rc = -EINVAL;
1470 goto free_out;
1471 }
1472 }
1473
1474 for (i = 0, m = mask; i < 8; i++) {
1475 if (m >= 16) {
1476 fullmask.s6_addr16[i] = 0xffff;
1477 m -= 16;
1478 } else if (m > 0) {
1479 fullmask.s6_addr16[i] = (1 << m) - 1;
1480 m = 0;
1481 } else
1482 fullmask.s6_addr16[i] = 0;
1483 newname.s6_addr16[i] &= fullmask.s6_addr16[i];
1484 }
1485
1486 /*
1487 * Only allow one writer at a time. Writes should be
1488 * quite rare and small in any case.
1489 */
1490 mutex_lock(&smk_net6addr_lock);
1491 /*
1492 * Try to find the prefix in the list
1493 */
1494 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
1495 if (mask != snp->smk_masks)
1496 continue;
1497 for (found = 1, i = 0; i < 8; i++) {
1498 if (newname.s6_addr16[i] !=
1499 snp->smk_host.s6_addr16[i]) {
1500 found = 0;
1501 break;
1502 }
1503 }
1504 if (found == 1)
1505 break;
1506 }
1507 if (found == 0) {
1508 snp = kzalloc(sizeof(*snp), GFP_KERNEL);
1509 if (snp == NULL)
1510 rc = -ENOMEM;
1511 else {
1512 snp->smk_host = newname;
1513 snp->smk_mask = fullmask;
1514 snp->smk_masks = mask;
1515 snp->smk_label = skp;
1516 smk_net6addr_insert(snp);
1517 }
1518 } else {
1519 snp->smk_label = skp;
1520 }
1521
1522 if (rc == 0)
1523 rc = count;
1524
1525 mutex_unlock(&smk_net6addr_lock);
1526
1527free_out:
1528 kfree(smack);
1529free_data_out:
1530 kfree(data);
1531
1532 return rc;
1533}
1534
1535static const struct file_operations smk_net6addr_ops = {
1536 .open = smk_open_net6addr,
1537 .read = seq_read,
1538 .llseek = seq_lseek,
1539 .write = smk_write_net6addr,
1540 .release = seq_release,
1541};
1542#endif /* CONFIG_IPV6 */
1543
Casey Schauflere114e472008-02-04 22:29:50 -08001544/**
1545 * smk_read_doi - read() for /smack/doi
1546 * @filp: file pointer, not actually used
1547 * @buf: where to put the result
1548 * @count: maximum to send along
1549 * @ppos: where to start
1550 *
1551 * Returns number of bytes read or error code, as appropriate
1552 */
1553static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1554 size_t count, loff_t *ppos)
1555{
1556 char temp[80];
1557 ssize_t rc;
1558
1559 if (*ppos != 0)
1560 return 0;
1561
1562 sprintf(temp, "%d", smk_cipso_doi_value);
1563 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1564
1565 return rc;
1566}
1567
1568/**
1569 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001570 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001571 * @buf: where to get the data from
1572 * @count: bytes sent
1573 * @ppos: where to start
1574 *
1575 * Returns number of bytes written or error code, as appropriate
1576 */
1577static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1578 size_t count, loff_t *ppos)
1579{
1580 char temp[80];
1581 int i;
1582
Casey Schaufler1880eff2012-06-05 15:28:30 -07001583 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001584 return -EPERM;
1585
1586 if (count >= sizeof(temp) || count == 0)
1587 return -EINVAL;
1588
1589 if (copy_from_user(temp, buf, count) != 0)
1590 return -EFAULT;
1591
1592 temp[count] = '\0';
1593
1594 if (sscanf(temp, "%d", &i) != 1)
1595 return -EINVAL;
1596
1597 smk_cipso_doi_value = i;
1598
1599 smk_cipso_doi();
1600
1601 return count;
1602}
1603
1604static const struct file_operations smk_doi_ops = {
1605 .read = smk_read_doi,
1606 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001607 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001608};
1609
1610/**
1611 * smk_read_direct - read() for /smack/direct
1612 * @filp: file pointer, not actually used
1613 * @buf: where to put the result
1614 * @count: maximum to send along
1615 * @ppos: where to start
1616 *
1617 * Returns number of bytes read or error code, as appropriate
1618 */
1619static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1620 size_t count, loff_t *ppos)
1621{
1622 char temp[80];
1623 ssize_t rc;
1624
1625 if (*ppos != 0)
1626 return 0;
1627
1628 sprintf(temp, "%d", smack_cipso_direct);
1629 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1630
1631 return rc;
1632}
1633
1634/**
1635 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001636 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001637 * @buf: where to get the data from
1638 * @count: bytes sent
1639 * @ppos: where to start
1640 *
1641 * Returns number of bytes written or error code, as appropriate
1642 */
1643static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1644 size_t count, loff_t *ppos)
1645{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001646 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001647 char temp[80];
1648 int i;
1649
Casey Schaufler1880eff2012-06-05 15:28:30 -07001650 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001651 return -EPERM;
1652
1653 if (count >= sizeof(temp) || count == 0)
1654 return -EINVAL;
1655
1656 if (copy_from_user(temp, buf, count) != 0)
1657 return -EFAULT;
1658
1659 temp[count] = '\0';
1660
1661 if (sscanf(temp, "%d", &i) != 1)
1662 return -EINVAL;
1663
Casey Schauflerf7112e62012-05-06 15:22:02 -07001664 /*
1665 * Don't do anything if the value hasn't actually changed.
1666 * If it is changing reset the level on entries that were
1667 * set up to be direct when they were created.
1668 */
1669 if (smack_cipso_direct != i) {
1670 mutex_lock(&smack_known_lock);
1671 list_for_each_entry_rcu(skp, &smack_known_list, list)
1672 if (skp->smk_netlabel.attr.mls.lvl ==
1673 smack_cipso_direct)
1674 skp->smk_netlabel.attr.mls.lvl = i;
1675 smack_cipso_direct = i;
1676 mutex_unlock(&smack_known_lock);
1677 }
Casey Schauflere114e472008-02-04 22:29:50 -08001678
1679 return count;
1680}
1681
1682static const struct file_operations smk_direct_ops = {
1683 .read = smk_read_direct,
1684 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001685 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001686};
1687
1688/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001689 * smk_read_mapped - read() for /smack/mapped
1690 * @filp: file pointer, not actually used
1691 * @buf: where to put the result
1692 * @count: maximum to send along
1693 * @ppos: where to start
1694 *
1695 * Returns number of bytes read or error code, as appropriate
1696 */
1697static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1698 size_t count, loff_t *ppos)
1699{
1700 char temp[80];
1701 ssize_t rc;
1702
1703 if (*ppos != 0)
1704 return 0;
1705
1706 sprintf(temp, "%d", smack_cipso_mapped);
1707 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1708
1709 return rc;
1710}
1711
1712/**
1713 * smk_write_mapped - write() for /smack/mapped
1714 * @file: file pointer, not actually used
1715 * @buf: where to get the data from
1716 * @count: bytes sent
1717 * @ppos: where to start
1718 *
1719 * Returns number of bytes written or error code, as appropriate
1720 */
1721static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1722 size_t count, loff_t *ppos)
1723{
1724 struct smack_known *skp;
1725 char temp[80];
1726 int i;
1727
Casey Schaufler1880eff2012-06-05 15:28:30 -07001728 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001729 return -EPERM;
1730
1731 if (count >= sizeof(temp) || count == 0)
1732 return -EINVAL;
1733
1734 if (copy_from_user(temp, buf, count) != 0)
1735 return -EFAULT;
1736
1737 temp[count] = '\0';
1738
1739 if (sscanf(temp, "%d", &i) != 1)
1740 return -EINVAL;
1741
1742 /*
1743 * Don't do anything if the value hasn't actually changed.
1744 * If it is changing reset the level on entries that were
1745 * set up to be mapped when they were created.
1746 */
1747 if (smack_cipso_mapped != i) {
1748 mutex_lock(&smack_known_lock);
1749 list_for_each_entry_rcu(skp, &smack_known_list, list)
1750 if (skp->smk_netlabel.attr.mls.lvl ==
1751 smack_cipso_mapped)
1752 skp->smk_netlabel.attr.mls.lvl = i;
1753 smack_cipso_mapped = i;
1754 mutex_unlock(&smack_known_lock);
1755 }
1756
1757 return count;
1758}
1759
1760static const struct file_operations smk_mapped_ops = {
1761 .read = smk_read_mapped,
1762 .write = smk_write_mapped,
1763 .llseek = default_llseek,
1764};
1765
1766/**
Casey Schauflere114e472008-02-04 22:29:50 -08001767 * smk_read_ambient - read() for /smack/ambient
1768 * @filp: file pointer, not actually used
1769 * @buf: where to put the result
1770 * @cn: maximum to send along
1771 * @ppos: where to start
1772 *
1773 * Returns number of bytes read or error code, as appropriate
1774 */
1775static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1776 size_t cn, loff_t *ppos)
1777{
1778 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001779 int asize;
1780
1781 if (*ppos != 0)
1782 return 0;
1783 /*
1784 * Being careful to avoid a problem in the case where
1785 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001786 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001787 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001788
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001789 asize = strlen(smack_net_ambient->smk_known) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001790
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001791 if (cn >= asize)
1792 rc = simple_read_from_buffer(buf, cn, ppos,
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001793 smack_net_ambient->smk_known,
1794 asize);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001795 else
1796 rc = -EINVAL;
1797
1798 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001799
1800 return rc;
1801}
1802
1803/**
1804 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001805 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001806 * @buf: where to get the data from
1807 * @count: bytes sent
1808 * @ppos: where to start
1809 *
1810 * Returns number of bytes written or error code, as appropriate
1811 */
1812static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1813 size_t count, loff_t *ppos)
1814{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001815 struct smack_known *skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001816 char *oldambient;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001817 char *data;
1818 int rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -08001819
Casey Schaufler1880eff2012-06-05 15:28:30 -07001820 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001821 return -EPERM;
1822
Al Viro16e5c1f2015-12-24 00:06:05 -05001823 data = memdup_user_nul(buf, count);
1824 if (IS_ERR(data))
1825 return PTR_ERR(data);
Casey Schauflere114e472008-02-04 22:29:50 -08001826
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001827 skp = smk_import_entry(data, count);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001828 if (IS_ERR(skp)) {
1829 rc = PTR_ERR(skp);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001830 goto out;
1831 }
Casey Schauflere114e472008-02-04 22:29:50 -08001832
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001833 mutex_lock(&smack_ambient_lock);
1834
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001835 oldambient = smack_net_ambient->smk_known;
1836 smack_net_ambient = skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001837 smk_unlbl_ambient(oldambient);
1838
1839 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001840
Casey Schauflerf7112e62012-05-06 15:22:02 -07001841out:
1842 kfree(data);
1843 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001844}
1845
1846static const struct file_operations smk_ambient_ops = {
1847 .read = smk_read_ambient,
1848 .write = smk_write_ambient,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001849 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001850};
1851
Rafal Krypac0d77c82015-06-02 11:23:48 +02001852/*
1853 * Seq_file operations for /smack/onlycap
Casey Schaufler15446232008-07-30 15:37:11 -07001854 */
Rafal Krypac0d77c82015-06-02 11:23:48 +02001855static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
Casey Schaufler15446232008-07-30 15:37:11 -07001856{
Rafal Krypac0d77c82015-06-02 11:23:48 +02001857 return smk_seq_start(s, pos, &smack_onlycap_list);
1858}
Casey Schaufler15446232008-07-30 15:37:11 -07001859
Rafal Krypac0d77c82015-06-02 11:23:48 +02001860static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
1861{
1862 return smk_seq_next(s, v, pos, &smack_onlycap_list);
1863}
Casey Schaufler15446232008-07-30 15:37:11 -07001864
Rafal Krypac0d77c82015-06-02 11:23:48 +02001865static int onlycap_seq_show(struct seq_file *s, void *v)
1866{
1867 struct list_head *list = v;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001868 struct smack_known_list_elem *sklep =
1869 list_entry_rcu(list, struct smack_known_list_elem, list);
Casey Schaufler15446232008-07-30 15:37:11 -07001870
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001871 seq_puts(s, sklep->smk_label->smk_known);
Rafal Krypac0d77c82015-06-02 11:23:48 +02001872 seq_putc(s, ' ');
Casey Schaufler15446232008-07-30 15:37:11 -07001873
Rafal Krypac0d77c82015-06-02 11:23:48 +02001874 return 0;
1875}
Casey Schaufler15446232008-07-30 15:37:11 -07001876
Rafal Krypac0d77c82015-06-02 11:23:48 +02001877static const struct seq_operations onlycap_seq_ops = {
1878 .start = onlycap_seq_start,
1879 .next = onlycap_seq_next,
1880 .show = onlycap_seq_show,
1881 .stop = smk_seq_stop,
1882};
1883
1884static int smk_open_onlycap(struct inode *inode, struct file *file)
1885{
1886 return seq_open(file, &onlycap_seq_ops);
1887}
1888
1889/**
1890 * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
1891 * The caller must hold appropriate mutex to prevent concurrent modifications
1892 * to the public list.
1893 * Private list is assumed to be not accessible to other threads yet.
1894 *
1895 * @public: public list
1896 * @private: private list
1897 */
1898static void smk_list_swap_rcu(struct list_head *public,
1899 struct list_head *private)
1900{
1901 struct list_head *first, *last;
1902
1903 if (list_empty(public)) {
1904 list_splice_init_rcu(private, public, synchronize_rcu);
1905 } else {
1906 /* Remember public list before replacing it */
1907 first = public->next;
1908 last = public->prev;
1909
1910 /* Publish private list in place of public in RCU-safe way */
1911 private->prev->next = public;
1912 private->next->prev = public;
1913 rcu_assign_pointer(public->next, private->next);
1914 public->prev = private->prev;
1915
1916 synchronize_rcu();
1917
1918 /* When all readers are done with the old public list,
1919 * attach it in place of private */
1920 private->next = first;
1921 private->prev = last;
1922 first->prev = private;
1923 last->next = private;
1924 }
Casey Schaufler15446232008-07-30 15:37:11 -07001925}
1926
1927/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001928 * smk_parse_label_list - parse list of Smack labels, separated by spaces
1929 *
1930 * @data: the string to parse
1931 * @private: destination list
1932 *
1933 * Returns zero on success or error code, as appropriate
1934 */
1935static int smk_parse_label_list(char *data, struct list_head *list)
1936{
1937 char *tok;
1938 struct smack_known *skp;
1939 struct smack_known_list_elem *sklep;
1940
1941 while ((tok = strsep(&data, " ")) != NULL) {
1942 if (!*tok)
1943 continue;
1944
1945 skp = smk_import_entry(tok, 0);
1946 if (IS_ERR(skp))
1947 return PTR_ERR(skp);
1948
1949 sklep = kzalloc(sizeof(*sklep), GFP_KERNEL);
1950 if (sklep == NULL)
1951 return -ENOMEM;
1952
1953 sklep->smk_label = skp;
1954 list_add(&sklep->list, list);
1955 }
1956
1957 return 0;
1958}
1959
1960/**
1961 * smk_destroy_label_list - destroy a list of smack_known_list_elem
1962 * @head: header pointer of the list to destroy
1963 */
1964void smk_destroy_label_list(struct list_head *list)
1965{
1966 struct smack_known_list_elem *sklep;
1967 struct smack_known_list_elem *sklep2;
1968
1969 list_for_each_entry_safe(sklep, sklep2, list, list)
1970 kfree(sklep);
1971
1972 INIT_LIST_HEAD(list);
1973}
1974
1975/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08001976 * smk_write_onlycap - write() for smackfs/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001977 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001978 * @buf: where to get the data from
1979 * @count: bytes sent
1980 * @ppos: where to start
1981 *
1982 * Returns number of bytes written or error code, as appropriate
1983 */
1984static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1985 size_t count, loff_t *ppos)
1986{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001987 char *data;
Rafal Krypac0d77c82015-06-02 11:23:48 +02001988 LIST_HEAD(list_tmp);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001989 int rc;
Casey Schaufler15446232008-07-30 15:37:11 -07001990
Casey Schaufler1880eff2012-06-05 15:28:30 -07001991 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler15446232008-07-30 15:37:11 -07001992 return -EPERM;
1993
Al Viro16e5c1f2015-12-24 00:06:05 -05001994 data = memdup_user_nul(buf, count);
1995 if (IS_ERR(data))
1996 return PTR_ERR(data);
Casey Schaufler15446232008-07-30 15:37:11 -07001997
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001998 rc = smk_parse_label_list(data, &list_tmp);
Rafal Krypac0d77c82015-06-02 11:23:48 +02001999 kfree(data);
2000
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002001 /*
2002 * Clear the smack_onlycap on invalid label errors. This means
2003 * that we can pass a null string to unset the onlycap value.
2004 *
2005 * Importing will also reject a label beginning with '-',
2006 * so "-usecapabilities" will also work.
2007 *
2008 * But do so only on invalid label, not on system errors.
Rafal Krypac0d77c82015-06-02 11:23:48 +02002009 * The invalid label must be first to count as clearing attempt.
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002010 */
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002011 if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
Rafal Krypac0d77c82015-06-02 11:23:48 +02002012 mutex_lock(&smack_onlycap_lock);
2013 smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
2014 mutex_unlock(&smack_onlycap_lock);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002015 rc = count;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002016 }
2017
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002018 smk_destroy_label_list(&list_tmp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002019
Casey Schauflerf7112e62012-05-06 15:22:02 -07002020 return rc;
Casey Schaufler15446232008-07-30 15:37:11 -07002021}
2022
2023static const struct file_operations smk_onlycap_ops = {
Rafal Krypac0d77c82015-06-02 11:23:48 +02002024 .open = smk_open_onlycap,
2025 .read = seq_read,
Casey Schaufler15446232008-07-30 15:37:11 -07002026 .write = smk_write_onlycap,
Rafal Krypac0d77c82015-06-02 11:23:48 +02002027 .llseek = seq_lseek,
2028 .release = seq_release,
Casey Schaufler15446232008-07-30 15:37:11 -07002029};
2030
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002031#ifdef CONFIG_SECURITY_SMACK_BRINGUP
2032/**
2033 * smk_read_unconfined - read() for smackfs/unconfined
2034 * @filp: file pointer, not actually used
2035 * @buf: where to put the result
2036 * @cn: maximum to send along
2037 * @ppos: where to start
2038 *
2039 * Returns number of bytes read or error code, as appropriate
2040 */
2041static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
2042 size_t cn, loff_t *ppos)
2043{
2044 char *smack = "";
2045 ssize_t rc = -EINVAL;
2046 int asize;
2047
2048 if (*ppos != 0)
2049 return 0;
2050
2051 if (smack_unconfined != NULL)
2052 smack = smack_unconfined->smk_known;
2053
2054 asize = strlen(smack) + 1;
2055
2056 if (cn >= asize)
2057 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
2058
2059 return rc;
2060}
2061
2062/**
2063 * smk_write_unconfined - write() for smackfs/unconfined
2064 * @file: file pointer, not actually used
2065 * @buf: where to get the data from
2066 * @count: bytes sent
2067 * @ppos: where to start
2068 *
2069 * Returns number of bytes written or error code, as appropriate
2070 */
2071static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
2072 size_t count, loff_t *ppos)
2073{
2074 char *data;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002075 struct smack_known *skp;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002076 int rc = count;
2077
2078 if (!smack_privileged(CAP_MAC_ADMIN))
2079 return -EPERM;
2080
Al Viro16e5c1f2015-12-24 00:06:05 -05002081 data = memdup_user_nul(buf, count);
2082 if (IS_ERR(data))
2083 return PTR_ERR(data);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002084
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002085 /*
2086 * Clear the smack_unconfined on invalid label errors. This means
2087 * that we can pass a null string to unset the unconfined value.
2088 *
2089 * Importing will also reject a label beginning with '-',
2090 * so "-confine" will also work.
2091 *
2092 * But do so only on invalid label, not on system errors.
2093 */
2094 skp = smk_import_entry(data, count);
2095 if (PTR_ERR(skp) == -EINVAL)
2096 skp = NULL;
2097 else if (IS_ERR(skp)) {
2098 rc = PTR_ERR(skp);
2099 goto freeout;
2100 }
2101
2102 smack_unconfined = skp;
2103
2104freeout:
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002105 kfree(data);
2106 return rc;
2107}
2108
2109static const struct file_operations smk_unconfined_ops = {
2110 .read = smk_read_unconfined,
2111 .write = smk_write_unconfined,
2112 .llseek = default_llseek,
2113};
2114#endif /* CONFIG_SECURITY_SMACK_BRINGUP */
2115
Casey Schauflere114e472008-02-04 22:29:50 -08002116/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002117 * smk_read_logging - read() for /smack/logging
2118 * @filp: file pointer, not actually used
2119 * @buf: where to put the result
2120 * @cn: maximum to send along
2121 * @ppos: where to start
2122 *
2123 * Returns number of bytes read or error code, as appropriate
2124 */
2125static ssize_t smk_read_logging(struct file *filp, char __user *buf,
2126 size_t count, loff_t *ppos)
2127{
2128 char temp[32];
2129 ssize_t rc;
2130
2131 if (*ppos != 0)
2132 return 0;
2133
2134 sprintf(temp, "%d\n", log_policy);
2135 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2136 return rc;
2137}
2138
2139/**
2140 * smk_write_logging - write() for /smack/logging
2141 * @file: file pointer, not actually used
2142 * @buf: where to get the data from
2143 * @count: bytes sent
2144 * @ppos: where to start
2145 *
2146 * Returns number of bytes written or error code, as appropriate
2147 */
2148static ssize_t smk_write_logging(struct file *file, const char __user *buf,
2149 size_t count, loff_t *ppos)
2150{
2151 char temp[32];
2152 int i;
2153
Casey Schaufler1880eff2012-06-05 15:28:30 -07002154 if (!smack_privileged(CAP_MAC_ADMIN))
Etienne Bassetecfcc532009-04-08 20:40:06 +02002155 return -EPERM;
2156
2157 if (count >= sizeof(temp) || count == 0)
2158 return -EINVAL;
2159
2160 if (copy_from_user(temp, buf, count) != 0)
2161 return -EFAULT;
2162
2163 temp[count] = '\0';
2164
2165 if (sscanf(temp, "%d", &i) != 1)
2166 return -EINVAL;
2167 if (i < 0 || i > 3)
2168 return -EINVAL;
2169 log_policy = i;
2170 return count;
2171}
2172
2173
2174
2175static const struct file_operations smk_logging_ops = {
2176 .read = smk_read_logging,
2177 .write = smk_write_logging,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002178 .llseek = default_llseek,
Etienne Bassetecfcc532009-04-08 20:40:06 +02002179};
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002180
2181/*
2182 * Seq_file read operations for /smack/load-self
2183 */
2184
2185static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
2186{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002187 struct task_smack *tsp = smack_cred(current_cred());
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002188
Casey Schaufler40809562011-11-10 15:02:22 -08002189 return smk_seq_start(s, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002190}
2191
2192static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2193{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002194 struct task_smack *tsp = smack_cred(current_cred());
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002195
Casey Schaufler40809562011-11-10 15:02:22 -08002196 return smk_seq_next(s, v, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002197}
2198
2199static int load_self_seq_show(struct seq_file *s, void *v)
2200{
2201 struct list_head *list = v;
2202 struct smack_rule *srp =
Rafal Krypa01fa8472015-05-21 18:24:31 +02002203 list_entry_rcu(list, struct smack_rule, list);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002204
Casey Schauflerf7112e62012-05-06 15:22:02 -07002205 smk_rule_show(s, srp, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002206
2207 return 0;
2208}
2209
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002210static const struct seq_operations load_self_seq_ops = {
2211 .start = load_self_seq_start,
2212 .next = load_self_seq_next,
2213 .show = load_self_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08002214 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002215};
2216
2217
2218/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07002219 * smk_open_load_self - open() for /smack/load-self2
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002220 * @inode: inode structure representing file
2221 * @file: "load" file pointer
2222 *
2223 * For reading, use load_seq_* seq_file reading operations.
2224 */
2225static int smk_open_load_self(struct inode *inode, struct file *file)
2226{
2227 return seq_open(file, &load_self_seq_ops);
2228}
2229
2230/**
2231 * smk_write_load_self - write() for /smack/load-self
2232 * @file: file pointer, not actually used
2233 * @buf: where to get the data from
2234 * @count: bytes sent
2235 * @ppos: where to start - must be 0
2236 *
2237 */
2238static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
2239 size_t count, loff_t *ppos)
2240{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002241 struct task_smack *tsp = smack_cred(current_cred());
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002242
Casey Schauflerf7112e62012-05-06 15:22:02 -07002243 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2244 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002245}
2246
2247static const struct file_operations smk_load_self_ops = {
2248 .open = smk_open_load_self,
2249 .read = seq_read,
2250 .llseek = seq_lseek,
2251 .write = smk_write_load_self,
2252 .release = seq_release,
2253};
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002254
2255/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07002256 * smk_user_access - handle access check transaction
2257 * @file: file pointer
2258 * @buf: data from user space
2259 * @count: bytes sent
2260 * @ppos: where to start - must be 0
2261 */
2262static ssize_t smk_user_access(struct file *file, const char __user *buf,
2263 size_t count, loff_t *ppos, int format)
2264{
Rafal Krypae05b6f92013-01-10 19:42:00 +01002265 struct smack_parsed_rule rule;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002266 char *data;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002267 int res;
2268
2269 data = simple_transaction_get(file, buf, count);
2270 if (IS_ERR(data))
2271 return PTR_ERR(data);
2272
2273 if (format == SMK_FIXED24_FMT) {
2274 if (count < SMK_LOADLEN)
2275 return -EINVAL;
2276 res = smk_parse_rule(data, &rule, 0);
2277 } else {
2278 /*
Rafal Krypa10289b02013-08-09 11:47:07 +02002279 * simple_transaction_get() returns null-terminated data
Casey Schauflerf7112e62012-05-06 15:22:02 -07002280 */
Rafal Krypa10289b02013-08-09 11:47:07 +02002281 res = smk_parse_long_rule(data, &rule, 0, 3);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002282 }
2283
Jarkko Sakkinen398ce072013-11-28 19:16:46 +02002284 if (res >= 0)
2285 res = smk_access(rule.smk_subject, rule.smk_object,
2286 rule.smk_access1, NULL);
2287 else if (res != -ENOENT)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002288 return res;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002289
Casey Schauflerd166c802014-08-27 14:51:27 -07002290 /*
2291 * smk_access() can return a value > 0 in the "bringup" case.
2292 */
2293 data[0] = res >= 0 ? '1' : '0';
Casey Schauflerf7112e62012-05-06 15:22:02 -07002294 data[1] = '\0';
2295
2296 simple_transaction_set(file, 2);
2297
2298 if (format == SMK_FIXED24_FMT)
2299 return SMK_LOADLEN;
2300 return count;
2301}
2302
2303/**
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002304 * smk_write_access - handle access check transaction
2305 * @file: file pointer
2306 * @buf: data from user space
2307 * @count: bytes sent
2308 * @ppos: where to start - must be 0
2309 */
2310static ssize_t smk_write_access(struct file *file, const char __user *buf,
2311 size_t count, loff_t *ppos)
2312{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002313 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002314}
2315
2316static const struct file_operations smk_access_ops = {
2317 .write = smk_write_access,
2318 .read = simple_transaction_read,
2319 .release = simple_transaction_release,
2320 .llseek = generic_file_llseek,
2321};
2322
Casey Schauflerf7112e62012-05-06 15:22:02 -07002323
2324/*
2325 * Seq_file read operations for /smack/load2
2326 */
2327
2328static int load2_seq_show(struct seq_file *s, void *v)
2329{
2330 struct list_head *list = v;
Vishal Goel460d95a2019-03-07 16:55:24 +05302331 struct smack_rule *srp;
2332 struct smack_known *skp =
2333 list_entry_rcu(list, struct smack_known, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002334
Vishal Goel460d95a2019-03-07 16:55:24 +05302335 list_for_each_entry_rcu(srp, &skp->smk_rules, list)
2336 smk_rule_show(s, srp, SMK_LONGLABEL);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002337
2338 return 0;
2339}
2340
2341static const struct seq_operations load2_seq_ops = {
2342 .start = load2_seq_start,
2343 .next = load2_seq_next,
2344 .show = load2_seq_show,
2345 .stop = smk_seq_stop,
2346};
2347
2348/**
2349 * smk_open_load2 - open() for /smack/load2
2350 * @inode: inode structure representing file
2351 * @file: "load2" file pointer
2352 *
2353 * For reading, use load2_seq_* seq_file reading operations.
2354 */
2355static int smk_open_load2(struct inode *inode, struct file *file)
2356{
2357 return seq_open(file, &load2_seq_ops);
2358}
2359
2360/**
2361 * smk_write_load2 - write() for /smack/load2
2362 * @file: file pointer, not actually used
2363 * @buf: where to get the data from
2364 * @count: bytes sent
2365 * @ppos: where to start - must be 0
2366 *
2367 */
2368static ssize_t smk_write_load2(struct file *file, const char __user *buf,
2369 size_t count, loff_t *ppos)
2370{
2371 /*
2372 * Must have privilege.
2373 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07002374 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07002375 return -EPERM;
2376
2377 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2378 SMK_LONG_FMT);
2379}
2380
2381static const struct file_operations smk_load2_ops = {
2382 .open = smk_open_load2,
2383 .read = seq_read,
2384 .llseek = seq_lseek,
2385 .write = smk_write_load2,
2386 .release = seq_release,
2387};
2388
2389/*
2390 * Seq_file read operations for /smack/load-self2
2391 */
2392
2393static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
2394{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002395 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerf7112e62012-05-06 15:22:02 -07002396
2397 return smk_seq_start(s, pos, &tsp->smk_rules);
2398}
2399
2400static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
2401{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002402 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerf7112e62012-05-06 15:22:02 -07002403
2404 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2405}
2406
2407static int load_self2_seq_show(struct seq_file *s, void *v)
2408{
2409 struct list_head *list = v;
2410 struct smack_rule *srp =
Rafal Krypa01fa8472015-05-21 18:24:31 +02002411 list_entry_rcu(list, struct smack_rule, list);
Casey Schauflerf7112e62012-05-06 15:22:02 -07002412
2413 smk_rule_show(s, srp, SMK_LONGLABEL);
2414
2415 return 0;
2416}
2417
2418static const struct seq_operations load_self2_seq_ops = {
2419 .start = load_self2_seq_start,
2420 .next = load_self2_seq_next,
2421 .show = load_self2_seq_show,
2422 .stop = smk_seq_stop,
2423};
2424
2425/**
2426 * smk_open_load_self2 - open() for /smack/load-self2
2427 * @inode: inode structure representing file
2428 * @file: "load" file pointer
2429 *
2430 * For reading, use load_seq_* seq_file reading operations.
2431 */
2432static int smk_open_load_self2(struct inode *inode, struct file *file)
2433{
2434 return seq_open(file, &load_self2_seq_ops);
2435}
2436
2437/**
2438 * smk_write_load_self2 - write() for /smack/load-self2
2439 * @file: file pointer, not actually used
2440 * @buf: where to get the data from
2441 * @count: bytes sent
2442 * @ppos: where to start - must be 0
2443 *
2444 */
2445static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2446 size_t count, loff_t *ppos)
2447{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002448 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerf7112e62012-05-06 15:22:02 -07002449
2450 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2451 &tsp->smk_rules_lock, SMK_LONG_FMT);
2452}
2453
2454static const struct file_operations smk_load_self2_ops = {
2455 .open = smk_open_load_self2,
2456 .read = seq_read,
2457 .llseek = seq_lseek,
2458 .write = smk_write_load_self2,
2459 .release = seq_release,
2460};
2461
2462/**
2463 * smk_write_access2 - handle access check transaction
2464 * @file: file pointer
2465 * @buf: data from user space
2466 * @count: bytes sent
2467 * @ppos: where to start - must be 0
2468 */
2469static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2470 size_t count, loff_t *ppos)
2471{
2472 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2473}
2474
2475static const struct file_operations smk_access2_ops = {
2476 .write = smk_write_access2,
2477 .read = simple_transaction_read,
2478 .release = simple_transaction_release,
2479 .llseek = generic_file_llseek,
2480};
2481
Etienne Bassetecfcc532009-04-08 20:40:06 +02002482/**
Rafal Krypa449543b2012-07-11 17:49:30 +02002483 * smk_write_revoke_subj - write() for /smack/revoke-subject
2484 * @file: file pointer
2485 * @buf: data from user space
2486 * @count: bytes sent
2487 * @ppos: where to start - must be 0
2488 */
2489static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2490 size_t count, loff_t *ppos)
2491{
Dan Carpenter54302092015-06-11 11:51:16 +03002492 char *data;
2493 const char *cp;
Rafal Krypa449543b2012-07-11 17:49:30 +02002494 struct smack_known *skp;
2495 struct smack_rule *sp;
2496 struct list_head *rule_list;
2497 struct mutex *rule_lock;
2498 int rc = count;
2499
2500 if (*ppos != 0)
2501 return -EINVAL;
2502
2503 if (!smack_privileged(CAP_MAC_ADMIN))
2504 return -EPERM;
2505
2506 if (count == 0 || count > SMK_LONGLABEL)
2507 return -EINVAL;
2508
Markus Elfring63e24c42016-08-21 20:17:36 +02002509 data = memdup_user(buf, count);
2510 if (IS_ERR(data))
2511 return PTR_ERR(data);
Rafal Krypa449543b2012-07-11 17:49:30 +02002512
2513 cp = smk_parse_smack(data, count);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002514 if (IS_ERR(cp)) {
2515 rc = PTR_ERR(cp);
Dan Carpenter54302092015-06-11 11:51:16 +03002516 goto out_data;
Rafal Krypa449543b2012-07-11 17:49:30 +02002517 }
2518
2519 skp = smk_find_entry(cp);
Rafal Krypad15d9fa2012-11-27 16:28:11 +01002520 if (skp == NULL)
Dan Carpenter54302092015-06-11 11:51:16 +03002521 goto out_cp;
Rafal Krypa449543b2012-07-11 17:49:30 +02002522
2523 rule_list = &skp->smk_rules;
2524 rule_lock = &skp->smk_rules_lock;
2525
2526 mutex_lock(rule_lock);
2527
2528 list_for_each_entry_rcu(sp, rule_list, list)
2529 sp->smk_access = 0;
2530
2531 mutex_unlock(rule_lock);
2532
Dan Carpenter54302092015-06-11 11:51:16 +03002533out_cp:
Rafal Krypa449543b2012-07-11 17:49:30 +02002534 kfree(cp);
Dan Carpenter54302092015-06-11 11:51:16 +03002535out_data:
2536 kfree(data);
2537
Rafal Krypa449543b2012-07-11 17:49:30 +02002538 return rc;
2539}
2540
2541static const struct file_operations smk_revoke_subj_ops = {
2542 .write = smk_write_revoke_subj,
2543 .read = simple_transaction_read,
2544 .release = simple_transaction_release,
2545 .llseek = generic_file_llseek,
2546};
2547
Casey Schauflere93072372012-11-01 18:14:32 -07002548/**
2549 * smk_init_sysfs - initialize /sys/fs/smackfs
2550 *
2551 */
2552static int smk_init_sysfs(void)
2553{
kbuild test robotca70d272015-06-24 07:41:07 +08002554 return sysfs_create_mount_point(fs_kobj, "smackfs");
Casey Schauflere93072372012-11-01 18:14:32 -07002555}
2556
Rafal Krypa449543b2012-07-11 17:49:30 +02002557/**
Rafal Krypae05b6f92013-01-10 19:42:00 +01002558 * smk_write_change_rule - write() for /smack/change-rule
2559 * @file: file pointer
2560 * @buf: data from user space
2561 * @count: bytes sent
2562 * @ppos: where to start - must be 0
2563 */
2564static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2565 size_t count, loff_t *ppos)
2566{
2567 /*
2568 * Must have privilege.
2569 */
Casey Schaufler4afde482013-12-19 13:23:26 -08002570 if (!smack_privileged(CAP_MAC_ADMIN))
Rafal Krypae05b6f92013-01-10 19:42:00 +01002571 return -EPERM;
2572
2573 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2574 SMK_CHANGE_FMT);
2575}
2576
2577static const struct file_operations smk_change_rule_ops = {
2578 .write = smk_write_change_rule,
2579 .read = simple_transaction_read,
2580 .release = simple_transaction_release,
2581 .llseek = generic_file_llseek,
2582};
2583
2584/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002585 * smk_read_syslog - read() for smackfs/syslog
2586 * @filp: file pointer, not actually used
2587 * @buf: where to put the result
2588 * @cn: maximum to send along
2589 * @ppos: where to start
2590 *
2591 * Returns number of bytes read or error code, as appropriate
2592 */
2593static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2594 size_t cn, loff_t *ppos)
2595{
2596 struct smack_known *skp;
2597 ssize_t rc = -EINVAL;
2598 int asize;
2599
2600 if (*ppos != 0)
2601 return 0;
2602
2603 if (smack_syslog_label == NULL)
2604 skp = &smack_known_star;
2605 else
2606 skp = smack_syslog_label;
2607
2608 asize = strlen(skp->smk_known) + 1;
2609
2610 if (cn >= asize)
2611 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2612 asize);
2613
2614 return rc;
2615}
2616
2617/**
2618 * smk_write_syslog - write() for smackfs/syslog
2619 * @file: file pointer, not actually used
2620 * @buf: where to get the data from
2621 * @count: bytes sent
2622 * @ppos: where to start
2623 *
2624 * Returns number of bytes written or error code, as appropriate
2625 */
2626static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2627 size_t count, loff_t *ppos)
2628{
2629 char *data;
2630 struct smack_known *skp;
2631 int rc = count;
2632
2633 if (!smack_privileged(CAP_MAC_ADMIN))
2634 return -EPERM;
2635
Al Viro16e5c1f2015-12-24 00:06:05 -05002636 data = memdup_user_nul(buf, count);
2637 if (IS_ERR(data))
2638 return PTR_ERR(data);
Casey Schaufler00f84f32013-12-23 11:07:10 -08002639
Al Viro16e5c1f2015-12-24 00:06:05 -05002640 skp = smk_import_entry(data, count);
2641 if (IS_ERR(skp))
2642 rc = PTR_ERR(skp);
2643 else
2644 smack_syslog_label = skp;
Casey Schaufler00f84f32013-12-23 11:07:10 -08002645
2646 kfree(data);
2647 return rc;
2648}
2649
2650static const struct file_operations smk_syslog_ops = {
2651 .read = smk_read_syslog,
2652 .write = smk_write_syslog,
2653 .llseek = default_llseek,
2654};
2655
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002656/*
2657 * Seq_file read operations for /smack/relabel-self
2658 */
2659
2660static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
2661{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002662 struct task_smack *tsp = smack_cred(current_cred());
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002663
2664 return smk_seq_start(s, pos, &tsp->smk_relabel);
2665}
2666
2667static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
2668{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002669 struct task_smack *tsp = smack_cred(current_cred());
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002670
2671 return smk_seq_next(s, v, pos, &tsp->smk_relabel);
2672}
2673
2674static int relabel_self_seq_show(struct seq_file *s, void *v)
2675{
2676 struct list_head *list = v;
2677 struct smack_known_list_elem *sklep =
2678 list_entry(list, struct smack_known_list_elem, list);
2679
2680 seq_puts(s, sklep->smk_label->smk_known);
2681 seq_putc(s, ' ');
2682
2683 return 0;
2684}
2685
2686static const struct seq_operations relabel_self_seq_ops = {
2687 .start = relabel_self_seq_start,
2688 .next = relabel_self_seq_next,
2689 .show = relabel_self_seq_show,
2690 .stop = smk_seq_stop,
2691};
2692
2693/**
2694 * smk_open_relabel_self - open() for /smack/relabel-self
2695 * @inode: inode structure representing file
2696 * @file: "relabel-self" file pointer
2697 *
2698 * Connect our relabel_self_seq_* operations with /smack/relabel-self
2699 * file_operations
2700 */
2701static int smk_open_relabel_self(struct inode *inode, struct file *file)
2702{
2703 return seq_open(file, &relabel_self_seq_ops);
2704}
2705
2706/**
2707 * smk_write_relabel_self - write() for /smack/relabel-self
2708 * @file: file pointer, not actually used
2709 * @buf: where to get the data from
2710 * @count: bytes sent
2711 * @ppos: where to start - must be 0
2712 *
2713 */
2714static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
2715 size_t count, loff_t *ppos)
2716{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002717 struct task_smack *tsp = smack_cred(current_cred());
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002718 char *data;
2719 int rc;
2720 LIST_HEAD(list_tmp);
2721
2722 /*
2723 * Must have privilege.
2724 */
2725 if (!smack_privileged(CAP_MAC_ADMIN))
2726 return -EPERM;
2727
2728 /*
2729 * Enough data must be present.
2730 */
2731 if (*ppos != 0)
2732 return -EINVAL;
2733
Al Viro16e5c1f2015-12-24 00:06:05 -05002734 data = memdup_user_nul(buf, count);
2735 if (IS_ERR(data))
2736 return PTR_ERR(data);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002737
2738 rc = smk_parse_label_list(data, &list_tmp);
2739 kfree(data);
2740
2741 if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2742 smk_destroy_label_list(&tsp->smk_relabel);
2743 list_splice(&list_tmp, &tsp->smk_relabel);
2744 return count;
2745 }
2746
2747 smk_destroy_label_list(&list_tmp);
2748 return rc;
2749}
2750
2751static const struct file_operations smk_relabel_self_ops = {
2752 .open = smk_open_relabel_self,
2753 .read = seq_read,
2754 .llseek = seq_lseek,
2755 .write = smk_write_relabel_self,
2756 .release = seq_release,
2757};
Casey Schaufler00f84f32013-12-23 11:07:10 -08002758
2759/**
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002760 * smk_read_ptrace - read() for /smack/ptrace
2761 * @filp: file pointer, not actually used
2762 * @buf: where to put the result
2763 * @count: maximum to send along
2764 * @ppos: where to start
2765 *
2766 * Returns number of bytes read or error code, as appropriate
2767 */
2768static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2769 size_t count, loff_t *ppos)
2770{
2771 char temp[32];
2772 ssize_t rc;
2773
2774 if (*ppos != 0)
2775 return 0;
2776
2777 sprintf(temp, "%d\n", smack_ptrace_rule);
2778 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2779 return rc;
2780}
2781
2782/**
2783 * smk_write_ptrace - write() for /smack/ptrace
2784 * @file: file pointer
2785 * @buf: data from user space
2786 * @count: bytes sent
2787 * @ppos: where to start - must be 0
2788 */
2789static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2790 size_t count, loff_t *ppos)
2791{
2792 char temp[32];
2793 int i;
2794
2795 if (!smack_privileged(CAP_MAC_ADMIN))
2796 return -EPERM;
2797
2798 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2799 return -EINVAL;
2800
2801 if (copy_from_user(temp, buf, count) != 0)
2802 return -EFAULT;
2803
2804 temp[count] = '\0';
2805
2806 if (sscanf(temp, "%d", &i) != 1)
2807 return -EINVAL;
2808 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2809 return -EINVAL;
2810 smack_ptrace_rule = i;
2811
2812 return count;
2813}
2814
2815static const struct file_operations smk_ptrace_ops = {
2816 .write = smk_write_ptrace,
2817 .read = smk_read_ptrace,
2818 .llseek = default_llseek,
2819};
2820
2821/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002822 * smk_fill_super - fill the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002823 * @sb: the empty superblock
David Howells5afdd0f2019-03-25 16:38:31 +00002824 * @fc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08002825 *
Casey Schaufler00f84f32013-12-23 11:07:10 -08002826 * Fill in the well known entries for the smack filesystem
Casey Schauflere114e472008-02-04 22:29:50 -08002827 *
2828 * Returns 0 on success, an error code on failure
2829 */
David Howells5afdd0f2019-03-25 16:38:31 +00002830static int smk_fill_super(struct super_block *sb, struct fs_context *fc)
Casey Schauflere114e472008-02-04 22:29:50 -08002831{
2832 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002833
Eric Biggerscda37122017-03-25 21:15:37 -07002834 static const struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002835 [SMK_LOAD] = {
2836 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2837 [SMK_CIPSO] = {
2838 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2839 [SMK_DOI] = {
2840 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2841 [SMK_DIRECT] = {
2842 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2843 [SMK_AMBIENT] = {
2844 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002845 [SMK_NET4ADDR] = {
2846 "netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002847 [SMK_ONLYCAP] = {
2848 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2849 [SMK_LOGGING] = {
2850 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2851 [SMK_LOAD_SELF] = {
2852 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002853 [SMK_ACCESSES] = {
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03002854 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
Casey Schauflerf7112e62012-05-06 15:22:02 -07002855 [SMK_MAPPED] = {
2856 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2857 [SMK_LOAD2] = {
2858 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2859 [SMK_LOAD_SELF2] = {
2860 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2861 [SMK_ACCESS2] = {
2862 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2863 [SMK_CIPSO2] = {
2864 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
Rafal Krypa449543b2012-07-11 17:49:30 +02002865 [SMK_REVOKE_SUBJ] = {
2866 "revoke-subject", &smk_revoke_subj_ops,
2867 S_IRUGO|S_IWUSR},
Rafal Krypae05b6f92013-01-10 19:42:00 +01002868 [SMK_CHANGE_RULE] = {
2869 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
Casey Schaufler00f84f32013-12-23 11:07:10 -08002870 [SMK_SYSLOG] = {
2871 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002872 [SMK_PTRACE] = {
2873 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -07002874#ifdef CONFIG_SECURITY_SMACK_BRINGUP
2875 [SMK_UNCONFINED] = {
2876 "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
2877#endif
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002878#if IS_ENABLED(CONFIG_IPV6)
2879 [SMK_NET6ADDR] = {
2880 "ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
2881#endif /* CONFIG_IPV6 */
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002882 [SMK_RELABEL_SELF] = {
2883 "relabel-self", &smk_relabel_self_ops,
2884 S_IRUGO|S_IWUGO},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002885 /* last one */
2886 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08002887 };
2888
2889 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2890 if (rc != 0) {
2891 printk(KERN_ERR "%s failed %d while creating inodes\n",
2892 __func__, rc);
2893 return rc;
2894 }
2895
Casey Schauflere114e472008-02-04 22:29:50 -08002896 return 0;
2897}
2898
2899/**
David Howells5afdd0f2019-03-25 16:38:31 +00002900 * smk_get_tree - get the smackfs superblock
2901 * @fc: The mount context, including any options
Casey Schauflere114e472008-02-04 22:29:50 -08002902 *
2903 * Just passes everything along.
2904 *
2905 * Returns what the lower level code does.
2906 */
David Howells5afdd0f2019-03-25 16:38:31 +00002907static int smk_get_tree(struct fs_context *fc)
Casey Schauflere114e472008-02-04 22:29:50 -08002908{
David Howells5afdd0f2019-03-25 16:38:31 +00002909 return get_tree_single(fc, smk_fill_super);
2910}
2911
2912static const struct fs_context_operations smk_context_ops = {
2913 .get_tree = smk_get_tree,
2914};
2915
2916/**
2917 * smk_init_fs_context - Initialise a filesystem context for smackfs
2918 * @fc: The blank mount context
2919 */
2920static int smk_init_fs_context(struct fs_context *fc)
2921{
2922 fc->ops = &smk_context_ops;
2923 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002924}
2925
2926static struct file_system_type smk_fs_type = {
2927 .name = "smackfs",
David Howells5afdd0f2019-03-25 16:38:31 +00002928 .init_fs_context = smk_init_fs_context,
Casey Schauflere114e472008-02-04 22:29:50 -08002929 .kill_sb = kill_litter_super,
2930};
2931
2932static struct vfsmount *smackfs_mount;
2933
Casey Schauflerf7112e62012-05-06 15:22:02 -07002934static int __init smk_preset_netlabel(struct smack_known *skp)
2935{
2936 skp->smk_netlabel.domain = skp->smk_known;
2937 skp->smk_netlabel.flags =
2938 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2939 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2940 &skp->smk_netlabel, strlen(skp->smk_known));
2941}
2942
Casey Schauflere114e472008-02-04 22:29:50 -08002943/**
2944 * init_smk_fs - get the smackfs superblock
2945 *
2946 * register the smackfs
2947 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002948 * Do not register smackfs if Smack wasn't enabled
2949 * on boot. We can not put this method normally under the
2950 * smack_init() code path since the security subsystem get
2951 * initialized before the vfs caches.
2952 *
2953 * Returns true if we were not chosen on boot or if
2954 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08002955 */
2956static int __init init_smk_fs(void)
2957{
2958 int err;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002959 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002960
José Bollod21b7b02015-10-02 15:15:56 +02002961 if (smack_enabled == 0)
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002962 return 0;
2963
Casey Schauflere93072372012-11-01 18:14:32 -07002964 err = smk_init_sysfs();
2965 if (err)
2966 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2967
Casey Schauflere114e472008-02-04 22:29:50 -08002968 err = register_filesystem(&smk_fs_type);
2969 if (!err) {
2970 smackfs_mount = kern_mount(&smk_fs_type);
2971 if (IS_ERR(smackfs_mount)) {
2972 printk(KERN_ERR "smackfs: could not mount!\n");
2973 err = PTR_ERR(smackfs_mount);
2974 smackfs_mount = NULL;
2975 }
2976 }
2977
Casey Schauflere114e472008-02-04 22:29:50 -08002978 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002979 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08002980
Casey Schauflerf7112e62012-05-06 15:22:02 -07002981 rc = smk_preset_netlabel(&smack_known_floor);
2982 if (err == 0 && rc < 0)
2983 err = rc;
2984 rc = smk_preset_netlabel(&smack_known_hat);
2985 if (err == 0 && rc < 0)
2986 err = rc;
2987 rc = smk_preset_netlabel(&smack_known_huh);
2988 if (err == 0 && rc < 0)
2989 err = rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002990 rc = smk_preset_netlabel(&smack_known_star);
2991 if (err == 0 && rc < 0)
2992 err = rc;
2993 rc = smk_preset_netlabel(&smack_known_web);
2994 if (err == 0 && rc < 0)
2995 err = rc;
2996
Casey Schauflere114e472008-02-04 22:29:50 -08002997 return err;
2998}
2999
3000__initcall(init_smk_fs);