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