blob: 29bb4e7fa5e896020e89bb676838c843bd4c26b8 [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 Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
19 */
20
21#include <linux/xattr.h>
22#include <linux/pagemap.h>
23#include <linux/mount.h>
24#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080031#include <linux/mutex.h>
32#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080033#include <net/cipso_ipv4.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100034#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070035#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050036#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030037#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000038#include <linux/msg.h>
39#include <linux/shm.h>
40#include <linux/binfmts.h>
Casey Schauflere114e472008-02-04 22:29:50 -080041#include "smack.h"
42
David Howellsc69e8d92008-11-14 10:39:19 +110043#define task_security(task) (task_cred_xxx((task), security))
44
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020045#define TRANS_TRUE "TRUE"
46#define TRANS_TRUE_SIZE 4
47
Casey Schauflere114e472008-02-04 22:29:50 -080048/**
49 * smk_fetch - Fetch the smack label from a file.
50 * @ip: a pointer to the inode
51 * @dp: a pointer to the dentry
52 *
53 * Returns a pointer to the master list entry for the Smack label
54 * or NULL if there was no label to fetch.
55 */
Casey Schaufler676dac42010-12-02 06:43:39 -080056static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080057{
58 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -070059 char *buffer;
60 char *result = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -080061
62 if (ip->i_op->getxattr == NULL)
63 return NULL;
64
Casey Schauflerf7112e62012-05-06 15:22:02 -070065 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
66 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -080067 return NULL;
68
Casey Schauflerf7112e62012-05-06 15:22:02 -070069 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
70 if (rc > 0)
71 result = smk_import(buffer, rc);
72
73 kfree(buffer);
74
75 return result;
Casey Schauflere114e472008-02-04 22:29:50 -080076}
77
78/**
79 * new_inode_smack - allocate an inode security blob
80 * @smack: a pointer to the Smack label to use in the blob
81 *
82 * Returns the new blob or NULL if there's no memory available
83 */
84struct inode_smack *new_inode_smack(char *smack)
85{
86 struct inode_smack *isp;
87
Tetsuo Handaceffec552012-03-29 16:19:05 +090088 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -080089 if (isp == NULL)
90 return NULL;
91
92 isp->smk_inode = smack;
93 isp->smk_flags = 0;
94 mutex_init(&isp->smk_lock);
95
96 return isp;
97}
98
Casey Schaufler7898e1f2011-01-17 08:05:27 -080099/**
100 * new_task_smack - allocate a task security blob
101 * @smack: a pointer to the Smack label to use in the blob
102 *
103 * Returns the new blob or NULL if there's no memory available
104 */
105static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
106{
107 struct task_smack *tsp;
108
109 tsp = kzalloc(sizeof(struct task_smack), gfp);
110 if (tsp == NULL)
111 return NULL;
112
113 tsp->smk_task = task;
114 tsp->smk_forked = forked;
115 INIT_LIST_HEAD(&tsp->smk_rules);
116 mutex_init(&tsp->smk_rules_lock);
117
118 return tsp;
119}
120
121/**
122 * smk_copy_rules - copy a rule set
123 * @nhead - new rules header pointer
124 * @ohead - old rules header pointer
125 *
126 * Returns 0 on success, -ENOMEM on error
127 */
128static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
129 gfp_t gfp)
130{
131 struct smack_rule *nrp;
132 struct smack_rule *orp;
133 int rc = 0;
134
135 INIT_LIST_HEAD(nhead);
136
137 list_for_each_entry_rcu(orp, ohead, list) {
138 nrp = kzalloc(sizeof(struct smack_rule), gfp);
139 if (nrp == NULL) {
140 rc = -ENOMEM;
141 break;
142 }
143 *nrp = *orp;
144 list_add_rcu(&nrp->list, nhead);
145 }
146 return rc;
147}
148
Casey Schauflere114e472008-02-04 22:29:50 -0800149/*
150 * LSM hooks.
151 * We he, that is fun!
152 */
153
154/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000155 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800156 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -0800157 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -0800158 *
159 * Returns 0 if access is OK, an error code otherwise
160 *
161 * Do the capability checks, and require read and write.
162 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000163static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800164{
165 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200166 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800167 char *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800168
Ingo Molnar9e488582009-05-07 19:26:19 +1000169 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800170 if (rc != 0)
171 return rc;
172
Casey Schaufler676dac42010-12-02 06:43:39 -0800173 tsp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200174 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
175 smk_ad_setfield_u_tsk(&ad, ctp);
176
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800177 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100178 return rc;
179}
Casey Schauflere114e472008-02-04 22:29:50 -0800180
David Howells5cd9c582008-08-14 11:37:28 +0100181/**
182 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
183 * @ptp: parent task pointer
184 *
185 * Returns 0 if access is OK, an error code otherwise
186 *
187 * Do the capability checks, and require read and write.
188 */
189static int smack_ptrace_traceme(struct task_struct *ptp)
190{
191 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200192 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800193 char *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100194
195 rc = cap_ptrace_traceme(ptp);
196 if (rc != 0)
197 return rc;
198
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800199 tsp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200200 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
201 smk_ad_setfield_u_tsk(&ad, ptp);
202
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800203 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800204 return rc;
205}
206
207/**
208 * smack_syslog - Smack approval on syslog
209 * @type: message type
210 *
211 * Require that the task has the floor label
212 *
213 * Returns 0 on success, error code otherwise.
214 */
Eric Paris12b30522010-11-15 18:36:29 -0500215static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800216{
Eric Paris12b30522010-11-15 18:36:29 -0500217 int rc = 0;
Casey Schaufler676dac42010-12-02 06:43:39 -0800218 char *sp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800219
Casey Schauflere114e472008-02-04 22:29:50 -0800220 if (capable(CAP_MAC_OVERRIDE))
221 return 0;
222
223 if (sp != smack_known_floor.smk_known)
224 rc = -EACCES;
225
226 return rc;
227}
228
229
230/*
231 * Superblock Hooks.
232 */
233
234/**
235 * smack_sb_alloc_security - allocate a superblock blob
236 * @sb: the superblock getting the blob
237 *
238 * Returns 0 on success or -ENOMEM on error.
239 */
240static int smack_sb_alloc_security(struct super_block *sb)
241{
242 struct superblock_smack *sbsp;
243
244 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
245
246 if (sbsp == NULL)
247 return -ENOMEM;
248
249 sbsp->smk_root = smack_known_floor.smk_known;
250 sbsp->smk_default = smack_known_floor.smk_known;
251 sbsp->smk_floor = smack_known_floor.smk_known;
252 sbsp->smk_hat = smack_known_hat.smk_known;
253 sbsp->smk_initialized = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800254
255 sb->s_security = sbsp;
256
257 return 0;
258}
259
260/**
261 * smack_sb_free_security - free a superblock blob
262 * @sb: the superblock getting the blob
263 *
264 */
265static void smack_sb_free_security(struct super_block *sb)
266{
267 kfree(sb->s_security);
268 sb->s_security = NULL;
269}
270
271/**
272 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800273 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800274 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800275 *
276 * Returns 0 on success or -ENOMEM on error.
277 *
278 * Copy the Smack specific mount options out of the mount
279 * options list.
280 */
Eric Parise0007522008-03-05 10:31:54 -0500281static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800282{
283 char *cp, *commap, *otheropts, *dp;
284
Casey Schauflere114e472008-02-04 22:29:50 -0800285 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
286 if (otheropts == NULL)
287 return -ENOMEM;
288
289 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
290 if (strstr(cp, SMK_FSDEFAULT) == cp)
291 dp = smackopts;
292 else if (strstr(cp, SMK_FSFLOOR) == cp)
293 dp = smackopts;
294 else if (strstr(cp, SMK_FSHAT) == cp)
295 dp = smackopts;
296 else if (strstr(cp, SMK_FSROOT) == cp)
297 dp = smackopts;
298 else
299 dp = otheropts;
300
301 commap = strchr(cp, ',');
302 if (commap != NULL)
303 *commap = '\0';
304
305 if (*dp != '\0')
306 strcat(dp, ",");
307 strcat(dp, cp);
308 }
309
310 strcpy(orig, otheropts);
311 free_page((unsigned long)otheropts);
312
313 return 0;
314}
315
316/**
317 * smack_sb_kern_mount - Smack specific mount processing
318 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100319 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800320 * @data: the smack mount options
321 *
322 * Returns 0 on success, an error code on failure
323 */
James Morris12204e22008-12-19 10:44:42 +1100324static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800325{
326 struct dentry *root = sb->s_root;
327 struct inode *inode = root->d_inode;
328 struct superblock_smack *sp = sb->s_security;
329 struct inode_smack *isp;
330 char *op;
331 char *commap;
332 char *nsp;
333
Casey Schauflereb982cb2012-05-23 17:46:58 -0700334 if (sp->smk_initialized != 0)
Casey Schauflere114e472008-02-04 22:29:50 -0800335 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700336
Casey Schauflere114e472008-02-04 22:29:50 -0800337 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800338
339 for (op = data; op != NULL; op = commap) {
340 commap = strchr(op, ',');
341 if (commap != NULL)
342 *commap++ = '\0';
343
344 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
345 op += strlen(SMK_FSHAT);
346 nsp = smk_import(op, 0);
347 if (nsp != NULL)
348 sp->smk_hat = nsp;
349 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
350 op += strlen(SMK_FSFLOOR);
351 nsp = smk_import(op, 0);
352 if (nsp != NULL)
353 sp->smk_floor = nsp;
354 } else if (strncmp(op, SMK_FSDEFAULT,
355 strlen(SMK_FSDEFAULT)) == 0) {
356 op += strlen(SMK_FSDEFAULT);
357 nsp = smk_import(op, 0);
358 if (nsp != NULL)
359 sp->smk_default = nsp;
360 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
361 op += strlen(SMK_FSROOT);
362 nsp = smk_import(op, 0);
363 if (nsp != NULL)
364 sp->smk_root = nsp;
365 }
366 }
367
368 /*
369 * Initialize the root inode.
370 */
371 isp = inode->i_security;
372 if (isp == NULL)
373 inode->i_security = new_inode_smack(sp->smk_root);
374 else
375 isp->smk_inode = sp->smk_root;
376
377 return 0;
378}
379
380/**
381 * smack_sb_statfs - Smack check on statfs
382 * @dentry: identifies the file system in question
383 *
384 * Returns 0 if current can read the floor of the filesystem,
385 * and error code otherwise
386 */
387static int smack_sb_statfs(struct dentry *dentry)
388{
389 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200390 int rc;
391 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800392
Eric Parisa2694342011-04-25 13:10:27 -0400393 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200394 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
395
396 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
397 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800398}
399
400/**
401 * smack_sb_mount - Smack check for mounting
402 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800403 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800404 * @type: unused
405 * @flags: unused
406 * @data: unused
407 *
408 * Returns 0 if current can write the floor of the filesystem
409 * being mounted on, an error code otherwise.
410 */
Al Virob5266eb2008-03-22 17:48:24 -0400411static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800412 char *type, unsigned long flags, void *data)
413{
Al Virod8c95842011-12-07 18:16:57 -0500414 struct superblock_smack *sbp = path->dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200415 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800416
Eric Parisf48b7392011-04-25 12:54:27 -0400417 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200418 smk_ad_setfield_u_fs_path(&ad, *path);
419
420 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800421}
422
423/**
424 * smack_sb_umount - Smack check for unmounting
425 * @mnt: file system to unmount
426 * @flags: unused
427 *
428 * Returns 0 if current can write the floor of the filesystem
429 * being unmounted, an error code otherwise.
430 */
431static int smack_sb_umount(struct vfsmount *mnt, int flags)
432{
433 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200434 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400435 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200436
Eric Parisa2694342011-04-25 13:10:27 -0400437 path.dentry = mnt->mnt_root;
438 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200439
Eric Parisf48b7392011-04-25 12:54:27 -0400440 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400441 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflere114e472008-02-04 22:29:50 -0800442
Al Virod8c95842011-12-07 18:16:57 -0500443 sbp = path.dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200444 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800445}
446
447/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800448 * BPRM hooks
449 */
450
Casey Schauflerce8a4322011-09-29 18:21:01 -0700451/**
452 * smack_bprm_set_creds - set creds for exec
453 * @bprm: the exec information
454 *
455 * Returns 0 if it gets a blob, -ENOMEM otherwise
456 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800457static int smack_bprm_set_creds(struct linux_binprm *bprm)
458{
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300459 struct inode *inode = bprm->file->f_path.dentry->d_inode;
460 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800461 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800462 int rc;
463
464 rc = cap_bprm_set_creds(bprm);
465 if (rc != 0)
466 return rc;
467
468 if (bprm->cred_prepared)
469 return 0;
470
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300471 isp = inode->i_security;
472 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800473 return 0;
474
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300475 if (bprm->unsafe)
476 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800477
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300478 bsp->smk_task = isp->smk_task;
479 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800480
481 return 0;
482}
483
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300484/**
485 * smack_bprm_committing_creds - Prepare to install the new credentials
486 * from bprm.
487 *
488 * @bprm: binprm for exec
489 */
490static void smack_bprm_committing_creds(struct linux_binprm *bprm)
491{
492 struct task_smack *bsp = bprm->cred->security;
493
494 if (bsp->smk_task != bsp->smk_forked)
495 current->pdeath_signal = 0;
496}
497
498/**
499 * smack_bprm_secureexec - Return the decision to use secureexec.
500 * @bprm: binprm for exec
501 *
502 * Returns 0 on success.
503 */
504static int smack_bprm_secureexec(struct linux_binprm *bprm)
505{
506 struct task_smack *tsp = current_security();
507 int ret = cap_bprm_secureexec(bprm);
508
509 if (!ret && (tsp->smk_task != tsp->smk_forked))
510 ret = 1;
511
512 return ret;
513}
514
Casey Schaufler676dac42010-12-02 06:43:39 -0800515/*
Casey Schauflere114e472008-02-04 22:29:50 -0800516 * Inode hooks
517 */
518
519/**
520 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800521 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800522 *
523 * Returns 0 if it gets a blob, -ENOMEM otherwise
524 */
525static int smack_inode_alloc_security(struct inode *inode)
526{
Casey Schaufler676dac42010-12-02 06:43:39 -0800527 inode->i_security = new_inode_smack(smk_of_current());
Casey Schauflere114e472008-02-04 22:29:50 -0800528 if (inode->i_security == NULL)
529 return -ENOMEM;
530 return 0;
531}
532
533/**
534 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800535 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800536 *
537 * Clears the blob pointer in inode
538 */
539static void smack_inode_free_security(struct inode *inode)
540{
541 kfree(inode->i_security);
542 inode->i_security = NULL;
543}
544
545/**
546 * smack_inode_init_security - copy out the smack from an inode
547 * @inode: the inode
548 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500549 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800550 * @name: where to put the attribute name
551 * @value: where to put the attribute value
552 * @len: where to put the length of the attribute
553 *
554 * Returns 0 if it all works out, -ENOMEM if there's no memory
555 */
556static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -0500557 const struct qstr *qstr, char **name,
558 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800559{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700560 struct smack_known *skp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700561 struct inode_smack *issp = inode->i_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700562 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800563 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200564 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800565 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800566
567 if (name) {
Tetsuo Handaceffec552012-03-29 16:19:05 +0900568 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800569 if (*name == NULL)
570 return -ENOMEM;
571 }
572
573 if (value) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700574 skp = smk_find_entry(csp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800575 rcu_read_lock();
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700576 may = smk_access_entry(csp, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800577 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200578
579 /*
580 * If the access rule allows transmutation and
581 * the directory requests transmutation then
582 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700583 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200584 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800585 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700586 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200587 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700588 issp->smk_flags |= SMK_INODE_CHANGED;
589 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200590
Tetsuo Handaceffec552012-03-29 16:19:05 +0900591 *value = kstrdup(isp, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800592 if (*value == NULL)
593 return -ENOMEM;
594 }
595
596 if (len)
597 *len = strlen(isp) + 1;
598
599 return 0;
600}
601
602/**
603 * smack_inode_link - Smack check on link
604 * @old_dentry: the existing object
605 * @dir: unused
606 * @new_dentry: the new object
607 *
608 * Returns 0 if access is permitted, an error code otherwise
609 */
610static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
611 struct dentry *new_dentry)
612{
Casey Schauflere114e472008-02-04 22:29:50 -0800613 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200614 struct smk_audit_info ad;
615 int rc;
616
Eric Parisa2694342011-04-25 13:10:27 -0400617 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200618 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800619
620 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200621 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800622
623 if (rc == 0 && new_dentry->d_inode != NULL) {
624 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200625 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
626 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800627 }
628
629 return rc;
630}
631
632/**
633 * smack_inode_unlink - Smack check on inode deletion
634 * @dir: containing directory object
635 * @dentry: file to unlink
636 *
637 * Returns 0 if current can write the containing directory
638 * and the object, error code otherwise
639 */
640static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
641{
642 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200643 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800644 int rc;
645
Eric Parisa2694342011-04-25 13:10:27 -0400646 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200647 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
648
Casey Schauflere114e472008-02-04 22:29:50 -0800649 /*
650 * You need write access to the thing you're unlinking
651 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200652 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
653 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800654 /*
655 * You also need write access to the containing directory
656 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200657 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
658 smk_ad_setfield_u_fs_inode(&ad, dir);
659 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
660 }
Casey Schauflere114e472008-02-04 22:29:50 -0800661 return rc;
662}
663
664/**
665 * smack_inode_rmdir - Smack check on directory deletion
666 * @dir: containing directory object
667 * @dentry: directory to unlink
668 *
669 * Returns 0 if current can write the containing directory
670 * and the directory, error code otherwise
671 */
672static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
673{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200674 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800675 int rc;
676
Eric Parisa2694342011-04-25 13:10:27 -0400677 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200678 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
679
Casey Schauflere114e472008-02-04 22:29:50 -0800680 /*
681 * You need write access to the thing you're removing
682 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200683 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
684 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800685 /*
686 * You also need write access to the containing directory
687 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200688 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
689 smk_ad_setfield_u_fs_inode(&ad, dir);
690 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
691 }
Casey Schauflere114e472008-02-04 22:29:50 -0800692
693 return rc;
694}
695
696/**
697 * smack_inode_rename - Smack check on rename
698 * @old_inode: the old directory
699 * @old_dentry: unused
700 * @new_inode: the new directory
701 * @new_dentry: unused
702 *
703 * Read and write access is required on both the old and
704 * new directories.
705 *
706 * Returns 0 if access is permitted, an error code otherwise
707 */
708static int smack_inode_rename(struct inode *old_inode,
709 struct dentry *old_dentry,
710 struct inode *new_inode,
711 struct dentry *new_dentry)
712{
713 int rc;
714 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200715 struct smk_audit_info ad;
716
Eric Parisa2694342011-04-25 13:10:27 -0400717 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200718 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800719
720 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200721 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800722
723 if (rc == 0 && new_dentry->d_inode != NULL) {
724 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200725 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
726 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800727 }
Casey Schauflere114e472008-02-04 22:29:50 -0800728 return rc;
729}
730
731/**
732 * smack_inode_permission - Smack version of permission()
733 * @inode: the inode in question
734 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800735 *
736 * This is the important Smack hook.
737 *
738 * Returns 0 if access is permitted, -EACCES otherwise
739 */
Al Viroe74f71e2011-06-20 19:38:15 -0400740static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800741{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200742 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400743 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400744
745 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800746 /*
747 * No permission to check. Existence test. Yup, it's there.
748 */
749 if (mask == 0)
750 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700751
752 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400753 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700754 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400755 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200756 smk_ad_setfield_u_fs_inode(&ad, inode);
757 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800758}
759
760/**
761 * smack_inode_setattr - Smack check for setting attributes
762 * @dentry: the object
763 * @iattr: for the force flag
764 *
765 * Returns 0 if access is permitted, an error code otherwise
766 */
767static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
768{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200769 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800770 /*
771 * Need to allow for clearing the setuid bit.
772 */
773 if (iattr->ia_valid & ATTR_FORCE)
774 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400775 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200776 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800777
Etienne Bassetecfcc532009-04-08 20:40:06 +0200778 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800779}
780
781/**
782 * smack_inode_getattr - Smack check for getting attributes
783 * @mnt: unused
784 * @dentry: the object
785 *
786 * Returns 0 if access is permitted, an error code otherwise
787 */
788static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
789{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200790 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400791 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200792
Eric Parisa2694342011-04-25 13:10:27 -0400793 path.dentry = dentry;
794 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200795
Eric Parisf48b7392011-04-25 12:54:27 -0400796 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400797 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200798 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800799}
800
801/**
802 * smack_inode_setxattr - Smack check for setting xattrs
803 * @dentry: the object
804 * @name: name of the attribute
805 * @value: unused
806 * @size: unused
807 * @flags: unused
808 *
809 * This protects the Smack attribute explicitly.
810 *
811 * Returns 0 if access is permitted, an error code otherwise
812 */
David Howells8f0cfa52008-04-29 00:59:41 -0700813static int smack_inode_setxattr(struct dentry *dentry, const char *name,
814 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800815{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200816 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800817 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800818
Casey Schauflerbcdca222008-02-23 15:24:04 -0800819 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
820 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800821 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800822 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
823 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800824 if (!capable(CAP_MAC_ADMIN))
825 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200826 /*
827 * check label validity here so import wont fail on
828 * post_setxattr
829 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700830 if (size == 0 || size >= SMK_LONGLABEL ||
Etienne Bassetdefc4332009-04-16 23:58:42 +0200831 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400832 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200833 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
834 if (!capable(CAP_MAC_ADMIN))
835 rc = -EPERM;
836 if (size != TRANS_TRUE_SIZE ||
837 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
838 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800839 } else
840 rc = cap_inode_setxattr(dentry, name, value, size, flags);
841
Eric Parisa2694342011-04-25 13:10:27 -0400842 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200843 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
844
Casey Schauflerbcdca222008-02-23 15:24:04 -0800845 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200846 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800847
848 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800849}
850
851/**
852 * smack_inode_post_setxattr - Apply the Smack update approved above
853 * @dentry: object
854 * @name: attribute name
855 * @value: attribute value
856 * @size: attribute size
857 * @flags: unused
858 *
859 * Set the pointer in the inode blob to the entry found
860 * in the master label list.
861 */
David Howells8f0cfa52008-04-29 00:59:41 -0700862static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
863 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800864{
Casey Schauflere114e472008-02-04 22:29:50 -0800865 char *nsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200866 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800867
868 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200869 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800870 if (nsp != NULL)
871 isp->smk_inode = nsp;
872 else
873 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200874 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
875 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800876 if (nsp != NULL)
877 isp->smk_task = nsp;
878 else
879 isp->smk_task = smack_known_invalid.smk_known;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800880 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
881 nsp = smk_import(value, size);
882 if (nsp != NULL)
883 isp->smk_mmap = nsp;
884 else
885 isp->smk_mmap = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200886 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
887 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflere114e472008-02-04 22:29:50 -0800888
889 return;
890}
891
Casey Schauflerce8a4322011-09-29 18:21:01 -0700892/**
Casey Schauflere114e472008-02-04 22:29:50 -0800893 * smack_inode_getxattr - Smack check on getxattr
894 * @dentry: the object
895 * @name: unused
896 *
897 * Returns 0 if access is permitted, an error code otherwise
898 */
David Howells8f0cfa52008-04-29 00:59:41 -0700899static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800900{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200901 struct smk_audit_info ad;
902
Eric Parisa2694342011-04-25 13:10:27 -0400903 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200904 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
905
906 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800907}
908
Casey Schauflerce8a4322011-09-29 18:21:01 -0700909/**
Casey Schauflere114e472008-02-04 22:29:50 -0800910 * smack_inode_removexattr - Smack check on removexattr
911 * @dentry: the object
912 * @name: name of the attribute
913 *
914 * Removing the Smack attribute requires CAP_MAC_ADMIN
915 *
916 * Returns 0 if access is permitted, an error code otherwise
917 */
David Howells8f0cfa52008-04-29 00:59:41 -0700918static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800919{
Casey Schaufler676dac42010-12-02 06:43:39 -0800920 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200921 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800922 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800923
Casey Schauflerbcdca222008-02-23 15:24:04 -0800924 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
925 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800926 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200927 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800928 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
929 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800930 if (!capable(CAP_MAC_ADMIN))
931 rc = -EPERM;
932 } else
933 rc = cap_inode_removexattr(dentry, name);
934
Eric Parisa2694342011-04-25 13:10:27 -0400935 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200936 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800937 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200938 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800939
Casey Schaufler676dac42010-12-02 06:43:39 -0800940 if (rc == 0) {
941 isp = dentry->d_inode->i_security;
942 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800943 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800944 }
945
Casey Schauflerbcdca222008-02-23 15:24:04 -0800946 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800947}
948
949/**
950 * smack_inode_getsecurity - get smack xattrs
951 * @inode: the object
952 * @name: attribute name
953 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800954 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800955 *
956 * Returns the size of the attribute or an error code
957 */
958static int smack_inode_getsecurity(const struct inode *inode,
959 const char *name, void **buffer,
960 bool alloc)
961{
962 struct socket_smack *ssp;
963 struct socket *sock;
964 struct super_block *sbp;
965 struct inode *ip = (struct inode *)inode;
966 char *isp;
967 int ilen;
968 int rc = 0;
969
970 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
971 isp = smk_of_inode(inode);
972 ilen = strlen(isp) + 1;
973 *buffer = isp;
974 return ilen;
975 }
976
977 /*
978 * The rest of the Smack xattrs are only on sockets.
979 */
980 sbp = ip->i_sb;
981 if (sbp->s_magic != SOCKFS_MAGIC)
982 return -EOPNOTSUPP;
983
984 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800985 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800986 return -EOPNOTSUPP;
987
988 ssp = sock->sk->sk_security;
989
990 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
991 isp = ssp->smk_in;
992 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
993 isp = ssp->smk_out;
994 else
995 return -EOPNOTSUPP;
996
997 ilen = strlen(isp) + 1;
998 if (rc == 0) {
999 *buffer = isp;
1000 rc = ilen;
1001 }
1002
1003 return rc;
1004}
1005
1006
1007/**
1008 * smack_inode_listsecurity - list the Smack attributes
1009 * @inode: the object
1010 * @buffer: where they go
1011 * @buffer_size: size of buffer
1012 *
1013 * Returns 0 on success, -EINVAL otherwise
1014 */
1015static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1016 size_t buffer_size)
1017{
1018 int len = strlen(XATTR_NAME_SMACK);
1019
1020 if (buffer != NULL && len <= buffer_size) {
1021 memcpy(buffer, XATTR_NAME_SMACK, len);
1022 return len;
1023 }
1024 return -EINVAL;
1025}
1026
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001027/**
1028 * smack_inode_getsecid - Extract inode's security id
1029 * @inode: inode to extract the info from
1030 * @secid: where result will be saved
1031 */
1032static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1033{
1034 struct inode_smack *isp = inode->i_security;
1035
1036 *secid = smack_to_secid(isp->smk_inode);
1037}
1038
Casey Schauflere114e472008-02-04 22:29:50 -08001039/*
1040 * File Hooks
1041 */
1042
1043/**
1044 * smack_file_permission - Smack check on file operations
1045 * @file: unused
1046 * @mask: unused
1047 *
1048 * Returns 0
1049 *
1050 * Should access checks be done on each read or write?
1051 * UNICOS and SELinux say yes.
1052 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1053 *
1054 * I'll say no for now. Smack does not do the frequent
1055 * label changing that SELinux does.
1056 */
1057static int smack_file_permission(struct file *file, int mask)
1058{
1059 return 0;
1060}
1061
1062/**
1063 * smack_file_alloc_security - assign a file security blob
1064 * @file: the object
1065 *
1066 * The security blob for a file is a pointer to the master
1067 * label list, so no allocation is done.
1068 *
1069 * Returns 0
1070 */
1071static int smack_file_alloc_security(struct file *file)
1072{
Casey Schaufler676dac42010-12-02 06:43:39 -08001073 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001074 return 0;
1075}
1076
1077/**
1078 * smack_file_free_security - clear a file security blob
1079 * @file: the object
1080 *
1081 * The security blob for a file is a pointer to the master
1082 * label list, so no memory is freed.
1083 */
1084static void smack_file_free_security(struct file *file)
1085{
1086 file->f_security = NULL;
1087}
1088
1089/**
1090 * smack_file_ioctl - Smack check on ioctls
1091 * @file: the object
1092 * @cmd: what to do
1093 * @arg: unused
1094 *
1095 * Relies heavily on the correct use of the ioctl command conventions.
1096 *
1097 * Returns 0 if allowed, error code otherwise
1098 */
1099static int smack_file_ioctl(struct file *file, unsigned int cmd,
1100 unsigned long arg)
1101{
1102 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 struct smk_audit_info ad;
1104
Eric Parisf48b7392011-04-25 12:54:27 -04001105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001107
1108 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001109 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001110
1111 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001112 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001113
1114 return rc;
1115}
1116
1117/**
1118 * smack_file_lock - Smack check on file locking
1119 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001120 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001121 *
1122 * Returns 0 if current has write access, error code otherwise
1123 */
1124static int smack_file_lock(struct file *file, unsigned int cmd)
1125{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001126 struct smk_audit_info ad;
1127
Eric Paris92f42502011-04-25 13:15:55 -04001128 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1129 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001130 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001131}
1132
1133/**
1134 * smack_file_fcntl - Smack check on fcntl
1135 * @file: the object
1136 * @cmd: what action to check
1137 * @arg: unused
1138 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001139 * Generally these operations are harmless.
1140 * File locking operations present an obvious mechanism
1141 * for passing information, so they require write access.
1142 *
Casey Schauflere114e472008-02-04 22:29:50 -08001143 * Returns 0 if current has access, error code otherwise
1144 */
1145static int smack_file_fcntl(struct file *file, unsigned int cmd,
1146 unsigned long arg)
1147{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001149 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001150
Etienne Bassetecfcc532009-04-08 20:40:06 +02001151
Casey Schauflere114e472008-02-04 22:29:50 -08001152 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001153 case F_GETLK:
Casey Schauflere114e472008-02-04 22:29:50 -08001154 case F_SETLK:
1155 case F_SETLKW:
1156 case F_SETOWN:
1157 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001158 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1159 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001160 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001161 break;
1162 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001163 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001164 }
1165
1166 return rc;
1167}
1168
1169/**
Al Viroe5467852012-05-30 13:30:51 -04001170 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001171 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1172 * if mapping anonymous memory.
1173 * @file contains the file structure for file to map (may be NULL).
1174 * @reqprot contains the protection requested by the application.
1175 * @prot contains the protection that will be applied by the kernel.
1176 * @flags contains the operational flags.
1177 * Return 0 if permission is granted.
1178 */
Al Viroe5467852012-05-30 13:30:51 -04001179static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001180 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001181 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001182{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001183 struct smack_known *skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001184 struct smack_rule *srp;
1185 struct task_smack *tsp;
1186 char *sp;
1187 char *msmack;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001188 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001189 struct inode_smack *isp;
1190 struct dentry *dp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001191 int may;
1192 int mmay;
1193 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001194 int rc;
1195
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001196 if (file == NULL || file->f_dentry == NULL)
1197 return 0;
1198
1199 dp = file->f_dentry;
1200
1201 if (dp->d_inode == NULL)
1202 return 0;
1203
1204 isp = dp->d_inode->i_security;
1205 if (isp->smk_mmap == NULL)
1206 return 0;
1207 msmack = isp->smk_mmap;
1208
1209 tsp = current_security();
1210 sp = smk_of_current();
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001211 skp = smk_find_entry(sp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001212 rc = 0;
1213
1214 rcu_read_lock();
1215 /*
1216 * For each Smack rule associated with the subject
1217 * label verify that the SMACK64MMAP also has access
1218 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001219 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001220 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001221 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001222 /*
1223 * Matching labels always allows access.
1224 */
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001225 if (msmack == osmack)
1226 continue;
1227 /*
1228 * If there is a matching local rule take
1229 * that into account as well.
1230 */
1231 may = smk_access_entry(srp->smk_subject, osmack,
1232 &tsp->smk_rules);
1233 if (may == -ENOENT)
1234 may = srp->smk_access;
1235 else
1236 may &= srp->smk_access;
1237 /*
1238 * If may is zero the SMACK64MMAP subject can't
1239 * possibly have less access.
1240 */
1241 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001242 continue;
1243
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001244 /*
1245 * Fetch the global list entry.
1246 * If there isn't one a SMACK64MMAP subject
1247 * can't have as much access as current.
1248 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001249 skp = smk_find_entry(msmack);
1250 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001251 if (mmay == -ENOENT) {
1252 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001253 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001254 }
1255 /*
1256 * If there is a local entry it modifies the
1257 * potential access, too.
1258 */
1259 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1260 if (tmay != -ENOENT)
1261 mmay &= tmay;
1262
1263 /*
1264 * If there is any access available to current that is
1265 * not available to a SMACK64MMAP subject
1266 * deny access.
1267 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001268 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001269 rc = -EACCES;
1270 break;
1271 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001272 }
1273
1274 rcu_read_unlock();
1275
1276 return rc;
1277}
1278
1279/**
Casey Schauflere114e472008-02-04 22:29:50 -08001280 * smack_file_set_fowner - set the file security blob value
1281 * @file: object in question
1282 *
1283 * Returns 0
1284 * Further research may be required on this one.
1285 */
1286static int smack_file_set_fowner(struct file *file)
1287{
Casey Schaufler676dac42010-12-02 06:43:39 -08001288 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001289 return 0;
1290}
1291
1292/**
1293 * smack_file_send_sigiotask - Smack on sigio
1294 * @tsk: The target task
1295 * @fown: the object the signal come from
1296 * @signum: unused
1297 *
1298 * Allow a privileged task to get signals even if it shouldn't
1299 *
1300 * Returns 0 if a subject with the object's smack could
1301 * write to the task, an error code otherwise.
1302 */
1303static int smack_file_send_sigiotask(struct task_struct *tsk,
1304 struct fown_struct *fown, int signum)
1305{
1306 struct file *file;
1307 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001308 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001309 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001310
1311 /*
1312 * struct fown_struct is never outside the context of a struct file
1313 */
1314 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001315
Etienne Bassetecfcc532009-04-08 20:40:06 +02001316 /* we don't log here as rc can be overriden */
1317 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001318 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001319 rc = 0;
1320
1321 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1322 smk_ad_setfield_u_tsk(&ad, tsk);
1323 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001324 return rc;
1325}
1326
1327/**
1328 * smack_file_receive - Smack file receive check
1329 * @file: the object
1330 *
1331 * Returns 0 if current has access, error code otherwise
1332 */
1333static int smack_file_receive(struct file *file)
1334{
1335 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001336 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001337
Etienne Bassetecfcc532009-04-08 20:40:06 +02001338 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1339 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001340 /*
1341 * This code relies on bitmasks.
1342 */
1343 if (file->f_mode & FMODE_READ)
1344 may = MAY_READ;
1345 if (file->f_mode & FMODE_WRITE)
1346 may |= MAY_WRITE;
1347
Etienne Bassetecfcc532009-04-08 20:40:06 +02001348 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001349}
1350
Casey Schaufler531f1d42011-09-19 12:41:42 -07001351/**
Eric Paris83d49852012-04-04 13:45:40 -04001352 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001353 * @file: the object
1354 * @cred: unused
1355 *
1356 * Set the security blob in the file structure.
1357 *
1358 * Returns 0
1359 */
Eric Paris83d49852012-04-04 13:45:40 -04001360static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001361{
1362 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1363
1364 file->f_security = isp->smk_inode;
1365
1366 return 0;
1367}
1368
Casey Schauflere114e472008-02-04 22:29:50 -08001369/*
1370 * Task hooks
1371 */
1372
1373/**
David Howellsee18d642009-09-02 09:14:21 +01001374 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1375 * @new: the new credentials
1376 * @gfp: the atomicity of any memory allocations
1377 *
1378 * Prepare a blank set of credentials for modification. This must allocate all
1379 * the memory the LSM module might require such that cred_transfer() can
1380 * complete without error.
1381 */
1382static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1383{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001384 struct task_smack *tsp;
1385
1386 tsp = new_task_smack(NULL, NULL, gfp);
1387 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001388 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001389
1390 cred->security = tsp;
1391
David Howellsee18d642009-09-02 09:14:21 +01001392 return 0;
1393}
1394
1395
1396/**
David Howellsf1752ee2008-11-14 10:39:17 +11001397 * smack_cred_free - "free" task-level security credentials
1398 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001399 *
Casey Schauflere114e472008-02-04 22:29:50 -08001400 */
David Howellsf1752ee2008-11-14 10:39:17 +11001401static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001402{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001403 struct task_smack *tsp = cred->security;
1404 struct smack_rule *rp;
1405 struct list_head *l;
1406 struct list_head *n;
1407
1408 if (tsp == NULL)
1409 return;
1410 cred->security = NULL;
1411
1412 list_for_each_safe(l, n, &tsp->smk_rules) {
1413 rp = list_entry(l, struct smack_rule, list);
1414 list_del(&rp->list);
1415 kfree(rp);
1416 }
1417 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001418}
1419
1420/**
David Howellsd84f4f92008-11-14 10:39:23 +11001421 * smack_cred_prepare - prepare new set of credentials for modification
1422 * @new: the new credentials
1423 * @old: the original credentials
1424 * @gfp: the atomicity of any memory allocations
1425 *
1426 * Prepare a new set of credentials for modification.
1427 */
1428static int smack_cred_prepare(struct cred *new, const struct cred *old,
1429 gfp_t gfp)
1430{
Casey Schaufler676dac42010-12-02 06:43:39 -08001431 struct task_smack *old_tsp = old->security;
1432 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001433 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001434
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001435 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001436 if (new_tsp == NULL)
1437 return -ENOMEM;
1438
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001439 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1440 if (rc != 0)
1441 return rc;
1442
Casey Schaufler676dac42010-12-02 06:43:39 -08001443 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001444 return 0;
1445}
1446
Randy Dunlap251a2a92009-02-18 11:42:33 -08001447/**
David Howellsee18d642009-09-02 09:14:21 +01001448 * smack_cred_transfer - Transfer the old credentials to the new credentials
1449 * @new: the new credentials
1450 * @old: the original credentials
1451 *
1452 * Fill in a set of blank credentials from another set of credentials.
1453 */
1454static void smack_cred_transfer(struct cred *new, const struct cred *old)
1455{
Casey Schaufler676dac42010-12-02 06:43:39 -08001456 struct task_smack *old_tsp = old->security;
1457 struct task_smack *new_tsp = new->security;
1458
1459 new_tsp->smk_task = old_tsp->smk_task;
1460 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001461 mutex_init(&new_tsp->smk_rules_lock);
1462 INIT_LIST_HEAD(&new_tsp->smk_rules);
1463
1464
1465 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001466}
1467
1468/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001469 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001470 * @new: points to the set of credentials to be modified.
1471 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001472 *
1473 * Set the security data for a kernel service.
1474 */
1475static int smack_kernel_act_as(struct cred *new, u32 secid)
1476{
Casey Schaufler676dac42010-12-02 06:43:39 -08001477 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001478 char *smack = smack_from_secid(secid);
1479
1480 if (smack == NULL)
1481 return -EINVAL;
1482
Casey Schaufler676dac42010-12-02 06:43:39 -08001483 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001484 return 0;
1485}
1486
1487/**
1488 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001489 * @new: points to the set of credentials to be modified
1490 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001491 *
1492 * Set the file creation context in a set of credentials to the same
1493 * as the objective context of the specified inode
1494 */
1495static int smack_kernel_create_files_as(struct cred *new,
1496 struct inode *inode)
1497{
1498 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001499 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001500
Casey Schaufler676dac42010-12-02 06:43:39 -08001501 tsp->smk_forked = isp->smk_inode;
1502 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001503 return 0;
1504}
1505
1506/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001507 * smk_curacc_on_task - helper to log task related access
1508 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001509 * @access: the access requested
1510 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001511 *
1512 * Return 0 if access is permitted
1513 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001514static int smk_curacc_on_task(struct task_struct *p, int access,
1515 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001516{
1517 struct smk_audit_info ad;
1518
Casey Schaufler531f1d42011-09-19 12:41:42 -07001519 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001520 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001521 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001522}
1523
1524/**
Casey Schauflere114e472008-02-04 22:29:50 -08001525 * smack_task_setpgid - Smack check on setting pgid
1526 * @p: the task object
1527 * @pgid: unused
1528 *
1529 * Return 0 if write access is permitted
1530 */
1531static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1532{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001533 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001534}
1535
1536/**
1537 * smack_task_getpgid - Smack access check for getpgid
1538 * @p: the object task
1539 *
1540 * Returns 0 if current can read the object task, error code otherwise
1541 */
1542static int smack_task_getpgid(struct task_struct *p)
1543{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001544 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001545}
1546
1547/**
1548 * smack_task_getsid - Smack access check for getsid
1549 * @p: the object task
1550 *
1551 * Returns 0 if current can read the object task, error code otherwise
1552 */
1553static int smack_task_getsid(struct task_struct *p)
1554{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001555 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001556}
1557
1558/**
1559 * smack_task_getsecid - get the secid of the task
1560 * @p: the object task
1561 * @secid: where to put the result
1562 *
1563 * Sets the secid to contain a u32 version of the smack label.
1564 */
1565static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1566{
Casey Schaufler676dac42010-12-02 06:43:39 -08001567 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001568}
1569
1570/**
1571 * smack_task_setnice - Smack check on setting nice
1572 * @p: the task object
1573 * @nice: unused
1574 *
1575 * Return 0 if write access is permitted
1576 */
1577static int smack_task_setnice(struct task_struct *p, int nice)
1578{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001579 int rc;
1580
1581 rc = cap_task_setnice(p, nice);
1582 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001583 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001584 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001585}
1586
1587/**
1588 * smack_task_setioprio - Smack check on setting ioprio
1589 * @p: the task object
1590 * @ioprio: unused
1591 *
1592 * Return 0 if write access is permitted
1593 */
1594static int smack_task_setioprio(struct task_struct *p, int ioprio)
1595{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001596 int rc;
1597
1598 rc = cap_task_setioprio(p, ioprio);
1599 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001600 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001601 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001602}
1603
1604/**
1605 * smack_task_getioprio - Smack check on reading ioprio
1606 * @p: the task object
1607 *
1608 * Return 0 if read access is permitted
1609 */
1610static int smack_task_getioprio(struct task_struct *p)
1611{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001612 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001613}
1614
1615/**
1616 * smack_task_setscheduler - Smack check on setting scheduler
1617 * @p: the task object
1618 * @policy: unused
1619 * @lp: unused
1620 *
1621 * Return 0 if read access is permitted
1622 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001623static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001624{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001625 int rc;
1626
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001627 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001628 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001629 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001630 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001631}
1632
1633/**
1634 * smack_task_getscheduler - Smack check on reading scheduler
1635 * @p: the task object
1636 *
1637 * Return 0 if read access is permitted
1638 */
1639static int smack_task_getscheduler(struct task_struct *p)
1640{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001641 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001642}
1643
1644/**
1645 * smack_task_movememory - Smack check on moving memory
1646 * @p: the task object
1647 *
1648 * Return 0 if write access is permitted
1649 */
1650static int smack_task_movememory(struct task_struct *p)
1651{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001652 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001653}
1654
1655/**
1656 * smack_task_kill - Smack check on signal delivery
1657 * @p: the task object
1658 * @info: unused
1659 * @sig: unused
1660 * @secid: identifies the smack to use in lieu of current's
1661 *
1662 * Return 0 if write access is permitted
1663 *
1664 * The secid behavior is an artifact of an SELinux hack
1665 * in the USB code. Someday it may go away.
1666 */
1667static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1668 int sig, u32 secid)
1669{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001670 struct smk_audit_info ad;
1671
1672 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1673 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001674 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001675 * Sending a signal requires that the sender
1676 * can write the receiver.
1677 */
1678 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001679 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1680 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001681 /*
1682 * If the secid isn't 0 we're dealing with some USB IO
1683 * specific behavior. This is not clean. For one thing
1684 * we can't take privilege into account.
1685 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001686 return smk_access(smack_from_secid(secid),
1687 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001688}
1689
1690/**
1691 * smack_task_wait - Smack access check for waiting
1692 * @p: task to wait for
1693 *
1694 * Returns 0 if current can wait for p, error code otherwise
1695 */
1696static int smack_task_wait(struct task_struct *p)
1697{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001698 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08001699 char *sp = smk_of_current();
1700 char *tsp = smk_of_forked(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001701 int rc;
1702
Etienne Bassetecfcc532009-04-08 20:40:06 +02001703 /* we don't log here, we can be overriden */
Casey Schaufler676dac42010-12-02 06:43:39 -08001704 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001705 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001706 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001707
1708 /*
1709 * Allow the operation to succeed if either task
1710 * has privilege to perform operations that might
1711 * account for the smack labels having gotten to
1712 * be different in the first place.
1713 *
David Howells5cd9c582008-08-14 11:37:28 +01001714 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001715 * control ideal, taking the object's privilege
1716 * state into account in the decision as well as
1717 * the smack value.
1718 */
David Howells5cd9c582008-08-14 11:37:28 +01001719 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001720 rc = 0;
1721 /* we log only if we didn't get overriden */
1722 out_log:
1723 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1724 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001725 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001726 return rc;
1727}
1728
1729/**
1730 * smack_task_to_inode - copy task smack into the inode blob
1731 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001732 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001733 *
1734 * Sets the smack pointer in the inode security blob
1735 */
1736static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1737{
1738 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001739 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001740}
1741
1742/*
1743 * Socket hooks.
1744 */
1745
1746/**
1747 * smack_sk_alloc_security - Allocate a socket blob
1748 * @sk: the socket
1749 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001750 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001751 *
1752 * Assign Smack pointers to current
1753 *
1754 * Returns 0 on success, -ENOMEM is there's no memory
1755 */
1756static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1757{
Casey Schaufler676dac42010-12-02 06:43:39 -08001758 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001759 struct socket_smack *ssp;
1760
1761 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1762 if (ssp == NULL)
1763 return -ENOMEM;
1764
1765 ssp->smk_in = csp;
1766 ssp->smk_out = csp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001767 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001768
1769 sk->sk_security = ssp;
1770
1771 return 0;
1772}
1773
1774/**
1775 * smack_sk_free_security - Free a socket blob
1776 * @sk: the socket
1777 *
1778 * Clears the blob pointer
1779 */
1780static void smack_sk_free_security(struct sock *sk)
1781{
1782 kfree(sk->sk_security);
1783}
1784
1785/**
Paul Moore07feee82009-03-27 17:10:54 -04001786* smack_host_label - check host based restrictions
1787* @sip: the object end
1788*
1789* looks for host based access restrictions
1790*
1791* This version will only be appropriate for really small sets of single label
1792* hosts. The caller is responsible for ensuring that the RCU read lock is
1793* taken before calling this function.
1794*
1795* Returns the label of the far end or NULL if it's not special.
1796*/
1797static char *smack_host_label(struct sockaddr_in *sip)
1798{
1799 struct smk_netlbladdr *snp;
1800 struct in_addr *siap = &sip->sin_addr;
1801
1802 if (siap->s_addr == 0)
1803 return NULL;
1804
1805 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1806 /*
1807 * we break after finding the first match because
1808 * the list is sorted from longest to shortest mask
1809 * so we have found the most specific match
1810 */
1811 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001812 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1813 /* we have found the special CIPSO option */
1814 if (snp->smk_label == smack_cipso_option)
1815 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001816 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001817 }
Paul Moore07feee82009-03-27 17:10:54 -04001818
1819 return NULL;
1820}
1821
1822/**
Casey Schauflere114e472008-02-04 22:29:50 -08001823 * smack_netlabel - Set the secattr on a socket
1824 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001825 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001826 *
1827 * Convert the outbound smack value (smk_out) to a
1828 * secattr and attach it to the socket.
1829 *
1830 * Returns 0 on success or an error code
1831 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001832static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001833{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001834 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001835 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001836 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001837
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001838 /*
1839 * Usually the netlabel code will handle changing the
1840 * packet labeling based on the label.
1841 * The case of a single label host is different, because
1842 * a single label host should never get a labeled packet
1843 * even though the label is usually associated with a packet
1844 * label.
1845 */
1846 local_bh_disable();
1847 bh_lock_sock_nested(sk);
1848
1849 if (ssp->smk_out == smack_net_ambient ||
1850 labeled == SMACK_UNLABELED_SOCKET)
1851 netlbl_sock_delattr(sk);
1852 else {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001853 skp = smk_find_entry(ssp->smk_out);
1854 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001855 }
1856
1857 bh_unlock_sock(sk);
1858 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001859
Casey Schauflere114e472008-02-04 22:29:50 -08001860 return rc;
1861}
1862
1863/**
Paul Moore07feee82009-03-27 17:10:54 -04001864 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1865 * @sk: the socket
1866 * @sap: the destination address
1867 *
1868 * Set the correct secattr for the given socket based on the destination
1869 * address and perform any outbound access checks needed.
1870 *
1871 * Returns 0 on success or an error code.
1872 *
1873 */
1874static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1875{
1876 int rc;
1877 int sk_lbl;
1878 char *hostsp;
1879 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001880 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001881
1882 rcu_read_lock();
1883 hostsp = smack_host_label(sap);
1884 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001885#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001886 struct lsm_network_audit net;
1887
Eric Paris48c62af2012-04-02 13:15:44 -04001888 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1889 ad.a.u.net->family = sap->sin_family;
1890 ad.a.u.net->dport = sap->sin_port;
1891 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001892#endif
Kees Cook923e9a12012-04-10 13:26:44 -07001893 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001894 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001895 } else {
1896 sk_lbl = SMACK_CIPSO_SOCKET;
1897 rc = 0;
1898 }
1899 rcu_read_unlock();
1900 if (rc != 0)
1901 return rc;
1902
1903 return smack_netlabel(sk, sk_lbl);
1904}
1905
1906/**
Casey Schauflere114e472008-02-04 22:29:50 -08001907 * smack_inode_setsecurity - set smack xattrs
1908 * @inode: the object
1909 * @name: attribute name
1910 * @value: attribute value
1911 * @size: size of the attribute
1912 * @flags: unused
1913 *
1914 * Sets the named attribute in the appropriate blob
1915 *
1916 * Returns 0 on success, or an error code
1917 */
1918static int smack_inode_setsecurity(struct inode *inode, const char *name,
1919 const void *value, size_t size, int flags)
1920{
1921 char *sp;
1922 struct inode_smack *nsp = inode->i_security;
1923 struct socket_smack *ssp;
1924 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001925 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001926
Casey Schauflerf7112e62012-05-06 15:22:02 -07001927 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001928 return -EACCES;
1929
1930 sp = smk_import(value, size);
1931 if (sp == NULL)
1932 return -EINVAL;
1933
1934 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1935 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04001936 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08001937 return 0;
1938 }
1939 /*
1940 * The rest of the Smack xattrs are only on sockets.
1941 */
1942 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1943 return -EOPNOTSUPP;
1944
1945 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001946 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001947 return -EOPNOTSUPP;
1948
1949 ssp = sock->sk->sk_security;
1950
1951 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1952 ssp->smk_in = sp;
1953 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
1954 ssp->smk_out = sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08001955 if (sock->sk->sk_family != PF_UNIX) {
1956 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1957 if (rc != 0)
1958 printk(KERN_WARNING
1959 "Smack: \"%s\" netlbl error %d.\n",
1960 __func__, -rc);
1961 }
Casey Schauflere114e472008-02-04 22:29:50 -08001962 } else
1963 return -EOPNOTSUPP;
1964
1965 return 0;
1966}
1967
1968/**
1969 * smack_socket_post_create - finish socket setup
1970 * @sock: the socket
1971 * @family: protocol family
1972 * @type: unused
1973 * @protocol: unused
1974 * @kern: unused
1975 *
1976 * Sets the netlabel information on the socket
1977 *
1978 * Returns 0 on success, and error code otherwise
1979 */
1980static int smack_socket_post_create(struct socket *sock, int family,
1981 int type, int protocol, int kern)
1982{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001983 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001984 return 0;
1985 /*
1986 * Set the outbound netlbl.
1987 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001988 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
1989}
1990
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001991/**
1992 * smack_socket_connect - connect access check
1993 * @sock: the socket
1994 * @sap: the other end
1995 * @addrlen: size of sap
1996 *
1997 * Verifies that a connection may be possible
1998 *
1999 * Returns 0 on success, and error code otherwise
2000 */
2001static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2002 int addrlen)
2003{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002004 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2005 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002006 if (addrlen < sizeof(struct sockaddr_in))
2007 return -EINVAL;
2008
Paul Moore07feee82009-03-27 17:10:54 -04002009 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08002010}
2011
2012/**
2013 * smack_flags_to_may - convert S_ to MAY_ values
2014 * @flags: the S_ value
2015 *
2016 * Returns the equivalent MAY_ value
2017 */
2018static int smack_flags_to_may(int flags)
2019{
2020 int may = 0;
2021
2022 if (flags & S_IRUGO)
2023 may |= MAY_READ;
2024 if (flags & S_IWUGO)
2025 may |= MAY_WRITE;
2026 if (flags & S_IXUGO)
2027 may |= MAY_EXEC;
2028
2029 return may;
2030}
2031
2032/**
2033 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2034 * @msg: the object
2035 *
2036 * Returns 0
2037 */
2038static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2039{
Casey Schaufler676dac42010-12-02 06:43:39 -08002040 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002041 return 0;
2042}
2043
2044/**
2045 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2046 * @msg: the object
2047 *
2048 * Clears the blob pointer
2049 */
2050static void smack_msg_msg_free_security(struct msg_msg *msg)
2051{
2052 msg->security = NULL;
2053}
2054
2055/**
2056 * smack_of_shm - the smack pointer for the shm
2057 * @shp: the object
2058 *
2059 * Returns a pointer to the smack value
2060 */
2061static char *smack_of_shm(struct shmid_kernel *shp)
2062{
2063 return (char *)shp->shm_perm.security;
2064}
2065
2066/**
2067 * smack_shm_alloc_security - Set the security blob for shm
2068 * @shp: the object
2069 *
2070 * Returns 0
2071 */
2072static int smack_shm_alloc_security(struct shmid_kernel *shp)
2073{
2074 struct kern_ipc_perm *isp = &shp->shm_perm;
2075
Casey Schaufler676dac42010-12-02 06:43:39 -08002076 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002077 return 0;
2078}
2079
2080/**
2081 * smack_shm_free_security - Clear the security blob for shm
2082 * @shp: the object
2083 *
2084 * Clears the blob pointer
2085 */
2086static void smack_shm_free_security(struct shmid_kernel *shp)
2087{
2088 struct kern_ipc_perm *isp = &shp->shm_perm;
2089
2090 isp->security = NULL;
2091}
2092
2093/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002094 * smk_curacc_shm : check if current has access on shm
2095 * @shp : the object
2096 * @access : access requested
2097 *
2098 * Returns 0 if current has the requested access, error code otherwise
2099 */
2100static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2101{
2102 char *ssp = smack_of_shm(shp);
2103 struct smk_audit_info ad;
2104
2105#ifdef CONFIG_AUDIT
2106 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2107 ad.a.u.ipc_id = shp->shm_perm.id;
2108#endif
2109 return smk_curacc(ssp, access, &ad);
2110}
2111
2112/**
Casey Schauflere114e472008-02-04 22:29:50 -08002113 * smack_shm_associate - Smack access check for shm
2114 * @shp: the object
2115 * @shmflg: access requested
2116 *
2117 * Returns 0 if current has the requested access, error code otherwise
2118 */
2119static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2120{
Casey Schauflere114e472008-02-04 22:29:50 -08002121 int may;
2122
2123 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002124 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002125}
2126
2127/**
2128 * smack_shm_shmctl - Smack access check for shm
2129 * @shp: the object
2130 * @cmd: what it wants to do
2131 *
2132 * Returns 0 if current has the requested access, error code otherwise
2133 */
2134static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2135{
Casey Schauflere114e472008-02-04 22:29:50 -08002136 int may;
2137
2138 switch (cmd) {
2139 case IPC_STAT:
2140 case SHM_STAT:
2141 may = MAY_READ;
2142 break;
2143 case IPC_SET:
2144 case SHM_LOCK:
2145 case SHM_UNLOCK:
2146 case IPC_RMID:
2147 may = MAY_READWRITE;
2148 break;
2149 case IPC_INFO:
2150 case SHM_INFO:
2151 /*
2152 * System level information.
2153 */
2154 return 0;
2155 default:
2156 return -EINVAL;
2157 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002158 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002159}
2160
2161/**
2162 * smack_shm_shmat - Smack access for shmat
2163 * @shp: the object
2164 * @shmaddr: unused
2165 * @shmflg: access requested
2166 *
2167 * Returns 0 if current has the requested access, error code otherwise
2168 */
2169static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2170 int shmflg)
2171{
Casey Schauflere114e472008-02-04 22:29:50 -08002172 int may;
2173
2174 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002175 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002176}
2177
2178/**
2179 * smack_of_sem - the smack pointer for the sem
2180 * @sma: the object
2181 *
2182 * Returns a pointer to the smack value
2183 */
2184static char *smack_of_sem(struct sem_array *sma)
2185{
2186 return (char *)sma->sem_perm.security;
2187}
2188
2189/**
2190 * smack_sem_alloc_security - Set the security blob for sem
2191 * @sma: the object
2192 *
2193 * Returns 0
2194 */
2195static int smack_sem_alloc_security(struct sem_array *sma)
2196{
2197 struct kern_ipc_perm *isp = &sma->sem_perm;
2198
Casey Schaufler676dac42010-12-02 06:43:39 -08002199 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002200 return 0;
2201}
2202
2203/**
2204 * smack_sem_free_security - Clear the security blob for sem
2205 * @sma: the object
2206 *
2207 * Clears the blob pointer
2208 */
2209static void smack_sem_free_security(struct sem_array *sma)
2210{
2211 struct kern_ipc_perm *isp = &sma->sem_perm;
2212
2213 isp->security = NULL;
2214}
2215
2216/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002217 * smk_curacc_sem : check if current has access on sem
2218 * @sma : the object
2219 * @access : access requested
2220 *
2221 * Returns 0 if current has the requested access, error code otherwise
2222 */
2223static int smk_curacc_sem(struct sem_array *sma, int access)
2224{
2225 char *ssp = smack_of_sem(sma);
2226 struct smk_audit_info ad;
2227
2228#ifdef CONFIG_AUDIT
2229 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2230 ad.a.u.ipc_id = sma->sem_perm.id;
2231#endif
2232 return smk_curacc(ssp, access, &ad);
2233}
2234
2235/**
Casey Schauflere114e472008-02-04 22:29:50 -08002236 * smack_sem_associate - Smack access check for sem
2237 * @sma: the object
2238 * @semflg: access requested
2239 *
2240 * Returns 0 if current has the requested access, error code otherwise
2241 */
2242static int smack_sem_associate(struct sem_array *sma, int semflg)
2243{
Casey Schauflere114e472008-02-04 22:29:50 -08002244 int may;
2245
2246 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002247 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002248}
2249
2250/**
2251 * smack_sem_shmctl - Smack access check for sem
2252 * @sma: the object
2253 * @cmd: what it wants to do
2254 *
2255 * Returns 0 if current has the requested access, error code otherwise
2256 */
2257static int smack_sem_semctl(struct sem_array *sma, int cmd)
2258{
Casey Schauflere114e472008-02-04 22:29:50 -08002259 int may;
2260
2261 switch (cmd) {
2262 case GETPID:
2263 case GETNCNT:
2264 case GETZCNT:
2265 case GETVAL:
2266 case GETALL:
2267 case IPC_STAT:
2268 case SEM_STAT:
2269 may = MAY_READ;
2270 break;
2271 case SETVAL:
2272 case SETALL:
2273 case IPC_RMID:
2274 case IPC_SET:
2275 may = MAY_READWRITE;
2276 break;
2277 case IPC_INFO:
2278 case SEM_INFO:
2279 /*
2280 * System level information
2281 */
2282 return 0;
2283 default:
2284 return -EINVAL;
2285 }
2286
Etienne Bassetecfcc532009-04-08 20:40:06 +02002287 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002288}
2289
2290/**
2291 * smack_sem_semop - Smack checks of semaphore operations
2292 * @sma: the object
2293 * @sops: unused
2294 * @nsops: unused
2295 * @alter: unused
2296 *
2297 * Treated as read and write in all cases.
2298 *
2299 * Returns 0 if access is allowed, error code otherwise
2300 */
2301static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2302 unsigned nsops, int alter)
2303{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002304 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002305}
2306
2307/**
2308 * smack_msg_alloc_security - Set the security blob for msg
2309 * @msq: the object
2310 *
2311 * Returns 0
2312 */
2313static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2314{
2315 struct kern_ipc_perm *kisp = &msq->q_perm;
2316
Casey Schaufler676dac42010-12-02 06:43:39 -08002317 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002318 return 0;
2319}
2320
2321/**
2322 * smack_msg_free_security - Clear the security blob for msg
2323 * @msq: the object
2324 *
2325 * Clears the blob pointer
2326 */
2327static void smack_msg_queue_free_security(struct msg_queue *msq)
2328{
2329 struct kern_ipc_perm *kisp = &msq->q_perm;
2330
2331 kisp->security = NULL;
2332}
2333
2334/**
2335 * smack_of_msq - the smack pointer for the msq
2336 * @msq: the object
2337 *
2338 * Returns a pointer to the smack value
2339 */
2340static char *smack_of_msq(struct msg_queue *msq)
2341{
2342 return (char *)msq->q_perm.security;
2343}
2344
2345/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002346 * smk_curacc_msq : helper to check if current has access on msq
2347 * @msq : the msq
2348 * @access : access requested
2349 *
2350 * return 0 if current has access, error otherwise
2351 */
2352static int smk_curacc_msq(struct msg_queue *msq, int access)
2353{
2354 char *msp = smack_of_msq(msq);
2355 struct smk_audit_info ad;
2356
2357#ifdef CONFIG_AUDIT
2358 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2359 ad.a.u.ipc_id = msq->q_perm.id;
2360#endif
2361 return smk_curacc(msp, access, &ad);
2362}
2363
2364/**
Casey Schauflere114e472008-02-04 22:29:50 -08002365 * smack_msg_queue_associate - Smack access check for msg_queue
2366 * @msq: the object
2367 * @msqflg: access requested
2368 *
2369 * Returns 0 if current has the requested access, error code otherwise
2370 */
2371static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2372{
Casey Schauflere114e472008-02-04 22:29:50 -08002373 int may;
2374
2375 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002376 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002377}
2378
2379/**
2380 * smack_msg_queue_msgctl - Smack access check for msg_queue
2381 * @msq: the object
2382 * @cmd: what it wants to do
2383 *
2384 * Returns 0 if current has the requested access, error code otherwise
2385 */
2386static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2387{
Casey Schauflere114e472008-02-04 22:29:50 -08002388 int may;
2389
2390 switch (cmd) {
2391 case IPC_STAT:
2392 case MSG_STAT:
2393 may = MAY_READ;
2394 break;
2395 case IPC_SET:
2396 case IPC_RMID:
2397 may = MAY_READWRITE;
2398 break;
2399 case IPC_INFO:
2400 case MSG_INFO:
2401 /*
2402 * System level information
2403 */
2404 return 0;
2405 default:
2406 return -EINVAL;
2407 }
2408
Etienne Bassetecfcc532009-04-08 20:40:06 +02002409 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002410}
2411
2412/**
2413 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2414 * @msq: the object
2415 * @msg: unused
2416 * @msqflg: access requested
2417 *
2418 * Returns 0 if current has the requested access, error code otherwise
2419 */
2420static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2421 int msqflg)
2422{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002423 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002424
Etienne Bassetecfcc532009-04-08 20:40:06 +02002425 may = smack_flags_to_may(msqflg);
2426 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002427}
2428
2429/**
2430 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2431 * @msq: the object
2432 * @msg: unused
2433 * @target: unused
2434 * @type: unused
2435 * @mode: unused
2436 *
2437 * Returns 0 if current has read and write access, error code otherwise
2438 */
2439static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2440 struct task_struct *target, long type, int mode)
2441{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002442 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002443}
2444
2445/**
2446 * smack_ipc_permission - Smack access for ipc_permission()
2447 * @ipp: the object permissions
2448 * @flag: access requested
2449 *
2450 * Returns 0 if current has read and write access, error code otherwise
2451 */
2452static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2453{
2454 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002455 int may = smack_flags_to_may(flag);
2456 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002457
Etienne Bassetecfcc532009-04-08 20:40:06 +02002458#ifdef CONFIG_AUDIT
2459 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2460 ad.a.u.ipc_id = ipp->id;
2461#endif
2462 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002463}
2464
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002465/**
2466 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002467 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002468 * @secid: where result will be saved
2469 */
2470static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2471{
2472 char *smack = ipp->security;
2473
2474 *secid = smack_to_secid(smack);
2475}
2476
Casey Schauflere114e472008-02-04 22:29:50 -08002477/**
2478 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002479 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002480 * @inode: the object
2481 *
2482 * Set the inode's security blob if it hasn't been done already.
2483 */
2484static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2485{
2486 struct super_block *sbp;
2487 struct superblock_smack *sbsp;
2488 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002489 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002490 char *fetched;
2491 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002492 char trattr[TRANS_TRUE_SIZE];
2493 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002494 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002495 struct dentry *dp;
2496
2497 if (inode == NULL)
2498 return;
2499
2500 isp = inode->i_security;
2501
2502 mutex_lock(&isp->smk_lock);
2503 /*
2504 * If the inode is already instantiated
2505 * take the quick way out
2506 */
2507 if (isp->smk_flags & SMK_INODE_INSTANT)
2508 goto unlockandout;
2509
2510 sbp = inode->i_sb;
2511 sbsp = sbp->s_security;
2512 /*
2513 * We're going to use the superblock default label
2514 * if there's no label on the file.
2515 */
2516 final = sbsp->smk_default;
2517
2518 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002519 * If this is the root inode the superblock
2520 * may be in the process of initialization.
2521 * If that is the case use the root value out
2522 * of the superblock.
2523 */
2524 if (opt_dentry->d_parent == opt_dentry) {
2525 isp->smk_inode = sbsp->smk_root;
2526 isp->smk_flags |= SMK_INODE_INSTANT;
2527 goto unlockandout;
2528 }
2529
2530 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002531 * This is pretty hackish.
2532 * Casey says that we shouldn't have to do
2533 * file system specific code, but it does help
2534 * with keeping it simple.
2535 */
2536 switch (sbp->s_magic) {
2537 case SMACK_MAGIC:
2538 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002539 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002540 * that the smack file system doesn't do
2541 * extended attributes.
2542 */
2543 final = smack_known_star.smk_known;
2544 break;
2545 case PIPEFS_MAGIC:
2546 /*
2547 * Casey says pipes are easy (?)
2548 */
2549 final = smack_known_star.smk_known;
2550 break;
2551 case DEVPTS_SUPER_MAGIC:
2552 /*
2553 * devpts seems content with the label of the task.
2554 * Programs that change smack have to treat the
2555 * pty with respect.
2556 */
2557 final = csp;
2558 break;
2559 case SOCKFS_MAGIC:
2560 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002561 * Socket access is controlled by the socket
2562 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002563 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002564 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002565 break;
2566 case PROC_SUPER_MAGIC:
2567 /*
2568 * Casey says procfs appears not to care.
2569 * The superblock default suffices.
2570 */
2571 break;
2572 case TMPFS_MAGIC:
2573 /*
2574 * Device labels should come from the filesystem,
2575 * but watch out, because they're volitile,
2576 * getting recreated on every reboot.
2577 */
2578 final = smack_known_star.smk_known;
2579 /*
2580 * No break.
2581 *
2582 * If a smack value has been set we want to use it,
2583 * but since tmpfs isn't giving us the opportunity
2584 * to set mount options simulate setting the
2585 * superblock default.
2586 */
2587 default:
2588 /*
2589 * This isn't an understood special case.
2590 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002591 */
2592
2593 /*
2594 * UNIX domain sockets use lower level socket data.
2595 */
2596 if (S_ISSOCK(inode->i_mode)) {
2597 final = smack_known_star.smk_known;
2598 break;
2599 }
2600 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002601 * No xattr support means, alas, no SMACK label.
2602 * Use the aforeapplied default.
2603 * It would be curious if the label of the task
2604 * does not match that assigned.
2605 */
2606 if (inode->i_op->getxattr == NULL)
2607 break;
2608 /*
2609 * Get the dentry for xattr.
2610 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002611 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002612 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Casey Schaufler2267b132012-03-13 19:14:19 -07002613 if (fetched != NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002614 final = fetched;
Casey Schaufler2267b132012-03-13 19:14:19 -07002615
2616 /*
2617 * Transmuting directory
2618 */
2619 if (S_ISDIR(inode->i_mode)) {
2620 /*
2621 * If this is a new directory and the label was
2622 * transmuted when the inode was initialized
2623 * set the transmute attribute on the directory
2624 * and mark the inode.
2625 *
2626 * If there is a transmute attribute on the
2627 * directory mark the inode.
2628 */
2629 if (isp->smk_flags & SMK_INODE_CHANGED) {
2630 isp->smk_flags &= ~SMK_INODE_CHANGED;
2631 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002632 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002633 TRANS_TRUE, TRANS_TRUE_SIZE,
2634 0);
2635 } else {
2636 rc = inode->i_op->getxattr(dp,
2637 XATTR_NAME_SMACKTRANSMUTE, trattr,
2638 TRANS_TRUE_SIZE);
2639 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2640 TRANS_TRUE_SIZE) != 0)
2641 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002642 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002643 if (rc >= 0)
2644 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002645 }
2646 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002647 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002648
Casey Schauflere114e472008-02-04 22:29:50 -08002649 dput(dp);
2650 break;
2651 }
2652
2653 if (final == NULL)
2654 isp->smk_inode = csp;
2655 else
2656 isp->smk_inode = final;
2657
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002658 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002659
2660unlockandout:
2661 mutex_unlock(&isp->smk_lock);
2662 return;
2663}
2664
2665/**
2666 * smack_getprocattr - Smack process attribute access
2667 * @p: the object task
2668 * @name: the name of the attribute in /proc/.../attr
2669 * @value: where to put the result
2670 *
2671 * Places a copy of the task Smack into value
2672 *
2673 * Returns the length of the smack label or an error code
2674 */
2675static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2676{
2677 char *cp;
2678 int slen;
2679
2680 if (strcmp(name, "current") != 0)
2681 return -EINVAL;
2682
Casey Schaufler676dac42010-12-02 06:43:39 -08002683 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002684 if (cp == NULL)
2685 return -ENOMEM;
2686
2687 slen = strlen(cp);
2688 *value = cp;
2689 return slen;
2690}
2691
2692/**
2693 * smack_setprocattr - Smack process attribute setting
2694 * @p: the object task
2695 * @name: the name of the attribute in /proc/.../attr
2696 * @value: the value to set
2697 * @size: the size of the value
2698 *
2699 * Sets the Smack value of the task. Only setting self
2700 * is permitted and only with privilege
2701 *
2702 * Returns the length of the smack label or an error code
2703 */
2704static int smack_setprocattr(struct task_struct *p, char *name,
2705 void *value, size_t size)
2706{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002707 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002708 struct task_smack *tsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002709 struct task_smack *oldtsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002710 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002711 char *newsmack;
2712
Casey Schauflere114e472008-02-04 22:29:50 -08002713 /*
2714 * Changing another process' Smack value is too dangerous
2715 * and supports no sane use case.
2716 */
2717 if (p != current)
2718 return -EPERM;
2719
David Howells5cd9c582008-08-14 11:37:28 +01002720 if (!capable(CAP_MAC_ADMIN))
2721 return -EPERM;
2722
Casey Schauflerf7112e62012-05-06 15:22:02 -07002723 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08002724 return -EINVAL;
2725
2726 if (strcmp(name, "current") != 0)
2727 return -EINVAL;
2728
2729 newsmack = smk_import(value, size);
2730 if (newsmack == NULL)
2731 return -EINVAL;
2732
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002733 /*
2734 * No process is ever allowed the web ("@") label.
2735 */
2736 if (newsmack == smack_known_web.smk_known)
2737 return -EPERM;
2738
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002739 oldtsp = p->cred->security;
David Howellsd84f4f92008-11-14 10:39:23 +11002740 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002741 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002742 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002743
2744 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08002745 if (tsp == NULL) {
2746 kfree(new);
2747 return -ENOMEM;
2748 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002749 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2750 if (rc != 0)
2751 return rc;
2752
Casey Schaufler676dac42010-12-02 06:43:39 -08002753 new->security = tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002754 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002755 return size;
2756}
2757
2758/**
2759 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002760 * @sock: one sock
2761 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002762 * @newsk: unused
2763 *
2764 * Return 0 if a subject with the smack of sock could access
2765 * an object with the smack of other, otherwise an error code
2766 */
David S. Miller3610cda2011-01-05 15:38:53 -08002767static int smack_unix_stream_connect(struct sock *sock,
2768 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002769{
James Morrisd2e7ad12011-01-10 09:46:24 +11002770 struct socket_smack *ssp = sock->sk_security;
2771 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002772 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002773 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002774 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002775
Kees Cook923e9a12012-04-10 13:26:44 -07002776#ifdef CONFIG_AUDIT
2777 struct lsm_network_audit net;
2778
Eric Paris48c62af2012-04-02 13:15:44 -04002779 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
David S. Miller3610cda2011-01-05 15:38:53 -08002780 smk_ad_setfield_u_net_sk(&ad, other);
Kees Cook923e9a12012-04-10 13:26:44 -07002781#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002782
2783 if (!capable(CAP_MAC_OVERRIDE))
2784 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2785
Casey Schaufler975d5e52011-09-26 14:43:39 -07002786 /*
2787 * Cross reference the peer labels for SO_PEERSEC.
2788 */
2789 if (rc == 0) {
2790 nsp->smk_packet = ssp->smk_out;
2791 ssp->smk_packet = osp->smk_out;
2792 }
2793
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002794 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002795}
2796
2797/**
2798 * smack_unix_may_send - Smack access on UDS
2799 * @sock: one socket
2800 * @other: the other socket
2801 *
2802 * Return 0 if a subject with the smack of sock could access
2803 * an object with the smack of other, otherwise an error code
2804 */
2805static int smack_unix_may_send(struct socket *sock, struct socket *other)
2806{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002807 struct socket_smack *ssp = sock->sk->sk_security;
2808 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002809 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002810 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002811
Kees Cook923e9a12012-04-10 13:26:44 -07002812#ifdef CONFIG_AUDIT
2813 struct lsm_network_audit net;
2814
Eric Paris48c62af2012-04-02 13:15:44 -04002815 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002816 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07002817#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002818
2819 if (!capable(CAP_MAC_OVERRIDE))
2820 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2821
2822 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002823}
2824
2825/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002826 * smack_socket_sendmsg - Smack check based on destination host
2827 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002828 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002829 * @size: the size of the message
2830 *
2831 * Return 0 if the current subject can write to the destination
2832 * host. This is only a question if the destination is a single
2833 * label host.
2834 */
2835static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2836 int size)
2837{
2838 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002839
2840 /*
2841 * Perfectly reasonable for this to be NULL
2842 */
Julia Lawallda34d422009-08-05 14:34:55 +02002843 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002844 return 0;
2845
Paul Moore07feee82009-03-27 17:10:54 -04002846 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002847}
2848
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002849/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002850 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002851 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002852 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08002853 *
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002854 * Returns a pointer to a Smack label found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08002855 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002856static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2857 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08002858{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002859 struct smack_known *kp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002860 char *sp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002861 int found = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002862
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002863 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002864 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002865 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002866 * If there are flags but no level netlabel isn't
2867 * behaving the way we expect it to.
2868 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07002869 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08002870 * Without guidance regarding the smack value
2871 * for the packet fall back on the network
2872 * ambient value.
2873 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07002874 rcu_read_lock();
2875 list_for_each_entry(kp, &smack_known_list, list) {
2876 if (sap->attr.mls.lvl != kp->smk_netlabel.attr.mls.lvl)
2877 continue;
2878 if (memcmp(sap->attr.mls.cat,
2879 kp->smk_netlabel.attr.mls.cat,
2880 SMK_CIPSOLEN) != 0)
2881 continue;
2882 found = 1;
2883 break;
Casey Schauflere114e472008-02-04 22:29:50 -08002884 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07002885 rcu_read_unlock();
2886
2887 if (found)
2888 return kp->smk_known;
2889
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002890 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2891 return smack_known_web.smk_known;
2892 return smack_known_star.smk_known;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002893 }
2894 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2895 /*
2896 * Looks like a fallback, which gives us a secid.
2897 */
2898 sp = smack_from_secid(sap->attr.secid);
2899 /*
2900 * This has got to be a bug because it is
2901 * impossible to specify a fallback without
2902 * specifying the label, which will ensure
2903 * it has a secid, and the only way to get a
2904 * secid is from a fallback.
2905 */
2906 BUG_ON(sp == NULL);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002907 return sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002908 }
2909 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002910 * Without guidance regarding the smack value
2911 * for the packet fall back on the network
2912 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002913 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002914 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08002915}
2916
2917/**
2918 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2919 * @sk: socket
2920 * @skb: packet
2921 *
2922 * Returns 0 if the packet should be delivered, an error code otherwise
2923 */
2924static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2925{
2926 struct netlbl_lsm_secattr secattr;
2927 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002928 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002929 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002930 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07002931#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04002932 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07002933#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002934 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2935 return 0;
2936
2937 /*
2938 * Translate what netlabel gave us.
2939 */
Casey Schauflere114e472008-02-04 22:29:50 -08002940 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002941
Casey Schauflere114e472008-02-04 22:29:50 -08002942 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002943 if (rc == 0)
2944 csp = smack_from_secattr(&secattr, ssp);
2945 else
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002946 csp = smack_net_ambient;
2947
Casey Schauflere114e472008-02-04 22:29:50 -08002948 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002949
Etienne Bassetecfcc532009-04-08 20:40:06 +02002950#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04002951 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2952 ad.a.u.net->family = sk->sk_family;
2953 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002954 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
2955#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002956 /*
2957 * Receiving a packet requires that the other end
2958 * be able to write here. Read access is not required.
2959 * This is the simplist possible security model
2960 * for networking.
2961 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02002962 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04002963 if (rc != 0)
2964 netlbl_skbuff_err(skb, rc, 0);
2965 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002966}
2967
2968/**
2969 * smack_socket_getpeersec_stream - pull in packet label
2970 * @sock: the socket
2971 * @optval: user's destination
2972 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08002973 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08002974 *
2975 * returns zero on success, an error code otherwise
2976 */
2977static int smack_socket_getpeersec_stream(struct socket *sock,
2978 char __user *optval,
2979 int __user *optlen, unsigned len)
2980{
2981 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002982 char *rcp = "";
2983 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08002984 int rc = 0;
2985
2986 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002987 if (ssp->smk_packet != NULL) {
2988 rcp = ssp->smk_packet;
2989 slen = strlen(rcp) + 1;
2990 }
Casey Schauflere114e472008-02-04 22:29:50 -08002991
2992 if (slen > len)
2993 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002994 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08002995 rc = -EFAULT;
2996
2997 if (put_user(slen, optlen) != 0)
2998 rc = -EFAULT;
2999
3000 return rc;
3001}
3002
3003
3004/**
3005 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003006 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003007 * @skb: packet data
3008 * @secid: pointer to where to put the secid of the packet
3009 *
3010 * Sets the netlabel socket state on sk from parent
3011 */
3012static int smack_socket_getpeersec_dgram(struct socket *sock,
3013 struct sk_buff *skb, u32 *secid)
3014
3015{
3016 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003017 struct socket_smack *ssp = NULL;
3018 char *sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003019 int family = PF_UNSPEC;
3020 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003021 int rc;
3022
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003023 if (skb != NULL) {
3024 if (skb->protocol == htons(ETH_P_IP))
3025 family = PF_INET;
3026 else if (skb->protocol == htons(ETH_P_IPV6))
3027 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003028 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003029 if (family == PF_UNSPEC && sock != NULL)
3030 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003031
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003032 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003033 ssp = sock->sk->sk_security;
3034 s = smack_to_secid(ssp->smk_out);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003035 } else if (family == PF_INET || family == PF_INET6) {
3036 /*
3037 * Translate what netlabel gave us.
3038 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003039 if (sock != NULL && sock->sk != NULL)
3040 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003041 netlbl_secattr_init(&secattr);
3042 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3043 if (rc == 0) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003044 sp = smack_from_secattr(&secattr, ssp);
3045 s = smack_to_secid(sp);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003046 }
3047 netlbl_secattr_destroy(&secattr);
3048 }
3049 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003050 if (s == 0)
3051 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003052 return 0;
3053}
3054
3055/**
Paul Moore07feee82009-03-27 17:10:54 -04003056 * smack_sock_graft - Initialize a newly created socket with an existing sock
3057 * @sk: child sock
3058 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003059 *
Paul Moore07feee82009-03-27 17:10:54 -04003060 * Set the smk_{in,out} state of an existing sock based on the process that
3061 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003062 */
3063static void smack_sock_graft(struct sock *sk, struct socket *parent)
3064{
3065 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08003066
Paul Moore07feee82009-03-27 17:10:54 -04003067 if (sk == NULL ||
3068 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003069 return;
3070
3071 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08003072 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04003073 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003074}
3075
3076/**
3077 * smack_inet_conn_request - Smack access check on connect
3078 * @sk: socket involved
3079 * @skb: packet
3080 * @req: unused
3081 *
3082 * Returns 0 if a task with the packet label could write to
3083 * the socket, otherwise an error code
3084 */
3085static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3086 struct request_sock *req)
3087{
Paul Moore07feee82009-03-27 17:10:54 -04003088 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003089 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003090 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003091 struct netlbl_lsm_secattr secattr;
3092 struct sockaddr_in addr;
3093 struct iphdr *hdr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003094 char *sp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003095 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003096 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003097 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003098#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003099 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003100#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003101
Paul Moore07feee82009-03-27 17:10:54 -04003102 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3103 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3104 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08003105
Paul Moore07feee82009-03-27 17:10:54 -04003106 netlbl_secattr_init(&secattr);
3107 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003108 if (rc == 0)
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003109 sp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003110 else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003111 sp = smack_known_huh.smk_known;
Paul Moore07feee82009-03-27 17:10:54 -04003112 netlbl_secattr_destroy(&secattr);
3113
Etienne Bassetecfcc532009-04-08 20:40:06 +02003114#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003115 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3116 ad.a.u.net->family = family;
3117 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003118 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3119#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003120 /*
Paul Moore07feee82009-03-27 17:10:54 -04003121 * Receiving a packet requires that the other end be able to write
3122 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003123 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003124 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003125 if (rc != 0)
3126 return rc;
3127
3128 /*
3129 * Save the peer's label in the request_sock so we can later setup
3130 * smk_packet in the child socket so that SO_PEERCRED can report it.
3131 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003132 req->peer_secid = smack_to_secid(sp);
Paul Moore07feee82009-03-27 17:10:54 -04003133
3134 /*
3135 * We need to decide if we want to label the incoming connection here
3136 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003137 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003138 */
3139 hdr = ip_hdr(skb);
3140 addr.sin_addr.s_addr = hdr->saddr;
3141 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003142 hsp = smack_host_label(&addr);
3143 rcu_read_unlock();
3144
3145 if (hsp == NULL) {
3146 skp = smk_find_entry(sp);
3147 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3148 } else
Paul Moore07feee82009-03-27 17:10:54 -04003149 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003150
3151 return rc;
3152}
3153
Paul Moore07feee82009-03-27 17:10:54 -04003154/**
3155 * smack_inet_csk_clone - Copy the connection information to the new socket
3156 * @sk: the new socket
3157 * @req: the connection's request_sock
3158 *
3159 * Transfer the connection's peer label to the newly created socket.
3160 */
3161static void smack_inet_csk_clone(struct sock *sk,
3162 const struct request_sock *req)
3163{
3164 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003165
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003166 if (req->peer_secid != 0)
3167 ssp->smk_packet = smack_from_secid(req->peer_secid);
3168 else
3169 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003170}
3171
Casey Schauflere114e472008-02-04 22:29:50 -08003172/*
3173 * Key management security hooks
3174 *
3175 * Casey has not tested key support very heavily.
3176 * The permission check is most likely too restrictive.
3177 * If you care about keys please have a look.
3178 */
3179#ifdef CONFIG_KEYS
3180
3181/**
3182 * smack_key_alloc - Set the key security blob
3183 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003184 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003185 * @flags: unused
3186 *
3187 * No allocation required
3188 *
3189 * Returns 0
3190 */
David Howellsd84f4f92008-11-14 10:39:23 +11003191static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003192 unsigned long flags)
3193{
Casey Schaufler676dac42010-12-02 06:43:39 -08003194 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003195 return 0;
3196}
3197
3198/**
3199 * smack_key_free - Clear the key security blob
3200 * @key: the object
3201 *
3202 * Clear the blob pointer
3203 */
3204static void smack_key_free(struct key *key)
3205{
3206 key->security = NULL;
3207}
3208
3209/*
3210 * smack_key_permission - Smack access on a key
3211 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003212 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003213 * @perm: unused
3214 *
3215 * Return 0 if the task has read and write to the object,
3216 * an error code otherwise
3217 */
3218static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003219 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003220{
3221 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003222 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08003223 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003224
3225 keyp = key_ref_to_ptr(key_ref);
3226 if (keyp == NULL)
3227 return -EINVAL;
3228 /*
3229 * If the key hasn't been initialized give it access so that
3230 * it may do so.
3231 */
3232 if (keyp->security == NULL)
3233 return 0;
3234 /*
3235 * This should not occur
3236 */
Casey Schaufler676dac42010-12-02 06:43:39 -08003237 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003238 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003239#ifdef CONFIG_AUDIT
3240 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3241 ad.a.u.key_struct.key = keyp->serial;
3242 ad.a.u.key_struct.key_desc = keyp->description;
3243#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08003244 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02003245 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003246}
3247#endif /* CONFIG_KEYS */
3248
3249/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003250 * Smack Audit hooks
3251 *
3252 * Audit requires a unique representation of each Smack specific
3253 * rule. This unique representation is used to distinguish the
3254 * object to be audited from remaining kernel objects and also
3255 * works as a glue between the audit hooks.
3256 *
3257 * Since repository entries are added but never deleted, we'll use
3258 * the smack_known label address related to the given audit rule as
3259 * the needed unique representation. This also better fits the smack
3260 * model where nearly everything is a label.
3261 */
3262#ifdef CONFIG_AUDIT
3263
3264/**
3265 * smack_audit_rule_init - Initialize a smack audit rule
3266 * @field: audit rule fields given from user-space (audit.h)
3267 * @op: required testing operator (=, !=, >, <, ...)
3268 * @rulestr: smack label to be audited
3269 * @vrule: pointer to save our own audit rule representation
3270 *
3271 * Prepare to audit cases where (@field @op @rulestr) is true.
3272 * The label to be audited is created if necessay.
3273 */
3274static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3275{
3276 char **rule = (char **)vrule;
3277 *rule = NULL;
3278
3279 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3280 return -EINVAL;
3281
Al Viro5af75d82008-12-16 05:59:26 -05003282 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003283 return -EINVAL;
3284
3285 *rule = smk_import(rulestr, 0);
3286
3287 return 0;
3288}
3289
3290/**
3291 * smack_audit_rule_known - Distinguish Smack audit rules
3292 * @krule: rule of interest, in Audit kernel representation format
3293 *
3294 * This is used to filter Smack rules from remaining Audit ones.
3295 * If it's proved that this rule belongs to us, the
3296 * audit_rule_match hook will be called to do the final judgement.
3297 */
3298static int smack_audit_rule_known(struct audit_krule *krule)
3299{
3300 struct audit_field *f;
3301 int i;
3302
3303 for (i = 0; i < krule->field_count; i++) {
3304 f = &krule->fields[i];
3305
3306 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3307 return 1;
3308 }
3309
3310 return 0;
3311}
3312
3313/**
3314 * smack_audit_rule_match - Audit given object ?
3315 * @secid: security id for identifying the object to test
3316 * @field: audit rule flags given from user-space
3317 * @op: required testing operator
3318 * @vrule: smack internal rule presentation
3319 * @actx: audit context associated with the check
3320 *
3321 * The core Audit hook. It's used to take the decision of
3322 * whether to audit or not to audit a given object.
3323 */
3324static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3325 struct audit_context *actx)
3326{
3327 char *smack;
3328 char *rule = vrule;
3329
3330 if (!rule) {
Tetsuo Handaceffec552012-03-29 16:19:05 +09003331 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003332 "Smack: missing rule\n");
3333 return -ENOENT;
3334 }
3335
3336 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3337 return 0;
3338
3339 smack = smack_from_secid(secid);
3340
3341 /*
3342 * No need to do string comparisons. If a match occurs,
3343 * both pointers will point to the same smack_known
3344 * label.
3345 */
Al Viro5af75d82008-12-16 05:59:26 -05003346 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003347 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003348 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003349 return (rule != smack);
3350
3351 return 0;
3352}
3353
3354/**
3355 * smack_audit_rule_free - free smack rule representation
3356 * @vrule: rule to be freed.
3357 *
3358 * No memory was allocated.
3359 */
3360static void smack_audit_rule_free(void *vrule)
3361{
3362 /* No-op */
3363}
3364
3365#endif /* CONFIG_AUDIT */
3366
Randy Dunlap251a2a92009-02-18 11:42:33 -08003367/**
Casey Schauflere114e472008-02-04 22:29:50 -08003368 * smack_secid_to_secctx - return the smack label for a secid
3369 * @secid: incoming integer
3370 * @secdata: destination
3371 * @seclen: how long it is
3372 *
3373 * Exists for networking code.
3374 */
3375static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3376{
3377 char *sp = smack_from_secid(secid);
3378
Eric Parisd5630b92010-10-13 16:24:48 -04003379 if (secdata)
3380 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003381 *seclen = strlen(sp);
3382 return 0;
3383}
3384
Randy Dunlap251a2a92009-02-18 11:42:33 -08003385/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003386 * smack_secctx_to_secid - return the secid for a smack label
3387 * @secdata: smack label
3388 * @seclen: how long result is
3389 * @secid: outgoing integer
3390 *
3391 * Exists for audit and networking code.
3392 */
David Howellse52c17642008-04-29 20:52:51 +01003393static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003394{
3395 *secid = smack_to_secid(secdata);
3396 return 0;
3397}
3398
Randy Dunlap251a2a92009-02-18 11:42:33 -08003399/**
Casey Schauflere114e472008-02-04 22:29:50 -08003400 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003401 * @secdata: unused
3402 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003403 *
3404 * Exists to make sure nothing gets done, and properly
3405 */
3406static void smack_release_secctx(char *secdata, u32 seclen)
3407{
3408}
3409
David P. Quigley1ee65e32009-09-03 14:25:57 -04003410static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3411{
3412 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3413}
3414
3415static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3416{
3417 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3418}
3419
3420static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3421{
3422 int len = 0;
3423 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3424
3425 if (len < 0)
3426 return len;
3427 *ctxlen = len;
3428 return 0;
3429}
3430
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003431struct security_operations smack_ops = {
3432 .name = "smack",
3433
Ingo Molnar9e488582009-05-07 19:26:19 +10003434 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003435 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003436 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003437
3438 .sb_alloc_security = smack_sb_alloc_security,
3439 .sb_free_security = smack_sb_free_security,
3440 .sb_copy_data = smack_sb_copy_data,
3441 .sb_kern_mount = smack_sb_kern_mount,
3442 .sb_statfs = smack_sb_statfs,
3443 .sb_mount = smack_sb_mount,
3444 .sb_umount = smack_sb_umount,
3445
Casey Schaufler676dac42010-12-02 06:43:39 -08003446 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003447 .bprm_committing_creds = smack_bprm_committing_creds,
3448 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003449
Casey Schauflere114e472008-02-04 22:29:50 -08003450 .inode_alloc_security = smack_inode_alloc_security,
3451 .inode_free_security = smack_inode_free_security,
3452 .inode_init_security = smack_inode_init_security,
3453 .inode_link = smack_inode_link,
3454 .inode_unlink = smack_inode_unlink,
3455 .inode_rmdir = smack_inode_rmdir,
3456 .inode_rename = smack_inode_rename,
3457 .inode_permission = smack_inode_permission,
3458 .inode_setattr = smack_inode_setattr,
3459 .inode_getattr = smack_inode_getattr,
3460 .inode_setxattr = smack_inode_setxattr,
3461 .inode_post_setxattr = smack_inode_post_setxattr,
3462 .inode_getxattr = smack_inode_getxattr,
3463 .inode_removexattr = smack_inode_removexattr,
3464 .inode_getsecurity = smack_inode_getsecurity,
3465 .inode_setsecurity = smack_inode_setsecurity,
3466 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003467 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003468
3469 .file_permission = smack_file_permission,
3470 .file_alloc_security = smack_file_alloc_security,
3471 .file_free_security = smack_file_free_security,
3472 .file_ioctl = smack_file_ioctl,
3473 .file_lock = smack_file_lock,
3474 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003475 .mmap_file = smack_mmap_file,
3476 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003477 .file_set_fowner = smack_file_set_fowner,
3478 .file_send_sigiotask = smack_file_send_sigiotask,
3479 .file_receive = smack_file_receive,
3480
Eric Paris83d49852012-04-04 13:45:40 -04003481 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003482
David Howellsee18d642009-09-02 09:14:21 +01003483 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003484 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003485 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003486 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003487 .kernel_act_as = smack_kernel_act_as,
3488 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003489 .task_setpgid = smack_task_setpgid,
3490 .task_getpgid = smack_task_getpgid,
3491 .task_getsid = smack_task_getsid,
3492 .task_getsecid = smack_task_getsecid,
3493 .task_setnice = smack_task_setnice,
3494 .task_setioprio = smack_task_setioprio,
3495 .task_getioprio = smack_task_getioprio,
3496 .task_setscheduler = smack_task_setscheduler,
3497 .task_getscheduler = smack_task_getscheduler,
3498 .task_movememory = smack_task_movememory,
3499 .task_kill = smack_task_kill,
3500 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003501 .task_to_inode = smack_task_to_inode,
3502
3503 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003504 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003505
3506 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3507 .msg_msg_free_security = smack_msg_msg_free_security,
3508
3509 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3510 .msg_queue_free_security = smack_msg_queue_free_security,
3511 .msg_queue_associate = smack_msg_queue_associate,
3512 .msg_queue_msgctl = smack_msg_queue_msgctl,
3513 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3514 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3515
3516 .shm_alloc_security = smack_shm_alloc_security,
3517 .shm_free_security = smack_shm_free_security,
3518 .shm_associate = smack_shm_associate,
3519 .shm_shmctl = smack_shm_shmctl,
3520 .shm_shmat = smack_shm_shmat,
3521
3522 .sem_alloc_security = smack_sem_alloc_security,
3523 .sem_free_security = smack_sem_free_security,
3524 .sem_associate = smack_sem_associate,
3525 .sem_semctl = smack_sem_semctl,
3526 .sem_semop = smack_sem_semop,
3527
Casey Schauflere114e472008-02-04 22:29:50 -08003528 .d_instantiate = smack_d_instantiate,
3529
3530 .getprocattr = smack_getprocattr,
3531 .setprocattr = smack_setprocattr,
3532
3533 .unix_stream_connect = smack_unix_stream_connect,
3534 .unix_may_send = smack_unix_may_send,
3535
3536 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003537 .socket_connect = smack_socket_connect,
3538 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003539 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3540 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3541 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3542 .sk_alloc_security = smack_sk_alloc_security,
3543 .sk_free_security = smack_sk_free_security,
3544 .sock_graft = smack_sock_graft,
3545 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003546 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003547
Casey Schauflere114e472008-02-04 22:29:50 -08003548 /* key management security hooks */
3549#ifdef CONFIG_KEYS
3550 .key_alloc = smack_key_alloc,
3551 .key_free = smack_key_free,
3552 .key_permission = smack_key_permission,
3553#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003554
3555 /* Audit hooks */
3556#ifdef CONFIG_AUDIT
3557 .audit_rule_init = smack_audit_rule_init,
3558 .audit_rule_known = smack_audit_rule_known,
3559 .audit_rule_match = smack_audit_rule_match,
3560 .audit_rule_free = smack_audit_rule_free,
3561#endif /* CONFIG_AUDIT */
3562
Casey Schauflere114e472008-02-04 22:29:50 -08003563 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003564 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003565 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003566 .inode_notifysecctx = smack_inode_notifysecctx,
3567 .inode_setsecctx = smack_inode_setsecctx,
3568 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003569};
3570
Etienne Basset7198e2e2009-03-24 20:53:24 +01003571
Casey Schaufler86812bb2012-04-17 18:55:46 -07003572static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003573{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003574 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003575 * Initialize rule list locks
3576 */
3577 mutex_init(&smack_known_huh.smk_rules_lock);
3578 mutex_init(&smack_known_hat.smk_rules_lock);
3579 mutex_init(&smack_known_floor.smk_rules_lock);
3580 mutex_init(&smack_known_star.smk_rules_lock);
3581 mutex_init(&smack_known_invalid.smk_rules_lock);
3582 mutex_init(&smack_known_web.smk_rules_lock);
3583 /*
3584 * Initialize rule lists
3585 */
3586 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3587 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3588 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3589 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3590 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3591 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3592 /*
3593 * Create the known labels list
3594 */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003595 list_add(&smack_known_huh.list, &smack_known_list);
3596 list_add(&smack_known_hat.list, &smack_known_list);
3597 list_add(&smack_known_star.list, &smack_known_list);
3598 list_add(&smack_known_floor.list, &smack_known_list);
3599 list_add(&smack_known_invalid.list, &smack_known_list);
3600 list_add(&smack_known_web.list, &smack_known_list);
3601}
3602
Casey Schauflere114e472008-02-04 22:29:50 -08003603/**
3604 * smack_init - initialize the smack system
3605 *
3606 * Returns 0
3607 */
3608static __init int smack_init(void)
3609{
David Howellsd84f4f92008-11-14 10:39:23 +11003610 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003611 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003612
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003613 if (!security_module_enable(&smack_ops))
3614 return 0;
3615
3616 tsp = new_task_smack(smack_known_floor.smk_known,
3617 smack_known_floor.smk_known, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003618 if (tsp == NULL)
3619 return -ENOMEM;
3620
Casey Schauflere114e472008-02-04 22:29:50 -08003621 printk(KERN_INFO "Smack: Initializing.\n");
3622
3623 /*
3624 * Set the security state for the initial task.
3625 */
David Howellsd84f4f92008-11-14 10:39:23 +11003626 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003627 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003628
Casey Schaufler86812bb2012-04-17 18:55:46 -07003629 /* initialize the smack_known_list */
3630 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003631
3632 /*
3633 * Register with LSM
3634 */
3635 if (register_security(&smack_ops))
3636 panic("smack: Unable to register with kernel.\n");
3637
3638 return 0;
3639}
3640
3641/*
3642 * Smack requires early initialization in order to label
3643 * all processes and objects when they are created.
3644 */
3645security_initcall(smack_init);