blob: 49a2248b525c1e544326407fa9d7bd152d1cb3da [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
7 *
8 * Authors:
9 * Casey Schaufler <casey@schaufler-ca.com>
10 * Ahmed S. Darwish <darwish.07@gmail.com>
11 *
12 * Special thanks to the authors of selinuxfs.
13 *
14 * Karl MacMillan <kmacmillan@tresys.com>
15 * James Morris <jmorris@redhat.com>
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/vmalloc.h>
21#include <linux/security.h>
22#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Casey Schaufler6d3dc072008-12-31 12:54:12 -050024#include <net/net_namespace.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <net/cipso_ipv4.h>
26#include <linux/seq_file.h>
27#include <linux/ctype.h>
Casey Schaufler4bc87e62008-02-15 15:24:25 -080028#include <linux/audit.h>
Casey Schaufler958d2c22013-04-02 11:41:18 -070029#include <linux/magic.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030#include "smack.h"
31
32/*
33 * smackfs pseudo filesystem.
34 */
35
36enum smk_inos {
37 SMK_ROOT_INO = 2,
38 SMK_LOAD = 3, /* load policy */
39 SMK_CIPSO = 4, /* load label -> CIPSO mapping */
40 SMK_DOI = 5, /* CIPSO DOI */
41 SMK_DIRECT = 6, /* CIPSO level indicating direct label */
42 SMK_AMBIENT = 7, /* internet ambient label */
Casey Schaufler6d3dc072008-12-31 12:54:12 -050043 SMK_NETLBLADDR = 8, /* single label hosts */
Casey Schaufler15446232008-07-30 15:37:11 -070044 SMK_ONLYCAP = 9, /* the only "capable" label */
Etienne Bassetecfcc532009-04-08 20:40:06 +020045 SMK_LOGGING = 10, /* logging */
Casey Schaufler7898e1f2011-01-17 08:05:27 -080046 SMK_LOAD_SELF = 11, /* task specific rules */
Jarkko Sakkinen828716c2011-09-08 10:12:01 +030047 SMK_ACCESSES = 12, /* access policy */
Casey Schauflerf7112e62012-05-06 15:22:02 -070048 SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
49 SMK_LOAD2 = 14, /* load policy with long labels */
50 SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
51 SMK_ACCESS2 = 16, /* make an access check with long labels */
52 SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
Rafal Krypa449543b2012-07-11 17:49:30 +020053 SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
Rafal Krypae05b6f92013-01-10 19:42:00 +010054 SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
Casey Schaufler00f84f32013-12-23 11:07:10 -080055 SMK_SYSLOG = 20, /* change syslog label) */
Lukasz Pawelczyk66867812014-03-11 17:07:06 +010056 SMK_PTRACE = 21, /* set ptrace rule */
Casey Schauflere114e472008-02-04 22:29:50 -080057};
58
59/*
60 * List locks
61 */
Casey Schauflere114e472008-02-04 22:29:50 -080062static DEFINE_MUTEX(smack_cipso_lock);
Casey Schaufler4bc87e62008-02-15 15:24:25 -080063static DEFINE_MUTEX(smack_ambient_lock);
Casey Schaufler00f84f32013-12-23 11:07:10 -080064static DEFINE_MUTEX(smack_syslog_lock);
Casey Schaufler6d3dc072008-12-31 12:54:12 -050065static DEFINE_MUTEX(smk_netlbladdr_lock);
Casey Schauflere114e472008-02-04 22:29:50 -080066
67/*
68 * This is the "ambient" label for network traffic.
69 * If it isn't somehow marked, use this.
70 * It can be reset via smackfs/ambient
71 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -070072struct smack_known *smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -080073
74/*
Casey Schauflere114e472008-02-04 22:29:50 -080075 * This is the level in a CIPSO header that indicates a
76 * smack label is contained directly in the category set.
77 * It can be reset via smackfs/direct
78 */
79int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
80
Casey Schaufler15446232008-07-30 15:37:11 -070081/*
Casey Schauflerf7112e62012-05-06 15:22:02 -070082 * This is the level in a CIPSO header that indicates a
83 * secid is contained directly in the category set.
84 * It can be reset via smackfs/mapped
85 */
86int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
87
88/*
Casey Schaufler15446232008-07-30 15:37:11 -070089 * Unless a process is running with this label even
90 * having CAP_MAC_OVERRIDE isn't enough to grant
91 * privilege to violate MAC policy. If no label is
92 * designated (the NULL case) capabilities apply to
93 * everyone. It is expected that the hat (^) label
94 * will be used if any label is used.
95 */
Casey Schaufler00f84f32013-12-23 11:07:10 -080096struct smack_known *smack_onlycap;
97
98/*
99 * If this value is set restrict syslog use to the label specified.
100 * It can be reset via smackfs/syslog
101 */
102struct smack_known *smack_syslog_label;
Casey Schaufler15446232008-07-30 15:37:11 -0700103
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500104/*
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100105 * Ptrace current rule
106 * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
107 * SMACK_PTRACE_EXACT labels must match, but can be overriden with
108 * CAP_SYS_PTRACE
109 * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
110 */
111int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
112
113/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500114 * Certain IP addresses may be designated as single label hosts.
115 * Packets are sent there unlabeled, but only from tasks that
116 * can write to the specified label.
117 */
Etienne Basset7198e2e2009-03-24 20:53:24 +0100118
119LIST_HEAD(smk_netlbladdr_list);
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700120
121/*
122 * Rule lists are maintained for each label.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700123 * This master list is just for reading /smack/load and /smack/load2.
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700124 */
125struct smack_master_list {
126 struct list_head list;
127 struct smack_rule *smk_rule;
128};
129
Etienne Basset7198e2e2009-03-24 20:53:24 +0100130LIST_HEAD(smack_rule_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500131
Rafal Krypae05b6f92013-01-10 19:42:00 +0100132struct smack_parsed_rule {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700133 struct smack_known *smk_subject;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100134 char *smk_object;
135 int smk_access1;
136 int smk_access2;
137};
138
Casey Schauflere114e472008-02-04 22:29:50 -0800139static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
Casey Schauflere114e472008-02-04 22:29:50 -0800140
Etienne Basset43031542009-03-27 17:11:01 -0400141const char *smack_cipso_option = SMACK_CIPSO_OPTION;
142
Casey Schauflere114e472008-02-04 22:29:50 -0800143/*
Casey Schauflere114e472008-02-04 22:29:50 -0800144 * Values for parsing cipso rules
145 * SMK_DIGITLEN: Length of a digit field in a rule.
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700146 * SMK_CIPSOMIN: Minimum possible cipso rule length.
147 * SMK_CIPSOMAX: Maximum possible cipso rule length.
Casey Schauflere114e472008-02-04 22:29:50 -0800148 */
149#define SMK_DIGITLEN 4
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700150#define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
151#define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
152
153/*
154 * Values for parsing MAC rules
155 * SMK_ACCESS: Maximum possible combination of access permissions
156 * SMK_ACCESSLEN: Maximum length for a rule access field
157 * SMK_LOADLEN: Smack rule length
158 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200159#define SMK_OACCESS "rwxa"
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700160#define SMK_ACCESS "rwxatl"
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200161#define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
162#define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
163#define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
164#define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
Ahmed S. Darwishb500ce82008-03-13 12:32:34 -0700165
Casey Schauflerf7112e62012-05-06 15:22:02 -0700166/*
167 * Stricly for CIPSO level manipulation.
168 * Set the category bit number in a smack label sized buffer.
169 */
170static inline void smack_catset_bit(unsigned int cat, char *catsetp)
171{
172 if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
173 return;
174
175 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
176}
177
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500178/**
179 * smk_netlabel_audit_set - fill a netlbl_audit struct
180 * @nap: structure to fill
181 */
182static void smk_netlabel_audit_set(struct netlbl_audit *nap)
183{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700184 struct smack_known *skp = smk_of_current();
185
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500186 nap->loginuid = audit_get_loginuid(current);
187 nap->sessionid = audit_get_sessionid(current);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700188 nap->secid = skp->smk_secid;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500189}
190
191/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700192 * Value for parsing single label host rules
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500193 * "1.2.3.4 X"
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500194 */
195#define SMK_NETLBLADDRMIN 9
Casey Schauflere114e472008-02-04 22:29:50 -0800196
Casey Schauflere114e472008-02-04 22:29:50 -0800197/**
Rafal Krypae05b6f92013-01-10 19:42:00 +0100198 * smk_set_access - add a rule to the rule list or replace an old rule
199 * @srp: the rule to add or replace
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800200 * @rule_list: the list of rules
201 * @rule_lock: the rule list lock
Rafal Krypae05b6f92013-01-10 19:42:00 +0100202 * @global: if non-zero, indicates a global rule
Casey Schauflere114e472008-02-04 22:29:50 -0800203 *
204 * Looks through the current subject/object/access list for
205 * the subject/object pair and replaces the access that was
206 * there. If the pair isn't found add it with the specified
207 * access.
Sergio Luis81ea7142008-12-22 01:16:15 -0300208 *
209 * Returns 0 if nothing goes wrong or -ENOMEM if it fails
210 * during the allocation of the new pair to add.
Casey Schauflere114e472008-02-04 22:29:50 -0800211 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100212static int smk_set_access(struct smack_parsed_rule *srp,
213 struct list_head *rule_list,
214 struct mutex *rule_lock, int global)
Casey Schauflere114e472008-02-04 22:29:50 -0800215{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100216 struct smack_rule *sp;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100217 struct smack_master_list *smlp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800218 int found = 0;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100219 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800220
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800221 mutex_lock(rule_lock);
222
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700223 /*
224 * Because the object label is less likely to match
225 * than the subject label check it first
226 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800227 list_for_each_entry_rcu(sp, rule_list, list) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700228 if (sp->smk_object == srp->smk_object &&
229 sp->smk_subject == srp->smk_subject) {
Etienne Basset7198e2e2009-03-24 20:53:24 +0100230 found = 1;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100231 sp->smk_access |= srp->smk_access1;
232 sp->smk_access &= ~srp->smk_access2;
Casey Schauflere114e472008-02-04 22:29:50 -0800233 break;
234 }
Casey Schauflere114e472008-02-04 22:29:50 -0800235 }
236
Rafal Krypae05b6f92013-01-10 19:42:00 +0100237 if (found == 0) {
238 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
239 if (sp == NULL) {
240 rc = -ENOMEM;
241 goto out;
242 }
243
244 sp->smk_subject = srp->smk_subject;
245 sp->smk_object = srp->smk_object;
246 sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
247
248 list_add_rcu(&sp->list, rule_list);
249 /*
250 * If this is a global as opposed to self and a new rule
251 * it needs to get added for reporting.
252 */
253 if (global) {
254 smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
255 if (smlp != NULL) {
256 smlp->smk_rule = sp;
257 list_add_rcu(&smlp->list, &smack_rule_list);
258 } else
259 rc = -ENOMEM;
260 }
261 }
262
263out:
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800264 mutex_unlock(rule_lock);
Rafal Krypae05b6f92013-01-10 19:42:00 +0100265 return rc;
266}
Casey Schauflere114e472008-02-04 22:29:50 -0800267
Rafal Krypae05b6f92013-01-10 19:42:00 +0100268/**
269 * smk_perm_from_str - parse smack accesses from a text string
270 * @string: a text string that contains a Smack accesses code
271 *
272 * Returns an integer with respective bits set for specified accesses.
273 */
274static int smk_perm_from_str(const char *string)
275{
276 int perm = 0;
277 const char *cp;
278
279 for (cp = string; ; cp++)
280 switch (*cp) {
281 case '-':
282 break;
283 case 'r':
284 case 'R':
285 perm |= MAY_READ;
286 break;
287 case 'w':
288 case 'W':
289 perm |= MAY_WRITE;
290 break;
291 case 'x':
292 case 'X':
293 perm |= MAY_EXEC;
294 break;
295 case 'a':
296 case 'A':
297 perm |= MAY_APPEND;
298 break;
299 case 't':
300 case 'T':
301 perm |= MAY_TRANSMUTE;
302 break;
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700303 case 'l':
304 case 'L':
305 perm |= MAY_LOCK;
306 break;
Casey Schauflerd166c802014-08-27 14:51:27 -0700307 case 'b':
308 case 'B':
309 perm |= MAY_BRINGUP;
310 break;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100311 default:
312 return perm;
313 }
Casey Schauflere114e472008-02-04 22:29:50 -0800314}
315
316/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700317 * smk_fill_rule - Fill Smack rule from strings
318 * @subject: subject label string
319 * @object: object label string
Rafal Krypae05b6f92013-01-10 19:42:00 +0100320 * @access1: access string
321 * @access2: string with permissions to be removed
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300322 * @rule: Smack rule
323 * @import: if non-zero, import labels
Casey Schaufler35187212012-06-18 19:01:36 -0700324 * @len: label length limit
Casey Schauflerf7112e62012-05-06 15:22:02 -0700325 *
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200326 * Returns 0 on success, -EINVAL on failure and -ENOENT when either subject
327 * or object is missing.
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300328 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700329static int smk_fill_rule(const char *subject, const char *object,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100330 const char *access1, const char *access2,
331 struct smack_parsed_rule *rule, int import,
332 int len)
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300333{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700334 const char *cp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300335 struct smack_known *skp;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300336
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300337 if (import) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700338 rule->smk_subject = smk_import_entry(subject, len);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300339 if (rule->smk_subject == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200340 return -EINVAL;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300341
Casey Schaufler35187212012-06-18 19:01:36 -0700342 rule->smk_object = smk_import(object, len);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300343 if (rule->smk_object == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200344 return -EINVAL;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300345 } else {
Casey Schaufler35187212012-06-18 19:01:36 -0700346 cp = smk_parse_smack(subject, len);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700347 if (cp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200348 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700349 skp = smk_find_entry(cp);
350 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300351 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200352 return -ENOENT;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700353 rule->smk_subject = skp;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300354
Casey Schaufler35187212012-06-18 19:01:36 -0700355 cp = smk_parse_smack(object, len);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700356 if (cp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200357 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700358 skp = smk_find_entry(cp);
359 kfree(cp);
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300360 if (skp == NULL)
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200361 return -ENOENT;
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +0300362 rule->smk_object = skp->smk_known;
363 }
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300364
Rafal Krypae05b6f92013-01-10 19:42:00 +0100365 rule->smk_access1 = smk_perm_from_str(access1);
366 if (access2)
367 rule->smk_access2 = smk_perm_from_str(access2);
368 else
369 rule->smk_access2 = ~rule->smk_access1;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300370
Casey Schaufler35187212012-06-18 19:01:36 -0700371 return 0;
Jarkko Sakkinen828716c2011-09-08 10:12:01 +0300372}
373
374/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700375 * smk_parse_rule - parse Smack rule from load string
376 * @data: string to be parsed whose size is SMK_LOADLEN
377 * @rule: Smack rule
378 * @import: if non-zero, import labels
379 *
380 * Returns 0 on success, -1 on errors.
381 */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100382static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
383 int import)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700384{
385 int rc;
386
387 rc = smk_fill_rule(data, data + SMK_LABELLEN,
Rafal Krypae05b6f92013-01-10 19:42:00 +0100388 data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
389 import, SMK_LABELLEN);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700390 return rc;
391}
392
393/**
394 * smk_parse_long_rule - parse Smack rule from rule string
395 * @data: string to be parsed, null terminated
Rafal Krypae05b6f92013-01-10 19:42:00 +0100396 * @rule: Will be filled with Smack parsed rule
Casey Schauflerf7112e62012-05-06 15:22:02 -0700397 * @import: if non-zero, import labels
Rafal Krypa10289b02013-08-09 11:47:07 +0200398 * @tokens: numer of substrings expected in data
Casey Schauflerf7112e62012-05-06 15:22:02 -0700399 *
Rafal Krypa10289b02013-08-09 11:47:07 +0200400 * Returns number of processed bytes on success, -1 on failure.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700401 */
Rafal Krypa10289b02013-08-09 11:47:07 +0200402static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
403 int import, int tokens)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700404{
Rafal Krypa10289b02013-08-09 11:47:07 +0200405 ssize_t cnt = 0;
406 char *tok[4];
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200407 int rc;
Rafal Krypa10289b02013-08-09 11:47:07 +0200408 int i;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700409
Rafal Krypa10289b02013-08-09 11:47:07 +0200410 /*
411 * Parsing the rule in-place, filling all white-spaces with '\0'
412 */
413 for (i = 0; i < tokens; ++i) {
414 while (isspace(data[cnt]))
415 data[cnt++] = '\0';
Alan Cox3b9fc372012-07-26 14:47:11 -0700416
Rafal Krypa10289b02013-08-09 11:47:07 +0200417 if (data[cnt] == '\0')
418 /* Unexpected end of data */
419 return -1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700420
Rafal Krypa10289b02013-08-09 11:47:07 +0200421 tok[i] = data + cnt;
422
423 while (data[cnt] && !isspace(data[cnt]))
424 ++cnt;
Rafal Krypae05b6f92013-01-10 19:42:00 +0100425 }
Rafal Krypa10289b02013-08-09 11:47:07 +0200426 while (isspace(data[cnt]))
427 data[cnt++] = '\0';
Casey Schauflerf7112e62012-05-06 15:22:02 -0700428
Rafal Krypa10289b02013-08-09 11:47:07 +0200429 while (i < 4)
430 tok[i++] = NULL;
431
Jarkko Sakkinen398ce072013-11-28 19:16:46 +0200432 rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
433 return rc == 0 ? cnt : rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700434}
435
436#define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
437#define SMK_LONG_FMT 1 /* Variable long label format */
Rafal Krypae05b6f92013-01-10 19:42:00 +0100438#define SMK_CHANGE_FMT 2 /* Rule modification format */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700439/**
440 * smk_write_rules_list - write() for any /smack rule file
Randy Dunlap251a2a92009-02-18 11:42:33 -0800441 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800442 * @buf: where to get the data from
443 * @count: bytes sent
444 * @ppos: where to start - must be 0
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800445 * @rule_list: the list of rules to write to
446 * @rule_lock: lock for the rule list
Rafal Krypae05b6f92013-01-10 19:42:00 +0100447 * @format: /smack/load or /smack/load2 or /smack/change-rule format.
Casey Schauflere114e472008-02-04 22:29:50 -0800448 *
449 * Get one smack access rule from above.
Casey Schauflerf7112e62012-05-06 15:22:02 -0700450 * The format for SMK_LONG_FMT is:
451 * "subject<whitespace>object<whitespace>access[<whitespace>...]"
452 * The format for SMK_FIXED24_FMT is exactly:
453 * "subject object rwxat"
Rafal Krypae05b6f92013-01-10 19:42:00 +0100454 * The format for SMK_CHANGE_FMT is:
455 * "subject<whitespace>object<whitespace>
456 * acc_enable<whitespace>acc_disable[<whitespace>...]"
Casey Schauflere114e472008-02-04 22:29:50 -0800457 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700458static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
459 size_t count, loff_t *ppos,
460 struct list_head *rule_list,
461 struct mutex *rule_lock, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800462{
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200463 struct smack_parsed_rule rule;
Casey Schauflere114e472008-02-04 22:29:50 -0800464 char *data;
Rafal Krypa10289b02013-08-09 11:47:07 +0200465 int rc;
466 int trunc = 0;
467 int tokens;
468 ssize_t cnt = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800469
470 /*
Casey Schauflere114e472008-02-04 22:29:50 -0800471 * No partial writes.
472 * Enough data must be present.
473 */
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200474 if (*ppos != 0)
475 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -0800476
Casey Schauflerf7112e62012-05-06 15:22:02 -0700477 if (format == SMK_FIXED24_FMT) {
478 /*
479 * Minor hack for backward compatibility
480 */
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700481 if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700482 return -EINVAL;
Rafal Krypa10289b02013-08-09 11:47:07 +0200483 } else {
484 if (count >= PAGE_SIZE) {
485 count = PAGE_SIZE - 1;
486 trunc = 1;
487 }
488 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700489
Rafal Krypa10289b02013-08-09 11:47:07 +0200490 data = kmalloc(count + 1, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -0800491 if (data == NULL)
492 return -ENOMEM;
493
494 if (copy_from_user(data, buf, count) != 0) {
495 rc = -EFAULT;
496 goto out;
497 }
498
Rafal Krypa10289b02013-08-09 11:47:07 +0200499 /*
500 * In case of parsing only part of user buf,
501 * avoid having partial rule at the data buffer
502 */
503 if (trunc) {
504 while (count > 0 && (data[count - 1] != '\n'))
505 --count;
506 if (count == 0) {
507 rc = -EINVAL;
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200508 goto out;
Rafal Krypa10289b02013-08-09 11:47:07 +0200509 }
510 }
511
512 data[count] = '\0';
513 tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
514 while (cnt < count) {
515 if (format == SMK_FIXED24_FMT) {
516 rc = smk_parse_rule(data, &rule, 1);
517 if (rc != 0) {
518 rc = -EINVAL;
519 goto out;
520 }
521 cnt = count;
522 } else {
523 rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
524 if (rc <= 0) {
525 rc = -EINVAL;
526 goto out;
527 }
528 cnt += rc;
529 }
530
531 if (rule_list == NULL)
532 rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
533 &rule.smk_subject->smk_rules_lock, 1);
534 else
535 rc = smk_set_access(&rule, rule_list, rule_lock, 0);
536
537 if (rc)
Tomasz Stanislawski470043b2013-06-06 09:30:50 +0200538 goto out;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700539 }
540
Rafal Krypa10289b02013-08-09 11:47:07 +0200541 rc = cnt;
Casey Schauflere114e472008-02-04 22:29:50 -0800542out:
543 kfree(data);
544 return rc;
545}
546
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800547/*
Casey Schaufler40809562011-11-10 15:02:22 -0800548 * Core logic for smackfs seq list operations.
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800549 */
550
Casey Schaufler40809562011-11-10 15:02:22 -0800551static void *smk_seq_start(struct seq_file *s, loff_t *pos,
552 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800553{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700554 struct list_head *list;
555
556 /*
557 * This is 0 the first time through.
558 */
559 if (s->index == 0)
Casey Schaufler40809562011-11-10 15:02:22 -0800560 s->private = head;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700561
562 if (s->private == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800563 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700564
565 list = s->private;
566 if (list_empty(list))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800567 return NULL;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700568
569 if (s->index == 0)
570 return list->next;
571 return list;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800572}
573
Casey Schaufler40809562011-11-10 15:02:22 -0800574static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
575 struct list_head *head)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800576{
577 struct list_head *list = v;
578
Casey Schaufler40809562011-11-10 15:02:22 -0800579 if (list_is_last(list, head)) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700580 s->private = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800581 return NULL;
582 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700583 s->private = list->next;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800584 return list->next;
585}
586
Casey Schaufler40809562011-11-10 15:02:22 -0800587static void smk_seq_stop(struct seq_file *s, void *v)
588{
589 /* No-op */
590}
591
Casey Schauflerf7112e62012-05-06 15:22:02 -0700592static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
Casey Schaufler40809562011-11-10 15:02:22 -0800593{
Casey Schauflerf7112e62012-05-06 15:22:02 -0700594 /*
595 * Don't show any rules with label names too long for
596 * interface file (/smack/load or /smack/load2)
597 * because you should expect to be able to write
598 * anything you read back.
599 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700600 if (strlen(srp->smk_subject->smk_known) >= max ||
601 strlen(srp->smk_object) >= max)
Casey Schauflerf7112e62012-05-06 15:22:02 -0700602 return;
Casey Schaufler40809562011-11-10 15:02:22 -0800603
Rafal Krypa65ee7f42012-07-09 19:36:34 +0200604 if (srp->smk_access == 0)
605 return;
606
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700607 seq_printf(s, "%s %s", srp->smk_subject->smk_known, srp->smk_object);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800608
609 seq_putc(s, ' ');
610
611 if (srp->smk_access & MAY_READ)
612 seq_putc(s, 'r');
613 if (srp->smk_access & MAY_WRITE)
614 seq_putc(s, 'w');
615 if (srp->smk_access & MAY_EXEC)
616 seq_putc(s, 'x');
617 if (srp->smk_access & MAY_APPEND)
618 seq_putc(s, 'a');
619 if (srp->smk_access & MAY_TRANSMUTE)
620 seq_putc(s, 't');
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700621 if (srp->smk_access & MAY_LOCK)
622 seq_putc(s, 'l');
Casey Schauflerd166c802014-08-27 14:51:27 -0700623 if (srp->smk_access & MAY_BRINGUP)
624 seq_putc(s, 'b');
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800625
626 seq_putc(s, '\n');
Casey Schauflerf7112e62012-05-06 15:22:02 -0700627}
628
629/*
630 * Seq_file read operations for /smack/load
631 */
632
633static void *load2_seq_start(struct seq_file *s, loff_t *pos)
634{
635 return smk_seq_start(s, pos, &smack_rule_list);
636}
637
638static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
639{
640 return smk_seq_next(s, v, pos, &smack_rule_list);
641}
642
643static int load_seq_show(struct seq_file *s, void *v)
644{
645 struct list_head *list = v;
646 struct smack_master_list *smlp =
647 list_entry(list, struct smack_master_list, list);
648
649 smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800650
651 return 0;
652}
653
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800654static const struct seq_operations load_seq_ops = {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700655 .start = load2_seq_start,
656 .next = load2_seq_next,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800657 .show = load_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800658 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800659};
660
661/**
662 * smk_open_load - open() for /smack/load
663 * @inode: inode structure representing file
664 * @file: "load" file pointer
665 *
666 * For reading, use load_seq_* seq_file reading operations.
667 */
668static int smk_open_load(struct inode *inode, struct file *file)
669{
670 return seq_open(file, &load_seq_ops);
671}
672
673/**
674 * smk_write_load - write() for /smack/load
675 * @file: file pointer, not actually used
676 * @buf: where to get the data from
677 * @count: bytes sent
678 * @ppos: where to start - must be 0
679 *
680 */
681static ssize_t smk_write_load(struct file *file, const char __user *buf,
682 size_t count, loff_t *ppos)
683{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800684 /*
685 * Must have privilege.
686 * No partial writes.
687 * Enough data must be present.
688 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700689 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800690 return -EPERM;
691
Casey Schauflerf7112e62012-05-06 15:22:02 -0700692 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
693 SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800694}
695
Casey Schauflere114e472008-02-04 22:29:50 -0800696static const struct file_operations smk_load_ops = {
697 .open = smk_open_load,
698 .read = seq_read,
699 .llseek = seq_lseek,
700 .write = smk_write_load,
Ahmed S. Darwishcb622bb2008-03-24 12:29:49 -0700701 .release = seq_release,
Casey Schauflere114e472008-02-04 22:29:50 -0800702};
703
704/**
705 * smk_cipso_doi - initialize the CIPSO domain
706 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700707static void smk_cipso_doi(void)
Casey Schauflere114e472008-02-04 22:29:50 -0800708{
709 int rc;
710 struct cipso_v4_doi *doip;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500711 struct netlbl_audit nai;
Casey Schauflere114e472008-02-04 22:29:50 -0800712
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500713 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800714
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500715 rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
Casey Schauflere114e472008-02-04 22:29:50 -0800716 if (rc != 0)
717 printk(KERN_WARNING "%s:%d remove rc = %d\n",
718 __func__, __LINE__, rc);
719
720 doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
721 if (doip == NULL)
722 panic("smack: Failed to initialize cipso DOI.\n");
723 doip->map.std = NULL;
724 doip->doi = smk_cipso_doi_value;
725 doip->type = CIPSO_V4_MAP_PASS;
726 doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
727 for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
728 doip->tags[rc] = CIPSO_V4_TAG_INVALID;
729
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500730 rc = netlbl_cfg_cipsov4_add(doip, &nai);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400731 if (rc != 0) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500732 printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
Casey Schauflere114e472008-02-04 22:29:50 -0800733 __func__, __LINE__, rc);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400734 kfree(doip);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500735 return;
736 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500737 rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500738 if (rc != 0) {
739 printk(KERN_WARNING "%s:%d map add rc = %d\n",
740 __func__, __LINE__, rc);
741 kfree(doip);
742 return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400743 }
Casey Schauflere114e472008-02-04 22:29:50 -0800744}
745
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800746/**
747 * smk_unlbl_ambient - initialize the unlabeled domain
Randy Dunlap251a2a92009-02-18 11:42:33 -0800748 * @oldambient: previous domain string
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800749 */
Casey Schaufler30aa4fa2008-04-28 02:13:43 -0700750static void smk_unlbl_ambient(char *oldambient)
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800751{
752 int rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500753 struct netlbl_audit nai;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800754
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500755 smk_netlabel_audit_set(&nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800756
757 if (oldambient != NULL) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500758 rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800759 if (rc != 0)
760 printk(KERN_WARNING "%s:%d remove rc = %d\n",
761 __func__, __LINE__, rc);
762 }
Casey Schauflerf7112e62012-05-06 15:22:02 -0700763 if (smack_net_ambient == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700764 smack_net_ambient = &smack_known_floor;
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800765
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700766 rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500767 NULL, NULL, &nai);
Casey Schaufler4bc87e62008-02-15 15:24:25 -0800768 if (rc != 0)
769 printk(KERN_WARNING "%s:%d add rc = %d\n",
770 __func__, __LINE__, rc);
771}
772
Casey Schauflere114e472008-02-04 22:29:50 -0800773/*
774 * Seq_file read operations for /smack/cipso
775 */
776
777static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
778{
Casey Schaufler40809562011-11-10 15:02:22 -0800779 return smk_seq_start(s, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800780}
781
782static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
783{
Casey Schaufler40809562011-11-10 15:02:22 -0800784 return smk_seq_next(s, v, pos, &smack_known_list);
Casey Schauflere114e472008-02-04 22:29:50 -0800785}
786
787/*
788 * Print cipso labels in format:
789 * label level[/cat[,cat]]
790 */
791static int cipso_seq_show(struct seq_file *s, void *v)
792{
Etienne Basset7198e2e2009-03-24 20:53:24 +0100793 struct list_head *list = v;
794 struct smack_known *skp =
795 list_entry(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400796 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800797 char sep = '/';
Casey Schauflere114e472008-02-04 22:29:50 -0800798 int i;
Casey Schauflere114e472008-02-04 22:29:50 -0800799
Casey Schauflerf7112e62012-05-06 15:22:02 -0700800 /*
801 * Don't show a label that could not have been set using
802 * /smack/cipso. This is in support of the notion that
803 * anything read from /smack/cipso ought to be writeable
804 * to /smack/cipso.
805 *
806 * /smack/cipso2 should be used instead.
807 */
808 if (strlen(skp->smk_known) >= SMK_LABELLEN)
Casey Schauflere114e472008-02-04 22:29:50 -0800809 return 0;
810
Casey Schauflerf7112e62012-05-06 15:22:02 -0700811 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
Casey Schauflere114e472008-02-04 22:29:50 -0800812
Paul Moore4fbe63d2014-08-01 11:17:37 -0400813 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
814 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700815 seq_printf(s, "%c%d", sep, i);
816 sep = ',';
817 }
Casey Schauflere114e472008-02-04 22:29:50 -0800818
819 seq_putc(s, '\n');
820
821 return 0;
822}
823
James Morris88e9d342009-09-22 16:43:43 -0700824static const struct seq_operations cipso_seq_ops = {
Casey Schauflere114e472008-02-04 22:29:50 -0800825 .start = cipso_seq_start,
Casey Schauflere114e472008-02-04 22:29:50 -0800826 .next = cipso_seq_next,
827 .show = cipso_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -0800828 .stop = smk_seq_stop,
Casey Schauflere114e472008-02-04 22:29:50 -0800829};
830
831/**
832 * smk_open_cipso - open() for /smack/cipso
833 * @inode: inode structure representing file
834 * @file: "cipso" file pointer
835 *
836 * Connect our cipso_seq_* operations with /smack/cipso
837 * file_operations
838 */
839static int smk_open_cipso(struct inode *inode, struct file *file)
840{
841 return seq_open(file, &cipso_seq_ops);
842}
843
844/**
Casey Schauflerf7112e62012-05-06 15:22:02 -0700845 * smk_set_cipso - do the work for write() for cipso and cipso2
Randy Dunlap251a2a92009-02-18 11:42:33 -0800846 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -0800847 * @buf: where to get the data from
848 * @count: bytes sent
849 * @ppos: where to start
Casey Schauflerf7112e62012-05-06 15:22:02 -0700850 * @format: /smack/cipso or /smack/cipso2
Casey Schauflere114e472008-02-04 22:29:50 -0800851 *
852 * Accepts only one cipso rule per write call.
853 * Returns number of bytes written or error code, as appropriate
854 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700855static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
856 size_t count, loff_t *ppos, int format)
Casey Schauflere114e472008-02-04 22:29:50 -0800857{
858 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700859 struct netlbl_lsm_secattr ncats;
860 char mapcatset[SMK_CIPSOLEN];
Casey Schauflere114e472008-02-04 22:29:50 -0800861 int maplevel;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700862 unsigned int cat;
Casey Schauflere114e472008-02-04 22:29:50 -0800863 int catlen;
864 ssize_t rc = -EINVAL;
865 char *data = NULL;
866 char *rule;
867 int ret;
868 int i;
869
870 /*
871 * Must have privilege.
872 * No partial writes.
873 * Enough data must be present.
874 */
Casey Schaufler1880eff2012-06-05 15:28:30 -0700875 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -0800876 return -EPERM;
877 if (*ppos != 0)
878 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700879 if (format == SMK_FIXED24_FMT &&
880 (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
Casey Schauflere114e472008-02-04 22:29:50 -0800881 return -EINVAL;
882
883 data = kzalloc(count + 1, GFP_KERNEL);
884 if (data == NULL)
885 return -ENOMEM;
886
887 if (copy_from_user(data, buf, count) != 0) {
888 rc = -EFAULT;
889 goto unlockedout;
890 }
891
892 data[count] = '\0';
893 rule = data;
894 /*
895 * Only allow one writer at a time. Writes should be
896 * quite rare and small in any case.
897 */
898 mutex_lock(&smack_cipso_lock);
899
900 skp = smk_import_entry(rule, 0);
901 if (skp == NULL)
902 goto out;
903
Casey Schauflerf7112e62012-05-06 15:22:02 -0700904 if (format == SMK_FIXED24_FMT)
905 rule += SMK_LABELLEN;
906 else
Passion,Zhao0fcfee62013-06-03 11:42:24 +0800907 rule += strlen(skp->smk_known) + 1;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700908
Casey Schauflere114e472008-02-04 22:29:50 -0800909 ret = sscanf(rule, "%d", &maplevel);
910 if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
911 goto out;
912
913 rule += SMK_DIGITLEN;
914 ret = sscanf(rule, "%d", &catlen);
915 if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
916 goto out;
917
Casey Schauflerf7112e62012-05-06 15:22:02 -0700918 if (format == SMK_FIXED24_FMT &&
919 count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
Casey Schauflere114e472008-02-04 22:29:50 -0800920 goto out;
921
922 memset(mapcatset, 0, sizeof(mapcatset));
923
924 for (i = 0; i < catlen; i++) {
925 rule += SMK_DIGITLEN;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700926 ret = sscanf(rule, "%u", &cat);
Casey Schaufler677264e2013-06-28 13:47:07 -0700927 if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
Casey Schauflere114e472008-02-04 22:29:50 -0800928 goto out;
929
930 smack_catset_bit(cat, mapcatset);
931 }
932
Casey Schauflerf7112e62012-05-06 15:22:02 -0700933 rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
934 if (rc >= 0) {
Paul Moore4fbe63d2014-08-01 11:17:37 -0400935 netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700936 skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
937 skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
938 rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -0800939 }
940
Casey Schauflere114e472008-02-04 22:29:50 -0800941out:
942 mutex_unlock(&smack_cipso_lock);
943unlockedout:
944 kfree(data);
945 return rc;
946}
947
Casey Schauflerf7112e62012-05-06 15:22:02 -0700948/**
949 * smk_write_cipso - write() for /smack/cipso
950 * @file: file pointer, not actually used
951 * @buf: where to get the data from
952 * @count: bytes sent
953 * @ppos: where to start
954 *
955 * Accepts only one cipso rule per write call.
956 * Returns number of bytes written or error code, as appropriate
957 */
958static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
959 size_t count, loff_t *ppos)
960{
961 return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
962}
963
Casey Schauflere114e472008-02-04 22:29:50 -0800964static const struct file_operations smk_cipso_ops = {
965 .open = smk_open_cipso,
966 .read = seq_read,
967 .llseek = seq_lseek,
968 .write = smk_write_cipso,
969 .release = seq_release,
970};
971
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500972/*
Casey Schauflerf7112e62012-05-06 15:22:02 -0700973 * Seq_file read operations for /smack/cipso2
974 */
975
976/*
977 * Print cipso labels in format:
978 * label level[/cat[,cat]]
979 */
980static int cipso2_seq_show(struct seq_file *s, void *v)
981{
982 struct list_head *list = v;
983 struct smack_known *skp =
984 list_entry(list, struct smack_known, list);
Paul Moore4fbe63d2014-08-01 11:17:37 -0400985 struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700986 char sep = '/';
987 int i;
988
989 seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
990
Paul Moore4fbe63d2014-08-01 11:17:37 -0400991 for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
992 i = netlbl_catmap_walk(cmp, i + 1)) {
Casey Schauflerf7112e62012-05-06 15:22:02 -0700993 seq_printf(s, "%c%d", sep, i);
994 sep = ',';
995 }
996
997 seq_putc(s, '\n');
998
999 return 0;
1000}
1001
1002static const struct seq_operations cipso2_seq_ops = {
1003 .start = cipso_seq_start,
1004 .next = cipso_seq_next,
1005 .show = cipso2_seq_show,
1006 .stop = smk_seq_stop,
1007};
1008
1009/**
1010 * smk_open_cipso2 - open() for /smack/cipso2
1011 * @inode: inode structure representing file
1012 * @file: "cipso2" file pointer
1013 *
1014 * Connect our cipso_seq_* operations with /smack/cipso2
1015 * file_operations
1016 */
1017static int smk_open_cipso2(struct inode *inode, struct file *file)
1018{
1019 return seq_open(file, &cipso2_seq_ops);
1020}
1021
1022/**
1023 * smk_write_cipso2 - write() for /smack/cipso2
1024 * @file: file pointer, not actually used
1025 * @buf: where to get the data from
1026 * @count: bytes sent
1027 * @ppos: where to start
1028 *
1029 * Accepts only one cipso rule per write call.
1030 * Returns number of bytes written or error code, as appropriate
1031 */
1032static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
1033 size_t count, loff_t *ppos)
1034{
1035 return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
1036}
1037
1038static const struct file_operations smk_cipso2_ops = {
1039 .open = smk_open_cipso2,
1040 .read = seq_read,
1041 .llseek = seq_lseek,
1042 .write = smk_write_cipso2,
1043 .release = seq_release,
1044};
1045
1046/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001047 * Seq_file read operations for /smack/netlabel
1048 */
1049
1050static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
1051{
Casey Schaufler40809562011-11-10 15:02:22 -08001052 return smk_seq_start(s, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001053}
1054
1055static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
1056{
Casey Schaufler40809562011-11-10 15:02:22 -08001057 return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001058}
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001059#define BEBITS (sizeof(__be32) * 8)
1060
1061/*
1062 * Print host/label pairs
1063 */
1064static int netlbladdr_seq_show(struct seq_file *s, void *v)
1065{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001066 struct list_head *list = v;
1067 struct smk_netlbladdr *skp =
1068 list_entry(list, struct smk_netlbladdr, list);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001069 unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001070 int maskn;
1071 u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001072
etienne113a0e42009-03-04 07:33:51 +01001073 for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001074
1075 seq_printf(s, "%u.%u.%u.%u/%d %s\n",
1076 hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
1077
1078 return 0;
1079}
1080
James Morris88e9d342009-09-22 16:43:43 -07001081static const struct seq_operations netlbladdr_seq_ops = {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001082 .start = netlbladdr_seq_start,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001083 .next = netlbladdr_seq_next,
1084 .show = netlbladdr_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001085 .stop = smk_seq_stop,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001086};
1087
1088/**
1089 * smk_open_netlbladdr - open() for /smack/netlabel
1090 * @inode: inode structure representing file
1091 * @file: "netlabel" file pointer
1092 *
1093 * Connect our netlbladdr_seq_* operations with /smack/netlabel
1094 * file_operations
1095 */
1096static int smk_open_netlbladdr(struct inode *inode, struct file *file)
1097{
1098 return seq_open(file, &netlbladdr_seq_ops);
1099}
1100
1101/**
etienne113a0e42009-03-04 07:33:51 +01001102 * smk_netlbladdr_insert
1103 * @new : netlabel to insert
1104 *
1105 * This helper insert netlabel in the smack_netlbladdrs list
1106 * sorted by netmask length (longest to smallest)
Etienne Basset7198e2e2009-03-24 20:53:24 +01001107 * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
1108 *
etienne113a0e42009-03-04 07:33:51 +01001109 */
1110static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
1111{
Etienne Basset7198e2e2009-03-24 20:53:24 +01001112 struct smk_netlbladdr *m, *m_next;
etienne113a0e42009-03-04 07:33:51 +01001113
Etienne Basset7198e2e2009-03-24 20:53:24 +01001114 if (list_empty(&smk_netlbladdr_list)) {
1115 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001116 return;
1117 }
1118
Jiri Pirko05725f72009-04-14 20:17:16 +02001119 m = list_entry_rcu(smk_netlbladdr_list.next,
1120 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001121
etienne113a0e42009-03-04 07:33:51 +01001122 /* the comparison '>' is a bit hacky, but works */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001123 if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
1124 list_add_rcu(&new->list, &smk_netlbladdr_list);
etienne113a0e42009-03-04 07:33:51 +01001125 return;
1126 }
Etienne Basset7198e2e2009-03-24 20:53:24 +01001127
1128 list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
1129 if (list_is_last(&m->list, &smk_netlbladdr_list)) {
1130 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001131 return;
1132 }
Jiri Pirko05725f72009-04-14 20:17:16 +02001133 m_next = list_entry_rcu(m->list.next,
1134 struct smk_netlbladdr, list);
Etienne Basset7198e2e2009-03-24 20:53:24 +01001135 if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
1136 list_add_rcu(&new->list, &m->list);
etienne113a0e42009-03-04 07:33:51 +01001137 return;
1138 }
1139 }
1140}
1141
1142
1143/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001144 * smk_write_netlbladdr - write() for /smack/netlabel
Randy Dunlap251a2a92009-02-18 11:42:33 -08001145 * @file: file pointer, not actually used
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001146 * @buf: where to get the data from
1147 * @count: bytes sent
1148 * @ppos: where to start
1149 *
1150 * Accepts only one netlbladdr per write call.
1151 * Returns number of bytes written or error code, as appropriate
1152 */
1153static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
1154 size_t count, loff_t *ppos)
1155{
1156 struct smk_netlbladdr *skp;
1157 struct sockaddr_in newname;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001158 char *smack;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001159 char *sp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001160 char *data;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001161 char *host = (char *)&newname.sin_addr.s_addr;
1162 int rc;
1163 struct netlbl_audit audit_info;
1164 struct in_addr mask;
1165 unsigned int m;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001166 int found;
etienne113a0e42009-03-04 07:33:51 +01001167 u32 mask_bits = (1<<31);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001168 __be32 nsa;
etienne113a0e42009-03-04 07:33:51 +01001169 u32 temp_mask;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001170
1171 /*
1172 * Must have privilege.
1173 * No partial writes.
1174 * Enough data must be present.
1175 * "<addr/mask, as a.b.c.d/e><space><label>"
1176 * "<addr, as a.b.c.d><space><label>"
1177 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001178 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001179 return -EPERM;
1180 if (*ppos != 0)
1181 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001182 if (count < SMK_NETLBLADDRMIN)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001183 return -EINVAL;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001184
1185 data = kzalloc(count + 1, GFP_KERNEL);
1186 if (data == NULL)
1187 return -ENOMEM;
1188
1189 if (copy_from_user(data, buf, count) != 0) {
1190 rc = -EFAULT;
1191 goto free_data_out;
1192 }
1193
1194 smack = kzalloc(count + 1, GFP_KERNEL);
1195 if (smack == NULL) {
1196 rc = -ENOMEM;
1197 goto free_data_out;
1198 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001199
1200 data[count] = '\0';
1201
Toralf Försterec554fa2014-04-27 19:33:34 +02001202 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001203 &host[0], &host[1], &host[2], &host[3], &m, smack);
1204 if (rc != 6) {
1205 rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
1206 &host[0], &host[1], &host[2], &host[3], smack);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001207 if (rc != 5) {
1208 rc = -EINVAL;
1209 goto free_out;
1210 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001211 m = BEBITS;
1212 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07001213 if (m > BEBITS) {
1214 rc = -EINVAL;
1215 goto free_out;
1216 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001217
Casey Schauflerf7112e62012-05-06 15:22:02 -07001218 /*
1219 * If smack begins with '-', it is an option, don't import it
1220 */
Etienne Basset43031542009-03-27 17:11:01 -04001221 if (smack[0] != '-') {
1222 sp = smk_import(smack, 0);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001223 if (sp == NULL) {
1224 rc = -EINVAL;
1225 goto free_out;
1226 }
Etienne Basset43031542009-03-27 17:11:01 -04001227 } else {
1228 /* check known options */
1229 if (strcmp(smack, smack_cipso_option) == 0)
1230 sp = (char *)smack_cipso_option;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001231 else {
1232 rc = -EINVAL;
1233 goto free_out;
1234 }
Etienne Basset43031542009-03-27 17:11:01 -04001235 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001236
etienne113a0e42009-03-04 07:33:51 +01001237 for (temp_mask = 0; m > 0; m--) {
1238 temp_mask |= mask_bits;
1239 mask_bits >>= 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001240 }
etienne113a0e42009-03-04 07:33:51 +01001241 mask.s_addr = cpu_to_be32(temp_mask);
1242
1243 newname.sin_addr.s_addr &= mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001244 /*
1245 * Only allow one writer at a time. Writes should be
1246 * quite rare and small in any case.
1247 */
1248 mutex_lock(&smk_netlbladdr_lock);
1249
1250 nsa = newname.sin_addr.s_addr;
etienne113a0e42009-03-04 07:33:51 +01001251 /* try to find if the prefix is already in the list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01001252 found = 0;
1253 list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001254 if (skp->smk_host.sin_addr.s_addr == nsa &&
Etienne Basset7198e2e2009-03-24 20:53:24 +01001255 skp->smk_mask.s_addr == mask.s_addr) {
1256 found = 1;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001257 break;
Etienne Basset7198e2e2009-03-24 20:53:24 +01001258 }
1259 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001260 smk_netlabel_audit_set(&audit_info);
1261
Etienne Basset7198e2e2009-03-24 20:53:24 +01001262 if (found == 0) {
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001263 skp = kzalloc(sizeof(*skp), GFP_KERNEL);
1264 if (skp == NULL)
1265 rc = -ENOMEM;
1266 else {
1267 rc = 0;
1268 skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
1269 skp->smk_mask.s_addr = mask.s_addr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001270 skp->smk_label = sp;
etienne113a0e42009-03-04 07:33:51 +01001271 smk_netlbladdr_insert(skp);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001272 }
1273 } else {
Etienne Basset43031542009-03-27 17:11:01 -04001274 /* we delete the unlabeled entry, only if the previous label
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001275 * wasn't the special CIPSO option */
Etienne Basset43031542009-03-27 17:11:01 -04001276 if (skp->smk_label != smack_cipso_option)
1277 rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
1278 &skp->smk_host.sin_addr, &skp->smk_mask,
1279 PF_INET, &audit_info);
1280 else
1281 rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001282 skp->smk_label = sp;
1283 }
1284
1285 /*
1286 * Now tell netlabel about the single label nature of
1287 * this host so that incoming packets get labeled.
Etienne Basset43031542009-03-27 17:11:01 -04001288 * but only if we didn't get the special CIPSO option
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001289 */
Etienne Basset43031542009-03-27 17:11:01 -04001290 if (rc == 0 && sp != smack_cipso_option)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001291 rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
1292 &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
1293 smack_to_secid(skp->smk_label), &audit_info);
1294
1295 if (rc == 0)
1296 rc = count;
1297
1298 mutex_unlock(&smk_netlbladdr_lock);
1299
Casey Schauflerf7112e62012-05-06 15:22:02 -07001300free_out:
1301 kfree(smack);
1302free_data_out:
1303 kfree(data);
1304
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001305 return rc;
1306}
1307
1308static const struct file_operations smk_netlbladdr_ops = {
1309 .open = smk_open_netlbladdr,
1310 .read = seq_read,
1311 .llseek = seq_lseek,
1312 .write = smk_write_netlbladdr,
1313 .release = seq_release,
1314};
1315
Casey Schauflere114e472008-02-04 22:29:50 -08001316/**
1317 * smk_read_doi - read() for /smack/doi
1318 * @filp: file pointer, not actually used
1319 * @buf: where to put the result
1320 * @count: maximum to send along
1321 * @ppos: where to start
1322 *
1323 * Returns number of bytes read or error code, as appropriate
1324 */
1325static ssize_t smk_read_doi(struct file *filp, char __user *buf,
1326 size_t count, loff_t *ppos)
1327{
1328 char temp[80];
1329 ssize_t rc;
1330
1331 if (*ppos != 0)
1332 return 0;
1333
1334 sprintf(temp, "%d", smk_cipso_doi_value);
1335 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1336
1337 return rc;
1338}
1339
1340/**
1341 * smk_write_doi - write() for /smack/doi
Randy Dunlap251a2a92009-02-18 11:42:33 -08001342 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001343 * @buf: where to get the data from
1344 * @count: bytes sent
1345 * @ppos: where to start
1346 *
1347 * Returns number of bytes written or error code, as appropriate
1348 */
1349static ssize_t smk_write_doi(struct file *file, const char __user *buf,
1350 size_t count, loff_t *ppos)
1351{
1352 char temp[80];
1353 int i;
1354
Casey Schaufler1880eff2012-06-05 15:28:30 -07001355 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001356 return -EPERM;
1357
1358 if (count >= sizeof(temp) || count == 0)
1359 return -EINVAL;
1360
1361 if (copy_from_user(temp, buf, count) != 0)
1362 return -EFAULT;
1363
1364 temp[count] = '\0';
1365
1366 if (sscanf(temp, "%d", &i) != 1)
1367 return -EINVAL;
1368
1369 smk_cipso_doi_value = i;
1370
1371 smk_cipso_doi();
1372
1373 return count;
1374}
1375
1376static const struct file_operations smk_doi_ops = {
1377 .read = smk_read_doi,
1378 .write = smk_write_doi,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001379 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001380};
1381
1382/**
1383 * smk_read_direct - read() for /smack/direct
1384 * @filp: file pointer, not actually used
1385 * @buf: where to put the result
1386 * @count: maximum to send along
1387 * @ppos: where to start
1388 *
1389 * Returns number of bytes read or error code, as appropriate
1390 */
1391static ssize_t smk_read_direct(struct file *filp, char __user *buf,
1392 size_t count, loff_t *ppos)
1393{
1394 char temp[80];
1395 ssize_t rc;
1396
1397 if (*ppos != 0)
1398 return 0;
1399
1400 sprintf(temp, "%d", smack_cipso_direct);
1401 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1402
1403 return rc;
1404}
1405
1406/**
1407 * smk_write_direct - write() for /smack/direct
Randy Dunlap251a2a92009-02-18 11:42:33 -08001408 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001409 * @buf: where to get the data from
1410 * @count: bytes sent
1411 * @ppos: where to start
1412 *
1413 * Returns number of bytes written or error code, as appropriate
1414 */
1415static ssize_t smk_write_direct(struct file *file, const char __user *buf,
1416 size_t count, loff_t *ppos)
1417{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001418 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001419 char temp[80];
1420 int i;
1421
Casey Schaufler1880eff2012-06-05 15:28:30 -07001422 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001423 return -EPERM;
1424
1425 if (count >= sizeof(temp) || count == 0)
1426 return -EINVAL;
1427
1428 if (copy_from_user(temp, buf, count) != 0)
1429 return -EFAULT;
1430
1431 temp[count] = '\0';
1432
1433 if (sscanf(temp, "%d", &i) != 1)
1434 return -EINVAL;
1435
Casey Schauflerf7112e62012-05-06 15:22:02 -07001436 /*
1437 * Don't do anything if the value hasn't actually changed.
1438 * If it is changing reset the level on entries that were
1439 * set up to be direct when they were created.
1440 */
1441 if (smack_cipso_direct != i) {
1442 mutex_lock(&smack_known_lock);
1443 list_for_each_entry_rcu(skp, &smack_known_list, list)
1444 if (skp->smk_netlabel.attr.mls.lvl ==
1445 smack_cipso_direct)
1446 skp->smk_netlabel.attr.mls.lvl = i;
1447 smack_cipso_direct = i;
1448 mutex_unlock(&smack_known_lock);
1449 }
Casey Schauflere114e472008-02-04 22:29:50 -08001450
1451 return count;
1452}
1453
1454static const struct file_operations smk_direct_ops = {
1455 .read = smk_read_direct,
1456 .write = smk_write_direct,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001457 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001458};
1459
1460/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001461 * smk_read_mapped - read() for /smack/mapped
1462 * @filp: file pointer, not actually used
1463 * @buf: where to put the result
1464 * @count: maximum to send along
1465 * @ppos: where to start
1466 *
1467 * Returns number of bytes read or error code, as appropriate
1468 */
1469static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
1470 size_t count, loff_t *ppos)
1471{
1472 char temp[80];
1473 ssize_t rc;
1474
1475 if (*ppos != 0)
1476 return 0;
1477
1478 sprintf(temp, "%d", smack_cipso_mapped);
1479 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1480
1481 return rc;
1482}
1483
1484/**
1485 * smk_write_mapped - write() for /smack/mapped
1486 * @file: file pointer, not actually used
1487 * @buf: where to get the data from
1488 * @count: bytes sent
1489 * @ppos: where to start
1490 *
1491 * Returns number of bytes written or error code, as appropriate
1492 */
1493static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
1494 size_t count, loff_t *ppos)
1495{
1496 struct smack_known *skp;
1497 char temp[80];
1498 int i;
1499
Casey Schaufler1880eff2012-06-05 15:28:30 -07001500 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001501 return -EPERM;
1502
1503 if (count >= sizeof(temp) || count == 0)
1504 return -EINVAL;
1505
1506 if (copy_from_user(temp, buf, count) != 0)
1507 return -EFAULT;
1508
1509 temp[count] = '\0';
1510
1511 if (sscanf(temp, "%d", &i) != 1)
1512 return -EINVAL;
1513
1514 /*
1515 * Don't do anything if the value hasn't actually changed.
1516 * If it is changing reset the level on entries that were
1517 * set up to be mapped when they were created.
1518 */
1519 if (smack_cipso_mapped != i) {
1520 mutex_lock(&smack_known_lock);
1521 list_for_each_entry_rcu(skp, &smack_known_list, list)
1522 if (skp->smk_netlabel.attr.mls.lvl ==
1523 smack_cipso_mapped)
1524 skp->smk_netlabel.attr.mls.lvl = i;
1525 smack_cipso_mapped = i;
1526 mutex_unlock(&smack_known_lock);
1527 }
1528
1529 return count;
1530}
1531
1532static const struct file_operations smk_mapped_ops = {
1533 .read = smk_read_mapped,
1534 .write = smk_write_mapped,
1535 .llseek = default_llseek,
1536};
1537
1538/**
Casey Schauflere114e472008-02-04 22:29:50 -08001539 * smk_read_ambient - read() for /smack/ambient
1540 * @filp: file pointer, not actually used
1541 * @buf: where to put the result
1542 * @cn: maximum to send along
1543 * @ppos: where to start
1544 *
1545 * Returns number of bytes read or error code, as appropriate
1546 */
1547static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
1548 size_t cn, loff_t *ppos)
1549{
1550 ssize_t rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001551 int asize;
1552
1553 if (*ppos != 0)
1554 return 0;
1555 /*
1556 * Being careful to avoid a problem in the case where
1557 * smack_net_ambient gets changed in midstream.
Casey Schauflere114e472008-02-04 22:29:50 -08001558 */
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001559 mutex_lock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001560
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001561 asize = strlen(smack_net_ambient->smk_known) + 1;
Casey Schauflere114e472008-02-04 22:29:50 -08001562
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001563 if (cn >= asize)
1564 rc = simple_read_from_buffer(buf, cn, ppos,
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001565 smack_net_ambient->smk_known,
1566 asize);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001567 else
1568 rc = -EINVAL;
1569
1570 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001571
1572 return rc;
1573}
1574
1575/**
1576 * smk_write_ambient - write() for /smack/ambient
Randy Dunlap251a2a92009-02-18 11:42:33 -08001577 * @file: file pointer, not actually used
Casey Schauflere114e472008-02-04 22:29:50 -08001578 * @buf: where to get the data from
1579 * @count: bytes sent
1580 * @ppos: where to start
1581 *
1582 * Returns number of bytes written or error code, as appropriate
1583 */
1584static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
1585 size_t count, loff_t *ppos)
1586{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001587 struct smack_known *skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001588 char *oldambient;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001589 char *data;
1590 int rc = count;
Casey Schauflere114e472008-02-04 22:29:50 -08001591
Casey Schaufler1880eff2012-06-05 15:28:30 -07001592 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflere114e472008-02-04 22:29:50 -08001593 return -EPERM;
1594
Casey Schauflerf7112e62012-05-06 15:22:02 -07001595 data = kzalloc(count + 1, GFP_KERNEL);
1596 if (data == NULL)
1597 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001598
Casey Schauflerf7112e62012-05-06 15:22:02 -07001599 if (copy_from_user(data, buf, count) != 0) {
1600 rc = -EFAULT;
1601 goto out;
1602 }
Casey Schauflere114e472008-02-04 22:29:50 -08001603
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001604 skp = smk_import_entry(data, count);
1605 if (skp == NULL) {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001606 rc = -EINVAL;
1607 goto out;
1608 }
Casey Schauflere114e472008-02-04 22:29:50 -08001609
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001610 mutex_lock(&smack_ambient_lock);
1611
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001612 oldambient = smack_net_ambient->smk_known;
1613 smack_net_ambient = skp;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001614 smk_unlbl_ambient(oldambient);
1615
1616 mutex_unlock(&smack_ambient_lock);
Casey Schauflere114e472008-02-04 22:29:50 -08001617
Casey Schauflerf7112e62012-05-06 15:22:02 -07001618out:
1619 kfree(data);
1620 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001621}
1622
1623static const struct file_operations smk_ambient_ops = {
1624 .read = smk_read_ambient,
1625 .write = smk_write_ambient,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001626 .llseek = default_llseek,
Casey Schauflere114e472008-02-04 22:29:50 -08001627};
1628
Casey Schaufler15446232008-07-30 15:37:11 -07001629/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08001630 * smk_read_onlycap - read() for smackfs/onlycap
Casey Schaufler15446232008-07-30 15:37:11 -07001631 * @filp: file pointer, not actually used
1632 * @buf: where to put the result
1633 * @cn: maximum to send along
1634 * @ppos: where to start
1635 *
1636 * Returns number of bytes read or error code, as appropriate
1637 */
1638static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
1639 size_t cn, loff_t *ppos)
1640{
1641 char *smack = "";
1642 ssize_t rc = -EINVAL;
1643 int asize;
1644
1645 if (*ppos != 0)
1646 return 0;
1647
1648 if (smack_onlycap != NULL)
Casey Schaufler00f84f32013-12-23 11:07:10 -08001649 smack = smack_onlycap->smk_known;
Casey Schaufler15446232008-07-30 15:37:11 -07001650
1651 asize = strlen(smack) + 1;
1652
1653 if (cn >= asize)
1654 rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
1655
1656 return rc;
1657}
1658
1659/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08001660 * smk_write_onlycap - write() for smackfs/onlycap
Randy Dunlap251a2a92009-02-18 11:42:33 -08001661 * @file: file pointer, not actually used
Casey Schaufler15446232008-07-30 15:37:11 -07001662 * @buf: where to get the data from
1663 * @count: bytes sent
1664 * @ppos: where to start
1665 *
1666 * Returns number of bytes written or error code, as appropriate
1667 */
1668static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
1669 size_t count, loff_t *ppos)
1670{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001671 char *data;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001672 struct smack_known *skp = smk_of_task(current->cred->security);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001673 int rc = count;
Casey Schaufler15446232008-07-30 15:37:11 -07001674
Casey Schaufler1880eff2012-06-05 15:28:30 -07001675 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schaufler15446232008-07-30 15:37:11 -07001676 return -EPERM;
1677
1678 /*
1679 * This can be done using smk_access() but is done
1680 * explicitly for clarity. The smk_access() implementation
1681 * would use smk_access(smack_onlycap, MAY_WRITE)
1682 */
Casey Schaufler00f84f32013-12-23 11:07:10 -08001683 if (smack_onlycap != NULL && smack_onlycap != skp)
Casey Schaufler15446232008-07-30 15:37:11 -07001684 return -EPERM;
1685
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001686 data = kzalloc(count + 1, GFP_KERNEL);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001687 if (data == NULL)
1688 return -ENOMEM;
Casey Schaufler15446232008-07-30 15:37:11 -07001689
1690 /*
1691 * Should the null string be passed in unset the onlycap value.
1692 * This seems like something to be careful with as usually
1693 * smk_import only expects to return NULL for errors. It
1694 * is usually the case that a nullstring or "\n" would be
1695 * bad to pass to smk_import but in fact this is useful here.
Casey Schauflerf7112e62012-05-06 15:22:02 -07001696 *
1697 * smk_import will also reject a label beginning with '-',
1698 * so "-usecapabilities" will also work.
Casey Schaufler15446232008-07-30 15:37:11 -07001699 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07001700 if (copy_from_user(data, buf, count) != 0)
1701 rc = -EFAULT;
1702 else
Casey Schaufler00f84f32013-12-23 11:07:10 -08001703 smack_onlycap = smk_import_entry(data, count);
Casey Schaufler15446232008-07-30 15:37:11 -07001704
Casey Schauflerf7112e62012-05-06 15:22:02 -07001705 kfree(data);
1706 return rc;
Casey Schaufler15446232008-07-30 15:37:11 -07001707}
1708
1709static const struct file_operations smk_onlycap_ops = {
1710 .read = smk_read_onlycap,
1711 .write = smk_write_onlycap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001712 .llseek = default_llseek,
Casey Schaufler15446232008-07-30 15:37:11 -07001713};
1714
Casey Schauflere114e472008-02-04 22:29:50 -08001715/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001716 * smk_read_logging - read() for /smack/logging
1717 * @filp: file pointer, not actually used
1718 * @buf: where to put the result
1719 * @cn: maximum to send along
1720 * @ppos: where to start
1721 *
1722 * Returns number of bytes read or error code, as appropriate
1723 */
1724static ssize_t smk_read_logging(struct file *filp, char __user *buf,
1725 size_t count, loff_t *ppos)
1726{
1727 char temp[32];
1728 ssize_t rc;
1729
1730 if (*ppos != 0)
1731 return 0;
1732
1733 sprintf(temp, "%d\n", log_policy);
1734 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
1735 return rc;
1736}
1737
1738/**
1739 * smk_write_logging - write() for /smack/logging
1740 * @file: file pointer, not actually used
1741 * @buf: where to get the data from
1742 * @count: bytes sent
1743 * @ppos: where to start
1744 *
1745 * Returns number of bytes written or error code, as appropriate
1746 */
1747static ssize_t smk_write_logging(struct file *file, const char __user *buf,
1748 size_t count, loff_t *ppos)
1749{
1750 char temp[32];
1751 int i;
1752
Casey Schaufler1880eff2012-06-05 15:28:30 -07001753 if (!smack_privileged(CAP_MAC_ADMIN))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001754 return -EPERM;
1755
1756 if (count >= sizeof(temp) || count == 0)
1757 return -EINVAL;
1758
1759 if (copy_from_user(temp, buf, count) != 0)
1760 return -EFAULT;
1761
1762 temp[count] = '\0';
1763
1764 if (sscanf(temp, "%d", &i) != 1)
1765 return -EINVAL;
1766 if (i < 0 || i > 3)
1767 return -EINVAL;
1768 log_policy = i;
1769 return count;
1770}
1771
1772
1773
1774static const struct file_operations smk_logging_ops = {
1775 .read = smk_read_logging,
1776 .write = smk_write_logging,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001777 .llseek = default_llseek,
Etienne Bassetecfcc532009-04-08 20:40:06 +02001778};
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001779
1780/*
1781 * Seq_file read operations for /smack/load-self
1782 */
1783
1784static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
1785{
1786 struct task_smack *tsp = current_security();
1787
Casey Schaufler40809562011-11-10 15:02:22 -08001788 return smk_seq_start(s, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001789}
1790
1791static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
1792{
1793 struct task_smack *tsp = current_security();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001794
Casey Schaufler40809562011-11-10 15:02:22 -08001795 return smk_seq_next(s, v, pos, &tsp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001796}
1797
1798static int load_self_seq_show(struct seq_file *s, void *v)
1799{
1800 struct list_head *list = v;
1801 struct smack_rule *srp =
1802 list_entry(list, struct smack_rule, list);
1803
Casey Schauflerf7112e62012-05-06 15:22:02 -07001804 smk_rule_show(s, srp, SMK_LABELLEN);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001805
1806 return 0;
1807}
1808
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001809static const struct seq_operations load_self_seq_ops = {
1810 .start = load_self_seq_start,
1811 .next = load_self_seq_next,
1812 .show = load_self_seq_show,
Casey Schaufler40809562011-11-10 15:02:22 -08001813 .stop = smk_seq_stop,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001814};
1815
1816
1817/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001818 * smk_open_load_self - open() for /smack/load-self2
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001819 * @inode: inode structure representing file
1820 * @file: "load" file pointer
1821 *
1822 * For reading, use load_seq_* seq_file reading operations.
1823 */
1824static int smk_open_load_self(struct inode *inode, struct file *file)
1825{
1826 return seq_open(file, &load_self_seq_ops);
1827}
1828
1829/**
1830 * smk_write_load_self - write() for /smack/load-self
1831 * @file: file pointer, not actually used
1832 * @buf: where to get the data from
1833 * @count: bytes sent
1834 * @ppos: where to start - must be 0
1835 *
1836 */
1837static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
1838 size_t count, loff_t *ppos)
1839{
1840 struct task_smack *tsp = current_security();
1841
Casey Schauflerf7112e62012-05-06 15:22:02 -07001842 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
1843 &tsp->smk_rules_lock, SMK_FIXED24_FMT);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001844}
1845
1846static const struct file_operations smk_load_self_ops = {
1847 .open = smk_open_load_self,
1848 .read = seq_read,
1849 .llseek = seq_lseek,
1850 .write = smk_write_load_self,
1851 .release = seq_release,
1852};
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001853
1854/**
Casey Schauflerf7112e62012-05-06 15:22:02 -07001855 * smk_user_access - handle access check transaction
1856 * @file: file pointer
1857 * @buf: data from user space
1858 * @count: bytes sent
1859 * @ppos: where to start - must be 0
1860 */
1861static ssize_t smk_user_access(struct file *file, const char __user *buf,
1862 size_t count, loff_t *ppos, int format)
1863{
Rafal Krypae05b6f92013-01-10 19:42:00 +01001864 struct smack_parsed_rule rule;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001865 char *data;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001866 int res;
1867
1868 data = simple_transaction_get(file, buf, count);
1869 if (IS_ERR(data))
1870 return PTR_ERR(data);
1871
1872 if (format == SMK_FIXED24_FMT) {
1873 if (count < SMK_LOADLEN)
1874 return -EINVAL;
1875 res = smk_parse_rule(data, &rule, 0);
1876 } else {
1877 /*
Rafal Krypa10289b02013-08-09 11:47:07 +02001878 * simple_transaction_get() returns null-terminated data
Casey Schauflerf7112e62012-05-06 15:22:02 -07001879 */
Rafal Krypa10289b02013-08-09 11:47:07 +02001880 res = smk_parse_long_rule(data, &rule, 0, 3);
Casey Schauflerf7112e62012-05-06 15:22:02 -07001881 }
1882
Jarkko Sakkinen398ce072013-11-28 19:16:46 +02001883 if (res >= 0)
1884 res = smk_access(rule.smk_subject, rule.smk_object,
1885 rule.smk_access1, NULL);
1886 else if (res != -ENOENT)
Casey Schauflerf7112e62012-05-06 15:22:02 -07001887 return -EINVAL;
1888
Casey Schauflerd166c802014-08-27 14:51:27 -07001889 /*
1890 * smk_access() can return a value > 0 in the "bringup" case.
1891 */
1892 data[0] = res >= 0 ? '1' : '0';
Casey Schauflerf7112e62012-05-06 15:22:02 -07001893 data[1] = '\0';
1894
1895 simple_transaction_set(file, 2);
1896
1897 if (format == SMK_FIXED24_FMT)
1898 return SMK_LOADLEN;
1899 return count;
1900}
1901
1902/**
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001903 * smk_write_access - handle access check transaction
1904 * @file: file pointer
1905 * @buf: data from user space
1906 * @count: bytes sent
1907 * @ppos: where to start - must be 0
1908 */
1909static ssize_t smk_write_access(struct file *file, const char __user *buf,
1910 size_t count, loff_t *ppos)
1911{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001912 return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03001913}
1914
1915static const struct file_operations smk_access_ops = {
1916 .write = smk_write_access,
1917 .read = simple_transaction_read,
1918 .release = simple_transaction_release,
1919 .llseek = generic_file_llseek,
1920};
1921
Casey Schauflerf7112e62012-05-06 15:22:02 -07001922
1923/*
1924 * Seq_file read operations for /smack/load2
1925 */
1926
1927static int load2_seq_show(struct seq_file *s, void *v)
1928{
1929 struct list_head *list = v;
1930 struct smack_master_list *smlp =
1931 list_entry(list, struct smack_master_list, list);
1932
1933 smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
1934
1935 return 0;
1936}
1937
1938static const struct seq_operations load2_seq_ops = {
1939 .start = load2_seq_start,
1940 .next = load2_seq_next,
1941 .show = load2_seq_show,
1942 .stop = smk_seq_stop,
1943};
1944
1945/**
1946 * smk_open_load2 - open() for /smack/load2
1947 * @inode: inode structure representing file
1948 * @file: "load2" file pointer
1949 *
1950 * For reading, use load2_seq_* seq_file reading operations.
1951 */
1952static int smk_open_load2(struct inode *inode, struct file *file)
1953{
1954 return seq_open(file, &load2_seq_ops);
1955}
1956
1957/**
1958 * smk_write_load2 - write() for /smack/load2
1959 * @file: file pointer, not actually used
1960 * @buf: where to get the data from
1961 * @count: bytes sent
1962 * @ppos: where to start - must be 0
1963 *
1964 */
1965static ssize_t smk_write_load2(struct file *file, const char __user *buf,
1966 size_t count, loff_t *ppos)
1967{
1968 /*
1969 * Must have privilege.
1970 */
Casey Schaufler1880eff2012-06-05 15:28:30 -07001971 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerf7112e62012-05-06 15:22:02 -07001972 return -EPERM;
1973
1974 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
1975 SMK_LONG_FMT);
1976}
1977
1978static const struct file_operations smk_load2_ops = {
1979 .open = smk_open_load2,
1980 .read = seq_read,
1981 .llseek = seq_lseek,
1982 .write = smk_write_load2,
1983 .release = seq_release,
1984};
1985
1986/*
1987 * Seq_file read operations for /smack/load-self2
1988 */
1989
1990static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
1991{
1992 struct task_smack *tsp = current_security();
1993
1994 return smk_seq_start(s, pos, &tsp->smk_rules);
1995}
1996
1997static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
1998{
1999 struct task_smack *tsp = current_security();
2000
2001 return smk_seq_next(s, v, pos, &tsp->smk_rules);
2002}
2003
2004static int load_self2_seq_show(struct seq_file *s, void *v)
2005{
2006 struct list_head *list = v;
2007 struct smack_rule *srp =
2008 list_entry(list, struct smack_rule, list);
2009
2010 smk_rule_show(s, srp, SMK_LONGLABEL);
2011
2012 return 0;
2013}
2014
2015static const struct seq_operations load_self2_seq_ops = {
2016 .start = load_self2_seq_start,
2017 .next = load_self2_seq_next,
2018 .show = load_self2_seq_show,
2019 .stop = smk_seq_stop,
2020};
2021
2022/**
2023 * smk_open_load_self2 - open() for /smack/load-self2
2024 * @inode: inode structure representing file
2025 * @file: "load" file pointer
2026 *
2027 * For reading, use load_seq_* seq_file reading operations.
2028 */
2029static int smk_open_load_self2(struct inode *inode, struct file *file)
2030{
2031 return seq_open(file, &load_self2_seq_ops);
2032}
2033
2034/**
2035 * smk_write_load_self2 - write() for /smack/load-self2
2036 * @file: file pointer, not actually used
2037 * @buf: where to get the data from
2038 * @count: bytes sent
2039 * @ppos: where to start - must be 0
2040 *
2041 */
2042static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
2043 size_t count, loff_t *ppos)
2044{
2045 struct task_smack *tsp = current_security();
2046
2047 return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
2048 &tsp->smk_rules_lock, SMK_LONG_FMT);
2049}
2050
2051static const struct file_operations smk_load_self2_ops = {
2052 .open = smk_open_load_self2,
2053 .read = seq_read,
2054 .llseek = seq_lseek,
2055 .write = smk_write_load_self2,
2056 .release = seq_release,
2057};
2058
2059/**
2060 * smk_write_access2 - handle access check transaction
2061 * @file: file pointer
2062 * @buf: data from user space
2063 * @count: bytes sent
2064 * @ppos: where to start - must be 0
2065 */
2066static ssize_t smk_write_access2(struct file *file, const char __user *buf,
2067 size_t count, loff_t *ppos)
2068{
2069 return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
2070}
2071
2072static const struct file_operations smk_access2_ops = {
2073 .write = smk_write_access2,
2074 .read = simple_transaction_read,
2075 .release = simple_transaction_release,
2076 .llseek = generic_file_llseek,
2077};
2078
Etienne Bassetecfcc532009-04-08 20:40:06 +02002079/**
Rafal Krypa449543b2012-07-11 17:49:30 +02002080 * smk_write_revoke_subj - write() for /smack/revoke-subject
2081 * @file: file pointer
2082 * @buf: data from user space
2083 * @count: bytes sent
2084 * @ppos: where to start - must be 0
2085 */
2086static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
2087 size_t count, loff_t *ppos)
2088{
2089 char *data = NULL;
2090 const char *cp = NULL;
2091 struct smack_known *skp;
2092 struct smack_rule *sp;
2093 struct list_head *rule_list;
2094 struct mutex *rule_lock;
2095 int rc = count;
2096
2097 if (*ppos != 0)
2098 return -EINVAL;
2099
2100 if (!smack_privileged(CAP_MAC_ADMIN))
2101 return -EPERM;
2102
2103 if (count == 0 || count > SMK_LONGLABEL)
2104 return -EINVAL;
2105
2106 data = kzalloc(count, GFP_KERNEL);
2107 if (data == NULL)
2108 return -ENOMEM;
2109
2110 if (copy_from_user(data, buf, count) != 0) {
2111 rc = -EFAULT;
2112 goto free_out;
2113 }
2114
2115 cp = smk_parse_smack(data, count);
2116 if (cp == NULL) {
2117 rc = -EINVAL;
2118 goto free_out;
2119 }
2120
2121 skp = smk_find_entry(cp);
Rafal Krypad15d9fa2012-11-27 16:28:11 +01002122 if (skp == NULL)
Rafal Krypa449543b2012-07-11 17:49:30 +02002123 goto free_out;
Rafal Krypa449543b2012-07-11 17:49:30 +02002124
2125 rule_list = &skp->smk_rules;
2126 rule_lock = &skp->smk_rules_lock;
2127
2128 mutex_lock(rule_lock);
2129
2130 list_for_each_entry_rcu(sp, rule_list, list)
2131 sp->smk_access = 0;
2132
2133 mutex_unlock(rule_lock);
2134
2135free_out:
2136 kfree(data);
2137 kfree(cp);
2138 return rc;
2139}
2140
2141static const struct file_operations smk_revoke_subj_ops = {
2142 .write = smk_write_revoke_subj,
2143 .read = simple_transaction_read,
2144 .release = simple_transaction_release,
2145 .llseek = generic_file_llseek,
2146};
2147
Casey Schauflere9307232012-11-01 18:14:32 -07002148static struct kset *smackfs_kset;
2149/**
2150 * smk_init_sysfs - initialize /sys/fs/smackfs
2151 *
2152 */
2153static int smk_init_sysfs(void)
2154{
2155 smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
2156 if (!smackfs_kset)
2157 return -ENOMEM;
2158 return 0;
2159}
2160
Rafal Krypa449543b2012-07-11 17:49:30 +02002161/**
Rafal Krypae05b6f92013-01-10 19:42:00 +01002162 * smk_write_change_rule - write() for /smack/change-rule
2163 * @file: file pointer
2164 * @buf: data from user space
2165 * @count: bytes sent
2166 * @ppos: where to start - must be 0
2167 */
2168static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
2169 size_t count, loff_t *ppos)
2170{
2171 /*
2172 * Must have privilege.
2173 */
Casey Schaufler4afde482013-12-19 13:23:26 -08002174 if (!smack_privileged(CAP_MAC_ADMIN))
Rafal Krypae05b6f92013-01-10 19:42:00 +01002175 return -EPERM;
2176
2177 return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
2178 SMK_CHANGE_FMT);
2179}
2180
2181static const struct file_operations smk_change_rule_ops = {
2182 .write = smk_write_change_rule,
2183 .read = simple_transaction_read,
2184 .release = simple_transaction_release,
2185 .llseek = generic_file_llseek,
2186};
2187
2188/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002189 * smk_read_syslog - read() for smackfs/syslog
2190 * @filp: file pointer, not actually used
2191 * @buf: where to put the result
2192 * @cn: maximum to send along
2193 * @ppos: where to start
2194 *
2195 * Returns number of bytes read or error code, as appropriate
2196 */
2197static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
2198 size_t cn, loff_t *ppos)
2199{
2200 struct smack_known *skp;
2201 ssize_t rc = -EINVAL;
2202 int asize;
2203
2204 if (*ppos != 0)
2205 return 0;
2206
2207 if (smack_syslog_label == NULL)
2208 skp = &smack_known_star;
2209 else
2210 skp = smack_syslog_label;
2211
2212 asize = strlen(skp->smk_known) + 1;
2213
2214 if (cn >= asize)
2215 rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
2216 asize);
2217
2218 return rc;
2219}
2220
2221/**
2222 * smk_write_syslog - write() for smackfs/syslog
2223 * @file: file pointer, not actually used
2224 * @buf: where to get the data from
2225 * @count: bytes sent
2226 * @ppos: where to start
2227 *
2228 * Returns number of bytes written or error code, as appropriate
2229 */
2230static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
2231 size_t count, loff_t *ppos)
2232{
2233 char *data;
2234 struct smack_known *skp;
2235 int rc = count;
2236
2237 if (!smack_privileged(CAP_MAC_ADMIN))
2238 return -EPERM;
2239
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04002240 data = kzalloc(count + 1, GFP_KERNEL);
Casey Schaufler00f84f32013-12-23 11:07:10 -08002241 if (data == NULL)
2242 return -ENOMEM;
2243
2244 if (copy_from_user(data, buf, count) != 0)
2245 rc = -EFAULT;
2246 else {
2247 skp = smk_import_entry(data, count);
2248 if (skp == NULL)
2249 rc = -EINVAL;
2250 else
2251 smack_syslog_label = smk_import_entry(data, count);
2252 }
2253
2254 kfree(data);
2255 return rc;
2256}
2257
2258static const struct file_operations smk_syslog_ops = {
2259 .read = smk_read_syslog,
2260 .write = smk_write_syslog,
2261 .llseek = default_llseek,
2262};
2263
2264
2265/**
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002266 * smk_read_ptrace - read() for /smack/ptrace
2267 * @filp: file pointer, not actually used
2268 * @buf: where to put the result
2269 * @count: maximum to send along
2270 * @ppos: where to start
2271 *
2272 * Returns number of bytes read or error code, as appropriate
2273 */
2274static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
2275 size_t count, loff_t *ppos)
2276{
2277 char temp[32];
2278 ssize_t rc;
2279
2280 if (*ppos != 0)
2281 return 0;
2282
2283 sprintf(temp, "%d\n", smack_ptrace_rule);
2284 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
2285 return rc;
2286}
2287
2288/**
2289 * smk_write_ptrace - write() for /smack/ptrace
2290 * @file: file pointer
2291 * @buf: data from user space
2292 * @count: bytes sent
2293 * @ppos: where to start - must be 0
2294 */
2295static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
2296 size_t count, loff_t *ppos)
2297{
2298 char temp[32];
2299 int i;
2300
2301 if (!smack_privileged(CAP_MAC_ADMIN))
2302 return -EPERM;
2303
2304 if (*ppos != 0 || count >= sizeof(temp) || count == 0)
2305 return -EINVAL;
2306
2307 if (copy_from_user(temp, buf, count) != 0)
2308 return -EFAULT;
2309
2310 temp[count] = '\0';
2311
2312 if (sscanf(temp, "%d", &i) != 1)
2313 return -EINVAL;
2314 if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
2315 return -EINVAL;
2316 smack_ptrace_rule = i;
2317
2318 return count;
2319}
2320
2321static const struct file_operations smk_ptrace_ops = {
2322 .write = smk_write_ptrace,
2323 .read = smk_read_ptrace,
2324 .llseek = default_llseek,
2325};
2326
2327/**
Casey Schaufler00f84f32013-12-23 11:07:10 -08002328 * smk_fill_super - fill the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002329 * @sb: the empty superblock
2330 * @data: unused
2331 * @silent: unused
2332 *
Casey Schaufler00f84f32013-12-23 11:07:10 -08002333 * Fill in the well known entries for the smack filesystem
Casey Schauflere114e472008-02-04 22:29:50 -08002334 *
2335 * Returns 0 on success, an error code on failure
2336 */
2337static int smk_fill_super(struct super_block *sb, void *data, int silent)
2338{
2339 int rc;
2340 struct inode *root_inode;
2341
2342 static struct tree_descr smack_files[] = {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002343 [SMK_LOAD] = {
2344 "load", &smk_load_ops, S_IRUGO|S_IWUSR},
2345 [SMK_CIPSO] = {
2346 "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
2347 [SMK_DOI] = {
2348 "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
2349 [SMK_DIRECT] = {
2350 "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
2351 [SMK_AMBIENT] = {
2352 "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
2353 [SMK_NETLBLADDR] = {
2354 "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
2355 [SMK_ONLYCAP] = {
2356 "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
2357 [SMK_LOGGING] = {
2358 "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
2359 [SMK_LOAD_SELF] = {
2360 "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
Jarkko Sakkinen828716c2011-09-08 10:12:01 +03002361 [SMK_ACCESSES] = {
Jarkko Sakkinen0e94ae12011-10-18 21:21:36 +03002362 "access", &smk_access_ops, S_IRUGO|S_IWUGO},
Casey Schauflerf7112e62012-05-06 15:22:02 -07002363 [SMK_MAPPED] = {
2364 "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
2365 [SMK_LOAD2] = {
2366 "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
2367 [SMK_LOAD_SELF2] = {
2368 "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
2369 [SMK_ACCESS2] = {
2370 "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
2371 [SMK_CIPSO2] = {
2372 "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
Rafal Krypa449543b2012-07-11 17:49:30 +02002373 [SMK_REVOKE_SUBJ] = {
2374 "revoke-subject", &smk_revoke_subj_ops,
2375 S_IRUGO|S_IWUSR},
Rafal Krypae05b6f92013-01-10 19:42:00 +01002376 [SMK_CHANGE_RULE] = {
2377 "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
Casey Schaufler00f84f32013-12-23 11:07:10 -08002378 [SMK_SYSLOG] = {
2379 "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
Lukasz Pawelczyk66867812014-03-11 17:07:06 +01002380 [SMK_PTRACE] = {
2381 "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002382 /* last one */
2383 {""}
Casey Schauflere114e472008-02-04 22:29:50 -08002384 };
2385
2386 rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
2387 if (rc != 0) {
2388 printk(KERN_ERR "%s failed %d while creating inodes\n",
2389 __func__, rc);
2390 return rc;
2391 }
2392
2393 root_inode = sb->s_root->d_inode;
Casey Schauflere114e472008-02-04 22:29:50 -08002394
2395 return 0;
2396}
2397
2398/**
Al Virofc14f2f2010-07-25 01:48:30 +04002399 * smk_mount - get the smackfs superblock
Casey Schauflere114e472008-02-04 22:29:50 -08002400 * @fs_type: passed along without comment
2401 * @flags: passed along without comment
2402 * @dev_name: passed along without comment
2403 * @data: passed along without comment
Casey Schauflere114e472008-02-04 22:29:50 -08002404 *
2405 * Just passes everything along.
2406 *
2407 * Returns what the lower level code does.
2408 */
Al Virofc14f2f2010-07-25 01:48:30 +04002409static struct dentry *smk_mount(struct file_system_type *fs_type,
2410 int flags, const char *dev_name, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -08002411{
Al Virofc14f2f2010-07-25 01:48:30 +04002412 return mount_single(fs_type, flags, data, smk_fill_super);
Casey Schauflere114e472008-02-04 22:29:50 -08002413}
2414
2415static struct file_system_type smk_fs_type = {
2416 .name = "smackfs",
Al Virofc14f2f2010-07-25 01:48:30 +04002417 .mount = smk_mount,
Casey Schauflere114e472008-02-04 22:29:50 -08002418 .kill_sb = kill_litter_super,
2419};
2420
2421static struct vfsmount *smackfs_mount;
2422
Casey Schauflerf7112e62012-05-06 15:22:02 -07002423static int __init smk_preset_netlabel(struct smack_known *skp)
2424{
2425 skp->smk_netlabel.domain = skp->smk_known;
2426 skp->smk_netlabel.flags =
2427 NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
2428 return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
2429 &skp->smk_netlabel, strlen(skp->smk_known));
2430}
2431
Casey Schauflere114e472008-02-04 22:29:50 -08002432/**
2433 * init_smk_fs - get the smackfs superblock
2434 *
2435 * register the smackfs
2436 *
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002437 * Do not register smackfs if Smack wasn't enabled
2438 * on boot. We can not put this method normally under the
2439 * smack_init() code path since the security subsystem get
2440 * initialized before the vfs caches.
2441 *
2442 * Returns true if we were not chosen on boot or if
2443 * we were chosen and filesystem registration succeeded.
Casey Schauflere114e472008-02-04 22:29:50 -08002444 */
2445static int __init init_smk_fs(void)
2446{
2447 int err;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002448 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002449
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02002450 if (!security_module_enable(&smack_ops))
2451 return 0;
2452
Casey Schauflere9307232012-11-01 18:14:32 -07002453 err = smk_init_sysfs();
2454 if (err)
2455 printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
2456
Casey Schauflere114e472008-02-04 22:29:50 -08002457 err = register_filesystem(&smk_fs_type);
2458 if (!err) {
2459 smackfs_mount = kern_mount(&smk_fs_type);
2460 if (IS_ERR(smackfs_mount)) {
2461 printk(KERN_ERR "smackfs: could not mount!\n");
2462 err = PTR_ERR(smackfs_mount);
2463 smackfs_mount = NULL;
2464 }
2465 }
2466
Casey Schauflere114e472008-02-04 22:29:50 -08002467 smk_cipso_doi();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002468 smk_unlbl_ambient(NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08002469
Casey Schauflerf7112e62012-05-06 15:22:02 -07002470 rc = smk_preset_netlabel(&smack_known_floor);
2471 if (err == 0 && rc < 0)
2472 err = rc;
2473 rc = smk_preset_netlabel(&smack_known_hat);
2474 if (err == 0 && rc < 0)
2475 err = rc;
2476 rc = smk_preset_netlabel(&smack_known_huh);
2477 if (err == 0 && rc < 0)
2478 err = rc;
2479 rc = smk_preset_netlabel(&smack_known_invalid);
2480 if (err == 0 && rc < 0)
2481 err = rc;
2482 rc = smk_preset_netlabel(&smack_known_star);
2483 if (err == 0 && rc < 0)
2484 err = rc;
2485 rc = smk_preset_netlabel(&smack_known_web);
2486 if (err == 0 && rc < 0)
2487 err = rc;
2488
Casey Schauflere114e472008-02-04 22:29:50 -08002489 return err;
2490}
2491
2492__initcall(init_smk_fs);