blob: 92cb71507f5ba0f1c03aeeac2e72043710378d66 [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>
Casey Schauflere114e472008-02-04 22:29:50 -080036#include "smack.h"
37
David Howellsc69e8d92008-11-14 10:39:19 +110038#define task_security(task) (task_cred_xxx((task), security))
39
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020040#define TRANS_TRUE "TRUE"
41#define TRANS_TRUE_SIZE 4
42
Casey Schauflere114e472008-02-04 22:29:50 -080043/**
44 * smk_fetch - Fetch the smack label from a file.
45 * @ip: a pointer to the inode
46 * @dp: a pointer to the dentry
47 *
48 * Returns a pointer to the master list entry for the Smack label
49 * or NULL if there was no label to fetch.
50 */
Casey Schaufler676dac42010-12-02 06:43:39 -080051static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080052{
53 int rc;
54 char in[SMK_LABELLEN];
55
56 if (ip->i_op->getxattr == NULL)
57 return NULL;
58
Casey Schaufler676dac42010-12-02 06:43:39 -080059 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
Casey Schauflere114e472008-02-04 22:29:50 -080060 if (rc < 0)
61 return NULL;
62
63 return smk_import(in, rc);
64}
65
66/**
67 * new_inode_smack - allocate an inode security blob
68 * @smack: a pointer to the Smack label to use in the blob
69 *
70 * Returns the new blob or NULL if there's no memory available
71 */
72struct inode_smack *new_inode_smack(char *smack)
73{
74 struct inode_smack *isp;
75
76 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
77 if (isp == NULL)
78 return NULL;
79
80 isp->smk_inode = smack;
81 isp->smk_flags = 0;
82 mutex_init(&isp->smk_lock);
83
84 return isp;
85}
86
Casey Schaufler7898e1f2011-01-17 08:05:27 -080087/**
88 * new_task_smack - allocate a task security blob
89 * @smack: a pointer to the Smack label to use in the blob
90 *
91 * Returns the new blob or NULL if there's no memory available
92 */
93static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
94{
95 struct task_smack *tsp;
96
97 tsp = kzalloc(sizeof(struct task_smack), gfp);
98 if (tsp == NULL)
99 return NULL;
100
101 tsp->smk_task = task;
102 tsp->smk_forked = forked;
103 INIT_LIST_HEAD(&tsp->smk_rules);
104 mutex_init(&tsp->smk_rules_lock);
105
106 return tsp;
107}
108
109/**
110 * smk_copy_rules - copy a rule set
111 * @nhead - new rules header pointer
112 * @ohead - old rules header pointer
113 *
114 * Returns 0 on success, -ENOMEM on error
115 */
116static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
117 gfp_t gfp)
118{
119 struct smack_rule *nrp;
120 struct smack_rule *orp;
121 int rc = 0;
122
123 INIT_LIST_HEAD(nhead);
124
125 list_for_each_entry_rcu(orp, ohead, list) {
126 nrp = kzalloc(sizeof(struct smack_rule), gfp);
127 if (nrp == NULL) {
128 rc = -ENOMEM;
129 break;
130 }
131 *nrp = *orp;
132 list_add_rcu(&nrp->list, nhead);
133 }
134 return rc;
135}
136
Casey Schauflere114e472008-02-04 22:29:50 -0800137/*
138 * LSM hooks.
139 * We he, that is fun!
140 */
141
142/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000143 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800144 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -0800145 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -0800146 *
147 * Returns 0 if access is OK, an error code otherwise
148 *
149 * Do the capability checks, and require read and write.
150 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000151static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800152{
153 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200154 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800155 char *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800156
Ingo Molnar9e488582009-05-07 19:26:19 +1000157 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800158 if (rc != 0)
159 return rc;
160
Casey Schaufler676dac42010-12-02 06:43:39 -0800161 tsp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200162 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
163 smk_ad_setfield_u_tsk(&ad, ctp);
164
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800165 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100166 return rc;
167}
Casey Schauflere114e472008-02-04 22:29:50 -0800168
David Howells5cd9c582008-08-14 11:37:28 +0100169/**
170 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
171 * @ptp: parent task pointer
172 *
173 * Returns 0 if access is OK, an error code otherwise
174 *
175 * Do the capability checks, and require read and write.
176 */
177static int smack_ptrace_traceme(struct task_struct *ptp)
178{
179 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200180 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800181 char *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100182
183 rc = cap_ptrace_traceme(ptp);
184 if (rc != 0)
185 return rc;
186
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800187 tsp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
189 smk_ad_setfield_u_tsk(&ad, ptp);
190
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800191 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800192 return rc;
193}
194
195/**
196 * smack_syslog - Smack approval on syslog
197 * @type: message type
198 *
199 * Require that the task has the floor label
200 *
201 * Returns 0 on success, error code otherwise.
202 */
Eric Paris12b30522010-11-15 18:36:29 -0500203static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800204{
Eric Paris12b30522010-11-15 18:36:29 -0500205 int rc = 0;
Casey Schaufler676dac42010-12-02 06:43:39 -0800206 char *sp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800207
Casey Schauflere114e472008-02-04 22:29:50 -0800208 if (capable(CAP_MAC_OVERRIDE))
209 return 0;
210
211 if (sp != smack_known_floor.smk_known)
212 rc = -EACCES;
213
214 return rc;
215}
216
217
218/*
219 * Superblock Hooks.
220 */
221
222/**
223 * smack_sb_alloc_security - allocate a superblock blob
224 * @sb: the superblock getting the blob
225 *
226 * Returns 0 on success or -ENOMEM on error.
227 */
228static int smack_sb_alloc_security(struct super_block *sb)
229{
230 struct superblock_smack *sbsp;
231
232 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
233
234 if (sbsp == NULL)
235 return -ENOMEM;
236
237 sbsp->smk_root = smack_known_floor.smk_known;
238 sbsp->smk_default = smack_known_floor.smk_known;
239 sbsp->smk_floor = smack_known_floor.smk_known;
240 sbsp->smk_hat = smack_known_hat.smk_known;
241 sbsp->smk_initialized = 0;
242 spin_lock_init(&sbsp->smk_sblock);
243
244 sb->s_security = sbsp;
245
246 return 0;
247}
248
249/**
250 * smack_sb_free_security - free a superblock blob
251 * @sb: the superblock getting the blob
252 *
253 */
254static void smack_sb_free_security(struct super_block *sb)
255{
256 kfree(sb->s_security);
257 sb->s_security = NULL;
258}
259
260/**
261 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800262 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800263 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800264 *
265 * Returns 0 on success or -ENOMEM on error.
266 *
267 * Copy the Smack specific mount options out of the mount
268 * options list.
269 */
Eric Parise0007522008-03-05 10:31:54 -0500270static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800271{
272 char *cp, *commap, *otheropts, *dp;
273
Casey Schauflere114e472008-02-04 22:29:50 -0800274 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
275 if (otheropts == NULL)
276 return -ENOMEM;
277
278 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
279 if (strstr(cp, SMK_FSDEFAULT) == cp)
280 dp = smackopts;
281 else if (strstr(cp, SMK_FSFLOOR) == cp)
282 dp = smackopts;
283 else if (strstr(cp, SMK_FSHAT) == cp)
284 dp = smackopts;
285 else if (strstr(cp, SMK_FSROOT) == cp)
286 dp = smackopts;
287 else
288 dp = otheropts;
289
290 commap = strchr(cp, ',');
291 if (commap != NULL)
292 *commap = '\0';
293
294 if (*dp != '\0')
295 strcat(dp, ",");
296 strcat(dp, cp);
297 }
298
299 strcpy(orig, otheropts);
300 free_page((unsigned long)otheropts);
301
302 return 0;
303}
304
305/**
306 * smack_sb_kern_mount - Smack specific mount processing
307 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100308 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800309 * @data: the smack mount options
310 *
311 * Returns 0 on success, an error code on failure
312 */
James Morris12204e22008-12-19 10:44:42 +1100313static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800314{
315 struct dentry *root = sb->s_root;
316 struct inode *inode = root->d_inode;
317 struct superblock_smack *sp = sb->s_security;
318 struct inode_smack *isp;
319 char *op;
320 char *commap;
321 char *nsp;
322
323 spin_lock(&sp->smk_sblock);
324 if (sp->smk_initialized != 0) {
325 spin_unlock(&sp->smk_sblock);
326 return 0;
327 }
328 sp->smk_initialized = 1;
329 spin_unlock(&sp->smk_sblock);
330
331 for (op = data; op != NULL; op = commap) {
332 commap = strchr(op, ',');
333 if (commap != NULL)
334 *commap++ = '\0';
335
336 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
337 op += strlen(SMK_FSHAT);
338 nsp = smk_import(op, 0);
339 if (nsp != NULL)
340 sp->smk_hat = nsp;
341 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
342 op += strlen(SMK_FSFLOOR);
343 nsp = smk_import(op, 0);
344 if (nsp != NULL)
345 sp->smk_floor = nsp;
346 } else if (strncmp(op, SMK_FSDEFAULT,
347 strlen(SMK_FSDEFAULT)) == 0) {
348 op += strlen(SMK_FSDEFAULT);
349 nsp = smk_import(op, 0);
350 if (nsp != NULL)
351 sp->smk_default = nsp;
352 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
353 op += strlen(SMK_FSROOT);
354 nsp = smk_import(op, 0);
355 if (nsp != NULL)
356 sp->smk_root = nsp;
357 }
358 }
359
360 /*
361 * Initialize the root inode.
362 */
363 isp = inode->i_security;
364 if (isp == NULL)
365 inode->i_security = new_inode_smack(sp->smk_root);
366 else
367 isp->smk_inode = sp->smk_root;
368
369 return 0;
370}
371
372/**
373 * smack_sb_statfs - Smack check on statfs
374 * @dentry: identifies the file system in question
375 *
376 * Returns 0 if current can read the floor of the filesystem,
377 * and error code otherwise
378 */
379static int smack_sb_statfs(struct dentry *dentry)
380{
381 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200382 int rc;
383 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800384
Etienne Bassetecfcc532009-04-08 20:40:06 +0200385 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
386 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
387
388 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
389 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800390}
391
392/**
393 * smack_sb_mount - Smack check for mounting
394 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800395 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800396 * @type: unused
397 * @flags: unused
398 * @data: unused
399 *
400 * Returns 0 if current can write the floor of the filesystem
401 * being mounted on, an error code otherwise.
402 */
Al Virob5266eb2008-03-22 17:48:24 -0400403static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800404 char *type, unsigned long flags, void *data)
405{
Al Virob5266eb2008-03-22 17:48:24 -0400406 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200407 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800408
Etienne Bassetecfcc532009-04-08 20:40:06 +0200409 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
410 smk_ad_setfield_u_fs_path(&ad, *path);
411
412 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800413}
414
415/**
416 * smack_sb_umount - Smack check for unmounting
417 * @mnt: file system to unmount
418 * @flags: unused
419 *
420 * Returns 0 if current can write the floor of the filesystem
421 * being unmounted, an error code otherwise.
422 */
423static int smack_sb_umount(struct vfsmount *mnt, int flags)
424{
425 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200426 struct smk_audit_info ad;
427
428 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
Al Virode27a5b2010-01-30 15:27:27 -0500429 smk_ad_setfield_u_fs_path_dentry(&ad, mnt->mnt_root);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200430 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
Casey Schauflere114e472008-02-04 22:29:50 -0800431
432 sbp = mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200433 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800434}
435
436/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800437 * BPRM hooks
438 */
439
440static int smack_bprm_set_creds(struct linux_binprm *bprm)
441{
442 struct task_smack *tsp = bprm->cred->security;
443 struct inode_smack *isp;
444 struct dentry *dp;
445 int rc;
446
447 rc = cap_bprm_set_creds(bprm);
448 if (rc != 0)
449 return rc;
450
451 if (bprm->cred_prepared)
452 return 0;
453
454 if (bprm->file == NULL || bprm->file->f_dentry == NULL)
455 return 0;
456
457 dp = bprm->file->f_dentry;
458
459 if (dp->d_inode == NULL)
460 return 0;
461
462 isp = dp->d_inode->i_security;
463
464 if (isp->smk_task != NULL)
465 tsp->smk_task = isp->smk_task;
466
467 return 0;
468}
469
470/*
Casey Schauflere114e472008-02-04 22:29:50 -0800471 * Inode hooks
472 */
473
474/**
475 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800476 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800477 *
478 * Returns 0 if it gets a blob, -ENOMEM otherwise
479 */
480static int smack_inode_alloc_security(struct inode *inode)
481{
Casey Schaufler676dac42010-12-02 06:43:39 -0800482 inode->i_security = new_inode_smack(smk_of_current());
Casey Schauflere114e472008-02-04 22:29:50 -0800483 if (inode->i_security == NULL)
484 return -ENOMEM;
485 return 0;
486}
487
488/**
489 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800490 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800491 *
492 * Clears the blob pointer in inode
493 */
494static void smack_inode_free_security(struct inode *inode)
495{
496 kfree(inode->i_security);
497 inode->i_security = NULL;
498}
499
500/**
501 * smack_inode_init_security - copy out the smack from an inode
502 * @inode: the inode
503 * @dir: unused
504 * @name: where to put the attribute name
505 * @value: where to put the attribute value
506 * @len: where to put the length of the attribute
507 *
508 * Returns 0 if it all works out, -ENOMEM if there's no memory
509 */
510static int smack_inode_init_security(struct inode *inode, struct inode *dir,
511 char **name, void **value, size_t *len)
512{
513 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200514 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800515 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800516
517 if (name) {
518 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
519 if (*name == NULL)
520 return -ENOMEM;
521 }
522
523 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800524 rcu_read_lock();
525 may = smk_access_entry(smk_of_current(), dsp, &smack_rule_list);
526 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200527
528 /*
529 * If the access rule allows transmutation and
530 * the directory requests transmutation then
531 * by all means transmute.
532 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800533 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
534 smk_inode_transmutable(dir))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200535 isp = dsp;
536
Casey Schauflere114e472008-02-04 22:29:50 -0800537 *value = kstrdup(isp, GFP_KERNEL);
538 if (*value == NULL)
539 return -ENOMEM;
540 }
541
542 if (len)
543 *len = strlen(isp) + 1;
544
545 return 0;
546}
547
548/**
549 * smack_inode_link - Smack check on link
550 * @old_dentry: the existing object
551 * @dir: unused
552 * @new_dentry: the new object
553 *
554 * Returns 0 if access is permitted, an error code otherwise
555 */
556static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
557 struct dentry *new_dentry)
558{
Casey Schauflere114e472008-02-04 22:29:50 -0800559 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200560 struct smk_audit_info ad;
561 int rc;
562
563 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
564 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800565
566 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200567 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800568
569 if (rc == 0 && new_dentry->d_inode != NULL) {
570 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200571 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
572 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800573 }
574
575 return rc;
576}
577
578/**
579 * smack_inode_unlink - Smack check on inode deletion
580 * @dir: containing directory object
581 * @dentry: file to unlink
582 *
583 * Returns 0 if current can write the containing directory
584 * and the object, error code otherwise
585 */
586static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
587{
588 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200589 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800590 int rc;
591
Etienne Bassetecfcc532009-04-08 20:40:06 +0200592 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
593 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
594
Casey Schauflere114e472008-02-04 22:29:50 -0800595 /*
596 * You need write access to the thing you're unlinking
597 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200598 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
599 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800600 /*
601 * You also need write access to the containing directory
602 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200603 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
604 smk_ad_setfield_u_fs_inode(&ad, dir);
605 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
606 }
Casey Schauflere114e472008-02-04 22:29:50 -0800607 return rc;
608}
609
610/**
611 * smack_inode_rmdir - Smack check on directory deletion
612 * @dir: containing directory object
613 * @dentry: directory to unlink
614 *
615 * Returns 0 if current can write the containing directory
616 * and the directory, error code otherwise
617 */
618static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
619{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200620 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800621 int rc;
622
Etienne Bassetecfcc532009-04-08 20:40:06 +0200623 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
624 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
625
Casey Schauflere114e472008-02-04 22:29:50 -0800626 /*
627 * You need write access to the thing you're removing
628 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200629 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
630 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800631 /*
632 * You also need write access to the containing directory
633 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200634 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
635 smk_ad_setfield_u_fs_inode(&ad, dir);
636 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
637 }
Casey Schauflere114e472008-02-04 22:29:50 -0800638
639 return rc;
640}
641
642/**
643 * smack_inode_rename - Smack check on rename
644 * @old_inode: the old directory
645 * @old_dentry: unused
646 * @new_inode: the new directory
647 * @new_dentry: unused
648 *
649 * Read and write access is required on both the old and
650 * new directories.
651 *
652 * Returns 0 if access is permitted, an error code otherwise
653 */
654static int smack_inode_rename(struct inode *old_inode,
655 struct dentry *old_dentry,
656 struct inode *new_inode,
657 struct dentry *new_dentry)
658{
659 int rc;
660 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200661 struct smk_audit_info ad;
662
663 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
664 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800665
666 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200667 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800668
669 if (rc == 0 && new_dentry->d_inode != NULL) {
670 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200671 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
672 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800673 }
Casey Schauflere114e472008-02-04 22:29:50 -0800674 return rc;
675}
676
677/**
678 * smack_inode_permission - Smack version of permission()
679 * @inode: the inode in question
680 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800681 *
682 * This is the important Smack hook.
683 *
684 * Returns 0 if access is permitted, -EACCES otherwise
685 */
Al Virob77b0642008-07-17 09:37:02 -0400686static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800687{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200688 struct smk_audit_info ad;
Eric Parisd09ca732010-07-23 11:43:57 -0400689
690 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800691 /*
692 * No permission to check. Existence test. Yup, it's there.
693 */
694 if (mask == 0)
695 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200696 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
697 smk_ad_setfield_u_fs_inode(&ad, inode);
698 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800699}
700
701/**
702 * smack_inode_setattr - Smack check for setting attributes
703 * @dentry: the object
704 * @iattr: for the force flag
705 *
706 * Returns 0 if access is permitted, an error code otherwise
707 */
708static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
709{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200710 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800711 /*
712 * Need to allow for clearing the setuid bit.
713 */
714 if (iattr->ia_valid & ATTR_FORCE)
715 return 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200716 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
717 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800718
Etienne Bassetecfcc532009-04-08 20:40:06 +0200719 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800720}
721
722/**
723 * smack_inode_getattr - Smack check for getting attributes
724 * @mnt: unused
725 * @dentry: the object
726 *
727 * Returns 0 if access is permitted, an error code otherwise
728 */
729static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
730{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200731 struct smk_audit_info ad;
732
733 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
734 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
735 smk_ad_setfield_u_fs_path_mnt(&ad, mnt);
736 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800737}
738
739/**
740 * smack_inode_setxattr - Smack check for setting xattrs
741 * @dentry: the object
742 * @name: name of the attribute
743 * @value: unused
744 * @size: unused
745 * @flags: unused
746 *
747 * This protects the Smack attribute explicitly.
748 *
749 * Returns 0 if access is permitted, an error code otherwise
750 */
David Howells8f0cfa52008-04-29 00:59:41 -0700751static int smack_inode_setxattr(struct dentry *dentry, const char *name,
752 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800753{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200754 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800755 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800756
Casey Schauflerbcdca222008-02-23 15:24:04 -0800757 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
758 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800759 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800760 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
761 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800762 if (!capable(CAP_MAC_ADMIN))
763 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200764 /*
765 * check label validity here so import wont fail on
766 * post_setxattr
767 */
768 if (size == 0 || size >= SMK_LABELLEN ||
769 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400770 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200771 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
772 if (!capable(CAP_MAC_ADMIN))
773 rc = -EPERM;
774 if (size != TRANS_TRUE_SIZE ||
775 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
776 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800777 } else
778 rc = cap_inode_setxattr(dentry, name, value, size, flags);
779
Etienne Bassetecfcc532009-04-08 20:40:06 +0200780 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
781 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
782
Casey Schauflerbcdca222008-02-23 15:24:04 -0800783 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200784 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800785
786 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800787}
788
789/**
790 * smack_inode_post_setxattr - Apply the Smack update approved above
791 * @dentry: object
792 * @name: attribute name
793 * @value: attribute value
794 * @size: attribute size
795 * @flags: unused
796 *
797 * Set the pointer in the inode blob to the entry found
798 * in the master label list.
799 */
David Howells8f0cfa52008-04-29 00:59:41 -0700800static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
801 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800802{
Casey Schauflere114e472008-02-04 22:29:50 -0800803 char *nsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200804 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800805
806 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200807 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800808 if (nsp != NULL)
809 isp->smk_inode = nsp;
810 else
811 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200812 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
813 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800814 if (nsp != NULL)
815 isp->smk_task = nsp;
816 else
817 isp->smk_task = smack_known_invalid.smk_known;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800818 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
819 nsp = smk_import(value, size);
820 if (nsp != NULL)
821 isp->smk_mmap = nsp;
822 else
823 isp->smk_mmap = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200824 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
825 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflere114e472008-02-04 22:29:50 -0800826
827 return;
828}
829
830/*
831 * smack_inode_getxattr - Smack check on getxattr
832 * @dentry: the object
833 * @name: unused
834 *
835 * Returns 0 if access is permitted, an error code otherwise
836 */
David Howells8f0cfa52008-04-29 00:59:41 -0700837static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800838{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200839 struct smk_audit_info ad;
840
841 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
842 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
843
844 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800845}
846
847/*
848 * smack_inode_removexattr - Smack check on removexattr
849 * @dentry: the object
850 * @name: name of the attribute
851 *
852 * Removing the Smack attribute requires CAP_MAC_ADMIN
853 *
854 * Returns 0 if access is permitted, an error code otherwise
855 */
David Howells8f0cfa52008-04-29 00:59:41 -0700856static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800857{
Casey Schaufler676dac42010-12-02 06:43:39 -0800858 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200859 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800860 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800861
Casey Schauflerbcdca222008-02-23 15:24:04 -0800862 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
863 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800864 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200865 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800866 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
867 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800868 if (!capable(CAP_MAC_ADMIN))
869 rc = -EPERM;
870 } else
871 rc = cap_inode_removexattr(dentry, name);
872
Etienne Bassetecfcc532009-04-08 20:40:06 +0200873 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
874 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800875 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200876 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800877
Casey Schaufler676dac42010-12-02 06:43:39 -0800878 if (rc == 0) {
879 isp = dentry->d_inode->i_security;
880 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800881 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800882 }
883
Casey Schauflerbcdca222008-02-23 15:24:04 -0800884 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800885}
886
887/**
888 * smack_inode_getsecurity - get smack xattrs
889 * @inode: the object
890 * @name: attribute name
891 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800892 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800893 *
894 * Returns the size of the attribute or an error code
895 */
896static int smack_inode_getsecurity(const struct inode *inode,
897 const char *name, void **buffer,
898 bool alloc)
899{
900 struct socket_smack *ssp;
901 struct socket *sock;
902 struct super_block *sbp;
903 struct inode *ip = (struct inode *)inode;
904 char *isp;
905 int ilen;
906 int rc = 0;
907
908 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
909 isp = smk_of_inode(inode);
910 ilen = strlen(isp) + 1;
911 *buffer = isp;
912 return ilen;
913 }
914
915 /*
916 * The rest of the Smack xattrs are only on sockets.
917 */
918 sbp = ip->i_sb;
919 if (sbp->s_magic != SOCKFS_MAGIC)
920 return -EOPNOTSUPP;
921
922 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800923 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800924 return -EOPNOTSUPP;
925
926 ssp = sock->sk->sk_security;
927
928 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
929 isp = ssp->smk_in;
930 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
931 isp = ssp->smk_out;
932 else
933 return -EOPNOTSUPP;
934
935 ilen = strlen(isp) + 1;
936 if (rc == 0) {
937 *buffer = isp;
938 rc = ilen;
939 }
940
941 return rc;
942}
943
944
945/**
946 * smack_inode_listsecurity - list the Smack attributes
947 * @inode: the object
948 * @buffer: where they go
949 * @buffer_size: size of buffer
950 *
951 * Returns 0 on success, -EINVAL otherwise
952 */
953static int smack_inode_listsecurity(struct inode *inode, char *buffer,
954 size_t buffer_size)
955{
956 int len = strlen(XATTR_NAME_SMACK);
957
958 if (buffer != NULL && len <= buffer_size) {
959 memcpy(buffer, XATTR_NAME_SMACK, len);
960 return len;
961 }
962 return -EINVAL;
963}
964
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +1000965/**
966 * smack_inode_getsecid - Extract inode's security id
967 * @inode: inode to extract the info from
968 * @secid: where result will be saved
969 */
970static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
971{
972 struct inode_smack *isp = inode->i_security;
973
974 *secid = smack_to_secid(isp->smk_inode);
975}
976
Casey Schauflere114e472008-02-04 22:29:50 -0800977/*
978 * File Hooks
979 */
980
981/**
982 * smack_file_permission - Smack check on file operations
983 * @file: unused
984 * @mask: unused
985 *
986 * Returns 0
987 *
988 * Should access checks be done on each read or write?
989 * UNICOS and SELinux say yes.
990 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
991 *
992 * I'll say no for now. Smack does not do the frequent
993 * label changing that SELinux does.
994 */
995static int smack_file_permission(struct file *file, int mask)
996{
997 return 0;
998}
999
1000/**
1001 * smack_file_alloc_security - assign a file security blob
1002 * @file: the object
1003 *
1004 * The security blob for a file is a pointer to the master
1005 * label list, so no allocation is done.
1006 *
1007 * Returns 0
1008 */
1009static int smack_file_alloc_security(struct file *file)
1010{
Casey Schaufler676dac42010-12-02 06:43:39 -08001011 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001012 return 0;
1013}
1014
1015/**
1016 * smack_file_free_security - clear a file security blob
1017 * @file: the object
1018 *
1019 * The security blob for a file is a pointer to the master
1020 * label list, so no memory is freed.
1021 */
1022static void smack_file_free_security(struct file *file)
1023{
1024 file->f_security = NULL;
1025}
1026
1027/**
1028 * smack_file_ioctl - Smack check on ioctls
1029 * @file: the object
1030 * @cmd: what to do
1031 * @arg: unused
1032 *
1033 * Relies heavily on the correct use of the ioctl command conventions.
1034 *
1035 * Returns 0 if allowed, error code otherwise
1036 */
1037static int smack_file_ioctl(struct file *file, unsigned int cmd,
1038 unsigned long arg)
1039{
1040 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001041 struct smk_audit_info ad;
1042
1043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1044 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001045
1046 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001047 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001048
1049 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001050 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001051
1052 return rc;
1053}
1054
1055/**
1056 * smack_file_lock - Smack check on file locking
1057 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001058 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001059 *
1060 * Returns 0 if current has write access, error code otherwise
1061 */
1062static int smack_file_lock(struct file *file, unsigned int cmd)
1063{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001064 struct smk_audit_info ad;
1065
1066 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1067 smk_ad_setfield_u_fs_path_dentry(&ad, file->f_path.dentry);
1068 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001069}
1070
1071/**
1072 * smack_file_fcntl - Smack check on fcntl
1073 * @file: the object
1074 * @cmd: what action to check
1075 * @arg: unused
1076 *
1077 * Returns 0 if current has access, error code otherwise
1078 */
1079static int smack_file_fcntl(struct file *file, unsigned int cmd,
1080 unsigned long arg)
1081{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001082 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001083 int rc;
1084
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_FS);
1086 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1087
Casey Schauflere114e472008-02-04 22:29:50 -08001088 switch (cmd) {
1089 case F_DUPFD:
1090 case F_GETFD:
1091 case F_GETFL:
1092 case F_GETLK:
1093 case F_GETOWN:
1094 case F_GETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001095 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001096 break;
1097 case F_SETFD:
1098 case F_SETFL:
1099 case F_SETLK:
1100 case F_SETLKW:
1101 case F_SETOWN:
1102 case F_SETSIG:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001104 break;
1105 default:
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 rc = smk_curacc(file->f_security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001107 }
1108
1109 return rc;
1110}
1111
1112/**
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001113 * smack_file_mmap :
1114 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1115 * if mapping anonymous memory.
1116 * @file contains the file structure for file to map (may be NULL).
1117 * @reqprot contains the protection requested by the application.
1118 * @prot contains the protection that will be applied by the kernel.
1119 * @flags contains the operational flags.
1120 * Return 0 if permission is granted.
1121 */
1122static int smack_file_mmap(struct file *file,
1123 unsigned long reqprot, unsigned long prot,
1124 unsigned long flags, unsigned long addr,
1125 unsigned long addr_only)
1126{
1127 struct smack_rule *srp;
1128 struct task_smack *tsp;
1129 char *sp;
1130 char *msmack;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001131 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001132 struct inode_smack *isp;
1133 struct dentry *dp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001134 int may;
1135 int mmay;
1136 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001137 int rc;
1138
1139 /* do DAC check on address space usage */
1140 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1141 if (rc || addr_only)
1142 return rc;
1143
1144 if (file == NULL || file->f_dentry == NULL)
1145 return 0;
1146
1147 dp = file->f_dentry;
1148
1149 if (dp->d_inode == NULL)
1150 return 0;
1151
1152 isp = dp->d_inode->i_security;
1153 if (isp->smk_mmap == NULL)
1154 return 0;
1155 msmack = isp->smk_mmap;
1156
1157 tsp = current_security();
1158 sp = smk_of_current();
1159 rc = 0;
1160
1161 rcu_read_lock();
1162 /*
1163 * For each Smack rule associated with the subject
1164 * label verify that the SMACK64MMAP also has access
1165 * to that rule's object label.
1166 *
1167 * Because neither of the labels comes
1168 * from the networking code it is sufficient
1169 * to compare pointers.
1170 */
1171 list_for_each_entry_rcu(srp, &smack_rule_list, list) {
1172 if (srp->smk_subject != sp)
1173 continue;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001174
1175 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001176 /*
1177 * Matching labels always allows access.
1178 */
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001179 if (msmack == osmack)
1180 continue;
1181 /*
1182 * If there is a matching local rule take
1183 * that into account as well.
1184 */
1185 may = smk_access_entry(srp->smk_subject, osmack,
1186 &tsp->smk_rules);
1187 if (may == -ENOENT)
1188 may = srp->smk_access;
1189 else
1190 may &= srp->smk_access;
1191 /*
1192 * If may is zero the SMACK64MMAP subject can't
1193 * possibly have less access.
1194 */
1195 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001196 continue;
1197
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001198 /*
1199 * Fetch the global list entry.
1200 * If there isn't one a SMACK64MMAP subject
1201 * can't have as much access as current.
1202 */
1203 mmay = smk_access_entry(msmack, osmack, &smack_rule_list);
1204 if (mmay == -ENOENT) {
1205 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001206 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001207 }
1208 /*
1209 * If there is a local entry it modifies the
1210 * potential access, too.
1211 */
1212 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1213 if (tmay != -ENOENT)
1214 mmay &= tmay;
1215
1216 /*
1217 * If there is any access available to current that is
1218 * not available to a SMACK64MMAP subject
1219 * deny access.
1220 */
1221 if ((may | mmay) != may) {
1222 rc = -EACCES;
1223 break;
1224 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001225 }
1226
1227 rcu_read_unlock();
1228
1229 return rc;
1230}
1231
1232/**
Casey Schauflere114e472008-02-04 22:29:50 -08001233 * smack_file_set_fowner - set the file security blob value
1234 * @file: object in question
1235 *
1236 * Returns 0
1237 * Further research may be required on this one.
1238 */
1239static int smack_file_set_fowner(struct file *file)
1240{
Casey Schaufler676dac42010-12-02 06:43:39 -08001241 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001242 return 0;
1243}
1244
1245/**
1246 * smack_file_send_sigiotask - Smack on sigio
1247 * @tsk: The target task
1248 * @fown: the object the signal come from
1249 * @signum: unused
1250 *
1251 * Allow a privileged task to get signals even if it shouldn't
1252 *
1253 * Returns 0 if a subject with the object's smack could
1254 * write to the task, an error code otherwise.
1255 */
1256static int smack_file_send_sigiotask(struct task_struct *tsk,
1257 struct fown_struct *fown, int signum)
1258{
1259 struct file *file;
1260 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001261 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001262 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001263
1264 /*
1265 * struct fown_struct is never outside the context of a struct file
1266 */
1267 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001268
Etienne Bassetecfcc532009-04-08 20:40:06 +02001269 /* we don't log here as rc can be overriden */
1270 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001271 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001272 rc = 0;
1273
1274 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1275 smk_ad_setfield_u_tsk(&ad, tsk);
1276 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001277 return rc;
1278}
1279
1280/**
1281 * smack_file_receive - Smack file receive check
1282 * @file: the object
1283 *
1284 * Returns 0 if current has access, error code otherwise
1285 */
1286static int smack_file_receive(struct file *file)
1287{
1288 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001289 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001290
Etienne Bassetecfcc532009-04-08 20:40:06 +02001291 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1292 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001293 /*
1294 * This code relies on bitmasks.
1295 */
1296 if (file->f_mode & FMODE_READ)
1297 may = MAY_READ;
1298 if (file->f_mode & FMODE_WRITE)
1299 may |= MAY_WRITE;
1300
Etienne Bassetecfcc532009-04-08 20:40:06 +02001301 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001302}
1303
1304/*
1305 * Task hooks
1306 */
1307
1308/**
David Howellsee18d642009-09-02 09:14:21 +01001309 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1310 * @new: the new credentials
1311 * @gfp: the atomicity of any memory allocations
1312 *
1313 * Prepare a blank set of credentials for modification. This must allocate all
1314 * the memory the LSM module might require such that cred_transfer() can
1315 * complete without error.
1316 */
1317static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1318{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001319 struct task_smack *tsp;
1320
1321 tsp = new_task_smack(NULL, NULL, gfp);
1322 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001323 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001324
1325 cred->security = tsp;
1326
David Howellsee18d642009-09-02 09:14:21 +01001327 return 0;
1328}
1329
1330
1331/**
David Howellsf1752ee2008-11-14 10:39:17 +11001332 * smack_cred_free - "free" task-level security credentials
1333 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001334 *
Casey Schauflere114e472008-02-04 22:29:50 -08001335 */
David Howellsf1752ee2008-11-14 10:39:17 +11001336static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001337{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001338 struct task_smack *tsp = cred->security;
1339 struct smack_rule *rp;
1340 struct list_head *l;
1341 struct list_head *n;
1342
1343 if (tsp == NULL)
1344 return;
1345 cred->security = NULL;
1346
1347 list_for_each_safe(l, n, &tsp->smk_rules) {
1348 rp = list_entry(l, struct smack_rule, list);
1349 list_del(&rp->list);
1350 kfree(rp);
1351 }
1352 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001353}
1354
1355/**
David Howellsd84f4f92008-11-14 10:39:23 +11001356 * smack_cred_prepare - prepare new set of credentials for modification
1357 * @new: the new credentials
1358 * @old: the original credentials
1359 * @gfp: the atomicity of any memory allocations
1360 *
1361 * Prepare a new set of credentials for modification.
1362 */
1363static int smack_cred_prepare(struct cred *new, const struct cred *old,
1364 gfp_t gfp)
1365{
Casey Schaufler676dac42010-12-02 06:43:39 -08001366 struct task_smack *old_tsp = old->security;
1367 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001368 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001369
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001370 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001371 if (new_tsp == NULL)
1372 return -ENOMEM;
1373
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001374 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1375 if (rc != 0)
1376 return rc;
1377
Casey Schaufler676dac42010-12-02 06:43:39 -08001378 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001379 return 0;
1380}
1381
Randy Dunlap251a2a92009-02-18 11:42:33 -08001382/**
David Howellsee18d642009-09-02 09:14:21 +01001383 * smack_cred_transfer - Transfer the old credentials to the new credentials
1384 * @new: the new credentials
1385 * @old: the original credentials
1386 *
1387 * Fill in a set of blank credentials from another set of credentials.
1388 */
1389static void smack_cred_transfer(struct cred *new, const struct cred *old)
1390{
Casey Schaufler676dac42010-12-02 06:43:39 -08001391 struct task_smack *old_tsp = old->security;
1392 struct task_smack *new_tsp = new->security;
1393
1394 new_tsp->smk_task = old_tsp->smk_task;
1395 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001396 mutex_init(&new_tsp->smk_rules_lock);
1397 INIT_LIST_HEAD(&new_tsp->smk_rules);
1398
1399
1400 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001401}
1402
1403/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001404 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001405 * @new: points to the set of credentials to be modified.
1406 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001407 *
1408 * Set the security data for a kernel service.
1409 */
1410static int smack_kernel_act_as(struct cred *new, u32 secid)
1411{
Casey Schaufler676dac42010-12-02 06:43:39 -08001412 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001413 char *smack = smack_from_secid(secid);
1414
1415 if (smack == NULL)
1416 return -EINVAL;
1417
Casey Schaufler676dac42010-12-02 06:43:39 -08001418 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001419 return 0;
1420}
1421
1422/**
1423 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001424 * @new: points to the set of credentials to be modified
1425 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001426 *
1427 * Set the file creation context in a set of credentials to the same
1428 * as the objective context of the specified inode
1429 */
1430static int smack_kernel_create_files_as(struct cred *new,
1431 struct inode *inode)
1432{
1433 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001434 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001435
Casey Schaufler676dac42010-12-02 06:43:39 -08001436 tsp->smk_forked = isp->smk_inode;
1437 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001438 return 0;
1439}
1440
1441/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001442 * smk_curacc_on_task - helper to log task related access
1443 * @p: the task object
1444 * @access : the access requested
1445 *
1446 * Return 0 if access is permitted
1447 */
1448static int smk_curacc_on_task(struct task_struct *p, int access)
1449{
1450 struct smk_audit_info ad;
1451
1452 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1453 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001454 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001455}
1456
1457/**
Casey Schauflere114e472008-02-04 22:29:50 -08001458 * smack_task_setpgid - Smack check on setting pgid
1459 * @p: the task object
1460 * @pgid: unused
1461 *
1462 * Return 0 if write access is permitted
1463 */
1464static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1465{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001466 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001467}
1468
1469/**
1470 * smack_task_getpgid - Smack access check for getpgid
1471 * @p: the object task
1472 *
1473 * Returns 0 if current can read the object task, error code otherwise
1474 */
1475static int smack_task_getpgid(struct task_struct *p)
1476{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001477 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001478}
1479
1480/**
1481 * smack_task_getsid - Smack access check for getsid
1482 * @p: the object task
1483 *
1484 * Returns 0 if current can read the object task, error code otherwise
1485 */
1486static int smack_task_getsid(struct task_struct *p)
1487{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001488 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001489}
1490
1491/**
1492 * smack_task_getsecid - get the secid of the task
1493 * @p: the object task
1494 * @secid: where to put the result
1495 *
1496 * Sets the secid to contain a u32 version of the smack label.
1497 */
1498static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1499{
Casey Schaufler676dac42010-12-02 06:43:39 -08001500 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001501}
1502
1503/**
1504 * smack_task_setnice - Smack check on setting nice
1505 * @p: the task object
1506 * @nice: unused
1507 *
1508 * Return 0 if write access is permitted
1509 */
1510static int smack_task_setnice(struct task_struct *p, int nice)
1511{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001512 int rc;
1513
1514 rc = cap_task_setnice(p, nice);
1515 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001516 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001517 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001518}
1519
1520/**
1521 * smack_task_setioprio - Smack check on setting ioprio
1522 * @p: the task object
1523 * @ioprio: unused
1524 *
1525 * Return 0 if write access is permitted
1526 */
1527static int smack_task_setioprio(struct task_struct *p, int ioprio)
1528{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001529 int rc;
1530
1531 rc = cap_task_setioprio(p, ioprio);
1532 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001533 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001534 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001535}
1536
1537/**
1538 * smack_task_getioprio - Smack check on reading ioprio
1539 * @p: the task object
1540 *
1541 * Return 0 if read access is permitted
1542 */
1543static int smack_task_getioprio(struct task_struct *p)
1544{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001545 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001546}
1547
1548/**
1549 * smack_task_setscheduler - Smack check on setting scheduler
1550 * @p: the task object
1551 * @policy: unused
1552 * @lp: unused
1553 *
1554 * Return 0 if read access is permitted
1555 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001556static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001557{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001558 int rc;
1559
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001560 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001561 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001562 rc = smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001563 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001564}
1565
1566/**
1567 * smack_task_getscheduler - Smack check on reading scheduler
1568 * @p: the task object
1569 *
1570 * Return 0 if read access is permitted
1571 */
1572static int smack_task_getscheduler(struct task_struct *p)
1573{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001574 return smk_curacc_on_task(p, MAY_READ);
Casey Schauflere114e472008-02-04 22:29:50 -08001575}
1576
1577/**
1578 * smack_task_movememory - Smack check on moving memory
1579 * @p: the task object
1580 *
1581 * Return 0 if write access is permitted
1582 */
1583static int smack_task_movememory(struct task_struct *p)
1584{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001585 return smk_curacc_on_task(p, MAY_WRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08001586}
1587
1588/**
1589 * smack_task_kill - Smack check on signal delivery
1590 * @p: the task object
1591 * @info: unused
1592 * @sig: unused
1593 * @secid: identifies the smack to use in lieu of current's
1594 *
1595 * Return 0 if write access is permitted
1596 *
1597 * The secid behavior is an artifact of an SELinux hack
1598 * in the USB code. Someday it may go away.
1599 */
1600static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1601 int sig, u32 secid)
1602{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001603 struct smk_audit_info ad;
1604
1605 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1606 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001607 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001608 * Sending a signal requires that the sender
1609 * can write the receiver.
1610 */
1611 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001612 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1613 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001614 /*
1615 * If the secid isn't 0 we're dealing with some USB IO
1616 * specific behavior. This is not clean. For one thing
1617 * we can't take privilege into account.
1618 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001619 return smk_access(smack_from_secid(secid),
1620 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001621}
1622
1623/**
1624 * smack_task_wait - Smack access check for waiting
1625 * @p: task to wait for
1626 *
1627 * Returns 0 if current can wait for p, error code otherwise
1628 */
1629static int smack_task_wait(struct task_struct *p)
1630{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001631 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08001632 char *sp = smk_of_current();
1633 char *tsp = smk_of_forked(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001634 int rc;
1635
Etienne Bassetecfcc532009-04-08 20:40:06 +02001636 /* we don't log here, we can be overriden */
Casey Schaufler676dac42010-12-02 06:43:39 -08001637 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001638 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001639 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001640
1641 /*
1642 * Allow the operation to succeed if either task
1643 * has privilege to perform operations that might
1644 * account for the smack labels having gotten to
1645 * be different in the first place.
1646 *
David Howells5cd9c582008-08-14 11:37:28 +01001647 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001648 * control ideal, taking the object's privilege
1649 * state into account in the decision as well as
1650 * the smack value.
1651 */
David Howells5cd9c582008-08-14 11:37:28 +01001652 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001653 rc = 0;
1654 /* we log only if we didn't get overriden */
1655 out_log:
1656 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1657 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001658 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001659 return rc;
1660}
1661
1662/**
1663 * smack_task_to_inode - copy task smack into the inode blob
1664 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001665 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001666 *
1667 * Sets the smack pointer in the inode security blob
1668 */
1669static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1670{
1671 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001672 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001673}
1674
1675/*
1676 * Socket hooks.
1677 */
1678
1679/**
1680 * smack_sk_alloc_security - Allocate a socket blob
1681 * @sk: the socket
1682 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001683 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001684 *
1685 * Assign Smack pointers to current
1686 *
1687 * Returns 0 on success, -ENOMEM is there's no memory
1688 */
1689static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1690{
Casey Schaufler676dac42010-12-02 06:43:39 -08001691 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001692 struct socket_smack *ssp;
1693
1694 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1695 if (ssp == NULL)
1696 return -ENOMEM;
1697
1698 ssp->smk_in = csp;
1699 ssp->smk_out = csp;
1700 ssp->smk_packet[0] = '\0';
1701
1702 sk->sk_security = ssp;
1703
1704 return 0;
1705}
1706
1707/**
1708 * smack_sk_free_security - Free a socket blob
1709 * @sk: the socket
1710 *
1711 * Clears the blob pointer
1712 */
1713static void smack_sk_free_security(struct sock *sk)
1714{
1715 kfree(sk->sk_security);
1716}
1717
1718/**
Paul Moore07feee82009-03-27 17:10:54 -04001719* smack_host_label - check host based restrictions
1720* @sip: the object end
1721*
1722* looks for host based access restrictions
1723*
1724* This version will only be appropriate for really small sets of single label
1725* hosts. The caller is responsible for ensuring that the RCU read lock is
1726* taken before calling this function.
1727*
1728* Returns the label of the far end or NULL if it's not special.
1729*/
1730static char *smack_host_label(struct sockaddr_in *sip)
1731{
1732 struct smk_netlbladdr *snp;
1733 struct in_addr *siap = &sip->sin_addr;
1734
1735 if (siap->s_addr == 0)
1736 return NULL;
1737
1738 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1739 /*
1740 * we break after finding the first match because
1741 * the list is sorted from longest to shortest mask
1742 * so we have found the most specific match
1743 */
1744 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001745 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1746 /* we have found the special CIPSO option */
1747 if (snp->smk_label == smack_cipso_option)
1748 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001749 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001750 }
Paul Moore07feee82009-03-27 17:10:54 -04001751
1752 return NULL;
1753}
1754
1755/**
Casey Schauflere114e472008-02-04 22:29:50 -08001756 * smack_set_catset - convert a capset to netlabel mls categories
1757 * @catset: the Smack categories
1758 * @sap: where to put the netlabel categories
1759 *
1760 * Allocates and fills attr.mls.cat
1761 */
1762static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1763{
1764 unsigned char *cp;
1765 unsigned char m;
1766 int cat;
1767 int rc;
1768 int byte;
1769
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001770 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001771 return;
1772
1773 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1774 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1775 sap->attr.mls.cat->startbit = 0;
1776
1777 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1778 for (m = 0x80; m != 0; m >>= 1, cat++) {
1779 if ((m & *cp) == 0)
1780 continue;
1781 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1782 cat, GFP_ATOMIC);
1783 }
1784}
1785
1786/**
1787 * smack_to_secattr - fill a secattr from a smack value
1788 * @smack: the smack value
1789 * @nlsp: where the result goes
1790 *
1791 * Casey says that CIPSO is good enough for now.
1792 * It can be used to effect.
1793 * It can also be abused to effect when necessary.
1794 * Appologies to the TSIG group in general and GW in particular.
1795 */
1796static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1797{
1798 struct smack_cipso cipso;
1799 int rc;
1800
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001801 nlsp->domain = smack;
1802 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001803
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001804 rc = smack_to_cipso(smack, &cipso);
1805 if (rc == 0) {
1806 nlsp->attr.mls.lvl = cipso.smk_level;
1807 smack_set_catset(cipso.smk_catset, nlsp);
1808 } else {
1809 nlsp->attr.mls.lvl = smack_cipso_direct;
1810 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001811 }
1812}
1813
1814/**
1815 * smack_netlabel - Set the secattr on a socket
1816 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001817 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001818 *
1819 * Convert the outbound smack value (smk_out) to a
1820 * secattr and attach it to the socket.
1821 *
1822 * Returns 0 on success or an error code
1823 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001824static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001825{
Paul Moore07feee82009-03-27 17:10:54 -04001826 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001827 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001828 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001829
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001830 /*
1831 * Usually the netlabel code will handle changing the
1832 * packet labeling based on the label.
1833 * The case of a single label host is different, because
1834 * a single label host should never get a labeled packet
1835 * even though the label is usually associated with a packet
1836 * label.
1837 */
1838 local_bh_disable();
1839 bh_lock_sock_nested(sk);
1840
1841 if (ssp->smk_out == smack_net_ambient ||
1842 labeled == SMACK_UNLABELED_SOCKET)
1843 netlbl_sock_delattr(sk);
1844 else {
1845 netlbl_secattr_init(&secattr);
1846 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001847 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001848 netlbl_secattr_destroy(&secattr);
1849 }
1850
1851 bh_unlock_sock(sk);
1852 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001853
Casey Schauflere114e472008-02-04 22:29:50 -08001854 return rc;
1855}
1856
1857/**
Paul Moore07feee82009-03-27 17:10:54 -04001858 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1859 * @sk: the socket
1860 * @sap: the destination address
1861 *
1862 * Set the correct secattr for the given socket based on the destination
1863 * address and perform any outbound access checks needed.
1864 *
1865 * Returns 0 on success or an error code.
1866 *
1867 */
1868static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1869{
1870 int rc;
1871 int sk_lbl;
1872 char *hostsp;
1873 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001874 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001875
1876 rcu_read_lock();
1877 hostsp = smack_host_label(sap);
1878 if (hostsp != NULL) {
1879 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001880#ifdef CONFIG_AUDIT
1881 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1882 ad.a.u.net.family = sap->sin_family;
1883 ad.a.u.net.dport = sap->sin_port;
1884 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1885#endif
1886 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001887 } else {
1888 sk_lbl = SMACK_CIPSO_SOCKET;
1889 rc = 0;
1890 }
1891 rcu_read_unlock();
1892 if (rc != 0)
1893 return rc;
1894
1895 return smack_netlabel(sk, sk_lbl);
1896}
1897
1898/**
Casey Schauflere114e472008-02-04 22:29:50 -08001899 * smack_inode_setsecurity - set smack xattrs
1900 * @inode: the object
1901 * @name: attribute name
1902 * @value: attribute value
1903 * @size: size of the attribute
1904 * @flags: unused
1905 *
1906 * Sets the named attribute in the appropriate blob
1907 *
1908 * Returns 0 on success, or an error code
1909 */
1910static int smack_inode_setsecurity(struct inode *inode, const char *name,
1911 const void *value, size_t size, int flags)
1912{
1913 char *sp;
1914 struct inode_smack *nsp = inode->i_security;
1915 struct socket_smack *ssp;
1916 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001917 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001918
Etienne Basset43031542009-03-27 17:11:01 -04001919 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001920 return -EACCES;
1921
1922 sp = smk_import(value, size);
1923 if (sp == NULL)
1924 return -EINVAL;
1925
1926 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1927 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04001928 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08001929 return 0;
1930 }
1931 /*
1932 * The rest of the Smack xattrs are only on sockets.
1933 */
1934 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1935 return -EOPNOTSUPP;
1936
1937 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001938 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001939 return -EOPNOTSUPP;
1940
1941 ssp = sock->sk->sk_security;
1942
1943 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1944 ssp->smk_in = sp;
1945 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1946 ssp->smk_out = sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08001947 if (sock->sk->sk_family != PF_UNIX) {
1948 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1949 if (rc != 0)
1950 printk(KERN_WARNING
1951 "Smack: \"%s\" netlbl error %d.\n",
1952 __func__, -rc);
1953 }
Casey Schauflere114e472008-02-04 22:29:50 -08001954 } else
1955 return -EOPNOTSUPP;
1956
1957 return 0;
1958}
1959
1960/**
1961 * smack_socket_post_create - finish socket setup
1962 * @sock: the socket
1963 * @family: protocol family
1964 * @type: unused
1965 * @protocol: unused
1966 * @kern: unused
1967 *
1968 * Sets the netlabel information on the socket
1969 *
1970 * Returns 0 on success, and error code otherwise
1971 */
1972static int smack_socket_post_create(struct socket *sock, int family,
1973 int type, int protocol, int kern)
1974{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001975 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001976 return 0;
1977 /*
1978 * Set the outbound netlbl.
1979 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001980 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1981}
1982
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001983/**
1984 * smack_socket_connect - connect access check
1985 * @sock: the socket
1986 * @sap: the other end
1987 * @addrlen: size of sap
1988 *
1989 * Verifies that a connection may be possible
1990 *
1991 * Returns 0 on success, and error code otherwise
1992 */
1993static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
1994 int addrlen)
1995{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001996 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
1997 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001998 if (addrlen < sizeof(struct sockaddr_in))
1999 return -EINVAL;
2000
Paul Moore07feee82009-03-27 17:10:54 -04002001 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08002002}
2003
2004/**
2005 * smack_flags_to_may - convert S_ to MAY_ values
2006 * @flags: the S_ value
2007 *
2008 * Returns the equivalent MAY_ value
2009 */
2010static int smack_flags_to_may(int flags)
2011{
2012 int may = 0;
2013
2014 if (flags & S_IRUGO)
2015 may |= MAY_READ;
2016 if (flags & S_IWUGO)
2017 may |= MAY_WRITE;
2018 if (flags & S_IXUGO)
2019 may |= MAY_EXEC;
2020
2021 return may;
2022}
2023
2024/**
2025 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2026 * @msg: the object
2027 *
2028 * Returns 0
2029 */
2030static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2031{
Casey Schaufler676dac42010-12-02 06:43:39 -08002032 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002033 return 0;
2034}
2035
2036/**
2037 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2038 * @msg: the object
2039 *
2040 * Clears the blob pointer
2041 */
2042static void smack_msg_msg_free_security(struct msg_msg *msg)
2043{
2044 msg->security = NULL;
2045}
2046
2047/**
2048 * smack_of_shm - the smack pointer for the shm
2049 * @shp: the object
2050 *
2051 * Returns a pointer to the smack value
2052 */
2053static char *smack_of_shm(struct shmid_kernel *shp)
2054{
2055 return (char *)shp->shm_perm.security;
2056}
2057
2058/**
2059 * smack_shm_alloc_security - Set the security blob for shm
2060 * @shp: the object
2061 *
2062 * Returns 0
2063 */
2064static int smack_shm_alloc_security(struct shmid_kernel *shp)
2065{
2066 struct kern_ipc_perm *isp = &shp->shm_perm;
2067
Casey Schaufler676dac42010-12-02 06:43:39 -08002068 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002069 return 0;
2070}
2071
2072/**
2073 * smack_shm_free_security - Clear the security blob for shm
2074 * @shp: the object
2075 *
2076 * Clears the blob pointer
2077 */
2078static void smack_shm_free_security(struct shmid_kernel *shp)
2079{
2080 struct kern_ipc_perm *isp = &shp->shm_perm;
2081
2082 isp->security = NULL;
2083}
2084
2085/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002086 * smk_curacc_shm : check if current has access on shm
2087 * @shp : the object
2088 * @access : access requested
2089 *
2090 * Returns 0 if current has the requested access, error code otherwise
2091 */
2092static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2093{
2094 char *ssp = smack_of_shm(shp);
2095 struct smk_audit_info ad;
2096
2097#ifdef CONFIG_AUDIT
2098 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2099 ad.a.u.ipc_id = shp->shm_perm.id;
2100#endif
2101 return smk_curacc(ssp, access, &ad);
2102}
2103
2104/**
Casey Schauflere114e472008-02-04 22:29:50 -08002105 * smack_shm_associate - Smack access check for shm
2106 * @shp: the object
2107 * @shmflg: access requested
2108 *
2109 * Returns 0 if current has the requested access, error code otherwise
2110 */
2111static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2112{
Casey Schauflere114e472008-02-04 22:29:50 -08002113 int may;
2114
2115 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002116 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002117}
2118
2119/**
2120 * smack_shm_shmctl - Smack access check for shm
2121 * @shp: the object
2122 * @cmd: what it wants to do
2123 *
2124 * Returns 0 if current has the requested access, error code otherwise
2125 */
2126static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2127{
Casey Schauflere114e472008-02-04 22:29:50 -08002128 int may;
2129
2130 switch (cmd) {
2131 case IPC_STAT:
2132 case SHM_STAT:
2133 may = MAY_READ;
2134 break;
2135 case IPC_SET:
2136 case SHM_LOCK:
2137 case SHM_UNLOCK:
2138 case IPC_RMID:
2139 may = MAY_READWRITE;
2140 break;
2141 case IPC_INFO:
2142 case SHM_INFO:
2143 /*
2144 * System level information.
2145 */
2146 return 0;
2147 default:
2148 return -EINVAL;
2149 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002150 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002151}
2152
2153/**
2154 * smack_shm_shmat - Smack access for shmat
2155 * @shp: the object
2156 * @shmaddr: unused
2157 * @shmflg: access requested
2158 *
2159 * Returns 0 if current has the requested access, error code otherwise
2160 */
2161static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2162 int shmflg)
2163{
Casey Schauflere114e472008-02-04 22:29:50 -08002164 int may;
2165
2166 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002167 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002168}
2169
2170/**
2171 * smack_of_sem - the smack pointer for the sem
2172 * @sma: the object
2173 *
2174 * Returns a pointer to the smack value
2175 */
2176static char *smack_of_sem(struct sem_array *sma)
2177{
2178 return (char *)sma->sem_perm.security;
2179}
2180
2181/**
2182 * smack_sem_alloc_security - Set the security blob for sem
2183 * @sma: the object
2184 *
2185 * Returns 0
2186 */
2187static int smack_sem_alloc_security(struct sem_array *sma)
2188{
2189 struct kern_ipc_perm *isp = &sma->sem_perm;
2190
Casey Schaufler676dac42010-12-02 06:43:39 -08002191 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002192 return 0;
2193}
2194
2195/**
2196 * smack_sem_free_security - Clear the security blob for sem
2197 * @sma: the object
2198 *
2199 * Clears the blob pointer
2200 */
2201static void smack_sem_free_security(struct sem_array *sma)
2202{
2203 struct kern_ipc_perm *isp = &sma->sem_perm;
2204
2205 isp->security = NULL;
2206}
2207
2208/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002209 * smk_curacc_sem : check if current has access on sem
2210 * @sma : the object
2211 * @access : access requested
2212 *
2213 * Returns 0 if current has the requested access, error code otherwise
2214 */
2215static int smk_curacc_sem(struct sem_array *sma, int access)
2216{
2217 char *ssp = smack_of_sem(sma);
2218 struct smk_audit_info ad;
2219
2220#ifdef CONFIG_AUDIT
2221 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2222 ad.a.u.ipc_id = sma->sem_perm.id;
2223#endif
2224 return smk_curacc(ssp, access, &ad);
2225}
2226
2227/**
Casey Schauflere114e472008-02-04 22:29:50 -08002228 * smack_sem_associate - Smack access check for sem
2229 * @sma: the object
2230 * @semflg: access requested
2231 *
2232 * Returns 0 if current has the requested access, error code otherwise
2233 */
2234static int smack_sem_associate(struct sem_array *sma, int semflg)
2235{
Casey Schauflere114e472008-02-04 22:29:50 -08002236 int may;
2237
2238 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002239 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002240}
2241
2242/**
2243 * smack_sem_shmctl - Smack access check for sem
2244 * @sma: the object
2245 * @cmd: what it wants to do
2246 *
2247 * Returns 0 if current has the requested access, error code otherwise
2248 */
2249static int smack_sem_semctl(struct sem_array *sma, int cmd)
2250{
Casey Schauflere114e472008-02-04 22:29:50 -08002251 int may;
2252
2253 switch (cmd) {
2254 case GETPID:
2255 case GETNCNT:
2256 case GETZCNT:
2257 case GETVAL:
2258 case GETALL:
2259 case IPC_STAT:
2260 case SEM_STAT:
2261 may = MAY_READ;
2262 break;
2263 case SETVAL:
2264 case SETALL:
2265 case IPC_RMID:
2266 case IPC_SET:
2267 may = MAY_READWRITE;
2268 break;
2269 case IPC_INFO:
2270 case SEM_INFO:
2271 /*
2272 * System level information
2273 */
2274 return 0;
2275 default:
2276 return -EINVAL;
2277 }
2278
Etienne Bassetecfcc532009-04-08 20:40:06 +02002279 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002280}
2281
2282/**
2283 * smack_sem_semop - Smack checks of semaphore operations
2284 * @sma: the object
2285 * @sops: unused
2286 * @nsops: unused
2287 * @alter: unused
2288 *
2289 * Treated as read and write in all cases.
2290 *
2291 * Returns 0 if access is allowed, error code otherwise
2292 */
2293static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2294 unsigned nsops, int alter)
2295{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002296 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002297}
2298
2299/**
2300 * smack_msg_alloc_security - Set the security blob for msg
2301 * @msq: the object
2302 *
2303 * Returns 0
2304 */
2305static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2306{
2307 struct kern_ipc_perm *kisp = &msq->q_perm;
2308
Casey Schaufler676dac42010-12-02 06:43:39 -08002309 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002310 return 0;
2311}
2312
2313/**
2314 * smack_msg_free_security - Clear the security blob for msg
2315 * @msq: the object
2316 *
2317 * Clears the blob pointer
2318 */
2319static void smack_msg_queue_free_security(struct msg_queue *msq)
2320{
2321 struct kern_ipc_perm *kisp = &msq->q_perm;
2322
2323 kisp->security = NULL;
2324}
2325
2326/**
2327 * smack_of_msq - the smack pointer for the msq
2328 * @msq: the object
2329 *
2330 * Returns a pointer to the smack value
2331 */
2332static char *smack_of_msq(struct msg_queue *msq)
2333{
2334 return (char *)msq->q_perm.security;
2335}
2336
2337/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002338 * smk_curacc_msq : helper to check if current has access on msq
2339 * @msq : the msq
2340 * @access : access requested
2341 *
2342 * return 0 if current has access, error otherwise
2343 */
2344static int smk_curacc_msq(struct msg_queue *msq, int access)
2345{
2346 char *msp = smack_of_msq(msq);
2347 struct smk_audit_info ad;
2348
2349#ifdef CONFIG_AUDIT
2350 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2351 ad.a.u.ipc_id = msq->q_perm.id;
2352#endif
2353 return smk_curacc(msp, access, &ad);
2354}
2355
2356/**
Casey Schauflere114e472008-02-04 22:29:50 -08002357 * smack_msg_queue_associate - Smack access check for msg_queue
2358 * @msq: the object
2359 * @msqflg: access requested
2360 *
2361 * Returns 0 if current has the requested access, error code otherwise
2362 */
2363static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2364{
Casey Schauflere114e472008-02-04 22:29:50 -08002365 int may;
2366
2367 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002368 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002369}
2370
2371/**
2372 * smack_msg_queue_msgctl - Smack access check for msg_queue
2373 * @msq: the object
2374 * @cmd: what it wants to do
2375 *
2376 * Returns 0 if current has the requested access, error code otherwise
2377 */
2378static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2379{
Casey Schauflere114e472008-02-04 22:29:50 -08002380 int may;
2381
2382 switch (cmd) {
2383 case IPC_STAT:
2384 case MSG_STAT:
2385 may = MAY_READ;
2386 break;
2387 case IPC_SET:
2388 case IPC_RMID:
2389 may = MAY_READWRITE;
2390 break;
2391 case IPC_INFO:
2392 case MSG_INFO:
2393 /*
2394 * System level information
2395 */
2396 return 0;
2397 default:
2398 return -EINVAL;
2399 }
2400
Etienne Bassetecfcc532009-04-08 20:40:06 +02002401 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002402}
2403
2404/**
2405 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2406 * @msq: the object
2407 * @msg: unused
2408 * @msqflg: access requested
2409 *
2410 * Returns 0 if current has the requested access, error code otherwise
2411 */
2412static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2413 int msqflg)
2414{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002415 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002416
Etienne Bassetecfcc532009-04-08 20:40:06 +02002417 may = smack_flags_to_may(msqflg);
2418 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002419}
2420
2421/**
2422 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2423 * @msq: the object
2424 * @msg: unused
2425 * @target: unused
2426 * @type: unused
2427 * @mode: unused
2428 *
2429 * Returns 0 if current has read and write access, error code otherwise
2430 */
2431static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2432 struct task_struct *target, long type, int mode)
2433{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002434 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002435}
2436
2437/**
2438 * smack_ipc_permission - Smack access for ipc_permission()
2439 * @ipp: the object permissions
2440 * @flag: access requested
2441 *
2442 * Returns 0 if current has read and write access, error code otherwise
2443 */
2444static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2445{
2446 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002447 int may = smack_flags_to_may(flag);
2448 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002449
Etienne Bassetecfcc532009-04-08 20:40:06 +02002450#ifdef CONFIG_AUDIT
2451 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2452 ad.a.u.ipc_id = ipp->id;
2453#endif
2454 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002455}
2456
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002457/**
2458 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002459 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002460 * @secid: where result will be saved
2461 */
2462static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2463{
2464 char *smack = ipp->security;
2465
2466 *secid = smack_to_secid(smack);
2467}
2468
Casey Schauflere114e472008-02-04 22:29:50 -08002469/**
2470 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002471 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002472 * @inode: the object
2473 *
2474 * Set the inode's security blob if it hasn't been done already.
2475 */
2476static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2477{
2478 struct super_block *sbp;
2479 struct superblock_smack *sbsp;
2480 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002481 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002482 char *fetched;
2483 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002484 char trattr[TRANS_TRUE_SIZE];
2485 int transflag = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002486 struct dentry *dp;
2487
2488 if (inode == NULL)
2489 return;
2490
2491 isp = inode->i_security;
2492
2493 mutex_lock(&isp->smk_lock);
2494 /*
2495 * If the inode is already instantiated
2496 * take the quick way out
2497 */
2498 if (isp->smk_flags & SMK_INODE_INSTANT)
2499 goto unlockandout;
2500
2501 sbp = inode->i_sb;
2502 sbsp = sbp->s_security;
2503 /*
2504 * We're going to use the superblock default label
2505 * if there's no label on the file.
2506 */
2507 final = sbsp->smk_default;
2508
2509 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002510 * If this is the root inode the superblock
2511 * may be in the process of initialization.
2512 * If that is the case use the root value out
2513 * of the superblock.
2514 */
2515 if (opt_dentry->d_parent == opt_dentry) {
2516 isp->smk_inode = sbsp->smk_root;
2517 isp->smk_flags |= SMK_INODE_INSTANT;
2518 goto unlockandout;
2519 }
2520
2521 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002522 * This is pretty hackish.
2523 * Casey says that we shouldn't have to do
2524 * file system specific code, but it does help
2525 * with keeping it simple.
2526 */
2527 switch (sbp->s_magic) {
2528 case SMACK_MAGIC:
2529 /*
2530 * Casey says that it's a little embarassing
2531 * that the smack file system doesn't do
2532 * extended attributes.
2533 */
2534 final = smack_known_star.smk_known;
2535 break;
2536 case PIPEFS_MAGIC:
2537 /*
2538 * Casey says pipes are easy (?)
2539 */
2540 final = smack_known_star.smk_known;
2541 break;
2542 case DEVPTS_SUPER_MAGIC:
2543 /*
2544 * devpts seems content with the label of the task.
2545 * Programs that change smack have to treat the
2546 * pty with respect.
2547 */
2548 final = csp;
2549 break;
2550 case SOCKFS_MAGIC:
2551 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002552 * Socket access is controlled by the socket
2553 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002554 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002555 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002556 break;
2557 case PROC_SUPER_MAGIC:
2558 /*
2559 * Casey says procfs appears not to care.
2560 * The superblock default suffices.
2561 */
2562 break;
2563 case TMPFS_MAGIC:
2564 /*
2565 * Device labels should come from the filesystem,
2566 * but watch out, because they're volitile,
2567 * getting recreated on every reboot.
2568 */
2569 final = smack_known_star.smk_known;
2570 /*
2571 * No break.
2572 *
2573 * If a smack value has been set we want to use it,
2574 * but since tmpfs isn't giving us the opportunity
2575 * to set mount options simulate setting the
2576 * superblock default.
2577 */
2578 default:
2579 /*
2580 * This isn't an understood special case.
2581 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002582 */
2583
2584 /*
2585 * UNIX domain sockets use lower level socket data.
2586 */
2587 if (S_ISSOCK(inode->i_mode)) {
2588 final = smack_known_star.smk_known;
2589 break;
2590 }
2591 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002592 * No xattr support means, alas, no SMACK label.
2593 * Use the aforeapplied default.
2594 * It would be curious if the label of the task
2595 * does not match that assigned.
2596 */
2597 if (inode->i_op->getxattr == NULL)
2598 break;
2599 /*
2600 * Get the dentry for xattr.
2601 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002602 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002603 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002604 if (fetched != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -08002605 final = fetched;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002606 if (S_ISDIR(inode->i_mode)) {
2607 trattr[0] = '\0';
2608 inode->i_op->getxattr(dp,
2609 XATTR_NAME_SMACKTRANSMUTE,
2610 trattr, TRANS_TRUE_SIZE);
2611 if (strncmp(trattr, TRANS_TRUE,
2612 TRANS_TRUE_SIZE) == 0)
2613 transflag = SMK_INODE_TRANSMUTE;
2614 }
2615 }
2616 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002617 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002618
Casey Schauflere114e472008-02-04 22:29:50 -08002619 dput(dp);
2620 break;
2621 }
2622
2623 if (final == NULL)
2624 isp->smk_inode = csp;
2625 else
2626 isp->smk_inode = final;
2627
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002628 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002629
2630unlockandout:
2631 mutex_unlock(&isp->smk_lock);
2632 return;
2633}
2634
2635/**
2636 * smack_getprocattr - Smack process attribute access
2637 * @p: the object task
2638 * @name: the name of the attribute in /proc/.../attr
2639 * @value: where to put the result
2640 *
2641 * Places a copy of the task Smack into value
2642 *
2643 * Returns the length of the smack label or an error code
2644 */
2645static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2646{
2647 char *cp;
2648 int slen;
2649
2650 if (strcmp(name, "current") != 0)
2651 return -EINVAL;
2652
Casey Schaufler676dac42010-12-02 06:43:39 -08002653 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002654 if (cp == NULL)
2655 return -ENOMEM;
2656
2657 slen = strlen(cp);
2658 *value = cp;
2659 return slen;
2660}
2661
2662/**
2663 * smack_setprocattr - Smack process attribute setting
2664 * @p: the object task
2665 * @name: the name of the attribute in /proc/.../attr
2666 * @value: the value to set
2667 * @size: the size of the value
2668 *
2669 * Sets the Smack value of the task. Only setting self
2670 * is permitted and only with privilege
2671 *
2672 * Returns the length of the smack label or an error code
2673 */
2674static int smack_setprocattr(struct task_struct *p, char *name,
2675 void *value, size_t size)
2676{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002677 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002678 struct task_smack *tsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002679 struct task_smack *oldtsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002680 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002681 char *newsmack;
2682
Casey Schauflere114e472008-02-04 22:29:50 -08002683 /*
2684 * Changing another process' Smack value is too dangerous
2685 * and supports no sane use case.
2686 */
2687 if (p != current)
2688 return -EPERM;
2689
David Howells5cd9c582008-08-14 11:37:28 +01002690 if (!capable(CAP_MAC_ADMIN))
2691 return -EPERM;
2692
Casey Schauflere114e472008-02-04 22:29:50 -08002693 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2694 return -EINVAL;
2695
2696 if (strcmp(name, "current") != 0)
2697 return -EINVAL;
2698
2699 newsmack = smk_import(value, size);
2700 if (newsmack == NULL)
2701 return -EINVAL;
2702
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002703 /*
2704 * No process is ever allowed the web ("@") label.
2705 */
2706 if (newsmack == smack_known_web.smk_known)
2707 return -EPERM;
2708
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002709 oldtsp = p->cred->security;
David Howellsd84f4f92008-11-14 10:39:23 +11002710 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002711 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002712 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002713
2714 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08002715 if (tsp == NULL) {
2716 kfree(new);
2717 return -ENOMEM;
2718 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002719 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2720 if (rc != 0)
2721 return rc;
2722
Casey Schaufler676dac42010-12-02 06:43:39 -08002723 new->security = tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002724 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002725 return size;
2726}
2727
2728/**
2729 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002730 * @sock: one sock
2731 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002732 * @newsk: unused
2733 *
2734 * Return 0 if a subject with the smack of sock could access
2735 * an object with the smack of other, otherwise an error code
2736 */
David S. Miller3610cda2011-01-05 15:38:53 -08002737static int smack_unix_stream_connect(struct sock *sock,
2738 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002739{
James Morrisd2e7ad12011-01-10 09:46:24 +11002740 struct socket_smack *ssp = sock->sk_security;
2741 struct socket_smack *osp = other->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002742 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002743 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002744
Etienne Bassetecfcc532009-04-08 20:40:06 +02002745 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
David S. Miller3610cda2011-01-05 15:38:53 -08002746 smk_ad_setfield_u_net_sk(&ad, other);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002747
2748 if (!capable(CAP_MAC_OVERRIDE))
2749 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2750
2751 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002752}
2753
2754/**
2755 * smack_unix_may_send - Smack access on UDS
2756 * @sock: one socket
2757 * @other: the other socket
2758 *
2759 * Return 0 if a subject with the smack of sock could access
2760 * an object with the smack of other, otherwise an error code
2761 */
2762static int smack_unix_may_send(struct socket *sock, struct socket *other)
2763{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002764 struct socket_smack *ssp = sock->sk->sk_security;
2765 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002766 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002767 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002768
Etienne Bassetecfcc532009-04-08 20:40:06 +02002769 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2770 smk_ad_setfield_u_net_sk(&ad, other->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002771
2772 if (!capable(CAP_MAC_OVERRIDE))
2773 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2774
2775 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002776}
2777
2778/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002779 * smack_socket_sendmsg - Smack check based on destination host
2780 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002781 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002782 * @size: the size of the message
2783 *
2784 * Return 0 if the current subject can write to the destination
2785 * host. This is only a question if the destination is a single
2786 * label host.
2787 */
2788static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2789 int size)
2790{
2791 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002792
2793 /*
2794 * Perfectly reasonable for this to be NULL
2795 */
Julia Lawallda34d422009-08-05 14:34:55 +02002796 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002797 return 0;
2798
Paul Moore07feee82009-03-27 17:10:54 -04002799 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002800}
2801
2802
2803/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002804 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002805 * @sap: netlabel secattr
2806 * @sip: where to put the result
2807 *
2808 * Copies a smack label into sip
2809 */
2810static void smack_from_secattr(struct netlbl_lsm_secattr *sap, char *sip)
2811{
2812 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002813 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002814 int pcat;
2815
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002816 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002817 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002818 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002819 * If there are flags but no level netlabel isn't
2820 * behaving the way we expect it to.
2821 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002822 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002823 * Without guidance regarding the smack value
2824 * for the packet fall back on the network
2825 * ambient value.
2826 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002827 memset(smack, '\0', SMK_LABELLEN);
2828 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2829 for (pcat = -1;;) {
2830 pcat = netlbl_secattr_catmap_walk(
2831 sap->attr.mls.cat, pcat + 1);
2832 if (pcat < 0)
2833 break;
2834 smack_catset_bit(pcat, smack);
2835 }
2836 /*
2837 * If it is CIPSO using smack direct mapping
2838 * we are already done. WeeHee.
2839 */
2840 if (sap->attr.mls.lvl == smack_cipso_direct) {
2841 memcpy(sip, smack, SMK_MAXLEN);
2842 return;
Casey Schauflere114e472008-02-04 22:29:50 -08002843 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002844 /*
2845 * Look it up in the supplied table if it is not
2846 * a direct mapping.
2847 */
2848 smack_from_cipso(sap->attr.mls.lvl, smack, sip);
2849 return;
2850 }
2851 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2852 /*
2853 * Looks like a fallback, which gives us a secid.
2854 */
2855 sp = smack_from_secid(sap->attr.secid);
2856 /*
2857 * This has got to be a bug because it is
2858 * impossible to specify a fallback without
2859 * specifying the label, which will ensure
2860 * it has a secid, and the only way to get a
2861 * secid is from a fallback.
2862 */
2863 BUG_ON(sp == NULL);
2864 strncpy(sip, sp, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002865 return;
2866 }
2867 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002868 * Without guidance regarding the smack value
2869 * for the packet fall back on the network
2870 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002871 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002872 strncpy(sip, smack_net_ambient, SMK_MAXLEN);
Casey Schauflere114e472008-02-04 22:29:50 -08002873 return;
2874}
2875
2876/**
2877 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2878 * @sk: socket
2879 * @skb: packet
2880 *
2881 * Returns 0 if the packet should be delivered, an error code otherwise
2882 */
2883static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2884{
2885 struct netlbl_lsm_secattr secattr;
2886 struct socket_smack *ssp = sk->sk_security;
2887 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002888 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002889 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002890 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002891 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2892 return 0;
2893
2894 /*
2895 * Translate what netlabel gave us.
2896 */
Casey Schauflere114e472008-02-04 22:29:50 -08002897 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002898
Casey Schauflere114e472008-02-04 22:29:50 -08002899 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002900 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002901 smack_from_secattr(&secattr, smack);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002902 csp = smack;
2903 } else
2904 csp = smack_net_ambient;
2905
Casey Schauflere114e472008-02-04 22:29:50 -08002906 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002907
Etienne Bassetecfcc532009-04-08 20:40:06 +02002908#ifdef CONFIG_AUDIT
2909 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2910 ad.a.u.net.family = sk->sk_family;
Eric Dumazet8964be42009-11-20 15:35:04 -08002911 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002912 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2913#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002914 /*
2915 * Receiving a packet requires that the other end
2916 * be able to write here. Read access is not required.
2917 * This is the simplist possible security model
2918 * for networking.
2919 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002920 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002921 if (rc != 0)
2922 netlbl_skbuff_err(skb, rc, 0);
2923 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002924}
2925
2926/**
2927 * smack_socket_getpeersec_stream - pull in packet label
2928 * @sock: the socket
2929 * @optval: user's destination
2930 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002931 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002932 *
2933 * returns zero on success, an error code otherwise
2934 */
2935static int smack_socket_getpeersec_stream(struct socket *sock,
2936 char __user *optval,
2937 int __user *optlen, unsigned len)
2938{
2939 struct socket_smack *ssp;
2940 int slen;
2941 int rc = 0;
2942
2943 ssp = sock->sk->sk_security;
2944 slen = strlen(ssp->smk_packet) + 1;
2945
2946 if (slen > len)
2947 rc = -ERANGE;
2948 else if (copy_to_user(optval, ssp->smk_packet, slen) != 0)
2949 rc = -EFAULT;
2950
2951 if (put_user(slen, optlen) != 0)
2952 rc = -EFAULT;
2953
2954 return rc;
2955}
2956
2957
2958/**
2959 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002960 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08002961 * @skb: packet data
2962 * @secid: pointer to where to put the secid of the packet
2963 *
2964 * Sets the netlabel socket state on sk from parent
2965 */
2966static int smack_socket_getpeersec_dgram(struct socket *sock,
2967 struct sk_buff *skb, u32 *secid)
2968
2969{
2970 struct netlbl_lsm_secattr secattr;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002971 struct socket_smack *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002972 char smack[SMK_LABELLEN];
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002973 int family = PF_UNSPEC;
2974 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08002975 int rc;
2976
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002977 if (skb != NULL) {
2978 if (skb->protocol == htons(ETH_P_IP))
2979 family = PF_INET;
2980 else if (skb->protocol == htons(ETH_P_IPV6))
2981 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08002982 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002983 if (family == PF_UNSPEC && sock != NULL)
2984 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08002985
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002986 if (family == PF_UNIX) {
2987 sp = sock->sk->sk_security;
2988 s = smack_to_secid(sp->smk_out);
2989 } else if (family == PF_INET || family == PF_INET6) {
2990 /*
2991 * Translate what netlabel gave us.
2992 */
2993 netlbl_secattr_init(&secattr);
2994 rc = netlbl_skbuff_getattr(skb, family, &secattr);
2995 if (rc == 0) {
2996 smack_from_secattr(&secattr, smack);
2997 s = smack_to_secid(smack);
2998 }
2999 netlbl_secattr_destroy(&secattr);
3000 }
3001 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003002 if (s == 0)
3003 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003004 return 0;
3005}
3006
3007/**
Paul Moore07feee82009-03-27 17:10:54 -04003008 * smack_sock_graft - Initialize a newly created socket with an existing sock
3009 * @sk: child sock
3010 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003011 *
Paul Moore07feee82009-03-27 17:10:54 -04003012 * Set the smk_{in,out} state of an existing sock based on the process that
3013 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003014 */
3015static void smack_sock_graft(struct sock *sk, struct socket *parent)
3016{
3017 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08003018
Paul Moore07feee82009-03-27 17:10:54 -04003019 if (sk == NULL ||
3020 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003021 return;
3022
3023 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08003024 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04003025 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003026}
3027
3028/**
3029 * smack_inet_conn_request - Smack access check on connect
3030 * @sk: socket involved
3031 * @skb: packet
3032 * @req: unused
3033 *
3034 * Returns 0 if a task with the packet label could write to
3035 * the socket, otherwise an error code
3036 */
3037static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3038 struct request_sock *req)
3039{
Paul Moore07feee82009-03-27 17:10:54 -04003040 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003041 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003042 struct netlbl_lsm_secattr secattr;
3043 struct sockaddr_in addr;
3044 struct iphdr *hdr;
Casey Schauflere114e472008-02-04 22:29:50 -08003045 char smack[SMK_LABELLEN];
3046 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003047 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003048
Paul Moore07feee82009-03-27 17:10:54 -04003049 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3050 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3051 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08003052
Paul Moore07feee82009-03-27 17:10:54 -04003053 netlbl_secattr_init(&secattr);
3054 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003055 if (rc == 0)
Paul Moore07feee82009-03-27 17:10:54 -04003056 smack_from_secattr(&secattr, smack);
Casey Schauflere114e472008-02-04 22:29:50 -08003057 else
3058 strncpy(smack, smack_known_huh.smk_known, SMK_MAXLEN);
Paul Moore07feee82009-03-27 17:10:54 -04003059 netlbl_secattr_destroy(&secattr);
3060
Etienne Bassetecfcc532009-04-08 20:40:06 +02003061#ifdef CONFIG_AUDIT
3062 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3063 ad.a.u.net.family = family;
Eric Dumazet8964be42009-11-20 15:35:04 -08003064 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003065 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3066#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003067 /*
Paul Moore07feee82009-03-27 17:10:54 -04003068 * Receiving a packet requires that the other end be able to write
3069 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003070 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02003071 rc = smk_access(smack, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003072 if (rc != 0)
3073 return rc;
3074
3075 /*
3076 * Save the peer's label in the request_sock so we can later setup
3077 * smk_packet in the child socket so that SO_PEERCRED can report it.
3078 */
3079 req->peer_secid = smack_to_secid(smack);
3080
3081 /*
3082 * We need to decide if we want to label the incoming connection here
3083 * if we do we only need to label the request_sock and the stack will
3084 * propogate the wire-label to the sock when it is created.
3085 */
3086 hdr = ip_hdr(skb);
3087 addr.sin_addr.s_addr = hdr->saddr;
3088 rcu_read_lock();
3089 if (smack_host_label(&addr) == NULL) {
3090 rcu_read_unlock();
3091 netlbl_secattr_init(&secattr);
3092 smack_to_secattr(smack, &secattr);
3093 rc = netlbl_req_setattr(req, &secattr);
3094 netlbl_secattr_destroy(&secattr);
3095 } else {
3096 rcu_read_unlock();
3097 netlbl_req_delattr(req);
3098 }
Casey Schauflere114e472008-02-04 22:29:50 -08003099
3100 return rc;
3101}
3102
Paul Moore07feee82009-03-27 17:10:54 -04003103/**
3104 * smack_inet_csk_clone - Copy the connection information to the new socket
3105 * @sk: the new socket
3106 * @req: the connection's request_sock
3107 *
3108 * Transfer the connection's peer label to the newly created socket.
3109 */
3110static void smack_inet_csk_clone(struct sock *sk,
3111 const struct request_sock *req)
3112{
3113 struct socket_smack *ssp = sk->sk_security;
3114 char *smack;
3115
3116 if (req->peer_secid != 0) {
3117 smack = smack_from_secid(req->peer_secid);
3118 strncpy(ssp->smk_packet, smack, SMK_MAXLEN);
3119 } else
3120 ssp->smk_packet[0] = '\0';
3121}
3122
Casey Schauflere114e472008-02-04 22:29:50 -08003123/*
3124 * Key management security hooks
3125 *
3126 * Casey has not tested key support very heavily.
3127 * The permission check is most likely too restrictive.
3128 * If you care about keys please have a look.
3129 */
3130#ifdef CONFIG_KEYS
3131
3132/**
3133 * smack_key_alloc - Set the key security blob
3134 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003135 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003136 * @flags: unused
3137 *
3138 * No allocation required
3139 *
3140 * Returns 0
3141 */
David Howellsd84f4f92008-11-14 10:39:23 +11003142static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003143 unsigned long flags)
3144{
Casey Schaufler676dac42010-12-02 06:43:39 -08003145 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003146 return 0;
3147}
3148
3149/**
3150 * smack_key_free - Clear the key security blob
3151 * @key: the object
3152 *
3153 * Clear the blob pointer
3154 */
3155static void smack_key_free(struct key *key)
3156{
3157 key->security = NULL;
3158}
3159
3160/*
3161 * smack_key_permission - Smack access on a key
3162 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003163 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003164 * @perm: unused
3165 *
3166 * Return 0 if the task has read and write to the object,
3167 * an error code otherwise
3168 */
3169static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003170 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003171{
3172 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003173 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08003174 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003175
3176 keyp = key_ref_to_ptr(key_ref);
3177 if (keyp == NULL)
3178 return -EINVAL;
3179 /*
3180 * If the key hasn't been initialized give it access so that
3181 * it may do so.
3182 */
3183 if (keyp->security == NULL)
3184 return 0;
3185 /*
3186 * This should not occur
3187 */
Casey Schaufler676dac42010-12-02 06:43:39 -08003188 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003189 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003190#ifdef CONFIG_AUDIT
3191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3192 ad.a.u.key_struct.key = keyp->serial;
3193 ad.a.u.key_struct.key_desc = keyp->description;
3194#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08003195 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02003196 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003197}
3198#endif /* CONFIG_KEYS */
3199
3200/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003201 * Smack Audit hooks
3202 *
3203 * Audit requires a unique representation of each Smack specific
3204 * rule. This unique representation is used to distinguish the
3205 * object to be audited from remaining kernel objects and also
3206 * works as a glue between the audit hooks.
3207 *
3208 * Since repository entries are added but never deleted, we'll use
3209 * the smack_known label address related to the given audit rule as
3210 * the needed unique representation. This also better fits the smack
3211 * model where nearly everything is a label.
3212 */
3213#ifdef CONFIG_AUDIT
3214
3215/**
3216 * smack_audit_rule_init - Initialize a smack audit rule
3217 * @field: audit rule fields given from user-space (audit.h)
3218 * @op: required testing operator (=, !=, >, <, ...)
3219 * @rulestr: smack label to be audited
3220 * @vrule: pointer to save our own audit rule representation
3221 *
3222 * Prepare to audit cases where (@field @op @rulestr) is true.
3223 * The label to be audited is created if necessay.
3224 */
3225static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3226{
3227 char **rule = (char **)vrule;
3228 *rule = NULL;
3229
3230 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3231 return -EINVAL;
3232
Al Viro5af75d82008-12-16 05:59:26 -05003233 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003234 return -EINVAL;
3235
3236 *rule = smk_import(rulestr, 0);
3237
3238 return 0;
3239}
3240
3241/**
3242 * smack_audit_rule_known - Distinguish Smack audit rules
3243 * @krule: rule of interest, in Audit kernel representation format
3244 *
3245 * This is used to filter Smack rules from remaining Audit ones.
3246 * If it's proved that this rule belongs to us, the
3247 * audit_rule_match hook will be called to do the final judgement.
3248 */
3249static int smack_audit_rule_known(struct audit_krule *krule)
3250{
3251 struct audit_field *f;
3252 int i;
3253
3254 for (i = 0; i < krule->field_count; i++) {
3255 f = &krule->fields[i];
3256
3257 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3258 return 1;
3259 }
3260
3261 return 0;
3262}
3263
3264/**
3265 * smack_audit_rule_match - Audit given object ?
3266 * @secid: security id for identifying the object to test
3267 * @field: audit rule flags given from user-space
3268 * @op: required testing operator
3269 * @vrule: smack internal rule presentation
3270 * @actx: audit context associated with the check
3271 *
3272 * The core Audit hook. It's used to take the decision of
3273 * whether to audit or not to audit a given object.
3274 */
3275static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3276 struct audit_context *actx)
3277{
3278 char *smack;
3279 char *rule = vrule;
3280
3281 if (!rule) {
3282 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3283 "Smack: missing rule\n");
3284 return -ENOENT;
3285 }
3286
3287 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3288 return 0;
3289
3290 smack = smack_from_secid(secid);
3291
3292 /*
3293 * No need to do string comparisons. If a match occurs,
3294 * both pointers will point to the same smack_known
3295 * label.
3296 */
Al Viro5af75d82008-12-16 05:59:26 -05003297 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003298 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003299 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003300 return (rule != smack);
3301
3302 return 0;
3303}
3304
3305/**
3306 * smack_audit_rule_free - free smack rule representation
3307 * @vrule: rule to be freed.
3308 *
3309 * No memory was allocated.
3310 */
3311static void smack_audit_rule_free(void *vrule)
3312{
3313 /* No-op */
3314}
3315
3316#endif /* CONFIG_AUDIT */
3317
Randy Dunlap251a2a92009-02-18 11:42:33 -08003318/**
Casey Schauflere114e472008-02-04 22:29:50 -08003319 * smack_secid_to_secctx - return the smack label for a secid
3320 * @secid: incoming integer
3321 * @secdata: destination
3322 * @seclen: how long it is
3323 *
3324 * Exists for networking code.
3325 */
3326static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3327{
3328 char *sp = smack_from_secid(secid);
3329
Eric Parisd5630b92010-10-13 16:24:48 -04003330 if (secdata)
3331 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003332 *seclen = strlen(sp);
3333 return 0;
3334}
3335
Randy Dunlap251a2a92009-02-18 11:42:33 -08003336/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003337 * smack_secctx_to_secid - return the secid for a smack label
3338 * @secdata: smack label
3339 * @seclen: how long result is
3340 * @secid: outgoing integer
3341 *
3342 * Exists for audit and networking code.
3343 */
David Howellse52c17642008-04-29 20:52:51 +01003344static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003345{
3346 *secid = smack_to_secid(secdata);
3347 return 0;
3348}
3349
Randy Dunlap251a2a92009-02-18 11:42:33 -08003350/**
Casey Schauflere114e472008-02-04 22:29:50 -08003351 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003352 * @secdata: unused
3353 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003354 *
3355 * Exists to make sure nothing gets done, and properly
3356 */
3357static void smack_release_secctx(char *secdata, u32 seclen)
3358{
3359}
3360
David P. Quigley1ee65e32009-09-03 14:25:57 -04003361static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3362{
3363 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3364}
3365
3366static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3367{
3368 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3369}
3370
3371static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3372{
3373 int len = 0;
3374 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3375
3376 if (len < 0)
3377 return len;
3378 *ctxlen = len;
3379 return 0;
3380}
3381
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003382struct security_operations smack_ops = {
3383 .name = "smack",
3384
Ingo Molnar9e488582009-05-07 19:26:19 +10003385 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003386 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003387 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003388
3389 .sb_alloc_security = smack_sb_alloc_security,
3390 .sb_free_security = smack_sb_free_security,
3391 .sb_copy_data = smack_sb_copy_data,
3392 .sb_kern_mount = smack_sb_kern_mount,
3393 .sb_statfs = smack_sb_statfs,
3394 .sb_mount = smack_sb_mount,
3395 .sb_umount = smack_sb_umount,
3396
Casey Schaufler676dac42010-12-02 06:43:39 -08003397 .bprm_set_creds = smack_bprm_set_creds,
3398
Casey Schauflere114e472008-02-04 22:29:50 -08003399 .inode_alloc_security = smack_inode_alloc_security,
3400 .inode_free_security = smack_inode_free_security,
3401 .inode_init_security = smack_inode_init_security,
3402 .inode_link = smack_inode_link,
3403 .inode_unlink = smack_inode_unlink,
3404 .inode_rmdir = smack_inode_rmdir,
3405 .inode_rename = smack_inode_rename,
3406 .inode_permission = smack_inode_permission,
3407 .inode_setattr = smack_inode_setattr,
3408 .inode_getattr = smack_inode_getattr,
3409 .inode_setxattr = smack_inode_setxattr,
3410 .inode_post_setxattr = smack_inode_post_setxattr,
3411 .inode_getxattr = smack_inode_getxattr,
3412 .inode_removexattr = smack_inode_removexattr,
3413 .inode_getsecurity = smack_inode_getsecurity,
3414 .inode_setsecurity = smack_inode_setsecurity,
3415 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003416 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003417
3418 .file_permission = smack_file_permission,
3419 .file_alloc_security = smack_file_alloc_security,
3420 .file_free_security = smack_file_free_security,
3421 .file_ioctl = smack_file_ioctl,
3422 .file_lock = smack_file_lock,
3423 .file_fcntl = smack_file_fcntl,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003424 .file_mmap = smack_file_mmap,
Casey Schauflere114e472008-02-04 22:29:50 -08003425 .file_set_fowner = smack_file_set_fowner,
3426 .file_send_sigiotask = smack_file_send_sigiotask,
3427 .file_receive = smack_file_receive,
3428
David Howellsee18d642009-09-02 09:14:21 +01003429 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003430 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003431 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003432 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003433 .kernel_act_as = smack_kernel_act_as,
3434 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003435 .task_setpgid = smack_task_setpgid,
3436 .task_getpgid = smack_task_getpgid,
3437 .task_getsid = smack_task_getsid,
3438 .task_getsecid = smack_task_getsecid,
3439 .task_setnice = smack_task_setnice,
3440 .task_setioprio = smack_task_setioprio,
3441 .task_getioprio = smack_task_getioprio,
3442 .task_setscheduler = smack_task_setscheduler,
3443 .task_getscheduler = smack_task_getscheduler,
3444 .task_movememory = smack_task_movememory,
3445 .task_kill = smack_task_kill,
3446 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003447 .task_to_inode = smack_task_to_inode,
3448
3449 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003450 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003451
3452 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3453 .msg_msg_free_security = smack_msg_msg_free_security,
3454
3455 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3456 .msg_queue_free_security = smack_msg_queue_free_security,
3457 .msg_queue_associate = smack_msg_queue_associate,
3458 .msg_queue_msgctl = smack_msg_queue_msgctl,
3459 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3460 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3461
3462 .shm_alloc_security = smack_shm_alloc_security,
3463 .shm_free_security = smack_shm_free_security,
3464 .shm_associate = smack_shm_associate,
3465 .shm_shmctl = smack_shm_shmctl,
3466 .shm_shmat = smack_shm_shmat,
3467
3468 .sem_alloc_security = smack_sem_alloc_security,
3469 .sem_free_security = smack_sem_free_security,
3470 .sem_associate = smack_sem_associate,
3471 .sem_semctl = smack_sem_semctl,
3472 .sem_semop = smack_sem_semop,
3473
Casey Schauflere114e472008-02-04 22:29:50 -08003474 .d_instantiate = smack_d_instantiate,
3475
3476 .getprocattr = smack_getprocattr,
3477 .setprocattr = smack_setprocattr,
3478
3479 .unix_stream_connect = smack_unix_stream_connect,
3480 .unix_may_send = smack_unix_may_send,
3481
3482 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003483 .socket_connect = smack_socket_connect,
3484 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003485 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3486 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3487 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3488 .sk_alloc_security = smack_sk_alloc_security,
3489 .sk_free_security = smack_sk_free_security,
3490 .sock_graft = smack_sock_graft,
3491 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003492 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003493
Casey Schauflere114e472008-02-04 22:29:50 -08003494 /* key management security hooks */
3495#ifdef CONFIG_KEYS
3496 .key_alloc = smack_key_alloc,
3497 .key_free = smack_key_free,
3498 .key_permission = smack_key_permission,
3499#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003500
3501 /* Audit hooks */
3502#ifdef CONFIG_AUDIT
3503 .audit_rule_init = smack_audit_rule_init,
3504 .audit_rule_known = smack_audit_rule_known,
3505 .audit_rule_match = smack_audit_rule_match,
3506 .audit_rule_free = smack_audit_rule_free,
3507#endif /* CONFIG_AUDIT */
3508
Casey Schauflere114e472008-02-04 22:29:50 -08003509 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003510 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003511 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003512 .inode_notifysecctx = smack_inode_notifysecctx,
3513 .inode_setsecctx = smack_inode_setsecctx,
3514 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003515};
3516
Etienne Basset7198e2e2009-03-24 20:53:24 +01003517
3518static __init void init_smack_know_list(void)
3519{
3520 list_add(&smack_known_huh.list, &smack_known_list);
3521 list_add(&smack_known_hat.list, &smack_known_list);
3522 list_add(&smack_known_star.list, &smack_known_list);
3523 list_add(&smack_known_floor.list, &smack_known_list);
3524 list_add(&smack_known_invalid.list, &smack_known_list);
3525 list_add(&smack_known_web.list, &smack_known_list);
3526}
3527
Casey Schauflere114e472008-02-04 22:29:50 -08003528/**
3529 * smack_init - initialize the smack system
3530 *
3531 * Returns 0
3532 */
3533static __init int smack_init(void)
3534{
David Howellsd84f4f92008-11-14 10:39:23 +11003535 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003536 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003537
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003538 if (!security_module_enable(&smack_ops))
3539 return 0;
3540
3541 tsp = new_task_smack(smack_known_floor.smk_known,
3542 smack_known_floor.smk_known, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003543 if (tsp == NULL)
3544 return -ENOMEM;
3545
Casey Schauflere114e472008-02-04 22:29:50 -08003546 printk(KERN_INFO "Smack: Initializing.\n");
3547
3548 /*
3549 * Set the security state for the initial task.
3550 */
David Howellsd84f4f92008-11-14 10:39:23 +11003551 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003552 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003553
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003554 /* initialize the smack_know_list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003555 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003556 /*
3557 * Initialize locks
3558 */
Casey Schauflere114e472008-02-04 22:29:50 -08003559 spin_lock_init(&smack_known_huh.smk_cipsolock);
3560 spin_lock_init(&smack_known_hat.smk_cipsolock);
3561 spin_lock_init(&smack_known_star.smk_cipsolock);
3562 spin_lock_init(&smack_known_floor.smk_cipsolock);
3563 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3564
3565 /*
3566 * Register with LSM
3567 */
3568 if (register_security(&smack_ops))
3569 panic("smack: Unable to register with kernel.\n");
3570
3571 return 0;
3572}
3573
3574/*
3575 * Smack requires early initialization in order to label
3576 * all processes and objects when they are created.
3577 */
3578security_initcall(smack_init);