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