blob: fb915163f967a4f27b9b18790794ff5cbb5f00b3 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02008 * Jarkko Sakkinen <ext-jarkko.2.sakkinen@nokia.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Casey Schauflere114e472008-02-04 22:29:50 -080014 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2,
17 * as published by the Free Software Foundation.
18 */
19
20#include <linux/xattr.h>
21#include <linux/pagemap.h>
22#include <linux/mount.h>
23#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080024#include <linux/kd.h>
25#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040026#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080027#include <linux/tcp.h>
28#include <linux/udp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030#include <linux/mutex.h>
31#include <linux/pipe_fs_i.h>
32#include <net/netlabel.h>
33#include <net/cipso_ipv4.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100034#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070035#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050036#include <linux/dcache.h>
Casey Schauflere114e472008-02-04 22:29:50 -080037#include "smack.h"
38
David Howellsc69e8d92008-11-14 10:39:19 +110039#define task_security(task) (task_cred_xxx((task), security))
40
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020041#define TRANS_TRUE "TRUE"
42#define TRANS_TRUE_SIZE 4
43
Casey Schauflere114e472008-02-04 22:29:50 -080044/**
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 */
Casey Schaufler676dac42010-12-02 06:43:39 -080052static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080053{
54 int rc;
55 char in[SMK_LABELLEN];
56
57 if (ip->i_op->getxattr == NULL)
58 return NULL;
59
Casey Schaufler676dac42010-12-02 06:43:39 -080060 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
Casey Schauflere114e472008-02-04 22:29:50 -080061 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
Casey Schaufler7898e1f2011-01-17 08:05:27 -080088/**
89 * new_task_smack - allocate a task security blob
90 * @smack: a pointer to the Smack label to use in the blob
91 *
92 * Returns the new blob or NULL if there's no memory available
93 */
94static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
95{
96 struct task_smack *tsp;
97
98 tsp = kzalloc(sizeof(struct task_smack), gfp);
99 if (tsp == NULL)
100 return NULL;
101
102 tsp->smk_task = task;
103 tsp->smk_forked = forked;
104 INIT_LIST_HEAD(&tsp->smk_rules);
105 mutex_init(&tsp->smk_rules_lock);
106
107 return tsp;
108}
109
110/**
111 * smk_copy_rules - copy a rule set
112 * @nhead - new rules header pointer
113 * @ohead - old rules header pointer
114 *
115 * Returns 0 on success, -ENOMEM on error
116 */
117static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
118 gfp_t gfp)
119{
120 struct smack_rule *nrp;
121 struct smack_rule *orp;
122 int rc = 0;
123
124 INIT_LIST_HEAD(nhead);
125
126 list_for_each_entry_rcu(orp, ohead, list) {
127 nrp = kzalloc(sizeof(struct smack_rule), gfp);
128 if (nrp == NULL) {
129 rc = -ENOMEM;
130 break;
131 }
132 *nrp = *orp;
133 list_add_rcu(&nrp->list, nhead);
134 }
135 return rc;
136}
137
Casey Schauflere114e472008-02-04 22:29:50 -0800138/*
139 * LSM hooks.
140 * We he, that is fun!
141 */
142
143/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000144 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800145 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -0800146 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -0800147 *
148 * Returns 0 if access is OK, an error code otherwise
149 *
150 * Do the capability checks, and require read and write.
151 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000152static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800153{
154 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200155 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800156 char *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800157
Ingo Molnar9e488582009-05-07 19:26:19 +1000158 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800159 if (rc != 0)
160 return rc;
161
Casey Schaufler676dac42010-12-02 06:43:39 -0800162 tsp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200163 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
164 smk_ad_setfield_u_tsk(&ad, ctp);
165
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800166 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100167 return rc;
168}
Casey Schauflere114e472008-02-04 22:29:50 -0800169
David Howells5cd9c582008-08-14 11:37:28 +0100170/**
171 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
172 * @ptp: parent task pointer
173 *
174 * Returns 0 if access is OK, an error code otherwise
175 *
176 * Do the capability checks, and require read and write.
177 */
178static int smack_ptrace_traceme(struct task_struct *ptp)
179{
180 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200181 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800182 char *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100183
184 rc = cap_ptrace_traceme(ptp);
185 if (rc != 0)
186 return rc;
187
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800188 tsp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200189 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
190 smk_ad_setfield_u_tsk(&ad, ptp);
191
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800192 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800193 return rc;
194}
195
196/**
197 * smack_syslog - Smack approval on syslog
198 * @type: message type
199 *
200 * Require that the task has the floor label
201 *
202 * Returns 0 on success, error code otherwise.
203 */
Eric Paris12b30522010-11-15 18:36:29 -0500204static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800205{
Eric Paris12b30522010-11-15 18:36:29 -0500206 int rc = 0;
Casey Schaufler676dac42010-12-02 06:43:39 -0800207 char *sp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800208
Casey Schauflere114e472008-02-04 22:29:50 -0800209 if (capable(CAP_MAC_OVERRIDE))
210 return 0;
211
212 if (sp != smack_known_floor.smk_known)
213 rc = -EACCES;
214
215 return rc;
216}
217
218
219/*
220 * Superblock Hooks.
221 */
222
223/**
224 * smack_sb_alloc_security - allocate a superblock blob
225 * @sb: the superblock getting the blob
226 *
227 * Returns 0 on success or -ENOMEM on error.
228 */
229static int smack_sb_alloc_security(struct super_block *sb)
230{
231 struct superblock_smack *sbsp;
232
233 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
234
235 if (sbsp == NULL)
236 return -ENOMEM;
237
238 sbsp->smk_root = smack_known_floor.smk_known;
239 sbsp->smk_default = smack_known_floor.smk_known;
240 sbsp->smk_floor = smack_known_floor.smk_known;
241 sbsp->smk_hat = smack_known_hat.smk_known;
242 sbsp->smk_initialized = 0;
243 spin_lock_init(&sbsp->smk_sblock);
244
245 sb->s_security = sbsp;
246
247 return 0;
248}
249
250/**
251 * smack_sb_free_security - free a superblock blob
252 * @sb: the superblock getting the blob
253 *
254 */
255static void smack_sb_free_security(struct super_block *sb)
256{
257 kfree(sb->s_security);
258 sb->s_security = NULL;
259}
260
261/**
262 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800263 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800264 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800265 *
266 * Returns 0 on success or -ENOMEM on error.
267 *
268 * Copy the Smack specific mount options out of the mount
269 * options list.
270 */
Eric Parise0007522008-03-05 10:31:54 -0500271static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800272{
273 char *cp, *commap, *otheropts, *dp;
274
Casey Schauflere114e472008-02-04 22:29:50 -0800275 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
276 if (otheropts == NULL)
277 return -ENOMEM;
278
279 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
280 if (strstr(cp, SMK_FSDEFAULT) == cp)
281 dp = smackopts;
282 else if (strstr(cp, SMK_FSFLOOR) == cp)
283 dp = smackopts;
284 else if (strstr(cp, SMK_FSHAT) == cp)
285 dp = smackopts;
286 else if (strstr(cp, SMK_FSROOT) == cp)
287 dp = smackopts;
288 else
289 dp = otheropts;
290
291 commap = strchr(cp, ',');
292 if (commap != NULL)
293 *commap = '\0';
294
295 if (*dp != '\0')
296 strcat(dp, ",");
297 strcat(dp, cp);
298 }
299
300 strcpy(orig, otheropts);
301 free_page((unsigned long)otheropts);
302
303 return 0;
304}
305
306/**
307 * smack_sb_kern_mount - Smack specific mount processing
308 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100309 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800310 * @data: the smack mount options
311 *
312 * Returns 0 on success, an error code on failure
313 */
James Morris12204e22008-12-19 10:44:42 +1100314static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800315{
316 struct dentry *root = sb->s_root;
317 struct inode *inode = root->d_inode;
318 struct superblock_smack *sp = sb->s_security;
319 struct inode_smack *isp;
320 char *op;
321 char *commap;
322 char *nsp;
323
324 spin_lock(&sp->smk_sblock);
325 if (sp->smk_initialized != 0) {
326 spin_unlock(&sp->smk_sblock);
327 return 0;
328 }
329 sp->smk_initialized = 1;
330 spin_unlock(&sp->smk_sblock);
331
332 for (op = data; op != NULL; op = commap) {
333 commap = strchr(op, ',');
334 if (commap != NULL)
335 *commap++ = '\0';
336
337 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
338 op += strlen(SMK_FSHAT);
339 nsp = smk_import(op, 0);
340 if (nsp != NULL)
341 sp->smk_hat = nsp;
342 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
343 op += strlen(SMK_FSFLOOR);
344 nsp = smk_import(op, 0);
345 if (nsp != NULL)
346 sp->smk_floor = nsp;
347 } else if (strncmp(op, SMK_FSDEFAULT,
348 strlen(SMK_FSDEFAULT)) == 0) {
349 op += strlen(SMK_FSDEFAULT);
350 nsp = smk_import(op, 0);
351 if (nsp != NULL)
352 sp->smk_default = nsp;
353 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
354 op += strlen(SMK_FSROOT);
355 nsp = smk_import(op, 0);
356 if (nsp != NULL)
357 sp->smk_root = nsp;
358 }
359 }
360
361 /*
362 * Initialize the root inode.
363 */
364 isp = inode->i_security;
365 if (isp == NULL)
366 inode->i_security = new_inode_smack(sp->smk_root);
367 else
368 isp->smk_inode = sp->smk_root;
369
370 return 0;
371}
372
373/**
374 * smack_sb_statfs - Smack check on statfs
375 * @dentry: identifies the file system in question
376 *
377 * Returns 0 if current can read the floor of the filesystem,
378 * and error code otherwise
379 */
380static int smack_sb_statfs(struct dentry *dentry)
381{
382 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200383 int rc;
384 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800385
Eric Parisa2694342011-04-25 13:10:27 -0400386 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200387 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
388
389 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
390 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800391}
392
393/**
394 * smack_sb_mount - Smack check for mounting
395 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800396 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800397 * @type: unused
398 * @flags: unused
399 * @data: unused
400 *
401 * Returns 0 if current can write the floor of the filesystem
402 * being mounted on, an error code otherwise.
403 */
Al Virob5266eb2008-03-22 17:48:24 -0400404static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800405 char *type, unsigned long flags, void *data)
406{
Al Virob5266eb2008-03-22 17:48:24 -0400407 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200408 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800409
Eric Parisf48b7392011-04-25 12:54:27 -0400410 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200411 smk_ad_setfield_u_fs_path(&ad, *path);
412
413 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800414}
415
416/**
417 * smack_sb_umount - Smack check for unmounting
418 * @mnt: file system to unmount
419 * @flags: unused
420 *
421 * Returns 0 if current can write the floor of the filesystem
422 * being unmounted, an error code otherwise.
423 */
424static int smack_sb_umount(struct vfsmount *mnt, int flags)
425{
426 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200427 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400428 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200429
Eric Parisa2694342011-04-25 13:10:27 -0400430 path.dentry = mnt->mnt_root;
431 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200432
Eric Parisf48b7392011-04-25 12:54:27 -0400433 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400434 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflere114e472008-02-04 22:29:50 -0800435
436 sbp = mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200437 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800438}
439
440/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800441 * BPRM hooks
442 */
443
444static int smack_bprm_set_creds(struct linux_binprm *bprm)
445{
446 struct task_smack *tsp = bprm->cred->security;
447 struct inode_smack *isp;
448 struct dentry *dp;
449 int rc;
450
451 rc = cap_bprm_set_creds(bprm);
452 if (rc != 0)
453 return rc;
454
455 if (bprm->cred_prepared)
456 return 0;
457
458 if (bprm->file == NULL || bprm->file->f_dentry == NULL)
459 return 0;
460
461 dp = bprm->file->f_dentry;
462
463 if (dp->d_inode == NULL)
464 return 0;
465
466 isp = dp->d_inode->i_security;
467
468 if (isp->smk_task != NULL)
469 tsp->smk_task = isp->smk_task;
470
471 return 0;
472}
473
474/*
Casey Schauflere114e472008-02-04 22:29:50 -0800475 * Inode hooks
476 */
477
478/**
479 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800480 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800481 *
482 * Returns 0 if it gets a blob, -ENOMEM otherwise
483 */
484static int smack_inode_alloc_security(struct inode *inode)
485{
Casey Schaufler676dac42010-12-02 06:43:39 -0800486 inode->i_security = new_inode_smack(smk_of_current());
Casey Schauflere114e472008-02-04 22:29:50 -0800487 if (inode->i_security == NULL)
488 return -ENOMEM;
489 return 0;
490}
491
492/**
493 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800494 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800495 *
496 * Clears the blob pointer in inode
497 */
498static void smack_inode_free_security(struct inode *inode)
499{
500 kfree(inode->i_security);
501 inode->i_security = NULL;
502}
503
504/**
505 * smack_inode_init_security - copy out the smack from an inode
506 * @inode: the inode
507 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500508 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800509 * @name: where to put the attribute name
510 * @value: where to put the attribute value
511 * @len: where to put the length of the attribute
512 *
513 * Returns 0 if it all works out, -ENOMEM if there's no memory
514 */
515static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -0500516 const struct qstr *qstr, char **name,
517 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800518{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700519 struct smack_known *skp;
520 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800521 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200522 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800523 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800524
525 if (name) {
526 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
527 if (*name == NULL)
528 return -ENOMEM;
529 }
530
531 if (value) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700532 skp = smk_find_entry(csp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800533 rcu_read_lock();
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700534 may = smk_access_entry(csp, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800535 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200536
537 /*
538 * If the access rule allows transmutation and
539 * the directory requests transmutation then
540 * by all means transmute.
541 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800542 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
543 smk_inode_transmutable(dir))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200544 isp = dsp;
545
Casey Schauflere114e472008-02-04 22:29:50 -0800546 *value = kstrdup(isp, GFP_KERNEL);
547 if (*value == NULL)
548 return -ENOMEM;
549 }
550
551 if (len)
552 *len = strlen(isp) + 1;
553
554 return 0;
555}
556
557/**
558 * smack_inode_link - Smack check on link
559 * @old_dentry: the existing object
560 * @dir: unused
561 * @new_dentry: the new object
562 *
563 * Returns 0 if access is permitted, an error code otherwise
564 */
565static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
566 struct dentry *new_dentry)
567{
Casey Schauflere114e472008-02-04 22:29:50 -0800568 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200569 struct smk_audit_info ad;
570 int rc;
571
Eric Parisa2694342011-04-25 13:10:27 -0400572 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200573 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800574
575 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200576 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800577
578 if (rc == 0 && new_dentry->d_inode != NULL) {
579 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200580 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
581 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800582 }
583
584 return rc;
585}
586
587/**
588 * smack_inode_unlink - Smack check on inode deletion
589 * @dir: containing directory object
590 * @dentry: file to unlink
591 *
592 * Returns 0 if current can write the containing directory
593 * and the object, error code otherwise
594 */
595static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
596{
597 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200598 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800599 int rc;
600
Eric Parisa2694342011-04-25 13:10:27 -0400601 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200602 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
603
Casey Schauflere114e472008-02-04 22:29:50 -0800604 /*
605 * You need write access to the thing you're unlinking
606 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200607 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
608 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800609 /*
610 * You also need write access to the containing directory
611 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200612 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
613 smk_ad_setfield_u_fs_inode(&ad, dir);
614 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
615 }
Casey Schauflere114e472008-02-04 22:29:50 -0800616 return rc;
617}
618
619/**
620 * smack_inode_rmdir - Smack check on directory deletion
621 * @dir: containing directory object
622 * @dentry: directory to unlink
623 *
624 * Returns 0 if current can write the containing directory
625 * and the directory, error code otherwise
626 */
627static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
628{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200629 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800630 int rc;
631
Eric Parisa2694342011-04-25 13:10:27 -0400632 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200633 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
634
Casey Schauflere114e472008-02-04 22:29:50 -0800635 /*
636 * You need write access to the thing you're removing
637 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200638 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
639 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800640 /*
641 * You also need write access to the containing directory
642 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200643 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
644 smk_ad_setfield_u_fs_inode(&ad, dir);
645 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
646 }
Casey Schauflere114e472008-02-04 22:29:50 -0800647
648 return rc;
649}
650
651/**
652 * smack_inode_rename - Smack check on rename
653 * @old_inode: the old directory
654 * @old_dentry: unused
655 * @new_inode: the new directory
656 * @new_dentry: unused
657 *
658 * Read and write access is required on both the old and
659 * new directories.
660 *
661 * Returns 0 if access is permitted, an error code otherwise
662 */
663static int smack_inode_rename(struct inode *old_inode,
664 struct dentry *old_dentry,
665 struct inode *new_inode,
666 struct dentry *new_dentry)
667{
668 int rc;
669 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200670 struct smk_audit_info ad;
671
Eric Parisa2694342011-04-25 13:10:27 -0400672 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200673 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800674
675 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200676 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800677
678 if (rc == 0 && new_dentry->d_inode != NULL) {
679 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200680 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
681 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800682 }
Casey Schauflere114e472008-02-04 22:29:50 -0800683 return rc;
684}
685
686/**
687 * smack_inode_permission - Smack version of permission()
688 * @inode: the inode in question
689 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800690 *
691 * This is the important Smack hook.
692 *
693 * Returns 0 if access is permitted, -EACCES otherwise
694 */
Al Viroe74f71e2011-06-20 19:38:15 -0400695static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800696{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200697 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400698 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400699
700 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800701 /*
702 * No permission to check. Existence test. Yup, it's there.
703 */
704 if (mask == 0)
705 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700706
707 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400708 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700709 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400710 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200711 smk_ad_setfield_u_fs_inode(&ad, inode);
712 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800713}
714
715/**
716 * smack_inode_setattr - Smack check for setting attributes
717 * @dentry: the object
718 * @iattr: for the force flag
719 *
720 * Returns 0 if access is permitted, an error code otherwise
721 */
722static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
723{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200724 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800725 /*
726 * Need to allow for clearing the setuid bit.
727 */
728 if (iattr->ia_valid & ATTR_FORCE)
729 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400730 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200731 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800732
Etienne Bassetecfcc532009-04-08 20:40:06 +0200733 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800734}
735
736/**
737 * smack_inode_getattr - Smack check for getting attributes
738 * @mnt: unused
739 * @dentry: the object
740 *
741 * Returns 0 if access is permitted, an error code otherwise
742 */
743static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
744{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200745 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400746 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200747
Eric Parisa2694342011-04-25 13:10:27 -0400748 path.dentry = dentry;
749 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200750
Eric Parisf48b7392011-04-25 12:54:27 -0400751 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400752 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200753 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800754}
755
756/**
757 * smack_inode_setxattr - Smack check for setting xattrs
758 * @dentry: the object
759 * @name: name of the attribute
760 * @value: unused
761 * @size: unused
762 * @flags: unused
763 *
764 * This protects the Smack attribute explicitly.
765 *
766 * Returns 0 if access is permitted, an error code otherwise
767 */
David Howells8f0cfa52008-04-29 00:59:41 -0700768static int smack_inode_setxattr(struct dentry *dentry, const char *name,
769 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800770{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200771 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800772 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800773
Casey Schauflerbcdca222008-02-23 15:24:04 -0800774 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
775 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800776 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800777 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
778 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800779 if (!capable(CAP_MAC_ADMIN))
780 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200781 /*
782 * check label validity here so import wont fail on
783 * post_setxattr
784 */
785 if (size == 0 || size >= SMK_LABELLEN ||
786 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400787 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200788 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
789 if (!capable(CAP_MAC_ADMIN))
790 rc = -EPERM;
791 if (size != TRANS_TRUE_SIZE ||
792 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
793 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800794 } else
795 rc = cap_inode_setxattr(dentry, name, value, size, flags);
796
Eric Parisa2694342011-04-25 13:10:27 -0400797 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200798 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
799
Casey Schauflerbcdca222008-02-23 15:24:04 -0800800 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200801 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800802
803 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800804}
805
806/**
807 * smack_inode_post_setxattr - Apply the Smack update approved above
808 * @dentry: object
809 * @name: attribute name
810 * @value: attribute value
811 * @size: attribute size
812 * @flags: unused
813 *
814 * Set the pointer in the inode blob to the entry found
815 * in the master label list.
816 */
David Howells8f0cfa52008-04-29 00:59:41 -0700817static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
818 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800819{
Casey Schauflere114e472008-02-04 22:29:50 -0800820 char *nsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200821 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800822
823 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200824 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800825 if (nsp != NULL)
826 isp->smk_inode = nsp;
827 else
828 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200829 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
830 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800831 if (nsp != NULL)
832 isp->smk_task = nsp;
833 else
834 isp->smk_task = smack_known_invalid.smk_known;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800835 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
836 nsp = smk_import(value, size);
837 if (nsp != NULL)
838 isp->smk_mmap = nsp;
839 else
840 isp->smk_mmap = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200841 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
842 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflere114e472008-02-04 22:29:50 -0800843
844 return;
845}
846
847/*
848 * smack_inode_getxattr - Smack check on getxattr
849 * @dentry: the object
850 * @name: unused
851 *
852 * Returns 0 if access is permitted, an error code otherwise
853 */
David Howells8f0cfa52008-04-29 00:59:41 -0700854static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800855{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200856 struct smk_audit_info ad;
857
Eric Parisa2694342011-04-25 13:10:27 -0400858 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200859 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
860
861 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800862}
863
864/*
865 * smack_inode_removexattr - Smack check on removexattr
866 * @dentry: the object
867 * @name: name of the attribute
868 *
869 * Removing the Smack attribute requires CAP_MAC_ADMIN
870 *
871 * Returns 0 if access is permitted, an error code otherwise
872 */
David Howells8f0cfa52008-04-29 00:59:41 -0700873static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800874{
Casey Schaufler676dac42010-12-02 06:43:39 -0800875 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200876 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800877 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800878
Casey Schauflerbcdca222008-02-23 15:24:04 -0800879 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
880 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800881 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200882 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800883 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
884 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800885 if (!capable(CAP_MAC_ADMIN))
886 rc = -EPERM;
887 } else
888 rc = cap_inode_removexattr(dentry, name);
889
Eric Parisa2694342011-04-25 13:10:27 -0400890 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200891 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800892 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200893 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800894
Casey Schaufler676dac42010-12-02 06:43:39 -0800895 if (rc == 0) {
896 isp = dentry->d_inode->i_security;
897 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800898 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800899 }
900
Casey Schauflerbcdca222008-02-23 15:24:04 -0800901 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800902}
903
904/**
905 * smack_inode_getsecurity - get smack xattrs
906 * @inode: the object
907 * @name: attribute name
908 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800909 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800910 *
911 * Returns the size of the attribute or an error code
912 */
913static int smack_inode_getsecurity(const struct inode *inode,
914 const char *name, void **buffer,
915 bool alloc)
916{
917 struct socket_smack *ssp;
918 struct socket *sock;
919 struct super_block *sbp;
920 struct inode *ip = (struct inode *)inode;
921 char *isp;
922 int ilen;
923 int rc = 0;
924
925 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
926 isp = smk_of_inode(inode);
927 ilen = strlen(isp) + 1;
928 *buffer = isp;
929 return ilen;
930 }
931
932 /*
933 * The rest of the Smack xattrs are only on sockets.
934 */
935 sbp = ip->i_sb;
936 if (sbp->s_magic != SOCKFS_MAGIC)
937 return -EOPNOTSUPP;
938
939 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800940 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800941 return -EOPNOTSUPP;
942
943 ssp = sock->sk->sk_security;
944
945 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
946 isp = ssp->smk_in;
947 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
948 isp = ssp->smk_out;
949 else
950 return -EOPNOTSUPP;
951
952 ilen = strlen(isp) + 1;
953 if (rc == 0) {
954 *buffer = isp;
955 rc = ilen;
956 }
957
958 return rc;
959}
960
961
962/**
963 * smack_inode_listsecurity - list the Smack attributes
964 * @inode: the object
965 * @buffer: where they go
966 * @buffer_size: size of buffer
967 *
968 * Returns 0 on success, -EINVAL otherwise
969 */
970static int smack_inode_listsecurity(struct inode *inode, char *buffer,
971 size_t buffer_size)
972{
973 int len = strlen(XATTR_NAME_SMACK);
974
975 if (buffer != NULL && len <= buffer_size) {
976 memcpy(buffer, XATTR_NAME_SMACK, len);
977 return len;
978 }
979 return -EINVAL;
980}
981
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +1000982/**
983 * smack_inode_getsecid - Extract inode's security id
984 * @inode: inode to extract the info from
985 * @secid: where result will be saved
986 */
987static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
988{
989 struct inode_smack *isp = inode->i_security;
990
991 *secid = smack_to_secid(isp->smk_inode);
992}
993
Casey Schauflere114e472008-02-04 22:29:50 -0800994/*
995 * File Hooks
996 */
997
998/**
999 * smack_file_permission - Smack check on file operations
1000 * @file: unused
1001 * @mask: unused
1002 *
1003 * Returns 0
1004 *
1005 * Should access checks be done on each read or write?
1006 * UNICOS and SELinux say yes.
1007 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1008 *
1009 * I'll say no for now. Smack does not do the frequent
1010 * label changing that SELinux does.
1011 */
1012static int smack_file_permission(struct file *file, int mask)
1013{
1014 return 0;
1015}
1016
1017/**
1018 * smack_file_alloc_security - assign a file security blob
1019 * @file: the object
1020 *
1021 * The security blob for a file is a pointer to the master
1022 * label list, so no allocation is done.
1023 *
1024 * Returns 0
1025 */
1026static int smack_file_alloc_security(struct file *file)
1027{
Casey Schaufler676dac42010-12-02 06:43:39 -08001028 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001029 return 0;
1030}
1031
1032/**
1033 * smack_file_free_security - clear a file security blob
1034 * @file: the object
1035 *
1036 * The security blob for a file is a pointer to the master
1037 * label list, so no memory is freed.
1038 */
1039static void smack_file_free_security(struct file *file)
1040{
1041 file->f_security = NULL;
1042}
1043
1044/**
1045 * smack_file_ioctl - Smack check on ioctls
1046 * @file: the object
1047 * @cmd: what to do
1048 * @arg: unused
1049 *
1050 * Relies heavily on the correct use of the ioctl command conventions.
1051 *
1052 * Returns 0 if allowed, error code otherwise
1053 */
1054static int smack_file_ioctl(struct file *file, unsigned int cmd,
1055 unsigned long arg)
1056{
1057 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001058 struct smk_audit_info ad;
1059
Eric Parisf48b7392011-04-25 12:54:27 -04001060 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001061 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001062
1063 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001064 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001065
1066 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001067 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001068
1069 return rc;
1070}
1071
1072/**
1073 * smack_file_lock - Smack check on file locking
1074 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001075 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001076 *
1077 * Returns 0 if current has write access, error code otherwise
1078 */
1079static int smack_file_lock(struct file *file, unsigned int cmd)
1080{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001081 struct smk_audit_info ad;
1082
Eric Paris92f42502011-04-25 13:15:55 -04001083 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1084 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001086}
1087
1088/**
1089 * smack_file_fcntl - Smack check on fcntl
1090 * @file: the object
1091 * @cmd: what action to check
1092 * @arg: unused
1093 *
1094 * Returns 0 if current has access, error code otherwise
1095 */
1096static int smack_file_fcntl(struct file *file, unsigned int cmd,
1097 unsigned long arg)
1098{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001099 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001100 int rc;
1101
Eric Parisf48b7392011-04-25 12:54:27 -04001102 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1104
Casey Schauflere114e472008-02-04 22:29:50 -08001105 switch (cmd) {
1106 case F_DUPFD:
1107 case F_GETFD:
1108 case F_GETFL:
1109 case F_GETLK:
1110 case F_GETOWN:
1111 case F_GETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001112 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001113 break;
1114 case F_SETFD:
1115 case F_SETFL:
1116 case F_SETLK:
1117 case F_SETLKW:
1118 case F_SETOWN:
1119 case F_SETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001120 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001121 break;
1122 default:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001123 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001124 }
1125
1126 return rc;
1127}
1128
1129/**
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001130 * smack_file_mmap :
1131 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1132 * if mapping anonymous memory.
1133 * @file contains the file structure for file to map (may be NULL).
1134 * @reqprot contains the protection requested by the application.
1135 * @prot contains the protection that will be applied by the kernel.
1136 * @flags contains the operational flags.
1137 * Return 0 if permission is granted.
1138 */
1139static int smack_file_mmap(struct file *file,
1140 unsigned long reqprot, unsigned long prot,
1141 unsigned long flags, unsigned long addr,
1142 unsigned long addr_only)
1143{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001144 struct smack_known *skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001145 struct smack_rule *srp;
1146 struct task_smack *tsp;
1147 char *sp;
1148 char *msmack;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001149 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001150 struct inode_smack *isp;
1151 struct dentry *dp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001152 int may;
1153 int mmay;
1154 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001155 int rc;
1156
1157 /* do DAC check on address space usage */
1158 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1159 if (rc || addr_only)
1160 return rc;
1161
1162 if (file == NULL || file->f_dentry == NULL)
1163 return 0;
1164
1165 dp = file->f_dentry;
1166
1167 if (dp->d_inode == NULL)
1168 return 0;
1169
1170 isp = dp->d_inode->i_security;
1171 if (isp->smk_mmap == NULL)
1172 return 0;
1173 msmack = isp->smk_mmap;
1174
1175 tsp = current_security();
1176 sp = smk_of_current();
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001177 skp = smk_find_entry(sp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001178 rc = 0;
1179
1180 rcu_read_lock();
1181 /*
1182 * For each Smack rule associated with the subject
1183 * label verify that the SMACK64MMAP also has access
1184 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001185 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001186 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001187 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001188 /*
1189 * Matching labels always allows access.
1190 */
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001191 if (msmack == osmack)
1192 continue;
1193 /*
1194 * If there is a matching local rule take
1195 * that into account as well.
1196 */
1197 may = smk_access_entry(srp->smk_subject, osmack,
1198 &tsp->smk_rules);
1199 if (may == -ENOENT)
1200 may = srp->smk_access;
1201 else
1202 may &= srp->smk_access;
1203 /*
1204 * If may is zero the SMACK64MMAP subject can't
1205 * possibly have less access.
1206 */
1207 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001208 continue;
1209
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001210 /*
1211 * Fetch the global list entry.
1212 * If there isn't one a SMACK64MMAP subject
1213 * can't have as much access as current.
1214 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001215 skp = smk_find_entry(msmack);
1216 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001217 if (mmay == -ENOENT) {
1218 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001219 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001220 }
1221 /*
1222 * If there is a local entry it modifies the
1223 * potential access, too.
1224 */
1225 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1226 if (tmay != -ENOENT)
1227 mmay &= tmay;
1228
1229 /*
1230 * If there is any access available to current that is
1231 * not available to a SMACK64MMAP subject
1232 * deny access.
1233 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001234 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001235 rc = -EACCES;
1236 break;
1237 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001238 }
1239
1240 rcu_read_unlock();
1241
1242 return rc;
1243}
1244
1245/**
Casey Schauflere114e472008-02-04 22:29:50 -08001246 * smack_file_set_fowner - set the file security blob value
1247 * @file: object in question
1248 *
1249 * Returns 0
1250 * Further research may be required on this one.
1251 */
1252static int smack_file_set_fowner(struct file *file)
1253{
Casey Schaufler676dac42010-12-02 06:43:39 -08001254 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001255 return 0;
1256}
1257
1258/**
1259 * smack_file_send_sigiotask - Smack on sigio
1260 * @tsk: The target task
1261 * @fown: the object the signal come from
1262 * @signum: unused
1263 *
1264 * Allow a privileged task to get signals even if it shouldn't
1265 *
1266 * Returns 0 if a subject with the object's smack could
1267 * write to the task, an error code otherwise.
1268 */
1269static int smack_file_send_sigiotask(struct task_struct *tsk,
1270 struct fown_struct *fown, int signum)
1271{
1272 struct file *file;
1273 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001274 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001275 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001276
1277 /*
1278 * struct fown_struct is never outside the context of a struct file
1279 */
1280 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001281
Etienne Bassetecfcc532009-04-08 20:40:06 +02001282 /* we don't log here as rc can be overriden */
1283 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001284 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001285 rc = 0;
1286
1287 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1288 smk_ad_setfield_u_tsk(&ad, tsk);
1289 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001290 return rc;
1291}
1292
1293/**
1294 * smack_file_receive - Smack file receive check
1295 * @file: the object
1296 *
1297 * Returns 0 if current has access, error code otherwise
1298 */
1299static int smack_file_receive(struct file *file)
1300{
1301 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001302 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001303
Etienne Bassetecfcc532009-04-08 20:40:06 +02001304 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1305 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001306 /*
1307 * This code relies on bitmasks.
1308 */
1309 if (file->f_mode & FMODE_READ)
1310 may = MAY_READ;
1311 if (file->f_mode & FMODE_WRITE)
1312 may |= MAY_WRITE;
1313
Etienne Bassetecfcc532009-04-08 20:40:06 +02001314 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001315}
1316
1317/*
1318 * Task hooks
1319 */
1320
1321/**
David Howellsee18d642009-09-02 09:14:21 +01001322 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1323 * @new: the new credentials
1324 * @gfp: the atomicity of any memory allocations
1325 *
1326 * Prepare a blank set of credentials for modification. This must allocate all
1327 * the memory the LSM module might require such that cred_transfer() can
1328 * complete without error.
1329 */
1330static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1331{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001332 struct task_smack *tsp;
1333
1334 tsp = new_task_smack(NULL, NULL, gfp);
1335 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001336 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001337
1338 cred->security = tsp;
1339
David Howellsee18d642009-09-02 09:14:21 +01001340 return 0;
1341}
1342
1343
1344/**
David Howellsf1752ee2008-11-14 10:39:17 +11001345 * smack_cred_free - "free" task-level security credentials
1346 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001347 *
Casey Schauflere114e472008-02-04 22:29:50 -08001348 */
David Howellsf1752ee2008-11-14 10:39:17 +11001349static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001350{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001351 struct task_smack *tsp = cred->security;
1352 struct smack_rule *rp;
1353 struct list_head *l;
1354 struct list_head *n;
1355
1356 if (tsp == NULL)
1357 return;
1358 cred->security = NULL;
1359
1360 list_for_each_safe(l, n, &tsp->smk_rules) {
1361 rp = list_entry(l, struct smack_rule, list);
1362 list_del(&rp->list);
1363 kfree(rp);
1364 }
1365 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001366}
1367
1368/**
David Howellsd84f4f92008-11-14 10:39:23 +11001369 * smack_cred_prepare - prepare new set of credentials for modification
1370 * @new: the new credentials
1371 * @old: the original credentials
1372 * @gfp: the atomicity of any memory allocations
1373 *
1374 * Prepare a new set of credentials for modification.
1375 */
1376static int smack_cred_prepare(struct cred *new, const struct cred *old,
1377 gfp_t gfp)
1378{
Casey Schaufler676dac42010-12-02 06:43:39 -08001379 struct task_smack *old_tsp = old->security;
1380 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001381 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001382
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001383 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001384 if (new_tsp == NULL)
1385 return -ENOMEM;
1386
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001387 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1388 if (rc != 0)
1389 return rc;
1390
Casey Schaufler676dac42010-12-02 06:43:39 -08001391 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001392 return 0;
1393}
1394
Randy Dunlap251a2a92009-02-18 11:42:33 -08001395/**
David Howellsee18d642009-09-02 09:14:21 +01001396 * smack_cred_transfer - Transfer the old credentials to the new credentials
1397 * @new: the new credentials
1398 * @old: the original credentials
1399 *
1400 * Fill in a set of blank credentials from another set of credentials.
1401 */
1402static void smack_cred_transfer(struct cred *new, const struct cred *old)
1403{
Casey Schaufler676dac42010-12-02 06:43:39 -08001404 struct task_smack *old_tsp = old->security;
1405 struct task_smack *new_tsp = new->security;
1406
1407 new_tsp->smk_task = old_tsp->smk_task;
1408 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001409 mutex_init(&new_tsp->smk_rules_lock);
1410 INIT_LIST_HEAD(&new_tsp->smk_rules);
1411
1412
1413 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001414}
1415
1416/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001417 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001418 * @new: points to the set of credentials to be modified.
1419 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001420 *
1421 * Set the security data for a kernel service.
1422 */
1423static int smack_kernel_act_as(struct cred *new, u32 secid)
1424{
Casey Schaufler676dac42010-12-02 06:43:39 -08001425 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001426 char *smack = smack_from_secid(secid);
1427
1428 if (smack == NULL)
1429 return -EINVAL;
1430
Casey Schaufler676dac42010-12-02 06:43:39 -08001431 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001432 return 0;
1433}
1434
1435/**
1436 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001437 * @new: points to the set of credentials to be modified
1438 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001439 *
1440 * Set the file creation context in a set of credentials to the same
1441 * as the objective context of the specified inode
1442 */
1443static int smack_kernel_create_files_as(struct cred *new,
1444 struct inode *inode)
1445{
1446 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001447 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001448
Casey Schaufler676dac42010-12-02 06:43:39 -08001449 tsp->smk_forked = isp->smk_inode;
1450 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001451 return 0;
1452}
1453
1454/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001455 * smk_curacc_on_task - helper to log task related access
1456 * @p: the task object
1457 * @access : the access requested
1458 *
1459 * Return 0 if access is permitted
1460 */
1461static int smk_curacc_on_task(struct task_struct *p, int access)
1462{
1463 struct smk_audit_info ad;
1464
1465 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1466 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001467 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001468}
1469
1470/**
Casey Schauflere114e472008-02-04 22:29:50 -08001471 * smack_task_setpgid - Smack check on setting pgid
1472 * @p: the task object
1473 * @pgid: unused
1474 *
1475 * Return 0 if write access is permitted
1476 */
1477static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1478{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001479 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001480}
1481
1482/**
1483 * smack_task_getpgid - Smack access check for getpgid
1484 * @p: the object task
1485 *
1486 * Returns 0 if current can read the object task, error code otherwise
1487 */
1488static int smack_task_getpgid(struct task_struct *p)
1489{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001490 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001491}
1492
1493/**
1494 * smack_task_getsid - Smack access check for getsid
1495 * @p: the object task
1496 *
1497 * Returns 0 if current can read the object task, error code otherwise
1498 */
1499static int smack_task_getsid(struct task_struct *p)
1500{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001501 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001502}
1503
1504/**
1505 * smack_task_getsecid - get the secid of the task
1506 * @p: the object task
1507 * @secid: where to put the result
1508 *
1509 * Sets the secid to contain a u32 version of the smack label.
1510 */
1511static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1512{
Casey Schaufler676dac42010-12-02 06:43:39 -08001513 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001514}
1515
1516/**
1517 * smack_task_setnice - Smack check on setting nice
1518 * @p: the task object
1519 * @nice: unused
1520 *
1521 * Return 0 if write access is permitted
1522 */
1523static int smack_task_setnice(struct task_struct *p, int nice)
1524{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001525 int rc;
1526
1527 rc = cap_task_setnice(p, nice);
1528 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001529 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001530 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001531}
1532
1533/**
1534 * smack_task_setioprio - Smack check on setting ioprio
1535 * @p: the task object
1536 * @ioprio: unused
1537 *
1538 * Return 0 if write access is permitted
1539 */
1540static int smack_task_setioprio(struct task_struct *p, int ioprio)
1541{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001542 int rc;
1543
1544 rc = cap_task_setioprio(p, ioprio);
1545 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001546 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001547 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001548}
1549
1550/**
1551 * smack_task_getioprio - Smack check on reading ioprio
1552 * @p: the task object
1553 *
1554 * Return 0 if read access is permitted
1555 */
1556static int smack_task_getioprio(struct task_struct *p)
1557{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001558 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001559}
1560
1561/**
1562 * smack_task_setscheduler - Smack check on setting scheduler
1563 * @p: the task object
1564 * @policy: unused
1565 * @lp: unused
1566 *
1567 * Return 0 if read access is permitted
1568 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001569static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001570{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001571 int rc;
1572
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001573 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001574 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001575 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001576 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001577}
1578
1579/**
1580 * smack_task_getscheduler - Smack check on reading scheduler
1581 * @p: the task object
1582 *
1583 * Return 0 if read access is permitted
1584 */
1585static int smack_task_getscheduler(struct task_struct *p)
1586{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001587 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001588}
1589
1590/**
1591 * smack_task_movememory - Smack check on moving memory
1592 * @p: the task object
1593 *
1594 * Return 0 if write access is permitted
1595 */
1596static int smack_task_movememory(struct task_struct *p)
1597{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001598 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001599}
1600
1601/**
1602 * smack_task_kill - Smack check on signal delivery
1603 * @p: the task object
1604 * @info: unused
1605 * @sig: unused
1606 * @secid: identifies the smack to use in lieu of current's
1607 *
1608 * Return 0 if write access is permitted
1609 *
1610 * The secid behavior is an artifact of an SELinux hack
1611 * in the USB code. Someday it may go away.
1612 */
1613static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1614 int sig, u32 secid)
1615{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001616 struct smk_audit_info ad;
1617
1618 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1619 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001620 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001621 * Sending a signal requires that the sender
1622 * can write the receiver.
1623 */
1624 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001625 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1626 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001627 /*
1628 * If the secid isn't 0 we're dealing with some USB IO
1629 * specific behavior. This is not clean. For one thing
1630 * we can't take privilege into account.
1631 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001632 return smk_access(smack_from_secid(secid),
1633 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001634}
1635
1636/**
1637 * smack_task_wait - Smack access check for waiting
1638 * @p: task to wait for
1639 *
1640 * Returns 0 if current can wait for p, error code otherwise
1641 */
1642static int smack_task_wait(struct task_struct *p)
1643{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001644 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08001645 char *sp = smk_of_current();
1646 char *tsp = smk_of_forked(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001647 int rc;
1648
Etienne Bassetecfcc532009-04-08 20:40:06 +02001649 /* we don't log here, we can be overriden */
Casey Schaufler676dac42010-12-02 06:43:39 -08001650 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001651 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001652 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001653
1654 /*
1655 * Allow the operation to succeed if either task
1656 * has privilege to perform operations that might
1657 * account for the smack labels having gotten to
1658 * be different in the first place.
1659 *
David Howells5cd9c582008-08-14 11:37:28 +01001660 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001661 * control ideal, taking the object's privilege
1662 * state into account in the decision as well as
1663 * the smack value.
1664 */
David Howells5cd9c582008-08-14 11:37:28 +01001665 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001666 rc = 0;
1667 /* we log only if we didn't get overriden */
1668 out_log:
1669 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1670 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001671 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001672 return rc;
1673}
1674
1675/**
1676 * smack_task_to_inode - copy task smack into the inode blob
1677 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001678 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001679 *
1680 * Sets the smack pointer in the inode security blob
1681 */
1682static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1683{
1684 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001685 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001686}
1687
1688/*
1689 * Socket hooks.
1690 */
1691
1692/**
1693 * smack_sk_alloc_security - Allocate a socket blob
1694 * @sk: the socket
1695 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001696 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001697 *
1698 * Assign Smack pointers to current
1699 *
1700 * Returns 0 on success, -ENOMEM is there's no memory
1701 */
1702static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1703{
Casey Schaufler676dac42010-12-02 06:43:39 -08001704 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001705 struct socket_smack *ssp;
1706
1707 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1708 if (ssp == NULL)
1709 return -ENOMEM;
1710
1711 ssp->smk_in = csp;
1712 ssp->smk_out = csp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001713 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001714
1715 sk->sk_security = ssp;
1716
1717 return 0;
1718}
1719
1720/**
1721 * smack_sk_free_security - Free a socket blob
1722 * @sk: the socket
1723 *
1724 * Clears the blob pointer
1725 */
1726static void smack_sk_free_security(struct sock *sk)
1727{
1728 kfree(sk->sk_security);
1729}
1730
1731/**
Paul Moore07feee82009-03-27 17:10:54 -04001732* smack_host_label - check host based restrictions
1733* @sip: the object end
1734*
1735* looks for host based access restrictions
1736*
1737* This version will only be appropriate for really small sets of single label
1738* hosts. The caller is responsible for ensuring that the RCU read lock is
1739* taken before calling this function.
1740*
1741* Returns the label of the far end or NULL if it's not special.
1742*/
1743static char *smack_host_label(struct sockaddr_in *sip)
1744{
1745 struct smk_netlbladdr *snp;
1746 struct in_addr *siap = &sip->sin_addr;
1747
1748 if (siap->s_addr == 0)
1749 return NULL;
1750
1751 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1752 /*
1753 * we break after finding the first match because
1754 * the list is sorted from longest to shortest mask
1755 * so we have found the most specific match
1756 */
1757 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001758 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1759 /* we have found the special CIPSO option */
1760 if (snp->smk_label == smack_cipso_option)
1761 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001762 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001763 }
Paul Moore07feee82009-03-27 17:10:54 -04001764
1765 return NULL;
1766}
1767
1768/**
Casey Schauflere114e472008-02-04 22:29:50 -08001769 * smack_set_catset - convert a capset to netlabel mls categories
1770 * @catset: the Smack categories
1771 * @sap: where to put the netlabel categories
1772 *
1773 * Allocates and fills attr.mls.cat
1774 */
1775static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1776{
1777 unsigned char *cp;
1778 unsigned char m;
1779 int cat;
1780 int rc;
1781 int byte;
1782
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001783 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001784 return;
1785
1786 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1787 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1788 sap->attr.mls.cat->startbit = 0;
1789
1790 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1791 for (m = 0x80; m != 0; m >>= 1, cat++) {
1792 if ((m & *cp) == 0)
1793 continue;
1794 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1795 cat, GFP_ATOMIC);
1796 }
1797}
1798
1799/**
1800 * smack_to_secattr - fill a secattr from a smack value
1801 * @smack: the smack value
1802 * @nlsp: where the result goes
1803 *
1804 * Casey says that CIPSO is good enough for now.
1805 * It can be used to effect.
1806 * It can also be abused to effect when necessary.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001807 * Apologies to the TSIG group in general and GW in particular.
Casey Schauflere114e472008-02-04 22:29:50 -08001808 */
1809static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1810{
1811 struct smack_cipso cipso;
1812 int rc;
1813
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001814 nlsp->domain = smack;
1815 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001816
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001817 rc = smack_to_cipso(smack, &cipso);
1818 if (rc == 0) {
1819 nlsp->attr.mls.lvl = cipso.smk_level;
1820 smack_set_catset(cipso.smk_catset, nlsp);
1821 } else {
1822 nlsp->attr.mls.lvl = smack_cipso_direct;
1823 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001824 }
1825}
1826
1827/**
1828 * smack_netlabel - Set the secattr on a socket
1829 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001830 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001831 *
1832 * Convert the outbound smack value (smk_out) to a
1833 * secattr and attach it to the socket.
1834 *
1835 * Returns 0 on success or an error code
1836 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001837static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001838{
Paul Moore07feee82009-03-27 17:10:54 -04001839 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001840 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001841 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001842
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001843 /*
1844 * Usually the netlabel code will handle changing the
1845 * packet labeling based on the label.
1846 * The case of a single label host is different, because
1847 * a single label host should never get a labeled packet
1848 * even though the label is usually associated with a packet
1849 * label.
1850 */
1851 local_bh_disable();
1852 bh_lock_sock_nested(sk);
1853
1854 if (ssp->smk_out == smack_net_ambient ||
1855 labeled == SMACK_UNLABELED_SOCKET)
1856 netlbl_sock_delattr(sk);
1857 else {
1858 netlbl_secattr_init(&secattr);
1859 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001860 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001861 netlbl_secattr_destroy(&secattr);
1862 }
1863
1864 bh_unlock_sock(sk);
1865 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001866
Casey Schauflere114e472008-02-04 22:29:50 -08001867 return rc;
1868}
1869
1870/**
Paul Moore07feee82009-03-27 17:10:54 -04001871 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1872 * @sk: the socket
1873 * @sap: the destination address
1874 *
1875 * Set the correct secattr for the given socket based on the destination
1876 * address and perform any outbound access checks needed.
1877 *
1878 * Returns 0 on success or an error code.
1879 *
1880 */
1881static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1882{
1883 int rc;
1884 int sk_lbl;
1885 char *hostsp;
1886 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001887 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001888
1889 rcu_read_lock();
1890 hostsp = smack_host_label(sap);
1891 if (hostsp != NULL) {
1892 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001893#ifdef CONFIG_AUDIT
1894 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1895 ad.a.u.net.family = sap->sin_family;
1896 ad.a.u.net.dport = sap->sin_port;
1897 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1898#endif
1899 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001900 } else {
1901 sk_lbl = SMACK_CIPSO_SOCKET;
1902 rc = 0;
1903 }
1904 rcu_read_unlock();
1905 if (rc != 0)
1906 return rc;
1907
1908 return smack_netlabel(sk, sk_lbl);
1909}
1910
1911/**
Casey Schauflere114e472008-02-04 22:29:50 -08001912 * smack_inode_setsecurity - set smack xattrs
1913 * @inode: the object
1914 * @name: attribute name
1915 * @value: attribute value
1916 * @size: size of the attribute
1917 * @flags: unused
1918 *
1919 * Sets the named attribute in the appropriate blob
1920 *
1921 * Returns 0 on success, or an error code
1922 */
1923static int smack_inode_setsecurity(struct inode *inode, const char *name,
1924 const void *value, size_t size, int flags)
1925{
1926 char *sp;
1927 struct inode_smack *nsp = inode->i_security;
1928 struct socket_smack *ssp;
1929 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001930 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001931
Etienne Basset43031542009-03-27 17:11:01 -04001932 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001933 return -EACCES;
1934
1935 sp = smk_import(value, size);
1936 if (sp == NULL)
1937 return -EINVAL;
1938
1939 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1940 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04001941 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08001942 return 0;
1943 }
1944 /*
1945 * The rest of the Smack xattrs are only on sockets.
1946 */
1947 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1948 return -EOPNOTSUPP;
1949
1950 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001951 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001952 return -EOPNOTSUPP;
1953
1954 ssp = sock->sk->sk_security;
1955
1956 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1957 ssp->smk_in = sp;
1958 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1959 ssp->smk_out = sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08001960 if (sock->sk->sk_family != PF_UNIX) {
1961 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1962 if (rc != 0)
1963 printk(KERN_WARNING
1964 "Smack: \"%s\" netlbl error %d.\n",
1965 __func__, -rc);
1966 }
Casey Schauflere114e472008-02-04 22:29:50 -08001967 } else
1968 return -EOPNOTSUPP;
1969
1970 return 0;
1971}
1972
1973/**
1974 * smack_socket_post_create - finish socket setup
1975 * @sock: the socket
1976 * @family: protocol family
1977 * @type: unused
1978 * @protocol: unused
1979 * @kern: unused
1980 *
1981 * Sets the netlabel information on the socket
1982 *
1983 * Returns 0 on success, and error code otherwise
1984 */
1985static int smack_socket_post_create(struct socket *sock, int family,
1986 int type, int protocol, int kern)
1987{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001988 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001989 return 0;
1990 /*
1991 * Set the outbound netlbl.
1992 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001993 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1994}
1995
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001996/**
1997 * smack_socket_connect - connect access check
1998 * @sock: the socket
1999 * @sap: the other end
2000 * @addrlen: size of sap
2001 *
2002 * Verifies that a connection may be possible
2003 *
2004 * Returns 0 on success, and error code otherwise
2005 */
2006static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2007 int addrlen)
2008{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002009 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2010 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002011 if (addrlen < sizeof(struct sockaddr_in))
2012 return -EINVAL;
2013
Paul Moore07feee82009-03-27 17:10:54 -04002014 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08002015}
2016
2017/**
2018 * smack_flags_to_may - convert S_ to MAY_ values
2019 * @flags: the S_ value
2020 *
2021 * Returns the equivalent MAY_ value
2022 */
2023static int smack_flags_to_may(int flags)
2024{
2025 int may = 0;
2026
2027 if (flags & S_IRUGO)
2028 may |= MAY_READ;
2029 if (flags & S_IWUGO)
2030 may |= MAY_WRITE;
2031 if (flags & S_IXUGO)
2032 may |= MAY_EXEC;
2033
2034 return may;
2035}
2036
2037/**
2038 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2039 * @msg: the object
2040 *
2041 * Returns 0
2042 */
2043static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2044{
Casey Schaufler676dac42010-12-02 06:43:39 -08002045 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002046 return 0;
2047}
2048
2049/**
2050 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2051 * @msg: the object
2052 *
2053 * Clears the blob pointer
2054 */
2055static void smack_msg_msg_free_security(struct msg_msg *msg)
2056{
2057 msg->security = NULL;
2058}
2059
2060/**
2061 * smack_of_shm - the smack pointer for the shm
2062 * @shp: the object
2063 *
2064 * Returns a pointer to the smack value
2065 */
2066static char *smack_of_shm(struct shmid_kernel *shp)
2067{
2068 return (char *)shp->shm_perm.security;
2069}
2070
2071/**
2072 * smack_shm_alloc_security - Set the security blob for shm
2073 * @shp: the object
2074 *
2075 * Returns 0
2076 */
2077static int smack_shm_alloc_security(struct shmid_kernel *shp)
2078{
2079 struct kern_ipc_perm *isp = &shp->shm_perm;
2080
Casey Schaufler676dac42010-12-02 06:43:39 -08002081 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002082 return 0;
2083}
2084
2085/**
2086 * smack_shm_free_security - Clear the security blob for shm
2087 * @shp: the object
2088 *
2089 * Clears the blob pointer
2090 */
2091static void smack_shm_free_security(struct shmid_kernel *shp)
2092{
2093 struct kern_ipc_perm *isp = &shp->shm_perm;
2094
2095 isp->security = NULL;
2096}
2097
2098/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002099 * smk_curacc_shm : check if current has access on shm
2100 * @shp : the object
2101 * @access : access requested
2102 *
2103 * Returns 0 if current has the requested access, error code otherwise
2104 */
2105static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2106{
2107 char *ssp = smack_of_shm(shp);
2108 struct smk_audit_info ad;
2109
2110#ifdef CONFIG_AUDIT
2111 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2112 ad.a.u.ipc_id = shp->shm_perm.id;
2113#endif
2114 return smk_curacc(ssp, access, &ad);
2115}
2116
2117/**
Casey Schauflere114e472008-02-04 22:29:50 -08002118 * smack_shm_associate - Smack access check for shm
2119 * @shp: the object
2120 * @shmflg: access requested
2121 *
2122 * Returns 0 if current has the requested access, error code otherwise
2123 */
2124static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2125{
Casey Schauflere114e472008-02-04 22:29:50 -08002126 int may;
2127
2128 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002129 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002130}
2131
2132/**
2133 * smack_shm_shmctl - Smack access check for shm
2134 * @shp: the object
2135 * @cmd: what it wants to do
2136 *
2137 * Returns 0 if current has the requested access, error code otherwise
2138 */
2139static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2140{
Casey Schauflere114e472008-02-04 22:29:50 -08002141 int may;
2142
2143 switch (cmd) {
2144 case IPC_STAT:
2145 case SHM_STAT:
2146 may = MAY_READ;
2147 break;
2148 case IPC_SET:
2149 case SHM_LOCK:
2150 case SHM_UNLOCK:
2151 case IPC_RMID:
2152 may = MAY_READWRITE;
2153 break;
2154 case IPC_INFO:
2155 case SHM_INFO:
2156 /*
2157 * System level information.
2158 */
2159 return 0;
2160 default:
2161 return -EINVAL;
2162 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002163 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002164}
2165
2166/**
2167 * smack_shm_shmat - Smack access for shmat
2168 * @shp: the object
2169 * @shmaddr: unused
2170 * @shmflg: access requested
2171 *
2172 * Returns 0 if current has the requested access, error code otherwise
2173 */
2174static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2175 int shmflg)
2176{
Casey Schauflere114e472008-02-04 22:29:50 -08002177 int may;
2178
2179 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002180 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002181}
2182
2183/**
2184 * smack_of_sem - the smack pointer for the sem
2185 * @sma: the object
2186 *
2187 * Returns a pointer to the smack value
2188 */
2189static char *smack_of_sem(struct sem_array *sma)
2190{
2191 return (char *)sma->sem_perm.security;
2192}
2193
2194/**
2195 * smack_sem_alloc_security - Set the security blob for sem
2196 * @sma: the object
2197 *
2198 * Returns 0
2199 */
2200static int smack_sem_alloc_security(struct sem_array *sma)
2201{
2202 struct kern_ipc_perm *isp = &sma->sem_perm;
2203
Casey Schaufler676dac42010-12-02 06:43:39 -08002204 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002205 return 0;
2206}
2207
2208/**
2209 * smack_sem_free_security - Clear the security blob for sem
2210 * @sma: the object
2211 *
2212 * Clears the blob pointer
2213 */
2214static void smack_sem_free_security(struct sem_array *sma)
2215{
2216 struct kern_ipc_perm *isp = &sma->sem_perm;
2217
2218 isp->security = NULL;
2219}
2220
2221/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002222 * smk_curacc_sem : check if current has access on sem
2223 * @sma : the object
2224 * @access : access requested
2225 *
2226 * Returns 0 if current has the requested access, error code otherwise
2227 */
2228static int smk_curacc_sem(struct sem_array *sma, int access)
2229{
2230 char *ssp = smack_of_sem(sma);
2231 struct smk_audit_info ad;
2232
2233#ifdef CONFIG_AUDIT
2234 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2235 ad.a.u.ipc_id = sma->sem_perm.id;
2236#endif
2237 return smk_curacc(ssp, access, &ad);
2238}
2239
2240/**
Casey Schauflere114e472008-02-04 22:29:50 -08002241 * smack_sem_associate - Smack access check for sem
2242 * @sma: the object
2243 * @semflg: access requested
2244 *
2245 * Returns 0 if current has the requested access, error code otherwise
2246 */
2247static int smack_sem_associate(struct sem_array *sma, int semflg)
2248{
Casey Schauflere114e472008-02-04 22:29:50 -08002249 int may;
2250
2251 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002252 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002253}
2254
2255/**
2256 * smack_sem_shmctl - Smack access check for sem
2257 * @sma: the object
2258 * @cmd: what it wants to do
2259 *
2260 * Returns 0 if current has the requested access, error code otherwise
2261 */
2262static int smack_sem_semctl(struct sem_array *sma, int cmd)
2263{
Casey Schauflere114e472008-02-04 22:29:50 -08002264 int may;
2265
2266 switch (cmd) {
2267 case GETPID:
2268 case GETNCNT:
2269 case GETZCNT:
2270 case GETVAL:
2271 case GETALL:
2272 case IPC_STAT:
2273 case SEM_STAT:
2274 may = MAY_READ;
2275 break;
2276 case SETVAL:
2277 case SETALL:
2278 case IPC_RMID:
2279 case IPC_SET:
2280 may = MAY_READWRITE;
2281 break;
2282 case IPC_INFO:
2283 case SEM_INFO:
2284 /*
2285 * System level information
2286 */
2287 return 0;
2288 default:
2289 return -EINVAL;
2290 }
2291
Etienne Bassetecfcc532009-04-08 20:40:06 +02002292 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002293}
2294
2295/**
2296 * smack_sem_semop - Smack checks of semaphore operations
2297 * @sma: the object
2298 * @sops: unused
2299 * @nsops: unused
2300 * @alter: unused
2301 *
2302 * Treated as read and write in all cases.
2303 *
2304 * Returns 0 if access is allowed, error code otherwise
2305 */
2306static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2307 unsigned nsops, int alter)
2308{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002309 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002310}
2311
2312/**
2313 * smack_msg_alloc_security - Set the security blob for msg
2314 * @msq: the object
2315 *
2316 * Returns 0
2317 */
2318static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2319{
2320 struct kern_ipc_perm *kisp = &msq->q_perm;
2321
Casey Schaufler676dac42010-12-02 06:43:39 -08002322 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002323 return 0;
2324}
2325
2326/**
2327 * smack_msg_free_security - Clear the security blob for msg
2328 * @msq: the object
2329 *
2330 * Clears the blob pointer
2331 */
2332static void smack_msg_queue_free_security(struct msg_queue *msq)
2333{
2334 struct kern_ipc_perm *kisp = &msq->q_perm;
2335
2336 kisp->security = NULL;
2337}
2338
2339/**
2340 * smack_of_msq - the smack pointer for the msq
2341 * @msq: the object
2342 *
2343 * Returns a pointer to the smack value
2344 */
2345static char *smack_of_msq(struct msg_queue *msq)
2346{
2347 return (char *)msq->q_perm.security;
2348}
2349
2350/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002351 * smk_curacc_msq : helper to check if current has access on msq
2352 * @msq : the msq
2353 * @access : access requested
2354 *
2355 * return 0 if current has access, error otherwise
2356 */
2357static int smk_curacc_msq(struct msg_queue *msq, int access)
2358{
2359 char *msp = smack_of_msq(msq);
2360 struct smk_audit_info ad;
2361
2362#ifdef CONFIG_AUDIT
2363 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2364 ad.a.u.ipc_id = msq->q_perm.id;
2365#endif
2366 return smk_curacc(msp, access, &ad);
2367}
2368
2369/**
Casey Schauflere114e472008-02-04 22:29:50 -08002370 * smack_msg_queue_associate - Smack access check for msg_queue
2371 * @msq: the object
2372 * @msqflg: access requested
2373 *
2374 * Returns 0 if current has the requested access, error code otherwise
2375 */
2376static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2377{
Casey Schauflere114e472008-02-04 22:29:50 -08002378 int may;
2379
2380 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002381 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002382}
2383
2384/**
2385 * smack_msg_queue_msgctl - Smack access check for msg_queue
2386 * @msq: the object
2387 * @cmd: what it wants to do
2388 *
2389 * Returns 0 if current has the requested access, error code otherwise
2390 */
2391static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2392{
Casey Schauflere114e472008-02-04 22:29:50 -08002393 int may;
2394
2395 switch (cmd) {
2396 case IPC_STAT:
2397 case MSG_STAT:
2398 may = MAY_READ;
2399 break;
2400 case IPC_SET:
2401 case IPC_RMID:
2402 may = MAY_READWRITE;
2403 break;
2404 case IPC_INFO:
2405 case MSG_INFO:
2406 /*
2407 * System level information
2408 */
2409 return 0;
2410 default:
2411 return -EINVAL;
2412 }
2413
Etienne Bassetecfcc532009-04-08 20:40:06 +02002414 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002415}
2416
2417/**
2418 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2419 * @msq: the object
2420 * @msg: unused
2421 * @msqflg: access requested
2422 *
2423 * Returns 0 if current has the requested access, error code otherwise
2424 */
2425static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2426 int msqflg)
2427{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002428 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002429
Etienne Bassetecfcc532009-04-08 20:40:06 +02002430 may = smack_flags_to_may(msqflg);
2431 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002432}
2433
2434/**
2435 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2436 * @msq: the object
2437 * @msg: unused
2438 * @target: unused
2439 * @type: unused
2440 * @mode: unused
2441 *
2442 * Returns 0 if current has read and write access, error code otherwise
2443 */
2444static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2445 struct task_struct *target, long type, int mode)
2446{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002447 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002448}
2449
2450/**
2451 * smack_ipc_permission - Smack access for ipc_permission()
2452 * @ipp: the object permissions
2453 * @flag: access requested
2454 *
2455 * Returns 0 if current has read and write access, error code otherwise
2456 */
2457static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2458{
2459 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002460 int may = smack_flags_to_may(flag);
2461 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002462
Etienne Bassetecfcc532009-04-08 20:40:06 +02002463#ifdef CONFIG_AUDIT
2464 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2465 ad.a.u.ipc_id = ipp->id;
2466#endif
2467 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002468}
2469
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002470/**
2471 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002472 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002473 * @secid: where result will be saved
2474 */
2475static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2476{
2477 char *smack = ipp->security;
2478
2479 *secid = smack_to_secid(smack);
2480}
2481
Casey Schauflere114e472008-02-04 22:29:50 -08002482/**
2483 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002484 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002485 * @inode: the object
2486 *
2487 * Set the inode's security blob if it hasn't been done already.
2488 */
2489static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2490{
2491 struct super_block *sbp;
2492 struct superblock_smack *sbsp;
2493 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002494 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002495 char *fetched;
2496 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002497 char trattr[TRANS_TRUE_SIZE];
2498 int transflag = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002499 struct dentry *dp;
2500
2501 if (inode == NULL)
2502 return;
2503
2504 isp = inode->i_security;
2505
2506 mutex_lock(&isp->smk_lock);
2507 /*
2508 * If the inode is already instantiated
2509 * take the quick way out
2510 */
2511 if (isp->smk_flags & SMK_INODE_INSTANT)
2512 goto unlockandout;
2513
2514 sbp = inode->i_sb;
2515 sbsp = sbp->s_security;
2516 /*
2517 * We're going to use the superblock default label
2518 * if there's no label on the file.
2519 */
2520 final = sbsp->smk_default;
2521
2522 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002523 * If this is the root inode the superblock
2524 * may be in the process of initialization.
2525 * If that is the case use the root value out
2526 * of the superblock.
2527 */
2528 if (opt_dentry->d_parent == opt_dentry) {
2529 isp->smk_inode = sbsp->smk_root;
2530 isp->smk_flags |= SMK_INODE_INSTANT;
2531 goto unlockandout;
2532 }
2533
2534 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002535 * This is pretty hackish.
2536 * Casey says that we shouldn't have to do
2537 * file system specific code, but it does help
2538 * with keeping it simple.
2539 */
2540 switch (sbp->s_magic) {
2541 case SMACK_MAGIC:
2542 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002543 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002544 * that the smack file system doesn't do
2545 * extended attributes.
2546 */
2547 final = smack_known_star.smk_known;
2548 break;
2549 case PIPEFS_MAGIC:
2550 /*
2551 * Casey says pipes are easy (?)
2552 */
2553 final = smack_known_star.smk_known;
2554 break;
2555 case DEVPTS_SUPER_MAGIC:
2556 /*
2557 * devpts seems content with the label of the task.
2558 * Programs that change smack have to treat the
2559 * pty with respect.
2560 */
2561 final = csp;
2562 break;
2563 case SOCKFS_MAGIC:
2564 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002565 * Socket access is controlled by the socket
2566 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002567 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002568 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002569 break;
2570 case PROC_SUPER_MAGIC:
2571 /*
2572 * Casey says procfs appears not to care.
2573 * The superblock default suffices.
2574 */
2575 break;
2576 case TMPFS_MAGIC:
2577 /*
2578 * Device labels should come from the filesystem,
2579 * but watch out, because they're volitile,
2580 * getting recreated on every reboot.
2581 */
2582 final = smack_known_star.smk_known;
2583 /*
2584 * No break.
2585 *
2586 * If a smack value has been set we want to use it,
2587 * but since tmpfs isn't giving us the opportunity
2588 * to set mount options simulate setting the
2589 * superblock default.
2590 */
2591 default:
2592 /*
2593 * This isn't an understood special case.
2594 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002595 */
2596
2597 /*
2598 * UNIX domain sockets use lower level socket data.
2599 */
2600 if (S_ISSOCK(inode->i_mode)) {
2601 final = smack_known_star.smk_known;
2602 break;
2603 }
2604 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002605 * No xattr support means, alas, no SMACK label.
2606 * Use the aforeapplied default.
2607 * It would be curious if the label of the task
2608 * does not match that assigned.
2609 */
2610 if (inode->i_op->getxattr == NULL)
2611 break;
2612 /*
2613 * Get the dentry for xattr.
2614 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002615 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002616 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002617 if (fetched != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -08002618 final = fetched;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002619 if (S_ISDIR(inode->i_mode)) {
2620 trattr[0] = '\0';
2621 inode->i_op->getxattr(dp,
2622 XATTR_NAME_SMACKTRANSMUTE,
2623 trattr, TRANS_TRUE_SIZE);
2624 if (strncmp(trattr, TRANS_TRUE,
2625 TRANS_TRUE_SIZE) == 0)
2626 transflag = SMK_INODE_TRANSMUTE;
2627 }
2628 }
2629 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002630 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002631
Casey Schauflere114e472008-02-04 22:29:50 -08002632 dput(dp);
2633 break;
2634 }
2635
2636 if (final == NULL)
2637 isp->smk_inode = csp;
2638 else
2639 isp->smk_inode = final;
2640
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002641 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002642
2643unlockandout:
2644 mutex_unlock(&isp->smk_lock);
2645 return;
2646}
2647
2648/**
2649 * smack_getprocattr - Smack process attribute access
2650 * @p: the object task
2651 * @name: the name of the attribute in /proc/.../attr
2652 * @value: where to put the result
2653 *
2654 * Places a copy of the task Smack into value
2655 *
2656 * Returns the length of the smack label or an error code
2657 */
2658static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2659{
2660 char *cp;
2661 int slen;
2662
2663 if (strcmp(name, "current") != 0)
2664 return -EINVAL;
2665
Casey Schaufler676dac42010-12-02 06:43:39 -08002666 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002667 if (cp == NULL)
2668 return -ENOMEM;
2669
2670 slen = strlen(cp);
2671 *value = cp;
2672 return slen;
2673}
2674
2675/**
2676 * smack_setprocattr - Smack process attribute setting
2677 * @p: the object task
2678 * @name: the name of the attribute in /proc/.../attr
2679 * @value: the value to set
2680 * @size: the size of the value
2681 *
2682 * Sets the Smack value of the task. Only setting self
2683 * is permitted and only with privilege
2684 *
2685 * Returns the length of the smack label or an error code
2686 */
2687static int smack_setprocattr(struct task_struct *p, char *name,
2688 void *value, size_t size)
2689{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002690 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002691 struct task_smack *tsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002692 struct task_smack *oldtsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002693 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002694 char *newsmack;
2695
Casey Schauflere114e472008-02-04 22:29:50 -08002696 /*
2697 * Changing another process' Smack value is too dangerous
2698 * and supports no sane use case.
2699 */
2700 if (p != current)
2701 return -EPERM;
2702
David Howells5cd9c582008-08-14 11:37:28 +01002703 if (!capable(CAP_MAC_ADMIN))
2704 return -EPERM;
2705
Casey Schauflere114e472008-02-04 22:29:50 -08002706 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2707 return -EINVAL;
2708
2709 if (strcmp(name, "current") != 0)
2710 return -EINVAL;
2711
2712 newsmack = smk_import(value, size);
2713 if (newsmack == NULL)
2714 return -EINVAL;
2715
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002716 /*
2717 * No process is ever allowed the web ("@") label.
2718 */
2719 if (newsmack == smack_known_web.smk_known)
2720 return -EPERM;
2721
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002722 oldtsp = p->cred->security;
David Howellsd84f4f92008-11-14 10:39:23 +11002723 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002724 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002725 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002726
2727 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08002728 if (tsp == NULL) {
2729 kfree(new);
2730 return -ENOMEM;
2731 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002732 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2733 if (rc != 0)
2734 return rc;
2735
Casey Schaufler676dac42010-12-02 06:43:39 -08002736 new->security = tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002737 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002738 return size;
2739}
2740
2741/**
2742 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002743 * @sock: one sock
2744 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002745 * @newsk: unused
2746 *
2747 * Return 0 if a subject with the smack of sock could access
2748 * an object with the smack of other, otherwise an error code
2749 */
David S. Miller3610cda2011-01-05 15:38:53 -08002750static int smack_unix_stream_connect(struct sock *sock,
2751 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002752{
James Morrisd2e7ad12011-01-10 09:46:24 +11002753 struct socket_smack *ssp = sock->sk_security;
2754 struct socket_smack *osp = other->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002755 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002756 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002757
Etienne Bassetecfcc532009-04-08 20:40:06 +02002758 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
David S. Miller3610cda2011-01-05 15:38:53 -08002759 smk_ad_setfield_u_net_sk(&ad, other);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002760
2761 if (!capable(CAP_MAC_OVERRIDE))
2762 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2763
2764 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002765}
2766
2767/**
2768 * smack_unix_may_send - Smack access on UDS
2769 * @sock: one socket
2770 * @other: the other socket
2771 *
2772 * Return 0 if a subject with the smack of sock could access
2773 * an object with the smack of other, otherwise an error code
2774 */
2775static int smack_unix_may_send(struct socket *sock, struct socket *other)
2776{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002777 struct socket_smack *ssp = sock->sk->sk_security;
2778 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002779 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002780 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002781
Etienne Bassetecfcc532009-04-08 20:40:06 +02002782 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2783 smk_ad_setfield_u_net_sk(&ad, other->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002784
2785 if (!capable(CAP_MAC_OVERRIDE))
2786 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2787
2788 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002789}
2790
2791/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002792 * smack_socket_sendmsg - Smack check based on destination host
2793 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002794 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002795 * @size: the size of the message
2796 *
2797 * Return 0 if the current subject can write to the destination
2798 * host. This is only a question if the destination is a single
2799 * label host.
2800 */
2801static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2802 int size)
2803{
2804 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002805
2806 /*
2807 * Perfectly reasonable for this to be NULL
2808 */
Julia Lawallda34d422009-08-05 14:34:55 +02002809 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002810 return 0;
2811
Paul Moore07feee82009-03-27 17:10:54 -04002812 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002813}
2814
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002815/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002816 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002817 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002818 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08002819 *
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002820 * Returns a pointer to a Smack label found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08002821 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002822static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2823 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08002824{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002825 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002826 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002827 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002828 int pcat;
2829
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002830 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002831 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002832 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002833 * If there are flags but no level netlabel isn't
2834 * behaving the way we expect it to.
2835 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002836 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002837 * Without guidance regarding the smack value
2838 * for the packet fall back on the network
2839 * ambient value.
2840 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002841 memset(smack, '\0', SMK_LABELLEN);
2842 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2843 for (pcat = -1;;) {
2844 pcat = netlbl_secattr_catmap_walk(
2845 sap->attr.mls.cat, pcat + 1);
2846 if (pcat < 0)
2847 break;
2848 smack_catset_bit(pcat, smack);
2849 }
2850 /*
2851 * If it is CIPSO using smack direct mapping
2852 * we are already done. WeeHee.
2853 */
2854 if (sap->attr.mls.lvl == smack_cipso_direct) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002855 /*
2856 * The label sent is usually on the label list.
2857 *
2858 * If it is not we may still want to allow the
2859 * delivery.
2860 *
2861 * If the recipient is accepting all packets
2862 * because it is using the star ("*") label
2863 * for SMACK64IPIN provide the web ("@") label
2864 * so that a directed response will succeed.
2865 * This is not very correct from a MAC point
2866 * of view, but gets around the problem that
2867 * locking prevents adding the newly discovered
2868 * label to the list.
2869 * The case where the recipient is not using
2870 * the star label should obviously fail.
2871 * The easy way to do this is to provide the
2872 * star label as the subject label.
2873 */
2874 skp = smk_find_entry(smack);
2875 if (skp != NULL)
2876 return skp->smk_known;
2877 if (ssp != NULL &&
2878 ssp->smk_in == smack_known_star.smk_known)
2879 return smack_known_web.smk_known;
2880 return smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002881 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002882 /*
2883 * Look it up in the supplied table if it is not
2884 * a direct mapping.
2885 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002886 sp = smack_from_cipso(sap->attr.mls.lvl, smack);
2887 if (sp != NULL)
2888 return sp;
2889 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2890 return smack_known_web.smk_known;
2891 return smack_known_star.smk_known;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002892 }
2893 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2894 /*
2895 * Looks like a fallback, which gives us a secid.
2896 */
2897 sp = smack_from_secid(sap->attr.secid);
2898 /*
2899 * This has got to be a bug because it is
2900 * impossible to specify a fallback without
2901 * specifying the label, which will ensure
2902 * it has a secid, and the only way to get a
2903 * secid is from a fallback.
2904 */
2905 BUG_ON(sp == NULL);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002906 return sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002907 }
2908 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002909 * Without guidance regarding the smack value
2910 * for the packet fall back on the network
2911 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002912 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002913 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08002914}
2915
2916/**
2917 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2918 * @sk: socket
2919 * @skb: packet
2920 *
2921 * Returns 0 if the packet should be delivered, an error code otherwise
2922 */
2923static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2924{
2925 struct netlbl_lsm_secattr secattr;
2926 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002927 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002928 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002929 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002930 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2931 return 0;
2932
2933 /*
2934 * Translate what netlabel gave us.
2935 */
Casey Schauflere114e472008-02-04 22:29:50 -08002936 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002937
Casey Schauflere114e472008-02-04 22:29:50 -08002938 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002939 if (rc == 0)
2940 csp = smack_from_secattr(&secattr, ssp);
2941 else
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002942 csp = smack_net_ambient;
2943
Casey Schauflere114e472008-02-04 22:29:50 -08002944 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002945
Etienne Bassetecfcc532009-04-08 20:40:06 +02002946#ifdef CONFIG_AUDIT
2947 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2948 ad.a.u.net.family = sk->sk_family;
Eric Dumazet8964be42009-11-20 15:35:04 -08002949 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002950 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2951#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002952 /*
2953 * Receiving a packet requires that the other end
2954 * be able to write here. Read access is not required.
2955 * This is the simplist possible security model
2956 * for networking.
2957 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002958 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002959 if (rc != 0)
2960 netlbl_skbuff_err(skb, rc, 0);
2961 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002962}
2963
2964/**
2965 * smack_socket_getpeersec_stream - pull in packet label
2966 * @sock: the socket
2967 * @optval: user's destination
2968 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002969 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002970 *
2971 * returns zero on success, an error code otherwise
2972 */
2973static int smack_socket_getpeersec_stream(struct socket *sock,
2974 char __user *optval,
2975 int __user *optlen, unsigned len)
2976{
2977 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002978 char *rcp = "";
2979 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08002980 int rc = 0;
2981
2982 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002983 if (ssp->smk_packet != NULL) {
2984 rcp = ssp->smk_packet;
2985 slen = strlen(rcp) + 1;
2986 }
Casey Schauflere114e472008-02-04 22:29:50 -08002987
2988 if (slen > len)
2989 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002990 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08002991 rc = -EFAULT;
2992
2993 if (put_user(slen, optlen) != 0)
2994 rc = -EFAULT;
2995
2996 return rc;
2997}
2998
2999
3000/**
3001 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003002 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003003 * @skb: packet data
3004 * @secid: pointer to where to put the secid of the packet
3005 *
3006 * Sets the netlabel socket state on sk from parent
3007 */
3008static int smack_socket_getpeersec_dgram(struct socket *sock,
3009 struct sk_buff *skb, u32 *secid)
3010
3011{
3012 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003013 struct socket_smack *ssp = NULL;
3014 char *sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003015 int family = PF_UNSPEC;
3016 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003017 int rc;
3018
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003019 if (skb != NULL) {
3020 if (skb->protocol == htons(ETH_P_IP))
3021 family = PF_INET;
3022 else if (skb->protocol == htons(ETH_P_IPV6))
3023 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003024 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003025 if (family == PF_UNSPEC && sock != NULL)
3026 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003027
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003028 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003029 ssp = sock->sk->sk_security;
3030 s = smack_to_secid(ssp->smk_out);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003031 } else if (family == PF_INET || family == PF_INET6) {
3032 /*
3033 * Translate what netlabel gave us.
3034 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003035 if (sock != NULL && sock->sk != NULL)
3036 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003037 netlbl_secattr_init(&secattr);
3038 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3039 if (rc == 0) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003040 sp = smack_from_secattr(&secattr, ssp);
3041 s = smack_to_secid(sp);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003042 }
3043 netlbl_secattr_destroy(&secattr);
3044 }
3045 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003046 if (s == 0)
3047 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003048 return 0;
3049}
3050
3051/**
Paul Moore07feee82009-03-27 17:10:54 -04003052 * smack_sock_graft - Initialize a newly created socket with an existing sock
3053 * @sk: child sock
3054 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003055 *
Paul Moore07feee82009-03-27 17:10:54 -04003056 * Set the smk_{in,out} state of an existing sock based on the process that
3057 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003058 */
3059static void smack_sock_graft(struct sock *sk, struct socket *parent)
3060{
3061 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08003062
Paul Moore07feee82009-03-27 17:10:54 -04003063 if (sk == NULL ||
3064 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003065 return;
3066
3067 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08003068 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04003069 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003070}
3071
3072/**
3073 * smack_inet_conn_request - Smack access check on connect
3074 * @sk: socket involved
3075 * @skb: packet
3076 * @req: unused
3077 *
3078 * Returns 0 if a task with the packet label could write to
3079 * the socket, otherwise an error code
3080 */
3081static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3082 struct request_sock *req)
3083{
Paul Moore07feee82009-03-27 17:10:54 -04003084 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003085 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003086 struct netlbl_lsm_secattr secattr;
3087 struct sockaddr_in addr;
3088 struct iphdr *hdr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003089 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003090 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003091 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003092
Paul Moore07feee82009-03-27 17:10:54 -04003093 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3094 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3095 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08003096
Paul Moore07feee82009-03-27 17:10:54 -04003097 netlbl_secattr_init(&secattr);
3098 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003099 if (rc == 0)
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003100 sp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003101 else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003102 sp = smack_known_huh.smk_known;
Paul Moore07feee82009-03-27 17:10:54 -04003103 netlbl_secattr_destroy(&secattr);
3104
Etienne Bassetecfcc532009-04-08 20:40:06 +02003105#ifdef CONFIG_AUDIT
3106 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3107 ad.a.u.net.family = family;
Eric Dumazet8964be42009-11-20 15:35:04 -08003108 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003109 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3110#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003111 /*
Paul Moore07feee82009-03-27 17:10:54 -04003112 * Receiving a packet requires that the other end be able to write
3113 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003114 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003115 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003116 if (rc != 0)
3117 return rc;
3118
3119 /*
3120 * Save the peer's label in the request_sock so we can later setup
3121 * smk_packet in the child socket so that SO_PEERCRED can report it.
3122 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003123 req->peer_secid = smack_to_secid(sp);
Paul Moore07feee82009-03-27 17:10:54 -04003124
3125 /*
3126 * We need to decide if we want to label the incoming connection here
3127 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003128 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003129 */
3130 hdr = ip_hdr(skb);
3131 addr.sin_addr.s_addr = hdr->saddr;
3132 rcu_read_lock();
3133 if (smack_host_label(&addr) == NULL) {
3134 rcu_read_unlock();
3135 netlbl_secattr_init(&secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003136 smack_to_secattr(sp, &secattr);
Paul Moore07feee82009-03-27 17:10:54 -04003137 rc = netlbl_req_setattr(req, &secattr);
3138 netlbl_secattr_destroy(&secattr);
3139 } else {
3140 rcu_read_unlock();
3141 netlbl_req_delattr(req);
3142 }
Casey Schauflere114e472008-02-04 22:29:50 -08003143
3144 return rc;
3145}
3146
Paul Moore07feee82009-03-27 17:10:54 -04003147/**
3148 * smack_inet_csk_clone - Copy the connection information to the new socket
3149 * @sk: the new socket
3150 * @req: the connection's request_sock
3151 *
3152 * Transfer the connection's peer label to the newly created socket.
3153 */
3154static void smack_inet_csk_clone(struct sock *sk,
3155 const struct request_sock *req)
3156{
3157 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003158
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003159 if (req->peer_secid != 0)
3160 ssp->smk_packet = smack_from_secid(req->peer_secid);
3161 else
3162 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003163}
3164
Casey Schauflere114e472008-02-04 22:29:50 -08003165/*
3166 * Key management security hooks
3167 *
3168 * Casey has not tested key support very heavily.
3169 * The permission check is most likely too restrictive.
3170 * If you care about keys please have a look.
3171 */
3172#ifdef CONFIG_KEYS
3173
3174/**
3175 * smack_key_alloc - Set the key security blob
3176 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003177 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003178 * @flags: unused
3179 *
3180 * No allocation required
3181 *
3182 * Returns 0
3183 */
David Howellsd84f4f92008-11-14 10:39:23 +11003184static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003185 unsigned long flags)
3186{
Casey Schaufler676dac42010-12-02 06:43:39 -08003187 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003188 return 0;
3189}
3190
3191/**
3192 * smack_key_free - Clear the key security blob
3193 * @key: the object
3194 *
3195 * Clear the blob pointer
3196 */
3197static void smack_key_free(struct key *key)
3198{
3199 key->security = NULL;
3200}
3201
3202/*
3203 * smack_key_permission - Smack access on a key
3204 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003205 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003206 * @perm: unused
3207 *
3208 * Return 0 if the task has read and write to the object,
3209 * an error code otherwise
3210 */
3211static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003212 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003213{
3214 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003215 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08003216 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003217
3218 keyp = key_ref_to_ptr(key_ref);
3219 if (keyp == NULL)
3220 return -EINVAL;
3221 /*
3222 * If the key hasn't been initialized give it access so that
3223 * it may do so.
3224 */
3225 if (keyp->security == NULL)
3226 return 0;
3227 /*
3228 * This should not occur
3229 */
Casey Schaufler676dac42010-12-02 06:43:39 -08003230 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003231 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003232#ifdef CONFIG_AUDIT
3233 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3234 ad.a.u.key_struct.key = keyp->serial;
3235 ad.a.u.key_struct.key_desc = keyp->description;
3236#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08003237 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02003238 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003239}
3240#endif /* CONFIG_KEYS */
3241
3242/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003243 * Smack Audit hooks
3244 *
3245 * Audit requires a unique representation of each Smack specific
3246 * rule. This unique representation is used to distinguish the
3247 * object to be audited from remaining kernel objects and also
3248 * works as a glue between the audit hooks.
3249 *
3250 * Since repository entries are added but never deleted, we'll use
3251 * the smack_known label address related to the given audit rule as
3252 * the needed unique representation. This also better fits the smack
3253 * model where nearly everything is a label.
3254 */
3255#ifdef CONFIG_AUDIT
3256
3257/**
3258 * smack_audit_rule_init - Initialize a smack audit rule
3259 * @field: audit rule fields given from user-space (audit.h)
3260 * @op: required testing operator (=, !=, >, <, ...)
3261 * @rulestr: smack label to be audited
3262 * @vrule: pointer to save our own audit rule representation
3263 *
3264 * Prepare to audit cases where (@field @op @rulestr) is true.
3265 * The label to be audited is created if necessay.
3266 */
3267static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3268{
3269 char **rule = (char **)vrule;
3270 *rule = NULL;
3271
3272 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3273 return -EINVAL;
3274
Al Viro5af75d82008-12-16 05:59:26 -05003275 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003276 return -EINVAL;
3277
3278 *rule = smk_import(rulestr, 0);
3279
3280 return 0;
3281}
3282
3283/**
3284 * smack_audit_rule_known - Distinguish Smack audit rules
3285 * @krule: rule of interest, in Audit kernel representation format
3286 *
3287 * This is used to filter Smack rules from remaining Audit ones.
3288 * If it's proved that this rule belongs to us, the
3289 * audit_rule_match hook will be called to do the final judgement.
3290 */
3291static int smack_audit_rule_known(struct audit_krule *krule)
3292{
3293 struct audit_field *f;
3294 int i;
3295
3296 for (i = 0; i < krule->field_count; i++) {
3297 f = &krule->fields[i];
3298
3299 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3300 return 1;
3301 }
3302
3303 return 0;
3304}
3305
3306/**
3307 * smack_audit_rule_match - Audit given object ?
3308 * @secid: security id for identifying the object to test
3309 * @field: audit rule flags given from user-space
3310 * @op: required testing operator
3311 * @vrule: smack internal rule presentation
3312 * @actx: audit context associated with the check
3313 *
3314 * The core Audit hook. It's used to take the decision of
3315 * whether to audit or not to audit a given object.
3316 */
3317static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3318 struct audit_context *actx)
3319{
3320 char *smack;
3321 char *rule = vrule;
3322
3323 if (!rule) {
3324 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3325 "Smack: missing rule\n");
3326 return -ENOENT;
3327 }
3328
3329 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3330 return 0;
3331
3332 smack = smack_from_secid(secid);
3333
3334 /*
3335 * No need to do string comparisons. If a match occurs,
3336 * both pointers will point to the same smack_known
3337 * label.
3338 */
Al Viro5af75d82008-12-16 05:59:26 -05003339 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003340 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003341 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003342 return (rule != smack);
3343
3344 return 0;
3345}
3346
3347/**
3348 * smack_audit_rule_free - free smack rule representation
3349 * @vrule: rule to be freed.
3350 *
3351 * No memory was allocated.
3352 */
3353static void smack_audit_rule_free(void *vrule)
3354{
3355 /* No-op */
3356}
3357
3358#endif /* CONFIG_AUDIT */
3359
Randy Dunlap251a2a92009-02-18 11:42:33 -08003360/**
Casey Schauflere114e472008-02-04 22:29:50 -08003361 * smack_secid_to_secctx - return the smack label for a secid
3362 * @secid: incoming integer
3363 * @secdata: destination
3364 * @seclen: how long it is
3365 *
3366 * Exists for networking code.
3367 */
3368static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3369{
3370 char *sp = smack_from_secid(secid);
3371
Eric Parisd5630b92010-10-13 16:24:48 -04003372 if (secdata)
3373 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003374 *seclen = strlen(sp);
3375 return 0;
3376}
3377
Randy Dunlap251a2a92009-02-18 11:42:33 -08003378/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003379 * smack_secctx_to_secid - return the secid for a smack label
3380 * @secdata: smack label
3381 * @seclen: how long result is
3382 * @secid: outgoing integer
3383 *
3384 * Exists for audit and networking code.
3385 */
David Howellse52c17642008-04-29 20:52:51 +01003386static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003387{
3388 *secid = smack_to_secid(secdata);
3389 return 0;
3390}
3391
Randy Dunlap251a2a92009-02-18 11:42:33 -08003392/**
Casey Schauflere114e472008-02-04 22:29:50 -08003393 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003394 * @secdata: unused
3395 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003396 *
3397 * Exists to make sure nothing gets done, and properly
3398 */
3399static void smack_release_secctx(char *secdata, u32 seclen)
3400{
3401}
3402
David P. Quigley1ee65e32009-09-03 14:25:57 -04003403static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3404{
3405 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3406}
3407
3408static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3409{
3410 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3411}
3412
3413static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3414{
3415 int len = 0;
3416 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3417
3418 if (len < 0)
3419 return len;
3420 *ctxlen = len;
3421 return 0;
3422}
3423
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003424struct security_operations smack_ops = {
3425 .name = "smack",
3426
Ingo Molnar9e488582009-05-07 19:26:19 +10003427 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003428 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003429 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003430
3431 .sb_alloc_security = smack_sb_alloc_security,
3432 .sb_free_security = smack_sb_free_security,
3433 .sb_copy_data = smack_sb_copy_data,
3434 .sb_kern_mount = smack_sb_kern_mount,
3435 .sb_statfs = smack_sb_statfs,
3436 .sb_mount = smack_sb_mount,
3437 .sb_umount = smack_sb_umount,
3438
Casey Schaufler676dac42010-12-02 06:43:39 -08003439 .bprm_set_creds = smack_bprm_set_creds,
3440
Casey Schauflere114e472008-02-04 22:29:50 -08003441 .inode_alloc_security = smack_inode_alloc_security,
3442 .inode_free_security = smack_inode_free_security,
3443 .inode_init_security = smack_inode_init_security,
3444 .inode_link = smack_inode_link,
3445 .inode_unlink = smack_inode_unlink,
3446 .inode_rmdir = smack_inode_rmdir,
3447 .inode_rename = smack_inode_rename,
3448 .inode_permission = smack_inode_permission,
3449 .inode_setattr = smack_inode_setattr,
3450 .inode_getattr = smack_inode_getattr,
3451 .inode_setxattr = smack_inode_setxattr,
3452 .inode_post_setxattr = smack_inode_post_setxattr,
3453 .inode_getxattr = smack_inode_getxattr,
3454 .inode_removexattr = smack_inode_removexattr,
3455 .inode_getsecurity = smack_inode_getsecurity,
3456 .inode_setsecurity = smack_inode_setsecurity,
3457 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003458 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003459
3460 .file_permission = smack_file_permission,
3461 .file_alloc_security = smack_file_alloc_security,
3462 .file_free_security = smack_file_free_security,
3463 .file_ioctl = smack_file_ioctl,
3464 .file_lock = smack_file_lock,
3465 .file_fcntl = smack_file_fcntl,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003466 .file_mmap = smack_file_mmap,
Casey Schauflere114e472008-02-04 22:29:50 -08003467 .file_set_fowner = smack_file_set_fowner,
3468 .file_send_sigiotask = smack_file_send_sigiotask,
3469 .file_receive = smack_file_receive,
3470
David Howellsee18d642009-09-02 09:14:21 +01003471 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003472 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003473 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003474 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003475 .kernel_act_as = smack_kernel_act_as,
3476 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003477 .task_setpgid = smack_task_setpgid,
3478 .task_getpgid = smack_task_getpgid,
3479 .task_getsid = smack_task_getsid,
3480 .task_getsecid = smack_task_getsecid,
3481 .task_setnice = smack_task_setnice,
3482 .task_setioprio = smack_task_setioprio,
3483 .task_getioprio = smack_task_getioprio,
3484 .task_setscheduler = smack_task_setscheduler,
3485 .task_getscheduler = smack_task_getscheduler,
3486 .task_movememory = smack_task_movememory,
3487 .task_kill = smack_task_kill,
3488 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003489 .task_to_inode = smack_task_to_inode,
3490
3491 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003492 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003493
3494 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3495 .msg_msg_free_security = smack_msg_msg_free_security,
3496
3497 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3498 .msg_queue_free_security = smack_msg_queue_free_security,
3499 .msg_queue_associate = smack_msg_queue_associate,
3500 .msg_queue_msgctl = smack_msg_queue_msgctl,
3501 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3502 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3503
3504 .shm_alloc_security = smack_shm_alloc_security,
3505 .shm_free_security = smack_shm_free_security,
3506 .shm_associate = smack_shm_associate,
3507 .shm_shmctl = smack_shm_shmctl,
3508 .shm_shmat = smack_shm_shmat,
3509
3510 .sem_alloc_security = smack_sem_alloc_security,
3511 .sem_free_security = smack_sem_free_security,
3512 .sem_associate = smack_sem_associate,
3513 .sem_semctl = smack_sem_semctl,
3514 .sem_semop = smack_sem_semop,
3515
Casey Schauflere114e472008-02-04 22:29:50 -08003516 .d_instantiate = smack_d_instantiate,
3517
3518 .getprocattr = smack_getprocattr,
3519 .setprocattr = smack_setprocattr,
3520
3521 .unix_stream_connect = smack_unix_stream_connect,
3522 .unix_may_send = smack_unix_may_send,
3523
3524 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003525 .socket_connect = smack_socket_connect,
3526 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003527 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3528 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3529 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3530 .sk_alloc_security = smack_sk_alloc_security,
3531 .sk_free_security = smack_sk_free_security,
3532 .sock_graft = smack_sock_graft,
3533 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003534 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003535
Casey Schauflere114e472008-02-04 22:29:50 -08003536 /* key management security hooks */
3537#ifdef CONFIG_KEYS
3538 .key_alloc = smack_key_alloc,
3539 .key_free = smack_key_free,
3540 .key_permission = smack_key_permission,
3541#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003542
3543 /* Audit hooks */
3544#ifdef CONFIG_AUDIT
3545 .audit_rule_init = smack_audit_rule_init,
3546 .audit_rule_known = smack_audit_rule_known,
3547 .audit_rule_match = smack_audit_rule_match,
3548 .audit_rule_free = smack_audit_rule_free,
3549#endif /* CONFIG_AUDIT */
3550
Casey Schauflere114e472008-02-04 22:29:50 -08003551 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003552 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003553 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003554 .inode_notifysecctx = smack_inode_notifysecctx,
3555 .inode_setsecctx = smack_inode_setsecctx,
3556 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003557};
3558
Etienne Basset7198e2e2009-03-24 20:53:24 +01003559
3560static __init void init_smack_know_list(void)
3561{
3562 list_add(&smack_known_huh.list, &smack_known_list);
3563 list_add(&smack_known_hat.list, &smack_known_list);
3564 list_add(&smack_known_star.list, &smack_known_list);
3565 list_add(&smack_known_floor.list, &smack_known_list);
3566 list_add(&smack_known_invalid.list, &smack_known_list);
3567 list_add(&smack_known_web.list, &smack_known_list);
3568}
3569
Casey Schauflere114e472008-02-04 22:29:50 -08003570/**
3571 * smack_init - initialize the smack system
3572 *
3573 * Returns 0
3574 */
3575static __init int smack_init(void)
3576{
David Howellsd84f4f92008-11-14 10:39:23 +11003577 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003578 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003579
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003580 if (!security_module_enable(&smack_ops))
3581 return 0;
3582
3583 tsp = new_task_smack(smack_known_floor.smk_known,
3584 smack_known_floor.smk_known, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003585 if (tsp == NULL)
3586 return -ENOMEM;
3587
Casey Schauflere114e472008-02-04 22:29:50 -08003588 printk(KERN_INFO "Smack: Initializing.\n");
3589
3590 /*
3591 * Set the security state for the initial task.
3592 */
David Howellsd84f4f92008-11-14 10:39:23 +11003593 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003594 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003595
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003596 /* initialize the smack_know_list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003597 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003598 /*
3599 * Initialize locks
3600 */
Casey Schauflere114e472008-02-04 22:29:50 -08003601 spin_lock_init(&smack_known_huh.smk_cipsolock);
3602 spin_lock_init(&smack_known_hat.smk_cipsolock);
3603 spin_lock_init(&smack_known_star.smk_cipsolock);
3604 spin_lock_init(&smack_known_floor.smk_cipsolock);
3605 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3606
3607 /*
3608 * Register with LSM
3609 */
3610 if (register_security(&smack_ops))
3611 panic("smack: Unable to register with kernel.\n");
3612
3613 return 0;
3614}
3615
3616/*
3617 * Smack requires early initialization in order to label
3618 * all processes and objects when they are created.
3619 */
3620security_initcall(smack_init);