blob: 7e19afe0e7388203368e1950cd04cbefab907eac [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 *
6 * Author:
7 * Casey Schaufler <casey@schaufler-ca.com>
8 *
9 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040010 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
11 * Paul Moore <paul.moore@hp.com>
Casey Schauflere114e472008-02-04 22:29:50 -080012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2,
15 * as published by the Free Software Foundation.
16 */
17
18#include <linux/xattr.h>
19#include <linux/pagemap.h>
20#include <linux/mount.h>
21#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080022#include <linux/kd.h>
23#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040024#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/tcp.h>
26#include <linux/udp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/mutex.h>
29#include <linux/pipe_fs_i.h>
30#include <net/netlabel.h>
31#include <net/cipso_ipv4.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100032#include <linux/audit.h>
Nick Black1fd73172009-09-22 16:43:33 -070033#include <linux/magic.h>
Casey Schauflere114e472008-02-04 22:29:50 -080034#include "smack.h"
35
David Howellsc69e8d92008-11-14 10:39:19 +110036#define task_security(task) (task_cred_xxx((task), security))
37
Casey Schauflere114e472008-02-04 22:29:50 -080038/**
39 * smk_fetch - Fetch the smack label from a file.
40 * @ip: a pointer to the inode
41 * @dp: a pointer to the dentry
42 *
43 * Returns a pointer to the master list entry for the Smack label
44 * or NULL if there was no label to fetch.
45 */
Casey Schaufler676dac42010-12-02 06:43:39 -080046static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080047{
48 int rc;
49 char in[SMK_LABELLEN];
50
51 if (ip->i_op->getxattr == NULL)
52 return NULL;
53
Casey Schaufler676dac42010-12-02 06:43:39 -080054 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
Casey Schauflere114e472008-02-04 22:29:50 -080055 if (rc < 0)
56 return NULL;
57
58 return smk_import(in, rc);
59}
60
61/**
62 * new_inode_smack - allocate an inode security blob
63 * @smack: a pointer to the Smack label to use in the blob
64 *
65 * Returns the new blob or NULL if there's no memory available
66 */
67struct inode_smack *new_inode_smack(char *smack)
68{
69 struct inode_smack *isp;
70
71 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
72 if (isp == NULL)
73 return NULL;
74
75 isp->smk_inode = smack;
76 isp->smk_flags = 0;
77 mutex_init(&isp->smk_lock);
78
79 return isp;
80}
81
82/*
83 * LSM hooks.
84 * We he, that is fun!
85 */
86
87/**
Ingo Molnar9e488582009-05-07 19:26:19 +100088 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -080089 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -080090 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -080091 *
92 * Returns 0 if access is OK, an error code otherwise
93 *
94 * Do the capability checks, and require read and write.
95 */
Ingo Molnar9e488582009-05-07 19:26:19 +100096static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -080097{
98 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +020099 struct smk_audit_info ad;
100 char *sp, *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800101
Ingo Molnar9e488582009-05-07 19:26:19 +1000102 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800103 if (rc != 0)
104 return rc;
105
Casey Schaufler676dac42010-12-02 06:43:39 -0800106 sp = smk_of_current();
107 tsp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200108 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
109 smk_ad_setfield_u_tsk(&ad, ctp);
110
111 /* we won't log here, because rc can be overriden */
112 rc = smk_access(sp, tsp, MAY_READWRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +0100113 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200114 rc = 0;
115
116 smack_log(sp, tsp, MAY_READWRITE, rc, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100117 return rc;
118}
Casey Schauflere114e472008-02-04 22:29:50 -0800119
David Howells5cd9c582008-08-14 11:37:28 +0100120/**
121 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
122 * @ptp: parent task pointer
123 *
124 * Returns 0 if access is OK, an error code otherwise
125 *
126 * Do the capability checks, and require read and write.
127 */
128static int smack_ptrace_traceme(struct task_struct *ptp)
129{
130 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200131 struct smk_audit_info ad;
132 char *sp, *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100133
134 rc = cap_ptrace_traceme(ptp);
135 if (rc != 0)
136 return rc;
137
Etienne Bassetecfcc532009-04-08 20:40:06 +0200138 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
139 smk_ad_setfield_u_tsk(&ad, ptp);
140
Casey Schaufler676dac42010-12-02 06:43:39 -0800141 sp = smk_of_current();
142 tsp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200143 /* we won't log here, because rc can be overriden */
144 rc = smk_access(tsp, sp, MAY_READWRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +0100145 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200146 rc = 0;
147
148 smack_log(tsp, sp, MAY_READWRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800149 return rc;
150}
151
152/**
153 * smack_syslog - Smack approval on syslog
154 * @type: message type
155 *
156 * Require that the task has the floor label
157 *
158 * Returns 0 on success, error code otherwise.
159 */
Eric Paris12b30522010-11-15 18:36:29 -0500160static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800161{
Eric Paris12b30522010-11-15 18:36:29 -0500162 int rc = 0;
Casey Schaufler676dac42010-12-02 06:43:39 -0800163 char *sp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800164
Casey Schauflere114e472008-02-04 22:29:50 -0800165 if (capable(CAP_MAC_OVERRIDE))
166 return 0;
167
168 if (sp != smack_known_floor.smk_known)
169 rc = -EACCES;
170
171 return rc;
172}
173
174
175/*
176 * Superblock Hooks.
177 */
178
179/**
180 * smack_sb_alloc_security - allocate a superblock blob
181 * @sb: the superblock getting the blob
182 *
183 * Returns 0 on success or -ENOMEM on error.
184 */
185static int smack_sb_alloc_security(struct super_block *sb)
186{
187 struct superblock_smack *sbsp;
188
189 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
190
191 if (sbsp == NULL)
192 return -ENOMEM;
193
194 sbsp->smk_root = smack_known_floor.smk_known;
195 sbsp->smk_default = smack_known_floor.smk_known;
196 sbsp->smk_floor = smack_known_floor.smk_known;
197 sbsp->smk_hat = smack_known_hat.smk_known;
198 sbsp->smk_initialized = 0;
199 spin_lock_init(&sbsp->smk_sblock);
200
201 sb->s_security = sbsp;
202
203 return 0;
204}
205
206/**
207 * smack_sb_free_security - free a superblock blob
208 * @sb: the superblock getting the blob
209 *
210 */
211static void smack_sb_free_security(struct super_block *sb)
212{
213 kfree(sb->s_security);
214 sb->s_security = NULL;
215}
216
217/**
218 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800219 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800220 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800221 *
222 * Returns 0 on success or -ENOMEM on error.
223 *
224 * Copy the Smack specific mount options out of the mount
225 * options list.
226 */
Eric Parise0007522008-03-05 10:31:54 -0500227static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800228{
229 char *cp, *commap, *otheropts, *dp;
230
Casey Schauflere114e472008-02-04 22:29:50 -0800231 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
232 if (otheropts == NULL)
233 return -ENOMEM;
234
235 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
236 if (strstr(cp, SMK_FSDEFAULT) == cp)
237 dp = smackopts;
238 else if (strstr(cp, SMK_FSFLOOR) == cp)
239 dp = smackopts;
240 else if (strstr(cp, SMK_FSHAT) == cp)
241 dp = smackopts;
242 else if (strstr(cp, SMK_FSROOT) == cp)
243 dp = smackopts;
244 else
245 dp = otheropts;
246
247 commap = strchr(cp, ',');
248 if (commap != NULL)
249 *commap = '\0';
250
251 if (*dp != '\0')
252 strcat(dp, ",");
253 strcat(dp, cp);
254 }
255
256 strcpy(orig, otheropts);
257 free_page((unsigned long)otheropts);
258
259 return 0;
260}
261
262/**
263 * smack_sb_kern_mount - Smack specific mount processing
264 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100265 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800266 * @data: the smack mount options
267 *
268 * Returns 0 on success, an error code on failure
269 */
James Morris12204e22008-12-19 10:44:42 +1100270static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800271{
272 struct dentry *root = sb->s_root;
273 struct inode *inode = root->d_inode;
274 struct superblock_smack *sp = sb->s_security;
275 struct inode_smack *isp;
276 char *op;
277 char *commap;
278 char *nsp;
279
280 spin_lock(&sp->smk_sblock);
281 if (sp->smk_initialized != 0) {
282 spin_unlock(&sp->smk_sblock);
283 return 0;
284 }
285 sp->smk_initialized = 1;
286 spin_unlock(&sp->smk_sblock);
287
288 for (op = data; op != NULL; op = commap) {
289 commap = strchr(op, ',');
290 if (commap != NULL)
291 *commap++ = '\0';
292
293 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
294 op += strlen(SMK_FSHAT);
295 nsp = smk_import(op, 0);
296 if (nsp != NULL)
297 sp->smk_hat = nsp;
298 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
299 op += strlen(SMK_FSFLOOR);
300 nsp = smk_import(op, 0);
301 if (nsp != NULL)
302 sp->smk_floor = nsp;
303 } else if (strncmp(op, SMK_FSDEFAULT,
304 strlen(SMK_FSDEFAULT)) == 0) {
305 op += strlen(SMK_FSDEFAULT);
306 nsp = smk_import(op, 0);
307 if (nsp != NULL)
308 sp->smk_default = nsp;
309 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
310 op += strlen(SMK_FSROOT);
311 nsp = smk_import(op, 0);
312 if (nsp != NULL)
313 sp->smk_root = nsp;
314 }
315 }
316
317 /*
318 * Initialize the root inode.
319 */
320 isp = inode->i_security;
321 if (isp == NULL)
322 inode->i_security = new_inode_smack(sp->smk_root);
323 else
324 isp->smk_inode = sp->smk_root;
325
326 return 0;
327}
328
329/**
330 * smack_sb_statfs - Smack check on statfs
331 * @dentry: identifies the file system in question
332 *
333 * Returns 0 if current can read the floor of the filesystem,
334 * and error code otherwise
335 */
336static int smack_sb_statfs(struct dentry *dentry)
337{
338 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200339 int rc;
340 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800341
Etienne Bassetecfcc532009-04-08 20:40:06 +0200342 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
343 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
344
345 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
346 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800347}
348
349/**
350 * smack_sb_mount - Smack check for mounting
351 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800352 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800353 * @type: unused
354 * @flags: unused
355 * @data: unused
356 *
357 * Returns 0 if current can write the floor of the filesystem
358 * being mounted on, an error code otherwise.
359 */
Al Virob5266eb2008-03-22 17:48:24 -0400360static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800361 char *type, unsigned long flags, void *data)
362{
Al Virob5266eb2008-03-22 17:48:24 -0400363 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200364 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800365
Etienne Bassetecfcc532009-04-08 20:40:06 +0200366 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
367 smk_ad_setfield_u_fs_path(&ad, *path);
368
369 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800370}
371
372/**
373 * smack_sb_umount - Smack check for unmounting
374 * @mnt: file system to unmount
375 * @flags: unused
376 *
377 * Returns 0 if current can write the floor of the filesystem
378 * being unmounted, an error code otherwise.
379 */
380static int smack_sb_umount(struct vfsmount *mnt, int flags)
381{
382 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200383 struct smk_audit_info ad;
384
385 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
Al Virode27a5b2010-01-30 15:27:27 -0500386 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_root);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200387 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
Casey Schauflere114e472008-02-04 22:29:50 -0800388
389 sbp = mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200390 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800391}
392
393/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800394 * BPRM hooks
395 */
396
397static int smack_bprm_set_creds(struct linux_binprm *bprm)
398{
399 struct task_smack *tsp = bprm->cred->security;
400 struct inode_smack *isp;
401 struct dentry *dp;
402 int rc;
403
404 rc = cap_bprm_set_creds(bprm);
405 if (rc != 0)
406 return rc;
407
408 if (bprm->cred_prepared)
409 return 0;
410
411 if (bprm->file == NULL || bprm->file->f_dentry == NULL)
412 return 0;
413
414 dp = bprm->file->f_dentry;
415
416 if (dp->d_inode == NULL)
417 return 0;
418
419 isp = dp->d_inode->i_security;
420
421 if (isp->smk_task != NULL)
422 tsp->smk_task = isp->smk_task;
423
424 return 0;
425}
426
427/*
Casey Schauflere114e472008-02-04 22:29:50 -0800428 * Inode hooks
429 */
430
431/**
432 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800433 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800434 *
435 * Returns 0 if it gets a blob, -ENOMEM otherwise
436 */
437static int smack_inode_alloc_security(struct inode *inode)
438{
Casey Schaufler676dac42010-12-02 06:43:39 -0800439 inode->i_security = new_inode_smack(smk_of_current());
Casey Schauflere114e472008-02-04 22:29:50 -0800440 if (inode->i_security == NULL)
441 return -ENOMEM;
442 return 0;
443}
444
445/**
446 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800447 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800448 *
449 * Clears the blob pointer in inode
450 */
451static void smack_inode_free_security(struct inode *inode)
452{
453 kfree(inode->i_security);
454 inode->i_security = NULL;
455}
456
457/**
458 * smack_inode_init_security - copy out the smack from an inode
459 * @inode: the inode
460 * @dir: unused
461 * @name: where to put the attribute name
462 * @value: where to put the attribute value
463 * @len: where to put the length of the attribute
464 *
465 * Returns 0 if it all works out, -ENOMEM if there's no memory
466 */
467static int smack_inode_init_security(struct inode *inode, struct inode *dir,
468 char **name, void **value, size_t *len)
469{
470 char *isp = smk_of_inode(inode);
471
472 if (name) {
473 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
474 if (*name == NULL)
475 return -ENOMEM;
476 }
477
478 if (value) {
479 *value = kstrdup(isp, GFP_KERNEL);
480 if (*value == NULL)
481 return -ENOMEM;
482 }
483
484 if (len)
485 *len = strlen(isp) + 1;
486
487 return 0;
488}
489
490/**
491 * smack_inode_link - Smack check on link
492 * @old_dentry: the existing object
493 * @dir: unused
494 * @new_dentry: the new object
495 *
496 * Returns 0 if access is permitted, an error code otherwise
497 */
498static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
499 struct dentry *new_dentry)
500{
Casey Schauflere114e472008-02-04 22:29:50 -0800501 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200502 struct smk_audit_info ad;
503 int rc;
504
505 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
506 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800507
508 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200509 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800510
511 if (rc == 0 && new_dentry->d_inode != NULL) {
512 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200513 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
514 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800515 }
516
517 return rc;
518}
519
520/**
521 * smack_inode_unlink - Smack check on inode deletion
522 * @dir: containing directory object
523 * @dentry: file to unlink
524 *
525 * Returns 0 if current can write the containing directory
526 * and the object, error code otherwise
527 */
528static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
529{
530 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200531 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800532 int rc;
533
Etienne Bassetecfcc532009-04-08 20:40:06 +0200534 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
535 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
536
Casey Schauflere114e472008-02-04 22:29:50 -0800537 /*
538 * You need write access to the thing you're unlinking
539 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200540 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
541 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800542 /*
543 * You also need write access to the containing directory
544 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200545 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
546 smk_ad_setfield_u_fs_inode(&ad, dir);
547 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
548 }
Casey Schauflere114e472008-02-04 22:29:50 -0800549 return rc;
550}
551
552/**
553 * smack_inode_rmdir - Smack check on directory deletion
554 * @dir: containing directory object
555 * @dentry: directory to unlink
556 *
557 * Returns 0 if current can write the containing directory
558 * and the directory, error code otherwise
559 */
560static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
561{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200562 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800563 int rc;
564
Etienne Bassetecfcc532009-04-08 20:40:06 +0200565 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
566 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
567
Casey Schauflere114e472008-02-04 22:29:50 -0800568 /*
569 * You need write access to the thing you're removing
570 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200571 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
572 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800573 /*
574 * You also need write access to the containing directory
575 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200576 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
577 smk_ad_setfield_u_fs_inode(&ad, dir);
578 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
579 }
Casey Schauflere114e472008-02-04 22:29:50 -0800580
581 return rc;
582}
583
584/**
585 * smack_inode_rename - Smack check on rename
586 * @old_inode: the old directory
587 * @old_dentry: unused
588 * @new_inode: the new directory
589 * @new_dentry: unused
590 *
591 * Read and write access is required on both the old and
592 * new directories.
593 *
594 * Returns 0 if access is permitted, an error code otherwise
595 */
596static int smack_inode_rename(struct inode *old_inode,
597 struct dentry *old_dentry,
598 struct inode *new_inode,
599 struct dentry *new_dentry)
600{
601 int rc;
602 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200603 struct smk_audit_info ad;
604
605 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
606 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800607
608 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200609 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800610
611 if (rc == 0 && new_dentry->d_inode != NULL) {
612 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200613 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
614 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800615 }
Casey Schauflere114e472008-02-04 22:29:50 -0800616 return rc;
617}
618
619/**
620 * smack_inode_permission - Smack version of permission()
621 * @inode: the inode in question
622 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800623 *
624 * This is the important Smack hook.
625 *
626 * Returns 0 if access is permitted, -EACCES otherwise
627 */
Al Virob77b0642008-07-17 09:37:02 -0400628static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800629{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200630 struct smk_audit_info ad;
Eric Parisd09ca732010-07-23 11:43:57 -0400631
632 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800633 /*
634 * No permission to check. Existence test. Yup, it's there.
635 */
636 if (mask == 0)
637 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200638 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
639 smk_ad_setfield_u_fs_inode(&ad, inode);
640 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800641}
642
643/**
644 * smack_inode_setattr - Smack check for setting attributes
645 * @dentry: the object
646 * @iattr: for the force flag
647 *
648 * Returns 0 if access is permitted, an error code otherwise
649 */
650static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
651{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200652 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800653 /*
654 * Need to allow for clearing the setuid bit.
655 */
656 if (iattr->ia_valid & ATTR_FORCE)
657 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200658 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
659 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800660
Etienne Bassetecfcc532009-04-08 20:40:06 +0200661 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800662}
663
664/**
665 * smack_inode_getattr - Smack check for getting attributes
666 * @mnt: unused
667 * @dentry: the object
668 *
669 * Returns 0 if access is permitted, an error code otherwise
670 */
671static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
672{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200673 struct smk_audit_info ad;
674
675 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
676 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
677 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
678 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800679}
680
681/**
682 * smack_inode_setxattr - Smack check for setting xattrs
683 * @dentry: the object
684 * @name: name of the attribute
685 * @value: unused
686 * @size: unused
687 * @flags: unused
688 *
689 * This protects the Smack attribute explicitly.
690 *
691 * Returns 0 if access is permitted, an error code otherwise
692 */
David Howells8f0cfa52008-04-29 00:59:41 -0700693static int smack_inode_setxattr(struct dentry *dentry, const char *name,
694 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800695{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200696 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800697 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800698
Casey Schauflerbcdca222008-02-23 15:24:04 -0800699 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
700 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800701 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
702 strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800703 if (!capable(CAP_MAC_ADMIN))
704 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200705 /*
706 * check label validity here so import wont fail on
707 * post_setxattr
708 */
709 if (size == 0 || size >= SMK_LABELLEN ||
710 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400711 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800712 } else
713 rc = cap_inode_setxattr(dentry, name, value, size, flags);
714
Etienne Bassetecfcc532009-04-08 20:40:06 +0200715 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
716 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
717
Casey Schauflerbcdca222008-02-23 15:24:04 -0800718 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200719 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800720
721 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800722}
723
724/**
725 * smack_inode_post_setxattr - Apply the Smack update approved above
726 * @dentry: object
727 * @name: attribute name
728 * @value: attribute value
729 * @size: attribute size
730 * @flags: unused
731 *
732 * Set the pointer in the inode blob to the entry found
733 * in the master label list.
734 */
David Howells8f0cfa52008-04-29 00:59:41 -0700735static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
736 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800737{
738 struct inode_smack *isp;
739 char *nsp;
740
741 /*
Casey Schaufler676dac42010-12-02 06:43:39 -0800742 * Not SMACK or SMACKEXEC
Casey Schauflere114e472008-02-04 22:29:50 -0800743 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800744 if (strcmp(name, XATTR_NAME_SMACK) &&
745 strcmp(name, XATTR_NAME_SMACKEXEC))
Casey Schauflere114e472008-02-04 22:29:50 -0800746 return;
747
Casey Schauflere114e472008-02-04 22:29:50 -0800748 isp = dentry->d_inode->i_security;
749
750 /*
751 * No locking is done here. This is a pointer
752 * assignment.
753 */
754 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800755
756 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
757 if (nsp != NULL)
758 isp->smk_inode = nsp;
759 else
760 isp->smk_inode = smack_known_invalid.smk_known;
761 } else {
762 if (nsp != NULL)
763 isp->smk_task = nsp;
764 else
765 isp->smk_task = smack_known_invalid.smk_known;
766 }
Casey Schauflere114e472008-02-04 22:29:50 -0800767
768 return;
769}
770
771/*
772 * smack_inode_getxattr - Smack check on getxattr
773 * @dentry: the object
774 * @name: unused
775 *
776 * Returns 0 if access is permitted, an error code otherwise
777 */
David Howells8f0cfa52008-04-29 00:59:41 -0700778static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800779{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200780 struct smk_audit_info ad;
781
782 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
783 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
784
785 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800786}
787
788/*
789 * smack_inode_removexattr - Smack check on removexattr
790 * @dentry: the object
791 * @name: name of the attribute
792 *
793 * Removing the Smack attribute requires CAP_MAC_ADMIN
794 *
795 * Returns 0 if access is permitted, an error code otherwise
796 */
David Howells8f0cfa52008-04-29 00:59:41 -0700797static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800798{
Casey Schaufler676dac42010-12-02 06:43:39 -0800799 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200800 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800801 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800802
Casey Schauflerbcdca222008-02-23 15:24:04 -0800803 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
804 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800805 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
806 strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800807 if (!capable(CAP_MAC_ADMIN))
808 rc = -EPERM;
809 } else
810 rc = cap_inode_removexattr(dentry, name);
811
Etienne Bassetecfcc532009-04-08 20:40:06 +0200812 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
813 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800814 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200815 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800816
Casey Schaufler676dac42010-12-02 06:43:39 -0800817 if (rc == 0) {
818 isp = dentry->d_inode->i_security;
819 isp->smk_task = NULL;
820 }
821
Casey Schauflerbcdca222008-02-23 15:24:04 -0800822 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800823}
824
825/**
826 * smack_inode_getsecurity - get smack xattrs
827 * @inode: the object
828 * @name: attribute name
829 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800830 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800831 *
832 * Returns the size of the attribute or an error code
833 */
834static int smack_inode_getsecurity(const struct inode *inode,
835 const char *name, void **buffer,
836 bool alloc)
837{
838 struct socket_smack *ssp;
839 struct socket *sock;
840 struct super_block *sbp;
841 struct inode *ip = (struct inode *)inode;
842 char *isp;
843 int ilen;
844 int rc = 0;
845
846 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
847 isp = smk_of_inode(inode);
848 ilen = strlen(isp) + 1;
849 *buffer = isp;
850 return ilen;
851 }
852
853 /*
854 * The rest of the Smack xattrs are only on sockets.
855 */
856 sbp = ip->i_sb;
857 if (sbp->s_magic != SOCKFS_MAGIC)
858 return -EOPNOTSUPP;
859
860 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800861 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800862 return -EOPNOTSUPP;
863
864 ssp = sock->sk->sk_security;
865
866 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
867 isp = ssp->smk_in;
868 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
869 isp = ssp->smk_out;
870 else
871 return -EOPNOTSUPP;
872
873 ilen = strlen(isp) + 1;
874 if (rc == 0) {
875 *buffer = isp;
876 rc = ilen;
877 }
878
879 return rc;
880}
881
882
883/**
884 * smack_inode_listsecurity - list the Smack attributes
885 * @inode: the object
886 * @buffer: where they go
887 * @buffer_size: size of buffer
888 *
889 * Returns 0 on success, -EINVAL otherwise
890 */
891static int smack_inode_listsecurity(struct inode *inode, char *buffer,
892 size_t buffer_size)
893{
894 int len = strlen(XATTR_NAME_SMACK);
895
896 if (buffer != NULL && len <= buffer_size) {
897 memcpy(buffer, XATTR_NAME_SMACK, len);
898 return len;
899 }
900 return -EINVAL;
901}
902
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +1000903/**
904 * smack_inode_getsecid - Extract inode's security id
905 * @inode: inode to extract the info from
906 * @secid: where result will be saved
907 */
908static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
909{
910 struct inode_smack *isp = inode->i_security;
911
912 *secid = smack_to_secid(isp->smk_inode);
913}
914
Casey Schauflere114e472008-02-04 22:29:50 -0800915/*
916 * File Hooks
917 */
918
919/**
920 * smack_file_permission - Smack check on file operations
921 * @file: unused
922 * @mask: unused
923 *
924 * Returns 0
925 *
926 * Should access checks be done on each read or write?
927 * UNICOS and SELinux say yes.
928 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
929 *
930 * I'll say no for now. Smack does not do the frequent
931 * label changing that SELinux does.
932 */
933static int smack_file_permission(struct file *file, int mask)
934{
935 return 0;
936}
937
938/**
939 * smack_file_alloc_security - assign a file security blob
940 * @file: the object
941 *
942 * The security blob for a file is a pointer to the master
943 * label list, so no allocation is done.
944 *
945 * Returns 0
946 */
947static int smack_file_alloc_security(struct file *file)
948{
Casey Schaufler676dac42010-12-02 06:43:39 -0800949 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800950 return 0;
951}
952
953/**
954 * smack_file_free_security - clear a file security blob
955 * @file: the object
956 *
957 * The security blob for a file is a pointer to the master
958 * label list, so no memory is freed.
959 */
960static void smack_file_free_security(struct file *file)
961{
962 file->f_security = NULL;
963}
964
965/**
966 * smack_file_ioctl - Smack check on ioctls
967 * @file: the object
968 * @cmd: what to do
969 * @arg: unused
970 *
971 * Relies heavily on the correct use of the ioctl command conventions.
972 *
973 * Returns 0 if allowed, error code otherwise
974 */
975static int smack_file_ioctl(struct file *file, unsigned int cmd,
976 unsigned long arg)
977{
978 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200979 struct smk_audit_info ad;
980
981 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
982 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -0800983
984 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200985 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800986
987 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200988 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800989
990 return rc;
991}
992
993/**
994 * smack_file_lock - Smack check on file locking
995 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -0800996 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800997 *
998 * Returns 0 if current has write access, error code otherwise
999 */
1000static int smack_file_lock(struct file *file, unsigned int cmd)
1001{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001002 struct smk_audit_info ad;
1003
1004 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1005 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
1006 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001007}
1008
1009/**
1010 * smack_file_fcntl - Smack check on fcntl
1011 * @file: the object
1012 * @cmd: what action to check
1013 * @arg: unused
1014 *
1015 * Returns 0 if current has access, error code otherwise
1016 */
1017static int smack_file_fcntl(struct file *file, unsigned int cmd,
1018 unsigned long arg)
1019{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001020 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001021 int rc;
1022
Etienne Bassetecfcc532009-04-08 20:40:06 +02001023 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1024 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1025
Casey Schauflere114e472008-02-04 22:29:50 -08001026 switch (cmd) {
1027 case F_DUPFD:
1028 case F_GETFD:
1029 case F_GETFL:
1030 case F_GETLK:
1031 case F_GETOWN:
1032 case F_GETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001033 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001034 break;
1035 case F_SETFD:
1036 case F_SETFL:
1037 case F_SETLK:
1038 case F_SETLKW:
1039 case F_SETOWN:
1040 case F_SETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001041 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001042 break;
1043 default:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001044 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001045 }
1046
1047 return rc;
1048}
1049
1050/**
1051 * smack_file_set_fowner - set the file security blob value
1052 * @file: object in question
1053 *
1054 * Returns 0
1055 * Further research may be required on this one.
1056 */
1057static int smack_file_set_fowner(struct file *file)
1058{
Casey Schaufler676dac42010-12-02 06:43:39 -08001059 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001060 return 0;
1061}
1062
1063/**
1064 * smack_file_send_sigiotask - Smack on sigio
1065 * @tsk: The target task
1066 * @fown: the object the signal come from
1067 * @signum: unused
1068 *
1069 * Allow a privileged task to get signals even if it shouldn't
1070 *
1071 * Returns 0 if a subject with the object's smack could
1072 * write to the task, an error code otherwise.
1073 */
1074static int smack_file_send_sigiotask(struct task_struct *tsk,
1075 struct fown_struct *fown, int signum)
1076{
1077 struct file *file;
1078 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001079 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001080 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001081
1082 /*
1083 * struct fown_struct is never outside the context of a struct file
1084 */
1085 file = container_of(fown, struct file, f_owner);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001086 /* we don't log here as rc can be overriden */
1087 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001088 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001089 rc = 0;
1090
1091 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1092 smk_ad_setfield_u_tsk(&ad, tsk);
1093 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001094 return rc;
1095}
1096
1097/**
1098 * smack_file_receive - Smack file receive check
1099 * @file: the object
1100 *
1101 * Returns 0 if current has access, error code otherwise
1102 */
1103static int smack_file_receive(struct file *file)
1104{
1105 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001107
Etienne Bassetecfcc532009-04-08 20:40:06 +02001108 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1109 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001110 /*
1111 * This code relies on bitmasks.
1112 */
1113 if (file->f_mode & FMODE_READ)
1114 may = MAY_READ;
1115 if (file->f_mode & FMODE_WRITE)
1116 may |= MAY_WRITE;
1117
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001119}
1120
1121/*
1122 * Task hooks
1123 */
1124
1125/**
David Howellsee18d642009-09-02 09:14:21 +01001126 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1127 * @new: the new credentials
1128 * @gfp: the atomicity of any memory allocations
1129 *
1130 * Prepare a blank set of credentials for modification. This must allocate all
1131 * the memory the LSM module might require such that cred_transfer() can
1132 * complete without error.
1133 */
1134static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1135{
Casey Schaufler676dac42010-12-02 06:43:39 -08001136 cred->security = kzalloc(sizeof(struct task_smack), gfp);
1137 if (cred->security == NULL)
1138 return -ENOMEM;
David Howellsee18d642009-09-02 09:14:21 +01001139 return 0;
1140}
1141
1142
1143/**
David Howellsf1752ee2008-11-14 10:39:17 +11001144 * smack_cred_free - "free" task-level security credentials
1145 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001146 *
1147 * Smack isn't using copies of blobs. Everyone
1148 * points to an immutable list. The blobs never go away.
1149 * There is no leak here.
1150 */
David Howellsf1752ee2008-11-14 10:39:17 +11001151static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001152{
Casey Schaufler676dac42010-12-02 06:43:39 -08001153 kfree(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001154}
1155
1156/**
David Howellsd84f4f92008-11-14 10:39:23 +11001157 * smack_cred_prepare - prepare new set of credentials for modification
1158 * @new: the new credentials
1159 * @old: the original credentials
1160 * @gfp: the atomicity of any memory allocations
1161 *
1162 * Prepare a new set of credentials for modification.
1163 */
1164static int smack_cred_prepare(struct cred *new, const struct cred *old,
1165 gfp_t gfp)
1166{
Casey Schaufler676dac42010-12-02 06:43:39 -08001167 struct task_smack *old_tsp = old->security;
1168 struct task_smack *new_tsp;
1169
1170 new_tsp = kzalloc(sizeof(struct task_smack), gfp);
1171 if (new_tsp == NULL)
1172 return -ENOMEM;
1173
1174 new_tsp->smk_task = old_tsp->smk_task;
1175 new_tsp->smk_forked = old_tsp->smk_task;
1176 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001177 return 0;
1178}
1179
Randy Dunlap251a2a92009-02-18 11:42:33 -08001180/**
David Howellsee18d642009-09-02 09:14:21 +01001181 * smack_cred_transfer - Transfer the old credentials to the new credentials
1182 * @new: the new credentials
1183 * @old: the original credentials
1184 *
1185 * Fill in a set of blank credentials from another set of credentials.
1186 */
1187static void smack_cred_transfer(struct cred *new, const struct cred *old)
1188{
Casey Schaufler676dac42010-12-02 06:43:39 -08001189 struct task_smack *old_tsp = old->security;
1190 struct task_smack *new_tsp = new->security;
1191
1192 new_tsp->smk_task = old_tsp->smk_task;
1193 new_tsp->smk_forked = old_tsp->smk_task;
David Howellsee18d642009-09-02 09:14:21 +01001194}
1195
1196/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001197 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001198 * @new: points to the set of credentials to be modified.
1199 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001200 *
1201 * Set the security data for a kernel service.
1202 */
1203static int smack_kernel_act_as(struct cred *new, u32 secid)
1204{
Casey Schaufler676dac42010-12-02 06:43:39 -08001205 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001206 char *smack = smack_from_secid(secid);
1207
1208 if (smack == NULL)
1209 return -EINVAL;
1210
Casey Schaufler676dac42010-12-02 06:43:39 -08001211 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001212 return 0;
1213}
1214
1215/**
1216 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001217 * @new: points to the set of credentials to be modified
1218 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001219 *
1220 * Set the file creation context in a set of credentials to the same
1221 * as the objective context of the specified inode
1222 */
1223static int smack_kernel_create_files_as(struct cred *new,
1224 struct inode *inode)
1225{
1226 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001227 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001228
Casey Schaufler676dac42010-12-02 06:43:39 -08001229 tsp->smk_forked = isp->smk_inode;
1230 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001231 return 0;
1232}
1233
1234/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001235 * smk_curacc_on_task - helper to log task related access
1236 * @p: the task object
1237 * @access : the access requested
1238 *
1239 * Return 0 if access is permitted
1240 */
1241static int smk_curacc_on_task(struct task_struct *p, int access)
1242{
1243 struct smk_audit_info ad;
1244
1245 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1246 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001247 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001248}
1249
1250/**
Casey Schauflere114e472008-02-04 22:29:50 -08001251 * smack_task_setpgid - Smack check on setting pgid
1252 * @p: the task object
1253 * @pgid: unused
1254 *
1255 * Return 0 if write access is permitted
1256 */
1257static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1258{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001259 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001260}
1261
1262/**
1263 * smack_task_getpgid - Smack access check for getpgid
1264 * @p: the object task
1265 *
1266 * Returns 0 if current can read the object task, error code otherwise
1267 */
1268static int smack_task_getpgid(struct task_struct *p)
1269{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001270 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001271}
1272
1273/**
1274 * smack_task_getsid - Smack access check for getsid
1275 * @p: the object task
1276 *
1277 * Returns 0 if current can read the object task, error code otherwise
1278 */
1279static int smack_task_getsid(struct task_struct *p)
1280{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001281 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001282}
1283
1284/**
1285 * smack_task_getsecid - get the secid of the task
1286 * @p: the object task
1287 * @secid: where to put the result
1288 *
1289 * Sets the secid to contain a u32 version of the smack label.
1290 */
1291static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1292{
Casey Schaufler676dac42010-12-02 06:43:39 -08001293 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001294}
1295
1296/**
1297 * smack_task_setnice - Smack check on setting nice
1298 * @p: the task object
1299 * @nice: unused
1300 *
1301 * Return 0 if write access is permitted
1302 */
1303static int smack_task_setnice(struct task_struct *p, int nice)
1304{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001305 int rc;
1306
1307 rc = cap_task_setnice(p, nice);
1308 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001309 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001310 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001311}
1312
1313/**
1314 * smack_task_setioprio - Smack check on setting ioprio
1315 * @p: the task object
1316 * @ioprio: unused
1317 *
1318 * Return 0 if write access is permitted
1319 */
1320static int smack_task_setioprio(struct task_struct *p, int ioprio)
1321{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001322 int rc;
1323
1324 rc = cap_task_setioprio(p, ioprio);
1325 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001326 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001327 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001328}
1329
1330/**
1331 * smack_task_getioprio - Smack check on reading ioprio
1332 * @p: the task object
1333 *
1334 * Return 0 if read access is permitted
1335 */
1336static int smack_task_getioprio(struct task_struct *p)
1337{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001338 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001339}
1340
1341/**
1342 * smack_task_setscheduler - Smack check on setting scheduler
1343 * @p: the task object
1344 * @policy: unused
1345 * @lp: unused
1346 *
1347 * Return 0 if read access is permitted
1348 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001349static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001350{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001351 int rc;
1352
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001353 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001354 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001355 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001356 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001357}
1358
1359/**
1360 * smack_task_getscheduler - Smack check on reading scheduler
1361 * @p: the task object
1362 *
1363 * Return 0 if read access is permitted
1364 */
1365static int smack_task_getscheduler(struct task_struct *p)
1366{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001367 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001368}
1369
1370/**
1371 * smack_task_movememory - Smack check on moving memory
1372 * @p: the task object
1373 *
1374 * Return 0 if write access is permitted
1375 */
1376static int smack_task_movememory(struct task_struct *p)
1377{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001378 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001379}
1380
1381/**
1382 * smack_task_kill - Smack check on signal delivery
1383 * @p: the task object
1384 * @info: unused
1385 * @sig: unused
1386 * @secid: identifies the smack to use in lieu of current's
1387 *
1388 * Return 0 if write access is permitted
1389 *
1390 * The secid behavior is an artifact of an SELinux hack
1391 * in the USB code. Someday it may go away.
1392 */
1393static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1394 int sig, u32 secid)
1395{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001396 struct smk_audit_info ad;
1397
1398 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1399 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001400 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001401 * Sending a signal requires that the sender
1402 * can write the receiver.
1403 */
1404 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001405 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1406 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001407 /*
1408 * If the secid isn't 0 we're dealing with some USB IO
1409 * specific behavior. This is not clean. For one thing
1410 * we can't take privilege into account.
1411 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001412 return smk_access(smack_from_secid(secid),
1413 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001414}
1415
1416/**
1417 * smack_task_wait - Smack access check for waiting
1418 * @p: task to wait for
1419 *
1420 * Returns 0 if current can wait for p, error code otherwise
1421 */
1422static int smack_task_wait(struct task_struct *p)
1423{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001424 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08001425 char *sp = smk_of_current();
1426 char *tsp = smk_of_forked(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001427 int rc;
1428
Etienne Bassetecfcc532009-04-08 20:40:06 +02001429 /* we don't log here, we can be overriden */
Casey Schaufler676dac42010-12-02 06:43:39 -08001430 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001431 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001432 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001433
1434 /*
1435 * Allow the operation to succeed if either task
1436 * has privilege to perform operations that might
1437 * account for the smack labels having gotten to
1438 * be different in the first place.
1439 *
David Howells5cd9c582008-08-14 11:37:28 +01001440 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001441 * control ideal, taking the object's privilege
1442 * state into account in the decision as well as
1443 * the smack value.
1444 */
David Howells5cd9c582008-08-14 11:37:28 +01001445 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001446 rc = 0;
1447 /* we log only if we didn't get overriden */
1448 out_log:
1449 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1450 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001451 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001452 return rc;
1453}
1454
1455/**
1456 * smack_task_to_inode - copy task smack into the inode blob
1457 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001458 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001459 *
1460 * Sets the smack pointer in the inode security blob
1461 */
1462static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1463{
1464 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001465 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001466}
1467
1468/*
1469 * Socket hooks.
1470 */
1471
1472/**
1473 * smack_sk_alloc_security - Allocate a socket blob
1474 * @sk: the socket
1475 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001476 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001477 *
1478 * Assign Smack pointers to current
1479 *
1480 * Returns 0 on success, -ENOMEM is there's no memory
1481 */
1482static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1483{
Casey Schaufler676dac42010-12-02 06:43:39 -08001484 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001485 struct socket_smack *ssp;
1486
1487 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1488 if (ssp == NULL)
1489 return -ENOMEM;
1490
1491 ssp->smk_in = csp;
1492 ssp->smk_out = csp;
1493 ssp->smk_packet[0] = '\0';
1494
1495 sk->sk_security = ssp;
1496
1497 return 0;
1498}
1499
1500/**
1501 * smack_sk_free_security - Free a socket blob
1502 * @sk: the socket
1503 *
1504 * Clears the blob pointer
1505 */
1506static void smack_sk_free_security(struct sock *sk)
1507{
1508 kfree(sk->sk_security);
1509}
1510
1511/**
Paul Moore07feee82009-03-27 17:10:54 -04001512* smack_host_label - check host based restrictions
1513* @sip: the object end
1514*
1515* looks for host based access restrictions
1516*
1517* This version will only be appropriate for really small sets of single label
1518* hosts. The caller is responsible for ensuring that the RCU read lock is
1519* taken before calling this function.
1520*
1521* Returns the label of the far end or NULL if it's not special.
1522*/
1523static char *smack_host_label(struct sockaddr_in *sip)
1524{
1525 struct smk_netlbladdr *snp;
1526 struct in_addr *siap = &sip->sin_addr;
1527
1528 if (siap->s_addr == 0)
1529 return NULL;
1530
1531 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1532 /*
1533 * we break after finding the first match because
1534 * the list is sorted from longest to shortest mask
1535 * so we have found the most specific match
1536 */
1537 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001538 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1539 /* we have found the special CIPSO option */
1540 if (snp->smk_label == smack_cipso_option)
1541 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001542 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001543 }
Paul Moore07feee82009-03-27 17:10:54 -04001544
1545 return NULL;
1546}
1547
1548/**
Casey Schauflere114e472008-02-04 22:29:50 -08001549 * smack_set_catset - convert a capset to netlabel mls categories
1550 * @catset: the Smack categories
1551 * @sap: where to put the netlabel categories
1552 *
1553 * Allocates and fills attr.mls.cat
1554 */
1555static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1556{
1557 unsigned char *cp;
1558 unsigned char m;
1559 int cat;
1560 int rc;
1561 int byte;
1562
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001563 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001564 return;
1565
1566 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1567 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1568 sap->attr.mls.cat->startbit = 0;
1569
1570 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1571 for (m = 0x80; m != 0; m >>= 1, cat++) {
1572 if ((m & *cp) == 0)
1573 continue;
1574 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1575 cat, GFP_ATOMIC);
1576 }
1577}
1578
1579/**
1580 * smack_to_secattr - fill a secattr from a smack value
1581 * @smack: the smack value
1582 * @nlsp: where the result goes
1583 *
1584 * Casey says that CIPSO is good enough for now.
1585 * It can be used to effect.
1586 * It can also be abused to effect when necessary.
1587 * Appologies to the TSIG group in general and GW in particular.
1588 */
1589static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1590{
1591 struct smack_cipso cipso;
1592 int rc;
1593
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001594 nlsp->domain = smack;
1595 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001596
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001597 rc = smack_to_cipso(smack, &cipso);
1598 if (rc == 0) {
1599 nlsp->attr.mls.lvl = cipso.smk_level;
1600 smack_set_catset(cipso.smk_catset, nlsp);
1601 } else {
1602 nlsp->attr.mls.lvl = smack_cipso_direct;
1603 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001604 }
1605}
1606
1607/**
1608 * smack_netlabel - Set the secattr on a socket
1609 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001610 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001611 *
1612 * Convert the outbound smack value (smk_out) to a
1613 * secattr and attach it to the socket.
1614 *
1615 * Returns 0 on success or an error code
1616 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001617static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001618{
Paul Moore07feee82009-03-27 17:10:54 -04001619 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001620 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001621 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001622
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001623 /*
1624 * Usually the netlabel code will handle changing the
1625 * packet labeling based on the label.
1626 * The case of a single label host is different, because
1627 * a single label host should never get a labeled packet
1628 * even though the label is usually associated with a packet
1629 * label.
1630 */
1631 local_bh_disable();
1632 bh_lock_sock_nested(sk);
1633
1634 if (ssp->smk_out == smack_net_ambient ||
1635 labeled == SMACK_UNLABELED_SOCKET)
1636 netlbl_sock_delattr(sk);
1637 else {
1638 netlbl_secattr_init(&secattr);
1639 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001640 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001641 netlbl_secattr_destroy(&secattr);
1642 }
1643
1644 bh_unlock_sock(sk);
1645 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001646
Casey Schauflere114e472008-02-04 22:29:50 -08001647 return rc;
1648}
1649
1650/**
Paul Moore07feee82009-03-27 17:10:54 -04001651 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1652 * @sk: the socket
1653 * @sap: the destination address
1654 *
1655 * Set the correct secattr for the given socket based on the destination
1656 * address and perform any outbound access checks needed.
1657 *
1658 * Returns 0 on success or an error code.
1659 *
1660 */
1661static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1662{
1663 int rc;
1664 int sk_lbl;
1665 char *hostsp;
1666 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001667 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001668
1669 rcu_read_lock();
1670 hostsp = smack_host_label(sap);
1671 if (hostsp != NULL) {
1672 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001673#ifdef CONFIG_AUDIT
1674 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1675 ad.a.u.net.family = sap->sin_family;
1676 ad.a.u.net.dport = sap->sin_port;
1677 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1678#endif
1679 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001680 } else {
1681 sk_lbl = SMACK_CIPSO_SOCKET;
1682 rc = 0;
1683 }
1684 rcu_read_unlock();
1685 if (rc != 0)
1686 return rc;
1687
1688 return smack_netlabel(sk, sk_lbl);
1689}
1690
1691/**
Casey Schauflere114e472008-02-04 22:29:50 -08001692 * smack_inode_setsecurity - set smack xattrs
1693 * @inode: the object
1694 * @name: attribute name
1695 * @value: attribute value
1696 * @size: size of the attribute
1697 * @flags: unused
1698 *
1699 * Sets the named attribute in the appropriate blob
1700 *
1701 * Returns 0 on success, or an error code
1702 */
1703static int smack_inode_setsecurity(struct inode *inode, const char *name,
1704 const void *value, size_t size, int flags)
1705{
1706 char *sp;
1707 struct inode_smack *nsp = inode->i_security;
1708 struct socket_smack *ssp;
1709 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001710 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001711
Etienne Basset43031542009-03-27 17:11:01 -04001712 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001713 return -EACCES;
1714
1715 sp = smk_import(value, size);
1716 if (sp == NULL)
1717 return -EINVAL;
1718
1719 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1720 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04001721 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08001722 return 0;
1723 }
1724 /*
1725 * The rest of the Smack xattrs are only on sockets.
1726 */
1727 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1728 return -EOPNOTSUPP;
1729
1730 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001731 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001732 return -EOPNOTSUPP;
1733
1734 ssp = sock->sk->sk_security;
1735
1736 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1737 ssp->smk_in = sp;
1738 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1739 ssp->smk_out = sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08001740 if (sock->sk->sk_family != PF_UNIX) {
1741 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1742 if (rc != 0)
1743 printk(KERN_WARNING
1744 "Smack: \"%s\" netlbl error %d.\n",
1745 __func__, -rc);
1746 }
Casey Schauflere114e472008-02-04 22:29:50 -08001747 } else
1748 return -EOPNOTSUPP;
1749
1750 return 0;
1751}
1752
1753/**
1754 * smack_socket_post_create - finish socket setup
1755 * @sock: the socket
1756 * @family: protocol family
1757 * @type: unused
1758 * @protocol: unused
1759 * @kern: unused
1760 *
1761 * Sets the netlabel information on the socket
1762 *
1763 * Returns 0 on success, and error code otherwise
1764 */
1765static int smack_socket_post_create(struct socket *sock, int family,
1766 int type, int protocol, int kern)
1767{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001768 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001769 return 0;
1770 /*
1771 * Set the outbound netlbl.
1772 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001773 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1774}
1775
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001776/**
1777 * smack_socket_connect - connect access check
1778 * @sock: the socket
1779 * @sap: the other end
1780 * @addrlen: size of sap
1781 *
1782 * Verifies that a connection may be possible
1783 *
1784 * Returns 0 on success, and error code otherwise
1785 */
1786static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1787 int addrlen)
1788{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001789 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1790 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001791 if (addrlen < sizeof(struct sockaddr_in))
1792 return -EINVAL;
1793
Paul Moore07feee82009-03-27 17:10:54 -04001794 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08001795}
1796
1797/**
1798 * smack_flags_to_may - convert S_ to MAY_ values
1799 * @flags: the S_ value
1800 *
1801 * Returns the equivalent MAY_ value
1802 */
1803static int smack_flags_to_may(int flags)
1804{
1805 int may = 0;
1806
1807 if (flags & S_IRUGO)
1808 may |= MAY_READ;
1809 if (flags & S_IWUGO)
1810 may |= MAY_WRITE;
1811 if (flags & S_IXUGO)
1812 may |= MAY_EXEC;
1813
1814 return may;
1815}
1816
1817/**
1818 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1819 * @msg: the object
1820 *
1821 * Returns 0
1822 */
1823static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1824{
Casey Schaufler676dac42010-12-02 06:43:39 -08001825 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001826 return 0;
1827}
1828
1829/**
1830 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1831 * @msg: the object
1832 *
1833 * Clears the blob pointer
1834 */
1835static void smack_msg_msg_free_security(struct msg_msg *msg)
1836{
1837 msg->security = NULL;
1838}
1839
1840/**
1841 * smack_of_shm - the smack pointer for the shm
1842 * @shp: the object
1843 *
1844 * Returns a pointer to the smack value
1845 */
1846static char *smack_of_shm(struct shmid_kernel *shp)
1847{
1848 return (char *)shp->shm_perm.security;
1849}
1850
1851/**
1852 * smack_shm_alloc_security - Set the security blob for shm
1853 * @shp: the object
1854 *
1855 * Returns 0
1856 */
1857static int smack_shm_alloc_security(struct shmid_kernel *shp)
1858{
1859 struct kern_ipc_perm *isp = &shp->shm_perm;
1860
Casey Schaufler676dac42010-12-02 06:43:39 -08001861 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001862 return 0;
1863}
1864
1865/**
1866 * smack_shm_free_security - Clear the security blob for shm
1867 * @shp: the object
1868 *
1869 * Clears the blob pointer
1870 */
1871static void smack_shm_free_security(struct shmid_kernel *shp)
1872{
1873 struct kern_ipc_perm *isp = &shp->shm_perm;
1874
1875 isp->security = NULL;
1876}
1877
1878/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001879 * smk_curacc_shm : check if current has access on shm
1880 * @shp : the object
1881 * @access : access requested
1882 *
1883 * Returns 0 if current has the requested access, error code otherwise
1884 */
1885static int smk_curacc_shm(struct shmid_kernel *shp, int access)
1886{
1887 char *ssp = smack_of_shm(shp);
1888 struct smk_audit_info ad;
1889
1890#ifdef CONFIG_AUDIT
1891 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1892 ad.a.u.ipc_id = shp->shm_perm.id;
1893#endif
1894 return smk_curacc(ssp, access, &ad);
1895}
1896
1897/**
Casey Schauflere114e472008-02-04 22:29:50 -08001898 * smack_shm_associate - Smack access check for shm
1899 * @shp: the object
1900 * @shmflg: access requested
1901 *
1902 * Returns 0 if current has the requested access, error code otherwise
1903 */
1904static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1905{
Casey Schauflere114e472008-02-04 22:29:50 -08001906 int may;
1907
1908 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001909 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001910}
1911
1912/**
1913 * smack_shm_shmctl - Smack access check for shm
1914 * @shp: the object
1915 * @cmd: what it wants to do
1916 *
1917 * Returns 0 if current has the requested access, error code otherwise
1918 */
1919static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1920{
Casey Schauflere114e472008-02-04 22:29:50 -08001921 int may;
1922
1923 switch (cmd) {
1924 case IPC_STAT:
1925 case SHM_STAT:
1926 may = MAY_READ;
1927 break;
1928 case IPC_SET:
1929 case SHM_LOCK:
1930 case SHM_UNLOCK:
1931 case IPC_RMID:
1932 may = MAY_READWRITE;
1933 break;
1934 case IPC_INFO:
1935 case SHM_INFO:
1936 /*
1937 * System level information.
1938 */
1939 return 0;
1940 default:
1941 return -EINVAL;
1942 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02001943 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001944}
1945
1946/**
1947 * smack_shm_shmat - Smack access for shmat
1948 * @shp: the object
1949 * @shmaddr: unused
1950 * @shmflg: access requested
1951 *
1952 * Returns 0 if current has the requested access, error code otherwise
1953 */
1954static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1955 int shmflg)
1956{
Casey Schauflere114e472008-02-04 22:29:50 -08001957 int may;
1958
1959 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001960 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001961}
1962
1963/**
1964 * smack_of_sem - the smack pointer for the sem
1965 * @sma: the object
1966 *
1967 * Returns a pointer to the smack value
1968 */
1969static char *smack_of_sem(struct sem_array *sma)
1970{
1971 return (char *)sma->sem_perm.security;
1972}
1973
1974/**
1975 * smack_sem_alloc_security - Set the security blob for sem
1976 * @sma: the object
1977 *
1978 * Returns 0
1979 */
1980static int smack_sem_alloc_security(struct sem_array *sma)
1981{
1982 struct kern_ipc_perm *isp = &sma->sem_perm;
1983
Casey Schaufler676dac42010-12-02 06:43:39 -08001984 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001985 return 0;
1986}
1987
1988/**
1989 * smack_sem_free_security - Clear the security blob for sem
1990 * @sma: the object
1991 *
1992 * Clears the blob pointer
1993 */
1994static void smack_sem_free_security(struct sem_array *sma)
1995{
1996 struct kern_ipc_perm *isp = &sma->sem_perm;
1997
1998 isp->security = NULL;
1999}
2000
2001/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002002 * smk_curacc_sem : check if current has access on sem
2003 * @sma : the object
2004 * @access : access requested
2005 *
2006 * Returns 0 if current has the requested access, error code otherwise
2007 */
2008static int smk_curacc_sem(struct sem_array *sma, int access)
2009{
2010 char *ssp = smack_of_sem(sma);
2011 struct smk_audit_info ad;
2012
2013#ifdef CONFIG_AUDIT
2014 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2015 ad.a.u.ipc_id = sma->sem_perm.id;
2016#endif
2017 return smk_curacc(ssp, access, &ad);
2018}
2019
2020/**
Casey Schauflere114e472008-02-04 22:29:50 -08002021 * smack_sem_associate - Smack access check for sem
2022 * @sma: the object
2023 * @semflg: access requested
2024 *
2025 * Returns 0 if current has the requested access, error code otherwise
2026 */
2027static int smack_sem_associate(struct sem_array *sma, int semflg)
2028{
Casey Schauflere114e472008-02-04 22:29:50 -08002029 int may;
2030
2031 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002032 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002033}
2034
2035/**
2036 * smack_sem_shmctl - Smack access check for sem
2037 * @sma: the object
2038 * @cmd: what it wants to do
2039 *
2040 * Returns 0 if current has the requested access, error code otherwise
2041 */
2042static int smack_sem_semctl(struct sem_array *sma, int cmd)
2043{
Casey Schauflere114e472008-02-04 22:29:50 -08002044 int may;
2045
2046 switch (cmd) {
2047 case GETPID:
2048 case GETNCNT:
2049 case GETZCNT:
2050 case GETVAL:
2051 case GETALL:
2052 case IPC_STAT:
2053 case SEM_STAT:
2054 may = MAY_READ;
2055 break;
2056 case SETVAL:
2057 case SETALL:
2058 case IPC_RMID:
2059 case IPC_SET:
2060 may = MAY_READWRITE;
2061 break;
2062 case IPC_INFO:
2063 case SEM_INFO:
2064 /*
2065 * System level information
2066 */
2067 return 0;
2068 default:
2069 return -EINVAL;
2070 }
2071
Etienne Bassetecfcc532009-04-08 20:40:06 +02002072 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002073}
2074
2075/**
2076 * smack_sem_semop - Smack checks of semaphore operations
2077 * @sma: the object
2078 * @sops: unused
2079 * @nsops: unused
2080 * @alter: unused
2081 *
2082 * Treated as read and write in all cases.
2083 *
2084 * Returns 0 if access is allowed, error code otherwise
2085 */
2086static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2087 unsigned nsops, int alter)
2088{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002089 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002090}
2091
2092/**
2093 * smack_msg_alloc_security - Set the security blob for msg
2094 * @msq: the object
2095 *
2096 * Returns 0
2097 */
2098static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2099{
2100 struct kern_ipc_perm *kisp = &msq->q_perm;
2101
Casey Schaufler676dac42010-12-02 06:43:39 -08002102 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002103 return 0;
2104}
2105
2106/**
2107 * smack_msg_free_security - Clear the security blob for msg
2108 * @msq: the object
2109 *
2110 * Clears the blob pointer
2111 */
2112static void smack_msg_queue_free_security(struct msg_queue *msq)
2113{
2114 struct kern_ipc_perm *kisp = &msq->q_perm;
2115
2116 kisp->security = NULL;
2117}
2118
2119/**
2120 * smack_of_msq - the smack pointer for the msq
2121 * @msq: the object
2122 *
2123 * Returns a pointer to the smack value
2124 */
2125static char *smack_of_msq(struct msg_queue *msq)
2126{
2127 return (char *)msq->q_perm.security;
2128}
2129
2130/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002131 * smk_curacc_msq : helper to check if current has access on msq
2132 * @msq : the msq
2133 * @access : access requested
2134 *
2135 * return 0 if current has access, error otherwise
2136 */
2137static int smk_curacc_msq(struct msg_queue *msq, int access)
2138{
2139 char *msp = smack_of_msq(msq);
2140 struct smk_audit_info ad;
2141
2142#ifdef CONFIG_AUDIT
2143 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2144 ad.a.u.ipc_id = msq->q_perm.id;
2145#endif
2146 return smk_curacc(msp, access, &ad);
2147}
2148
2149/**
Casey Schauflere114e472008-02-04 22:29:50 -08002150 * smack_msg_queue_associate - Smack access check for msg_queue
2151 * @msq: the object
2152 * @msqflg: access requested
2153 *
2154 * Returns 0 if current has the requested access, error code otherwise
2155 */
2156static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2157{
Casey Schauflere114e472008-02-04 22:29:50 -08002158 int may;
2159
2160 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002161 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002162}
2163
2164/**
2165 * smack_msg_queue_msgctl - Smack access check for msg_queue
2166 * @msq: the object
2167 * @cmd: what it wants to do
2168 *
2169 * Returns 0 if current has the requested access, error code otherwise
2170 */
2171static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2172{
Casey Schauflere114e472008-02-04 22:29:50 -08002173 int may;
2174
2175 switch (cmd) {
2176 case IPC_STAT:
2177 case MSG_STAT:
2178 may = MAY_READ;
2179 break;
2180 case IPC_SET:
2181 case IPC_RMID:
2182 may = MAY_READWRITE;
2183 break;
2184 case IPC_INFO:
2185 case MSG_INFO:
2186 /*
2187 * System level information
2188 */
2189 return 0;
2190 default:
2191 return -EINVAL;
2192 }
2193
Etienne Bassetecfcc532009-04-08 20:40:06 +02002194 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002195}
2196
2197/**
2198 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2199 * @msq: the object
2200 * @msg: unused
2201 * @msqflg: access requested
2202 *
2203 * Returns 0 if current has the requested access, error code otherwise
2204 */
2205static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2206 int msqflg)
2207{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002208 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002209
Etienne Bassetecfcc532009-04-08 20:40:06 +02002210 may = smack_flags_to_may(msqflg);
2211 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002212}
2213
2214/**
2215 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2216 * @msq: the object
2217 * @msg: unused
2218 * @target: unused
2219 * @type: unused
2220 * @mode: unused
2221 *
2222 * Returns 0 if current has read and write access, error code otherwise
2223 */
2224static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2225 struct task_struct *target, long type, int mode)
2226{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002227 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002228}
2229
2230/**
2231 * smack_ipc_permission - Smack access for ipc_permission()
2232 * @ipp: the object permissions
2233 * @flag: access requested
2234 *
2235 * Returns 0 if current has read and write access, error code otherwise
2236 */
2237static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2238{
2239 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002240 int may = smack_flags_to_may(flag);
2241 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002242
Etienne Bassetecfcc532009-04-08 20:40:06 +02002243#ifdef CONFIG_AUDIT
2244 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2245 ad.a.u.ipc_id = ipp->id;
2246#endif
2247 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002248}
2249
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002250/**
2251 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002252 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002253 * @secid: where result will be saved
2254 */
2255static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2256{
2257 char *smack = ipp->security;
2258
2259 *secid = smack_to_secid(smack);
2260}
2261
Casey Schauflere114e472008-02-04 22:29:50 -08002262/**
2263 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002264 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002265 * @inode: the object
2266 *
2267 * Set the inode's security blob if it hasn't been done already.
2268 */
2269static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2270{
2271 struct super_block *sbp;
2272 struct superblock_smack *sbsp;
2273 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002274 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002275 char *fetched;
2276 char *final;
2277 struct dentry *dp;
2278
2279 if (inode == NULL)
2280 return;
2281
2282 isp = inode->i_security;
2283
2284 mutex_lock(&isp->smk_lock);
2285 /*
2286 * If the inode is already instantiated
2287 * take the quick way out
2288 */
2289 if (isp->smk_flags & SMK_INODE_INSTANT)
2290 goto unlockandout;
2291
2292 sbp = inode->i_sb;
2293 sbsp = sbp->s_security;
2294 /*
2295 * We're going to use the superblock default label
2296 * if there's no label on the file.
2297 */
2298 final = sbsp->smk_default;
2299
2300 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002301 * If this is the root inode the superblock
2302 * may be in the process of initialization.
2303 * If that is the case use the root value out
2304 * of the superblock.
2305 */
2306 if (opt_dentry->d_parent == opt_dentry) {
2307 isp->smk_inode = sbsp->smk_root;
2308 isp->smk_flags |= SMK_INODE_INSTANT;
2309 goto unlockandout;
2310 }
2311
2312 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002313 * This is pretty hackish.
2314 * Casey says that we shouldn't have to do
2315 * file system specific code, but it does help
2316 * with keeping it simple.
2317 */
2318 switch (sbp->s_magic) {
2319 case SMACK_MAGIC:
2320 /*
2321 * Casey says that it's a little embarassing
2322 * that the smack file system doesn't do
2323 * extended attributes.
2324 */
2325 final = smack_known_star.smk_known;
2326 break;
2327 case PIPEFS_MAGIC:
2328 /*
2329 * Casey says pipes are easy (?)
2330 */
2331 final = smack_known_star.smk_known;
2332 break;
2333 case DEVPTS_SUPER_MAGIC:
2334 /*
2335 * devpts seems content with the label of the task.
2336 * Programs that change smack have to treat the
2337 * pty with respect.
2338 */
2339 final = csp;
2340 break;
2341 case SOCKFS_MAGIC:
2342 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002343 * Socket access is controlled by the socket
2344 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002345 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002346 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002347 break;
2348 case PROC_SUPER_MAGIC:
2349 /*
2350 * Casey says procfs appears not to care.
2351 * The superblock default suffices.
2352 */
2353 break;
2354 case TMPFS_MAGIC:
2355 /*
2356 * Device labels should come from the filesystem,
2357 * but watch out, because they're volitile,
2358 * getting recreated on every reboot.
2359 */
2360 final = smack_known_star.smk_known;
2361 /*
2362 * No break.
2363 *
2364 * If a smack value has been set we want to use it,
2365 * but since tmpfs isn't giving us the opportunity
2366 * to set mount options simulate setting the
2367 * superblock default.
2368 */
2369 default:
2370 /*
2371 * This isn't an understood special case.
2372 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002373 */
2374
2375 /*
2376 * UNIX domain sockets use lower level socket data.
2377 */
2378 if (S_ISSOCK(inode->i_mode)) {
2379 final = smack_known_star.smk_known;
2380 break;
2381 }
2382 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002383 * No xattr support means, alas, no SMACK label.
2384 * Use the aforeapplied default.
2385 * It would be curious if the label of the task
2386 * does not match that assigned.
2387 */
2388 if (inode->i_op->getxattr == NULL)
2389 break;
2390 /*
2391 * Get the dentry for xattr.
2392 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002393 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002394 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Casey Schauflere114e472008-02-04 22:29:50 -08002395 if (fetched != NULL)
2396 final = fetched;
Casey Schaufler676dac42010-12-02 06:43:39 -08002397 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode,
2398 dp);
2399
Casey Schauflere114e472008-02-04 22:29:50 -08002400 dput(dp);
2401 break;
2402 }
2403
2404 if (final == NULL)
2405 isp->smk_inode = csp;
2406 else
2407 isp->smk_inode = final;
2408
2409 isp->smk_flags |= SMK_INODE_INSTANT;
2410
2411unlockandout:
2412 mutex_unlock(&isp->smk_lock);
2413 return;
2414}
2415
2416/**
2417 * smack_getprocattr - Smack process attribute access
2418 * @p: the object task
2419 * @name: the name of the attribute in /proc/.../attr
2420 * @value: where to put the result
2421 *
2422 * Places a copy of the task Smack into value
2423 *
2424 * Returns the length of the smack label or an error code
2425 */
2426static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2427{
2428 char *cp;
2429 int slen;
2430
2431 if (strcmp(name, "current") != 0)
2432 return -EINVAL;
2433
Casey Schaufler676dac42010-12-02 06:43:39 -08002434 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002435 if (cp == NULL)
2436 return -ENOMEM;
2437
2438 slen = strlen(cp);
2439 *value = cp;
2440 return slen;
2441}
2442
2443/**
2444 * smack_setprocattr - Smack process attribute setting
2445 * @p: the object task
2446 * @name: the name of the attribute in /proc/.../attr
2447 * @value: the value to set
2448 * @size: the size of the value
2449 *
2450 * Sets the Smack value of the task. Only setting self
2451 * is permitted and only with privilege
2452 *
2453 * Returns the length of the smack label or an error code
2454 */
2455static int smack_setprocattr(struct task_struct *p, char *name,
2456 void *value, size_t size)
2457{
Casey Schaufler676dac42010-12-02 06:43:39 -08002458 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002459 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002460 char *newsmack;
2461
Casey Schauflere114e472008-02-04 22:29:50 -08002462 /*
2463 * Changing another process' Smack value is too dangerous
2464 * and supports no sane use case.
2465 */
2466 if (p != current)
2467 return -EPERM;
2468
David Howells5cd9c582008-08-14 11:37:28 +01002469 if (!capable(CAP_MAC_ADMIN))
2470 return -EPERM;
2471
Casey Schauflere114e472008-02-04 22:29:50 -08002472 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2473 return -EINVAL;
2474
2475 if (strcmp(name, "current") != 0)
2476 return -EINVAL;
2477
2478 newsmack = smk_import(value, size);
2479 if (newsmack == NULL)
2480 return -EINVAL;
2481
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002482 /*
2483 * No process is ever allowed the web ("@") label.
2484 */
2485 if (newsmack == smack_known_web.smk_known)
2486 return -EPERM;
2487
David Howellsd84f4f92008-11-14 10:39:23 +11002488 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002489 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002490 return -ENOMEM;
Casey Schaufler676dac42010-12-02 06:43:39 -08002491 tsp = kzalloc(sizeof(struct task_smack), GFP_KERNEL);
2492 if (tsp == NULL) {
2493 kfree(new);
2494 return -ENOMEM;
2495 }
2496 tsp->smk_task = newsmack;
2497 new->security = tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002498 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002499 return size;
2500}
2501
2502/**
2503 * smack_unix_stream_connect - Smack access on UDS
2504 * @sock: one socket
2505 * @other: the other socket
2506 * @newsk: unused
2507 *
2508 * Return 0 if a subject with the smack of sock could access
2509 * an object with the smack of other, otherwise an error code
2510 */
2511static int smack_unix_stream_connect(struct socket *sock,
2512 struct socket *other, struct sock *newsk)
2513{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002514 struct socket_smack *ssp = sock->sk->sk_security;
2515 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002516 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002517 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002518
Etienne Bassetecfcc532009-04-08 20:40:06 +02002519 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2520 smk_ad_setfield_u_net_sk(&ad, other->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002521
2522 if (!capable(CAP_MAC_OVERRIDE))
2523 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2524
2525 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002526}
2527
2528/**
2529 * smack_unix_may_send - Smack access on UDS
2530 * @sock: one socket
2531 * @other: the other socket
2532 *
2533 * Return 0 if a subject with the smack of sock could access
2534 * an object with the smack of other, otherwise an error code
2535 */
2536static int smack_unix_may_send(struct socket *sock, struct socket *other)
2537{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002538 struct socket_smack *ssp = sock->sk->sk_security;
2539 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002540 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002541 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002542
Etienne Bassetecfcc532009-04-08 20:40:06 +02002543 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2544 smk_ad_setfield_u_net_sk(&ad, other->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002545
2546 if (!capable(CAP_MAC_OVERRIDE))
2547 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2548
2549 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002550}
2551
2552/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002553 * smack_socket_sendmsg - Smack check based on destination host
2554 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002555 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002556 * @size: the size of the message
2557 *
2558 * Return 0 if the current subject can write to the destination
2559 * host. This is only a question if the destination is a single
2560 * label host.
2561 */
2562static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2563 int size)
2564{
2565 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002566
2567 /*
2568 * Perfectly reasonable for this to be NULL
2569 */
Julia Lawallda34d422009-08-05 14:34:55 +02002570 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002571 return 0;
2572
Paul Moore07feee82009-03-27 17:10:54 -04002573 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002574}
2575
2576
2577/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002578 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002579 * @sap: netlabel secattr
2580 * @sip: where to put the result
2581 *
2582 * Copies a smack label into sip
2583 */
2584static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2585{
2586 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002587 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002588 int pcat;
2589
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002590 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002591 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002592 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002593 * If there are flags but no level netlabel isn't
2594 * behaving the way we expect it to.
2595 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002596 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002597 * Without guidance regarding the smack value
2598 * for the packet fall back on the network
2599 * ambient value.
2600 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002601 memset(smack, '\0', SMK_LABELLEN);
2602 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2603 for (pcat = -1;;) {
2604 pcat = netlbl_secattr_catmap_walk(
2605 sap->attr.mls.cat, pcat + 1);
2606 if (pcat < 0)
2607 break;
2608 smack_catset_bit(pcat, smack);
2609 }
2610 /*
2611 * If it is CIPSO using smack direct mapping
2612 * we are already done. WeeHee.
2613 */
2614 if (sap->attr.mls.lvl == smack_cipso_direct) {
2615 memcpy(sip, smack, SMK_MAXLEN);
2616 return;
Casey Schauflere114e472008-02-04 22:29:50 -08002617 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002618 /*
2619 * Look it up in the supplied table if it is not
2620 * a direct mapping.
2621 */
2622 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2623 return;
2624 }
2625 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2626 /*
2627 * Looks like a fallback, which gives us a secid.
2628 */
2629 sp = smack_from_secid(sap->attr.secid);
2630 /*
2631 * This has got to be a bug because it is
2632 * impossible to specify a fallback without
2633 * specifying the label, which will ensure
2634 * it has a secid, and the only way to get a
2635 * secid is from a fallback.
2636 */
2637 BUG_ON(sp == NULL);
2638 strncpy(sip, sp, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002639 return;
2640 }
2641 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002642 * Without guidance regarding the smack value
2643 * for the packet fall back on the network
2644 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002645 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002646 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002647 return;
2648}
2649
2650/**
2651 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2652 * @sk: socket
2653 * @skb: packet
2654 *
2655 * Returns 0 if the packet should be delivered, an error code otherwise
2656 */
2657static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2658{
2659 struct netlbl_lsm_secattr secattr;
2660 struct socket_smack *ssp = sk->sk_security;
2661 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002662 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002663 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002664 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002665 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2666 return 0;
2667
2668 /*
2669 * Translate what netlabel gave us.
2670 */
Casey Schauflere114e472008-02-04 22:29:50 -08002671 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002672
Casey Schauflere114e472008-02-04 22:29:50 -08002673 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002674 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002675 smack_from_secattr(&secattr, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002676 csp = smack;
2677 } else
2678 csp = smack_net_ambient;
2679
Casey Schauflere114e472008-02-04 22:29:50 -08002680 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002681
Etienne Bassetecfcc532009-04-08 20:40:06 +02002682#ifdef CONFIG_AUDIT
2683 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2684 ad.a.u.net.family = sk->sk_family;
Eric Dumazet8964be42009-11-20 15:35:04 -08002685 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002686 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2687#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002688 /*
2689 * Receiving a packet requires that the other end
2690 * be able to write here. Read access is not required.
2691 * This is the simplist possible security model
2692 * for networking.
2693 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002694 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002695 if (rc != 0)
2696 netlbl_skbuff_err(skb, rc, 0);
2697 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002698}
2699
2700/**
2701 * smack_socket_getpeersec_stream - pull in packet label
2702 * @sock: the socket
2703 * @optval: user's destination
2704 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002705 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002706 *
2707 * returns zero on success, an error code otherwise
2708 */
2709static int smack_socket_getpeersec_stream(struct socket *sock,
2710 char __user *optval,
2711 int __user *optlen, unsigned len)
2712{
2713 struct socket_smack *ssp;
2714 int slen;
2715 int rc = 0;
2716
2717 ssp = sock->sk->sk_security;
2718 slen = strlen(ssp->smk_packet) + 1;
2719
2720 if (slen > len)
2721 rc = -ERANGE;
2722 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2723 rc = -EFAULT;
2724
2725 if (put_user(slen, optlen) != 0)
2726 rc = -EFAULT;
2727
2728 return rc;
2729}
2730
2731
2732/**
2733 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002734 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08002735 * @skb: packet data
2736 * @secid: pointer to where to put the secid of the packet
2737 *
2738 * Sets the netlabel socket state on sk from parent
2739 */
2740static int smack_socket_getpeersec_dgram(struct socket *sock,
2741 struct sk_buff *skb, u32 *secid)
2742
2743{
2744 struct netlbl_lsm_secattr secattr;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002745 struct socket_smack *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002746 char smack[SMK_LABELLEN];
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002747 int family = PF_UNSPEC;
2748 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08002749 int rc;
2750
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002751 if (skb != NULL) {
2752 if (skb->protocol == htons(ETH_P_IP))
2753 family = PF_INET;
2754 else if (skb->protocol == htons(ETH_P_IPV6))
2755 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08002756 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002757 if (family == PF_UNSPEC && sock != NULL)
2758 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08002759
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002760 if (family == PF_UNIX) {
2761 sp = sock->sk->sk_security;
2762 s = smack_to_secid(sp->smk_out);
2763 } else if (family == PF_INET || family == PF_INET6) {
2764 /*
2765 * Translate what netlabel gave us.
2766 */
2767 netlbl_secattr_init(&secattr);
2768 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2769 if (rc == 0) {
2770 smack_from_secattr(&secattr, smack);
2771 s = smack_to_secid(smack);
2772 }
2773 netlbl_secattr_destroy(&secattr);
2774 }
2775 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08002776 if (s == 0)
2777 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002778 return 0;
2779}
2780
2781/**
Paul Moore07feee82009-03-27 17:10:54 -04002782 * smack_sock_graft - Initialize a newly created socket with an existing sock
2783 * @sk: child sock
2784 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08002785 *
Paul Moore07feee82009-03-27 17:10:54 -04002786 * Set the smk_{in,out} state of an existing sock based on the process that
2787 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08002788 */
2789static void smack_sock_graft(struct sock *sk, struct socket *parent)
2790{
2791 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08002792
Paul Moore07feee82009-03-27 17:10:54 -04002793 if (sk == NULL ||
2794 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08002795 return;
2796
2797 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002798 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04002799 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08002800}
2801
2802/**
2803 * smack_inet_conn_request - Smack access check on connect
2804 * @sk: socket involved
2805 * @skb: packet
2806 * @req: unused
2807 *
2808 * Returns 0 if a task with the packet label could write to
2809 * the socket, otherwise an error code
2810 */
2811static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2812 struct request_sock *req)
2813{
Paul Moore07feee82009-03-27 17:10:54 -04002814 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08002815 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04002816 struct netlbl_lsm_secattr secattr;
2817 struct sockaddr_in addr;
2818 struct iphdr *hdr;
Casey Schauflere114e472008-02-04 22:29:50 -08002819 char smack[SMK_LABELLEN];
2820 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002821 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002822
Paul Moore07feee82009-03-27 17:10:54 -04002823 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2824 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2825 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08002826
Paul Moore07feee82009-03-27 17:10:54 -04002827 netlbl_secattr_init(&secattr);
2828 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08002829 if (rc == 0)
Paul Moore07feee82009-03-27 17:10:54 -04002830 smack_from_secattr(&secattr, smack);
Casey Schauflere114e472008-02-04 22:29:50 -08002831 else
2832 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
Paul Moore07feee82009-03-27 17:10:54 -04002833 netlbl_secattr_destroy(&secattr);
2834
Etienne Bassetecfcc532009-04-08 20:40:06 +02002835#ifdef CONFIG_AUDIT
2836 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2837 ad.a.u.net.family = family;
Eric Dumazet8964be42009-11-20 15:35:04 -08002838 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002839 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2840#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002841 /*
Paul Moore07feee82009-03-27 17:10:54 -04002842 * Receiving a packet requires that the other end be able to write
2843 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08002844 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002845 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04002846 if (rc != 0)
2847 return rc;
2848
2849 /*
2850 * Save the peer's label in the request_sock so we can later setup
2851 * smk_packet in the child socket so that SO_PEERCRED can report it.
2852 */
2853 req->peer_secid = smack_to_secid(smack);
2854
2855 /*
2856 * We need to decide if we want to label the incoming connection here
2857 * if we do we only need to label the request_sock and the stack will
2858 * propogate the wire-label to the sock when it is created.
2859 */
2860 hdr = ip_hdr(skb);
2861 addr.sin_addr.s_addr = hdr->saddr;
2862 rcu_read_lock();
2863 if (smack_host_label(&addr) == NULL) {
2864 rcu_read_unlock();
2865 netlbl_secattr_init(&secattr);
2866 smack_to_secattr(smack, &secattr);
2867 rc = netlbl_req_setattr(req, &secattr);
2868 netlbl_secattr_destroy(&secattr);
2869 } else {
2870 rcu_read_unlock();
2871 netlbl_req_delattr(req);
2872 }
Casey Schauflere114e472008-02-04 22:29:50 -08002873
2874 return rc;
2875}
2876
Paul Moore07feee82009-03-27 17:10:54 -04002877/**
2878 * smack_inet_csk_clone - Copy the connection information to the new socket
2879 * @sk: the new socket
2880 * @req: the connection's request_sock
2881 *
2882 * Transfer the connection's peer label to the newly created socket.
2883 */
2884static void smack_inet_csk_clone(struct sock *sk,
2885 const struct request_sock *req)
2886{
2887 struct socket_smack *ssp = sk->sk_security;
2888 char *smack;
2889
2890 if (req->peer_secid != 0) {
2891 smack = smack_from_secid(req->peer_secid);
2892 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2893 } else
2894 ssp->smk_packet[0] = '\0';
2895}
2896
Casey Schauflere114e472008-02-04 22:29:50 -08002897/*
2898 * Key management security hooks
2899 *
2900 * Casey has not tested key support very heavily.
2901 * The permission check is most likely too restrictive.
2902 * If you care about keys please have a look.
2903 */
2904#ifdef CONFIG_KEYS
2905
2906/**
2907 * smack_key_alloc - Set the key security blob
2908 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11002909 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002910 * @flags: unused
2911 *
2912 * No allocation required
2913 *
2914 * Returns 0
2915 */
David Howellsd84f4f92008-11-14 10:39:23 +11002916static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08002917 unsigned long flags)
2918{
Casey Schaufler676dac42010-12-02 06:43:39 -08002919 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08002920 return 0;
2921}
2922
2923/**
2924 * smack_key_free - Clear the key security blob
2925 * @key: the object
2926 *
2927 * Clear the blob pointer
2928 */
2929static void smack_key_free(struct key *key)
2930{
2931 key->security = NULL;
2932}
2933
2934/*
2935 * smack_key_permission - Smack access on a key
2936 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11002937 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002938 * @perm: unused
2939 *
2940 * Return 0 if the task has read and write to the object,
2941 * an error code otherwise
2942 */
2943static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11002944 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08002945{
2946 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002947 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08002948 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08002949
2950 keyp = key_ref_to_ptr(key_ref);
2951 if (keyp == NULL)
2952 return -EINVAL;
2953 /*
2954 * If the key hasn't been initialized give it access so that
2955 * it may do so.
2956 */
2957 if (keyp->security == NULL)
2958 return 0;
2959 /*
2960 * This should not occur
2961 */
Casey Schaufler676dac42010-12-02 06:43:39 -08002962 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002963 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002964#ifdef CONFIG_AUDIT
2965 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
2966 ad.a.u.key_struct.key = keyp->serial;
2967 ad.a.u.key_struct.key_desc = keyp->description;
2968#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08002969 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02002970 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002971}
2972#endif /* CONFIG_KEYS */
2973
2974/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002975 * Smack Audit hooks
2976 *
2977 * Audit requires a unique representation of each Smack specific
2978 * rule. This unique representation is used to distinguish the
2979 * object to be audited from remaining kernel objects and also
2980 * works as a glue between the audit hooks.
2981 *
2982 * Since repository entries are added but never deleted, we'll use
2983 * the smack_known label address related to the given audit rule as
2984 * the needed unique representation. This also better fits the smack
2985 * model where nearly everything is a label.
2986 */
2987#ifdef CONFIG_AUDIT
2988
2989/**
2990 * smack_audit_rule_init - Initialize a smack audit rule
2991 * @field: audit rule fields given from user-space (audit.h)
2992 * @op: required testing operator (=, !=, >, <, ...)
2993 * @rulestr: smack label to be audited
2994 * @vrule: pointer to save our own audit rule representation
2995 *
2996 * Prepare to audit cases where (@field @op @rulestr) is true.
2997 * The label to be audited is created if necessay.
2998 */
2999static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3000{
3001 char **rule = (char **)vrule;
3002 *rule = NULL;
3003
3004 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3005 return -EINVAL;
3006
Al Viro5af75d82008-12-16 05:59:26 -05003007 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003008 return -EINVAL;
3009
3010 *rule = smk_import(rulestr, 0);
3011
3012 return 0;
3013}
3014
3015/**
3016 * smack_audit_rule_known - Distinguish Smack audit rules
3017 * @krule: rule of interest, in Audit kernel representation format
3018 *
3019 * This is used to filter Smack rules from remaining Audit ones.
3020 * If it's proved that this rule belongs to us, the
3021 * audit_rule_match hook will be called to do the final judgement.
3022 */
3023static int smack_audit_rule_known(struct audit_krule *krule)
3024{
3025 struct audit_field *f;
3026 int i;
3027
3028 for (i = 0; i < krule->field_count; i++) {
3029 f = &krule->fields[i];
3030
3031 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3032 return 1;
3033 }
3034
3035 return 0;
3036}
3037
3038/**
3039 * smack_audit_rule_match - Audit given object ?
3040 * @secid: security id for identifying the object to test
3041 * @field: audit rule flags given from user-space
3042 * @op: required testing operator
3043 * @vrule: smack internal rule presentation
3044 * @actx: audit context associated with the check
3045 *
3046 * The core Audit hook. It's used to take the decision of
3047 * whether to audit or not to audit a given object.
3048 */
3049static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3050 struct audit_context *actx)
3051{
3052 char *smack;
3053 char *rule = vrule;
3054
3055 if (!rule) {
3056 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3057 "Smack: missing rule\n");
3058 return -ENOENT;
3059 }
3060
3061 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3062 return 0;
3063
3064 smack = smack_from_secid(secid);
3065
3066 /*
3067 * No need to do string comparisons. If a match occurs,
3068 * both pointers will point to the same smack_known
3069 * label.
3070 */
Al Viro5af75d82008-12-16 05:59:26 -05003071 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003072 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003073 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003074 return (rule != smack);
3075
3076 return 0;
3077}
3078
3079/**
3080 * smack_audit_rule_free - free smack rule representation
3081 * @vrule: rule to be freed.
3082 *
3083 * No memory was allocated.
3084 */
3085static void smack_audit_rule_free(void *vrule)
3086{
3087 /* No-op */
3088}
3089
3090#endif /* CONFIG_AUDIT */
3091
Randy Dunlap251a2a92009-02-18 11:42:33 -08003092/**
Casey Schauflere114e472008-02-04 22:29:50 -08003093 * smack_secid_to_secctx - return the smack label for a secid
3094 * @secid: incoming integer
3095 * @secdata: destination
3096 * @seclen: how long it is
3097 *
3098 * Exists for networking code.
3099 */
3100static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3101{
3102 char *sp = smack_from_secid(secid);
3103
Eric Parisd5630b92010-10-13 16:24:48 -04003104 if (secdata)
3105 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003106 *seclen = strlen(sp);
3107 return 0;
3108}
3109
Randy Dunlap251a2a92009-02-18 11:42:33 -08003110/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003111 * smack_secctx_to_secid - return the secid for a smack label
3112 * @secdata: smack label
3113 * @seclen: how long result is
3114 * @secid: outgoing integer
3115 *
3116 * Exists for audit and networking code.
3117 */
David Howellse52c17642008-04-29 20:52:51 +01003118static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003119{
3120 *secid = smack_to_secid(secdata);
3121 return 0;
3122}
3123
Randy Dunlap251a2a92009-02-18 11:42:33 -08003124/**
Casey Schauflere114e472008-02-04 22:29:50 -08003125 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003126 * @secdata: unused
3127 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003128 *
3129 * Exists to make sure nothing gets done, and properly
3130 */
3131static void smack_release_secctx(char *secdata, u32 seclen)
3132{
3133}
3134
David P. Quigley1ee65e32009-09-03 14:25:57 -04003135static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3136{
3137 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3138}
3139
3140static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3141{
3142 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3143}
3144
3145static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3146{
3147 int len = 0;
3148 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3149
3150 if (len < 0)
3151 return len;
3152 *ctxlen = len;
3153 return 0;
3154}
3155
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003156struct security_operations smack_ops = {
3157 .name = "smack",
3158
Ingo Molnar9e488582009-05-07 19:26:19 +10003159 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003160 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003161 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003162
3163 .sb_alloc_security = smack_sb_alloc_security,
3164 .sb_free_security = smack_sb_free_security,
3165 .sb_copy_data = smack_sb_copy_data,
3166 .sb_kern_mount = smack_sb_kern_mount,
3167 .sb_statfs = smack_sb_statfs,
3168 .sb_mount = smack_sb_mount,
3169 .sb_umount = smack_sb_umount,
3170
Casey Schaufler676dac42010-12-02 06:43:39 -08003171 .bprm_set_creds = smack_bprm_set_creds,
3172
Casey Schauflere114e472008-02-04 22:29:50 -08003173 .inode_alloc_security = smack_inode_alloc_security,
3174 .inode_free_security = smack_inode_free_security,
3175 .inode_init_security = smack_inode_init_security,
3176 .inode_link = smack_inode_link,
3177 .inode_unlink = smack_inode_unlink,
3178 .inode_rmdir = smack_inode_rmdir,
3179 .inode_rename = smack_inode_rename,
3180 .inode_permission = smack_inode_permission,
3181 .inode_setattr = smack_inode_setattr,
3182 .inode_getattr = smack_inode_getattr,
3183 .inode_setxattr = smack_inode_setxattr,
3184 .inode_post_setxattr = smack_inode_post_setxattr,
3185 .inode_getxattr = smack_inode_getxattr,
3186 .inode_removexattr = smack_inode_removexattr,
3187 .inode_getsecurity = smack_inode_getsecurity,
3188 .inode_setsecurity = smack_inode_setsecurity,
3189 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003190 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003191
3192 .file_permission = smack_file_permission,
3193 .file_alloc_security = smack_file_alloc_security,
3194 .file_free_security = smack_file_free_security,
3195 .file_ioctl = smack_file_ioctl,
3196 .file_lock = smack_file_lock,
3197 .file_fcntl = smack_file_fcntl,
3198 .file_set_fowner = smack_file_set_fowner,
3199 .file_send_sigiotask = smack_file_send_sigiotask,
3200 .file_receive = smack_file_receive,
3201
David Howellsee18d642009-09-02 09:14:21 +01003202 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003203 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003204 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003205 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003206 .kernel_act_as = smack_kernel_act_as,
3207 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003208 .task_setpgid = smack_task_setpgid,
3209 .task_getpgid = smack_task_getpgid,
3210 .task_getsid = smack_task_getsid,
3211 .task_getsecid = smack_task_getsecid,
3212 .task_setnice = smack_task_setnice,
3213 .task_setioprio = smack_task_setioprio,
3214 .task_getioprio = smack_task_getioprio,
3215 .task_setscheduler = smack_task_setscheduler,
3216 .task_getscheduler = smack_task_getscheduler,
3217 .task_movememory = smack_task_movememory,
3218 .task_kill = smack_task_kill,
3219 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003220 .task_to_inode = smack_task_to_inode,
3221
3222 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003223 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003224
3225 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3226 .msg_msg_free_security = smack_msg_msg_free_security,
3227
3228 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3229 .msg_queue_free_security = smack_msg_queue_free_security,
3230 .msg_queue_associate = smack_msg_queue_associate,
3231 .msg_queue_msgctl = smack_msg_queue_msgctl,
3232 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3233 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3234
3235 .shm_alloc_security = smack_shm_alloc_security,
3236 .shm_free_security = smack_shm_free_security,
3237 .shm_associate = smack_shm_associate,
3238 .shm_shmctl = smack_shm_shmctl,
3239 .shm_shmat = smack_shm_shmat,
3240
3241 .sem_alloc_security = smack_sem_alloc_security,
3242 .sem_free_security = smack_sem_free_security,
3243 .sem_associate = smack_sem_associate,
3244 .sem_semctl = smack_sem_semctl,
3245 .sem_semop = smack_sem_semop,
3246
Casey Schauflere114e472008-02-04 22:29:50 -08003247 .d_instantiate = smack_d_instantiate,
3248
3249 .getprocattr = smack_getprocattr,
3250 .setprocattr = smack_setprocattr,
3251
3252 .unix_stream_connect = smack_unix_stream_connect,
3253 .unix_may_send = smack_unix_may_send,
3254
3255 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003256 .socket_connect = smack_socket_connect,
3257 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003258 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3259 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3260 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3261 .sk_alloc_security = smack_sk_alloc_security,
3262 .sk_free_security = smack_sk_free_security,
3263 .sock_graft = smack_sock_graft,
3264 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003265 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003266
Casey Schauflere114e472008-02-04 22:29:50 -08003267 /* key management security hooks */
3268#ifdef CONFIG_KEYS
3269 .key_alloc = smack_key_alloc,
3270 .key_free = smack_key_free,
3271 .key_permission = smack_key_permission,
3272#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003273
3274 /* Audit hooks */
3275#ifdef CONFIG_AUDIT
3276 .audit_rule_init = smack_audit_rule_init,
3277 .audit_rule_known = smack_audit_rule_known,
3278 .audit_rule_match = smack_audit_rule_match,
3279 .audit_rule_free = smack_audit_rule_free,
3280#endif /* CONFIG_AUDIT */
3281
Casey Schauflere114e472008-02-04 22:29:50 -08003282 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003283 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003284 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003285 .inode_notifysecctx = smack_inode_notifysecctx,
3286 .inode_setsecctx = smack_inode_setsecctx,
3287 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003288};
3289
Etienne Basset7198e2e2009-03-24 20:53:24 +01003290
3291static __init void init_smack_know_list(void)
3292{
3293 list_add(&smack_known_huh.list, &smack_known_list);
3294 list_add(&smack_known_hat.list, &smack_known_list);
3295 list_add(&smack_known_star.list, &smack_known_list);
3296 list_add(&smack_known_floor.list, &smack_known_list);
3297 list_add(&smack_known_invalid.list, &smack_known_list);
3298 list_add(&smack_known_web.list, &smack_known_list);
3299}
3300
Casey Schauflere114e472008-02-04 22:29:50 -08003301/**
3302 * smack_init - initialize the smack system
3303 *
3304 * Returns 0
3305 */
3306static __init int smack_init(void)
3307{
David Howellsd84f4f92008-11-14 10:39:23 +11003308 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003309 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003310
Casey Schaufler676dac42010-12-02 06:43:39 -08003311 tsp = kzalloc(sizeof(struct task_smack), GFP_KERNEL);
3312 if (tsp == NULL)
3313 return -ENOMEM;
3314
3315 if (!security_module_enable(&smack_ops)) {
3316 kfree(tsp);
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003317 return 0;
Casey Schaufler676dac42010-12-02 06:43:39 -08003318 }
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003319
Casey Schauflere114e472008-02-04 22:29:50 -08003320 printk(KERN_INFO "Smack: Initializing.\n");
3321
3322 /*
3323 * Set the security state for the initial task.
3324 */
David Howellsd84f4f92008-11-14 10:39:23 +11003325 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003326 tsp->smk_forked = smack_known_floor.smk_known;
3327 tsp->smk_task = smack_known_floor.smk_known;
3328 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003329
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003330 /* initialize the smack_know_list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003331 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003332 /*
3333 * Initialize locks
3334 */
Casey Schauflere114e472008-02-04 22:29:50 -08003335 spin_lock_init(&smack_known_huh.smk_cipsolock);
3336 spin_lock_init(&smack_known_hat.smk_cipsolock);
3337 spin_lock_init(&smack_known_star.smk_cipsolock);
3338 spin_lock_init(&smack_known_floor.smk_cipsolock);
3339 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3340
3341 /*
3342 * Register with LSM
3343 */
3344 if (register_security(&smack_ops))
3345 panic("smack: Unable to register with kernel.\n");
3346
3347 return 0;
3348}
3349
3350/*
3351 * Smack requires early initialization in order to label
3352 * all processes and objects when they are created.
3353 */
3354security_initcall(smack_init);