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