blob: d55b991268d56b8554b5463e37e16e5f018cc363 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
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>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080031#include <linux/mutex.h>
32#include <linux/pipe_fs_i.h>
33#include <net/netlabel.h>
34#include <net/cipso_ipv4.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100035#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070036#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050037#include <linux/dcache.h>
Casey Schauflere114e472008-02-04 22:29:50 -080038#include "smack.h"
39
David Howellsc69e8d92008-11-14 10:39:19 +110040#define task_security(task) (task_cred_xxx((task), security))
41
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020042#define TRANS_TRUE "TRUE"
43#define TRANS_TRUE_SIZE 4
44
Casey Schauflere114e472008-02-04 22:29:50 -080045/**
46 * smk_fetch - Fetch the smack label from a file.
47 * @ip: a pointer to the inode
48 * @dp: a pointer to the dentry
49 *
50 * Returns a pointer to the master list entry for the Smack label
51 * or NULL if there was no label to fetch.
52 */
Casey Schaufler676dac42010-12-02 06:43:39 -080053static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080054{
55 int rc;
56 char in[SMK_LABELLEN];
57
58 if (ip->i_op->getxattr == NULL)
59 return NULL;
60
Casey Schaufler676dac42010-12-02 06:43:39 -080061 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
Casey Schauflere114e472008-02-04 22:29:50 -080062 if (rc < 0)
63 return NULL;
64
65 return smk_import(in, rc);
66}
67
68/**
69 * new_inode_smack - allocate an inode security blob
70 * @smack: a pointer to the Smack label to use in the blob
71 *
72 * Returns the new blob or NULL if there's no memory available
73 */
74struct inode_smack *new_inode_smack(char *smack)
75{
76 struct inode_smack *isp;
77
78 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
79 if (isp == NULL)
80 return NULL;
81
82 isp->smk_inode = smack;
83 isp->smk_flags = 0;
84 mutex_init(&isp->smk_lock);
85
86 return isp;
87}
88
Casey Schaufler7898e1f2011-01-17 08:05:27 -080089/**
90 * new_task_smack - allocate a task security blob
91 * @smack: a pointer to the Smack label to use in the blob
92 *
93 * Returns the new blob or NULL if there's no memory available
94 */
95static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
96{
97 struct task_smack *tsp;
98
99 tsp = kzalloc(sizeof(struct task_smack), gfp);
100 if (tsp == NULL)
101 return NULL;
102
103 tsp->smk_task = task;
104 tsp->smk_forked = forked;
105 INIT_LIST_HEAD(&tsp->smk_rules);
106 mutex_init(&tsp->smk_rules_lock);
107
108 return tsp;
109}
110
111/**
112 * smk_copy_rules - copy a rule set
113 * @nhead - new rules header pointer
114 * @ohead - old rules header pointer
115 *
116 * Returns 0 on success, -ENOMEM on error
117 */
118static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
119 gfp_t gfp)
120{
121 struct smack_rule *nrp;
122 struct smack_rule *orp;
123 int rc = 0;
124
125 INIT_LIST_HEAD(nhead);
126
127 list_for_each_entry_rcu(orp, ohead, list) {
128 nrp = kzalloc(sizeof(struct smack_rule), gfp);
129 if (nrp == NULL) {
130 rc = -ENOMEM;
131 break;
132 }
133 *nrp = *orp;
134 list_add_rcu(&nrp->list, nhead);
135 }
136 return rc;
137}
138
Casey Schauflere114e472008-02-04 22:29:50 -0800139/*
140 * LSM hooks.
141 * We he, that is fun!
142 */
143
144/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000145 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800146 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -0800147 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -0800148 *
149 * Returns 0 if access is OK, an error code otherwise
150 *
151 * Do the capability checks, and require read and write.
152 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000153static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800154{
155 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200156 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800157 char *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800158
Ingo Molnar9e488582009-05-07 19:26:19 +1000159 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800160 if (rc != 0)
161 return rc;
162
Casey Schaufler676dac42010-12-02 06:43:39 -0800163 tsp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200164 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
165 smk_ad_setfield_u_tsk(&ad, ctp);
166
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800167 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100168 return rc;
169}
Casey Schauflere114e472008-02-04 22:29:50 -0800170
David Howells5cd9c582008-08-14 11:37:28 +0100171/**
172 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
173 * @ptp: parent task pointer
174 *
175 * Returns 0 if access is OK, an error code otherwise
176 *
177 * Do the capability checks, and require read and write.
178 */
179static int smack_ptrace_traceme(struct task_struct *ptp)
180{
181 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200182 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800183 char *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100184
185 rc = cap_ptrace_traceme(ptp);
186 if (rc != 0)
187 return rc;
188
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800189 tsp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
191 smk_ad_setfield_u_tsk(&ad, ptp);
192
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800193 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800194 return rc;
195}
196
197/**
198 * smack_syslog - Smack approval on syslog
199 * @type: message type
200 *
201 * Require that the task has the floor label
202 *
203 * Returns 0 on success, error code otherwise.
204 */
Eric Paris12b30522010-11-15 18:36:29 -0500205static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800206{
Eric Paris12b30522010-11-15 18:36:29 -0500207 int rc = 0;
Casey Schaufler676dac42010-12-02 06:43:39 -0800208 char *sp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800209
Casey Schauflere114e472008-02-04 22:29:50 -0800210 if (capable(CAP_MAC_OVERRIDE))
211 return 0;
212
213 if (sp != smack_known_floor.smk_known)
214 rc = -EACCES;
215
216 return rc;
217}
218
219
220/*
221 * Superblock Hooks.
222 */
223
224/**
225 * smack_sb_alloc_security - allocate a superblock blob
226 * @sb: the superblock getting the blob
227 *
228 * Returns 0 on success or -ENOMEM on error.
229 */
230static int smack_sb_alloc_security(struct super_block *sb)
231{
232 struct superblock_smack *sbsp;
233
234 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
235
236 if (sbsp == NULL)
237 return -ENOMEM;
238
239 sbsp->smk_root = smack_known_floor.smk_known;
240 sbsp->smk_default = smack_known_floor.smk_known;
241 sbsp->smk_floor = smack_known_floor.smk_known;
242 sbsp->smk_hat = smack_known_hat.smk_known;
243 sbsp->smk_initialized = 0;
244 spin_lock_init(&sbsp->smk_sblock);
245
246 sb->s_security = sbsp;
247
248 return 0;
249}
250
251/**
252 * smack_sb_free_security - free a superblock blob
253 * @sb: the superblock getting the blob
254 *
255 */
256static void smack_sb_free_security(struct super_block *sb)
257{
258 kfree(sb->s_security);
259 sb->s_security = NULL;
260}
261
262/**
263 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800264 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800265 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800266 *
267 * Returns 0 on success or -ENOMEM on error.
268 *
269 * Copy the Smack specific mount options out of the mount
270 * options list.
271 */
Eric Parise0007522008-03-05 10:31:54 -0500272static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800273{
274 char *cp, *commap, *otheropts, *dp;
275
Casey Schauflere114e472008-02-04 22:29:50 -0800276 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
277 if (otheropts == NULL)
278 return -ENOMEM;
279
280 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
281 if (strstr(cp, SMK_FSDEFAULT) == cp)
282 dp = smackopts;
283 else if (strstr(cp, SMK_FSFLOOR) == cp)
284 dp = smackopts;
285 else if (strstr(cp, SMK_FSHAT) == cp)
286 dp = smackopts;
287 else if (strstr(cp, SMK_FSROOT) == cp)
288 dp = smackopts;
289 else
290 dp = otheropts;
291
292 commap = strchr(cp, ',');
293 if (commap != NULL)
294 *commap = '\0';
295
296 if (*dp != '\0')
297 strcat(dp, ",");
298 strcat(dp, cp);
299 }
300
301 strcpy(orig, otheropts);
302 free_page((unsigned long)otheropts);
303
304 return 0;
305}
306
307/**
308 * smack_sb_kern_mount - Smack specific mount processing
309 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100310 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800311 * @data: the smack mount options
312 *
313 * Returns 0 on success, an error code on failure
314 */
James Morris12204e22008-12-19 10:44:42 +1100315static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800316{
317 struct dentry *root = sb->s_root;
318 struct inode *inode = root->d_inode;
319 struct superblock_smack *sp = sb->s_security;
320 struct inode_smack *isp;
321 char *op;
322 char *commap;
323 char *nsp;
324
325 spin_lock(&sp->smk_sblock);
326 if (sp->smk_initialized != 0) {
327 spin_unlock(&sp->smk_sblock);
328 return 0;
329 }
330 sp->smk_initialized = 1;
331 spin_unlock(&sp->smk_sblock);
332
333 for (op = data; op != NULL; op = commap) {
334 commap = strchr(op, ',');
335 if (commap != NULL)
336 *commap++ = '\0';
337
338 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
339 op += strlen(SMK_FSHAT);
340 nsp = smk_import(op, 0);
341 if (nsp != NULL)
342 sp->smk_hat = nsp;
343 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
344 op += strlen(SMK_FSFLOOR);
345 nsp = smk_import(op, 0);
346 if (nsp != NULL)
347 sp->smk_floor = nsp;
348 } else if (strncmp(op, SMK_FSDEFAULT,
349 strlen(SMK_FSDEFAULT)) == 0) {
350 op += strlen(SMK_FSDEFAULT);
351 nsp = smk_import(op, 0);
352 if (nsp != NULL)
353 sp->smk_default = nsp;
354 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
355 op += strlen(SMK_FSROOT);
356 nsp = smk_import(op, 0);
357 if (nsp != NULL)
358 sp->smk_root = nsp;
359 }
360 }
361
362 /*
363 * Initialize the root inode.
364 */
365 isp = inode->i_security;
366 if (isp == NULL)
367 inode->i_security = new_inode_smack(sp->smk_root);
368 else
369 isp->smk_inode = sp->smk_root;
370
371 return 0;
372}
373
374/**
375 * smack_sb_statfs - Smack check on statfs
376 * @dentry: identifies the file system in question
377 *
378 * Returns 0 if current can read the floor of the filesystem,
379 * and error code otherwise
380 */
381static int smack_sb_statfs(struct dentry *dentry)
382{
383 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200384 int rc;
385 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800386
Eric Parisa2694342011-04-25 13:10:27 -0400387 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200388 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
389
390 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
391 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800392}
393
394/**
395 * smack_sb_mount - Smack check for mounting
396 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800397 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800398 * @type: unused
399 * @flags: unused
400 * @data: unused
401 *
402 * Returns 0 if current can write the floor of the filesystem
403 * being mounted on, an error code otherwise.
404 */
Al Virob5266eb2008-03-22 17:48:24 -0400405static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800406 char *type, unsigned long flags, void *data)
407{
Al Virob5266eb2008-03-22 17:48:24 -0400408 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200409 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800410
Eric Parisf48b7392011-04-25 12:54:27 -0400411 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200412 smk_ad_setfield_u_fs_path(&ad, *path);
413
414 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800415}
416
417/**
418 * smack_sb_umount - Smack check for unmounting
419 * @mnt: file system to unmount
420 * @flags: unused
421 *
422 * Returns 0 if current can write the floor of the filesystem
423 * being unmounted, an error code otherwise.
424 */
425static int smack_sb_umount(struct vfsmount *mnt, int flags)
426{
427 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200428 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400429 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200430
Eric Parisa2694342011-04-25 13:10:27 -0400431 path.dentry = mnt->mnt_root;
432 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200433
Eric Parisf48b7392011-04-25 12:54:27 -0400434 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400435 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflere114e472008-02-04 22:29:50 -0800436
437 sbp = mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200438 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800439}
440
441/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800442 * BPRM hooks
443 */
444
Casey Schauflerce8a4322011-09-29 18:21:01 -0700445/**
446 * smack_bprm_set_creds - set creds for exec
447 * @bprm: the exec information
448 *
449 * Returns 0 if it gets a blob, -ENOMEM otherwise
450 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800451static int smack_bprm_set_creds(struct linux_binprm *bprm)
452{
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300453 struct inode *inode = bprm->file->f_path.dentry->d_inode;
454 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800455 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800456 int rc;
457
458 rc = cap_bprm_set_creds(bprm);
459 if (rc != 0)
460 return rc;
461
462 if (bprm->cred_prepared)
463 return 0;
464
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300465 isp = inode->i_security;
466 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800467 return 0;
468
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300469 if (bprm->unsafe)
470 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800471
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300472 bsp->smk_task = isp->smk_task;
473 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800474
475 return 0;
476}
477
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300478/**
479 * smack_bprm_committing_creds - Prepare to install the new credentials
480 * from bprm.
481 *
482 * @bprm: binprm for exec
483 */
484static void smack_bprm_committing_creds(struct linux_binprm *bprm)
485{
486 struct task_smack *bsp = bprm->cred->security;
487
488 if (bsp->smk_task != bsp->smk_forked)
489 current->pdeath_signal = 0;
490}
491
492/**
493 * smack_bprm_secureexec - Return the decision to use secureexec.
494 * @bprm: binprm for exec
495 *
496 * Returns 0 on success.
497 */
498static int smack_bprm_secureexec(struct linux_binprm *bprm)
499{
500 struct task_smack *tsp = current_security();
501 int ret = cap_bprm_secureexec(bprm);
502
503 if (!ret && (tsp->smk_task != tsp->smk_forked))
504 ret = 1;
505
506 return ret;
507}
508
Casey Schaufler676dac42010-12-02 06:43:39 -0800509/*
Casey Schauflere114e472008-02-04 22:29:50 -0800510 * Inode hooks
511 */
512
513/**
514 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800515 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800516 *
517 * Returns 0 if it gets a blob, -ENOMEM otherwise
518 */
519static int smack_inode_alloc_security(struct inode *inode)
520{
Casey Schaufler676dac42010-12-02 06:43:39 -0800521 inode->i_security = new_inode_smack(smk_of_current());
Casey Schauflere114e472008-02-04 22:29:50 -0800522 if (inode->i_security == NULL)
523 return -ENOMEM;
524 return 0;
525}
526
527/**
528 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800529 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800530 *
531 * Clears the blob pointer in inode
532 */
533static void smack_inode_free_security(struct inode *inode)
534{
535 kfree(inode->i_security);
536 inode->i_security = NULL;
537}
538
539/**
540 * smack_inode_init_security - copy out the smack from an inode
541 * @inode: the inode
542 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500543 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800544 * @name: where to put the attribute name
545 * @value: where to put the attribute value
546 * @len: where to put the length of the attribute
547 *
548 * Returns 0 if it all works out, -ENOMEM if there's no memory
549 */
550static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -0500551 const struct qstr *qstr, char **name,
552 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800553{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700554 struct smack_known *skp;
555 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800556 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200557 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800558 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800559
560 if (name) {
561 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
562 if (*name == NULL)
563 return -ENOMEM;
564 }
565
566 if (value) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700567 skp = smk_find_entry(csp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800568 rcu_read_lock();
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700569 may = smk_access_entry(csp, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800570 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200571
572 /*
573 * If the access rule allows transmutation and
574 * the directory requests transmutation then
575 * by all means transmute.
576 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800577 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
578 smk_inode_transmutable(dir))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200579 isp = dsp;
580
Casey Schauflere114e472008-02-04 22:29:50 -0800581 *value = kstrdup(isp, GFP_KERNEL);
582 if (*value == NULL)
583 return -ENOMEM;
584 }
585
586 if (len)
587 *len = strlen(isp) + 1;
588
589 return 0;
590}
591
592/**
593 * smack_inode_link - Smack check on link
594 * @old_dentry: the existing object
595 * @dir: unused
596 * @new_dentry: the new object
597 *
598 * Returns 0 if access is permitted, an error code otherwise
599 */
600static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
601 struct dentry *new_dentry)
602{
Casey Schauflere114e472008-02-04 22:29:50 -0800603 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200604 struct smk_audit_info ad;
605 int rc;
606
Eric Parisa2694342011-04-25 13:10:27 -0400607 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200608 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800609
610 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200611 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800612
613 if (rc == 0 && new_dentry->d_inode != NULL) {
614 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200615 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
616 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800617 }
618
619 return rc;
620}
621
622/**
623 * smack_inode_unlink - Smack check on inode deletion
624 * @dir: containing directory object
625 * @dentry: file to unlink
626 *
627 * Returns 0 if current can write the containing directory
628 * and the object, error code otherwise
629 */
630static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
631{
632 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200633 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800634 int rc;
635
Eric Parisa2694342011-04-25 13:10:27 -0400636 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200637 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
638
Casey Schauflere114e472008-02-04 22:29:50 -0800639 /*
640 * You need write access to the thing you're unlinking
641 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200642 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
643 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800644 /*
645 * You also need write access to the containing directory
646 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200647 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
648 smk_ad_setfield_u_fs_inode(&ad, dir);
649 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
650 }
Casey Schauflere114e472008-02-04 22:29:50 -0800651 return rc;
652}
653
654/**
655 * smack_inode_rmdir - Smack check on directory deletion
656 * @dir: containing directory object
657 * @dentry: directory to unlink
658 *
659 * Returns 0 if current can write the containing directory
660 * and the directory, error code otherwise
661 */
662static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
663{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200664 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800665 int rc;
666
Eric Parisa2694342011-04-25 13:10:27 -0400667 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200668 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
669
Casey Schauflere114e472008-02-04 22:29:50 -0800670 /*
671 * You need write access to the thing you're removing
672 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200673 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
674 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800675 /*
676 * You also need write access to the containing directory
677 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200678 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
679 smk_ad_setfield_u_fs_inode(&ad, dir);
680 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
681 }
Casey Schauflere114e472008-02-04 22:29:50 -0800682
683 return rc;
684}
685
686/**
687 * smack_inode_rename - Smack check on rename
688 * @old_inode: the old directory
689 * @old_dentry: unused
690 * @new_inode: the new directory
691 * @new_dentry: unused
692 *
693 * Read and write access is required on both the old and
694 * new directories.
695 *
696 * Returns 0 if access is permitted, an error code otherwise
697 */
698static int smack_inode_rename(struct inode *old_inode,
699 struct dentry *old_dentry,
700 struct inode *new_inode,
701 struct dentry *new_dentry)
702{
703 int rc;
704 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200705 struct smk_audit_info ad;
706
Eric Parisa2694342011-04-25 13:10:27 -0400707 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200708 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800709
710 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200711 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800712
713 if (rc == 0 && new_dentry->d_inode != NULL) {
714 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200715 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
716 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800717 }
Casey Schauflere114e472008-02-04 22:29:50 -0800718 return rc;
719}
720
721/**
722 * smack_inode_permission - Smack version of permission()
723 * @inode: the inode in question
724 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800725 *
726 * This is the important Smack hook.
727 *
728 * Returns 0 if access is permitted, -EACCES otherwise
729 */
Al Viroe74f71e2011-06-20 19:38:15 -0400730static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800731{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200732 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400733 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400734
735 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800736 /*
737 * No permission to check. Existence test. Yup, it's there.
738 */
739 if (mask == 0)
740 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700741
742 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400743 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700744 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400745 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200746 smk_ad_setfield_u_fs_inode(&ad, inode);
747 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800748}
749
750/**
751 * smack_inode_setattr - Smack check for setting attributes
752 * @dentry: the object
753 * @iattr: for the force flag
754 *
755 * Returns 0 if access is permitted, an error code otherwise
756 */
757static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
758{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200759 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800760 /*
761 * Need to allow for clearing the setuid bit.
762 */
763 if (iattr->ia_valid & ATTR_FORCE)
764 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400765 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200766 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800767
Etienne Bassetecfcc532009-04-08 20:40:06 +0200768 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800769}
770
771/**
772 * smack_inode_getattr - Smack check for getting attributes
773 * @mnt: unused
774 * @dentry: the object
775 *
776 * Returns 0 if access is permitted, an error code otherwise
777 */
778static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
779{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200780 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400781 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200782
Eric Parisa2694342011-04-25 13:10:27 -0400783 path.dentry = dentry;
784 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200785
Eric Parisf48b7392011-04-25 12:54:27 -0400786 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400787 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200788 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800789}
790
791/**
792 * smack_inode_setxattr - Smack check for setting xattrs
793 * @dentry: the object
794 * @name: name of the attribute
795 * @value: unused
796 * @size: unused
797 * @flags: unused
798 *
799 * This protects the Smack attribute explicitly.
800 *
801 * Returns 0 if access is permitted, an error code otherwise
802 */
David Howells8f0cfa52008-04-29 00:59:41 -0700803static int smack_inode_setxattr(struct dentry *dentry, const char *name,
804 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800805{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200806 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800807 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800808
Casey Schauflerbcdca222008-02-23 15:24:04 -0800809 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
810 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800811 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800812 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
813 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800814 if (!capable(CAP_MAC_ADMIN))
815 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200816 /*
817 * check label validity here so import wont fail on
818 * post_setxattr
819 */
820 if (size == 0 || size >= SMK_LABELLEN ||
821 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400822 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200823 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
824 if (!capable(CAP_MAC_ADMIN))
825 rc = -EPERM;
826 if (size != TRANS_TRUE_SIZE ||
827 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
828 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800829 } else
830 rc = cap_inode_setxattr(dentry, name, value, size, flags);
831
Eric Parisa2694342011-04-25 13:10:27 -0400832 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200833 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
834
Casey Schauflerbcdca222008-02-23 15:24:04 -0800835 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200836 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800837
838 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800839}
840
841/**
842 * smack_inode_post_setxattr - Apply the Smack update approved above
843 * @dentry: object
844 * @name: attribute name
845 * @value: attribute value
846 * @size: attribute size
847 * @flags: unused
848 *
849 * Set the pointer in the inode blob to the entry found
850 * in the master label list.
851 */
David Howells8f0cfa52008-04-29 00:59:41 -0700852static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
853 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800854{
Casey Schauflere114e472008-02-04 22:29:50 -0800855 char *nsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200856 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800857
858 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200859 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800860 if (nsp != NULL)
861 isp->smk_inode = nsp;
862 else
863 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200864 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
865 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800866 if (nsp != NULL)
867 isp->smk_task = nsp;
868 else
869 isp->smk_task = smack_known_invalid.smk_known;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800870 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
871 nsp = smk_import(value, size);
872 if (nsp != NULL)
873 isp->smk_mmap = nsp;
874 else
875 isp->smk_mmap = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200876 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
877 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflere114e472008-02-04 22:29:50 -0800878
879 return;
880}
881
Casey Schauflerce8a4322011-09-29 18:21:01 -0700882/**
Casey Schauflere114e472008-02-04 22:29:50 -0800883 * smack_inode_getxattr - Smack check on getxattr
884 * @dentry: the object
885 * @name: unused
886 *
887 * Returns 0 if access is permitted, an error code otherwise
888 */
David Howells8f0cfa52008-04-29 00:59:41 -0700889static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800890{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200891 struct smk_audit_info ad;
892
Eric Parisa2694342011-04-25 13:10:27 -0400893 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200894 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
895
896 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800897}
898
Casey Schauflerce8a4322011-09-29 18:21:01 -0700899/**
Casey Schauflere114e472008-02-04 22:29:50 -0800900 * smack_inode_removexattr - Smack check on removexattr
901 * @dentry: the object
902 * @name: name of the attribute
903 *
904 * Removing the Smack attribute requires CAP_MAC_ADMIN
905 *
906 * Returns 0 if access is permitted, an error code otherwise
907 */
David Howells8f0cfa52008-04-29 00:59:41 -0700908static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800909{
Casey Schaufler676dac42010-12-02 06:43:39 -0800910 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200911 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800912 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800913
Casey Schauflerbcdca222008-02-23 15:24:04 -0800914 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
915 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800916 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200917 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800918 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
919 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800920 if (!capable(CAP_MAC_ADMIN))
921 rc = -EPERM;
922 } else
923 rc = cap_inode_removexattr(dentry, name);
924
Eric Parisa2694342011-04-25 13:10:27 -0400925 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200926 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800927 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200928 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800929
Casey Schaufler676dac42010-12-02 06:43:39 -0800930 if (rc == 0) {
931 isp = dentry->d_inode->i_security;
932 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800933 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800934 }
935
Casey Schauflerbcdca222008-02-23 15:24:04 -0800936 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800937}
938
939/**
940 * smack_inode_getsecurity - get smack xattrs
941 * @inode: the object
942 * @name: attribute name
943 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800944 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800945 *
946 * Returns the size of the attribute or an error code
947 */
948static int smack_inode_getsecurity(const struct inode *inode,
949 const char *name, void **buffer,
950 bool alloc)
951{
952 struct socket_smack *ssp;
953 struct socket *sock;
954 struct super_block *sbp;
955 struct inode *ip = (struct inode *)inode;
956 char *isp;
957 int ilen;
958 int rc = 0;
959
960 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
961 isp = smk_of_inode(inode);
962 ilen = strlen(isp) + 1;
963 *buffer = isp;
964 return ilen;
965 }
966
967 /*
968 * The rest of the Smack xattrs are only on sockets.
969 */
970 sbp = ip->i_sb;
971 if (sbp->s_magic != SOCKFS_MAGIC)
972 return -EOPNOTSUPP;
973
974 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800975 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800976 return -EOPNOTSUPP;
977
978 ssp = sock->sk->sk_security;
979
980 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
981 isp = ssp->smk_in;
982 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
983 isp = ssp->smk_out;
984 else
985 return -EOPNOTSUPP;
986
987 ilen = strlen(isp) + 1;
988 if (rc == 0) {
989 *buffer = isp;
990 rc = ilen;
991 }
992
993 return rc;
994}
995
996
997/**
998 * smack_inode_listsecurity - list the Smack attributes
999 * @inode: the object
1000 * @buffer: where they go
1001 * @buffer_size: size of buffer
1002 *
1003 * Returns 0 on success, -EINVAL otherwise
1004 */
1005static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1006 size_t buffer_size)
1007{
1008 int len = strlen(XATTR_NAME_SMACK);
1009
1010 if (buffer != NULL && len <= buffer_size) {
1011 memcpy(buffer, XATTR_NAME_SMACK, len);
1012 return len;
1013 }
1014 return -EINVAL;
1015}
1016
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001017/**
1018 * smack_inode_getsecid - Extract inode's security id
1019 * @inode: inode to extract the info from
1020 * @secid: where result will be saved
1021 */
1022static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1023{
1024 struct inode_smack *isp = inode->i_security;
1025
1026 *secid = smack_to_secid(isp->smk_inode);
1027}
1028
Casey Schauflere114e472008-02-04 22:29:50 -08001029/*
1030 * File Hooks
1031 */
1032
1033/**
1034 * smack_file_permission - Smack check on file operations
1035 * @file: unused
1036 * @mask: unused
1037 *
1038 * Returns 0
1039 *
1040 * Should access checks be done on each read or write?
1041 * UNICOS and SELinux say yes.
1042 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1043 *
1044 * I'll say no for now. Smack does not do the frequent
1045 * label changing that SELinux does.
1046 */
1047static int smack_file_permission(struct file *file, int mask)
1048{
1049 return 0;
1050}
1051
1052/**
1053 * smack_file_alloc_security - assign a file security blob
1054 * @file: the object
1055 *
1056 * The security blob for a file is a pointer to the master
1057 * label list, so no allocation is done.
1058 *
1059 * Returns 0
1060 */
1061static int smack_file_alloc_security(struct file *file)
1062{
Casey Schaufler676dac42010-12-02 06:43:39 -08001063 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001064 return 0;
1065}
1066
1067/**
1068 * smack_file_free_security - clear a file security blob
1069 * @file: the object
1070 *
1071 * The security blob for a file is a pointer to the master
1072 * label list, so no memory is freed.
1073 */
1074static void smack_file_free_security(struct file *file)
1075{
1076 file->f_security = NULL;
1077}
1078
1079/**
1080 * smack_file_ioctl - Smack check on ioctls
1081 * @file: the object
1082 * @cmd: what to do
1083 * @arg: unused
1084 *
1085 * Relies heavily on the correct use of the ioctl command conventions.
1086 *
1087 * Returns 0 if allowed, error code otherwise
1088 */
1089static int smack_file_ioctl(struct file *file, unsigned int cmd,
1090 unsigned long arg)
1091{
1092 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001093 struct smk_audit_info ad;
1094
Eric Parisf48b7392011-04-25 12:54:27 -04001095 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001096 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001097
1098 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001099 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001100
1101 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001103
1104 return rc;
1105}
1106
1107/**
1108 * smack_file_lock - Smack check on file locking
1109 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001110 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001111 *
1112 * Returns 0 if current has write access, error code otherwise
1113 */
1114static int smack_file_lock(struct file *file, unsigned int cmd)
1115{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001116 struct smk_audit_info ad;
1117
Eric Paris92f42502011-04-25 13:15:55 -04001118 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1119 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001120 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001121}
1122
1123/**
1124 * smack_file_fcntl - Smack check on fcntl
1125 * @file: the object
1126 * @cmd: what action to check
1127 * @arg: unused
1128 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001129 * Generally these operations are harmless.
1130 * File locking operations present an obvious mechanism
1131 * for passing information, so they require write access.
1132 *
Casey Schauflere114e472008-02-04 22:29:50 -08001133 * Returns 0 if current has access, error code otherwise
1134 */
1135static int smack_file_fcntl(struct file *file, unsigned int cmd,
1136 unsigned long arg)
1137{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001138 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001139 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001140
Etienne Bassetecfcc532009-04-08 20:40:06 +02001141
Casey Schauflere114e472008-02-04 22:29:50 -08001142 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001143 case F_GETLK:
Casey Schauflere114e472008-02-04 22:29:50 -08001144 case F_SETLK:
1145 case F_SETLKW:
1146 case F_SETOWN:
1147 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001148 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1149 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001150 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001151 break;
1152 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001153 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001154 }
1155
1156 return rc;
1157}
1158
1159/**
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001160 * smack_file_mmap :
1161 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1162 * if mapping anonymous memory.
1163 * @file contains the file structure for file to map (may be NULL).
1164 * @reqprot contains the protection requested by the application.
1165 * @prot contains the protection that will be applied by the kernel.
1166 * @flags contains the operational flags.
1167 * Return 0 if permission is granted.
1168 */
1169static int smack_file_mmap(struct file *file,
1170 unsigned long reqprot, unsigned long prot,
1171 unsigned long flags, unsigned long addr,
1172 unsigned long addr_only)
1173{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001174 struct smack_known *skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001175 struct smack_rule *srp;
1176 struct task_smack *tsp;
1177 char *sp;
1178 char *msmack;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001179 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001180 struct inode_smack *isp;
1181 struct dentry *dp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001182 int may;
1183 int mmay;
1184 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001185 int rc;
1186
1187 /* do DAC check on address space usage */
1188 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1189 if (rc || addr_only)
1190 return rc;
1191
1192 if (file == NULL || file->f_dentry == NULL)
1193 return 0;
1194
1195 dp = file->f_dentry;
1196
1197 if (dp->d_inode == NULL)
1198 return 0;
1199
1200 isp = dp->d_inode->i_security;
1201 if (isp->smk_mmap == NULL)
1202 return 0;
1203 msmack = isp->smk_mmap;
1204
1205 tsp = current_security();
1206 sp = smk_of_current();
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001207 skp = smk_find_entry(sp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001208 rc = 0;
1209
1210 rcu_read_lock();
1211 /*
1212 * For each Smack rule associated with the subject
1213 * label verify that the SMACK64MMAP also has access
1214 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001215 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001216 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001217 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001218 /*
1219 * Matching labels always allows access.
1220 */
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001221 if (msmack == osmack)
1222 continue;
1223 /*
1224 * If there is a matching local rule take
1225 * that into account as well.
1226 */
1227 may = smk_access_entry(srp->smk_subject, osmack,
1228 &tsp->smk_rules);
1229 if (may == -ENOENT)
1230 may = srp->smk_access;
1231 else
1232 may &= srp->smk_access;
1233 /*
1234 * If may is zero the SMACK64MMAP subject can't
1235 * possibly have less access.
1236 */
1237 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001238 continue;
1239
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001240 /*
1241 * Fetch the global list entry.
1242 * If there isn't one a SMACK64MMAP subject
1243 * can't have as much access as current.
1244 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001245 skp = smk_find_entry(msmack);
1246 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001247 if (mmay == -ENOENT) {
1248 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001249 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001250 }
1251 /*
1252 * If there is a local entry it modifies the
1253 * potential access, too.
1254 */
1255 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1256 if (tmay != -ENOENT)
1257 mmay &= tmay;
1258
1259 /*
1260 * If there is any access available to current that is
1261 * not available to a SMACK64MMAP subject
1262 * deny access.
1263 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001264 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001265 rc = -EACCES;
1266 break;
1267 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001268 }
1269
1270 rcu_read_unlock();
1271
1272 return rc;
1273}
1274
1275/**
Casey Schauflere114e472008-02-04 22:29:50 -08001276 * smack_file_set_fowner - set the file security blob value
1277 * @file: object in question
1278 *
1279 * Returns 0
1280 * Further research may be required on this one.
1281 */
1282static int smack_file_set_fowner(struct file *file)
1283{
Casey Schaufler676dac42010-12-02 06:43:39 -08001284 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001285 return 0;
1286}
1287
1288/**
1289 * smack_file_send_sigiotask - Smack on sigio
1290 * @tsk: The target task
1291 * @fown: the object the signal come from
1292 * @signum: unused
1293 *
1294 * Allow a privileged task to get signals even if it shouldn't
1295 *
1296 * Returns 0 if a subject with the object's smack could
1297 * write to the task, an error code otherwise.
1298 */
1299static int smack_file_send_sigiotask(struct task_struct *tsk,
1300 struct fown_struct *fown, int signum)
1301{
1302 struct file *file;
1303 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001304 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001305 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001306
1307 /*
1308 * struct fown_struct is never outside the context of a struct file
1309 */
1310 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001311
Etienne Bassetecfcc532009-04-08 20:40:06 +02001312 /* we don't log here as rc can be overriden */
1313 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001314 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001315 rc = 0;
1316
1317 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1318 smk_ad_setfield_u_tsk(&ad, tsk);
1319 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001320 return rc;
1321}
1322
1323/**
1324 * smack_file_receive - Smack file receive check
1325 * @file: the object
1326 *
1327 * Returns 0 if current has access, error code otherwise
1328 */
1329static int smack_file_receive(struct file *file)
1330{
1331 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001332 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001333
Etienne Bassetecfcc532009-04-08 20:40:06 +02001334 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1335 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001336 /*
1337 * This code relies on bitmasks.
1338 */
1339 if (file->f_mode & FMODE_READ)
1340 may = MAY_READ;
1341 if (file->f_mode & FMODE_WRITE)
1342 may |= MAY_WRITE;
1343
Etienne Bassetecfcc532009-04-08 20:40:06 +02001344 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001345}
1346
Casey Schaufler531f1d42011-09-19 12:41:42 -07001347/**
1348 * smack_dentry_open - Smack dentry open processing
1349 * @file: the object
1350 * @cred: unused
1351 *
1352 * Set the security blob in the file structure.
1353 *
1354 * Returns 0
1355 */
1356static int smack_dentry_open(struct file *file, const struct cred *cred)
1357{
1358 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1359
1360 file->f_security = isp->smk_inode;
1361
1362 return 0;
1363}
1364
Casey Schauflere114e472008-02-04 22:29:50 -08001365/*
1366 * Task hooks
1367 */
1368
1369/**
David Howellsee18d642009-09-02 09:14:21 +01001370 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1371 * @new: the new credentials
1372 * @gfp: the atomicity of any memory allocations
1373 *
1374 * Prepare a blank set of credentials for modification. This must allocate all
1375 * the memory the LSM module might require such that cred_transfer() can
1376 * complete without error.
1377 */
1378static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1379{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001380 struct task_smack *tsp;
1381
1382 tsp = new_task_smack(NULL, NULL, gfp);
1383 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001384 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001385
1386 cred->security = tsp;
1387
David Howellsee18d642009-09-02 09:14:21 +01001388 return 0;
1389}
1390
1391
1392/**
David Howellsf1752ee2008-11-14 10:39:17 +11001393 * smack_cred_free - "free" task-level security credentials
1394 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001395 *
Casey Schauflere114e472008-02-04 22:29:50 -08001396 */
David Howellsf1752ee2008-11-14 10:39:17 +11001397static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001398{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001399 struct task_smack *tsp = cred->security;
1400 struct smack_rule *rp;
1401 struct list_head *l;
1402 struct list_head *n;
1403
1404 if (tsp == NULL)
1405 return;
1406 cred->security = NULL;
1407
1408 list_for_each_safe(l, n, &tsp->smk_rules) {
1409 rp = list_entry(l, struct smack_rule, list);
1410 list_del(&rp->list);
1411 kfree(rp);
1412 }
1413 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001414}
1415
1416/**
David Howellsd84f4f92008-11-14 10:39:23 +11001417 * smack_cred_prepare - prepare new set of credentials for modification
1418 * @new: the new credentials
1419 * @old: the original credentials
1420 * @gfp: the atomicity of any memory allocations
1421 *
1422 * Prepare a new set of credentials for modification.
1423 */
1424static int smack_cred_prepare(struct cred *new, const struct cred *old,
1425 gfp_t gfp)
1426{
Casey Schaufler676dac42010-12-02 06:43:39 -08001427 struct task_smack *old_tsp = old->security;
1428 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001429 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001430
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001431 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001432 if (new_tsp == NULL)
1433 return -ENOMEM;
1434
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001435 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1436 if (rc != 0)
1437 return rc;
1438
Casey Schaufler676dac42010-12-02 06:43:39 -08001439 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001440 return 0;
1441}
1442
Randy Dunlap251a2a92009-02-18 11:42:33 -08001443/**
David Howellsee18d642009-09-02 09:14:21 +01001444 * smack_cred_transfer - Transfer the old credentials to the new credentials
1445 * @new: the new credentials
1446 * @old: the original credentials
1447 *
1448 * Fill in a set of blank credentials from another set of credentials.
1449 */
1450static void smack_cred_transfer(struct cred *new, const struct cred *old)
1451{
Casey Schaufler676dac42010-12-02 06:43:39 -08001452 struct task_smack *old_tsp = old->security;
1453 struct task_smack *new_tsp = new->security;
1454
1455 new_tsp->smk_task = old_tsp->smk_task;
1456 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001457 mutex_init(&new_tsp->smk_rules_lock);
1458 INIT_LIST_HEAD(&new_tsp->smk_rules);
1459
1460
1461 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001462}
1463
1464/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001465 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001466 * @new: points to the set of credentials to be modified.
1467 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001468 *
1469 * Set the security data for a kernel service.
1470 */
1471static int smack_kernel_act_as(struct cred *new, u32 secid)
1472{
Casey Schaufler676dac42010-12-02 06:43:39 -08001473 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001474 char *smack = smack_from_secid(secid);
1475
1476 if (smack == NULL)
1477 return -EINVAL;
1478
Casey Schaufler676dac42010-12-02 06:43:39 -08001479 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001480 return 0;
1481}
1482
1483/**
1484 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001485 * @new: points to the set of credentials to be modified
1486 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001487 *
1488 * Set the file creation context in a set of credentials to the same
1489 * as the objective context of the specified inode
1490 */
1491static int smack_kernel_create_files_as(struct cred *new,
1492 struct inode *inode)
1493{
1494 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001495 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001496
Casey Schaufler676dac42010-12-02 06:43:39 -08001497 tsp->smk_forked = isp->smk_inode;
1498 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001499 return 0;
1500}
1501
1502/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001503 * smk_curacc_on_task - helper to log task related access
1504 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001505 * @access: the access requested
1506 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001507 *
1508 * Return 0 if access is permitted
1509 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001510static int smk_curacc_on_task(struct task_struct *p, int access,
1511 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001512{
1513 struct smk_audit_info ad;
1514
Casey Schaufler531f1d42011-09-19 12:41:42 -07001515 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001516 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001517 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001518}
1519
1520/**
Casey Schauflere114e472008-02-04 22:29:50 -08001521 * smack_task_setpgid - Smack check on setting pgid
1522 * @p: the task object
1523 * @pgid: unused
1524 *
1525 * Return 0 if write access is permitted
1526 */
1527static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1528{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001529 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001530}
1531
1532/**
1533 * smack_task_getpgid - Smack access check for getpgid
1534 * @p: the object task
1535 *
1536 * Returns 0 if current can read the object task, error code otherwise
1537 */
1538static int smack_task_getpgid(struct task_struct *p)
1539{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001540 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001541}
1542
1543/**
1544 * smack_task_getsid - Smack access check for getsid
1545 * @p: the object task
1546 *
1547 * Returns 0 if current can read the object task, error code otherwise
1548 */
1549static int smack_task_getsid(struct task_struct *p)
1550{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001551 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001552}
1553
1554/**
1555 * smack_task_getsecid - get the secid of the task
1556 * @p: the object task
1557 * @secid: where to put the result
1558 *
1559 * Sets the secid to contain a u32 version of the smack label.
1560 */
1561static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1562{
Casey Schaufler676dac42010-12-02 06:43:39 -08001563 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001564}
1565
1566/**
1567 * smack_task_setnice - Smack check on setting nice
1568 * @p: the task object
1569 * @nice: unused
1570 *
1571 * Return 0 if write access is permitted
1572 */
1573static int smack_task_setnice(struct task_struct *p, int nice)
1574{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001575 int rc;
1576
1577 rc = cap_task_setnice(p, nice);
1578 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001579 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001580 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001581}
1582
1583/**
1584 * smack_task_setioprio - Smack check on setting ioprio
1585 * @p: the task object
1586 * @ioprio: unused
1587 *
1588 * Return 0 if write access is permitted
1589 */
1590static int smack_task_setioprio(struct task_struct *p, int ioprio)
1591{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001592 int rc;
1593
1594 rc = cap_task_setioprio(p, ioprio);
1595 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001596 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001597 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001598}
1599
1600/**
1601 * smack_task_getioprio - Smack check on reading ioprio
1602 * @p: the task object
1603 *
1604 * Return 0 if read access is permitted
1605 */
1606static int smack_task_getioprio(struct task_struct *p)
1607{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001608 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001609}
1610
1611/**
1612 * smack_task_setscheduler - Smack check on setting scheduler
1613 * @p: the task object
1614 * @policy: unused
1615 * @lp: unused
1616 *
1617 * Return 0 if read access is permitted
1618 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001619static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001620{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001621 int rc;
1622
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001623 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001624 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001625 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001626 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001627}
1628
1629/**
1630 * smack_task_getscheduler - Smack check on reading scheduler
1631 * @p: the task object
1632 *
1633 * Return 0 if read access is permitted
1634 */
1635static int smack_task_getscheduler(struct task_struct *p)
1636{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001637 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001638}
1639
1640/**
1641 * smack_task_movememory - Smack check on moving memory
1642 * @p: the task object
1643 *
1644 * Return 0 if write access is permitted
1645 */
1646static int smack_task_movememory(struct task_struct *p)
1647{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001648 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001649}
1650
1651/**
1652 * smack_task_kill - Smack check on signal delivery
1653 * @p: the task object
1654 * @info: unused
1655 * @sig: unused
1656 * @secid: identifies the smack to use in lieu of current's
1657 *
1658 * Return 0 if write access is permitted
1659 *
1660 * The secid behavior is an artifact of an SELinux hack
1661 * in the USB code. Someday it may go away.
1662 */
1663static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1664 int sig, u32 secid)
1665{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001666 struct smk_audit_info ad;
1667
1668 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1669 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001670 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001671 * Sending a signal requires that the sender
1672 * can write the receiver.
1673 */
1674 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001675 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1676 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001677 /*
1678 * If the secid isn't 0 we're dealing with some USB IO
1679 * specific behavior. This is not clean. For one thing
1680 * we can't take privilege into account.
1681 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001682 return smk_access(smack_from_secid(secid),
1683 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001684}
1685
1686/**
1687 * smack_task_wait - Smack access check for waiting
1688 * @p: task to wait for
1689 *
1690 * Returns 0 if current can wait for p, error code otherwise
1691 */
1692static int smack_task_wait(struct task_struct *p)
1693{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001694 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08001695 char *sp = smk_of_current();
1696 char *tsp = smk_of_forked(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001697 int rc;
1698
Etienne Bassetecfcc532009-04-08 20:40:06 +02001699 /* we don't log here, we can be overriden */
Casey Schaufler676dac42010-12-02 06:43:39 -08001700 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001701 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001702 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001703
1704 /*
1705 * Allow the operation to succeed if either task
1706 * has privilege to perform operations that might
1707 * account for the smack labels having gotten to
1708 * be different in the first place.
1709 *
David Howells5cd9c582008-08-14 11:37:28 +01001710 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001711 * control ideal, taking the object's privilege
1712 * state into account in the decision as well as
1713 * the smack value.
1714 */
David Howells5cd9c582008-08-14 11:37:28 +01001715 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001716 rc = 0;
1717 /* we log only if we didn't get overriden */
1718 out_log:
1719 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1720 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001721 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001722 return rc;
1723}
1724
1725/**
1726 * smack_task_to_inode - copy task smack into the inode blob
1727 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001728 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001729 *
1730 * Sets the smack pointer in the inode security blob
1731 */
1732static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1733{
1734 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001735 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001736}
1737
1738/*
1739 * Socket hooks.
1740 */
1741
1742/**
1743 * smack_sk_alloc_security - Allocate a socket blob
1744 * @sk: the socket
1745 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001746 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001747 *
1748 * Assign Smack pointers to current
1749 *
1750 * Returns 0 on success, -ENOMEM is there's no memory
1751 */
1752static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1753{
Casey Schaufler676dac42010-12-02 06:43:39 -08001754 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001755 struct socket_smack *ssp;
1756
1757 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1758 if (ssp == NULL)
1759 return -ENOMEM;
1760
1761 ssp->smk_in = csp;
1762 ssp->smk_out = csp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001763 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001764
1765 sk->sk_security = ssp;
1766
1767 return 0;
1768}
1769
1770/**
1771 * smack_sk_free_security - Free a socket blob
1772 * @sk: the socket
1773 *
1774 * Clears the blob pointer
1775 */
1776static void smack_sk_free_security(struct sock *sk)
1777{
1778 kfree(sk->sk_security);
1779}
1780
1781/**
Paul Moore07feee82009-03-27 17:10:54 -04001782* smack_host_label - check host based restrictions
1783* @sip: the object end
1784*
1785* looks for host based access restrictions
1786*
1787* This version will only be appropriate for really small sets of single label
1788* hosts. The caller is responsible for ensuring that the RCU read lock is
1789* taken before calling this function.
1790*
1791* Returns the label of the far end or NULL if it's not special.
1792*/
1793static char *smack_host_label(struct sockaddr_in *sip)
1794{
1795 struct smk_netlbladdr *snp;
1796 struct in_addr *siap = &sip->sin_addr;
1797
1798 if (siap->s_addr == 0)
1799 return NULL;
1800
1801 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1802 /*
1803 * we break after finding the first match because
1804 * the list is sorted from longest to shortest mask
1805 * so we have found the most specific match
1806 */
1807 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001808 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1809 /* we have found the special CIPSO option */
1810 if (snp->smk_label == smack_cipso_option)
1811 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001812 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001813 }
Paul Moore07feee82009-03-27 17:10:54 -04001814
1815 return NULL;
1816}
1817
1818/**
Casey Schauflere114e472008-02-04 22:29:50 -08001819 * smack_set_catset - convert a capset to netlabel mls categories
1820 * @catset: the Smack categories
1821 * @sap: where to put the netlabel categories
1822 *
1823 * Allocates and fills attr.mls.cat
1824 */
1825static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1826{
1827 unsigned char *cp;
1828 unsigned char m;
1829 int cat;
1830 int rc;
1831 int byte;
1832
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001833 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001834 return;
1835
1836 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1837 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1838 sap->attr.mls.cat->startbit = 0;
1839
1840 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1841 for (m = 0x80; m != 0; m >>= 1, cat++) {
1842 if ((m & *cp) == 0)
1843 continue;
1844 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1845 cat, GFP_ATOMIC);
1846 }
1847}
1848
1849/**
1850 * smack_to_secattr - fill a secattr from a smack value
1851 * @smack: the smack value
1852 * @nlsp: where the result goes
1853 *
1854 * Casey says that CIPSO is good enough for now.
1855 * It can be used to effect.
1856 * It can also be abused to effect when necessary.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001857 * Apologies to the TSIG group in general and GW in particular.
Casey Schauflere114e472008-02-04 22:29:50 -08001858 */
1859static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1860{
1861 struct smack_cipso cipso;
1862 int rc;
1863
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001864 nlsp->domain = smack;
1865 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001866
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001867 rc = smack_to_cipso(smack, &cipso);
1868 if (rc == 0) {
1869 nlsp->attr.mls.lvl = cipso.smk_level;
1870 smack_set_catset(cipso.smk_catset, nlsp);
1871 } else {
1872 nlsp->attr.mls.lvl = smack_cipso_direct;
1873 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001874 }
1875}
1876
1877/**
1878 * smack_netlabel - Set the secattr on a socket
1879 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001880 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001881 *
1882 * Convert the outbound smack value (smk_out) to a
1883 * secattr and attach it to the socket.
1884 *
1885 * Returns 0 on success or an error code
1886 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001887static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001888{
Paul Moore07feee82009-03-27 17:10:54 -04001889 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001890 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001891 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001892
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001893 /*
1894 * Usually the netlabel code will handle changing the
1895 * packet labeling based on the label.
1896 * The case of a single label host is different, because
1897 * a single label host should never get a labeled packet
1898 * even though the label is usually associated with a packet
1899 * label.
1900 */
1901 local_bh_disable();
1902 bh_lock_sock_nested(sk);
1903
1904 if (ssp->smk_out == smack_net_ambient ||
1905 labeled == SMACK_UNLABELED_SOCKET)
1906 netlbl_sock_delattr(sk);
1907 else {
1908 netlbl_secattr_init(&secattr);
1909 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001910 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001911 netlbl_secattr_destroy(&secattr);
1912 }
1913
1914 bh_unlock_sock(sk);
1915 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001916
Casey Schauflere114e472008-02-04 22:29:50 -08001917 return rc;
1918}
1919
1920/**
Paul Moore07feee82009-03-27 17:10:54 -04001921 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1922 * @sk: the socket
1923 * @sap: the destination address
1924 *
1925 * Set the correct secattr for the given socket based on the destination
1926 * address and perform any outbound access checks needed.
1927 *
1928 * Returns 0 on success or an error code.
1929 *
1930 */
1931static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1932{
1933 int rc;
1934 int sk_lbl;
1935 char *hostsp;
1936 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001937 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001938
1939 rcu_read_lock();
1940 hostsp = smack_host_label(sap);
1941 if (hostsp != NULL) {
1942 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001943#ifdef CONFIG_AUDIT
1944 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1945 ad.a.u.net.family = sap->sin_family;
1946 ad.a.u.net.dport = sap->sin_port;
1947 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1948#endif
1949 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001950 } else {
1951 sk_lbl = SMACK_CIPSO_SOCKET;
1952 rc = 0;
1953 }
1954 rcu_read_unlock();
1955 if (rc != 0)
1956 return rc;
1957
1958 return smack_netlabel(sk, sk_lbl);
1959}
1960
1961/**
Casey Schauflere114e472008-02-04 22:29:50 -08001962 * smack_inode_setsecurity - set smack xattrs
1963 * @inode: the object
1964 * @name: attribute name
1965 * @value: attribute value
1966 * @size: size of the attribute
1967 * @flags: unused
1968 *
1969 * Sets the named attribute in the appropriate blob
1970 *
1971 * Returns 0 on success, or an error code
1972 */
1973static int smack_inode_setsecurity(struct inode *inode, const char *name,
1974 const void *value, size_t size, int flags)
1975{
1976 char *sp;
1977 struct inode_smack *nsp = inode->i_security;
1978 struct socket_smack *ssp;
1979 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001980 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001981
Etienne Basset43031542009-03-27 17:11:01 -04001982 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001983 return -EACCES;
1984
1985 sp = smk_import(value, size);
1986 if (sp == NULL)
1987 return -EINVAL;
1988
1989 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1990 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04001991 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08001992 return 0;
1993 }
1994 /*
1995 * The rest of the Smack xattrs are only on sockets.
1996 */
1997 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1998 return -EOPNOTSUPP;
1999
2000 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002001 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002002 return -EOPNOTSUPP;
2003
2004 ssp = sock->sk->sk_security;
2005
2006 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2007 ssp->smk_in = sp;
2008 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2009 ssp->smk_out = sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002010 if (sock->sk->sk_family != PF_UNIX) {
2011 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2012 if (rc != 0)
2013 printk(KERN_WARNING
2014 "Smack: \"%s\" netlbl error %d.\n",
2015 __func__, -rc);
2016 }
Casey Schauflere114e472008-02-04 22:29:50 -08002017 } else
2018 return -EOPNOTSUPP;
2019
2020 return 0;
2021}
2022
2023/**
2024 * smack_socket_post_create - finish socket setup
2025 * @sock: the socket
2026 * @family: protocol family
2027 * @type: unused
2028 * @protocol: unused
2029 * @kern: unused
2030 *
2031 * Sets the netlabel information on the socket
2032 *
2033 * Returns 0 on success, and error code otherwise
2034 */
2035static int smack_socket_post_create(struct socket *sock, int family,
2036 int type, int protocol, int kern)
2037{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002038 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002039 return 0;
2040 /*
2041 * Set the outbound netlbl.
2042 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002043 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2044}
2045
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002046/**
2047 * smack_socket_connect - connect access check
2048 * @sock: the socket
2049 * @sap: the other end
2050 * @addrlen: size of sap
2051 *
2052 * Verifies that a connection may be possible
2053 *
2054 * Returns 0 on success, and error code otherwise
2055 */
2056static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2057 int addrlen)
2058{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002059 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2060 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002061 if (addrlen < sizeof(struct sockaddr_in))
2062 return -EINVAL;
2063
Paul Moore07feee82009-03-27 17:10:54 -04002064 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08002065}
2066
2067/**
2068 * smack_flags_to_may - convert S_ to MAY_ values
2069 * @flags: the S_ value
2070 *
2071 * Returns the equivalent MAY_ value
2072 */
2073static int smack_flags_to_may(int flags)
2074{
2075 int may = 0;
2076
2077 if (flags & S_IRUGO)
2078 may |= MAY_READ;
2079 if (flags & S_IWUGO)
2080 may |= MAY_WRITE;
2081 if (flags & S_IXUGO)
2082 may |= MAY_EXEC;
2083
2084 return may;
2085}
2086
2087/**
2088 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2089 * @msg: the object
2090 *
2091 * Returns 0
2092 */
2093static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2094{
Casey Schaufler676dac42010-12-02 06:43:39 -08002095 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002096 return 0;
2097}
2098
2099/**
2100 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2101 * @msg: the object
2102 *
2103 * Clears the blob pointer
2104 */
2105static void smack_msg_msg_free_security(struct msg_msg *msg)
2106{
2107 msg->security = NULL;
2108}
2109
2110/**
2111 * smack_of_shm - the smack pointer for the shm
2112 * @shp: the object
2113 *
2114 * Returns a pointer to the smack value
2115 */
2116static char *smack_of_shm(struct shmid_kernel *shp)
2117{
2118 return (char *)shp->shm_perm.security;
2119}
2120
2121/**
2122 * smack_shm_alloc_security - Set the security blob for shm
2123 * @shp: the object
2124 *
2125 * Returns 0
2126 */
2127static int smack_shm_alloc_security(struct shmid_kernel *shp)
2128{
2129 struct kern_ipc_perm *isp = &shp->shm_perm;
2130
Casey Schaufler676dac42010-12-02 06:43:39 -08002131 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002132 return 0;
2133}
2134
2135/**
2136 * smack_shm_free_security - Clear the security blob for shm
2137 * @shp: the object
2138 *
2139 * Clears the blob pointer
2140 */
2141static void smack_shm_free_security(struct shmid_kernel *shp)
2142{
2143 struct kern_ipc_perm *isp = &shp->shm_perm;
2144
2145 isp->security = NULL;
2146}
2147
2148/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002149 * smk_curacc_shm : check if current has access on shm
2150 * @shp : the object
2151 * @access : access requested
2152 *
2153 * Returns 0 if current has the requested access, error code otherwise
2154 */
2155static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2156{
2157 char *ssp = smack_of_shm(shp);
2158 struct smk_audit_info ad;
2159
2160#ifdef CONFIG_AUDIT
2161 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2162 ad.a.u.ipc_id = shp->shm_perm.id;
2163#endif
2164 return smk_curacc(ssp, access, &ad);
2165}
2166
2167/**
Casey Schauflere114e472008-02-04 22:29:50 -08002168 * smack_shm_associate - Smack access check for shm
2169 * @shp: the object
2170 * @shmflg: access requested
2171 *
2172 * Returns 0 if current has the requested access, error code otherwise
2173 */
2174static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2175{
Casey Schauflere114e472008-02-04 22:29:50 -08002176 int may;
2177
2178 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002179 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002180}
2181
2182/**
2183 * smack_shm_shmctl - Smack access check for shm
2184 * @shp: the object
2185 * @cmd: what it wants to do
2186 *
2187 * Returns 0 if current has the requested access, error code otherwise
2188 */
2189static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2190{
Casey Schauflere114e472008-02-04 22:29:50 -08002191 int may;
2192
2193 switch (cmd) {
2194 case IPC_STAT:
2195 case SHM_STAT:
2196 may = MAY_READ;
2197 break;
2198 case IPC_SET:
2199 case SHM_LOCK:
2200 case SHM_UNLOCK:
2201 case IPC_RMID:
2202 may = MAY_READWRITE;
2203 break;
2204 case IPC_INFO:
2205 case SHM_INFO:
2206 /*
2207 * System level information.
2208 */
2209 return 0;
2210 default:
2211 return -EINVAL;
2212 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002213 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002214}
2215
2216/**
2217 * smack_shm_shmat - Smack access for shmat
2218 * @shp: the object
2219 * @shmaddr: unused
2220 * @shmflg: access requested
2221 *
2222 * Returns 0 if current has the requested access, error code otherwise
2223 */
2224static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2225 int shmflg)
2226{
Casey Schauflere114e472008-02-04 22:29:50 -08002227 int may;
2228
2229 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002230 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002231}
2232
2233/**
2234 * smack_of_sem - the smack pointer for the sem
2235 * @sma: the object
2236 *
2237 * Returns a pointer to the smack value
2238 */
2239static char *smack_of_sem(struct sem_array *sma)
2240{
2241 return (char *)sma->sem_perm.security;
2242}
2243
2244/**
2245 * smack_sem_alloc_security - Set the security blob for sem
2246 * @sma: the object
2247 *
2248 * Returns 0
2249 */
2250static int smack_sem_alloc_security(struct sem_array *sma)
2251{
2252 struct kern_ipc_perm *isp = &sma->sem_perm;
2253
Casey Schaufler676dac42010-12-02 06:43:39 -08002254 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002255 return 0;
2256}
2257
2258/**
2259 * smack_sem_free_security - Clear the security blob for sem
2260 * @sma: the object
2261 *
2262 * Clears the blob pointer
2263 */
2264static void smack_sem_free_security(struct sem_array *sma)
2265{
2266 struct kern_ipc_perm *isp = &sma->sem_perm;
2267
2268 isp->security = NULL;
2269}
2270
2271/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002272 * smk_curacc_sem : check if current has access on sem
2273 * @sma : the object
2274 * @access : access requested
2275 *
2276 * Returns 0 if current has the requested access, error code otherwise
2277 */
2278static int smk_curacc_sem(struct sem_array *sma, int access)
2279{
2280 char *ssp = smack_of_sem(sma);
2281 struct smk_audit_info ad;
2282
2283#ifdef CONFIG_AUDIT
2284 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2285 ad.a.u.ipc_id = sma->sem_perm.id;
2286#endif
2287 return smk_curacc(ssp, access, &ad);
2288}
2289
2290/**
Casey Schauflere114e472008-02-04 22:29:50 -08002291 * smack_sem_associate - Smack access check for sem
2292 * @sma: the object
2293 * @semflg: access requested
2294 *
2295 * Returns 0 if current has the requested access, error code otherwise
2296 */
2297static int smack_sem_associate(struct sem_array *sma, int semflg)
2298{
Casey Schauflere114e472008-02-04 22:29:50 -08002299 int may;
2300
2301 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002302 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002303}
2304
2305/**
2306 * smack_sem_shmctl - Smack access check for sem
2307 * @sma: the object
2308 * @cmd: what it wants to do
2309 *
2310 * Returns 0 if current has the requested access, error code otherwise
2311 */
2312static int smack_sem_semctl(struct sem_array *sma, int cmd)
2313{
Casey Schauflere114e472008-02-04 22:29:50 -08002314 int may;
2315
2316 switch (cmd) {
2317 case GETPID:
2318 case GETNCNT:
2319 case GETZCNT:
2320 case GETVAL:
2321 case GETALL:
2322 case IPC_STAT:
2323 case SEM_STAT:
2324 may = MAY_READ;
2325 break;
2326 case SETVAL:
2327 case SETALL:
2328 case IPC_RMID:
2329 case IPC_SET:
2330 may = MAY_READWRITE;
2331 break;
2332 case IPC_INFO:
2333 case SEM_INFO:
2334 /*
2335 * System level information
2336 */
2337 return 0;
2338 default:
2339 return -EINVAL;
2340 }
2341
Etienne Bassetecfcc532009-04-08 20:40:06 +02002342 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002343}
2344
2345/**
2346 * smack_sem_semop - Smack checks of semaphore operations
2347 * @sma: the object
2348 * @sops: unused
2349 * @nsops: unused
2350 * @alter: unused
2351 *
2352 * Treated as read and write in all cases.
2353 *
2354 * Returns 0 if access is allowed, error code otherwise
2355 */
2356static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2357 unsigned nsops, int alter)
2358{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002359 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002360}
2361
2362/**
2363 * smack_msg_alloc_security - Set the security blob for msg
2364 * @msq: the object
2365 *
2366 * Returns 0
2367 */
2368static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2369{
2370 struct kern_ipc_perm *kisp = &msq->q_perm;
2371
Casey Schaufler676dac42010-12-02 06:43:39 -08002372 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002373 return 0;
2374}
2375
2376/**
2377 * smack_msg_free_security - Clear the security blob for msg
2378 * @msq: the object
2379 *
2380 * Clears the blob pointer
2381 */
2382static void smack_msg_queue_free_security(struct msg_queue *msq)
2383{
2384 struct kern_ipc_perm *kisp = &msq->q_perm;
2385
2386 kisp->security = NULL;
2387}
2388
2389/**
2390 * smack_of_msq - the smack pointer for the msq
2391 * @msq: the object
2392 *
2393 * Returns a pointer to the smack value
2394 */
2395static char *smack_of_msq(struct msg_queue *msq)
2396{
2397 return (char *)msq->q_perm.security;
2398}
2399
2400/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002401 * smk_curacc_msq : helper to check if current has access on msq
2402 * @msq : the msq
2403 * @access : access requested
2404 *
2405 * return 0 if current has access, error otherwise
2406 */
2407static int smk_curacc_msq(struct msg_queue *msq, int access)
2408{
2409 char *msp = smack_of_msq(msq);
2410 struct smk_audit_info ad;
2411
2412#ifdef CONFIG_AUDIT
2413 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2414 ad.a.u.ipc_id = msq->q_perm.id;
2415#endif
2416 return smk_curacc(msp, access, &ad);
2417}
2418
2419/**
Casey Schauflere114e472008-02-04 22:29:50 -08002420 * smack_msg_queue_associate - Smack access check for msg_queue
2421 * @msq: the object
2422 * @msqflg: access requested
2423 *
2424 * Returns 0 if current has the requested access, error code otherwise
2425 */
2426static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2427{
Casey Schauflere114e472008-02-04 22:29:50 -08002428 int may;
2429
2430 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002431 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002432}
2433
2434/**
2435 * smack_msg_queue_msgctl - Smack access check for msg_queue
2436 * @msq: the object
2437 * @cmd: what it wants to do
2438 *
2439 * Returns 0 if current has the requested access, error code otherwise
2440 */
2441static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2442{
Casey Schauflere114e472008-02-04 22:29:50 -08002443 int may;
2444
2445 switch (cmd) {
2446 case IPC_STAT:
2447 case MSG_STAT:
2448 may = MAY_READ;
2449 break;
2450 case IPC_SET:
2451 case IPC_RMID:
2452 may = MAY_READWRITE;
2453 break;
2454 case IPC_INFO:
2455 case MSG_INFO:
2456 /*
2457 * System level information
2458 */
2459 return 0;
2460 default:
2461 return -EINVAL;
2462 }
2463
Etienne Bassetecfcc532009-04-08 20:40:06 +02002464 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002465}
2466
2467/**
2468 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2469 * @msq: the object
2470 * @msg: unused
2471 * @msqflg: access requested
2472 *
2473 * Returns 0 if current has the requested access, error code otherwise
2474 */
2475static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2476 int msqflg)
2477{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002478 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002479
Etienne Bassetecfcc532009-04-08 20:40:06 +02002480 may = smack_flags_to_may(msqflg);
2481 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002482}
2483
2484/**
2485 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2486 * @msq: the object
2487 * @msg: unused
2488 * @target: unused
2489 * @type: unused
2490 * @mode: unused
2491 *
2492 * Returns 0 if current has read and write access, error code otherwise
2493 */
2494static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2495 struct task_struct *target, long type, int mode)
2496{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002497 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002498}
2499
2500/**
2501 * smack_ipc_permission - Smack access for ipc_permission()
2502 * @ipp: the object permissions
2503 * @flag: access requested
2504 *
2505 * Returns 0 if current has read and write access, error code otherwise
2506 */
2507static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2508{
2509 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002510 int may = smack_flags_to_may(flag);
2511 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002512
Etienne Bassetecfcc532009-04-08 20:40:06 +02002513#ifdef CONFIG_AUDIT
2514 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2515 ad.a.u.ipc_id = ipp->id;
2516#endif
2517 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002518}
2519
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002520/**
2521 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002522 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002523 * @secid: where result will be saved
2524 */
2525static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2526{
2527 char *smack = ipp->security;
2528
2529 *secid = smack_to_secid(smack);
2530}
2531
Casey Schauflere114e472008-02-04 22:29:50 -08002532/**
2533 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002534 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002535 * @inode: the object
2536 *
2537 * Set the inode's security blob if it hasn't been done already.
2538 */
2539static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2540{
2541 struct super_block *sbp;
2542 struct superblock_smack *sbsp;
2543 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002544 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002545 char *fetched;
2546 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002547 char trattr[TRANS_TRUE_SIZE];
2548 int transflag = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002549 struct dentry *dp;
2550
2551 if (inode == NULL)
2552 return;
2553
2554 isp = inode->i_security;
2555
2556 mutex_lock(&isp->smk_lock);
2557 /*
2558 * If the inode is already instantiated
2559 * take the quick way out
2560 */
2561 if (isp->smk_flags & SMK_INODE_INSTANT)
2562 goto unlockandout;
2563
2564 sbp = inode->i_sb;
2565 sbsp = sbp->s_security;
2566 /*
2567 * We're going to use the superblock default label
2568 * if there's no label on the file.
2569 */
2570 final = sbsp->smk_default;
2571
2572 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002573 * If this is the root inode the superblock
2574 * may be in the process of initialization.
2575 * If that is the case use the root value out
2576 * of the superblock.
2577 */
2578 if (opt_dentry->d_parent == opt_dentry) {
2579 isp->smk_inode = sbsp->smk_root;
2580 isp->smk_flags |= SMK_INODE_INSTANT;
2581 goto unlockandout;
2582 }
2583
2584 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002585 * This is pretty hackish.
2586 * Casey says that we shouldn't have to do
2587 * file system specific code, but it does help
2588 * with keeping it simple.
2589 */
2590 switch (sbp->s_magic) {
2591 case SMACK_MAGIC:
2592 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002593 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002594 * that the smack file system doesn't do
2595 * extended attributes.
2596 */
2597 final = smack_known_star.smk_known;
2598 break;
2599 case PIPEFS_MAGIC:
2600 /*
2601 * Casey says pipes are easy (?)
2602 */
2603 final = smack_known_star.smk_known;
2604 break;
2605 case DEVPTS_SUPER_MAGIC:
2606 /*
2607 * devpts seems content with the label of the task.
2608 * Programs that change smack have to treat the
2609 * pty with respect.
2610 */
2611 final = csp;
2612 break;
2613 case SOCKFS_MAGIC:
2614 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002615 * Socket access is controlled by the socket
2616 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002617 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002618 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002619 break;
2620 case PROC_SUPER_MAGIC:
2621 /*
2622 * Casey says procfs appears not to care.
2623 * The superblock default suffices.
2624 */
2625 break;
2626 case TMPFS_MAGIC:
2627 /*
2628 * Device labels should come from the filesystem,
2629 * but watch out, because they're volitile,
2630 * getting recreated on every reboot.
2631 */
2632 final = smack_known_star.smk_known;
2633 /*
2634 * No break.
2635 *
2636 * If a smack value has been set we want to use it,
2637 * but since tmpfs isn't giving us the opportunity
2638 * to set mount options simulate setting the
2639 * superblock default.
2640 */
2641 default:
2642 /*
2643 * This isn't an understood special case.
2644 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002645 */
2646
2647 /*
2648 * UNIX domain sockets use lower level socket data.
2649 */
2650 if (S_ISSOCK(inode->i_mode)) {
2651 final = smack_known_star.smk_known;
2652 break;
2653 }
2654 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002655 * No xattr support means, alas, no SMACK label.
2656 * Use the aforeapplied default.
2657 * It would be curious if the label of the task
2658 * does not match that assigned.
2659 */
2660 if (inode->i_op->getxattr == NULL)
2661 break;
2662 /*
2663 * Get the dentry for xattr.
2664 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002665 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002666 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002667 if (fetched != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -08002668 final = fetched;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002669 if (S_ISDIR(inode->i_mode)) {
2670 trattr[0] = '\0';
2671 inode->i_op->getxattr(dp,
2672 XATTR_NAME_SMACKTRANSMUTE,
2673 trattr, TRANS_TRUE_SIZE);
2674 if (strncmp(trattr, TRANS_TRUE,
2675 TRANS_TRUE_SIZE) == 0)
2676 transflag = SMK_INODE_TRANSMUTE;
2677 }
2678 }
2679 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002680 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002681
Casey Schauflere114e472008-02-04 22:29:50 -08002682 dput(dp);
2683 break;
2684 }
2685
2686 if (final == NULL)
2687 isp->smk_inode = csp;
2688 else
2689 isp->smk_inode = final;
2690
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002691 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002692
2693unlockandout:
2694 mutex_unlock(&isp->smk_lock);
2695 return;
2696}
2697
2698/**
2699 * smack_getprocattr - Smack process attribute access
2700 * @p: the object task
2701 * @name: the name of the attribute in /proc/.../attr
2702 * @value: where to put the result
2703 *
2704 * Places a copy of the task Smack into value
2705 *
2706 * Returns the length of the smack label or an error code
2707 */
2708static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2709{
2710 char *cp;
2711 int slen;
2712
2713 if (strcmp(name, "current") != 0)
2714 return -EINVAL;
2715
Casey Schaufler676dac42010-12-02 06:43:39 -08002716 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002717 if (cp == NULL)
2718 return -ENOMEM;
2719
2720 slen = strlen(cp);
2721 *value = cp;
2722 return slen;
2723}
2724
2725/**
2726 * smack_setprocattr - Smack process attribute setting
2727 * @p: the object task
2728 * @name: the name of the attribute in /proc/.../attr
2729 * @value: the value to set
2730 * @size: the size of the value
2731 *
2732 * Sets the Smack value of the task. Only setting self
2733 * is permitted and only with privilege
2734 *
2735 * Returns the length of the smack label or an error code
2736 */
2737static int smack_setprocattr(struct task_struct *p, char *name,
2738 void *value, size_t size)
2739{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002740 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002741 struct task_smack *tsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002742 struct task_smack *oldtsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002743 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002744 char *newsmack;
2745
Casey Schauflere114e472008-02-04 22:29:50 -08002746 /*
2747 * Changing another process' Smack value is too dangerous
2748 * and supports no sane use case.
2749 */
2750 if (p != current)
2751 return -EPERM;
2752
David Howells5cd9c582008-08-14 11:37:28 +01002753 if (!capable(CAP_MAC_ADMIN))
2754 return -EPERM;
2755
Casey Schauflere114e472008-02-04 22:29:50 -08002756 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2757 return -EINVAL;
2758
2759 if (strcmp(name, "current") != 0)
2760 return -EINVAL;
2761
2762 newsmack = smk_import(value, size);
2763 if (newsmack == NULL)
2764 return -EINVAL;
2765
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002766 /*
2767 * No process is ever allowed the web ("@") label.
2768 */
2769 if (newsmack == smack_known_web.smk_known)
2770 return -EPERM;
2771
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002772 oldtsp = p->cred->security;
David Howellsd84f4f92008-11-14 10:39:23 +11002773 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002774 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002775 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002776
2777 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08002778 if (tsp == NULL) {
2779 kfree(new);
2780 return -ENOMEM;
2781 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002782 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2783 if (rc != 0)
2784 return rc;
2785
Casey Schaufler676dac42010-12-02 06:43:39 -08002786 new->security = tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002787 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002788 return size;
2789}
2790
2791/**
2792 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002793 * @sock: one sock
2794 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002795 * @newsk: unused
2796 *
2797 * Return 0 if a subject with the smack of sock could access
2798 * an object with the smack of other, otherwise an error code
2799 */
David S. Miller3610cda2011-01-05 15:38:53 -08002800static int smack_unix_stream_connect(struct sock *sock,
2801 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002802{
James Morrisd2e7ad12011-01-10 09:46:24 +11002803 struct socket_smack *ssp = sock->sk_security;
2804 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002805 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002806 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002807 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002808
Etienne Bassetecfcc532009-04-08 20:40:06 +02002809 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
David S. Miller3610cda2011-01-05 15:38:53 -08002810 smk_ad_setfield_u_net_sk(&ad, other);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002811
2812 if (!capable(CAP_MAC_OVERRIDE))
2813 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2814
Casey Schaufler975d5e52011-09-26 14:43:39 -07002815 /*
2816 * Cross reference the peer labels for SO_PEERSEC.
2817 */
2818 if (rc == 0) {
2819 nsp->smk_packet = ssp->smk_out;
2820 ssp->smk_packet = osp->smk_out;
2821 }
2822
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002823 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002824}
2825
2826/**
2827 * smack_unix_may_send - Smack access on UDS
2828 * @sock: one socket
2829 * @other: the other socket
2830 *
2831 * Return 0 if a subject with the smack of sock could access
2832 * an object with the smack of other, otherwise an error code
2833 */
2834static int smack_unix_may_send(struct socket *sock, struct socket *other)
2835{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002836 struct socket_smack *ssp = sock->sk->sk_security;
2837 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002838 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002839 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002840
Etienne Bassetecfcc532009-04-08 20:40:06 +02002841 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2842 smk_ad_setfield_u_net_sk(&ad, other->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002843
2844 if (!capable(CAP_MAC_OVERRIDE))
2845 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2846
2847 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002848}
2849
2850/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002851 * smack_socket_sendmsg - Smack check based on destination host
2852 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002853 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002854 * @size: the size of the message
2855 *
2856 * Return 0 if the current subject can write to the destination
2857 * host. This is only a question if the destination is a single
2858 * label host.
2859 */
2860static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2861 int size)
2862{
2863 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002864
2865 /*
2866 * Perfectly reasonable for this to be NULL
2867 */
Julia Lawallda34d422009-08-05 14:34:55 +02002868 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002869 return 0;
2870
Paul Moore07feee82009-03-27 17:10:54 -04002871 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002872}
2873
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002874/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002875 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002876 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002877 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08002878 *
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002879 * Returns a pointer to a Smack label found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08002880 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002881static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2882 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08002883{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002884 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002885 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002886 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002887 int pcat;
2888
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002889 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002890 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002891 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002892 * If there are flags but no level netlabel isn't
2893 * behaving the way we expect it to.
2894 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002895 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002896 * Without guidance regarding the smack value
2897 * for the packet fall back on the network
2898 * ambient value.
2899 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002900 memset(smack, '\0', SMK_LABELLEN);
2901 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2902 for (pcat = -1;;) {
2903 pcat = netlbl_secattr_catmap_walk(
2904 sap->attr.mls.cat, pcat + 1);
2905 if (pcat < 0)
2906 break;
2907 smack_catset_bit(pcat, smack);
2908 }
2909 /*
2910 * If it is CIPSO using smack direct mapping
2911 * we are already done. WeeHee.
2912 */
2913 if (sap->attr.mls.lvl == smack_cipso_direct) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002914 /*
2915 * The label sent is usually on the label list.
2916 *
2917 * If it is not we may still want to allow the
2918 * delivery.
2919 *
2920 * If the recipient is accepting all packets
2921 * because it is using the star ("*") label
2922 * for SMACK64IPIN provide the web ("@") label
2923 * so that a directed response will succeed.
2924 * This is not very correct from a MAC point
2925 * of view, but gets around the problem that
2926 * locking prevents adding the newly discovered
2927 * label to the list.
2928 * The case where the recipient is not using
2929 * the star label should obviously fail.
2930 * The easy way to do this is to provide the
2931 * star label as the subject label.
2932 */
2933 skp = smk_find_entry(smack);
2934 if (skp != NULL)
2935 return skp->smk_known;
2936 if (ssp != NULL &&
2937 ssp->smk_in == smack_known_star.smk_known)
2938 return smack_known_web.smk_known;
2939 return smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002940 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002941 /*
2942 * Look it up in the supplied table if it is not
2943 * a direct mapping.
2944 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002945 sp = smack_from_cipso(sap->attr.mls.lvl, smack);
2946 if (sp != NULL)
2947 return sp;
2948 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2949 return smack_known_web.smk_known;
2950 return smack_known_star.smk_known;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002951 }
2952 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2953 /*
2954 * Looks like a fallback, which gives us a secid.
2955 */
2956 sp = smack_from_secid(sap->attr.secid);
2957 /*
2958 * This has got to be a bug because it is
2959 * impossible to specify a fallback without
2960 * specifying the label, which will ensure
2961 * it has a secid, and the only way to get a
2962 * secid is from a fallback.
2963 */
2964 BUG_ON(sp == NULL);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002965 return sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002966 }
2967 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002968 * Without guidance regarding the smack value
2969 * for the packet fall back on the network
2970 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002971 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002972 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08002973}
2974
2975/**
2976 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2977 * @sk: socket
2978 * @skb: packet
2979 *
2980 * Returns 0 if the packet should be delivered, an error code otherwise
2981 */
2982static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2983{
2984 struct netlbl_lsm_secattr secattr;
2985 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002986 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002987 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002988 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002989 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2990 return 0;
2991
2992 /*
2993 * Translate what netlabel gave us.
2994 */
Casey Schauflere114e472008-02-04 22:29:50 -08002995 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002996
Casey Schauflere114e472008-02-04 22:29:50 -08002997 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002998 if (rc == 0)
2999 csp = smack_from_secattr(&secattr, ssp);
3000 else
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003001 csp = smack_net_ambient;
3002
Casey Schauflere114e472008-02-04 22:29:50 -08003003 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003004
Etienne Bassetecfcc532009-04-08 20:40:06 +02003005#ifdef CONFIG_AUDIT
3006 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3007 ad.a.u.net.family = sk->sk_family;
Eric Dumazet8964be42009-11-20 15:35:04 -08003008 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003009 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3010#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003011 /*
3012 * Receiving a packet requires that the other end
3013 * be able to write here. Read access is not required.
3014 * This is the simplist possible security model
3015 * for networking.
3016 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02003017 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04003018 if (rc != 0)
3019 netlbl_skbuff_err(skb, rc, 0);
3020 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003021}
3022
3023/**
3024 * smack_socket_getpeersec_stream - pull in packet label
3025 * @sock: the socket
3026 * @optval: user's destination
3027 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003028 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003029 *
3030 * returns zero on success, an error code otherwise
3031 */
3032static int smack_socket_getpeersec_stream(struct socket *sock,
3033 char __user *optval,
3034 int __user *optlen, unsigned len)
3035{
3036 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003037 char *rcp = "";
3038 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003039 int rc = 0;
3040
3041 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003042 if (ssp->smk_packet != NULL) {
3043 rcp = ssp->smk_packet;
3044 slen = strlen(rcp) + 1;
3045 }
Casey Schauflere114e472008-02-04 22:29:50 -08003046
3047 if (slen > len)
3048 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003049 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003050 rc = -EFAULT;
3051
3052 if (put_user(slen, optlen) != 0)
3053 rc = -EFAULT;
3054
3055 return rc;
3056}
3057
3058
3059/**
3060 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003061 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003062 * @skb: packet data
3063 * @secid: pointer to where to put the secid of the packet
3064 *
3065 * Sets the netlabel socket state on sk from parent
3066 */
3067static int smack_socket_getpeersec_dgram(struct socket *sock,
3068 struct sk_buff *skb, u32 *secid)
3069
3070{
3071 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003072 struct socket_smack *ssp = NULL;
3073 char *sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003074 int family = PF_UNSPEC;
3075 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003076 int rc;
3077
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003078 if (skb != NULL) {
3079 if (skb->protocol == htons(ETH_P_IP))
3080 family = PF_INET;
3081 else if (skb->protocol == htons(ETH_P_IPV6))
3082 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003083 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003084 if (family == PF_UNSPEC && sock != NULL)
3085 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003086
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003087 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003088 ssp = sock->sk->sk_security;
3089 s = smack_to_secid(ssp->smk_out);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003090 } else if (family == PF_INET || family == PF_INET6) {
3091 /*
3092 * Translate what netlabel gave us.
3093 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003094 if (sock != NULL && sock->sk != NULL)
3095 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003096 netlbl_secattr_init(&secattr);
3097 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3098 if (rc == 0) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003099 sp = smack_from_secattr(&secattr, ssp);
3100 s = smack_to_secid(sp);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003101 }
3102 netlbl_secattr_destroy(&secattr);
3103 }
3104 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003105 if (s == 0)
3106 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003107 return 0;
3108}
3109
3110/**
Paul Moore07feee82009-03-27 17:10:54 -04003111 * smack_sock_graft - Initialize a newly created socket with an existing sock
3112 * @sk: child sock
3113 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003114 *
Paul Moore07feee82009-03-27 17:10:54 -04003115 * Set the smk_{in,out} state of an existing sock based on the process that
3116 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003117 */
3118static void smack_sock_graft(struct sock *sk, struct socket *parent)
3119{
3120 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08003121
Paul Moore07feee82009-03-27 17:10:54 -04003122 if (sk == NULL ||
3123 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003124 return;
3125
3126 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08003127 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04003128 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003129}
3130
3131/**
3132 * smack_inet_conn_request - Smack access check on connect
3133 * @sk: socket involved
3134 * @skb: packet
3135 * @req: unused
3136 *
3137 * Returns 0 if a task with the packet label could write to
3138 * the socket, otherwise an error code
3139 */
3140static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3141 struct request_sock *req)
3142{
Paul Moore07feee82009-03-27 17:10:54 -04003143 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003144 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003145 struct netlbl_lsm_secattr secattr;
3146 struct sockaddr_in addr;
3147 struct iphdr *hdr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003148 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003149 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003150 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003151
Paul Moore07feee82009-03-27 17:10:54 -04003152 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3153 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3154 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08003155
Paul Moore07feee82009-03-27 17:10:54 -04003156 netlbl_secattr_init(&secattr);
3157 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003158 if (rc == 0)
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003159 sp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003160 else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003161 sp = smack_known_huh.smk_known;
Paul Moore07feee82009-03-27 17:10:54 -04003162 netlbl_secattr_destroy(&secattr);
3163
Etienne Bassetecfcc532009-04-08 20:40:06 +02003164#ifdef CONFIG_AUDIT
3165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3166 ad.a.u.net.family = family;
Eric Dumazet8964be42009-11-20 15:35:04 -08003167 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003168 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3169#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003170 /*
Paul Moore07feee82009-03-27 17:10:54 -04003171 * Receiving a packet requires that the other end be able to write
3172 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003173 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003174 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003175 if (rc != 0)
3176 return rc;
3177
3178 /*
3179 * Save the peer's label in the request_sock so we can later setup
3180 * smk_packet in the child socket so that SO_PEERCRED can report it.
3181 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003182 req->peer_secid = smack_to_secid(sp);
Paul Moore07feee82009-03-27 17:10:54 -04003183
3184 /*
3185 * We need to decide if we want to label the incoming connection here
3186 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003187 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003188 */
3189 hdr = ip_hdr(skb);
3190 addr.sin_addr.s_addr = hdr->saddr;
3191 rcu_read_lock();
3192 if (smack_host_label(&addr) == NULL) {
3193 rcu_read_unlock();
3194 netlbl_secattr_init(&secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003195 smack_to_secattr(sp, &secattr);
Paul Moore07feee82009-03-27 17:10:54 -04003196 rc = netlbl_req_setattr(req, &secattr);
3197 netlbl_secattr_destroy(&secattr);
3198 } else {
3199 rcu_read_unlock();
3200 netlbl_req_delattr(req);
3201 }
Casey Schauflere114e472008-02-04 22:29:50 -08003202
3203 return rc;
3204}
3205
Paul Moore07feee82009-03-27 17:10:54 -04003206/**
3207 * smack_inet_csk_clone - Copy the connection information to the new socket
3208 * @sk: the new socket
3209 * @req: the connection's request_sock
3210 *
3211 * Transfer the connection's peer label to the newly created socket.
3212 */
3213static void smack_inet_csk_clone(struct sock *sk,
3214 const struct request_sock *req)
3215{
3216 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003217
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003218 if (req->peer_secid != 0)
3219 ssp->smk_packet = smack_from_secid(req->peer_secid);
3220 else
3221 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003222}
3223
Casey Schauflere114e472008-02-04 22:29:50 -08003224/*
3225 * Key management security hooks
3226 *
3227 * Casey has not tested key support very heavily.
3228 * The permission check is most likely too restrictive.
3229 * If you care about keys please have a look.
3230 */
3231#ifdef CONFIG_KEYS
3232
3233/**
3234 * smack_key_alloc - Set the key security blob
3235 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003236 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003237 * @flags: unused
3238 *
3239 * No allocation required
3240 *
3241 * Returns 0
3242 */
David Howellsd84f4f92008-11-14 10:39:23 +11003243static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003244 unsigned long flags)
3245{
Casey Schaufler676dac42010-12-02 06:43:39 -08003246 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003247 return 0;
3248}
3249
3250/**
3251 * smack_key_free - Clear the key security blob
3252 * @key: the object
3253 *
3254 * Clear the blob pointer
3255 */
3256static void smack_key_free(struct key *key)
3257{
3258 key->security = NULL;
3259}
3260
3261/*
3262 * smack_key_permission - Smack access on a key
3263 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003264 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003265 * @perm: unused
3266 *
3267 * Return 0 if the task has read and write to the object,
3268 * an error code otherwise
3269 */
3270static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003271 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003272{
3273 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003274 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08003275 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003276
3277 keyp = key_ref_to_ptr(key_ref);
3278 if (keyp == NULL)
3279 return -EINVAL;
3280 /*
3281 * If the key hasn't been initialized give it access so that
3282 * it may do so.
3283 */
3284 if (keyp->security == NULL)
3285 return 0;
3286 /*
3287 * This should not occur
3288 */
Casey Schaufler676dac42010-12-02 06:43:39 -08003289 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003290 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003291#ifdef CONFIG_AUDIT
3292 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3293 ad.a.u.key_struct.key = keyp->serial;
3294 ad.a.u.key_struct.key_desc = keyp->description;
3295#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08003296 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02003297 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003298}
3299#endif /* CONFIG_KEYS */
3300
3301/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003302 * Smack Audit hooks
3303 *
3304 * Audit requires a unique representation of each Smack specific
3305 * rule. This unique representation is used to distinguish the
3306 * object to be audited from remaining kernel objects and also
3307 * works as a glue between the audit hooks.
3308 *
3309 * Since repository entries are added but never deleted, we'll use
3310 * the smack_known label address related to the given audit rule as
3311 * the needed unique representation. This also better fits the smack
3312 * model where nearly everything is a label.
3313 */
3314#ifdef CONFIG_AUDIT
3315
3316/**
3317 * smack_audit_rule_init - Initialize a smack audit rule
3318 * @field: audit rule fields given from user-space (audit.h)
3319 * @op: required testing operator (=, !=, >, <, ...)
3320 * @rulestr: smack label to be audited
3321 * @vrule: pointer to save our own audit rule representation
3322 *
3323 * Prepare to audit cases where (@field @op @rulestr) is true.
3324 * The label to be audited is created if necessay.
3325 */
3326static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3327{
3328 char **rule = (char **)vrule;
3329 *rule = NULL;
3330
3331 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3332 return -EINVAL;
3333
Al Viro5af75d82008-12-16 05:59:26 -05003334 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003335 return -EINVAL;
3336
3337 *rule = smk_import(rulestr, 0);
3338
3339 return 0;
3340}
3341
3342/**
3343 * smack_audit_rule_known - Distinguish Smack audit rules
3344 * @krule: rule of interest, in Audit kernel representation format
3345 *
3346 * This is used to filter Smack rules from remaining Audit ones.
3347 * If it's proved that this rule belongs to us, the
3348 * audit_rule_match hook will be called to do the final judgement.
3349 */
3350static int smack_audit_rule_known(struct audit_krule *krule)
3351{
3352 struct audit_field *f;
3353 int i;
3354
3355 for (i = 0; i < krule->field_count; i++) {
3356 f = &krule->fields[i];
3357
3358 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3359 return 1;
3360 }
3361
3362 return 0;
3363}
3364
3365/**
3366 * smack_audit_rule_match - Audit given object ?
3367 * @secid: security id for identifying the object to test
3368 * @field: audit rule flags given from user-space
3369 * @op: required testing operator
3370 * @vrule: smack internal rule presentation
3371 * @actx: audit context associated with the check
3372 *
3373 * The core Audit hook. It's used to take the decision of
3374 * whether to audit or not to audit a given object.
3375 */
3376static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3377 struct audit_context *actx)
3378{
3379 char *smack;
3380 char *rule = vrule;
3381
3382 if (!rule) {
3383 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3384 "Smack: missing rule\n");
3385 return -ENOENT;
3386 }
3387
3388 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3389 return 0;
3390
3391 smack = smack_from_secid(secid);
3392
3393 /*
3394 * No need to do string comparisons. If a match occurs,
3395 * both pointers will point to the same smack_known
3396 * label.
3397 */
Al Viro5af75d82008-12-16 05:59:26 -05003398 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003399 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003400 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003401 return (rule != smack);
3402
3403 return 0;
3404}
3405
3406/**
3407 * smack_audit_rule_free - free smack rule representation
3408 * @vrule: rule to be freed.
3409 *
3410 * No memory was allocated.
3411 */
3412static void smack_audit_rule_free(void *vrule)
3413{
3414 /* No-op */
3415}
3416
3417#endif /* CONFIG_AUDIT */
3418
Randy Dunlap251a2a92009-02-18 11:42:33 -08003419/**
Casey Schauflere114e472008-02-04 22:29:50 -08003420 * smack_secid_to_secctx - return the smack label for a secid
3421 * @secid: incoming integer
3422 * @secdata: destination
3423 * @seclen: how long it is
3424 *
3425 * Exists for networking code.
3426 */
3427static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3428{
3429 char *sp = smack_from_secid(secid);
3430
Eric Parisd5630b92010-10-13 16:24:48 -04003431 if (secdata)
3432 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003433 *seclen = strlen(sp);
3434 return 0;
3435}
3436
Randy Dunlap251a2a92009-02-18 11:42:33 -08003437/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003438 * smack_secctx_to_secid - return the secid for a smack label
3439 * @secdata: smack label
3440 * @seclen: how long result is
3441 * @secid: outgoing integer
3442 *
3443 * Exists for audit and networking code.
3444 */
David Howellse52c17642008-04-29 20:52:51 +01003445static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003446{
3447 *secid = smack_to_secid(secdata);
3448 return 0;
3449}
3450
Randy Dunlap251a2a92009-02-18 11:42:33 -08003451/**
Casey Schauflere114e472008-02-04 22:29:50 -08003452 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003453 * @secdata: unused
3454 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003455 *
3456 * Exists to make sure nothing gets done, and properly
3457 */
3458static void smack_release_secctx(char *secdata, u32 seclen)
3459{
3460}
3461
David P. Quigley1ee65e32009-09-03 14:25:57 -04003462static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3463{
3464 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3465}
3466
3467static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3468{
3469 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3470}
3471
3472static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3473{
3474 int len = 0;
3475 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3476
3477 if (len < 0)
3478 return len;
3479 *ctxlen = len;
3480 return 0;
3481}
3482
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003483struct security_operations smack_ops = {
3484 .name = "smack",
3485
Ingo Molnar9e488582009-05-07 19:26:19 +10003486 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003487 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003488 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003489
3490 .sb_alloc_security = smack_sb_alloc_security,
3491 .sb_free_security = smack_sb_free_security,
3492 .sb_copy_data = smack_sb_copy_data,
3493 .sb_kern_mount = smack_sb_kern_mount,
3494 .sb_statfs = smack_sb_statfs,
3495 .sb_mount = smack_sb_mount,
3496 .sb_umount = smack_sb_umount,
3497
Casey Schaufler676dac42010-12-02 06:43:39 -08003498 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003499 .bprm_committing_creds = smack_bprm_committing_creds,
3500 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003501
Casey Schauflere114e472008-02-04 22:29:50 -08003502 .inode_alloc_security = smack_inode_alloc_security,
3503 .inode_free_security = smack_inode_free_security,
3504 .inode_init_security = smack_inode_init_security,
3505 .inode_link = smack_inode_link,
3506 .inode_unlink = smack_inode_unlink,
3507 .inode_rmdir = smack_inode_rmdir,
3508 .inode_rename = smack_inode_rename,
3509 .inode_permission = smack_inode_permission,
3510 .inode_setattr = smack_inode_setattr,
3511 .inode_getattr = smack_inode_getattr,
3512 .inode_setxattr = smack_inode_setxattr,
3513 .inode_post_setxattr = smack_inode_post_setxattr,
3514 .inode_getxattr = smack_inode_getxattr,
3515 .inode_removexattr = smack_inode_removexattr,
3516 .inode_getsecurity = smack_inode_getsecurity,
3517 .inode_setsecurity = smack_inode_setsecurity,
3518 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003519 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003520
3521 .file_permission = smack_file_permission,
3522 .file_alloc_security = smack_file_alloc_security,
3523 .file_free_security = smack_file_free_security,
3524 .file_ioctl = smack_file_ioctl,
3525 .file_lock = smack_file_lock,
3526 .file_fcntl = smack_file_fcntl,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003527 .file_mmap = smack_file_mmap,
Casey Schauflere114e472008-02-04 22:29:50 -08003528 .file_set_fowner = smack_file_set_fowner,
3529 .file_send_sigiotask = smack_file_send_sigiotask,
3530 .file_receive = smack_file_receive,
3531
Casey Schaufler531f1d42011-09-19 12:41:42 -07003532 .dentry_open = smack_dentry_open,
3533
David Howellsee18d642009-09-02 09:14:21 +01003534 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003535 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003536 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003537 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003538 .kernel_act_as = smack_kernel_act_as,
3539 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003540 .task_setpgid = smack_task_setpgid,
3541 .task_getpgid = smack_task_getpgid,
3542 .task_getsid = smack_task_getsid,
3543 .task_getsecid = smack_task_getsecid,
3544 .task_setnice = smack_task_setnice,
3545 .task_setioprio = smack_task_setioprio,
3546 .task_getioprio = smack_task_getioprio,
3547 .task_setscheduler = smack_task_setscheduler,
3548 .task_getscheduler = smack_task_getscheduler,
3549 .task_movememory = smack_task_movememory,
3550 .task_kill = smack_task_kill,
3551 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003552 .task_to_inode = smack_task_to_inode,
3553
3554 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003555 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003556
3557 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3558 .msg_msg_free_security = smack_msg_msg_free_security,
3559
3560 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3561 .msg_queue_free_security = smack_msg_queue_free_security,
3562 .msg_queue_associate = smack_msg_queue_associate,
3563 .msg_queue_msgctl = smack_msg_queue_msgctl,
3564 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3565 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3566
3567 .shm_alloc_security = smack_shm_alloc_security,
3568 .shm_free_security = smack_shm_free_security,
3569 .shm_associate = smack_shm_associate,
3570 .shm_shmctl = smack_shm_shmctl,
3571 .shm_shmat = smack_shm_shmat,
3572
3573 .sem_alloc_security = smack_sem_alloc_security,
3574 .sem_free_security = smack_sem_free_security,
3575 .sem_associate = smack_sem_associate,
3576 .sem_semctl = smack_sem_semctl,
3577 .sem_semop = smack_sem_semop,
3578
Casey Schauflere114e472008-02-04 22:29:50 -08003579 .d_instantiate = smack_d_instantiate,
3580
3581 .getprocattr = smack_getprocattr,
3582 .setprocattr = smack_setprocattr,
3583
3584 .unix_stream_connect = smack_unix_stream_connect,
3585 .unix_may_send = smack_unix_may_send,
3586
3587 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003588 .socket_connect = smack_socket_connect,
3589 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003590 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3591 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3592 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3593 .sk_alloc_security = smack_sk_alloc_security,
3594 .sk_free_security = smack_sk_free_security,
3595 .sock_graft = smack_sock_graft,
3596 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003597 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003598
Casey Schauflere114e472008-02-04 22:29:50 -08003599 /* key management security hooks */
3600#ifdef CONFIG_KEYS
3601 .key_alloc = smack_key_alloc,
3602 .key_free = smack_key_free,
3603 .key_permission = smack_key_permission,
3604#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003605
3606 /* Audit hooks */
3607#ifdef CONFIG_AUDIT
3608 .audit_rule_init = smack_audit_rule_init,
3609 .audit_rule_known = smack_audit_rule_known,
3610 .audit_rule_match = smack_audit_rule_match,
3611 .audit_rule_free = smack_audit_rule_free,
3612#endif /* CONFIG_AUDIT */
3613
Casey Schauflere114e472008-02-04 22:29:50 -08003614 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003615 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003616 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003617 .inode_notifysecctx = smack_inode_notifysecctx,
3618 .inode_setsecctx = smack_inode_setsecctx,
3619 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003620};
3621
Etienne Basset7198e2e2009-03-24 20:53:24 +01003622
3623static __init void init_smack_know_list(void)
3624{
3625 list_add(&smack_known_huh.list, &smack_known_list);
3626 list_add(&smack_known_hat.list, &smack_known_list);
3627 list_add(&smack_known_star.list, &smack_known_list);
3628 list_add(&smack_known_floor.list, &smack_known_list);
3629 list_add(&smack_known_invalid.list, &smack_known_list);
3630 list_add(&smack_known_web.list, &smack_known_list);
3631}
3632
Casey Schauflere114e472008-02-04 22:29:50 -08003633/**
3634 * smack_init - initialize the smack system
3635 *
3636 * Returns 0
3637 */
3638static __init int smack_init(void)
3639{
David Howellsd84f4f92008-11-14 10:39:23 +11003640 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003641 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003642
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003643 if (!security_module_enable(&smack_ops))
3644 return 0;
3645
3646 tsp = new_task_smack(smack_known_floor.smk_known,
3647 smack_known_floor.smk_known, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003648 if (tsp == NULL)
3649 return -ENOMEM;
3650
Casey Schauflere114e472008-02-04 22:29:50 -08003651 printk(KERN_INFO "Smack: Initializing.\n");
3652
3653 /*
3654 * Set the security state for the initial task.
3655 */
David Howellsd84f4f92008-11-14 10:39:23 +11003656 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003657 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003658
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003659 /* initialize the smack_know_list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003660 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003661 /*
3662 * Initialize locks
3663 */
Casey Schauflere114e472008-02-04 22:29:50 -08003664 spin_lock_init(&smack_known_huh.smk_cipsolock);
3665 spin_lock_init(&smack_known_hat.smk_cipsolock);
3666 spin_lock_init(&smack_known_star.smk_cipsolock);
3667 spin_lock_init(&smack_known_floor.smk_cipsolock);
3668 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3669
3670 /*
3671 * Register with LSM
3672 */
3673 if (register_security(&smack_ops))
3674 panic("smack: Unable to register with kernel.\n");
3675
3676 return 0;
3677}
3678
3679/*
3680 * Smack requires early initialization in order to label
3681 * all processes and objects when they are created.
3682 */
3683security_initcall(smack_init);