]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Simplified MAC Kernel (smack) security module | |
3 | * | |
4 | * This file contains the smack hook function implementations. | |
5 | * | |
6 | * Authors: | |
7 | * Casey Schaufler <casey@schaufler-ca.com> | |
8 | * Jarkko Sakkinen <jarkko.sakkinen@intel.com> | |
9 | * | |
10 | * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com> | |
11 | * Copyright (C) 2009 Hewlett-Packard Development Company, L.P. | |
12 | * Paul Moore <paul@paul-moore.com> | |
13 | * Copyright (C) 2010 Nokia Corporation | |
14 | * Copyright (C) 2011 Intel Corporation. | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License version 2, | |
18 | * as published by the Free Software Foundation. | |
19 | */ | |
20 | ||
21 | #include <linux/xattr.h> | |
22 | #include <linux/pagemap.h> | |
23 | #include <linux/mount.h> | |
24 | #include <linux/stat.h> | |
25 | #include <linux/kd.h> | |
26 | #include <asm/ioctls.h> | |
27 | #include <linux/ip.h> | |
28 | #include <linux/tcp.h> | |
29 | #include <linux/udp.h> | |
30 | #include <linux/dccp.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/mutex.h> | |
33 | #include <linux/pipe_fs_i.h> | |
34 | #include <net/cipso_ipv4.h> | |
35 | #include <net/ip.h> | |
36 | #include <net/ipv6.h> | |
37 | #include <linux/audit.h> | |
38 | #include <linux/magic.h> | |
39 | #include <linux/dcache.h> | |
40 | #include <linux/personality.h> | |
41 | #include <linux/msg.h> | |
42 | #include <linux/shm.h> | |
43 | #include <linux/binfmts.h> | |
44 | #include <linux/parser.h> | |
45 | #include "smack.h" | |
46 | ||
47 | #define TRANS_TRUE "TRUE" | |
48 | #define TRANS_TRUE_SIZE 4 | |
49 | ||
50 | #define SMK_CONNECTING 0 | |
51 | #define SMK_RECEIVING 1 | |
52 | #define SMK_SENDING 2 | |
53 | ||
54 | #ifdef SMACK_IPV6_PORT_LABELING | |
55 | DEFINE_MUTEX(smack_ipv6_lock); | |
56 | static LIST_HEAD(smk_ipv6_port_list); | |
57 | #endif | |
58 | static struct kmem_cache *smack_inode_cache; | |
59 | int smack_enabled; | |
60 | ||
61 | static const match_table_t smk_mount_tokens = { | |
62 | {Opt_fsdefault, SMK_FSDEFAULT "%s"}, | |
63 | {Opt_fsfloor, SMK_FSFLOOR "%s"}, | |
64 | {Opt_fshat, SMK_FSHAT "%s"}, | |
65 | {Opt_fsroot, SMK_FSROOT "%s"}, | |
66 | {Opt_fstransmute, SMK_FSTRANS "%s"}, | |
67 | {Opt_error, NULL}, | |
68 | }; | |
69 | ||
70 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP | |
71 | static char *smk_bu_mess[] = { | |
72 | "Bringup Error", /* Unused */ | |
73 | "Bringup", /* SMACK_BRINGUP_ALLOW */ | |
74 | "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */ | |
75 | "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */ | |
76 | }; | |
77 | ||
78 | static void smk_bu_mode(int mode, char *s) | |
79 | { | |
80 | int i = 0; | |
81 | ||
82 | if (mode & MAY_READ) | |
83 | s[i++] = 'r'; | |
84 | if (mode & MAY_WRITE) | |
85 | s[i++] = 'w'; | |
86 | if (mode & MAY_EXEC) | |
87 | s[i++] = 'x'; | |
88 | if (mode & MAY_APPEND) | |
89 | s[i++] = 'a'; | |
90 | if (mode & MAY_TRANSMUTE) | |
91 | s[i++] = 't'; | |
92 | if (mode & MAY_LOCK) | |
93 | s[i++] = 'l'; | |
94 | if (i == 0) | |
95 | s[i++] = '-'; | |
96 | s[i] = '\0'; | |
97 | } | |
98 | #endif | |
99 | ||
100 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP | |
101 | static int smk_bu_note(char *note, struct smack_known *sskp, | |
102 | struct smack_known *oskp, int mode, int rc) | |
103 | { | |
104 | char acc[SMK_NUM_ACCESS_TYPE + 1]; | |
105 | ||
106 | if (rc <= 0) | |
107 | return rc; | |
108 | if (rc > SMACK_UNCONFINED_OBJECT) | |
109 | rc = 0; | |
110 | ||
111 | smk_bu_mode(mode, acc); | |
112 | pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc], | |
113 | sskp->smk_known, oskp->smk_known, acc, note); | |
114 | return 0; | |
115 | } | |
116 | #else | |
117 | #define smk_bu_note(note, sskp, oskp, mode, RC) (RC) | |
118 | #endif | |
119 | ||
120 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP | |
121 | static int smk_bu_current(char *note, struct smack_known *oskp, | |
122 | int mode, int rc) | |
123 | { | |
124 | struct task_smack *tsp = smack_cred(current_cred()); | |
125 | char acc[SMK_NUM_ACCESS_TYPE + 1]; | |
126 | ||
127 | if (rc <= 0) | |
128 | return rc; | |
129 | if (rc > SMACK_UNCONFINED_OBJECT) | |
130 | rc = 0; | |
131 | ||
132 | smk_bu_mode(mode, acc); | |
133 | pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc], | |
134 | tsp->smk_task->smk_known, oskp->smk_known, | |
135 | acc, current->comm, note); | |
136 | return 0; | |
137 | } | |
138 | #else | |
139 | #define smk_bu_current(note, oskp, mode, RC) (RC) | |
140 | #endif | |
141 | ||
142 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP | |
143 | static int smk_bu_task(struct task_struct *otp, int mode, int rc) | |
144 | { | |
145 | struct task_smack *tsp = smack_cred(current_cred()); | |
146 | struct smack_known *smk_task = smk_of_task_struct(otp); | |
147 | char acc[SMK_NUM_ACCESS_TYPE + 1]; | |
148 | ||
149 | if (rc <= 0) | |
150 | return rc; | |
151 | if (rc > SMACK_UNCONFINED_OBJECT) | |
152 | rc = 0; | |
153 | ||
154 | smk_bu_mode(mode, acc); | |
155 | pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc], | |
156 | tsp->smk_task->smk_known, smk_task->smk_known, acc, | |
157 | current->comm, otp->comm); | |
158 | return 0; | |
159 | } | |
160 | #else | |
161 | #define smk_bu_task(otp, mode, RC) (RC) | |
162 | #endif | |
163 | ||
164 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP | |
165 | static int smk_bu_inode(struct inode *inode, int mode, int rc) | |
166 | { | |
167 | struct task_smack *tsp = smack_cred(current_cred()); | |
168 | struct inode_smack *isp = smack_inode(inode); | |
169 | char acc[SMK_NUM_ACCESS_TYPE + 1]; | |
170 | ||
171 | if (isp->smk_flags & SMK_INODE_IMPURE) | |
172 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", | |
173 | inode->i_sb->s_id, inode->i_ino, current->comm); | |
174 | ||
175 | if (rc <= 0) | |
176 | return rc; | |
177 | if (rc > SMACK_UNCONFINED_OBJECT) | |
178 | rc = 0; | |
179 | if (rc == SMACK_UNCONFINED_SUBJECT && | |
180 | (mode & (MAY_WRITE | MAY_APPEND))) | |
181 | isp->smk_flags |= SMK_INODE_IMPURE; | |
182 | ||
183 | smk_bu_mode(mode, acc); | |
184 | ||
185 | pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc], | |
186 | tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc, | |
187 | inode->i_sb->s_id, inode->i_ino, current->comm); | |
188 | return 0; | |
189 | } | |
190 | #else | |
191 | #define smk_bu_inode(inode, mode, RC) (RC) | |
192 | #endif | |
193 | ||
194 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP | |
195 | static int smk_bu_file(struct file *file, int mode, int rc) | |
196 | { | |
197 | struct task_smack *tsp = smack_cred(current_cred()); | |
198 | struct smack_known *sskp = tsp->smk_task; | |
199 | struct inode *inode = file_inode(file); | |
200 | struct inode_smack *isp = smack_inode(inode); | |
201 | char acc[SMK_NUM_ACCESS_TYPE + 1]; | |
202 | ||
203 | if (isp->smk_flags & SMK_INODE_IMPURE) | |
204 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", | |
205 | inode->i_sb->s_id, inode->i_ino, current->comm); | |
206 | ||
207 | if (rc <= 0) | |
208 | return rc; | |
209 | if (rc > SMACK_UNCONFINED_OBJECT) | |
210 | rc = 0; | |
211 | ||
212 | smk_bu_mode(mode, acc); | |
213 | pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc], | |
214 | sskp->smk_known, smk_of_inode(inode)->smk_known, acc, | |
215 | inode->i_sb->s_id, inode->i_ino, file, | |
216 | current->comm); | |
217 | return 0; | |
218 | } | |
219 | #else | |
220 | #define smk_bu_file(file, mode, RC) (RC) | |
221 | #endif | |
222 | ||
223 | #ifdef CONFIG_SECURITY_SMACK_BRINGUP | |
224 | static int smk_bu_credfile(const struct cred *cred, struct file *file, | |
225 | int mode, int rc) | |
226 | { | |
227 | struct task_smack *tsp = smack_cred(cred); | |
228 | struct smack_known *sskp = tsp->smk_task; | |
229 | struct inode *inode = file_inode(file); | |
230 | struct inode_smack *isp = smack_inode(inode); | |
231 | char acc[SMK_NUM_ACCESS_TYPE + 1]; | |
232 | ||
233 | if (isp->smk_flags & SMK_INODE_IMPURE) | |
234 | pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n", | |
235 | inode->i_sb->s_id, inode->i_ino, current->comm); | |
236 | ||
237 | if (rc <= 0) | |
238 | return rc; | |
239 | if (rc > SMACK_UNCONFINED_OBJECT) | |
240 | rc = 0; | |
241 | ||
242 | smk_bu_mode(mode, acc); | |
243 | pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc], | |
244 | sskp->smk_known, smk_of_inode(inode)->smk_known, acc, | |
245 | inode->i_sb->s_id, inode->i_ino, file, | |
246 | current->comm); | |
247 | return 0; | |
248 | } | |
249 | #else | |
250 | #define smk_bu_credfile(cred, file, mode, RC) (RC) | |
251 | #endif | |
252 | ||
253 | /** | |
254 | * smk_fetch - Fetch the smack label from a file. | |
255 | * @name: type of the label (attribute) | |
256 | * @ip: a pointer to the inode | |
257 | * @dp: a pointer to the dentry | |
258 | * | |
259 | * Returns a pointer to the master list entry for the Smack label, | |
260 | * NULL if there was no label to fetch, or an error code. | |
261 | */ | |
262 | static struct smack_known *smk_fetch(const char *name, struct inode *ip, | |
263 | struct dentry *dp) | |
264 | { | |
265 | int rc; | |
266 | char *buffer; | |
267 | struct smack_known *skp = NULL; | |
268 | ||
269 | if (!(ip->i_opflags & IOP_XATTR)) | |
270 | return ERR_PTR(-EOPNOTSUPP); | |
271 | ||
272 | buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL); | |
273 | if (buffer == NULL) | |
274 | return ERR_PTR(-ENOMEM); | |
275 | ||
276 | rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL); | |
277 | if (rc < 0) | |
278 | skp = ERR_PTR(rc); | |
279 | else if (rc == 0) | |
280 | skp = NULL; | |
281 | else | |
282 | skp = smk_import_entry(buffer, rc); | |
283 | ||
284 | kfree(buffer); | |
285 | ||
286 | return skp; | |
287 | } | |
288 | ||
289 | /** | |
290 | * init_inode_smack - initialize an inode security blob | |
291 | * @isp: the blob to initialize | |
292 | * @skp: a pointer to the Smack label entry to use in the blob | |
293 | * | |
294 | */ | |
295 | static void init_inode_smack(struct inode *inode, struct smack_known *skp) | |
296 | { | |
297 | struct inode_smack *isp = smack_inode(inode); | |
298 | ||
299 | isp->smk_inode = skp; | |
300 | isp->smk_flags = 0; | |
301 | mutex_init(&isp->smk_lock); | |
302 | } | |
303 | ||
304 | /** | |
305 | * init_task_smack - initialize a task security blob | |
306 | * @tsp: blob to initialize | |
307 | * @task: a pointer to the Smack label for the running task | |
308 | * @forked: a pointer to the Smack label for the forked task | |
309 | * | |
310 | */ | |
311 | static void init_task_smack(struct task_smack *tsp, struct smack_known *task, | |
312 | struct smack_known *forked) | |
313 | { | |
314 | tsp->smk_task = task; | |
315 | tsp->smk_forked = forked; | |
316 | INIT_LIST_HEAD(&tsp->smk_rules); | |
317 | INIT_LIST_HEAD(&tsp->smk_relabel); | |
318 | mutex_init(&tsp->smk_rules_lock); | |
319 | } | |
320 | ||
321 | /** | |
322 | * smk_copy_rules - copy a rule set | |
323 | * @nhead: new rules header pointer | |
324 | * @ohead: old rules header pointer | |
325 | * @gfp: type of the memory for the allocation | |
326 | * | |
327 | * Returns 0 on success, -ENOMEM on error | |
328 | */ | |
329 | static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead, | |
330 | gfp_t gfp) | |
331 | { | |
332 | struct smack_rule *nrp; | |
333 | struct smack_rule *orp; | |
334 | int rc = 0; | |
335 | ||
336 | list_for_each_entry_rcu(orp, ohead, list) { | |
337 | nrp = kzalloc(sizeof(struct smack_rule), gfp); | |
338 | if (nrp == NULL) { | |
339 | rc = -ENOMEM; | |
340 | break; | |
341 | } | |
342 | *nrp = *orp; | |
343 | list_add_rcu(&nrp->list, nhead); | |
344 | } | |
345 | return rc; | |
346 | } | |
347 | ||
348 | /** | |
349 | * smk_copy_relabel - copy smk_relabel labels list | |
350 | * @nhead: new rules header pointer | |
351 | * @ohead: old rules header pointer | |
352 | * @gfp: type of the memory for the allocation | |
353 | * | |
354 | * Returns 0 on success, -ENOMEM on error | |
355 | */ | |
356 | static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead, | |
357 | gfp_t gfp) | |
358 | { | |
359 | struct smack_known_list_elem *nklep; | |
360 | struct smack_known_list_elem *oklep; | |
361 | ||
362 | list_for_each_entry(oklep, ohead, list) { | |
363 | nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp); | |
364 | if (nklep == NULL) { | |
365 | smk_destroy_label_list(nhead); | |
366 | return -ENOMEM; | |
367 | } | |
368 | nklep->smk_label = oklep->smk_label; | |
369 | list_add(&nklep->list, nhead); | |
370 | } | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | /** | |
376 | * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_* | |
377 | * @mode - input mode in form of PTRACE_MODE_* | |
378 | * | |
379 | * Returns a converted MAY_* mode usable by smack rules | |
380 | */ | |
381 | static inline unsigned int smk_ptrace_mode(unsigned int mode) | |
382 | { | |
383 | if (mode & PTRACE_MODE_ATTACH) | |
384 | return MAY_READWRITE; | |
385 | if (mode & PTRACE_MODE_READ) | |
386 | return MAY_READ; | |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
391 | /** | |
392 | * smk_ptrace_rule_check - helper for ptrace access | |
393 | * @tracer: tracer process | |
394 | * @tracee_known: label entry of the process that's about to be traced | |
395 | * @mode: ptrace attachment mode (PTRACE_MODE_*) | |
396 | * @func: name of the function that called us, used for audit | |
397 | * | |
398 | * Returns 0 on access granted, -error on error | |
399 | */ | |
400 | static int smk_ptrace_rule_check(struct task_struct *tracer, | |
401 | struct smack_known *tracee_known, | |
402 | unsigned int mode, const char *func) | |
403 | { | |
404 | int rc; | |
405 | struct smk_audit_info ad, *saip = NULL; | |
406 | struct task_smack *tsp; | |
407 | struct smack_known *tracer_known; | |
408 | ||
409 | if ((mode & PTRACE_MODE_NOAUDIT) == 0) { | |
410 | smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK); | |
411 | smk_ad_setfield_u_tsk(&ad, tracer); | |
412 | saip = &ad; | |
413 | } | |
414 | ||
415 | rcu_read_lock(); | |
416 | tsp = smack_cred(__task_cred(tracer)); | |
417 | tracer_known = smk_of_task(tsp); | |
418 | ||
419 | if ((mode & PTRACE_MODE_ATTACH) && | |
420 | (smack_ptrace_rule == SMACK_PTRACE_EXACT || | |
421 | smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) { | |
422 | if (tracer_known->smk_known == tracee_known->smk_known) | |
423 | rc = 0; | |
424 | else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN) | |
425 | rc = -EACCES; | |
426 | else if (capable(CAP_SYS_PTRACE)) | |
427 | rc = 0; | |
428 | else | |
429 | rc = -EACCES; | |
430 | ||
431 | if (saip) | |
432 | smack_log(tracer_known->smk_known, | |
433 | tracee_known->smk_known, | |
434 | 0, rc, saip); | |
435 | ||
436 | rcu_read_unlock(); | |
437 | return rc; | |
438 | } | |
439 | ||
440 | /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */ | |
441 | rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip); | |
442 | ||
443 | rcu_read_unlock(); | |
444 | return rc; | |
445 | } | |
446 | ||
447 | /* | |
448 | * LSM hooks. | |
449 | * We he, that is fun! | |
450 | */ | |
451 | ||
452 | /** | |
453 | * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH | |
454 | * @ctp: child task pointer | |
455 | * @mode: ptrace attachment mode (PTRACE_MODE_*) | |
456 | * | |
457 | * Returns 0 if access is OK, an error code otherwise | |
458 | * | |
459 | * Do the capability checks. | |
460 | */ | |
461 | static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode) | |
462 | { | |
463 | struct smack_known *skp; | |
464 | ||
465 | skp = smk_of_task_struct(ctp); | |
466 | ||
467 | return smk_ptrace_rule_check(current, skp, mode, __func__); | |
468 | } | |
469 | ||
470 | /** | |
471 | * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME | |
472 | * @ptp: parent task pointer | |
473 | * | |
474 | * Returns 0 if access is OK, an error code otherwise | |
475 | * | |
476 | * Do the capability checks, and require PTRACE_MODE_ATTACH. | |
477 | */ | |
478 | static int smack_ptrace_traceme(struct task_struct *ptp) | |
479 | { | |
480 | int rc; | |
481 | struct smack_known *skp; | |
482 | ||
483 | skp = smk_of_task(smack_cred(current_cred())); | |
484 | ||
485 | rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__); | |
486 | return rc; | |
487 | } | |
488 | ||
489 | /** | |
490 | * smack_syslog - Smack approval on syslog | |
491 | * @type: message type | |
492 | * | |
493 | * Returns 0 on success, error code otherwise. | |
494 | */ | |
495 | static int smack_syslog(int typefrom_file) | |
496 | { | |
497 | int rc = 0; | |
498 | struct smack_known *skp = smk_of_current(); | |
499 | ||
500 | if (smack_privileged(CAP_MAC_OVERRIDE)) | |
501 | return 0; | |
502 | ||
503 | if (smack_syslog_label != NULL && smack_syslog_label != skp) | |
504 | rc = -EACCES; | |
505 | ||
506 | return rc; | |
507 | } | |
508 | ||
509 | ||
510 | /* | |
511 | * Superblock Hooks. | |
512 | */ | |
513 | ||
514 | /** | |
515 | * smack_sb_alloc_security - allocate a superblock blob | |
516 | * @sb: the superblock getting the blob | |
517 | * | |
518 | * Returns 0 on success or -ENOMEM on error. | |
519 | */ | |
520 | static int smack_sb_alloc_security(struct super_block *sb) | |
521 | { | |
522 | struct superblock_smack *sbsp = smack_superblock(sb); | |
523 | ||
524 | sbsp->smk_root = &smack_known_floor; | |
525 | sbsp->smk_default = &smack_known_floor; | |
526 | sbsp->smk_floor = &smack_known_floor; | |
527 | sbsp->smk_hat = &smack_known_hat; | |
528 | /* | |
529 | * SMK_SB_INITIALIZED will be zero from kzalloc. | |
530 | */ | |
531 | ||
532 | return 0; | |
533 | } | |
534 | ||
535 | /** | |
536 | * smack_sb_copy_data - copy mount options data for processing | |
537 | * @orig: where to start | |
538 | * @smackopts: mount options string | |
539 | * | |
540 | * Returns 0 on success or -ENOMEM on error. | |
541 | * | |
542 | * Copy the Smack specific mount options out of the mount | |
543 | * options list. | |
544 | */ | |
545 | static int smack_sb_copy_data(char *orig, char *smackopts) | |
546 | { | |
547 | char *cp, *commap, *otheropts, *dp; | |
548 | ||
549 | otheropts = (char *)get_zeroed_page(GFP_KERNEL); | |
550 | if (otheropts == NULL) | |
551 | return -ENOMEM; | |
552 | ||
553 | for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) { | |
554 | if (strstr(cp, SMK_FSDEFAULT) == cp) | |
555 | dp = smackopts; | |
556 | else if (strstr(cp, SMK_FSFLOOR) == cp) | |
557 | dp = smackopts; | |
558 | else if (strstr(cp, SMK_FSHAT) == cp) | |
559 | dp = smackopts; | |
560 | else if (strstr(cp, SMK_FSROOT) == cp) | |
561 | dp = smackopts; | |
562 | else if (strstr(cp, SMK_FSTRANS) == cp) | |
563 | dp = smackopts; | |
564 | else | |
565 | dp = otheropts; | |
566 | ||
567 | commap = strchr(cp, ','); | |
568 | if (commap != NULL) | |
569 | *commap = '\0'; | |
570 | ||
571 | if (*dp != '\0') | |
572 | strcat(dp, ","); | |
573 | strcat(dp, cp); | |
574 | } | |
575 | ||
576 | strcpy(orig, otheropts); | |
577 | free_page((unsigned long)otheropts); | |
578 | ||
579 | return 0; | |
580 | } | |
581 | ||
582 | /** | |
583 | * smack_parse_opts_str - parse Smack specific mount options | |
584 | * @options: mount options string | |
585 | * @opts: where to store converted mount opts | |
586 | * | |
587 | * Returns 0 on success or -ENOMEM on error. | |
588 | * | |
589 | * converts Smack specific mount options to generic security option format | |
590 | */ | |
591 | static int smack_parse_opts_str(char *options, | |
592 | struct security_mnt_opts *opts) | |
593 | { | |
594 | char *p; | |
595 | char *fsdefault = NULL; | |
596 | char *fsfloor = NULL; | |
597 | char *fshat = NULL; | |
598 | char *fsroot = NULL; | |
599 | char *fstransmute = NULL; | |
600 | int rc = -ENOMEM; | |
601 | int num_mnt_opts = 0; | |
602 | int token; | |
603 | ||
604 | opts->num_mnt_opts = 0; | |
605 | ||
606 | if (!options) | |
607 | return 0; | |
608 | ||
609 | while ((p = strsep(&options, ",")) != NULL) { | |
610 | substring_t args[MAX_OPT_ARGS]; | |
611 | ||
612 | if (!*p) | |
613 | continue; | |
614 | ||
615 | token = match_token(p, smk_mount_tokens, args); | |
616 | ||
617 | switch (token) { | |
618 | case Opt_fsdefault: | |
619 | if (fsdefault) | |
620 | goto out_opt_err; | |
621 | fsdefault = match_strdup(&args[0]); | |
622 | if (!fsdefault) | |
623 | goto out_err; | |
624 | break; | |
625 | case Opt_fsfloor: | |
626 | if (fsfloor) | |
627 | goto out_opt_err; | |
628 | fsfloor = match_strdup(&args[0]); | |
629 | if (!fsfloor) | |
630 | goto out_err; | |
631 | break; | |
632 | case Opt_fshat: | |
633 | if (fshat) | |
634 | goto out_opt_err; | |
635 | fshat = match_strdup(&args[0]); | |
636 | if (!fshat) | |
637 | goto out_err; | |
638 | break; | |
639 | case Opt_fsroot: | |
640 | if (fsroot) | |
641 | goto out_opt_err; | |
642 | fsroot = match_strdup(&args[0]); | |
643 | if (!fsroot) | |
644 | goto out_err; | |
645 | break; | |
646 | case Opt_fstransmute: | |
647 | if (fstransmute) | |
648 | goto out_opt_err; | |
649 | fstransmute = match_strdup(&args[0]); | |
650 | if (!fstransmute) | |
651 | goto out_err; | |
652 | break; | |
653 | default: | |
654 | rc = -EINVAL; | |
655 | pr_warn("Smack: unknown mount option\n"); | |
656 | goto out_err; | |
657 | } | |
658 | } | |
659 | ||
660 | opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_KERNEL); | |
661 | if (!opts->mnt_opts) | |
662 | goto out_err; | |
663 | ||
664 | opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int), | |
665 | GFP_KERNEL); | |
666 | if (!opts->mnt_opts_flags) | |
667 | goto out_err; | |
668 | ||
669 | if (fsdefault) { | |
670 | opts->mnt_opts[num_mnt_opts] = fsdefault; | |
671 | opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT; | |
672 | } | |
673 | if (fsfloor) { | |
674 | opts->mnt_opts[num_mnt_opts] = fsfloor; | |
675 | opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT; | |
676 | } | |
677 | if (fshat) { | |
678 | opts->mnt_opts[num_mnt_opts] = fshat; | |
679 | opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT; | |
680 | } | |
681 | if (fsroot) { | |
682 | opts->mnt_opts[num_mnt_opts] = fsroot; | |
683 | opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT; | |
684 | } | |
685 | if (fstransmute) { | |
686 | opts->mnt_opts[num_mnt_opts] = fstransmute; | |
687 | opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT; | |
688 | } | |
689 | ||
690 | opts->num_mnt_opts = num_mnt_opts; | |
691 | return 0; | |
692 | ||
693 | out_opt_err: | |
694 | rc = -EINVAL; | |
695 | pr_warn("Smack: duplicate mount options\n"); | |
696 | ||
697 | out_err: | |
698 | kfree(fsdefault); | |
699 | kfree(fsfloor); | |
700 | kfree(fshat); | |
701 | kfree(fsroot); | |
702 | kfree(fstransmute); | |
703 | return rc; | |
704 | } | |
705 | ||
706 | /** | |
707 | * smack_set_mnt_opts - set Smack specific mount options | |
708 | * @sb: the file system superblock | |
709 | * @opts: Smack mount options | |
710 | * @kern_flags: mount option from kernel space or user space | |
711 | * @set_kern_flags: where to store converted mount opts | |
712 | * | |
713 | * Returns 0 on success, an error code on failure | |
714 | * | |
715 | * Allow filesystems with binary mount data to explicitly set Smack mount | |
716 | * labels. | |
717 | */ | |
718 | static int smack_set_mnt_opts(struct super_block *sb, | |
719 | struct security_mnt_opts *opts, | |
720 | unsigned long kern_flags, | |
721 | unsigned long *set_kern_flags) | |
722 | { | |
723 | struct dentry *root = sb->s_root; | |
724 | struct inode *inode = d_backing_inode(root); | |
725 | struct superblock_smack *sp = smack_superblock(sb); | |
726 | struct inode_smack *isp; | |
727 | struct smack_known *skp; | |
728 | int i; | |
729 | int num_opts = opts->num_mnt_opts; | |
730 | int transmute = 0; | |
731 | ||
732 | if (sp->smk_flags & SMK_SB_INITIALIZED) | |
733 | return 0; | |
734 | ||
735 | if (!smack_privileged(CAP_MAC_ADMIN)) { | |
736 | /* | |
737 | * Unprivileged mounts don't get to specify Smack values. | |
738 | */ | |
739 | if (num_opts) | |
740 | return -EPERM; | |
741 | /* | |
742 | * Unprivileged mounts get root and default from the caller. | |
743 | */ | |
744 | skp = smk_of_current(); | |
745 | sp->smk_root = skp; | |
746 | sp->smk_default = skp; | |
747 | /* | |
748 | * For a handful of fs types with no user-controlled | |
749 | * backing store it's okay to trust security labels | |
750 | * in the filesystem. The rest are untrusted. | |
751 | */ | |
752 | if (sb->s_user_ns != &init_user_ns && | |
753 | sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC && | |
754 | sb->s_magic != RAMFS_MAGIC) { | |
755 | transmute = 1; | |
756 | sp->smk_flags |= SMK_SB_UNTRUSTED; | |
757 | } | |
758 | } | |
759 | ||
760 | sp->smk_flags |= SMK_SB_INITIALIZED; | |
761 | ||
762 | for (i = 0; i < num_opts; i++) { | |
763 | switch (opts->mnt_opts_flags[i]) { | |
764 | case FSDEFAULT_MNT: | |
765 | skp = smk_import_entry(opts->mnt_opts[i], 0); | |
766 | if (IS_ERR(skp)) | |
767 | return PTR_ERR(skp); | |
768 | sp->smk_default = skp; | |
769 | break; | |
770 | case FSFLOOR_MNT: | |
771 | skp = smk_import_entry(opts->mnt_opts[i], 0); | |
772 | if (IS_ERR(skp)) | |
773 | return PTR_ERR(skp); | |
774 | sp->smk_floor = skp; | |
775 | break; | |
776 | case FSHAT_MNT: | |
777 | skp = smk_import_entry(opts->mnt_opts[i], 0); | |
778 | if (IS_ERR(skp)) | |
779 | return PTR_ERR(skp); | |
780 | sp->smk_hat = skp; | |
781 | break; | |
782 | case FSROOT_MNT: | |
783 | skp = smk_import_entry(opts->mnt_opts[i], 0); | |
784 | if (IS_ERR(skp)) | |
785 | return PTR_ERR(skp); | |
786 | sp->smk_root = skp; | |
787 | break; | |
788 | case FSTRANS_MNT: | |
789 | skp = smk_import_entry(opts->mnt_opts[i], 0); | |
790 | if (IS_ERR(skp)) | |
791 | return PTR_ERR(skp); | |
792 | sp->smk_root = skp; | |
793 | transmute = 1; | |
794 | break; | |
795 | default: | |
796 | break; | |
797 | } | |
798 | } | |
799 | ||
800 | /* | |
801 | * Initialize the root inode. | |
802 | */ | |
803 | lsm_early_inode(inode); | |
804 | init_inode_smack(inode, sp->smk_root); | |
805 | ||
806 | if (transmute) { | |
807 | isp = smack_inode(inode); | |
808 | isp->smk_flags |= SMK_INODE_TRANSMUTE; | |
809 | } | |
810 | ||
811 | return 0; | |
812 | } | |
813 | ||
814 | /** | |
815 | * smack_sb_kern_mount - Smack specific mount processing | |
816 | * @sb: the file system superblock | |
817 | * @flags: the mount flags | |
818 | * @data: the smack mount options | |
819 | * | |
820 | * Returns 0 on success, an error code on failure | |
821 | */ | |
822 | static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data) | |
823 | { | |
824 | int rc = 0; | |
825 | char *options = data; | |
826 | struct security_mnt_opts opts; | |
827 | ||
828 | security_init_mnt_opts(&opts); | |
829 | ||
830 | if (!options) | |
831 | goto out; | |
832 | ||
833 | rc = smack_parse_opts_str(options, &opts); | |
834 | if (rc) | |
835 | goto out_err; | |
836 | ||
837 | out: | |
838 | rc = smack_set_mnt_opts(sb, &opts, 0, NULL); | |
839 | ||
840 | out_err: | |
841 | security_free_mnt_opts(&opts); | |
842 | return rc; | |
843 | } | |
844 | ||
845 | /** | |
846 | * smack_sb_statfs - Smack check on statfs | |
847 | * @dentry: identifies the file system in question | |
848 | * | |
849 | * Returns 0 if current can read the floor of the filesystem, | |
850 | * and error code otherwise | |
851 | */ | |
852 | static int smack_sb_statfs(struct dentry *dentry) | |
853 | { | |
854 | struct superblock_smack *sbp = smack_superblock(dentry->d_sb); | |
855 | int rc; | |
856 | struct smk_audit_info ad; | |
857 | ||
858 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
859 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); | |
860 | ||
861 | rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad); | |
862 | rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc); | |
863 | return rc; | |
864 | } | |
865 | ||
866 | /* | |
867 | * BPRM hooks | |
868 | */ | |
869 | ||
870 | /** | |
871 | * smack_bprm_set_creds - set creds for exec | |
872 | * @bprm: the exec information | |
873 | * | |
874 | * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise | |
875 | */ | |
876 | static int smack_bprm_set_creds(struct linux_binprm *bprm) | |
877 | { | |
878 | struct inode *inode = file_inode(bprm->file); | |
879 | struct task_smack *bsp = smack_cred(bprm->cred); | |
880 | struct inode_smack *isp; | |
881 | struct superblock_smack *sbsp; | |
882 | int rc; | |
883 | ||
884 | if (bprm->called_set_creds) | |
885 | return 0; | |
886 | ||
887 | isp = smack_inode(inode); | |
888 | if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task) | |
889 | return 0; | |
890 | ||
891 | sbsp = smack_superblock(inode->i_sb); | |
892 | if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) && | |
893 | isp->smk_task != sbsp->smk_root) | |
894 | return 0; | |
895 | ||
896 | if (bprm->unsafe & LSM_UNSAFE_PTRACE) { | |
897 | struct task_struct *tracer; | |
898 | rc = 0; | |
899 | ||
900 | rcu_read_lock(); | |
901 | tracer = ptrace_parent(current); | |
902 | if (likely(tracer != NULL)) | |
903 | rc = smk_ptrace_rule_check(tracer, | |
904 | isp->smk_task, | |
905 | PTRACE_MODE_ATTACH, | |
906 | __func__); | |
907 | rcu_read_unlock(); | |
908 | ||
909 | if (rc != 0) | |
910 | return rc; | |
911 | } else if (bprm->unsafe) | |
912 | return -EPERM; | |
913 | ||
914 | bsp->smk_task = isp->smk_task; | |
915 | bprm->per_clear |= PER_CLEAR_ON_SETID; | |
916 | ||
917 | /* Decide if this is a secure exec. */ | |
918 | if (bsp->smk_task != bsp->smk_forked) | |
919 | bprm->secureexec = 1; | |
920 | ||
921 | return 0; | |
922 | } | |
923 | ||
924 | /* | |
925 | * Inode hooks | |
926 | */ | |
927 | ||
928 | /** | |
929 | * smack_inode_alloc_security - allocate an inode blob | |
930 | * @inode: the inode in need of a blob | |
931 | * | |
932 | * Returns 0 if it gets a blob, -ENOMEM otherwise | |
933 | */ | |
934 | static int smack_inode_alloc_security(struct inode *inode) | |
935 | { | |
936 | struct smack_known *skp = smk_of_current(); | |
937 | ||
938 | init_inode_smack(inode, skp); | |
939 | return 0; | |
940 | } | |
941 | ||
942 | /** | |
943 | * smack_inode_init_security - copy out the smack from an inode | |
944 | * @inode: the newly created inode | |
945 | * @dir: containing directory object | |
946 | * @qstr: unused | |
947 | * @name: where to put the attribute name | |
948 | * @value: where to put the attribute value | |
949 | * @len: where to put the length of the attribute | |
950 | * | |
951 | * Returns 0 if it all works out, -ENOMEM if there's no memory | |
952 | */ | |
953 | static int smack_inode_init_security(struct inode *inode, struct inode *dir, | |
954 | const struct qstr *qstr, const char **name, | |
955 | void **value, size_t *len) | |
956 | { | |
957 | struct inode_smack *issp = smack_inode(inode); | |
958 | struct smack_known *skp = smk_of_current(); | |
959 | struct smack_known *isp = smk_of_inode(inode); | |
960 | struct smack_known *dsp = smk_of_inode(dir); | |
961 | int may; | |
962 | ||
963 | if (name) | |
964 | *name = XATTR_SMACK_SUFFIX; | |
965 | ||
966 | if (value && len) { | |
967 | rcu_read_lock(); | |
968 | may = smk_access_entry(skp->smk_known, dsp->smk_known, | |
969 | &skp->smk_rules); | |
970 | rcu_read_unlock(); | |
971 | ||
972 | /* | |
973 | * If the access rule allows transmutation and | |
974 | * the directory requests transmutation then | |
975 | * by all means transmute. | |
976 | * Mark the inode as changed. | |
977 | */ | |
978 | if (may > 0 && ((may & MAY_TRANSMUTE) != 0) && | |
979 | smk_inode_transmutable(dir)) { | |
980 | isp = dsp; | |
981 | issp->smk_flags |= SMK_INODE_CHANGED; | |
982 | } | |
983 | ||
984 | *value = kstrdup(isp->smk_known, GFP_NOFS); | |
985 | if (*value == NULL) | |
986 | return -ENOMEM; | |
987 | ||
988 | *len = strlen(isp->smk_known); | |
989 | } | |
990 | ||
991 | return 0; | |
992 | } | |
993 | ||
994 | /** | |
995 | * smack_inode_link - Smack check on link | |
996 | * @old_dentry: the existing object | |
997 | * @dir: unused | |
998 | * @new_dentry: the new object | |
999 | * | |
1000 | * Returns 0 if access is permitted, an error code otherwise | |
1001 | */ | |
1002 | static int smack_inode_link(struct dentry *old_dentry, struct inode *dir, | |
1003 | struct dentry *new_dentry) | |
1004 | { | |
1005 | struct smack_known *isp; | |
1006 | struct smk_audit_info ad; | |
1007 | int rc; | |
1008 | ||
1009 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
1010 | smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry); | |
1011 | ||
1012 | isp = smk_of_inode(d_backing_inode(old_dentry)); | |
1013 | rc = smk_curacc(isp, MAY_WRITE, &ad); | |
1014 | rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc); | |
1015 | ||
1016 | if (rc == 0 && d_is_positive(new_dentry)) { | |
1017 | isp = smk_of_inode(d_backing_inode(new_dentry)); | |
1018 | smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry); | |
1019 | rc = smk_curacc(isp, MAY_WRITE, &ad); | |
1020 | rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc); | |
1021 | } | |
1022 | ||
1023 | return rc; | |
1024 | } | |
1025 | ||
1026 | /** | |
1027 | * smack_inode_unlink - Smack check on inode deletion | |
1028 | * @dir: containing directory object | |
1029 | * @dentry: file to unlink | |
1030 | * | |
1031 | * Returns 0 if current can write the containing directory | |
1032 | * and the object, error code otherwise | |
1033 | */ | |
1034 | static int smack_inode_unlink(struct inode *dir, struct dentry *dentry) | |
1035 | { | |
1036 | struct inode *ip = d_backing_inode(dentry); | |
1037 | struct smk_audit_info ad; | |
1038 | int rc; | |
1039 | ||
1040 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
1041 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); | |
1042 | ||
1043 | /* | |
1044 | * You need write access to the thing you're unlinking | |
1045 | */ | |
1046 | rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad); | |
1047 | rc = smk_bu_inode(ip, MAY_WRITE, rc); | |
1048 | if (rc == 0) { | |
1049 | /* | |
1050 | * You also need write access to the containing directory | |
1051 | */ | |
1052 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); | |
1053 | smk_ad_setfield_u_fs_inode(&ad, dir); | |
1054 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad); | |
1055 | rc = smk_bu_inode(dir, MAY_WRITE, rc); | |
1056 | } | |
1057 | return rc; | |
1058 | } | |
1059 | ||
1060 | /** | |
1061 | * smack_inode_rmdir - Smack check on directory deletion | |
1062 | * @dir: containing directory object | |
1063 | * @dentry: directory to unlink | |
1064 | * | |
1065 | * Returns 0 if current can write the containing directory | |
1066 | * and the directory, error code otherwise | |
1067 | */ | |
1068 | static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry) | |
1069 | { | |
1070 | struct smk_audit_info ad; | |
1071 | int rc; | |
1072 | ||
1073 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
1074 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); | |
1075 | ||
1076 | /* | |
1077 | * You need write access to the thing you're removing | |
1078 | */ | |
1079 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); | |
1080 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); | |
1081 | if (rc == 0) { | |
1082 | /* | |
1083 | * You also need write access to the containing directory | |
1084 | */ | |
1085 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); | |
1086 | smk_ad_setfield_u_fs_inode(&ad, dir); | |
1087 | rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad); | |
1088 | rc = smk_bu_inode(dir, MAY_WRITE, rc); | |
1089 | } | |
1090 | ||
1091 | return rc; | |
1092 | } | |
1093 | ||
1094 | /** | |
1095 | * smack_inode_rename - Smack check on rename | |
1096 | * @old_inode: unused | |
1097 | * @old_dentry: the old object | |
1098 | * @new_inode: unused | |
1099 | * @new_dentry: the new object | |
1100 | * | |
1101 | * Read and write access is required on both the old and | |
1102 | * new directories. | |
1103 | * | |
1104 | * Returns 0 if access is permitted, an error code otherwise | |
1105 | */ | |
1106 | static int smack_inode_rename(struct inode *old_inode, | |
1107 | struct dentry *old_dentry, | |
1108 | struct inode *new_inode, | |
1109 | struct dentry *new_dentry) | |
1110 | { | |
1111 | int rc; | |
1112 | struct smack_known *isp; | |
1113 | struct smk_audit_info ad; | |
1114 | ||
1115 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
1116 | smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry); | |
1117 | ||
1118 | isp = smk_of_inode(d_backing_inode(old_dentry)); | |
1119 | rc = smk_curacc(isp, MAY_READWRITE, &ad); | |
1120 | rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc); | |
1121 | ||
1122 | if (rc == 0 && d_is_positive(new_dentry)) { | |
1123 | isp = smk_of_inode(d_backing_inode(new_dentry)); | |
1124 | smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry); | |
1125 | rc = smk_curacc(isp, MAY_READWRITE, &ad); | |
1126 | rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc); | |
1127 | } | |
1128 | return rc; | |
1129 | } | |
1130 | ||
1131 | /** | |
1132 | * smack_inode_permission - Smack version of permission() | |
1133 | * @inode: the inode in question | |
1134 | * @mask: the access requested | |
1135 | * | |
1136 | * This is the important Smack hook. | |
1137 | * | |
1138 | * Returns 0 if access is permitted, -EACCES otherwise | |
1139 | */ | |
1140 | static int smack_inode_permission(struct inode *inode, int mask) | |
1141 | { | |
1142 | struct superblock_smack *sbsp = smack_superblock(inode->i_sb); | |
1143 | struct smk_audit_info ad; | |
1144 | int no_block = mask & MAY_NOT_BLOCK; | |
1145 | int rc; | |
1146 | ||
1147 | mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); | |
1148 | /* | |
1149 | * No permission to check. Existence test. Yup, it's there. | |
1150 | */ | |
1151 | if (mask == 0) | |
1152 | return 0; | |
1153 | ||
1154 | if (sbsp->smk_flags & SMK_SB_UNTRUSTED) { | |
1155 | if (smk_of_inode(inode) != sbsp->smk_root) | |
1156 | return -EACCES; | |
1157 | } | |
1158 | ||
1159 | /* May be droppable after audit */ | |
1160 | if (no_block) | |
1161 | return -ECHILD; | |
1162 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE); | |
1163 | smk_ad_setfield_u_fs_inode(&ad, inode); | |
1164 | rc = smk_curacc(smk_of_inode(inode), mask, &ad); | |
1165 | rc = smk_bu_inode(inode, mask, rc); | |
1166 | return rc; | |
1167 | } | |
1168 | ||
1169 | /** | |
1170 | * smack_inode_setattr - Smack check for setting attributes | |
1171 | * @dentry: the object | |
1172 | * @iattr: for the force flag | |
1173 | * | |
1174 | * Returns 0 if access is permitted, an error code otherwise | |
1175 | */ | |
1176 | static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr) | |
1177 | { | |
1178 | struct smk_audit_info ad; | |
1179 | int rc; | |
1180 | ||
1181 | /* | |
1182 | * Need to allow for clearing the setuid bit. | |
1183 | */ | |
1184 | if (iattr->ia_valid & ATTR_FORCE) | |
1185 | return 0; | |
1186 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
1187 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); | |
1188 | ||
1189 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); | |
1190 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); | |
1191 | return rc; | |
1192 | } | |
1193 | ||
1194 | /** | |
1195 | * smack_inode_getattr - Smack check for getting attributes | |
1196 | * @mnt: vfsmount of the object | |
1197 | * @dentry: the object | |
1198 | * | |
1199 | * Returns 0 if access is permitted, an error code otherwise | |
1200 | */ | |
1201 | static int smack_inode_getattr(const struct path *path) | |
1202 | { | |
1203 | struct smk_audit_info ad; | |
1204 | struct inode *inode = d_backing_inode(path->dentry); | |
1205 | int rc; | |
1206 | ||
1207 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); | |
1208 | smk_ad_setfield_u_fs_path(&ad, *path); | |
1209 | rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad); | |
1210 | rc = smk_bu_inode(inode, MAY_READ, rc); | |
1211 | return rc; | |
1212 | } | |
1213 | ||
1214 | /** | |
1215 | * smack_inode_setxattr - Smack check for setting xattrs | |
1216 | * @dentry: the object | |
1217 | * @name: name of the attribute | |
1218 | * @value: value of the attribute | |
1219 | * @size: size of the value | |
1220 | * @flags: unused | |
1221 | * | |
1222 | * This protects the Smack attribute explicitly. | |
1223 | * | |
1224 | * Returns 0 if access is permitted, an error code otherwise | |
1225 | */ | |
1226 | static int smack_inode_setxattr(struct dentry *dentry, const char *name, | |
1227 | const void *value, size_t size, int flags) | |
1228 | { | |
1229 | struct smk_audit_info ad; | |
1230 | struct smack_known *skp; | |
1231 | int check_priv = 0; | |
1232 | int check_import = 0; | |
1233 | int check_star = 0; | |
1234 | int rc = 0; | |
1235 | ||
1236 | /* | |
1237 | * Check label validity here so import won't fail in post_setxattr | |
1238 | */ | |
1239 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || | |
1240 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || | |
1241 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) { | |
1242 | check_priv = 1; | |
1243 | check_import = 1; | |
1244 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 || | |
1245 | strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { | |
1246 | check_priv = 1; | |
1247 | check_import = 1; | |
1248 | check_star = 1; | |
1249 | } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { | |
1250 | check_priv = 1; | |
1251 | if (size != TRANS_TRUE_SIZE || | |
1252 | strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0) | |
1253 | rc = -EINVAL; | |
1254 | } else | |
1255 | rc = cap_inode_setxattr(dentry, name, value, size, flags); | |
1256 | ||
1257 | if (check_priv && !smack_privileged(CAP_MAC_ADMIN)) | |
1258 | rc = -EPERM; | |
1259 | ||
1260 | if (rc == 0 && check_import) { | |
1261 | skp = size ? smk_import_entry(value, size) : NULL; | |
1262 | if (IS_ERR(skp)) | |
1263 | rc = PTR_ERR(skp); | |
1264 | else if (skp == NULL || (check_star && | |
1265 | (skp == &smack_known_star || skp == &smack_known_web))) | |
1266 | rc = -EINVAL; | |
1267 | } | |
1268 | ||
1269 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
1270 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); | |
1271 | ||
1272 | if (rc == 0) { | |
1273 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); | |
1274 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); | |
1275 | } | |
1276 | ||
1277 | return rc; | |
1278 | } | |
1279 | ||
1280 | /** | |
1281 | * smack_inode_post_setxattr - Apply the Smack update approved above | |
1282 | * @dentry: object | |
1283 | * @name: attribute name | |
1284 | * @value: attribute value | |
1285 | * @size: attribute size | |
1286 | * @flags: unused | |
1287 | * | |
1288 | * Set the pointer in the inode blob to the entry found | |
1289 | * in the master label list. | |
1290 | */ | |
1291 | static void smack_inode_post_setxattr(struct dentry *dentry, const char *name, | |
1292 | const void *value, size_t size, int flags) | |
1293 | { | |
1294 | struct smack_known *skp; | |
1295 | struct inode_smack *isp = smack_inode(d_backing_inode(dentry)); | |
1296 | ||
1297 | if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) { | |
1298 | isp->smk_flags |= SMK_INODE_TRANSMUTE; | |
1299 | return; | |
1300 | } | |
1301 | ||
1302 | if (strcmp(name, XATTR_NAME_SMACK) == 0) { | |
1303 | skp = smk_import_entry(value, size); | |
1304 | if (!IS_ERR(skp)) | |
1305 | isp->smk_inode = skp; | |
1306 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) { | |
1307 | skp = smk_import_entry(value, size); | |
1308 | if (!IS_ERR(skp)) | |
1309 | isp->smk_task = skp; | |
1310 | } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { | |
1311 | skp = smk_import_entry(value, size); | |
1312 | if (!IS_ERR(skp)) | |
1313 | isp->smk_mmap = skp; | |
1314 | } | |
1315 | ||
1316 | return; | |
1317 | } | |
1318 | ||
1319 | /** | |
1320 | * smack_inode_getxattr - Smack check on getxattr | |
1321 | * @dentry: the object | |
1322 | * @name: unused | |
1323 | * | |
1324 | * Returns 0 if access is permitted, an error code otherwise | |
1325 | */ | |
1326 | static int smack_inode_getxattr(struct dentry *dentry, const char *name) | |
1327 | { | |
1328 | struct smk_audit_info ad; | |
1329 | int rc; | |
1330 | ||
1331 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
1332 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); | |
1333 | ||
1334 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad); | |
1335 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc); | |
1336 | return rc; | |
1337 | } | |
1338 | ||
1339 | /** | |
1340 | * smack_inode_removexattr - Smack check on removexattr | |
1341 | * @dentry: the object | |
1342 | * @name: name of the attribute | |
1343 | * | |
1344 | * Removing the Smack attribute requires CAP_MAC_ADMIN | |
1345 | * | |
1346 | * Returns 0 if access is permitted, an error code otherwise | |
1347 | */ | |
1348 | static int smack_inode_removexattr(struct dentry *dentry, const char *name) | |
1349 | { | |
1350 | struct inode_smack *isp; | |
1351 | struct smk_audit_info ad; | |
1352 | int rc = 0; | |
1353 | ||
1354 | if (strcmp(name, XATTR_NAME_SMACK) == 0 || | |
1355 | strcmp(name, XATTR_NAME_SMACKIPIN) == 0 || | |
1356 | strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 || | |
1357 | strcmp(name, XATTR_NAME_SMACKEXEC) == 0 || | |
1358 | strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 || | |
1359 | strcmp(name, XATTR_NAME_SMACKMMAP) == 0) { | |
1360 | if (!smack_privileged(CAP_MAC_ADMIN)) | |
1361 | rc = -EPERM; | |
1362 | } else | |
1363 | rc = cap_inode_removexattr(dentry, name); | |
1364 | ||
1365 | if (rc != 0) | |
1366 | return rc; | |
1367 | ||
1368 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY); | |
1369 | smk_ad_setfield_u_fs_path_dentry(&ad, dentry); | |
1370 | ||
1371 | rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad); | |
1372 | rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc); | |
1373 | if (rc != 0) | |
1374 | return rc; | |
1375 | ||
1376 | isp = smack_inode(d_backing_inode(dentry)); | |
1377 | /* | |
1378 | * Don't do anything special for these. | |
1379 | * XATTR_NAME_SMACKIPIN | |
1380 | * XATTR_NAME_SMACKIPOUT | |
1381 | */ | |
1382 | if (strcmp(name, XATTR_NAME_SMACK) == 0) { | |
1383 | struct super_block *sbp = dentry->d_sb; | |
1384 | struct superblock_smack *sbsp = smack_superblock(sbp); | |
1385 | ||
1386 | isp->smk_inode = sbsp->smk_default; | |
1387 | } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) | |
1388 | isp->smk_task = NULL; | |
1389 | else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) | |
1390 | isp->smk_mmap = NULL; | |
1391 | else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) | |
1392 | isp->smk_flags &= ~SMK_INODE_TRANSMUTE; | |
1393 | ||
1394 | return 0; | |
1395 | } | |
1396 | ||
1397 | /** | |
1398 | * smack_inode_getsecurity - get smack xattrs | |
1399 | * @inode: the object | |
1400 | * @name: attribute name | |
1401 | * @buffer: where to put the result | |
1402 | * @alloc: duplicate memory | |
1403 | * | |
1404 | * Returns the size of the attribute or an error code | |
1405 | */ | |
1406 | static int smack_inode_getsecurity(struct inode *inode, | |
1407 | const char *name, void **buffer, | |
1408 | bool alloc) | |
1409 | { | |
1410 | struct socket_smack *ssp; | |
1411 | struct socket *sock; | |
1412 | struct super_block *sbp; | |
1413 | struct inode *ip = (struct inode *)inode; | |
1414 | struct smack_known *isp; | |
1415 | ||
1416 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) | |
1417 | isp = smk_of_inode(inode); | |
1418 | else { | |
1419 | /* | |
1420 | * The rest of the Smack xattrs are only on sockets. | |
1421 | */ | |
1422 | sbp = ip->i_sb; | |
1423 | if (sbp->s_magic != SOCKFS_MAGIC) | |
1424 | return -EOPNOTSUPP; | |
1425 | ||
1426 | sock = SOCKET_I(ip); | |
1427 | if (sock == NULL || sock->sk == NULL) | |
1428 | return -EOPNOTSUPP; | |
1429 | ||
1430 | ssp = smack_sock(sock->sk); | |
1431 | ||
1432 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) | |
1433 | isp = ssp->smk_in; | |
1434 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) | |
1435 | isp = ssp->smk_out; | |
1436 | else | |
1437 | return -EOPNOTSUPP; | |
1438 | } | |
1439 | ||
1440 | if (alloc) { | |
1441 | *buffer = kstrdup(isp->smk_known, GFP_KERNEL); | |
1442 | if (*buffer == NULL) | |
1443 | return -ENOMEM; | |
1444 | } | |
1445 | ||
1446 | return strlen(isp->smk_known); | |
1447 | } | |
1448 | ||
1449 | ||
1450 | /** | |
1451 | * smack_inode_listsecurity - list the Smack attributes | |
1452 | * @inode: the object | |
1453 | * @buffer: where they go | |
1454 | * @buffer_size: size of buffer | |
1455 | */ | |
1456 | static int smack_inode_listsecurity(struct inode *inode, char *buffer, | |
1457 | size_t buffer_size) | |
1458 | { | |
1459 | int len = sizeof(XATTR_NAME_SMACK); | |
1460 | ||
1461 | if (buffer != NULL && len <= buffer_size) | |
1462 | memcpy(buffer, XATTR_NAME_SMACK, len); | |
1463 | ||
1464 | return len; | |
1465 | } | |
1466 | ||
1467 | /** | |
1468 | * smack_inode_getsecid - Extract inode's security id | |
1469 | * @inode: inode to extract the info from | |
1470 | * @secid: where result will be saved | |
1471 | */ | |
1472 | static void smack_inode_getsecid(struct inode *inode, u32 *secid) | |
1473 | { | |
1474 | struct inode_smack *isp = smack_inode(inode); | |
1475 | ||
1476 | *secid = isp->smk_inode->smk_secid; | |
1477 | } | |
1478 | ||
1479 | /* | |
1480 | * File Hooks | |
1481 | */ | |
1482 | ||
1483 | /* | |
1484 | * There is no smack_file_permission hook | |
1485 | * | |
1486 | * Should access checks be done on each read or write? | |
1487 | * UNICOS and SELinux say yes. | |
1488 | * Trusted Solaris, Trusted Irix, and just about everyone else says no. | |
1489 | * | |
1490 | * I'll say no for now. Smack does not do the frequent | |
1491 | * label changing that SELinux does. | |
1492 | */ | |
1493 | ||
1494 | /** | |
1495 | * smack_file_alloc_security - assign a file security blob | |
1496 | * @file: the object | |
1497 | * | |
1498 | * The security blob for a file is a pointer to the master | |
1499 | * label list, so no allocation is done. | |
1500 | * | |
1501 | * f_security is the owner security information. It | |
1502 | * isn't used on file access checks, it's for send_sigio. | |
1503 | * | |
1504 | * Returns 0 | |
1505 | */ | |
1506 | static int smack_file_alloc_security(struct file *file) | |
1507 | { | |
1508 | struct smack_known **blob = smack_file(file); | |
1509 | ||
1510 | *blob = smk_of_current(); | |
1511 | return 0; | |
1512 | } | |
1513 | ||
1514 | /** | |
1515 | * smack_file_ioctl - Smack check on ioctls | |
1516 | * @file: the object | |
1517 | * @cmd: what to do | |
1518 | * @arg: unused | |
1519 | * | |
1520 | * Relies heavily on the correct use of the ioctl command conventions. | |
1521 | * | |
1522 | * Returns 0 if allowed, error code otherwise | |
1523 | */ | |
1524 | static int smack_file_ioctl(struct file *file, unsigned int cmd, | |
1525 | unsigned long arg) | |
1526 | { | |
1527 | int rc = 0; | |
1528 | struct smk_audit_info ad; | |
1529 | struct inode *inode = file_inode(file); | |
1530 | ||
1531 | if (unlikely(IS_PRIVATE(inode))) | |
1532 | return 0; | |
1533 | ||
1534 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); | |
1535 | smk_ad_setfield_u_fs_path(&ad, file->f_path); | |
1536 | ||
1537 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
1538 | rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad); | |
1539 | rc = smk_bu_file(file, MAY_WRITE, rc); | |
1540 | } | |
1541 | ||
1542 | if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) { | |
1543 | rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad); | |
1544 | rc = smk_bu_file(file, MAY_READ, rc); | |
1545 | } | |
1546 | ||
1547 | return rc; | |
1548 | } | |
1549 | ||
1550 | /** | |
1551 | * smack_file_lock - Smack check on file locking | |
1552 | * @file: the object | |
1553 | * @cmd: unused | |
1554 | * | |
1555 | * Returns 0 if current has lock access, error code otherwise | |
1556 | */ | |
1557 | static int smack_file_lock(struct file *file, unsigned int cmd) | |
1558 | { | |
1559 | struct smk_audit_info ad; | |
1560 | int rc; | |
1561 | struct inode *inode = file_inode(file); | |
1562 | ||
1563 | if (unlikely(IS_PRIVATE(inode))) | |
1564 | return 0; | |
1565 | ||
1566 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); | |
1567 | smk_ad_setfield_u_fs_path(&ad, file->f_path); | |
1568 | rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad); | |
1569 | rc = smk_bu_file(file, MAY_LOCK, rc); | |
1570 | return rc; | |
1571 | } | |
1572 | ||
1573 | /** | |
1574 | * smack_file_fcntl - Smack check on fcntl | |
1575 | * @file: the object | |
1576 | * @cmd: what action to check | |
1577 | * @arg: unused | |
1578 | * | |
1579 | * Generally these operations are harmless. | |
1580 | * File locking operations present an obvious mechanism | |
1581 | * for passing information, so they require write access. | |
1582 | * | |
1583 | * Returns 0 if current has access, error code otherwise | |
1584 | */ | |
1585 | static int smack_file_fcntl(struct file *file, unsigned int cmd, | |
1586 | unsigned long arg) | |
1587 | { | |
1588 | struct smk_audit_info ad; | |
1589 | int rc = 0; | |
1590 | struct inode *inode = file_inode(file); | |
1591 | ||
1592 | if (unlikely(IS_PRIVATE(inode))) | |
1593 | return 0; | |
1594 | ||
1595 | switch (cmd) { | |
1596 | case F_GETLK: | |
1597 | break; | |
1598 | case F_SETLK: | |
1599 | case F_SETLKW: | |
1600 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); | |
1601 | smk_ad_setfield_u_fs_path(&ad, file->f_path); | |
1602 | rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad); | |
1603 | rc = smk_bu_file(file, MAY_LOCK, rc); | |
1604 | break; | |
1605 | case F_SETOWN: | |
1606 | case F_SETSIG: | |
1607 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); | |
1608 | smk_ad_setfield_u_fs_path(&ad, file->f_path); | |
1609 | rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad); | |
1610 | rc = smk_bu_file(file, MAY_WRITE, rc); | |
1611 | break; | |
1612 | default: | |
1613 | break; | |
1614 | } | |
1615 | ||
1616 | return rc; | |
1617 | } | |
1618 | ||
1619 | /** | |
1620 | * smack_mmap_file : | |
1621 | * Check permissions for a mmap operation. The @file may be NULL, e.g. | |
1622 | * if mapping anonymous memory. | |
1623 | * @file contains the file structure for file to map (may be NULL). | |
1624 | * @reqprot contains the protection requested by the application. | |
1625 | * @prot contains the protection that will be applied by the kernel. | |
1626 | * @flags contains the operational flags. | |
1627 | * Return 0 if permission is granted. | |
1628 | */ | |
1629 | static int smack_mmap_file(struct file *file, | |
1630 | unsigned long reqprot, unsigned long prot, | |
1631 | unsigned long flags) | |
1632 | { | |
1633 | struct smack_known *skp; | |
1634 | struct smack_known *mkp; | |
1635 | struct smack_rule *srp; | |
1636 | struct task_smack *tsp; | |
1637 | struct smack_known *okp; | |
1638 | struct inode_smack *isp; | |
1639 | struct superblock_smack *sbsp; | |
1640 | int may; | |
1641 | int mmay; | |
1642 | int tmay; | |
1643 | int rc; | |
1644 | ||
1645 | if (file == NULL) | |
1646 | return 0; | |
1647 | ||
1648 | if (unlikely(IS_PRIVATE(file_inode(file)))) | |
1649 | return 0; | |
1650 | ||
1651 | isp = smack_inode(file_inode(file)); | |
1652 | if (isp->smk_mmap == NULL) | |
1653 | return 0; | |
1654 | sbsp = smack_superblock(file_inode(file)->i_sb); | |
1655 | if (sbsp->smk_flags & SMK_SB_UNTRUSTED && | |
1656 | isp->smk_mmap != sbsp->smk_root) | |
1657 | return -EACCES; | |
1658 | mkp = isp->smk_mmap; | |
1659 | ||
1660 | tsp = smack_cred(current_cred()); | |
1661 | skp = smk_of_current(); | |
1662 | rc = 0; | |
1663 | ||
1664 | rcu_read_lock(); | |
1665 | /* | |
1666 | * For each Smack rule associated with the subject | |
1667 | * label verify that the SMACK64MMAP also has access | |
1668 | * to that rule's object label. | |
1669 | */ | |
1670 | list_for_each_entry_rcu(srp, &skp->smk_rules, list) { | |
1671 | okp = srp->smk_object; | |
1672 | /* | |
1673 | * Matching labels always allows access. | |
1674 | */ | |
1675 | if (mkp->smk_known == okp->smk_known) | |
1676 | continue; | |
1677 | /* | |
1678 | * If there is a matching local rule take | |
1679 | * that into account as well. | |
1680 | */ | |
1681 | may = smk_access_entry(srp->smk_subject->smk_known, | |
1682 | okp->smk_known, | |
1683 | &tsp->smk_rules); | |
1684 | if (may == -ENOENT) | |
1685 | may = srp->smk_access; | |
1686 | else | |
1687 | may &= srp->smk_access; | |
1688 | /* | |
1689 | * If may is zero the SMACK64MMAP subject can't | |
1690 | * possibly have less access. | |
1691 | */ | |
1692 | if (may == 0) | |
1693 | continue; | |
1694 | ||
1695 | /* | |
1696 | * Fetch the global list entry. | |
1697 | * If there isn't one a SMACK64MMAP subject | |
1698 | * can't have as much access as current. | |
1699 | */ | |
1700 | mmay = smk_access_entry(mkp->smk_known, okp->smk_known, | |
1701 | &mkp->smk_rules); | |
1702 | if (mmay == -ENOENT) { | |
1703 | rc = -EACCES; | |
1704 | break; | |
1705 | } | |
1706 | /* | |
1707 | * If there is a local entry it modifies the | |
1708 | * potential access, too. | |
1709 | */ | |
1710 | tmay = smk_access_entry(mkp->smk_known, okp->smk_known, | |
1711 | &tsp->smk_rules); | |
1712 | if (tmay != -ENOENT) | |
1713 | mmay &= tmay; | |
1714 | ||
1715 | /* | |
1716 | * If there is any access available to current that is | |
1717 | * not available to a SMACK64MMAP subject | |
1718 | * deny access. | |
1719 | */ | |
1720 | if ((may | mmay) != mmay) { | |
1721 | rc = -EACCES; | |
1722 | break; | |
1723 | } | |
1724 | } | |
1725 | ||
1726 | rcu_read_unlock(); | |
1727 | ||
1728 | return rc; | |
1729 | } | |
1730 | ||
1731 | /** | |
1732 | * smack_file_set_fowner - set the file security blob value | |
1733 | * @file: object in question | |
1734 | * | |
1735 | */ | |
1736 | static void smack_file_set_fowner(struct file *file) | |
1737 | { | |
1738 | struct smack_known **blob = smack_file(file); | |
1739 | ||
1740 | *blob = smk_of_current(); | |
1741 | } | |
1742 | ||
1743 | /** | |
1744 | * smack_file_send_sigiotask - Smack on sigio | |
1745 | * @tsk: The target task | |
1746 | * @fown: the object the signal come from | |
1747 | * @signum: unused | |
1748 | * | |
1749 | * Allow a privileged task to get signals even if it shouldn't | |
1750 | * | |
1751 | * Returns 0 if a subject with the object's smack could | |
1752 | * write to the task, an error code otherwise. | |
1753 | */ | |
1754 | static int smack_file_send_sigiotask(struct task_struct *tsk, | |
1755 | struct fown_struct *fown, int signum) | |
1756 | { | |
1757 | struct smack_known **blob; | |
1758 | struct smack_known *skp; | |
1759 | struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred)); | |
1760 | struct file *file; | |
1761 | int rc; | |
1762 | struct smk_audit_info ad; | |
1763 | ||
1764 | /* | |
1765 | * struct fown_struct is never outside the context of a struct file | |
1766 | */ | |
1767 | file = container_of(fown, struct file, f_owner); | |
1768 | ||
1769 | /* we don't log here as rc can be overriden */ | |
1770 | blob = smack_file(file); | |
1771 | skp = *blob; | |
1772 | rc = smk_access(skp, tkp, MAY_DELIVER, NULL); | |
1773 | rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc); | |
1774 | if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE)) | |
1775 | rc = 0; | |
1776 | ||
1777 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK); | |
1778 | smk_ad_setfield_u_tsk(&ad, tsk); | |
1779 | smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad); | |
1780 | return rc; | |
1781 | } | |
1782 | ||
1783 | /** | |
1784 | * smack_file_receive - Smack file receive check | |
1785 | * @file: the object | |
1786 | * | |
1787 | * Returns 0 if current has access, error code otherwise | |
1788 | */ | |
1789 | static int smack_file_receive(struct file *file) | |
1790 | { | |
1791 | int rc; | |
1792 | int may = 0; | |
1793 | struct smk_audit_info ad; | |
1794 | struct inode *inode = file_inode(file); | |
1795 | struct socket *sock; | |
1796 | struct task_smack *tsp; | |
1797 | struct socket_smack *ssp; | |
1798 | ||
1799 | if (unlikely(IS_PRIVATE(inode))) | |
1800 | return 0; | |
1801 | ||
1802 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); | |
1803 | smk_ad_setfield_u_fs_path(&ad, file->f_path); | |
1804 | ||
1805 | if (inode->i_sb->s_magic == SOCKFS_MAGIC) { | |
1806 | sock = SOCKET_I(inode); | |
1807 | ssp = smack_sock(sock->sk); | |
1808 | tsp = smack_cred(current_cred()); | |
1809 | /* | |
1810 | * If the receiving process can't write to the | |
1811 | * passed socket or if the passed socket can't | |
1812 | * write to the receiving process don't accept | |
1813 | * the passed socket. | |
1814 | */ | |
1815 | rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad); | |
1816 | rc = smk_bu_file(file, may, rc); | |
1817 | if (rc < 0) | |
1818 | return rc; | |
1819 | rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad); | |
1820 | rc = smk_bu_file(file, may, rc); | |
1821 | return rc; | |
1822 | } | |
1823 | /* | |
1824 | * This code relies on bitmasks. | |
1825 | */ | |
1826 | if (file->f_mode & FMODE_READ) | |
1827 | may = MAY_READ; | |
1828 | if (file->f_mode & FMODE_WRITE) | |
1829 | may |= MAY_WRITE; | |
1830 | ||
1831 | rc = smk_curacc(smk_of_inode(inode), may, &ad); | |
1832 | rc = smk_bu_file(file, may, rc); | |
1833 | return rc; | |
1834 | } | |
1835 | ||
1836 | /** | |
1837 | * smack_file_open - Smack dentry open processing | |
1838 | * @file: the object | |
1839 | * @cred: task credential | |
1840 | * | |
1841 | * Set the security blob in the file structure. | |
1842 | * Allow the open only if the task has read access. There are | |
1843 | * many read operations (e.g. fstat) that you can do with an | |
1844 | * fd even if you have the file open write-only. | |
1845 | * | |
1846 | * Returns 0 | |
1847 | */ | |
1848 | static int smack_file_open(struct file *file, const struct cred *cred) | |
1849 | { | |
1850 | struct task_smack *tsp = smack_cred(cred); | |
1851 | struct inode *inode = file_inode(file); | |
1852 | struct smk_audit_info ad; | |
1853 | int rc; | |
1854 | ||
1855 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH); | |
1856 | smk_ad_setfield_u_fs_path(&ad, file->f_path); | |
1857 | rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad); | |
1858 | rc = smk_bu_credfile(cred, file, MAY_READ, rc); | |
1859 | ||
1860 | return rc; | |
1861 | } | |
1862 | ||
1863 | /* | |
1864 | * Task hooks | |
1865 | */ | |
1866 | ||
1867 | /** | |
1868 | * smack_cred_alloc_blank - "allocate" blank task-level security credentials | |
1869 | * @new: the new credentials | |
1870 | * @gfp: the atomicity of any memory allocations | |
1871 | * | |
1872 | * Prepare a blank set of credentials for modification. This must allocate all | |
1873 | * the memory the LSM module might require such that cred_transfer() can | |
1874 | * complete without error. | |
1875 | */ | |
1876 | static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp) | |
1877 | { | |
1878 | init_task_smack(smack_cred(cred), NULL, NULL); | |
1879 | return 0; | |
1880 | } | |
1881 | ||
1882 | ||
1883 | /** | |
1884 | * smack_cred_free - "free" task-level security credentials | |
1885 | * @cred: the credentials in question | |
1886 | * | |
1887 | */ | |
1888 | static void smack_cred_free(struct cred *cred) | |
1889 | { | |
1890 | struct task_smack *tsp = smack_cred(cred); | |
1891 | struct smack_rule *rp; | |
1892 | struct list_head *l; | |
1893 | struct list_head *n; | |
1894 | ||
1895 | smk_destroy_label_list(&tsp->smk_relabel); | |
1896 | ||
1897 | list_for_each_safe(l, n, &tsp->smk_rules) { | |
1898 | rp = list_entry(l, struct smack_rule, list); | |
1899 | list_del(&rp->list); | |
1900 | kfree(rp); | |
1901 | } | |
1902 | } | |
1903 | ||
1904 | /** | |
1905 | * smack_cred_prepare - prepare new set of credentials for modification | |
1906 | * @new: the new credentials | |
1907 | * @old: the original credentials | |
1908 | * @gfp: the atomicity of any memory allocations | |
1909 | * | |
1910 | * Prepare a new set of credentials for modification. | |
1911 | */ | |
1912 | static int smack_cred_prepare(struct cred *new, const struct cred *old, | |
1913 | gfp_t gfp) | |
1914 | { | |
1915 | struct task_smack *old_tsp = smack_cred(old); | |
1916 | struct task_smack *new_tsp = smack_cred(new); | |
1917 | int rc; | |
1918 | ||
1919 | init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task); | |
1920 | ||
1921 | rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp); | |
1922 | if (rc != 0) | |
1923 | return rc; | |
1924 | ||
1925 | rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel, | |
1926 | gfp); | |
1927 | return rc; | |
1928 | } | |
1929 | ||
1930 | /** | |
1931 | * smack_cred_transfer - Transfer the old credentials to the new credentials | |
1932 | * @new: the new credentials | |
1933 | * @old: the original credentials | |
1934 | * | |
1935 | * Fill in a set of blank credentials from another set of credentials. | |
1936 | */ | |
1937 | static void smack_cred_transfer(struct cred *new, const struct cred *old) | |
1938 | { | |
1939 | struct task_smack *old_tsp = smack_cred(old); | |
1940 | struct task_smack *new_tsp = smack_cred(new); | |
1941 | ||
1942 | new_tsp->smk_task = old_tsp->smk_task; | |
1943 | new_tsp->smk_forked = old_tsp->smk_task; | |
1944 | mutex_init(&new_tsp->smk_rules_lock); | |
1945 | INIT_LIST_HEAD(&new_tsp->smk_rules); | |
1946 | ||
1947 | /* cbs copy rule list */ | |
1948 | } | |
1949 | ||
1950 | /** | |
1951 | * smack_kernel_act_as - Set the subjective context in a set of credentials | |
1952 | * @new: points to the set of credentials to be modified. | |
1953 | * @secid: specifies the security ID to be set | |
1954 | * | |
1955 | * Set the security data for a kernel service. | |
1956 | */ | |
1957 | static int smack_kernel_act_as(struct cred *new, u32 secid) | |
1958 | { | |
1959 | struct task_smack *new_tsp = smack_cred(new); | |
1960 | ||
1961 | new_tsp->smk_task = smack_from_secid(secid); | |
1962 | return 0; | |
1963 | } | |
1964 | ||
1965 | /** | |
1966 | * smack_kernel_create_files_as - Set the file creation label in a set of creds | |
1967 | * @new: points to the set of credentials to be modified | |
1968 | * @inode: points to the inode to use as a reference | |
1969 | * | |
1970 | * Set the file creation context in a set of credentials to the same | |
1971 | * as the objective context of the specified inode | |
1972 | */ | |
1973 | static int smack_kernel_create_files_as(struct cred *new, | |
1974 | struct inode *inode) | |
1975 | { | |
1976 | struct inode_smack *isp = smack_inode(inode); | |
1977 | struct task_smack *tsp = smack_cred(new); | |
1978 | ||
1979 | tsp->smk_forked = isp->smk_inode; | |
1980 | tsp->smk_task = tsp->smk_forked; | |
1981 | return 0; | |
1982 | } | |
1983 | ||
1984 | /** | |
1985 | * smk_curacc_on_task - helper to log task related access | |
1986 | * @p: the task object | |
1987 | * @access: the access requested | |
1988 | * @caller: name of the calling function for audit | |
1989 | * | |
1990 | * Return 0 if access is permitted | |
1991 | */ | |
1992 | static int smk_curacc_on_task(struct task_struct *p, int access, | |
1993 | const char *caller) | |
1994 | { | |
1995 | struct smk_audit_info ad; | |
1996 | struct smack_known *skp = smk_of_task_struct(p); | |
1997 | int rc; | |
1998 | ||
1999 | smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK); | |
2000 | smk_ad_setfield_u_tsk(&ad, p); | |
2001 | rc = smk_curacc(skp, access, &ad); | |
2002 | rc = smk_bu_task(p, access, rc); | |
2003 | return rc; | |
2004 | } | |
2005 | ||
2006 | /** | |
2007 | * smack_task_setpgid - Smack check on setting pgid | |
2008 | * @p: the task object | |
2009 | * @pgid: unused | |
2010 | * | |
2011 | * Return 0 if write access is permitted | |
2012 | */ | |
2013 | static int smack_task_setpgid(struct task_struct *p, pid_t pgid) | |
2014 | { | |
2015 | return smk_curacc_on_task(p, MAY_WRITE, __func__); | |
2016 | } | |
2017 | ||
2018 | /** | |
2019 | * smack_task_getpgid - Smack access check for getpgid | |
2020 | * @p: the object task | |
2021 | * | |
2022 | * Returns 0 if current can read the object task, error code otherwise | |
2023 | */ | |
2024 | static int smack_task_getpgid(struct task_struct *p) | |
2025 | { | |
2026 | return smk_curacc_on_task(p, MAY_READ, __func__); | |
2027 | } | |
2028 | ||
2029 | /** | |
2030 | * smack_task_getsid - Smack access check for getsid | |
2031 | * @p: the object task | |
2032 | * | |
2033 | * Returns 0 if current can read the object task, error code otherwise | |
2034 | */ | |
2035 | static int smack_task_getsid(struct task_struct *p) | |
2036 | { | |
2037 | return smk_curacc_on_task(p, MAY_READ, __func__); | |
2038 | } | |
2039 | ||
2040 | /** | |
2041 | * smack_task_getsecid - get the secid of the task | |
2042 | * @p: the object task | |
2043 | * @secid: where to put the result | |
2044 | * | |
2045 | * Sets the secid to contain a u32 version of the smack label. | |
2046 | */ | |
2047 | static void smack_task_getsecid(struct task_struct *p, u32 *secid) | |
2048 | { | |
2049 | struct smack_known *skp = smk_of_task_struct(p); | |
2050 | ||
2051 | *secid = skp->smk_secid; | |
2052 | } | |
2053 | ||
2054 | /** | |
2055 | * smack_task_setnice - Smack check on setting nice | |
2056 | * @p: the task object | |
2057 | * @nice: unused | |
2058 | * | |
2059 | * Return 0 if write access is permitted | |
2060 | */ | |
2061 | static int smack_task_setnice(struct task_struct *p, int nice) | |
2062 | { | |
2063 | return smk_curacc_on_task(p, MAY_WRITE, __func__); | |
2064 | } | |
2065 | ||
2066 | /** | |
2067 | * smack_task_setioprio - Smack check on setting ioprio | |
2068 | * @p: the task object | |
2069 | * @ioprio: unused | |
2070 | * | |
2071 | * Return 0 if write access is permitted | |
2072 | */ | |
2073 | static int smack_task_setioprio(struct task_struct *p, int ioprio) | |
2074 | { | |
2075 | return smk_curacc_on_task(p, MAY_WRITE, __func__); | |
2076 | } | |
2077 | ||
2078 | /** | |
2079 | * smack_task_getioprio - Smack check on reading ioprio | |
2080 | * @p: the task object | |
2081 | * | |
2082 | * Return 0 if read access is permitted | |
2083 | */ | |
2084 | static int smack_task_getioprio(struct task_struct *p) | |
2085 | { | |
2086 | return smk_curacc_on_task(p, MAY_READ, __func__); | |
2087 | } | |
2088 | ||
2089 | /** | |
2090 | * smack_task_setscheduler - Smack check on setting scheduler | |
2091 | * @p: the task object | |
2092 | * @policy: unused | |
2093 | * @lp: unused | |
2094 | * | |
2095 | * Return 0 if read access is permitted | |
2096 | */ | |
2097 | static int smack_task_setscheduler(struct task_struct *p) | |
2098 | { | |
2099 | return smk_curacc_on_task(p, MAY_WRITE, __func__); | |
2100 | } | |
2101 | ||
2102 | /** | |
2103 | * smack_task_getscheduler - Smack check on reading scheduler | |
2104 | * @p: the task object | |
2105 | * | |
2106 | * Return 0 if read access is permitted | |
2107 | */ | |
2108 | static int smack_task_getscheduler(struct task_struct *p) | |
2109 | { | |
2110 | return smk_curacc_on_task(p, MAY_READ, __func__); | |
2111 | } | |
2112 | ||
2113 | /** | |
2114 | * smack_task_movememory - Smack check on moving memory | |
2115 | * @p: the task object | |
2116 | * | |
2117 | * Return 0 if write access is permitted | |
2118 | */ | |
2119 | static int smack_task_movememory(struct task_struct *p) | |
2120 | { | |
2121 | return smk_curacc_on_task(p, MAY_WRITE, __func__); | |
2122 | } | |
2123 | ||
2124 | /** | |
2125 | * smack_task_kill - Smack check on signal delivery | |
2126 | * @p: the task object | |
2127 | * @info: unused | |
2128 | * @sig: unused | |
2129 | * @secid: identifies the smack to use in lieu of current's | |
2130 | * | |
2131 | * Return 0 if write access is permitted | |
2132 | * | |
2133 | * The secid behavior is an artifact of an SELinux hack | |
2134 | * in the USB code. Someday it may go away. | |
2135 | */ | |
2136 | static int smack_task_kill(struct task_struct *p, struct siginfo *info, | |
2137 | int sig, u32 secid) | |
2138 | { | |
2139 | struct smk_audit_info ad; | |
2140 | struct smack_known *skp; | |
2141 | struct smack_known *tkp = smk_of_task_struct(p); | |
2142 | int rc; | |
2143 | ||
2144 | if (!sig) | |
2145 | return 0; /* null signal; existence test */ | |
2146 | ||
2147 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK); | |
2148 | smk_ad_setfield_u_tsk(&ad, p); | |
2149 | /* | |
2150 | * Sending a signal requires that the sender | |
2151 | * can write the receiver. | |
2152 | */ | |
2153 | if (secid == 0) { | |
2154 | rc = smk_curacc(tkp, MAY_DELIVER, &ad); | |
2155 | rc = smk_bu_task(p, MAY_DELIVER, rc); | |
2156 | return rc; | |
2157 | } | |
2158 | /* | |
2159 | * If the secid isn't 0 we're dealing with some USB IO | |
2160 | * specific behavior. This is not clean. For one thing | |
2161 | * we can't take privilege into account. | |
2162 | */ | |
2163 | skp = smack_from_secid(secid); | |
2164 | rc = smk_access(skp, tkp, MAY_DELIVER, &ad); | |
2165 | rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc); | |
2166 | return rc; | |
2167 | } | |
2168 | ||
2169 | /** | |
2170 | * smack_task_to_inode - copy task smack into the inode blob | |
2171 | * @p: task to copy from | |
2172 | * @inode: inode to copy to | |
2173 | * | |
2174 | * Sets the smack pointer in the inode security blob | |
2175 | */ | |
2176 | static void smack_task_to_inode(struct task_struct *p, struct inode *inode) | |
2177 | { | |
2178 | struct inode_smack *isp = smack_inode(inode); | |
2179 | struct smack_known *skp = smk_of_task_struct(p); | |
2180 | ||
2181 | isp->smk_inode = skp; | |
2182 | isp->smk_flags |= SMK_INODE_INSTANT; | |
2183 | } | |
2184 | ||
2185 | /* | |
2186 | * Socket hooks. | |
2187 | */ | |
2188 | ||
2189 | /** | |
2190 | * smack_sk_alloc_security - Allocate a socket blob | |
2191 | * @sk: the socket | |
2192 | * @family: unused | |
2193 | * @gfp_flags: memory allocation flags | |
2194 | * | |
2195 | * Assign Smack pointers to current | |
2196 | * | |
2197 | * Returns 0 on success, -ENOMEM is there's no memory | |
2198 | */ | |
2199 | static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags) | |
2200 | { | |
2201 | struct smack_known *skp = smk_of_current(); | |
2202 | struct socket_smack *ssp = smack_sock(sk); | |
2203 | ||
2204 | /* | |
2205 | * Sockets created by kernel threads receive web label. | |
2206 | */ | |
2207 | if (unlikely(current->flags & PF_KTHREAD)) { | |
2208 | ssp->smk_in = &smack_known_web; | |
2209 | ssp->smk_out = &smack_known_web; | |
2210 | } else { | |
2211 | ssp->smk_in = skp; | |
2212 | ssp->smk_out = skp; | |
2213 | } | |
2214 | ssp->smk_packet = NULL; | |
2215 | ||
2216 | return 0; | |
2217 | } | |
2218 | ||
2219 | #ifdef SMACK_IPV6_PORT_LABELING | |
2220 | /** | |
2221 | * smack_sk_free_security - Free a socket blob | |
2222 | * @sk: the socket | |
2223 | * | |
2224 | * Clears the blob pointer | |
2225 | */ | |
2226 | static void smack_sk_free_security(struct sock *sk) | |
2227 | { | |
2228 | struct smk_port_label *spp; | |
2229 | ||
2230 | if (sk->sk_family == PF_INET6) { | |
2231 | rcu_read_lock(); | |
2232 | list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { | |
2233 | if (spp->smk_sock != sk) | |
2234 | continue; | |
2235 | spp->smk_can_reuse = 1; | |
2236 | break; | |
2237 | } | |
2238 | rcu_read_unlock(); | |
2239 | } | |
2240 | } | |
2241 | #endif | |
2242 | ||
2243 | /** | |
2244 | * smack_ipv4host_label - check host based restrictions | |
2245 | * @sip: the object end | |
2246 | * | |
2247 | * looks for host based access restrictions | |
2248 | * | |
2249 | * This version will only be appropriate for really small sets of single label | |
2250 | * hosts. The caller is responsible for ensuring that the RCU read lock is | |
2251 | * taken before calling this function. | |
2252 | * | |
2253 | * Returns the label of the far end or NULL if it's not special. | |
2254 | */ | |
2255 | static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip) | |
2256 | { | |
2257 | struct smk_net4addr *snp; | |
2258 | struct in_addr *siap = &sip->sin_addr; | |
2259 | ||
2260 | if (siap->s_addr == 0) | |
2261 | return NULL; | |
2262 | ||
2263 | list_for_each_entry_rcu(snp, &smk_net4addr_list, list) | |
2264 | /* | |
2265 | * we break after finding the first match because | |
2266 | * the list is sorted from longest to shortest mask | |
2267 | * so we have found the most specific match | |
2268 | */ | |
2269 | if (snp->smk_host.s_addr == | |
2270 | (siap->s_addr & snp->smk_mask.s_addr)) | |
2271 | return snp->smk_label; | |
2272 | ||
2273 | return NULL; | |
2274 | } | |
2275 | ||
2276 | #if IS_ENABLED(CONFIG_IPV6) | |
2277 | /* | |
2278 | * smk_ipv6_localhost - Check for local ipv6 host address | |
2279 | * @sip: the address | |
2280 | * | |
2281 | * Returns boolean true if this is the localhost address | |
2282 | */ | |
2283 | static bool smk_ipv6_localhost(struct sockaddr_in6 *sip) | |
2284 | { | |
2285 | __be16 *be16p = (__be16 *)&sip->sin6_addr; | |
2286 | __be32 *be32p = (__be32 *)&sip->sin6_addr; | |
2287 | ||
2288 | if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 && | |
2289 | ntohs(be16p[7]) == 1) | |
2290 | return true; | |
2291 | return false; | |
2292 | } | |
2293 | ||
2294 | /** | |
2295 | * smack_ipv6host_label - check host based restrictions | |
2296 | * @sip: the object end | |
2297 | * | |
2298 | * looks for host based access restrictions | |
2299 | * | |
2300 | * This version will only be appropriate for really small sets of single label | |
2301 | * hosts. The caller is responsible for ensuring that the RCU read lock is | |
2302 | * taken before calling this function. | |
2303 | * | |
2304 | * Returns the label of the far end or NULL if it's not special. | |
2305 | */ | |
2306 | static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip) | |
2307 | { | |
2308 | struct smk_net6addr *snp; | |
2309 | struct in6_addr *sap = &sip->sin6_addr; | |
2310 | int i; | |
2311 | int found = 0; | |
2312 | ||
2313 | /* | |
2314 | * It's local. Don't look for a host label. | |
2315 | */ | |
2316 | if (smk_ipv6_localhost(sip)) | |
2317 | return NULL; | |
2318 | ||
2319 | list_for_each_entry_rcu(snp, &smk_net6addr_list, list) { | |
2320 | /* | |
2321 | * If the label is NULL the entry has | |
2322 | * been renounced. Ignore it. | |
2323 | */ | |
2324 | if (snp->smk_label == NULL) | |
2325 | continue; | |
2326 | /* | |
2327 | * we break after finding the first match because | |
2328 | * the list is sorted from longest to shortest mask | |
2329 | * so we have found the most specific match | |
2330 | */ | |
2331 | for (found = 1, i = 0; i < 8; i++) { | |
2332 | if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) != | |
2333 | snp->smk_host.s6_addr16[i]) { | |
2334 | found = 0; | |
2335 | break; | |
2336 | } | |
2337 | } | |
2338 | if (found) | |
2339 | return snp->smk_label; | |
2340 | } | |
2341 | ||
2342 | return NULL; | |
2343 | } | |
2344 | #endif /* CONFIG_IPV6 */ | |
2345 | ||
2346 | /** | |
2347 | * smack_netlabel - Set the secattr on a socket | |
2348 | * @sk: the socket | |
2349 | * @labeled: socket label scheme | |
2350 | * | |
2351 | * Convert the outbound smack value (smk_out) to a | |
2352 | * secattr and attach it to the socket. | |
2353 | * | |
2354 | * Returns 0 on success or an error code | |
2355 | */ | |
2356 | static int smack_netlabel(struct sock *sk, int labeled) | |
2357 | { | |
2358 | struct smack_known *skp; | |
2359 | struct socket_smack *ssp = smack_sock(sk); | |
2360 | int rc = 0; | |
2361 | ||
2362 | /* | |
2363 | * Usually the netlabel code will handle changing the | |
2364 | * packet labeling based on the label. | |
2365 | * The case of a single label host is different, because | |
2366 | * a single label host should never get a labeled packet | |
2367 | * even though the label is usually associated with a packet | |
2368 | * label. | |
2369 | */ | |
2370 | local_bh_disable(); | |
2371 | bh_lock_sock_nested(sk); | |
2372 | ||
2373 | if (ssp->smk_out == smack_net_ambient || | |
2374 | labeled == SMACK_UNLABELED_SOCKET) | |
2375 | netlbl_sock_delattr(sk); | |
2376 | else { | |
2377 | skp = ssp->smk_out; | |
2378 | rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel); | |
2379 | } | |
2380 | ||
2381 | bh_unlock_sock(sk); | |
2382 | local_bh_enable(); | |
2383 | ||
2384 | return rc; | |
2385 | } | |
2386 | ||
2387 | /** | |
2388 | * smack_netlbel_send - Set the secattr on a socket and perform access checks | |
2389 | * @sk: the socket | |
2390 | * @sap: the destination address | |
2391 | * | |
2392 | * Set the correct secattr for the given socket based on the destination | |
2393 | * address and perform any outbound access checks needed. | |
2394 | * | |
2395 | * Returns 0 on success or an error code. | |
2396 | * | |
2397 | */ | |
2398 | static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap) | |
2399 | { | |
2400 | struct smack_known *skp; | |
2401 | int rc; | |
2402 | int sk_lbl; | |
2403 | struct smack_known *hkp; | |
2404 | struct socket_smack *ssp = smack_sock(sk); | |
2405 | struct smk_audit_info ad; | |
2406 | ||
2407 | rcu_read_lock(); | |
2408 | hkp = smack_ipv4host_label(sap); | |
2409 | if (hkp != NULL) { | |
2410 | #ifdef CONFIG_AUDIT | |
2411 | struct lsm_network_audit net; | |
2412 | ||
2413 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); | |
2414 | ad.a.u.net->family = sap->sin_family; | |
2415 | ad.a.u.net->dport = sap->sin_port; | |
2416 | ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr; | |
2417 | #endif | |
2418 | sk_lbl = SMACK_UNLABELED_SOCKET; | |
2419 | skp = ssp->smk_out; | |
2420 | rc = smk_access(skp, hkp, MAY_WRITE, &ad); | |
2421 | rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc); | |
2422 | } else { | |
2423 | sk_lbl = SMACK_CIPSO_SOCKET; | |
2424 | rc = 0; | |
2425 | } | |
2426 | rcu_read_unlock(); | |
2427 | if (rc != 0) | |
2428 | return rc; | |
2429 | ||
2430 | return smack_netlabel(sk, sk_lbl); | |
2431 | } | |
2432 | ||
2433 | #if IS_ENABLED(CONFIG_IPV6) | |
2434 | /** | |
2435 | * smk_ipv6_check - check Smack access | |
2436 | * @subject: subject Smack label | |
2437 | * @object: object Smack label | |
2438 | * @address: address | |
2439 | * @act: the action being taken | |
2440 | * | |
2441 | * Check an IPv6 access | |
2442 | */ | |
2443 | static int smk_ipv6_check(struct smack_known *subject, | |
2444 | struct smack_known *object, | |
2445 | struct sockaddr_in6 *address, int act) | |
2446 | { | |
2447 | #ifdef CONFIG_AUDIT | |
2448 | struct lsm_network_audit net; | |
2449 | #endif | |
2450 | struct smk_audit_info ad; | |
2451 | int rc; | |
2452 | ||
2453 | #ifdef CONFIG_AUDIT | |
2454 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); | |
2455 | ad.a.u.net->family = PF_INET6; | |
2456 | ad.a.u.net->dport = ntohs(address->sin6_port); | |
2457 | if (act == SMK_RECEIVING) | |
2458 | ad.a.u.net->v6info.saddr = address->sin6_addr; | |
2459 | else | |
2460 | ad.a.u.net->v6info.daddr = address->sin6_addr; | |
2461 | #endif | |
2462 | rc = smk_access(subject, object, MAY_WRITE, &ad); | |
2463 | rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc); | |
2464 | return rc; | |
2465 | } | |
2466 | #endif /* CONFIG_IPV6 */ | |
2467 | ||
2468 | #ifdef SMACK_IPV6_PORT_LABELING | |
2469 | /** | |
2470 | * smk_ipv6_port_label - Smack port access table management | |
2471 | * @sock: socket | |
2472 | * @address: address | |
2473 | * | |
2474 | * Create or update the port list entry | |
2475 | */ | |
2476 | static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address) | |
2477 | { | |
2478 | struct sock *sk = sock->sk; | |
2479 | struct sockaddr_in6 *addr6; | |
2480 | struct socket_smack *ssp = smack_sock(sock->sk); | |
2481 | struct smk_port_label *spp; | |
2482 | unsigned short port = 0; | |
2483 | ||
2484 | if (address == NULL) { | |
2485 | /* | |
2486 | * This operation is changing the Smack information | |
2487 | * on the bound socket. Take the changes to the port | |
2488 | * as well. | |
2489 | */ | |
2490 | rcu_read_lock(); | |
2491 | list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { | |
2492 | if (sk != spp->smk_sock) | |
2493 | continue; | |
2494 | spp->smk_in = ssp->smk_in; | |
2495 | spp->smk_out = ssp->smk_out; | |
2496 | rcu_read_unlock(); | |
2497 | return; | |
2498 | } | |
2499 | /* | |
2500 | * A NULL address is only used for updating existing | |
2501 | * bound entries. If there isn't one, it's OK. | |
2502 | */ | |
2503 | rcu_read_unlock(); | |
2504 | return; | |
2505 | } | |
2506 | ||
2507 | addr6 = (struct sockaddr_in6 *)address; | |
2508 | port = ntohs(addr6->sin6_port); | |
2509 | /* | |
2510 | * This is a special case that is safely ignored. | |
2511 | */ | |
2512 | if (port == 0) | |
2513 | return; | |
2514 | ||
2515 | /* | |
2516 | * Look for an existing port list entry. | |
2517 | * This is an indication that a port is getting reused. | |
2518 | */ | |
2519 | rcu_read_lock(); | |
2520 | list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { | |
2521 | if (spp->smk_port != port || spp->smk_sock_type != sock->type) | |
2522 | continue; | |
2523 | if (spp->smk_can_reuse != 1) { | |
2524 | rcu_read_unlock(); | |
2525 | return; | |
2526 | } | |
2527 | spp->smk_port = port; | |
2528 | spp->smk_sock = sk; | |
2529 | spp->smk_in = ssp->smk_in; | |
2530 | spp->smk_out = ssp->smk_out; | |
2531 | spp->smk_can_reuse = 0; | |
2532 | rcu_read_unlock(); | |
2533 | return; | |
2534 | } | |
2535 | rcu_read_unlock(); | |
2536 | /* | |
2537 | * A new port entry is required. | |
2538 | */ | |
2539 | spp = kzalloc(sizeof(*spp), GFP_KERNEL); | |
2540 | if (spp == NULL) | |
2541 | return; | |
2542 | ||
2543 | spp->smk_port = port; | |
2544 | spp->smk_sock = sk; | |
2545 | spp->smk_in = ssp->smk_in; | |
2546 | spp->smk_out = ssp->smk_out; | |
2547 | spp->smk_sock_type = sock->type; | |
2548 | spp->smk_can_reuse = 0; | |
2549 | ||
2550 | mutex_lock(&smack_ipv6_lock); | |
2551 | list_add_rcu(&spp->list, &smk_ipv6_port_list); | |
2552 | mutex_unlock(&smack_ipv6_lock); | |
2553 | return; | |
2554 | } | |
2555 | ||
2556 | /** | |
2557 | * smk_ipv6_port_check - check Smack port access | |
2558 | * @sock: socket | |
2559 | * @address: address | |
2560 | * | |
2561 | * Create or update the port list entry | |
2562 | */ | |
2563 | static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address, | |
2564 | int act) | |
2565 | { | |
2566 | struct smk_port_label *spp; | |
2567 | struct socket_smack *ssp = smack_sock(sk); | |
2568 | struct smack_known *skp = NULL; | |
2569 | unsigned short port; | |
2570 | struct smack_known *object; | |
2571 | ||
2572 | if (act == SMK_RECEIVING) { | |
2573 | skp = smack_ipv6host_label(address); | |
2574 | object = ssp->smk_in; | |
2575 | } else { | |
2576 | skp = ssp->smk_out; | |
2577 | object = smack_ipv6host_label(address); | |
2578 | } | |
2579 | ||
2580 | /* | |
2581 | * The other end is a single label host. | |
2582 | */ | |
2583 | if (skp != NULL && object != NULL) | |
2584 | return smk_ipv6_check(skp, object, address, act); | |
2585 | if (skp == NULL) | |
2586 | skp = smack_net_ambient; | |
2587 | if (object == NULL) | |
2588 | object = smack_net_ambient; | |
2589 | ||
2590 | /* | |
2591 | * It's remote, so port lookup does no good. | |
2592 | */ | |
2593 | if (!smk_ipv6_localhost(address)) | |
2594 | return smk_ipv6_check(skp, object, address, act); | |
2595 | ||
2596 | /* | |
2597 | * It's local so the send check has to have passed. | |
2598 | */ | |
2599 | if (act == SMK_RECEIVING) | |
2600 | return 0; | |
2601 | ||
2602 | port = ntohs(address->sin6_port); | |
2603 | rcu_read_lock(); | |
2604 | list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) { | |
2605 | if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type) | |
2606 | continue; | |
2607 | object = spp->smk_in; | |
2608 | if (act == SMK_CONNECTING) | |
2609 | ssp->smk_packet = spp->smk_out; | |
2610 | break; | |
2611 | } | |
2612 | rcu_read_unlock(); | |
2613 | ||
2614 | return smk_ipv6_check(skp, object, address, act); | |
2615 | } | |
2616 | #endif /* SMACK_IPV6_PORT_LABELING */ | |
2617 | ||
2618 | /** | |
2619 | * smack_inode_setsecurity - set smack xattrs | |
2620 | * @inode: the object | |
2621 | * @name: attribute name | |
2622 | * @value: attribute value | |
2623 | * @size: size of the attribute | |
2624 | * @flags: unused | |
2625 | * | |
2626 | * Sets the named attribute in the appropriate blob | |
2627 | * | |
2628 | * Returns 0 on success, or an error code | |
2629 | */ | |
2630 | static int smack_inode_setsecurity(struct inode *inode, const char *name, | |
2631 | const void *value, size_t size, int flags) | |
2632 | { | |
2633 | struct smack_known *skp; | |
2634 | struct inode_smack *nsp = smack_inode(inode); | |
2635 | struct socket_smack *ssp; | |
2636 | struct socket *sock; | |
2637 | int rc = 0; | |
2638 | ||
2639 | if (value == NULL || size > SMK_LONGLABEL || size == 0) | |
2640 | return -EINVAL; | |
2641 | ||
2642 | skp = smk_import_entry(value, size); | |
2643 | if (IS_ERR(skp)) | |
2644 | return PTR_ERR(skp); | |
2645 | ||
2646 | if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) { | |
2647 | nsp->smk_inode = skp; | |
2648 | nsp->smk_flags |= SMK_INODE_INSTANT; | |
2649 | return 0; | |
2650 | } | |
2651 | /* | |
2652 | * The rest of the Smack xattrs are only on sockets. | |
2653 | */ | |
2654 | if (inode->i_sb->s_magic != SOCKFS_MAGIC) | |
2655 | return -EOPNOTSUPP; | |
2656 | ||
2657 | sock = SOCKET_I(inode); | |
2658 | if (sock == NULL || sock->sk == NULL) | |
2659 | return -EOPNOTSUPP; | |
2660 | ||
2661 | ssp = smack_sock(sock->sk); | |
2662 | ||
2663 | if (strcmp(name, XATTR_SMACK_IPIN) == 0) | |
2664 | ssp->smk_in = skp; | |
2665 | else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) { | |
2666 | ssp->smk_out = skp; | |
2667 | if (sock->sk->sk_family == PF_INET) { | |
2668 | rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET); | |
2669 | if (rc != 0) | |
2670 | printk(KERN_WARNING | |
2671 | "Smack: \"%s\" netlbl error %d.\n", | |
2672 | __func__, -rc); | |
2673 | } | |
2674 | } else | |
2675 | return -EOPNOTSUPP; | |
2676 | ||
2677 | #ifdef SMACK_IPV6_PORT_LABELING | |
2678 | if (sock->sk->sk_family == PF_INET6) | |
2679 | smk_ipv6_port_label(sock, NULL); | |
2680 | #endif | |
2681 | ||
2682 | return 0; | |
2683 | } | |
2684 | ||
2685 | /** | |
2686 | * smack_socket_post_create - finish socket setup | |
2687 | * @sock: the socket | |
2688 | * @family: protocol family | |
2689 | * @type: unused | |
2690 | * @protocol: unused | |
2691 | * @kern: unused | |
2692 | * | |
2693 | * Sets the netlabel information on the socket | |
2694 | * | |
2695 | * Returns 0 on success, and error code otherwise | |
2696 | */ | |
2697 | static int smack_socket_post_create(struct socket *sock, int family, | |
2698 | int type, int protocol, int kern) | |
2699 | { | |
2700 | struct socket_smack *ssp; | |
2701 | ||
2702 | if (sock->sk == NULL) | |
2703 | return 0; | |
2704 | ||
2705 | /* | |
2706 | * Sockets created by kernel threads receive web label. | |
2707 | */ | |
2708 | if (unlikely(current->flags & PF_KTHREAD)) { | |
2709 | ssp = smack_sock(sock->sk); | |
2710 | ssp->smk_in = &smack_known_web; | |
2711 | ssp->smk_out = &smack_known_web; | |
2712 | } | |
2713 | ||
2714 | if (family != PF_INET) | |
2715 | return 0; | |
2716 | /* | |
2717 | * Set the outbound netlbl. | |
2718 | */ | |
2719 | return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET); | |
2720 | } | |
2721 | ||
2722 | #ifdef SMACK_IPV6_PORT_LABELING | |
2723 | /** | |
2724 | * smack_socket_bind - record port binding information. | |
2725 | * @sock: the socket | |
2726 | * @address: the port address | |
2727 | * @addrlen: size of the address | |
2728 | * | |
2729 | * Records the label bound to a port. | |
2730 | * | |
2731 | * Returns 0 | |
2732 | */ | |
2733 | static int smack_socket_bind(struct socket *sock, struct sockaddr *address, | |
2734 | int addrlen) | |
2735 | { | |
2736 | if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) | |
2737 | smk_ipv6_port_label(sock, address); | |
2738 | return 0; | |
2739 | } | |
2740 | #endif /* SMACK_IPV6_PORT_LABELING */ | |
2741 | ||
2742 | /** | |
2743 | * smack_socket_connect - connect access check | |
2744 | * @sock: the socket | |
2745 | * @sap: the other end | |
2746 | * @addrlen: size of sap | |
2747 | * | |
2748 | * Verifies that a connection may be possible | |
2749 | * | |
2750 | * Returns 0 on success, and error code otherwise | |
2751 | */ | |
2752 | static int smack_socket_connect(struct socket *sock, struct sockaddr *sap, | |
2753 | int addrlen) | |
2754 | { | |
2755 | int rc = 0; | |
2756 | #if IS_ENABLED(CONFIG_IPV6) | |
2757 | struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap; | |
2758 | #endif | |
2759 | #ifdef SMACK_IPV6_SECMARK_LABELING | |
2760 | struct smack_known *rsp; | |
2761 | struct socket_smack *ssp = sock->sk->sk_security; | |
2762 | #endif | |
2763 | ||
2764 | if (sock->sk == NULL) | |
2765 | return 0; | |
2766 | ||
2767 | switch (sock->sk->sk_family) { | |
2768 | case PF_INET: | |
2769 | if (addrlen < sizeof(struct sockaddr_in)) | |
2770 | return -EINVAL; | |
2771 | rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap); | |
2772 | break; | |
2773 | case PF_INET6: | |
2774 | if (addrlen < sizeof(struct sockaddr_in6)) | |
2775 | return -EINVAL; | |
2776 | #ifdef SMACK_IPV6_SECMARK_LABELING | |
2777 | rsp = smack_ipv6host_label(sip); | |
2778 | if (rsp != NULL) | |
2779 | rc = smk_ipv6_check(ssp->smk_out, rsp, sip, | |
2780 | SMK_CONNECTING); | |
2781 | #endif | |
2782 | #ifdef SMACK_IPV6_PORT_LABELING | |
2783 | rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING); | |
2784 | #endif | |
2785 | break; | |
2786 | } | |
2787 | return rc; | |
2788 | } | |
2789 | ||
2790 | /** | |
2791 | * smack_flags_to_may - convert S_ to MAY_ values | |
2792 | * @flags: the S_ value | |
2793 | * | |
2794 | * Returns the equivalent MAY_ value | |
2795 | */ | |
2796 | static int smack_flags_to_may(int flags) | |
2797 | { | |
2798 | int may = 0; | |
2799 | ||
2800 | if (flags & S_IRUGO) | |
2801 | may |= MAY_READ; | |
2802 | if (flags & S_IWUGO) | |
2803 | may |= MAY_WRITE; | |
2804 | if (flags & S_IXUGO) | |
2805 | may |= MAY_EXEC; | |
2806 | ||
2807 | return may; | |
2808 | } | |
2809 | ||
2810 | /** | |
2811 | * smack_msg_msg_alloc_security - Set the security blob for msg_msg | |
2812 | * @msg: the object | |
2813 | * | |
2814 | * Returns 0 | |
2815 | */ | |
2816 | static int smack_msg_msg_alloc_security(struct msg_msg *msg) | |
2817 | { | |
2818 | struct smack_known **blob = smack_msg_msg(msg); | |
2819 | ||
2820 | *blob = smk_of_current(); | |
2821 | return 0; | |
2822 | } | |
2823 | ||
2824 | /** | |
2825 | * smack_shm_alloc_security - Set the security blob for shm | |
2826 | * @shp: the object | |
2827 | * | |
2828 | * Returns 0 | |
2829 | */ | |
2830 | static int smack_shm_alloc_security(struct shmid_kernel *shp) | |
2831 | { | |
2832 | struct smack_known **blob = smack_ipc(&shp->shm_perm); | |
2833 | ||
2834 | *blob = smk_of_current(); | |
2835 | return 0; | |
2836 | } | |
2837 | ||
2838 | /** | |
2839 | * smk_curacc_shm : check if current has access on shm | |
2840 | * @shp : the object | |
2841 | * @access : access requested | |
2842 | * | |
2843 | * Returns 0 if current has the requested access, error code otherwise | |
2844 | */ | |
2845 | static int smk_curacc_shm(struct shmid_kernel *shp, int access) | |
2846 | { | |
2847 | struct smack_known **blob = smack_ipc(&shp->shm_perm); | |
2848 | struct smack_known *ssp = *blob; | |
2849 | struct smk_audit_info ad; | |
2850 | int rc; | |
2851 | ||
2852 | #ifdef CONFIG_AUDIT | |
2853 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); | |
2854 | ad.a.u.ipc_id = shp->shm_perm.id; | |
2855 | #endif | |
2856 | rc = smk_curacc(ssp, access, &ad); | |
2857 | rc = smk_bu_current("shm", ssp, access, rc); | |
2858 | return rc; | |
2859 | } | |
2860 | ||
2861 | /** | |
2862 | * smack_shm_associate - Smack access check for shm | |
2863 | * @shp: the object | |
2864 | * @shmflg: access requested | |
2865 | * | |
2866 | * Returns 0 if current has the requested access, error code otherwise | |
2867 | */ | |
2868 | static int smack_shm_associate(struct shmid_kernel *shp, int shmflg) | |
2869 | { | |
2870 | int may; | |
2871 | ||
2872 | may = smack_flags_to_may(shmflg); | |
2873 | return smk_curacc_shm(shp, may); | |
2874 | } | |
2875 | ||
2876 | /** | |
2877 | * smack_shm_shmctl - Smack access check for shm | |
2878 | * @shp: the object | |
2879 | * @cmd: what it wants to do | |
2880 | * | |
2881 | * Returns 0 if current has the requested access, error code otherwise | |
2882 | */ | |
2883 | static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd) | |
2884 | { | |
2885 | int may; | |
2886 | ||
2887 | switch (cmd) { | |
2888 | case IPC_STAT: | |
2889 | case SHM_STAT: | |
2890 | may = MAY_READ; | |
2891 | break; | |
2892 | case IPC_SET: | |
2893 | case SHM_LOCK: | |
2894 | case SHM_UNLOCK: | |
2895 | case IPC_RMID: | |
2896 | may = MAY_READWRITE; | |
2897 | break; | |
2898 | case IPC_INFO: | |
2899 | case SHM_INFO: | |
2900 | /* | |
2901 | * System level information. | |
2902 | */ | |
2903 | return 0; | |
2904 | default: | |
2905 | return -EINVAL; | |
2906 | } | |
2907 | return smk_curacc_shm(shp, may); | |
2908 | } | |
2909 | ||
2910 | /** | |
2911 | * smack_shm_shmat - Smack access for shmat | |
2912 | * @shp: the object | |
2913 | * @shmaddr: unused | |
2914 | * @shmflg: access requested | |
2915 | * | |
2916 | * Returns 0 if current has the requested access, error code otherwise | |
2917 | */ | |
2918 | static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr, | |
2919 | int shmflg) | |
2920 | { | |
2921 | int may; | |
2922 | ||
2923 | may = smack_flags_to_may(shmflg); | |
2924 | return smk_curacc_shm(shp, may); | |
2925 | } | |
2926 | ||
2927 | /** | |
2928 | * smack_sem_alloc_security - Set the security blob for sem | |
2929 | * @sma: the object | |
2930 | * | |
2931 | * Returns 0 | |
2932 | */ | |
2933 | static int smack_sem_alloc_security(struct sem_array *sma) | |
2934 | { | |
2935 | struct smack_known **blob = smack_ipc(&sma->sem_perm); | |
2936 | ||
2937 | *blob = smk_of_current(); | |
2938 | return 0; | |
2939 | } | |
2940 | ||
2941 | /** | |
2942 | * smk_curacc_sem : check if current has access on sem | |
2943 | * @sma : the object | |
2944 | * @access : access requested | |
2945 | * | |
2946 | * Returns 0 if current has the requested access, error code otherwise | |
2947 | */ | |
2948 | static int smk_curacc_sem(struct sem_array *sma, int access) | |
2949 | { | |
2950 | struct smack_known **blob = smack_ipc(&sma->sem_perm); | |
2951 | struct smack_known *ssp = *blob; | |
2952 | struct smk_audit_info ad; | |
2953 | int rc; | |
2954 | ||
2955 | #ifdef CONFIG_AUDIT | |
2956 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); | |
2957 | ad.a.u.ipc_id = sma->sem_perm.id; | |
2958 | #endif | |
2959 | rc = smk_curacc(ssp, access, &ad); | |
2960 | rc = smk_bu_current("sem", ssp, access, rc); | |
2961 | return rc; | |
2962 | } | |
2963 | ||
2964 | /** | |
2965 | * smack_sem_associate - Smack access check for sem | |
2966 | * @sma: the object | |
2967 | * @semflg: access requested | |
2968 | * | |
2969 | * Returns 0 if current has the requested access, error code otherwise | |
2970 | */ | |
2971 | static int smack_sem_associate(struct sem_array *sma, int semflg) | |
2972 | { | |
2973 | int may; | |
2974 | ||
2975 | may = smack_flags_to_may(semflg); | |
2976 | return smk_curacc_sem(sma, may); | |
2977 | } | |
2978 | ||
2979 | /** | |
2980 | * smack_sem_shmctl - Smack access check for sem | |
2981 | * @sma: the object | |
2982 | * @cmd: what it wants to do | |
2983 | * | |
2984 | * Returns 0 if current has the requested access, error code otherwise | |
2985 | */ | |
2986 | static int smack_sem_semctl(struct sem_array *sma, int cmd) | |
2987 | { | |
2988 | int may; | |
2989 | ||
2990 | switch (cmd) { | |
2991 | case GETPID: | |
2992 | case GETNCNT: | |
2993 | case GETZCNT: | |
2994 | case GETVAL: | |
2995 | case GETALL: | |
2996 | case IPC_STAT: | |
2997 | case SEM_STAT: | |
2998 | may = MAY_READ; | |
2999 | break; | |
3000 | case SETVAL: | |
3001 | case SETALL: | |
3002 | case IPC_RMID: | |
3003 | case IPC_SET: | |
3004 | may = MAY_READWRITE; | |
3005 | break; | |
3006 | case IPC_INFO: | |
3007 | case SEM_INFO: | |
3008 | /* | |
3009 | * System level information | |
3010 | */ | |
3011 | return 0; | |
3012 | default: | |
3013 | return -EINVAL; | |
3014 | } | |
3015 | ||
3016 | return smk_curacc_sem(sma, may); | |
3017 | } | |
3018 | ||
3019 | /** | |
3020 | * smack_sem_semop - Smack checks of semaphore operations | |
3021 | * @sma: the object | |
3022 | * @sops: unused | |
3023 | * @nsops: unused | |
3024 | * @alter: unused | |
3025 | * | |
3026 | * Treated as read and write in all cases. | |
3027 | * | |
3028 | * Returns 0 if access is allowed, error code otherwise | |
3029 | */ | |
3030 | static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops, | |
3031 | unsigned nsops, int alter) | |
3032 | { | |
3033 | return smk_curacc_sem(sma, MAY_READWRITE); | |
3034 | } | |
3035 | ||
3036 | /** | |
3037 | * smack_msg_queue_alloc_security - Set the security blob for msg | |
3038 | * @msq: the object | |
3039 | * | |
3040 | * Returns 0 | |
3041 | */ | |
3042 | static int smack_msg_queue_alloc_security(struct msg_queue *msq) | |
3043 | { | |
3044 | struct smack_known **blob = smack_ipc(&msq->q_perm); | |
3045 | ||
3046 | *blob = smk_of_current(); | |
3047 | return 0; | |
3048 | } | |
3049 | ||
3050 | /** | |
3051 | * smk_curacc_msq : helper to check if current has access on msq | |
3052 | * @msq : the msq | |
3053 | * @access : access requested | |
3054 | * | |
3055 | * return 0 if current has access, error otherwise | |
3056 | */ | |
3057 | static int smk_curacc_msq(struct msg_queue *msq, int access) | |
3058 | { | |
3059 | struct smack_known **blob = smack_ipc(&msq->q_perm); | |
3060 | struct smack_known *msp = *blob; | |
3061 | struct smk_audit_info ad; | |
3062 | int rc; | |
3063 | ||
3064 | #ifdef CONFIG_AUDIT | |
3065 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); | |
3066 | ad.a.u.ipc_id = msq->q_perm.id; | |
3067 | #endif | |
3068 | rc = smk_curacc(msp, access, &ad); | |
3069 | rc = smk_bu_current("msq", msp, access, rc); | |
3070 | return rc; | |
3071 | } | |
3072 | ||
3073 | /** | |
3074 | * smack_msg_queue_associate - Smack access check for msg_queue | |
3075 | * @msq: the object | |
3076 | * @msqflg: access requested | |
3077 | * | |
3078 | * Returns 0 if current has the requested access, error code otherwise | |
3079 | */ | |
3080 | static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg) | |
3081 | { | |
3082 | int may; | |
3083 | ||
3084 | may = smack_flags_to_may(msqflg); | |
3085 | return smk_curacc_msq(msq, may); | |
3086 | } | |
3087 | ||
3088 | /** | |
3089 | * smack_msg_queue_msgctl - Smack access check for msg_queue | |
3090 | * @msq: the object | |
3091 | * @cmd: what it wants to do | |
3092 | * | |
3093 | * Returns 0 if current has the requested access, error code otherwise | |
3094 | */ | |
3095 | static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd) | |
3096 | { | |
3097 | int may; | |
3098 | ||
3099 | switch (cmd) { | |
3100 | case IPC_STAT: | |
3101 | case MSG_STAT: | |
3102 | may = MAY_READ; | |
3103 | break; | |
3104 | case IPC_SET: | |
3105 | case IPC_RMID: | |
3106 | may = MAY_READWRITE; | |
3107 | break; | |
3108 | case IPC_INFO: | |
3109 | case MSG_INFO: | |
3110 | /* | |
3111 | * System level information | |
3112 | */ | |
3113 | return 0; | |
3114 | default: | |
3115 | return -EINVAL; | |
3116 | } | |
3117 | ||
3118 | return smk_curacc_msq(msq, may); | |
3119 | } | |
3120 | ||
3121 | /** | |
3122 | * smack_msg_queue_msgsnd - Smack access check for msg_queue | |
3123 | * @msq: the object | |
3124 | * @msg: unused | |
3125 | * @msqflg: access requested | |
3126 | * | |
3127 | * Returns 0 if current has the requested access, error code otherwise | |
3128 | */ | |
3129 | static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, | |
3130 | int msqflg) | |
3131 | { | |
3132 | int may; | |
3133 | ||
3134 | may = smack_flags_to_may(msqflg); | |
3135 | return smk_curacc_msq(msq, may); | |
3136 | } | |
3137 | ||
3138 | /** | |
3139 | * smack_msg_queue_msgsnd - Smack access check for msg_queue | |
3140 | * @msq: the object | |
3141 | * @msg: unused | |
3142 | * @target: unused | |
3143 | * @type: unused | |
3144 | * @mode: unused | |
3145 | * | |
3146 | * Returns 0 if current has read and write access, error code otherwise | |
3147 | */ | |
3148 | static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, | |
3149 | struct task_struct *target, long type, int mode) | |
3150 | { | |
3151 | return smk_curacc_msq(msq, MAY_READWRITE); | |
3152 | } | |
3153 | ||
3154 | /** | |
3155 | * smack_ipc_permission - Smack access for ipc_permission() | |
3156 | * @ipp: the object permissions | |
3157 | * @flag: access requested | |
3158 | * | |
3159 | * Returns 0 if current has read and write access, error code otherwise | |
3160 | */ | |
3161 | static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag) | |
3162 | { | |
3163 | struct smack_known **blob = smack_ipc(ipp); | |
3164 | struct smack_known *iskp = *blob; | |
3165 | int may = smack_flags_to_may(flag); | |
3166 | struct smk_audit_info ad; | |
3167 | int rc; | |
3168 | ||
3169 | #ifdef CONFIG_AUDIT | |
3170 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC); | |
3171 | ad.a.u.ipc_id = ipp->id; | |
3172 | #endif | |
3173 | rc = smk_curacc(iskp, may, &ad); | |
3174 | rc = smk_bu_current("svipc", iskp, may, rc); | |
3175 | return rc; | |
3176 | } | |
3177 | ||
3178 | /** | |
3179 | * smack_ipc_getsecid - Extract smack security id | |
3180 | * @ipp: the object permissions | |
3181 | * @secid: where result will be saved | |
3182 | */ | |
3183 | static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid) | |
3184 | { | |
3185 | struct smack_known **blob = smack_ipc(ipp); | |
3186 | struct smack_known *iskp = *blob; | |
3187 | ||
3188 | *secid = iskp->smk_secid; | |
3189 | } | |
3190 | ||
3191 | /** | |
3192 | * smack_d_instantiate - Make sure the blob is correct on an inode | |
3193 | * @opt_dentry: dentry where inode will be attached | |
3194 | * @inode: the object | |
3195 | * | |
3196 | * Set the inode's security blob if it hasn't been done already. | |
3197 | */ | |
3198 | static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode) | |
3199 | { | |
3200 | struct super_block *sbp; | |
3201 | struct superblock_smack *sbsp; | |
3202 | struct inode_smack *isp; | |
3203 | struct smack_known *skp; | |
3204 | struct smack_known *ckp = smk_of_current(); | |
3205 | struct smack_known *final; | |
3206 | char trattr[TRANS_TRUE_SIZE]; | |
3207 | int transflag = 0; | |
3208 | int rc; | |
3209 | struct dentry *dp; | |
3210 | ||
3211 | if (inode == NULL) | |
3212 | return; | |
3213 | ||
3214 | isp = smack_inode(inode); | |
3215 | ||
3216 | mutex_lock(&isp->smk_lock); | |
3217 | /* | |
3218 | * If the inode is already instantiated | |
3219 | * take the quick way out | |
3220 | */ | |
3221 | if (isp->smk_flags & SMK_INODE_INSTANT) | |
3222 | goto unlockandout; | |
3223 | ||
3224 | sbp = inode->i_sb; | |
3225 | sbsp = smack_superblock(sbp); | |
3226 | /* | |
3227 | * We're going to use the superblock default label | |
3228 | * if there's no label on the file. | |
3229 | */ | |
3230 | final = sbsp->smk_default; | |
3231 | ||
3232 | /* | |
3233 | * If this is the root inode the superblock | |
3234 | * may be in the process of initialization. | |
3235 | * If that is the case use the root value out | |
3236 | * of the superblock. | |
3237 | */ | |
3238 | if (opt_dentry->d_parent == opt_dentry) { | |
3239 | switch (sbp->s_magic) { | |
3240 | case CGROUP_SUPER_MAGIC: | |
3241 | /* | |
3242 | * The cgroup filesystem is never mounted, | |
3243 | * so there's no opportunity to set the mount | |
3244 | * options. | |
3245 | */ | |
3246 | sbsp->smk_root = &smack_known_star; | |
3247 | sbsp->smk_default = &smack_known_star; | |
3248 | isp->smk_inode = sbsp->smk_root; | |
3249 | break; | |
3250 | case TMPFS_MAGIC: | |
3251 | /* | |
3252 | * What about shmem/tmpfs anonymous files with dentry | |
3253 | * obtained from d_alloc_pseudo()? | |
3254 | */ | |
3255 | isp->smk_inode = smk_of_current(); | |
3256 | break; | |
3257 | case PIPEFS_MAGIC: | |
3258 | isp->smk_inode = smk_of_current(); | |
3259 | break; | |
3260 | case SOCKFS_MAGIC: | |
3261 | /* | |
3262 | * Socket access is controlled by the socket | |
3263 | * structures associated with the task involved. | |
3264 | */ | |
3265 | isp->smk_inode = &smack_known_star; | |
3266 | break; | |
3267 | default: | |
3268 | isp->smk_inode = sbsp->smk_root; | |
3269 | break; | |
3270 | } | |
3271 | isp->smk_flags |= SMK_INODE_INSTANT; | |
3272 | goto unlockandout; | |
3273 | } | |
3274 | ||
3275 | /* | |
3276 | * This is pretty hackish. | |
3277 | * Casey says that we shouldn't have to do | |
3278 | * file system specific code, but it does help | |
3279 | * with keeping it simple. | |
3280 | */ | |
3281 | switch (sbp->s_magic) { | |
3282 | case SMACK_MAGIC: | |
3283 | case CGROUP_SUPER_MAGIC: | |
3284 | /* | |
3285 | * Casey says that it's a little embarrassing | |
3286 | * that the smack file system doesn't do | |
3287 | * extended attributes. | |
3288 | * | |
3289 | * Cgroupfs is special | |
3290 | */ | |
3291 | final = &smack_known_star; | |
3292 | break; | |
3293 | case DEVPTS_SUPER_MAGIC: | |
3294 | /* | |
3295 | * devpts seems content with the label of the task. | |
3296 | * Programs that change smack have to treat the | |
3297 | * pty with respect. | |
3298 | */ | |
3299 | final = ckp; | |
3300 | break; | |
3301 | case PROC_SUPER_MAGIC: | |
3302 | /* | |
3303 | * Casey says procfs appears not to care. | |
3304 | * The superblock default suffices. | |
3305 | */ | |
3306 | break; | |
3307 | case TMPFS_MAGIC: | |
3308 | /* | |
3309 | * Device labels should come from the filesystem, | |
3310 | * but watch out, because they're volitile, | |
3311 | * getting recreated on every reboot. | |
3312 | */ | |
3313 | final = &smack_known_star; | |
3314 | /* | |
3315 | * No break. | |
3316 | * | |
3317 | * If a smack value has been set we want to use it, | |
3318 | * but since tmpfs isn't giving us the opportunity | |
3319 | * to set mount options simulate setting the | |
3320 | * superblock default. | |
3321 | */ | |
3322 | default: | |
3323 | /* | |
3324 | * This isn't an understood special case. | |
3325 | * Get the value from the xattr. | |
3326 | */ | |
3327 | ||
3328 | /* | |
3329 | * UNIX domain sockets use lower level socket data. | |
3330 | */ | |
3331 | if (S_ISSOCK(inode->i_mode)) { | |
3332 | final = &smack_known_star; | |
3333 | break; | |
3334 | } | |
3335 | /* | |
3336 | * No xattr support means, alas, no SMACK label. | |
3337 | * Use the aforeapplied default. | |
3338 | * It would be curious if the label of the task | |
3339 | * does not match that assigned. | |
3340 | */ | |
3341 | if (!(inode->i_opflags & IOP_XATTR)) | |
3342 | break; | |
3343 | /* | |
3344 | * Get the dentry for xattr. | |
3345 | */ | |
3346 | dp = dget(opt_dentry); | |
3347 | skp = smk_fetch(XATTR_NAME_SMACK, inode, dp); | |
3348 | if (!IS_ERR_OR_NULL(skp)) | |
3349 | final = skp; | |
3350 | ||
3351 | /* | |
3352 | * Transmuting directory | |
3353 | */ | |
3354 | if (S_ISDIR(inode->i_mode)) { | |
3355 | /* | |
3356 | * If this is a new directory and the label was | |
3357 | * transmuted when the inode was initialized | |
3358 | * set the transmute attribute on the directory | |
3359 | * and mark the inode. | |
3360 | * | |
3361 | * If there is a transmute attribute on the | |
3362 | * directory mark the inode. | |
3363 | */ | |
3364 | if (isp->smk_flags & SMK_INODE_CHANGED) { | |
3365 | isp->smk_flags &= ~SMK_INODE_CHANGED; | |
3366 | rc = __vfs_setxattr(dp, inode, | |
3367 | XATTR_NAME_SMACKTRANSMUTE, | |
3368 | TRANS_TRUE, TRANS_TRUE_SIZE, | |
3369 | 0); | |
3370 | } else { | |
3371 | rc = __vfs_getxattr(dp, inode, | |
3372 | XATTR_NAME_SMACKTRANSMUTE, trattr, | |
3373 | TRANS_TRUE_SIZE); | |
3374 | if (rc >= 0 && strncmp(trattr, TRANS_TRUE, | |
3375 | TRANS_TRUE_SIZE) != 0) | |
3376 | rc = -EINVAL; | |
3377 | } | |
3378 | if (rc >= 0) | |
3379 | transflag = SMK_INODE_TRANSMUTE; | |
3380 | } | |
3381 | /* | |
3382 | * Don't let the exec or mmap label be "*" or "@". | |
3383 | */ | |
3384 | skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp); | |
3385 | if (IS_ERR(skp) || skp == &smack_known_star || | |
3386 | skp == &smack_known_web) | |
3387 | skp = NULL; | |
3388 | isp->smk_task = skp; | |
3389 | ||
3390 | skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp); | |
3391 | if (IS_ERR(skp) || skp == &smack_known_star || | |
3392 | skp == &smack_known_web) | |
3393 | skp = NULL; | |
3394 | isp->smk_mmap = skp; | |
3395 | ||
3396 | dput(dp); | |
3397 | break; | |
3398 | } | |
3399 | ||
3400 | if (final == NULL) | |
3401 | isp->smk_inode = ckp; | |
3402 | else | |
3403 | isp->smk_inode = final; | |
3404 | ||
3405 | isp->smk_flags |= (SMK_INODE_INSTANT | transflag); | |
3406 | ||
3407 | unlockandout: | |
3408 | mutex_unlock(&isp->smk_lock); | |
3409 | return; | |
3410 | } | |
3411 | ||
3412 | /** | |
3413 | * smack_getprocattr - Smack process attribute access | |
3414 | * @p: the object task | |
3415 | * @name: the name of the attribute in /proc/.../attr | |
3416 | * @value: where to put the result | |
3417 | * | |
3418 | * Places a copy of the task Smack into value | |
3419 | * | |
3420 | * Returns the length of the smack label or an error code | |
3421 | */ | |
3422 | static int smack_getprocattr(struct task_struct *p, char *name, char **value) | |
3423 | { | |
3424 | struct smack_known *skp = smk_of_task_struct(p); | |
3425 | char *cp; | |
3426 | ||
3427 | if (strcmp(name, "current") == 0) { | |
3428 | cp = kstrdup(skp->smk_known, GFP_KERNEL); | |
3429 | if (cp == NULL) | |
3430 | return -ENOMEM; | |
3431 | } else | |
3432 | return -EINVAL; | |
3433 | ||
3434 | *value = cp; | |
3435 | return strlen(cp); | |
3436 | } | |
3437 | ||
3438 | /** | |
3439 | * smack_setprocattr - Smack process attribute setting | |
3440 | * @name: the name of the attribute in /proc/.../attr | |
3441 | * @value: the value to set | |
3442 | * @size: the size of the value | |
3443 | * | |
3444 | * Sets the Smack value of the task. Only setting self | |
3445 | * is permitted and only with privilege | |
3446 | * | |
3447 | * Returns the length of the smack label or an error code | |
3448 | */ | |
3449 | static int smack_setprocattr(const char *name, void *value, size_t size) | |
3450 | { | |
3451 | struct task_smack *tsp = smack_cred(current_cred()); | |
3452 | struct cred *new; | |
3453 | struct smack_known *skp; | |
3454 | struct smack_known_list_elem *sklep; | |
3455 | int rc; | |
3456 | ||
3457 | if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel)) | |
3458 | return -EPERM; | |
3459 | ||
3460 | if (value == NULL || size == 0 || size >= SMK_LONGLABEL) | |
3461 | return -EINVAL; | |
3462 | ||
3463 | if (strcmp(name, "current") != 0) | |
3464 | return -EINVAL; | |
3465 | ||
3466 | skp = smk_import_entry(value, size); | |
3467 | if (IS_ERR(skp)) | |
3468 | return PTR_ERR(skp); | |
3469 | ||
3470 | /* | |
3471 | * No process is ever allowed the web ("@") label | |
3472 | * and the star ("*") label. | |
3473 | */ | |
3474 | if (skp == &smack_known_web || skp == &smack_known_star) | |
3475 | return -EINVAL; | |
3476 | ||
3477 | if (!smack_privileged(CAP_MAC_ADMIN)) { | |
3478 | rc = -EPERM; | |
3479 | list_for_each_entry(sklep, &tsp->smk_relabel, list) | |
3480 | if (sklep->smk_label == skp) { | |
3481 | rc = 0; | |
3482 | break; | |
3483 | } | |
3484 | if (rc) | |
3485 | return rc; | |
3486 | } | |
3487 | ||
3488 | new = prepare_creds(); | |
3489 | if (new == NULL) | |
3490 | return -ENOMEM; | |
3491 | ||
3492 | tsp = smack_cred(new); | |
3493 | tsp->smk_task = skp; | |
3494 | /* | |
3495 | * process can change its label only once | |
3496 | */ | |
3497 | smk_destroy_label_list(&tsp->smk_relabel); | |
3498 | ||
3499 | commit_creds(new); | |
3500 | return size; | |
3501 | } | |
3502 | ||
3503 | /** | |
3504 | * smack_unix_stream_connect - Smack access on UDS | |
3505 | * @sock: one sock | |
3506 | * @other: the other sock | |
3507 | * @newsk: unused | |
3508 | * | |
3509 | * Return 0 if a subject with the smack of sock could access | |
3510 | * an object with the smack of other, otherwise an error code | |
3511 | */ | |
3512 | static int smack_unix_stream_connect(struct sock *sock, | |
3513 | struct sock *other, struct sock *newsk) | |
3514 | { | |
3515 | struct smack_known *skp; | |
3516 | struct smack_known *okp; | |
3517 | struct socket_smack *ssp = smack_sock(sock); | |
3518 | struct socket_smack *osp = smack_sock(other); | |
3519 | struct socket_smack *nsp = smack_sock(newsk); | |
3520 | struct smk_audit_info ad; | |
3521 | int rc = 0; | |
3522 | #ifdef CONFIG_AUDIT | |
3523 | struct lsm_network_audit net; | |
3524 | #endif | |
3525 | ||
3526 | if (!smack_privileged(CAP_MAC_OVERRIDE)) { | |
3527 | skp = ssp->smk_out; | |
3528 | okp = osp->smk_in; | |
3529 | #ifdef CONFIG_AUDIT | |
3530 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); | |
3531 | smk_ad_setfield_u_net_sk(&ad, other); | |
3532 | #endif | |
3533 | rc = smk_access(skp, okp, MAY_WRITE, &ad); | |
3534 | rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc); | |
3535 | if (rc == 0) { | |
3536 | okp = osp->smk_out; | |
3537 | skp = ssp->smk_in; | |
3538 | rc = smk_access(okp, skp, MAY_WRITE, &ad); | |
3539 | rc = smk_bu_note("UDS connect", okp, skp, | |
3540 | MAY_WRITE, rc); | |
3541 | } | |
3542 | } | |
3543 | ||
3544 | /* | |
3545 | * Cross reference the peer labels for SO_PEERSEC. | |
3546 | */ | |
3547 | if (rc == 0) { | |
3548 | nsp->smk_packet = ssp->smk_out; | |
3549 | ssp->smk_packet = osp->smk_out; | |
3550 | } | |
3551 | ||
3552 | return rc; | |
3553 | } | |
3554 | ||
3555 | /** | |
3556 | * smack_unix_may_send - Smack access on UDS | |
3557 | * @sock: one socket | |
3558 | * @other: the other socket | |
3559 | * | |
3560 | * Return 0 if a subject with the smack of sock could access | |
3561 | * an object with the smack of other, otherwise an error code | |
3562 | */ | |
3563 | static int smack_unix_may_send(struct socket *sock, struct socket *other) | |
3564 | { | |
3565 | struct socket_smack *ssp = smack_sock(sock->sk); | |
3566 | struct socket_smack *osp = smack_sock(other->sk); | |
3567 | struct smk_audit_info ad; | |
3568 | int rc; | |
3569 | ||
3570 | #ifdef CONFIG_AUDIT | |
3571 | struct lsm_network_audit net; | |
3572 | ||
3573 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); | |
3574 | smk_ad_setfield_u_net_sk(&ad, other->sk); | |
3575 | #endif | |
3576 | ||
3577 | if (smack_privileged(CAP_MAC_OVERRIDE)) | |
3578 | return 0; | |
3579 | ||
3580 | rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad); | |
3581 | rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc); | |
3582 | return rc; | |
3583 | } | |
3584 | ||
3585 | /** | |
3586 | * smack_socket_sendmsg - Smack check based on destination host | |
3587 | * @sock: the socket | |
3588 | * @msg: the message | |
3589 | * @size: the size of the message | |
3590 | * | |
3591 | * Return 0 if the current subject can write to the destination host. | |
3592 | * For IPv4 this is only a question if the destination is a single label host. | |
3593 | * For IPv6 this is a check against the label of the port. | |
3594 | */ | |
3595 | static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg, | |
3596 | int size) | |
3597 | { | |
3598 | struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name; | |
3599 | #if IS_ENABLED(CONFIG_IPV6) | |
3600 | struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name; | |
3601 | #endif | |
3602 | #ifdef SMACK_IPV6_SECMARK_LABELING | |
3603 | struct socket_smack *ssp = smack_sock(sock->sk); | |
3604 | struct smack_known *rsp; | |
3605 | #endif | |
3606 | int rc = 0; | |
3607 | ||
3608 | /* | |
3609 | * Perfectly reasonable for this to be NULL | |
3610 | */ | |
3611 | if (sip == NULL) | |
3612 | return 0; | |
3613 | ||
3614 | switch (sock->sk->sk_family) { | |
3615 | case AF_INET: | |
3616 | rc = smack_netlabel_send(sock->sk, sip); | |
3617 | break; | |
3618 | case AF_INET6: | |
3619 | #ifdef SMACK_IPV6_SECMARK_LABELING | |
3620 | rsp = smack_ipv6host_label(sap); | |
3621 | if (rsp != NULL) | |
3622 | rc = smk_ipv6_check(ssp->smk_out, rsp, sap, | |
3623 | SMK_CONNECTING); | |
3624 | #endif | |
3625 | #ifdef SMACK_IPV6_PORT_LABELING | |
3626 | rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING); | |
3627 | #endif | |
3628 | break; | |
3629 | } | |
3630 | return rc; | |
3631 | } | |
3632 | ||
3633 | /** | |
3634 | * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack | |
3635 | * @sap: netlabel secattr | |
3636 | * @ssp: socket security information | |
3637 | * | |
3638 | * Returns a pointer to a Smack label entry found on the label list. | |
3639 | */ | |
3640 | static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap, | |
3641 | struct socket_smack *ssp) | |
3642 | { | |
3643 | struct smack_known *skp; | |
3644 | int found = 0; | |
3645 | int acat; | |
3646 | int kcat; | |
3647 | ||
3648 | if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) { | |
3649 | /* | |
3650 | * Looks like a CIPSO packet. | |
3651 | * If there are flags but no level netlabel isn't | |
3652 | * behaving the way we expect it to. | |
3653 | * | |
3654 | * Look it up in the label table | |
3655 | * Without guidance regarding the smack value | |
3656 | * for the packet fall back on the network | |
3657 | * ambient value. | |
3658 | */ | |
3659 | rcu_read_lock(); | |
3660 | list_for_each_entry_rcu(skp, &smack_known_list, list) { | |
3661 | if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl) | |
3662 | continue; | |
3663 | /* | |
3664 | * Compare the catsets. Use the netlbl APIs. | |
3665 | */ | |
3666 | if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) { | |
3667 | if ((skp->smk_netlabel.flags & | |
3668 | NETLBL_SECATTR_MLS_CAT) == 0) | |
3669 | found = 1; | |
3670 | break; | |
3671 | } | |
3672 | for (acat = -1, kcat = -1; acat == kcat; ) { | |
3673 | acat = netlbl_catmap_walk(sap->attr.mls.cat, | |
3674 | acat + 1); | |
3675 | kcat = netlbl_catmap_walk( | |
3676 | skp->smk_netlabel.attr.mls.cat, | |
3677 | kcat + 1); | |
3678 | if (acat < 0 || kcat < 0) | |
3679 | break; | |
3680 | } | |
3681 | if (acat == kcat) { | |
3682 | found = 1; | |
3683 | break; | |
3684 | } | |
3685 | } | |
3686 | rcu_read_unlock(); | |
3687 | ||
3688 | if (found) | |
3689 | return skp; | |
3690 | ||
3691 | if (ssp != NULL && ssp->smk_in == &smack_known_star) | |
3692 | return &smack_known_web; | |
3693 | return &smack_known_star; | |
3694 | } | |
3695 | if ((sap->flags & NETLBL_SECATTR_SECID) != 0) | |
3696 | /* | |
3697 | * Looks like a fallback, which gives us a secid. | |
3698 | */ | |
3699 | return smack_from_secid(sap->attr.secid); | |
3700 | /* | |
3701 | * Without guidance regarding the smack value | |
3702 | * for the packet fall back on the network | |
3703 | * ambient value. | |
3704 | */ | |
3705 | return smack_net_ambient; | |
3706 | } | |
3707 | ||
3708 | #if IS_ENABLED(CONFIG_IPV6) | |
3709 | static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip) | |
3710 | { | |
3711 | u8 nexthdr; | |
3712 | int offset; | |
3713 | int proto = -EINVAL; | |
3714 | struct ipv6hdr _ipv6h; | |
3715 | struct ipv6hdr *ip6; | |
3716 | __be16 frag_off; | |
3717 | struct tcphdr _tcph, *th; | |
3718 | struct udphdr _udph, *uh; | |
3719 | struct dccp_hdr _dccph, *dh; | |
3720 | ||
3721 | sip->sin6_port = 0; | |
3722 | ||
3723 | offset = skb_network_offset(skb); | |
3724 | ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); | |
3725 | if (ip6 == NULL) | |
3726 | return -EINVAL; | |
3727 | sip->sin6_addr = ip6->saddr; | |
3728 | ||
3729 | nexthdr = ip6->nexthdr; | |
3730 | offset += sizeof(_ipv6h); | |
3731 | offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); | |
3732 | if (offset < 0) | |
3733 | return -EINVAL; | |
3734 | ||
3735 | proto = nexthdr; | |
3736 | switch (proto) { | |
3737 | case IPPROTO_TCP: | |
3738 | th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); | |
3739 | if (th != NULL) | |
3740 | sip->sin6_port = th->source; | |
3741 | break; | |
3742 | case IPPROTO_UDP: | |
3743 | uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); | |
3744 | if (uh != NULL) | |
3745 | sip->sin6_port = uh->source; | |
3746 | break; | |
3747 | case IPPROTO_DCCP: | |
3748 | dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); | |
3749 | if (dh != NULL) | |
3750 | sip->sin6_port = dh->dccph_sport; | |
3751 | break; | |
3752 | } | |
3753 | return proto; | |
3754 | } | |
3755 | #endif /* CONFIG_IPV6 */ | |
3756 | ||
3757 | /** | |
3758 | * smack_socket_sock_rcv_skb - Smack packet delivery access check | |
3759 | * @sk: socket | |
3760 | * @skb: packet | |
3761 | * | |
3762 | * Returns 0 if the packet should be delivered, an error code otherwise | |
3763 | */ | |
3764 | static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) | |
3765 | { | |
3766 | struct netlbl_lsm_secattr secattr; | |
3767 | struct socket_smack *ssp = smack_sock(sk); | |
3768 | struct smack_known *skp = NULL; | |
3769 | int rc = 0; | |
3770 | struct smk_audit_info ad; | |
3771 | u16 family = sk->sk_family; | |
3772 | #ifdef CONFIG_AUDIT | |
3773 | struct lsm_network_audit net; | |
3774 | #endif | |
3775 | #if IS_ENABLED(CONFIG_IPV6) | |
3776 | struct sockaddr_in6 sadd; | |
3777 | int proto; | |
3778 | ||
3779 | if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) | |
3780 | family = PF_INET; | |
3781 | #endif /* CONFIG_IPV6 */ | |
3782 | ||
3783 | switch (family) { | |
3784 | case PF_INET: | |
3785 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER | |
3786 | /* | |
3787 | * If there is a secmark use it rather than the CIPSO label. | |
3788 | * If there is no secmark fall back to CIPSO. | |
3789 | * The secmark is assumed to reflect policy better. | |
3790 | */ | |
3791 | if (skb && skb->secmark != 0) { | |
3792 | skp = smack_from_secid(skb->secmark); | |
3793 | goto access_check; | |
3794 | } | |
3795 | #endif /* CONFIG_SECURITY_SMACK_NETFILTER */ | |
3796 | /* | |
3797 | * Translate what netlabel gave us. | |
3798 | */ | |
3799 | netlbl_secattr_init(&secattr); | |
3800 | ||
3801 | rc = netlbl_skbuff_getattr(skb, family, &secattr); | |
3802 | if (rc == 0) | |
3803 | skp = smack_from_secattr(&secattr, ssp); | |
3804 | else | |
3805 | skp = smack_net_ambient; | |
3806 | ||
3807 | netlbl_secattr_destroy(&secattr); | |
3808 | ||
3809 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER | |
3810 | access_check: | |
3811 | #endif | |
3812 | #ifdef CONFIG_AUDIT | |
3813 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); | |
3814 | ad.a.u.net->family = family; | |
3815 | ad.a.u.net->netif = skb->skb_iif; | |
3816 | ipv4_skb_to_auditdata(skb, &ad.a, NULL); | |
3817 | #endif | |
3818 | /* | |
3819 | * Receiving a packet requires that the other end | |
3820 | * be able to write here. Read access is not required. | |
3821 | * This is the simplist possible security model | |
3822 | * for networking. | |
3823 | */ | |
3824 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); | |
3825 | rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in, | |
3826 | MAY_WRITE, rc); | |
3827 | if (rc != 0) | |
3828 | netlbl_skbuff_err(skb, family, rc, 0); | |
3829 | break; | |
3830 | #if IS_ENABLED(CONFIG_IPV6) | |
3831 | case PF_INET6: | |
3832 | proto = smk_skb_to_addr_ipv6(skb, &sadd); | |
3833 | if (proto != IPPROTO_UDP && proto != IPPROTO_TCP) | |
3834 | break; | |
3835 | #ifdef SMACK_IPV6_SECMARK_LABELING | |
3836 | if (skb && skb->secmark != 0) | |
3837 | skp = smack_from_secid(skb->secmark); | |
3838 | else | |
3839 | skp = smack_ipv6host_label(&sadd); | |
3840 | if (skp == NULL) | |
3841 | skp = smack_net_ambient; | |
3842 | if (skb == NULL) | |
3843 | break; | |
3844 | #ifdef CONFIG_AUDIT | |
3845 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); | |
3846 | ad.a.u.net->family = family; | |
3847 | ad.a.u.net->netif = skb->skb_iif; | |
3848 | ipv6_skb_to_auditdata(skb, &ad.a, NULL); | |
3849 | #endif /* CONFIG_AUDIT */ | |
3850 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); | |
3851 | rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in, | |
3852 | MAY_WRITE, rc); | |
3853 | #endif /* SMACK_IPV6_SECMARK_LABELING */ | |
3854 | #ifdef SMACK_IPV6_PORT_LABELING | |
3855 | rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING); | |
3856 | #endif /* SMACK_IPV6_PORT_LABELING */ | |
3857 | break; | |
3858 | #endif /* CONFIG_IPV6 */ | |
3859 | } | |
3860 | ||
3861 | return rc; | |
3862 | } | |
3863 | ||
3864 | /** | |
3865 | * smack_socket_getpeersec_stream - pull in packet label | |
3866 | * @sock: the socket | |
3867 | * @optval: user's destination | |
3868 | * @optlen: size thereof | |
3869 | * @len: max thereof | |
3870 | * | |
3871 | * returns zero on success, an error code otherwise | |
3872 | */ | |
3873 | static int smack_socket_getpeersec_stream(struct socket *sock, | |
3874 | char __user *optval, | |
3875 | int __user *optlen, unsigned len) | |
3876 | { | |
3877 | struct socket_smack *ssp; | |
3878 | char *rcp = ""; | |
3879 | int slen = 1; | |
3880 | int rc = 0; | |
3881 | ||
3882 | ssp = smack_sock(sock->sk); | |
3883 | if (ssp->smk_packet != NULL) { | |
3884 | rcp = ssp->smk_packet->smk_known; | |
3885 | slen = strlen(rcp) + 1; | |
3886 | } | |
3887 | ||
3888 | if (slen > len) | |
3889 | rc = -ERANGE; | |
3890 | else if (copy_to_user(optval, rcp, slen) != 0) | |
3891 | rc = -EFAULT; | |
3892 | ||
3893 | if (put_user(slen, optlen) != 0) | |
3894 | rc = -EFAULT; | |
3895 | ||
3896 | return rc; | |
3897 | } | |
3898 | ||
3899 | ||
3900 | /** | |
3901 | * smack_socket_getpeersec_dgram - pull in packet label | |
3902 | * @sock: the peer socket | |
3903 | * @skb: packet data | |
3904 | * @secid: pointer to where to put the secid of the packet | |
3905 | * | |
3906 | * Sets the netlabel socket state on sk from parent | |
3907 | */ | |
3908 | static int smack_socket_getpeersec_dgram(struct socket *sock, | |
3909 | struct sk_buff *skb, u32 *secid) | |
3910 | ||
3911 | { | |
3912 | struct netlbl_lsm_secattr secattr; | |
3913 | struct socket_smack *ssp = NULL; | |
3914 | struct smack_known *skp; | |
3915 | int family = PF_UNSPEC; | |
3916 | u32 s = 0; /* 0 is the invalid secid */ | |
3917 | int rc; | |
3918 | ||
3919 | if (skb != NULL) { | |
3920 | if (skb->protocol == htons(ETH_P_IP)) | |
3921 | family = PF_INET; | |
3922 | #if IS_ENABLED(CONFIG_IPV6) | |
3923 | else if (skb->protocol == htons(ETH_P_IPV6)) | |
3924 | family = PF_INET6; | |
3925 | #endif /* CONFIG_IPV6 */ | |
3926 | } | |
3927 | if (family == PF_UNSPEC && sock != NULL) | |
3928 | family = sock->sk->sk_family; | |
3929 | ||
3930 | switch (family) { | |
3931 | case PF_UNIX: | |
3932 | ssp = smack_sock(sock->sk); | |
3933 | s = ssp->smk_out->smk_secid; | |
3934 | break; | |
3935 | case PF_INET: | |
3936 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER | |
3937 | s = skb->secmark; | |
3938 | if (s != 0) | |
3939 | break; | |
3940 | #endif | |
3941 | /* | |
3942 | * Translate what netlabel gave us. | |
3943 | */ | |
3944 | if (sock != NULL && sock->sk != NULL) | |
3945 | ssp = smack_sock(sock->sk); | |
3946 | netlbl_secattr_init(&secattr); | |
3947 | rc = netlbl_skbuff_getattr(skb, family, &secattr); | |
3948 | if (rc == 0) { | |
3949 | skp = smack_from_secattr(&secattr, ssp); | |
3950 | s = skp->smk_secid; | |
3951 | } | |
3952 | netlbl_secattr_destroy(&secattr); | |
3953 | break; | |
3954 | case PF_INET6: | |
3955 | #ifdef SMACK_IPV6_SECMARK_LABELING | |
3956 | s = skb->secmark; | |
3957 | #endif | |
3958 | break; | |
3959 | } | |
3960 | *secid = s; | |
3961 | if (s == 0) | |
3962 | return -EINVAL; | |
3963 | return 0; | |
3964 | } | |
3965 | ||
3966 | /** | |
3967 | * smack_sock_graft - Initialize a newly created socket with an existing sock | |
3968 | * @sk: child sock | |
3969 | * @parent: parent socket | |
3970 | * | |
3971 | * Set the smk_{in,out} state of an existing sock based on the process that | |
3972 | * is creating the new socket. | |
3973 | */ | |
3974 | static void smack_sock_graft(struct sock *sk, struct socket *parent) | |
3975 | { | |
3976 | struct socket_smack *ssp; | |
3977 | struct smack_known *skp = smk_of_current(); | |
3978 | ||
3979 | if (sk == NULL || | |
3980 | (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)) | |
3981 | return; | |
3982 | ||
3983 | ssp = smack_sock(sk); | |
3984 | ssp->smk_in = skp; | |
3985 | ssp->smk_out = skp; | |
3986 | /* cssp->smk_packet is already set in smack_inet_csk_clone() */ | |
3987 | } | |
3988 | ||
3989 | /** | |
3990 | * smack_inet_conn_request - Smack access check on connect | |
3991 | * @sk: socket involved | |
3992 | * @skb: packet | |
3993 | * @req: unused | |
3994 | * | |
3995 | * Returns 0 if a task with the packet label could write to | |
3996 | * the socket, otherwise an error code | |
3997 | */ | |
3998 | static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb, | |
3999 | struct request_sock *req) | |
4000 | { | |
4001 | u16 family = sk->sk_family; | |
4002 | struct smack_known *skp; | |
4003 | struct socket_smack *ssp = smack_sock(sk); | |
4004 | struct netlbl_lsm_secattr secattr; | |
4005 | struct sockaddr_in addr; | |
4006 | struct iphdr *hdr; | |
4007 | struct smack_known *hskp; | |
4008 | int rc; | |
4009 | struct smk_audit_info ad; | |
4010 | #ifdef CONFIG_AUDIT | |
4011 | struct lsm_network_audit net; | |
4012 | #endif | |
4013 | ||
4014 | #if IS_ENABLED(CONFIG_IPV6) | |
4015 | if (family == PF_INET6) { | |
4016 | /* | |
4017 | * Handle mapped IPv4 packets arriving | |
4018 | * via IPv6 sockets. Don't set up netlabel | |
4019 | * processing on IPv6. | |
4020 | */ | |
4021 | if (skb->protocol == htons(ETH_P_IP)) | |
4022 | family = PF_INET; | |
4023 | else | |
4024 | return 0; | |
4025 | } | |
4026 | #endif /* CONFIG_IPV6 */ | |
4027 | ||
4028 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER | |
4029 | /* | |
4030 | * If there is a secmark use it rather than the CIPSO label. | |
4031 | * If there is no secmark fall back to CIPSO. | |
4032 | * The secmark is assumed to reflect policy better. | |
4033 | */ | |
4034 | if (skb && skb->secmark != 0) { | |
4035 | skp = smack_from_secid(skb->secmark); | |
4036 | goto access_check; | |
4037 | } | |
4038 | #endif /* CONFIG_SECURITY_SMACK_NETFILTER */ | |
4039 | ||
4040 | netlbl_secattr_init(&secattr); | |
4041 | rc = netlbl_skbuff_getattr(skb, family, &secattr); | |
4042 | if (rc == 0) | |
4043 | skp = smack_from_secattr(&secattr, ssp); | |
4044 | else | |
4045 | skp = &smack_known_huh; | |
4046 | netlbl_secattr_destroy(&secattr); | |
4047 | ||
4048 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER | |
4049 | access_check: | |
4050 | #endif | |
4051 | ||
4052 | #ifdef CONFIG_AUDIT | |
4053 | smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net); | |
4054 | ad.a.u.net->family = family; | |
4055 | ad.a.u.net->netif = skb->skb_iif; | |
4056 | ipv4_skb_to_auditdata(skb, &ad.a, NULL); | |
4057 | #endif | |
4058 | /* | |
4059 | * Receiving a packet requires that the other end be able to write | |
4060 | * here. Read access is not required. | |
4061 | */ | |
4062 | rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad); | |
4063 | rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc); | |
4064 | if (rc != 0) | |
4065 | return rc; | |
4066 | ||
4067 | /* | |
4068 | * Save the peer's label in the request_sock so we can later setup | |
4069 | * smk_packet in the child socket so that SO_PEERCRED can report it. | |
4070 | */ | |
4071 | req->peer_secid = skp->smk_secid; | |
4072 | ||
4073 | /* | |
4074 | * We need to decide if we want to label the incoming connection here | |
4075 | * if we do we only need to label the request_sock and the stack will | |
4076 | * propagate the wire-label to the sock when it is created. | |
4077 | */ | |
4078 | hdr = ip_hdr(skb); | |
4079 | addr.sin_addr.s_addr = hdr->saddr; | |
4080 | rcu_read_lock(); | |
4081 | hskp = smack_ipv4host_label(&addr); | |
4082 | rcu_read_unlock(); | |
4083 | ||
4084 | if (hskp == NULL) | |
4085 | rc = netlbl_req_setattr(req, &skp->smk_netlabel); | |
4086 | else | |
4087 | netlbl_req_delattr(req); | |
4088 | ||
4089 | return rc; | |
4090 | } | |
4091 | ||
4092 | /** | |
4093 | * smack_inet_csk_clone - Copy the connection information to the new socket | |
4094 | * @sk: the new socket | |
4095 | * @req: the connection's request_sock | |
4096 | * | |
4097 | * Transfer the connection's peer label to the newly created socket. | |
4098 | */ | |
4099 | static void smack_inet_csk_clone(struct sock *sk, | |
4100 | const struct request_sock *req) | |
4101 | { | |
4102 | struct socket_smack *ssp = smack_sock(sk); | |
4103 | struct smack_known *skp; | |
4104 | ||
4105 | if (req->peer_secid != 0) { | |
4106 | skp = smack_from_secid(req->peer_secid); | |
4107 | ssp->smk_packet = skp; | |
4108 | } else | |
4109 | ssp->smk_packet = NULL; | |
4110 | } | |
4111 | ||
4112 | /* | |
4113 | * Key management security hooks | |
4114 | * | |
4115 | * Casey has not tested key support very heavily. | |
4116 | * The permission check is most likely too restrictive. | |
4117 | * If you care about keys please have a look. | |
4118 | */ | |
4119 | #ifdef CONFIG_KEYS | |
4120 | ||
4121 | /** | |
4122 | * smack_key_alloc - Set the key security blob | |
4123 | * @key: object | |
4124 | * @cred: the credentials to use | |
4125 | * @flags: unused | |
4126 | * | |
4127 | * No allocation required | |
4128 | * | |
4129 | * Returns 0 | |
4130 | */ | |
4131 | static int smack_key_alloc(struct key *key, const struct cred *cred, | |
4132 | unsigned long flags) | |
4133 | { | |
4134 | struct smack_known **blob = smack_key(key); | |
4135 | struct smack_known *skp = smk_of_task(smack_cred(cred)); | |
4136 | ||
4137 | *blob = skp; | |
4138 | return 0; | |
4139 | } | |
4140 | ||
4141 | /** | |
4142 | * smack_key_permission - Smack access on a key | |
4143 | * @key_ref: gets to the object | |
4144 | * @cred: the credentials to use | |
4145 | * @perm: requested key permissions | |
4146 | * | |
4147 | * Return 0 if the task has read and write to the object, | |
4148 | * an error code otherwise | |
4149 | */ | |
4150 | static int smack_key_permission(key_ref_t key_ref, | |
4151 | const struct cred *cred, unsigned perm) | |
4152 | { | |
4153 | struct smack_known **blob; | |
4154 | struct smack_known *skp; | |
4155 | struct key *keyp; | |
4156 | struct smk_audit_info ad; | |
4157 | struct smack_known *tkp = smk_of_task(smack_cred(cred)); | |
4158 | int request = 0; | |
4159 | int rc; | |
4160 | ||
4161 | /* | |
4162 | * Validate requested permissions | |
4163 | */ | |
4164 | if (perm & ~KEY_NEED_ALL) | |
4165 | return -EINVAL; | |
4166 | ||
4167 | keyp = key_ref_to_ptr(key_ref); | |
4168 | if (keyp == NULL) | |
4169 | return -EINVAL; | |
4170 | /* | |
4171 | * If the key hasn't been initialized give it access so that | |
4172 | * it may do so. | |
4173 | */ | |
4174 | blob = smack_key(keyp); | |
4175 | skp = *blob; | |
4176 | if (skp == NULL) | |
4177 | return 0; | |
4178 | /* | |
4179 | * This should not occur | |
4180 | */ | |
4181 | if (tkp == NULL) | |
4182 | return -EACCES; | |
4183 | #ifdef CONFIG_AUDIT | |
4184 | smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY); | |
4185 | ad.a.u.key_struct.key = keyp->serial; | |
4186 | ad.a.u.key_struct.key_desc = keyp->description; | |
4187 | #endif | |
4188 | if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW)) | |
4189 | request |= MAY_READ; | |
4190 | if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR)) | |
4191 | request |= MAY_WRITE; | |
4192 | rc = smk_access(tkp, skp, request, &ad); | |
4193 | rc = smk_bu_note("key access", tkp, skp, request, rc); | |
4194 | return rc; | |
4195 | } | |
4196 | ||
4197 | /* | |
4198 | * smack_key_getsecurity - Smack label tagging the key | |
4199 | * @key points to the key to be queried | |
4200 | * @_buffer points to a pointer that should be set to point to the | |
4201 | * resulting string (if no label or an error occurs). | |
4202 | * Return the length of the string (including terminating NUL) or -ve if | |
4203 | * an error. | |
4204 | * May also return 0 (and a NULL buffer pointer) if there is no label. | |
4205 | */ | |
4206 | static int smack_key_getsecurity(struct key *key, char **_buffer) | |
4207 | { | |
4208 | struct smack_known **blob = smack_key(key); | |
4209 | struct smack_known *skp = *blob; | |
4210 | size_t length; | |
4211 | char *copy; | |
4212 | ||
4213 | if (skp == NULL) { | |
4214 | *_buffer = NULL; | |
4215 | return 0; | |
4216 | } | |
4217 | ||
4218 | copy = kstrdup(skp->smk_known, GFP_KERNEL); | |
4219 | if (copy == NULL) | |
4220 | return -ENOMEM; | |
4221 | length = strlen(copy) + 1; | |
4222 | ||
4223 | *_buffer = copy; | |
4224 | return length; | |
4225 | } | |
4226 | ||
4227 | #endif /* CONFIG_KEYS */ | |
4228 | ||
4229 | /* | |
4230 | * Smack Audit hooks | |
4231 | * | |
4232 | * Audit requires a unique representation of each Smack specific | |
4233 | * rule. This unique representation is used to distinguish the | |
4234 | * object to be audited from remaining kernel objects and also | |
4235 | * works as a glue between the audit hooks. | |
4236 | * | |
4237 | * Since repository entries are added but never deleted, we'll use | |
4238 | * the smack_known label address related to the given audit rule as | |
4239 | * the needed unique representation. This also better fits the smack | |
4240 | * model where nearly everything is a label. | |
4241 | */ | |
4242 | #ifdef CONFIG_AUDIT | |
4243 | ||
4244 | /** | |
4245 | * smack_audit_rule_init - Initialize a smack audit rule | |
4246 | * @field: audit rule fields given from user-space (audit.h) | |
4247 | * @op: required testing operator (=, !=, >, <, ...) | |
4248 | * @rulestr: smack label to be audited | |
4249 | * @vrule: pointer to save our own audit rule representation | |
4250 | * | |
4251 | * Prepare to audit cases where (@field @op @rulestr) is true. | |
4252 | * The label to be audited is created if necessay. | |
4253 | */ | |
4254 | static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) | |
4255 | { | |
4256 | struct smack_known *skp; | |
4257 | char **rule = (char **)vrule; | |
4258 | *rule = NULL; | |
4259 | ||
4260 | if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) | |
4261 | return -EINVAL; | |
4262 | ||
4263 | if (op != Audit_equal && op != Audit_not_equal) | |
4264 | return -EINVAL; | |
4265 | ||
4266 | skp = smk_import_entry(rulestr, 0); | |
4267 | if (IS_ERR(skp)) | |
4268 | return PTR_ERR(skp); | |
4269 | ||
4270 | *rule = skp->smk_known; | |
4271 | ||
4272 | return 0; | |
4273 | } | |
4274 | ||
4275 | /** | |
4276 | * smack_audit_rule_known - Distinguish Smack audit rules | |
4277 | * @krule: rule of interest, in Audit kernel representation format | |
4278 | * | |
4279 | * This is used to filter Smack rules from remaining Audit ones. | |
4280 | * If it's proved that this rule belongs to us, the | |
4281 | * audit_rule_match hook will be called to do the final judgement. | |
4282 | */ | |
4283 | static int smack_audit_rule_known(struct audit_krule *krule) | |
4284 | { | |
4285 | struct audit_field *f; | |
4286 | int i; | |
4287 | ||
4288 | for (i = 0; i < krule->field_count; i++) { | |
4289 | f = &krule->fields[i]; | |
4290 | ||
4291 | if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER) | |
4292 | return 1; | |
4293 | } | |
4294 | ||
4295 | return 0; | |
4296 | } | |
4297 | ||
4298 | /** | |
4299 | * smack_audit_rule_match - Audit given object ? | |
4300 | * @secid: security id for identifying the object to test | |
4301 | * @field: audit rule flags given from user-space | |
4302 | * @op: required testing operator | |
4303 | * @vrule: smack internal rule presentation | |
4304 | * @actx: audit context associated with the check | |
4305 | * | |
4306 | * The core Audit hook. It's used to take the decision of | |
4307 | * whether to audit or not to audit a given object. | |
4308 | */ | |
4309 | static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule, | |
4310 | struct audit_context *actx) | |
4311 | { | |
4312 | struct smack_known *skp; | |
4313 | char *rule = vrule; | |
4314 | ||
4315 | if (unlikely(!rule)) { | |
4316 | WARN_ONCE(1, "Smack: missing rule\n"); | |
4317 | return -ENOENT; | |
4318 | } | |
4319 | ||
4320 | if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER) | |
4321 | return 0; | |
4322 | ||
4323 | skp = smack_from_secid(secid); | |
4324 | ||
4325 | /* | |
4326 | * No need to do string comparisons. If a match occurs, | |
4327 | * both pointers will point to the same smack_known | |
4328 | * label. | |
4329 | */ | |
4330 | if (op == Audit_equal) | |
4331 | return (rule == skp->smk_known); | |
4332 | if (op == Audit_not_equal) | |
4333 | return (rule != skp->smk_known); | |
4334 | ||
4335 | return 0; | |
4336 | } | |
4337 | ||
4338 | /* | |
4339 | * There is no need for a smack_audit_rule_free hook. | |
4340 | * No memory was allocated. | |
4341 | */ | |
4342 | ||
4343 | #endif /* CONFIG_AUDIT */ | |
4344 | ||
4345 | /** | |
4346 | * smack_ismaclabel - check if xattr @name references a smack MAC label | |
4347 | * @name: Full xattr name to check. | |
4348 | */ | |
4349 | static int smack_ismaclabel(const char *name) | |
4350 | { | |
4351 | return (strcmp(name, XATTR_SMACK_SUFFIX) == 0); | |
4352 | } | |
4353 | ||
4354 | ||
4355 | /** | |
4356 | * smack_secid_to_secctx - return the smack label for a secid | |
4357 | * @secid: incoming integer | |
4358 | * @secdata: destination | |
4359 | * @seclen: how long it is | |
4360 | * | |
4361 | * Exists for networking code. | |
4362 | */ | |
4363 | static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) | |
4364 | { | |
4365 | struct smack_known *skp = smack_from_secid(secid); | |
4366 | ||
4367 | if (secdata) | |
4368 | *secdata = skp->smk_known; | |
4369 | *seclen = strlen(skp->smk_known); | |
4370 | return 0; | |
4371 | } | |
4372 | ||
4373 | /** | |
4374 | * smack_secctx_to_secid - return the secid for a smack label | |
4375 | * @secdata: smack label | |
4376 | * @seclen: how long result is | |
4377 | * @secid: outgoing integer | |
4378 | * | |
4379 | * Exists for audit and networking code. | |
4380 | */ | |
4381 | static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) | |
4382 | { | |
4383 | struct smack_known *skp = smk_find_entry(secdata); | |
4384 | ||
4385 | if (skp) | |
4386 | *secid = skp->smk_secid; | |
4387 | else | |
4388 | *secid = 0; | |
4389 | return 0; | |
4390 | } | |
4391 | ||
4392 | /* | |
4393 | * There used to be a smack_release_secctx hook | |
4394 | * that did nothing back when hooks were in a vector. | |
4395 | * Now that there's a list such a hook adds cost. | |
4396 | */ | |
4397 | ||
4398 | static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) | |
4399 | { | |
4400 | return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0); | |
4401 | } | |
4402 | ||
4403 | static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) | |
4404 | { | |
4405 | return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0); | |
4406 | } | |
4407 | ||
4408 | static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) | |
4409 | { | |
4410 | int len = 0; | |
4411 | len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true); | |
4412 | ||
4413 | if (len < 0) | |
4414 | return len; | |
4415 | *ctxlen = len; | |
4416 | return 0; | |
4417 | } | |
4418 | ||
4419 | static int smack_inode_copy_up(struct dentry *dentry, struct cred **new) | |
4420 | { | |
4421 | ||
4422 | struct task_smack *tsp; | |
4423 | struct smack_known *skp; | |
4424 | struct inode_smack *isp; | |
4425 | struct cred *new_creds = *new; | |
4426 | ||
4427 | if (new_creds == NULL) { | |
4428 | new_creds = prepare_creds(); | |
4429 | if (new_creds == NULL) | |
4430 | return -ENOMEM; | |
4431 | } | |
4432 | ||
4433 | tsp = smack_cred(new_creds); | |
4434 | ||
4435 | /* | |
4436 | * Get label from overlay inode and set it in create_sid | |
4437 | */ | |
4438 | isp = smack_inode(d_inode(dentry->d_parent)); | |
4439 | skp = isp->smk_inode; | |
4440 | tsp->smk_task = skp; | |
4441 | *new = new_creds; | |
4442 | return 0; | |
4443 | } | |
4444 | ||
4445 | static int smack_inode_copy_up_xattr(const char *name) | |
4446 | { | |
4447 | /* | |
4448 | * Return 1 if this is the smack access Smack attribute. | |
4449 | */ | |
4450 | if (strcmp(name, XATTR_NAME_SMACK) == 0) | |
4451 | return 1; | |
4452 | ||
4453 | return -EOPNOTSUPP; | |
4454 | } | |
4455 | ||
4456 | static int smack_dentry_create_files_as(struct dentry *dentry, int mode, | |
4457 | struct qstr *name, | |
4458 | const struct cred *old, | |
4459 | struct cred *new) | |
4460 | { | |
4461 | struct task_smack *otsp = smack_cred(old); | |
4462 | struct task_smack *ntsp = smack_cred(new); | |
4463 | struct inode_smack *isp; | |
4464 | int may; | |
4465 | ||
4466 | /* | |
4467 | * Use the process credential unless all of | |
4468 | * the transmuting criteria are met | |
4469 | */ | |
4470 | ntsp->smk_task = otsp->smk_task; | |
4471 | ||
4472 | /* | |
4473 | * the attribute of the containing directory | |
4474 | */ | |
4475 | isp = smack_inode(d_inode(dentry->d_parent)); | |
4476 | ||
4477 | if (isp->smk_flags & SMK_INODE_TRANSMUTE) { | |
4478 | rcu_read_lock(); | |
4479 | may = smk_access_entry(otsp->smk_task->smk_known, | |
4480 | isp->smk_inode->smk_known, | |
4481 | &otsp->smk_task->smk_rules); | |
4482 | rcu_read_unlock(); | |
4483 | ||
4484 | /* | |
4485 | * If the directory is transmuting and the rule | |
4486 | * providing access is transmuting use the containing | |
4487 | * directory label instead of the process label. | |
4488 | */ | |
4489 | if (may > 0 && (may & MAY_TRANSMUTE)) | |
4490 | ntsp->smk_task = isp->smk_inode; | |
4491 | } | |
4492 | return 0; | |
4493 | } | |
4494 | ||
4495 | struct lsm_blob_sizes smack_blob_sizes = { | |
4496 | .lbs_cred = sizeof(struct task_smack), | |
4497 | .lbs_file = sizeof(struct smack_known *), | |
4498 | .lbs_inode = sizeof(struct inode_smack), | |
4499 | .lbs_ipc = sizeof(struct smack_known *), | |
4500 | #ifdef CONFIG_KEYS | |
4501 | .lbs_key = sizeof(struct smack_known *), | |
4502 | #endif /* CONFIG_KEYS */ | |
4503 | .lbs_msg_msg = sizeof(struct smack_known *), | |
4504 | .lbs_sock = sizeof(struct socket_smack), | |
4505 | .lbs_superblock = sizeof(struct superblock_smack), | |
4506 | }; | |
4507 | ||
4508 | static struct security_hook_list smack_hooks[] __lsm_ro_after_init = { | |
4509 | LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check), | |
4510 | LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme), | |
4511 | LSM_HOOK_INIT(syslog, smack_syslog), | |
4512 | ||
4513 | LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security), | |
4514 | LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data), | |
4515 | LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount), | |
4516 | LSM_HOOK_INIT(sb_statfs, smack_sb_statfs), | |
4517 | LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts), | |
4518 | LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str), | |
4519 | ||
4520 | LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds), | |
4521 | ||
4522 | LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security), | |
4523 | LSM_HOOK_INIT(inode_init_security, smack_inode_init_security), | |
4524 | LSM_HOOK_INIT(inode_link, smack_inode_link), | |
4525 | LSM_HOOK_INIT(inode_unlink, smack_inode_unlink), | |
4526 | LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir), | |
4527 | LSM_HOOK_INIT(inode_rename, smack_inode_rename), | |
4528 | LSM_HOOK_INIT(inode_permission, smack_inode_permission), | |
4529 | LSM_HOOK_INIT(inode_setattr, smack_inode_setattr), | |
4530 | LSM_HOOK_INIT(inode_getattr, smack_inode_getattr), | |
4531 | LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr), | |
4532 | LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr), | |
4533 | LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr), | |
4534 | LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr), | |
4535 | LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity), | |
4536 | LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity), | |
4537 | LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity), | |
4538 | LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid), | |
4539 | ||
4540 | LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security), | |
4541 | LSM_HOOK_INIT(file_ioctl, smack_file_ioctl), | |
4542 | LSM_HOOK_INIT(file_lock, smack_file_lock), | |
4543 | LSM_HOOK_INIT(file_fcntl, smack_file_fcntl), | |
4544 | LSM_HOOK_INIT(mmap_file, smack_mmap_file), | |
4545 | LSM_HOOK_INIT(mmap_addr, cap_mmap_addr), | |
4546 | LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner), | |
4547 | LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask), | |
4548 | LSM_HOOK_INIT(file_receive, smack_file_receive), | |
4549 | ||
4550 | LSM_HOOK_INIT(file_open, smack_file_open), | |
4551 | ||
4552 | LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank), | |
4553 | LSM_HOOK_INIT(cred_free, smack_cred_free), | |
4554 | LSM_HOOK_INIT(cred_prepare, smack_cred_prepare), | |
4555 | LSM_HOOK_INIT(cred_transfer, smack_cred_transfer), | |
4556 | LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as), | |
4557 | LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as), | |
4558 | LSM_HOOK_INIT(task_setpgid, smack_task_setpgid), | |
4559 | LSM_HOOK_INIT(task_getpgid, smack_task_getpgid), | |
4560 | LSM_HOOK_INIT(task_getsid, smack_task_getsid), | |
4561 | LSM_HOOK_INIT(task_getsecid, smack_task_getsecid), | |
4562 | LSM_HOOK_INIT(task_setnice, smack_task_setnice), | |
4563 | LSM_HOOK_INIT(task_setioprio, smack_task_setioprio), | |
4564 | LSM_HOOK_INIT(task_getioprio, smack_task_getioprio), | |
4565 | LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler), | |
4566 | LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler), | |
4567 | LSM_HOOK_INIT(task_movememory, smack_task_movememory), | |
4568 | LSM_HOOK_INIT(task_kill, smack_task_kill), | |
4569 | LSM_HOOK_INIT(task_to_inode, smack_task_to_inode), | |
4570 | ||
4571 | LSM_HOOK_INIT(ipc_permission, smack_ipc_permission), | |
4572 | LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid), | |
4573 | ||
4574 | LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security), | |
4575 | ||
4576 | LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security), | |
4577 | LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate), | |
4578 | LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl), | |
4579 | LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd), | |
4580 | LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv), | |
4581 | ||
4582 | LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security), | |
4583 | LSM_HOOK_INIT(shm_associate, smack_shm_associate), | |
4584 | LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl), | |
4585 | LSM_HOOK_INIT(shm_shmat, smack_shm_shmat), | |
4586 | ||
4587 | LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security), | |
4588 | LSM_HOOK_INIT(sem_associate, smack_sem_associate), | |
4589 | LSM_HOOK_INIT(sem_semctl, smack_sem_semctl), | |
4590 | LSM_HOOK_INIT(sem_semop, smack_sem_semop), | |
4591 | ||
4592 | LSM_HOOK_INIT(d_instantiate, smack_d_instantiate), | |
4593 | ||
4594 | LSM_HOOK_INIT(getprocattr, smack_getprocattr), | |
4595 | LSM_HOOK_INIT(setprocattr, smack_setprocattr), | |
4596 | ||
4597 | LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect), | |
4598 | LSM_HOOK_INIT(unix_may_send, smack_unix_may_send), | |
4599 | ||
4600 | LSM_HOOK_INIT(socket_post_create, smack_socket_post_create), | |
4601 | #ifdef SMACK_IPV6_PORT_LABELING | |
4602 | LSM_HOOK_INIT(socket_bind, smack_socket_bind), | |
4603 | #endif | |
4604 | LSM_HOOK_INIT(socket_connect, smack_socket_connect), | |
4605 | LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg), | |
4606 | LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb), | |
4607 | LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream), | |
4608 | LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram), | |
4609 | LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security), | |
4610 | #ifdef SMACK_IPV6_PORT_LABELING | |
4611 | LSM_HOOK_INIT(sk_free_security, smack_sk_free_security), | |
4612 | #endif | |
4613 | LSM_HOOK_INIT(sock_graft, smack_sock_graft), | |
4614 | LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request), | |
4615 | LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone), | |
4616 | ||
4617 | /* key management security hooks */ | |
4618 | #ifdef CONFIG_KEYS | |
4619 | LSM_HOOK_INIT(key_alloc, smack_key_alloc), | |
4620 | LSM_HOOK_INIT(key_permission, smack_key_permission), | |
4621 | LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity), | |
4622 | #endif /* CONFIG_KEYS */ | |
4623 | ||
4624 | /* Audit hooks */ | |
4625 | #ifdef CONFIG_AUDIT | |
4626 | LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init), | |
4627 | LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known), | |
4628 | LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match), | |
4629 | #endif /* CONFIG_AUDIT */ | |
4630 | ||
4631 | LSM_HOOK_INIT(ismaclabel, smack_ismaclabel), | |
4632 | LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx), | |
4633 | LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid), | |
4634 | LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx), | |
4635 | LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx), | |
4636 | LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx), | |
4637 | LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up), | |
4638 | LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr), | |
4639 | LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as), | |
4640 | }; | |
4641 | ||
4642 | ||
4643 | static __init void init_smack_known_list(void) | |
4644 | { | |
4645 | /* | |
4646 | * Initialize rule list locks | |
4647 | */ | |
4648 | mutex_init(&smack_known_huh.smk_rules_lock); | |
4649 | mutex_init(&smack_known_hat.smk_rules_lock); | |
4650 | mutex_init(&smack_known_floor.smk_rules_lock); | |
4651 | mutex_init(&smack_known_star.smk_rules_lock); | |
4652 | mutex_init(&smack_known_web.smk_rules_lock); | |
4653 | /* | |
4654 | * Initialize rule lists | |
4655 | */ | |
4656 | INIT_LIST_HEAD(&smack_known_huh.smk_rules); | |
4657 | INIT_LIST_HEAD(&smack_known_hat.smk_rules); | |
4658 | INIT_LIST_HEAD(&smack_known_star.smk_rules); | |
4659 | INIT_LIST_HEAD(&smack_known_floor.smk_rules); | |
4660 | INIT_LIST_HEAD(&smack_known_web.smk_rules); | |
4661 | /* | |
4662 | * Create the known labels list | |
4663 | */ | |
4664 | smk_insert_entry(&smack_known_huh); | |
4665 | smk_insert_entry(&smack_known_hat); | |
4666 | smk_insert_entry(&smack_known_star); | |
4667 | smk_insert_entry(&smack_known_floor); | |
4668 | smk_insert_entry(&smack_known_web); | |
4669 | } | |
4670 | ||
4671 | /** | |
4672 | * smack_init - initialize the smack system | |
4673 | * | |
4674 | * Returns 0 | |
4675 | */ | |
4676 | static __init int smack_init(void) | |
4677 | { | |
4678 | static int finish; | |
4679 | struct cred *cred = (struct cred *) current->cred; | |
4680 | struct task_smack *tsp; | |
4681 | ||
4682 | if (!security_module_enable("smack", | |
4683 | IS_ENABLED(CONFIG_SECURITY_SMACK_STACKED))) | |
4684 | return 0; | |
4685 | ||
4686 | if (!finish) { | |
4687 | security_add_blobs(&smack_blob_sizes); | |
4688 | finish = 1; | |
4689 | return 0; | |
4690 | } | |
4691 | ||
4692 | smack_inode_cache = KMEM_CACHE(inode_smack, 0); | |
4693 | if (!smack_inode_cache) | |
4694 | return -ENOMEM; | |
4695 | ||
4696 | lsm_early_cred(cred); | |
4697 | ||
4698 | /* | |
4699 | * Set the security state for the initial task. | |
4700 | */ | |
4701 | tsp = smack_cred(cred); | |
4702 | init_task_smack(tsp, &smack_known_floor, &smack_known_floor); | |
4703 | ||
4704 | /* | |
4705 | * Register with LSM | |
4706 | */ | |
4707 | security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack"); | |
4708 | smack_enabled = 1; | |
4709 | ||
4710 | pr_info("Smack: Initializing.\n"); | |
4711 | #ifdef CONFIG_SECURITY_SMACK_NETFILTER | |
4712 | pr_info("Smack: Netfilter enabled.\n"); | |
4713 | #endif | |
4714 | #ifdef SMACK_IPV6_PORT_LABELING | |
4715 | pr_info("Smack: IPv6 port labeling enabled.\n"); | |
4716 | #endif | |
4717 | #ifdef SMACK_IPV6_SECMARK_LABELING | |
4718 | pr_info("Smack: IPv6 Netfilter enabled.\n"); | |
4719 | #endif | |
4720 | ||
4721 | /* initialize the smack_known_list */ | |
4722 | init_smack_known_list(); | |
4723 | ||
4724 | return 0; | |
4725 | } | |
4726 | ||
4727 | /* | |
4728 | * Smack requires early initialization in order to label | |
4729 | * all processes and objects when they are created. | |
4730 | */ | |
4731 | security_initcall(smack_init); |