blob: 0023182078c726797de5ccab784a1922eba17859 [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>
22#include <linux/ext2_fs.h>
23#include <linux/kd.h>
24#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040025#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080026#include <linux/tcp.h>
27#include <linux/udp.h>
28#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>
Casey Schauflere114e472008-02-04 22:29:50 -080033#include "smack.h"
34
David Howellsc69e8d92008-11-14 10:39:19 +110035#define task_security(task) (task_cred_xxx((task), security))
36
Casey Schauflere114e472008-02-04 22:29:50 -080037/*
38 * I hope these are the hokeyist lines of code in the module. Casey.
39 */
40#define DEVPTS_SUPER_MAGIC 0x1cd1
41#define SOCKFS_MAGIC 0x534F434B
42#define TMPFS_MAGIC 0x01021994
43
44/**
45 * smk_fetch - Fetch the smack label from a file.
46 * @ip: a pointer to the inode
47 * @dp: a pointer to the dentry
48 *
49 * Returns a pointer to the master list entry for the Smack label
50 * or NULL if there was no label to fetch.
51 */
52static char *smk_fetch(struct inode *ip, struct dentry *dp)
53{
54 int rc;
55 char in[SMK_LABELLEN];
56
57 if (ip->i_op->getxattr == NULL)
58 return NULL;
59
60 rc = ip->i_op->getxattr(dp, XATTR_NAME_SMACK, in, SMK_LABELLEN);
61 if (rc < 0)
62 return NULL;
63
64 return smk_import(in, rc);
65}
66
67/**
68 * new_inode_smack - allocate an inode security blob
69 * @smack: a pointer to the Smack label to use in the blob
70 *
71 * Returns the new blob or NULL if there's no memory available
72 */
73struct inode_smack *new_inode_smack(char *smack)
74{
75 struct inode_smack *isp;
76
77 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
78 if (isp == NULL)
79 return NULL;
80
81 isp->smk_inode = smack;
82 isp->smk_flags = 0;
83 mutex_init(&isp->smk_lock);
84
85 return isp;
86}
87
88/*
89 * LSM hooks.
90 * We he, that is fun!
91 */
92
93/**
David Howells5cd9c582008-08-14 11:37:28 +010094 * smack_ptrace_may_access - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -080095 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -080096 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -080097 *
98 * Returns 0 if access is OK, an error code otherwise
99 *
100 * Do the capability checks, and require read and write.
101 */
David Howells5cd9c582008-08-14 11:37:28 +0100102static int smack_ptrace_may_access(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800103{
104 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200105 struct smk_audit_info ad;
106 char *sp, *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800107
David Howells5cd9c582008-08-14 11:37:28 +0100108 rc = cap_ptrace_may_access(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800109 if (rc != 0)
110 return rc;
111
Etienne Bassetecfcc532009-04-08 20:40:06 +0200112 sp = current_security();
113 tsp = task_security(ctp);
114 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
115 smk_ad_setfield_u_tsk(&ad, ctp);
116
117 /* we won't log here, because rc can be overriden */
118 rc = smk_access(sp, tsp, MAY_READWRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +0100119 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200120 rc = 0;
121
122 smack_log(sp, tsp, MAY_READWRITE, rc, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100123 return rc;
124}
Casey Schauflere114e472008-02-04 22:29:50 -0800125
David Howells5cd9c582008-08-14 11:37:28 +0100126/**
127 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
128 * @ptp: parent task pointer
129 *
130 * Returns 0 if access is OK, an error code otherwise
131 *
132 * Do the capability checks, and require read and write.
133 */
134static int smack_ptrace_traceme(struct task_struct *ptp)
135{
136 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200137 struct smk_audit_info ad;
138 char *sp, *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100139
140 rc = cap_ptrace_traceme(ptp);
141 if (rc != 0)
142 return rc;
143
Etienne Bassetecfcc532009-04-08 20:40:06 +0200144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
145 smk_ad_setfield_u_tsk(&ad, ptp);
146
147 sp = current_security();
148 tsp = task_security(ptp);
149 /* we won't log here, because rc can be overriden */
150 rc = smk_access(tsp, sp, MAY_READWRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +0100151 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200152 rc = 0;
153
154 smack_log(tsp, sp, MAY_READWRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800155 return rc;
156}
157
158/**
159 * smack_syslog - Smack approval on syslog
160 * @type: message type
161 *
162 * Require that the task has the floor label
163 *
164 * Returns 0 on success, error code otherwise.
165 */
166static int smack_syslog(int type)
167{
168 int rc;
David Howells86a264a2008-11-14 10:39:18 +1100169 char *sp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800170
171 rc = cap_syslog(type);
172 if (rc != 0)
173 return rc;
174
175 if (capable(CAP_MAC_OVERRIDE))
176 return 0;
177
178 if (sp != smack_known_floor.smk_known)
179 rc = -EACCES;
180
181 return rc;
182}
183
184
185/*
186 * Superblock Hooks.
187 */
188
189/**
190 * smack_sb_alloc_security - allocate a superblock blob
191 * @sb: the superblock getting the blob
192 *
193 * Returns 0 on success or -ENOMEM on error.
194 */
195static int smack_sb_alloc_security(struct super_block *sb)
196{
197 struct superblock_smack *sbsp;
198
199 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
200
201 if (sbsp == NULL)
202 return -ENOMEM;
203
204 sbsp->smk_root = smack_known_floor.smk_known;
205 sbsp->smk_default = smack_known_floor.smk_known;
206 sbsp->smk_floor = smack_known_floor.smk_known;
207 sbsp->smk_hat = smack_known_hat.smk_known;
208 sbsp->smk_initialized = 0;
209 spin_lock_init(&sbsp->smk_sblock);
210
211 sb->s_security = sbsp;
212
213 return 0;
214}
215
216/**
217 * smack_sb_free_security - free a superblock blob
218 * @sb: the superblock getting the blob
219 *
220 */
221static void smack_sb_free_security(struct super_block *sb)
222{
223 kfree(sb->s_security);
224 sb->s_security = NULL;
225}
226
227/**
228 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800229 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800230 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800231 *
232 * Returns 0 on success or -ENOMEM on error.
233 *
234 * Copy the Smack specific mount options out of the mount
235 * options list.
236 */
Eric Parise0007522008-03-05 10:31:54 -0500237static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800238{
239 char *cp, *commap, *otheropts, *dp;
240
Casey Schauflere114e472008-02-04 22:29:50 -0800241 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
242 if (otheropts == NULL)
243 return -ENOMEM;
244
245 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
246 if (strstr(cp, SMK_FSDEFAULT) == cp)
247 dp = smackopts;
248 else if (strstr(cp, SMK_FSFLOOR) == cp)
249 dp = smackopts;
250 else if (strstr(cp, SMK_FSHAT) == cp)
251 dp = smackopts;
252 else if (strstr(cp, SMK_FSROOT) == cp)
253 dp = smackopts;
254 else
255 dp = otheropts;
256
257 commap = strchr(cp, ',');
258 if (commap != NULL)
259 *commap = '\0';
260
261 if (*dp != '\0')
262 strcat(dp, ",");
263 strcat(dp, cp);
264 }
265
266 strcpy(orig, otheropts);
267 free_page((unsigned long)otheropts);
268
269 return 0;
270}
271
272/**
273 * smack_sb_kern_mount - Smack specific mount processing
274 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100275 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800276 * @data: the smack mount options
277 *
278 * Returns 0 on success, an error code on failure
279 */
James Morris12204e22008-12-19 10:44:42 +1100280static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800281{
282 struct dentry *root = sb->s_root;
283 struct inode *inode = root->d_inode;
284 struct superblock_smack *sp = sb->s_security;
285 struct inode_smack *isp;
286 char *op;
287 char *commap;
288 char *nsp;
289
290 spin_lock(&sp->smk_sblock);
291 if (sp->smk_initialized != 0) {
292 spin_unlock(&sp->smk_sblock);
293 return 0;
294 }
295 sp->smk_initialized = 1;
296 spin_unlock(&sp->smk_sblock);
297
298 for (op = data; op != NULL; op = commap) {
299 commap = strchr(op, ',');
300 if (commap != NULL)
301 *commap++ = '\0';
302
303 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
304 op += strlen(SMK_FSHAT);
305 nsp = smk_import(op, 0);
306 if (nsp != NULL)
307 sp->smk_hat = nsp;
308 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
309 op += strlen(SMK_FSFLOOR);
310 nsp = smk_import(op, 0);
311 if (nsp != NULL)
312 sp->smk_floor = nsp;
313 } else if (strncmp(op, SMK_FSDEFAULT,
314 strlen(SMK_FSDEFAULT)) == 0) {
315 op += strlen(SMK_FSDEFAULT);
316 nsp = smk_import(op, 0);
317 if (nsp != NULL)
318 sp->smk_default = nsp;
319 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
320 op += strlen(SMK_FSROOT);
321 nsp = smk_import(op, 0);
322 if (nsp != NULL)
323 sp->smk_root = nsp;
324 }
325 }
326
327 /*
328 * Initialize the root inode.
329 */
330 isp = inode->i_security;
331 if (isp == NULL)
332 inode->i_security = new_inode_smack(sp->smk_root);
333 else
334 isp->smk_inode = sp->smk_root;
335
336 return 0;
337}
338
339/**
340 * smack_sb_statfs - Smack check on statfs
341 * @dentry: identifies the file system in question
342 *
343 * Returns 0 if current can read the floor of the filesystem,
344 * and error code otherwise
345 */
346static int smack_sb_statfs(struct dentry *dentry)
347{
348 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200349 int rc;
350 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800351
Etienne Bassetecfcc532009-04-08 20:40:06 +0200352 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
353 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
354
355 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
356 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800357}
358
359/**
360 * smack_sb_mount - Smack check for mounting
361 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800362 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800363 * @type: unused
364 * @flags: unused
365 * @data: unused
366 *
367 * Returns 0 if current can write the floor of the filesystem
368 * being mounted on, an error code otherwise.
369 */
Al Virob5266eb2008-03-22 17:48:24 -0400370static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800371 char *type, unsigned long flags, void *data)
372{
Al Virob5266eb2008-03-22 17:48:24 -0400373 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200374 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800375
Etienne Bassetecfcc532009-04-08 20:40:06 +0200376 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
377 smk_ad_setfield_u_fs_path(&ad, *path);
378
379 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800380}
381
382/**
383 * smack_sb_umount - Smack check for unmounting
384 * @mnt: file system to unmount
385 * @flags: unused
386 *
387 * Returns 0 if current can write the floor of the filesystem
388 * being unmounted, an error code otherwise.
389 */
390static int smack_sb_umount(struct vfsmount *mnt, int flags)
391{
392 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200393 struct smk_audit_info ad;
394
395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
396 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_mountpoint);
397 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
Casey Schauflere114e472008-02-04 22:29:50 -0800398
399 sbp = mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200400 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800401}
402
403/*
404 * Inode hooks
405 */
406
407/**
408 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800409 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800410 *
411 * Returns 0 if it gets a blob, -ENOMEM otherwise
412 */
413static int smack_inode_alloc_security(struct inode *inode)
414{
David Howells86a264a2008-11-14 10:39:18 +1100415 inode->i_security = new_inode_smack(current_security());
Casey Schauflere114e472008-02-04 22:29:50 -0800416 if (inode->i_security == NULL)
417 return -ENOMEM;
418 return 0;
419}
420
421/**
422 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800423 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800424 *
425 * Clears the blob pointer in inode
426 */
427static void smack_inode_free_security(struct inode *inode)
428{
429 kfree(inode->i_security);
430 inode->i_security = NULL;
431}
432
433/**
434 * smack_inode_init_security - copy out the smack from an inode
435 * @inode: the inode
436 * @dir: unused
437 * @name: where to put the attribute name
438 * @value: where to put the attribute value
439 * @len: where to put the length of the attribute
440 *
441 * Returns 0 if it all works out, -ENOMEM if there's no memory
442 */
443static int smack_inode_init_security(struct inode *inode, struct inode *dir,
444 char **name, void **value, size_t *len)
445{
446 char *isp = smk_of_inode(inode);
447
448 if (name) {
449 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
450 if (*name == NULL)
451 return -ENOMEM;
452 }
453
454 if (value) {
455 *value = kstrdup(isp, GFP_KERNEL);
456 if (*value == NULL)
457 return -ENOMEM;
458 }
459
460 if (len)
461 *len = strlen(isp) + 1;
462
463 return 0;
464}
465
466/**
467 * smack_inode_link - Smack check on link
468 * @old_dentry: the existing object
469 * @dir: unused
470 * @new_dentry: the new object
471 *
472 * Returns 0 if access is permitted, an error code otherwise
473 */
474static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
475 struct dentry *new_dentry)
476{
Casey Schauflere114e472008-02-04 22:29:50 -0800477 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200478 struct smk_audit_info ad;
479 int rc;
480
481 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
482 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800483
484 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200485 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800486
487 if (rc == 0 && new_dentry->d_inode != NULL) {
488 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200489 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
490 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800491 }
492
493 return rc;
494}
495
496/**
497 * smack_inode_unlink - Smack check on inode deletion
498 * @dir: containing directory object
499 * @dentry: file to unlink
500 *
501 * Returns 0 if current can write the containing directory
502 * and the object, error code otherwise
503 */
504static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
505{
506 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200507 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800508 int rc;
509
Etienne Bassetecfcc532009-04-08 20:40:06 +0200510 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
511 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
512
Casey Schauflere114e472008-02-04 22:29:50 -0800513 /*
514 * You need write access to the thing you're unlinking
515 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200516 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
517 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800518 /*
519 * You also need write access to the containing directory
520 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200521 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
522 smk_ad_setfield_u_fs_inode(&ad, dir);
523 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
524 }
Casey Schauflere114e472008-02-04 22:29:50 -0800525 return rc;
526}
527
528/**
529 * smack_inode_rmdir - Smack check on directory deletion
530 * @dir: containing directory object
531 * @dentry: directory to unlink
532 *
533 * Returns 0 if current can write the containing directory
534 * and the directory, error code otherwise
535 */
536static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
537{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200538 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800539 int rc;
540
Etienne Bassetecfcc532009-04-08 20:40:06 +0200541 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
542 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
543
Casey Schauflere114e472008-02-04 22:29:50 -0800544 /*
545 * You need write access to the thing you're removing
546 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200547 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
548 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800549 /*
550 * You also need write access to the containing directory
551 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200552 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
553 smk_ad_setfield_u_fs_inode(&ad, dir);
554 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
555 }
Casey Schauflere114e472008-02-04 22:29:50 -0800556
557 return rc;
558}
559
560/**
561 * smack_inode_rename - Smack check on rename
562 * @old_inode: the old directory
563 * @old_dentry: unused
564 * @new_inode: the new directory
565 * @new_dentry: unused
566 *
567 * Read and write access is required on both the old and
568 * new directories.
569 *
570 * Returns 0 if access is permitted, an error code otherwise
571 */
572static int smack_inode_rename(struct inode *old_inode,
573 struct dentry *old_dentry,
574 struct inode *new_inode,
575 struct dentry *new_dentry)
576{
577 int rc;
578 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200579 struct smk_audit_info ad;
580
581 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
582 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800583
584 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200585 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800586
587 if (rc == 0 && new_dentry->d_inode != NULL) {
588 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200589 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
590 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800591 }
Casey Schauflere114e472008-02-04 22:29:50 -0800592 return rc;
593}
594
595/**
596 * smack_inode_permission - Smack version of permission()
597 * @inode: the inode in question
598 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800599 *
600 * This is the important Smack hook.
601 *
602 * Returns 0 if access is permitted, -EACCES otherwise
603 */
Al Virob77b0642008-07-17 09:37:02 -0400604static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800605{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200606 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800607 /*
608 * No permission to check. Existence test. Yup, it's there.
609 */
610 if (mask == 0)
611 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
613 smk_ad_setfield_u_fs_inode(&ad, inode);
614 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800615}
616
617/**
618 * smack_inode_setattr - Smack check for setting attributes
619 * @dentry: the object
620 * @iattr: for the force flag
621 *
622 * Returns 0 if access is permitted, an error code otherwise
623 */
624static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
625{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200626 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800627 /*
628 * Need to allow for clearing the setuid bit.
629 */
630 if (iattr->ia_valid & ATTR_FORCE)
631 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200632 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
633 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800634
Etienne Bassetecfcc532009-04-08 20:40:06 +0200635 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800636}
637
638/**
639 * smack_inode_getattr - Smack check for getting attributes
640 * @mnt: unused
641 * @dentry: the object
642 *
643 * Returns 0 if access is permitted, an error code otherwise
644 */
645static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
646{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200647 struct smk_audit_info ad;
648
649 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
650 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
651 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
652 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800653}
654
655/**
656 * smack_inode_setxattr - Smack check for setting xattrs
657 * @dentry: the object
658 * @name: name of the attribute
659 * @value: unused
660 * @size: unused
661 * @flags: unused
662 *
663 * This protects the Smack attribute explicitly.
664 *
665 * Returns 0 if access is permitted, an error code otherwise
666 */
David Howells8f0cfa52008-04-29 00:59:41 -0700667static int smack_inode_setxattr(struct dentry *dentry, const char *name,
668 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800669{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200670 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800671 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800672
Casey Schauflerbcdca222008-02-23 15:24:04 -0800673 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
674 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
675 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
676 if (!capable(CAP_MAC_ADMIN))
677 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200678 /*
679 * check label validity here so import wont fail on
680 * post_setxattr
681 */
682 if (size == 0 || size >= SMK_LABELLEN ||
683 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400684 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800685 } else
686 rc = cap_inode_setxattr(dentry, name, value, size, flags);
687
Etienne Bassetecfcc532009-04-08 20:40:06 +0200688 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
689 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
690
Casey Schauflerbcdca222008-02-23 15:24:04 -0800691 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200692 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800693
694 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800695}
696
697/**
698 * smack_inode_post_setxattr - Apply the Smack update approved above
699 * @dentry: object
700 * @name: attribute name
701 * @value: attribute value
702 * @size: attribute size
703 * @flags: unused
704 *
705 * Set the pointer in the inode blob to the entry found
706 * in the master label list.
707 */
David Howells8f0cfa52008-04-29 00:59:41 -0700708static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
709 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800710{
711 struct inode_smack *isp;
712 char *nsp;
713
714 /*
715 * Not SMACK
716 */
717 if (strcmp(name, XATTR_NAME_SMACK))
718 return;
719
Casey Schauflere114e472008-02-04 22:29:50 -0800720 isp = dentry->d_inode->i_security;
721
722 /*
723 * No locking is done here. This is a pointer
724 * assignment.
725 */
726 nsp = smk_import(value, size);
727 if (nsp != NULL)
728 isp->smk_inode = nsp;
729 else
730 isp->smk_inode = smack_known_invalid.smk_known;
731
732 return;
733}
734
735/*
736 * smack_inode_getxattr - Smack check on getxattr
737 * @dentry: the object
738 * @name: unused
739 *
740 * Returns 0 if access is permitted, an error code otherwise
741 */
David Howells8f0cfa52008-04-29 00:59:41 -0700742static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800743{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200744 struct smk_audit_info ad;
745
746 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
747 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
748
749 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800750}
751
752/*
753 * smack_inode_removexattr - Smack check on removexattr
754 * @dentry: the object
755 * @name: name of the attribute
756 *
757 * Removing the Smack attribute requires CAP_MAC_ADMIN
758 *
759 * Returns 0 if access is permitted, an error code otherwise
760 */
David Howells8f0cfa52008-04-29 00:59:41 -0700761static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800762{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200763 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800764 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800765
Casey Schauflerbcdca222008-02-23 15:24:04 -0800766 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
767 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
768 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
769 if (!capable(CAP_MAC_ADMIN))
770 rc = -EPERM;
771 } else
772 rc = cap_inode_removexattr(dentry, name);
773
Etienne Bassetecfcc532009-04-08 20:40:06 +0200774 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
775 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800776 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200777 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800778
779 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800780}
781
782/**
783 * smack_inode_getsecurity - get smack xattrs
784 * @inode: the object
785 * @name: attribute name
786 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800787 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800788 *
789 * Returns the size of the attribute or an error code
790 */
791static int smack_inode_getsecurity(const struct inode *inode,
792 const char *name, void **buffer,
793 bool alloc)
794{
795 struct socket_smack *ssp;
796 struct socket *sock;
797 struct super_block *sbp;
798 struct inode *ip = (struct inode *)inode;
799 char *isp;
800 int ilen;
801 int rc = 0;
802
803 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
804 isp = smk_of_inode(inode);
805 ilen = strlen(isp) + 1;
806 *buffer = isp;
807 return ilen;
808 }
809
810 /*
811 * The rest of the Smack xattrs are only on sockets.
812 */
813 sbp = ip->i_sb;
814 if (sbp->s_magic != SOCKFS_MAGIC)
815 return -EOPNOTSUPP;
816
817 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800818 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800819 return -EOPNOTSUPP;
820
821 ssp = sock->sk->sk_security;
822
823 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
824 isp = ssp->smk_in;
825 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
826 isp = ssp->smk_out;
827 else
828 return -EOPNOTSUPP;
829
830 ilen = strlen(isp) + 1;
831 if (rc == 0) {
832 *buffer = isp;
833 rc = ilen;
834 }
835
836 return rc;
837}
838
839
840/**
841 * smack_inode_listsecurity - list the Smack attributes
842 * @inode: the object
843 * @buffer: where they go
844 * @buffer_size: size of buffer
845 *
846 * Returns 0 on success, -EINVAL otherwise
847 */
848static int smack_inode_listsecurity(struct inode *inode, char *buffer,
849 size_t buffer_size)
850{
851 int len = strlen(XATTR_NAME_SMACK);
852
853 if (buffer != NULL && len <= buffer_size) {
854 memcpy(buffer, XATTR_NAME_SMACK, len);
855 return len;
856 }
857 return -EINVAL;
858}
859
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +1000860/**
861 * smack_inode_getsecid - Extract inode's security id
862 * @inode: inode to extract the info from
863 * @secid: where result will be saved
864 */
865static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
866{
867 struct inode_smack *isp = inode->i_security;
868
869 *secid = smack_to_secid(isp->smk_inode);
870}
871
Casey Schauflere114e472008-02-04 22:29:50 -0800872/*
873 * File Hooks
874 */
875
876/**
877 * smack_file_permission - Smack check on file operations
878 * @file: unused
879 * @mask: unused
880 *
881 * Returns 0
882 *
883 * Should access checks be done on each read or write?
884 * UNICOS and SELinux say yes.
885 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
886 *
887 * I'll say no for now. Smack does not do the frequent
888 * label changing that SELinux does.
889 */
890static int smack_file_permission(struct file *file, int mask)
891{
892 return 0;
893}
894
895/**
896 * smack_file_alloc_security - assign a file security blob
897 * @file: the object
898 *
899 * The security blob for a file is a pointer to the master
900 * label list, so no allocation is done.
901 *
902 * Returns 0
903 */
904static int smack_file_alloc_security(struct file *file)
905{
David Howells86a264a2008-11-14 10:39:18 +1100906 file->f_security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800907 return 0;
908}
909
910/**
911 * smack_file_free_security - clear a file security blob
912 * @file: the object
913 *
914 * The security blob for a file is a pointer to the master
915 * label list, so no memory is freed.
916 */
917static void smack_file_free_security(struct file *file)
918{
919 file->f_security = NULL;
920}
921
922/**
923 * smack_file_ioctl - Smack check on ioctls
924 * @file: the object
925 * @cmd: what to do
926 * @arg: unused
927 *
928 * Relies heavily on the correct use of the ioctl command conventions.
929 *
930 * Returns 0 if allowed, error code otherwise
931 */
932static int smack_file_ioctl(struct file *file, unsigned int cmd,
933 unsigned long arg)
934{
935 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200936 struct smk_audit_info ad;
937
938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
939 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -0800940
941 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200942 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800943
944 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200945 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800946
947 return rc;
948}
949
950/**
951 * smack_file_lock - Smack check on file locking
952 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -0800953 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800954 *
955 * Returns 0 if current has write access, error code otherwise
956 */
957static int smack_file_lock(struct file *file, unsigned int cmd)
958{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200959 struct smk_audit_info ad;
960
961 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
962 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
963 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800964}
965
966/**
967 * smack_file_fcntl - Smack check on fcntl
968 * @file: the object
969 * @cmd: what action to check
970 * @arg: unused
971 *
972 * Returns 0 if current has access, error code otherwise
973 */
974static int smack_file_fcntl(struct file *file, unsigned int cmd,
975 unsigned long arg)
976{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200977 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800978 int rc;
979
Etienne Bassetecfcc532009-04-08 20:40:06 +0200980 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
981 smk_ad_setfield_u_fs_path(&ad, file->f_path);
982
Casey Schauflere114e472008-02-04 22:29:50 -0800983 switch (cmd) {
984 case F_DUPFD:
985 case F_GETFD:
986 case F_GETFL:
987 case F_GETLK:
988 case F_GETOWN:
989 case F_GETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +0200990 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800991 break;
992 case F_SETFD:
993 case F_SETFL:
994 case F_SETLK:
995 case F_SETLKW:
996 case F_SETOWN:
997 case F_SETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +0200998 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800999 break;
1000 default:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001001 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001002 }
1003
1004 return rc;
1005}
1006
1007/**
1008 * smack_file_set_fowner - set the file security blob value
1009 * @file: object in question
1010 *
1011 * Returns 0
1012 * Further research may be required on this one.
1013 */
1014static int smack_file_set_fowner(struct file *file)
1015{
David Howells86a264a2008-11-14 10:39:18 +11001016 file->f_security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001017 return 0;
1018}
1019
1020/**
1021 * smack_file_send_sigiotask - Smack on sigio
1022 * @tsk: The target task
1023 * @fown: the object the signal come from
1024 * @signum: unused
1025 *
1026 * Allow a privileged task to get signals even if it shouldn't
1027 *
1028 * Returns 0 if a subject with the object's smack could
1029 * write to the task, an error code otherwise.
1030 */
1031static int smack_file_send_sigiotask(struct task_struct *tsk,
1032 struct fown_struct *fown, int signum)
1033{
1034 struct file *file;
1035 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001036 char *tsp = tsk->cred->security;
1037 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001038
1039 /*
1040 * struct fown_struct is never outside the context of a struct file
1041 */
1042 file = container_of(fown, struct file, f_owner);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001043 /* we don't log here as rc can be overriden */
1044 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001045 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001046 rc = 0;
1047
1048 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1049 smk_ad_setfield_u_tsk(&ad, tsk);
1050 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001051 return rc;
1052}
1053
1054/**
1055 * smack_file_receive - Smack file receive check
1056 * @file: the object
1057 *
1058 * Returns 0 if current has access, error code otherwise
1059 */
1060static int smack_file_receive(struct file *file)
1061{
1062 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001063 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001064
Etienne Bassetecfcc532009-04-08 20:40:06 +02001065 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1066 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001067 /*
1068 * This code relies on bitmasks.
1069 */
1070 if (file->f_mode & FMODE_READ)
1071 may = MAY_READ;
1072 if (file->f_mode & FMODE_WRITE)
1073 may |= MAY_WRITE;
1074
Etienne Bassetecfcc532009-04-08 20:40:06 +02001075 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001076}
1077
1078/*
1079 * Task hooks
1080 */
1081
1082/**
David Howellsf1752ee2008-11-14 10:39:17 +11001083 * smack_cred_free - "free" task-level security credentials
1084 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001085 *
1086 * Smack isn't using copies of blobs. Everyone
1087 * points to an immutable list. The blobs never go away.
1088 * There is no leak here.
1089 */
David Howellsf1752ee2008-11-14 10:39:17 +11001090static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001091{
David Howellsf1752ee2008-11-14 10:39:17 +11001092 cred->security = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001093}
1094
1095/**
David Howellsd84f4f92008-11-14 10:39:23 +11001096 * smack_cred_prepare - prepare new set of credentials for modification
1097 * @new: the new credentials
1098 * @old: the original credentials
1099 * @gfp: the atomicity of any memory allocations
1100 *
1101 * Prepare a new set of credentials for modification.
1102 */
1103static int smack_cred_prepare(struct cred *new, const struct cred *old,
1104 gfp_t gfp)
1105{
1106 new->security = old->security;
1107 return 0;
1108}
1109
Randy Dunlap251a2a92009-02-18 11:42:33 -08001110/**
1111 * smack_cred_commit - commit new credentials
David Howellsd84f4f92008-11-14 10:39:23 +11001112 * @new: the new credentials
1113 * @old: the original credentials
1114 */
1115static void smack_cred_commit(struct cred *new, const struct cred *old)
1116{
1117}
1118
1119/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001120 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001121 * @new: points to the set of credentials to be modified.
1122 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001123 *
1124 * Set the security data for a kernel service.
1125 */
1126static int smack_kernel_act_as(struct cred *new, u32 secid)
1127{
1128 char *smack = smack_from_secid(secid);
1129
1130 if (smack == NULL)
1131 return -EINVAL;
1132
1133 new->security = smack;
1134 return 0;
1135}
1136
1137/**
1138 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001139 * @new: points to the set of credentials to be modified
1140 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001141 *
1142 * Set the file creation context in a set of credentials to the same
1143 * as the objective context of the specified inode
1144 */
1145static int smack_kernel_create_files_as(struct cred *new,
1146 struct inode *inode)
1147{
1148 struct inode_smack *isp = inode->i_security;
1149
1150 new->security = isp->smk_inode;
1151 return 0;
1152}
1153
1154/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001155 * smk_curacc_on_task - helper to log task related access
1156 * @p: the task object
1157 * @access : the access requested
1158 *
1159 * Return 0 if access is permitted
1160 */
1161static int smk_curacc_on_task(struct task_struct *p, int access)
1162{
1163 struct smk_audit_info ad;
1164
1165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1166 smk_ad_setfield_u_tsk(&ad, p);
1167 return smk_curacc(task_security(p), access, &ad);
1168}
1169
1170/**
Casey Schauflere114e472008-02-04 22:29:50 -08001171 * smack_task_setpgid - Smack check on setting pgid
1172 * @p: the task object
1173 * @pgid: unused
1174 *
1175 * Return 0 if write access is permitted
1176 */
1177static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1178{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001179 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001180}
1181
1182/**
1183 * smack_task_getpgid - Smack access check for getpgid
1184 * @p: the object task
1185 *
1186 * Returns 0 if current can read the object task, error code otherwise
1187 */
1188static int smack_task_getpgid(struct task_struct *p)
1189{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001190 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001191}
1192
1193/**
1194 * smack_task_getsid - Smack access check for getsid
1195 * @p: the object task
1196 *
1197 * Returns 0 if current can read the object task, error code otherwise
1198 */
1199static int smack_task_getsid(struct task_struct *p)
1200{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001201 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001202}
1203
1204/**
1205 * smack_task_getsecid - get the secid of the task
1206 * @p: the object task
1207 * @secid: where to put the result
1208 *
1209 * Sets the secid to contain a u32 version of the smack label.
1210 */
1211static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1212{
David Howellsc69e8d92008-11-14 10:39:19 +11001213 *secid = smack_to_secid(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001214}
1215
1216/**
1217 * smack_task_setnice - Smack check on setting nice
1218 * @p: the task object
1219 * @nice: unused
1220 *
1221 * Return 0 if write access is permitted
1222 */
1223static int smack_task_setnice(struct task_struct *p, int nice)
1224{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001225 int rc;
1226
1227 rc = cap_task_setnice(p, nice);
1228 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001229 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001230 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001231}
1232
1233/**
1234 * smack_task_setioprio - Smack check on setting ioprio
1235 * @p: the task object
1236 * @ioprio: unused
1237 *
1238 * Return 0 if write access is permitted
1239 */
1240static int smack_task_setioprio(struct task_struct *p, int ioprio)
1241{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001242 int rc;
1243
1244 rc = cap_task_setioprio(p, ioprio);
1245 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001246 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001247 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001248}
1249
1250/**
1251 * smack_task_getioprio - Smack check on reading ioprio
1252 * @p: the task object
1253 *
1254 * Return 0 if read access is permitted
1255 */
1256static int smack_task_getioprio(struct task_struct *p)
1257{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001258 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001259}
1260
1261/**
1262 * smack_task_setscheduler - Smack check on setting scheduler
1263 * @p: the task object
1264 * @policy: unused
1265 * @lp: unused
1266 *
1267 * Return 0 if read access is permitted
1268 */
1269static int smack_task_setscheduler(struct task_struct *p, int policy,
1270 struct sched_param *lp)
1271{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001272 int rc;
1273
1274 rc = cap_task_setscheduler(p, policy, lp);
1275 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001276 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001277 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001278}
1279
1280/**
1281 * smack_task_getscheduler - Smack check on reading scheduler
1282 * @p: the task object
1283 *
1284 * Return 0 if read access is permitted
1285 */
1286static int smack_task_getscheduler(struct task_struct *p)
1287{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001288 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001289}
1290
1291/**
1292 * smack_task_movememory - Smack check on moving memory
1293 * @p: the task object
1294 *
1295 * Return 0 if write access is permitted
1296 */
1297static int smack_task_movememory(struct task_struct *p)
1298{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001299 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001300}
1301
1302/**
1303 * smack_task_kill - Smack check on signal delivery
1304 * @p: the task object
1305 * @info: unused
1306 * @sig: unused
1307 * @secid: identifies the smack to use in lieu of current's
1308 *
1309 * Return 0 if write access is permitted
1310 *
1311 * The secid behavior is an artifact of an SELinux hack
1312 * in the USB code. Someday it may go away.
1313 */
1314static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1315 int sig, u32 secid)
1316{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001317 struct smk_audit_info ad;
1318
1319 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1320 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001321 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001322 * Sending a signal requires that the sender
1323 * can write the receiver.
1324 */
1325 if (secid == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001326 return smk_curacc(task_security(p), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001327 /*
1328 * If the secid isn't 0 we're dealing with some USB IO
1329 * specific behavior. This is not clean. For one thing
1330 * we can't take privilege into account.
1331 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001332 return smk_access(smack_from_secid(secid), task_security(p),
1333 MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001334}
1335
1336/**
1337 * smack_task_wait - Smack access check for waiting
1338 * @p: task to wait for
1339 *
1340 * Returns 0 if current can wait for p, error code otherwise
1341 */
1342static int smack_task_wait(struct task_struct *p)
1343{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001344 struct smk_audit_info ad;
1345 char *sp = current_security();
1346 char *tsp = task_security(p);
Casey Schauflere114e472008-02-04 22:29:50 -08001347 int rc;
1348
Etienne Bassetecfcc532009-04-08 20:40:06 +02001349 /* we don't log here, we can be overriden */
1350 rc = smk_access(sp, tsp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001351 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001352 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001353
1354 /*
1355 * Allow the operation to succeed if either task
1356 * has privilege to perform operations that might
1357 * account for the smack labels having gotten to
1358 * be different in the first place.
1359 *
David Howells5cd9c582008-08-14 11:37:28 +01001360 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001361 * control ideal, taking the object's privilege
1362 * state into account in the decision as well as
1363 * the smack value.
1364 */
David Howells5cd9c582008-08-14 11:37:28 +01001365 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001366 rc = 0;
1367 /* we log only if we didn't get overriden */
1368 out_log:
1369 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1370 smk_ad_setfield_u_tsk(&ad, p);
1371 smack_log(sp, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001372 return rc;
1373}
1374
1375/**
1376 * smack_task_to_inode - copy task smack into the inode blob
1377 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001378 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001379 *
1380 * Sets the smack pointer in the inode security blob
1381 */
1382static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1383{
1384 struct inode_smack *isp = inode->i_security;
David Howellsc69e8d92008-11-14 10:39:19 +11001385 isp->smk_inode = task_security(p);
Casey Schauflere114e472008-02-04 22:29:50 -08001386}
1387
1388/*
1389 * Socket hooks.
1390 */
1391
1392/**
1393 * smack_sk_alloc_security - Allocate a socket blob
1394 * @sk: the socket
1395 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001396 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001397 *
1398 * Assign Smack pointers to current
1399 *
1400 * Returns 0 on success, -ENOMEM is there's no memory
1401 */
1402static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1403{
David Howells86a264a2008-11-14 10:39:18 +11001404 char *csp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001405 struct socket_smack *ssp;
1406
1407 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1408 if (ssp == NULL)
1409 return -ENOMEM;
1410
1411 ssp->smk_in = csp;
1412 ssp->smk_out = csp;
1413 ssp->smk_packet[0] = '\0';
1414
1415 sk->sk_security = ssp;
1416
1417 return 0;
1418}
1419
1420/**
1421 * smack_sk_free_security - Free a socket blob
1422 * @sk: the socket
1423 *
1424 * Clears the blob pointer
1425 */
1426static void smack_sk_free_security(struct sock *sk)
1427{
1428 kfree(sk->sk_security);
1429}
1430
1431/**
Paul Moore07feee82009-03-27 17:10:54 -04001432* smack_host_label - check host based restrictions
1433* @sip: the object end
1434*
1435* looks for host based access restrictions
1436*
1437* This version will only be appropriate for really small sets of single label
1438* hosts. The caller is responsible for ensuring that the RCU read lock is
1439* taken before calling this function.
1440*
1441* Returns the label of the far end or NULL if it's not special.
1442*/
1443static char *smack_host_label(struct sockaddr_in *sip)
1444{
1445 struct smk_netlbladdr *snp;
1446 struct in_addr *siap = &sip->sin_addr;
1447
1448 if (siap->s_addr == 0)
1449 return NULL;
1450
1451 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1452 /*
1453 * we break after finding the first match because
1454 * the list is sorted from longest to shortest mask
1455 * so we have found the most specific match
1456 */
1457 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001458 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1459 /* we have found the special CIPSO option */
1460 if (snp->smk_label == smack_cipso_option)
1461 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001462 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001463 }
Paul Moore07feee82009-03-27 17:10:54 -04001464
1465 return NULL;
1466}
1467
1468/**
Casey Schauflere114e472008-02-04 22:29:50 -08001469 * smack_set_catset - convert a capset to netlabel mls categories
1470 * @catset: the Smack categories
1471 * @sap: where to put the netlabel categories
1472 *
1473 * Allocates and fills attr.mls.cat
1474 */
1475static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1476{
1477 unsigned char *cp;
1478 unsigned char m;
1479 int cat;
1480 int rc;
1481 int byte;
1482
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001483 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001484 return;
1485
1486 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1487 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1488 sap->attr.mls.cat->startbit = 0;
1489
1490 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1491 for (m = 0x80; m != 0; m >>= 1, cat++) {
1492 if ((m & *cp) == 0)
1493 continue;
1494 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1495 cat, GFP_ATOMIC);
1496 }
1497}
1498
1499/**
1500 * smack_to_secattr - fill a secattr from a smack value
1501 * @smack: the smack value
1502 * @nlsp: where the result goes
1503 *
1504 * Casey says that CIPSO is good enough for now.
1505 * It can be used to effect.
1506 * It can also be abused to effect when necessary.
1507 * Appologies to the TSIG group in general and GW in particular.
1508 */
1509static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1510{
1511 struct smack_cipso cipso;
1512 int rc;
1513
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001514 nlsp->domain = smack;
1515 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001516
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001517 rc = smack_to_cipso(smack, &cipso);
1518 if (rc == 0) {
1519 nlsp->attr.mls.lvl = cipso.smk_level;
1520 smack_set_catset(cipso.smk_catset, nlsp);
1521 } else {
1522 nlsp->attr.mls.lvl = smack_cipso_direct;
1523 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001524 }
1525}
1526
1527/**
1528 * smack_netlabel - Set the secattr on a socket
1529 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001530 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001531 *
1532 * Convert the outbound smack value (smk_out) to a
1533 * secattr and attach it to the socket.
1534 *
1535 * Returns 0 on success or an error code
1536 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001537static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001538{
Paul Moore07feee82009-03-27 17:10:54 -04001539 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001540 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001541 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001542
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001543 /*
1544 * Usually the netlabel code will handle changing the
1545 * packet labeling based on the label.
1546 * The case of a single label host is different, because
1547 * a single label host should never get a labeled packet
1548 * even though the label is usually associated with a packet
1549 * label.
1550 */
1551 local_bh_disable();
1552 bh_lock_sock_nested(sk);
1553
1554 if (ssp->smk_out == smack_net_ambient ||
1555 labeled == SMACK_UNLABELED_SOCKET)
1556 netlbl_sock_delattr(sk);
1557 else {
1558 netlbl_secattr_init(&secattr);
1559 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001560 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001561 netlbl_secattr_destroy(&secattr);
1562 }
1563
1564 bh_unlock_sock(sk);
1565 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001566
Casey Schauflere114e472008-02-04 22:29:50 -08001567 return rc;
1568}
1569
1570/**
Paul Moore07feee82009-03-27 17:10:54 -04001571 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1572 * @sk: the socket
1573 * @sap: the destination address
1574 *
1575 * Set the correct secattr for the given socket based on the destination
1576 * address and perform any outbound access checks needed.
1577 *
1578 * Returns 0 on success or an error code.
1579 *
1580 */
1581static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1582{
1583 int rc;
1584 int sk_lbl;
1585 char *hostsp;
1586 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001587 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001588
1589 rcu_read_lock();
1590 hostsp = smack_host_label(sap);
1591 if (hostsp != NULL) {
1592 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001593#ifdef CONFIG_AUDIT
1594 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1595 ad.a.u.net.family = sap->sin_family;
1596 ad.a.u.net.dport = sap->sin_port;
1597 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1598#endif
1599 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001600 } else {
1601 sk_lbl = SMACK_CIPSO_SOCKET;
1602 rc = 0;
1603 }
1604 rcu_read_unlock();
1605 if (rc != 0)
1606 return rc;
1607
1608 return smack_netlabel(sk, sk_lbl);
1609}
1610
1611/**
Casey Schauflere114e472008-02-04 22:29:50 -08001612 * smack_inode_setsecurity - set smack xattrs
1613 * @inode: the object
1614 * @name: attribute name
1615 * @value: attribute value
1616 * @size: size of the attribute
1617 * @flags: unused
1618 *
1619 * Sets the named attribute in the appropriate blob
1620 *
1621 * Returns 0 on success, or an error code
1622 */
1623static int smack_inode_setsecurity(struct inode *inode, const char *name,
1624 const void *value, size_t size, int flags)
1625{
1626 char *sp;
1627 struct inode_smack *nsp = inode->i_security;
1628 struct socket_smack *ssp;
1629 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001630 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001631
Etienne Basset43031542009-03-27 17:11:01 -04001632 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001633 return -EACCES;
1634
1635 sp = smk_import(value, size);
1636 if (sp == NULL)
1637 return -EINVAL;
1638
1639 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1640 nsp->smk_inode = sp;
1641 return 0;
1642 }
1643 /*
1644 * The rest of the Smack xattrs are only on sockets.
1645 */
1646 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1647 return -EOPNOTSUPP;
1648
1649 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001650 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001651 return -EOPNOTSUPP;
1652
1653 ssp = sock->sk->sk_security;
1654
1655 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1656 ssp->smk_in = sp;
1657 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1658 ssp->smk_out = sp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001659 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001660 if (rc != 0)
1661 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1662 __func__, -rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001663 } else
1664 return -EOPNOTSUPP;
1665
1666 return 0;
1667}
1668
1669/**
1670 * smack_socket_post_create - finish socket setup
1671 * @sock: the socket
1672 * @family: protocol family
1673 * @type: unused
1674 * @protocol: unused
1675 * @kern: unused
1676 *
1677 * Sets the netlabel information on the socket
1678 *
1679 * Returns 0 on success, and error code otherwise
1680 */
1681static int smack_socket_post_create(struct socket *sock, int family,
1682 int type, int protocol, int kern)
1683{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001684 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001685 return 0;
1686 /*
1687 * Set the outbound netlbl.
1688 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001689 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1690}
1691
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001692/**
1693 * smack_socket_connect - connect access check
1694 * @sock: the socket
1695 * @sap: the other end
1696 * @addrlen: size of sap
1697 *
1698 * Verifies that a connection may be possible
1699 *
1700 * Returns 0 on success, and error code otherwise
1701 */
1702static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1703 int addrlen)
1704{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001705 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1706 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001707 if (addrlen < sizeof(struct sockaddr_in))
1708 return -EINVAL;
1709
Paul Moore07feee82009-03-27 17:10:54 -04001710 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08001711}
1712
1713/**
1714 * smack_flags_to_may - convert S_ to MAY_ values
1715 * @flags: the S_ value
1716 *
1717 * Returns the equivalent MAY_ value
1718 */
1719static int smack_flags_to_may(int flags)
1720{
1721 int may = 0;
1722
1723 if (flags & S_IRUGO)
1724 may |= MAY_READ;
1725 if (flags & S_IWUGO)
1726 may |= MAY_WRITE;
1727 if (flags & S_IXUGO)
1728 may |= MAY_EXEC;
1729
1730 return may;
1731}
1732
1733/**
1734 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1735 * @msg: the object
1736 *
1737 * Returns 0
1738 */
1739static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1740{
David Howells86a264a2008-11-14 10:39:18 +11001741 msg->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001742 return 0;
1743}
1744
1745/**
1746 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1747 * @msg: the object
1748 *
1749 * Clears the blob pointer
1750 */
1751static void smack_msg_msg_free_security(struct msg_msg *msg)
1752{
1753 msg->security = NULL;
1754}
1755
1756/**
1757 * smack_of_shm - the smack pointer for the shm
1758 * @shp: the object
1759 *
1760 * Returns a pointer to the smack value
1761 */
1762static char *smack_of_shm(struct shmid_kernel *shp)
1763{
1764 return (char *)shp->shm_perm.security;
1765}
1766
1767/**
1768 * smack_shm_alloc_security - Set the security blob for shm
1769 * @shp: the object
1770 *
1771 * Returns 0
1772 */
1773static int smack_shm_alloc_security(struct shmid_kernel *shp)
1774{
1775 struct kern_ipc_perm *isp = &shp->shm_perm;
1776
David Howells86a264a2008-11-14 10:39:18 +11001777 isp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001778 return 0;
1779}
1780
1781/**
1782 * smack_shm_free_security - Clear the security blob for shm
1783 * @shp: the object
1784 *
1785 * Clears the blob pointer
1786 */
1787static void smack_shm_free_security(struct shmid_kernel *shp)
1788{
1789 struct kern_ipc_perm *isp = &shp->shm_perm;
1790
1791 isp->security = NULL;
1792}
1793
1794/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001795 * smk_curacc_shm : check if current has access on shm
1796 * @shp : the object
1797 * @access : access requested
1798 *
1799 * Returns 0 if current has the requested access, error code otherwise
1800 */
1801static int smk_curacc_shm(struct shmid_kernel *shp, int access)
1802{
1803 char *ssp = smack_of_shm(shp);
1804 struct smk_audit_info ad;
1805
1806#ifdef CONFIG_AUDIT
1807 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1808 ad.a.u.ipc_id = shp->shm_perm.id;
1809#endif
1810 return smk_curacc(ssp, access, &ad);
1811}
1812
1813/**
Casey Schauflere114e472008-02-04 22:29:50 -08001814 * smack_shm_associate - Smack access check for shm
1815 * @shp: the object
1816 * @shmflg: access requested
1817 *
1818 * Returns 0 if current has the requested access, error code otherwise
1819 */
1820static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1821{
Casey Schauflere114e472008-02-04 22:29:50 -08001822 int may;
1823
1824 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001825 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001826}
1827
1828/**
1829 * smack_shm_shmctl - Smack access check for shm
1830 * @shp: the object
1831 * @cmd: what it wants to do
1832 *
1833 * Returns 0 if current has the requested access, error code otherwise
1834 */
1835static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1836{
Casey Schauflere114e472008-02-04 22:29:50 -08001837 int may;
1838
1839 switch (cmd) {
1840 case IPC_STAT:
1841 case SHM_STAT:
1842 may = MAY_READ;
1843 break;
1844 case IPC_SET:
1845 case SHM_LOCK:
1846 case SHM_UNLOCK:
1847 case IPC_RMID:
1848 may = MAY_READWRITE;
1849 break;
1850 case IPC_INFO:
1851 case SHM_INFO:
1852 /*
1853 * System level information.
1854 */
1855 return 0;
1856 default:
1857 return -EINVAL;
1858 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02001859 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001860}
1861
1862/**
1863 * smack_shm_shmat - Smack access for shmat
1864 * @shp: the object
1865 * @shmaddr: unused
1866 * @shmflg: access requested
1867 *
1868 * Returns 0 if current has the requested access, error code otherwise
1869 */
1870static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1871 int shmflg)
1872{
Casey Schauflere114e472008-02-04 22:29:50 -08001873 int may;
1874
1875 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001876 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001877}
1878
1879/**
1880 * smack_of_sem - the smack pointer for the sem
1881 * @sma: the object
1882 *
1883 * Returns a pointer to the smack value
1884 */
1885static char *smack_of_sem(struct sem_array *sma)
1886{
1887 return (char *)sma->sem_perm.security;
1888}
1889
1890/**
1891 * smack_sem_alloc_security - Set the security blob for sem
1892 * @sma: the object
1893 *
1894 * Returns 0
1895 */
1896static int smack_sem_alloc_security(struct sem_array *sma)
1897{
1898 struct kern_ipc_perm *isp = &sma->sem_perm;
1899
David Howells86a264a2008-11-14 10:39:18 +11001900 isp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001901 return 0;
1902}
1903
1904/**
1905 * smack_sem_free_security - Clear the security blob for sem
1906 * @sma: the object
1907 *
1908 * Clears the blob pointer
1909 */
1910static void smack_sem_free_security(struct sem_array *sma)
1911{
1912 struct kern_ipc_perm *isp = &sma->sem_perm;
1913
1914 isp->security = NULL;
1915}
1916
1917/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001918 * smk_curacc_sem : check if current has access on sem
1919 * @sma : the object
1920 * @access : access requested
1921 *
1922 * Returns 0 if current has the requested access, error code otherwise
1923 */
1924static int smk_curacc_sem(struct sem_array *sma, int access)
1925{
1926 char *ssp = smack_of_sem(sma);
1927 struct smk_audit_info ad;
1928
1929#ifdef CONFIG_AUDIT
1930 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1931 ad.a.u.ipc_id = sma->sem_perm.id;
1932#endif
1933 return smk_curacc(ssp, access, &ad);
1934}
1935
1936/**
Casey Schauflere114e472008-02-04 22:29:50 -08001937 * smack_sem_associate - Smack access check for sem
1938 * @sma: the object
1939 * @semflg: access requested
1940 *
1941 * Returns 0 if current has the requested access, error code otherwise
1942 */
1943static int smack_sem_associate(struct sem_array *sma, int semflg)
1944{
Casey Schauflere114e472008-02-04 22:29:50 -08001945 int may;
1946
1947 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001948 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001949}
1950
1951/**
1952 * smack_sem_shmctl - Smack access check for sem
1953 * @sma: the object
1954 * @cmd: what it wants to do
1955 *
1956 * Returns 0 if current has the requested access, error code otherwise
1957 */
1958static int smack_sem_semctl(struct sem_array *sma, int cmd)
1959{
Casey Schauflere114e472008-02-04 22:29:50 -08001960 int may;
1961
1962 switch (cmd) {
1963 case GETPID:
1964 case GETNCNT:
1965 case GETZCNT:
1966 case GETVAL:
1967 case GETALL:
1968 case IPC_STAT:
1969 case SEM_STAT:
1970 may = MAY_READ;
1971 break;
1972 case SETVAL:
1973 case SETALL:
1974 case IPC_RMID:
1975 case IPC_SET:
1976 may = MAY_READWRITE;
1977 break;
1978 case IPC_INFO:
1979 case SEM_INFO:
1980 /*
1981 * System level information
1982 */
1983 return 0;
1984 default:
1985 return -EINVAL;
1986 }
1987
Etienne Bassetecfcc532009-04-08 20:40:06 +02001988 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001989}
1990
1991/**
1992 * smack_sem_semop - Smack checks of semaphore operations
1993 * @sma: the object
1994 * @sops: unused
1995 * @nsops: unused
1996 * @alter: unused
1997 *
1998 * Treated as read and write in all cases.
1999 *
2000 * Returns 0 if access is allowed, error code otherwise
2001 */
2002static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2003 unsigned nsops, int alter)
2004{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002005 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002006}
2007
2008/**
2009 * smack_msg_alloc_security - Set the security blob for msg
2010 * @msq: the object
2011 *
2012 * Returns 0
2013 */
2014static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2015{
2016 struct kern_ipc_perm *kisp = &msq->q_perm;
2017
David Howells86a264a2008-11-14 10:39:18 +11002018 kisp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08002019 return 0;
2020}
2021
2022/**
2023 * smack_msg_free_security - Clear the security blob for msg
2024 * @msq: the object
2025 *
2026 * Clears the blob pointer
2027 */
2028static void smack_msg_queue_free_security(struct msg_queue *msq)
2029{
2030 struct kern_ipc_perm *kisp = &msq->q_perm;
2031
2032 kisp->security = NULL;
2033}
2034
2035/**
2036 * smack_of_msq - the smack pointer for the msq
2037 * @msq: the object
2038 *
2039 * Returns a pointer to the smack value
2040 */
2041static char *smack_of_msq(struct msg_queue *msq)
2042{
2043 return (char *)msq->q_perm.security;
2044}
2045
2046/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002047 * smk_curacc_msq : helper to check if current has access on msq
2048 * @msq : the msq
2049 * @access : access requested
2050 *
2051 * return 0 if current has access, error otherwise
2052 */
2053static int smk_curacc_msq(struct msg_queue *msq, int access)
2054{
2055 char *msp = smack_of_msq(msq);
2056 struct smk_audit_info ad;
2057
2058#ifdef CONFIG_AUDIT
2059 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2060 ad.a.u.ipc_id = msq->q_perm.id;
2061#endif
2062 return smk_curacc(msp, access, &ad);
2063}
2064
2065/**
Casey Schauflere114e472008-02-04 22:29:50 -08002066 * smack_msg_queue_associate - Smack access check for msg_queue
2067 * @msq: the object
2068 * @msqflg: access requested
2069 *
2070 * Returns 0 if current has the requested access, error code otherwise
2071 */
2072static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2073{
Casey Schauflere114e472008-02-04 22:29:50 -08002074 int may;
2075
2076 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002077 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002078}
2079
2080/**
2081 * smack_msg_queue_msgctl - Smack access check for msg_queue
2082 * @msq: the object
2083 * @cmd: what it wants to do
2084 *
2085 * Returns 0 if current has the requested access, error code otherwise
2086 */
2087static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2088{
Casey Schauflere114e472008-02-04 22:29:50 -08002089 int may;
2090
2091 switch (cmd) {
2092 case IPC_STAT:
2093 case MSG_STAT:
2094 may = MAY_READ;
2095 break;
2096 case IPC_SET:
2097 case IPC_RMID:
2098 may = MAY_READWRITE;
2099 break;
2100 case IPC_INFO:
2101 case MSG_INFO:
2102 /*
2103 * System level information
2104 */
2105 return 0;
2106 default:
2107 return -EINVAL;
2108 }
2109
Etienne Bassetecfcc532009-04-08 20:40:06 +02002110 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002111}
2112
2113/**
2114 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2115 * @msq: the object
2116 * @msg: unused
2117 * @msqflg: access requested
2118 *
2119 * Returns 0 if current has the requested access, error code otherwise
2120 */
2121static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2122 int msqflg)
2123{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002124 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002125
Etienne Bassetecfcc532009-04-08 20:40:06 +02002126 may = smack_flags_to_may(msqflg);
2127 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002128}
2129
2130/**
2131 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2132 * @msq: the object
2133 * @msg: unused
2134 * @target: unused
2135 * @type: unused
2136 * @mode: unused
2137 *
2138 * Returns 0 if current has read and write access, error code otherwise
2139 */
2140static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2141 struct task_struct *target, long type, int mode)
2142{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002143 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002144}
2145
2146/**
2147 * smack_ipc_permission - Smack access for ipc_permission()
2148 * @ipp: the object permissions
2149 * @flag: access requested
2150 *
2151 * Returns 0 if current has read and write access, error code otherwise
2152 */
2153static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2154{
2155 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002156 int may = smack_flags_to_may(flag);
2157 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002158
Etienne Bassetecfcc532009-04-08 20:40:06 +02002159#ifdef CONFIG_AUDIT
2160 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2161 ad.a.u.ipc_id = ipp->id;
2162#endif
2163 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002164}
2165
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002166/**
2167 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002168 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002169 * @secid: where result will be saved
2170 */
2171static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2172{
2173 char *smack = ipp->security;
2174
2175 *secid = smack_to_secid(smack);
2176}
2177
Casey Schauflere114e472008-02-04 22:29:50 -08002178/**
2179 * smack_d_instantiate - Make sure the blob is correct on an inode
2180 * @opt_dentry: unused
2181 * @inode: the object
2182 *
2183 * Set the inode's security blob if it hasn't been done already.
2184 */
2185static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2186{
2187 struct super_block *sbp;
2188 struct superblock_smack *sbsp;
2189 struct inode_smack *isp;
David Howells86a264a2008-11-14 10:39:18 +11002190 char *csp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08002191 char *fetched;
2192 char *final;
2193 struct dentry *dp;
2194
2195 if (inode == NULL)
2196 return;
2197
2198 isp = inode->i_security;
2199
2200 mutex_lock(&isp->smk_lock);
2201 /*
2202 * If the inode is already instantiated
2203 * take the quick way out
2204 */
2205 if (isp->smk_flags & SMK_INODE_INSTANT)
2206 goto unlockandout;
2207
2208 sbp = inode->i_sb;
2209 sbsp = sbp->s_security;
2210 /*
2211 * We're going to use the superblock default label
2212 * if there's no label on the file.
2213 */
2214 final = sbsp->smk_default;
2215
2216 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002217 * If this is the root inode the superblock
2218 * may be in the process of initialization.
2219 * If that is the case use the root value out
2220 * of the superblock.
2221 */
2222 if (opt_dentry->d_parent == opt_dentry) {
2223 isp->smk_inode = sbsp->smk_root;
2224 isp->smk_flags |= SMK_INODE_INSTANT;
2225 goto unlockandout;
2226 }
2227
2228 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002229 * This is pretty hackish.
2230 * Casey says that we shouldn't have to do
2231 * file system specific code, but it does help
2232 * with keeping it simple.
2233 */
2234 switch (sbp->s_magic) {
2235 case SMACK_MAGIC:
2236 /*
2237 * Casey says that it's a little embarassing
2238 * that the smack file system doesn't do
2239 * extended attributes.
2240 */
2241 final = smack_known_star.smk_known;
2242 break;
2243 case PIPEFS_MAGIC:
2244 /*
2245 * Casey says pipes are easy (?)
2246 */
2247 final = smack_known_star.smk_known;
2248 break;
2249 case DEVPTS_SUPER_MAGIC:
2250 /*
2251 * devpts seems content with the label of the task.
2252 * Programs that change smack have to treat the
2253 * pty with respect.
2254 */
2255 final = csp;
2256 break;
2257 case SOCKFS_MAGIC:
2258 /*
2259 * Casey says sockets get the smack of the task.
2260 */
2261 final = csp;
2262 break;
2263 case PROC_SUPER_MAGIC:
2264 /*
2265 * Casey says procfs appears not to care.
2266 * The superblock default suffices.
2267 */
2268 break;
2269 case TMPFS_MAGIC:
2270 /*
2271 * Device labels should come from the filesystem,
2272 * but watch out, because they're volitile,
2273 * getting recreated on every reboot.
2274 */
2275 final = smack_known_star.smk_known;
2276 /*
2277 * No break.
2278 *
2279 * If a smack value has been set we want to use it,
2280 * but since tmpfs isn't giving us the opportunity
2281 * to set mount options simulate setting the
2282 * superblock default.
2283 */
2284 default:
2285 /*
2286 * This isn't an understood special case.
2287 * Get the value from the xattr.
2288 *
2289 * No xattr support means, alas, no SMACK label.
2290 * Use the aforeapplied default.
2291 * It would be curious if the label of the task
2292 * does not match that assigned.
2293 */
2294 if (inode->i_op->getxattr == NULL)
2295 break;
2296 /*
2297 * Get the dentry for xattr.
2298 */
2299 if (opt_dentry == NULL) {
2300 dp = d_find_alias(inode);
2301 if (dp == NULL)
2302 break;
2303 } else {
2304 dp = dget(opt_dentry);
2305 if (dp == NULL)
2306 break;
2307 }
2308
2309 fetched = smk_fetch(inode, dp);
2310 if (fetched != NULL)
2311 final = fetched;
2312
2313 dput(dp);
2314 break;
2315 }
2316
2317 if (final == NULL)
2318 isp->smk_inode = csp;
2319 else
2320 isp->smk_inode = final;
2321
2322 isp->smk_flags |= SMK_INODE_INSTANT;
2323
2324unlockandout:
2325 mutex_unlock(&isp->smk_lock);
2326 return;
2327}
2328
2329/**
2330 * smack_getprocattr - Smack process attribute access
2331 * @p: the object task
2332 * @name: the name of the attribute in /proc/.../attr
2333 * @value: where to put the result
2334 *
2335 * Places a copy of the task Smack into value
2336 *
2337 * Returns the length of the smack label or an error code
2338 */
2339static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2340{
2341 char *cp;
2342 int slen;
2343
2344 if (strcmp(name, "current") != 0)
2345 return -EINVAL;
2346
David Howellsc69e8d92008-11-14 10:39:19 +11002347 cp = kstrdup(task_security(p), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002348 if (cp == NULL)
2349 return -ENOMEM;
2350
2351 slen = strlen(cp);
2352 *value = cp;
2353 return slen;
2354}
2355
2356/**
2357 * smack_setprocattr - Smack process attribute setting
2358 * @p: the object task
2359 * @name: the name of the attribute in /proc/.../attr
2360 * @value: the value to set
2361 * @size: the size of the value
2362 *
2363 * Sets the Smack value of the task. Only setting self
2364 * is permitted and only with privilege
2365 *
2366 * Returns the length of the smack label or an error code
2367 */
2368static int smack_setprocattr(struct task_struct *p, char *name,
2369 void *value, size_t size)
2370{
David Howellsd84f4f92008-11-14 10:39:23 +11002371 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002372 char *newsmack;
2373
Casey Schauflere114e472008-02-04 22:29:50 -08002374 /*
2375 * Changing another process' Smack value is too dangerous
2376 * and supports no sane use case.
2377 */
2378 if (p != current)
2379 return -EPERM;
2380
David Howells5cd9c582008-08-14 11:37:28 +01002381 if (!capable(CAP_MAC_ADMIN))
2382 return -EPERM;
2383
Casey Schauflere114e472008-02-04 22:29:50 -08002384 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2385 return -EINVAL;
2386
2387 if (strcmp(name, "current") != 0)
2388 return -EINVAL;
2389
2390 newsmack = smk_import(value, size);
2391 if (newsmack == NULL)
2392 return -EINVAL;
2393
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002394 /*
2395 * No process is ever allowed the web ("@") label.
2396 */
2397 if (newsmack == smack_known_web.smk_known)
2398 return -EPERM;
2399
David Howellsd84f4f92008-11-14 10:39:23 +11002400 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002401 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002402 return -ENOMEM;
2403 new->security = newsmack;
2404 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002405 return size;
2406}
2407
2408/**
2409 * smack_unix_stream_connect - Smack access on UDS
2410 * @sock: one socket
2411 * @other: the other socket
2412 * @newsk: unused
2413 *
2414 * Return 0 if a subject with the smack of sock could access
2415 * an object with the smack of other, otherwise an error code
2416 */
2417static int smack_unix_stream_connect(struct socket *sock,
2418 struct socket *other, struct sock *newsk)
2419{
2420 struct inode *sp = SOCK_INODE(sock);
2421 struct inode *op = SOCK_INODE(other);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002422 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002423
Etienne Bassetecfcc532009-04-08 20:40:06 +02002424 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2425 smk_ad_setfield_u_net_sk(&ad, other->sk);
2426 return smk_access(smk_of_inode(sp), smk_of_inode(op),
2427 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002428}
2429
2430/**
2431 * smack_unix_may_send - Smack access on UDS
2432 * @sock: one socket
2433 * @other: the other socket
2434 *
2435 * Return 0 if a subject with the smack of sock could access
2436 * an object with the smack of other, otherwise an error code
2437 */
2438static int smack_unix_may_send(struct socket *sock, struct socket *other)
2439{
2440 struct inode *sp = SOCK_INODE(sock);
2441 struct inode *op = SOCK_INODE(other);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002442 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002443
Etienne Bassetecfcc532009-04-08 20:40:06 +02002444 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2445 smk_ad_setfield_u_net_sk(&ad, other->sk);
2446 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002447}
2448
2449/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002450 * smack_socket_sendmsg - Smack check based on destination host
2451 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002452 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002453 * @size: the size of the message
2454 *
2455 * Return 0 if the current subject can write to the destination
2456 * host. This is only a question if the destination is a single
2457 * label host.
2458 */
2459static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2460 int size)
2461{
2462 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002463
2464 /*
2465 * Perfectly reasonable for this to be NULL
2466 */
2467 if (sip == NULL || sip->sin_family != PF_INET)
2468 return 0;
2469
Paul Moore07feee82009-03-27 17:10:54 -04002470 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002471}
2472
2473
2474/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002475 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002476 * @sap: netlabel secattr
2477 * @sip: where to put the result
2478 *
2479 * Copies a smack label into sip
2480 */
2481static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2482{
2483 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002484 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002485 int pcat;
2486
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002487 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002488 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002489 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002490 * If there are flags but no level netlabel isn't
2491 * behaving the way we expect it to.
2492 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002493 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002494 * Without guidance regarding the smack value
2495 * for the packet fall back on the network
2496 * ambient value.
2497 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002498 memset(smack, '\0', SMK_LABELLEN);
2499 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2500 for (pcat = -1;;) {
2501 pcat = netlbl_secattr_catmap_walk(
2502 sap->attr.mls.cat, pcat + 1);
2503 if (pcat < 0)
2504 break;
2505 smack_catset_bit(pcat, smack);
2506 }
2507 /*
2508 * If it is CIPSO using smack direct mapping
2509 * we are already done. WeeHee.
2510 */
2511 if (sap->attr.mls.lvl == smack_cipso_direct) {
2512 memcpy(sip, smack, SMK_MAXLEN);
2513 return;
Casey Schauflere114e472008-02-04 22:29:50 -08002514 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002515 /*
2516 * Look it up in the supplied table if it is not
2517 * a direct mapping.
2518 */
2519 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2520 return;
2521 }
2522 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2523 /*
2524 * Looks like a fallback, which gives us a secid.
2525 */
2526 sp = smack_from_secid(sap->attr.secid);
2527 /*
2528 * This has got to be a bug because it is
2529 * impossible to specify a fallback without
2530 * specifying the label, which will ensure
2531 * it has a secid, and the only way to get a
2532 * secid is from a fallback.
2533 */
2534 BUG_ON(sp == NULL);
2535 strncpy(sip, sp, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002536 return;
2537 }
2538 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002539 * Without guidance regarding the smack value
2540 * for the packet fall back on the network
2541 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002542 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002543 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002544 return;
2545}
2546
2547/**
2548 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2549 * @sk: socket
2550 * @skb: packet
2551 *
2552 * Returns 0 if the packet should be delivered, an error code otherwise
2553 */
2554static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2555{
2556 struct netlbl_lsm_secattr secattr;
2557 struct socket_smack *ssp = sk->sk_security;
2558 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002559 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002560 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002561 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002562 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2563 return 0;
2564
2565 /*
2566 * Translate what netlabel gave us.
2567 */
Casey Schauflere114e472008-02-04 22:29:50 -08002568 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002569
Casey Schauflere114e472008-02-04 22:29:50 -08002570 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002571 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002572 smack_from_secattr(&secattr, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002573 csp = smack;
2574 } else
2575 csp = smack_net_ambient;
2576
Casey Schauflere114e472008-02-04 22:29:50 -08002577 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002578
Etienne Bassetecfcc532009-04-08 20:40:06 +02002579#ifdef CONFIG_AUDIT
2580 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2581 ad.a.u.net.family = sk->sk_family;
2582 ad.a.u.net.netif = skb->iif;
2583 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2584#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002585 /*
2586 * Receiving a packet requires that the other end
2587 * be able to write here. Read access is not required.
2588 * This is the simplist possible security model
2589 * for networking.
2590 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002591 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002592 if (rc != 0)
2593 netlbl_skbuff_err(skb, rc, 0);
2594 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002595}
2596
2597/**
2598 * smack_socket_getpeersec_stream - pull in packet label
2599 * @sock: the socket
2600 * @optval: user's destination
2601 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002602 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002603 *
2604 * returns zero on success, an error code otherwise
2605 */
2606static int smack_socket_getpeersec_stream(struct socket *sock,
2607 char __user *optval,
2608 int __user *optlen, unsigned len)
2609{
2610 struct socket_smack *ssp;
2611 int slen;
2612 int rc = 0;
2613
2614 ssp = sock->sk->sk_security;
2615 slen = strlen(ssp->smk_packet) + 1;
2616
2617 if (slen > len)
2618 rc = -ERANGE;
2619 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2620 rc = -EFAULT;
2621
2622 if (put_user(slen, optlen) != 0)
2623 rc = -EFAULT;
2624
2625 return rc;
2626}
2627
2628
2629/**
2630 * smack_socket_getpeersec_dgram - pull in packet label
2631 * @sock: the socket
2632 * @skb: packet data
2633 * @secid: pointer to where to put the secid of the packet
2634 *
2635 * Sets the netlabel socket state on sk from parent
2636 */
2637static int smack_socket_getpeersec_dgram(struct socket *sock,
2638 struct sk_buff *skb, u32 *secid)
2639
2640{
2641 struct netlbl_lsm_secattr secattr;
2642 struct sock *sk;
2643 char smack[SMK_LABELLEN];
2644 int family = PF_INET;
2645 u32 s;
2646 int rc;
2647
2648 /*
2649 * Only works for families with packets.
2650 */
2651 if (sock != NULL) {
2652 sk = sock->sk;
2653 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2654 return 0;
2655 family = sk->sk_family;
2656 }
2657 /*
2658 * Translate what netlabel gave us.
2659 */
Casey Schauflere114e472008-02-04 22:29:50 -08002660 netlbl_secattr_init(&secattr);
2661 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2662 if (rc == 0)
2663 smack_from_secattr(&secattr, smack);
2664 netlbl_secattr_destroy(&secattr);
2665
2666 /*
2667 * Give up if we couldn't get anything
2668 */
2669 if (rc != 0)
2670 return rc;
2671
2672 s = smack_to_secid(smack);
2673 if (s == 0)
2674 return -EINVAL;
2675
2676 *secid = s;
2677 return 0;
2678}
2679
2680/**
Paul Moore07feee82009-03-27 17:10:54 -04002681 * smack_sock_graft - Initialize a newly created socket with an existing sock
2682 * @sk: child sock
2683 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08002684 *
Paul Moore07feee82009-03-27 17:10:54 -04002685 * Set the smk_{in,out} state of an existing sock based on the process that
2686 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08002687 */
2688static void smack_sock_graft(struct sock *sk, struct socket *parent)
2689{
2690 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08002691
Paul Moore07feee82009-03-27 17:10:54 -04002692 if (sk == NULL ||
2693 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08002694 return;
2695
2696 ssp = sk->sk_security;
David Howells86a264a2008-11-14 10:39:18 +11002697 ssp->smk_in = ssp->smk_out = current_security();
Paul Moore07feee82009-03-27 17:10:54 -04002698 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08002699}
2700
2701/**
2702 * smack_inet_conn_request - Smack access check on connect
2703 * @sk: socket involved
2704 * @skb: packet
2705 * @req: unused
2706 *
2707 * Returns 0 if a task with the packet label could write to
2708 * the socket, otherwise an error code
2709 */
2710static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2711 struct request_sock *req)
2712{
Paul Moore07feee82009-03-27 17:10:54 -04002713 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08002714 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04002715 struct netlbl_lsm_secattr secattr;
2716 struct sockaddr_in addr;
2717 struct iphdr *hdr;
Casey Schauflere114e472008-02-04 22:29:50 -08002718 char smack[SMK_LABELLEN];
2719 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002720 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002721
Paul Moore07feee82009-03-27 17:10:54 -04002722 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2723 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2724 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08002725
Paul Moore07feee82009-03-27 17:10:54 -04002726 netlbl_secattr_init(&secattr);
2727 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08002728 if (rc == 0)
Paul Moore07feee82009-03-27 17:10:54 -04002729 smack_from_secattr(&secattr, smack);
Casey Schauflere114e472008-02-04 22:29:50 -08002730 else
2731 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
Paul Moore07feee82009-03-27 17:10:54 -04002732 netlbl_secattr_destroy(&secattr);
2733
Etienne Bassetecfcc532009-04-08 20:40:06 +02002734#ifdef CONFIG_AUDIT
2735 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2736 ad.a.u.net.family = family;
2737 ad.a.u.net.netif = skb->iif;
2738 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2739#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002740 /*
Paul Moore07feee82009-03-27 17:10:54 -04002741 * Receiving a packet requires that the other end be able to write
2742 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08002743 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002744 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04002745 if (rc != 0)
2746 return rc;
2747
2748 /*
2749 * Save the peer's label in the request_sock so we can later setup
2750 * smk_packet in the child socket so that SO_PEERCRED can report it.
2751 */
2752 req->peer_secid = smack_to_secid(smack);
2753
2754 /*
2755 * We need to decide if we want to label the incoming connection here
2756 * if we do we only need to label the request_sock and the stack will
2757 * propogate the wire-label to the sock when it is created.
2758 */
2759 hdr = ip_hdr(skb);
2760 addr.sin_addr.s_addr = hdr->saddr;
2761 rcu_read_lock();
2762 if (smack_host_label(&addr) == NULL) {
2763 rcu_read_unlock();
2764 netlbl_secattr_init(&secattr);
2765 smack_to_secattr(smack, &secattr);
2766 rc = netlbl_req_setattr(req, &secattr);
2767 netlbl_secattr_destroy(&secattr);
2768 } else {
2769 rcu_read_unlock();
2770 netlbl_req_delattr(req);
2771 }
Casey Schauflere114e472008-02-04 22:29:50 -08002772
2773 return rc;
2774}
2775
Paul Moore07feee82009-03-27 17:10:54 -04002776/**
2777 * smack_inet_csk_clone - Copy the connection information to the new socket
2778 * @sk: the new socket
2779 * @req: the connection's request_sock
2780 *
2781 * Transfer the connection's peer label to the newly created socket.
2782 */
2783static void smack_inet_csk_clone(struct sock *sk,
2784 const struct request_sock *req)
2785{
2786 struct socket_smack *ssp = sk->sk_security;
2787 char *smack;
2788
2789 if (req->peer_secid != 0) {
2790 smack = smack_from_secid(req->peer_secid);
2791 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2792 } else
2793 ssp->smk_packet[0] = '\0';
2794}
2795
Casey Schauflere114e472008-02-04 22:29:50 -08002796/*
2797 * Key management security hooks
2798 *
2799 * Casey has not tested key support very heavily.
2800 * The permission check is most likely too restrictive.
2801 * If you care about keys please have a look.
2802 */
2803#ifdef CONFIG_KEYS
2804
2805/**
2806 * smack_key_alloc - Set the key security blob
2807 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11002808 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002809 * @flags: unused
2810 *
2811 * No allocation required
2812 *
2813 * Returns 0
2814 */
David Howellsd84f4f92008-11-14 10:39:23 +11002815static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08002816 unsigned long flags)
2817{
David Howellsd84f4f92008-11-14 10:39:23 +11002818 key->security = cred->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002819 return 0;
2820}
2821
2822/**
2823 * smack_key_free - Clear the key security blob
2824 * @key: the object
2825 *
2826 * Clear the blob pointer
2827 */
2828static void smack_key_free(struct key *key)
2829{
2830 key->security = NULL;
2831}
2832
2833/*
2834 * smack_key_permission - Smack access on a key
2835 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11002836 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002837 * @perm: unused
2838 *
2839 * Return 0 if the task has read and write to the object,
2840 * an error code otherwise
2841 */
2842static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11002843 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08002844{
2845 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002846 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002847
2848 keyp = key_ref_to_ptr(key_ref);
2849 if (keyp == NULL)
2850 return -EINVAL;
2851 /*
2852 * If the key hasn't been initialized give it access so that
2853 * it may do so.
2854 */
2855 if (keyp->security == NULL)
2856 return 0;
2857 /*
2858 * This should not occur
2859 */
David Howellsd84f4f92008-11-14 10:39:23 +11002860 if (cred->security == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002861 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002862#ifdef CONFIG_AUDIT
2863 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
2864 ad.a.u.key_struct.key = keyp->serial;
2865 ad.a.u.key_struct.key_desc = keyp->description;
2866#endif
2867 return smk_access(cred->security, keyp->security,
2868 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002869}
2870#endif /* CONFIG_KEYS */
2871
2872/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002873 * Smack Audit hooks
2874 *
2875 * Audit requires a unique representation of each Smack specific
2876 * rule. This unique representation is used to distinguish the
2877 * object to be audited from remaining kernel objects and also
2878 * works as a glue between the audit hooks.
2879 *
2880 * Since repository entries are added but never deleted, we'll use
2881 * the smack_known label address related to the given audit rule as
2882 * the needed unique representation. This also better fits the smack
2883 * model where nearly everything is a label.
2884 */
2885#ifdef CONFIG_AUDIT
2886
2887/**
2888 * smack_audit_rule_init - Initialize a smack audit rule
2889 * @field: audit rule fields given from user-space (audit.h)
2890 * @op: required testing operator (=, !=, >, <, ...)
2891 * @rulestr: smack label to be audited
2892 * @vrule: pointer to save our own audit rule representation
2893 *
2894 * Prepare to audit cases where (@field @op @rulestr) is true.
2895 * The label to be audited is created if necessay.
2896 */
2897static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2898{
2899 char **rule = (char **)vrule;
2900 *rule = NULL;
2901
2902 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2903 return -EINVAL;
2904
Al Viro5af75d82008-12-16 05:59:26 -05002905 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002906 return -EINVAL;
2907
2908 *rule = smk_import(rulestr, 0);
2909
2910 return 0;
2911}
2912
2913/**
2914 * smack_audit_rule_known - Distinguish Smack audit rules
2915 * @krule: rule of interest, in Audit kernel representation format
2916 *
2917 * This is used to filter Smack rules from remaining Audit ones.
2918 * If it's proved that this rule belongs to us, the
2919 * audit_rule_match hook will be called to do the final judgement.
2920 */
2921static int smack_audit_rule_known(struct audit_krule *krule)
2922{
2923 struct audit_field *f;
2924 int i;
2925
2926 for (i = 0; i < krule->field_count; i++) {
2927 f = &krule->fields[i];
2928
2929 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2930 return 1;
2931 }
2932
2933 return 0;
2934}
2935
2936/**
2937 * smack_audit_rule_match - Audit given object ?
2938 * @secid: security id for identifying the object to test
2939 * @field: audit rule flags given from user-space
2940 * @op: required testing operator
2941 * @vrule: smack internal rule presentation
2942 * @actx: audit context associated with the check
2943 *
2944 * The core Audit hook. It's used to take the decision of
2945 * whether to audit or not to audit a given object.
2946 */
2947static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2948 struct audit_context *actx)
2949{
2950 char *smack;
2951 char *rule = vrule;
2952
2953 if (!rule) {
2954 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2955 "Smack: missing rule\n");
2956 return -ENOENT;
2957 }
2958
2959 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2960 return 0;
2961
2962 smack = smack_from_secid(secid);
2963
2964 /*
2965 * No need to do string comparisons. If a match occurs,
2966 * both pointers will point to the same smack_known
2967 * label.
2968 */
Al Viro5af75d82008-12-16 05:59:26 -05002969 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002970 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05002971 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002972 return (rule != smack);
2973
2974 return 0;
2975}
2976
2977/**
2978 * smack_audit_rule_free - free smack rule representation
2979 * @vrule: rule to be freed.
2980 *
2981 * No memory was allocated.
2982 */
2983static void smack_audit_rule_free(void *vrule)
2984{
2985 /* No-op */
2986}
2987
2988#endif /* CONFIG_AUDIT */
2989
Randy Dunlap251a2a92009-02-18 11:42:33 -08002990/**
Casey Schauflere114e472008-02-04 22:29:50 -08002991 * smack_secid_to_secctx - return the smack label for a secid
2992 * @secid: incoming integer
2993 * @secdata: destination
2994 * @seclen: how long it is
2995 *
2996 * Exists for networking code.
2997 */
2998static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2999{
3000 char *sp = smack_from_secid(secid);
3001
3002 *secdata = sp;
3003 *seclen = strlen(sp);
3004 return 0;
3005}
3006
Randy Dunlap251a2a92009-02-18 11:42:33 -08003007/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003008 * smack_secctx_to_secid - return the secid for a smack label
3009 * @secdata: smack label
3010 * @seclen: how long result is
3011 * @secid: outgoing integer
3012 *
3013 * Exists for audit and networking code.
3014 */
David Howellse52c17642008-04-29 20:52:51 +01003015static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003016{
3017 *secid = smack_to_secid(secdata);
3018 return 0;
3019}
3020
Randy Dunlap251a2a92009-02-18 11:42:33 -08003021/**
Casey Schauflere114e472008-02-04 22:29:50 -08003022 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003023 * @secdata: unused
3024 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003025 *
3026 * Exists to make sure nothing gets done, and properly
3027 */
3028static void smack_release_secctx(char *secdata, u32 seclen)
3029{
3030}
3031
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003032struct security_operations smack_ops = {
3033 .name = "smack",
3034
David Howells5cd9c582008-08-14 11:37:28 +01003035 .ptrace_may_access = smack_ptrace_may_access,
3036 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003037 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003038
3039 .sb_alloc_security = smack_sb_alloc_security,
3040 .sb_free_security = smack_sb_free_security,
3041 .sb_copy_data = smack_sb_copy_data,
3042 .sb_kern_mount = smack_sb_kern_mount,
3043 .sb_statfs = smack_sb_statfs,
3044 .sb_mount = smack_sb_mount,
3045 .sb_umount = smack_sb_umount,
3046
3047 .inode_alloc_security = smack_inode_alloc_security,
3048 .inode_free_security = smack_inode_free_security,
3049 .inode_init_security = smack_inode_init_security,
3050 .inode_link = smack_inode_link,
3051 .inode_unlink = smack_inode_unlink,
3052 .inode_rmdir = smack_inode_rmdir,
3053 .inode_rename = smack_inode_rename,
3054 .inode_permission = smack_inode_permission,
3055 .inode_setattr = smack_inode_setattr,
3056 .inode_getattr = smack_inode_getattr,
3057 .inode_setxattr = smack_inode_setxattr,
3058 .inode_post_setxattr = smack_inode_post_setxattr,
3059 .inode_getxattr = smack_inode_getxattr,
3060 .inode_removexattr = smack_inode_removexattr,
3061 .inode_getsecurity = smack_inode_getsecurity,
3062 .inode_setsecurity = smack_inode_setsecurity,
3063 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003064 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003065
3066 .file_permission = smack_file_permission,
3067 .file_alloc_security = smack_file_alloc_security,
3068 .file_free_security = smack_file_free_security,
3069 .file_ioctl = smack_file_ioctl,
3070 .file_lock = smack_file_lock,
3071 .file_fcntl = smack_file_fcntl,
3072 .file_set_fowner = smack_file_set_fowner,
3073 .file_send_sigiotask = smack_file_send_sigiotask,
3074 .file_receive = smack_file_receive,
3075
David Howellsf1752ee2008-11-14 10:39:17 +11003076 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003077 .cred_prepare = smack_cred_prepare,
3078 .cred_commit = smack_cred_commit,
David Howells3a3b7ce2008-11-14 10:39:28 +11003079 .kernel_act_as = smack_kernel_act_as,
3080 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003081 .task_setpgid = smack_task_setpgid,
3082 .task_getpgid = smack_task_getpgid,
3083 .task_getsid = smack_task_getsid,
3084 .task_getsecid = smack_task_getsecid,
3085 .task_setnice = smack_task_setnice,
3086 .task_setioprio = smack_task_setioprio,
3087 .task_getioprio = smack_task_getioprio,
3088 .task_setscheduler = smack_task_setscheduler,
3089 .task_getscheduler = smack_task_getscheduler,
3090 .task_movememory = smack_task_movememory,
3091 .task_kill = smack_task_kill,
3092 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003093 .task_to_inode = smack_task_to_inode,
3094
3095 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003096 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003097
3098 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3099 .msg_msg_free_security = smack_msg_msg_free_security,
3100
3101 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3102 .msg_queue_free_security = smack_msg_queue_free_security,
3103 .msg_queue_associate = smack_msg_queue_associate,
3104 .msg_queue_msgctl = smack_msg_queue_msgctl,
3105 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3106 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3107
3108 .shm_alloc_security = smack_shm_alloc_security,
3109 .shm_free_security = smack_shm_free_security,
3110 .shm_associate = smack_shm_associate,
3111 .shm_shmctl = smack_shm_shmctl,
3112 .shm_shmat = smack_shm_shmat,
3113
3114 .sem_alloc_security = smack_sem_alloc_security,
3115 .sem_free_security = smack_sem_free_security,
3116 .sem_associate = smack_sem_associate,
3117 .sem_semctl = smack_sem_semctl,
3118 .sem_semop = smack_sem_semop,
3119
Casey Schauflere114e472008-02-04 22:29:50 -08003120 .d_instantiate = smack_d_instantiate,
3121
3122 .getprocattr = smack_getprocattr,
3123 .setprocattr = smack_setprocattr,
3124
3125 .unix_stream_connect = smack_unix_stream_connect,
3126 .unix_may_send = smack_unix_may_send,
3127
3128 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003129 .socket_connect = smack_socket_connect,
3130 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003131 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3132 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3133 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3134 .sk_alloc_security = smack_sk_alloc_security,
3135 .sk_free_security = smack_sk_free_security,
3136 .sock_graft = smack_sock_graft,
3137 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003138 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003139
Casey Schauflere114e472008-02-04 22:29:50 -08003140 /* key management security hooks */
3141#ifdef CONFIG_KEYS
3142 .key_alloc = smack_key_alloc,
3143 .key_free = smack_key_free,
3144 .key_permission = smack_key_permission,
3145#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003146
3147 /* Audit hooks */
3148#ifdef CONFIG_AUDIT
3149 .audit_rule_init = smack_audit_rule_init,
3150 .audit_rule_known = smack_audit_rule_known,
3151 .audit_rule_match = smack_audit_rule_match,
3152 .audit_rule_free = smack_audit_rule_free,
3153#endif /* CONFIG_AUDIT */
3154
Casey Schauflere114e472008-02-04 22:29:50 -08003155 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003156 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003157 .release_secctx = smack_release_secctx,
3158};
3159
Etienne Basset7198e2e2009-03-24 20:53:24 +01003160
3161static __init void init_smack_know_list(void)
3162{
3163 list_add(&smack_known_huh.list, &smack_known_list);
3164 list_add(&smack_known_hat.list, &smack_known_list);
3165 list_add(&smack_known_star.list, &smack_known_list);
3166 list_add(&smack_known_floor.list, &smack_known_list);
3167 list_add(&smack_known_invalid.list, &smack_known_list);
3168 list_add(&smack_known_web.list, &smack_known_list);
3169}
3170
Casey Schauflere114e472008-02-04 22:29:50 -08003171/**
3172 * smack_init - initialize the smack system
3173 *
3174 * Returns 0
3175 */
3176static __init int smack_init(void)
3177{
David Howellsd84f4f92008-11-14 10:39:23 +11003178 struct cred *cred;
3179
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003180 if (!security_module_enable(&smack_ops))
3181 return 0;
3182
Casey Schauflere114e472008-02-04 22:29:50 -08003183 printk(KERN_INFO "Smack: Initializing.\n");
3184
3185 /*
3186 * Set the security state for the initial task.
3187 */
David Howellsd84f4f92008-11-14 10:39:23 +11003188 cred = (struct cred *) current->cred;
3189 cred->security = &smack_known_floor.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003190
Etienne Basset7198e2e2009-03-24 20:53:24 +01003191 /* initilize the smack_know_list */
3192 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003193 /*
3194 * Initialize locks
3195 */
Casey Schauflere114e472008-02-04 22:29:50 -08003196 spin_lock_init(&smack_known_huh.smk_cipsolock);
3197 spin_lock_init(&smack_known_hat.smk_cipsolock);
3198 spin_lock_init(&smack_known_star.smk_cipsolock);
3199 spin_lock_init(&smack_known_floor.smk_cipsolock);
3200 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3201
3202 /*
3203 * Register with LSM
3204 */
3205 if (register_security(&smack_ops))
3206 panic("smack: Unable to register with kernel.\n");
3207
3208 return 0;
3209}
3210
3211/*
3212 * Smack requires early initialization in order to label
3213 * all processes and objects when they are created.
3214 */
3215security_initcall(smack_init);