blob: 7db62b48eb4241d2ec15b18edc934faae67faa83 [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>
33#include <net/netlabel.h>
34#include <net/cipso_ipv4.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100035#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070036#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050037#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030038#include <linux/personality.h>
Casey Schauflere114e472008-02-04 22:29:50 -080039#include "smack.h"
40
David Howellsc69e8d92008-11-14 10:39:19 +110041#define task_security(task) (task_cred_xxx((task), security))
42
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020043#define TRANS_TRUE "TRUE"
44#define TRANS_TRUE_SIZE 4
45
Casey Schauflere114e472008-02-04 22:29:50 -080046/**
47 * smk_fetch - Fetch the smack label from a file.
48 * @ip: a pointer to the inode
49 * @dp: a pointer to the dentry
50 *
51 * Returns a pointer to the master list entry for the Smack label
52 * or NULL if there was no label to fetch.
53 */
Casey Schaufler676dac42010-12-02 06:43:39 -080054static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080055{
56 int rc;
57 char in[SMK_LABELLEN];
58
59 if (ip->i_op->getxattr == NULL)
60 return NULL;
61
Casey Schaufler676dac42010-12-02 06:43:39 -080062 rc = ip->i_op->getxattr(dp, name, in, SMK_LABELLEN);
Casey Schauflere114e472008-02-04 22:29:50 -080063 if (rc < 0)
64 return NULL;
65
66 return smk_import(in, rc);
67}
68
69/**
70 * new_inode_smack - allocate an inode security blob
71 * @smack: a pointer to the Smack label to use in the blob
72 *
73 * Returns the new blob or NULL if there's no memory available
74 */
75struct inode_smack *new_inode_smack(char *smack)
76{
77 struct inode_smack *isp;
78
79 isp = kzalloc(sizeof(struct inode_smack), GFP_KERNEL);
80 if (isp == NULL)
81 return NULL;
82
83 isp->smk_inode = smack;
84 isp->smk_flags = 0;
85 mutex_init(&isp->smk_lock);
86
87 return isp;
88}
89
Casey Schaufler7898e1f2011-01-17 08:05:27 -080090/**
91 * new_task_smack - allocate a task security blob
92 * @smack: a pointer to the Smack label to use in the blob
93 *
94 * Returns the new blob or NULL if there's no memory available
95 */
96static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
97{
98 struct task_smack *tsp;
99
100 tsp = kzalloc(sizeof(struct task_smack), gfp);
101 if (tsp == NULL)
102 return NULL;
103
104 tsp->smk_task = task;
105 tsp->smk_forked = forked;
106 INIT_LIST_HEAD(&tsp->smk_rules);
107 mutex_init(&tsp->smk_rules_lock);
108
109 return tsp;
110}
111
112/**
113 * smk_copy_rules - copy a rule set
114 * @nhead - new rules header pointer
115 * @ohead - old rules header pointer
116 *
117 * Returns 0 on success, -ENOMEM on error
118 */
119static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
120 gfp_t gfp)
121{
122 struct smack_rule *nrp;
123 struct smack_rule *orp;
124 int rc = 0;
125
126 INIT_LIST_HEAD(nhead);
127
128 list_for_each_entry_rcu(orp, ohead, list) {
129 nrp = kzalloc(sizeof(struct smack_rule), gfp);
130 if (nrp == NULL) {
131 rc = -ENOMEM;
132 break;
133 }
134 *nrp = *orp;
135 list_add_rcu(&nrp->list, nhead);
136 }
137 return rc;
138}
139
Casey Schauflere114e472008-02-04 22:29:50 -0800140/*
141 * LSM hooks.
142 * We he, that is fun!
143 */
144
145/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000146 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800147 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -0800148 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -0800149 *
150 * Returns 0 if access is OK, an error code otherwise
151 *
152 * Do the capability checks, and require read and write.
153 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000154static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800155{
156 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200157 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800158 char *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800159
Ingo Molnar9e488582009-05-07 19:26:19 +1000160 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800161 if (rc != 0)
162 return rc;
163
Casey Schaufler676dac42010-12-02 06:43:39 -0800164 tsp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200165 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
166 smk_ad_setfield_u_tsk(&ad, ctp);
167
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800168 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100169 return rc;
170}
Casey Schauflere114e472008-02-04 22:29:50 -0800171
David Howells5cd9c582008-08-14 11:37:28 +0100172/**
173 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
174 * @ptp: parent task pointer
175 *
176 * Returns 0 if access is OK, an error code otherwise
177 *
178 * Do the capability checks, and require read and write.
179 */
180static int smack_ptrace_traceme(struct task_struct *ptp)
181{
182 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200183 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800184 char *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100185
186 rc = cap_ptrace_traceme(ptp);
187 if (rc != 0)
188 return rc;
189
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800190 tsp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
192 smk_ad_setfield_u_tsk(&ad, ptp);
193
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800194 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800195 return rc;
196}
197
198/**
199 * smack_syslog - Smack approval on syslog
200 * @type: message type
201 *
202 * Require that the task has the floor label
203 *
204 * Returns 0 on success, error code otherwise.
205 */
Eric Paris12b30522010-11-15 18:36:29 -0500206static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800207{
Eric Paris12b30522010-11-15 18:36:29 -0500208 int rc = 0;
Casey Schaufler676dac42010-12-02 06:43:39 -0800209 char *sp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800210
Casey Schauflere114e472008-02-04 22:29:50 -0800211 if (capable(CAP_MAC_OVERRIDE))
212 return 0;
213
214 if (sp != smack_known_floor.smk_known)
215 rc = -EACCES;
216
217 return rc;
218}
219
220
221/*
222 * Superblock Hooks.
223 */
224
225/**
226 * smack_sb_alloc_security - allocate a superblock blob
227 * @sb: the superblock getting the blob
228 *
229 * Returns 0 on success or -ENOMEM on error.
230 */
231static int smack_sb_alloc_security(struct super_block *sb)
232{
233 struct superblock_smack *sbsp;
234
235 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
236
237 if (sbsp == NULL)
238 return -ENOMEM;
239
240 sbsp->smk_root = smack_known_floor.smk_known;
241 sbsp->smk_default = smack_known_floor.smk_known;
242 sbsp->smk_floor = smack_known_floor.smk_known;
243 sbsp->smk_hat = smack_known_hat.smk_known;
244 sbsp->smk_initialized = 0;
245 spin_lock_init(&sbsp->smk_sblock);
246
247 sb->s_security = sbsp;
248
249 return 0;
250}
251
252/**
253 * smack_sb_free_security - free a superblock blob
254 * @sb: the superblock getting the blob
255 *
256 */
257static void smack_sb_free_security(struct super_block *sb)
258{
259 kfree(sb->s_security);
260 sb->s_security = NULL;
261}
262
263/**
264 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800265 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800266 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800267 *
268 * Returns 0 on success or -ENOMEM on error.
269 *
270 * Copy the Smack specific mount options out of the mount
271 * options list.
272 */
Eric Parise0007522008-03-05 10:31:54 -0500273static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800274{
275 char *cp, *commap, *otheropts, *dp;
276
Casey Schauflere114e472008-02-04 22:29:50 -0800277 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
278 if (otheropts == NULL)
279 return -ENOMEM;
280
281 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
282 if (strstr(cp, SMK_FSDEFAULT) == cp)
283 dp = smackopts;
284 else if (strstr(cp, SMK_FSFLOOR) == cp)
285 dp = smackopts;
286 else if (strstr(cp, SMK_FSHAT) == cp)
287 dp = smackopts;
288 else if (strstr(cp, SMK_FSROOT) == cp)
289 dp = smackopts;
290 else
291 dp = otheropts;
292
293 commap = strchr(cp, ',');
294 if (commap != NULL)
295 *commap = '\0';
296
297 if (*dp != '\0')
298 strcat(dp, ",");
299 strcat(dp, cp);
300 }
301
302 strcpy(orig, otheropts);
303 free_page((unsigned long)otheropts);
304
305 return 0;
306}
307
308/**
309 * smack_sb_kern_mount - Smack specific mount processing
310 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100311 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800312 * @data: the smack mount options
313 *
314 * Returns 0 on success, an error code on failure
315 */
James Morris12204e22008-12-19 10:44:42 +1100316static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800317{
318 struct dentry *root = sb->s_root;
319 struct inode *inode = root->d_inode;
320 struct superblock_smack *sp = sb->s_security;
321 struct inode_smack *isp;
322 char *op;
323 char *commap;
324 char *nsp;
325
326 spin_lock(&sp->smk_sblock);
327 if (sp->smk_initialized != 0) {
328 spin_unlock(&sp->smk_sblock);
329 return 0;
330 }
331 sp->smk_initialized = 1;
332 spin_unlock(&sp->smk_sblock);
333
334 for (op = data; op != NULL; op = commap) {
335 commap = strchr(op, ',');
336 if (commap != NULL)
337 *commap++ = '\0';
338
339 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
340 op += strlen(SMK_FSHAT);
341 nsp = smk_import(op, 0);
342 if (nsp != NULL)
343 sp->smk_hat = nsp;
344 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
345 op += strlen(SMK_FSFLOOR);
346 nsp = smk_import(op, 0);
347 if (nsp != NULL)
348 sp->smk_floor = nsp;
349 } else if (strncmp(op, SMK_FSDEFAULT,
350 strlen(SMK_FSDEFAULT)) == 0) {
351 op += strlen(SMK_FSDEFAULT);
352 nsp = smk_import(op, 0);
353 if (nsp != NULL)
354 sp->smk_default = nsp;
355 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
356 op += strlen(SMK_FSROOT);
357 nsp = smk_import(op, 0);
358 if (nsp != NULL)
359 sp->smk_root = nsp;
360 }
361 }
362
363 /*
364 * Initialize the root inode.
365 */
366 isp = inode->i_security;
367 if (isp == NULL)
368 inode->i_security = new_inode_smack(sp->smk_root);
369 else
370 isp->smk_inode = sp->smk_root;
371
372 return 0;
373}
374
375/**
376 * smack_sb_statfs - Smack check on statfs
377 * @dentry: identifies the file system in question
378 *
379 * Returns 0 if current can read the floor of the filesystem,
380 * and error code otherwise
381 */
382static int smack_sb_statfs(struct dentry *dentry)
383{
384 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200385 int rc;
386 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800387
Eric Parisa2694342011-04-25 13:10:27 -0400388 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200389 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
390
391 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
392 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800393}
394
395/**
396 * smack_sb_mount - Smack check for mounting
397 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800398 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800399 * @type: unused
400 * @flags: unused
401 * @data: unused
402 *
403 * Returns 0 if current can write the floor of the filesystem
404 * being mounted on, an error code otherwise.
405 */
Al Virob5266eb2008-03-22 17:48:24 -0400406static int smack_sb_mount(char *dev_name, struct path *path,
Casey Schauflere114e472008-02-04 22:29:50 -0800407 char *type, unsigned long flags, void *data)
408{
Al Virob5266eb2008-03-22 17:48:24 -0400409 struct superblock_smack *sbp = path->mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200410 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800411
Eric Parisf48b7392011-04-25 12:54:27 -0400412 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200413 smk_ad_setfield_u_fs_path(&ad, *path);
414
415 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800416}
417
418/**
419 * smack_sb_umount - Smack check for unmounting
420 * @mnt: file system to unmount
421 * @flags: unused
422 *
423 * Returns 0 if current can write the floor of the filesystem
424 * being unmounted, an error code otherwise.
425 */
426static int smack_sb_umount(struct vfsmount *mnt, int flags)
427{
428 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200429 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400430 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200431
Eric Parisa2694342011-04-25 13:10:27 -0400432 path.dentry = mnt->mnt_root;
433 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200434
Eric Parisf48b7392011-04-25 12:54:27 -0400435 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400436 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflere114e472008-02-04 22:29:50 -0800437
438 sbp = mnt->mnt_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200439 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800440}
441
442/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800443 * BPRM hooks
444 */
445
Casey Schauflerce8a4322011-09-29 18:21:01 -0700446/**
447 * smack_bprm_set_creds - set creds for exec
448 * @bprm: the exec information
449 *
450 * Returns 0 if it gets a blob, -ENOMEM otherwise
451 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800452static int smack_bprm_set_creds(struct linux_binprm *bprm)
453{
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300454 struct inode *inode = bprm->file->f_path.dentry->d_inode;
455 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800456 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800457 int rc;
458
459 rc = cap_bprm_set_creds(bprm);
460 if (rc != 0)
461 return rc;
462
463 if (bprm->cred_prepared)
464 return 0;
465
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300466 isp = inode->i_security;
467 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800468 return 0;
469
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300470 if (bprm->unsafe)
471 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800472
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300473 bsp->smk_task = isp->smk_task;
474 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800475
476 return 0;
477}
478
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300479/**
480 * smack_bprm_committing_creds - Prepare to install the new credentials
481 * from bprm.
482 *
483 * @bprm: binprm for exec
484 */
485static void smack_bprm_committing_creds(struct linux_binprm *bprm)
486{
487 struct task_smack *bsp = bprm->cred->security;
488
489 if (bsp->smk_task != bsp->smk_forked)
490 current->pdeath_signal = 0;
491}
492
493/**
494 * smack_bprm_secureexec - Return the decision to use secureexec.
495 * @bprm: binprm for exec
496 *
497 * Returns 0 on success.
498 */
499static int smack_bprm_secureexec(struct linux_binprm *bprm)
500{
501 struct task_smack *tsp = current_security();
502 int ret = cap_bprm_secureexec(bprm);
503
504 if (!ret && (tsp->smk_task != tsp->smk_forked))
505 ret = 1;
506
507 return ret;
508}
509
Casey Schaufler676dac42010-12-02 06:43:39 -0800510/*
Casey Schauflere114e472008-02-04 22:29:50 -0800511 * Inode hooks
512 */
513
514/**
515 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800516 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800517 *
518 * Returns 0 if it gets a blob, -ENOMEM otherwise
519 */
520static int smack_inode_alloc_security(struct inode *inode)
521{
Casey Schaufler676dac42010-12-02 06:43:39 -0800522 inode->i_security = new_inode_smack(smk_of_current());
Casey Schauflere114e472008-02-04 22:29:50 -0800523 if (inode->i_security == NULL)
524 return -ENOMEM;
525 return 0;
526}
527
528/**
529 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800530 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800531 *
532 * Clears the blob pointer in inode
533 */
534static void smack_inode_free_security(struct inode *inode)
535{
536 kfree(inode->i_security);
537 inode->i_security = NULL;
538}
539
540/**
541 * smack_inode_init_security - copy out the smack from an inode
542 * @inode: the inode
543 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500544 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800545 * @name: where to put the attribute name
546 * @value: where to put the attribute value
547 * @len: where to put the length of the attribute
548 *
549 * Returns 0 if it all works out, -ENOMEM if there's no memory
550 */
551static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -0500552 const struct qstr *qstr, char **name,
553 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800554{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700555 struct smack_known *skp;
556 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800557 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200558 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800559 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800560
561 if (name) {
562 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_KERNEL);
563 if (*name == NULL)
564 return -ENOMEM;
565 }
566
567 if (value) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700568 skp = smk_find_entry(csp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800569 rcu_read_lock();
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700570 may = smk_access_entry(csp, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800571 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200572
573 /*
574 * If the access rule allows transmutation and
575 * the directory requests transmutation then
576 * by all means transmute.
577 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800578 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
579 smk_inode_transmutable(dir))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200580 isp = dsp;
581
Casey Schauflere114e472008-02-04 22:29:50 -0800582 *value = kstrdup(isp, GFP_KERNEL);
583 if (*value == NULL)
584 return -ENOMEM;
585 }
586
587 if (len)
588 *len = strlen(isp) + 1;
589
590 return 0;
591}
592
593/**
594 * smack_inode_link - Smack check on link
595 * @old_dentry: the existing object
596 * @dir: unused
597 * @new_dentry: the new object
598 *
599 * Returns 0 if access is permitted, an error code otherwise
600 */
601static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
602 struct dentry *new_dentry)
603{
Casey Schauflere114e472008-02-04 22:29:50 -0800604 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200605 struct smk_audit_info ad;
606 int rc;
607
Eric Parisa2694342011-04-25 13:10:27 -0400608 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200609 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800610
611 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200612 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800613
614 if (rc == 0 && new_dentry->d_inode != NULL) {
615 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200616 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
617 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800618 }
619
620 return rc;
621}
622
623/**
624 * smack_inode_unlink - Smack check on inode deletion
625 * @dir: containing directory object
626 * @dentry: file to unlink
627 *
628 * Returns 0 if current can write the containing directory
629 * and the object, error code otherwise
630 */
631static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
632{
633 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200634 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800635 int rc;
636
Eric Parisa2694342011-04-25 13:10:27 -0400637 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200638 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
639
Casey Schauflere114e472008-02-04 22:29:50 -0800640 /*
641 * You need write access to the thing you're unlinking
642 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200643 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
644 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800645 /*
646 * You also need write access to the containing directory
647 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200648 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
649 smk_ad_setfield_u_fs_inode(&ad, dir);
650 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
651 }
Casey Schauflere114e472008-02-04 22:29:50 -0800652 return rc;
653}
654
655/**
656 * smack_inode_rmdir - Smack check on directory deletion
657 * @dir: containing directory object
658 * @dentry: directory to unlink
659 *
660 * Returns 0 if current can write the containing directory
661 * and the directory, error code otherwise
662 */
663static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
664{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200665 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800666 int rc;
667
Eric Parisa2694342011-04-25 13:10:27 -0400668 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200669 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
670
Casey Schauflere114e472008-02-04 22:29:50 -0800671 /*
672 * You need write access to the thing you're removing
673 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200674 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
675 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800676 /*
677 * You also need write access to the containing directory
678 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200679 smk_ad_setfield_u_fs_path_dentry(&ad, NULL);
680 smk_ad_setfield_u_fs_inode(&ad, dir);
681 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
682 }
Casey Schauflere114e472008-02-04 22:29:50 -0800683
684 return rc;
685}
686
687/**
688 * smack_inode_rename - Smack check on rename
689 * @old_inode: the old directory
690 * @old_dentry: unused
691 * @new_inode: the new directory
692 * @new_dentry: unused
693 *
694 * Read and write access is required on both the old and
695 * new directories.
696 *
697 * Returns 0 if access is permitted, an error code otherwise
698 */
699static int smack_inode_rename(struct inode *old_inode,
700 struct dentry *old_dentry,
701 struct inode *new_inode,
702 struct dentry *new_dentry)
703{
704 int rc;
705 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200706 struct smk_audit_info ad;
707
Eric Parisa2694342011-04-25 13:10:27 -0400708 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200709 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800710
711 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200712 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800713
714 if (rc == 0 && new_dentry->d_inode != NULL) {
715 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200716 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
717 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800718 }
Casey Schauflere114e472008-02-04 22:29:50 -0800719 return rc;
720}
721
722/**
723 * smack_inode_permission - Smack version of permission()
724 * @inode: the inode in question
725 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800726 *
727 * This is the important Smack hook.
728 *
729 * Returns 0 if access is permitted, -EACCES otherwise
730 */
Al Viroe74f71e2011-06-20 19:38:15 -0400731static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800732{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200733 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400734 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400735
736 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800737 /*
738 * No permission to check. Existence test. Yup, it's there.
739 */
740 if (mask == 0)
741 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700742
743 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400744 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700745 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400746 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200747 smk_ad_setfield_u_fs_inode(&ad, inode);
748 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800749}
750
751/**
752 * smack_inode_setattr - Smack check for setting attributes
753 * @dentry: the object
754 * @iattr: for the force flag
755 *
756 * Returns 0 if access is permitted, an error code otherwise
757 */
758static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
759{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200760 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800761 /*
762 * Need to allow for clearing the setuid bit.
763 */
764 if (iattr->ia_valid & ATTR_FORCE)
765 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400766 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200767 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800768
Etienne Bassetecfcc532009-04-08 20:40:06 +0200769 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800770}
771
772/**
773 * smack_inode_getattr - Smack check for getting attributes
774 * @mnt: unused
775 * @dentry: the object
776 *
777 * Returns 0 if access is permitted, an error code otherwise
778 */
779static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
780{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200781 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400782 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200783
Eric Parisa2694342011-04-25 13:10:27 -0400784 path.dentry = dentry;
785 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200786
Eric Parisf48b7392011-04-25 12:54:27 -0400787 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400788 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200789 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800790}
791
792/**
793 * smack_inode_setxattr - Smack check for setting xattrs
794 * @dentry: the object
795 * @name: name of the attribute
796 * @value: unused
797 * @size: unused
798 * @flags: unused
799 *
800 * This protects the Smack attribute explicitly.
801 *
802 * Returns 0 if access is permitted, an error code otherwise
803 */
David Howells8f0cfa52008-04-29 00:59:41 -0700804static int smack_inode_setxattr(struct dentry *dentry, const char *name,
805 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800806{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200807 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800808 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800809
Casey Schauflerbcdca222008-02-23 15:24:04 -0800810 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
811 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800812 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800813 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
814 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800815 if (!capable(CAP_MAC_ADMIN))
816 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200817 /*
818 * check label validity here so import wont fail on
819 * post_setxattr
820 */
821 if (size == 0 || size >= SMK_LABELLEN ||
822 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400823 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200824 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
825 if (!capable(CAP_MAC_ADMIN))
826 rc = -EPERM;
827 if (size != TRANS_TRUE_SIZE ||
828 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
829 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800830 } else
831 rc = cap_inode_setxattr(dentry, name, value, size, flags);
832
Eric Parisa2694342011-04-25 13:10:27 -0400833 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200834 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
835
Casey Schauflerbcdca222008-02-23 15:24:04 -0800836 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200837 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800838
839 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800840}
841
842/**
843 * smack_inode_post_setxattr - Apply the Smack update approved above
844 * @dentry: object
845 * @name: attribute name
846 * @value: attribute value
847 * @size: attribute size
848 * @flags: unused
849 *
850 * Set the pointer in the inode blob to the entry found
851 * in the master label list.
852 */
David Howells8f0cfa52008-04-29 00:59:41 -0700853static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
854 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800855{
Casey Schauflere114e472008-02-04 22:29:50 -0800856 char *nsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200857 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800858
859 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200860 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800861 if (nsp != NULL)
862 isp->smk_inode = nsp;
863 else
864 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200865 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
866 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800867 if (nsp != NULL)
868 isp->smk_task = nsp;
869 else
870 isp->smk_task = smack_known_invalid.smk_known;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800871 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
872 nsp = smk_import(value, size);
873 if (nsp != NULL)
874 isp->smk_mmap = nsp;
875 else
876 isp->smk_mmap = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200877 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
878 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflere114e472008-02-04 22:29:50 -0800879
880 return;
881}
882
Casey Schauflerce8a4322011-09-29 18:21:01 -0700883/**
Casey Schauflere114e472008-02-04 22:29:50 -0800884 * smack_inode_getxattr - Smack check on getxattr
885 * @dentry: the object
886 * @name: unused
887 *
888 * Returns 0 if access is permitted, an error code otherwise
889 */
David Howells8f0cfa52008-04-29 00:59:41 -0700890static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800891{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200892 struct smk_audit_info ad;
893
Eric Parisa2694342011-04-25 13:10:27 -0400894 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200895 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
896
897 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800898}
899
Casey Schauflerce8a4322011-09-29 18:21:01 -0700900/**
Casey Schauflere114e472008-02-04 22:29:50 -0800901 * smack_inode_removexattr - Smack check on removexattr
902 * @dentry: the object
903 * @name: name of the attribute
904 *
905 * Removing the Smack attribute requires CAP_MAC_ADMIN
906 *
907 * Returns 0 if access is permitted, an error code otherwise
908 */
David Howells8f0cfa52008-04-29 00:59:41 -0700909static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800910{
Casey Schaufler676dac42010-12-02 06:43:39 -0800911 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200912 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800913 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800914
Casey Schauflerbcdca222008-02-23 15:24:04 -0800915 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
916 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800917 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200918 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800919 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
920 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schauflerbcdca222008-02-23 15:24:04 -0800921 if (!capable(CAP_MAC_ADMIN))
922 rc = -EPERM;
923 } else
924 rc = cap_inode_removexattr(dentry, name);
925
Eric Parisa2694342011-04-25 13:10:27 -0400926 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200927 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800928 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200929 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800930
Casey Schaufler676dac42010-12-02 06:43:39 -0800931 if (rc == 0) {
932 isp = dentry->d_inode->i_security;
933 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800934 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800935 }
936
Casey Schauflerbcdca222008-02-23 15:24:04 -0800937 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800938}
939
940/**
941 * smack_inode_getsecurity - get smack xattrs
942 * @inode: the object
943 * @name: attribute name
944 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800945 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800946 *
947 * Returns the size of the attribute or an error code
948 */
949static int smack_inode_getsecurity(const struct inode *inode,
950 const char *name, void **buffer,
951 bool alloc)
952{
953 struct socket_smack *ssp;
954 struct socket *sock;
955 struct super_block *sbp;
956 struct inode *ip = (struct inode *)inode;
957 char *isp;
958 int ilen;
959 int rc = 0;
960
961 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
962 isp = smk_of_inode(inode);
963 ilen = strlen(isp) + 1;
964 *buffer = isp;
965 return ilen;
966 }
967
968 /*
969 * The rest of the Smack xattrs are only on sockets.
970 */
971 sbp = ip->i_sb;
972 if (sbp->s_magic != SOCKFS_MAGIC)
973 return -EOPNOTSUPP;
974
975 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800976 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800977 return -EOPNOTSUPP;
978
979 ssp = sock->sk->sk_security;
980
981 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
982 isp = ssp->smk_in;
983 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
984 isp = ssp->smk_out;
985 else
986 return -EOPNOTSUPP;
987
988 ilen = strlen(isp) + 1;
989 if (rc == 0) {
990 *buffer = isp;
991 rc = ilen;
992 }
993
994 return rc;
995}
996
997
998/**
999 * smack_inode_listsecurity - list the Smack attributes
1000 * @inode: the object
1001 * @buffer: where they go
1002 * @buffer_size: size of buffer
1003 *
1004 * Returns 0 on success, -EINVAL otherwise
1005 */
1006static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1007 size_t buffer_size)
1008{
1009 int len = strlen(XATTR_NAME_SMACK);
1010
1011 if (buffer != NULL && len <= buffer_size) {
1012 memcpy(buffer, XATTR_NAME_SMACK, len);
1013 return len;
1014 }
1015 return -EINVAL;
1016}
1017
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001018/**
1019 * smack_inode_getsecid - Extract inode's security id
1020 * @inode: inode to extract the info from
1021 * @secid: where result will be saved
1022 */
1023static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1024{
1025 struct inode_smack *isp = inode->i_security;
1026
1027 *secid = smack_to_secid(isp->smk_inode);
1028}
1029
Casey Schauflere114e472008-02-04 22:29:50 -08001030/*
1031 * File Hooks
1032 */
1033
1034/**
1035 * smack_file_permission - Smack check on file operations
1036 * @file: unused
1037 * @mask: unused
1038 *
1039 * Returns 0
1040 *
1041 * Should access checks be done on each read or write?
1042 * UNICOS and SELinux say yes.
1043 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1044 *
1045 * I'll say no for now. Smack does not do the frequent
1046 * label changing that SELinux does.
1047 */
1048static int smack_file_permission(struct file *file, int mask)
1049{
1050 return 0;
1051}
1052
1053/**
1054 * smack_file_alloc_security - assign a file security blob
1055 * @file: the object
1056 *
1057 * The security blob for a file is a pointer to the master
1058 * label list, so no allocation is done.
1059 *
1060 * Returns 0
1061 */
1062static int smack_file_alloc_security(struct file *file)
1063{
Casey Schaufler676dac42010-12-02 06:43:39 -08001064 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001065 return 0;
1066}
1067
1068/**
1069 * smack_file_free_security - clear a file security blob
1070 * @file: the object
1071 *
1072 * The security blob for a file is a pointer to the master
1073 * label list, so no memory is freed.
1074 */
1075static void smack_file_free_security(struct file *file)
1076{
1077 file->f_security = NULL;
1078}
1079
1080/**
1081 * smack_file_ioctl - Smack check on ioctls
1082 * @file: the object
1083 * @cmd: what to do
1084 * @arg: unused
1085 *
1086 * Relies heavily on the correct use of the ioctl command conventions.
1087 *
1088 * Returns 0 if allowed, error code otherwise
1089 */
1090static int smack_file_ioctl(struct file *file, unsigned int cmd,
1091 unsigned long arg)
1092{
1093 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001094 struct smk_audit_info ad;
1095
Eric Parisf48b7392011-04-25 12:54:27 -04001096 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001097 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001098
1099 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001100 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001101
1102 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001104
1105 return rc;
1106}
1107
1108/**
1109 * smack_file_lock - Smack check on file locking
1110 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001111 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001112 *
1113 * Returns 0 if current has write access, error code otherwise
1114 */
1115static int smack_file_lock(struct file *file, unsigned int cmd)
1116{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001117 struct smk_audit_info ad;
1118
Eric Paris92f42502011-04-25 13:15:55 -04001119 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1120 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001121 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001122}
1123
1124/**
1125 * smack_file_fcntl - Smack check on fcntl
1126 * @file: the object
1127 * @cmd: what action to check
1128 * @arg: unused
1129 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001130 * Generally these operations are harmless.
1131 * File locking operations present an obvious mechanism
1132 * for passing information, so they require write access.
1133 *
Casey Schauflere114e472008-02-04 22:29:50 -08001134 * Returns 0 if current has access, error code otherwise
1135 */
1136static int smack_file_fcntl(struct file *file, unsigned int cmd,
1137 unsigned long arg)
1138{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001139 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001140 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001141
Etienne Bassetecfcc532009-04-08 20:40:06 +02001142
Casey Schauflere114e472008-02-04 22:29:50 -08001143 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001144 case F_GETLK:
Casey Schauflere114e472008-02-04 22:29:50 -08001145 case F_SETLK:
1146 case F_SETLKW:
1147 case F_SETOWN:
1148 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001149 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1150 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001151 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001152 break;
1153 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001154 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001155 }
1156
1157 return rc;
1158}
1159
1160/**
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001161 * smack_file_mmap :
1162 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1163 * if mapping anonymous memory.
1164 * @file contains the file structure for file to map (may be NULL).
1165 * @reqprot contains the protection requested by the application.
1166 * @prot contains the protection that will be applied by the kernel.
1167 * @flags contains the operational flags.
1168 * Return 0 if permission is granted.
1169 */
1170static int smack_file_mmap(struct file *file,
1171 unsigned long reqprot, unsigned long prot,
1172 unsigned long flags, unsigned long addr,
1173 unsigned long addr_only)
1174{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001175 struct smack_known *skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001176 struct smack_rule *srp;
1177 struct task_smack *tsp;
1178 char *sp;
1179 char *msmack;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001180 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001181 struct inode_smack *isp;
1182 struct dentry *dp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001183 int may;
1184 int mmay;
1185 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001186 int rc;
1187
1188 /* do DAC check on address space usage */
1189 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
1190 if (rc || addr_only)
1191 return rc;
1192
1193 if (file == NULL || file->f_dentry == NULL)
1194 return 0;
1195
1196 dp = file->f_dentry;
1197
1198 if (dp->d_inode == NULL)
1199 return 0;
1200
1201 isp = dp->d_inode->i_security;
1202 if (isp->smk_mmap == NULL)
1203 return 0;
1204 msmack = isp->smk_mmap;
1205
1206 tsp = current_security();
1207 sp = smk_of_current();
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001208 skp = smk_find_entry(sp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001209 rc = 0;
1210
1211 rcu_read_lock();
1212 /*
1213 * For each Smack rule associated with the subject
1214 * label verify that the SMACK64MMAP also has access
1215 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001216 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001217 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001218 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001219 /*
1220 * Matching labels always allows access.
1221 */
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001222 if (msmack == osmack)
1223 continue;
1224 /*
1225 * If there is a matching local rule take
1226 * that into account as well.
1227 */
1228 may = smk_access_entry(srp->smk_subject, osmack,
1229 &tsp->smk_rules);
1230 if (may == -ENOENT)
1231 may = srp->smk_access;
1232 else
1233 may &= srp->smk_access;
1234 /*
1235 * If may is zero the SMACK64MMAP subject can't
1236 * possibly have less access.
1237 */
1238 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001239 continue;
1240
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001241 /*
1242 * Fetch the global list entry.
1243 * If there isn't one a SMACK64MMAP subject
1244 * can't have as much access as current.
1245 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001246 skp = smk_find_entry(msmack);
1247 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001248 if (mmay == -ENOENT) {
1249 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001250 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001251 }
1252 /*
1253 * If there is a local entry it modifies the
1254 * potential access, too.
1255 */
1256 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1257 if (tmay != -ENOENT)
1258 mmay &= tmay;
1259
1260 /*
1261 * If there is any access available to current that is
1262 * not available to a SMACK64MMAP subject
1263 * deny access.
1264 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001265 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001266 rc = -EACCES;
1267 break;
1268 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001269 }
1270
1271 rcu_read_unlock();
1272
1273 return rc;
1274}
1275
1276/**
Casey Schauflere114e472008-02-04 22:29:50 -08001277 * smack_file_set_fowner - set the file security blob value
1278 * @file: object in question
1279 *
1280 * Returns 0
1281 * Further research may be required on this one.
1282 */
1283static int smack_file_set_fowner(struct file *file)
1284{
Casey Schaufler676dac42010-12-02 06:43:39 -08001285 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001286 return 0;
1287}
1288
1289/**
1290 * smack_file_send_sigiotask - Smack on sigio
1291 * @tsk: The target task
1292 * @fown: the object the signal come from
1293 * @signum: unused
1294 *
1295 * Allow a privileged task to get signals even if it shouldn't
1296 *
1297 * Returns 0 if a subject with the object's smack could
1298 * write to the task, an error code otherwise.
1299 */
1300static int smack_file_send_sigiotask(struct task_struct *tsk,
1301 struct fown_struct *fown, int signum)
1302{
1303 struct file *file;
1304 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001305 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001306 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001307
1308 /*
1309 * struct fown_struct is never outside the context of a struct file
1310 */
1311 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001312
Etienne Bassetecfcc532009-04-08 20:40:06 +02001313 /* we don't log here as rc can be overriden */
1314 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001315 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001316 rc = 0;
1317
1318 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1319 smk_ad_setfield_u_tsk(&ad, tsk);
1320 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001321 return rc;
1322}
1323
1324/**
1325 * smack_file_receive - Smack file receive check
1326 * @file: the object
1327 *
1328 * Returns 0 if current has access, error code otherwise
1329 */
1330static int smack_file_receive(struct file *file)
1331{
1332 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001333 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001334
Etienne Bassetecfcc532009-04-08 20:40:06 +02001335 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1336 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001337 /*
1338 * This code relies on bitmasks.
1339 */
1340 if (file->f_mode & FMODE_READ)
1341 may = MAY_READ;
1342 if (file->f_mode & FMODE_WRITE)
1343 may |= MAY_WRITE;
1344
Etienne Bassetecfcc532009-04-08 20:40:06 +02001345 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001346}
1347
Casey Schaufler531f1d42011-09-19 12:41:42 -07001348/**
1349 * smack_dentry_open - Smack dentry open processing
1350 * @file: the object
1351 * @cred: unused
1352 *
1353 * Set the security blob in the file structure.
1354 *
1355 * Returns 0
1356 */
1357static int smack_dentry_open(struct file *file, const struct cred *cred)
1358{
1359 struct inode_smack *isp = file->f_path.dentry->d_inode->i_security;
1360
1361 file->f_security = isp->smk_inode;
1362
1363 return 0;
1364}
1365
Casey Schauflere114e472008-02-04 22:29:50 -08001366/*
1367 * Task hooks
1368 */
1369
1370/**
David Howellsee18d642009-09-02 09:14:21 +01001371 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1372 * @new: the new credentials
1373 * @gfp: the atomicity of any memory allocations
1374 *
1375 * Prepare a blank set of credentials for modification. This must allocate all
1376 * the memory the LSM module might require such that cred_transfer() can
1377 * complete without error.
1378 */
1379static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1380{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001381 struct task_smack *tsp;
1382
1383 tsp = new_task_smack(NULL, NULL, gfp);
1384 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001385 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001386
1387 cred->security = tsp;
1388
David Howellsee18d642009-09-02 09:14:21 +01001389 return 0;
1390}
1391
1392
1393/**
David Howellsf1752ee2008-11-14 10:39:17 +11001394 * smack_cred_free - "free" task-level security credentials
1395 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001396 *
Casey Schauflere114e472008-02-04 22:29:50 -08001397 */
David Howellsf1752ee2008-11-14 10:39:17 +11001398static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001399{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001400 struct task_smack *tsp = cred->security;
1401 struct smack_rule *rp;
1402 struct list_head *l;
1403 struct list_head *n;
1404
1405 if (tsp == NULL)
1406 return;
1407 cred->security = NULL;
1408
1409 list_for_each_safe(l, n, &tsp->smk_rules) {
1410 rp = list_entry(l, struct smack_rule, list);
1411 list_del(&rp->list);
1412 kfree(rp);
1413 }
1414 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001415}
1416
1417/**
David Howellsd84f4f92008-11-14 10:39:23 +11001418 * smack_cred_prepare - prepare new set of credentials for modification
1419 * @new: the new credentials
1420 * @old: the original credentials
1421 * @gfp: the atomicity of any memory allocations
1422 *
1423 * Prepare a new set of credentials for modification.
1424 */
1425static int smack_cred_prepare(struct cred *new, const struct cred *old,
1426 gfp_t gfp)
1427{
Casey Schaufler676dac42010-12-02 06:43:39 -08001428 struct task_smack *old_tsp = old->security;
1429 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001430 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001431
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001432 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001433 if (new_tsp == NULL)
1434 return -ENOMEM;
1435
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001436 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1437 if (rc != 0)
1438 return rc;
1439
Casey Schaufler676dac42010-12-02 06:43:39 -08001440 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001441 return 0;
1442}
1443
Randy Dunlap251a2a92009-02-18 11:42:33 -08001444/**
David Howellsee18d642009-09-02 09:14:21 +01001445 * smack_cred_transfer - Transfer the old credentials to the new credentials
1446 * @new: the new credentials
1447 * @old: the original credentials
1448 *
1449 * Fill in a set of blank credentials from another set of credentials.
1450 */
1451static void smack_cred_transfer(struct cred *new, const struct cred *old)
1452{
Casey Schaufler676dac42010-12-02 06:43:39 -08001453 struct task_smack *old_tsp = old->security;
1454 struct task_smack *new_tsp = new->security;
1455
1456 new_tsp->smk_task = old_tsp->smk_task;
1457 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001458 mutex_init(&new_tsp->smk_rules_lock);
1459 INIT_LIST_HEAD(&new_tsp->smk_rules);
1460
1461
1462 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001463}
1464
1465/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001466 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001467 * @new: points to the set of credentials to be modified.
1468 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001469 *
1470 * Set the security data for a kernel service.
1471 */
1472static int smack_kernel_act_as(struct cred *new, u32 secid)
1473{
Casey Schaufler676dac42010-12-02 06:43:39 -08001474 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001475 char *smack = smack_from_secid(secid);
1476
1477 if (smack == NULL)
1478 return -EINVAL;
1479
Casey Schaufler676dac42010-12-02 06:43:39 -08001480 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001481 return 0;
1482}
1483
1484/**
1485 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001486 * @new: points to the set of credentials to be modified
1487 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001488 *
1489 * Set the file creation context in a set of credentials to the same
1490 * as the objective context of the specified inode
1491 */
1492static int smack_kernel_create_files_as(struct cred *new,
1493 struct inode *inode)
1494{
1495 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001496 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001497
Casey Schaufler676dac42010-12-02 06:43:39 -08001498 tsp->smk_forked = isp->smk_inode;
1499 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001500 return 0;
1501}
1502
1503/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001504 * smk_curacc_on_task - helper to log task related access
1505 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001506 * @access: the access requested
1507 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001508 *
1509 * Return 0 if access is permitted
1510 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001511static int smk_curacc_on_task(struct task_struct *p, int access,
1512 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001513{
1514 struct smk_audit_info ad;
1515
Casey Schaufler531f1d42011-09-19 12:41:42 -07001516 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001517 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001518 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001519}
1520
1521/**
Casey Schauflere114e472008-02-04 22:29:50 -08001522 * smack_task_setpgid - Smack check on setting pgid
1523 * @p: the task object
1524 * @pgid: unused
1525 *
1526 * Return 0 if write access is permitted
1527 */
1528static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1529{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001530 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001531}
1532
1533/**
1534 * smack_task_getpgid - Smack access check for getpgid
1535 * @p: the object task
1536 *
1537 * Returns 0 if current can read the object task, error code otherwise
1538 */
1539static int smack_task_getpgid(struct task_struct *p)
1540{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001541 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001542}
1543
1544/**
1545 * smack_task_getsid - Smack access check for getsid
1546 * @p: the object task
1547 *
1548 * Returns 0 if current can read the object task, error code otherwise
1549 */
1550static int smack_task_getsid(struct task_struct *p)
1551{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001552 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001553}
1554
1555/**
1556 * smack_task_getsecid - get the secid of the task
1557 * @p: the object task
1558 * @secid: where to put the result
1559 *
1560 * Sets the secid to contain a u32 version of the smack label.
1561 */
1562static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1563{
Casey Schaufler676dac42010-12-02 06:43:39 -08001564 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001565}
1566
1567/**
1568 * smack_task_setnice - Smack check on setting nice
1569 * @p: the task object
1570 * @nice: unused
1571 *
1572 * Return 0 if write access is permitted
1573 */
1574static int smack_task_setnice(struct task_struct *p, int nice)
1575{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001576 int rc;
1577
1578 rc = cap_task_setnice(p, nice);
1579 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001580 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001581 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001582}
1583
1584/**
1585 * smack_task_setioprio - Smack check on setting ioprio
1586 * @p: the task object
1587 * @ioprio: unused
1588 *
1589 * Return 0 if write access is permitted
1590 */
1591static int smack_task_setioprio(struct task_struct *p, int ioprio)
1592{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001593 int rc;
1594
1595 rc = cap_task_setioprio(p, ioprio);
1596 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001597 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001598 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001599}
1600
1601/**
1602 * smack_task_getioprio - Smack check on reading ioprio
1603 * @p: the task object
1604 *
1605 * Return 0 if read access is permitted
1606 */
1607static int smack_task_getioprio(struct task_struct *p)
1608{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001609 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001610}
1611
1612/**
1613 * smack_task_setscheduler - Smack check on setting scheduler
1614 * @p: the task object
1615 * @policy: unused
1616 * @lp: unused
1617 *
1618 * Return 0 if read access is permitted
1619 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001620static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001621{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001622 int rc;
1623
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001624 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001625 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001626 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001627 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001628}
1629
1630/**
1631 * smack_task_getscheduler - Smack check on reading scheduler
1632 * @p: the task object
1633 *
1634 * Return 0 if read access is permitted
1635 */
1636static int smack_task_getscheduler(struct task_struct *p)
1637{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001638 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001639}
1640
1641/**
1642 * smack_task_movememory - Smack check on moving memory
1643 * @p: the task object
1644 *
1645 * Return 0 if write access is permitted
1646 */
1647static int smack_task_movememory(struct task_struct *p)
1648{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001649 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001650}
1651
1652/**
1653 * smack_task_kill - Smack check on signal delivery
1654 * @p: the task object
1655 * @info: unused
1656 * @sig: unused
1657 * @secid: identifies the smack to use in lieu of current's
1658 *
1659 * Return 0 if write access is permitted
1660 *
1661 * The secid behavior is an artifact of an SELinux hack
1662 * in the USB code. Someday it may go away.
1663 */
1664static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1665 int sig, u32 secid)
1666{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001667 struct smk_audit_info ad;
1668
1669 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1670 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001671 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001672 * Sending a signal requires that the sender
1673 * can write the receiver.
1674 */
1675 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001676 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1677 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001678 /*
1679 * If the secid isn't 0 we're dealing with some USB IO
1680 * specific behavior. This is not clean. For one thing
1681 * we can't take privilege into account.
1682 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001683 return smk_access(smack_from_secid(secid),
1684 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001685}
1686
1687/**
1688 * smack_task_wait - Smack access check for waiting
1689 * @p: task to wait for
1690 *
1691 * Returns 0 if current can wait for p, error code otherwise
1692 */
1693static int smack_task_wait(struct task_struct *p)
1694{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001695 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08001696 char *sp = smk_of_current();
1697 char *tsp = smk_of_forked(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001698 int rc;
1699
Etienne Bassetecfcc532009-04-08 20:40:06 +02001700 /* we don't log here, we can be overriden */
Casey Schaufler676dac42010-12-02 06:43:39 -08001701 rc = smk_access(tsp, sp, MAY_WRITE, NULL);
Casey Schauflere114e472008-02-04 22:29:50 -08001702 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001703 goto out_log;
Casey Schauflere114e472008-02-04 22:29:50 -08001704
1705 /*
1706 * Allow the operation to succeed if either task
1707 * has privilege to perform operations that might
1708 * account for the smack labels having gotten to
1709 * be different in the first place.
1710 *
David Howells5cd9c582008-08-14 11:37:28 +01001711 * This breaks the strict subject/object access
Casey Schauflere114e472008-02-04 22:29:50 -08001712 * control ideal, taking the object's privilege
1713 * state into account in the decision as well as
1714 * the smack value.
1715 */
David Howells5cd9c582008-08-14 11:37:28 +01001716 if (capable(CAP_MAC_OVERRIDE) || has_capability(p, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001717 rc = 0;
1718 /* we log only if we didn't get overriden */
1719 out_log:
1720 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1721 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001722 smack_log(tsp, sp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001723 return rc;
1724}
1725
1726/**
1727 * smack_task_to_inode - copy task smack into the inode blob
1728 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001729 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001730 *
1731 * Sets the smack pointer in the inode security blob
1732 */
1733static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1734{
1735 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001736 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001737}
1738
1739/*
1740 * Socket hooks.
1741 */
1742
1743/**
1744 * smack_sk_alloc_security - Allocate a socket blob
1745 * @sk: the socket
1746 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001747 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001748 *
1749 * Assign Smack pointers to current
1750 *
1751 * Returns 0 on success, -ENOMEM is there's no memory
1752 */
1753static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1754{
Casey Schaufler676dac42010-12-02 06:43:39 -08001755 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001756 struct socket_smack *ssp;
1757
1758 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1759 if (ssp == NULL)
1760 return -ENOMEM;
1761
1762 ssp->smk_in = csp;
1763 ssp->smk_out = csp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001764 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001765
1766 sk->sk_security = ssp;
1767
1768 return 0;
1769}
1770
1771/**
1772 * smack_sk_free_security - Free a socket blob
1773 * @sk: the socket
1774 *
1775 * Clears the blob pointer
1776 */
1777static void smack_sk_free_security(struct sock *sk)
1778{
1779 kfree(sk->sk_security);
1780}
1781
1782/**
Paul Moore07feee82009-03-27 17:10:54 -04001783* smack_host_label - check host based restrictions
1784* @sip: the object end
1785*
1786* looks for host based access restrictions
1787*
1788* This version will only be appropriate for really small sets of single label
1789* hosts. The caller is responsible for ensuring that the RCU read lock is
1790* taken before calling this function.
1791*
1792* Returns the label of the far end or NULL if it's not special.
1793*/
1794static char *smack_host_label(struct sockaddr_in *sip)
1795{
1796 struct smk_netlbladdr *snp;
1797 struct in_addr *siap = &sip->sin_addr;
1798
1799 if (siap->s_addr == 0)
1800 return NULL;
1801
1802 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1803 /*
1804 * we break after finding the first match because
1805 * the list is sorted from longest to shortest mask
1806 * so we have found the most specific match
1807 */
1808 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001809 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1810 /* we have found the special CIPSO option */
1811 if (snp->smk_label == smack_cipso_option)
1812 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001813 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001814 }
Paul Moore07feee82009-03-27 17:10:54 -04001815
1816 return NULL;
1817}
1818
1819/**
Casey Schauflere114e472008-02-04 22:29:50 -08001820 * smack_set_catset - convert a capset to netlabel mls categories
1821 * @catset: the Smack categories
1822 * @sap: where to put the netlabel categories
1823 *
1824 * Allocates and fills attr.mls.cat
1825 */
1826static void smack_set_catset(char *catset, struct netlbl_lsm_secattr *sap)
1827{
1828 unsigned char *cp;
1829 unsigned char m;
1830 int cat;
1831 int rc;
1832 int byte;
1833
Harvey Harrisonc60264c2008-04-28 02:13:41 -07001834 if (!catset)
Casey Schauflere114e472008-02-04 22:29:50 -08001835 return;
1836
1837 sap->flags |= NETLBL_SECATTR_MLS_CAT;
1838 sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1839 sap->attr.mls.cat->startbit = 0;
1840
1841 for (cat = 1, cp = catset, byte = 0; byte < SMK_LABELLEN; cp++, byte++)
1842 for (m = 0x80; m != 0; m >>= 1, cat++) {
1843 if ((m & *cp) == 0)
1844 continue;
1845 rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
1846 cat, GFP_ATOMIC);
1847 }
1848}
1849
1850/**
1851 * smack_to_secattr - fill a secattr from a smack value
1852 * @smack: the smack value
1853 * @nlsp: where the result goes
1854 *
1855 * Casey says that CIPSO is good enough for now.
1856 * It can be used to effect.
1857 * It can also be abused to effect when necessary.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001858 * Apologies to the TSIG group in general and GW in particular.
Casey Schauflere114e472008-02-04 22:29:50 -08001859 */
1860static void smack_to_secattr(char *smack, struct netlbl_lsm_secattr *nlsp)
1861{
1862 struct smack_cipso cipso;
1863 int rc;
1864
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001865 nlsp->domain = smack;
1866 nlsp->flags = NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
Casey Schauflere114e472008-02-04 22:29:50 -08001867
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001868 rc = smack_to_cipso(smack, &cipso);
1869 if (rc == 0) {
1870 nlsp->attr.mls.lvl = cipso.smk_level;
1871 smack_set_catset(cipso.smk_catset, nlsp);
1872 } else {
1873 nlsp->attr.mls.lvl = smack_cipso_direct;
1874 smack_set_catset(smack, nlsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001875 }
1876}
1877
1878/**
1879 * smack_netlabel - Set the secattr on a socket
1880 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001881 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001882 *
1883 * Convert the outbound smack value (smk_out) to a
1884 * secattr and attach it to the socket.
1885 *
1886 * Returns 0 on success or an error code
1887 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001888static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001889{
Paul Moore07feee82009-03-27 17:10:54 -04001890 struct socket_smack *ssp = sk->sk_security;
Casey Schauflere114e472008-02-04 22:29:50 -08001891 struct netlbl_lsm_secattr secattr;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001892 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001893
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001894 /*
1895 * Usually the netlabel code will handle changing the
1896 * packet labeling based on the label.
1897 * The case of a single label host is different, because
1898 * a single label host should never get a labeled packet
1899 * even though the label is usually associated with a packet
1900 * label.
1901 */
1902 local_bh_disable();
1903 bh_lock_sock_nested(sk);
1904
1905 if (ssp->smk_out == smack_net_ambient ||
1906 labeled == SMACK_UNLABELED_SOCKET)
1907 netlbl_sock_delattr(sk);
1908 else {
1909 netlbl_secattr_init(&secattr);
1910 smack_to_secattr(ssp->smk_out, &secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001911 rc = netlbl_sock_setattr(sk, sk->sk_family, &secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001912 netlbl_secattr_destroy(&secattr);
1913 }
1914
1915 bh_unlock_sock(sk);
1916 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001917
Casey Schauflere114e472008-02-04 22:29:50 -08001918 return rc;
1919}
1920
1921/**
Paul Moore07feee82009-03-27 17:10:54 -04001922 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1923 * @sk: the socket
1924 * @sap: the destination address
1925 *
1926 * Set the correct secattr for the given socket based on the destination
1927 * address and perform any outbound access checks needed.
1928 *
1929 * Returns 0 on success or an error code.
1930 *
1931 */
1932static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1933{
1934 int rc;
1935 int sk_lbl;
1936 char *hostsp;
1937 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001938 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001939
1940 rcu_read_lock();
1941 hostsp = smack_host_label(sap);
1942 if (hostsp != NULL) {
1943 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001944#ifdef CONFIG_AUDIT
1945 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
1946 ad.a.u.net.family = sap->sin_family;
1947 ad.a.u.net.dport = sap->sin_port;
1948 ad.a.u.net.v4info.daddr = sap->sin_addr.s_addr;
1949#endif
1950 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001951 } else {
1952 sk_lbl = SMACK_CIPSO_SOCKET;
1953 rc = 0;
1954 }
1955 rcu_read_unlock();
1956 if (rc != 0)
1957 return rc;
1958
1959 return smack_netlabel(sk, sk_lbl);
1960}
1961
1962/**
Casey Schauflere114e472008-02-04 22:29:50 -08001963 * smack_inode_setsecurity - set smack xattrs
1964 * @inode: the object
1965 * @name: attribute name
1966 * @value: attribute value
1967 * @size: size of the attribute
1968 * @flags: unused
1969 *
1970 * Sets the named attribute in the appropriate blob
1971 *
1972 * Returns 0 on success, or an error code
1973 */
1974static int smack_inode_setsecurity(struct inode *inode, const char *name,
1975 const void *value, size_t size, int flags)
1976{
1977 char *sp;
1978 struct inode_smack *nsp = inode->i_security;
1979 struct socket_smack *ssp;
1980 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001981 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001982
Etienne Basset43031542009-03-27 17:11:01 -04001983 if (value == NULL || size > SMK_LABELLEN || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001984 return -EACCES;
1985
1986 sp = smk_import(value, size);
1987 if (sp == NULL)
1988 return -EINVAL;
1989
1990 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1991 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04001992 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08001993 return 0;
1994 }
1995 /*
1996 * The rest of the Smack xattrs are only on sockets.
1997 */
1998 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
1999 return -EOPNOTSUPP;
2000
2001 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002002 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002003 return -EOPNOTSUPP;
2004
2005 ssp = sock->sk->sk_security;
2006
2007 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2008 ssp->smk_in = sp;
2009 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2010 ssp->smk_out = sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002011 if (sock->sk->sk_family != PF_UNIX) {
2012 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2013 if (rc != 0)
2014 printk(KERN_WARNING
2015 "Smack: \"%s\" netlbl error %d.\n",
2016 __func__, -rc);
2017 }
Casey Schauflere114e472008-02-04 22:29:50 -08002018 } else
2019 return -EOPNOTSUPP;
2020
2021 return 0;
2022}
2023
2024/**
2025 * smack_socket_post_create - finish socket setup
2026 * @sock: the socket
2027 * @family: protocol family
2028 * @type: unused
2029 * @protocol: unused
2030 * @kern: unused
2031 *
2032 * Sets the netlabel information on the socket
2033 *
2034 * Returns 0 on success, and error code otherwise
2035 */
2036static int smack_socket_post_create(struct socket *sock, int family,
2037 int type, int protocol, int kern)
2038{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002039 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002040 return 0;
2041 /*
2042 * Set the outbound netlbl.
2043 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002044 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2045}
2046
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002047/**
2048 * smack_socket_connect - connect access check
2049 * @sock: the socket
2050 * @sap: the other end
2051 * @addrlen: size of sap
2052 *
2053 * Verifies that a connection may be possible
2054 *
2055 * Returns 0 on success, and error code otherwise
2056 */
2057static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2058 int addrlen)
2059{
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002060 if (sock->sk == NULL || sock->sk->sk_family != PF_INET)
2061 return 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002062 if (addrlen < sizeof(struct sockaddr_in))
2063 return -EINVAL;
2064
Paul Moore07feee82009-03-27 17:10:54 -04002065 return smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflere114e472008-02-04 22:29:50 -08002066}
2067
2068/**
2069 * smack_flags_to_may - convert S_ to MAY_ values
2070 * @flags: the S_ value
2071 *
2072 * Returns the equivalent MAY_ value
2073 */
2074static int smack_flags_to_may(int flags)
2075{
2076 int may = 0;
2077
2078 if (flags & S_IRUGO)
2079 may |= MAY_READ;
2080 if (flags & S_IWUGO)
2081 may |= MAY_WRITE;
2082 if (flags & S_IXUGO)
2083 may |= MAY_EXEC;
2084
2085 return may;
2086}
2087
2088/**
2089 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2090 * @msg: the object
2091 *
2092 * Returns 0
2093 */
2094static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2095{
Casey Schaufler676dac42010-12-02 06:43:39 -08002096 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002097 return 0;
2098}
2099
2100/**
2101 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2102 * @msg: the object
2103 *
2104 * Clears the blob pointer
2105 */
2106static void smack_msg_msg_free_security(struct msg_msg *msg)
2107{
2108 msg->security = NULL;
2109}
2110
2111/**
2112 * smack_of_shm - the smack pointer for the shm
2113 * @shp: the object
2114 *
2115 * Returns a pointer to the smack value
2116 */
2117static char *smack_of_shm(struct shmid_kernel *shp)
2118{
2119 return (char *)shp->shm_perm.security;
2120}
2121
2122/**
2123 * smack_shm_alloc_security - Set the security blob for shm
2124 * @shp: the object
2125 *
2126 * Returns 0
2127 */
2128static int smack_shm_alloc_security(struct shmid_kernel *shp)
2129{
2130 struct kern_ipc_perm *isp = &shp->shm_perm;
2131
Casey Schaufler676dac42010-12-02 06:43:39 -08002132 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002133 return 0;
2134}
2135
2136/**
2137 * smack_shm_free_security - Clear the security blob for shm
2138 * @shp: the object
2139 *
2140 * Clears the blob pointer
2141 */
2142static void smack_shm_free_security(struct shmid_kernel *shp)
2143{
2144 struct kern_ipc_perm *isp = &shp->shm_perm;
2145
2146 isp->security = NULL;
2147}
2148
2149/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002150 * smk_curacc_shm : check if current has access on shm
2151 * @shp : the object
2152 * @access : access requested
2153 *
2154 * Returns 0 if current has the requested access, error code otherwise
2155 */
2156static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2157{
2158 char *ssp = smack_of_shm(shp);
2159 struct smk_audit_info ad;
2160
2161#ifdef CONFIG_AUDIT
2162 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2163 ad.a.u.ipc_id = shp->shm_perm.id;
2164#endif
2165 return smk_curacc(ssp, access, &ad);
2166}
2167
2168/**
Casey Schauflere114e472008-02-04 22:29:50 -08002169 * smack_shm_associate - Smack access check for shm
2170 * @shp: the object
2171 * @shmflg: access requested
2172 *
2173 * Returns 0 if current has the requested access, error code otherwise
2174 */
2175static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2176{
Casey Schauflere114e472008-02-04 22:29:50 -08002177 int may;
2178
2179 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002180 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002181}
2182
2183/**
2184 * smack_shm_shmctl - Smack access check for shm
2185 * @shp: the object
2186 * @cmd: what it wants to do
2187 *
2188 * Returns 0 if current has the requested access, error code otherwise
2189 */
2190static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2191{
Casey Schauflere114e472008-02-04 22:29:50 -08002192 int may;
2193
2194 switch (cmd) {
2195 case IPC_STAT:
2196 case SHM_STAT:
2197 may = MAY_READ;
2198 break;
2199 case IPC_SET:
2200 case SHM_LOCK:
2201 case SHM_UNLOCK:
2202 case IPC_RMID:
2203 may = MAY_READWRITE;
2204 break;
2205 case IPC_INFO:
2206 case SHM_INFO:
2207 /*
2208 * System level information.
2209 */
2210 return 0;
2211 default:
2212 return -EINVAL;
2213 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002214 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002215}
2216
2217/**
2218 * smack_shm_shmat - Smack access for shmat
2219 * @shp: the object
2220 * @shmaddr: unused
2221 * @shmflg: access requested
2222 *
2223 * Returns 0 if current has the requested access, error code otherwise
2224 */
2225static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2226 int shmflg)
2227{
Casey Schauflere114e472008-02-04 22:29:50 -08002228 int may;
2229
2230 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002231 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002232}
2233
2234/**
2235 * smack_of_sem - the smack pointer for the sem
2236 * @sma: the object
2237 *
2238 * Returns a pointer to the smack value
2239 */
2240static char *smack_of_sem(struct sem_array *sma)
2241{
2242 return (char *)sma->sem_perm.security;
2243}
2244
2245/**
2246 * smack_sem_alloc_security - Set the security blob for sem
2247 * @sma: the object
2248 *
2249 * Returns 0
2250 */
2251static int smack_sem_alloc_security(struct sem_array *sma)
2252{
2253 struct kern_ipc_perm *isp = &sma->sem_perm;
2254
Casey Schaufler676dac42010-12-02 06:43:39 -08002255 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002256 return 0;
2257}
2258
2259/**
2260 * smack_sem_free_security - Clear the security blob for sem
2261 * @sma: the object
2262 *
2263 * Clears the blob pointer
2264 */
2265static void smack_sem_free_security(struct sem_array *sma)
2266{
2267 struct kern_ipc_perm *isp = &sma->sem_perm;
2268
2269 isp->security = NULL;
2270}
2271
2272/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002273 * smk_curacc_sem : check if current has access on sem
2274 * @sma : the object
2275 * @access : access requested
2276 *
2277 * Returns 0 if current has the requested access, error code otherwise
2278 */
2279static int smk_curacc_sem(struct sem_array *sma, int access)
2280{
2281 char *ssp = smack_of_sem(sma);
2282 struct smk_audit_info ad;
2283
2284#ifdef CONFIG_AUDIT
2285 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2286 ad.a.u.ipc_id = sma->sem_perm.id;
2287#endif
2288 return smk_curacc(ssp, access, &ad);
2289}
2290
2291/**
Casey Schauflere114e472008-02-04 22:29:50 -08002292 * smack_sem_associate - Smack access check for sem
2293 * @sma: the object
2294 * @semflg: access requested
2295 *
2296 * Returns 0 if current has the requested access, error code otherwise
2297 */
2298static int smack_sem_associate(struct sem_array *sma, int semflg)
2299{
Casey Schauflere114e472008-02-04 22:29:50 -08002300 int may;
2301
2302 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002303 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002304}
2305
2306/**
2307 * smack_sem_shmctl - Smack access check for sem
2308 * @sma: the object
2309 * @cmd: what it wants to do
2310 *
2311 * Returns 0 if current has the requested access, error code otherwise
2312 */
2313static int smack_sem_semctl(struct sem_array *sma, int cmd)
2314{
Casey Schauflere114e472008-02-04 22:29:50 -08002315 int may;
2316
2317 switch (cmd) {
2318 case GETPID:
2319 case GETNCNT:
2320 case GETZCNT:
2321 case GETVAL:
2322 case GETALL:
2323 case IPC_STAT:
2324 case SEM_STAT:
2325 may = MAY_READ;
2326 break;
2327 case SETVAL:
2328 case SETALL:
2329 case IPC_RMID:
2330 case IPC_SET:
2331 may = MAY_READWRITE;
2332 break;
2333 case IPC_INFO:
2334 case SEM_INFO:
2335 /*
2336 * System level information
2337 */
2338 return 0;
2339 default:
2340 return -EINVAL;
2341 }
2342
Etienne Bassetecfcc532009-04-08 20:40:06 +02002343 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002344}
2345
2346/**
2347 * smack_sem_semop - Smack checks of semaphore operations
2348 * @sma: the object
2349 * @sops: unused
2350 * @nsops: unused
2351 * @alter: unused
2352 *
2353 * Treated as read and write in all cases.
2354 *
2355 * Returns 0 if access is allowed, error code otherwise
2356 */
2357static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2358 unsigned nsops, int alter)
2359{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002360 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002361}
2362
2363/**
2364 * smack_msg_alloc_security - Set the security blob for msg
2365 * @msq: the object
2366 *
2367 * Returns 0
2368 */
2369static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2370{
2371 struct kern_ipc_perm *kisp = &msq->q_perm;
2372
Casey Schaufler676dac42010-12-02 06:43:39 -08002373 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002374 return 0;
2375}
2376
2377/**
2378 * smack_msg_free_security - Clear the security blob for msg
2379 * @msq: the object
2380 *
2381 * Clears the blob pointer
2382 */
2383static void smack_msg_queue_free_security(struct msg_queue *msq)
2384{
2385 struct kern_ipc_perm *kisp = &msq->q_perm;
2386
2387 kisp->security = NULL;
2388}
2389
2390/**
2391 * smack_of_msq - the smack pointer for the msq
2392 * @msq: the object
2393 *
2394 * Returns a pointer to the smack value
2395 */
2396static char *smack_of_msq(struct msg_queue *msq)
2397{
2398 return (char *)msq->q_perm.security;
2399}
2400
2401/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002402 * smk_curacc_msq : helper to check if current has access on msq
2403 * @msq : the msq
2404 * @access : access requested
2405 *
2406 * return 0 if current has access, error otherwise
2407 */
2408static int smk_curacc_msq(struct msg_queue *msq, int access)
2409{
2410 char *msp = smack_of_msq(msq);
2411 struct smk_audit_info ad;
2412
2413#ifdef CONFIG_AUDIT
2414 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2415 ad.a.u.ipc_id = msq->q_perm.id;
2416#endif
2417 return smk_curacc(msp, access, &ad);
2418}
2419
2420/**
Casey Schauflere114e472008-02-04 22:29:50 -08002421 * smack_msg_queue_associate - Smack access check for msg_queue
2422 * @msq: the object
2423 * @msqflg: access requested
2424 *
2425 * Returns 0 if current has the requested access, error code otherwise
2426 */
2427static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2428{
Casey Schauflere114e472008-02-04 22:29:50 -08002429 int may;
2430
2431 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002432 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002433}
2434
2435/**
2436 * smack_msg_queue_msgctl - Smack access check for msg_queue
2437 * @msq: the object
2438 * @cmd: what it wants to do
2439 *
2440 * Returns 0 if current has the requested access, error code otherwise
2441 */
2442static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2443{
Casey Schauflere114e472008-02-04 22:29:50 -08002444 int may;
2445
2446 switch (cmd) {
2447 case IPC_STAT:
2448 case MSG_STAT:
2449 may = MAY_READ;
2450 break;
2451 case IPC_SET:
2452 case IPC_RMID:
2453 may = MAY_READWRITE;
2454 break;
2455 case IPC_INFO:
2456 case MSG_INFO:
2457 /*
2458 * System level information
2459 */
2460 return 0;
2461 default:
2462 return -EINVAL;
2463 }
2464
Etienne Bassetecfcc532009-04-08 20:40:06 +02002465 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002466}
2467
2468/**
2469 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2470 * @msq: the object
2471 * @msg: unused
2472 * @msqflg: access requested
2473 *
2474 * Returns 0 if current has the requested access, error code otherwise
2475 */
2476static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2477 int msqflg)
2478{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002479 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002480
Etienne Bassetecfcc532009-04-08 20:40:06 +02002481 may = smack_flags_to_may(msqflg);
2482 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002483}
2484
2485/**
2486 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2487 * @msq: the object
2488 * @msg: unused
2489 * @target: unused
2490 * @type: unused
2491 * @mode: unused
2492 *
2493 * Returns 0 if current has read and write access, error code otherwise
2494 */
2495static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2496 struct task_struct *target, long type, int mode)
2497{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002498 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002499}
2500
2501/**
2502 * smack_ipc_permission - Smack access for ipc_permission()
2503 * @ipp: the object permissions
2504 * @flag: access requested
2505 *
2506 * Returns 0 if current has read and write access, error code otherwise
2507 */
2508static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2509{
2510 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002511 int may = smack_flags_to_may(flag);
2512 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002513
Etienne Bassetecfcc532009-04-08 20:40:06 +02002514#ifdef CONFIG_AUDIT
2515 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2516 ad.a.u.ipc_id = ipp->id;
2517#endif
2518 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002519}
2520
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002521/**
2522 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002523 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002524 * @secid: where result will be saved
2525 */
2526static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2527{
2528 char *smack = ipp->security;
2529
2530 *secid = smack_to_secid(smack);
2531}
2532
Casey Schauflere114e472008-02-04 22:29:50 -08002533/**
2534 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002535 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002536 * @inode: the object
2537 *
2538 * Set the inode's security blob if it hasn't been done already.
2539 */
2540static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2541{
2542 struct super_block *sbp;
2543 struct superblock_smack *sbsp;
2544 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002545 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002546 char *fetched;
2547 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002548 char trattr[TRANS_TRUE_SIZE];
2549 int transflag = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002550 struct dentry *dp;
2551
2552 if (inode == NULL)
2553 return;
2554
2555 isp = inode->i_security;
2556
2557 mutex_lock(&isp->smk_lock);
2558 /*
2559 * If the inode is already instantiated
2560 * take the quick way out
2561 */
2562 if (isp->smk_flags & SMK_INODE_INSTANT)
2563 goto unlockandout;
2564
2565 sbp = inode->i_sb;
2566 sbsp = sbp->s_security;
2567 /*
2568 * We're going to use the superblock default label
2569 * if there's no label on the file.
2570 */
2571 final = sbsp->smk_default;
2572
2573 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002574 * If this is the root inode the superblock
2575 * may be in the process of initialization.
2576 * If that is the case use the root value out
2577 * of the superblock.
2578 */
2579 if (opt_dentry->d_parent == opt_dentry) {
2580 isp->smk_inode = sbsp->smk_root;
2581 isp->smk_flags |= SMK_INODE_INSTANT;
2582 goto unlockandout;
2583 }
2584
2585 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002586 * This is pretty hackish.
2587 * Casey says that we shouldn't have to do
2588 * file system specific code, but it does help
2589 * with keeping it simple.
2590 */
2591 switch (sbp->s_magic) {
2592 case SMACK_MAGIC:
2593 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002594 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002595 * that the smack file system doesn't do
2596 * extended attributes.
2597 */
2598 final = smack_known_star.smk_known;
2599 break;
2600 case PIPEFS_MAGIC:
2601 /*
2602 * Casey says pipes are easy (?)
2603 */
2604 final = smack_known_star.smk_known;
2605 break;
2606 case DEVPTS_SUPER_MAGIC:
2607 /*
2608 * devpts seems content with the label of the task.
2609 * Programs that change smack have to treat the
2610 * pty with respect.
2611 */
2612 final = csp;
2613 break;
2614 case SOCKFS_MAGIC:
2615 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002616 * Socket access is controlled by the socket
2617 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002618 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002619 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002620 break;
2621 case PROC_SUPER_MAGIC:
2622 /*
2623 * Casey says procfs appears not to care.
2624 * The superblock default suffices.
2625 */
2626 break;
2627 case TMPFS_MAGIC:
2628 /*
2629 * Device labels should come from the filesystem,
2630 * but watch out, because they're volitile,
2631 * getting recreated on every reboot.
2632 */
2633 final = smack_known_star.smk_known;
2634 /*
2635 * No break.
2636 *
2637 * If a smack value has been set we want to use it,
2638 * but since tmpfs isn't giving us the opportunity
2639 * to set mount options simulate setting the
2640 * superblock default.
2641 */
2642 default:
2643 /*
2644 * This isn't an understood special case.
2645 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002646 */
2647
2648 /*
2649 * UNIX domain sockets use lower level socket data.
2650 */
2651 if (S_ISSOCK(inode->i_mode)) {
2652 final = smack_known_star.smk_known;
2653 break;
2654 }
2655 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002656 * No xattr support means, alas, no SMACK label.
2657 * Use the aforeapplied default.
2658 * It would be curious if the label of the task
2659 * does not match that assigned.
2660 */
2661 if (inode->i_op->getxattr == NULL)
2662 break;
2663 /*
2664 * Get the dentry for xattr.
2665 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002666 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002667 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002668 if (fetched != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -08002669 final = fetched;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002670 if (S_ISDIR(inode->i_mode)) {
2671 trattr[0] = '\0';
2672 inode->i_op->getxattr(dp,
2673 XATTR_NAME_SMACKTRANSMUTE,
2674 trattr, TRANS_TRUE_SIZE);
2675 if (strncmp(trattr, TRANS_TRUE,
2676 TRANS_TRUE_SIZE) == 0)
2677 transflag = SMK_INODE_TRANSMUTE;
2678 }
2679 }
2680 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002681 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002682
Casey Schauflere114e472008-02-04 22:29:50 -08002683 dput(dp);
2684 break;
2685 }
2686
2687 if (final == NULL)
2688 isp->smk_inode = csp;
2689 else
2690 isp->smk_inode = final;
2691
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002692 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002693
2694unlockandout:
2695 mutex_unlock(&isp->smk_lock);
2696 return;
2697}
2698
2699/**
2700 * smack_getprocattr - Smack process attribute access
2701 * @p: the object task
2702 * @name: the name of the attribute in /proc/.../attr
2703 * @value: where to put the result
2704 *
2705 * Places a copy of the task Smack into value
2706 *
2707 * Returns the length of the smack label or an error code
2708 */
2709static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2710{
2711 char *cp;
2712 int slen;
2713
2714 if (strcmp(name, "current") != 0)
2715 return -EINVAL;
2716
Casey Schaufler676dac42010-12-02 06:43:39 -08002717 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002718 if (cp == NULL)
2719 return -ENOMEM;
2720
2721 slen = strlen(cp);
2722 *value = cp;
2723 return slen;
2724}
2725
2726/**
2727 * smack_setprocattr - Smack process attribute setting
2728 * @p: the object task
2729 * @name: the name of the attribute in /proc/.../attr
2730 * @value: the value to set
2731 * @size: the size of the value
2732 *
2733 * Sets the Smack value of the task. Only setting self
2734 * is permitted and only with privilege
2735 *
2736 * Returns the length of the smack label or an error code
2737 */
2738static int smack_setprocattr(struct task_struct *p, char *name,
2739 void *value, size_t size)
2740{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002741 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002742 struct task_smack *tsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002743 struct task_smack *oldtsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002744 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002745 char *newsmack;
2746
Casey Schauflere114e472008-02-04 22:29:50 -08002747 /*
2748 * Changing another process' Smack value is too dangerous
2749 * and supports no sane use case.
2750 */
2751 if (p != current)
2752 return -EPERM;
2753
David Howells5cd9c582008-08-14 11:37:28 +01002754 if (!capable(CAP_MAC_ADMIN))
2755 return -EPERM;
2756
Casey Schauflere114e472008-02-04 22:29:50 -08002757 if (value == NULL || size == 0 || size >= SMK_LABELLEN)
2758 return -EINVAL;
2759
2760 if (strcmp(name, "current") != 0)
2761 return -EINVAL;
2762
2763 newsmack = smk_import(value, size);
2764 if (newsmack == NULL)
2765 return -EINVAL;
2766
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002767 /*
2768 * No process is ever allowed the web ("@") label.
2769 */
2770 if (newsmack == smack_known_web.smk_known)
2771 return -EPERM;
2772
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002773 oldtsp = p->cred->security;
David Howellsd84f4f92008-11-14 10:39:23 +11002774 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002775 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002776 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002777
2778 tsp = new_task_smack(newsmack, oldtsp->smk_forked, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08002779 if (tsp == NULL) {
2780 kfree(new);
2781 return -ENOMEM;
2782 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002783 rc = smk_copy_rules(&tsp->smk_rules, &oldtsp->smk_rules, GFP_KERNEL);
2784 if (rc != 0)
2785 return rc;
2786
Casey Schaufler676dac42010-12-02 06:43:39 -08002787 new->security = tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002788 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002789 return size;
2790}
2791
2792/**
2793 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002794 * @sock: one sock
2795 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002796 * @newsk: unused
2797 *
2798 * Return 0 if a subject with the smack of sock could access
2799 * an object with the smack of other, otherwise an error code
2800 */
David S. Miller3610cda2011-01-05 15:38:53 -08002801static int smack_unix_stream_connect(struct sock *sock,
2802 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002803{
James Morrisd2e7ad12011-01-10 09:46:24 +11002804 struct socket_smack *ssp = sock->sk_security;
2805 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002806 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002807 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002808 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002809
Etienne Bassetecfcc532009-04-08 20:40:06 +02002810 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
David S. Miller3610cda2011-01-05 15:38:53 -08002811 smk_ad_setfield_u_net_sk(&ad, other);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002812
2813 if (!capable(CAP_MAC_OVERRIDE))
2814 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2815
Casey Schaufler975d5e52011-09-26 14:43:39 -07002816 /*
2817 * Cross reference the peer labels for SO_PEERSEC.
2818 */
2819 if (rc == 0) {
2820 nsp->smk_packet = ssp->smk_out;
2821 ssp->smk_packet = osp->smk_out;
2822 }
2823
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002824 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002825}
2826
2827/**
2828 * smack_unix_may_send - Smack access on UDS
2829 * @sock: one socket
2830 * @other: the other socket
2831 *
2832 * Return 0 if a subject with the smack of sock could access
2833 * an object with the smack of other, otherwise an error code
2834 */
2835static int smack_unix_may_send(struct socket *sock, struct socket *other)
2836{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002837 struct socket_smack *ssp = sock->sk->sk_security;
2838 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002839 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002840 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002841
Etienne Bassetecfcc532009-04-08 20:40:06 +02002842 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
2843 smk_ad_setfield_u_net_sk(&ad, other->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002844
2845 if (!capable(CAP_MAC_OVERRIDE))
2846 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2847
2848 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002849}
2850
2851/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002852 * smack_socket_sendmsg - Smack check based on destination host
2853 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002854 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002855 * @size: the size of the message
2856 *
2857 * Return 0 if the current subject can write to the destination
2858 * host. This is only a question if the destination is a single
2859 * label host.
2860 */
2861static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2862 int size)
2863{
2864 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002865
2866 /*
2867 * Perfectly reasonable for this to be NULL
2868 */
Julia Lawallda34d422009-08-05 14:34:55 +02002869 if (sip == NULL || sip->sin_family != AF_INET)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002870 return 0;
2871
Paul Moore07feee82009-03-27 17:10:54 -04002872 return smack_netlabel_send(sock->sk, sip);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002873}
2874
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002875/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08002876 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08002877 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002878 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08002879 *
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002880 * Returns a pointer to a Smack label found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08002881 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002882static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
2883 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08002884{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002885 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002886 char smack[SMK_LABELLEN];
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002887 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002888 int pcat;
2889
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002890 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08002891 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002892 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08002893 * If there are flags but no level netlabel isn't
2894 * behaving the way we expect it to.
2895 *
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002896 * Get the categories, if any
Casey Schauflere114e472008-02-04 22:29:50 -08002897 * Without guidance regarding the smack value
2898 * for the packet fall back on the network
2899 * ambient value.
2900 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002901 memset(smack, '\0', SMK_LABELLEN);
2902 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) != 0)
2903 for (pcat = -1;;) {
2904 pcat = netlbl_secattr_catmap_walk(
2905 sap->attr.mls.cat, pcat + 1);
2906 if (pcat < 0)
2907 break;
2908 smack_catset_bit(pcat, smack);
2909 }
2910 /*
2911 * If it is CIPSO using smack direct mapping
2912 * we are already done. WeeHee.
2913 */
2914 if (sap->attr.mls.lvl == smack_cipso_direct) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002915 /*
2916 * The label sent is usually on the label list.
2917 *
2918 * If it is not we may still want to allow the
2919 * delivery.
2920 *
2921 * If the recipient is accepting all packets
2922 * because it is using the star ("*") label
2923 * for SMACK64IPIN provide the web ("@") label
2924 * so that a directed response will succeed.
2925 * This is not very correct from a MAC point
2926 * of view, but gets around the problem that
2927 * locking prevents adding the newly discovered
2928 * label to the list.
2929 * The case where the recipient is not using
2930 * the star label should obviously fail.
2931 * The easy way to do this is to provide the
2932 * star label as the subject label.
2933 */
2934 skp = smk_find_entry(smack);
2935 if (skp != NULL)
2936 return skp->smk_known;
2937 if (ssp != NULL &&
2938 ssp->smk_in == smack_known_star.smk_known)
2939 return smack_known_web.smk_known;
2940 return smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002941 }
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002942 /*
2943 * Look it up in the supplied table if it is not
2944 * a direct mapping.
2945 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002946 sp = smack_from_cipso(sap->attr.mls.lvl, smack);
2947 if (sp != NULL)
2948 return sp;
2949 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
2950 return smack_known_web.smk_known;
2951 return smack_known_star.smk_known;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002952 }
2953 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
2954 /*
2955 * Looks like a fallback, which gives us a secid.
2956 */
2957 sp = smack_from_secid(sap->attr.secid);
2958 /*
2959 * This has got to be a bug because it is
2960 * impossible to specify a fallback without
2961 * specifying the label, which will ensure
2962 * it has a secid, and the only way to get a
2963 * secid is from a fallback.
2964 */
2965 BUG_ON(sp == NULL);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002966 return sp;
Casey Schauflere114e472008-02-04 22:29:50 -08002967 }
2968 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002969 * Without guidance regarding the smack value
2970 * for the packet fall back on the network
2971 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08002972 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002973 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08002974}
2975
2976/**
2977 * smack_socket_sock_rcv_skb - Smack packet delivery access check
2978 * @sk: socket
2979 * @skb: packet
2980 *
2981 * Returns 0 if the packet should be delivered, an error code otherwise
2982 */
2983static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
2984{
2985 struct netlbl_lsm_secattr secattr;
2986 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002987 char *csp;
Casey Schauflere114e472008-02-04 22:29:50 -08002988 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002989 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002990 if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
2991 return 0;
2992
2993 /*
2994 * Translate what netlabel gave us.
2995 */
Casey Schauflere114e472008-02-04 22:29:50 -08002996 netlbl_secattr_init(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002997
Casey Schauflere114e472008-02-04 22:29:50 -08002998 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002999 if (rc == 0)
3000 csp = smack_from_secattr(&secattr, ssp);
3001 else
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003002 csp = smack_net_ambient;
3003
Casey Schauflere114e472008-02-04 22:29:50 -08003004 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003005
Etienne Bassetecfcc532009-04-08 20:40:06 +02003006#ifdef CONFIG_AUDIT
3007 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3008 ad.a.u.net.family = sk->sk_family;
Eric Dumazet8964be42009-11-20 15:35:04 -08003009 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003010 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3011#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003012 /*
3013 * Receiving a packet requires that the other end
3014 * be able to write here. Read access is not required.
3015 * This is the simplist possible security model
3016 * for networking.
3017 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02003018 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moorea8134292008-10-10 10:16:31 -04003019 if (rc != 0)
3020 netlbl_skbuff_err(skb, rc, 0);
3021 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003022}
3023
3024/**
3025 * smack_socket_getpeersec_stream - pull in packet label
3026 * @sock: the socket
3027 * @optval: user's destination
3028 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003029 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003030 *
3031 * returns zero on success, an error code otherwise
3032 */
3033static int smack_socket_getpeersec_stream(struct socket *sock,
3034 char __user *optval,
3035 int __user *optlen, unsigned len)
3036{
3037 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003038 char *rcp = "";
3039 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003040 int rc = 0;
3041
3042 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003043 if (ssp->smk_packet != NULL) {
3044 rcp = ssp->smk_packet;
3045 slen = strlen(rcp) + 1;
3046 }
Casey Schauflere114e472008-02-04 22:29:50 -08003047
3048 if (slen > len)
3049 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003050 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003051 rc = -EFAULT;
3052
3053 if (put_user(slen, optlen) != 0)
3054 rc = -EFAULT;
3055
3056 return rc;
3057}
3058
3059
3060/**
3061 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003062 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003063 * @skb: packet data
3064 * @secid: pointer to where to put the secid of the packet
3065 *
3066 * Sets the netlabel socket state on sk from parent
3067 */
3068static int smack_socket_getpeersec_dgram(struct socket *sock,
3069 struct sk_buff *skb, u32 *secid)
3070
3071{
3072 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003073 struct socket_smack *ssp = NULL;
3074 char *sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003075 int family = PF_UNSPEC;
3076 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003077 int rc;
3078
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003079 if (skb != NULL) {
3080 if (skb->protocol == htons(ETH_P_IP))
3081 family = PF_INET;
3082 else if (skb->protocol == htons(ETH_P_IPV6))
3083 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003084 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003085 if (family == PF_UNSPEC && sock != NULL)
3086 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003087
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003088 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003089 ssp = sock->sk->sk_security;
3090 s = smack_to_secid(ssp->smk_out);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003091 } else if (family == PF_INET || family == PF_INET6) {
3092 /*
3093 * Translate what netlabel gave us.
3094 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003095 if (sock != NULL && sock->sk != NULL)
3096 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003097 netlbl_secattr_init(&secattr);
3098 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3099 if (rc == 0) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003100 sp = smack_from_secattr(&secattr, ssp);
3101 s = smack_to_secid(sp);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003102 }
3103 netlbl_secattr_destroy(&secattr);
3104 }
3105 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003106 if (s == 0)
3107 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003108 return 0;
3109}
3110
3111/**
Paul Moore07feee82009-03-27 17:10:54 -04003112 * smack_sock_graft - Initialize a newly created socket with an existing sock
3113 * @sk: child sock
3114 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003115 *
Paul Moore07feee82009-03-27 17:10:54 -04003116 * Set the smk_{in,out} state of an existing sock based on the process that
3117 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003118 */
3119static void smack_sock_graft(struct sock *sk, struct socket *parent)
3120{
3121 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08003122
Paul Moore07feee82009-03-27 17:10:54 -04003123 if (sk == NULL ||
3124 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003125 return;
3126
3127 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08003128 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04003129 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003130}
3131
3132/**
3133 * smack_inet_conn_request - Smack access check on connect
3134 * @sk: socket involved
3135 * @skb: packet
3136 * @req: unused
3137 *
3138 * Returns 0 if a task with the packet label could write to
3139 * the socket, otherwise an error code
3140 */
3141static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3142 struct request_sock *req)
3143{
Paul Moore07feee82009-03-27 17:10:54 -04003144 u16 family = sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003145 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003146 struct netlbl_lsm_secattr secattr;
3147 struct sockaddr_in addr;
3148 struct iphdr *hdr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003149 char *sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003150 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003151 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003152
Paul Moore07feee82009-03-27 17:10:54 -04003153 /* handle mapped IPv4 packets arriving via IPv6 sockets */
3154 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3155 family = PF_INET;
Casey Schauflere114e472008-02-04 22:29:50 -08003156
Paul Moore07feee82009-03-27 17:10:54 -04003157 netlbl_secattr_init(&secattr);
3158 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003159 if (rc == 0)
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003160 sp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003161 else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003162 sp = smack_known_huh.smk_known;
Paul Moore07feee82009-03-27 17:10:54 -04003163 netlbl_secattr_destroy(&secattr);
3164
Etienne Bassetecfcc532009-04-08 20:40:06 +02003165#ifdef CONFIG_AUDIT
3166 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NET);
3167 ad.a.u.net.family = family;
Eric Dumazet8964be42009-11-20 15:35:04 -08003168 ad.a.u.net.netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003169 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3170#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003171 /*
Paul Moore07feee82009-03-27 17:10:54 -04003172 * Receiving a packet requires that the other end be able to write
3173 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003174 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003175 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003176 if (rc != 0)
3177 return rc;
3178
3179 /*
3180 * Save the peer's label in the request_sock so we can later setup
3181 * smk_packet in the child socket so that SO_PEERCRED can report it.
3182 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003183 req->peer_secid = smack_to_secid(sp);
Paul Moore07feee82009-03-27 17:10:54 -04003184
3185 /*
3186 * We need to decide if we want to label the incoming connection here
3187 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003188 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003189 */
3190 hdr = ip_hdr(skb);
3191 addr.sin_addr.s_addr = hdr->saddr;
3192 rcu_read_lock();
3193 if (smack_host_label(&addr) == NULL) {
3194 rcu_read_unlock();
3195 netlbl_secattr_init(&secattr);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003196 smack_to_secattr(sp, &secattr);
Paul Moore07feee82009-03-27 17:10:54 -04003197 rc = netlbl_req_setattr(req, &secattr);
3198 netlbl_secattr_destroy(&secattr);
3199 } else {
3200 rcu_read_unlock();
3201 netlbl_req_delattr(req);
3202 }
Casey Schauflere114e472008-02-04 22:29:50 -08003203
3204 return rc;
3205}
3206
Paul Moore07feee82009-03-27 17:10:54 -04003207/**
3208 * smack_inet_csk_clone - Copy the connection information to the new socket
3209 * @sk: the new socket
3210 * @req: the connection's request_sock
3211 *
3212 * Transfer the connection's peer label to the newly created socket.
3213 */
3214static void smack_inet_csk_clone(struct sock *sk,
3215 const struct request_sock *req)
3216{
3217 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003218
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003219 if (req->peer_secid != 0)
3220 ssp->smk_packet = smack_from_secid(req->peer_secid);
3221 else
3222 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003223}
3224
Casey Schauflere114e472008-02-04 22:29:50 -08003225/*
3226 * Key management security hooks
3227 *
3228 * Casey has not tested key support very heavily.
3229 * The permission check is most likely too restrictive.
3230 * If you care about keys please have a look.
3231 */
3232#ifdef CONFIG_KEYS
3233
3234/**
3235 * smack_key_alloc - Set the key security blob
3236 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003237 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003238 * @flags: unused
3239 *
3240 * No allocation required
3241 *
3242 * Returns 0
3243 */
David Howellsd84f4f92008-11-14 10:39:23 +11003244static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003245 unsigned long flags)
3246{
Casey Schaufler676dac42010-12-02 06:43:39 -08003247 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003248 return 0;
3249}
3250
3251/**
3252 * smack_key_free - Clear the key security blob
3253 * @key: the object
3254 *
3255 * Clear the blob pointer
3256 */
3257static void smack_key_free(struct key *key)
3258{
3259 key->security = NULL;
3260}
3261
3262/*
3263 * smack_key_permission - Smack access on a key
3264 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003265 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003266 * @perm: unused
3267 *
3268 * Return 0 if the task has read and write to the object,
3269 * an error code otherwise
3270 */
3271static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003272 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003273{
3274 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003275 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08003276 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003277
3278 keyp = key_ref_to_ptr(key_ref);
3279 if (keyp == NULL)
3280 return -EINVAL;
3281 /*
3282 * If the key hasn't been initialized give it access so that
3283 * it may do so.
3284 */
3285 if (keyp->security == NULL)
3286 return 0;
3287 /*
3288 * This should not occur
3289 */
Casey Schaufler676dac42010-12-02 06:43:39 -08003290 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003291 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003292#ifdef CONFIG_AUDIT
3293 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3294 ad.a.u.key_struct.key = keyp->serial;
3295 ad.a.u.key_struct.key_desc = keyp->description;
3296#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08003297 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02003298 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003299}
3300#endif /* CONFIG_KEYS */
3301
3302/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003303 * Smack Audit hooks
3304 *
3305 * Audit requires a unique representation of each Smack specific
3306 * rule. This unique representation is used to distinguish the
3307 * object to be audited from remaining kernel objects and also
3308 * works as a glue between the audit hooks.
3309 *
3310 * Since repository entries are added but never deleted, we'll use
3311 * the smack_known label address related to the given audit rule as
3312 * the needed unique representation. This also better fits the smack
3313 * model where nearly everything is a label.
3314 */
3315#ifdef CONFIG_AUDIT
3316
3317/**
3318 * smack_audit_rule_init - Initialize a smack audit rule
3319 * @field: audit rule fields given from user-space (audit.h)
3320 * @op: required testing operator (=, !=, >, <, ...)
3321 * @rulestr: smack label to be audited
3322 * @vrule: pointer to save our own audit rule representation
3323 *
3324 * Prepare to audit cases where (@field @op @rulestr) is true.
3325 * The label to be audited is created if necessay.
3326 */
3327static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3328{
3329 char **rule = (char **)vrule;
3330 *rule = NULL;
3331
3332 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3333 return -EINVAL;
3334
Al Viro5af75d82008-12-16 05:59:26 -05003335 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003336 return -EINVAL;
3337
3338 *rule = smk_import(rulestr, 0);
3339
3340 return 0;
3341}
3342
3343/**
3344 * smack_audit_rule_known - Distinguish Smack audit rules
3345 * @krule: rule of interest, in Audit kernel representation format
3346 *
3347 * This is used to filter Smack rules from remaining Audit ones.
3348 * If it's proved that this rule belongs to us, the
3349 * audit_rule_match hook will be called to do the final judgement.
3350 */
3351static int smack_audit_rule_known(struct audit_krule *krule)
3352{
3353 struct audit_field *f;
3354 int i;
3355
3356 for (i = 0; i < krule->field_count; i++) {
3357 f = &krule->fields[i];
3358
3359 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3360 return 1;
3361 }
3362
3363 return 0;
3364}
3365
3366/**
3367 * smack_audit_rule_match - Audit given object ?
3368 * @secid: security id for identifying the object to test
3369 * @field: audit rule flags given from user-space
3370 * @op: required testing operator
3371 * @vrule: smack internal rule presentation
3372 * @actx: audit context associated with the check
3373 *
3374 * The core Audit hook. It's used to take the decision of
3375 * whether to audit or not to audit a given object.
3376 */
3377static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3378 struct audit_context *actx)
3379{
3380 char *smack;
3381 char *rule = vrule;
3382
3383 if (!rule) {
3384 audit_log(actx, GFP_KERNEL, AUDIT_SELINUX_ERR,
3385 "Smack: missing rule\n");
3386 return -ENOENT;
3387 }
3388
3389 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3390 return 0;
3391
3392 smack = smack_from_secid(secid);
3393
3394 /*
3395 * No need to do string comparisons. If a match occurs,
3396 * both pointers will point to the same smack_known
3397 * label.
3398 */
Al Viro5af75d82008-12-16 05:59:26 -05003399 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003400 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003401 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003402 return (rule != smack);
3403
3404 return 0;
3405}
3406
3407/**
3408 * smack_audit_rule_free - free smack rule representation
3409 * @vrule: rule to be freed.
3410 *
3411 * No memory was allocated.
3412 */
3413static void smack_audit_rule_free(void *vrule)
3414{
3415 /* No-op */
3416}
3417
3418#endif /* CONFIG_AUDIT */
3419
Randy Dunlap251a2a92009-02-18 11:42:33 -08003420/**
Casey Schauflere114e472008-02-04 22:29:50 -08003421 * smack_secid_to_secctx - return the smack label for a secid
3422 * @secid: incoming integer
3423 * @secdata: destination
3424 * @seclen: how long it is
3425 *
3426 * Exists for networking code.
3427 */
3428static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3429{
3430 char *sp = smack_from_secid(secid);
3431
Eric Parisd5630b92010-10-13 16:24:48 -04003432 if (secdata)
3433 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003434 *seclen = strlen(sp);
3435 return 0;
3436}
3437
Randy Dunlap251a2a92009-02-18 11:42:33 -08003438/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003439 * smack_secctx_to_secid - return the secid for a smack label
3440 * @secdata: smack label
3441 * @seclen: how long result is
3442 * @secid: outgoing integer
3443 *
3444 * Exists for audit and networking code.
3445 */
David Howellse52c17642008-04-29 20:52:51 +01003446static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003447{
3448 *secid = smack_to_secid(secdata);
3449 return 0;
3450}
3451
Randy Dunlap251a2a92009-02-18 11:42:33 -08003452/**
Casey Schauflere114e472008-02-04 22:29:50 -08003453 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003454 * @secdata: unused
3455 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003456 *
3457 * Exists to make sure nothing gets done, and properly
3458 */
3459static void smack_release_secctx(char *secdata, u32 seclen)
3460{
3461}
3462
David P. Quigley1ee65e32009-09-03 14:25:57 -04003463static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3464{
3465 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3466}
3467
3468static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3469{
3470 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3471}
3472
3473static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3474{
3475 int len = 0;
3476 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3477
3478 if (len < 0)
3479 return len;
3480 *ctxlen = len;
3481 return 0;
3482}
3483
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003484struct security_operations smack_ops = {
3485 .name = "smack",
3486
Ingo Molnar9e488582009-05-07 19:26:19 +10003487 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003488 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003489 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003490
3491 .sb_alloc_security = smack_sb_alloc_security,
3492 .sb_free_security = smack_sb_free_security,
3493 .sb_copy_data = smack_sb_copy_data,
3494 .sb_kern_mount = smack_sb_kern_mount,
3495 .sb_statfs = smack_sb_statfs,
3496 .sb_mount = smack_sb_mount,
3497 .sb_umount = smack_sb_umount,
3498
Casey Schaufler676dac42010-12-02 06:43:39 -08003499 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003500 .bprm_committing_creds = smack_bprm_committing_creds,
3501 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003502
Casey Schauflere114e472008-02-04 22:29:50 -08003503 .inode_alloc_security = smack_inode_alloc_security,
3504 .inode_free_security = smack_inode_free_security,
3505 .inode_init_security = smack_inode_init_security,
3506 .inode_link = smack_inode_link,
3507 .inode_unlink = smack_inode_unlink,
3508 .inode_rmdir = smack_inode_rmdir,
3509 .inode_rename = smack_inode_rename,
3510 .inode_permission = smack_inode_permission,
3511 .inode_setattr = smack_inode_setattr,
3512 .inode_getattr = smack_inode_getattr,
3513 .inode_setxattr = smack_inode_setxattr,
3514 .inode_post_setxattr = smack_inode_post_setxattr,
3515 .inode_getxattr = smack_inode_getxattr,
3516 .inode_removexattr = smack_inode_removexattr,
3517 .inode_getsecurity = smack_inode_getsecurity,
3518 .inode_setsecurity = smack_inode_setsecurity,
3519 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003520 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003521
3522 .file_permission = smack_file_permission,
3523 .file_alloc_security = smack_file_alloc_security,
3524 .file_free_security = smack_file_free_security,
3525 .file_ioctl = smack_file_ioctl,
3526 .file_lock = smack_file_lock,
3527 .file_fcntl = smack_file_fcntl,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003528 .file_mmap = smack_file_mmap,
Casey Schauflere114e472008-02-04 22:29:50 -08003529 .file_set_fowner = smack_file_set_fowner,
3530 .file_send_sigiotask = smack_file_send_sigiotask,
3531 .file_receive = smack_file_receive,
3532
Casey Schaufler531f1d42011-09-19 12:41:42 -07003533 .dentry_open = smack_dentry_open,
3534
David Howellsee18d642009-09-02 09:14:21 +01003535 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003536 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003537 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003538 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003539 .kernel_act_as = smack_kernel_act_as,
3540 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003541 .task_setpgid = smack_task_setpgid,
3542 .task_getpgid = smack_task_getpgid,
3543 .task_getsid = smack_task_getsid,
3544 .task_getsecid = smack_task_getsecid,
3545 .task_setnice = smack_task_setnice,
3546 .task_setioprio = smack_task_setioprio,
3547 .task_getioprio = smack_task_getioprio,
3548 .task_setscheduler = smack_task_setscheduler,
3549 .task_getscheduler = smack_task_getscheduler,
3550 .task_movememory = smack_task_movememory,
3551 .task_kill = smack_task_kill,
3552 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003553 .task_to_inode = smack_task_to_inode,
3554
3555 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003556 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003557
3558 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3559 .msg_msg_free_security = smack_msg_msg_free_security,
3560
3561 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3562 .msg_queue_free_security = smack_msg_queue_free_security,
3563 .msg_queue_associate = smack_msg_queue_associate,
3564 .msg_queue_msgctl = smack_msg_queue_msgctl,
3565 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3566 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3567
3568 .shm_alloc_security = smack_shm_alloc_security,
3569 .shm_free_security = smack_shm_free_security,
3570 .shm_associate = smack_shm_associate,
3571 .shm_shmctl = smack_shm_shmctl,
3572 .shm_shmat = smack_shm_shmat,
3573
3574 .sem_alloc_security = smack_sem_alloc_security,
3575 .sem_free_security = smack_sem_free_security,
3576 .sem_associate = smack_sem_associate,
3577 .sem_semctl = smack_sem_semctl,
3578 .sem_semop = smack_sem_semop,
3579
Casey Schauflere114e472008-02-04 22:29:50 -08003580 .d_instantiate = smack_d_instantiate,
3581
3582 .getprocattr = smack_getprocattr,
3583 .setprocattr = smack_setprocattr,
3584
3585 .unix_stream_connect = smack_unix_stream_connect,
3586 .unix_may_send = smack_unix_may_send,
3587
3588 .socket_post_create = smack_socket_post_create,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003589 .socket_connect = smack_socket_connect,
3590 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003591 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3592 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3593 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3594 .sk_alloc_security = smack_sk_alloc_security,
3595 .sk_free_security = smack_sk_free_security,
3596 .sock_graft = smack_sock_graft,
3597 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003598 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003599
Casey Schauflere114e472008-02-04 22:29:50 -08003600 /* key management security hooks */
3601#ifdef CONFIG_KEYS
3602 .key_alloc = smack_key_alloc,
3603 .key_free = smack_key_free,
3604 .key_permission = smack_key_permission,
3605#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003606
3607 /* Audit hooks */
3608#ifdef CONFIG_AUDIT
3609 .audit_rule_init = smack_audit_rule_init,
3610 .audit_rule_known = smack_audit_rule_known,
3611 .audit_rule_match = smack_audit_rule_match,
3612 .audit_rule_free = smack_audit_rule_free,
3613#endif /* CONFIG_AUDIT */
3614
Casey Schauflere114e472008-02-04 22:29:50 -08003615 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003616 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003617 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003618 .inode_notifysecctx = smack_inode_notifysecctx,
3619 .inode_setsecctx = smack_inode_setsecctx,
3620 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003621};
3622
Etienne Basset7198e2e2009-03-24 20:53:24 +01003623
3624static __init void init_smack_know_list(void)
3625{
3626 list_add(&smack_known_huh.list, &smack_known_list);
3627 list_add(&smack_known_hat.list, &smack_known_list);
3628 list_add(&smack_known_star.list, &smack_known_list);
3629 list_add(&smack_known_floor.list, &smack_known_list);
3630 list_add(&smack_known_invalid.list, &smack_known_list);
3631 list_add(&smack_known_web.list, &smack_known_list);
3632}
3633
Casey Schauflere114e472008-02-04 22:29:50 -08003634/**
3635 * smack_init - initialize the smack system
3636 *
3637 * Returns 0
3638 */
3639static __init int smack_init(void)
3640{
David Howellsd84f4f92008-11-14 10:39:23 +11003641 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003642 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003643
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003644 if (!security_module_enable(&smack_ops))
3645 return 0;
3646
3647 tsp = new_task_smack(smack_known_floor.smk_known,
3648 smack_known_floor.smk_known, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003649 if (tsp == NULL)
3650 return -ENOMEM;
3651
Casey Schauflere114e472008-02-04 22:29:50 -08003652 printk(KERN_INFO "Smack: Initializing.\n");
3653
3654 /*
3655 * Set the security state for the initial task.
3656 */
David Howellsd84f4f92008-11-14 10:39:23 +11003657 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003658 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003659
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003660 /* initialize the smack_know_list */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003661 init_smack_know_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003662 /*
3663 * Initialize locks
3664 */
Casey Schauflere114e472008-02-04 22:29:50 -08003665 spin_lock_init(&smack_known_huh.smk_cipsolock);
3666 spin_lock_init(&smack_known_hat.smk_cipsolock);
3667 spin_lock_init(&smack_known_star.smk_cipsolock);
3668 spin_lock_init(&smack_known_floor.smk_cipsolock);
3669 spin_lock_init(&smack_known_invalid.smk_cipsolock);
3670
3671 /*
3672 * Register with LSM
3673 */
3674 if (register_security(&smack_ops))
3675 panic("smack: Unable to register with kernel.\n");
3676
3677 return 0;
3678}
3679
3680/*
3681 * Smack requires early initialization in order to label
3682 * all processes and objects when they are created.
3683 */
3684security_initcall(smack_init);