blob: 0b3bb646f90e5e236b6a3c290b23d78a69291cf1 [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/**
Ingo Molnar9e488582009-05-07 19:26:19 +100094 * smack_ptrace_access_check - 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 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000102static int smack_ptrace_access_check(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
Ingo Molnar9e488582009-05-07 19:26:19 +1000108 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800109 if (rc != 0)
110 return rc;
111
Etienne Bassetecfcc532009-04-08 20:40:06 +0200112 sp = current_security();
113 tsp = task_security(ctp);
114 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
115 smk_ad_setfield_u_tsk(&ad, ctp);
116
117 /* we won't log here, because rc can be overriden */
118 rc = smk_access(sp, tsp, MAY_READWRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +0100119 if (rc != 0 && capable(CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200120 rc = 0;
121
122 smack_log(sp, tsp, MAY_READWRITE, rc, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100123 return rc;
124}
Casey Schauflere114e472008-02-04 22:29:50 -0800125
David Howells5cd9c582008-08-14 11:37:28 +0100126/**
127 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
128 * @ptp: parent task pointer
129 *
130 * Returns 0 if access is OK, an error code otherwise
131 *
132 * Do the capability checks, and require read and write.
133 */
134static int smack_ptrace_traceme(struct task_struct *ptp)
135{
136 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200137 struct smk_audit_info ad;
138 char *sp, *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100139
140 rc = cap_ptrace_traceme(ptp);
141 if (rc != 0)
142 return rc;
143
Etienne Bassetecfcc532009-04-08 20:40:06 +0200144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
145 smk_ad_setfield_u_tsk(&ad, ptp);
146
147 sp = current_security();
148 tsp = task_security(ptp);
149 /* we won't log here, because rc can be overriden */
150 rc = smk_access(tsp, sp, MAY_READWRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +0100151 if (rc != 0 && has_capability(ptp, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200152 rc = 0;
153
154 smack_log(tsp, sp, MAY_READWRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800155 return rc;
156}
157
158/**
159 * smack_syslog - Smack approval on syslog
160 * @type: message type
161 *
162 * Require that the task has the floor label
163 *
164 * Returns 0 on success, error code otherwise.
165 */
166static int smack_syslog(int type)
167{
168 int rc;
David Howells86a264a2008-11-14 10:39:18 +1100169 char *sp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800170
171 rc = cap_syslog(type);
172 if (rc != 0)
173 return rc;
174
175 if (capable(CAP_MAC_OVERRIDE))
176 return 0;
177
178 if (sp != smack_known_floor.smk_known)
179 rc = -EACCES;
180
181 return rc;
182}
183
184
185/*
186 * Superblock Hooks.
187 */
188
189/**
190 * smack_sb_alloc_security - allocate a superblock blob
191 * @sb: the superblock getting the blob
192 *
193 * Returns 0 on success or -ENOMEM on error.
194 */
195static int smack_sb_alloc_security(struct super_block *sb)
196{
197 struct superblock_smack *sbsp;
198
199 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
200
201 if (sbsp == NULL)
202 return -ENOMEM;
203
204 sbsp->smk_root = smack_known_floor.smk_known;
205 sbsp->smk_default = smack_known_floor.smk_known;
206 sbsp->smk_floor = smack_known_floor.smk_known;
207 sbsp->smk_hat = smack_known_hat.smk_known;
208 sbsp->smk_initialized = 0;
209 spin_lock_init(&sbsp->smk_sblock);
210
211 sb->s_security = sbsp;
212
213 return 0;
214}
215
216/**
217 * smack_sb_free_security - free a superblock blob
218 * @sb: the superblock getting the blob
219 *
220 */
221static void smack_sb_free_security(struct super_block *sb)
222{
223 kfree(sb->s_security);
224 sb->s_security = NULL;
225}
226
227/**
228 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800229 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800230 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800231 *
232 * Returns 0 on success or -ENOMEM on error.
233 *
234 * Copy the Smack specific mount options out of the mount
235 * options list.
236 */
Eric Parise0007522008-03-05 10:31:54 -0500237static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800238{
239 char *cp, *commap, *otheropts, *dp;
240
Casey Schauflere114e472008-02-04 22:29:50 -0800241 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
242 if (otheropts == NULL)
243 return -ENOMEM;
244
245 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
246 if (strstr(cp, SMK_FSDEFAULT) == cp)
247 dp = smackopts;
248 else if (strstr(cp, SMK_FSFLOOR) == cp)
249 dp = smackopts;
250 else if (strstr(cp, SMK_FSHAT) == cp)
251 dp = smackopts;
252 else if (strstr(cp, SMK_FSROOT) == cp)
253 dp = smackopts;
254 else
255 dp = otheropts;
256
257 commap = strchr(cp, ',');
258 if (commap != NULL)
259 *commap = '\0';
260
261 if (*dp != '\0')
262 strcat(dp, ",");
263 strcat(dp, cp);
264 }
265
266 strcpy(orig, otheropts);
267 free_page((unsigned long)otheropts);
268
269 return 0;
270}
271
272/**
273 * smack_sb_kern_mount - Smack specific mount processing
274 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100275 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800276 * @data: the smack mount options
277 *
278 * Returns 0 on success, an error code on failure
279 */
James Morris12204e22008-12-19 10:44:42 +1100280static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800281{
282 struct dentry *root = sb->s_root;
283 struct inode *inode = root->d_inode;
284 struct superblock_smack *sp = sb->s_security;
285 struct inode_smack *isp;
286 char *op;
287 char *commap;
288 char *nsp;
289
290 spin_lock(&sp->smk_sblock);
291 if (sp->smk_initialized != 0) {
292 spin_unlock(&sp->smk_sblock);
293 return 0;
294 }
295 sp->smk_initialized = 1;
296 spin_unlock(&sp->smk_sblock);
297
298 for (op = data; op != NULL; op = commap) {
299 commap = strchr(op, ',');
300 if (commap != NULL)
301 *commap++ = '\0';
302
303 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
304 op += strlen(SMK_FSHAT);
305 nsp = smk_import(op, 0);
306 if (nsp != NULL)
307 sp->smk_hat = nsp;
308 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
309 op += strlen(SMK_FSFLOOR);
310 nsp = smk_import(op, 0);
311 if (nsp != NULL)
312 sp->smk_floor = nsp;
313 } else if (strncmp(op, SMK_FSDEFAULT,
314 strlen(SMK_FSDEFAULT)) == 0) {
315 op += strlen(SMK_FSDEFAULT);
316 nsp = smk_import(op, 0);
317 if (nsp != NULL)
318 sp->smk_default = nsp;
319 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
320 op += strlen(SMK_FSROOT);
321 nsp = smk_import(op, 0);
322 if (nsp != NULL)
323 sp->smk_root = nsp;
324 }
325 }
326
327 /*
328 * Initialize the root inode.
329 */
330 isp = inode->i_security;
331 if (isp == NULL)
332 inode->i_security = new_inode_smack(sp->smk_root);
333 else
334 isp->smk_inode = sp->smk_root;
335
336 return 0;
337}
338
339/**
340 * smack_sb_statfs - Smack check on statfs
341 * @dentry: identifies the file system in question
342 *
343 * Returns 0 if current can read the floor of the filesystem,
344 * and error code otherwise
345 */
346static int smack_sb_statfs(struct dentry *dentry)
347{
348 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200349 int rc;
350 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800351
Etienne Bassetecfcc532009-04-08 20:40:06 +0200352 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
353 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
354
355 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
356 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800357}
358
359/**
360 * smack_sb_mount - Smack check for mounting
361 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800362 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800363 * @type: unused
364 * @flags: unused
365 * @data: unused
366 *
367 * Returns 0 if current can write the floor of the filesystem
368 * being mounted on, an error code otherwise.
369 */
Al Virob5266eb2008-03-22 17:48:24 -0400370static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800371 char *type, unsigned long flags, void *data)
372{
Al Virob5266eb2008-03-22 17:48:24 -0400373 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200374 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800375
Etienne Bassetecfcc532009-04-08 20:40:06 +0200376 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
377 smk_ad_setfield_u_fs_path(&ad, *path);
378
379 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800380}
381
382/**
383 * smack_sb_umount - Smack check for unmounting
384 * @mnt: file system to unmount
385 * @flags: unused
386 *
387 * Returns 0 if current can write the floor of the filesystem
388 * being unmounted, an error code otherwise.
389 */
390static int smack_sb_umount(struct vfsmount *mnt, int flags)
391{
392 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200393 struct smk_audit_info ad;
394
395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
396 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_mountpoint);
397 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
Casey Schauflere114e472008-02-04 22:29:50 -0800398
399 sbp = mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200400 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800401}
402
403/*
404 * Inode hooks
405 */
406
407/**
408 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800409 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800410 *
411 * Returns 0 if it gets a blob, -ENOMEM otherwise
412 */
413static int smack_inode_alloc_security(struct inode *inode)
414{
David Howells86a264a2008-11-14 10:39:18 +1100415 inode->i_security = new_inode_smack(current_security());
Casey Schauflere114e472008-02-04 22:29:50 -0800416 if (inode->i_security == NULL)
417 return -ENOMEM;
418 return 0;
419}
420
421/**
422 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800423 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800424 *
425 * Clears the blob pointer in inode
426 */
427static void smack_inode_free_security(struct inode *inode)
428{
429 kfree(inode->i_security);
430 inode->i_security = NULL;
431}
432
433/**
434 * smack_inode_init_security - copy out the smack from an inode
435 * @inode: the inode
436 * @dir: unused
437 * @name: where to put the attribute name
438 * @value: where to put the attribute value
439 * @len: where to put the length of the attribute
440 *
441 * Returns 0 if it all works out, -ENOMEM if there's no memory
442 */
443static int smack_inode_init_security(struct inode *inode, struct inode *dir,
444 char **name, void **value, size_t *len)
445{
446 char *isp = smk_of_inode(inode);
447
448 if (name) {
449 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
450 if (*name == NULL)
451 return -ENOMEM;
452 }
453
454 if (value) {
455 *value = kstrdup(isp, GFP_KERNEL);
456 if (*value == NULL)
457 return -ENOMEM;
458 }
459
460 if (len)
461 *len = strlen(isp) + 1;
462
463 return 0;
464}
465
466/**
467 * smack_inode_link - Smack check on link
468 * @old_dentry: the existing object
469 * @dir: unused
470 * @new_dentry: the new object
471 *
472 * Returns 0 if access is permitted, an error code otherwise
473 */
474static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
475 struct dentry *new_dentry)
476{
Casey Schauflere114e472008-02-04 22:29:50 -0800477 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200478 struct smk_audit_info ad;
479 int rc;
480
481 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
482 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800483
484 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200485 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800486
487 if (rc == 0 && new_dentry->d_inode != NULL) {
488 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200489 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
490 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800491 }
492
493 return rc;
494}
495
496/**
497 * smack_inode_unlink - Smack check on inode deletion
498 * @dir: containing directory object
499 * @dentry: file to unlink
500 *
501 * Returns 0 if current can write the containing directory
502 * and the object, error code otherwise
503 */
504static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
505{
506 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200507 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800508 int rc;
509
Etienne Bassetecfcc532009-04-08 20:40:06 +0200510 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
511 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
512
Casey Schauflere114e472008-02-04 22:29:50 -0800513 /*
514 * You need write access to the thing you're unlinking
515 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200516 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
517 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800518 /*
519 * You also need write access to the containing directory
520 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200521 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
522 smk_ad_setfield_u_fs_inode(&ad, dir);
523 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
524 }
Casey Schauflere114e472008-02-04 22:29:50 -0800525 return rc;
526}
527
528/**
529 * smack_inode_rmdir - Smack check on directory deletion
530 * @dir: containing directory object
531 * @dentry: directory to unlink
532 *
533 * Returns 0 if current can write the containing directory
534 * and the directory, error code otherwise
535 */
536static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
537{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200538 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800539 int rc;
540
Etienne Bassetecfcc532009-04-08 20:40:06 +0200541 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
542 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
543
Casey Schauflere114e472008-02-04 22:29:50 -0800544 /*
545 * You need write access to the thing you're removing
546 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200547 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
548 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800549 /*
550 * You also need write access to the containing directory
551 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200552 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
553 smk_ad_setfield_u_fs_inode(&ad, dir);
554 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
555 }
Casey Schauflere114e472008-02-04 22:29:50 -0800556
557 return rc;
558}
559
560/**
561 * smack_inode_rename - Smack check on rename
562 * @old_inode: the old directory
563 * @old_dentry: unused
564 * @new_inode: the new directory
565 * @new_dentry: unused
566 *
567 * Read and write access is required on both the old and
568 * new directories.
569 *
570 * Returns 0 if access is permitted, an error code otherwise
571 */
572static int smack_inode_rename(struct inode *old_inode,
573 struct dentry *old_dentry,
574 struct inode *new_inode,
575 struct dentry *new_dentry)
576{
577 int rc;
578 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200579 struct smk_audit_info ad;
580
581 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
582 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800583
584 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200585 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800586
587 if (rc == 0 && new_dentry->d_inode != NULL) {
588 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200589 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
590 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800591 }
Casey Schauflere114e472008-02-04 22:29:50 -0800592 return rc;
593}
594
595/**
596 * smack_inode_permission - Smack version of permission()
597 * @inode: the inode in question
598 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800599 *
600 * This is the important Smack hook.
601 *
602 * Returns 0 if access is permitted, -EACCES otherwise
603 */
Al Virob77b0642008-07-17 09:37:02 -0400604static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800605{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200606 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800607 /*
608 * No permission to check. Existence test. Yup, it's there.
609 */
610 if (mask == 0)
611 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
613 smk_ad_setfield_u_fs_inode(&ad, inode);
614 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800615}
616
617/**
618 * smack_inode_setattr - Smack check for setting attributes
619 * @dentry: the object
620 * @iattr: for the force flag
621 *
622 * Returns 0 if access is permitted, an error code otherwise
623 */
624static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
625{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200626 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800627 /*
628 * Need to allow for clearing the setuid bit.
629 */
630 if (iattr->ia_valid & ATTR_FORCE)
631 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200632 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
633 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800634
Etienne Bassetecfcc532009-04-08 20:40:06 +0200635 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800636}
637
638/**
639 * smack_inode_getattr - Smack check for getting attributes
640 * @mnt: unused
641 * @dentry: the object
642 *
643 * Returns 0 if access is permitted, an error code otherwise
644 */
645static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
646{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200647 struct smk_audit_info ad;
648
649 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
650 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
651 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
652 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800653}
654
655/**
656 * smack_inode_setxattr - Smack check for setting xattrs
657 * @dentry: the object
658 * @name: name of the attribute
659 * @value: unused
660 * @size: unused
661 * @flags: unused
662 *
663 * This protects the Smack attribute explicitly.
664 *
665 * Returns 0 if access is permitted, an error code otherwise
666 */
David Howells8f0cfa52008-04-29 00:59:41 -0700667static int smack_inode_setxattr(struct dentry *dentry, const char *name,
668 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800669{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200670 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800671 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800672
Casey Schauflerbcdca222008-02-23 15:24:04 -0800673 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
674 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
675 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
676 if (!capable(CAP_MAC_ADMIN))
677 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200678 /*
679 * check label validity here so import wont fail on
680 * post_setxattr
681 */
682 if (size == 0 || size >= SMK_LABELLEN ||
683 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400684 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800685 } else
686 rc = cap_inode_setxattr(dentry, name, value, size, flags);
687
Etienne Bassetecfcc532009-04-08 20:40:06 +0200688 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
689 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
690
Casey Schauflerbcdca222008-02-23 15:24:04 -0800691 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200692 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800693
694 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800695}
696
697/**
698 * smack_inode_post_setxattr - Apply the Smack update approved above
699 * @dentry: object
700 * @name: attribute name
701 * @value: attribute value
702 * @size: attribute size
703 * @flags: unused
704 *
705 * Set the pointer in the inode blob to the entry found
706 * in the master label list.
707 */
David Howells8f0cfa52008-04-29 00:59:41 -0700708static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
709 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800710{
711 struct inode_smack *isp;
712 char *nsp;
713
714 /*
715 * Not SMACK
716 */
717 if (strcmp(name, XATTR_NAME_SMACK))
718 return;
719
Casey Schauflere114e472008-02-04 22:29:50 -0800720 isp = dentry->d_inode->i_security;
721
722 /*
723 * No locking is done here. This is a pointer
724 * assignment.
725 */
726 nsp = smk_import(value, size);
727 if (nsp != NULL)
728 isp->smk_inode = nsp;
729 else
730 isp->smk_inode = smack_known_invalid.smk_known;
731
732 return;
733}
734
735/*
736 * smack_inode_getxattr - Smack check on getxattr
737 * @dentry: the object
738 * @name: unused
739 *
740 * Returns 0 if access is permitted, an error code otherwise
741 */
David Howells8f0cfa52008-04-29 00:59:41 -0700742static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800743{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200744 struct smk_audit_info ad;
745
746 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
747 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
748
749 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800750}
751
752/*
753 * smack_inode_removexattr - Smack check on removexattr
754 * @dentry: the object
755 * @name: name of the attribute
756 *
757 * Removing the Smack attribute requires CAP_MAC_ADMIN
758 *
759 * Returns 0 if access is permitted, an error code otherwise
760 */
David Howells8f0cfa52008-04-29 00:59:41 -0700761static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800762{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200763 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800764 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800765
Casey Schauflerbcdca222008-02-23 15:24:04 -0800766 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
767 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
768 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
769 if (!capable(CAP_MAC_ADMIN))
770 rc = -EPERM;
771 } else
772 rc = cap_inode_removexattr(dentry, name);
773
Etienne Bassetecfcc532009-04-08 20:40:06 +0200774 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
775 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800776 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200777 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800778
779 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800780}
781
782/**
783 * smack_inode_getsecurity - get smack xattrs
784 * @inode: the object
785 * @name: attribute name
786 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800787 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800788 *
789 * Returns the size of the attribute or an error code
790 */
791static int smack_inode_getsecurity(const struct inode *inode,
792 const char *name, void **buffer,
793 bool alloc)
794{
795 struct socket_smack *ssp;
796 struct socket *sock;
797 struct super_block *sbp;
798 struct inode *ip = (struct inode *)inode;
799 char *isp;
800 int ilen;
801 int rc = 0;
802
803 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
804 isp = smk_of_inode(inode);
805 ilen = strlen(isp) + 1;
806 *buffer = isp;
807 return ilen;
808 }
809
810 /*
811 * The rest of the Smack xattrs are only on sockets.
812 */
813 sbp = ip->i_sb;
814 if (sbp->s_magic != SOCKFS_MAGIC)
815 return -EOPNOTSUPP;
816
817 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800818 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800819 return -EOPNOTSUPP;
820
821 ssp = sock->sk->sk_security;
822
823 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
824 isp = ssp->smk_in;
825 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
826 isp = ssp->smk_out;
827 else
828 return -EOPNOTSUPP;
829
830 ilen = strlen(isp) + 1;
831 if (rc == 0) {
832 *buffer = isp;
833 rc = ilen;
834 }
835
836 return rc;
837}
838
839
840/**
841 * smack_inode_listsecurity - list the Smack attributes
842 * @inode: the object
843 * @buffer: where they go
844 * @buffer_size: size of buffer
845 *
846 * Returns 0 on success, -EINVAL otherwise
847 */
848static int smack_inode_listsecurity(struct inode *inode, char *buffer,
849 size_t buffer_size)
850{
851 int len = strlen(XATTR_NAME_SMACK);
852
853 if (buffer != NULL && len <= buffer_size) {
854 memcpy(buffer, XATTR_NAME_SMACK, len);
855 return len;
856 }
857 return -EINVAL;
858}
859
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +1000860/**
861 * smack_inode_getsecid - Extract inode's security id
862 * @inode: inode to extract the info from
863 * @secid: where result will be saved
864 */
865static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
866{
867 struct inode_smack *isp = inode->i_security;
868
869 *secid = smack_to_secid(isp->smk_inode);
870}
871
Casey Schauflere114e472008-02-04 22:29:50 -0800872/*
873 * File Hooks
874 */
875
876/**
877 * smack_file_permission - Smack check on file operations
878 * @file: unused
879 * @mask: unused
880 *
881 * Returns 0
882 *
883 * Should access checks be done on each read or write?
884 * UNICOS and SELinux say yes.
885 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
886 *
887 * I'll say no for now. Smack does not do the frequent
888 * label changing that SELinux does.
889 */
890static int smack_file_permission(struct file *file, int mask)
891{
892 return 0;
893}
894
895/**
896 * smack_file_alloc_security - assign a file security blob
897 * @file: the object
898 *
899 * The security blob for a file is a pointer to the master
900 * label list, so no allocation is done.
901 *
902 * Returns 0
903 */
904static int smack_file_alloc_security(struct file *file)
905{
David Howells86a264a2008-11-14 10:39:18 +1100906 file->f_security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -0800907 return 0;
908}
909
910/**
911 * smack_file_free_security - clear a file security blob
912 * @file: the object
913 *
914 * The security blob for a file is a pointer to the master
915 * label list, so no memory is freed.
916 */
917static void smack_file_free_security(struct file *file)
918{
919 file->f_security = NULL;
920}
921
922/**
923 * smack_file_ioctl - Smack check on ioctls
924 * @file: the object
925 * @cmd: what to do
926 * @arg: unused
927 *
928 * Relies heavily on the correct use of the ioctl command conventions.
929 *
930 * Returns 0 if allowed, error code otherwise
931 */
932static int smack_file_ioctl(struct file *file, unsigned int cmd,
933 unsigned long arg)
934{
935 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200936 struct smk_audit_info ad;
937
938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
939 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -0800940
941 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200942 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800943
944 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +0200945 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800946
947 return rc;
948}
949
950/**
951 * smack_file_lock - Smack check on file locking
952 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -0800953 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800954 *
955 * Returns 0 if current has write access, error code otherwise
956 */
957static int smack_file_lock(struct file *file, unsigned int cmd)
958{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200959 struct smk_audit_info ad;
960
961 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
962 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
963 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800964}
965
966/**
967 * smack_file_fcntl - Smack check on fcntl
968 * @file: the object
969 * @cmd: what action to check
970 * @arg: unused
971 *
972 * Returns 0 if current has access, error code otherwise
973 */
974static int smack_file_fcntl(struct file *file, unsigned int cmd,
975 unsigned long arg)
976{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200977 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800978 int rc;
979
Etienne Bassetecfcc532009-04-08 20:40:06 +0200980 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
981 smk_ad_setfield_u_fs_path(&ad, file->f_path);
982
Casey Schauflere114e472008-02-04 22:29:50 -0800983 switch (cmd) {
984 case F_DUPFD:
985 case F_GETFD:
986 case F_GETFL:
987 case F_GETLK:
988 case F_GETOWN:
989 case F_GETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +0200990 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800991 break;
992 case F_SETFD:
993 case F_SETFL:
994 case F_SETLK:
995 case F_SETLKW:
996 case F_SETOWN:
997 case F_SETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +0200998 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800999 break;
1000 default:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001001 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001002 }
1003
1004 return rc;
1005}
1006
1007/**
1008 * smack_file_set_fowner - set the file security blob value
1009 * @file: object in question
1010 *
1011 * Returns 0
1012 * Further research may be required on this one.
1013 */
1014static int smack_file_set_fowner(struct file *file)
1015{
David Howells86a264a2008-11-14 10:39:18 +11001016 file->f_security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001017 return 0;
1018}
1019
1020/**
1021 * smack_file_send_sigiotask - Smack on sigio
1022 * @tsk: The target task
1023 * @fown: the object the signal come from
1024 * @signum: unused
1025 *
1026 * Allow a privileged task to get signals even if it shouldn't
1027 *
1028 * Returns 0 if a subject with the object's smack could
1029 * write to the task, an error code otherwise.
1030 */
1031static int smack_file_send_sigiotask(struct task_struct *tsk,
1032 struct fown_struct *fown, int signum)
1033{
1034 struct file *file;
1035 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001036 char *tsp = tsk->cred->security;
1037 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001038
1039 /*
1040 * struct fown_struct is never outside the context of a struct file
1041 */
1042 file = container_of(fown, struct file, f_owner);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001043 /* we don't log here as rc can be overriden */
1044 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001045 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001046 rc = 0;
1047
1048 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1049 smk_ad_setfield_u_tsk(&ad, tsk);
1050 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001051 return rc;
1052}
1053
1054/**
1055 * smack_file_receive - Smack file receive check
1056 * @file: the object
1057 *
1058 * Returns 0 if current has access, error code otherwise
1059 */
1060static int smack_file_receive(struct file *file)
1061{
1062 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001063 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001064
Etienne Bassetecfcc532009-04-08 20:40:06 +02001065 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1066 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001067 /*
1068 * This code relies on bitmasks.
1069 */
1070 if (file->f_mode & FMODE_READ)
1071 may = MAY_READ;
1072 if (file->f_mode & FMODE_WRITE)
1073 may |= MAY_WRITE;
1074
Etienne Bassetecfcc532009-04-08 20:40:06 +02001075 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001076}
1077
1078/*
1079 * Task hooks
1080 */
1081
1082/**
David Howellsee18d642009-09-02 09:14:21 +01001083 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1084 * @new: the new credentials
1085 * @gfp: the atomicity of any memory allocations
1086 *
1087 * Prepare a blank set of credentials for modification. This must allocate all
1088 * the memory the LSM module might require such that cred_transfer() can
1089 * complete without error.
1090 */
1091static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1092{
1093 cred->security = NULL;
1094 return 0;
1095}
1096
1097
1098/**
David Howellsf1752ee2008-11-14 10:39:17 +11001099 * smack_cred_free - "free" task-level security credentials
1100 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001101 *
1102 * Smack isn't using copies of blobs. Everyone
1103 * points to an immutable list. The blobs never go away.
1104 * There is no leak here.
1105 */
David Howellsf1752ee2008-11-14 10:39:17 +11001106static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001107{
David Howellsf1752ee2008-11-14 10:39:17 +11001108 cred->security = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001109}
1110
1111/**
David Howellsd84f4f92008-11-14 10:39:23 +11001112 * smack_cred_prepare - prepare new set of credentials for modification
1113 * @new: the new credentials
1114 * @old: the original credentials
1115 * @gfp: the atomicity of any memory allocations
1116 *
1117 * Prepare a new set of credentials for modification.
1118 */
1119static int smack_cred_prepare(struct cred *new, const struct cred *old,
1120 gfp_t gfp)
1121{
1122 new->security = old->security;
1123 return 0;
1124}
1125
Randy Dunlap251a2a92009-02-18 11:42:33 -08001126/**
1127 * smack_cred_commit - commit new credentials
David Howellsd84f4f92008-11-14 10:39:23 +11001128 * @new: the new credentials
1129 * @old: the original credentials
1130 */
1131static void smack_cred_commit(struct cred *new, const struct cred *old)
1132{
1133}
1134
1135/**
David Howellsee18d642009-09-02 09:14:21 +01001136 * smack_cred_transfer - Transfer the old credentials to the new credentials
1137 * @new: the new credentials
1138 * @old: the original credentials
1139 *
1140 * Fill in a set of blank credentials from another set of credentials.
1141 */
1142static void smack_cred_transfer(struct cred *new, const struct cred *old)
1143{
1144 new->security = old->security;
1145}
1146
1147/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001148 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001149 * @new: points to the set of credentials to be modified.
1150 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001151 *
1152 * Set the security data for a kernel service.
1153 */
1154static int smack_kernel_act_as(struct cred *new, u32 secid)
1155{
1156 char *smack = smack_from_secid(secid);
1157
1158 if (smack == NULL)
1159 return -EINVAL;
1160
1161 new->security = smack;
1162 return 0;
1163}
1164
1165/**
1166 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001167 * @new: points to the set of credentials to be modified
1168 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001169 *
1170 * Set the file creation context in a set of credentials to the same
1171 * as the objective context of the specified inode
1172 */
1173static int smack_kernel_create_files_as(struct cred *new,
1174 struct inode *inode)
1175{
1176 struct inode_smack *isp = inode->i_security;
1177
1178 new->security = isp->smk_inode;
1179 return 0;
1180}
1181
1182/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001183 * smk_curacc_on_task - helper to log task related access
1184 * @p: the task object
1185 * @access : the access requested
1186 *
1187 * Return 0 if access is permitted
1188 */
1189static int smk_curacc_on_task(struct task_struct *p, int access)
1190{
1191 struct smk_audit_info ad;
1192
1193 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1194 smk_ad_setfield_u_tsk(&ad, p);
1195 return smk_curacc(task_security(p), access, &ad);
1196}
1197
1198/**
Casey Schauflere114e472008-02-04 22:29:50 -08001199 * smack_task_setpgid - Smack check on setting pgid
1200 * @p: the task object
1201 * @pgid: unused
1202 *
1203 * Return 0 if write access is permitted
1204 */
1205static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1206{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001207 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001208}
1209
1210/**
1211 * smack_task_getpgid - Smack access check for getpgid
1212 * @p: the object task
1213 *
1214 * Returns 0 if current can read the object task, error code otherwise
1215 */
1216static int smack_task_getpgid(struct task_struct *p)
1217{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001218 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001219}
1220
1221/**
1222 * smack_task_getsid - Smack access check for getsid
1223 * @p: the object task
1224 *
1225 * Returns 0 if current can read the object task, error code otherwise
1226 */
1227static int smack_task_getsid(struct task_struct *p)
1228{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001229 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001230}
1231
1232/**
1233 * smack_task_getsecid - get the secid of the task
1234 * @p: the object task
1235 * @secid: where to put the result
1236 *
1237 * Sets the secid to contain a u32 version of the smack label.
1238 */
1239static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1240{
David Howellsc69e8d92008-11-14 10:39:19 +11001241 *secid = smack_to_secid(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001242}
1243
1244/**
1245 * smack_task_setnice - Smack check on setting nice
1246 * @p: the task object
1247 * @nice: unused
1248 *
1249 * Return 0 if write access is permitted
1250 */
1251static int smack_task_setnice(struct task_struct *p, int nice)
1252{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001253 int rc;
1254
1255 rc = cap_task_setnice(p, nice);
1256 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001257 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001258 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001259}
1260
1261/**
1262 * smack_task_setioprio - Smack check on setting ioprio
1263 * @p: the task object
1264 * @ioprio: unused
1265 *
1266 * Return 0 if write access is permitted
1267 */
1268static int smack_task_setioprio(struct task_struct *p, int ioprio)
1269{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001270 int rc;
1271
1272 rc = cap_task_setioprio(p, ioprio);
1273 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001274 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001275 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001276}
1277
1278/**
1279 * smack_task_getioprio - Smack check on reading ioprio
1280 * @p: the task object
1281 *
1282 * Return 0 if read access is permitted
1283 */
1284static int smack_task_getioprio(struct task_struct *p)
1285{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001286 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001287}
1288
1289/**
1290 * smack_task_setscheduler - Smack check on setting scheduler
1291 * @p: the task object
1292 * @policy: unused
1293 * @lp: unused
1294 *
1295 * Return 0 if read access is permitted
1296 */
1297static int smack_task_setscheduler(struct task_struct *p, int policy,
1298 struct sched_param *lp)
1299{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001300 int rc;
1301
1302 rc = cap_task_setscheduler(p, policy, lp);
1303 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001304 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001305 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001306}
1307
1308/**
1309 * smack_task_getscheduler - Smack check on reading scheduler
1310 * @p: the task object
1311 *
1312 * Return 0 if read access is permitted
1313 */
1314static int smack_task_getscheduler(struct task_struct *p)
1315{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001316 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001317}
1318
1319/**
1320 * smack_task_movememory - Smack check on moving memory
1321 * @p: the task object
1322 *
1323 * Return 0 if write access is permitted
1324 */
1325static int smack_task_movememory(struct task_struct *p)
1326{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001327 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001328}
1329
1330/**
1331 * smack_task_kill - Smack check on signal delivery
1332 * @p: the task object
1333 * @info: unused
1334 * @sig: unused
1335 * @secid: identifies the smack to use in lieu of current's
1336 *
1337 * Return 0 if write access is permitted
1338 *
1339 * The secid behavior is an artifact of an SELinux hack
1340 * in the USB code. Someday it may go away.
1341 */
1342static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1343 int sig, u32 secid)
1344{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001345 struct smk_audit_info ad;
1346
1347 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1348 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001349 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001350 * Sending a signal requires that the sender
1351 * can write the receiver.
1352 */
1353 if (secid == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001354 return smk_curacc(task_security(p), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001355 /*
1356 * If the secid isn't 0 we're dealing with some USB IO
1357 * specific behavior. This is not clean. For one thing
1358 * we can't take privilege into account.
1359 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360 return smk_access(smack_from_secid(secid), task_security(p),
1361 MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001362}
1363
1364/**
1365 * smack_task_wait - Smack access check for waiting
1366 * @p: task to wait for
1367 *
1368 * Returns 0 if current can wait for p, error code otherwise
1369 */
1370static int smack_task_wait(struct task_struct *p)
1371{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001372 struct smk_audit_info ad;
1373 char *sp = current_security();
1374 char *tsp = task_security(p);
Casey Schauflere114e472008-02-04 22:29:50 -08001375 int rc;
1376
Etienne Bassetecfcc532009-04-08 20:40:06 +02001377 /* we don't log here, we can be overriden */
1378 rc = smk_access(sp, tsp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001379 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001380 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001381
1382 /*
1383 * Allow the operation to succeed if either task
1384 * has privilege to perform operations that might
1385 * account for the smack labels having gotten to
1386 * be different in the first place.
1387 *
David Howells5cd9c582008-08-14 11:37:28 +01001388 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001389 * control ideal, taking the object's privilege
1390 * state into account in the decision as well as
1391 * the smack value.
1392 */
David Howells5cd9c582008-08-14 11:37:28 +01001393 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001394 rc = 0;
1395 /* we log only if we didn't get overriden */
1396 out_log:
1397 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1398 smk_ad_setfield_u_tsk(&ad, p);
1399 smack_log(sp, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001400 return rc;
1401}
1402
1403/**
1404 * smack_task_to_inode - copy task smack into the inode blob
1405 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001406 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001407 *
1408 * Sets the smack pointer in the inode security blob
1409 */
1410static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1411{
1412 struct inode_smack *isp = inode->i_security;
David Howellsc69e8d92008-11-14 10:39:19 +11001413 isp->smk_inode = task_security(p);
Casey Schauflere114e472008-02-04 22:29:50 -08001414}
1415
1416/*
1417 * Socket hooks.
1418 */
1419
1420/**
1421 * smack_sk_alloc_security - Allocate a socket blob
1422 * @sk: the socket
1423 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001424 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001425 *
1426 * Assign Smack pointers to current
1427 *
1428 * Returns 0 on success, -ENOMEM is there's no memory
1429 */
1430static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1431{
David Howells86a264a2008-11-14 10:39:18 +11001432 char *csp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001433 struct socket_smack *ssp;
1434
1435 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1436 if (ssp == NULL)
1437 return -ENOMEM;
1438
1439 ssp->smk_in = csp;
1440 ssp->smk_out = csp;
1441 ssp->smk_packet[0] = '\0';
1442
1443 sk->sk_security = ssp;
1444
1445 return 0;
1446}
1447
1448/**
1449 * smack_sk_free_security - Free a socket blob
1450 * @sk: the socket
1451 *
1452 * Clears the blob pointer
1453 */
1454static void smack_sk_free_security(struct sock *sk)
1455{
1456 kfree(sk->sk_security);
1457}
1458
1459/**
Paul Moore07feee82009-03-27 17:10:54 -04001460* smack_host_label - check host based restrictions
1461* @sip: the object end
1462*
1463* looks for host based access restrictions
1464*
1465* This version will only be appropriate for really small sets of single label
1466* hosts. The caller is responsible for ensuring that the RCU read lock is
1467* taken before calling this function.
1468*
1469* Returns the label of the far end or NULL if it's not special.
1470*/
1471static char *smack_host_label(struct sockaddr_in *sip)
1472{
1473 struct smk_netlbladdr *snp;
1474 struct in_addr *siap = &sip->sin_addr;
1475
1476 if (siap->s_addr == 0)
1477 return NULL;
1478
1479 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1480 /*
1481 * we break after finding the first match because
1482 * the list is sorted from longest to shortest mask
1483 * so we have found the most specific match
1484 */
1485 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001486 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1487 /* we have found the special CIPSO option */
1488 if (snp->smk_label == smack_cipso_option)
1489 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001490 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001491 }
Paul Moore07feee82009-03-27 17:10:54 -04001492
1493 return NULL;
1494}
1495
1496/**
Casey Schauflere114e472008-02-04 22:29:50 -08001497 * smack_set_catset - convert a capset to netlabel mls categories
1498 * @catset: the Smack categories
1499 * @sap: where to put the netlabel categories
1500 *
1501 * Allocates and fills attr.mls.cat
1502 */
1503static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1504{
1505 unsigned char *cp;
1506 unsigned char m;
1507 int cat;
1508 int rc;
1509 int byte;
1510
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001511 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001512 return;
1513
1514 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1515 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1516 sap->attr.mls.cat->startbit = 0;
1517
1518 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1519 for (m = 0x80; m != 0; m >>= 1, cat++) {
1520 if ((m & *cp) == 0)
1521 continue;
1522 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1523 cat, GFP_ATOMIC);
1524 }
1525}
1526
1527/**
1528 * smack_to_secattr - fill a secattr from a smack value
1529 * @smack: the smack value
1530 * @nlsp: where the result goes
1531 *
1532 * Casey says that CIPSO is good enough for now.
1533 * It can be used to effect.
1534 * It can also be abused to effect when necessary.
1535 * Appologies to the TSIG group in general and GW in particular.
1536 */
1537static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1538{
1539 struct smack_cipso cipso;
1540 int rc;
1541
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001542 nlsp->domain = smack;
1543 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001544
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001545 rc = smack_to_cipso(smack, &cipso);
1546 if (rc == 0) {
1547 nlsp->attr.mls.lvl = cipso.smk_level;
1548 smack_set_catset(cipso.smk_catset, nlsp);
1549 } else {
1550 nlsp->attr.mls.lvl = smack_cipso_direct;
1551 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001552 }
1553}
1554
1555/**
1556 * smack_netlabel - Set the secattr on a socket
1557 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001558 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001559 *
1560 * Convert the outbound smack value (smk_out) to a
1561 * secattr and attach it to the socket.
1562 *
1563 * Returns 0 on success or an error code
1564 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001565static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001566{
Paul Moore07feee82009-03-27 17:10:54 -04001567 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001568 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001569 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001570
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001571 /*
1572 * Usually the netlabel code will handle changing the
1573 * packet labeling based on the label.
1574 * The case of a single label host is different, because
1575 * a single label host should never get a labeled packet
1576 * even though the label is usually associated with a packet
1577 * label.
1578 */
1579 local_bh_disable();
1580 bh_lock_sock_nested(sk);
1581
1582 if (ssp->smk_out == smack_net_ambient ||
1583 labeled == SMACK_UNLABELED_SOCKET)
1584 netlbl_sock_delattr(sk);
1585 else {
1586 netlbl_secattr_init(&secattr);
1587 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001588 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001589 netlbl_secattr_destroy(&secattr);
1590 }
1591
1592 bh_unlock_sock(sk);
1593 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001594
Casey Schauflere114e472008-02-04 22:29:50 -08001595 return rc;
1596}
1597
1598/**
Paul Moore07feee82009-03-27 17:10:54 -04001599 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1600 * @sk: the socket
1601 * @sap: the destination address
1602 *
1603 * Set the correct secattr for the given socket based on the destination
1604 * address and perform any outbound access checks needed.
1605 *
1606 * Returns 0 on success or an error code.
1607 *
1608 */
1609static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1610{
1611 int rc;
1612 int sk_lbl;
1613 char *hostsp;
1614 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001615 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001616
1617 rcu_read_lock();
1618 hostsp = smack_host_label(sap);
1619 if (hostsp != NULL) {
1620 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001621#ifdef CONFIG_AUDIT
1622 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1623 ad.a.u.net.family = sap->sin_family;
1624 ad.a.u.net.dport = sap->sin_port;
1625 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1626#endif
1627 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001628 } else {
1629 sk_lbl = SMACK_CIPSO_SOCKET;
1630 rc = 0;
1631 }
1632 rcu_read_unlock();
1633 if (rc != 0)
1634 return rc;
1635
1636 return smack_netlabel(sk, sk_lbl);
1637}
1638
1639/**
Casey Schauflere114e472008-02-04 22:29:50 -08001640 * smack_inode_setsecurity - set smack xattrs
1641 * @inode: the object
1642 * @name: attribute name
1643 * @value: attribute value
1644 * @size: size of the attribute
1645 * @flags: unused
1646 *
1647 * Sets the named attribute in the appropriate blob
1648 *
1649 * Returns 0 on success, or an error code
1650 */
1651static int smack_inode_setsecurity(struct inode *inode, const char *name,
1652 const void *value, size_t size, int flags)
1653{
1654 char *sp;
1655 struct inode_smack *nsp = inode->i_security;
1656 struct socket_smack *ssp;
1657 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001658 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001659
Etienne Basset43031542009-03-27 17:11:01 -04001660 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001661 return -EACCES;
1662
1663 sp = smk_import(value, size);
1664 if (sp == NULL)
1665 return -EINVAL;
1666
1667 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1668 nsp->smk_inode = sp;
1669 return 0;
1670 }
1671 /*
1672 * The rest of the Smack xattrs are only on sockets.
1673 */
1674 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1675 return -EOPNOTSUPP;
1676
1677 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001678 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001679 return -EOPNOTSUPP;
1680
1681 ssp = sock->sk->sk_security;
1682
1683 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1684 ssp->smk_in = sp;
1685 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1686 ssp->smk_out = sp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001687 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001688 if (rc != 0)
1689 printk(KERN_WARNING "Smack: \"%s\" netlbl error %d.\n",
1690 __func__, -rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001691 } else
1692 return -EOPNOTSUPP;
1693
1694 return 0;
1695}
1696
1697/**
1698 * smack_socket_post_create - finish socket setup
1699 * @sock: the socket
1700 * @family: protocol family
1701 * @type: unused
1702 * @protocol: unused
1703 * @kern: unused
1704 *
1705 * Sets the netlabel information on the socket
1706 *
1707 * Returns 0 on success, and error code otherwise
1708 */
1709static int smack_socket_post_create(struct socket *sock, int family,
1710 int type, int protocol, int kern)
1711{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001712 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001713 return 0;
1714 /*
1715 * Set the outbound netlbl.
1716 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001717 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1718}
1719
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001720/**
1721 * smack_socket_connect - connect access check
1722 * @sock: the socket
1723 * @sap: the other end
1724 * @addrlen: size of sap
1725 *
1726 * Verifies that a connection may be possible
1727 *
1728 * Returns 0 on success, and error code otherwise
1729 */
1730static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1731 int addrlen)
1732{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001733 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1734 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001735 if (addrlen < sizeof(struct sockaddr_in))
1736 return -EINVAL;
1737
Paul Moore07feee82009-03-27 17:10:54 -04001738 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08001739}
1740
1741/**
1742 * smack_flags_to_may - convert S_ to MAY_ values
1743 * @flags: the S_ value
1744 *
1745 * Returns the equivalent MAY_ value
1746 */
1747static int smack_flags_to_may(int flags)
1748{
1749 int may = 0;
1750
1751 if (flags & S_IRUGO)
1752 may |= MAY_READ;
1753 if (flags & S_IWUGO)
1754 may |= MAY_WRITE;
1755 if (flags & S_IXUGO)
1756 may |= MAY_EXEC;
1757
1758 return may;
1759}
1760
1761/**
1762 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
1763 * @msg: the object
1764 *
1765 * Returns 0
1766 */
1767static int smack_msg_msg_alloc_security(struct msg_msg *msg)
1768{
David Howells86a264a2008-11-14 10:39:18 +11001769 msg->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001770 return 0;
1771}
1772
1773/**
1774 * smack_msg_msg_free_security - Clear the security blob for msg_msg
1775 * @msg: the object
1776 *
1777 * Clears the blob pointer
1778 */
1779static void smack_msg_msg_free_security(struct msg_msg *msg)
1780{
1781 msg->security = NULL;
1782}
1783
1784/**
1785 * smack_of_shm - the smack pointer for the shm
1786 * @shp: the object
1787 *
1788 * Returns a pointer to the smack value
1789 */
1790static char *smack_of_shm(struct shmid_kernel *shp)
1791{
1792 return (char *)shp->shm_perm.security;
1793}
1794
1795/**
1796 * smack_shm_alloc_security - Set the security blob for shm
1797 * @shp: the object
1798 *
1799 * Returns 0
1800 */
1801static int smack_shm_alloc_security(struct shmid_kernel *shp)
1802{
1803 struct kern_ipc_perm *isp = &shp->shm_perm;
1804
David Howells86a264a2008-11-14 10:39:18 +11001805 isp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001806 return 0;
1807}
1808
1809/**
1810 * smack_shm_free_security - Clear the security blob for shm
1811 * @shp: the object
1812 *
1813 * Clears the blob pointer
1814 */
1815static void smack_shm_free_security(struct shmid_kernel *shp)
1816{
1817 struct kern_ipc_perm *isp = &shp->shm_perm;
1818
1819 isp->security = NULL;
1820}
1821
1822/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001823 * smk_curacc_shm : check if current has access on shm
1824 * @shp : the object
1825 * @access : access requested
1826 *
1827 * Returns 0 if current has the requested access, error code otherwise
1828 */
1829static int smk_curacc_shm(struct shmid_kernel *shp, int access)
1830{
1831 char *ssp = smack_of_shm(shp);
1832 struct smk_audit_info ad;
1833
1834#ifdef CONFIG_AUDIT
1835 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1836 ad.a.u.ipc_id = shp->shm_perm.id;
1837#endif
1838 return smk_curacc(ssp, access, &ad);
1839}
1840
1841/**
Casey Schauflere114e472008-02-04 22:29:50 -08001842 * smack_shm_associate - Smack access check for shm
1843 * @shp: the object
1844 * @shmflg: access requested
1845 *
1846 * Returns 0 if current has the requested access, error code otherwise
1847 */
1848static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
1849{
Casey Schauflere114e472008-02-04 22:29:50 -08001850 int may;
1851
1852 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001853 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001854}
1855
1856/**
1857 * smack_shm_shmctl - Smack access check for shm
1858 * @shp: the object
1859 * @cmd: what it wants to do
1860 *
1861 * Returns 0 if current has the requested access, error code otherwise
1862 */
1863static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
1864{
Casey Schauflere114e472008-02-04 22:29:50 -08001865 int may;
1866
1867 switch (cmd) {
1868 case IPC_STAT:
1869 case SHM_STAT:
1870 may = MAY_READ;
1871 break;
1872 case IPC_SET:
1873 case SHM_LOCK:
1874 case SHM_UNLOCK:
1875 case IPC_RMID:
1876 may = MAY_READWRITE;
1877 break;
1878 case IPC_INFO:
1879 case SHM_INFO:
1880 /*
1881 * System level information.
1882 */
1883 return 0;
1884 default:
1885 return -EINVAL;
1886 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02001887 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001888}
1889
1890/**
1891 * smack_shm_shmat - Smack access for shmat
1892 * @shp: the object
1893 * @shmaddr: unused
1894 * @shmflg: access requested
1895 *
1896 * Returns 0 if current has the requested access, error code otherwise
1897 */
1898static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
1899 int shmflg)
1900{
Casey Schauflere114e472008-02-04 22:29:50 -08001901 int may;
1902
1903 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001904 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001905}
1906
1907/**
1908 * smack_of_sem - the smack pointer for the sem
1909 * @sma: the object
1910 *
1911 * Returns a pointer to the smack value
1912 */
1913static char *smack_of_sem(struct sem_array *sma)
1914{
1915 return (char *)sma->sem_perm.security;
1916}
1917
1918/**
1919 * smack_sem_alloc_security - Set the security blob for sem
1920 * @sma: the object
1921 *
1922 * Returns 0
1923 */
1924static int smack_sem_alloc_security(struct sem_array *sma)
1925{
1926 struct kern_ipc_perm *isp = &sma->sem_perm;
1927
David Howells86a264a2008-11-14 10:39:18 +11001928 isp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08001929 return 0;
1930}
1931
1932/**
1933 * smack_sem_free_security - Clear the security blob for sem
1934 * @sma: the object
1935 *
1936 * Clears the blob pointer
1937 */
1938static void smack_sem_free_security(struct sem_array *sma)
1939{
1940 struct kern_ipc_perm *isp = &sma->sem_perm;
1941
1942 isp->security = NULL;
1943}
1944
1945/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001946 * smk_curacc_sem : check if current has access on sem
1947 * @sma : the object
1948 * @access : access requested
1949 *
1950 * Returns 0 if current has the requested access, error code otherwise
1951 */
1952static int smk_curacc_sem(struct sem_array *sma, int access)
1953{
1954 char *ssp = smack_of_sem(sma);
1955 struct smk_audit_info ad;
1956
1957#ifdef CONFIG_AUDIT
1958 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
1959 ad.a.u.ipc_id = sma->sem_perm.id;
1960#endif
1961 return smk_curacc(ssp, access, &ad);
1962}
1963
1964/**
Casey Schauflere114e472008-02-04 22:29:50 -08001965 * smack_sem_associate - Smack access check for sem
1966 * @sma: the object
1967 * @semflg: access requested
1968 *
1969 * Returns 0 if current has the requested access, error code otherwise
1970 */
1971static int smack_sem_associate(struct sem_array *sma, int semflg)
1972{
Casey Schauflere114e472008-02-04 22:29:50 -08001973 int may;
1974
1975 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001976 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08001977}
1978
1979/**
1980 * smack_sem_shmctl - Smack access check for sem
1981 * @sma: the object
1982 * @cmd: what it wants to do
1983 *
1984 * Returns 0 if current has the requested access, error code otherwise
1985 */
1986static int smack_sem_semctl(struct sem_array *sma, int cmd)
1987{
Casey Schauflere114e472008-02-04 22:29:50 -08001988 int may;
1989
1990 switch (cmd) {
1991 case GETPID:
1992 case GETNCNT:
1993 case GETZCNT:
1994 case GETVAL:
1995 case GETALL:
1996 case IPC_STAT:
1997 case SEM_STAT:
1998 may = MAY_READ;
1999 break;
2000 case SETVAL:
2001 case SETALL:
2002 case IPC_RMID:
2003 case IPC_SET:
2004 may = MAY_READWRITE;
2005 break;
2006 case IPC_INFO:
2007 case SEM_INFO:
2008 /*
2009 * System level information
2010 */
2011 return 0;
2012 default:
2013 return -EINVAL;
2014 }
2015
Etienne Bassetecfcc532009-04-08 20:40:06 +02002016 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002017}
2018
2019/**
2020 * smack_sem_semop - Smack checks of semaphore operations
2021 * @sma: the object
2022 * @sops: unused
2023 * @nsops: unused
2024 * @alter: unused
2025 *
2026 * Treated as read and write in all cases.
2027 *
2028 * Returns 0 if access is allowed, error code otherwise
2029 */
2030static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2031 unsigned nsops, int alter)
2032{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002033 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002034}
2035
2036/**
2037 * smack_msg_alloc_security - Set the security blob for msg
2038 * @msq: the object
2039 *
2040 * Returns 0
2041 */
2042static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2043{
2044 struct kern_ipc_perm *kisp = &msq->q_perm;
2045
David Howells86a264a2008-11-14 10:39:18 +11002046 kisp->security = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08002047 return 0;
2048}
2049
2050/**
2051 * smack_msg_free_security - Clear the security blob for msg
2052 * @msq: the object
2053 *
2054 * Clears the blob pointer
2055 */
2056static void smack_msg_queue_free_security(struct msg_queue *msq)
2057{
2058 struct kern_ipc_perm *kisp = &msq->q_perm;
2059
2060 kisp->security = NULL;
2061}
2062
2063/**
2064 * smack_of_msq - the smack pointer for the msq
2065 * @msq: the object
2066 *
2067 * Returns a pointer to the smack value
2068 */
2069static char *smack_of_msq(struct msg_queue *msq)
2070{
2071 return (char *)msq->q_perm.security;
2072}
2073
2074/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002075 * smk_curacc_msq : helper to check if current has access on msq
2076 * @msq : the msq
2077 * @access : access requested
2078 *
2079 * return 0 if current has access, error otherwise
2080 */
2081static int smk_curacc_msq(struct msg_queue *msq, int access)
2082{
2083 char *msp = smack_of_msq(msq);
2084 struct smk_audit_info ad;
2085
2086#ifdef CONFIG_AUDIT
2087 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2088 ad.a.u.ipc_id = msq->q_perm.id;
2089#endif
2090 return smk_curacc(msp, access, &ad);
2091}
2092
2093/**
Casey Schauflere114e472008-02-04 22:29:50 -08002094 * smack_msg_queue_associate - Smack access check for msg_queue
2095 * @msq: the object
2096 * @msqflg: access requested
2097 *
2098 * Returns 0 if current has the requested access, error code otherwise
2099 */
2100static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2101{
Casey Schauflere114e472008-02-04 22:29:50 -08002102 int may;
2103
2104 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002105 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002106}
2107
2108/**
2109 * smack_msg_queue_msgctl - Smack access check for msg_queue
2110 * @msq: the object
2111 * @cmd: what it wants to do
2112 *
2113 * Returns 0 if current has the requested access, error code otherwise
2114 */
2115static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2116{
Casey Schauflere114e472008-02-04 22:29:50 -08002117 int may;
2118
2119 switch (cmd) {
2120 case IPC_STAT:
2121 case MSG_STAT:
2122 may = MAY_READ;
2123 break;
2124 case IPC_SET:
2125 case IPC_RMID:
2126 may = MAY_READWRITE;
2127 break;
2128 case IPC_INFO:
2129 case MSG_INFO:
2130 /*
2131 * System level information
2132 */
2133 return 0;
2134 default:
2135 return -EINVAL;
2136 }
2137
Etienne Bassetecfcc532009-04-08 20:40:06 +02002138 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002139}
2140
2141/**
2142 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2143 * @msq: the object
2144 * @msg: unused
2145 * @msqflg: access requested
2146 *
2147 * Returns 0 if current has the requested access, error code otherwise
2148 */
2149static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2150 int msqflg)
2151{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002152 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002153
Etienne Bassetecfcc532009-04-08 20:40:06 +02002154 may = smack_flags_to_may(msqflg);
2155 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002156}
2157
2158/**
2159 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2160 * @msq: the object
2161 * @msg: unused
2162 * @target: unused
2163 * @type: unused
2164 * @mode: unused
2165 *
2166 * Returns 0 if current has read and write access, error code otherwise
2167 */
2168static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2169 struct task_struct *target, long type, int mode)
2170{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002171 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002172}
2173
2174/**
2175 * smack_ipc_permission - Smack access for ipc_permission()
2176 * @ipp: the object permissions
2177 * @flag: access requested
2178 *
2179 * Returns 0 if current has read and write access, error code otherwise
2180 */
2181static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2182{
2183 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002184 int may = smack_flags_to_may(flag);
2185 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002186
Etienne Bassetecfcc532009-04-08 20:40:06 +02002187#ifdef CONFIG_AUDIT
2188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2189 ad.a.u.ipc_id = ipp->id;
2190#endif
2191 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002192}
2193
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002194/**
2195 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002196 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002197 * @secid: where result will be saved
2198 */
2199static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2200{
2201 char *smack = ipp->security;
2202
2203 *secid = smack_to_secid(smack);
2204}
2205
Casey Schauflere114e472008-02-04 22:29:50 -08002206/**
2207 * smack_d_instantiate - Make sure the blob is correct on an inode
2208 * @opt_dentry: unused
2209 * @inode: the object
2210 *
2211 * Set the inode's security blob if it hasn't been done already.
2212 */
2213static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2214{
2215 struct super_block *sbp;
2216 struct superblock_smack *sbsp;
2217 struct inode_smack *isp;
David Howells86a264a2008-11-14 10:39:18 +11002218 char *csp = current_security();
Casey Schauflere114e472008-02-04 22:29:50 -08002219 char *fetched;
2220 char *final;
2221 struct dentry *dp;
2222
2223 if (inode == NULL)
2224 return;
2225
2226 isp = inode->i_security;
2227
2228 mutex_lock(&isp->smk_lock);
2229 /*
2230 * If the inode is already instantiated
2231 * take the quick way out
2232 */
2233 if (isp->smk_flags & SMK_INODE_INSTANT)
2234 goto unlockandout;
2235
2236 sbp = inode->i_sb;
2237 sbsp = sbp->s_security;
2238 /*
2239 * We're going to use the superblock default label
2240 * if there's no label on the file.
2241 */
2242 final = sbsp->smk_default;
2243
2244 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002245 * If this is the root inode the superblock
2246 * may be in the process of initialization.
2247 * If that is the case use the root value out
2248 * of the superblock.
2249 */
2250 if (opt_dentry->d_parent == opt_dentry) {
2251 isp->smk_inode = sbsp->smk_root;
2252 isp->smk_flags |= SMK_INODE_INSTANT;
2253 goto unlockandout;
2254 }
2255
2256 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002257 * This is pretty hackish.
2258 * Casey says that we shouldn't have to do
2259 * file system specific code, but it does help
2260 * with keeping it simple.
2261 */
2262 switch (sbp->s_magic) {
2263 case SMACK_MAGIC:
2264 /*
2265 * Casey says that it's a little embarassing
2266 * that the smack file system doesn't do
2267 * extended attributes.
2268 */
2269 final = smack_known_star.smk_known;
2270 break;
2271 case PIPEFS_MAGIC:
2272 /*
2273 * Casey says pipes are easy (?)
2274 */
2275 final = smack_known_star.smk_known;
2276 break;
2277 case DEVPTS_SUPER_MAGIC:
2278 /*
2279 * devpts seems content with the label of the task.
2280 * Programs that change smack have to treat the
2281 * pty with respect.
2282 */
2283 final = csp;
2284 break;
2285 case SOCKFS_MAGIC:
2286 /*
2287 * Casey says sockets get the smack of the task.
2288 */
2289 final = csp;
2290 break;
2291 case PROC_SUPER_MAGIC:
2292 /*
2293 * Casey says procfs appears not to care.
2294 * The superblock default suffices.
2295 */
2296 break;
2297 case TMPFS_MAGIC:
2298 /*
2299 * Device labels should come from the filesystem,
2300 * but watch out, because they're volitile,
2301 * getting recreated on every reboot.
2302 */
2303 final = smack_known_star.smk_known;
2304 /*
2305 * No break.
2306 *
2307 * If a smack value has been set we want to use it,
2308 * but since tmpfs isn't giving us the opportunity
2309 * to set mount options simulate setting the
2310 * superblock default.
2311 */
2312 default:
2313 /*
2314 * This isn't an understood special case.
2315 * Get the value from the xattr.
2316 *
2317 * No xattr support means, alas, no SMACK label.
2318 * Use the aforeapplied default.
2319 * It would be curious if the label of the task
2320 * does not match that assigned.
2321 */
2322 if (inode->i_op->getxattr == NULL)
2323 break;
2324 /*
2325 * Get the dentry for xattr.
2326 */
2327 if (opt_dentry == NULL) {
2328 dp = d_find_alias(inode);
2329 if (dp == NULL)
2330 break;
2331 } else {
2332 dp = dget(opt_dentry);
2333 if (dp == NULL)
2334 break;
2335 }
2336
2337 fetched = smk_fetch(inode, dp);
2338 if (fetched != NULL)
2339 final = fetched;
2340
2341 dput(dp);
2342 break;
2343 }
2344
2345 if (final == NULL)
2346 isp->smk_inode = csp;
2347 else
2348 isp->smk_inode = final;
2349
2350 isp->smk_flags |= SMK_INODE_INSTANT;
2351
2352unlockandout:
2353 mutex_unlock(&isp->smk_lock);
2354 return;
2355}
2356
2357/**
2358 * smack_getprocattr - Smack process attribute access
2359 * @p: the object task
2360 * @name: the name of the attribute in /proc/.../attr
2361 * @value: where to put the result
2362 *
2363 * Places a copy of the task Smack into value
2364 *
2365 * Returns the length of the smack label or an error code
2366 */
2367static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2368{
2369 char *cp;
2370 int slen;
2371
2372 if (strcmp(name, "current") != 0)
2373 return -EINVAL;
2374
David Howellsc69e8d92008-11-14 10:39:19 +11002375 cp = kstrdup(task_security(p), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002376 if (cp == NULL)
2377 return -ENOMEM;
2378
2379 slen = strlen(cp);
2380 *value = cp;
2381 return slen;
2382}
2383
2384/**
2385 * smack_setprocattr - Smack process attribute setting
2386 * @p: the object task
2387 * @name: the name of the attribute in /proc/.../attr
2388 * @value: the value to set
2389 * @size: the size of the value
2390 *
2391 * Sets the Smack value of the task. Only setting self
2392 * is permitted and only with privilege
2393 *
2394 * Returns the length of the smack label or an error code
2395 */
2396static int smack_setprocattr(struct task_struct *p, char *name,
2397 void *value, size_t size)
2398{
David Howellsd84f4f92008-11-14 10:39:23 +11002399 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002400 char *newsmack;
2401
Casey Schauflere114e472008-02-04 22:29:50 -08002402 /*
2403 * Changing another process' Smack value is too dangerous
2404 * and supports no sane use case.
2405 */
2406 if (p != current)
2407 return -EPERM;
2408
David Howells5cd9c582008-08-14 11:37:28 +01002409 if (!capable(CAP_MAC_ADMIN))
2410 return -EPERM;
2411
Casey Schauflere114e472008-02-04 22:29:50 -08002412 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2413 return -EINVAL;
2414
2415 if (strcmp(name, "current") != 0)
2416 return -EINVAL;
2417
2418 newsmack = smk_import(value, size);
2419 if (newsmack == NULL)
2420 return -EINVAL;
2421
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002422 /*
2423 * No process is ever allowed the web ("@") label.
2424 */
2425 if (newsmack == smack_known_web.smk_known)
2426 return -EPERM;
2427
David Howellsd84f4f92008-11-14 10:39:23 +11002428 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002429 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002430 return -ENOMEM;
2431 new->security = newsmack;
2432 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002433 return size;
2434}
2435
2436/**
2437 * smack_unix_stream_connect - Smack access on UDS
2438 * @sock: one socket
2439 * @other: the other socket
2440 * @newsk: unused
2441 *
2442 * Return 0 if a subject with the smack of sock could access
2443 * an object with the smack of other, otherwise an error code
2444 */
2445static int smack_unix_stream_connect(struct socket *sock,
2446 struct socket *other, struct sock *newsk)
2447{
2448 struct inode *sp = SOCK_INODE(sock);
2449 struct inode *op = SOCK_INODE(other);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002450 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002451
Etienne Bassetecfcc532009-04-08 20:40:06 +02002452 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2453 smk_ad_setfield_u_net_sk(&ad, other->sk);
2454 return smk_access(smk_of_inode(sp), smk_of_inode(op),
2455 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002456}
2457
2458/**
2459 * smack_unix_may_send - Smack access on UDS
2460 * @sock: one socket
2461 * @other: the other socket
2462 *
2463 * Return 0 if a subject with the smack of sock could access
2464 * an object with the smack of other, otherwise an error code
2465 */
2466static int smack_unix_may_send(struct socket *sock, struct socket *other)
2467{
2468 struct inode *sp = SOCK_INODE(sock);
2469 struct inode *op = SOCK_INODE(other);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002470 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002471
Etienne Bassetecfcc532009-04-08 20:40:06 +02002472 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2473 smk_ad_setfield_u_net_sk(&ad, other->sk);
2474 return smk_access(smk_of_inode(sp), smk_of_inode(op), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002475}
2476
2477/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002478 * smack_socket_sendmsg - Smack check based on destination host
2479 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002480 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002481 * @size: the size of the message
2482 *
2483 * Return 0 if the current subject can write to the destination
2484 * host. This is only a question if the destination is a single
2485 * label host.
2486 */
2487static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2488 int size)
2489{
2490 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002491
2492 /*
2493 * Perfectly reasonable for this to be NULL
2494 */
Julia Lawallda34d422009-08-05 14:34:55 +02002495 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002496 return 0;
2497
Paul Moore07feee82009-03-27 17:10:54 -04002498 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002499}
2500
2501
2502/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002503 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002504 * @sap: netlabel secattr
2505 * @sip: where to put the result
2506 *
2507 * Copies a smack label into sip
2508 */
2509static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2510{
2511 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002512 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002513 int pcat;
2514
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002515 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002516 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002517 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002518 * If there are flags but no level netlabel isn't
2519 * behaving the way we expect it to.
2520 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002521 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002522 * Without guidance regarding the smack value
2523 * for the packet fall back on the network
2524 * ambient value.
2525 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002526 memset(smack, '\0', SMK_LABELLEN);
2527 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2528 for (pcat = -1;;) {
2529 pcat = netlbl_secattr_catmap_walk(
2530 sap->attr.mls.cat, pcat + 1);
2531 if (pcat < 0)
2532 break;
2533 smack_catset_bit(pcat, smack);
2534 }
2535 /*
2536 * If it is CIPSO using smack direct mapping
2537 * we are already done. WeeHee.
2538 */
2539 if (sap->attr.mls.lvl == smack_cipso_direct) {
2540 memcpy(sip, smack, SMK_MAXLEN);
2541 return;
Casey Schauflere114e472008-02-04 22:29:50 -08002542 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002543 /*
2544 * Look it up in the supplied table if it is not
2545 * a direct mapping.
2546 */
2547 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2548 return;
2549 }
2550 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2551 /*
2552 * Looks like a fallback, which gives us a secid.
2553 */
2554 sp = smack_from_secid(sap->attr.secid);
2555 /*
2556 * This has got to be a bug because it is
2557 * impossible to specify a fallback without
2558 * specifying the label, which will ensure
2559 * it has a secid, and the only way to get a
2560 * secid is from a fallback.
2561 */
2562 BUG_ON(sp == NULL);
2563 strncpy(sip, sp, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002564 return;
2565 }
2566 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002567 * Without guidance regarding the smack value
2568 * for the packet fall back on the network
2569 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002570 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002571 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002572 return;
2573}
2574
2575/**
2576 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2577 * @sk: socket
2578 * @skb: packet
2579 *
2580 * Returns 0 if the packet should be delivered, an error code otherwise
2581 */
2582static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2583{
2584 struct netlbl_lsm_secattr secattr;
2585 struct socket_smack *ssp = sk->sk_security;
2586 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002587 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002588 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002589 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002590 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2591 return 0;
2592
2593 /*
2594 * Translate what netlabel gave us.
2595 */
Casey Schauflere114e472008-02-04 22:29:50 -08002596 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002597
Casey Schauflere114e472008-02-04 22:29:50 -08002598 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002599 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002600 smack_from_secattr(&secattr, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002601 csp = smack;
2602 } else
2603 csp = smack_net_ambient;
2604
Casey Schauflere114e472008-02-04 22:29:50 -08002605 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002606
Etienne Bassetecfcc532009-04-08 20:40:06 +02002607#ifdef CONFIG_AUDIT
2608 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2609 ad.a.u.net.family = sk->sk_family;
2610 ad.a.u.net.netif = skb->iif;
2611 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2612#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002613 /*
2614 * Receiving a packet requires that the other end
2615 * be able to write here. Read access is not required.
2616 * This is the simplist possible security model
2617 * for networking.
2618 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002619 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002620 if (rc != 0)
2621 netlbl_skbuff_err(skb, rc, 0);
2622 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002623}
2624
2625/**
2626 * smack_socket_getpeersec_stream - pull in packet label
2627 * @sock: the socket
2628 * @optval: user's destination
2629 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002630 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002631 *
2632 * returns zero on success, an error code otherwise
2633 */
2634static int smack_socket_getpeersec_stream(struct socket *sock,
2635 char __user *optval,
2636 int __user *optlen, unsigned len)
2637{
2638 struct socket_smack *ssp;
2639 int slen;
2640 int rc = 0;
2641
2642 ssp = sock->sk->sk_security;
2643 slen = strlen(ssp->smk_packet) + 1;
2644
2645 if (slen > len)
2646 rc = -ERANGE;
2647 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2648 rc = -EFAULT;
2649
2650 if (put_user(slen, optlen) != 0)
2651 rc = -EFAULT;
2652
2653 return rc;
2654}
2655
2656
2657/**
2658 * smack_socket_getpeersec_dgram - pull in packet label
2659 * @sock: the socket
2660 * @skb: packet data
2661 * @secid: pointer to where to put the secid of the packet
2662 *
2663 * Sets the netlabel socket state on sk from parent
2664 */
2665static int smack_socket_getpeersec_dgram(struct socket *sock,
2666 struct sk_buff *skb, u32 *secid)
2667
2668{
2669 struct netlbl_lsm_secattr secattr;
2670 struct sock *sk;
2671 char smack[SMK_LABELLEN];
2672 int family = PF_INET;
2673 u32 s;
2674 int rc;
2675
2676 /*
2677 * Only works for families with packets.
2678 */
2679 if (sock != NULL) {
2680 sk = sock->sk;
2681 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2682 return 0;
2683 family = sk->sk_family;
2684 }
2685 /*
2686 * Translate what netlabel gave us.
2687 */
Casey Schauflere114e472008-02-04 22:29:50 -08002688 netlbl_secattr_init(&secattr);
2689 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2690 if (rc == 0)
2691 smack_from_secattr(&secattr, smack);
2692 netlbl_secattr_destroy(&secattr);
2693
2694 /*
2695 * Give up if we couldn't get anything
2696 */
2697 if (rc != 0)
2698 return rc;
2699
2700 s = smack_to_secid(smack);
2701 if (s == 0)
2702 return -EINVAL;
2703
2704 *secid = s;
2705 return 0;
2706}
2707
2708/**
Paul Moore07feee82009-03-27 17:10:54 -04002709 * smack_sock_graft - Initialize a newly created socket with an existing sock
2710 * @sk: child sock
2711 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08002712 *
Paul Moore07feee82009-03-27 17:10:54 -04002713 * Set the smk_{in,out} state of an existing sock based on the process that
2714 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08002715 */
2716static void smack_sock_graft(struct sock *sk, struct socket *parent)
2717{
2718 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08002719
Paul Moore07feee82009-03-27 17:10:54 -04002720 if (sk == NULL ||
2721 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08002722 return;
2723
2724 ssp = sk->sk_security;
David Howells86a264a2008-11-14 10:39:18 +11002725 ssp->smk_in = ssp->smk_out = current_security();
Paul Moore07feee82009-03-27 17:10:54 -04002726 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08002727}
2728
2729/**
2730 * smack_inet_conn_request - Smack access check on connect
2731 * @sk: socket involved
2732 * @skb: packet
2733 * @req: unused
2734 *
2735 * Returns 0 if a task with the packet label could write to
2736 * the socket, otherwise an error code
2737 */
2738static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
2739 struct request_sock *req)
2740{
Paul Moore07feee82009-03-27 17:10:54 -04002741 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08002742 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04002743 struct netlbl_lsm_secattr secattr;
2744 struct sockaddr_in addr;
2745 struct iphdr *hdr;
Casey Schauflere114e472008-02-04 22:29:50 -08002746 char smack[SMK_LABELLEN];
2747 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002748 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002749
Paul Moore07feee82009-03-27 17:10:54 -04002750 /* handle mapped IPv4 packets arriving via IPv6 sockets */
2751 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
2752 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08002753
Paul Moore07feee82009-03-27 17:10:54 -04002754 netlbl_secattr_init(&secattr);
2755 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08002756 if (rc == 0)
Paul Moore07feee82009-03-27 17:10:54 -04002757 smack_from_secattr(&secattr, smack);
Casey Schauflere114e472008-02-04 22:29:50 -08002758 else
2759 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
Paul Moore07feee82009-03-27 17:10:54 -04002760 netlbl_secattr_destroy(&secattr);
2761
Etienne Bassetecfcc532009-04-08 20:40:06 +02002762#ifdef CONFIG_AUDIT
2763 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2764 ad.a.u.net.family = family;
2765 ad.a.u.net.netif = skb->iif;
2766 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2767#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002768 /*
Paul Moore07feee82009-03-27 17:10:54 -04002769 * Receiving a packet requires that the other end be able to write
2770 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08002771 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002772 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04002773 if (rc != 0)
2774 return rc;
2775
2776 /*
2777 * Save the peer's label in the request_sock so we can later setup
2778 * smk_packet in the child socket so that SO_PEERCRED can report it.
2779 */
2780 req->peer_secid = smack_to_secid(smack);
2781
2782 /*
2783 * We need to decide if we want to label the incoming connection here
2784 * if we do we only need to label the request_sock and the stack will
2785 * propogate the wire-label to the sock when it is created.
2786 */
2787 hdr = ip_hdr(skb);
2788 addr.sin_addr.s_addr = hdr->saddr;
2789 rcu_read_lock();
2790 if (smack_host_label(&addr) == NULL) {
2791 rcu_read_unlock();
2792 netlbl_secattr_init(&secattr);
2793 smack_to_secattr(smack, &secattr);
2794 rc = netlbl_req_setattr(req, &secattr);
2795 netlbl_secattr_destroy(&secattr);
2796 } else {
2797 rcu_read_unlock();
2798 netlbl_req_delattr(req);
2799 }
Casey Schauflere114e472008-02-04 22:29:50 -08002800
2801 return rc;
2802}
2803
Paul Moore07feee82009-03-27 17:10:54 -04002804/**
2805 * smack_inet_csk_clone - Copy the connection information to the new socket
2806 * @sk: the new socket
2807 * @req: the connection's request_sock
2808 *
2809 * Transfer the connection's peer label to the newly created socket.
2810 */
2811static void smack_inet_csk_clone(struct sock *sk,
2812 const struct request_sock *req)
2813{
2814 struct socket_smack *ssp = sk->sk_security;
2815 char *smack;
2816
2817 if (req->peer_secid != 0) {
2818 smack = smack_from_secid(req->peer_secid);
2819 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
2820 } else
2821 ssp->smk_packet[0] = '\0';
2822}
2823
Casey Schauflere114e472008-02-04 22:29:50 -08002824/*
2825 * Key management security hooks
2826 *
2827 * Casey has not tested key support very heavily.
2828 * The permission check is most likely too restrictive.
2829 * If you care about keys please have a look.
2830 */
2831#ifdef CONFIG_KEYS
2832
2833/**
2834 * smack_key_alloc - Set the key security blob
2835 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11002836 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002837 * @flags: unused
2838 *
2839 * No allocation required
2840 *
2841 * Returns 0
2842 */
David Howellsd84f4f92008-11-14 10:39:23 +11002843static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08002844 unsigned long flags)
2845{
David Howellsd84f4f92008-11-14 10:39:23 +11002846 key->security = cred->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002847 return 0;
2848}
2849
2850/**
2851 * smack_key_free - Clear the key security blob
2852 * @key: the object
2853 *
2854 * Clear the blob pointer
2855 */
2856static void smack_key_free(struct key *key)
2857{
2858 key->security = NULL;
2859}
2860
2861/*
2862 * smack_key_permission - Smack access on a key
2863 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11002864 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08002865 * @perm: unused
2866 *
2867 * Return 0 if the task has read and write to the object,
2868 * an error code otherwise
2869 */
2870static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11002871 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08002872{
2873 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002874 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002875
2876 keyp = key_ref_to_ptr(key_ref);
2877 if (keyp == NULL)
2878 return -EINVAL;
2879 /*
2880 * If the key hasn't been initialized give it access so that
2881 * it may do so.
2882 */
2883 if (keyp->security == NULL)
2884 return 0;
2885 /*
2886 * This should not occur
2887 */
David Howellsd84f4f92008-11-14 10:39:23 +11002888 if (cred->security == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002889 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002890#ifdef CONFIG_AUDIT
2891 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
2892 ad.a.u.key_struct.key = keyp->serial;
2893 ad.a.u.key_struct.key_desc = keyp->description;
2894#endif
2895 return smk_access(cred->security, keyp->security,
2896 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002897}
2898#endif /* CONFIG_KEYS */
2899
2900/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002901 * Smack Audit hooks
2902 *
2903 * Audit requires a unique representation of each Smack specific
2904 * rule. This unique representation is used to distinguish the
2905 * object to be audited from remaining kernel objects and also
2906 * works as a glue between the audit hooks.
2907 *
2908 * Since repository entries are added but never deleted, we'll use
2909 * the smack_known label address related to the given audit rule as
2910 * the needed unique representation. This also better fits the smack
2911 * model where nearly everything is a label.
2912 */
2913#ifdef CONFIG_AUDIT
2914
2915/**
2916 * smack_audit_rule_init - Initialize a smack audit rule
2917 * @field: audit rule fields given from user-space (audit.h)
2918 * @op: required testing operator (=, !=, >, <, ...)
2919 * @rulestr: smack label to be audited
2920 * @vrule: pointer to save our own audit rule representation
2921 *
2922 * Prepare to audit cases where (@field @op @rulestr) is true.
2923 * The label to be audited is created if necessay.
2924 */
2925static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2926{
2927 char **rule = (char **)vrule;
2928 *rule = NULL;
2929
2930 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2931 return -EINVAL;
2932
Al Viro5af75d82008-12-16 05:59:26 -05002933 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002934 return -EINVAL;
2935
2936 *rule = smk_import(rulestr, 0);
2937
2938 return 0;
2939}
2940
2941/**
2942 * smack_audit_rule_known - Distinguish Smack audit rules
2943 * @krule: rule of interest, in Audit kernel representation format
2944 *
2945 * This is used to filter Smack rules from remaining Audit ones.
2946 * If it's proved that this rule belongs to us, the
2947 * audit_rule_match hook will be called to do the final judgement.
2948 */
2949static int smack_audit_rule_known(struct audit_krule *krule)
2950{
2951 struct audit_field *f;
2952 int i;
2953
2954 for (i = 0; i < krule->field_count; i++) {
2955 f = &krule->fields[i];
2956
2957 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
2958 return 1;
2959 }
2960
2961 return 0;
2962}
2963
2964/**
2965 * smack_audit_rule_match - Audit given object ?
2966 * @secid: security id for identifying the object to test
2967 * @field: audit rule flags given from user-space
2968 * @op: required testing operator
2969 * @vrule: smack internal rule presentation
2970 * @actx: audit context associated with the check
2971 *
2972 * The core Audit hook. It's used to take the decision of
2973 * whether to audit or not to audit a given object.
2974 */
2975static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
2976 struct audit_context *actx)
2977{
2978 char *smack;
2979 char *rule = vrule;
2980
2981 if (!rule) {
2982 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
2983 "Smack: missing rule\n");
2984 return -ENOENT;
2985 }
2986
2987 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
2988 return 0;
2989
2990 smack = smack_from_secid(secid);
2991
2992 /*
2993 * No need to do string comparisons. If a match occurs,
2994 * both pointers will point to the same smack_known
2995 * label.
2996 */
Al Viro5af75d82008-12-16 05:59:26 -05002997 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002998 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05002999 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003000 return (rule != smack);
3001
3002 return 0;
3003}
3004
3005/**
3006 * smack_audit_rule_free - free smack rule representation
3007 * @vrule: rule to be freed.
3008 *
3009 * No memory was allocated.
3010 */
3011static void smack_audit_rule_free(void *vrule)
3012{
3013 /* No-op */
3014}
3015
3016#endif /* CONFIG_AUDIT */
3017
Randy Dunlap251a2a92009-02-18 11:42:33 -08003018/**
Casey Schauflere114e472008-02-04 22:29:50 -08003019 * smack_secid_to_secctx - return the smack label for a secid
3020 * @secid: incoming integer
3021 * @secdata: destination
3022 * @seclen: how long it is
3023 *
3024 * Exists for networking code.
3025 */
3026static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3027{
3028 char *sp = smack_from_secid(secid);
3029
3030 *secdata = sp;
3031 *seclen = strlen(sp);
3032 return 0;
3033}
3034
Randy Dunlap251a2a92009-02-18 11:42:33 -08003035/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003036 * smack_secctx_to_secid - return the secid for a smack label
3037 * @secdata: smack label
3038 * @seclen: how long result is
3039 * @secid: outgoing integer
3040 *
3041 * Exists for audit and networking code.
3042 */
David Howellse52c17642008-04-29 20:52:51 +01003043static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003044{
3045 *secid = smack_to_secid(secdata);
3046 return 0;
3047}
3048
Randy Dunlap251a2a92009-02-18 11:42:33 -08003049/**
Casey Schauflere114e472008-02-04 22:29:50 -08003050 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003051 * @secdata: unused
3052 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003053 *
3054 * Exists to make sure nothing gets done, and properly
3055 */
3056static void smack_release_secctx(char *secdata, u32 seclen)
3057{
3058}
3059
David P. Quigley1ee65e32009-09-03 14:25:57 -04003060static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3061{
3062 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3063}
3064
3065static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3066{
3067 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3068}
3069
3070static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3071{
3072 int len = 0;
3073 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3074
3075 if (len < 0)
3076 return len;
3077 *ctxlen = len;
3078 return 0;
3079}
3080
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003081struct security_operations smack_ops = {
3082 .name = "smack",
3083
Ingo Molnar9e488582009-05-07 19:26:19 +10003084 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003085 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003086 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003087
3088 .sb_alloc_security = smack_sb_alloc_security,
3089 .sb_free_security = smack_sb_free_security,
3090 .sb_copy_data = smack_sb_copy_data,
3091 .sb_kern_mount = smack_sb_kern_mount,
3092 .sb_statfs = smack_sb_statfs,
3093 .sb_mount = smack_sb_mount,
3094 .sb_umount = smack_sb_umount,
3095
3096 .inode_alloc_security = smack_inode_alloc_security,
3097 .inode_free_security = smack_inode_free_security,
3098 .inode_init_security = smack_inode_init_security,
3099 .inode_link = smack_inode_link,
3100 .inode_unlink = smack_inode_unlink,
3101 .inode_rmdir = smack_inode_rmdir,
3102 .inode_rename = smack_inode_rename,
3103 .inode_permission = smack_inode_permission,
3104 .inode_setattr = smack_inode_setattr,
3105 .inode_getattr = smack_inode_getattr,
3106 .inode_setxattr = smack_inode_setxattr,
3107 .inode_post_setxattr = smack_inode_post_setxattr,
3108 .inode_getxattr = smack_inode_getxattr,
3109 .inode_removexattr = smack_inode_removexattr,
3110 .inode_getsecurity = smack_inode_getsecurity,
3111 .inode_setsecurity = smack_inode_setsecurity,
3112 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003113 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003114
3115 .file_permission = smack_file_permission,
3116 .file_alloc_security = smack_file_alloc_security,
3117 .file_free_security = smack_file_free_security,
3118 .file_ioctl = smack_file_ioctl,
3119 .file_lock = smack_file_lock,
3120 .file_fcntl = smack_file_fcntl,
3121 .file_set_fowner = smack_file_set_fowner,
3122 .file_send_sigiotask = smack_file_send_sigiotask,
3123 .file_receive = smack_file_receive,
3124
David Howellsee18d642009-09-02 09:14:21 +01003125 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003126 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003127 .cred_prepare = smack_cred_prepare,
3128 .cred_commit = smack_cred_commit,
David Howellsee18d642009-09-02 09:14:21 +01003129 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003130 .kernel_act_as = smack_kernel_act_as,
3131 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003132 .task_setpgid = smack_task_setpgid,
3133 .task_getpgid = smack_task_getpgid,
3134 .task_getsid = smack_task_getsid,
3135 .task_getsecid = smack_task_getsecid,
3136 .task_setnice = smack_task_setnice,
3137 .task_setioprio = smack_task_setioprio,
3138 .task_getioprio = smack_task_getioprio,
3139 .task_setscheduler = smack_task_setscheduler,
3140 .task_getscheduler = smack_task_getscheduler,
3141 .task_movememory = smack_task_movememory,
3142 .task_kill = smack_task_kill,
3143 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003144 .task_to_inode = smack_task_to_inode,
3145
3146 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003147 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003148
3149 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3150 .msg_msg_free_security = smack_msg_msg_free_security,
3151
3152 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3153 .msg_queue_free_security = smack_msg_queue_free_security,
3154 .msg_queue_associate = smack_msg_queue_associate,
3155 .msg_queue_msgctl = smack_msg_queue_msgctl,
3156 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3157 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3158
3159 .shm_alloc_security = smack_shm_alloc_security,
3160 .shm_free_security = smack_shm_free_security,
3161 .shm_associate = smack_shm_associate,
3162 .shm_shmctl = smack_shm_shmctl,
3163 .shm_shmat = smack_shm_shmat,
3164
3165 .sem_alloc_security = smack_sem_alloc_security,
3166 .sem_free_security = smack_sem_free_security,
3167 .sem_associate = smack_sem_associate,
3168 .sem_semctl = smack_sem_semctl,
3169 .sem_semop = smack_sem_semop,
3170
Casey Schauflere114e472008-02-04 22:29:50 -08003171 .d_instantiate = smack_d_instantiate,
3172
3173 .getprocattr = smack_getprocattr,
3174 .setprocattr = smack_setprocattr,
3175
3176 .unix_stream_connect = smack_unix_stream_connect,
3177 .unix_may_send = smack_unix_may_send,
3178
3179 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003180 .socket_connect = smack_socket_connect,
3181 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003182 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3183 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3184 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3185 .sk_alloc_security = smack_sk_alloc_security,
3186 .sk_free_security = smack_sk_free_security,
3187 .sock_graft = smack_sock_graft,
3188 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003189 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003190
Casey Schauflere114e472008-02-04 22:29:50 -08003191 /* key management security hooks */
3192#ifdef CONFIG_KEYS
3193 .key_alloc = smack_key_alloc,
3194 .key_free = smack_key_free,
3195 .key_permission = smack_key_permission,
3196#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003197
3198 /* Audit hooks */
3199#ifdef CONFIG_AUDIT
3200 .audit_rule_init = smack_audit_rule_init,
3201 .audit_rule_known = smack_audit_rule_known,
3202 .audit_rule_match = smack_audit_rule_match,
3203 .audit_rule_free = smack_audit_rule_free,
3204#endif /* CONFIG_AUDIT */
3205
Casey Schauflere114e472008-02-04 22:29:50 -08003206 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003207 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003208 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003209 .inode_notifysecctx = smack_inode_notifysecctx,
3210 .inode_setsecctx = smack_inode_setsecctx,
3211 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003212};
3213
Etienne Basset7198e2e2009-03-24 20:53:24 +01003214
3215static __init void init_smack_know_list(void)
3216{
3217 list_add(&smack_known_huh.list, &smack_known_list);
3218 list_add(&smack_known_hat.list, &smack_known_list);
3219 list_add(&smack_known_star.list, &smack_known_list);
3220 list_add(&smack_known_floor.list, &smack_known_list);
3221 list_add(&smack_known_invalid.list, &smack_known_list);
3222 list_add(&smack_known_web.list, &smack_known_list);
3223}
3224
Casey Schauflere114e472008-02-04 22:29:50 -08003225/**
3226 * smack_init - initialize the smack system
3227 *
3228 * Returns 0
3229 */
3230static __init int smack_init(void)
3231{
David Howellsd84f4f92008-11-14 10:39:23 +11003232 struct cred *cred;
3233
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003234 if (!security_module_enable(&smack_ops))
3235 return 0;
3236
Casey Schauflere114e472008-02-04 22:29:50 -08003237 printk(KERN_INFO "Smack: Initializing.\n");
3238
3239 /*
3240 * Set the security state for the initial task.
3241 */
David Howellsd84f4f92008-11-14 10:39:23 +11003242 cred = (struct cred *) current->cred;
3243 cred->security = &smack_known_floor.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003244
Etienne Basset7198e2e2009-03-24 20:53:24 +01003245 /* initilize the smack_know_list */
3246 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003247 /*
3248 * Initialize locks
3249 */
Casey Schauflere114e472008-02-04 22:29:50 -08003250 spin_lock_init(&smack_known_huh.smk_cipsolock);
3251 spin_lock_init(&smack_known_hat.smk_cipsolock);
3252 spin_lock_init(&smack_known_star.smk_cipsolock);
3253 spin_lock_init(&smack_known_floor.smk_cipsolock);
3254 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3255
3256 /*
3257 * Register with LSM
3258 */
3259 if (register_security(&smack_ops))
3260 panic("smack: Unable to register with kernel.\n");
3261
3262 return 0;
3263}
3264
3265/*
3266 * Smack requires early initialization in order to label
3267 * all processes and objects when they are created.
3268 */
3269security_initcall(smack_init);