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