Commit | Line | Data |
---|---|---|
e114e473 CS |
1 | /* |
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> | |
5a0e3ad6 | 23 | #include <linux/slab.h> |
6d3dc07c | 24 | #include <net/net_namespace.h> |
e114e473 CS |
25 | #include <net/cipso_ipv4.h> |
26 | #include <linux/seq_file.h> | |
27 | #include <linux/ctype.h> | |
4bc87e62 | 28 | #include <linux/audit.h> |
958d2c2f | 29 | #include <linux/magic.h> |
e114e473 CS |
30 | #include "smack.h" |
31 | ||
32 | /* | |
33 | * smackfs pseudo filesystem. | |
34 | */ | |
35 | ||
36 | enum 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 */ | |
6d3dc07c | 43 | SMK_NETLBLADDR = 8, /* single label hosts */ |
15446235 | 44 | SMK_ONLYCAP = 9, /* the only "capable" label */ |
ecfcc53f | 45 | SMK_LOGGING = 10, /* logging */ |
7898e1f8 | 46 | SMK_LOAD_SELF = 11, /* task specific rules */ |
828716c2 | 47 | SMK_ACCESSES = 12, /* access policy */ |
f7112e6c CS |
48 | 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 */ | |
449543b0 | 53 | SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */ |
e05b6f98 | 54 | SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */ |
00f84f3f | 55 | SMK_SYSLOG = 20, /* change syslog label) */ |
66867818 | 56 | SMK_PTRACE = 21, /* set ptrace rule */ |
bf4b2fee CS |
57 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
58 | SMK_UNCONFINED = 22, /* define an unconfined label */ | |
59 | #endif | |
e114e473 CS |
60 | }; |
61 | ||
62 | /* | |
63 | * List locks | |
64 | */ | |
e114e473 | 65 | static DEFINE_MUTEX(smack_cipso_lock); |
4bc87e62 | 66 | static DEFINE_MUTEX(smack_ambient_lock); |
6d3dc07c | 67 | static DEFINE_MUTEX(smk_netlbladdr_lock); |
e114e473 CS |
68 | |
69 | /* | |
70 | * This is the "ambient" label for network traffic. | |
71 | * If it isn't somehow marked, use this. | |
72 | * It can be reset via smackfs/ambient | |
73 | */ | |
2f823ff8 | 74 | struct smack_known *smack_net_ambient; |
e114e473 | 75 | |
e114e473 CS |
76 | /* |
77 | * This is the level in a CIPSO header that indicates a | |
78 | * smack label is contained directly in the category set. | |
79 | * It can be reset via smackfs/direct | |
80 | */ | |
81 | int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT; | |
82 | ||
f7112e6c CS |
83 | /* |
84 | * This is the level in a CIPSO header that indicates a | |
85 | * secid is contained directly in the category set. | |
86 | * It can be reset via smackfs/mapped | |
87 | */ | |
88 | int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT; | |
89 | ||
15446235 CS |
90 | /* |
91 | * Unless a process is running with this label even | |
92 | * having CAP_MAC_OVERRIDE isn't enough to grant | |
93 | * privilege to violate MAC policy. If no label is | |
94 | * designated (the NULL case) capabilities apply to | |
95 | * everyone. It is expected that the hat (^) label | |
96 | * will be used if any label is used. | |
97 | */ | |
00f84f3f CS |
98 | struct smack_known *smack_onlycap; |
99 | ||
bf4b2fee CS |
100 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
101 | /* | |
102 | * Allow one label to be unconfined. This is for | |
103 | * debugging and application bring-up purposes only. | |
104 | * It is bad and wrong, but everyone seems to expect | |
105 | * to have it. | |
106 | */ | |
107 | struct smack_known *smack_unconfined; | |
108 | #endif | |
109 | ||
00f84f3f CS |
110 | /* |
111 | * If this value is set restrict syslog use to the label specified. | |
112 | * It can be reset via smackfs/syslog | |
113 | */ | |
114 | struct smack_known *smack_syslog_label; | |
15446235 | 115 | |
66867818 LP |
116 | /* |
117 | * Ptrace current rule | |
118 | * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based) | |
119 | * SMACK_PTRACE_EXACT labels must match, but can be overriden with | |
120 | * CAP_SYS_PTRACE | |
121 | * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect | |
122 | */ | |
123 | int smack_ptrace_rule = SMACK_PTRACE_DEFAULT; | |
124 | ||
6d3dc07c CS |
125 | /* |
126 | * Certain IP addresses may be designated as single label hosts. | |
127 | * Packets are sent there unlabeled, but only from tasks that | |
128 | * can write to the specified label. | |
129 | */ | |
7198e2ee EB |
130 | |
131 | LIST_HEAD(smk_netlbladdr_list); | |
272cd7a8 CS |
132 | |
133 | /* | |
134 | * Rule lists are maintained for each label. | |
f7112e6c | 135 | * This master list is just for reading /smack/load and /smack/load2. |
272cd7a8 CS |
136 | */ |
137 | struct smack_master_list { | |
138 | struct list_head list; | |
139 | struct smack_rule *smk_rule; | |
140 | }; | |
141 | ||
7198e2ee | 142 | LIST_HEAD(smack_rule_list); |
6d3dc07c | 143 | |
e05b6f98 | 144 | struct smack_parsed_rule { |
2f823ff8 | 145 | struct smack_known *smk_subject; |
21c7eae2 | 146 | struct smack_known *smk_object; |
e05b6f98 RK |
147 | int smk_access1; |
148 | int smk_access2; | |
149 | }; | |
150 | ||
e114e473 | 151 | static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT; |
e114e473 | 152 | |
21c7eae2 LP |
153 | struct smack_known smack_cipso_option = { |
154 | .smk_known = SMACK_CIPSO_OPTION, | |
155 | .smk_secid = 0, | |
156 | }; | |
4303154e | 157 | |
e114e473 CS |
158 | /* |
159 | * Values for parsing cipso rules | |
160 | * SMK_DIGITLEN: Length of a digit field in a rule. | |
b500ce8d AD |
161 | * SMK_CIPSOMIN: Minimum possible cipso rule length. |
162 | * SMK_CIPSOMAX: Maximum possible cipso rule length. | |
e114e473 CS |
163 | */ |
164 | #define SMK_DIGITLEN 4 | |
b500ce8d AD |
165 | #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN) |
166 | #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN) | |
167 | ||
168 | /* | |
169 | * Values for parsing MAC rules | |
170 | * SMK_ACCESS: Maximum possible combination of access permissions | |
171 | * SMK_ACCESSLEN: Maximum length for a rule access field | |
172 | * SMK_LOADLEN: Smack rule length | |
173 | */ | |
5c6d1125 | 174 | #define SMK_OACCESS "rwxa" |
c0ab6e56 | 175 | #define SMK_ACCESS "rwxatl" |
5c6d1125 JS |
176 | #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1) |
177 | #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1) | |
178 | #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN) | |
179 | #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN) | |
b500ce8d | 180 | |
f7112e6c CS |
181 | /* |
182 | * Stricly for CIPSO level manipulation. | |
183 | * Set the category bit number in a smack label sized buffer. | |
184 | */ | |
185 | static inline void smack_catset_bit(unsigned int cat, char *catsetp) | |
186 | { | |
187 | if (cat == 0 || cat > (SMK_CIPSOLEN * 8)) | |
188 | return; | |
189 | ||
190 | catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8); | |
191 | } | |
192 | ||
6d3dc07c CS |
193 | /** |
194 | * smk_netlabel_audit_set - fill a netlbl_audit struct | |
195 | * @nap: structure to fill | |
196 | */ | |
197 | static void smk_netlabel_audit_set(struct netlbl_audit *nap) | |
198 | { | |
2f823ff8 CS |
199 | struct smack_known *skp = smk_of_current(); |
200 | ||
6d3dc07c CS |
201 | nap->loginuid = audit_get_loginuid(current); |
202 | nap->sessionid = audit_get_sessionid(current); | |
2f823ff8 | 203 | nap->secid = skp->smk_secid; |
6d3dc07c CS |
204 | } |
205 | ||
206 | /* | |
f7112e6c | 207 | * Value for parsing single label host rules |
6d3dc07c | 208 | * "1.2.3.4 X" |
6d3dc07c CS |
209 | */ |
210 | #define SMK_NETLBLADDRMIN 9 | |
e114e473 | 211 | |
e114e473 | 212 | /** |
e05b6f98 RK |
213 | * smk_set_access - add a rule to the rule list or replace an old rule |
214 | * @srp: the rule to add or replace | |
7898e1f8 CS |
215 | * @rule_list: the list of rules |
216 | * @rule_lock: the rule list lock | |
e05b6f98 | 217 | * @global: if non-zero, indicates a global rule |
e114e473 CS |
218 | * |
219 | * Looks through the current subject/object/access list for | |
220 | * the subject/object pair and replaces the access that was | |
221 | * there. If the pair isn't found add it with the specified | |
222 | * access. | |
81ea714b SL |
223 | * |
224 | * Returns 0 if nothing goes wrong or -ENOMEM if it fails | |
225 | * during the allocation of the new pair to add. | |
e114e473 | 226 | */ |
e05b6f98 RK |
227 | static int smk_set_access(struct smack_parsed_rule *srp, |
228 | struct list_head *rule_list, | |
229 | struct mutex *rule_lock, int global) | |
e114e473 | 230 | { |
7198e2ee | 231 | struct smack_rule *sp; |
e05b6f98 | 232 | struct smack_master_list *smlp; |
7898e1f8 | 233 | int found = 0; |
e05b6f98 | 234 | int rc = 0; |
e114e473 | 235 | |
7898e1f8 CS |
236 | mutex_lock(rule_lock); |
237 | ||
272cd7a8 CS |
238 | /* |
239 | * Because the object label is less likely to match | |
240 | * than the subject label check it first | |
241 | */ | |
7898e1f8 | 242 | list_for_each_entry_rcu(sp, rule_list, list) { |
272cd7a8 CS |
243 | if (sp->smk_object == srp->smk_object && |
244 | sp->smk_subject == srp->smk_subject) { | |
7198e2ee | 245 | found = 1; |
e05b6f98 RK |
246 | sp->smk_access |= srp->smk_access1; |
247 | sp->smk_access &= ~srp->smk_access2; | |
e114e473 CS |
248 | break; |
249 | } | |
e114e473 CS |
250 | } |
251 | ||
e05b6f98 RK |
252 | if (found == 0) { |
253 | sp = kzalloc(sizeof(*sp), GFP_KERNEL); | |
254 | if (sp == NULL) { | |
255 | rc = -ENOMEM; | |
256 | goto out; | |
257 | } | |
258 | ||
259 | sp->smk_subject = srp->smk_subject; | |
260 | sp->smk_object = srp->smk_object; | |
261 | sp->smk_access = srp->smk_access1 & ~srp->smk_access2; | |
262 | ||
263 | list_add_rcu(&sp->list, rule_list); | |
264 | /* | |
265 | * If this is a global as opposed to self and a new rule | |
266 | * it needs to get added for reporting. | |
267 | */ | |
268 | if (global) { | |
269 | smlp = kzalloc(sizeof(*smlp), GFP_KERNEL); | |
270 | if (smlp != NULL) { | |
271 | smlp->smk_rule = sp; | |
272 | list_add_rcu(&smlp->list, &smack_rule_list); | |
273 | } else | |
274 | rc = -ENOMEM; | |
275 | } | |
276 | } | |
277 | ||
278 | out: | |
7898e1f8 | 279 | mutex_unlock(rule_lock); |
e05b6f98 RK |
280 | return rc; |
281 | } | |
282 | ||
283 | /** | |
284 | * smk_perm_from_str - parse smack accesses from a text string | |
285 | * @string: a text string that contains a Smack accesses code | |
286 | * | |
287 | * Returns an integer with respective bits set for specified accesses. | |
288 | */ | |
289 | static int smk_perm_from_str(const char *string) | |
290 | { | |
291 | int perm = 0; | |
292 | const char *cp; | |
e114e473 | 293 | |
e05b6f98 RK |
294 | for (cp = string; ; cp++) |
295 | switch (*cp) { | |
296 | case '-': | |
297 | break; | |
298 | case 'r': | |
299 | case 'R': | |
300 | perm |= MAY_READ; | |
301 | break; | |
302 | case 'w': | |
303 | case 'W': | |
304 | perm |= MAY_WRITE; | |
305 | break; | |
306 | case 'x': | |
307 | case 'X': | |
308 | perm |= MAY_EXEC; | |
309 | break; | |
310 | case 'a': | |
311 | case 'A': | |
312 | perm |= MAY_APPEND; | |
313 | break; | |
314 | case 't': | |
315 | case 'T': | |
316 | perm |= MAY_TRANSMUTE; | |
317 | break; | |
c0ab6e56 CS |
318 | case 'l': |
319 | case 'L': | |
320 | perm |= MAY_LOCK; | |
321 | break; | |
d166c802 CS |
322 | case 'b': |
323 | case 'B': | |
324 | perm |= MAY_BRINGUP; | |
325 | break; | |
e05b6f98 RK |
326 | default: |
327 | return perm; | |
328 | } | |
e114e473 CS |
329 | } |
330 | ||
331 | /** | |
f7112e6c CS |
332 | * smk_fill_rule - Fill Smack rule from strings |
333 | * @subject: subject label string | |
334 | * @object: object label string | |
e05b6f98 RK |
335 | * @access1: access string |
336 | * @access2: string with permissions to be removed | |
0e94ae17 JS |
337 | * @rule: Smack rule |
338 | * @import: if non-zero, import labels | |
3518721a | 339 | * @len: label length limit |
f7112e6c | 340 | * |
398ce073 JS |
341 | * Returns 0 on success, -EINVAL on failure and -ENOENT when either subject |
342 | * or object is missing. | |
e114e473 | 343 | */ |
f7112e6c | 344 | static int smk_fill_rule(const char *subject, const char *object, |
e05b6f98 RK |
345 | const char *access1, const char *access2, |
346 | struct smack_parsed_rule *rule, int import, | |
347 | int len) | |
e114e473 | 348 | { |
f7112e6c | 349 | const char *cp; |
0e94ae17 | 350 | struct smack_known *skp; |
e114e473 | 351 | |
0e94ae17 | 352 | if (import) { |
2f823ff8 | 353 | rule->smk_subject = smk_import_entry(subject, len); |
0e94ae17 | 354 | if (rule->smk_subject == NULL) |
398ce073 | 355 | return -EINVAL; |
0e94ae17 | 356 | |
21c7eae2 | 357 | rule->smk_object = smk_import_entry(object, len); |
0e94ae17 | 358 | if (rule->smk_object == NULL) |
398ce073 | 359 | return -EINVAL; |
0e94ae17 | 360 | } else { |
3518721a | 361 | cp = smk_parse_smack(subject, len); |
f7112e6c | 362 | if (cp == NULL) |
398ce073 | 363 | return -EINVAL; |
f7112e6c CS |
364 | skp = smk_find_entry(cp); |
365 | kfree(cp); | |
0e94ae17 | 366 | if (skp == NULL) |
398ce073 | 367 | return -ENOENT; |
2f823ff8 | 368 | rule->smk_subject = skp; |
0e94ae17 | 369 | |
3518721a | 370 | cp = smk_parse_smack(object, len); |
f7112e6c | 371 | if (cp == NULL) |
398ce073 | 372 | return -EINVAL; |
f7112e6c CS |
373 | skp = smk_find_entry(cp); |
374 | kfree(cp); | |
0e94ae17 | 375 | if (skp == NULL) |
398ce073 | 376 | return -ENOENT; |
21c7eae2 | 377 | rule->smk_object = skp; |
0e94ae17 | 378 | } |
7198e2ee | 379 | |
e05b6f98 RK |
380 | rule->smk_access1 = smk_perm_from_str(access1); |
381 | if (access2) | |
382 | rule->smk_access2 = smk_perm_from_str(access2); | |
383 | else | |
384 | rule->smk_access2 = ~rule->smk_access1; | |
e114e473 | 385 | |
3518721a | 386 | return 0; |
f7112e6c | 387 | } |
e114e473 | 388 | |
f7112e6c CS |
389 | /** |
390 | * smk_parse_rule - parse Smack rule from load string | |
391 | * @data: string to be parsed whose size is SMK_LOADLEN | |
392 | * @rule: Smack rule | |
393 | * @import: if non-zero, import labels | |
394 | * | |
395 | * Returns 0 on success, -1 on errors. | |
396 | */ | |
e05b6f98 RK |
397 | static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule, |
398 | int import) | |
f7112e6c CS |
399 | { |
400 | int rc; | |
e114e473 | 401 | |
f7112e6c | 402 | rc = smk_fill_rule(data, data + SMK_LABELLEN, |
e05b6f98 RK |
403 | data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule, |
404 | import, SMK_LABELLEN); | |
f7112e6c CS |
405 | return rc; |
406 | } | |
e114e473 | 407 | |
f7112e6c CS |
408 | /** |
409 | * smk_parse_long_rule - parse Smack rule from rule string | |
410 | * @data: string to be parsed, null terminated | |
e05b6f98 | 411 | * @rule: Will be filled with Smack parsed rule |
f7112e6c | 412 | * @import: if non-zero, import labels |
10289b0f | 413 | * @tokens: numer of substrings expected in data |
f7112e6c | 414 | * |
10289b0f | 415 | * Returns number of processed bytes on success, -1 on failure. |
f7112e6c | 416 | */ |
10289b0f RK |
417 | static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule, |
418 | int import, int tokens) | |
f7112e6c | 419 | { |
10289b0f RK |
420 | ssize_t cnt = 0; |
421 | char *tok[4]; | |
398ce073 | 422 | int rc; |
10289b0f RK |
423 | int i; |
424 | ||
425 | /* | |
426 | * Parsing the rule in-place, filling all white-spaces with '\0' | |
427 | */ | |
428 | for (i = 0; i < tokens; ++i) { | |
429 | while (isspace(data[cnt])) | |
430 | data[cnt++] = '\0'; | |
431 | ||
432 | if (data[cnt] == '\0') | |
433 | /* Unexpected end of data */ | |
434 | return -1; | |
435 | ||
436 | tok[i] = data + cnt; | |
437 | ||
438 | while (data[cnt] && !isspace(data[cnt])) | |
439 | ++cnt; | |
e05b6f98 | 440 | } |
10289b0f RK |
441 | while (isspace(data[cnt])) |
442 | data[cnt++] = '\0'; | |
f7112e6c | 443 | |
10289b0f RK |
444 | while (i < 4) |
445 | tok[i++] = NULL; | |
446 | ||
398ce073 JS |
447 | rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0); |
448 | return rc == 0 ? cnt : rc; | |
828716c2 JS |
449 | } |
450 | ||
f7112e6c CS |
451 | #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */ |
452 | #define SMK_LONG_FMT 1 /* Variable long label format */ | |
e05b6f98 | 453 | #define SMK_CHANGE_FMT 2 /* Rule modification format */ |
828716c2 | 454 | /** |
f7112e6c | 455 | * smk_write_rules_list - write() for any /smack rule file |
828716c2 JS |
456 | * @file: file pointer, not actually used |
457 | * @buf: where to get the data from | |
458 | * @count: bytes sent | |
459 | * @ppos: where to start - must be 0 | |
460 | * @rule_list: the list of rules to write to | |
461 | * @rule_lock: lock for the rule list | |
e05b6f98 | 462 | * @format: /smack/load or /smack/load2 or /smack/change-rule format. |
828716c2 JS |
463 | * |
464 | * Get one smack access rule from above. | |
f7112e6c CS |
465 | * The format for SMK_LONG_FMT is: |
466 | * "subject<whitespace>object<whitespace>access[<whitespace>...]" | |
467 | * The format for SMK_FIXED24_FMT is exactly: | |
468 | * "subject object rwxat" | |
e05b6f98 RK |
469 | * The format for SMK_CHANGE_FMT is: |
470 | * "subject<whitespace>object<whitespace> | |
471 | * acc_enable<whitespace>acc_disable[<whitespace>...]" | |
828716c2 | 472 | */ |
f7112e6c CS |
473 | static ssize_t smk_write_rules_list(struct file *file, const char __user *buf, |
474 | size_t count, loff_t *ppos, | |
475 | struct list_head *rule_list, | |
476 | struct mutex *rule_lock, int format) | |
828716c2 | 477 | { |
470043ba | 478 | struct smack_parsed_rule rule; |
828716c2 | 479 | char *data; |
10289b0f RK |
480 | int rc; |
481 | int trunc = 0; | |
482 | int tokens; | |
483 | ssize_t cnt = 0; | |
828716c2 JS |
484 | |
485 | /* | |
486 | * No partial writes. | |
487 | * Enough data must be present. | |
488 | */ | |
489 | if (*ppos != 0) | |
490 | return -EINVAL; | |
828716c2 | 491 | |
f7112e6c CS |
492 | if (format == SMK_FIXED24_FMT) { |
493 | /* | |
494 | * Minor hack for backward compatibility | |
495 | */ | |
c0ab6e56 | 496 | if (count < SMK_OLOADLEN || count > SMK_LOADLEN) |
f7112e6c | 497 | return -EINVAL; |
10289b0f RK |
498 | } else { |
499 | if (count >= PAGE_SIZE) { | |
500 | count = PAGE_SIZE - 1; | |
501 | trunc = 1; | |
502 | } | |
503 | } | |
f7112e6c | 504 | |
10289b0f | 505 | data = kmalloc(count + 1, GFP_KERNEL); |
828716c2 JS |
506 | if (data == NULL) |
507 | return -ENOMEM; | |
508 | ||
509 | if (copy_from_user(data, buf, count) != 0) { | |
510 | rc = -EFAULT; | |
511 | goto out; | |
512 | } | |
513 | ||
10289b0f RK |
514 | /* |
515 | * In case of parsing only part of user buf, | |
516 | * avoid having partial rule at the data buffer | |
517 | */ | |
518 | if (trunc) { | |
519 | while (count > 0 && (data[count - 1] != '\n')) | |
520 | --count; | |
521 | if (count == 0) { | |
522 | rc = -EINVAL; | |
470043ba | 523 | goto out; |
10289b0f | 524 | } |
f7112e6c CS |
525 | } |
526 | ||
10289b0f RK |
527 | data[count] = '\0'; |
528 | tokens = (format == SMK_CHANGE_FMT ? 4 : 3); | |
529 | while (cnt < count) { | |
530 | if (format == SMK_FIXED24_FMT) { | |
531 | rc = smk_parse_rule(data, &rule, 1); | |
532 | if (rc != 0) { | |
533 | rc = -EINVAL; | |
534 | goto out; | |
535 | } | |
536 | cnt = count; | |
537 | } else { | |
538 | rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens); | |
539 | if (rc <= 0) { | |
540 | rc = -EINVAL; | |
541 | goto out; | |
542 | } | |
543 | cnt += rc; | |
544 | } | |
545 | ||
546 | if (rule_list == NULL) | |
547 | rc = smk_set_access(&rule, &rule.smk_subject->smk_rules, | |
548 | &rule.smk_subject->smk_rules_lock, 1); | |
549 | else | |
550 | rc = smk_set_access(&rule, rule_list, rule_lock, 0); | |
551 | ||
552 | if (rc) | |
553 | goto out; | |
272cd7a8 CS |
554 | } |
555 | ||
10289b0f | 556 | rc = cnt; |
e114e473 CS |
557 | out: |
558 | kfree(data); | |
559 | return rc; | |
560 | } | |
561 | ||
7898e1f8 | 562 | /* |
40809565 | 563 | * Core logic for smackfs seq list operations. |
7898e1f8 CS |
564 | */ |
565 | ||
40809565 CS |
566 | static void *smk_seq_start(struct seq_file *s, loff_t *pos, |
567 | struct list_head *head) | |
7898e1f8 | 568 | { |
272cd7a8 CS |
569 | struct list_head *list; |
570 | ||
571 | /* | |
572 | * This is 0 the first time through. | |
573 | */ | |
574 | if (s->index == 0) | |
40809565 | 575 | s->private = head; |
272cd7a8 CS |
576 | |
577 | if (s->private == NULL) | |
7898e1f8 | 578 | return NULL; |
272cd7a8 CS |
579 | |
580 | list = s->private; | |
581 | if (list_empty(list)) | |
7898e1f8 | 582 | return NULL; |
272cd7a8 CS |
583 | |
584 | if (s->index == 0) | |
585 | return list->next; | |
586 | return list; | |
7898e1f8 CS |
587 | } |
588 | ||
40809565 CS |
589 | static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos, |
590 | struct list_head *head) | |
7898e1f8 CS |
591 | { |
592 | struct list_head *list = v; | |
593 | ||
40809565 | 594 | if (list_is_last(list, head)) { |
272cd7a8 | 595 | s->private = NULL; |
7898e1f8 CS |
596 | return NULL; |
597 | } | |
272cd7a8 | 598 | s->private = list->next; |
7898e1f8 CS |
599 | return list->next; |
600 | } | |
601 | ||
40809565 CS |
602 | static void smk_seq_stop(struct seq_file *s, void *v) |
603 | { | |
604 | /* No-op */ | |
605 | } | |
606 | ||
f7112e6c | 607 | static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max) |
40809565 | 608 | { |
f7112e6c CS |
609 | /* |
610 | * Don't show any rules with label names too long for | |
611 | * interface file (/smack/load or /smack/load2) | |
612 | * because you should expect to be able to write | |
613 | * anything you read back. | |
614 | */ | |
2f823ff8 | 615 | if (strlen(srp->smk_subject->smk_known) >= max || |
21c7eae2 | 616 | strlen(srp->smk_object->smk_known) >= max) |
f7112e6c | 617 | return; |
7898e1f8 | 618 | |
65ee7f45 RK |
619 | if (srp->smk_access == 0) |
620 | return; | |
621 | ||
21c7eae2 LP |
622 | seq_printf(s, "%s %s", |
623 | srp->smk_subject->smk_known, | |
624 | srp->smk_object->smk_known); | |
7898e1f8 CS |
625 | |
626 | seq_putc(s, ' '); | |
627 | ||
628 | if (srp->smk_access & MAY_READ) | |
629 | seq_putc(s, 'r'); | |
630 | if (srp->smk_access & MAY_WRITE) | |
631 | seq_putc(s, 'w'); | |
632 | if (srp->smk_access & MAY_EXEC) | |
633 | seq_putc(s, 'x'); | |
634 | if (srp->smk_access & MAY_APPEND) | |
635 | seq_putc(s, 'a'); | |
636 | if (srp->smk_access & MAY_TRANSMUTE) | |
637 | seq_putc(s, 't'); | |
c0ab6e56 CS |
638 | if (srp->smk_access & MAY_LOCK) |
639 | seq_putc(s, 'l'); | |
d166c802 CS |
640 | if (srp->smk_access & MAY_BRINGUP) |
641 | seq_putc(s, 'b'); | |
7898e1f8 CS |
642 | |
643 | seq_putc(s, '\n'); | |
f7112e6c CS |
644 | } |
645 | ||
646 | /* | |
647 | * Seq_file read operations for /smack/load | |
648 | */ | |
649 | ||
650 | static void *load2_seq_start(struct seq_file *s, loff_t *pos) | |
651 | { | |
652 | return smk_seq_start(s, pos, &smack_rule_list); | |
653 | } | |
654 | ||
655 | static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
656 | { | |
657 | return smk_seq_next(s, v, pos, &smack_rule_list); | |
658 | } | |
659 | ||
660 | static int load_seq_show(struct seq_file *s, void *v) | |
661 | { | |
662 | struct list_head *list = v; | |
663 | struct smack_master_list *smlp = | |
664 | list_entry(list, struct smack_master_list, list); | |
665 | ||
666 | smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN); | |
7898e1f8 CS |
667 | |
668 | return 0; | |
669 | } | |
670 | ||
7898e1f8 | 671 | static const struct seq_operations load_seq_ops = { |
f7112e6c CS |
672 | .start = load2_seq_start, |
673 | .next = load2_seq_next, | |
7898e1f8 | 674 | .show = load_seq_show, |
40809565 | 675 | .stop = smk_seq_stop, |
7898e1f8 CS |
676 | }; |
677 | ||
678 | /** | |
679 | * smk_open_load - open() for /smack/load | |
680 | * @inode: inode structure representing file | |
681 | * @file: "load" file pointer | |
682 | * | |
683 | * For reading, use load_seq_* seq_file reading operations. | |
684 | */ | |
685 | static int smk_open_load(struct inode *inode, struct file *file) | |
686 | { | |
687 | return seq_open(file, &load_seq_ops); | |
688 | } | |
689 | ||
690 | /** | |
691 | * smk_write_load - write() for /smack/load | |
692 | * @file: file pointer, not actually used | |
693 | * @buf: where to get the data from | |
694 | * @count: bytes sent | |
695 | * @ppos: where to start - must be 0 | |
696 | * | |
697 | */ | |
698 | static ssize_t smk_write_load(struct file *file, const char __user *buf, | |
699 | size_t count, loff_t *ppos) | |
700 | { | |
7898e1f8 CS |
701 | /* |
702 | * Must have privilege. | |
703 | * No partial writes. | |
704 | * Enough data must be present. | |
705 | */ | |
1880eff7 | 706 | if (!smack_privileged(CAP_MAC_ADMIN)) |
7898e1f8 CS |
707 | return -EPERM; |
708 | ||
f7112e6c CS |
709 | return smk_write_rules_list(file, buf, count, ppos, NULL, NULL, |
710 | SMK_FIXED24_FMT); | |
7898e1f8 CS |
711 | } |
712 | ||
e114e473 CS |
713 | static const struct file_operations smk_load_ops = { |
714 | .open = smk_open_load, | |
715 | .read = seq_read, | |
716 | .llseek = seq_lseek, | |
717 | .write = smk_write_load, | |
cb622bbb | 718 | .release = seq_release, |
e114e473 CS |
719 | }; |
720 | ||
721 | /** | |
722 | * smk_cipso_doi - initialize the CIPSO domain | |
723 | */ | |
30aa4faf | 724 | static void smk_cipso_doi(void) |
e114e473 CS |
725 | { |
726 | int rc; | |
727 | struct cipso_v4_doi *doip; | |
6d3dc07c | 728 | struct netlbl_audit nai; |
e114e473 | 729 | |
6d3dc07c | 730 | smk_netlabel_audit_set(&nai); |
4bc87e62 | 731 | |
6d3dc07c | 732 | rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai); |
e114e473 CS |
733 | if (rc != 0) |
734 | printk(KERN_WARNING "%s:%d remove rc = %d\n", | |
735 | __func__, __LINE__, rc); | |
736 | ||
737 | doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL); | |
738 | if (doip == NULL) | |
739 | panic("smack: Failed to initialize cipso DOI.\n"); | |
740 | doip->map.std = NULL; | |
741 | doip->doi = smk_cipso_doi_value; | |
742 | doip->type = CIPSO_V4_MAP_PASS; | |
743 | doip->tags[0] = CIPSO_V4_TAG_RBITMAP; | |
744 | for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++) | |
745 | doip->tags[rc] = CIPSO_V4_TAG_INVALID; | |
746 | ||
6d3dc07c | 747 | rc = netlbl_cfg_cipsov4_add(doip, &nai); |
b1edeb10 | 748 | if (rc != 0) { |
6c2e8ac0 PM |
749 | printk(KERN_WARNING "%s:%d cipso add rc = %d\n", |
750 | __func__, __LINE__, rc); | |
751 | kfree(doip); | |
752 | return; | |
753 | } | |
6d3dc07c | 754 | rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai); |
6c2e8ac0 PM |
755 | if (rc != 0) { |
756 | printk(KERN_WARNING "%s:%d map add rc = %d\n", | |
e114e473 | 757 | __func__, __LINE__, rc); |
b1edeb10 | 758 | kfree(doip); |
6c2e8ac0 | 759 | return; |
b1edeb10 | 760 | } |
e114e473 CS |
761 | } |
762 | ||
4bc87e62 CS |
763 | /** |
764 | * smk_unlbl_ambient - initialize the unlabeled domain | |
251a2a95 | 765 | * @oldambient: previous domain string |
4bc87e62 | 766 | */ |
30aa4faf | 767 | static void smk_unlbl_ambient(char *oldambient) |
4bc87e62 CS |
768 | { |
769 | int rc; | |
6d3dc07c | 770 | struct netlbl_audit nai; |
4bc87e62 | 771 | |
6d3dc07c | 772 | smk_netlabel_audit_set(&nai); |
4bc87e62 CS |
773 | |
774 | if (oldambient != NULL) { | |
6d3dc07c | 775 | rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai); |
4bc87e62 CS |
776 | if (rc != 0) |
777 | printk(KERN_WARNING "%s:%d remove rc = %d\n", | |
778 | __func__, __LINE__, rc); | |
779 | } | |
f7112e6c | 780 | if (smack_net_ambient == NULL) |
2f823ff8 | 781 | smack_net_ambient = &smack_known_floor; |
4bc87e62 | 782 | |
2f823ff8 | 783 | rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET, |
6d3dc07c | 784 | NULL, NULL, &nai); |
4bc87e62 CS |
785 | if (rc != 0) |
786 | printk(KERN_WARNING "%s:%d add rc = %d\n", | |
787 | __func__, __LINE__, rc); | |
788 | } | |
789 | ||
e114e473 CS |
790 | /* |
791 | * Seq_file read operations for /smack/cipso | |
792 | */ | |
793 | ||
794 | static void *cipso_seq_start(struct seq_file *s, loff_t *pos) | |
795 | { | |
40809565 | 796 | return smk_seq_start(s, pos, &smack_known_list); |
e114e473 CS |
797 | } |
798 | ||
799 | static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
800 | { | |
40809565 | 801 | return smk_seq_next(s, v, pos, &smack_known_list); |
e114e473 CS |
802 | } |
803 | ||
804 | /* | |
805 | * Print cipso labels in format: | |
806 | * label level[/cat[,cat]] | |
807 | */ | |
808 | static int cipso_seq_show(struct seq_file *s, void *v) | |
809 | { | |
7198e2ee EB |
810 | struct list_head *list = v; |
811 | struct smack_known *skp = | |
812 | list_entry(list, struct smack_known, list); | |
4fbe63d1 | 813 | struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat; |
e114e473 | 814 | char sep = '/'; |
e114e473 | 815 | int i; |
e114e473 | 816 | |
f7112e6c CS |
817 | /* |
818 | * Don't show a label that could not have been set using | |
819 | * /smack/cipso. This is in support of the notion that | |
820 | * anything read from /smack/cipso ought to be writeable | |
821 | * to /smack/cipso. | |
822 | * | |
823 | * /smack/cipso2 should be used instead. | |
824 | */ | |
825 | if (strlen(skp->smk_known) >= SMK_LABELLEN) | |
e114e473 CS |
826 | return 0; |
827 | ||
f7112e6c | 828 | seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl); |
e114e473 | 829 | |
4fbe63d1 PM |
830 | for (i = netlbl_catmap_walk(cmp, 0); i >= 0; |
831 | i = netlbl_catmap_walk(cmp, i + 1)) { | |
f7112e6c CS |
832 | seq_printf(s, "%c%d", sep, i); |
833 | sep = ','; | |
834 | } | |
e114e473 CS |
835 | |
836 | seq_putc(s, '\n'); | |
837 | ||
838 | return 0; | |
839 | } | |
840 | ||
88e9d34c | 841 | static const struct seq_operations cipso_seq_ops = { |
e114e473 | 842 | .start = cipso_seq_start, |
e114e473 CS |
843 | .next = cipso_seq_next, |
844 | .show = cipso_seq_show, | |
40809565 | 845 | .stop = smk_seq_stop, |
e114e473 CS |
846 | }; |
847 | ||
848 | /** | |
849 | * smk_open_cipso - open() for /smack/cipso | |
850 | * @inode: inode structure representing file | |
851 | * @file: "cipso" file pointer | |
852 | * | |
853 | * Connect our cipso_seq_* operations with /smack/cipso | |
854 | * file_operations | |
855 | */ | |
856 | static int smk_open_cipso(struct inode *inode, struct file *file) | |
857 | { | |
858 | return seq_open(file, &cipso_seq_ops); | |
859 | } | |
860 | ||
861 | /** | |
f7112e6c | 862 | * smk_set_cipso - do the work for write() for cipso and cipso2 |
251a2a95 | 863 | * @file: file pointer, not actually used |
e114e473 CS |
864 | * @buf: where to get the data from |
865 | * @count: bytes sent | |
866 | * @ppos: where to start | |
f7112e6c | 867 | * @format: /smack/cipso or /smack/cipso2 |
e114e473 CS |
868 | * |
869 | * Accepts only one cipso rule per write call. | |
870 | * Returns number of bytes written or error code, as appropriate | |
871 | */ | |
f7112e6c CS |
872 | static ssize_t smk_set_cipso(struct file *file, const char __user *buf, |
873 | size_t count, loff_t *ppos, int format) | |
e114e473 CS |
874 | { |
875 | struct smack_known *skp; | |
f7112e6c CS |
876 | struct netlbl_lsm_secattr ncats; |
877 | char mapcatset[SMK_CIPSOLEN]; | |
e114e473 | 878 | int maplevel; |
f7112e6c | 879 | unsigned int cat; |
e114e473 CS |
880 | int catlen; |
881 | ssize_t rc = -EINVAL; | |
882 | char *data = NULL; | |
883 | char *rule; | |
884 | int ret; | |
885 | int i; | |
886 | ||
887 | /* | |
888 | * Must have privilege. | |
889 | * No partial writes. | |
890 | * Enough data must be present. | |
891 | */ | |
1880eff7 | 892 | if (!smack_privileged(CAP_MAC_ADMIN)) |
e114e473 CS |
893 | return -EPERM; |
894 | if (*ppos != 0) | |
895 | return -EINVAL; | |
f7112e6c CS |
896 | if (format == SMK_FIXED24_FMT && |
897 | (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)) | |
e114e473 CS |
898 | return -EINVAL; |
899 | ||
900 | data = kzalloc(count + 1, GFP_KERNEL); | |
901 | if (data == NULL) | |
902 | return -ENOMEM; | |
903 | ||
904 | if (copy_from_user(data, buf, count) != 0) { | |
905 | rc = -EFAULT; | |
906 | goto unlockedout; | |
907 | } | |
908 | ||
909 | data[count] = '\0'; | |
910 | rule = data; | |
911 | /* | |
912 | * Only allow one writer at a time. Writes should be | |
913 | * quite rare and small in any case. | |
914 | */ | |
915 | mutex_lock(&smack_cipso_lock); | |
916 | ||
917 | skp = smk_import_entry(rule, 0); | |
918 | if (skp == NULL) | |
919 | goto out; | |
920 | ||
f7112e6c CS |
921 | if (format == SMK_FIXED24_FMT) |
922 | rule += SMK_LABELLEN; | |
923 | else | |
0fcfee61 | 924 | rule += strlen(skp->smk_known) + 1; |
f7112e6c | 925 | |
e114e473 CS |
926 | ret = sscanf(rule, "%d", &maplevel); |
927 | if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL) | |
928 | goto out; | |
929 | ||
930 | rule += SMK_DIGITLEN; | |
931 | ret = sscanf(rule, "%d", &catlen); | |
932 | if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM) | |
933 | goto out; | |
934 | ||
f7112e6c CS |
935 | if (format == SMK_FIXED24_FMT && |
936 | count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN)) | |
e114e473 CS |
937 | goto out; |
938 | ||
939 | memset(mapcatset, 0, sizeof(mapcatset)); | |
940 | ||
941 | for (i = 0; i < catlen; i++) { | |
942 | rule += SMK_DIGITLEN; | |
f7112e6c | 943 | ret = sscanf(rule, "%u", &cat); |
677264e8 | 944 | if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM) |
e114e473 CS |
945 | goto out; |
946 | ||
947 | smack_catset_bit(cat, mapcatset); | |
948 | } | |
949 | ||
f7112e6c CS |
950 | rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN); |
951 | if (rc >= 0) { | |
4fbe63d1 | 952 | netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat); |
f7112e6c CS |
953 | skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat; |
954 | skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl; | |
955 | rc = count; | |
e114e473 CS |
956 | } |
957 | ||
e114e473 CS |
958 | out: |
959 | mutex_unlock(&smack_cipso_lock); | |
960 | unlockedout: | |
961 | kfree(data); | |
962 | return rc; | |
963 | } | |
964 | ||
f7112e6c CS |
965 | /** |
966 | * smk_write_cipso - write() for /smack/cipso | |
967 | * @file: file pointer, not actually used | |
968 | * @buf: where to get the data from | |
969 | * @count: bytes sent | |
970 | * @ppos: where to start | |
971 | * | |
972 | * Accepts only one cipso rule per write call. | |
973 | * Returns number of bytes written or error code, as appropriate | |
974 | */ | |
975 | static ssize_t smk_write_cipso(struct file *file, const char __user *buf, | |
976 | size_t count, loff_t *ppos) | |
977 | { | |
978 | return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT); | |
979 | } | |
980 | ||
e114e473 CS |
981 | static const struct file_operations smk_cipso_ops = { |
982 | .open = smk_open_cipso, | |
983 | .read = seq_read, | |
984 | .llseek = seq_lseek, | |
985 | .write = smk_write_cipso, | |
986 | .release = seq_release, | |
987 | }; | |
988 | ||
f7112e6c CS |
989 | /* |
990 | * Seq_file read operations for /smack/cipso2 | |
991 | */ | |
992 | ||
993 | /* | |
994 | * Print cipso labels in format: | |
995 | * label level[/cat[,cat]] | |
996 | */ | |
997 | static int cipso2_seq_show(struct seq_file *s, void *v) | |
998 | { | |
999 | struct list_head *list = v; | |
1000 | struct smack_known *skp = | |
1001 | list_entry(list, struct smack_known, list); | |
4fbe63d1 | 1002 | struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat; |
f7112e6c CS |
1003 | char sep = '/'; |
1004 | int i; | |
1005 | ||
1006 | seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl); | |
1007 | ||
4fbe63d1 PM |
1008 | for (i = netlbl_catmap_walk(cmp, 0); i >= 0; |
1009 | i = netlbl_catmap_walk(cmp, i + 1)) { | |
f7112e6c CS |
1010 | seq_printf(s, "%c%d", sep, i); |
1011 | sep = ','; | |
1012 | } | |
1013 | ||
1014 | seq_putc(s, '\n'); | |
1015 | ||
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | static const struct seq_operations cipso2_seq_ops = { | |
1020 | .start = cipso_seq_start, | |
1021 | .next = cipso_seq_next, | |
1022 | .show = cipso2_seq_show, | |
1023 | .stop = smk_seq_stop, | |
1024 | }; | |
1025 | ||
1026 | /** | |
1027 | * smk_open_cipso2 - open() for /smack/cipso2 | |
1028 | * @inode: inode structure representing file | |
1029 | * @file: "cipso2" file pointer | |
1030 | * | |
1031 | * Connect our cipso_seq_* operations with /smack/cipso2 | |
1032 | * file_operations | |
1033 | */ | |
1034 | static int smk_open_cipso2(struct inode *inode, struct file *file) | |
1035 | { | |
1036 | return seq_open(file, &cipso2_seq_ops); | |
1037 | } | |
1038 | ||
1039 | /** | |
1040 | * smk_write_cipso2 - write() for /smack/cipso2 | |
1041 | * @file: file pointer, not actually used | |
1042 | * @buf: where to get the data from | |
1043 | * @count: bytes sent | |
1044 | * @ppos: where to start | |
1045 | * | |
1046 | * Accepts only one cipso rule per write call. | |
1047 | * Returns number of bytes written or error code, as appropriate | |
1048 | */ | |
1049 | static ssize_t smk_write_cipso2(struct file *file, const char __user *buf, | |
1050 | size_t count, loff_t *ppos) | |
1051 | { | |
1052 | return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT); | |
1053 | } | |
1054 | ||
1055 | static const struct file_operations smk_cipso2_ops = { | |
1056 | .open = smk_open_cipso2, | |
1057 | .read = seq_read, | |
1058 | .llseek = seq_lseek, | |
1059 | .write = smk_write_cipso2, | |
1060 | .release = seq_release, | |
1061 | }; | |
1062 | ||
6d3dc07c CS |
1063 | /* |
1064 | * Seq_file read operations for /smack/netlabel | |
1065 | */ | |
1066 | ||
1067 | static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos) | |
1068 | { | |
40809565 | 1069 | return smk_seq_start(s, pos, &smk_netlbladdr_list); |
6d3dc07c CS |
1070 | } |
1071 | ||
1072 | static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
1073 | { | |
40809565 | 1074 | return smk_seq_next(s, v, pos, &smk_netlbladdr_list); |
6d3dc07c | 1075 | } |
6d3dc07c CS |
1076 | #define BEBITS (sizeof(__be32) * 8) |
1077 | ||
1078 | /* | |
1079 | * Print host/label pairs | |
1080 | */ | |
1081 | static int netlbladdr_seq_show(struct seq_file *s, void *v) | |
1082 | { | |
7198e2ee EB |
1083 | struct list_head *list = v; |
1084 | struct smk_netlbladdr *skp = | |
1085 | list_entry(list, struct smk_netlbladdr, list); | |
6d3dc07c | 1086 | unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr; |
113a0e45 | 1087 | int maskn; |
1088 | u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr); | |
6d3dc07c | 1089 | |
113a0e45 | 1090 | for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++); |
6d3dc07c CS |
1091 | |
1092 | seq_printf(s, "%u.%u.%u.%u/%d %s\n", | |
21c7eae2 | 1093 | hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label->smk_known); |
6d3dc07c CS |
1094 | |
1095 | return 0; | |
1096 | } | |
1097 | ||
88e9d34c | 1098 | static const struct seq_operations netlbladdr_seq_ops = { |
6d3dc07c | 1099 | .start = netlbladdr_seq_start, |
6d3dc07c CS |
1100 | .next = netlbladdr_seq_next, |
1101 | .show = netlbladdr_seq_show, | |
40809565 | 1102 | .stop = smk_seq_stop, |
6d3dc07c CS |
1103 | }; |
1104 | ||
1105 | /** | |
1106 | * smk_open_netlbladdr - open() for /smack/netlabel | |
1107 | * @inode: inode structure representing file | |
1108 | * @file: "netlabel" file pointer | |
1109 | * | |
1110 | * Connect our netlbladdr_seq_* operations with /smack/netlabel | |
1111 | * file_operations | |
1112 | */ | |
1113 | static int smk_open_netlbladdr(struct inode *inode, struct file *file) | |
1114 | { | |
1115 | return seq_open(file, &netlbladdr_seq_ops); | |
1116 | } | |
1117 | ||
113a0e45 | 1118 | /** |
1119 | * smk_netlbladdr_insert | |
1120 | * @new : netlabel to insert | |
1121 | * | |
1122 | * This helper insert netlabel in the smack_netlbladdrs list | |
1123 | * sorted by netmask length (longest to smallest) | |
7198e2ee EB |
1124 | * locked by &smk_netlbladdr_lock in smk_write_netlbladdr |
1125 | * | |
113a0e45 | 1126 | */ |
1127 | static void smk_netlbladdr_insert(struct smk_netlbladdr *new) | |
1128 | { | |
7198e2ee | 1129 | struct smk_netlbladdr *m, *m_next; |
113a0e45 | 1130 | |
7198e2ee EB |
1131 | if (list_empty(&smk_netlbladdr_list)) { |
1132 | list_add_rcu(&new->list, &smk_netlbladdr_list); | |
113a0e45 | 1133 | return; |
1134 | } | |
1135 | ||
05725f7e JP |
1136 | m = list_entry_rcu(smk_netlbladdr_list.next, |
1137 | struct smk_netlbladdr, list); | |
7198e2ee | 1138 | |
113a0e45 | 1139 | /* the comparison '>' is a bit hacky, but works */ |
7198e2ee EB |
1140 | if (new->smk_mask.s_addr > m->smk_mask.s_addr) { |
1141 | list_add_rcu(&new->list, &smk_netlbladdr_list); | |
113a0e45 | 1142 | return; |
1143 | } | |
7198e2ee EB |
1144 | |
1145 | list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) { | |
1146 | if (list_is_last(&m->list, &smk_netlbladdr_list)) { | |
1147 | list_add_rcu(&new->list, &m->list); | |
113a0e45 | 1148 | return; |
1149 | } | |
05725f7e JP |
1150 | m_next = list_entry_rcu(m->list.next, |
1151 | struct smk_netlbladdr, list); | |
7198e2ee EB |
1152 | if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) { |
1153 | list_add_rcu(&new->list, &m->list); | |
113a0e45 | 1154 | return; |
1155 | } | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | ||
6d3dc07c CS |
1160 | /** |
1161 | * smk_write_netlbladdr - write() for /smack/netlabel | |
251a2a95 | 1162 | * @file: file pointer, not actually used |
6d3dc07c CS |
1163 | * @buf: where to get the data from |
1164 | * @count: bytes sent | |
1165 | * @ppos: where to start | |
1166 | * | |
1167 | * Accepts only one netlbladdr per write call. | |
1168 | * Returns number of bytes written or error code, as appropriate | |
1169 | */ | |
1170 | static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf, | |
1171 | size_t count, loff_t *ppos) | |
1172 | { | |
21c7eae2 | 1173 | struct smk_netlbladdr *snp; |
6d3dc07c | 1174 | struct sockaddr_in newname; |
f7112e6c | 1175 | char *smack; |
21c7eae2 | 1176 | struct smack_known *skp; |
f7112e6c | 1177 | char *data; |
6d3dc07c CS |
1178 | char *host = (char *)&newname.sin_addr.s_addr; |
1179 | int rc; | |
1180 | struct netlbl_audit audit_info; | |
1181 | struct in_addr mask; | |
1182 | unsigned int m; | |
7198e2ee | 1183 | int found; |
113a0e45 | 1184 | u32 mask_bits = (1<<31); |
6d3dc07c | 1185 | __be32 nsa; |
113a0e45 | 1186 | u32 temp_mask; |
6d3dc07c CS |
1187 | |
1188 | /* | |
1189 | * Must have privilege. | |
1190 | * No partial writes. | |
1191 | * Enough data must be present. | |
1192 | * "<addr/mask, as a.b.c.d/e><space><label>" | |
1193 | * "<addr, as a.b.c.d><space><label>" | |
1194 | */ | |
1880eff7 | 1195 | if (!smack_privileged(CAP_MAC_ADMIN)) |
6d3dc07c CS |
1196 | return -EPERM; |
1197 | if (*ppos != 0) | |
1198 | return -EINVAL; | |
f7112e6c | 1199 | if (count < SMK_NETLBLADDRMIN) |
6d3dc07c | 1200 | return -EINVAL; |
f7112e6c CS |
1201 | |
1202 | data = kzalloc(count + 1, GFP_KERNEL); | |
1203 | if (data == NULL) | |
1204 | return -ENOMEM; | |
1205 | ||
1206 | if (copy_from_user(data, buf, count) != 0) { | |
1207 | rc = -EFAULT; | |
1208 | goto free_data_out; | |
1209 | } | |
1210 | ||
1211 | smack = kzalloc(count + 1, GFP_KERNEL); | |
1212 | if (smack == NULL) { | |
1213 | rc = -ENOMEM; | |
1214 | goto free_data_out; | |
1215 | } | |
6d3dc07c CS |
1216 | |
1217 | data[count] = '\0'; | |
1218 | ||
ec554fa7 | 1219 | rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s", |
6d3dc07c CS |
1220 | &host[0], &host[1], &host[2], &host[3], &m, smack); |
1221 | if (rc != 6) { | |
1222 | rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s", | |
1223 | &host[0], &host[1], &host[2], &host[3], smack); | |
f7112e6c CS |
1224 | if (rc != 5) { |
1225 | rc = -EINVAL; | |
1226 | goto free_out; | |
1227 | } | |
6d3dc07c CS |
1228 | m = BEBITS; |
1229 | } | |
f7112e6c CS |
1230 | if (m > BEBITS) { |
1231 | rc = -EINVAL; | |
1232 | goto free_out; | |
1233 | } | |
6d3dc07c | 1234 | |
f7112e6c CS |
1235 | /* |
1236 | * If smack begins with '-', it is an option, don't import it | |
1237 | */ | |
4303154e | 1238 | if (smack[0] != '-') { |
21c7eae2 LP |
1239 | skp = smk_import_entry(smack, 0); |
1240 | if (skp == NULL) { | |
f7112e6c CS |
1241 | rc = -EINVAL; |
1242 | goto free_out; | |
1243 | } | |
4303154e EB |
1244 | } else { |
1245 | /* check known options */ | |
21c7eae2 LP |
1246 | if (strcmp(smack, smack_cipso_option.smk_known) == 0) |
1247 | skp = &smack_cipso_option; | |
f7112e6c CS |
1248 | else { |
1249 | rc = -EINVAL; | |
1250 | goto free_out; | |
1251 | } | |
4303154e | 1252 | } |
6d3dc07c | 1253 | |
113a0e45 | 1254 | for (temp_mask = 0; m > 0; m--) { |
1255 | temp_mask |= mask_bits; | |
1256 | mask_bits >>= 1; | |
6d3dc07c | 1257 | } |
113a0e45 | 1258 | mask.s_addr = cpu_to_be32(temp_mask); |
1259 | ||
1260 | newname.sin_addr.s_addr &= mask.s_addr; | |
6d3dc07c CS |
1261 | /* |
1262 | * Only allow one writer at a time. Writes should be | |
1263 | * quite rare and small in any case. | |
1264 | */ | |
1265 | mutex_lock(&smk_netlbladdr_lock); | |
1266 | ||
1267 | nsa = newname.sin_addr.s_addr; | |
113a0e45 | 1268 | /* try to find if the prefix is already in the list */ |
7198e2ee | 1269 | found = 0; |
21c7eae2 LP |
1270 | list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list) { |
1271 | if (snp->smk_host.sin_addr.s_addr == nsa && | |
1272 | snp->smk_mask.s_addr == mask.s_addr) { | |
7198e2ee | 1273 | found = 1; |
6d3dc07c | 1274 | break; |
7198e2ee EB |
1275 | } |
1276 | } | |
6d3dc07c CS |
1277 | smk_netlabel_audit_set(&audit_info); |
1278 | ||
7198e2ee | 1279 | if (found == 0) { |
21c7eae2 LP |
1280 | snp = kzalloc(sizeof(*snp), GFP_KERNEL); |
1281 | if (snp == NULL) | |
6d3dc07c CS |
1282 | rc = -ENOMEM; |
1283 | else { | |
1284 | rc = 0; | |
21c7eae2 LP |
1285 | snp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr; |
1286 | snp->smk_mask.s_addr = mask.s_addr; | |
1287 | snp->smk_label = skp; | |
1288 | smk_netlbladdr_insert(snp); | |
6d3dc07c CS |
1289 | } |
1290 | } else { | |
4303154e | 1291 | /* we delete the unlabeled entry, only if the previous label |
25985edc | 1292 | * wasn't the special CIPSO option */ |
21c7eae2 | 1293 | if (snp->smk_label != &smack_cipso_option) |
4303154e | 1294 | rc = netlbl_cfg_unlbl_static_del(&init_net, NULL, |
21c7eae2 | 1295 | &snp->smk_host.sin_addr, &snp->smk_mask, |
4303154e EB |
1296 | PF_INET, &audit_info); |
1297 | else | |
1298 | rc = 0; | |
21c7eae2 | 1299 | snp->smk_label = skp; |
6d3dc07c CS |
1300 | } |
1301 | ||
1302 | /* | |
1303 | * Now tell netlabel about the single label nature of | |
1304 | * this host so that incoming packets get labeled. | |
4303154e | 1305 | * but only if we didn't get the special CIPSO option |
6d3dc07c | 1306 | */ |
21c7eae2 | 1307 | if (rc == 0 && skp != &smack_cipso_option) |
6d3dc07c | 1308 | rc = netlbl_cfg_unlbl_static_add(&init_net, NULL, |
21c7eae2 LP |
1309 | &snp->smk_host.sin_addr, &snp->smk_mask, PF_INET, |
1310 | snp->smk_label->smk_secid, &audit_info); | |
6d3dc07c CS |
1311 | |
1312 | if (rc == 0) | |
1313 | rc = count; | |
1314 | ||
1315 | mutex_unlock(&smk_netlbladdr_lock); | |
1316 | ||
f7112e6c CS |
1317 | free_out: |
1318 | kfree(smack); | |
1319 | free_data_out: | |
1320 | kfree(data); | |
1321 | ||
6d3dc07c CS |
1322 | return rc; |
1323 | } | |
1324 | ||
1325 | static const struct file_operations smk_netlbladdr_ops = { | |
1326 | .open = smk_open_netlbladdr, | |
1327 | .read = seq_read, | |
1328 | .llseek = seq_lseek, | |
1329 | .write = smk_write_netlbladdr, | |
1330 | .release = seq_release, | |
1331 | }; | |
1332 | ||
e114e473 CS |
1333 | /** |
1334 | * smk_read_doi - read() for /smack/doi | |
1335 | * @filp: file pointer, not actually used | |
1336 | * @buf: where to put the result | |
1337 | * @count: maximum to send along | |
1338 | * @ppos: where to start | |
1339 | * | |
1340 | * Returns number of bytes read or error code, as appropriate | |
1341 | */ | |
1342 | static ssize_t smk_read_doi(struct file *filp, char __user *buf, | |
1343 | size_t count, loff_t *ppos) | |
1344 | { | |
1345 | char temp[80]; | |
1346 | ssize_t rc; | |
1347 | ||
1348 | if (*ppos != 0) | |
1349 | return 0; | |
1350 | ||
1351 | sprintf(temp, "%d", smk_cipso_doi_value); | |
1352 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
1353 | ||
1354 | return rc; | |
1355 | } | |
1356 | ||
1357 | /** | |
1358 | * smk_write_doi - write() for /smack/doi | |
251a2a95 | 1359 | * @file: file pointer, not actually used |
e114e473 CS |
1360 | * @buf: where to get the data from |
1361 | * @count: bytes sent | |
1362 | * @ppos: where to start | |
1363 | * | |
1364 | * Returns number of bytes written or error code, as appropriate | |
1365 | */ | |
1366 | static ssize_t smk_write_doi(struct file *file, const char __user *buf, | |
1367 | size_t count, loff_t *ppos) | |
1368 | { | |
1369 | char temp[80]; | |
1370 | int i; | |
1371 | ||
1880eff7 | 1372 | if (!smack_privileged(CAP_MAC_ADMIN)) |
e114e473 CS |
1373 | return -EPERM; |
1374 | ||
1375 | if (count >= sizeof(temp) || count == 0) | |
1376 | return -EINVAL; | |
1377 | ||
1378 | if (copy_from_user(temp, buf, count) != 0) | |
1379 | return -EFAULT; | |
1380 | ||
1381 | temp[count] = '\0'; | |
1382 | ||
1383 | if (sscanf(temp, "%d", &i) != 1) | |
1384 | return -EINVAL; | |
1385 | ||
1386 | smk_cipso_doi_value = i; | |
1387 | ||
1388 | smk_cipso_doi(); | |
1389 | ||
1390 | return count; | |
1391 | } | |
1392 | ||
1393 | static const struct file_operations smk_doi_ops = { | |
1394 | .read = smk_read_doi, | |
1395 | .write = smk_write_doi, | |
6038f373 | 1396 | .llseek = default_llseek, |
e114e473 CS |
1397 | }; |
1398 | ||
1399 | /** | |
1400 | * smk_read_direct - read() for /smack/direct | |
1401 | * @filp: file pointer, not actually used | |
1402 | * @buf: where to put the result | |
1403 | * @count: maximum to send along | |
1404 | * @ppos: where to start | |
1405 | * | |
1406 | * Returns number of bytes read or error code, as appropriate | |
1407 | */ | |
1408 | static ssize_t smk_read_direct(struct file *filp, char __user *buf, | |
1409 | size_t count, loff_t *ppos) | |
1410 | { | |
1411 | char temp[80]; | |
1412 | ssize_t rc; | |
1413 | ||
1414 | if (*ppos != 0) | |
1415 | return 0; | |
1416 | ||
1417 | sprintf(temp, "%d", smack_cipso_direct); | |
1418 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
1419 | ||
1420 | return rc; | |
1421 | } | |
1422 | ||
1423 | /** | |
1424 | * smk_write_direct - write() for /smack/direct | |
251a2a95 | 1425 | * @file: file pointer, not actually used |
e114e473 CS |
1426 | * @buf: where to get the data from |
1427 | * @count: bytes sent | |
1428 | * @ppos: where to start | |
1429 | * | |
1430 | * Returns number of bytes written or error code, as appropriate | |
1431 | */ | |
1432 | static ssize_t smk_write_direct(struct file *file, const char __user *buf, | |
1433 | size_t count, loff_t *ppos) | |
1434 | { | |
f7112e6c | 1435 | struct smack_known *skp; |
e114e473 CS |
1436 | char temp[80]; |
1437 | int i; | |
1438 | ||
1880eff7 | 1439 | if (!smack_privileged(CAP_MAC_ADMIN)) |
e114e473 CS |
1440 | return -EPERM; |
1441 | ||
1442 | if (count >= sizeof(temp) || count == 0) | |
1443 | return -EINVAL; | |
1444 | ||
1445 | if (copy_from_user(temp, buf, count) != 0) | |
1446 | return -EFAULT; | |
1447 | ||
1448 | temp[count] = '\0'; | |
1449 | ||
1450 | if (sscanf(temp, "%d", &i) != 1) | |
1451 | return -EINVAL; | |
1452 | ||
f7112e6c CS |
1453 | /* |
1454 | * Don't do anything if the value hasn't actually changed. | |
1455 | * If it is changing reset the level on entries that were | |
1456 | * set up to be direct when they were created. | |
1457 | */ | |
1458 | if (smack_cipso_direct != i) { | |
1459 | mutex_lock(&smack_known_lock); | |
1460 | list_for_each_entry_rcu(skp, &smack_known_list, list) | |
1461 | if (skp->smk_netlabel.attr.mls.lvl == | |
1462 | smack_cipso_direct) | |
1463 | skp->smk_netlabel.attr.mls.lvl = i; | |
1464 | smack_cipso_direct = i; | |
1465 | mutex_unlock(&smack_known_lock); | |
1466 | } | |
e114e473 CS |
1467 | |
1468 | return count; | |
1469 | } | |
1470 | ||
1471 | static const struct file_operations smk_direct_ops = { | |
1472 | .read = smk_read_direct, | |
1473 | .write = smk_write_direct, | |
6038f373 | 1474 | .llseek = default_llseek, |
e114e473 CS |
1475 | }; |
1476 | ||
f7112e6c CS |
1477 | /** |
1478 | * smk_read_mapped - read() for /smack/mapped | |
1479 | * @filp: file pointer, not actually used | |
1480 | * @buf: where to put the result | |
1481 | * @count: maximum to send along | |
1482 | * @ppos: where to start | |
1483 | * | |
1484 | * Returns number of bytes read or error code, as appropriate | |
1485 | */ | |
1486 | static ssize_t smk_read_mapped(struct file *filp, char __user *buf, | |
1487 | size_t count, loff_t *ppos) | |
1488 | { | |
1489 | char temp[80]; | |
1490 | ssize_t rc; | |
1491 | ||
1492 | if (*ppos != 0) | |
1493 | return 0; | |
1494 | ||
1495 | sprintf(temp, "%d", smack_cipso_mapped); | |
1496 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
1497 | ||
1498 | return rc; | |
1499 | } | |
1500 | ||
1501 | /** | |
1502 | * smk_write_mapped - write() for /smack/mapped | |
1503 | * @file: file pointer, not actually used | |
1504 | * @buf: where to get the data from | |
1505 | * @count: bytes sent | |
1506 | * @ppos: where to start | |
1507 | * | |
1508 | * Returns number of bytes written or error code, as appropriate | |
1509 | */ | |
1510 | static ssize_t smk_write_mapped(struct file *file, const char __user *buf, | |
1511 | size_t count, loff_t *ppos) | |
1512 | { | |
1513 | struct smack_known *skp; | |
1514 | char temp[80]; | |
1515 | int i; | |
1516 | ||
1880eff7 | 1517 | if (!smack_privileged(CAP_MAC_ADMIN)) |
f7112e6c CS |
1518 | return -EPERM; |
1519 | ||
1520 | if (count >= sizeof(temp) || count == 0) | |
1521 | return -EINVAL; | |
1522 | ||
1523 | if (copy_from_user(temp, buf, count) != 0) | |
1524 | return -EFAULT; | |
1525 | ||
1526 | temp[count] = '\0'; | |
1527 | ||
1528 | if (sscanf(temp, "%d", &i) != 1) | |
1529 | return -EINVAL; | |
1530 | ||
1531 | /* | |
1532 | * Don't do anything if the value hasn't actually changed. | |
1533 | * If it is changing reset the level on entries that were | |
1534 | * set up to be mapped when they were created. | |
1535 | */ | |
1536 | if (smack_cipso_mapped != i) { | |
1537 | mutex_lock(&smack_known_lock); | |
1538 | list_for_each_entry_rcu(skp, &smack_known_list, list) | |
1539 | if (skp->smk_netlabel.attr.mls.lvl == | |
1540 | smack_cipso_mapped) | |
1541 | skp->smk_netlabel.attr.mls.lvl = i; | |
1542 | smack_cipso_mapped = i; | |
1543 | mutex_unlock(&smack_known_lock); | |
1544 | } | |
1545 | ||
1546 | return count; | |
1547 | } | |
1548 | ||
1549 | static const struct file_operations smk_mapped_ops = { | |
1550 | .read = smk_read_mapped, | |
1551 | .write = smk_write_mapped, | |
1552 | .llseek = default_llseek, | |
1553 | }; | |
1554 | ||
e114e473 CS |
1555 | /** |
1556 | * smk_read_ambient - read() for /smack/ambient | |
1557 | * @filp: file pointer, not actually used | |
1558 | * @buf: where to put the result | |
1559 | * @cn: maximum to send along | |
1560 | * @ppos: where to start | |
1561 | * | |
1562 | * Returns number of bytes read or error code, as appropriate | |
1563 | */ | |
1564 | static ssize_t smk_read_ambient(struct file *filp, char __user *buf, | |
1565 | size_t cn, loff_t *ppos) | |
1566 | { | |
1567 | ssize_t rc; | |
e114e473 CS |
1568 | int asize; |
1569 | ||
1570 | if (*ppos != 0) | |
1571 | return 0; | |
1572 | /* | |
1573 | * Being careful to avoid a problem in the case where | |
1574 | * smack_net_ambient gets changed in midstream. | |
e114e473 | 1575 | */ |
4bc87e62 | 1576 | mutex_lock(&smack_ambient_lock); |
e114e473 | 1577 | |
2f823ff8 | 1578 | asize = strlen(smack_net_ambient->smk_known) + 1; |
4bc87e62 CS |
1579 | |
1580 | if (cn >= asize) | |
1581 | rc = simple_read_from_buffer(buf, cn, ppos, | |
2f823ff8 CS |
1582 | smack_net_ambient->smk_known, |
1583 | asize); | |
4bc87e62 CS |
1584 | else |
1585 | rc = -EINVAL; | |
e114e473 | 1586 | |
4bc87e62 | 1587 | mutex_unlock(&smack_ambient_lock); |
e114e473 CS |
1588 | |
1589 | return rc; | |
1590 | } | |
1591 | ||
1592 | /** | |
1593 | * smk_write_ambient - write() for /smack/ambient | |
251a2a95 | 1594 | * @file: file pointer, not actually used |
e114e473 CS |
1595 | * @buf: where to get the data from |
1596 | * @count: bytes sent | |
1597 | * @ppos: where to start | |
1598 | * | |
1599 | * Returns number of bytes written or error code, as appropriate | |
1600 | */ | |
1601 | static ssize_t smk_write_ambient(struct file *file, const char __user *buf, | |
1602 | size_t count, loff_t *ppos) | |
1603 | { | |
2f823ff8 | 1604 | struct smack_known *skp; |
4bc87e62 | 1605 | char *oldambient; |
f7112e6c CS |
1606 | char *data; |
1607 | int rc = count; | |
e114e473 | 1608 | |
1880eff7 | 1609 | if (!smack_privileged(CAP_MAC_ADMIN)) |
e114e473 CS |
1610 | return -EPERM; |
1611 | ||
f7112e6c CS |
1612 | data = kzalloc(count + 1, GFP_KERNEL); |
1613 | if (data == NULL) | |
1614 | return -ENOMEM; | |
e114e473 | 1615 | |
f7112e6c CS |
1616 | if (copy_from_user(data, buf, count) != 0) { |
1617 | rc = -EFAULT; | |
1618 | goto out; | |
1619 | } | |
e114e473 | 1620 | |
2f823ff8 CS |
1621 | skp = smk_import_entry(data, count); |
1622 | if (skp == NULL) { | |
f7112e6c CS |
1623 | rc = -EINVAL; |
1624 | goto out; | |
1625 | } | |
e114e473 | 1626 | |
4bc87e62 CS |
1627 | mutex_lock(&smack_ambient_lock); |
1628 | ||
2f823ff8 CS |
1629 | oldambient = smack_net_ambient->smk_known; |
1630 | smack_net_ambient = skp; | |
4bc87e62 CS |
1631 | smk_unlbl_ambient(oldambient); |
1632 | ||
1633 | mutex_unlock(&smack_ambient_lock); | |
e114e473 | 1634 | |
f7112e6c CS |
1635 | out: |
1636 | kfree(data); | |
1637 | return rc; | |
e114e473 CS |
1638 | } |
1639 | ||
1640 | static const struct file_operations smk_ambient_ops = { | |
1641 | .read = smk_read_ambient, | |
1642 | .write = smk_write_ambient, | |
6038f373 | 1643 | .llseek = default_llseek, |
e114e473 CS |
1644 | }; |
1645 | ||
15446235 | 1646 | /** |
00f84f3f | 1647 | * smk_read_onlycap - read() for smackfs/onlycap |
15446235 CS |
1648 | * @filp: file pointer, not actually used |
1649 | * @buf: where to put the result | |
1650 | * @cn: maximum to send along | |
1651 | * @ppos: where to start | |
1652 | * | |
1653 | * Returns number of bytes read or error code, as appropriate | |
1654 | */ | |
1655 | static ssize_t smk_read_onlycap(struct file *filp, char __user *buf, | |
1656 | size_t cn, loff_t *ppos) | |
1657 | { | |
1658 | char *smack = ""; | |
1659 | ssize_t rc = -EINVAL; | |
1660 | int asize; | |
1661 | ||
1662 | if (*ppos != 0) | |
1663 | return 0; | |
1664 | ||
1665 | if (smack_onlycap != NULL) | |
00f84f3f | 1666 | smack = smack_onlycap->smk_known; |
15446235 CS |
1667 | |
1668 | asize = strlen(smack) + 1; | |
1669 | ||
1670 | if (cn >= asize) | |
1671 | rc = simple_read_from_buffer(buf, cn, ppos, smack, asize); | |
1672 | ||
1673 | return rc; | |
1674 | } | |
1675 | ||
1676 | /** | |
00f84f3f | 1677 | * smk_write_onlycap - write() for smackfs/onlycap |
251a2a95 | 1678 | * @file: file pointer, not actually used |
15446235 CS |
1679 | * @buf: where to get the data from |
1680 | * @count: bytes sent | |
1681 | * @ppos: where to start | |
1682 | * | |
1683 | * Returns number of bytes written or error code, as appropriate | |
1684 | */ | |
1685 | static ssize_t smk_write_onlycap(struct file *file, const char __user *buf, | |
1686 | size_t count, loff_t *ppos) | |
1687 | { | |
f7112e6c | 1688 | char *data; |
2f823ff8 | 1689 | struct smack_known *skp = smk_of_task(current->cred->security); |
f7112e6c | 1690 | int rc = count; |
15446235 | 1691 | |
1880eff7 | 1692 | if (!smack_privileged(CAP_MAC_ADMIN)) |
15446235 CS |
1693 | return -EPERM; |
1694 | ||
1695 | /* | |
1696 | * This can be done using smk_access() but is done | |
1697 | * explicitly for clarity. The smk_access() implementation | |
1698 | * would use smk_access(smack_onlycap, MAY_WRITE) | |
1699 | */ | |
00f84f3f | 1700 | if (smack_onlycap != NULL && smack_onlycap != skp) |
15446235 CS |
1701 | return -EPERM; |
1702 | ||
b862e561 | 1703 | data = kzalloc(count + 1, GFP_KERNEL); |
f7112e6c CS |
1704 | if (data == NULL) |
1705 | return -ENOMEM; | |
15446235 CS |
1706 | |
1707 | /* | |
1708 | * Should the null string be passed in unset the onlycap value. | |
1709 | * This seems like something to be careful with as usually | |
1710 | * smk_import only expects to return NULL for errors. It | |
1711 | * is usually the case that a nullstring or "\n" would be | |
1712 | * bad to pass to smk_import but in fact this is useful here. | |
f7112e6c CS |
1713 | * |
1714 | * smk_import will also reject a label beginning with '-', | |
1715 | * so "-usecapabilities" will also work. | |
15446235 | 1716 | */ |
f7112e6c CS |
1717 | if (copy_from_user(data, buf, count) != 0) |
1718 | rc = -EFAULT; | |
1719 | else | |
00f84f3f | 1720 | smack_onlycap = smk_import_entry(data, count); |
15446235 | 1721 | |
f7112e6c CS |
1722 | kfree(data); |
1723 | return rc; | |
15446235 CS |
1724 | } |
1725 | ||
1726 | static const struct file_operations smk_onlycap_ops = { | |
1727 | .read = smk_read_onlycap, | |
1728 | .write = smk_write_onlycap, | |
6038f373 | 1729 | .llseek = default_llseek, |
15446235 CS |
1730 | }; |
1731 | ||
bf4b2fee CS |
1732 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
1733 | /** | |
1734 | * smk_read_unconfined - read() for smackfs/unconfined | |
1735 | * @filp: file pointer, not actually used | |
1736 | * @buf: where to put the result | |
1737 | * @cn: maximum to send along | |
1738 | * @ppos: where to start | |
1739 | * | |
1740 | * Returns number of bytes read or error code, as appropriate | |
1741 | */ | |
1742 | static ssize_t smk_read_unconfined(struct file *filp, char __user *buf, | |
1743 | size_t cn, loff_t *ppos) | |
1744 | { | |
1745 | char *smack = ""; | |
1746 | ssize_t rc = -EINVAL; | |
1747 | int asize; | |
1748 | ||
1749 | if (*ppos != 0) | |
1750 | return 0; | |
1751 | ||
1752 | if (smack_unconfined != NULL) | |
1753 | smack = smack_unconfined->smk_known; | |
1754 | ||
1755 | asize = strlen(smack) + 1; | |
1756 | ||
1757 | if (cn >= asize) | |
1758 | rc = simple_read_from_buffer(buf, cn, ppos, smack, asize); | |
1759 | ||
1760 | return rc; | |
1761 | } | |
1762 | ||
1763 | /** | |
1764 | * smk_write_unconfined - write() for smackfs/unconfined | |
1765 | * @file: file pointer, not actually used | |
1766 | * @buf: where to get the data from | |
1767 | * @count: bytes sent | |
1768 | * @ppos: where to start | |
1769 | * | |
1770 | * Returns number of bytes written or error code, as appropriate | |
1771 | */ | |
1772 | static ssize_t smk_write_unconfined(struct file *file, const char __user *buf, | |
1773 | size_t count, loff_t *ppos) | |
1774 | { | |
1775 | char *data; | |
1776 | int rc = count; | |
1777 | ||
1778 | if (!smack_privileged(CAP_MAC_ADMIN)) | |
1779 | return -EPERM; | |
1780 | ||
1781 | data = kzalloc(count + 1, GFP_KERNEL); | |
1782 | if (data == NULL) | |
1783 | return -ENOMEM; | |
1784 | ||
1785 | /* | |
1786 | * Should the null string be passed in unset the unconfined value. | |
1787 | * This seems like something to be careful with as usually | |
1788 | * smk_import only expects to return NULL for errors. It | |
1789 | * is usually the case that a nullstring or "\n" would be | |
1790 | * bad to pass to smk_import but in fact this is useful here. | |
1791 | * | |
1792 | * smk_import will also reject a label beginning with '-', | |
1793 | * so "-confine" will also work. | |
1794 | */ | |
1795 | if (copy_from_user(data, buf, count) != 0) | |
1796 | rc = -EFAULT; | |
1797 | else | |
1798 | smack_unconfined = smk_import_entry(data, count); | |
1799 | ||
1800 | kfree(data); | |
1801 | return rc; | |
1802 | } | |
1803 | ||
1804 | static const struct file_operations smk_unconfined_ops = { | |
1805 | .read = smk_read_unconfined, | |
1806 | .write = smk_write_unconfined, | |
1807 | .llseek = default_llseek, | |
1808 | }; | |
1809 | #endif /* CONFIG_SECURITY_SMACK_BRINGUP */ | |
1810 | ||
ecfcc53f EB |
1811 | /** |
1812 | * smk_read_logging - read() for /smack/logging | |
1813 | * @filp: file pointer, not actually used | |
1814 | * @buf: where to put the result | |
1815 | * @cn: maximum to send along | |
1816 | * @ppos: where to start | |
1817 | * | |
1818 | * Returns number of bytes read or error code, as appropriate | |
1819 | */ | |
1820 | static ssize_t smk_read_logging(struct file *filp, char __user *buf, | |
1821 | size_t count, loff_t *ppos) | |
1822 | { | |
1823 | char temp[32]; | |
1824 | ssize_t rc; | |
1825 | ||
1826 | if (*ppos != 0) | |
1827 | return 0; | |
1828 | ||
1829 | sprintf(temp, "%d\n", log_policy); | |
1830 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
1831 | return rc; | |
1832 | } | |
1833 | ||
1834 | /** | |
1835 | * smk_write_logging - write() for /smack/logging | |
1836 | * @file: file pointer, not actually used | |
1837 | * @buf: where to get the data from | |
1838 | * @count: bytes sent | |
1839 | * @ppos: where to start | |
1840 | * | |
1841 | * Returns number of bytes written or error code, as appropriate | |
1842 | */ | |
1843 | static ssize_t smk_write_logging(struct file *file, const char __user *buf, | |
1844 | size_t count, loff_t *ppos) | |
1845 | { | |
1846 | char temp[32]; | |
1847 | int i; | |
1848 | ||
1880eff7 | 1849 | if (!smack_privileged(CAP_MAC_ADMIN)) |
ecfcc53f EB |
1850 | return -EPERM; |
1851 | ||
1852 | if (count >= sizeof(temp) || count == 0) | |
1853 | return -EINVAL; | |
1854 | ||
1855 | if (copy_from_user(temp, buf, count) != 0) | |
1856 | return -EFAULT; | |
1857 | ||
1858 | temp[count] = '\0'; | |
1859 | ||
1860 | if (sscanf(temp, "%d", &i) != 1) | |
1861 | return -EINVAL; | |
1862 | if (i < 0 || i > 3) | |
1863 | return -EINVAL; | |
1864 | log_policy = i; | |
1865 | return count; | |
1866 | } | |
1867 | ||
1868 | ||
1869 | ||
1870 | static const struct file_operations smk_logging_ops = { | |
1871 | .read = smk_read_logging, | |
1872 | .write = smk_write_logging, | |
6038f373 | 1873 | .llseek = default_llseek, |
ecfcc53f | 1874 | }; |
7898e1f8 CS |
1875 | |
1876 | /* | |
1877 | * Seq_file read operations for /smack/load-self | |
1878 | */ | |
1879 | ||
1880 | static void *load_self_seq_start(struct seq_file *s, loff_t *pos) | |
1881 | { | |
1882 | struct task_smack *tsp = current_security(); | |
1883 | ||
40809565 | 1884 | return smk_seq_start(s, pos, &tsp->smk_rules); |
7898e1f8 CS |
1885 | } |
1886 | ||
1887 | static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
1888 | { | |
1889 | struct task_smack *tsp = current_security(); | |
7898e1f8 | 1890 | |
40809565 | 1891 | return smk_seq_next(s, v, pos, &tsp->smk_rules); |
7898e1f8 CS |
1892 | } |
1893 | ||
1894 | static int load_self_seq_show(struct seq_file *s, void *v) | |
1895 | { | |
1896 | struct list_head *list = v; | |
1897 | struct smack_rule *srp = | |
1898 | list_entry(list, struct smack_rule, list); | |
1899 | ||
f7112e6c | 1900 | smk_rule_show(s, srp, SMK_LABELLEN); |
7898e1f8 CS |
1901 | |
1902 | return 0; | |
1903 | } | |
1904 | ||
7898e1f8 CS |
1905 | static const struct seq_operations load_self_seq_ops = { |
1906 | .start = load_self_seq_start, | |
1907 | .next = load_self_seq_next, | |
1908 | .show = load_self_seq_show, | |
40809565 | 1909 | .stop = smk_seq_stop, |
7898e1f8 CS |
1910 | }; |
1911 | ||
1912 | ||
1913 | /** | |
f7112e6c | 1914 | * smk_open_load_self - open() for /smack/load-self2 |
7898e1f8 CS |
1915 | * @inode: inode structure representing file |
1916 | * @file: "load" file pointer | |
1917 | * | |
1918 | * For reading, use load_seq_* seq_file reading operations. | |
1919 | */ | |
1920 | static int smk_open_load_self(struct inode *inode, struct file *file) | |
1921 | { | |
1922 | return seq_open(file, &load_self_seq_ops); | |
1923 | } | |
1924 | ||
1925 | /** | |
1926 | * smk_write_load_self - write() for /smack/load-self | |
1927 | * @file: file pointer, not actually used | |
1928 | * @buf: where to get the data from | |
1929 | * @count: bytes sent | |
1930 | * @ppos: where to start - must be 0 | |
1931 | * | |
1932 | */ | |
1933 | static ssize_t smk_write_load_self(struct file *file, const char __user *buf, | |
1934 | size_t count, loff_t *ppos) | |
1935 | { | |
1936 | struct task_smack *tsp = current_security(); | |
1937 | ||
f7112e6c CS |
1938 | return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules, |
1939 | &tsp->smk_rules_lock, SMK_FIXED24_FMT); | |
7898e1f8 CS |
1940 | } |
1941 | ||
1942 | static const struct file_operations smk_load_self_ops = { | |
1943 | .open = smk_open_load_self, | |
1944 | .read = seq_read, | |
1945 | .llseek = seq_lseek, | |
1946 | .write = smk_write_load_self, | |
1947 | .release = seq_release, | |
1948 | }; | |
828716c2 JS |
1949 | |
1950 | /** | |
f7112e6c | 1951 | * smk_user_access - handle access check transaction |
828716c2 JS |
1952 | * @file: file pointer |
1953 | * @buf: data from user space | |
1954 | * @count: bytes sent | |
1955 | * @ppos: where to start - must be 0 | |
1956 | */ | |
f7112e6c CS |
1957 | static ssize_t smk_user_access(struct file *file, const char __user *buf, |
1958 | size_t count, loff_t *ppos, int format) | |
828716c2 | 1959 | { |
e05b6f98 | 1960 | struct smack_parsed_rule rule; |
828716c2 | 1961 | char *data; |
f8859d98 | 1962 | int res; |
828716c2 | 1963 | |
828716c2 JS |
1964 | data = simple_transaction_get(file, buf, count); |
1965 | if (IS_ERR(data)) | |
1966 | return PTR_ERR(data); | |
1967 | ||
f7112e6c CS |
1968 | if (format == SMK_FIXED24_FMT) { |
1969 | if (count < SMK_LOADLEN) | |
1970 | return -EINVAL; | |
1971 | res = smk_parse_rule(data, &rule, 0); | |
1972 | } else { | |
1973 | /* | |
10289b0f | 1974 | * simple_transaction_get() returns null-terminated data |
f7112e6c | 1975 | */ |
10289b0f | 1976 | res = smk_parse_long_rule(data, &rule, 0, 3); |
f7112e6c CS |
1977 | } |
1978 | ||
398ce073 JS |
1979 | if (res >= 0) |
1980 | res = smk_access(rule.smk_subject, rule.smk_object, | |
1981 | rule.smk_access1, NULL); | |
1982 | else if (res != -ENOENT) | |
828716c2 JS |
1983 | return -EINVAL; |
1984 | ||
d166c802 CS |
1985 | /* |
1986 | * smk_access() can return a value > 0 in the "bringup" case. | |
1987 | */ | |
1988 | data[0] = res >= 0 ? '1' : '0'; | |
f8859d98 | 1989 | data[1] = '\0'; |
828716c2 | 1990 | |
d86b2b61 | 1991 | simple_transaction_set(file, 2); |
f7112e6c CS |
1992 | |
1993 | if (format == SMK_FIXED24_FMT) | |
1994 | return SMK_LOADLEN; | |
1995 | return count; | |
1996 | } | |
1997 | ||
1998 | /** | |
1999 | * smk_write_access - handle access check transaction | |
2000 | * @file: file pointer | |
2001 | * @buf: data from user space | |
2002 | * @count: bytes sent | |
2003 | * @ppos: where to start - must be 0 | |
2004 | */ | |
2005 | static ssize_t smk_write_access(struct file *file, const char __user *buf, | |
2006 | size_t count, loff_t *ppos) | |
2007 | { | |
2008 | return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT); | |
828716c2 JS |
2009 | } |
2010 | ||
2011 | static const struct file_operations smk_access_ops = { | |
2012 | .write = smk_write_access, | |
2013 | .read = simple_transaction_read, | |
2014 | .release = simple_transaction_release, | |
2015 | .llseek = generic_file_llseek, | |
2016 | }; | |
2017 | ||
f7112e6c CS |
2018 | |
2019 | /* | |
2020 | * Seq_file read operations for /smack/load2 | |
2021 | */ | |
2022 | ||
2023 | static int load2_seq_show(struct seq_file *s, void *v) | |
2024 | { | |
2025 | struct list_head *list = v; | |
2026 | struct smack_master_list *smlp = | |
2027 | list_entry(list, struct smack_master_list, list); | |
2028 | ||
2029 | smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL); | |
2030 | ||
2031 | return 0; | |
2032 | } | |
2033 | ||
2034 | static const struct seq_operations load2_seq_ops = { | |
2035 | .start = load2_seq_start, | |
2036 | .next = load2_seq_next, | |
2037 | .show = load2_seq_show, | |
2038 | .stop = smk_seq_stop, | |
2039 | }; | |
2040 | ||
2041 | /** | |
2042 | * smk_open_load2 - open() for /smack/load2 | |
2043 | * @inode: inode structure representing file | |
2044 | * @file: "load2" file pointer | |
2045 | * | |
2046 | * For reading, use load2_seq_* seq_file reading operations. | |
2047 | */ | |
2048 | static int smk_open_load2(struct inode *inode, struct file *file) | |
2049 | { | |
2050 | return seq_open(file, &load2_seq_ops); | |
2051 | } | |
2052 | ||
2053 | /** | |
2054 | * smk_write_load2 - write() for /smack/load2 | |
2055 | * @file: file pointer, not actually used | |
2056 | * @buf: where to get the data from | |
2057 | * @count: bytes sent | |
2058 | * @ppos: where to start - must be 0 | |
2059 | * | |
2060 | */ | |
2061 | static ssize_t smk_write_load2(struct file *file, const char __user *buf, | |
2062 | size_t count, loff_t *ppos) | |
2063 | { | |
2064 | /* | |
2065 | * Must have privilege. | |
2066 | */ | |
1880eff7 | 2067 | if (!smack_privileged(CAP_MAC_ADMIN)) |
f7112e6c CS |
2068 | return -EPERM; |
2069 | ||
2070 | return smk_write_rules_list(file, buf, count, ppos, NULL, NULL, | |
2071 | SMK_LONG_FMT); | |
2072 | } | |
2073 | ||
2074 | static const struct file_operations smk_load2_ops = { | |
2075 | .open = smk_open_load2, | |
2076 | .read = seq_read, | |
2077 | .llseek = seq_lseek, | |
2078 | .write = smk_write_load2, | |
2079 | .release = seq_release, | |
2080 | }; | |
2081 | ||
2082 | /* | |
2083 | * Seq_file read operations for /smack/load-self2 | |
2084 | */ | |
2085 | ||
2086 | static void *load_self2_seq_start(struct seq_file *s, loff_t *pos) | |
2087 | { | |
2088 | struct task_smack *tsp = current_security(); | |
2089 | ||
2090 | return smk_seq_start(s, pos, &tsp->smk_rules); | |
2091 | } | |
2092 | ||
2093 | static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
2094 | { | |
2095 | struct task_smack *tsp = current_security(); | |
2096 | ||
2097 | return smk_seq_next(s, v, pos, &tsp->smk_rules); | |
2098 | } | |
2099 | ||
2100 | static int load_self2_seq_show(struct seq_file *s, void *v) | |
2101 | { | |
2102 | struct list_head *list = v; | |
2103 | struct smack_rule *srp = | |
2104 | list_entry(list, struct smack_rule, list); | |
2105 | ||
2106 | smk_rule_show(s, srp, SMK_LONGLABEL); | |
2107 | ||
2108 | return 0; | |
2109 | } | |
2110 | ||
2111 | static const struct seq_operations load_self2_seq_ops = { | |
2112 | .start = load_self2_seq_start, | |
2113 | .next = load_self2_seq_next, | |
2114 | .show = load_self2_seq_show, | |
2115 | .stop = smk_seq_stop, | |
2116 | }; | |
2117 | ||
2118 | /** | |
2119 | * smk_open_load_self2 - open() for /smack/load-self2 | |
2120 | * @inode: inode structure representing file | |
2121 | * @file: "load" file pointer | |
2122 | * | |
2123 | * For reading, use load_seq_* seq_file reading operations. | |
2124 | */ | |
2125 | static int smk_open_load_self2(struct inode *inode, struct file *file) | |
2126 | { | |
2127 | return seq_open(file, &load_self2_seq_ops); | |
2128 | } | |
2129 | ||
2130 | /** | |
2131 | * smk_write_load_self2 - write() for /smack/load-self2 | |
2132 | * @file: file pointer, not actually used | |
2133 | * @buf: where to get the data from | |
2134 | * @count: bytes sent | |
2135 | * @ppos: where to start - must be 0 | |
2136 | * | |
2137 | */ | |
2138 | static ssize_t smk_write_load_self2(struct file *file, const char __user *buf, | |
2139 | size_t count, loff_t *ppos) | |
2140 | { | |
2141 | struct task_smack *tsp = current_security(); | |
2142 | ||
2143 | return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules, | |
2144 | &tsp->smk_rules_lock, SMK_LONG_FMT); | |
2145 | } | |
2146 | ||
2147 | static const struct file_operations smk_load_self2_ops = { | |
2148 | .open = smk_open_load_self2, | |
2149 | .read = seq_read, | |
2150 | .llseek = seq_lseek, | |
2151 | .write = smk_write_load_self2, | |
2152 | .release = seq_release, | |
2153 | }; | |
2154 | ||
2155 | /** | |
2156 | * smk_write_access2 - handle access check transaction | |
2157 | * @file: file pointer | |
2158 | * @buf: data from user space | |
2159 | * @count: bytes sent | |
2160 | * @ppos: where to start - must be 0 | |
2161 | */ | |
2162 | static ssize_t smk_write_access2(struct file *file, const char __user *buf, | |
2163 | size_t count, loff_t *ppos) | |
2164 | { | |
2165 | return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT); | |
2166 | } | |
2167 | ||
2168 | static const struct file_operations smk_access2_ops = { | |
2169 | .write = smk_write_access2, | |
2170 | .read = simple_transaction_read, | |
2171 | .release = simple_transaction_release, | |
2172 | .llseek = generic_file_llseek, | |
2173 | }; | |
2174 | ||
449543b0 RK |
2175 | /** |
2176 | * smk_write_revoke_subj - write() for /smack/revoke-subject | |
2177 | * @file: file pointer | |
2178 | * @buf: data from user space | |
2179 | * @count: bytes sent | |
2180 | * @ppos: where to start - must be 0 | |
2181 | */ | |
2182 | static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf, | |
2183 | size_t count, loff_t *ppos) | |
2184 | { | |
2185 | char *data = NULL; | |
2186 | const char *cp = NULL; | |
2187 | struct smack_known *skp; | |
2188 | struct smack_rule *sp; | |
2189 | struct list_head *rule_list; | |
2190 | struct mutex *rule_lock; | |
2191 | int rc = count; | |
2192 | ||
2193 | if (*ppos != 0) | |
2194 | return -EINVAL; | |
2195 | ||
2196 | if (!smack_privileged(CAP_MAC_ADMIN)) | |
2197 | return -EPERM; | |
2198 | ||
2199 | if (count == 0 || count > SMK_LONGLABEL) | |
2200 | return -EINVAL; | |
2201 | ||
2202 | data = kzalloc(count, GFP_KERNEL); | |
2203 | if (data == NULL) | |
2204 | return -ENOMEM; | |
2205 | ||
2206 | if (copy_from_user(data, buf, count) != 0) { | |
2207 | rc = -EFAULT; | |
2208 | goto free_out; | |
2209 | } | |
2210 | ||
2211 | cp = smk_parse_smack(data, count); | |
2212 | if (cp == NULL) { | |
2213 | rc = -EINVAL; | |
2214 | goto free_out; | |
2215 | } | |
2216 | ||
2217 | skp = smk_find_entry(cp); | |
d15d9fad | 2218 | if (skp == NULL) |
449543b0 | 2219 | goto free_out; |
449543b0 RK |
2220 | |
2221 | rule_list = &skp->smk_rules; | |
2222 | rule_lock = &skp->smk_rules_lock; | |
2223 | ||
2224 | mutex_lock(rule_lock); | |
2225 | ||
2226 | list_for_each_entry_rcu(sp, rule_list, list) | |
2227 | sp->smk_access = 0; | |
2228 | ||
2229 | mutex_unlock(rule_lock); | |
2230 | ||
2231 | free_out: | |
2232 | kfree(data); | |
2233 | kfree(cp); | |
2234 | return rc; | |
2235 | } | |
2236 | ||
2237 | static const struct file_operations smk_revoke_subj_ops = { | |
2238 | .write = smk_write_revoke_subj, | |
2239 | .read = simple_transaction_read, | |
2240 | .release = simple_transaction_release, | |
2241 | .llseek = generic_file_llseek, | |
2242 | }; | |
2243 | ||
e9307237 CS |
2244 | static struct kset *smackfs_kset; |
2245 | /** | |
2246 | * smk_init_sysfs - initialize /sys/fs/smackfs | |
2247 | * | |
2248 | */ | |
2249 | static int smk_init_sysfs(void) | |
2250 | { | |
2251 | smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj); | |
2252 | if (!smackfs_kset) | |
2253 | return -ENOMEM; | |
2254 | return 0; | |
2255 | } | |
2256 | ||
e05b6f98 RK |
2257 | /** |
2258 | * smk_write_change_rule - write() for /smack/change-rule | |
2259 | * @file: file pointer | |
2260 | * @buf: data from user space | |
2261 | * @count: bytes sent | |
2262 | * @ppos: where to start - must be 0 | |
2263 | */ | |
2264 | static ssize_t smk_write_change_rule(struct file *file, const char __user *buf, | |
2265 | size_t count, loff_t *ppos) | |
2266 | { | |
2267 | /* | |
2268 | * Must have privilege. | |
2269 | */ | |
4afde48b | 2270 | if (!smack_privileged(CAP_MAC_ADMIN)) |
e05b6f98 RK |
2271 | return -EPERM; |
2272 | ||
2273 | return smk_write_rules_list(file, buf, count, ppos, NULL, NULL, | |
2274 | SMK_CHANGE_FMT); | |
2275 | } | |
2276 | ||
2277 | static const struct file_operations smk_change_rule_ops = { | |
2278 | .write = smk_write_change_rule, | |
2279 | .read = simple_transaction_read, | |
2280 | .release = simple_transaction_release, | |
2281 | .llseek = generic_file_llseek, | |
2282 | }; | |
2283 | ||
e114e473 | 2284 | /** |
00f84f3f CS |
2285 | * smk_read_syslog - read() for smackfs/syslog |
2286 | * @filp: file pointer, not actually used | |
2287 | * @buf: where to put the result | |
2288 | * @cn: maximum to send along | |
2289 | * @ppos: where to start | |
2290 | * | |
2291 | * Returns number of bytes read or error code, as appropriate | |
2292 | */ | |
2293 | static ssize_t smk_read_syslog(struct file *filp, char __user *buf, | |
2294 | size_t cn, loff_t *ppos) | |
2295 | { | |
2296 | struct smack_known *skp; | |
2297 | ssize_t rc = -EINVAL; | |
2298 | int asize; | |
2299 | ||
2300 | if (*ppos != 0) | |
2301 | return 0; | |
2302 | ||
2303 | if (smack_syslog_label == NULL) | |
2304 | skp = &smack_known_star; | |
2305 | else | |
2306 | skp = smack_syslog_label; | |
2307 | ||
2308 | asize = strlen(skp->smk_known) + 1; | |
2309 | ||
2310 | if (cn >= asize) | |
2311 | rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known, | |
2312 | asize); | |
2313 | ||
2314 | return rc; | |
2315 | } | |
2316 | ||
2317 | /** | |
2318 | * smk_write_syslog - write() for smackfs/syslog | |
2319 | * @file: file pointer, not actually used | |
2320 | * @buf: where to get the data from | |
2321 | * @count: bytes sent | |
2322 | * @ppos: where to start | |
2323 | * | |
2324 | * Returns number of bytes written or error code, as appropriate | |
2325 | */ | |
2326 | static ssize_t smk_write_syslog(struct file *file, const char __user *buf, | |
2327 | size_t count, loff_t *ppos) | |
2328 | { | |
2329 | char *data; | |
2330 | struct smack_known *skp; | |
2331 | int rc = count; | |
2332 | ||
2333 | if (!smack_privileged(CAP_MAC_ADMIN)) | |
2334 | return -EPERM; | |
2335 | ||
b862e561 | 2336 | data = kzalloc(count + 1, GFP_KERNEL); |
00f84f3f CS |
2337 | if (data == NULL) |
2338 | return -ENOMEM; | |
2339 | ||
2340 | if (copy_from_user(data, buf, count) != 0) | |
2341 | rc = -EFAULT; | |
2342 | else { | |
2343 | skp = smk_import_entry(data, count); | |
2344 | if (skp == NULL) | |
2345 | rc = -EINVAL; | |
2346 | else | |
2347 | smack_syslog_label = smk_import_entry(data, count); | |
2348 | } | |
2349 | ||
2350 | kfree(data); | |
2351 | return rc; | |
2352 | } | |
2353 | ||
2354 | static const struct file_operations smk_syslog_ops = { | |
2355 | .read = smk_read_syslog, | |
2356 | .write = smk_write_syslog, | |
2357 | .llseek = default_llseek, | |
2358 | }; | |
2359 | ||
2360 | ||
66867818 LP |
2361 | /** |
2362 | * smk_read_ptrace - read() for /smack/ptrace | |
2363 | * @filp: file pointer, not actually used | |
2364 | * @buf: where to put the result | |
2365 | * @count: maximum to send along | |
2366 | * @ppos: where to start | |
2367 | * | |
2368 | * Returns number of bytes read or error code, as appropriate | |
2369 | */ | |
2370 | static ssize_t smk_read_ptrace(struct file *filp, char __user *buf, | |
2371 | size_t count, loff_t *ppos) | |
2372 | { | |
2373 | char temp[32]; | |
2374 | ssize_t rc; | |
2375 | ||
2376 | if (*ppos != 0) | |
2377 | return 0; | |
2378 | ||
2379 | sprintf(temp, "%d\n", smack_ptrace_rule); | |
2380 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); | |
2381 | return rc; | |
2382 | } | |
2383 | ||
2384 | /** | |
2385 | * smk_write_ptrace - write() for /smack/ptrace | |
2386 | * @file: file pointer | |
2387 | * @buf: data from user space | |
2388 | * @count: bytes sent | |
2389 | * @ppos: where to start - must be 0 | |
2390 | */ | |
2391 | static ssize_t smk_write_ptrace(struct file *file, const char __user *buf, | |
2392 | size_t count, loff_t *ppos) | |
2393 | { | |
2394 | char temp[32]; | |
2395 | int i; | |
2396 | ||
2397 | if (!smack_privileged(CAP_MAC_ADMIN)) | |
2398 | return -EPERM; | |
2399 | ||
2400 | if (*ppos != 0 || count >= sizeof(temp) || count == 0) | |
2401 | return -EINVAL; | |
2402 | ||
2403 | if (copy_from_user(temp, buf, count) != 0) | |
2404 | return -EFAULT; | |
2405 | ||
2406 | temp[count] = '\0'; | |
2407 | ||
2408 | if (sscanf(temp, "%d", &i) != 1) | |
2409 | return -EINVAL; | |
2410 | if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX) | |
2411 | return -EINVAL; | |
2412 | smack_ptrace_rule = i; | |
2413 | ||
2414 | return count; | |
2415 | } | |
2416 | ||
2417 | static const struct file_operations smk_ptrace_ops = { | |
2418 | .write = smk_write_ptrace, | |
2419 | .read = smk_read_ptrace, | |
2420 | .llseek = default_llseek, | |
2421 | }; | |
2422 | ||
00f84f3f CS |
2423 | /** |
2424 | * smk_fill_super - fill the smackfs superblock | |
e114e473 CS |
2425 | * @sb: the empty superblock |
2426 | * @data: unused | |
2427 | * @silent: unused | |
2428 | * | |
00f84f3f | 2429 | * Fill in the well known entries for the smack filesystem |
e114e473 CS |
2430 | * |
2431 | * Returns 0 on success, an error code on failure | |
2432 | */ | |
2433 | static int smk_fill_super(struct super_block *sb, void *data, int silent) | |
2434 | { | |
2435 | int rc; | |
2436 | struct inode *root_inode; | |
2437 | ||
2438 | static struct tree_descr smack_files[] = { | |
7898e1f8 CS |
2439 | [SMK_LOAD] = { |
2440 | "load", &smk_load_ops, S_IRUGO|S_IWUSR}, | |
2441 | [SMK_CIPSO] = { | |
2442 | "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR}, | |
2443 | [SMK_DOI] = { | |
2444 | "doi", &smk_doi_ops, S_IRUGO|S_IWUSR}, | |
2445 | [SMK_DIRECT] = { | |
2446 | "direct", &smk_direct_ops, S_IRUGO|S_IWUSR}, | |
2447 | [SMK_AMBIENT] = { | |
2448 | "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR}, | |
2449 | [SMK_NETLBLADDR] = { | |
2450 | "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR}, | |
2451 | [SMK_ONLYCAP] = { | |
2452 | "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR}, | |
2453 | [SMK_LOGGING] = { | |
2454 | "logging", &smk_logging_ops, S_IRUGO|S_IWUSR}, | |
2455 | [SMK_LOAD_SELF] = { | |
2456 | "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO}, | |
828716c2 | 2457 | [SMK_ACCESSES] = { |
0e94ae17 | 2458 | "access", &smk_access_ops, S_IRUGO|S_IWUGO}, |
f7112e6c CS |
2459 | [SMK_MAPPED] = { |
2460 | "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR}, | |
2461 | [SMK_LOAD2] = { | |
2462 | "load2", &smk_load2_ops, S_IRUGO|S_IWUSR}, | |
2463 | [SMK_LOAD_SELF2] = { | |
2464 | "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO}, | |
2465 | [SMK_ACCESS2] = { | |
2466 | "access2", &smk_access2_ops, S_IRUGO|S_IWUGO}, | |
2467 | [SMK_CIPSO2] = { | |
2468 | "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR}, | |
449543b0 RK |
2469 | [SMK_REVOKE_SUBJ] = { |
2470 | "revoke-subject", &smk_revoke_subj_ops, | |
2471 | S_IRUGO|S_IWUSR}, | |
e05b6f98 RK |
2472 | [SMK_CHANGE_RULE] = { |
2473 | "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR}, | |
00f84f3f CS |
2474 | [SMK_SYSLOG] = { |
2475 | "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR}, | |
66867818 LP |
2476 | [SMK_PTRACE] = { |
2477 | "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR}, | |
bf4b2fee CS |
2478 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP |
2479 | [SMK_UNCONFINED] = { | |
2480 | "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR}, | |
2481 | #endif | |
7898e1f8 CS |
2482 | /* last one */ |
2483 | {""} | |
e114e473 CS |
2484 | }; |
2485 | ||
2486 | rc = simple_fill_super(sb, SMACK_MAGIC, smack_files); | |
2487 | if (rc != 0) { | |
2488 | printk(KERN_ERR "%s failed %d while creating inodes\n", | |
2489 | __func__, rc); | |
2490 | return rc; | |
2491 | } | |
2492 | ||
2493 | root_inode = sb->s_root->d_inode; | |
e114e473 CS |
2494 | |
2495 | return 0; | |
2496 | } | |
2497 | ||
2498 | /** | |
fc14f2fe | 2499 | * smk_mount - get the smackfs superblock |
e114e473 CS |
2500 | * @fs_type: passed along without comment |
2501 | * @flags: passed along without comment | |
2502 | * @dev_name: passed along without comment | |
2503 | * @data: passed along without comment | |
e114e473 CS |
2504 | * |
2505 | * Just passes everything along. | |
2506 | * | |
2507 | * Returns what the lower level code does. | |
2508 | */ | |
fc14f2fe AV |
2509 | static struct dentry *smk_mount(struct file_system_type *fs_type, |
2510 | int flags, const char *dev_name, void *data) | |
e114e473 | 2511 | { |
fc14f2fe | 2512 | return mount_single(fs_type, flags, data, smk_fill_super); |
e114e473 CS |
2513 | } |
2514 | ||
2515 | static struct file_system_type smk_fs_type = { | |
2516 | .name = "smackfs", | |
fc14f2fe | 2517 | .mount = smk_mount, |
e114e473 CS |
2518 | .kill_sb = kill_litter_super, |
2519 | }; | |
2520 | ||
2521 | static struct vfsmount *smackfs_mount; | |
2522 | ||
f7112e6c CS |
2523 | static int __init smk_preset_netlabel(struct smack_known *skp) |
2524 | { | |
2525 | skp->smk_netlabel.domain = skp->smk_known; | |
2526 | skp->smk_netlabel.flags = | |
2527 | NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL; | |
2528 | return smk_netlbl_mls(smack_cipso_direct, skp->smk_known, | |
2529 | &skp->smk_netlabel, strlen(skp->smk_known)); | |
2530 | } | |
2531 | ||
e114e473 CS |
2532 | /** |
2533 | * init_smk_fs - get the smackfs superblock | |
2534 | * | |
2535 | * register the smackfs | |
2536 | * | |
076c54c5 AD |
2537 | * Do not register smackfs if Smack wasn't enabled |
2538 | * on boot. We can not put this method normally under the | |
2539 | * smack_init() code path since the security subsystem get | |
2540 | * initialized before the vfs caches. | |
2541 | * | |
2542 | * Returns true if we were not chosen on boot or if | |
2543 | * we were chosen and filesystem registration succeeded. | |
e114e473 CS |
2544 | */ |
2545 | static int __init init_smk_fs(void) | |
2546 | { | |
2547 | int err; | |
f7112e6c | 2548 | int rc; |
e114e473 | 2549 | |
076c54c5 AD |
2550 | if (!security_module_enable(&smack_ops)) |
2551 | return 0; | |
2552 | ||
e9307237 CS |
2553 | err = smk_init_sysfs(); |
2554 | if (err) | |
2555 | printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n"); | |
2556 | ||
e114e473 CS |
2557 | err = register_filesystem(&smk_fs_type); |
2558 | if (!err) { | |
2559 | smackfs_mount = kern_mount(&smk_fs_type); | |
2560 | if (IS_ERR(smackfs_mount)) { | |
2561 | printk(KERN_ERR "smackfs: could not mount!\n"); | |
2562 | err = PTR_ERR(smackfs_mount); | |
2563 | smackfs_mount = NULL; | |
2564 | } | |
2565 | } | |
2566 | ||
e114e473 | 2567 | smk_cipso_doi(); |
4bc87e62 | 2568 | smk_unlbl_ambient(NULL); |
e114e473 | 2569 | |
f7112e6c CS |
2570 | rc = smk_preset_netlabel(&smack_known_floor); |
2571 | if (err == 0 && rc < 0) | |
2572 | err = rc; | |
2573 | rc = smk_preset_netlabel(&smack_known_hat); | |
2574 | if (err == 0 && rc < 0) | |
2575 | err = rc; | |
2576 | rc = smk_preset_netlabel(&smack_known_huh); | |
2577 | if (err == 0 && rc < 0) | |
2578 | err = rc; | |
2579 | rc = smk_preset_netlabel(&smack_known_invalid); | |
2580 | if (err == 0 && rc < 0) | |
2581 | err = rc; | |
2582 | rc = smk_preset_netlabel(&smack_known_star); | |
2583 | if (err == 0 && rc < 0) | |
2584 | err = rc; | |
2585 | rc = smk_preset_netlabel(&smack_known_web); | |
2586 | if (err == 0 && rc < 0) | |
2587 | err = rc; | |
2588 | ||
e114e473 CS |
2589 | return err; |
2590 | } | |
2591 | ||
2592 | __initcall(init_smk_fs); |