blob: f557767911c93f042250dff18537cb7756c851bb [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 Basset43031542009-03-27 17:11:01 -0400678 /* a label cannot be void and cannot begin with '-' */
679 if (size == 0 || (size > 0 && ((char *)value)[0] == '-'))
680 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800681 } else
682 rc = cap_inode_setxattr(dentry, name, value, size, flags);
683
Etienne Bassetecfcc532009-04-08 20:40:06 +0200684 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
685 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
686
Casey Schauflerbcdca222008-02-23 15:24:04 -0800687 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200688 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800689
690 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800691}
692
693/**
694 * smack_inode_post_setxattr - Apply the Smack update approved above
695 * @dentry: object
696 * @name: attribute name
697 * @value: attribute value
698 * @size: attribute size
699 * @flags: unused
700 *
701 * Set the pointer in the inode blob to the entry found
702 * in the master label list.
703 */
David Howells8f0cfa52008-04-29 00:59:41 -0700704static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
705 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800706{
707 struct inode_smack *isp;
708 char *nsp;
709
710 /*
711 * Not SMACK
712 */
713 if (strcmp(name, XATTR_NAME_SMACK))
714 return;
715
716 if (size >= SMK_LABELLEN)
717 return;
718
719 isp = dentry->d_inode->i_security;
720
721 /*
722 * No locking is done here. This is a pointer
723 * assignment.
724 */
725 nsp = smk_import(value, size);
726 if (nsp != NULL)
727 isp->smk_inode = nsp;
728 else
729 isp->smk_inode = smack_known_invalid.smk_known;
730
731 return;
732}
733
734/*
735 * smack_inode_getxattr - Smack check on getxattr
736 * @dentry: the object
737 * @name: unused
738 *
739 * Returns 0 if access is permitted, an error code otherwise
740 */
David Howells8f0cfa52008-04-29 00:59:41 -0700741static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800742{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200743 struct smk_audit_info ad;
744
745 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
746 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
747
748 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800749}
750
751/*
752 * smack_inode_removexattr - Smack check on removexattr
753 * @dentry: the object
754 * @name: name of the attribute
755 *
756 * Removing the Smack attribute requires CAP_MAC_ADMIN
757 *
758 * Returns 0 if access is permitted, an error code otherwise
759 */
David Howells8f0cfa52008-04-29 00:59:41 -0700760static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800761{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200762 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800763 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800764
Casey Schauflerbcdca222008-02-23 15:24:04 -0800765 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
766 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
767 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
768 if (!capable(CAP_MAC_ADMIN))
769 rc = -EPERM;
770 } else
771 rc = cap_inode_removexattr(dentry, name);
772
Etienne Bassetecfcc532009-04-08 20:40:06 +0200773 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
774 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800775 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200776 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800777
778 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800779}
780
781/**
782 * smack_inode_getsecurity - get smack xattrs
783 * @inode: the object
784 * @name: attribute name
785 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800786 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800787 *
788 * Returns the size of the attribute or an error code
789 */
790static int smack_inode_getsecurity(const struct inode *inode,
791 const char *name, void **buffer,
792 bool alloc)
793{
794 struct socket_smack *ssp;
795 struct socket *sock;
796 struct super_block *sbp;
797 struct inode *ip = (struct inode *)inode;
798 char *isp;
799 int ilen;
800 int rc = 0;
801
802 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
803 isp = smk_of_inode(inode);
804 ilen = strlen(isp) + 1;
805 *buffer = isp;
806 return ilen;
807 }
808
809 /*
810 * The rest of the Smack xattrs are only on sockets.
811 */
812 sbp = ip->i_sb;
813 if (sbp->s_magic != SOCKFS_MAGIC)
814 return -EOPNOTSUPP;
815
816 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800817 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800818 return -EOPNOTSUPP;
819
820 ssp = sock->sk->sk_security;
821
822 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
823 isp = ssp->smk_in;
824 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
825 isp = ssp->smk_out;
826 else
827 return -EOPNOTSUPP;
828
829 ilen = strlen(isp) + 1;
830 if (rc == 0) {
831 *buffer = isp;
832 rc = ilen;
833 }
834
835 return rc;
836}
837
838
839/**
840 * smack_inode_listsecurity - list the Smack attributes
841 * @inode: the object
842 * @buffer: where they go
843 * @buffer_size: size of buffer
844 *
845 * Returns 0 on success, -EINVAL otherwise
846 */
847static int smack_inode_listsecurity(struct inode *inode, char *buffer,
848 size_t buffer_size)
849{
850 int len = strlen(XATTR_NAME_SMACK);
851
852 if (buffer != NULL && len <= buffer_size) {
853 memcpy(buffer, XATTR_NAME_SMACK, len);
854 return len;
855 }
856 return -EINVAL;
857}
858
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +1000859/**
860 * smack_inode_getsecid - Extract inode's security id
861 * @inode: inode to extract the info from
862 * @secid: where result will be saved
863 */
864static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
865{
866 struct inode_smack *isp = inode->i_security;
867
868 *secid = smack_to_secid(isp->smk_inode);
869}
870
Casey Schauflere114e472008-02-04 22:29:50 -0800871/*
872 * File Hooks
873 */
874
875/**
876 * smack_file_permission - Smack check on file operations
877 * @file: unused
878 * @mask: unused
879 *
880 * Returns 0
881 *
882 * Should access checks be done on each read or write?
883 * UNICOS and SELinux say yes.
884 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
885 *
886 * I'll say no for now. Smack does not do the frequent
887 * label changing that SELinux does.
888 */
889static int smack_file_permission(struct file *file, int mask)
890{
891 return 0;
892}
893
894/**
895 * smack_file_alloc_security - assign a file security blob
896 * @file: the object
897 *
898 * The security blob for a file is a pointer to the master
899 * label list, so no allocation is done.
900 *
901 * Returns 0
902 */
903static int smack_file_alloc_security(struct file *file)
904{
David Howells86a264a2008-11-14 10:39:18 +1100905 file->f_security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800906 return 0;
907}
908
909/**
910 * smack_file_free_security - clear a file security blob
911 * @file: the object
912 *
913 * The security blob for a file is a pointer to the master
914 * label list, so no memory is freed.
915 */
916static void smack_file_free_security(struct file *file)
917{
918 file->f_security = NULL;
919}
920
921/**
922 * smack_file_ioctl - Smack check on ioctls
923 * @file: the object
924 * @cmd: what to do
925 * @arg: unused
926 *
927 * Relies heavily on the correct use of the ioctl command conventions.
928 *
929 * Returns 0 if allowed, error code otherwise
930 */
931static int smack_file_ioctl(struct file *file, unsigned int cmd,
932 unsigned long arg)
933{
934 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200935 struct smk_audit_info ad;
936
937 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
938 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -0800939
940 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200941 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800942
943 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200944 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800945
946 return rc;
947}
948
949/**
950 * smack_file_lock - Smack check on file locking
951 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -0800952 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800953 *
954 * Returns 0 if current has write access, error code otherwise
955 */
956static int smack_file_lock(struct file *file, unsigned int cmd)
957{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200958 struct smk_audit_info ad;
959
960 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
961 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
962 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800963}
964
965/**
966 * smack_file_fcntl - Smack check on fcntl
967 * @file: the object
968 * @cmd: what action to check
969 * @arg: unused
970 *
971 * Returns 0 if current has access, error code otherwise
972 */
973static int smack_file_fcntl(struct file *file, unsigned int cmd,
974 unsigned long arg)
975{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200976 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800977 int rc;
978
Etienne Bassetecfcc532009-04-08 20:40:06 +0200979 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
980 smk_ad_setfield_u_fs_path(&ad, file->f_path);
981
Casey Schauflere114e472008-02-04 22:29:50 -0800982 switch (cmd) {
983 case F_DUPFD:
984 case F_GETFD:
985 case F_GETFL:
986 case F_GETLK:
987 case F_GETOWN:
988 case F_GETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +0200989 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800990 break;
991 case F_SETFD:
992 case F_SETFL:
993 case F_SETLK:
994 case F_SETLKW:
995 case F_SETOWN:
996 case F_SETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +0200997 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800998 break;
999 default:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001000 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001001 }
1002
1003 return rc;
1004}
1005
1006/**
1007 * smack_file_set_fowner - set the file security blob value
1008 * @file: object in question
1009 *
1010 * Returns 0
1011 * Further research may be required on this one.
1012 */
1013static int smack_file_set_fowner(struct file *file)
1014{
David Howells86a264a2008-11-14 10:39:18 +11001015 file->f_security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001016 return 0;
1017}
1018
1019/**
1020 * smack_file_send_sigiotask - Smack on sigio
1021 * @tsk: The target task
1022 * @fown: the object the signal come from
1023 * @signum: unused
1024 *
1025 * Allow a privileged task to get signals even if it shouldn't
1026 *
1027 * Returns 0 if a subject with the object's smack could
1028 * write to the task, an error code otherwise.
1029 */
1030static int smack_file_send_sigiotask(struct task_struct *tsk,
1031 struct fown_struct *fown, int signum)
1032{
1033 struct file *file;
1034 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001035 char *tsp = tsk->cred->security;
1036 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001037
1038 /*
1039 * struct fown_struct is never outside the context of a struct file
1040 */
1041 file = container_of(fown, struct file, f_owner);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042 /* we don't log here as rc can be overriden */
1043 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001044 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001045 rc = 0;
1046
1047 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1048 smk_ad_setfield_u_tsk(&ad, tsk);
1049 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001050 return rc;
1051}
1052
1053/**
1054 * smack_file_receive - Smack file receive check
1055 * @file: the object
1056 *
1057 * Returns 0 if current has access, error code otherwise
1058 */
1059static int smack_file_receive(struct file *file)
1060{
1061 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001062 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001063
Etienne Bassetecfcc532009-04-08 20:40:06 +02001064 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1065 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001066 /*
1067 * This code relies on bitmasks.
1068 */
1069 if (file->f_mode & FMODE_READ)
1070 may = MAY_READ;
1071 if (file->f_mode & FMODE_WRITE)
1072 may |= MAY_WRITE;
1073
Etienne Bassetecfcc532009-04-08 20:40:06 +02001074 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001075}
1076
1077/*
1078 * Task hooks
1079 */
1080
1081/**
David Howellsf1752ee2008-11-14 10:39:17 +11001082 * smack_cred_free - "free" task-level security credentials
1083 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001084 *
1085 * Smack isn't using copies of blobs. Everyone
1086 * points to an immutable list. The blobs never go away.
1087 * There is no leak here.
1088 */
David Howellsf1752ee2008-11-14 10:39:17 +11001089static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001090{
David Howellsf1752ee2008-11-14 10:39:17 +11001091 cred->security = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001092}
1093
1094/**
David Howellsd84f4f92008-11-14 10:39:23 +11001095 * smack_cred_prepare - prepare new set of credentials for modification
1096 * @new: the new credentials
1097 * @old: the original credentials
1098 * @gfp: the atomicity of any memory allocations
1099 *
1100 * Prepare a new set of credentials for modification.
1101 */
1102static int smack_cred_prepare(struct cred *new, const struct cred *old,
1103 gfp_t gfp)
1104{
1105 new->security = old->security;
1106 return 0;
1107}
1108
Randy Dunlap251a2a92009-02-18 11:42:33 -08001109/**
1110 * smack_cred_commit - commit new credentials
David Howellsd84f4f92008-11-14 10:39:23 +11001111 * @new: the new credentials
1112 * @old: the original credentials
1113 */
1114static void smack_cred_commit(struct cred *new, const struct cred *old)
1115{
1116}
1117
1118/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001119 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001120 * @new: points to the set of credentials to be modified.
1121 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001122 *
1123 * Set the security data for a kernel service.
1124 */
1125static int smack_kernel_act_as(struct cred *new, u32 secid)
1126{
1127 char *smack = smack_from_secid(secid);
1128
1129 if (smack == NULL)
1130 return -EINVAL;
1131
1132 new->security = smack;
1133 return 0;
1134}
1135
1136/**
1137 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001138 * @new: points to the set of credentials to be modified
1139 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001140 *
1141 * Set the file creation context in a set of credentials to the same
1142 * as the objective context of the specified inode
1143 */
1144static int smack_kernel_create_files_as(struct cred *new,
1145 struct inode *inode)
1146{
1147 struct inode_smack *isp = inode->i_security;
1148
1149 new->security = isp->smk_inode;
1150 return 0;
1151}
1152
1153/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001154 * smk_curacc_on_task - helper to log task related access
1155 * @p: the task object
1156 * @access : the access requested
1157 *
1158 * Return 0 if access is permitted
1159 */
1160static int smk_curacc_on_task(struct task_struct *p, int access)
1161{
1162 struct smk_audit_info ad;
1163
1164 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1165 smk_ad_setfield_u_tsk(&ad, p);
1166 return smk_curacc(task_security(p), access, &ad);
1167}
1168
1169/**
Casey Schauflere114e472008-02-04 22:29:50 -08001170 * smack_task_setpgid - Smack check on setting pgid
1171 * @p: the task object
1172 * @pgid: unused
1173 *
1174 * Return 0 if write access is permitted
1175 */
1176static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1177{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001178 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001179}
1180
1181/**
1182 * smack_task_getpgid - Smack access check for getpgid
1183 * @p: the object task
1184 *
1185 * Returns 0 if current can read the object task, error code otherwise
1186 */
1187static int smack_task_getpgid(struct task_struct *p)
1188{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001189 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001190}
1191
1192/**
1193 * smack_task_getsid - Smack access check for getsid
1194 * @p: the object task
1195 *
1196 * Returns 0 if current can read the object task, error code otherwise
1197 */
1198static int smack_task_getsid(struct task_struct *p)
1199{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001200 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001201}
1202
1203/**
1204 * smack_task_getsecid - get the secid of the task
1205 * @p: the object task
1206 * @secid: where to put the result
1207 *
1208 * Sets the secid to contain a u32 version of the smack label.
1209 */
1210static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1211{
David Howellsc69e8d92008-11-14 10:39:19 +11001212 *secid = smack_to_secid(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001213}
1214
1215/**
1216 * smack_task_setnice - Smack check on setting nice
1217 * @p: the task object
1218 * @nice: unused
1219 *
1220 * Return 0 if write access is permitted
1221 */
1222static int smack_task_setnice(struct task_struct *p, int nice)
1223{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001224 int rc;
1225
1226 rc = cap_task_setnice(p, nice);
1227 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001228 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001229 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001230}
1231
1232/**
1233 * smack_task_setioprio - Smack check on setting ioprio
1234 * @p: the task object
1235 * @ioprio: unused
1236 *
1237 * Return 0 if write access is permitted
1238 */
1239static int smack_task_setioprio(struct task_struct *p, int ioprio)
1240{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001241 int rc;
1242
1243 rc = cap_task_setioprio(p, ioprio);
1244 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001245 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001246 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001247}
1248
1249/**
1250 * smack_task_getioprio - Smack check on reading ioprio
1251 * @p: the task object
1252 *
1253 * Return 0 if read access is permitted
1254 */
1255static int smack_task_getioprio(struct task_struct *p)
1256{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001257 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001258}
1259
1260/**
1261 * smack_task_setscheduler - Smack check on setting scheduler
1262 * @p: the task object
1263 * @policy: unused
1264 * @lp: unused
1265 *
1266 * Return 0 if read access is permitted
1267 */
1268static int smack_task_setscheduler(struct task_struct *p, int policy,
1269 struct sched_param *lp)
1270{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001271 int rc;
1272
1273 rc = cap_task_setscheduler(p, policy, lp);
1274 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001275 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001276 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001277}
1278
1279/**
1280 * smack_task_getscheduler - Smack check on reading scheduler
1281 * @p: the task object
1282 *
1283 * Return 0 if read access is permitted
1284 */
1285static int smack_task_getscheduler(struct task_struct *p)
1286{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001287 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001288}
1289
1290/**
1291 * smack_task_movememory - Smack check on moving memory
1292 * @p: the task object
1293 *
1294 * Return 0 if write access is permitted
1295 */
1296static int smack_task_movememory(struct task_struct *p)
1297{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001298 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001299}
1300
1301/**
1302 * smack_task_kill - Smack check on signal delivery
1303 * @p: the task object
1304 * @info: unused
1305 * @sig: unused
1306 * @secid: identifies the smack to use in lieu of current's
1307 *
1308 * Return 0 if write access is permitted
1309 *
1310 * The secid behavior is an artifact of an SELinux hack
1311 * in the USB code. Someday it may go away.
1312 */
1313static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1314 int sig, u32 secid)
1315{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001316 struct smk_audit_info ad;
1317
1318 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1319 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001320 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001321 * Sending a signal requires that the sender
1322 * can write the receiver.
1323 */
1324 if (secid == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001325 return smk_curacc(task_security(p), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001326 /*
1327 * If the secid isn't 0 we're dealing with some USB IO
1328 * specific behavior. This is not clean. For one thing
1329 * we can't take privilege into account.
1330 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001331 return smk_access(smack_from_secid(secid), task_security(p),
1332 MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001333}
1334
1335/**
1336 * smack_task_wait - Smack access check for waiting
1337 * @p: task to wait for
1338 *
1339 * Returns 0 if current can wait for p, error code otherwise
1340 */
1341static int smack_task_wait(struct task_struct *p)
1342{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001343 struct smk_audit_info ad;
1344 char *sp = current_security();
1345 char *tsp = task_security(p);
Casey Schauflere114e472008-02-04 22:29:50 -08001346 int rc;
1347
Etienne Bassetecfcc532009-04-08 20:40:06 +02001348 /* we don't log here, we can be overriden */
1349 rc = smk_access(sp, tsp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001350 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001351 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001352
1353 /*
1354 * Allow the operation to succeed if either task
1355 * has privilege to perform operations that might
1356 * account for the smack labels having gotten to
1357 * be different in the first place.
1358 *
David Howells5cd9c582008-08-14 11:37:28 +01001359 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001360 * control ideal, taking the object's privilege
1361 * state into account in the decision as well as
1362 * the smack value.
1363 */
David Howells5cd9c582008-08-14 11:37:28 +01001364 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001365 rc = 0;
1366 /* we log only if we didn't get overriden */
1367 out_log:
1368 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1369 smk_ad_setfield_u_tsk(&ad, p);
1370 smack_log(sp, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001371 return rc;
1372}
1373
1374/**
1375 * smack_task_to_inode - copy task smack into the inode blob
1376 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001377 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001378 *
1379 * Sets the smack pointer in the inode security blob
1380 */
1381static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1382{
1383 struct inode_smack *isp = inode->i_security;
David Howellsc69e8d92008-11-14 10:39:19 +11001384 isp->smk_inode = task_security(p);
Casey Schauflere114e472008-02-04 22:29:50 -08001385}
1386
1387/*
1388 * Socket hooks.
1389 */
1390
1391/**
1392 * smack_sk_alloc_security - Allocate a socket blob
1393 * @sk: the socket
1394 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001395 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001396 *
1397 * Assign Smack pointers to current
1398 *
1399 * Returns 0 on success, -ENOMEM is there's no memory
1400 */
1401static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1402{
David Howells86a264a2008-11-14 10:39:18 +11001403 char *csp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001404 struct socket_smack *ssp;
1405
1406 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1407 if (ssp == NULL)
1408 return -ENOMEM;
1409
1410 ssp->smk_in = csp;
1411 ssp->smk_out = csp;
1412 ssp->smk_packet[0] = '\0';
1413
1414 sk->sk_security = ssp;
1415
1416 return 0;
1417}
1418
1419/**
1420 * smack_sk_free_security - Free a socket blob
1421 * @sk: the socket
1422 *
1423 * Clears the blob pointer
1424 */
1425static void smack_sk_free_security(struct sock *sk)
1426{
1427 kfree(sk->sk_security);
1428}
1429
1430/**
Paul Moore07feee82009-03-27 17:10:54 -04001431* smack_host_label - check host based restrictions
1432* @sip: the object end
1433*
1434* looks for host based access restrictions
1435*
1436* This version will only be appropriate for really small sets of single label
1437* hosts. The caller is responsible for ensuring that the RCU read lock is
1438* taken before calling this function.
1439*
1440* Returns the label of the far end or NULL if it's not special.
1441*/
1442static char *smack_host_label(struct sockaddr_in *sip)
1443{
1444 struct smk_netlbladdr *snp;
1445 struct in_addr *siap = &sip->sin_addr;
1446
1447 if (siap->s_addr == 0)
1448 return NULL;
1449
1450 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1451 /*
1452 * we break after finding the first match because
1453 * the list is sorted from longest to shortest mask
1454 * so we have found the most specific match
1455 */
1456 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001457 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1458 /* we have found the special CIPSO option */
1459 if (snp->smk_label == smack_cipso_option)
1460 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001461 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001462 }
Paul Moore07feee82009-03-27 17:10:54 -04001463
1464 return NULL;
1465}
1466
1467/**
Casey Schauflere114e472008-02-04 22:29:50 -08001468 * smack_set_catset - convert a capset to netlabel mls categories
1469 * @catset: the Smack categories
1470 * @sap: where to put the netlabel categories
1471 *
1472 * Allocates and fills attr.mls.cat
1473 */
1474static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1475{
1476 unsigned char *cp;
1477 unsigned char m;
1478 int cat;
1479 int rc;
1480 int byte;
1481
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001482 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001483 return;
1484
1485 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1486 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1487 sap->attr.mls.cat->startbit = 0;
1488
1489 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1490 for (m = 0x80; m != 0; m >>= 1, cat++) {
1491 if ((m & *cp) == 0)
1492 continue;
1493 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1494 cat, GFP_ATOMIC);
1495 }
1496}
1497
1498/**
1499 * smack_to_secattr - fill a secattr from a smack value
1500 * @smack: the smack value
1501 * @nlsp: where the result goes
1502 *
1503 * Casey says that CIPSO is good enough for now.
1504 * It can be used to effect.
1505 * It can also be abused to effect when necessary.
1506 * Appologies to the TSIG group in general and GW in particular.
1507 */
1508static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1509{
1510 struct smack_cipso cipso;
1511 int rc;
1512
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001513 nlsp->domain = smack;
1514 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001515
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001516 rc = smack_to_cipso(smack, &cipso);
1517 if (rc == 0) {
1518 nlsp->attr.mls.lvl = cipso.smk_level;
1519 smack_set_catset(cipso.smk_catset, nlsp);
1520 } else {
1521 nlsp->attr.mls.lvl = smack_cipso_direct;
1522 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001523 }
1524}
1525
1526/**
1527 * smack_netlabel - Set the secattr on a socket
1528 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001529 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001530 *
1531 * Convert the outbound smack value (smk_out) to a
1532 * secattr and attach it to the socket.
1533 *
1534 * Returns 0 on success or an error code
1535 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001536static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001537{
Paul Moore07feee82009-03-27 17:10:54 -04001538 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001539 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001540 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001541
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001542 /*
1543 * Usually the netlabel code will handle changing the
1544 * packet labeling based on the label.
1545 * The case of a single label host is different, because
1546 * a single label host should never get a labeled packet
1547 * even though the label is usually associated with a packet
1548 * label.
1549 */
1550 local_bh_disable();
1551 bh_lock_sock_nested(sk);
1552
1553 if (ssp->smk_out == smack_net_ambient ||
1554 labeled == SMACK_UNLABELED_SOCKET)
1555 netlbl_sock_delattr(sk);
1556 else {
1557 netlbl_secattr_init(&secattr);
1558 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001559 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001560 netlbl_secattr_destroy(&secattr);
1561 }
1562
1563 bh_unlock_sock(sk);
1564 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001565
Casey Schauflere114e472008-02-04 22:29:50 -08001566 return rc;
1567}
1568
1569/**
Paul Moore07feee82009-03-27 17:10:54 -04001570 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1571 * @sk: the socket
1572 * @sap: the destination address
1573 *
1574 * Set the correct secattr for the given socket based on the destination
1575 * address and perform any outbound access checks needed.
1576 *
1577 * Returns 0 on success or an error code.
1578 *
1579 */
1580static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1581{
1582 int rc;
1583 int sk_lbl;
1584 char *hostsp;
1585 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001586 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001587
1588 rcu_read_lock();
1589 hostsp = smack_host_label(sap);
1590 if (hostsp != NULL) {
1591 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001592#ifdef CONFIG_AUDIT
1593 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1594 ad.a.u.net.family = sap->sin_family;
1595 ad.a.u.net.dport = sap->sin_port;
1596 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1597#endif
1598 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001599 } else {
1600 sk_lbl = SMACK_CIPSO_SOCKET;
1601 rc = 0;
1602 }
1603 rcu_read_unlock();
1604 if (rc != 0)
1605 return rc;
1606
1607 return smack_netlabel(sk, sk_lbl);
1608}
1609
1610/**
Casey Schauflere114e472008-02-04 22:29:50 -08001611 * smack_inode_setsecurity - set smack xattrs
1612 * @inode: the object
1613 * @name: attribute name
1614 * @value: attribute value
1615 * @size: size of the attribute
1616 * @flags: unused
1617 *
1618 * Sets the named attribute in the appropriate blob
1619 *
1620 * Returns 0 on success, or an error code
1621 */
1622static int smack_inode_setsecurity(struct inode *inode, const char *name,
1623 const void *value, size_t size, int flags)
1624{
1625 char *sp;
1626 struct inode_smack *nsp = inode->i_security;
1627 struct socket_smack *ssp;
1628 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001629 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001630
Etienne Basset43031542009-03-27 17:11:01 -04001631 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001632 return -EACCES;
1633
1634 sp = smk_import(value, size);
1635 if (sp == NULL)
1636 return -EINVAL;
1637
1638 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1639 nsp->smk_inode = sp;
1640 return 0;
1641 }
1642 /*
1643 * The rest of the Smack xattrs are only on sockets.
1644 */
1645 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1646 return -EOPNOTSUPP;
1647
1648 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001649 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001650 return -EOPNOTSUPP;
1651
1652 ssp = sock->sk->sk_security;
1653
1654 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1655 ssp->smk_in = sp;
1656 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1657 ssp->smk_out = sp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001658 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001659 if (rc != 0)
1660 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1661 __func__, -rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001662 } else
1663 return -EOPNOTSUPP;
1664
1665 return 0;
1666}
1667
1668/**
1669 * smack_socket_post_create - finish socket setup
1670 * @sock: the socket
1671 * @family: protocol family
1672 * @type: unused
1673 * @protocol: unused
1674 * @kern: unused
1675 *
1676 * Sets the netlabel information on the socket
1677 *
1678 * Returns 0 on success, and error code otherwise
1679 */
1680static int smack_socket_post_create(struct socket *sock, int family,
1681 int type, int protocol, int kern)
1682{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001683 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001684 return 0;
1685 /*
1686 * Set the outbound netlbl.
1687 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001688 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1689}
1690
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001691/**
1692 * smack_socket_connect - connect access check
1693 * @sock: the socket
1694 * @sap: the other end
1695 * @addrlen: size of sap
1696 *
1697 * Verifies that a connection may be possible
1698 *
1699 * Returns 0 on success, and error code otherwise
1700 */
1701static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1702 int addrlen)
1703{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001704 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1705 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001706 if (addrlen < sizeof(struct sockaddr_in))
1707 return -EINVAL;
1708
Paul Moore07feee82009-03-27 17:10:54 -04001709 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08001710}
1711
1712/**
1713 * smack_flags_to_may - convert S_ to MAY_ values
1714 * @flags: the S_ value
1715 *
1716 * Returns the equivalent MAY_ value
1717 */
1718static int smack_flags_to_may(int flags)
1719{
1720 int may = 0;
1721
1722 if (flags & S_IRUGO)
1723 may |= MAY_READ;
1724 if (flags & S_IWUGO)
1725 may |= MAY_WRITE;
1726 if (flags & S_IXUGO)
1727 may |= MAY_EXEC;
1728
1729 return may;
1730}
1731
1732/**
1733 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1734 * @msg: the object
1735 *
1736 * Returns 0
1737 */
1738static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1739{
David Howells86a264a2008-11-14 10:39:18 +11001740 msg->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001741 return 0;
1742}
1743
1744/**
1745 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1746 * @msg: the object
1747 *
1748 * Clears the blob pointer
1749 */
1750static void smack_msg_msg_free_security(struct msg_msg *msg)
1751{
1752 msg->security = NULL;
1753}
1754
1755/**
1756 * smack_of_shm - the smack pointer for the shm
1757 * @shp: the object
1758 *
1759 * Returns a pointer to the smack value
1760 */
1761static char *smack_of_shm(struct shmid_kernel *shp)
1762{
1763 return (char *)shp->shm_perm.security;
1764}
1765
1766/**
1767 * smack_shm_alloc_security - Set the security blob for shm
1768 * @shp: the object
1769 *
1770 * Returns 0
1771 */
1772static int smack_shm_alloc_security(struct shmid_kernel *shp)
1773{
1774 struct kern_ipc_perm *isp = &shp->shm_perm;
1775
David Howells86a264a2008-11-14 10:39:18 +11001776 isp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001777 return 0;
1778}
1779
1780/**
1781 * smack_shm_free_security - Clear the security blob for shm
1782 * @shp: the object
1783 *
1784 * Clears the blob pointer
1785 */
1786static void smack_shm_free_security(struct shmid_kernel *shp)
1787{
1788 struct kern_ipc_perm *isp = &shp->shm_perm;
1789
1790 isp->security = NULL;
1791}
1792
1793/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001794 * smk_curacc_shm : check if current has access on shm
1795 * @shp : the object
1796 * @access : access requested
1797 *
1798 * Returns 0 if current has the requested access, error code otherwise
1799 */
1800static int smk_curacc_shm(struct shmid_kernel *shp, int access)
1801{
1802 char *ssp = smack_of_shm(shp);
1803 struct smk_audit_info ad;
1804
1805#ifdef CONFIG_AUDIT
1806 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1807 ad.a.u.ipc_id = shp->shm_perm.id;
1808#endif
1809 return smk_curacc(ssp, access, &ad);
1810}
1811
1812/**
Casey Schauflere114e472008-02-04 22:29:50 -08001813 * smack_shm_associate - Smack access check for shm
1814 * @shp: the object
1815 * @shmflg: access requested
1816 *
1817 * Returns 0 if current has the requested access, error code otherwise
1818 */
1819static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1820{
Casey Schauflere114e472008-02-04 22:29:50 -08001821 int may;
1822
1823 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001824 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001825}
1826
1827/**
1828 * smack_shm_shmctl - Smack access check for shm
1829 * @shp: the object
1830 * @cmd: what it wants to do
1831 *
1832 * Returns 0 if current has the requested access, error code otherwise
1833 */
1834static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1835{
Casey Schauflere114e472008-02-04 22:29:50 -08001836 int may;
1837
1838 switch (cmd) {
1839 case IPC_STAT:
1840 case SHM_STAT:
1841 may = MAY_READ;
1842 break;
1843 case IPC_SET:
1844 case SHM_LOCK:
1845 case SHM_UNLOCK:
1846 case IPC_RMID:
1847 may = MAY_READWRITE;
1848 break;
1849 case IPC_INFO:
1850 case SHM_INFO:
1851 /*
1852 * System level information.
1853 */
1854 return 0;
1855 default:
1856 return -EINVAL;
1857 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02001858 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001859}
1860
1861/**
1862 * smack_shm_shmat - Smack access for shmat
1863 * @shp: the object
1864 * @shmaddr: unused
1865 * @shmflg: access requested
1866 *
1867 * Returns 0 if current has the requested access, error code otherwise
1868 */
1869static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1870 int shmflg)
1871{
Casey Schauflere114e472008-02-04 22:29:50 -08001872 int may;
1873
1874 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001875 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001876}
1877
1878/**
1879 * smack_of_sem - the smack pointer for the sem
1880 * @sma: the object
1881 *
1882 * Returns a pointer to the smack value
1883 */
1884static char *smack_of_sem(struct sem_array *sma)
1885{
1886 return (char *)sma->sem_perm.security;
1887}
1888
1889/**
1890 * smack_sem_alloc_security - Set the security blob for sem
1891 * @sma: the object
1892 *
1893 * Returns 0
1894 */
1895static int smack_sem_alloc_security(struct sem_array *sma)
1896{
1897 struct kern_ipc_perm *isp = &sma->sem_perm;
1898
David Howells86a264a2008-11-14 10:39:18 +11001899 isp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001900 return 0;
1901}
1902
1903/**
1904 * smack_sem_free_security - Clear the security blob for sem
1905 * @sma: the object
1906 *
1907 * Clears the blob pointer
1908 */
1909static void smack_sem_free_security(struct sem_array *sma)
1910{
1911 struct kern_ipc_perm *isp = &sma->sem_perm;
1912
1913 isp->security = NULL;
1914}
1915
1916/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001917 * smk_curacc_sem : check if current has access on sem
1918 * @sma : the object
1919 * @access : access requested
1920 *
1921 * Returns 0 if current has the requested access, error code otherwise
1922 */
1923static int smk_curacc_sem(struct sem_array *sma, int access)
1924{
1925 char *ssp = smack_of_sem(sma);
1926 struct smk_audit_info ad;
1927
1928#ifdef CONFIG_AUDIT
1929 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1930 ad.a.u.ipc_id = sma->sem_perm.id;
1931#endif
1932 return smk_curacc(ssp, access, &ad);
1933}
1934
1935/**
Casey Schauflere114e472008-02-04 22:29:50 -08001936 * smack_sem_associate - Smack access check for sem
1937 * @sma: the object
1938 * @semflg: access requested
1939 *
1940 * Returns 0 if current has the requested access, error code otherwise
1941 */
1942static int smack_sem_associate(struct sem_array *sma, int semflg)
1943{
Casey Schauflere114e472008-02-04 22:29:50 -08001944 int may;
1945
1946 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001947 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001948}
1949
1950/**
1951 * smack_sem_shmctl - Smack access check for sem
1952 * @sma: the object
1953 * @cmd: what it wants to do
1954 *
1955 * Returns 0 if current has the requested access, error code otherwise
1956 */
1957static int smack_sem_semctl(struct sem_array *sma, int cmd)
1958{
Casey Schauflere114e472008-02-04 22:29:50 -08001959 int may;
1960
1961 switch (cmd) {
1962 case GETPID:
1963 case GETNCNT:
1964 case GETZCNT:
1965 case GETVAL:
1966 case GETALL:
1967 case IPC_STAT:
1968 case SEM_STAT:
1969 may = MAY_READ;
1970 break;
1971 case SETVAL:
1972 case SETALL:
1973 case IPC_RMID:
1974 case IPC_SET:
1975 may = MAY_READWRITE;
1976 break;
1977 case IPC_INFO:
1978 case SEM_INFO:
1979 /*
1980 * System level information
1981 */
1982 return 0;
1983 default:
1984 return -EINVAL;
1985 }
1986
Etienne Bassetecfcc532009-04-08 20:40:06 +02001987 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001988}
1989
1990/**
1991 * smack_sem_semop - Smack checks of semaphore operations
1992 * @sma: the object
1993 * @sops: unused
1994 * @nsops: unused
1995 * @alter: unused
1996 *
1997 * Treated as read and write in all cases.
1998 *
1999 * Returns 0 if access is allowed, error code otherwise
2000 */
2001static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2002 unsigned nsops, int alter)
2003{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002004 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002005}
2006
2007/**
2008 * smack_msg_alloc_security - Set the security blob for msg
2009 * @msq: the object
2010 *
2011 * Returns 0
2012 */
2013static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2014{
2015 struct kern_ipc_perm *kisp = &msq->q_perm;
2016
David Howells86a264a2008-11-14 10:39:18 +11002017 kisp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08002018 return 0;
2019}
2020
2021/**
2022 * smack_msg_free_security - Clear the security blob for msg
2023 * @msq: the object
2024 *
2025 * Clears the blob pointer
2026 */
2027static void smack_msg_queue_free_security(struct msg_queue *msq)
2028{
2029 struct kern_ipc_perm *kisp = &msq->q_perm;
2030
2031 kisp->security = NULL;
2032}
2033
2034/**
2035 * smack_of_msq - the smack pointer for the msq
2036 * @msq: the object
2037 *
2038 * Returns a pointer to the smack value
2039 */
2040static char *smack_of_msq(struct msg_queue *msq)
2041{
2042 return (char *)msq->q_perm.security;
2043}
2044
2045/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002046 * smk_curacc_msq : helper to check if current has access on msq
2047 * @msq : the msq
2048 * @access : access requested
2049 *
2050 * return 0 if current has access, error otherwise
2051 */
2052static int smk_curacc_msq(struct msg_queue *msq, int access)
2053{
2054 char *msp = smack_of_msq(msq);
2055 struct smk_audit_info ad;
2056
2057#ifdef CONFIG_AUDIT
2058 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2059 ad.a.u.ipc_id = msq->q_perm.id;
2060#endif
2061 return smk_curacc(msp, access, &ad);
2062}
2063
2064/**
Casey Schauflere114e472008-02-04 22:29:50 -08002065 * smack_msg_queue_associate - Smack access check for msg_queue
2066 * @msq: the object
2067 * @msqflg: access requested
2068 *
2069 * Returns 0 if current has the requested access, error code otherwise
2070 */
2071static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2072{
Casey Schauflere114e472008-02-04 22:29:50 -08002073 int may;
2074
2075 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002076 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002077}
2078
2079/**
2080 * smack_msg_queue_msgctl - Smack access check for msg_queue
2081 * @msq: the object
2082 * @cmd: what it wants to do
2083 *
2084 * Returns 0 if current has the requested access, error code otherwise
2085 */
2086static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2087{
Casey Schauflere114e472008-02-04 22:29:50 -08002088 int may;
2089
2090 switch (cmd) {
2091 case IPC_STAT:
2092 case MSG_STAT:
2093 may = MAY_READ;
2094 break;
2095 case IPC_SET:
2096 case IPC_RMID:
2097 may = MAY_READWRITE;
2098 break;
2099 case IPC_INFO:
2100 case MSG_INFO:
2101 /*
2102 * System level information
2103 */
2104 return 0;
2105 default:
2106 return -EINVAL;
2107 }
2108
Etienne Bassetecfcc532009-04-08 20:40:06 +02002109 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002110}
2111
2112/**
2113 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2114 * @msq: the object
2115 * @msg: unused
2116 * @msqflg: access requested
2117 *
2118 * Returns 0 if current has the requested access, error code otherwise
2119 */
2120static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2121 int msqflg)
2122{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002123 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002124
Etienne Bassetecfcc532009-04-08 20:40:06 +02002125 may = smack_flags_to_may(msqflg);
2126 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002127}
2128
2129/**
2130 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2131 * @msq: the object
2132 * @msg: unused
2133 * @target: unused
2134 * @type: unused
2135 * @mode: unused
2136 *
2137 * Returns 0 if current has read and write access, error code otherwise
2138 */
2139static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2140 struct task_struct *target, long type, int mode)
2141{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002142 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002143}
2144
2145/**
2146 * smack_ipc_permission - Smack access for ipc_permission()
2147 * @ipp: the object permissions
2148 * @flag: access requested
2149 *
2150 * Returns 0 if current has read and write access, error code otherwise
2151 */
2152static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2153{
2154 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002155 int may = smack_flags_to_may(flag);
2156 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002157
Etienne Bassetecfcc532009-04-08 20:40:06 +02002158#ifdef CONFIG_AUDIT
2159 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2160 ad.a.u.ipc_id = ipp->id;
2161#endif
2162 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002163}
2164
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002165/**
2166 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002167 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002168 * @secid: where result will be saved
2169 */
2170static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2171{
2172 char *smack = ipp->security;
2173
2174 *secid = smack_to_secid(smack);
2175}
2176
Casey Schauflere114e472008-02-04 22:29:50 -08002177/**
2178 * smack_d_instantiate - Make sure the blob is correct on an inode
2179 * @opt_dentry: unused
2180 * @inode: the object
2181 *
2182 * Set the inode's security blob if it hasn't been done already.
2183 */
2184static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2185{
2186 struct super_block *sbp;
2187 struct superblock_smack *sbsp;
2188 struct inode_smack *isp;
David Howells86a264a2008-11-14 10:39:18 +11002189 char *csp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08002190 char *fetched;
2191 char *final;
2192 struct dentry *dp;
2193
2194 if (inode == NULL)
2195 return;
2196
2197 isp = inode->i_security;
2198
2199 mutex_lock(&isp->smk_lock);
2200 /*
2201 * If the inode is already instantiated
2202 * take the quick way out
2203 */
2204 if (isp->smk_flags & SMK_INODE_INSTANT)
2205 goto unlockandout;
2206
2207 sbp = inode->i_sb;
2208 sbsp = sbp->s_security;
2209 /*
2210 * We're going to use the superblock default label
2211 * if there's no label on the file.
2212 */
2213 final = sbsp->smk_default;
2214
2215 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002216 * If this is the root inode the superblock
2217 * may be in the process of initialization.
2218 * If that is the case use the root value out
2219 * of the superblock.
2220 */
2221 if (opt_dentry->d_parent == opt_dentry) {
2222 isp->smk_inode = sbsp->smk_root;
2223 isp->smk_flags |= SMK_INODE_INSTANT;
2224 goto unlockandout;
2225 }
2226
2227 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002228 * This is pretty hackish.
2229 * Casey says that we shouldn't have to do
2230 * file system specific code, but it does help
2231 * with keeping it simple.
2232 */
2233 switch (sbp->s_magic) {
2234 case SMACK_MAGIC:
2235 /*
2236 * Casey says that it's a little embarassing
2237 * that the smack file system doesn't do
2238 * extended attributes.
2239 */
2240 final = smack_known_star.smk_known;
2241 break;
2242 case PIPEFS_MAGIC:
2243 /*
2244 * Casey says pipes are easy (?)
2245 */
2246 final = smack_known_star.smk_known;
2247 break;
2248 case DEVPTS_SUPER_MAGIC:
2249 /*
2250 * devpts seems content with the label of the task.
2251 * Programs that change smack have to treat the
2252 * pty with respect.
2253 */
2254 final = csp;
2255 break;
2256 case SOCKFS_MAGIC:
2257 /*
2258 * Casey says sockets get the smack of the task.
2259 */
2260 final = csp;
2261 break;
2262 case PROC_SUPER_MAGIC:
2263 /*
2264 * Casey says procfs appears not to care.
2265 * The superblock default suffices.
2266 */
2267 break;
2268 case TMPFS_MAGIC:
2269 /*
2270 * Device labels should come from the filesystem,
2271 * but watch out, because they're volitile,
2272 * getting recreated on every reboot.
2273 */
2274 final = smack_known_star.smk_known;
2275 /*
2276 * No break.
2277 *
2278 * If a smack value has been set we want to use it,
2279 * but since tmpfs isn't giving us the opportunity
2280 * to set mount options simulate setting the
2281 * superblock default.
2282 */
2283 default:
2284 /*
2285 * This isn't an understood special case.
2286 * Get the value from the xattr.
2287 *
2288 * No xattr support means, alas, no SMACK label.
2289 * Use the aforeapplied default.
2290 * It would be curious if the label of the task
2291 * does not match that assigned.
2292 */
2293 if (inode->i_op->getxattr == NULL)
2294 break;
2295 /*
2296 * Get the dentry for xattr.
2297 */
2298 if (opt_dentry == NULL) {
2299 dp = d_find_alias(inode);
2300 if (dp == NULL)
2301 break;
2302 } else {
2303 dp = dget(opt_dentry);
2304 if (dp == NULL)
2305 break;
2306 }
2307
2308 fetched = smk_fetch(inode, dp);
2309 if (fetched != NULL)
2310 final = fetched;
2311
2312 dput(dp);
2313 break;
2314 }
2315
2316 if (final == NULL)
2317 isp->smk_inode = csp;
2318 else
2319 isp->smk_inode = final;
2320
2321 isp->smk_flags |= SMK_INODE_INSTANT;
2322
2323unlockandout:
2324 mutex_unlock(&isp->smk_lock);
2325 return;
2326}
2327
2328/**
2329 * smack_getprocattr - Smack process attribute access
2330 * @p: the object task
2331 * @name: the name of the attribute in /proc/.../attr
2332 * @value: where to put the result
2333 *
2334 * Places a copy of the task Smack into value
2335 *
2336 * Returns the length of the smack label or an error code
2337 */
2338static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2339{
2340 char *cp;
2341 int slen;
2342
2343 if (strcmp(name, "current") != 0)
2344 return -EINVAL;
2345
David Howellsc69e8d92008-11-14 10:39:19 +11002346 cp = kstrdup(task_security(p), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002347 if (cp == NULL)
2348 return -ENOMEM;
2349
2350 slen = strlen(cp);
2351 *value = cp;
2352 return slen;
2353}
2354
2355/**
2356 * smack_setprocattr - Smack process attribute setting
2357 * @p: the object task
2358 * @name: the name of the attribute in /proc/.../attr
2359 * @value: the value to set
2360 * @size: the size of the value
2361 *
2362 * Sets the Smack value of the task. Only setting self
2363 * is permitted and only with privilege
2364 *
2365 * Returns the length of the smack label or an error code
2366 */
2367static int smack_setprocattr(struct task_struct *p, char *name,
2368 void *value, size_t size)
2369{
David Howellsd84f4f92008-11-14 10:39:23 +11002370 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002371 char *newsmack;
2372
Casey Schauflere114e472008-02-04 22:29:50 -08002373 /*
2374 * Changing another process' Smack value is too dangerous
2375 * and supports no sane use case.
2376 */
2377 if (p != current)
2378 return -EPERM;
2379
David Howells5cd9c582008-08-14 11:37:28 +01002380 if (!capable(CAP_MAC_ADMIN))
2381 return -EPERM;
2382
Casey Schauflere114e472008-02-04 22:29:50 -08002383 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2384 return -EINVAL;
2385
2386 if (strcmp(name, "current") != 0)
2387 return -EINVAL;
2388
2389 newsmack = smk_import(value, size);
2390 if (newsmack == NULL)
2391 return -EINVAL;
2392
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002393 /*
2394 * No process is ever allowed the web ("@") label.
2395 */
2396 if (newsmack == smack_known_web.smk_known)
2397 return -EPERM;
2398
David Howellsd84f4f92008-11-14 10:39:23 +11002399 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002400 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002401 return -ENOMEM;
2402 new->security = newsmack;
2403 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002404 return size;
2405}
2406
2407/**
2408 * smack_unix_stream_connect - Smack access on UDS
2409 * @sock: one socket
2410 * @other: the other socket
2411 * @newsk: unused
2412 *
2413 * Return 0 if a subject with the smack of sock could access
2414 * an object with the smack of other, otherwise an error code
2415 */
2416static int smack_unix_stream_connect(struct socket *sock,
2417 struct socket *other, struct sock *newsk)
2418{
2419 struct inode *sp = SOCK_INODE(sock);
2420 struct inode *op = SOCK_INODE(other);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002421 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002422
Etienne Bassetecfcc532009-04-08 20:40:06 +02002423 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2424 smk_ad_setfield_u_net_sk(&ad, other->sk);
2425 return smk_access(smk_of_inode(sp), smk_of_inode(op),
2426 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002427}
2428
2429/**
2430 * smack_unix_may_send - Smack access on UDS
2431 * @sock: one socket
2432 * @other: the other socket
2433 *
2434 * Return 0 if a subject with the smack of sock could access
2435 * an object with the smack of other, otherwise an error code
2436 */
2437static int smack_unix_may_send(struct socket *sock, struct socket *other)
2438{
2439 struct inode *sp = SOCK_INODE(sock);
2440 struct inode *op = SOCK_INODE(other);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002441 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002442
Etienne Bassetecfcc532009-04-08 20:40:06 +02002443 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2444 smk_ad_setfield_u_net_sk(&ad, other->sk);
2445 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002446}
2447
2448/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002449 * smack_socket_sendmsg - Smack check based on destination host
2450 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002451 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002452 * @size: the size of the message
2453 *
2454 * Return 0 if the current subject can write to the destination
2455 * host. This is only a question if the destination is a single
2456 * label host.
2457 */
2458static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2459 int size)
2460{
2461 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002462
2463 /*
2464 * Perfectly reasonable for this to be NULL
2465 */
2466 if (sip == NULL || sip->sin_family != PF_INET)
2467 return 0;
2468
Paul Moore07feee82009-03-27 17:10:54 -04002469 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002470}
2471
2472
2473/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002474 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002475 * @sap: netlabel secattr
2476 * @sip: where to put the result
2477 *
2478 * Copies a smack label into sip
2479 */
2480static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2481{
2482 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002483 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002484 int pcat;
2485
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002486 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002487 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002488 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002489 * If there are flags but no level netlabel isn't
2490 * behaving the way we expect it to.
2491 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002492 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002493 * Without guidance regarding the smack value
2494 * for the packet fall back on the network
2495 * ambient value.
2496 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002497 memset(smack, '\0', SMK_LABELLEN);
2498 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2499 for (pcat = -1;;) {
2500 pcat = netlbl_secattr_catmap_walk(
2501 sap->attr.mls.cat, pcat + 1);
2502 if (pcat < 0)
2503 break;
2504 smack_catset_bit(pcat, smack);
2505 }
2506 /*
2507 * If it is CIPSO using smack direct mapping
2508 * we are already done. WeeHee.
2509 */
2510 if (sap->attr.mls.lvl == smack_cipso_direct) {
2511 memcpy(sip, smack, SMK_MAXLEN);
2512 return;
Casey Schauflere114e472008-02-04 22:29:50 -08002513 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002514 /*
2515 * Look it up in the supplied table if it is not
2516 * a direct mapping.
2517 */
2518 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2519 return;
2520 }
2521 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2522 /*
2523 * Looks like a fallback, which gives us a secid.
2524 */
2525 sp = smack_from_secid(sap->attr.secid);
2526 /*
2527 * This has got to be a bug because it is
2528 * impossible to specify a fallback without
2529 * specifying the label, which will ensure
2530 * it has a secid, and the only way to get a
2531 * secid is from a fallback.
2532 */
2533 BUG_ON(sp == NULL);
2534 strncpy(sip, sp, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002535 return;
2536 }
2537 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002538 * Without guidance regarding the smack value
2539 * for the packet fall back on the network
2540 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002541 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002542 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002543 return;
2544}
2545
2546/**
2547 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2548 * @sk: socket
2549 * @skb: packet
2550 *
2551 * Returns 0 if the packet should be delivered, an error code otherwise
2552 */
2553static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2554{
2555 struct netlbl_lsm_secattr secattr;
2556 struct socket_smack *ssp = sk->sk_security;
2557 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002558 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002559 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002560 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002561 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2562 return 0;
2563
2564 /*
2565 * Translate what netlabel gave us.
2566 */
Casey Schauflere114e472008-02-04 22:29:50 -08002567 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002568
Casey Schauflere114e472008-02-04 22:29:50 -08002569 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002570 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002571 smack_from_secattr(&secattr, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002572 csp = smack;
2573 } else
2574 csp = smack_net_ambient;
2575
Casey Schauflere114e472008-02-04 22:29:50 -08002576 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002577
Etienne Bassetecfcc532009-04-08 20:40:06 +02002578#ifdef CONFIG_AUDIT
2579 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2580 ad.a.u.net.family = sk->sk_family;
2581 ad.a.u.net.netif = skb->iif;
2582 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2583#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002584 /*
2585 * Receiving a packet requires that the other end
2586 * be able to write here. Read access is not required.
2587 * This is the simplist possible security model
2588 * for networking.
2589 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002590 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002591 if (rc != 0)
2592 netlbl_skbuff_err(skb, rc, 0);
2593 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002594}
2595
2596/**
2597 * smack_socket_getpeersec_stream - pull in packet label
2598 * @sock: the socket
2599 * @optval: user's destination
2600 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002601 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002602 *
2603 * returns zero on success, an error code otherwise
2604 */
2605static int smack_socket_getpeersec_stream(struct socket *sock,
2606 char __user *optval,
2607 int __user *optlen, unsigned len)
2608{
2609 struct socket_smack *ssp;
2610 int slen;
2611 int rc = 0;
2612
2613 ssp = sock->sk->sk_security;
2614 slen = strlen(ssp->smk_packet) + 1;
2615
2616 if (slen > len)
2617 rc = -ERANGE;
2618 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2619 rc = -EFAULT;
2620
2621 if (put_user(slen, optlen) != 0)
2622 rc = -EFAULT;
2623
2624 return rc;
2625}
2626
2627
2628/**
2629 * smack_socket_getpeersec_dgram - pull in packet label
2630 * @sock: the socket
2631 * @skb: packet data
2632 * @secid: pointer to where to put the secid of the packet
2633 *
2634 * Sets the netlabel socket state on sk from parent
2635 */
2636static int smack_socket_getpeersec_dgram(struct socket *sock,
2637 struct sk_buff *skb, u32 *secid)
2638
2639{
2640 struct netlbl_lsm_secattr secattr;
2641 struct sock *sk;
2642 char smack[SMK_LABELLEN];
2643 int family = PF_INET;
2644 u32 s;
2645 int rc;
2646
2647 /*
2648 * Only works for families with packets.
2649 */
2650 if (sock != NULL) {
2651 sk = sock->sk;
2652 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2653 return 0;
2654 family = sk->sk_family;
2655 }
2656 /*
2657 * Translate what netlabel gave us.
2658 */
Casey Schauflere114e472008-02-04 22:29:50 -08002659 netlbl_secattr_init(&secattr);
2660 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2661 if (rc == 0)
2662 smack_from_secattr(&secattr, smack);
2663 netlbl_secattr_destroy(&secattr);
2664
2665 /*
2666 * Give up if we couldn't get anything
2667 */
2668 if (rc != 0)
2669 return rc;
2670
2671 s = smack_to_secid(smack);
2672 if (s == 0)
2673 return -EINVAL;
2674
2675 *secid = s;
2676 return 0;
2677}
2678
2679/**
Paul Moore07feee82009-03-27 17:10:54 -04002680 * smack_sock_graft - Initialize a newly created socket with an existing sock
2681 * @sk: child sock
2682 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08002683 *
Paul Moore07feee82009-03-27 17:10:54 -04002684 * Set the smk_{in,out} state of an existing sock based on the process that
2685 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08002686 */
2687static void smack_sock_graft(struct sock *sk, struct socket *parent)
2688{
2689 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08002690
Paul Moore07feee82009-03-27 17:10:54 -04002691 if (sk == NULL ||
2692 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08002693 return;
2694
2695 ssp = sk->sk_security;
David Howells86a264a2008-11-14 10:39:18 +11002696 ssp->smk_in = ssp->smk_out = current_security();
Paul Moore07feee82009-03-27 17:10:54 -04002697 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08002698}
2699
2700/**
2701 * smack_inet_conn_request - Smack access check on connect
2702 * @sk: socket involved
2703 * @skb: packet
2704 * @req: unused
2705 *
2706 * Returns 0 if a task with the packet label could write to
2707 * the socket, otherwise an error code
2708 */
2709static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2710 struct request_sock *req)
2711{
Paul Moore07feee82009-03-27 17:10:54 -04002712 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08002713 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04002714 struct netlbl_lsm_secattr secattr;
2715 struct sockaddr_in addr;
2716 struct iphdr *hdr;
Casey Schauflere114e472008-02-04 22:29:50 -08002717 char smack[SMK_LABELLEN];
2718 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002719 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002720
Paul Moore07feee82009-03-27 17:10:54 -04002721 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2722 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2723 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08002724
Paul Moore07feee82009-03-27 17:10:54 -04002725 netlbl_secattr_init(&secattr);
2726 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08002727 if (rc == 0)
Paul Moore07feee82009-03-27 17:10:54 -04002728 smack_from_secattr(&secattr, smack);
Casey Schauflere114e472008-02-04 22:29:50 -08002729 else
2730 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
Paul Moore07feee82009-03-27 17:10:54 -04002731 netlbl_secattr_destroy(&secattr);
2732
Etienne Bassetecfcc532009-04-08 20:40:06 +02002733#ifdef CONFIG_AUDIT
2734 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2735 ad.a.u.net.family = family;
2736 ad.a.u.net.netif = skb->iif;
2737 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2738#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002739 /*
Paul Moore07feee82009-03-27 17:10:54 -04002740 * Receiving a packet requires that the other end be able to write
2741 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08002742 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002743 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04002744 if (rc != 0)
2745 return rc;
2746
2747 /*
2748 * Save the peer's label in the request_sock so we can later setup
2749 * smk_packet in the child socket so that SO_PEERCRED can report it.
2750 */
2751 req->peer_secid = smack_to_secid(smack);
2752
2753 /*
2754 * We need to decide if we want to label the incoming connection here
2755 * if we do we only need to label the request_sock and the stack will
2756 * propogate the wire-label to the sock when it is created.
2757 */
2758 hdr = ip_hdr(skb);
2759 addr.sin_addr.s_addr = hdr->saddr;
2760 rcu_read_lock();
2761 if (smack_host_label(&addr) == NULL) {
2762 rcu_read_unlock();
2763 netlbl_secattr_init(&secattr);
2764 smack_to_secattr(smack, &secattr);
2765 rc = netlbl_req_setattr(req, &secattr);
2766 netlbl_secattr_destroy(&secattr);
2767 } else {
2768 rcu_read_unlock();
2769 netlbl_req_delattr(req);
2770 }
Casey Schauflere114e472008-02-04 22:29:50 -08002771
2772 return rc;
2773}
2774
Paul Moore07feee82009-03-27 17:10:54 -04002775/**
2776 * smack_inet_csk_clone - Copy the connection information to the new socket
2777 * @sk: the new socket
2778 * @req: the connection's request_sock
2779 *
2780 * Transfer the connection's peer label to the newly created socket.
2781 */
2782static void smack_inet_csk_clone(struct sock *sk,
2783 const struct request_sock *req)
2784{
2785 struct socket_smack *ssp = sk->sk_security;
2786 char *smack;
2787
2788 if (req->peer_secid != 0) {
2789 smack = smack_from_secid(req->peer_secid);
2790 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2791 } else
2792 ssp->smk_packet[0] = '\0';
2793}
2794
Casey Schauflere114e472008-02-04 22:29:50 -08002795/*
2796 * Key management security hooks
2797 *
2798 * Casey has not tested key support very heavily.
2799 * The permission check is most likely too restrictive.
2800 * If you care about keys please have a look.
2801 */
2802#ifdef CONFIG_KEYS
2803
2804/**
2805 * smack_key_alloc - Set the key security blob
2806 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11002807 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002808 * @flags: unused
2809 *
2810 * No allocation required
2811 *
2812 * Returns 0
2813 */
David Howellsd84f4f92008-11-14 10:39:23 +11002814static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08002815 unsigned long flags)
2816{
David Howellsd84f4f92008-11-14 10:39:23 +11002817 key->security = cred->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002818 return 0;
2819}
2820
2821/**
2822 * smack_key_free - Clear the key security blob
2823 * @key: the object
2824 *
2825 * Clear the blob pointer
2826 */
2827static void smack_key_free(struct key *key)
2828{
2829 key->security = NULL;
2830}
2831
2832/*
2833 * smack_key_permission - Smack access on a key
2834 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11002835 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002836 * @perm: unused
2837 *
2838 * Return 0 if the task has read and write to the object,
2839 * an error code otherwise
2840 */
2841static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11002842 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08002843{
2844 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002845 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002846
2847 keyp = key_ref_to_ptr(key_ref);
2848 if (keyp == NULL)
2849 return -EINVAL;
2850 /*
2851 * If the key hasn't been initialized give it access so that
2852 * it may do so.
2853 */
2854 if (keyp->security == NULL)
2855 return 0;
2856 /*
2857 * This should not occur
2858 */
David Howellsd84f4f92008-11-14 10:39:23 +11002859 if (cred->security == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002860 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002861#ifdef CONFIG_AUDIT
2862 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
2863 ad.a.u.key_struct.key = keyp->serial;
2864 ad.a.u.key_struct.key_desc = keyp->description;
2865#endif
2866 return smk_access(cred->security, keyp->security,
2867 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002868}
2869#endif /* CONFIG_KEYS */
2870
2871/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002872 * Smack Audit hooks
2873 *
2874 * Audit requires a unique representation of each Smack specific
2875 * rule. This unique representation is used to distinguish the
2876 * object to be audited from remaining kernel objects and also
2877 * works as a glue between the audit hooks.
2878 *
2879 * Since repository entries are added but never deleted, we'll use
2880 * the smack_known label address related to the given audit rule as
2881 * the needed unique representation. This also better fits the smack
2882 * model where nearly everything is a label.
2883 */
2884#ifdef CONFIG_AUDIT
2885
2886/**
2887 * smack_audit_rule_init - Initialize a smack audit rule
2888 * @field: audit rule fields given from user-space (audit.h)
2889 * @op: required testing operator (=, !=, >, <, ...)
2890 * @rulestr: smack label to be audited
2891 * @vrule: pointer to save our own audit rule representation
2892 *
2893 * Prepare to audit cases where (@field @op @rulestr) is true.
2894 * The label to be audited is created if necessay.
2895 */
2896static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2897{
2898 char **rule = (char **)vrule;
2899 *rule = NULL;
2900
2901 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2902 return -EINVAL;
2903
Al Viro5af75d82008-12-16 05:59:26 -05002904 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002905 return -EINVAL;
2906
2907 *rule = smk_import(rulestr, 0);
2908
2909 return 0;
2910}
2911
2912/**
2913 * smack_audit_rule_known - Distinguish Smack audit rules
2914 * @krule: rule of interest, in Audit kernel representation format
2915 *
2916 * This is used to filter Smack rules from remaining Audit ones.
2917 * If it's proved that this rule belongs to us, the
2918 * audit_rule_match hook will be called to do the final judgement.
2919 */
2920static int smack_audit_rule_known(struct audit_krule *krule)
2921{
2922 struct audit_field *f;
2923 int i;
2924
2925 for (i = 0; i < krule->field_count; i++) {
2926 f = &krule->fields[i];
2927
2928 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2929 return 1;
2930 }
2931
2932 return 0;
2933}
2934
2935/**
2936 * smack_audit_rule_match - Audit given object ?
2937 * @secid: security id for identifying the object to test
2938 * @field: audit rule flags given from user-space
2939 * @op: required testing operator
2940 * @vrule: smack internal rule presentation
2941 * @actx: audit context associated with the check
2942 *
2943 * The core Audit hook. It's used to take the decision of
2944 * whether to audit or not to audit a given object.
2945 */
2946static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2947 struct audit_context *actx)
2948{
2949 char *smack;
2950 char *rule = vrule;
2951
2952 if (!rule) {
2953 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2954 "Smack: missing rule\n");
2955 return -ENOENT;
2956 }
2957
2958 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2959 return 0;
2960
2961 smack = smack_from_secid(secid);
2962
2963 /*
2964 * No need to do string comparisons. If a match occurs,
2965 * both pointers will point to the same smack_known
2966 * label.
2967 */
Al Viro5af75d82008-12-16 05:59:26 -05002968 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002969 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05002970 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002971 return (rule != smack);
2972
2973 return 0;
2974}
2975
2976/**
2977 * smack_audit_rule_free - free smack rule representation
2978 * @vrule: rule to be freed.
2979 *
2980 * No memory was allocated.
2981 */
2982static void smack_audit_rule_free(void *vrule)
2983{
2984 /* No-op */
2985}
2986
2987#endif /* CONFIG_AUDIT */
2988
Randy Dunlap251a2a92009-02-18 11:42:33 -08002989/**
Casey Schauflere114e472008-02-04 22:29:50 -08002990 * smack_secid_to_secctx - return the smack label for a secid
2991 * @secid: incoming integer
2992 * @secdata: destination
2993 * @seclen: how long it is
2994 *
2995 * Exists for networking code.
2996 */
2997static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
2998{
2999 char *sp = smack_from_secid(secid);
3000
3001 *secdata = sp;
3002 *seclen = strlen(sp);
3003 return 0;
3004}
3005
Randy Dunlap251a2a92009-02-18 11:42:33 -08003006/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003007 * smack_secctx_to_secid - return the secid for a smack label
3008 * @secdata: smack label
3009 * @seclen: how long result is
3010 * @secid: outgoing integer
3011 *
3012 * Exists for audit and networking code.
3013 */
David Howellse52c17642008-04-29 20:52:51 +01003014static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003015{
3016 *secid = smack_to_secid(secdata);
3017 return 0;
3018}
3019
Randy Dunlap251a2a92009-02-18 11:42:33 -08003020/**
Casey Schauflere114e472008-02-04 22:29:50 -08003021 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003022 * @secdata: unused
3023 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003024 *
3025 * Exists to make sure nothing gets done, and properly
3026 */
3027static void smack_release_secctx(char *secdata, u32 seclen)
3028{
3029}
3030
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003031struct security_operations smack_ops = {
3032 .name = "smack",
3033
David Howells5cd9c582008-08-14 11:37:28 +01003034 .ptrace_may_access = smack_ptrace_may_access,
3035 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003036 .capget = cap_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11003037 .capset = cap_capset,
Casey Schauflere114e472008-02-04 22:29:50 -08003038 .capable = cap_capable,
3039 .syslog = smack_syslog,
3040 .settime = cap_settime,
3041 .vm_enough_memory = cap_vm_enough_memory,
3042
David Howellsa6f76f22008-11-14 10:39:24 +11003043 .bprm_set_creds = cap_bprm_set_creds,
Casey Schauflere114e472008-02-04 22:29:50 -08003044 .bprm_secureexec = cap_bprm_secureexec,
3045
3046 .sb_alloc_security = smack_sb_alloc_security,
3047 .sb_free_security = smack_sb_free_security,
3048 .sb_copy_data = smack_sb_copy_data,
3049 .sb_kern_mount = smack_sb_kern_mount,
3050 .sb_statfs = smack_sb_statfs,
3051 .sb_mount = smack_sb_mount,
3052 .sb_umount = smack_sb_umount,
3053
3054 .inode_alloc_security = smack_inode_alloc_security,
3055 .inode_free_security = smack_inode_free_security,
3056 .inode_init_security = smack_inode_init_security,
3057 .inode_link = smack_inode_link,
3058 .inode_unlink = smack_inode_unlink,
3059 .inode_rmdir = smack_inode_rmdir,
3060 .inode_rename = smack_inode_rename,
3061 .inode_permission = smack_inode_permission,
3062 .inode_setattr = smack_inode_setattr,
3063 .inode_getattr = smack_inode_getattr,
3064 .inode_setxattr = smack_inode_setxattr,
3065 .inode_post_setxattr = smack_inode_post_setxattr,
3066 .inode_getxattr = smack_inode_getxattr,
3067 .inode_removexattr = smack_inode_removexattr,
Casey Schauflerbcdca222008-02-23 15:24:04 -08003068 .inode_need_killpriv = cap_inode_need_killpriv,
3069 .inode_killpriv = cap_inode_killpriv,
Casey Schauflere114e472008-02-04 22:29:50 -08003070 .inode_getsecurity = smack_inode_getsecurity,
3071 .inode_setsecurity = smack_inode_setsecurity,
3072 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003073 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003074
3075 .file_permission = smack_file_permission,
3076 .file_alloc_security = smack_file_alloc_security,
3077 .file_free_security = smack_file_free_security,
3078 .file_ioctl = smack_file_ioctl,
3079 .file_lock = smack_file_lock,
3080 .file_fcntl = smack_file_fcntl,
3081 .file_set_fowner = smack_file_set_fowner,
3082 .file_send_sigiotask = smack_file_send_sigiotask,
3083 .file_receive = smack_file_receive,
3084
David Howellsf1752ee2008-11-14 10:39:17 +11003085 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003086 .cred_prepare = smack_cred_prepare,
3087 .cred_commit = smack_cred_commit,
David Howells3a3b7ce2008-11-14 10:39:28 +11003088 .kernel_act_as = smack_kernel_act_as,
3089 .kernel_create_files_as = smack_kernel_create_files_as,
David Howellsd84f4f92008-11-14 10:39:23 +11003090 .task_fix_setuid = cap_task_fix_setuid,
Casey Schauflere114e472008-02-04 22:29:50 -08003091 .task_setpgid = smack_task_setpgid,
3092 .task_getpgid = smack_task_getpgid,
3093 .task_getsid = smack_task_getsid,
3094 .task_getsecid = smack_task_getsecid,
3095 .task_setnice = smack_task_setnice,
3096 .task_setioprio = smack_task_setioprio,
3097 .task_getioprio = smack_task_getioprio,
3098 .task_setscheduler = smack_task_setscheduler,
3099 .task_getscheduler = smack_task_getscheduler,
3100 .task_movememory = smack_task_movememory,
3101 .task_kill = smack_task_kill,
3102 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003103 .task_to_inode = smack_task_to_inode,
Serge E. Hallyn1236cc32008-04-28 02:13:43 -07003104 .task_prctl = cap_task_prctl,
Casey Schauflere114e472008-02-04 22:29:50 -08003105
3106 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003107 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003108
3109 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3110 .msg_msg_free_security = smack_msg_msg_free_security,
3111
3112 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3113 .msg_queue_free_security = smack_msg_queue_free_security,
3114 .msg_queue_associate = smack_msg_queue_associate,
3115 .msg_queue_msgctl = smack_msg_queue_msgctl,
3116 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3117 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3118
3119 .shm_alloc_security = smack_shm_alloc_security,
3120 .shm_free_security = smack_shm_free_security,
3121 .shm_associate = smack_shm_associate,
3122 .shm_shmctl = smack_shm_shmctl,
3123 .shm_shmat = smack_shm_shmat,
3124
3125 .sem_alloc_security = smack_sem_alloc_security,
3126 .sem_free_security = smack_sem_free_security,
3127 .sem_associate = smack_sem_associate,
3128 .sem_semctl = smack_sem_semctl,
3129 .sem_semop = smack_sem_semop,
3130
3131 .netlink_send = cap_netlink_send,
3132 .netlink_recv = cap_netlink_recv,
3133
3134 .d_instantiate = smack_d_instantiate,
3135
3136 .getprocattr = smack_getprocattr,
3137 .setprocattr = smack_setprocattr,
3138
3139 .unix_stream_connect = smack_unix_stream_connect,
3140 .unix_may_send = smack_unix_may_send,
3141
3142 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003143 .socket_connect = smack_socket_connect,
3144 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003145 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3146 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3147 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3148 .sk_alloc_security = smack_sk_alloc_security,
3149 .sk_free_security = smack_sk_free_security,
3150 .sock_graft = smack_sock_graft,
3151 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003152 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003153
Casey Schauflere114e472008-02-04 22:29:50 -08003154 /* key management security hooks */
3155#ifdef CONFIG_KEYS
3156 .key_alloc = smack_key_alloc,
3157 .key_free = smack_key_free,
3158 .key_permission = smack_key_permission,
3159#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003160
3161 /* Audit hooks */
3162#ifdef CONFIG_AUDIT
3163 .audit_rule_init = smack_audit_rule_init,
3164 .audit_rule_known = smack_audit_rule_known,
3165 .audit_rule_match = smack_audit_rule_match,
3166 .audit_rule_free = smack_audit_rule_free,
3167#endif /* CONFIG_AUDIT */
3168
Casey Schauflere114e472008-02-04 22:29:50 -08003169 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003170 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003171 .release_secctx = smack_release_secctx,
3172};
3173
Etienne Basset7198e2e2009-03-24 20:53:24 +01003174
3175static __init void init_smack_know_list(void)
3176{
3177 list_add(&smack_known_huh.list, &smack_known_list);
3178 list_add(&smack_known_hat.list, &smack_known_list);
3179 list_add(&smack_known_star.list, &smack_known_list);
3180 list_add(&smack_known_floor.list, &smack_known_list);
3181 list_add(&smack_known_invalid.list, &smack_known_list);
3182 list_add(&smack_known_web.list, &smack_known_list);
3183}
3184
Casey Schauflere114e472008-02-04 22:29:50 -08003185/**
3186 * smack_init - initialize the smack system
3187 *
3188 * Returns 0
3189 */
3190static __init int smack_init(void)
3191{
David Howellsd84f4f92008-11-14 10:39:23 +11003192 struct cred *cred;
3193
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003194 if (!security_module_enable(&smack_ops))
3195 return 0;
3196
Casey Schauflere114e472008-02-04 22:29:50 -08003197 printk(KERN_INFO "Smack: Initializing.\n");
3198
3199 /*
3200 * Set the security state for the initial task.
3201 */
David Howellsd84f4f92008-11-14 10:39:23 +11003202 cred = (struct cred *) current->cred;
3203 cred->security = &smack_known_floor.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003204
Etienne Basset7198e2e2009-03-24 20:53:24 +01003205 /* initilize the smack_know_list */
3206 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003207 /*
3208 * Initialize locks
3209 */
Casey Schauflere114e472008-02-04 22:29:50 -08003210 spin_lock_init(&smack_known_huh.smk_cipsolock);
3211 spin_lock_init(&smack_known_hat.smk_cipsolock);
3212 spin_lock_init(&smack_known_star.smk_cipsolock);
3213 spin_lock_init(&smack_known_floor.smk_cipsolock);
3214 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3215
3216 /*
3217 * Register with LSM
3218 */
3219 if (register_security(&smack_ops))
3220 panic("smack: Unable to register with kernel.\n");
3221
3222 return 0;
3223}
3224
3225/*
3226 * Smack requires early initialization in order to label
3227 * all processes and objects when they are created.
3228 */
3229security_initcall(smack_init);