blob: 2e71c3f445fcfb226a96bb02bd5a1f47acc73387 [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 Black1fd73172009-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 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001094 * Generally these operations are harmless.
1095 * File locking operations present an obvious mechanism
1096 * for passing information, so they require write access.
1097 *
Casey Schauflere114e472008-02-04 22:29:50 -08001098 * Returns 0 if current has access, error code otherwise
1099 */
1100static int smack_file_fcntl(struct file *file, unsigned int cmd,
1101 unsigned long arg)
1102{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001104 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001105
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106
Casey Schauflere114e472008-02-04 22:29:50 -08001107 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001108 case F_GETLK:
Casey Schauflere114e472008-02-04 22:29:50 -08001109 case F_SETLK:
1110 case F_SETLKW:
1111 case F_SETOWN:
1112 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001113 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1114 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001115 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001116 break;
1117 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001118 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001119 }
1120
1121 return rc;
1122}
1123
1124/**
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001125 * smack_file_mmap :
1126 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1127 * if mapping anonymous memory.
1128 * @file contains the file structure for file to map (may be NULL).
1129 * @reqprot contains the protection requested by the application.
1130 * @prot contains the protection that will be applied by the kernel.
1131 * @flags contains the operational flags.
1132 * Return 0 if permission is granted.
1133 */
1134static int smack_file_mmap(struct file *file,
1135 unsigned long reqprot, unsigned long prot,
1136 unsigned long flags, unsigned long addr,
1137 unsigned long addr_only)
1138{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001139 struct smack_known *skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001140 struct smack_rule *srp;
1141 struct task_smack *tsp;
1142 char *sp;
1143 char *msmack;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001144 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001145 struct inode_smack *isp;
1146 struct dentry *dp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001147 int may;
1148 int mmay;
1149 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001150 int rc;
1151
1152 /* do DAC check on address space usage */
1153 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1154 if (rc || addr_only)
1155 return rc;
1156
1157 if (file == NULL || file->f_dentry == NULL)
1158 return 0;
1159
1160 dp = file->f_dentry;
1161
1162 if (dp->d_inode == NULL)
1163 return 0;
1164
1165 isp = dp->d_inode->i_security;
1166 if (isp->smk_mmap == NULL)
1167 return 0;
1168 msmack = isp->smk_mmap;
1169
1170 tsp = current_security();
1171 sp = smk_of_current();
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001172 skp = smk_find_entry(sp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001173 rc = 0;
1174
1175 rcu_read_lock();
1176 /*
1177 * For each Smack rule associated with the subject
1178 * label verify that the SMACK64MMAP also has access
1179 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001180 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001181 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001182 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001183 /*
1184 * Matching labels always allows access.
1185 */
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001186 if (msmack == osmack)
1187 continue;
1188 /*
1189 * If there is a matching local rule take
1190 * that into account as well.
1191 */
1192 may = smk_access_entry(srp->smk_subject, osmack,
1193 &tsp->smk_rules);
1194 if (may == -ENOENT)
1195 may = srp->smk_access;
1196 else
1197 may &= srp->smk_access;
1198 /*
1199 * If may is zero the SMACK64MMAP subject can't
1200 * possibly have less access.
1201 */
1202 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001203 continue;
1204
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001205 /*
1206 * Fetch the global list entry.
1207 * If there isn't one a SMACK64MMAP subject
1208 * can't have as much access as current.
1209 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001210 skp = smk_find_entry(msmack);
1211 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001212 if (mmay == -ENOENT) {
1213 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001214 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001215 }
1216 /*
1217 * If there is a local entry it modifies the
1218 * potential access, too.
1219 */
1220 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1221 if (tmay != -ENOENT)
1222 mmay &= tmay;
1223
1224 /*
1225 * If there is any access available to current that is
1226 * not available to a SMACK64MMAP subject
1227 * deny access.
1228 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001229 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001230 rc = -EACCES;
1231 break;
1232 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001233 }
1234
1235 rcu_read_unlock();
1236
1237 return rc;
1238}
1239
1240/**
Casey Schauflere114e472008-02-04 22:29:50 -08001241 * smack_file_set_fowner - set the file security blob value
1242 * @file: object in question
1243 *
1244 * Returns 0
1245 * Further research may be required on this one.
1246 */
1247static int smack_file_set_fowner(struct file *file)
1248{
Casey Schaufler676dac42010-12-02 06:43:39 -08001249 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001250 return 0;
1251}
1252
1253/**
1254 * smack_file_send_sigiotask - Smack on sigio
1255 * @tsk: The target task
1256 * @fown: the object the signal come from
1257 * @signum: unused
1258 *
1259 * Allow a privileged task to get signals even if it shouldn't
1260 *
1261 * Returns 0 if a subject with the object's smack could
1262 * write to the task, an error code otherwise.
1263 */
1264static int smack_file_send_sigiotask(struct task_struct *tsk,
1265 struct fown_struct *fown, int signum)
1266{
1267 struct file *file;
1268 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001269 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001270 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001271
1272 /*
1273 * struct fown_struct is never outside the context of a struct file
1274 */
1275 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001276
Etienne Bassetecfcc532009-04-08 20:40:06 +02001277 /* we don't log here as rc can be overriden */
1278 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001279 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001280 rc = 0;
1281
1282 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1283 smk_ad_setfield_u_tsk(&ad, tsk);
1284 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001285 return rc;
1286}
1287
1288/**
1289 * smack_file_receive - Smack file receive check
1290 * @file: the object
1291 *
1292 * Returns 0 if current has access, error code otherwise
1293 */
1294static int smack_file_receive(struct file *file)
1295{
1296 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001297 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001298
Etienne Bassetecfcc532009-04-08 20:40:06 +02001299 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1300 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001301 /*
1302 * This code relies on bitmasks.
1303 */
1304 if (file->f_mode & FMODE_READ)
1305 may = MAY_READ;
1306 if (file->f_mode & FMODE_WRITE)
1307 may |= MAY_WRITE;
1308
Etienne Bassetecfcc532009-04-08 20:40:06 +02001309 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001310}
1311
Casey Schaufler531f1d42011-09-19 12:41:42 -07001312/**
1313 * smack_dentry_open - Smack dentry open processing
1314 * @file: the object
1315 * @cred: unused
1316 *
1317 * Set the security blob in the file structure.
1318 *
1319 * Returns 0
1320 */
1321static int smack_dentry_open(struct file *file, const struct cred *cred)
1322{
1323 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1324
1325 file->f_security = isp->smk_inode;
1326
1327 return 0;
1328}
1329
Casey Schauflere114e472008-02-04 22:29:50 -08001330/*
1331 * Task hooks
1332 */
1333
1334/**
David Howellsee18d642009-09-02 09:14:21 +01001335 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1336 * @new: the new credentials
1337 * @gfp: the atomicity of any memory allocations
1338 *
1339 * Prepare a blank set of credentials for modification. This must allocate all
1340 * the memory the LSM module might require such that cred_transfer() can
1341 * complete without error.
1342 */
1343static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1344{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001345 struct task_smack *tsp;
1346
1347 tsp = new_task_smack(NULL, NULL, gfp);
1348 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001349 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001350
1351 cred->security = tsp;
1352
David Howellsee18d642009-09-02 09:14:21 +01001353 return 0;
1354}
1355
1356
1357/**
David Howellsf1752ee2008-11-14 10:39:17 +11001358 * smack_cred_free - "free" task-level security credentials
1359 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001360 *
Casey Schauflere114e472008-02-04 22:29:50 -08001361 */
David Howellsf1752ee2008-11-14 10:39:17 +11001362static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001363{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001364 struct task_smack *tsp = cred->security;
1365 struct smack_rule *rp;
1366 struct list_head *l;
1367 struct list_head *n;
1368
1369 if (tsp == NULL)
1370 return;
1371 cred->security = NULL;
1372
1373 list_for_each_safe(l, n, &tsp->smk_rules) {
1374 rp = list_entry(l, struct smack_rule, list);
1375 list_del(&rp->list);
1376 kfree(rp);
1377 }
1378 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001379}
1380
1381/**
David Howellsd84f4f92008-11-14 10:39:23 +11001382 * smack_cred_prepare - prepare new set of credentials for modification
1383 * @new: the new credentials
1384 * @old: the original credentials
1385 * @gfp: the atomicity of any memory allocations
1386 *
1387 * Prepare a new set of credentials for modification.
1388 */
1389static int smack_cred_prepare(struct cred *new, const struct cred *old,
1390 gfp_t gfp)
1391{
Casey Schaufler676dac42010-12-02 06:43:39 -08001392 struct task_smack *old_tsp = old->security;
1393 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001394 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001395
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001396 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001397 if (new_tsp == NULL)
1398 return -ENOMEM;
1399
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001400 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1401 if (rc != 0)
1402 return rc;
1403
Casey Schaufler676dac42010-12-02 06:43:39 -08001404 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001405 return 0;
1406}
1407
Randy Dunlap251a2a92009-02-18 11:42:33 -08001408/**
David Howellsee18d642009-09-02 09:14:21 +01001409 * smack_cred_transfer - Transfer the old credentials to the new credentials
1410 * @new: the new credentials
1411 * @old: the original credentials
1412 *
1413 * Fill in a set of blank credentials from another set of credentials.
1414 */
1415static void smack_cred_transfer(struct cred *new, const struct cred *old)
1416{
Casey Schaufler676dac42010-12-02 06:43:39 -08001417 struct task_smack *old_tsp = old->security;
1418 struct task_smack *new_tsp = new->security;
1419
1420 new_tsp->smk_task = old_tsp->smk_task;
1421 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001422 mutex_init(&new_tsp->smk_rules_lock);
1423 INIT_LIST_HEAD(&new_tsp->smk_rules);
1424
1425
1426 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001427}
1428
1429/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001430 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001431 * @new: points to the set of credentials to be modified.
1432 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001433 *
1434 * Set the security data for a kernel service.
1435 */
1436static int smack_kernel_act_as(struct cred *new, u32 secid)
1437{
Casey Schaufler676dac42010-12-02 06:43:39 -08001438 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001439 char *smack = smack_from_secid(secid);
1440
1441 if (smack == NULL)
1442 return -EINVAL;
1443
Casey Schaufler676dac42010-12-02 06:43:39 -08001444 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001445 return 0;
1446}
1447
1448/**
1449 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001450 * @new: points to the set of credentials to be modified
1451 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001452 *
1453 * Set the file creation context in a set of credentials to the same
1454 * as the objective context of the specified inode
1455 */
1456static int smack_kernel_create_files_as(struct cred *new,
1457 struct inode *inode)
1458{
1459 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001460 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001461
Casey Schaufler676dac42010-12-02 06:43:39 -08001462 tsp->smk_forked = isp->smk_inode;
1463 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001464 return 0;
1465}
1466
1467/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001468 * smk_curacc_on_task - helper to log task related access
1469 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001470 * @access: the access requested
1471 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001472 *
1473 * Return 0 if access is permitted
1474 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001475static int smk_curacc_on_task(struct task_struct *p, int access,
1476 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001477{
1478 struct smk_audit_info ad;
1479
Casey Schaufler531f1d42011-09-19 12:41:42 -07001480 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001481 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001482 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001483}
1484
1485/**
Casey Schauflere114e472008-02-04 22:29:50 -08001486 * smack_task_setpgid - Smack check on setting pgid
1487 * @p: the task object
1488 * @pgid: unused
1489 *
1490 * Return 0 if write access is permitted
1491 */
1492static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1493{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001494 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001495}
1496
1497/**
1498 * smack_task_getpgid - Smack access check for getpgid
1499 * @p: the object task
1500 *
1501 * Returns 0 if current can read the object task, error code otherwise
1502 */
1503static int smack_task_getpgid(struct task_struct *p)
1504{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001505 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001506}
1507
1508/**
1509 * smack_task_getsid - Smack access check for getsid
1510 * @p: the object task
1511 *
1512 * Returns 0 if current can read the object task, error code otherwise
1513 */
1514static int smack_task_getsid(struct task_struct *p)
1515{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001516 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001517}
1518
1519/**
1520 * smack_task_getsecid - get the secid of the task
1521 * @p: the object task
1522 * @secid: where to put the result
1523 *
1524 * Sets the secid to contain a u32 version of the smack label.
1525 */
1526static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1527{
Casey Schaufler676dac42010-12-02 06:43:39 -08001528 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001529}
1530
1531/**
1532 * smack_task_setnice - Smack check on setting nice
1533 * @p: the task object
1534 * @nice: unused
1535 *
1536 * Return 0 if write access is permitted
1537 */
1538static int smack_task_setnice(struct task_struct *p, int nice)
1539{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001540 int rc;
1541
1542 rc = cap_task_setnice(p, nice);
1543 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001544 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001545 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001546}
1547
1548/**
1549 * smack_task_setioprio - Smack check on setting ioprio
1550 * @p: the task object
1551 * @ioprio: unused
1552 *
1553 * Return 0 if write access is permitted
1554 */
1555static int smack_task_setioprio(struct task_struct *p, int ioprio)
1556{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001557 int rc;
1558
1559 rc = cap_task_setioprio(p, ioprio);
1560 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001561 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001562 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001563}
1564
1565/**
1566 * smack_task_getioprio - Smack check on reading ioprio
1567 * @p: the task object
1568 *
1569 * Return 0 if read access is permitted
1570 */
1571static int smack_task_getioprio(struct task_struct *p)
1572{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001573 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001574}
1575
1576/**
1577 * smack_task_setscheduler - Smack check on setting scheduler
1578 * @p: the task object
1579 * @policy: unused
1580 * @lp: unused
1581 *
1582 * Return 0 if read access is permitted
1583 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001584static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001585{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001586 int rc;
1587
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001588 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001589 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001590 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001591 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001592}
1593
1594/**
1595 * smack_task_getscheduler - Smack check on reading scheduler
1596 * @p: the task object
1597 *
1598 * Return 0 if read access is permitted
1599 */
1600static int smack_task_getscheduler(struct task_struct *p)
1601{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001602 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001603}
1604
1605/**
1606 * smack_task_movememory - Smack check on moving memory
1607 * @p: the task object
1608 *
1609 * Return 0 if write access is permitted
1610 */
1611static int smack_task_movememory(struct task_struct *p)
1612{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001613 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001614}
1615
1616/**
1617 * smack_task_kill - Smack check on signal delivery
1618 * @p: the task object
1619 * @info: unused
1620 * @sig: unused
1621 * @secid: identifies the smack to use in lieu of current's
1622 *
1623 * Return 0 if write access is permitted
1624 *
1625 * The secid behavior is an artifact of an SELinux hack
1626 * in the USB code. Someday it may go away.
1627 */
1628static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1629 int sig, u32 secid)
1630{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001631 struct smk_audit_info ad;
1632
1633 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1634 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001635 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001636 * Sending a signal requires that the sender
1637 * can write the receiver.
1638 */
1639 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001640 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1641 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001642 /*
1643 * If the secid isn't 0 we're dealing with some USB IO
1644 * specific behavior. This is not clean. For one thing
1645 * we can't take privilege into account.
1646 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001647 return smk_access(smack_from_secid(secid),
1648 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001649}
1650
1651/**
1652 * smack_task_wait - Smack access check for waiting
1653 * @p: task to wait for
1654 *
1655 * Returns 0 if current can wait for p, error code otherwise
1656 */
1657static int smack_task_wait(struct task_struct *p)
1658{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001659 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08001660 char *sp = smk_of_current();
1661 char *tsp = smk_of_forked(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001662 int rc;
1663
Etienne Bassetecfcc532009-04-08 20:40:06 +02001664 /* we don't log here, we can be overriden */
Casey Schaufler676dac42010-12-02 06:43:39 -08001665 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001666 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001667 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001668
1669 /*
1670 * Allow the operation to succeed if either task
1671 * has privilege to perform operations that might
1672 * account for the smack labels having gotten to
1673 * be different in the first place.
1674 *
David Howells5cd9c582008-08-14 11:37:28 +01001675 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001676 * control ideal, taking the object's privilege
1677 * state into account in the decision as well as
1678 * the smack value.
1679 */
David Howells5cd9c582008-08-14 11:37:28 +01001680 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001681 rc = 0;
1682 /* we log only if we didn't get overriden */
1683 out_log:
1684 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1685 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001686 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001687 return rc;
1688}
1689
1690/**
1691 * smack_task_to_inode - copy task smack into the inode blob
1692 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001693 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001694 *
1695 * Sets the smack pointer in the inode security blob
1696 */
1697static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1698{
1699 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001700 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001701}
1702
1703/*
1704 * Socket hooks.
1705 */
1706
1707/**
1708 * smack_sk_alloc_security - Allocate a socket blob
1709 * @sk: the socket
1710 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001711 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001712 *
1713 * Assign Smack pointers to current
1714 *
1715 * Returns 0 on success, -ENOMEM is there's no memory
1716 */
1717static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1718{
Casey Schaufler676dac42010-12-02 06:43:39 -08001719 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001720 struct socket_smack *ssp;
1721
1722 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1723 if (ssp == NULL)
1724 return -ENOMEM;
1725
1726 ssp->smk_in = csp;
1727 ssp->smk_out = csp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001728 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001729
1730 sk->sk_security = ssp;
1731
1732 return 0;
1733}
1734
1735/**
1736 * smack_sk_free_security - Free a socket blob
1737 * @sk: the socket
1738 *
1739 * Clears the blob pointer
1740 */
1741static void smack_sk_free_security(struct sock *sk)
1742{
1743 kfree(sk->sk_security);
1744}
1745
1746/**
Paul Moore07feee82009-03-27 17:10:54 -04001747* smack_host_label - check host based restrictions
1748* @sip: the object end
1749*
1750* looks for host based access restrictions
1751*
1752* This version will only be appropriate for really small sets of single label
1753* hosts. The caller is responsible for ensuring that the RCU read lock is
1754* taken before calling this function.
1755*
1756* Returns the label of the far end or NULL if it's not special.
1757*/
1758static char *smack_host_label(struct sockaddr_in *sip)
1759{
1760 struct smk_netlbladdr *snp;
1761 struct in_addr *siap = &sip->sin_addr;
1762
1763 if (siap->s_addr == 0)
1764 return NULL;
1765
1766 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1767 /*
1768 * we break after finding the first match because
1769 * the list is sorted from longest to shortest mask
1770 * so we have found the most specific match
1771 */
1772 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001773 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1774 /* we have found the special CIPSO option */
1775 if (snp->smk_label == smack_cipso_option)
1776 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001777 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001778 }
Paul Moore07feee82009-03-27 17:10:54 -04001779
1780 return NULL;
1781}
1782
1783/**
Casey Schauflere114e472008-02-04 22:29:50 -08001784 * smack_set_catset - convert a capset to netlabel mls categories
1785 * @catset: the Smack categories
1786 * @sap: where to put the netlabel categories
1787 *
1788 * Allocates and fills attr.mls.cat
1789 */
1790static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1791{
1792 unsigned char *cp;
1793 unsigned char m;
1794 int cat;
1795 int rc;
1796 int byte;
1797
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001798 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001799 return;
1800
1801 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1802 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1803 sap->attr.mls.cat->startbit = 0;
1804
1805 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1806 for (m = 0x80; m != 0; m >>= 1, cat++) {
1807 if ((m & *cp) == 0)
1808 continue;
1809 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1810 cat, GFP_ATOMIC);
1811 }
1812}
1813
1814/**
1815 * smack_to_secattr - fill a secattr from a smack value
1816 * @smack: the smack value
1817 * @nlsp: where the result goes
1818 *
1819 * Casey says that CIPSO is good enough for now.
1820 * It can be used to effect.
1821 * It can also be abused to effect when necessary.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001822 * Apologies to the TSIG group in general and GW in particular.
Casey Schauflere114e472008-02-04 22:29:50 -08001823 */
1824static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1825{
1826 struct smack_cipso cipso;
1827 int rc;
1828
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001829 nlsp->domain = smack;
1830 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001831
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001832 rc = smack_to_cipso(smack, &cipso);
1833 if (rc == 0) {
1834 nlsp->attr.mls.lvl = cipso.smk_level;
1835 smack_set_catset(cipso.smk_catset, nlsp);
1836 } else {
1837 nlsp->attr.mls.lvl = smack_cipso_direct;
1838 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001839 }
1840}
1841
1842/**
1843 * smack_netlabel - Set the secattr on a socket
1844 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001845 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001846 *
1847 * Convert the outbound smack value (smk_out) to a
1848 * secattr and attach it to the socket.
1849 *
1850 * Returns 0 on success or an error code
1851 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001852static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001853{
Paul Moore07feee82009-03-27 17:10:54 -04001854 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001855 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001856 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001857
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001858 /*
1859 * Usually the netlabel code will handle changing the
1860 * packet labeling based on the label.
1861 * The case of a single label host is different, because
1862 * a single label host should never get a labeled packet
1863 * even though the label is usually associated with a packet
1864 * label.
1865 */
1866 local_bh_disable();
1867 bh_lock_sock_nested(sk);
1868
1869 if (ssp->smk_out == smack_net_ambient ||
1870 labeled == SMACK_UNLABELED_SOCKET)
1871 netlbl_sock_delattr(sk);
1872 else {
1873 netlbl_secattr_init(&secattr);
1874 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001875 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001876 netlbl_secattr_destroy(&secattr);
1877 }
1878
1879 bh_unlock_sock(sk);
1880 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001881
Casey Schauflere114e472008-02-04 22:29:50 -08001882 return rc;
1883}
1884
1885/**
Paul Moore07feee82009-03-27 17:10:54 -04001886 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1887 * @sk: the socket
1888 * @sap: the destination address
1889 *
1890 * Set the correct secattr for the given socket based on the destination
1891 * address and perform any outbound access checks needed.
1892 *
1893 * Returns 0 on success or an error code.
1894 *
1895 */
1896static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1897{
1898 int rc;
1899 int sk_lbl;
1900 char *hostsp;
1901 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001902 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001903
1904 rcu_read_lock();
1905 hostsp = smack_host_label(sap);
1906 if (hostsp != NULL) {
1907 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001908#ifdef CONFIG_AUDIT
1909 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1910 ad.a.u.net.family = sap->sin_family;
1911 ad.a.u.net.dport = sap->sin_port;
1912 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1913#endif
1914 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001915 } else {
1916 sk_lbl = SMACK_CIPSO_SOCKET;
1917 rc = 0;
1918 }
1919 rcu_read_unlock();
1920 if (rc != 0)
1921 return rc;
1922
1923 return smack_netlabel(sk, sk_lbl);
1924}
1925
1926/**
Casey Schauflere114e472008-02-04 22:29:50 -08001927 * smack_inode_setsecurity - set smack xattrs
1928 * @inode: the object
1929 * @name: attribute name
1930 * @value: attribute value
1931 * @size: size of the attribute
1932 * @flags: unused
1933 *
1934 * Sets the named attribute in the appropriate blob
1935 *
1936 * Returns 0 on success, or an error code
1937 */
1938static int smack_inode_setsecurity(struct inode *inode, const char *name,
1939 const void *value, size_t size, int flags)
1940{
1941 char *sp;
1942 struct inode_smack *nsp = inode->i_security;
1943 struct socket_smack *ssp;
1944 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001945 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001946
Etienne Basset43031542009-03-27 17:11:01 -04001947 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001948 return -EACCES;
1949
1950 sp = smk_import(value, size);
1951 if (sp == NULL)
1952 return -EINVAL;
1953
1954 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1955 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04001956 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08001957 return 0;
1958 }
1959 /*
1960 * The rest of the Smack xattrs are only on sockets.
1961 */
1962 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1963 return -EOPNOTSUPP;
1964
1965 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001966 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001967 return -EOPNOTSUPP;
1968
1969 ssp = sock->sk->sk_security;
1970
1971 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1972 ssp->smk_in = sp;
1973 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1974 ssp->smk_out = sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08001975 if (sock->sk->sk_family != PF_UNIX) {
1976 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1977 if (rc != 0)
1978 printk(KERN_WARNING
1979 "Smack: \"%s\" netlbl error %d.\n",
1980 __func__, -rc);
1981 }
Casey Schauflere114e472008-02-04 22:29:50 -08001982 } else
1983 return -EOPNOTSUPP;
1984
1985 return 0;
1986}
1987
1988/**
1989 * smack_socket_post_create - finish socket setup
1990 * @sock: the socket
1991 * @family: protocol family
1992 * @type: unused
1993 * @protocol: unused
1994 * @kern: unused
1995 *
1996 * Sets the netlabel information on the socket
1997 *
1998 * Returns 0 on success, and error code otherwise
1999 */
2000static int smack_socket_post_create(struct socket *sock, int family,
2001 int type, int protocol, int kern)
2002{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002003 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002004 return 0;
2005 /*
2006 * Set the outbound netlbl.
2007 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002008 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2009}
2010
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002011/**
2012 * smack_socket_connect - connect access check
2013 * @sock: the socket
2014 * @sap: the other end
2015 * @addrlen: size of sap
2016 *
2017 * Verifies that a connection may be possible
2018 *
2019 * Returns 0 on success, and error code otherwise
2020 */
2021static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2022 int addrlen)
2023{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002024 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2025 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002026 if (addrlen < sizeof(struct sockaddr_in))
2027 return -EINVAL;
2028
Paul Moore07feee82009-03-27 17:10:54 -04002029 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08002030}
2031
2032/**
2033 * smack_flags_to_may - convert S_ to MAY_ values
2034 * @flags: the S_ value
2035 *
2036 * Returns the equivalent MAY_ value
2037 */
2038static int smack_flags_to_may(int flags)
2039{
2040 int may = 0;
2041
2042 if (flags & S_IRUGO)
2043 may |= MAY_READ;
2044 if (flags & S_IWUGO)
2045 may |= MAY_WRITE;
2046 if (flags & S_IXUGO)
2047 may |= MAY_EXEC;
2048
2049 return may;
2050}
2051
2052/**
2053 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2054 * @msg: the object
2055 *
2056 * Returns 0
2057 */
2058static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2059{
Casey Schaufler676dac42010-12-02 06:43:39 -08002060 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002061 return 0;
2062}
2063
2064/**
2065 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2066 * @msg: the object
2067 *
2068 * Clears the blob pointer
2069 */
2070static void smack_msg_msg_free_security(struct msg_msg *msg)
2071{
2072 msg->security = NULL;
2073}
2074
2075/**
2076 * smack_of_shm - the smack pointer for the shm
2077 * @shp: the object
2078 *
2079 * Returns a pointer to the smack value
2080 */
2081static char *smack_of_shm(struct shmid_kernel *shp)
2082{
2083 return (char *)shp->shm_perm.security;
2084}
2085
2086/**
2087 * smack_shm_alloc_security - Set the security blob for shm
2088 * @shp: the object
2089 *
2090 * Returns 0
2091 */
2092static int smack_shm_alloc_security(struct shmid_kernel *shp)
2093{
2094 struct kern_ipc_perm *isp = &shp->shm_perm;
2095
Casey Schaufler676dac42010-12-02 06:43:39 -08002096 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002097 return 0;
2098}
2099
2100/**
2101 * smack_shm_free_security - Clear the security blob for shm
2102 * @shp: the object
2103 *
2104 * Clears the blob pointer
2105 */
2106static void smack_shm_free_security(struct shmid_kernel *shp)
2107{
2108 struct kern_ipc_perm *isp = &shp->shm_perm;
2109
2110 isp->security = NULL;
2111}
2112
2113/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002114 * smk_curacc_shm : check if current has access on shm
2115 * @shp : the object
2116 * @access : access requested
2117 *
2118 * Returns 0 if current has the requested access, error code otherwise
2119 */
2120static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2121{
2122 char *ssp = smack_of_shm(shp);
2123 struct smk_audit_info ad;
2124
2125#ifdef CONFIG_AUDIT
2126 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2127 ad.a.u.ipc_id = shp->shm_perm.id;
2128#endif
2129 return smk_curacc(ssp, access, &ad);
2130}
2131
2132/**
Casey Schauflere114e472008-02-04 22:29:50 -08002133 * smack_shm_associate - Smack access check for shm
2134 * @shp: the object
2135 * @shmflg: access requested
2136 *
2137 * Returns 0 if current has the requested access, error code otherwise
2138 */
2139static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2140{
Casey Schauflere114e472008-02-04 22:29:50 -08002141 int may;
2142
2143 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002144 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002145}
2146
2147/**
2148 * smack_shm_shmctl - Smack access check for shm
2149 * @shp: the object
2150 * @cmd: what it wants to do
2151 *
2152 * Returns 0 if current has the requested access, error code otherwise
2153 */
2154static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2155{
Casey Schauflere114e472008-02-04 22:29:50 -08002156 int may;
2157
2158 switch (cmd) {
2159 case IPC_STAT:
2160 case SHM_STAT:
2161 may = MAY_READ;
2162 break;
2163 case IPC_SET:
2164 case SHM_LOCK:
2165 case SHM_UNLOCK:
2166 case IPC_RMID:
2167 may = MAY_READWRITE;
2168 break;
2169 case IPC_INFO:
2170 case SHM_INFO:
2171 /*
2172 * System level information.
2173 */
2174 return 0;
2175 default:
2176 return -EINVAL;
2177 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002178 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002179}
2180
2181/**
2182 * smack_shm_shmat - Smack access for shmat
2183 * @shp: the object
2184 * @shmaddr: unused
2185 * @shmflg: access requested
2186 *
2187 * Returns 0 if current has the requested access, error code otherwise
2188 */
2189static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2190 int shmflg)
2191{
Casey Schauflere114e472008-02-04 22:29:50 -08002192 int may;
2193
2194 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002195 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002196}
2197
2198/**
2199 * smack_of_sem - the smack pointer for the sem
2200 * @sma: the object
2201 *
2202 * Returns a pointer to the smack value
2203 */
2204static char *smack_of_sem(struct sem_array *sma)
2205{
2206 return (char *)sma->sem_perm.security;
2207}
2208
2209/**
2210 * smack_sem_alloc_security - Set the security blob for sem
2211 * @sma: the object
2212 *
2213 * Returns 0
2214 */
2215static int smack_sem_alloc_security(struct sem_array *sma)
2216{
2217 struct kern_ipc_perm *isp = &sma->sem_perm;
2218
Casey Schaufler676dac42010-12-02 06:43:39 -08002219 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002220 return 0;
2221}
2222
2223/**
2224 * smack_sem_free_security - Clear the security blob for sem
2225 * @sma: the object
2226 *
2227 * Clears the blob pointer
2228 */
2229static void smack_sem_free_security(struct sem_array *sma)
2230{
2231 struct kern_ipc_perm *isp = &sma->sem_perm;
2232
2233 isp->security = NULL;
2234}
2235
2236/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002237 * smk_curacc_sem : check if current has access on sem
2238 * @sma : the object
2239 * @access : access requested
2240 *
2241 * Returns 0 if current has the requested access, error code otherwise
2242 */
2243static int smk_curacc_sem(struct sem_array *sma, int access)
2244{
2245 char *ssp = smack_of_sem(sma);
2246 struct smk_audit_info ad;
2247
2248#ifdef CONFIG_AUDIT
2249 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2250 ad.a.u.ipc_id = sma->sem_perm.id;
2251#endif
2252 return smk_curacc(ssp, access, &ad);
2253}
2254
2255/**
Casey Schauflere114e472008-02-04 22:29:50 -08002256 * smack_sem_associate - Smack access check for sem
2257 * @sma: the object
2258 * @semflg: access requested
2259 *
2260 * Returns 0 if current has the requested access, error code otherwise
2261 */
2262static int smack_sem_associate(struct sem_array *sma, int semflg)
2263{
Casey Schauflere114e472008-02-04 22:29:50 -08002264 int may;
2265
2266 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002267 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002268}
2269
2270/**
2271 * smack_sem_shmctl - Smack access check for sem
2272 * @sma: the object
2273 * @cmd: what it wants to do
2274 *
2275 * Returns 0 if current has the requested access, error code otherwise
2276 */
2277static int smack_sem_semctl(struct sem_array *sma, int cmd)
2278{
Casey Schauflere114e472008-02-04 22:29:50 -08002279 int may;
2280
2281 switch (cmd) {
2282 case GETPID:
2283 case GETNCNT:
2284 case GETZCNT:
2285 case GETVAL:
2286 case GETALL:
2287 case IPC_STAT:
2288 case SEM_STAT:
2289 may = MAY_READ;
2290 break;
2291 case SETVAL:
2292 case SETALL:
2293 case IPC_RMID:
2294 case IPC_SET:
2295 may = MAY_READWRITE;
2296 break;
2297 case IPC_INFO:
2298 case SEM_INFO:
2299 /*
2300 * System level information
2301 */
2302 return 0;
2303 default:
2304 return -EINVAL;
2305 }
2306
Etienne Bassetecfcc532009-04-08 20:40:06 +02002307 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002308}
2309
2310/**
2311 * smack_sem_semop - Smack checks of semaphore operations
2312 * @sma: the object
2313 * @sops: unused
2314 * @nsops: unused
2315 * @alter: unused
2316 *
2317 * Treated as read and write in all cases.
2318 *
2319 * Returns 0 if access is allowed, error code otherwise
2320 */
2321static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2322 unsigned nsops, int alter)
2323{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002324 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002325}
2326
2327/**
2328 * smack_msg_alloc_security - Set the security blob for msg
2329 * @msq: the object
2330 *
2331 * Returns 0
2332 */
2333static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2334{
2335 struct kern_ipc_perm *kisp = &msq->q_perm;
2336
Casey Schaufler676dac42010-12-02 06:43:39 -08002337 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002338 return 0;
2339}
2340
2341/**
2342 * smack_msg_free_security - Clear the security blob for msg
2343 * @msq: the object
2344 *
2345 * Clears the blob pointer
2346 */
2347static void smack_msg_queue_free_security(struct msg_queue *msq)
2348{
2349 struct kern_ipc_perm *kisp = &msq->q_perm;
2350
2351 kisp->security = NULL;
2352}
2353
2354/**
2355 * smack_of_msq - the smack pointer for the msq
2356 * @msq: the object
2357 *
2358 * Returns a pointer to the smack value
2359 */
2360static char *smack_of_msq(struct msg_queue *msq)
2361{
2362 return (char *)msq->q_perm.security;
2363}
2364
2365/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002366 * smk_curacc_msq : helper to check if current has access on msq
2367 * @msq : the msq
2368 * @access : access requested
2369 *
2370 * return 0 if current has access, error otherwise
2371 */
2372static int smk_curacc_msq(struct msg_queue *msq, int access)
2373{
2374 char *msp = smack_of_msq(msq);
2375 struct smk_audit_info ad;
2376
2377#ifdef CONFIG_AUDIT
2378 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2379 ad.a.u.ipc_id = msq->q_perm.id;
2380#endif
2381 return smk_curacc(msp, access, &ad);
2382}
2383
2384/**
Casey Schauflere114e472008-02-04 22:29:50 -08002385 * smack_msg_queue_associate - Smack access check for msg_queue
2386 * @msq: the object
2387 * @msqflg: access requested
2388 *
2389 * Returns 0 if current has the requested access, error code otherwise
2390 */
2391static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2392{
Casey Schauflere114e472008-02-04 22:29:50 -08002393 int may;
2394
2395 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002396 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002397}
2398
2399/**
2400 * smack_msg_queue_msgctl - Smack access check for msg_queue
2401 * @msq: the object
2402 * @cmd: what it wants to do
2403 *
2404 * Returns 0 if current has the requested access, error code otherwise
2405 */
2406static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2407{
Casey Schauflere114e472008-02-04 22:29:50 -08002408 int may;
2409
2410 switch (cmd) {
2411 case IPC_STAT:
2412 case MSG_STAT:
2413 may = MAY_READ;
2414 break;
2415 case IPC_SET:
2416 case IPC_RMID:
2417 may = MAY_READWRITE;
2418 break;
2419 case IPC_INFO:
2420 case MSG_INFO:
2421 /*
2422 * System level information
2423 */
2424 return 0;
2425 default:
2426 return -EINVAL;
2427 }
2428
Etienne Bassetecfcc532009-04-08 20:40:06 +02002429 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002430}
2431
2432/**
2433 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2434 * @msq: the object
2435 * @msg: unused
2436 * @msqflg: access requested
2437 *
2438 * Returns 0 if current has the requested access, error code otherwise
2439 */
2440static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2441 int msqflg)
2442{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002443 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002444
Etienne Bassetecfcc532009-04-08 20:40:06 +02002445 may = smack_flags_to_may(msqflg);
2446 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002447}
2448
2449/**
2450 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2451 * @msq: the object
2452 * @msg: unused
2453 * @target: unused
2454 * @type: unused
2455 * @mode: unused
2456 *
2457 * Returns 0 if current has read and write access, error code otherwise
2458 */
2459static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2460 struct task_struct *target, long type, int mode)
2461{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002462 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002463}
2464
2465/**
2466 * smack_ipc_permission - Smack access for ipc_permission()
2467 * @ipp: the object permissions
2468 * @flag: access requested
2469 *
2470 * Returns 0 if current has read and write access, error code otherwise
2471 */
2472static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2473{
2474 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002475 int may = smack_flags_to_may(flag);
2476 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002477
Etienne Bassetecfcc532009-04-08 20:40:06 +02002478#ifdef CONFIG_AUDIT
2479 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2480 ad.a.u.ipc_id = ipp->id;
2481#endif
2482 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002483}
2484
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002485/**
2486 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002487 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002488 * @secid: where result will be saved
2489 */
2490static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2491{
2492 char *smack = ipp->security;
2493
2494 *secid = smack_to_secid(smack);
2495}
2496
Casey Schauflere114e472008-02-04 22:29:50 -08002497/**
2498 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002499 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002500 * @inode: the object
2501 *
2502 * Set the inode's security blob if it hasn't been done already.
2503 */
2504static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2505{
2506 struct super_block *sbp;
2507 struct superblock_smack *sbsp;
2508 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002509 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002510 char *fetched;
2511 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002512 char trattr[TRANS_TRUE_SIZE];
2513 int transflag = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002514 struct dentry *dp;
2515
2516 if (inode == NULL)
2517 return;
2518
2519 isp = inode->i_security;
2520
2521 mutex_lock(&isp->smk_lock);
2522 /*
2523 * If the inode is already instantiated
2524 * take the quick way out
2525 */
2526 if (isp->smk_flags & SMK_INODE_INSTANT)
2527 goto unlockandout;
2528
2529 sbp = inode->i_sb;
2530 sbsp = sbp->s_security;
2531 /*
2532 * We're going to use the superblock default label
2533 * if there's no label on the file.
2534 */
2535 final = sbsp->smk_default;
2536
2537 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002538 * If this is the root inode the superblock
2539 * may be in the process of initialization.
2540 * If that is the case use the root value out
2541 * of the superblock.
2542 */
2543 if (opt_dentry->d_parent == opt_dentry) {
2544 isp->smk_inode = sbsp->smk_root;
2545 isp->smk_flags |= SMK_INODE_INSTANT;
2546 goto unlockandout;
2547 }
2548
2549 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002550 * This is pretty hackish.
2551 * Casey says that we shouldn't have to do
2552 * file system specific code, but it does help
2553 * with keeping it simple.
2554 */
2555 switch (sbp->s_magic) {
2556 case SMACK_MAGIC:
2557 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002558 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002559 * that the smack file system doesn't do
2560 * extended attributes.
2561 */
2562 final = smack_known_star.smk_known;
2563 break;
2564 case PIPEFS_MAGIC:
2565 /*
2566 * Casey says pipes are easy (?)
2567 */
2568 final = smack_known_star.smk_known;
2569 break;
2570 case DEVPTS_SUPER_MAGIC:
2571 /*
2572 * devpts seems content with the label of the task.
2573 * Programs that change smack have to treat the
2574 * pty with respect.
2575 */
2576 final = csp;
2577 break;
2578 case SOCKFS_MAGIC:
2579 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002580 * Socket access is controlled by the socket
2581 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002582 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002583 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002584 break;
2585 case PROC_SUPER_MAGIC:
2586 /*
2587 * Casey says procfs appears not to care.
2588 * The superblock default suffices.
2589 */
2590 break;
2591 case TMPFS_MAGIC:
2592 /*
2593 * Device labels should come from the filesystem,
2594 * but watch out, because they're volitile,
2595 * getting recreated on every reboot.
2596 */
2597 final = smack_known_star.smk_known;
2598 /*
2599 * No break.
2600 *
2601 * If a smack value has been set we want to use it,
2602 * but since tmpfs isn't giving us the opportunity
2603 * to set mount options simulate setting the
2604 * superblock default.
2605 */
2606 default:
2607 /*
2608 * This isn't an understood special case.
2609 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002610 */
2611
2612 /*
2613 * UNIX domain sockets use lower level socket data.
2614 */
2615 if (S_ISSOCK(inode->i_mode)) {
2616 final = smack_known_star.smk_known;
2617 break;
2618 }
2619 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002620 * No xattr support means, alas, no SMACK label.
2621 * Use the aforeapplied default.
2622 * It would be curious if the label of the task
2623 * does not match that assigned.
2624 */
2625 if (inode->i_op->getxattr == NULL)
2626 break;
2627 /*
2628 * Get the dentry for xattr.
2629 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002630 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002631 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002632 if (fetched != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -08002633 final = fetched;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002634 if (S_ISDIR(inode->i_mode)) {
2635 trattr[0] = '\0';
2636 inode->i_op->getxattr(dp,
2637 XATTR_NAME_SMACKTRANSMUTE,
2638 trattr, TRANS_TRUE_SIZE);
2639 if (strncmp(trattr, TRANS_TRUE,
2640 TRANS_TRUE_SIZE) == 0)
2641 transflag = SMK_INODE_TRANSMUTE;
2642 }
2643 }
2644 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002645 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002646
Casey Schauflere114e472008-02-04 22:29:50 -08002647 dput(dp);
2648 break;
2649 }
2650
2651 if (final == NULL)
2652 isp->smk_inode = csp;
2653 else
2654 isp->smk_inode = final;
2655
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002656 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002657
2658unlockandout:
2659 mutex_unlock(&isp->smk_lock);
2660 return;
2661}
2662
2663/**
2664 * smack_getprocattr - Smack process attribute access
2665 * @p: the object task
2666 * @name: the name of the attribute in /proc/.../attr
2667 * @value: where to put the result
2668 *
2669 * Places a copy of the task Smack into value
2670 *
2671 * Returns the length of the smack label or an error code
2672 */
2673static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2674{
2675 char *cp;
2676 int slen;
2677
2678 if (strcmp(name, "current") != 0)
2679 return -EINVAL;
2680
Casey Schaufler676dac42010-12-02 06:43:39 -08002681 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002682 if (cp == NULL)
2683 return -ENOMEM;
2684
2685 slen = strlen(cp);
2686 *value = cp;
2687 return slen;
2688}
2689
2690/**
2691 * smack_setprocattr - Smack process attribute setting
2692 * @p: the object task
2693 * @name: the name of the attribute in /proc/.../attr
2694 * @value: the value to set
2695 * @size: the size of the value
2696 *
2697 * Sets the Smack value of the task. Only setting self
2698 * is permitted and only with privilege
2699 *
2700 * Returns the length of the smack label or an error code
2701 */
2702static int smack_setprocattr(struct task_struct *p, char *name,
2703 void *value, size_t size)
2704{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002705 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002706 struct task_smack *tsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002707 struct task_smack *oldtsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002708 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002709 char *newsmack;
2710
Casey Schauflere114e472008-02-04 22:29:50 -08002711 /*
2712 * Changing another process' Smack value is too dangerous
2713 * and supports no sane use case.
2714 */
2715 if (p != current)
2716 return -EPERM;
2717
David Howells5cd9c582008-08-14 11:37:28 +01002718 if (!capable(CAP_MAC_ADMIN))
2719 return -EPERM;
2720
Casey Schauflere114e472008-02-04 22:29:50 -08002721 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2722 return -EINVAL;
2723
2724 if (strcmp(name, "current") != 0)
2725 return -EINVAL;
2726
2727 newsmack = smk_import(value, size);
2728 if (newsmack == NULL)
2729 return -EINVAL;
2730
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002731 /*
2732 * No process is ever allowed the web ("@") label.
2733 */
2734 if (newsmack == smack_known_web.smk_known)
2735 return -EPERM;
2736
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002737 oldtsp = p->cred->security;
David Howellsd84f4f92008-11-14 10:39:23 +11002738 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002739 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002740 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002741
2742 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08002743 if (tsp == NULL) {
2744 kfree(new);
2745 return -ENOMEM;
2746 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002747 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2748 if (rc != 0)
2749 return rc;
2750
Casey Schaufler676dac42010-12-02 06:43:39 -08002751 new->security = tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002752 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002753 return size;
2754}
2755
2756/**
2757 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002758 * @sock: one sock
2759 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002760 * @newsk: unused
2761 *
2762 * Return 0 if a subject with the smack of sock could access
2763 * an object with the smack of other, otherwise an error code
2764 */
David S. Miller3610cda2011-01-05 15:38:53 -08002765static int smack_unix_stream_connect(struct sock *sock,
2766 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002767{
James Morrisd2e7ad12011-01-10 09:46:24 +11002768 struct socket_smack *ssp = sock->sk_security;
2769 struct socket_smack *osp = other->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002770 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002771 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002772
Etienne Bassetecfcc532009-04-08 20:40:06 +02002773 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
David S. Miller3610cda2011-01-05 15:38:53 -08002774 smk_ad_setfield_u_net_sk(&ad, other);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002775
2776 if (!capable(CAP_MAC_OVERRIDE))
2777 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2778
2779 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002780}
2781
2782/**
2783 * smack_unix_may_send - Smack access on UDS
2784 * @sock: one socket
2785 * @other: the other socket
2786 *
2787 * Return 0 if a subject with the smack of sock could access
2788 * an object with the smack of other, otherwise an error code
2789 */
2790static int smack_unix_may_send(struct socket *sock, struct socket *other)
2791{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002792 struct socket_smack *ssp = sock->sk->sk_security;
2793 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002794 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002795 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002796
Etienne Bassetecfcc532009-04-08 20:40:06 +02002797 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2798 smk_ad_setfield_u_net_sk(&ad, other->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002799
2800 if (!capable(CAP_MAC_OVERRIDE))
2801 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2802
2803 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002804}
2805
2806/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002807 * smack_socket_sendmsg - Smack check based on destination host
2808 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002809 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002810 * @size: the size of the message
2811 *
2812 * Return 0 if the current subject can write to the destination
2813 * host. This is only a question if the destination is a single
2814 * label host.
2815 */
2816static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2817 int size)
2818{
2819 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002820
2821 /*
2822 * Perfectly reasonable for this to be NULL
2823 */
Julia Lawallda34d422009-08-05 14:34:55 +02002824 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002825 return 0;
2826
Paul Moore07feee82009-03-27 17:10:54 -04002827 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002828}
2829
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002830/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002831 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002832 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002833 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08002834 *
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002835 * Returns a pointer to a Smack label found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08002836 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002837static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2838 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08002839{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002840 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002841 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002842 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002843 int pcat;
2844
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002845 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002846 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002847 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002848 * If there are flags but no level netlabel isn't
2849 * behaving the way we expect it to.
2850 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002851 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002852 * Without guidance regarding the smack value
2853 * for the packet fall back on the network
2854 * ambient value.
2855 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002856 memset(smack, '\0', SMK_LABELLEN);
2857 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2858 for (pcat = -1;;) {
2859 pcat = netlbl_secattr_catmap_walk(
2860 sap->attr.mls.cat, pcat + 1);
2861 if (pcat < 0)
2862 break;
2863 smack_catset_bit(pcat, smack);
2864 }
2865 /*
2866 * If it is CIPSO using smack direct mapping
2867 * we are already done. WeeHee.
2868 */
2869 if (sap->attr.mls.lvl == smack_cipso_direct) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002870 /*
2871 * The label sent is usually on the label list.
2872 *
2873 * If it is not we may still want to allow the
2874 * delivery.
2875 *
2876 * If the recipient is accepting all packets
2877 * because it is using the star ("*") label
2878 * for SMACK64IPIN provide the web ("@") label
2879 * so that a directed response will succeed.
2880 * This is not very correct from a MAC point
2881 * of view, but gets around the problem that
2882 * locking prevents adding the newly discovered
2883 * label to the list.
2884 * The case where the recipient is not using
2885 * the star label should obviously fail.
2886 * The easy way to do this is to provide the
2887 * star label as the subject label.
2888 */
2889 skp = smk_find_entry(smack);
2890 if (skp != NULL)
2891 return skp->smk_known;
2892 if (ssp != NULL &&
2893 ssp->smk_in == smack_known_star.smk_known)
2894 return smack_known_web.smk_known;
2895 return smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002896 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002897 /*
2898 * Look it up in the supplied table if it is not
2899 * a direct mapping.
2900 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002901 sp = smack_from_cipso(sap->attr.mls.lvl, smack);
2902 if (sp != NULL)
2903 return sp;
2904 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2905 return smack_known_web.smk_known;
2906 return smack_known_star.smk_known;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002907 }
2908 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2909 /*
2910 * Looks like a fallback, which gives us a secid.
2911 */
2912 sp = smack_from_secid(sap->attr.secid);
2913 /*
2914 * This has got to be a bug because it is
2915 * impossible to specify a fallback without
2916 * specifying the label, which will ensure
2917 * it has a secid, and the only way to get a
2918 * secid is from a fallback.
2919 */
2920 BUG_ON(sp == NULL);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002921 return sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002922 }
2923 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002924 * Without guidance regarding the smack value
2925 * for the packet fall back on the network
2926 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002927 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002928 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08002929}
2930
2931/**
2932 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2933 * @sk: socket
2934 * @skb: packet
2935 *
2936 * Returns 0 if the packet should be delivered, an error code otherwise
2937 */
2938static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2939{
2940 struct netlbl_lsm_secattr secattr;
2941 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002942 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002943 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002944 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002945 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2946 return 0;
2947
2948 /*
2949 * Translate what netlabel gave us.
2950 */
Casey Schauflere114e472008-02-04 22:29:50 -08002951 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002952
Casey Schauflere114e472008-02-04 22:29:50 -08002953 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002954 if (rc == 0)
2955 csp = smack_from_secattr(&secattr, ssp);
2956 else
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002957 csp = smack_net_ambient;
2958
Casey Schauflere114e472008-02-04 22:29:50 -08002959 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002960
Etienne Bassetecfcc532009-04-08 20:40:06 +02002961#ifdef CONFIG_AUDIT
2962 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2963 ad.a.u.net.family = sk->sk_family;
Eric Dumazet8964be42009-11-20 15:35:04 -08002964 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002965 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2966#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002967 /*
2968 * Receiving a packet requires that the other end
2969 * be able to write here. Read access is not required.
2970 * This is the simplist possible security model
2971 * for networking.
2972 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002973 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002974 if (rc != 0)
2975 netlbl_skbuff_err(skb, rc, 0);
2976 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002977}
2978
2979/**
2980 * smack_socket_getpeersec_stream - pull in packet label
2981 * @sock: the socket
2982 * @optval: user's destination
2983 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002984 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002985 *
2986 * returns zero on success, an error code otherwise
2987 */
2988static int smack_socket_getpeersec_stream(struct socket *sock,
2989 char __user *optval,
2990 int __user *optlen, unsigned len)
2991{
2992 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002993 char *rcp = "";
2994 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08002995 int rc = 0;
2996
2997 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002998 if (ssp->smk_packet != NULL) {
2999 rcp = ssp->smk_packet;
3000 slen = strlen(rcp) + 1;
3001 }
Casey Schauflere114e472008-02-04 22:29:50 -08003002
3003 if (slen > len)
3004 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003005 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003006 rc = -EFAULT;
3007
3008 if (put_user(slen, optlen) != 0)
3009 rc = -EFAULT;
3010
3011 return rc;
3012}
3013
3014
3015/**
3016 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003017 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003018 * @skb: packet data
3019 * @secid: pointer to where to put the secid of the packet
3020 *
3021 * Sets the netlabel socket state on sk from parent
3022 */
3023static int smack_socket_getpeersec_dgram(struct socket *sock,
3024 struct sk_buff *skb, u32 *secid)
3025
3026{
3027 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003028 struct socket_smack *ssp = NULL;
3029 char *sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003030 int family = PF_UNSPEC;
3031 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003032 int rc;
3033
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003034 if (skb != NULL) {
3035 if (skb->protocol == htons(ETH_P_IP))
3036 family = PF_INET;
3037 else if (skb->protocol == htons(ETH_P_IPV6))
3038 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003039 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003040 if (family == PF_UNSPEC && sock != NULL)
3041 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003042
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003043 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003044 ssp = sock->sk->sk_security;
3045 s = smack_to_secid(ssp->smk_out);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003046 } else if (family == PF_INET || family == PF_INET6) {
3047 /*
3048 * Translate what netlabel gave us.
3049 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003050 if (sock != NULL && sock->sk != NULL)
3051 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003052 netlbl_secattr_init(&secattr);
3053 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3054 if (rc == 0) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003055 sp = smack_from_secattr(&secattr, ssp);
3056 s = smack_to_secid(sp);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003057 }
3058 netlbl_secattr_destroy(&secattr);
3059 }
3060 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003061 if (s == 0)
3062 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003063 return 0;
3064}
3065
3066/**
Paul Moore07feee82009-03-27 17:10:54 -04003067 * smack_sock_graft - Initialize a newly created socket with an existing sock
3068 * @sk: child sock
3069 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003070 *
Paul Moore07feee82009-03-27 17:10:54 -04003071 * Set the smk_{in,out} state of an existing sock based on the process that
3072 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003073 */
3074static void smack_sock_graft(struct sock *sk, struct socket *parent)
3075{
3076 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08003077
Paul Moore07feee82009-03-27 17:10:54 -04003078 if (sk == NULL ||
3079 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003080 return;
3081
3082 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08003083 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04003084 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003085}
3086
3087/**
3088 * smack_inet_conn_request - Smack access check on connect
3089 * @sk: socket involved
3090 * @skb: packet
3091 * @req: unused
3092 *
3093 * Returns 0 if a task with the packet label could write to
3094 * the socket, otherwise an error code
3095 */
3096static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3097 struct request_sock *req)
3098{
Paul Moore07feee82009-03-27 17:10:54 -04003099 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003100 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003101 struct netlbl_lsm_secattr secattr;
3102 struct sockaddr_in addr;
3103 struct iphdr *hdr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003104 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003105 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003106 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003107
Paul Moore07feee82009-03-27 17:10:54 -04003108 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3109 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3110 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08003111
Paul Moore07feee82009-03-27 17:10:54 -04003112 netlbl_secattr_init(&secattr);
3113 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003114 if (rc == 0)
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003115 sp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003116 else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003117 sp = smack_known_huh.smk_known;
Paul Moore07feee82009-03-27 17:10:54 -04003118 netlbl_secattr_destroy(&secattr);
3119
Etienne Bassetecfcc532009-04-08 20:40:06 +02003120#ifdef CONFIG_AUDIT
3121 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3122 ad.a.u.net.family = family;
Eric Dumazet8964be42009-11-20 15:35:04 -08003123 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003124 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3125#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003126 /*
Paul Moore07feee82009-03-27 17:10:54 -04003127 * Receiving a packet requires that the other end be able to write
3128 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003129 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003130 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003131 if (rc != 0)
3132 return rc;
3133
3134 /*
3135 * Save the peer's label in the request_sock so we can later setup
3136 * smk_packet in the child socket so that SO_PEERCRED can report it.
3137 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003138 req->peer_secid = smack_to_secid(sp);
Paul Moore07feee82009-03-27 17:10:54 -04003139
3140 /*
3141 * We need to decide if we want to label the incoming connection here
3142 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003143 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003144 */
3145 hdr = ip_hdr(skb);
3146 addr.sin_addr.s_addr = hdr->saddr;
3147 rcu_read_lock();
3148 if (smack_host_label(&addr) == NULL) {
3149 rcu_read_unlock();
3150 netlbl_secattr_init(&secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003151 smack_to_secattr(sp, &secattr);
Paul Moore07feee82009-03-27 17:10:54 -04003152 rc = netlbl_req_setattr(req, &secattr);
3153 netlbl_secattr_destroy(&secattr);
3154 } else {
3155 rcu_read_unlock();
3156 netlbl_req_delattr(req);
3157 }
Casey Schauflere114e472008-02-04 22:29:50 -08003158
3159 return rc;
3160}
3161
Paul Moore07feee82009-03-27 17:10:54 -04003162/**
3163 * smack_inet_csk_clone - Copy the connection information to the new socket
3164 * @sk: the new socket
3165 * @req: the connection's request_sock
3166 *
3167 * Transfer the connection's peer label to the newly created socket.
3168 */
3169static void smack_inet_csk_clone(struct sock *sk,
3170 const struct request_sock *req)
3171{
3172 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003173
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003174 if (req->peer_secid != 0)
3175 ssp->smk_packet = smack_from_secid(req->peer_secid);
3176 else
3177 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003178}
3179
Casey Schauflere114e472008-02-04 22:29:50 -08003180/*
3181 * Key management security hooks
3182 *
3183 * Casey has not tested key support very heavily.
3184 * The permission check is most likely too restrictive.
3185 * If you care about keys please have a look.
3186 */
3187#ifdef CONFIG_KEYS
3188
3189/**
3190 * smack_key_alloc - Set the key security blob
3191 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003192 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003193 * @flags: unused
3194 *
3195 * No allocation required
3196 *
3197 * Returns 0
3198 */
David Howellsd84f4f92008-11-14 10:39:23 +11003199static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003200 unsigned long flags)
3201{
Casey Schaufler676dac42010-12-02 06:43:39 -08003202 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003203 return 0;
3204}
3205
3206/**
3207 * smack_key_free - Clear the key security blob
3208 * @key: the object
3209 *
3210 * Clear the blob pointer
3211 */
3212static void smack_key_free(struct key *key)
3213{
3214 key->security = NULL;
3215}
3216
3217/*
3218 * smack_key_permission - Smack access on a key
3219 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003220 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003221 * @perm: unused
3222 *
3223 * Return 0 if the task has read and write to the object,
3224 * an error code otherwise
3225 */
3226static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003227 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003228{
3229 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003230 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08003231 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003232
3233 keyp = key_ref_to_ptr(key_ref);
3234 if (keyp == NULL)
3235 return -EINVAL;
3236 /*
3237 * If the key hasn't been initialized give it access so that
3238 * it may do so.
3239 */
3240 if (keyp->security == NULL)
3241 return 0;
3242 /*
3243 * This should not occur
3244 */
Casey Schaufler676dac42010-12-02 06:43:39 -08003245 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003246 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003247#ifdef CONFIG_AUDIT
3248 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3249 ad.a.u.key_struct.key = keyp->serial;
3250 ad.a.u.key_struct.key_desc = keyp->description;
3251#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08003252 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02003253 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003254}
3255#endif /* CONFIG_KEYS */
3256
3257/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003258 * Smack Audit hooks
3259 *
3260 * Audit requires a unique representation of each Smack specific
3261 * rule. This unique representation is used to distinguish the
3262 * object to be audited from remaining kernel objects and also
3263 * works as a glue between the audit hooks.
3264 *
3265 * Since repository entries are added but never deleted, we'll use
3266 * the smack_known label address related to the given audit rule as
3267 * the needed unique representation. This also better fits the smack
3268 * model where nearly everything is a label.
3269 */
3270#ifdef CONFIG_AUDIT
3271
3272/**
3273 * smack_audit_rule_init - Initialize a smack audit rule
3274 * @field: audit rule fields given from user-space (audit.h)
3275 * @op: required testing operator (=, !=, >, <, ...)
3276 * @rulestr: smack label to be audited
3277 * @vrule: pointer to save our own audit rule representation
3278 *
3279 * Prepare to audit cases where (@field @op @rulestr) is true.
3280 * The label to be audited is created if necessay.
3281 */
3282static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3283{
3284 char **rule = (char **)vrule;
3285 *rule = NULL;
3286
3287 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3288 return -EINVAL;
3289
Al Viro5af75d82008-12-16 05:59:26 -05003290 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003291 return -EINVAL;
3292
3293 *rule = smk_import(rulestr, 0);
3294
3295 return 0;
3296}
3297
3298/**
3299 * smack_audit_rule_known - Distinguish Smack audit rules
3300 * @krule: rule of interest, in Audit kernel representation format
3301 *
3302 * This is used to filter Smack rules from remaining Audit ones.
3303 * If it's proved that this rule belongs to us, the
3304 * audit_rule_match hook will be called to do the final judgement.
3305 */
3306static int smack_audit_rule_known(struct audit_krule *krule)
3307{
3308 struct audit_field *f;
3309 int i;
3310
3311 for (i = 0; i < krule->field_count; i++) {
3312 f = &krule->fields[i];
3313
3314 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3315 return 1;
3316 }
3317
3318 return 0;
3319}
3320
3321/**
3322 * smack_audit_rule_match - Audit given object ?
3323 * @secid: security id for identifying the object to test
3324 * @field: audit rule flags given from user-space
3325 * @op: required testing operator
3326 * @vrule: smack internal rule presentation
3327 * @actx: audit context associated with the check
3328 *
3329 * The core Audit hook. It's used to take the decision of
3330 * whether to audit or not to audit a given object.
3331 */
3332static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3333 struct audit_context *actx)
3334{
3335 char *smack;
3336 char *rule = vrule;
3337
3338 if (!rule) {
3339 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3340 "Smack: missing rule\n");
3341 return -ENOENT;
3342 }
3343
3344 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3345 return 0;
3346
3347 smack = smack_from_secid(secid);
3348
3349 /*
3350 * No need to do string comparisons. If a match occurs,
3351 * both pointers will point to the same smack_known
3352 * label.
3353 */
Al Viro5af75d82008-12-16 05:59:26 -05003354 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003355 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003356 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003357 return (rule != smack);
3358
3359 return 0;
3360}
3361
3362/**
3363 * smack_audit_rule_free - free smack rule representation
3364 * @vrule: rule to be freed.
3365 *
3366 * No memory was allocated.
3367 */
3368static void smack_audit_rule_free(void *vrule)
3369{
3370 /* No-op */
3371}
3372
3373#endif /* CONFIG_AUDIT */
3374
Randy Dunlap251a2a92009-02-18 11:42:33 -08003375/**
Casey Schauflere114e472008-02-04 22:29:50 -08003376 * smack_secid_to_secctx - return the smack label for a secid
3377 * @secid: incoming integer
3378 * @secdata: destination
3379 * @seclen: how long it is
3380 *
3381 * Exists for networking code.
3382 */
3383static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3384{
3385 char *sp = smack_from_secid(secid);
3386
Eric Parisd5630b92010-10-13 16:24:48 -04003387 if (secdata)
3388 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003389 *seclen = strlen(sp);
3390 return 0;
3391}
3392
Randy Dunlap251a2a92009-02-18 11:42:33 -08003393/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003394 * smack_secctx_to_secid - return the secid for a smack label
3395 * @secdata: smack label
3396 * @seclen: how long result is
3397 * @secid: outgoing integer
3398 *
3399 * Exists for audit and networking code.
3400 */
David Howellse52c17642008-04-29 20:52:51 +01003401static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003402{
3403 *secid = smack_to_secid(secdata);
3404 return 0;
3405}
3406
Randy Dunlap251a2a92009-02-18 11:42:33 -08003407/**
Casey Schauflere114e472008-02-04 22:29:50 -08003408 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003409 * @secdata: unused
3410 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003411 *
3412 * Exists to make sure nothing gets done, and properly
3413 */
3414static void smack_release_secctx(char *secdata, u32 seclen)
3415{
3416}
3417
David P. Quigley1ee65e32009-09-03 14:25:57 -04003418static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3419{
3420 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3421}
3422
3423static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3424{
3425 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3426}
3427
3428static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3429{
3430 int len = 0;
3431 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3432
3433 if (len < 0)
3434 return len;
3435 *ctxlen = len;
3436 return 0;
3437}
3438
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003439struct security_operations smack_ops = {
3440 .name = "smack",
3441
Ingo Molnar9e488582009-05-07 19:26:19 +10003442 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003443 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003444 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003445
3446 .sb_alloc_security = smack_sb_alloc_security,
3447 .sb_free_security = smack_sb_free_security,
3448 .sb_copy_data = smack_sb_copy_data,
3449 .sb_kern_mount = smack_sb_kern_mount,
3450 .sb_statfs = smack_sb_statfs,
3451 .sb_mount = smack_sb_mount,
3452 .sb_umount = smack_sb_umount,
3453
Casey Schaufler676dac42010-12-02 06:43:39 -08003454 .bprm_set_creds = smack_bprm_set_creds,
3455
Casey Schauflere114e472008-02-04 22:29:50 -08003456 .inode_alloc_security = smack_inode_alloc_security,
3457 .inode_free_security = smack_inode_free_security,
3458 .inode_init_security = smack_inode_init_security,
3459 .inode_link = smack_inode_link,
3460 .inode_unlink = smack_inode_unlink,
3461 .inode_rmdir = smack_inode_rmdir,
3462 .inode_rename = smack_inode_rename,
3463 .inode_permission = smack_inode_permission,
3464 .inode_setattr = smack_inode_setattr,
3465 .inode_getattr = smack_inode_getattr,
3466 .inode_setxattr = smack_inode_setxattr,
3467 .inode_post_setxattr = smack_inode_post_setxattr,
3468 .inode_getxattr = smack_inode_getxattr,
3469 .inode_removexattr = smack_inode_removexattr,
3470 .inode_getsecurity = smack_inode_getsecurity,
3471 .inode_setsecurity = smack_inode_setsecurity,
3472 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003473 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003474
3475 .file_permission = smack_file_permission,
3476 .file_alloc_security = smack_file_alloc_security,
3477 .file_free_security = smack_file_free_security,
3478 .file_ioctl = smack_file_ioctl,
3479 .file_lock = smack_file_lock,
3480 .file_fcntl = smack_file_fcntl,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003481 .file_mmap = smack_file_mmap,
Casey Schauflere114e472008-02-04 22:29:50 -08003482 .file_set_fowner = smack_file_set_fowner,
3483 .file_send_sigiotask = smack_file_send_sigiotask,
3484 .file_receive = smack_file_receive,
3485
Casey Schaufler531f1d42011-09-19 12:41:42 -07003486 .dentry_open = smack_dentry_open,
3487
David Howellsee18d642009-09-02 09:14:21 +01003488 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003489 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003490 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003491 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003492 .kernel_act_as = smack_kernel_act_as,
3493 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003494 .task_setpgid = smack_task_setpgid,
3495 .task_getpgid = smack_task_getpgid,
3496 .task_getsid = smack_task_getsid,
3497 .task_getsecid = smack_task_getsecid,
3498 .task_setnice = smack_task_setnice,
3499 .task_setioprio = smack_task_setioprio,
3500 .task_getioprio = smack_task_getioprio,
3501 .task_setscheduler = smack_task_setscheduler,
3502 .task_getscheduler = smack_task_getscheduler,
3503 .task_movememory = smack_task_movememory,
3504 .task_kill = smack_task_kill,
3505 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003506 .task_to_inode = smack_task_to_inode,
3507
3508 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003509 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003510
3511 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3512 .msg_msg_free_security = smack_msg_msg_free_security,
3513
3514 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3515 .msg_queue_free_security = smack_msg_queue_free_security,
3516 .msg_queue_associate = smack_msg_queue_associate,
3517 .msg_queue_msgctl = smack_msg_queue_msgctl,
3518 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3519 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3520
3521 .shm_alloc_security = smack_shm_alloc_security,
3522 .shm_free_security = smack_shm_free_security,
3523 .shm_associate = smack_shm_associate,
3524 .shm_shmctl = smack_shm_shmctl,
3525 .shm_shmat = smack_shm_shmat,
3526
3527 .sem_alloc_security = smack_sem_alloc_security,
3528 .sem_free_security = smack_sem_free_security,
3529 .sem_associate = smack_sem_associate,
3530 .sem_semctl = smack_sem_semctl,
3531 .sem_semop = smack_sem_semop,
3532
Casey Schauflere114e472008-02-04 22:29:50 -08003533 .d_instantiate = smack_d_instantiate,
3534
3535 .getprocattr = smack_getprocattr,
3536 .setprocattr = smack_setprocattr,
3537
3538 .unix_stream_connect = smack_unix_stream_connect,
3539 .unix_may_send = smack_unix_may_send,
3540
3541 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003542 .socket_connect = smack_socket_connect,
3543 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003544 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3545 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3546 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3547 .sk_alloc_security = smack_sk_alloc_security,
3548 .sk_free_security = smack_sk_free_security,
3549 .sock_graft = smack_sock_graft,
3550 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003551 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003552
Casey Schauflere114e472008-02-04 22:29:50 -08003553 /* key management security hooks */
3554#ifdef CONFIG_KEYS
3555 .key_alloc = smack_key_alloc,
3556 .key_free = smack_key_free,
3557 .key_permission = smack_key_permission,
3558#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003559
3560 /* Audit hooks */
3561#ifdef CONFIG_AUDIT
3562 .audit_rule_init = smack_audit_rule_init,
3563 .audit_rule_known = smack_audit_rule_known,
3564 .audit_rule_match = smack_audit_rule_match,
3565 .audit_rule_free = smack_audit_rule_free,
3566#endif /* CONFIG_AUDIT */
3567
Casey Schauflere114e472008-02-04 22:29:50 -08003568 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003569 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003570 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003571 .inode_notifysecctx = smack_inode_notifysecctx,
3572 .inode_setsecctx = smack_inode_setsecctx,
3573 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003574};
3575
Etienne Basset7198e2e2009-03-24 20:53:24 +01003576
3577static __init void init_smack_know_list(void)
3578{
3579 list_add(&smack_known_huh.list, &smack_known_list);
3580 list_add(&smack_known_hat.list, &smack_known_list);
3581 list_add(&smack_known_star.list, &smack_known_list);
3582 list_add(&smack_known_floor.list, &smack_known_list);
3583 list_add(&smack_known_invalid.list, &smack_known_list);
3584 list_add(&smack_known_web.list, &smack_known_list);
3585}
3586
Casey Schauflere114e472008-02-04 22:29:50 -08003587/**
3588 * smack_init - initialize the smack system
3589 *
3590 * Returns 0
3591 */
3592static __init int smack_init(void)
3593{
David Howellsd84f4f92008-11-14 10:39:23 +11003594 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003595 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003596
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003597 if (!security_module_enable(&smack_ops))
3598 return 0;
3599
3600 tsp = new_task_smack(smack_known_floor.smk_known,
3601 smack_known_floor.smk_known, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003602 if (tsp == NULL)
3603 return -ENOMEM;
3604
Casey Schauflere114e472008-02-04 22:29:50 -08003605 printk(KERN_INFO "Smack: Initializing.\n");
3606
3607 /*
3608 * Set the security state for the initial task.
3609 */
David Howellsd84f4f92008-11-14 10:39:23 +11003610 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003611 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003612
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003613 /* initialize the smack_know_list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003614 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003615 /*
3616 * Initialize locks
3617 */
Casey Schauflere114e472008-02-04 22:29:50 -08003618 spin_lock_init(&smack_known_huh.smk_cipsolock);
3619 spin_lock_init(&smack_known_hat.smk_cipsolock);
3620 spin_lock_init(&smack_known_star.smk_cipsolock);
3621 spin_lock_init(&smack_known_floor.smk_cipsolock);
3622 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3623
3624 /*
3625 * Register with LSM
3626 */
3627 if (register_security(&smack_ops))
3628 panic("smack: Unable to register with kernel.\n");
3629
3630 return 0;
3631}
3632
3633/*
3634 * Smack requires early initialization in order to label
3635 * all processes and objects when they are created.
3636 */
3637security_initcall(smack_init);