blob: 400a5d5cde6183c9bc3c6d348f58b623e2821b46 [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.
12 * Paul Moore <paul.moore@hp.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Casey Schauflere114e472008-02-04 22:29:50 -080014 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2,
17 * as published by the Free Software Foundation.
18 */
19
20#include <linux/xattr.h>
21#include <linux/pagemap.h>
22#include <linux/mount.h>
23#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080024#include <linux/kd.h>
25#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040026#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080027#include <linux/tcp.h>
28#include <linux/udp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030#include <linux/mutex.h>
31#include <linux/pipe_fs_i.h>
32#include <net/netlabel.h>
33#include <net/cipso_ipv4.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100034#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070035#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050036#include <linux/dcache.h>
Casey Schauflere114e472008-02-04 22:29:50 -080037#include "smack.h"
38
David Howellsc69e8d92008-11-14 10:39:19 +110039#define task_security(task) (task_cred_xxx((task), security))
40
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020041#define TRANS_TRUE "TRUE"
42#define TRANS_TRUE_SIZE 4
43
Casey Schauflere114e472008-02-04 22:29:50 -080044/**
45 * smk_fetch - Fetch the smack label from a file.
46 * @ip: a pointer to the inode
47 * @dp: a pointer to the dentry
48 *
49 * Returns a pointer to the master list entry for the Smack label
50 * or NULL if there was no label to fetch.
51 */
Casey Schaufler676dac42010-12-02 06:43:39 -080052static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080053{
54 int rc;
55 char in[SMK_LABELLEN];
56
57 if (ip->i_op->getxattr == NULL)
58 return NULL;
59
Casey Schaufler676dac42010-12-02 06:43:39 -080060 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
Casey Schauflere114e472008-02-04 22:29:50 -080061 if (rc < 0)
62 return NULL;
63
64 return smk_import(in, rc);
65}
66
67/**
68 * new_inode_smack - allocate an inode security blob
69 * @smack: a pointer to the Smack label to use in the blob
70 *
71 * Returns the new blob or NULL if there's no memory available
72 */
73struct inode_smack *new_inode_smack(char *smack)
74{
75 struct inode_smack *isp;
76
77 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
78 if (isp == NULL)
79 return NULL;
80
81 isp->smk_inode = smack;
82 isp->smk_flags = 0;
83 mutex_init(&isp->smk_lock);
84
85 return isp;
86}
87
Casey Schaufler7898e1f2011-01-17 08:05:27 -080088/**
89 * new_task_smack - allocate a task security blob
90 * @smack: a pointer to the Smack label to use in the blob
91 *
92 * Returns the new blob or NULL if there's no memory available
93 */
94static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
95{
96 struct task_smack *tsp;
97
98 tsp = kzalloc(sizeof(struct task_smack), gfp);
99 if (tsp == NULL)
100 return NULL;
101
102 tsp->smk_task = task;
103 tsp->smk_forked = forked;
104 INIT_LIST_HEAD(&tsp->smk_rules);
105 mutex_init(&tsp->smk_rules_lock);
106
107 return tsp;
108}
109
110/**
111 * smk_copy_rules - copy a rule set
112 * @nhead - new rules header pointer
113 * @ohead - old rules header pointer
114 *
115 * Returns 0 on success, -ENOMEM on error
116 */
117static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
118 gfp_t gfp)
119{
120 struct smack_rule *nrp;
121 struct smack_rule *orp;
122 int rc = 0;
123
124 INIT_LIST_HEAD(nhead);
125
126 list_for_each_entry_rcu(orp, ohead, list) {
127 nrp = kzalloc(sizeof(struct smack_rule), gfp);
128 if (nrp == NULL) {
129 rc = -ENOMEM;
130 break;
131 }
132 *nrp = *orp;
133 list_add_rcu(&nrp->list, nhead);
134 }
135 return rc;
136}
137
Casey Schauflere114e472008-02-04 22:29:50 -0800138/*
139 * LSM hooks.
140 * We he, that is fun!
141 */
142
143/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000144 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800145 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -0800146 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -0800147 *
148 * Returns 0 if access is OK, an error code otherwise
149 *
150 * Do the capability checks, and require read and write.
151 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000152static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800153{
154 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200155 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800156 char *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800157
Ingo Molnar9e488582009-05-07 19:26:19 +1000158 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800159 if (rc != 0)
160 return rc;
161
Casey Schaufler676dac42010-12-02 06:43:39 -0800162 tsp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200163 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
164 smk_ad_setfield_u_tsk(&ad, ctp);
165
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800166 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100167 return rc;
168}
Casey Schauflere114e472008-02-04 22:29:50 -0800169
David Howells5cd9c582008-08-14 11:37:28 +0100170/**
171 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
172 * @ptp: parent task pointer
173 *
174 * Returns 0 if access is OK, an error code otherwise
175 *
176 * Do the capability checks, and require read and write.
177 */
178static int smack_ptrace_traceme(struct task_struct *ptp)
179{
180 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200181 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800182 char *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100183
184 rc = cap_ptrace_traceme(ptp);
185 if (rc != 0)
186 return rc;
187
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800188 tsp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200189 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
190 smk_ad_setfield_u_tsk(&ad, ptp);
191
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800192 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800193 return rc;
194}
195
196/**
197 * smack_syslog - Smack approval on syslog
198 * @type: message type
199 *
200 * Require that the task has the floor label
201 *
202 * Returns 0 on success, error code otherwise.
203 */
Eric Paris12b30522010-11-15 18:36:29 -0500204static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800205{
Eric Paris12b30522010-11-15 18:36:29 -0500206 int rc = 0;
Casey Schaufler676dac42010-12-02 06:43:39 -0800207 char *sp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800208
Casey Schauflere114e472008-02-04 22:29:50 -0800209 if (capable(CAP_MAC_OVERRIDE))
210 return 0;
211
212 if (sp != smack_known_floor.smk_known)
213 rc = -EACCES;
214
215 return rc;
216}
217
218
219/*
220 * Superblock Hooks.
221 */
222
223/**
224 * smack_sb_alloc_security - allocate a superblock blob
225 * @sb: the superblock getting the blob
226 *
227 * Returns 0 on success or -ENOMEM on error.
228 */
229static int smack_sb_alloc_security(struct super_block *sb)
230{
231 struct superblock_smack *sbsp;
232
233 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
234
235 if (sbsp == NULL)
236 return -ENOMEM;
237
238 sbsp->smk_root = smack_known_floor.smk_known;
239 sbsp->smk_default = smack_known_floor.smk_known;
240 sbsp->smk_floor = smack_known_floor.smk_known;
241 sbsp->smk_hat = smack_known_hat.smk_known;
242 sbsp->smk_initialized = 0;
243 spin_lock_init(&sbsp->smk_sblock);
244
245 sb->s_security = sbsp;
246
247 return 0;
248}
249
250/**
251 * smack_sb_free_security - free a superblock blob
252 * @sb: the superblock getting the blob
253 *
254 */
255static void smack_sb_free_security(struct super_block *sb)
256{
257 kfree(sb->s_security);
258 sb->s_security = NULL;
259}
260
261/**
262 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800263 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800264 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800265 *
266 * Returns 0 on success or -ENOMEM on error.
267 *
268 * Copy the Smack specific mount options out of the mount
269 * options list.
270 */
Eric Parise0007522008-03-05 10:31:54 -0500271static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800272{
273 char *cp, *commap, *otheropts, *dp;
274
Casey Schauflere114e472008-02-04 22:29:50 -0800275 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
276 if (otheropts == NULL)
277 return -ENOMEM;
278
279 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
280 if (strstr(cp, SMK_FSDEFAULT) == cp)
281 dp = smackopts;
282 else if (strstr(cp, SMK_FSFLOOR) == cp)
283 dp = smackopts;
284 else if (strstr(cp, SMK_FSHAT) == cp)
285 dp = smackopts;
286 else if (strstr(cp, SMK_FSROOT) == cp)
287 dp = smackopts;
288 else
289 dp = otheropts;
290
291 commap = strchr(cp, ',');
292 if (commap != NULL)
293 *commap = '\0';
294
295 if (*dp != '\0')
296 strcat(dp, ",");
297 strcat(dp, cp);
298 }
299
300 strcpy(orig, otheropts);
301 free_page((unsigned long)otheropts);
302
303 return 0;
304}
305
306/**
307 * smack_sb_kern_mount - Smack specific mount processing
308 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100309 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800310 * @data: the smack mount options
311 *
312 * Returns 0 on success, an error code on failure
313 */
James Morris12204e22008-12-19 10:44:42 +1100314static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800315{
316 struct dentry *root = sb->s_root;
317 struct inode *inode = root->d_inode;
318 struct superblock_smack *sp = sb->s_security;
319 struct inode_smack *isp;
320 char *op;
321 char *commap;
322 char *nsp;
323
324 spin_lock(&sp->smk_sblock);
325 if (sp->smk_initialized != 0) {
326 spin_unlock(&sp->smk_sblock);
327 return 0;
328 }
329 sp->smk_initialized = 1;
330 spin_unlock(&sp->smk_sblock);
331
332 for (op = data; op != NULL; op = commap) {
333 commap = strchr(op, ',');
334 if (commap != NULL)
335 *commap++ = '\0';
336
337 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
338 op += strlen(SMK_FSHAT);
339 nsp = smk_import(op, 0);
340 if (nsp != NULL)
341 sp->smk_hat = nsp;
342 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
343 op += strlen(SMK_FSFLOOR);
344 nsp = smk_import(op, 0);
345 if (nsp != NULL)
346 sp->smk_floor = nsp;
347 } else if (strncmp(op, SMK_FSDEFAULT,
348 strlen(SMK_FSDEFAULT)) == 0) {
349 op += strlen(SMK_FSDEFAULT);
350 nsp = smk_import(op, 0);
351 if (nsp != NULL)
352 sp->smk_default = nsp;
353 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
354 op += strlen(SMK_FSROOT);
355 nsp = smk_import(op, 0);
356 if (nsp != NULL)
357 sp->smk_root = nsp;
358 }
359 }
360
361 /*
362 * Initialize the root inode.
363 */
364 isp = inode->i_security;
365 if (isp == NULL)
366 inode->i_security = new_inode_smack(sp->smk_root);
367 else
368 isp->smk_inode = sp->smk_root;
369
370 return 0;
371}
372
373/**
374 * smack_sb_statfs - Smack check on statfs
375 * @dentry: identifies the file system in question
376 *
377 * Returns 0 if current can read the floor of the filesystem,
378 * and error code otherwise
379 */
380static int smack_sb_statfs(struct dentry *dentry)
381{
382 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200383 int rc;
384 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800385
Etienne Bassetecfcc532009-04-08 20:40:06 +0200386 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
387 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
Etienne Bassetecfcc532009-04-08 20:40:06 +0200410 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
411 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;
428
429 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
Al Virode27a5b2010-01-30 15:27:27 -0500430 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_root);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200431 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
Casey Schauflere114e472008-02-04 22:29:50 -0800432
433 sbp = mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200434 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800435}
436
437/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800438 * BPRM hooks
439 */
440
441static int smack_bprm_set_creds(struct linux_binprm *bprm)
442{
443 struct task_smack *tsp = bprm->cred->security;
444 struct inode_smack *isp;
445 struct dentry *dp;
446 int rc;
447
448 rc = cap_bprm_set_creds(bprm);
449 if (rc != 0)
450 return rc;
451
452 if (bprm->cred_prepared)
453 return 0;
454
455 if (bprm->file == NULL || bprm->file->f_dentry == NULL)
456 return 0;
457
458 dp = bprm->file->f_dentry;
459
460 if (dp->d_inode == NULL)
461 return 0;
462
463 isp = dp->d_inode->i_security;
464
465 if (isp->smk_task != NULL)
466 tsp->smk_task = isp->smk_task;
467
468 return 0;
469}
470
471/*
Casey Schauflere114e472008-02-04 22:29:50 -0800472 * Inode hooks
473 */
474
475/**
476 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800477 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800478 *
479 * Returns 0 if it gets a blob, -ENOMEM otherwise
480 */
481static int smack_inode_alloc_security(struct inode *inode)
482{
Casey Schaufler676dac42010-12-02 06:43:39 -0800483 inode->i_security = new_inode_smack(smk_of_current());
Casey Schauflere114e472008-02-04 22:29:50 -0800484 if (inode->i_security == NULL)
485 return -ENOMEM;
486 return 0;
487}
488
489/**
490 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800491 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800492 *
493 * Clears the blob pointer in inode
494 */
495static void smack_inode_free_security(struct inode *inode)
496{
497 kfree(inode->i_security);
498 inode->i_security = NULL;
499}
500
501/**
502 * smack_inode_init_security - copy out the smack from an inode
503 * @inode: the inode
504 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500505 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800506 * @name: where to put the attribute name
507 * @value: where to put the attribute value
508 * @len: where to put the length of the attribute
509 *
510 * Returns 0 if it all works out, -ENOMEM if there's no memory
511 */
512static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -0500513 const struct qstr *qstr, char **name,
514 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800515{
516 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200517 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800518 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800519
520 if (name) {
521 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
522 if (*name == NULL)
523 return -ENOMEM;
524 }
525
526 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800527 rcu_read_lock();
528 may = smk_access_entry(smk_of_current(), dsp, &smack_rule_list);
529 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200530
531 /*
532 * If the access rule allows transmutation and
533 * the directory requests transmutation then
534 * by all means transmute.
535 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800536 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
537 smk_inode_transmutable(dir))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200538 isp = dsp;
539
Casey Schauflere114e472008-02-04 22:29:50 -0800540 *value = kstrdup(isp, GFP_KERNEL);
541 if (*value == NULL)
542 return -ENOMEM;
543 }
544
545 if (len)
546 *len = strlen(isp) + 1;
547
548 return 0;
549}
550
551/**
552 * smack_inode_link - Smack check on link
553 * @old_dentry: the existing object
554 * @dir: unused
555 * @new_dentry: the new object
556 *
557 * Returns 0 if access is permitted, an error code otherwise
558 */
559static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
560 struct dentry *new_dentry)
561{
Casey Schauflere114e472008-02-04 22:29:50 -0800562 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200563 struct smk_audit_info ad;
564 int rc;
565
566 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
567 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800568
569 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200570 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800571
572 if (rc == 0 && new_dentry->d_inode != NULL) {
573 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200574 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
575 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800576 }
577
578 return rc;
579}
580
581/**
582 * smack_inode_unlink - Smack check on inode deletion
583 * @dir: containing directory object
584 * @dentry: file to unlink
585 *
586 * Returns 0 if current can write the containing directory
587 * and the object, error code otherwise
588 */
589static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
590{
591 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200592 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800593 int rc;
594
Etienne Bassetecfcc532009-04-08 20:40:06 +0200595 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
596 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
597
Casey Schauflere114e472008-02-04 22:29:50 -0800598 /*
599 * You need write access to the thing you're unlinking
600 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200601 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
602 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800603 /*
604 * You also need write access to the containing directory
605 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200606 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
607 smk_ad_setfield_u_fs_inode(&ad, dir);
608 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
609 }
Casey Schauflere114e472008-02-04 22:29:50 -0800610 return rc;
611}
612
613/**
614 * smack_inode_rmdir - Smack check on directory deletion
615 * @dir: containing directory object
616 * @dentry: directory to unlink
617 *
618 * Returns 0 if current can write the containing directory
619 * and the directory, error code otherwise
620 */
621static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
622{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200623 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800624 int rc;
625
Etienne Bassetecfcc532009-04-08 20:40:06 +0200626 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
627 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
628
Casey Schauflere114e472008-02-04 22:29:50 -0800629 /*
630 * You need write access to the thing you're removing
631 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200632 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
633 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800634 /*
635 * You also need write access to the containing directory
636 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200637 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
638 smk_ad_setfield_u_fs_inode(&ad, dir);
639 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
640 }
Casey Schauflere114e472008-02-04 22:29:50 -0800641
642 return rc;
643}
644
645/**
646 * smack_inode_rename - Smack check on rename
647 * @old_inode: the old directory
648 * @old_dentry: unused
649 * @new_inode: the new directory
650 * @new_dentry: unused
651 *
652 * Read and write access is required on both the old and
653 * new directories.
654 *
655 * Returns 0 if access is permitted, an error code otherwise
656 */
657static int smack_inode_rename(struct inode *old_inode,
658 struct dentry *old_dentry,
659 struct inode *new_inode,
660 struct dentry *new_dentry)
661{
662 int rc;
663 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200664 struct smk_audit_info ad;
665
666 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
667 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800668
669 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200670 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800671
672 if (rc == 0 && new_dentry->d_inode != NULL) {
673 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200674 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
675 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800676 }
Casey Schauflere114e472008-02-04 22:29:50 -0800677 return rc;
678}
679
680/**
681 * smack_inode_permission - Smack version of permission()
682 * @inode: the inode in question
683 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800684 *
685 * This is the important Smack hook.
686 *
687 * Returns 0 if access is permitted, -EACCES otherwise
688 */
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700689static int smack_inode_permission(struct inode *inode, int mask, unsigned flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800690{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200691 struct smk_audit_info ad;
Eric Parisd09ca732010-07-23 11:43:57 -0400692
693 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800694 /*
695 * No permission to check. Existence test. Yup, it's there.
696 */
697 if (mask == 0)
698 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700699
700 /* May be droppable after audit */
701 if (flags & IPERM_FLAG_RCU)
702 return -ECHILD;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200703 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
704 smk_ad_setfield_u_fs_inode(&ad, inode);
705 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800706}
707
708/**
709 * smack_inode_setattr - Smack check for setting attributes
710 * @dentry: the object
711 * @iattr: for the force flag
712 *
713 * Returns 0 if access is permitted, an error code otherwise
714 */
715static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
716{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200717 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800718 /*
719 * Need to allow for clearing the setuid bit.
720 */
721 if (iattr->ia_valid & ATTR_FORCE)
722 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200723 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
724 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800725
Etienne Bassetecfcc532009-04-08 20:40:06 +0200726 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800727}
728
729/**
730 * smack_inode_getattr - Smack check for getting attributes
731 * @mnt: unused
732 * @dentry: the object
733 *
734 * Returns 0 if access is permitted, an error code otherwise
735 */
736static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
737{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200738 struct smk_audit_info ad;
739
740 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
741 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
742 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
743 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800744}
745
746/**
747 * smack_inode_setxattr - Smack check for setting xattrs
748 * @dentry: the object
749 * @name: name of the attribute
750 * @value: unused
751 * @size: unused
752 * @flags: unused
753 *
754 * This protects the Smack attribute explicitly.
755 *
756 * Returns 0 if access is permitted, an error code otherwise
757 */
David Howells8f0cfa52008-04-29 00:59:41 -0700758static int smack_inode_setxattr(struct dentry *dentry, const char *name,
759 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800760{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200761 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800762 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800763
Casey Schauflerbcdca222008-02-23 15:24:04 -0800764 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
765 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800766 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800767 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
768 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800769 if (!capable(CAP_MAC_ADMIN))
770 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200771 /*
772 * check label validity here so import wont fail on
773 * post_setxattr
774 */
775 if (size == 0 || size >= SMK_LABELLEN ||
776 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400777 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200778 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
779 if (!capable(CAP_MAC_ADMIN))
780 rc = -EPERM;
781 if (size != TRANS_TRUE_SIZE ||
782 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
783 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800784 } else
785 rc = cap_inode_setxattr(dentry, name, value, size, flags);
786
Etienne Bassetecfcc532009-04-08 20:40:06 +0200787 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
788 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
789
Casey Schauflerbcdca222008-02-23 15:24:04 -0800790 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200791 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800792
793 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800794}
795
796/**
797 * smack_inode_post_setxattr - Apply the Smack update approved above
798 * @dentry: object
799 * @name: attribute name
800 * @value: attribute value
801 * @size: attribute size
802 * @flags: unused
803 *
804 * Set the pointer in the inode blob to the entry found
805 * in the master label list.
806 */
David Howells8f0cfa52008-04-29 00:59:41 -0700807static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
808 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800809{
Casey Schauflere114e472008-02-04 22:29:50 -0800810 char *nsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200811 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800812
813 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200814 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800815 if (nsp != NULL)
816 isp->smk_inode = nsp;
817 else
818 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200819 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
820 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800821 if (nsp != NULL)
822 isp->smk_task = nsp;
823 else
824 isp->smk_task = smack_known_invalid.smk_known;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800825 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
826 nsp = smk_import(value, size);
827 if (nsp != NULL)
828 isp->smk_mmap = nsp;
829 else
830 isp->smk_mmap = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200831 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
832 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflere114e472008-02-04 22:29:50 -0800833
834 return;
835}
836
837/*
838 * smack_inode_getxattr - Smack check on getxattr
839 * @dentry: the object
840 * @name: unused
841 *
842 * Returns 0 if access is permitted, an error code otherwise
843 */
David Howells8f0cfa52008-04-29 00:59:41 -0700844static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800845{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200846 struct smk_audit_info ad;
847
848 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
849 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
850
851 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800852}
853
854/*
855 * smack_inode_removexattr - Smack check on removexattr
856 * @dentry: the object
857 * @name: name of the attribute
858 *
859 * Removing the Smack attribute requires CAP_MAC_ADMIN
860 *
861 * Returns 0 if access is permitted, an error code otherwise
862 */
David Howells8f0cfa52008-04-29 00:59:41 -0700863static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800864{
Casey Schaufler676dac42010-12-02 06:43:39 -0800865 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200866 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800867 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800868
Casey Schauflerbcdca222008-02-23 15:24:04 -0800869 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
870 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800871 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200872 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800873 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
874 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800875 if (!capable(CAP_MAC_ADMIN))
876 rc = -EPERM;
877 } else
878 rc = cap_inode_removexattr(dentry, name);
879
Etienne Bassetecfcc532009-04-08 20:40:06 +0200880 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
881 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800882 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200883 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800884
Casey Schaufler676dac42010-12-02 06:43:39 -0800885 if (rc == 0) {
886 isp = dentry->d_inode->i_security;
887 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800888 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800889 }
890
Casey Schauflerbcdca222008-02-23 15:24:04 -0800891 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800892}
893
894/**
895 * smack_inode_getsecurity - get smack xattrs
896 * @inode: the object
897 * @name: attribute name
898 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800899 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800900 *
901 * Returns the size of the attribute or an error code
902 */
903static int smack_inode_getsecurity(const struct inode *inode,
904 const char *name, void **buffer,
905 bool alloc)
906{
907 struct socket_smack *ssp;
908 struct socket *sock;
909 struct super_block *sbp;
910 struct inode *ip = (struct inode *)inode;
911 char *isp;
912 int ilen;
913 int rc = 0;
914
915 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
916 isp = smk_of_inode(inode);
917 ilen = strlen(isp) + 1;
918 *buffer = isp;
919 return ilen;
920 }
921
922 /*
923 * The rest of the Smack xattrs are only on sockets.
924 */
925 sbp = ip->i_sb;
926 if (sbp->s_magic != SOCKFS_MAGIC)
927 return -EOPNOTSUPP;
928
929 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800930 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800931 return -EOPNOTSUPP;
932
933 ssp = sock->sk->sk_security;
934
935 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
936 isp = ssp->smk_in;
937 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
938 isp = ssp->smk_out;
939 else
940 return -EOPNOTSUPP;
941
942 ilen = strlen(isp) + 1;
943 if (rc == 0) {
944 *buffer = isp;
945 rc = ilen;
946 }
947
948 return rc;
949}
950
951
952/**
953 * smack_inode_listsecurity - list the Smack attributes
954 * @inode: the object
955 * @buffer: where they go
956 * @buffer_size: size of buffer
957 *
958 * Returns 0 on success, -EINVAL otherwise
959 */
960static int smack_inode_listsecurity(struct inode *inode, char *buffer,
961 size_t buffer_size)
962{
963 int len = strlen(XATTR_NAME_SMACK);
964
965 if (buffer != NULL && len <= buffer_size) {
966 memcpy(buffer, XATTR_NAME_SMACK, len);
967 return len;
968 }
969 return -EINVAL;
970}
971
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +1000972/**
973 * smack_inode_getsecid - Extract inode's security id
974 * @inode: inode to extract the info from
975 * @secid: where result will be saved
976 */
977static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
978{
979 struct inode_smack *isp = inode->i_security;
980
981 *secid = smack_to_secid(isp->smk_inode);
982}
983
Casey Schauflere114e472008-02-04 22:29:50 -0800984/*
985 * File Hooks
986 */
987
988/**
989 * smack_file_permission - Smack check on file operations
990 * @file: unused
991 * @mask: unused
992 *
993 * Returns 0
994 *
995 * Should access checks be done on each read or write?
996 * UNICOS and SELinux say yes.
997 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
998 *
999 * I'll say no for now. Smack does not do the frequent
1000 * label changing that SELinux does.
1001 */
1002static int smack_file_permission(struct file *file, int mask)
1003{
1004 return 0;
1005}
1006
1007/**
1008 * smack_file_alloc_security - assign a file security blob
1009 * @file: the object
1010 *
1011 * The security blob for a file is a pointer to the master
1012 * label list, so no allocation is done.
1013 *
1014 * Returns 0
1015 */
1016static int smack_file_alloc_security(struct file *file)
1017{
Casey Schaufler676dac42010-12-02 06:43:39 -08001018 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001019 return 0;
1020}
1021
1022/**
1023 * smack_file_free_security - clear a file security blob
1024 * @file: the object
1025 *
1026 * The security blob for a file is a pointer to the master
1027 * label list, so no memory is freed.
1028 */
1029static void smack_file_free_security(struct file *file)
1030{
1031 file->f_security = NULL;
1032}
1033
1034/**
1035 * smack_file_ioctl - Smack check on ioctls
1036 * @file: the object
1037 * @cmd: what to do
1038 * @arg: unused
1039 *
1040 * Relies heavily on the correct use of the ioctl command conventions.
1041 *
1042 * Returns 0 if allowed, error code otherwise
1043 */
1044static int smack_file_ioctl(struct file *file, unsigned int cmd,
1045 unsigned long arg)
1046{
1047 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001048 struct smk_audit_info ad;
1049
1050 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1051 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001052
1053 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001054 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001055
1056 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001057 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001058
1059 return rc;
1060}
1061
1062/**
1063 * smack_file_lock - Smack check on file locking
1064 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001065 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001066 *
1067 * Returns 0 if current has write access, error code otherwise
1068 */
1069static int smack_file_lock(struct file *file, unsigned int cmd)
1070{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001071 struct smk_audit_info ad;
1072
1073 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1074 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
1075 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001076}
1077
1078/**
1079 * smack_file_fcntl - Smack check on fcntl
1080 * @file: the object
1081 * @cmd: what action to check
1082 * @arg: unused
1083 *
1084 * Returns 0 if current has access, error code otherwise
1085 */
1086static int smack_file_fcntl(struct file *file, unsigned int cmd,
1087 unsigned long arg)
1088{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001089 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001090 int rc;
1091
Etienne Bassetecfcc532009-04-08 20:40:06 +02001092 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1093 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1094
Casey Schauflere114e472008-02-04 22:29:50 -08001095 switch (cmd) {
1096 case F_DUPFD:
1097 case F_GETFD:
1098 case F_GETFL:
1099 case F_GETLK:
1100 case F_GETOWN:
1101 case F_GETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001103 break;
1104 case F_SETFD:
1105 case F_SETFL:
1106 case F_SETLK:
1107 case F_SETLKW:
1108 case F_SETOWN:
1109 case F_SETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001110 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001111 break;
1112 default:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001113 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001114 }
1115
1116 return rc;
1117}
1118
1119/**
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001120 * smack_file_mmap :
1121 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1122 * if mapping anonymous memory.
1123 * @file contains the file structure for file to map (may be NULL).
1124 * @reqprot contains the protection requested by the application.
1125 * @prot contains the protection that will be applied by the kernel.
1126 * @flags contains the operational flags.
1127 * Return 0 if permission is granted.
1128 */
1129static int smack_file_mmap(struct file *file,
1130 unsigned long reqprot, unsigned long prot,
1131 unsigned long flags, unsigned long addr,
1132 unsigned long addr_only)
1133{
1134 struct smack_rule *srp;
1135 struct task_smack *tsp;
1136 char *sp;
1137 char *msmack;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001138 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001139 struct inode_smack *isp;
1140 struct dentry *dp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001141 int may;
1142 int mmay;
1143 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001144 int rc;
1145
1146 /* do DAC check on address space usage */
1147 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1148 if (rc || addr_only)
1149 return rc;
1150
1151 if (file == NULL || file->f_dentry == NULL)
1152 return 0;
1153
1154 dp = file->f_dentry;
1155
1156 if (dp->d_inode == NULL)
1157 return 0;
1158
1159 isp = dp->d_inode->i_security;
1160 if (isp->smk_mmap == NULL)
1161 return 0;
1162 msmack = isp->smk_mmap;
1163
1164 tsp = current_security();
1165 sp = smk_of_current();
1166 rc = 0;
1167
1168 rcu_read_lock();
1169 /*
1170 * For each Smack rule associated with the subject
1171 * label verify that the SMACK64MMAP also has access
1172 * to that rule's object label.
1173 *
1174 * Because neither of the labels comes
1175 * from the networking code it is sufficient
1176 * to compare pointers.
1177 */
1178 list_for_each_entry_rcu(srp, &smack_rule_list, list) {
1179 if (srp->smk_subject != sp)
1180 continue;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001181
1182 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 */
1210 mmay = smk_access_entry(msmack, osmack, &smack_rule_list);
1211 if (mmay == -ENOENT) {
1212 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001213 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001214 }
1215 /*
1216 * If there is a local entry it modifies the
1217 * potential access, too.
1218 */
1219 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1220 if (tmay != -ENOENT)
1221 mmay &= tmay;
1222
1223 /*
1224 * If there is any access available to current that is
1225 * not available to a SMACK64MMAP subject
1226 * deny access.
1227 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001228 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001229 rc = -EACCES;
1230 break;
1231 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001232 }
1233
1234 rcu_read_unlock();
1235
1236 return rc;
1237}
1238
1239/**
Casey Schauflere114e472008-02-04 22:29:50 -08001240 * smack_file_set_fowner - set the file security blob value
1241 * @file: object in question
1242 *
1243 * Returns 0
1244 * Further research may be required on this one.
1245 */
1246static int smack_file_set_fowner(struct file *file)
1247{
Casey Schaufler676dac42010-12-02 06:43:39 -08001248 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001249 return 0;
1250}
1251
1252/**
1253 * smack_file_send_sigiotask - Smack on sigio
1254 * @tsk: The target task
1255 * @fown: the object the signal come from
1256 * @signum: unused
1257 *
1258 * Allow a privileged task to get signals even if it shouldn't
1259 *
1260 * Returns 0 if a subject with the object's smack could
1261 * write to the task, an error code otherwise.
1262 */
1263static int smack_file_send_sigiotask(struct task_struct *tsk,
1264 struct fown_struct *fown, int signum)
1265{
1266 struct file *file;
1267 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001268 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001269 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001270
1271 /*
1272 * struct fown_struct is never outside the context of a struct file
1273 */
1274 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001275
Etienne Bassetecfcc532009-04-08 20:40:06 +02001276 /* we don't log here as rc can be overriden */
1277 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001278 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001279 rc = 0;
1280
1281 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1282 smk_ad_setfield_u_tsk(&ad, tsk);
1283 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001284 return rc;
1285}
1286
1287/**
1288 * smack_file_receive - Smack file receive check
1289 * @file: the object
1290 *
1291 * Returns 0 if current has access, error code otherwise
1292 */
1293static int smack_file_receive(struct file *file)
1294{
1295 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001296 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001297
Etienne Bassetecfcc532009-04-08 20:40:06 +02001298 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1299 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001300 /*
1301 * This code relies on bitmasks.
1302 */
1303 if (file->f_mode & FMODE_READ)
1304 may = MAY_READ;
1305 if (file->f_mode & FMODE_WRITE)
1306 may |= MAY_WRITE;
1307
Etienne Bassetecfcc532009-04-08 20:40:06 +02001308 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001309}
1310
1311/*
1312 * Task hooks
1313 */
1314
1315/**
David Howellsee18d642009-09-02 09:14:21 +01001316 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1317 * @new: the new credentials
1318 * @gfp: the atomicity of any memory allocations
1319 *
1320 * Prepare a blank set of credentials for modification. This must allocate all
1321 * the memory the LSM module might require such that cred_transfer() can
1322 * complete without error.
1323 */
1324static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1325{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001326 struct task_smack *tsp;
1327
1328 tsp = new_task_smack(NULL, NULL, gfp);
1329 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001330 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001331
1332 cred->security = tsp;
1333
David Howellsee18d642009-09-02 09:14:21 +01001334 return 0;
1335}
1336
1337
1338/**
David Howellsf1752ee2008-11-14 10:39:17 +11001339 * smack_cred_free - "free" task-level security credentials
1340 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001341 *
Casey Schauflere114e472008-02-04 22:29:50 -08001342 */
David Howellsf1752ee2008-11-14 10:39:17 +11001343static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001344{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001345 struct task_smack *tsp = cred->security;
1346 struct smack_rule *rp;
1347 struct list_head *l;
1348 struct list_head *n;
1349
1350 if (tsp == NULL)
1351 return;
1352 cred->security = NULL;
1353
1354 list_for_each_safe(l, n, &tsp->smk_rules) {
1355 rp = list_entry(l, struct smack_rule, list);
1356 list_del(&rp->list);
1357 kfree(rp);
1358 }
1359 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001360}
1361
1362/**
David Howellsd84f4f92008-11-14 10:39:23 +11001363 * smack_cred_prepare - prepare new set of credentials for modification
1364 * @new: the new credentials
1365 * @old: the original credentials
1366 * @gfp: the atomicity of any memory allocations
1367 *
1368 * Prepare a new set of credentials for modification.
1369 */
1370static int smack_cred_prepare(struct cred *new, const struct cred *old,
1371 gfp_t gfp)
1372{
Casey Schaufler676dac42010-12-02 06:43:39 -08001373 struct task_smack *old_tsp = old->security;
1374 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001375 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001376
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001377 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001378 if (new_tsp == NULL)
1379 return -ENOMEM;
1380
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001381 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1382 if (rc != 0)
1383 return rc;
1384
Casey Schaufler676dac42010-12-02 06:43:39 -08001385 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001386 return 0;
1387}
1388
Randy Dunlap251a2a92009-02-18 11:42:33 -08001389/**
David Howellsee18d642009-09-02 09:14:21 +01001390 * smack_cred_transfer - Transfer the old credentials to the new credentials
1391 * @new: the new credentials
1392 * @old: the original credentials
1393 *
1394 * Fill in a set of blank credentials from another set of credentials.
1395 */
1396static void smack_cred_transfer(struct cred *new, const struct cred *old)
1397{
Casey Schaufler676dac42010-12-02 06:43:39 -08001398 struct task_smack *old_tsp = old->security;
1399 struct task_smack *new_tsp = new->security;
1400
1401 new_tsp->smk_task = old_tsp->smk_task;
1402 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001403 mutex_init(&new_tsp->smk_rules_lock);
1404 INIT_LIST_HEAD(&new_tsp->smk_rules);
1405
1406
1407 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001408}
1409
1410/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001411 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001412 * @new: points to the set of credentials to be modified.
1413 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001414 *
1415 * Set the security data for a kernel service.
1416 */
1417static int smack_kernel_act_as(struct cred *new, u32 secid)
1418{
Casey Schaufler676dac42010-12-02 06:43:39 -08001419 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001420 char *smack = smack_from_secid(secid);
1421
1422 if (smack == NULL)
1423 return -EINVAL;
1424
Casey Schaufler676dac42010-12-02 06:43:39 -08001425 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001426 return 0;
1427}
1428
1429/**
1430 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001431 * @new: points to the set of credentials to be modified
1432 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001433 *
1434 * Set the file creation context in a set of credentials to the same
1435 * as the objective context of the specified inode
1436 */
1437static int smack_kernel_create_files_as(struct cred *new,
1438 struct inode *inode)
1439{
1440 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001441 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001442
Casey Schaufler676dac42010-12-02 06:43:39 -08001443 tsp->smk_forked = isp->smk_inode;
1444 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001445 return 0;
1446}
1447
1448/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001449 * smk_curacc_on_task - helper to log task related access
1450 * @p: the task object
1451 * @access : the access requested
1452 *
1453 * Return 0 if access is permitted
1454 */
1455static int smk_curacc_on_task(struct task_struct *p, int access)
1456{
1457 struct smk_audit_info ad;
1458
1459 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1460 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001461 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001462}
1463
1464/**
Casey Schauflere114e472008-02-04 22:29:50 -08001465 * smack_task_setpgid - Smack check on setting pgid
1466 * @p: the task object
1467 * @pgid: unused
1468 *
1469 * Return 0 if write access is permitted
1470 */
1471static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1472{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001473 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001474}
1475
1476/**
1477 * smack_task_getpgid - Smack access check for getpgid
1478 * @p: the object task
1479 *
1480 * Returns 0 if current can read the object task, error code otherwise
1481 */
1482static int smack_task_getpgid(struct task_struct *p)
1483{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001484 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001485}
1486
1487/**
1488 * smack_task_getsid - Smack access check for getsid
1489 * @p: the object task
1490 *
1491 * Returns 0 if current can read the object task, error code otherwise
1492 */
1493static int smack_task_getsid(struct task_struct *p)
1494{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001495 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001496}
1497
1498/**
1499 * smack_task_getsecid - get the secid of the task
1500 * @p: the object task
1501 * @secid: where to put the result
1502 *
1503 * Sets the secid to contain a u32 version of the smack label.
1504 */
1505static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1506{
Casey Schaufler676dac42010-12-02 06:43:39 -08001507 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001508}
1509
1510/**
1511 * smack_task_setnice - Smack check on setting nice
1512 * @p: the task object
1513 * @nice: unused
1514 *
1515 * Return 0 if write access is permitted
1516 */
1517static int smack_task_setnice(struct task_struct *p, int nice)
1518{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001519 int rc;
1520
1521 rc = cap_task_setnice(p, nice);
1522 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001523 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001524 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001525}
1526
1527/**
1528 * smack_task_setioprio - Smack check on setting ioprio
1529 * @p: the task object
1530 * @ioprio: unused
1531 *
1532 * Return 0 if write access is permitted
1533 */
1534static int smack_task_setioprio(struct task_struct *p, int ioprio)
1535{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001536 int rc;
1537
1538 rc = cap_task_setioprio(p, ioprio);
1539 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001540 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001541 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001542}
1543
1544/**
1545 * smack_task_getioprio - Smack check on reading ioprio
1546 * @p: the task object
1547 *
1548 * Return 0 if read access is permitted
1549 */
1550static int smack_task_getioprio(struct task_struct *p)
1551{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001552 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001553}
1554
1555/**
1556 * smack_task_setscheduler - Smack check on setting scheduler
1557 * @p: the task object
1558 * @policy: unused
1559 * @lp: unused
1560 *
1561 * Return 0 if read access is permitted
1562 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001563static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001564{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001565 int rc;
1566
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001567 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001568 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001569 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001570 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001571}
1572
1573/**
1574 * smack_task_getscheduler - Smack check on reading scheduler
1575 * @p: the task object
1576 *
1577 * Return 0 if read access is permitted
1578 */
1579static int smack_task_getscheduler(struct task_struct *p)
1580{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001581 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001582}
1583
1584/**
1585 * smack_task_movememory - Smack check on moving memory
1586 * @p: the task object
1587 *
1588 * Return 0 if write access is permitted
1589 */
1590static int smack_task_movememory(struct task_struct *p)
1591{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001592 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001593}
1594
1595/**
1596 * smack_task_kill - Smack check on signal delivery
1597 * @p: the task object
1598 * @info: unused
1599 * @sig: unused
1600 * @secid: identifies the smack to use in lieu of current's
1601 *
1602 * Return 0 if write access is permitted
1603 *
1604 * The secid behavior is an artifact of an SELinux hack
1605 * in the USB code. Someday it may go away.
1606 */
1607static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1608 int sig, u32 secid)
1609{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001610 struct smk_audit_info ad;
1611
1612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1613 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001614 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001615 * Sending a signal requires that the sender
1616 * can write the receiver.
1617 */
1618 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001619 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1620 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001621 /*
1622 * If the secid isn't 0 we're dealing with some USB IO
1623 * specific behavior. This is not clean. For one thing
1624 * we can't take privilege into account.
1625 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001626 return smk_access(smack_from_secid(secid),
1627 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001628}
1629
1630/**
1631 * smack_task_wait - Smack access check for waiting
1632 * @p: task to wait for
1633 *
1634 * Returns 0 if current can wait for p, error code otherwise
1635 */
1636static int smack_task_wait(struct task_struct *p)
1637{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001638 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08001639 char *sp = smk_of_current();
1640 char *tsp = smk_of_forked(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001641 int rc;
1642
Etienne Bassetecfcc532009-04-08 20:40:06 +02001643 /* we don't log here, we can be overriden */
Casey Schaufler676dac42010-12-02 06:43:39 -08001644 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001645 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001646 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001647
1648 /*
1649 * Allow the operation to succeed if either task
1650 * has privilege to perform operations that might
1651 * account for the smack labels having gotten to
1652 * be different in the first place.
1653 *
David Howells5cd9c582008-08-14 11:37:28 +01001654 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001655 * control ideal, taking the object's privilege
1656 * state into account in the decision as well as
1657 * the smack value.
1658 */
David Howells5cd9c582008-08-14 11:37:28 +01001659 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001660 rc = 0;
1661 /* we log only if we didn't get overriden */
1662 out_log:
1663 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1664 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001665 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001666 return rc;
1667}
1668
1669/**
1670 * smack_task_to_inode - copy task smack into the inode blob
1671 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001672 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001673 *
1674 * Sets the smack pointer in the inode security blob
1675 */
1676static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1677{
1678 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001679 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001680}
1681
1682/*
1683 * Socket hooks.
1684 */
1685
1686/**
1687 * smack_sk_alloc_security - Allocate a socket blob
1688 * @sk: the socket
1689 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001690 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001691 *
1692 * Assign Smack pointers to current
1693 *
1694 * Returns 0 on success, -ENOMEM is there's no memory
1695 */
1696static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1697{
Casey Schaufler676dac42010-12-02 06:43:39 -08001698 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001699 struct socket_smack *ssp;
1700
1701 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1702 if (ssp == NULL)
1703 return -ENOMEM;
1704
1705 ssp->smk_in = csp;
1706 ssp->smk_out = csp;
1707 ssp->smk_packet[0] = '\0';
1708
1709 sk->sk_security = ssp;
1710
1711 return 0;
1712}
1713
1714/**
1715 * smack_sk_free_security - Free a socket blob
1716 * @sk: the socket
1717 *
1718 * Clears the blob pointer
1719 */
1720static void smack_sk_free_security(struct sock *sk)
1721{
1722 kfree(sk->sk_security);
1723}
1724
1725/**
Paul Moore07feee82009-03-27 17:10:54 -04001726* smack_host_label - check host based restrictions
1727* @sip: the object end
1728*
1729* looks for host based access restrictions
1730*
1731* This version will only be appropriate for really small sets of single label
1732* hosts. The caller is responsible for ensuring that the RCU read lock is
1733* taken before calling this function.
1734*
1735* Returns the label of the far end or NULL if it's not special.
1736*/
1737static char *smack_host_label(struct sockaddr_in *sip)
1738{
1739 struct smk_netlbladdr *snp;
1740 struct in_addr *siap = &sip->sin_addr;
1741
1742 if (siap->s_addr == 0)
1743 return NULL;
1744
1745 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1746 /*
1747 * we break after finding the first match because
1748 * the list is sorted from longest to shortest mask
1749 * so we have found the most specific match
1750 */
1751 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001752 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1753 /* we have found the special CIPSO option */
1754 if (snp->smk_label == smack_cipso_option)
1755 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001756 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001757 }
Paul Moore07feee82009-03-27 17:10:54 -04001758
1759 return NULL;
1760}
1761
1762/**
Casey Schauflere114e472008-02-04 22:29:50 -08001763 * smack_set_catset - convert a capset to netlabel mls categories
1764 * @catset: the Smack categories
1765 * @sap: where to put the netlabel categories
1766 *
1767 * Allocates and fills attr.mls.cat
1768 */
1769static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1770{
1771 unsigned char *cp;
1772 unsigned char m;
1773 int cat;
1774 int rc;
1775 int byte;
1776
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001777 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001778 return;
1779
1780 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1781 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1782 sap->attr.mls.cat->startbit = 0;
1783
1784 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1785 for (m = 0x80; m != 0; m >>= 1, cat++) {
1786 if ((m & *cp) == 0)
1787 continue;
1788 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1789 cat, GFP_ATOMIC);
1790 }
1791}
1792
1793/**
1794 * smack_to_secattr - fill a secattr from a smack value
1795 * @smack: the smack value
1796 * @nlsp: where the result goes
1797 *
1798 * Casey says that CIPSO is good enough for now.
1799 * It can be used to effect.
1800 * It can also be abused to effect when necessary.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001801 * Apologies to the TSIG group in general and GW in particular.
Casey Schauflere114e472008-02-04 22:29:50 -08001802 */
1803static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1804{
1805 struct smack_cipso cipso;
1806 int rc;
1807
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001808 nlsp->domain = smack;
1809 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001810
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001811 rc = smack_to_cipso(smack, &cipso);
1812 if (rc == 0) {
1813 nlsp->attr.mls.lvl = cipso.smk_level;
1814 smack_set_catset(cipso.smk_catset, nlsp);
1815 } else {
1816 nlsp->attr.mls.lvl = smack_cipso_direct;
1817 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001818 }
1819}
1820
1821/**
1822 * smack_netlabel - Set the secattr on a socket
1823 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001824 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001825 *
1826 * Convert the outbound smack value (smk_out) to a
1827 * secattr and attach it to the socket.
1828 *
1829 * Returns 0 on success or an error code
1830 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001831static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001832{
Paul Moore07feee82009-03-27 17:10:54 -04001833 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001834 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001835 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001836
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001837 /*
1838 * Usually the netlabel code will handle changing the
1839 * packet labeling based on the label.
1840 * The case of a single label host is different, because
1841 * a single label host should never get a labeled packet
1842 * even though the label is usually associated with a packet
1843 * label.
1844 */
1845 local_bh_disable();
1846 bh_lock_sock_nested(sk);
1847
1848 if (ssp->smk_out == smack_net_ambient ||
1849 labeled == SMACK_UNLABELED_SOCKET)
1850 netlbl_sock_delattr(sk);
1851 else {
1852 netlbl_secattr_init(&secattr);
1853 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001854 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001855 netlbl_secattr_destroy(&secattr);
1856 }
1857
1858 bh_unlock_sock(sk);
1859 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001860
Casey Schauflere114e472008-02-04 22:29:50 -08001861 return rc;
1862}
1863
1864/**
Paul Moore07feee82009-03-27 17:10:54 -04001865 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1866 * @sk: the socket
1867 * @sap: the destination address
1868 *
1869 * Set the correct secattr for the given socket based on the destination
1870 * address and perform any outbound access checks needed.
1871 *
1872 * Returns 0 on success or an error code.
1873 *
1874 */
1875static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1876{
1877 int rc;
1878 int sk_lbl;
1879 char *hostsp;
1880 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001881 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001882
1883 rcu_read_lock();
1884 hostsp = smack_host_label(sap);
1885 if (hostsp != NULL) {
1886 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001887#ifdef CONFIG_AUDIT
1888 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1889 ad.a.u.net.family = sap->sin_family;
1890 ad.a.u.net.dport = sap->sin_port;
1891 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1892#endif
1893 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001894 } else {
1895 sk_lbl = SMACK_CIPSO_SOCKET;
1896 rc = 0;
1897 }
1898 rcu_read_unlock();
1899 if (rc != 0)
1900 return rc;
1901
1902 return smack_netlabel(sk, sk_lbl);
1903}
1904
1905/**
Casey Schauflere114e472008-02-04 22:29:50 -08001906 * smack_inode_setsecurity - set smack xattrs
1907 * @inode: the object
1908 * @name: attribute name
1909 * @value: attribute value
1910 * @size: size of the attribute
1911 * @flags: unused
1912 *
1913 * Sets the named attribute in the appropriate blob
1914 *
1915 * Returns 0 on success, or an error code
1916 */
1917static int smack_inode_setsecurity(struct inode *inode, const char *name,
1918 const void *value, size_t size, int flags)
1919{
1920 char *sp;
1921 struct inode_smack *nsp = inode->i_security;
1922 struct socket_smack *ssp;
1923 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001924 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001925
Etienne Basset43031542009-03-27 17:11:01 -04001926 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001927 return -EACCES;
1928
1929 sp = smk_import(value, size);
1930 if (sp == NULL)
1931 return -EINVAL;
1932
1933 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1934 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04001935 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08001936 return 0;
1937 }
1938 /*
1939 * The rest of the Smack xattrs are only on sockets.
1940 */
1941 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1942 return -EOPNOTSUPP;
1943
1944 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001945 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001946 return -EOPNOTSUPP;
1947
1948 ssp = sock->sk->sk_security;
1949
1950 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1951 ssp->smk_in = sp;
1952 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1953 ssp->smk_out = sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08001954 if (sock->sk->sk_family != PF_UNIX) {
1955 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1956 if (rc != 0)
1957 printk(KERN_WARNING
1958 "Smack: \"%s\" netlbl error %d.\n",
1959 __func__, -rc);
1960 }
Casey Schauflere114e472008-02-04 22:29:50 -08001961 } else
1962 return -EOPNOTSUPP;
1963
1964 return 0;
1965}
1966
1967/**
1968 * smack_socket_post_create - finish socket setup
1969 * @sock: the socket
1970 * @family: protocol family
1971 * @type: unused
1972 * @protocol: unused
1973 * @kern: unused
1974 *
1975 * Sets the netlabel information on the socket
1976 *
1977 * Returns 0 on success, and error code otherwise
1978 */
1979static int smack_socket_post_create(struct socket *sock, int family,
1980 int type, int protocol, int kern)
1981{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001982 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001983 return 0;
1984 /*
1985 * Set the outbound netlbl.
1986 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001987 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1988}
1989
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001990/**
1991 * smack_socket_connect - connect access check
1992 * @sock: the socket
1993 * @sap: the other end
1994 * @addrlen: size of sap
1995 *
1996 * Verifies that a connection may be possible
1997 *
1998 * Returns 0 on success, and error code otherwise
1999 */
2000static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2001 int addrlen)
2002{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002003 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2004 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002005 if (addrlen < sizeof(struct sockaddr_in))
2006 return -EINVAL;
2007
Paul Moore07feee82009-03-27 17:10:54 -04002008 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08002009}
2010
2011/**
2012 * smack_flags_to_may - convert S_ to MAY_ values
2013 * @flags: the S_ value
2014 *
2015 * Returns the equivalent MAY_ value
2016 */
2017static int smack_flags_to_may(int flags)
2018{
2019 int may = 0;
2020
2021 if (flags & S_IRUGO)
2022 may |= MAY_READ;
2023 if (flags & S_IWUGO)
2024 may |= MAY_WRITE;
2025 if (flags & S_IXUGO)
2026 may |= MAY_EXEC;
2027
2028 return may;
2029}
2030
2031/**
2032 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2033 * @msg: the object
2034 *
2035 * Returns 0
2036 */
2037static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2038{
Casey Schaufler676dac42010-12-02 06:43:39 -08002039 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002040 return 0;
2041}
2042
2043/**
2044 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2045 * @msg: the object
2046 *
2047 * Clears the blob pointer
2048 */
2049static void smack_msg_msg_free_security(struct msg_msg *msg)
2050{
2051 msg->security = NULL;
2052}
2053
2054/**
2055 * smack_of_shm - the smack pointer for the shm
2056 * @shp: the object
2057 *
2058 * Returns a pointer to the smack value
2059 */
2060static char *smack_of_shm(struct shmid_kernel *shp)
2061{
2062 return (char *)shp->shm_perm.security;
2063}
2064
2065/**
2066 * smack_shm_alloc_security - Set the security blob for shm
2067 * @shp: the object
2068 *
2069 * Returns 0
2070 */
2071static int smack_shm_alloc_security(struct shmid_kernel *shp)
2072{
2073 struct kern_ipc_perm *isp = &shp->shm_perm;
2074
Casey Schaufler676dac42010-12-02 06:43:39 -08002075 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002076 return 0;
2077}
2078
2079/**
2080 * smack_shm_free_security - Clear the security blob for shm
2081 * @shp: the object
2082 *
2083 * Clears the blob pointer
2084 */
2085static void smack_shm_free_security(struct shmid_kernel *shp)
2086{
2087 struct kern_ipc_perm *isp = &shp->shm_perm;
2088
2089 isp->security = NULL;
2090}
2091
2092/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002093 * smk_curacc_shm : check if current has access on shm
2094 * @shp : the object
2095 * @access : access requested
2096 *
2097 * Returns 0 if current has the requested access, error code otherwise
2098 */
2099static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2100{
2101 char *ssp = smack_of_shm(shp);
2102 struct smk_audit_info ad;
2103
2104#ifdef CONFIG_AUDIT
2105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2106 ad.a.u.ipc_id = shp->shm_perm.id;
2107#endif
2108 return smk_curacc(ssp, access, &ad);
2109}
2110
2111/**
Casey Schauflere114e472008-02-04 22:29:50 -08002112 * smack_shm_associate - Smack access check for shm
2113 * @shp: the object
2114 * @shmflg: access requested
2115 *
2116 * Returns 0 if current has the requested access, error code otherwise
2117 */
2118static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2119{
Casey Schauflere114e472008-02-04 22:29:50 -08002120 int may;
2121
2122 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002123 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002124}
2125
2126/**
2127 * smack_shm_shmctl - Smack access check for shm
2128 * @shp: the object
2129 * @cmd: what it wants to do
2130 *
2131 * Returns 0 if current has the requested access, error code otherwise
2132 */
2133static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2134{
Casey Schauflere114e472008-02-04 22:29:50 -08002135 int may;
2136
2137 switch (cmd) {
2138 case IPC_STAT:
2139 case SHM_STAT:
2140 may = MAY_READ;
2141 break;
2142 case IPC_SET:
2143 case SHM_LOCK:
2144 case SHM_UNLOCK:
2145 case IPC_RMID:
2146 may = MAY_READWRITE;
2147 break;
2148 case IPC_INFO:
2149 case SHM_INFO:
2150 /*
2151 * System level information.
2152 */
2153 return 0;
2154 default:
2155 return -EINVAL;
2156 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002157 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002158}
2159
2160/**
2161 * smack_shm_shmat - Smack access for shmat
2162 * @shp: the object
2163 * @shmaddr: unused
2164 * @shmflg: access requested
2165 *
2166 * Returns 0 if current has the requested access, error code otherwise
2167 */
2168static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2169 int shmflg)
2170{
Casey Schauflere114e472008-02-04 22:29:50 -08002171 int may;
2172
2173 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002174 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002175}
2176
2177/**
2178 * smack_of_sem - the smack pointer for the sem
2179 * @sma: the object
2180 *
2181 * Returns a pointer to the smack value
2182 */
2183static char *smack_of_sem(struct sem_array *sma)
2184{
2185 return (char *)sma->sem_perm.security;
2186}
2187
2188/**
2189 * smack_sem_alloc_security - Set the security blob for sem
2190 * @sma: the object
2191 *
2192 * Returns 0
2193 */
2194static int smack_sem_alloc_security(struct sem_array *sma)
2195{
2196 struct kern_ipc_perm *isp = &sma->sem_perm;
2197
Casey Schaufler676dac42010-12-02 06:43:39 -08002198 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002199 return 0;
2200}
2201
2202/**
2203 * smack_sem_free_security - Clear the security blob for sem
2204 * @sma: the object
2205 *
2206 * Clears the blob pointer
2207 */
2208static void smack_sem_free_security(struct sem_array *sma)
2209{
2210 struct kern_ipc_perm *isp = &sma->sem_perm;
2211
2212 isp->security = NULL;
2213}
2214
2215/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002216 * smk_curacc_sem : check if current has access on sem
2217 * @sma : the object
2218 * @access : access requested
2219 *
2220 * Returns 0 if current has the requested access, error code otherwise
2221 */
2222static int smk_curacc_sem(struct sem_array *sma, int access)
2223{
2224 char *ssp = smack_of_sem(sma);
2225 struct smk_audit_info ad;
2226
2227#ifdef CONFIG_AUDIT
2228 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2229 ad.a.u.ipc_id = sma->sem_perm.id;
2230#endif
2231 return smk_curacc(ssp, access, &ad);
2232}
2233
2234/**
Casey Schauflere114e472008-02-04 22:29:50 -08002235 * smack_sem_associate - Smack access check for sem
2236 * @sma: the object
2237 * @semflg: access requested
2238 *
2239 * Returns 0 if current has the requested access, error code otherwise
2240 */
2241static int smack_sem_associate(struct sem_array *sma, int semflg)
2242{
Casey Schauflere114e472008-02-04 22:29:50 -08002243 int may;
2244
2245 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002246 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002247}
2248
2249/**
2250 * smack_sem_shmctl - Smack access check for sem
2251 * @sma: the object
2252 * @cmd: what it wants to do
2253 *
2254 * Returns 0 if current has the requested access, error code otherwise
2255 */
2256static int smack_sem_semctl(struct sem_array *sma, int cmd)
2257{
Casey Schauflere114e472008-02-04 22:29:50 -08002258 int may;
2259
2260 switch (cmd) {
2261 case GETPID:
2262 case GETNCNT:
2263 case GETZCNT:
2264 case GETVAL:
2265 case GETALL:
2266 case IPC_STAT:
2267 case SEM_STAT:
2268 may = MAY_READ;
2269 break;
2270 case SETVAL:
2271 case SETALL:
2272 case IPC_RMID:
2273 case IPC_SET:
2274 may = MAY_READWRITE;
2275 break;
2276 case IPC_INFO:
2277 case SEM_INFO:
2278 /*
2279 * System level information
2280 */
2281 return 0;
2282 default:
2283 return -EINVAL;
2284 }
2285
Etienne Bassetecfcc532009-04-08 20:40:06 +02002286 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002287}
2288
2289/**
2290 * smack_sem_semop - Smack checks of semaphore operations
2291 * @sma: the object
2292 * @sops: unused
2293 * @nsops: unused
2294 * @alter: unused
2295 *
2296 * Treated as read and write in all cases.
2297 *
2298 * Returns 0 if access is allowed, error code otherwise
2299 */
2300static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2301 unsigned nsops, int alter)
2302{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002303 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002304}
2305
2306/**
2307 * smack_msg_alloc_security - Set the security blob for msg
2308 * @msq: the object
2309 *
2310 * Returns 0
2311 */
2312static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2313{
2314 struct kern_ipc_perm *kisp = &msq->q_perm;
2315
Casey Schaufler676dac42010-12-02 06:43:39 -08002316 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002317 return 0;
2318}
2319
2320/**
2321 * smack_msg_free_security - Clear the security blob for msg
2322 * @msq: the object
2323 *
2324 * Clears the blob pointer
2325 */
2326static void smack_msg_queue_free_security(struct msg_queue *msq)
2327{
2328 struct kern_ipc_perm *kisp = &msq->q_perm;
2329
2330 kisp->security = NULL;
2331}
2332
2333/**
2334 * smack_of_msq - the smack pointer for the msq
2335 * @msq: the object
2336 *
2337 * Returns a pointer to the smack value
2338 */
2339static char *smack_of_msq(struct msg_queue *msq)
2340{
2341 return (char *)msq->q_perm.security;
2342}
2343
2344/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002345 * smk_curacc_msq : helper to check if current has access on msq
2346 * @msq : the msq
2347 * @access : access requested
2348 *
2349 * return 0 if current has access, error otherwise
2350 */
2351static int smk_curacc_msq(struct msg_queue *msq, int access)
2352{
2353 char *msp = smack_of_msq(msq);
2354 struct smk_audit_info ad;
2355
2356#ifdef CONFIG_AUDIT
2357 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2358 ad.a.u.ipc_id = msq->q_perm.id;
2359#endif
2360 return smk_curacc(msp, access, &ad);
2361}
2362
2363/**
Casey Schauflere114e472008-02-04 22:29:50 -08002364 * smack_msg_queue_associate - Smack access check for msg_queue
2365 * @msq: the object
2366 * @msqflg: access requested
2367 *
2368 * Returns 0 if current has the requested access, error code otherwise
2369 */
2370static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2371{
Casey Schauflere114e472008-02-04 22:29:50 -08002372 int may;
2373
2374 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002375 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002376}
2377
2378/**
2379 * smack_msg_queue_msgctl - Smack access check for msg_queue
2380 * @msq: the object
2381 * @cmd: what it wants to do
2382 *
2383 * Returns 0 if current has the requested access, error code otherwise
2384 */
2385static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2386{
Casey Schauflere114e472008-02-04 22:29:50 -08002387 int may;
2388
2389 switch (cmd) {
2390 case IPC_STAT:
2391 case MSG_STAT:
2392 may = MAY_READ;
2393 break;
2394 case IPC_SET:
2395 case IPC_RMID:
2396 may = MAY_READWRITE;
2397 break;
2398 case IPC_INFO:
2399 case MSG_INFO:
2400 /*
2401 * System level information
2402 */
2403 return 0;
2404 default:
2405 return -EINVAL;
2406 }
2407
Etienne Bassetecfcc532009-04-08 20:40:06 +02002408 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002409}
2410
2411/**
2412 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2413 * @msq: the object
2414 * @msg: unused
2415 * @msqflg: access requested
2416 *
2417 * Returns 0 if current has the requested access, error code otherwise
2418 */
2419static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2420 int msqflg)
2421{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002422 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002423
Etienne Bassetecfcc532009-04-08 20:40:06 +02002424 may = smack_flags_to_may(msqflg);
2425 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002426}
2427
2428/**
2429 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2430 * @msq: the object
2431 * @msg: unused
2432 * @target: unused
2433 * @type: unused
2434 * @mode: unused
2435 *
2436 * Returns 0 if current has read and write access, error code otherwise
2437 */
2438static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2439 struct task_struct *target, long type, int mode)
2440{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002441 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002442}
2443
2444/**
2445 * smack_ipc_permission - Smack access for ipc_permission()
2446 * @ipp: the object permissions
2447 * @flag: access requested
2448 *
2449 * Returns 0 if current has read and write access, error code otherwise
2450 */
2451static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2452{
2453 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002454 int may = smack_flags_to_may(flag);
2455 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002456
Etienne Bassetecfcc532009-04-08 20:40:06 +02002457#ifdef CONFIG_AUDIT
2458 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2459 ad.a.u.ipc_id = ipp->id;
2460#endif
2461 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002462}
2463
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002464/**
2465 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002466 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002467 * @secid: where result will be saved
2468 */
2469static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2470{
2471 char *smack = ipp->security;
2472
2473 *secid = smack_to_secid(smack);
2474}
2475
Casey Schauflere114e472008-02-04 22:29:50 -08002476/**
2477 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002478 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002479 * @inode: the object
2480 *
2481 * Set the inode's security blob if it hasn't been done already.
2482 */
2483static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2484{
2485 struct super_block *sbp;
2486 struct superblock_smack *sbsp;
2487 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002488 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002489 char *fetched;
2490 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002491 char trattr[TRANS_TRUE_SIZE];
2492 int transflag = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002493 struct dentry *dp;
2494
2495 if (inode == NULL)
2496 return;
2497
2498 isp = inode->i_security;
2499
2500 mutex_lock(&isp->smk_lock);
2501 /*
2502 * If the inode is already instantiated
2503 * take the quick way out
2504 */
2505 if (isp->smk_flags & SMK_INODE_INSTANT)
2506 goto unlockandout;
2507
2508 sbp = inode->i_sb;
2509 sbsp = sbp->s_security;
2510 /*
2511 * We're going to use the superblock default label
2512 * if there's no label on the file.
2513 */
2514 final = sbsp->smk_default;
2515
2516 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002517 * If this is the root inode the superblock
2518 * may be in the process of initialization.
2519 * If that is the case use the root value out
2520 * of the superblock.
2521 */
2522 if (opt_dentry->d_parent == opt_dentry) {
2523 isp->smk_inode = sbsp->smk_root;
2524 isp->smk_flags |= SMK_INODE_INSTANT;
2525 goto unlockandout;
2526 }
2527
2528 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002529 * This is pretty hackish.
2530 * Casey says that we shouldn't have to do
2531 * file system specific code, but it does help
2532 * with keeping it simple.
2533 */
2534 switch (sbp->s_magic) {
2535 case SMACK_MAGIC:
2536 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002537 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002538 * that the smack file system doesn't do
2539 * extended attributes.
2540 */
2541 final = smack_known_star.smk_known;
2542 break;
2543 case PIPEFS_MAGIC:
2544 /*
2545 * Casey says pipes are easy (?)
2546 */
2547 final = smack_known_star.smk_known;
2548 break;
2549 case DEVPTS_SUPER_MAGIC:
2550 /*
2551 * devpts seems content with the label of the task.
2552 * Programs that change smack have to treat the
2553 * pty with respect.
2554 */
2555 final = csp;
2556 break;
2557 case SOCKFS_MAGIC:
2558 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002559 * Socket access is controlled by the socket
2560 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002561 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002562 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002563 break;
2564 case PROC_SUPER_MAGIC:
2565 /*
2566 * Casey says procfs appears not to care.
2567 * The superblock default suffices.
2568 */
2569 break;
2570 case TMPFS_MAGIC:
2571 /*
2572 * Device labels should come from the filesystem,
2573 * but watch out, because they're volitile,
2574 * getting recreated on every reboot.
2575 */
2576 final = smack_known_star.smk_known;
2577 /*
2578 * No break.
2579 *
2580 * If a smack value has been set we want to use it,
2581 * but since tmpfs isn't giving us the opportunity
2582 * to set mount options simulate setting the
2583 * superblock default.
2584 */
2585 default:
2586 /*
2587 * This isn't an understood special case.
2588 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002589 */
2590
2591 /*
2592 * UNIX domain sockets use lower level socket data.
2593 */
2594 if (S_ISSOCK(inode->i_mode)) {
2595 final = smack_known_star.smk_known;
2596 break;
2597 }
2598 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002599 * No xattr support means, alas, no SMACK label.
2600 * Use the aforeapplied default.
2601 * It would be curious if the label of the task
2602 * does not match that assigned.
2603 */
2604 if (inode->i_op->getxattr == NULL)
2605 break;
2606 /*
2607 * Get the dentry for xattr.
2608 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002609 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002610 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002611 if (fetched != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -08002612 final = fetched;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002613 if (S_ISDIR(inode->i_mode)) {
2614 trattr[0] = '\0';
2615 inode->i_op->getxattr(dp,
2616 XATTR_NAME_SMACKTRANSMUTE,
2617 trattr, TRANS_TRUE_SIZE);
2618 if (strncmp(trattr, TRANS_TRUE,
2619 TRANS_TRUE_SIZE) == 0)
2620 transflag = SMK_INODE_TRANSMUTE;
2621 }
2622 }
2623 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002624 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002625
Casey Schauflere114e472008-02-04 22:29:50 -08002626 dput(dp);
2627 break;
2628 }
2629
2630 if (final == NULL)
2631 isp->smk_inode = csp;
2632 else
2633 isp->smk_inode = final;
2634
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002635 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002636
2637unlockandout:
2638 mutex_unlock(&isp->smk_lock);
2639 return;
2640}
2641
2642/**
2643 * smack_getprocattr - Smack process attribute access
2644 * @p: the object task
2645 * @name: the name of the attribute in /proc/.../attr
2646 * @value: where to put the result
2647 *
2648 * Places a copy of the task Smack into value
2649 *
2650 * Returns the length of the smack label or an error code
2651 */
2652static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2653{
2654 char *cp;
2655 int slen;
2656
2657 if (strcmp(name, "current") != 0)
2658 return -EINVAL;
2659
Casey Schaufler676dac42010-12-02 06:43:39 -08002660 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002661 if (cp == NULL)
2662 return -ENOMEM;
2663
2664 slen = strlen(cp);
2665 *value = cp;
2666 return slen;
2667}
2668
2669/**
2670 * smack_setprocattr - Smack process attribute setting
2671 * @p: the object task
2672 * @name: the name of the attribute in /proc/.../attr
2673 * @value: the value to set
2674 * @size: the size of the value
2675 *
2676 * Sets the Smack value of the task. Only setting self
2677 * is permitted and only with privilege
2678 *
2679 * Returns the length of the smack label or an error code
2680 */
2681static int smack_setprocattr(struct task_struct *p, char *name,
2682 void *value, size_t size)
2683{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002684 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002685 struct task_smack *tsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002686 struct task_smack *oldtsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002687 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002688 char *newsmack;
2689
Casey Schauflere114e472008-02-04 22:29:50 -08002690 /*
2691 * Changing another process' Smack value is too dangerous
2692 * and supports no sane use case.
2693 */
2694 if (p != current)
2695 return -EPERM;
2696
David Howells5cd9c582008-08-14 11:37:28 +01002697 if (!capable(CAP_MAC_ADMIN))
2698 return -EPERM;
2699
Casey Schauflere114e472008-02-04 22:29:50 -08002700 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2701 return -EINVAL;
2702
2703 if (strcmp(name, "current") != 0)
2704 return -EINVAL;
2705
2706 newsmack = smk_import(value, size);
2707 if (newsmack == NULL)
2708 return -EINVAL;
2709
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002710 /*
2711 * No process is ever allowed the web ("@") label.
2712 */
2713 if (newsmack == smack_known_web.smk_known)
2714 return -EPERM;
2715
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002716 oldtsp = p->cred->security;
David Howellsd84f4f92008-11-14 10:39:23 +11002717 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002718 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002719 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002720
2721 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08002722 if (tsp == NULL) {
2723 kfree(new);
2724 return -ENOMEM;
2725 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002726 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2727 if (rc != 0)
2728 return rc;
2729
Casey Schaufler676dac42010-12-02 06:43:39 -08002730 new->security = tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002731 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002732 return size;
2733}
2734
2735/**
2736 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002737 * @sock: one sock
2738 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002739 * @newsk: unused
2740 *
2741 * Return 0 if a subject with the smack of sock could access
2742 * an object with the smack of other, otherwise an error code
2743 */
David S. Miller3610cda2011-01-05 15:38:53 -08002744static int smack_unix_stream_connect(struct sock *sock,
2745 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002746{
James Morrisd2e7ad12011-01-10 09:46:24 +11002747 struct socket_smack *ssp = sock->sk_security;
2748 struct socket_smack *osp = other->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002749 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002750 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002751
Etienne Bassetecfcc532009-04-08 20:40:06 +02002752 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
David S. Miller3610cda2011-01-05 15:38:53 -08002753 smk_ad_setfield_u_net_sk(&ad, other);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002754
2755 if (!capable(CAP_MAC_OVERRIDE))
2756 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2757
2758 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002759}
2760
2761/**
2762 * smack_unix_may_send - Smack access on UDS
2763 * @sock: one socket
2764 * @other: the other socket
2765 *
2766 * Return 0 if a subject with the smack of sock could access
2767 * an object with the smack of other, otherwise an error code
2768 */
2769static int smack_unix_may_send(struct socket *sock, struct socket *other)
2770{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002771 struct socket_smack *ssp = sock->sk->sk_security;
2772 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002773 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002774 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002775
Etienne Bassetecfcc532009-04-08 20:40:06 +02002776 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2777 smk_ad_setfield_u_net_sk(&ad, other->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002778
2779 if (!capable(CAP_MAC_OVERRIDE))
2780 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2781
2782 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002783}
2784
2785/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002786 * smack_socket_sendmsg - Smack check based on destination host
2787 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002788 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002789 * @size: the size of the message
2790 *
2791 * Return 0 if the current subject can write to the destination
2792 * host. This is only a question if the destination is a single
2793 * label host.
2794 */
2795static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2796 int size)
2797{
2798 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002799
2800 /*
2801 * Perfectly reasonable for this to be NULL
2802 */
Julia Lawallda34d422009-08-05 14:34:55 +02002803 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002804 return 0;
2805
Paul Moore07feee82009-03-27 17:10:54 -04002806 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002807}
2808
2809
2810/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002811 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002812 * @sap: netlabel secattr
2813 * @sip: where to put the result
2814 *
2815 * Copies a smack label into sip
2816 */
2817static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2818{
2819 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002820 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002821 int pcat;
2822
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002823 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002824 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002825 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002826 * If there are flags but no level netlabel isn't
2827 * behaving the way we expect it to.
2828 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002829 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002830 * Without guidance regarding the smack value
2831 * for the packet fall back on the network
2832 * ambient value.
2833 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002834 memset(smack, '\0', SMK_LABELLEN);
2835 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2836 for (pcat = -1;;) {
2837 pcat = netlbl_secattr_catmap_walk(
2838 sap->attr.mls.cat, pcat + 1);
2839 if (pcat < 0)
2840 break;
2841 smack_catset_bit(pcat, smack);
2842 }
2843 /*
2844 * If it is CIPSO using smack direct mapping
2845 * we are already done. WeeHee.
2846 */
2847 if (sap->attr.mls.lvl == smack_cipso_direct) {
2848 memcpy(sip, smack, SMK_MAXLEN);
2849 return;
Casey Schauflere114e472008-02-04 22:29:50 -08002850 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002851 /*
2852 * Look it up in the supplied table if it is not
2853 * a direct mapping.
2854 */
2855 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2856 return;
2857 }
2858 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2859 /*
2860 * Looks like a fallback, which gives us a secid.
2861 */
2862 sp = smack_from_secid(sap->attr.secid);
2863 /*
2864 * This has got to be a bug because it is
2865 * impossible to specify a fallback without
2866 * specifying the label, which will ensure
2867 * it has a secid, and the only way to get a
2868 * secid is from a fallback.
2869 */
2870 BUG_ON(sp == NULL);
2871 strncpy(sip, sp, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002872 return;
2873 }
2874 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002875 * Without guidance regarding the smack value
2876 * for the packet fall back on the network
2877 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002878 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002879 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002880 return;
2881}
2882
2883/**
2884 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2885 * @sk: socket
2886 * @skb: packet
2887 *
2888 * Returns 0 if the packet should be delivered, an error code otherwise
2889 */
2890static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2891{
2892 struct netlbl_lsm_secattr secattr;
2893 struct socket_smack *ssp = sk->sk_security;
2894 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002895 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002896 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002897 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002898 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2899 return 0;
2900
2901 /*
2902 * Translate what netlabel gave us.
2903 */
Casey Schauflere114e472008-02-04 22:29:50 -08002904 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002905
Casey Schauflere114e472008-02-04 22:29:50 -08002906 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002907 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002908 smack_from_secattr(&secattr, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002909 csp = smack;
2910 } else
2911 csp = smack_net_ambient;
2912
Casey Schauflere114e472008-02-04 22:29:50 -08002913 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002914
Etienne Bassetecfcc532009-04-08 20:40:06 +02002915#ifdef CONFIG_AUDIT
2916 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2917 ad.a.u.net.family = sk->sk_family;
Eric Dumazet8964be42009-11-20 15:35:04 -08002918 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002919 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2920#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002921 /*
2922 * Receiving a packet requires that the other end
2923 * be able to write here. Read access is not required.
2924 * This is the simplist possible security model
2925 * for networking.
2926 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002927 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002928 if (rc != 0)
2929 netlbl_skbuff_err(skb, rc, 0);
2930 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002931}
2932
2933/**
2934 * smack_socket_getpeersec_stream - pull in packet label
2935 * @sock: the socket
2936 * @optval: user's destination
2937 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002938 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002939 *
2940 * returns zero on success, an error code otherwise
2941 */
2942static int smack_socket_getpeersec_stream(struct socket *sock,
2943 char __user *optval,
2944 int __user *optlen, unsigned len)
2945{
2946 struct socket_smack *ssp;
2947 int slen;
2948 int rc = 0;
2949
2950 ssp = sock->sk->sk_security;
2951 slen = strlen(ssp->smk_packet) + 1;
2952
2953 if (slen > len)
2954 rc = -ERANGE;
2955 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2956 rc = -EFAULT;
2957
2958 if (put_user(slen, optlen) != 0)
2959 rc = -EFAULT;
2960
2961 return rc;
2962}
2963
2964
2965/**
2966 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002967 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08002968 * @skb: packet data
2969 * @secid: pointer to where to put the secid of the packet
2970 *
2971 * Sets the netlabel socket state on sk from parent
2972 */
2973static int smack_socket_getpeersec_dgram(struct socket *sock,
2974 struct sk_buff *skb, u32 *secid)
2975
2976{
2977 struct netlbl_lsm_secattr secattr;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002978 struct socket_smack *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002979 char smack[SMK_LABELLEN];
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002980 int family = PF_UNSPEC;
2981 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08002982 int rc;
2983
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002984 if (skb != NULL) {
2985 if (skb->protocol == htons(ETH_P_IP))
2986 family = PF_INET;
2987 else if (skb->protocol == htons(ETH_P_IPV6))
2988 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08002989 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002990 if (family == PF_UNSPEC && sock != NULL)
2991 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08002992
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002993 if (family == PF_UNIX) {
2994 sp = sock->sk->sk_security;
2995 s = smack_to_secid(sp->smk_out);
2996 } else if (family == PF_INET || family == PF_INET6) {
2997 /*
2998 * Translate what netlabel gave us.
2999 */
3000 netlbl_secattr_init(&secattr);
3001 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3002 if (rc == 0) {
3003 smack_from_secattr(&secattr, smack);
3004 s = smack_to_secid(smack);
3005 }
3006 netlbl_secattr_destroy(&secattr);
3007 }
3008 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003009 if (s == 0)
3010 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003011 return 0;
3012}
3013
3014/**
Paul Moore07feee82009-03-27 17:10:54 -04003015 * smack_sock_graft - Initialize a newly created socket with an existing sock
3016 * @sk: child sock
3017 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003018 *
Paul Moore07feee82009-03-27 17:10:54 -04003019 * Set the smk_{in,out} state of an existing sock based on the process that
3020 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003021 */
3022static void smack_sock_graft(struct sock *sk, struct socket *parent)
3023{
3024 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08003025
Paul Moore07feee82009-03-27 17:10:54 -04003026 if (sk == NULL ||
3027 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003028 return;
3029
3030 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08003031 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04003032 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003033}
3034
3035/**
3036 * smack_inet_conn_request - Smack access check on connect
3037 * @sk: socket involved
3038 * @skb: packet
3039 * @req: unused
3040 *
3041 * Returns 0 if a task with the packet label could write to
3042 * the socket, otherwise an error code
3043 */
3044static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3045 struct request_sock *req)
3046{
Paul Moore07feee82009-03-27 17:10:54 -04003047 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003048 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003049 struct netlbl_lsm_secattr secattr;
3050 struct sockaddr_in addr;
3051 struct iphdr *hdr;
Casey Schauflere114e472008-02-04 22:29:50 -08003052 char smack[SMK_LABELLEN];
3053 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003054 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003055
Paul Moore07feee82009-03-27 17:10:54 -04003056 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3057 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3058 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08003059
Paul Moore07feee82009-03-27 17:10:54 -04003060 netlbl_secattr_init(&secattr);
3061 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003062 if (rc == 0)
Paul Moore07feee82009-03-27 17:10:54 -04003063 smack_from_secattr(&secattr, smack);
Casey Schauflere114e472008-02-04 22:29:50 -08003064 else
3065 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
Paul Moore07feee82009-03-27 17:10:54 -04003066 netlbl_secattr_destroy(&secattr);
3067
Etienne Bassetecfcc532009-04-08 20:40:06 +02003068#ifdef CONFIG_AUDIT
3069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3070 ad.a.u.net.family = family;
Eric Dumazet8964be42009-11-20 15:35:04 -08003071 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003072 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3073#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003074 /*
Paul Moore07feee82009-03-27 17:10:54 -04003075 * Receiving a packet requires that the other end be able to write
3076 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003077 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02003078 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003079 if (rc != 0)
3080 return rc;
3081
3082 /*
3083 * Save the peer's label in the request_sock so we can later setup
3084 * smk_packet in the child socket so that SO_PEERCRED can report it.
3085 */
3086 req->peer_secid = smack_to_secid(smack);
3087
3088 /*
3089 * We need to decide if we want to label the incoming connection here
3090 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003091 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003092 */
3093 hdr = ip_hdr(skb);
3094 addr.sin_addr.s_addr = hdr->saddr;
3095 rcu_read_lock();
3096 if (smack_host_label(&addr) == NULL) {
3097 rcu_read_unlock();
3098 netlbl_secattr_init(&secattr);
3099 smack_to_secattr(smack, &secattr);
3100 rc = netlbl_req_setattr(req, &secattr);
3101 netlbl_secattr_destroy(&secattr);
3102 } else {
3103 rcu_read_unlock();
3104 netlbl_req_delattr(req);
3105 }
Casey Schauflere114e472008-02-04 22:29:50 -08003106
3107 return rc;
3108}
3109
Paul Moore07feee82009-03-27 17:10:54 -04003110/**
3111 * smack_inet_csk_clone - Copy the connection information to the new socket
3112 * @sk: the new socket
3113 * @req: the connection's request_sock
3114 *
3115 * Transfer the connection's peer label to the newly created socket.
3116 */
3117static void smack_inet_csk_clone(struct sock *sk,
3118 const struct request_sock *req)
3119{
3120 struct socket_smack *ssp = sk->sk_security;
3121 char *smack;
3122
3123 if (req->peer_secid != 0) {
3124 smack = smack_from_secid(req->peer_secid);
3125 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
3126 } else
3127 ssp->smk_packet[0] = '\0';
3128}
3129
Casey Schauflere114e472008-02-04 22:29:50 -08003130/*
3131 * Key management security hooks
3132 *
3133 * Casey has not tested key support very heavily.
3134 * The permission check is most likely too restrictive.
3135 * If you care about keys please have a look.
3136 */
3137#ifdef CONFIG_KEYS
3138
3139/**
3140 * smack_key_alloc - Set the key security blob
3141 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003142 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003143 * @flags: unused
3144 *
3145 * No allocation required
3146 *
3147 * Returns 0
3148 */
David Howellsd84f4f92008-11-14 10:39:23 +11003149static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003150 unsigned long flags)
3151{
Casey Schaufler676dac42010-12-02 06:43:39 -08003152 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003153 return 0;
3154}
3155
3156/**
3157 * smack_key_free - Clear the key security blob
3158 * @key: the object
3159 *
3160 * Clear the blob pointer
3161 */
3162static void smack_key_free(struct key *key)
3163{
3164 key->security = NULL;
3165}
3166
3167/*
3168 * smack_key_permission - Smack access on a key
3169 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003170 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003171 * @perm: unused
3172 *
3173 * Return 0 if the task has read and write to the object,
3174 * an error code otherwise
3175 */
3176static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003177 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003178{
3179 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003180 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08003181 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003182
3183 keyp = key_ref_to_ptr(key_ref);
3184 if (keyp == NULL)
3185 return -EINVAL;
3186 /*
3187 * If the key hasn't been initialized give it access so that
3188 * it may do so.
3189 */
3190 if (keyp->security == NULL)
3191 return 0;
3192 /*
3193 * This should not occur
3194 */
Casey Schaufler676dac42010-12-02 06:43:39 -08003195 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003196 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003197#ifdef CONFIG_AUDIT
3198 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3199 ad.a.u.key_struct.key = keyp->serial;
3200 ad.a.u.key_struct.key_desc = keyp->description;
3201#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08003202 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02003203 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003204}
3205#endif /* CONFIG_KEYS */
3206
3207/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003208 * Smack Audit hooks
3209 *
3210 * Audit requires a unique representation of each Smack specific
3211 * rule. This unique representation is used to distinguish the
3212 * object to be audited from remaining kernel objects and also
3213 * works as a glue between the audit hooks.
3214 *
3215 * Since repository entries are added but never deleted, we'll use
3216 * the smack_known label address related to the given audit rule as
3217 * the needed unique representation. This also better fits the smack
3218 * model where nearly everything is a label.
3219 */
3220#ifdef CONFIG_AUDIT
3221
3222/**
3223 * smack_audit_rule_init - Initialize a smack audit rule
3224 * @field: audit rule fields given from user-space (audit.h)
3225 * @op: required testing operator (=, !=, >, <, ...)
3226 * @rulestr: smack label to be audited
3227 * @vrule: pointer to save our own audit rule representation
3228 *
3229 * Prepare to audit cases where (@field @op @rulestr) is true.
3230 * The label to be audited is created if necessay.
3231 */
3232static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3233{
3234 char **rule = (char **)vrule;
3235 *rule = NULL;
3236
3237 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3238 return -EINVAL;
3239
Al Viro5af75d82008-12-16 05:59:26 -05003240 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003241 return -EINVAL;
3242
3243 *rule = smk_import(rulestr, 0);
3244
3245 return 0;
3246}
3247
3248/**
3249 * smack_audit_rule_known - Distinguish Smack audit rules
3250 * @krule: rule of interest, in Audit kernel representation format
3251 *
3252 * This is used to filter Smack rules from remaining Audit ones.
3253 * If it's proved that this rule belongs to us, the
3254 * audit_rule_match hook will be called to do the final judgement.
3255 */
3256static int smack_audit_rule_known(struct audit_krule *krule)
3257{
3258 struct audit_field *f;
3259 int i;
3260
3261 for (i = 0; i < krule->field_count; i++) {
3262 f = &krule->fields[i];
3263
3264 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3265 return 1;
3266 }
3267
3268 return 0;
3269}
3270
3271/**
3272 * smack_audit_rule_match - Audit given object ?
3273 * @secid: security id for identifying the object to test
3274 * @field: audit rule flags given from user-space
3275 * @op: required testing operator
3276 * @vrule: smack internal rule presentation
3277 * @actx: audit context associated with the check
3278 *
3279 * The core Audit hook. It's used to take the decision of
3280 * whether to audit or not to audit a given object.
3281 */
3282static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3283 struct audit_context *actx)
3284{
3285 char *smack;
3286 char *rule = vrule;
3287
3288 if (!rule) {
3289 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3290 "Smack: missing rule\n");
3291 return -ENOENT;
3292 }
3293
3294 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3295 return 0;
3296
3297 smack = smack_from_secid(secid);
3298
3299 /*
3300 * No need to do string comparisons. If a match occurs,
3301 * both pointers will point to the same smack_known
3302 * label.
3303 */
Al Viro5af75d82008-12-16 05:59:26 -05003304 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003305 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003306 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003307 return (rule != smack);
3308
3309 return 0;
3310}
3311
3312/**
3313 * smack_audit_rule_free - free smack rule representation
3314 * @vrule: rule to be freed.
3315 *
3316 * No memory was allocated.
3317 */
3318static void smack_audit_rule_free(void *vrule)
3319{
3320 /* No-op */
3321}
3322
3323#endif /* CONFIG_AUDIT */
3324
Randy Dunlap251a2a92009-02-18 11:42:33 -08003325/**
Casey Schauflere114e472008-02-04 22:29:50 -08003326 * smack_secid_to_secctx - return the smack label for a secid
3327 * @secid: incoming integer
3328 * @secdata: destination
3329 * @seclen: how long it is
3330 *
3331 * Exists for networking code.
3332 */
3333static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3334{
3335 char *sp = smack_from_secid(secid);
3336
Eric Parisd5630b92010-10-13 16:24:48 -04003337 if (secdata)
3338 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003339 *seclen = strlen(sp);
3340 return 0;
3341}
3342
Randy Dunlap251a2a92009-02-18 11:42:33 -08003343/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003344 * smack_secctx_to_secid - return the secid for a smack label
3345 * @secdata: smack label
3346 * @seclen: how long result is
3347 * @secid: outgoing integer
3348 *
3349 * Exists for audit and networking code.
3350 */
David Howellse52c17642008-04-29 20:52:51 +01003351static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003352{
3353 *secid = smack_to_secid(secdata);
3354 return 0;
3355}
3356
Randy Dunlap251a2a92009-02-18 11:42:33 -08003357/**
Casey Schauflere114e472008-02-04 22:29:50 -08003358 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003359 * @secdata: unused
3360 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003361 *
3362 * Exists to make sure nothing gets done, and properly
3363 */
3364static void smack_release_secctx(char *secdata, u32 seclen)
3365{
3366}
3367
David P. Quigley1ee65e32009-09-03 14:25:57 -04003368static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3369{
3370 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3371}
3372
3373static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3374{
3375 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3376}
3377
3378static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3379{
3380 int len = 0;
3381 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3382
3383 if (len < 0)
3384 return len;
3385 *ctxlen = len;
3386 return 0;
3387}
3388
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003389struct security_operations smack_ops = {
3390 .name = "smack",
3391
Ingo Molnar9e488582009-05-07 19:26:19 +10003392 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003393 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003394 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003395
3396 .sb_alloc_security = smack_sb_alloc_security,
3397 .sb_free_security = smack_sb_free_security,
3398 .sb_copy_data = smack_sb_copy_data,
3399 .sb_kern_mount = smack_sb_kern_mount,
3400 .sb_statfs = smack_sb_statfs,
3401 .sb_mount = smack_sb_mount,
3402 .sb_umount = smack_sb_umount,
3403
Casey Schaufler676dac42010-12-02 06:43:39 -08003404 .bprm_set_creds = smack_bprm_set_creds,
3405
Casey Schauflere114e472008-02-04 22:29:50 -08003406 .inode_alloc_security = smack_inode_alloc_security,
3407 .inode_free_security = smack_inode_free_security,
3408 .inode_init_security = smack_inode_init_security,
3409 .inode_link = smack_inode_link,
3410 .inode_unlink = smack_inode_unlink,
3411 .inode_rmdir = smack_inode_rmdir,
3412 .inode_rename = smack_inode_rename,
3413 .inode_permission = smack_inode_permission,
3414 .inode_setattr = smack_inode_setattr,
3415 .inode_getattr = smack_inode_getattr,
3416 .inode_setxattr = smack_inode_setxattr,
3417 .inode_post_setxattr = smack_inode_post_setxattr,
3418 .inode_getxattr = smack_inode_getxattr,
3419 .inode_removexattr = smack_inode_removexattr,
3420 .inode_getsecurity = smack_inode_getsecurity,
3421 .inode_setsecurity = smack_inode_setsecurity,
3422 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003423 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003424
3425 .file_permission = smack_file_permission,
3426 .file_alloc_security = smack_file_alloc_security,
3427 .file_free_security = smack_file_free_security,
3428 .file_ioctl = smack_file_ioctl,
3429 .file_lock = smack_file_lock,
3430 .file_fcntl = smack_file_fcntl,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003431 .file_mmap = smack_file_mmap,
Casey Schauflere114e472008-02-04 22:29:50 -08003432 .file_set_fowner = smack_file_set_fowner,
3433 .file_send_sigiotask = smack_file_send_sigiotask,
3434 .file_receive = smack_file_receive,
3435
David Howellsee18d642009-09-02 09:14:21 +01003436 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003437 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003438 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003439 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003440 .kernel_act_as = smack_kernel_act_as,
3441 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003442 .task_setpgid = smack_task_setpgid,
3443 .task_getpgid = smack_task_getpgid,
3444 .task_getsid = smack_task_getsid,
3445 .task_getsecid = smack_task_getsecid,
3446 .task_setnice = smack_task_setnice,
3447 .task_setioprio = smack_task_setioprio,
3448 .task_getioprio = smack_task_getioprio,
3449 .task_setscheduler = smack_task_setscheduler,
3450 .task_getscheduler = smack_task_getscheduler,
3451 .task_movememory = smack_task_movememory,
3452 .task_kill = smack_task_kill,
3453 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003454 .task_to_inode = smack_task_to_inode,
3455
3456 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003457 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003458
3459 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3460 .msg_msg_free_security = smack_msg_msg_free_security,
3461
3462 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3463 .msg_queue_free_security = smack_msg_queue_free_security,
3464 .msg_queue_associate = smack_msg_queue_associate,
3465 .msg_queue_msgctl = smack_msg_queue_msgctl,
3466 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3467 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3468
3469 .shm_alloc_security = smack_shm_alloc_security,
3470 .shm_free_security = smack_shm_free_security,
3471 .shm_associate = smack_shm_associate,
3472 .shm_shmctl = smack_shm_shmctl,
3473 .shm_shmat = smack_shm_shmat,
3474
3475 .sem_alloc_security = smack_sem_alloc_security,
3476 .sem_free_security = smack_sem_free_security,
3477 .sem_associate = smack_sem_associate,
3478 .sem_semctl = smack_sem_semctl,
3479 .sem_semop = smack_sem_semop,
3480
Casey Schauflere114e472008-02-04 22:29:50 -08003481 .d_instantiate = smack_d_instantiate,
3482
3483 .getprocattr = smack_getprocattr,
3484 .setprocattr = smack_setprocattr,
3485
3486 .unix_stream_connect = smack_unix_stream_connect,
3487 .unix_may_send = smack_unix_may_send,
3488
3489 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003490 .socket_connect = smack_socket_connect,
3491 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003492 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3493 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3494 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3495 .sk_alloc_security = smack_sk_alloc_security,
3496 .sk_free_security = smack_sk_free_security,
3497 .sock_graft = smack_sock_graft,
3498 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003499 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003500
Casey Schauflere114e472008-02-04 22:29:50 -08003501 /* key management security hooks */
3502#ifdef CONFIG_KEYS
3503 .key_alloc = smack_key_alloc,
3504 .key_free = smack_key_free,
3505 .key_permission = smack_key_permission,
3506#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003507
3508 /* Audit hooks */
3509#ifdef CONFIG_AUDIT
3510 .audit_rule_init = smack_audit_rule_init,
3511 .audit_rule_known = smack_audit_rule_known,
3512 .audit_rule_match = smack_audit_rule_match,
3513 .audit_rule_free = smack_audit_rule_free,
3514#endif /* CONFIG_AUDIT */
3515
Casey Schauflere114e472008-02-04 22:29:50 -08003516 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003517 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003518 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003519 .inode_notifysecctx = smack_inode_notifysecctx,
3520 .inode_setsecctx = smack_inode_setsecctx,
3521 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003522};
3523
Etienne Basset7198e2e2009-03-24 20:53:24 +01003524
3525static __init void init_smack_know_list(void)
3526{
3527 list_add(&smack_known_huh.list, &smack_known_list);
3528 list_add(&smack_known_hat.list, &smack_known_list);
3529 list_add(&smack_known_star.list, &smack_known_list);
3530 list_add(&smack_known_floor.list, &smack_known_list);
3531 list_add(&smack_known_invalid.list, &smack_known_list);
3532 list_add(&smack_known_web.list, &smack_known_list);
3533}
3534
Casey Schauflere114e472008-02-04 22:29:50 -08003535/**
3536 * smack_init - initialize the smack system
3537 *
3538 * Returns 0
3539 */
3540static __init int smack_init(void)
3541{
David Howellsd84f4f92008-11-14 10:39:23 +11003542 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003543 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003544
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003545 if (!security_module_enable(&smack_ops))
3546 return 0;
3547
3548 tsp = new_task_smack(smack_known_floor.smk_known,
3549 smack_known_floor.smk_known, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003550 if (tsp == NULL)
3551 return -ENOMEM;
3552
Casey Schauflere114e472008-02-04 22:29:50 -08003553 printk(KERN_INFO "Smack: Initializing.\n");
3554
3555 /*
3556 * Set the security state for the initial task.
3557 */
David Howellsd84f4f92008-11-14 10:39:23 +11003558 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003559 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003560
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003561 /* initialize the smack_know_list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003562 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003563 /*
3564 * Initialize locks
3565 */
Casey Schauflere114e472008-02-04 22:29:50 -08003566 spin_lock_init(&smack_known_huh.smk_cipsolock);
3567 spin_lock_init(&smack_known_hat.smk_cipsolock);
3568 spin_lock_init(&smack_known_star.smk_cipsolock);
3569 spin_lock_init(&smack_known_floor.smk_cipsolock);
3570 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3571
3572 /*
3573 * Register with LSM
3574 */
3575 if (register_security(&smack_ops))
3576 panic("smack: Unable to register with kernel.\n");
3577
3578 return 0;
3579}
3580
3581/*
3582 * Smack requires early initialization in order to label
3583 * all processes and objects when they are created.
3584 */
3585security_initcall(smack_init);