blob: 3669d9f9824e382995f04df65dca1fe91cad10bd [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>
Casey Schauflerc6739442013-05-22 18:42:56 -070030#include <linux/dccp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080032#include <linux/mutex.h>
33#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080034#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070035#include <net/ip.h>
36#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100037#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070038#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050039#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030040#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000041#include <linux/msg.h>
42#include <linux/shm.h>
43#include <linux/binfmts.h>
Casey Schauflere114e472008-02-04 22:29:50 -080044#include "smack.h"
45
David Howellsc69e8d92008-11-14 10:39:19 +110046#define task_security(task) (task_cred_xxx((task), security))
47
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020048#define TRANS_TRUE "TRUE"
49#define TRANS_TRUE_SIZE 4
50
Casey Schauflerc6739442013-05-22 18:42:56 -070051#define SMK_CONNECTING 0
52#define SMK_RECEIVING 1
53#define SMK_SENDING 2
54
55LIST_HEAD(smk_ipv6_port_list);
56
Casey Schauflere114e472008-02-04 22:29:50 -080057/**
58 * smk_fetch - Fetch the smack label from a file.
59 * @ip: a pointer to the inode
60 * @dp: a pointer to the dentry
61 *
62 * Returns a pointer to the master list entry for the Smack label
63 * or NULL if there was no label to fetch.
64 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -070065static struct smack_known *smk_fetch(const char *name, struct inode *ip,
66 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080067{
68 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -070069 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -070070 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -080071
72 if (ip->i_op->getxattr == NULL)
73 return NULL;
74
Casey Schauflerf7112e62012-05-06 15:22:02 -070075 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
76 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -080077 return NULL;
78
Casey Schauflerf7112e62012-05-06 15:22:02 -070079 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
80 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -070081 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -070082
83 kfree(buffer);
84
Casey Schaufler2f823ff2013-05-22 18:43:03 -070085 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -080086}
87
88/**
89 * new_inode_smack - allocate an inode security blob
90 * @smack: a pointer to the Smack label to use in the blob
91 *
92 * Returns the new blob or NULL if there's no memory available
93 */
94struct inode_smack *new_inode_smack(char *smack)
95{
96 struct inode_smack *isp;
97
Tetsuo Handaceffec552012-03-29 16:19:05 +090098 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -080099 if (isp == NULL)
100 return NULL;
101
102 isp->smk_inode = smack;
103 isp->smk_flags = 0;
104 mutex_init(&isp->smk_lock);
105
106 return isp;
107}
108
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800109/**
110 * new_task_smack - allocate a task security blob
111 * @smack: a pointer to the Smack label to use in the blob
112 *
113 * Returns the new blob or NULL if there's no memory available
114 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700115static struct task_smack *new_task_smack(struct smack_known *task,
116 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800117{
118 struct task_smack *tsp;
119
120 tsp = kzalloc(sizeof(struct task_smack), gfp);
121 if (tsp == NULL)
122 return NULL;
123
124 tsp->smk_task = task;
125 tsp->smk_forked = forked;
126 INIT_LIST_HEAD(&tsp->smk_rules);
127 mutex_init(&tsp->smk_rules_lock);
128
129 return tsp;
130}
131
132/**
133 * smk_copy_rules - copy a rule set
134 * @nhead - new rules header pointer
135 * @ohead - old rules header pointer
136 *
137 * Returns 0 on success, -ENOMEM on error
138 */
139static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
140 gfp_t gfp)
141{
142 struct smack_rule *nrp;
143 struct smack_rule *orp;
144 int rc = 0;
145
146 INIT_LIST_HEAD(nhead);
147
148 list_for_each_entry_rcu(orp, ohead, list) {
149 nrp = kzalloc(sizeof(struct smack_rule), gfp);
150 if (nrp == NULL) {
151 rc = -ENOMEM;
152 break;
153 }
154 *nrp = *orp;
155 list_add_rcu(&nrp->list, nhead);
156 }
157 return rc;
158}
159
Casey Schauflere114e472008-02-04 22:29:50 -0800160/*
161 * LSM hooks.
162 * We he, that is fun!
163 */
164
165/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000166 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800167 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -0800168 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -0800169 *
170 * Returns 0 if access is OK, an error code otherwise
171 *
172 * Do the capability checks, and require read and write.
173 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000174static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800175{
176 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200177 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700178 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800179
Ingo Molnar9e488582009-05-07 19:26:19 +1000180 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800181 if (rc != 0)
182 return rc;
183
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700184 skp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
186 smk_ad_setfield_u_tsk(&ad, ctp);
187
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700188 rc = smk_curacc(skp->smk_known, MAY_READWRITE, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100189 return rc;
190}
Casey Schauflere114e472008-02-04 22:29:50 -0800191
David Howells5cd9c582008-08-14 11:37:28 +0100192/**
193 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
194 * @ptp: parent task pointer
195 *
196 * Returns 0 if access is OK, an error code otherwise
197 *
198 * Do the capability checks, and require read and write.
199 */
200static int smack_ptrace_traceme(struct task_struct *ptp)
201{
202 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200203 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700204 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100205
206 rc = cap_ptrace_traceme(ptp);
207 if (rc != 0)
208 return rc;
209
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700210 skp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
212 smk_ad_setfield_u_tsk(&ad, ptp);
213
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700214 rc = smk_curacc(skp->smk_known, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800215 return rc;
216}
217
218/**
219 * smack_syslog - Smack approval on syslog
220 * @type: message type
221 *
222 * Require that the task has the floor label
223 *
224 * Returns 0 on success, error code otherwise.
225 */
Eric Paris12b30522010-11-15 18:36:29 -0500226static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800227{
Eric Paris12b30522010-11-15 18:36:29 -0500228 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700229 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800230
Casey Schaufler1880eff2012-06-05 15:28:30 -0700231 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800232 return 0;
233
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700234 if (skp != &smack_known_floor)
Casey Schauflere114e472008-02-04 22:29:50 -0800235 rc = -EACCES;
236
237 return rc;
238}
239
240
241/*
242 * Superblock Hooks.
243 */
244
245/**
246 * smack_sb_alloc_security - allocate a superblock blob
247 * @sb: the superblock getting the blob
248 *
249 * Returns 0 on success or -ENOMEM on error.
250 */
251static int smack_sb_alloc_security(struct super_block *sb)
252{
253 struct superblock_smack *sbsp;
254
255 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
256
257 if (sbsp == NULL)
258 return -ENOMEM;
259
260 sbsp->smk_root = smack_known_floor.smk_known;
261 sbsp->smk_default = smack_known_floor.smk_known;
262 sbsp->smk_floor = smack_known_floor.smk_known;
263 sbsp->smk_hat = smack_known_hat.smk_known;
264 sbsp->smk_initialized = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800265
266 sb->s_security = sbsp;
267
268 return 0;
269}
270
271/**
272 * smack_sb_free_security - free a superblock blob
273 * @sb: the superblock getting the blob
274 *
275 */
276static void smack_sb_free_security(struct super_block *sb)
277{
278 kfree(sb->s_security);
279 sb->s_security = NULL;
280}
281
282/**
283 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800284 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800285 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800286 *
287 * Returns 0 on success or -ENOMEM on error.
288 *
289 * Copy the Smack specific mount options out of the mount
290 * options list.
291 */
Eric Parise0007522008-03-05 10:31:54 -0500292static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800293{
294 char *cp, *commap, *otheropts, *dp;
295
Casey Schauflere114e472008-02-04 22:29:50 -0800296 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
297 if (otheropts == NULL)
298 return -ENOMEM;
299
300 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
301 if (strstr(cp, SMK_FSDEFAULT) == cp)
302 dp = smackopts;
303 else if (strstr(cp, SMK_FSFLOOR) == cp)
304 dp = smackopts;
305 else if (strstr(cp, SMK_FSHAT) == cp)
306 dp = smackopts;
307 else if (strstr(cp, SMK_FSROOT) == cp)
308 dp = smackopts;
309 else
310 dp = otheropts;
311
312 commap = strchr(cp, ',');
313 if (commap != NULL)
314 *commap = '\0';
315
316 if (*dp != '\0')
317 strcat(dp, ",");
318 strcat(dp, cp);
319 }
320
321 strcpy(orig, otheropts);
322 free_page((unsigned long)otheropts);
323
324 return 0;
325}
326
327/**
328 * smack_sb_kern_mount - Smack specific mount processing
329 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100330 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800331 * @data: the smack mount options
332 *
333 * Returns 0 on success, an error code on failure
334 */
James Morris12204e22008-12-19 10:44:42 +1100335static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800336{
337 struct dentry *root = sb->s_root;
338 struct inode *inode = root->d_inode;
339 struct superblock_smack *sp = sb->s_security;
340 struct inode_smack *isp;
341 char *op;
342 char *commap;
343 char *nsp;
344
Casey Schauflereb982cb2012-05-23 17:46:58 -0700345 if (sp->smk_initialized != 0)
Casey Schauflere114e472008-02-04 22:29:50 -0800346 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700347
Casey Schauflere114e472008-02-04 22:29:50 -0800348 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800349
350 for (op = data; op != NULL; op = commap) {
351 commap = strchr(op, ',');
352 if (commap != NULL)
353 *commap++ = '\0';
354
355 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
356 op += strlen(SMK_FSHAT);
357 nsp = smk_import(op, 0);
358 if (nsp != NULL)
359 sp->smk_hat = nsp;
360 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
361 op += strlen(SMK_FSFLOOR);
362 nsp = smk_import(op, 0);
363 if (nsp != NULL)
364 sp->smk_floor = nsp;
365 } else if (strncmp(op, SMK_FSDEFAULT,
366 strlen(SMK_FSDEFAULT)) == 0) {
367 op += strlen(SMK_FSDEFAULT);
368 nsp = smk_import(op, 0);
369 if (nsp != NULL)
370 sp->smk_default = nsp;
371 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
372 op += strlen(SMK_FSROOT);
373 nsp = smk_import(op, 0);
374 if (nsp != NULL)
375 sp->smk_root = nsp;
376 }
377 }
378
379 /*
380 * Initialize the root inode.
381 */
382 isp = inode->i_security;
383 if (isp == NULL)
384 inode->i_security = new_inode_smack(sp->smk_root);
385 else
386 isp->smk_inode = sp->smk_root;
387
388 return 0;
389}
390
391/**
392 * smack_sb_statfs - Smack check on statfs
393 * @dentry: identifies the file system in question
394 *
395 * Returns 0 if current can read the floor of the filesystem,
396 * and error code otherwise
397 */
398static int smack_sb_statfs(struct dentry *dentry)
399{
400 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200401 int rc;
402 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800403
Eric Parisa2694342011-04-25 13:10:27 -0400404 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200405 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
406
407 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
408 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800409}
410
411/**
412 * smack_sb_mount - Smack check for mounting
413 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800414 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800415 * @type: unused
416 * @flags: unused
417 * @data: unused
418 *
419 * Returns 0 if current can write the floor of the filesystem
420 * being mounted on, an error code otherwise.
421 */
Al Viro808d4e32012-10-11 11:42:01 -0400422static int smack_sb_mount(const char *dev_name, struct path *path,
423 const char *type, unsigned long flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800424{
Al Virod8c95842011-12-07 18:16:57 -0500425 struct superblock_smack *sbp = path->dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200426 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800427
Eric Parisf48b7392011-04-25 12:54:27 -0400428 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200429 smk_ad_setfield_u_fs_path(&ad, *path);
430
431 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800432}
433
434/**
435 * smack_sb_umount - Smack check for unmounting
436 * @mnt: file system to unmount
437 * @flags: unused
438 *
439 * Returns 0 if current can write the floor of the filesystem
440 * being unmounted, an error code otherwise.
441 */
442static int smack_sb_umount(struct vfsmount *mnt, int flags)
443{
444 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200445 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400446 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200447
Eric Parisa2694342011-04-25 13:10:27 -0400448 path.dentry = mnt->mnt_root;
449 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200450
Eric Parisf48b7392011-04-25 12:54:27 -0400451 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400452 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflere114e472008-02-04 22:29:50 -0800453
Al Virod8c95842011-12-07 18:16:57 -0500454 sbp = path.dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200455 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800456}
457
458/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800459 * BPRM hooks
460 */
461
Casey Schauflerce8a4322011-09-29 18:21:01 -0700462/**
463 * smack_bprm_set_creds - set creds for exec
464 * @bprm: the exec information
465 *
466 * Returns 0 if it gets a blob, -ENOMEM otherwise
467 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800468static int smack_bprm_set_creds(struct linux_binprm *bprm)
469{
Al Viro496ad9a2013-01-23 17:07:38 -0500470 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300471 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800472 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800473 int rc;
474
475 rc = cap_bprm_set_creds(bprm);
476 if (rc != 0)
477 return rc;
478
479 if (bprm->cred_prepared)
480 return 0;
481
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300482 isp = inode->i_security;
483 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800484 return 0;
485
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300486 if (bprm->unsafe)
487 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800488
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300489 bsp->smk_task = isp->smk_task;
490 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800491
492 return 0;
493}
494
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300495/**
496 * smack_bprm_committing_creds - Prepare to install the new credentials
497 * from bprm.
498 *
499 * @bprm: binprm for exec
500 */
501static void smack_bprm_committing_creds(struct linux_binprm *bprm)
502{
503 struct task_smack *bsp = bprm->cred->security;
504
505 if (bsp->smk_task != bsp->smk_forked)
506 current->pdeath_signal = 0;
507}
508
509/**
510 * smack_bprm_secureexec - Return the decision to use secureexec.
511 * @bprm: binprm for exec
512 *
513 * Returns 0 on success.
514 */
515static int smack_bprm_secureexec(struct linux_binprm *bprm)
516{
517 struct task_smack *tsp = current_security();
518 int ret = cap_bprm_secureexec(bprm);
519
520 if (!ret && (tsp->smk_task != tsp->smk_forked))
521 ret = 1;
522
523 return ret;
524}
525
Casey Schaufler676dac42010-12-02 06:43:39 -0800526/*
Casey Schauflere114e472008-02-04 22:29:50 -0800527 * Inode hooks
528 */
529
530/**
531 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800532 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800533 *
534 * Returns 0 if it gets a blob, -ENOMEM otherwise
535 */
536static int smack_inode_alloc_security(struct inode *inode)
537{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700538 struct smack_known *skp = smk_of_current();
539
540 inode->i_security = new_inode_smack(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -0800541 if (inode->i_security == NULL)
542 return -ENOMEM;
543 return 0;
544}
545
546/**
547 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800548 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800549 *
550 * Clears the blob pointer in inode
551 */
552static void smack_inode_free_security(struct inode *inode)
553{
554 kfree(inode->i_security);
555 inode->i_security = NULL;
556}
557
558/**
559 * smack_inode_init_security - copy out the smack from an inode
560 * @inode: the inode
561 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500562 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800563 * @name: where to put the attribute name
564 * @value: where to put the attribute value
565 * @len: where to put the length of the attribute
566 *
567 * Returns 0 if it all works out, -ENOMEM if there's no memory
568 */
569static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -0500570 const struct qstr *qstr, char **name,
571 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800572{
Casey Schaufler2267b132012-03-13 19:14:19 -0700573 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700574 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800575 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200576 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800577 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800578
579 if (name) {
Tetsuo Handaceffec552012-03-29 16:19:05 +0900580 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800581 if (*name == NULL)
582 return -ENOMEM;
583 }
584
585 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800586 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700587 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800588 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200589
590 /*
591 * If the access rule allows transmutation and
592 * the directory requests transmutation then
593 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700594 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200595 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800596 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700597 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200598 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700599 issp->smk_flags |= SMK_INODE_CHANGED;
600 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200601
Tetsuo Handaceffec552012-03-29 16:19:05 +0900602 *value = kstrdup(isp, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800603 if (*value == NULL)
604 return -ENOMEM;
605 }
606
607 if (len)
608 *len = strlen(isp) + 1;
609
610 return 0;
611}
612
613/**
614 * smack_inode_link - Smack check on link
615 * @old_dentry: the existing object
616 * @dir: unused
617 * @new_dentry: the new object
618 *
619 * Returns 0 if access is permitted, an error code otherwise
620 */
621static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
622 struct dentry *new_dentry)
623{
Casey Schauflere114e472008-02-04 22:29:50 -0800624 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200625 struct smk_audit_info ad;
626 int rc;
627
Eric Parisa2694342011-04-25 13:10:27 -0400628 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200629 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800630
631 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200632 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800633
634 if (rc == 0 && new_dentry->d_inode != NULL) {
635 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200636 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
637 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800638 }
639
640 return rc;
641}
642
643/**
644 * smack_inode_unlink - Smack check on inode deletion
645 * @dir: containing directory object
646 * @dentry: file to unlink
647 *
648 * Returns 0 if current can write the containing directory
649 * and the object, error code otherwise
650 */
651static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
652{
653 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200654 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800655 int rc;
656
Eric Parisa2694342011-04-25 13:10:27 -0400657 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200658 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
659
Casey Schauflere114e472008-02-04 22:29:50 -0800660 /*
661 * You need write access to the thing you're unlinking
662 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200663 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
664 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800665 /*
666 * You also need write access to the containing directory
667 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400668 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200669 smk_ad_setfield_u_fs_inode(&ad, dir);
670 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
671 }
Casey Schauflere114e472008-02-04 22:29:50 -0800672 return rc;
673}
674
675/**
676 * smack_inode_rmdir - Smack check on directory deletion
677 * @dir: containing directory object
678 * @dentry: directory to unlink
679 *
680 * Returns 0 if current can write the containing directory
681 * and the directory, error code otherwise
682 */
683static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
684{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200685 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800686 int rc;
687
Eric Parisa2694342011-04-25 13:10:27 -0400688 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200689 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
690
Casey Schauflere114e472008-02-04 22:29:50 -0800691 /*
692 * You need write access to the thing you're removing
693 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200694 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
695 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800696 /*
697 * You also need write access to the containing directory
698 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400699 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200700 smk_ad_setfield_u_fs_inode(&ad, dir);
701 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
702 }
Casey Schauflere114e472008-02-04 22:29:50 -0800703
704 return rc;
705}
706
707/**
708 * smack_inode_rename - Smack check on rename
709 * @old_inode: the old directory
710 * @old_dentry: unused
711 * @new_inode: the new directory
712 * @new_dentry: unused
713 *
714 * Read and write access is required on both the old and
715 * new directories.
716 *
717 * Returns 0 if access is permitted, an error code otherwise
718 */
719static int smack_inode_rename(struct inode *old_inode,
720 struct dentry *old_dentry,
721 struct inode *new_inode,
722 struct dentry *new_dentry)
723{
724 int rc;
725 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200726 struct smk_audit_info ad;
727
Eric Parisa2694342011-04-25 13:10:27 -0400728 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200729 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800730
731 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200732 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800733
734 if (rc == 0 && new_dentry->d_inode != NULL) {
735 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200736 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
737 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800738 }
Casey Schauflere114e472008-02-04 22:29:50 -0800739 return rc;
740}
741
742/**
743 * smack_inode_permission - Smack version of permission()
744 * @inode: the inode in question
745 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800746 *
747 * This is the important Smack hook.
748 *
749 * Returns 0 if access is permitted, -EACCES otherwise
750 */
Al Viroe74f71e2011-06-20 19:38:15 -0400751static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800752{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200753 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400754 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400755
756 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800757 /*
758 * No permission to check. Existence test. Yup, it's there.
759 */
760 if (mask == 0)
761 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700762
763 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400764 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700765 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400766 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200767 smk_ad_setfield_u_fs_inode(&ad, inode);
768 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800769}
770
771/**
772 * smack_inode_setattr - Smack check for setting attributes
773 * @dentry: the object
774 * @iattr: for the force flag
775 *
776 * Returns 0 if access is permitted, an error code otherwise
777 */
778static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
779{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200780 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800781 /*
782 * Need to allow for clearing the setuid bit.
783 */
784 if (iattr->ia_valid & ATTR_FORCE)
785 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400786 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200787 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800788
Etienne Bassetecfcc532009-04-08 20:40:06 +0200789 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800790}
791
792/**
793 * smack_inode_getattr - Smack check for getting attributes
794 * @mnt: unused
795 * @dentry: the object
796 *
797 * Returns 0 if access is permitted, an error code otherwise
798 */
799static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
800{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200801 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400802 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200803
Eric Parisa2694342011-04-25 13:10:27 -0400804 path.dentry = dentry;
805 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200806
Eric Parisf48b7392011-04-25 12:54:27 -0400807 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400808 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200809 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800810}
811
812/**
813 * smack_inode_setxattr - Smack check for setting xattrs
814 * @dentry: the object
815 * @name: name of the attribute
816 * @value: unused
817 * @size: unused
818 * @flags: unused
819 *
820 * This protects the Smack attribute explicitly.
821 *
822 * Returns 0 if access is permitted, an error code otherwise
823 */
David Howells8f0cfa52008-04-29 00:59:41 -0700824static int smack_inode_setxattr(struct dentry *dentry, const char *name,
825 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800826{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200827 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800828 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800829
Casey Schauflerbcdca222008-02-23 15:24:04 -0800830 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
831 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800832 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800833 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
834 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700835 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800836 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200837 /*
838 * check label validity here so import wont fail on
839 * post_setxattr
840 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700841 if (size == 0 || size >= SMK_LONGLABEL ||
Etienne Bassetdefc4332009-04-16 23:58:42 +0200842 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400843 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200844 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700845 if (!smack_privileged(CAP_MAC_ADMIN))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200846 rc = -EPERM;
847 if (size != TRANS_TRUE_SIZE ||
848 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
849 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800850 } else
851 rc = cap_inode_setxattr(dentry, name, value, size, flags);
852
Eric Parisa2694342011-04-25 13:10:27 -0400853 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200854 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
855
Casey Schauflerbcdca222008-02-23 15:24:04 -0800856 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200857 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800858
859 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800860}
861
862/**
863 * smack_inode_post_setxattr - Apply the Smack update approved above
864 * @dentry: object
865 * @name: attribute name
866 * @value: attribute value
867 * @size: attribute size
868 * @flags: unused
869 *
870 * Set the pointer in the inode blob to the entry found
871 * in the master label list.
872 */
David Howells8f0cfa52008-04-29 00:59:41 -0700873static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
874 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800875{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700876 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200877 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800878
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700879 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
880 isp->smk_flags |= SMK_INODE_TRANSMUTE;
881 return;
882 }
883
884 skp = smk_import_entry(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800885 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700886 if (skp != NULL)
887 isp->smk_inode = skp->smk_known;
Casey Schaufler676dac42010-12-02 06:43:39 -0800888 else
889 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200890 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700891 if (skp != NULL)
892 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800893 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700894 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800895 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700896 if (skp != NULL)
897 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800898 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700899 isp->smk_mmap = &smack_known_invalid;
900 }
Casey Schauflere114e472008-02-04 22:29:50 -0800901
902 return;
903}
904
Casey Schauflerce8a4322011-09-29 18:21:01 -0700905/**
Casey Schauflere114e472008-02-04 22:29:50 -0800906 * smack_inode_getxattr - Smack check on getxattr
907 * @dentry: the object
908 * @name: unused
909 *
910 * Returns 0 if access is permitted, an error code otherwise
911 */
David Howells8f0cfa52008-04-29 00:59:41 -0700912static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800913{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200914 struct smk_audit_info ad;
915
Eric Parisa2694342011-04-25 13:10:27 -0400916 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200917 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
918
919 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800920}
921
Casey Schauflerce8a4322011-09-29 18:21:01 -0700922/**
Casey Schauflere114e472008-02-04 22:29:50 -0800923 * smack_inode_removexattr - Smack check on removexattr
924 * @dentry: the object
925 * @name: name of the attribute
926 *
927 * Removing the Smack attribute requires CAP_MAC_ADMIN
928 *
929 * Returns 0 if access is permitted, an error code otherwise
930 */
David Howells8f0cfa52008-04-29 00:59:41 -0700931static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800932{
Casey Schaufler676dac42010-12-02 06:43:39 -0800933 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200934 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800935 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800936
Casey Schauflerbcdca222008-02-23 15:24:04 -0800937 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
938 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800939 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200940 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800941 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
942 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700943 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800944 rc = -EPERM;
945 } else
946 rc = cap_inode_removexattr(dentry, name);
947
Eric Parisa2694342011-04-25 13:10:27 -0400948 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200949 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800950 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200951 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800952
Casey Schaufler676dac42010-12-02 06:43:39 -0800953 if (rc == 0) {
954 isp = dentry->d_inode->i_security;
955 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800956 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800957 }
958
Casey Schauflerbcdca222008-02-23 15:24:04 -0800959 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800960}
961
962/**
963 * smack_inode_getsecurity - get smack xattrs
964 * @inode: the object
965 * @name: attribute name
966 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800967 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800968 *
969 * Returns the size of the attribute or an error code
970 */
971static int smack_inode_getsecurity(const struct inode *inode,
972 const char *name, void **buffer,
973 bool alloc)
974{
975 struct socket_smack *ssp;
976 struct socket *sock;
977 struct super_block *sbp;
978 struct inode *ip = (struct inode *)inode;
979 char *isp;
980 int ilen;
981 int rc = 0;
982
983 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
984 isp = smk_of_inode(inode);
985 ilen = strlen(isp) + 1;
986 *buffer = isp;
987 return ilen;
988 }
989
990 /*
991 * The rest of the Smack xattrs are only on sockets.
992 */
993 sbp = ip->i_sb;
994 if (sbp->s_magic != SOCKFS_MAGIC)
995 return -EOPNOTSUPP;
996
997 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800998 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800999 return -EOPNOTSUPP;
1000
1001 ssp = sock->sk->sk_security;
1002
1003 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1004 isp = ssp->smk_in;
1005 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001006 isp = ssp->smk_out->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001007 else
1008 return -EOPNOTSUPP;
1009
1010 ilen = strlen(isp) + 1;
1011 if (rc == 0) {
1012 *buffer = isp;
1013 rc = ilen;
1014 }
1015
1016 return rc;
1017}
1018
1019
1020/**
1021 * smack_inode_listsecurity - list the Smack attributes
1022 * @inode: the object
1023 * @buffer: where they go
1024 * @buffer_size: size of buffer
1025 *
1026 * Returns 0 on success, -EINVAL otherwise
1027 */
1028static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1029 size_t buffer_size)
1030{
1031 int len = strlen(XATTR_NAME_SMACK);
1032
1033 if (buffer != NULL && len <= buffer_size) {
1034 memcpy(buffer, XATTR_NAME_SMACK, len);
1035 return len;
1036 }
1037 return -EINVAL;
1038}
1039
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001040/**
1041 * smack_inode_getsecid - Extract inode's security id
1042 * @inode: inode to extract the info from
1043 * @secid: where result will be saved
1044 */
1045static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1046{
1047 struct inode_smack *isp = inode->i_security;
1048
1049 *secid = smack_to_secid(isp->smk_inode);
1050}
1051
Casey Schauflere114e472008-02-04 22:29:50 -08001052/*
1053 * File Hooks
1054 */
1055
1056/**
1057 * smack_file_permission - Smack check on file operations
1058 * @file: unused
1059 * @mask: unused
1060 *
1061 * Returns 0
1062 *
1063 * Should access checks be done on each read or write?
1064 * UNICOS and SELinux say yes.
1065 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1066 *
1067 * I'll say no for now. Smack does not do the frequent
1068 * label changing that SELinux does.
1069 */
1070static int smack_file_permission(struct file *file, int mask)
1071{
1072 return 0;
1073}
1074
1075/**
1076 * smack_file_alloc_security - assign a file security blob
1077 * @file: the object
1078 *
1079 * The security blob for a file is a pointer to the master
1080 * label list, so no allocation is done.
1081 *
1082 * Returns 0
1083 */
1084static int smack_file_alloc_security(struct file *file)
1085{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001086 struct smack_known *skp = smk_of_current();
1087
1088 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001089 return 0;
1090}
1091
1092/**
1093 * smack_file_free_security - clear a file security blob
1094 * @file: the object
1095 *
1096 * The security blob for a file is a pointer to the master
1097 * label list, so no memory is freed.
1098 */
1099static void smack_file_free_security(struct file *file)
1100{
1101 file->f_security = NULL;
1102}
1103
1104/**
1105 * smack_file_ioctl - Smack check on ioctls
1106 * @file: the object
1107 * @cmd: what to do
1108 * @arg: unused
1109 *
1110 * Relies heavily on the correct use of the ioctl command conventions.
1111 *
1112 * Returns 0 if allowed, error code otherwise
1113 */
1114static int smack_file_ioctl(struct file *file, unsigned int cmd,
1115 unsigned long arg)
1116{
1117 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 struct smk_audit_info ad;
1119
Eric Parisf48b7392011-04-25 12:54:27 -04001120 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001121 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001122
1123 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001124 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001125
1126 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001127 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001128
1129 return rc;
1130}
1131
1132/**
1133 * smack_file_lock - Smack check on file locking
1134 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001135 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001136 *
1137 * Returns 0 if current has write access, error code otherwise
1138 */
1139static int smack_file_lock(struct file *file, unsigned int cmd)
1140{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001141 struct smk_audit_info ad;
1142
Eric Paris92f42502011-04-25 13:15:55 -04001143 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1144 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001146}
1147
1148/**
1149 * smack_file_fcntl - Smack check on fcntl
1150 * @file: the object
1151 * @cmd: what action to check
1152 * @arg: unused
1153 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001154 * Generally these operations are harmless.
1155 * File locking operations present an obvious mechanism
1156 * for passing information, so they require write access.
1157 *
Casey Schauflere114e472008-02-04 22:29:50 -08001158 * Returns 0 if current has access, error code otherwise
1159 */
1160static int smack_file_fcntl(struct file *file, unsigned int cmd,
1161 unsigned long arg)
1162{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001163 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001164 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001165
Etienne Bassetecfcc532009-04-08 20:40:06 +02001166
Casey Schauflere114e472008-02-04 22:29:50 -08001167 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001168 case F_GETLK:
Casey Schauflere114e472008-02-04 22:29:50 -08001169 case F_SETLK:
1170 case F_SETLKW:
1171 case F_SETOWN:
1172 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001173 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1174 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001175 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001176 break;
1177 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001178 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001179 }
1180
1181 return rc;
1182}
1183
1184/**
Al Viroe5467852012-05-30 13:30:51 -04001185 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001186 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1187 * if mapping anonymous memory.
1188 * @file contains the file structure for file to map (may be NULL).
1189 * @reqprot contains the protection requested by the application.
1190 * @prot contains the protection that will be applied by the kernel.
1191 * @flags contains the operational flags.
1192 * Return 0 if permission is granted.
1193 */
Al Viroe5467852012-05-30 13:30:51 -04001194static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001195 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001196 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001197{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001198 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001199 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001200 struct smack_rule *srp;
1201 struct task_smack *tsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001202 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001203 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001204 int may;
1205 int mmay;
1206 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001207 int rc;
1208
Al Viro496ad9a2013-01-23 17:07:38 -05001209 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001210 return 0;
1211
Al Viro496ad9a2013-01-23 17:07:38 -05001212 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001213 if (isp->smk_mmap == NULL)
1214 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001215 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001216
1217 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001218 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001219 rc = 0;
1220
1221 rcu_read_lock();
1222 /*
1223 * For each Smack rule associated with the subject
1224 * label verify that the SMACK64MMAP also has access
1225 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001226 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001227 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001228 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001229 /*
1230 * Matching labels always allows access.
1231 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001232 if (mkp->smk_known == osmack)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001233 continue;
1234 /*
1235 * If there is a matching local rule take
1236 * that into account as well.
1237 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001238 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001239 &tsp->smk_rules);
1240 if (may == -ENOENT)
1241 may = srp->smk_access;
1242 else
1243 may &= srp->smk_access;
1244 /*
1245 * If may is zero the SMACK64MMAP subject can't
1246 * possibly have less access.
1247 */
1248 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001249 continue;
1250
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001251 /*
1252 * Fetch the global list entry.
1253 * If there isn't one a SMACK64MMAP subject
1254 * can't have as much access as current.
1255 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001256 mmay = smk_access_entry(mkp->smk_known, osmack,
1257 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001258 if (mmay == -ENOENT) {
1259 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001260 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001261 }
1262 /*
1263 * If there is a local entry it modifies the
1264 * potential access, too.
1265 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001266 tmay = smk_access_entry(mkp->smk_known, osmack,
1267 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001268 if (tmay != -ENOENT)
1269 mmay &= tmay;
1270
1271 /*
1272 * If there is any access available to current that is
1273 * not available to a SMACK64MMAP subject
1274 * deny access.
1275 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001276 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001277 rc = -EACCES;
1278 break;
1279 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001280 }
1281
1282 rcu_read_unlock();
1283
1284 return rc;
1285}
1286
1287/**
Casey Schauflere114e472008-02-04 22:29:50 -08001288 * smack_file_set_fowner - set the file security blob value
1289 * @file: object in question
1290 *
1291 * Returns 0
1292 * Further research may be required on this one.
1293 */
1294static int smack_file_set_fowner(struct file *file)
1295{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001296 struct smack_known *skp = smk_of_current();
1297
1298 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001299 return 0;
1300}
1301
1302/**
1303 * smack_file_send_sigiotask - Smack on sigio
1304 * @tsk: The target task
1305 * @fown: the object the signal come from
1306 * @signum: unused
1307 *
1308 * Allow a privileged task to get signals even if it shouldn't
1309 *
1310 * Returns 0 if a subject with the object's smack could
1311 * write to the task, an error code otherwise.
1312 */
1313static int smack_file_send_sigiotask(struct task_struct *tsk,
1314 struct fown_struct *fown, int signum)
1315{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001316 struct smack_known *skp;
1317 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001318 struct file *file;
1319 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001320 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001321
1322 /*
1323 * struct fown_struct is never outside the context of a struct file
1324 */
1325 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001326
Etienne Bassetecfcc532009-04-08 20:40:06 +02001327 /* we don't log here as rc can be overriden */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001328 skp = smk_find_entry(file->f_security);
1329 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001330 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001331 rc = 0;
1332
1333 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1334 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001335 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001336 return rc;
1337}
1338
1339/**
1340 * smack_file_receive - Smack file receive check
1341 * @file: the object
1342 *
1343 * Returns 0 if current has access, error code otherwise
1344 */
1345static int smack_file_receive(struct file *file)
1346{
1347 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001348 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001349
Etienne Bassetecfcc532009-04-08 20:40:06 +02001350 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1351 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001352 /*
1353 * This code relies on bitmasks.
1354 */
1355 if (file->f_mode & FMODE_READ)
1356 may = MAY_READ;
1357 if (file->f_mode & FMODE_WRITE)
1358 may |= MAY_WRITE;
1359
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001361}
1362
Casey Schaufler531f1d42011-09-19 12:41:42 -07001363/**
Eric Paris83d49852012-04-04 13:45:40 -04001364 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001365 * @file: the object
1366 * @cred: unused
1367 *
1368 * Set the security blob in the file structure.
1369 *
1370 * Returns 0
1371 */
Eric Paris83d49852012-04-04 13:45:40 -04001372static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001373{
Al Viro496ad9a2013-01-23 17:07:38 -05001374 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001375
1376 file->f_security = isp->smk_inode;
1377
1378 return 0;
1379}
1380
Casey Schauflere114e472008-02-04 22:29:50 -08001381/*
1382 * Task hooks
1383 */
1384
1385/**
David Howellsee18d642009-09-02 09:14:21 +01001386 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1387 * @new: the new credentials
1388 * @gfp: the atomicity of any memory allocations
1389 *
1390 * Prepare a blank set of credentials for modification. This must allocate all
1391 * the memory the LSM module might require such that cred_transfer() can
1392 * complete without error.
1393 */
1394static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1395{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001396 struct task_smack *tsp;
1397
1398 tsp = new_task_smack(NULL, NULL, gfp);
1399 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001400 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001401
1402 cred->security = tsp;
1403
David Howellsee18d642009-09-02 09:14:21 +01001404 return 0;
1405}
1406
1407
1408/**
David Howellsf1752ee2008-11-14 10:39:17 +11001409 * smack_cred_free - "free" task-level security credentials
1410 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001411 *
Casey Schauflere114e472008-02-04 22:29:50 -08001412 */
David Howellsf1752ee2008-11-14 10:39:17 +11001413static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001414{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001415 struct task_smack *tsp = cred->security;
1416 struct smack_rule *rp;
1417 struct list_head *l;
1418 struct list_head *n;
1419
1420 if (tsp == NULL)
1421 return;
1422 cred->security = NULL;
1423
1424 list_for_each_safe(l, n, &tsp->smk_rules) {
1425 rp = list_entry(l, struct smack_rule, list);
1426 list_del(&rp->list);
1427 kfree(rp);
1428 }
1429 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001430}
1431
1432/**
David Howellsd84f4f92008-11-14 10:39:23 +11001433 * smack_cred_prepare - prepare new set of credentials for modification
1434 * @new: the new credentials
1435 * @old: the original credentials
1436 * @gfp: the atomicity of any memory allocations
1437 *
1438 * Prepare a new set of credentials for modification.
1439 */
1440static int smack_cred_prepare(struct cred *new, const struct cred *old,
1441 gfp_t gfp)
1442{
Casey Schaufler676dac42010-12-02 06:43:39 -08001443 struct task_smack *old_tsp = old->security;
1444 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001445 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001446
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001447 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001448 if (new_tsp == NULL)
1449 return -ENOMEM;
1450
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001451 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1452 if (rc != 0)
1453 return rc;
1454
Casey Schaufler676dac42010-12-02 06:43:39 -08001455 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001456 return 0;
1457}
1458
Randy Dunlap251a2a92009-02-18 11:42:33 -08001459/**
David Howellsee18d642009-09-02 09:14:21 +01001460 * smack_cred_transfer - Transfer the old credentials to the new credentials
1461 * @new: the new credentials
1462 * @old: the original credentials
1463 *
1464 * Fill in a set of blank credentials from another set of credentials.
1465 */
1466static void smack_cred_transfer(struct cred *new, const struct cred *old)
1467{
Casey Schaufler676dac42010-12-02 06:43:39 -08001468 struct task_smack *old_tsp = old->security;
1469 struct task_smack *new_tsp = new->security;
1470
1471 new_tsp->smk_task = old_tsp->smk_task;
1472 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001473 mutex_init(&new_tsp->smk_rules_lock);
1474 INIT_LIST_HEAD(&new_tsp->smk_rules);
1475
1476
1477 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001478}
1479
1480/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001481 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001482 * @new: points to the set of credentials to be modified.
1483 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001484 *
1485 * Set the security data for a kernel service.
1486 */
1487static int smack_kernel_act_as(struct cred *new, u32 secid)
1488{
Casey Schaufler676dac42010-12-02 06:43:39 -08001489 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001490 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001491
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001492 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001493 return -EINVAL;
1494
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001495 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001496 return 0;
1497}
1498
1499/**
1500 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001501 * @new: points to the set of credentials to be modified
1502 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001503 *
1504 * Set the file creation context in a set of credentials to the same
1505 * as the objective context of the specified inode
1506 */
1507static int smack_kernel_create_files_as(struct cred *new,
1508 struct inode *inode)
1509{
1510 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001511 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001512
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001513 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1514 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001515 return 0;
1516}
1517
1518/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001519 * smk_curacc_on_task - helper to log task related access
1520 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001521 * @access: the access requested
1522 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001523 *
1524 * Return 0 if access is permitted
1525 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001526static int smk_curacc_on_task(struct task_struct *p, int access,
1527 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001528{
1529 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001530 struct smack_known *skp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001531
Casey Schaufler531f1d42011-09-19 12:41:42 -07001532 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001533 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001534 return smk_curacc(skp->smk_known, access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001535}
1536
1537/**
Casey Schauflere114e472008-02-04 22:29:50 -08001538 * smack_task_setpgid - Smack check on setting pgid
1539 * @p: the task object
1540 * @pgid: unused
1541 *
1542 * Return 0 if write access is permitted
1543 */
1544static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1545{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001546 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001547}
1548
1549/**
1550 * smack_task_getpgid - Smack access check for getpgid
1551 * @p: the object task
1552 *
1553 * Returns 0 if current can read the object task, error code otherwise
1554 */
1555static int smack_task_getpgid(struct task_struct *p)
1556{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001557 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001558}
1559
1560/**
1561 * smack_task_getsid - Smack access check for getsid
1562 * @p: the object task
1563 *
1564 * Returns 0 if current can read the object task, error code otherwise
1565 */
1566static int smack_task_getsid(struct task_struct *p)
1567{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001568 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001569}
1570
1571/**
1572 * smack_task_getsecid - get the secid of the task
1573 * @p: the object task
1574 * @secid: where to put the result
1575 *
1576 * Sets the secid to contain a u32 version of the smack label.
1577 */
1578static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1579{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001580 struct smack_known *skp = smk_of_task(task_security(p));
1581
1582 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001583}
1584
1585/**
1586 * smack_task_setnice - Smack check on setting nice
1587 * @p: the task object
1588 * @nice: unused
1589 *
1590 * Return 0 if write access is permitted
1591 */
1592static int smack_task_setnice(struct task_struct *p, int nice)
1593{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001594 int rc;
1595
1596 rc = cap_task_setnice(p, nice);
1597 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001598 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001599 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001600}
1601
1602/**
1603 * smack_task_setioprio - Smack check on setting ioprio
1604 * @p: the task object
1605 * @ioprio: unused
1606 *
1607 * Return 0 if write access is permitted
1608 */
1609static int smack_task_setioprio(struct task_struct *p, int ioprio)
1610{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001611 int rc;
1612
1613 rc = cap_task_setioprio(p, ioprio);
1614 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001615 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001616 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001617}
1618
1619/**
1620 * smack_task_getioprio - Smack check on reading ioprio
1621 * @p: the task object
1622 *
1623 * Return 0 if read access is permitted
1624 */
1625static int smack_task_getioprio(struct task_struct *p)
1626{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001627 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001628}
1629
1630/**
1631 * smack_task_setscheduler - Smack check on setting scheduler
1632 * @p: the task object
1633 * @policy: unused
1634 * @lp: unused
1635 *
1636 * Return 0 if read access is permitted
1637 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001638static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001639{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001640 int rc;
1641
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001642 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001643 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001644 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001645 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001646}
1647
1648/**
1649 * smack_task_getscheduler - Smack check on reading scheduler
1650 * @p: the task object
1651 *
1652 * Return 0 if read access is permitted
1653 */
1654static int smack_task_getscheduler(struct task_struct *p)
1655{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001656 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001657}
1658
1659/**
1660 * smack_task_movememory - Smack check on moving memory
1661 * @p: the task object
1662 *
1663 * Return 0 if write access is permitted
1664 */
1665static int smack_task_movememory(struct task_struct *p)
1666{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001667 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001668}
1669
1670/**
1671 * smack_task_kill - Smack check on signal delivery
1672 * @p: the task object
1673 * @info: unused
1674 * @sig: unused
1675 * @secid: identifies the smack to use in lieu of current's
1676 *
1677 * Return 0 if write access is permitted
1678 *
1679 * The secid behavior is an artifact of an SELinux hack
1680 * in the USB code. Someday it may go away.
1681 */
1682static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1683 int sig, u32 secid)
1684{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001685 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001686 struct smack_known *skp;
1687 struct smack_known *tkp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001688
1689 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1690 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001691 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001692 * Sending a signal requires that the sender
1693 * can write the receiver.
1694 */
1695 if (secid == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001696 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001697 /*
1698 * If the secid isn't 0 we're dealing with some USB IO
1699 * specific behavior. This is not clean. For one thing
1700 * we can't take privilege into account.
1701 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001702 skp = smack_from_secid(secid);
1703 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001704}
1705
1706/**
1707 * smack_task_wait - Smack access check for waiting
1708 * @p: task to wait for
1709 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001710 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08001711 */
1712static int smack_task_wait(struct task_struct *p)
1713{
Casey Schauflere114e472008-02-04 22:29:50 -08001714 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001715 * Allow the operation to succeed.
1716 * Zombies are bad.
1717 * In userless environments (e.g. phones) programs
1718 * get marked with SMACK64EXEC and even if the parent
1719 * and child shouldn't be talking the parent still
1720 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08001721 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001722 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001723}
1724
1725/**
1726 * smack_task_to_inode - copy task smack into the inode blob
1727 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001728 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001729 *
1730 * Sets the smack pointer in the inode security blob
1731 */
1732static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1733{
1734 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001735 struct smack_known *skp = smk_of_task(task_security(p));
1736
1737 isp->smk_inode = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001738}
1739
1740/*
1741 * Socket hooks.
1742 */
1743
1744/**
1745 * smack_sk_alloc_security - Allocate a socket blob
1746 * @sk: the socket
1747 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001748 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001749 *
1750 * Assign Smack pointers to current
1751 *
1752 * Returns 0 on success, -ENOMEM is there's no memory
1753 */
1754static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1755{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001756 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001757 struct socket_smack *ssp;
1758
1759 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1760 if (ssp == NULL)
1761 return -ENOMEM;
1762
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001763 ssp->smk_in = skp->smk_known;
1764 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001765 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001766
1767 sk->sk_security = ssp;
1768
1769 return 0;
1770}
1771
1772/**
1773 * smack_sk_free_security - Free a socket blob
1774 * @sk: the socket
1775 *
1776 * Clears the blob pointer
1777 */
1778static void smack_sk_free_security(struct sock *sk)
1779{
1780 kfree(sk->sk_security);
1781}
1782
1783/**
Paul Moore07feee82009-03-27 17:10:54 -04001784* smack_host_label - check host based restrictions
1785* @sip: the object end
1786*
1787* looks for host based access restrictions
1788*
1789* This version will only be appropriate for really small sets of single label
1790* hosts. The caller is responsible for ensuring that the RCU read lock is
1791* taken before calling this function.
1792*
1793* Returns the label of the far end or NULL if it's not special.
1794*/
1795static char *smack_host_label(struct sockaddr_in *sip)
1796{
1797 struct smk_netlbladdr *snp;
1798 struct in_addr *siap = &sip->sin_addr;
1799
1800 if (siap->s_addr == 0)
1801 return NULL;
1802
1803 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1804 /*
1805 * we break after finding the first match because
1806 * the list is sorted from longest to shortest mask
1807 * so we have found the most specific match
1808 */
1809 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001810 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1811 /* we have found the special CIPSO option */
1812 if (snp->smk_label == smack_cipso_option)
1813 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001814 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001815 }
Paul Moore07feee82009-03-27 17:10:54 -04001816
1817 return NULL;
1818}
1819
1820/**
Casey Schauflere114e472008-02-04 22:29:50 -08001821 * smack_netlabel - Set the secattr on a socket
1822 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001823 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001824 *
1825 * Convert the outbound smack value (smk_out) to a
1826 * secattr and attach it to the socket.
1827 *
1828 * Returns 0 on success or an error code
1829 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001830static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001831{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001832 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001833 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001834 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001835
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001836 /*
1837 * Usually the netlabel code will handle changing the
1838 * packet labeling based on the label.
1839 * The case of a single label host is different, because
1840 * a single label host should never get a labeled packet
1841 * even though the label is usually associated with a packet
1842 * label.
1843 */
1844 local_bh_disable();
1845 bh_lock_sock_nested(sk);
1846
1847 if (ssp->smk_out == smack_net_ambient ||
1848 labeled == SMACK_UNLABELED_SOCKET)
1849 netlbl_sock_delattr(sk);
1850 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001851 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001852 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001853 }
1854
1855 bh_unlock_sock(sk);
1856 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001857
Casey Schauflere114e472008-02-04 22:29:50 -08001858 return rc;
1859}
1860
1861/**
Paul Moore07feee82009-03-27 17:10:54 -04001862 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1863 * @sk: the socket
1864 * @sap: the destination address
1865 *
1866 * Set the correct secattr for the given socket based on the destination
1867 * address and perform any outbound access checks needed.
1868 *
1869 * Returns 0 on success or an error code.
1870 *
1871 */
1872static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1873{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001874 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001875 int rc;
1876 int sk_lbl;
1877 char *hostsp;
1878 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001879 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001880
1881 rcu_read_lock();
1882 hostsp = smack_host_label(sap);
1883 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001884#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001885 struct lsm_network_audit net;
1886
Eric Paris48c62af2012-04-02 13:15:44 -04001887 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1888 ad.a.u.net->family = sap->sin_family;
1889 ad.a.u.net->dport = sap->sin_port;
1890 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001891#endif
Kees Cook923e9a12012-04-10 13:26:44 -07001892 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001893 skp = ssp->smk_out;
1894 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001895 } else {
1896 sk_lbl = SMACK_CIPSO_SOCKET;
1897 rc = 0;
1898 }
1899 rcu_read_unlock();
1900 if (rc != 0)
1901 return rc;
1902
1903 return smack_netlabel(sk, sk_lbl);
1904}
1905
1906/**
Casey Schauflerc6739442013-05-22 18:42:56 -07001907 * smk_ipv6_port_label - Smack port access table management
1908 * @sock: socket
1909 * @address: address
1910 *
1911 * Create or update the port list entry
1912 */
1913static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1914{
1915 struct sock *sk = sock->sk;
1916 struct sockaddr_in6 *addr6;
1917 struct socket_smack *ssp = sock->sk->sk_security;
1918 struct smk_port_label *spp;
1919 unsigned short port = 0;
1920
1921 if (address == NULL) {
1922 /*
1923 * This operation is changing the Smack information
1924 * on the bound socket. Take the changes to the port
1925 * as well.
1926 */
1927 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1928 if (sk != spp->smk_sock)
1929 continue;
1930 spp->smk_in = ssp->smk_in;
1931 spp->smk_out = ssp->smk_out;
1932 return;
1933 }
1934 /*
1935 * A NULL address is only used for updating existing
1936 * bound entries. If there isn't one, it's OK.
1937 */
1938 return;
1939 }
1940
1941 addr6 = (struct sockaddr_in6 *)address;
1942 port = ntohs(addr6->sin6_port);
1943 /*
1944 * This is a special case that is safely ignored.
1945 */
1946 if (port == 0)
1947 return;
1948
1949 /*
1950 * Look for an existing port list entry.
1951 * This is an indication that a port is getting reused.
1952 */
1953 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1954 if (spp->smk_port != port)
1955 continue;
1956 spp->smk_port = port;
1957 spp->smk_sock = sk;
1958 spp->smk_in = ssp->smk_in;
1959 spp->smk_out = ssp->smk_out;
1960 return;
1961 }
1962
1963 /*
1964 * A new port entry is required.
1965 */
1966 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1967 if (spp == NULL)
1968 return;
1969
1970 spp->smk_port = port;
1971 spp->smk_sock = sk;
1972 spp->smk_in = ssp->smk_in;
1973 spp->smk_out = ssp->smk_out;
1974
1975 list_add(&spp->list, &smk_ipv6_port_list);
1976 return;
1977}
1978
1979/**
1980 * smk_ipv6_port_check - check Smack port access
1981 * @sock: socket
1982 * @address: address
1983 *
1984 * Create or update the port list entry
1985 */
1986static int smk_ipv6_port_check(struct sock *sk, struct sockaddr *address,
1987 int act)
1988{
1989 __be16 *bep;
1990 __be32 *be32p;
1991 struct sockaddr_in6 *addr6;
1992 struct smk_port_label *spp;
1993 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001994 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07001995 unsigned short port = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07001996 char *object;
1997 struct smk_audit_info ad;
1998#ifdef CONFIG_AUDIT
1999 struct lsm_network_audit net;
2000#endif
2001
2002 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002003 skp = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002004 object = ssp->smk_in;
2005 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002006 skp = ssp->smk_out;
2007 object = smack_net_ambient->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002008 }
2009
2010 /*
2011 * Get the IP address and port from the address.
2012 */
2013 addr6 = (struct sockaddr_in6 *)address;
2014 port = ntohs(addr6->sin6_port);
2015 bep = (__be16 *)(&addr6->sin6_addr);
2016 be32p = (__be32 *)(&addr6->sin6_addr);
2017
2018 /*
2019 * It's remote, so port lookup does no good.
2020 */
2021 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2022 goto auditout;
2023
2024 /*
2025 * It's local so the send check has to have passed.
2026 */
2027 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002028 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002029 goto auditout;
2030 }
2031
2032 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2033 if (spp->smk_port != port)
2034 continue;
2035 object = spp->smk_in;
2036 if (act == SMK_CONNECTING)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002037 ssp->smk_packet = spp->smk_out->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002038 break;
2039 }
2040
2041auditout:
2042
2043#ifdef CONFIG_AUDIT
2044 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2045 ad.a.u.net->family = sk->sk_family;
2046 ad.a.u.net->dport = port;
2047 if (act == SMK_RECEIVING)
2048 ad.a.u.net->v6info.saddr = addr6->sin6_addr;
2049 else
2050 ad.a.u.net->v6info.daddr = addr6->sin6_addr;
2051#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002052 return smk_access(skp, object, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07002053}
2054
2055/**
Casey Schauflere114e472008-02-04 22:29:50 -08002056 * smack_inode_setsecurity - set smack xattrs
2057 * @inode: the object
2058 * @name: attribute name
2059 * @value: attribute value
2060 * @size: size of the attribute
2061 * @flags: unused
2062 *
2063 * Sets the named attribute in the appropriate blob
2064 *
2065 * Returns 0 on success, or an error code
2066 */
2067static int smack_inode_setsecurity(struct inode *inode, const char *name,
2068 const void *value, size_t size, int flags)
2069{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002070 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002071 struct inode_smack *nsp = inode->i_security;
2072 struct socket_smack *ssp;
2073 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002074 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002075
Casey Schauflerf7112e62012-05-06 15:22:02 -07002076 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08002077 return -EACCES;
2078
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002079 skp = smk_import_entry(value, size);
2080 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002081 return -EINVAL;
2082
2083 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002084 nsp->smk_inode = skp->smk_known;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002085 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002086 return 0;
2087 }
2088 /*
2089 * The rest of the Smack xattrs are only on sockets.
2090 */
2091 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2092 return -EOPNOTSUPP;
2093
2094 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002095 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002096 return -EOPNOTSUPP;
2097
2098 ssp = sock->sk->sk_security;
2099
2100 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002101 ssp->smk_in = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002102 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002103 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002104 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002105 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2106 if (rc != 0)
2107 printk(KERN_WARNING
2108 "Smack: \"%s\" netlbl error %d.\n",
2109 __func__, -rc);
2110 }
Casey Schauflere114e472008-02-04 22:29:50 -08002111 } else
2112 return -EOPNOTSUPP;
2113
Casey Schauflerc6739442013-05-22 18:42:56 -07002114 if (sock->sk->sk_family == PF_INET6)
2115 smk_ipv6_port_label(sock, NULL);
2116
Casey Schauflere114e472008-02-04 22:29:50 -08002117 return 0;
2118}
2119
2120/**
2121 * smack_socket_post_create - finish socket setup
2122 * @sock: the socket
2123 * @family: protocol family
2124 * @type: unused
2125 * @protocol: unused
2126 * @kern: unused
2127 *
2128 * Sets the netlabel information on the socket
2129 *
2130 * Returns 0 on success, and error code otherwise
2131 */
2132static int smack_socket_post_create(struct socket *sock, int family,
2133 int type, int protocol, int kern)
2134{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002135 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002136 return 0;
2137 /*
2138 * Set the outbound netlbl.
2139 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002140 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2141}
2142
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002143/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002144 * smack_socket_bind - record port binding information.
2145 * @sock: the socket
2146 * @address: the port address
2147 * @addrlen: size of the address
2148 *
2149 * Records the label bound to a port.
2150 *
2151 * Returns 0
2152 */
2153static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2154 int addrlen)
2155{
2156 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2157 smk_ipv6_port_label(sock, address);
2158
2159 return 0;
2160}
2161
2162/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002163 * smack_socket_connect - connect access check
2164 * @sock: the socket
2165 * @sap: the other end
2166 * @addrlen: size of sap
2167 *
2168 * Verifies that a connection may be possible
2169 *
2170 * Returns 0 on success, and error code otherwise
2171 */
2172static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2173 int addrlen)
2174{
Casey Schauflerc6739442013-05-22 18:42:56 -07002175 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002176
Casey Schauflerc6739442013-05-22 18:42:56 -07002177 if (sock->sk == NULL)
2178 return 0;
2179
2180 switch (sock->sk->sk_family) {
2181 case PF_INET:
2182 if (addrlen < sizeof(struct sockaddr_in))
2183 return -EINVAL;
2184 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2185 break;
2186 case PF_INET6:
2187 if (addrlen < sizeof(struct sockaddr_in6))
2188 return -EINVAL;
2189 rc = smk_ipv6_port_check(sock->sk, sap, SMK_CONNECTING);
2190 break;
2191 }
2192 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002193}
2194
2195/**
2196 * smack_flags_to_may - convert S_ to MAY_ values
2197 * @flags: the S_ value
2198 *
2199 * Returns the equivalent MAY_ value
2200 */
2201static int smack_flags_to_may(int flags)
2202{
2203 int may = 0;
2204
2205 if (flags & S_IRUGO)
2206 may |= MAY_READ;
2207 if (flags & S_IWUGO)
2208 may |= MAY_WRITE;
2209 if (flags & S_IXUGO)
2210 may |= MAY_EXEC;
2211
2212 return may;
2213}
2214
2215/**
2216 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2217 * @msg: the object
2218 *
2219 * Returns 0
2220 */
2221static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2222{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002223 struct smack_known *skp = smk_of_current();
2224
2225 msg->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002226 return 0;
2227}
2228
2229/**
2230 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2231 * @msg: the object
2232 *
2233 * Clears the blob pointer
2234 */
2235static void smack_msg_msg_free_security(struct msg_msg *msg)
2236{
2237 msg->security = NULL;
2238}
2239
2240/**
2241 * smack_of_shm - the smack pointer for the shm
2242 * @shp: the object
2243 *
2244 * Returns a pointer to the smack value
2245 */
2246static char *smack_of_shm(struct shmid_kernel *shp)
2247{
2248 return (char *)shp->shm_perm.security;
2249}
2250
2251/**
2252 * smack_shm_alloc_security - Set the security blob for shm
2253 * @shp: the object
2254 *
2255 * Returns 0
2256 */
2257static int smack_shm_alloc_security(struct shmid_kernel *shp)
2258{
2259 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002260 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002261
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002262 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002263 return 0;
2264}
2265
2266/**
2267 * smack_shm_free_security - Clear the security blob for shm
2268 * @shp: the object
2269 *
2270 * Clears the blob pointer
2271 */
2272static void smack_shm_free_security(struct shmid_kernel *shp)
2273{
2274 struct kern_ipc_perm *isp = &shp->shm_perm;
2275
2276 isp->security = NULL;
2277}
2278
2279/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002280 * smk_curacc_shm : check if current has access on shm
2281 * @shp : the object
2282 * @access : access requested
2283 *
2284 * Returns 0 if current has the requested access, error code otherwise
2285 */
2286static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2287{
2288 char *ssp = smack_of_shm(shp);
2289 struct smk_audit_info ad;
2290
2291#ifdef CONFIG_AUDIT
2292 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2293 ad.a.u.ipc_id = shp->shm_perm.id;
2294#endif
2295 return smk_curacc(ssp, access, &ad);
2296}
2297
2298/**
Casey Schauflere114e472008-02-04 22:29:50 -08002299 * smack_shm_associate - Smack access check for shm
2300 * @shp: the object
2301 * @shmflg: access requested
2302 *
2303 * Returns 0 if current has the requested access, error code otherwise
2304 */
2305static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2306{
Casey Schauflere114e472008-02-04 22:29:50 -08002307 int may;
2308
2309 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002310 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002311}
2312
2313/**
2314 * smack_shm_shmctl - Smack access check for shm
2315 * @shp: the object
2316 * @cmd: what it wants to do
2317 *
2318 * Returns 0 if current has the requested access, error code otherwise
2319 */
2320static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2321{
Casey Schauflere114e472008-02-04 22:29:50 -08002322 int may;
2323
2324 switch (cmd) {
2325 case IPC_STAT:
2326 case SHM_STAT:
2327 may = MAY_READ;
2328 break;
2329 case IPC_SET:
2330 case SHM_LOCK:
2331 case SHM_UNLOCK:
2332 case IPC_RMID:
2333 may = MAY_READWRITE;
2334 break;
2335 case IPC_INFO:
2336 case SHM_INFO:
2337 /*
2338 * System level information.
2339 */
2340 return 0;
2341 default:
2342 return -EINVAL;
2343 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002344 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002345}
2346
2347/**
2348 * smack_shm_shmat - Smack access for shmat
2349 * @shp: the object
2350 * @shmaddr: unused
2351 * @shmflg: access requested
2352 *
2353 * Returns 0 if current has the requested access, error code otherwise
2354 */
2355static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2356 int shmflg)
2357{
Casey Schauflere114e472008-02-04 22:29:50 -08002358 int may;
2359
2360 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002361 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002362}
2363
2364/**
2365 * smack_of_sem - the smack pointer for the sem
2366 * @sma: the object
2367 *
2368 * Returns a pointer to the smack value
2369 */
2370static char *smack_of_sem(struct sem_array *sma)
2371{
2372 return (char *)sma->sem_perm.security;
2373}
2374
2375/**
2376 * smack_sem_alloc_security - Set the security blob for sem
2377 * @sma: the object
2378 *
2379 * Returns 0
2380 */
2381static int smack_sem_alloc_security(struct sem_array *sma)
2382{
2383 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002384 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002385
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002386 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002387 return 0;
2388}
2389
2390/**
2391 * smack_sem_free_security - Clear the security blob for sem
2392 * @sma: the object
2393 *
2394 * Clears the blob pointer
2395 */
2396static void smack_sem_free_security(struct sem_array *sma)
2397{
2398 struct kern_ipc_perm *isp = &sma->sem_perm;
2399
2400 isp->security = NULL;
2401}
2402
2403/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002404 * smk_curacc_sem : check if current has access on sem
2405 * @sma : the object
2406 * @access : access requested
2407 *
2408 * Returns 0 if current has the requested access, error code otherwise
2409 */
2410static int smk_curacc_sem(struct sem_array *sma, int access)
2411{
2412 char *ssp = smack_of_sem(sma);
2413 struct smk_audit_info ad;
2414
2415#ifdef CONFIG_AUDIT
2416 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2417 ad.a.u.ipc_id = sma->sem_perm.id;
2418#endif
2419 return smk_curacc(ssp, access, &ad);
2420}
2421
2422/**
Casey Schauflere114e472008-02-04 22:29:50 -08002423 * smack_sem_associate - Smack access check for sem
2424 * @sma: the object
2425 * @semflg: access requested
2426 *
2427 * Returns 0 if current has the requested access, error code otherwise
2428 */
2429static int smack_sem_associate(struct sem_array *sma, int semflg)
2430{
Casey Schauflere114e472008-02-04 22:29:50 -08002431 int may;
2432
2433 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002434 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002435}
2436
2437/**
2438 * smack_sem_shmctl - Smack access check for sem
2439 * @sma: the object
2440 * @cmd: what it wants to do
2441 *
2442 * Returns 0 if current has the requested access, error code otherwise
2443 */
2444static int smack_sem_semctl(struct sem_array *sma, int cmd)
2445{
Casey Schauflere114e472008-02-04 22:29:50 -08002446 int may;
2447
2448 switch (cmd) {
2449 case GETPID:
2450 case GETNCNT:
2451 case GETZCNT:
2452 case GETVAL:
2453 case GETALL:
2454 case IPC_STAT:
2455 case SEM_STAT:
2456 may = MAY_READ;
2457 break;
2458 case SETVAL:
2459 case SETALL:
2460 case IPC_RMID:
2461 case IPC_SET:
2462 may = MAY_READWRITE;
2463 break;
2464 case IPC_INFO:
2465 case SEM_INFO:
2466 /*
2467 * System level information
2468 */
2469 return 0;
2470 default:
2471 return -EINVAL;
2472 }
2473
Etienne Bassetecfcc532009-04-08 20:40:06 +02002474 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002475}
2476
2477/**
2478 * smack_sem_semop - Smack checks of semaphore operations
2479 * @sma: the object
2480 * @sops: unused
2481 * @nsops: unused
2482 * @alter: unused
2483 *
2484 * Treated as read and write in all cases.
2485 *
2486 * Returns 0 if access is allowed, error code otherwise
2487 */
2488static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2489 unsigned nsops, int alter)
2490{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002491 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002492}
2493
2494/**
2495 * smack_msg_alloc_security - Set the security blob for msg
2496 * @msq: the object
2497 *
2498 * Returns 0
2499 */
2500static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2501{
2502 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002503 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002504
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002505 kisp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002506 return 0;
2507}
2508
2509/**
2510 * smack_msg_free_security - Clear the security blob for msg
2511 * @msq: the object
2512 *
2513 * Clears the blob pointer
2514 */
2515static void smack_msg_queue_free_security(struct msg_queue *msq)
2516{
2517 struct kern_ipc_perm *kisp = &msq->q_perm;
2518
2519 kisp->security = NULL;
2520}
2521
2522/**
2523 * smack_of_msq - the smack pointer for the msq
2524 * @msq: the object
2525 *
2526 * Returns a pointer to the smack value
2527 */
2528static char *smack_of_msq(struct msg_queue *msq)
2529{
2530 return (char *)msq->q_perm.security;
2531}
2532
2533/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002534 * smk_curacc_msq : helper to check if current has access on msq
2535 * @msq : the msq
2536 * @access : access requested
2537 *
2538 * return 0 if current has access, error otherwise
2539 */
2540static int smk_curacc_msq(struct msg_queue *msq, int access)
2541{
2542 char *msp = smack_of_msq(msq);
2543 struct smk_audit_info ad;
2544
2545#ifdef CONFIG_AUDIT
2546 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2547 ad.a.u.ipc_id = msq->q_perm.id;
2548#endif
2549 return smk_curacc(msp, access, &ad);
2550}
2551
2552/**
Casey Schauflere114e472008-02-04 22:29:50 -08002553 * smack_msg_queue_associate - Smack access check for msg_queue
2554 * @msq: the object
2555 * @msqflg: access requested
2556 *
2557 * Returns 0 if current has the requested access, error code otherwise
2558 */
2559static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2560{
Casey Schauflere114e472008-02-04 22:29:50 -08002561 int may;
2562
2563 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002564 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002565}
2566
2567/**
2568 * smack_msg_queue_msgctl - Smack access check for msg_queue
2569 * @msq: the object
2570 * @cmd: what it wants to do
2571 *
2572 * Returns 0 if current has the requested access, error code otherwise
2573 */
2574static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2575{
Casey Schauflere114e472008-02-04 22:29:50 -08002576 int may;
2577
2578 switch (cmd) {
2579 case IPC_STAT:
2580 case MSG_STAT:
2581 may = MAY_READ;
2582 break;
2583 case IPC_SET:
2584 case IPC_RMID:
2585 may = MAY_READWRITE;
2586 break;
2587 case IPC_INFO:
2588 case MSG_INFO:
2589 /*
2590 * System level information
2591 */
2592 return 0;
2593 default:
2594 return -EINVAL;
2595 }
2596
Etienne Bassetecfcc532009-04-08 20:40:06 +02002597 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002598}
2599
2600/**
2601 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2602 * @msq: the object
2603 * @msg: unused
2604 * @msqflg: access requested
2605 *
2606 * Returns 0 if current has the requested access, error code otherwise
2607 */
2608static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2609 int msqflg)
2610{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002611 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002612
Etienne Bassetecfcc532009-04-08 20:40:06 +02002613 may = smack_flags_to_may(msqflg);
2614 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002615}
2616
2617/**
2618 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2619 * @msq: the object
2620 * @msg: unused
2621 * @target: unused
2622 * @type: unused
2623 * @mode: unused
2624 *
2625 * Returns 0 if current has read and write access, error code otherwise
2626 */
2627static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2628 struct task_struct *target, long type, int mode)
2629{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002630 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002631}
2632
2633/**
2634 * smack_ipc_permission - Smack access for ipc_permission()
2635 * @ipp: the object permissions
2636 * @flag: access requested
2637 *
2638 * Returns 0 if current has read and write access, error code otherwise
2639 */
2640static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2641{
2642 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002643 int may = smack_flags_to_may(flag);
2644 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002645
Etienne Bassetecfcc532009-04-08 20:40:06 +02002646#ifdef CONFIG_AUDIT
2647 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2648 ad.a.u.ipc_id = ipp->id;
2649#endif
2650 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002651}
2652
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002653/**
2654 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002655 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002656 * @secid: where result will be saved
2657 */
2658static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2659{
2660 char *smack = ipp->security;
2661
2662 *secid = smack_to_secid(smack);
2663}
2664
Casey Schauflere114e472008-02-04 22:29:50 -08002665/**
2666 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002667 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002668 * @inode: the object
2669 *
2670 * Set the inode's security blob if it hasn't been done already.
2671 */
2672static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2673{
2674 struct super_block *sbp;
2675 struct superblock_smack *sbsp;
2676 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002677 struct smack_known *skp;
2678 struct smack_known *ckp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002679 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002680 char trattr[TRANS_TRUE_SIZE];
2681 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002682 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002683 struct dentry *dp;
2684
2685 if (inode == NULL)
2686 return;
2687
2688 isp = inode->i_security;
2689
2690 mutex_lock(&isp->smk_lock);
2691 /*
2692 * If the inode is already instantiated
2693 * take the quick way out
2694 */
2695 if (isp->smk_flags & SMK_INODE_INSTANT)
2696 goto unlockandout;
2697
2698 sbp = inode->i_sb;
2699 sbsp = sbp->s_security;
2700 /*
2701 * We're going to use the superblock default label
2702 * if there's no label on the file.
2703 */
2704 final = sbsp->smk_default;
2705
2706 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002707 * If this is the root inode the superblock
2708 * may be in the process of initialization.
2709 * If that is the case use the root value out
2710 * of the superblock.
2711 */
2712 if (opt_dentry->d_parent == opt_dentry) {
2713 isp->smk_inode = sbsp->smk_root;
2714 isp->smk_flags |= SMK_INODE_INSTANT;
2715 goto unlockandout;
2716 }
2717
2718 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002719 * This is pretty hackish.
2720 * Casey says that we shouldn't have to do
2721 * file system specific code, but it does help
2722 * with keeping it simple.
2723 */
2724 switch (sbp->s_magic) {
2725 case SMACK_MAGIC:
2726 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002727 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002728 * that the smack file system doesn't do
2729 * extended attributes.
2730 */
2731 final = smack_known_star.smk_known;
2732 break;
2733 case PIPEFS_MAGIC:
2734 /*
2735 * Casey says pipes are easy (?)
2736 */
2737 final = smack_known_star.smk_known;
2738 break;
2739 case DEVPTS_SUPER_MAGIC:
2740 /*
2741 * devpts seems content with the label of the task.
2742 * Programs that change smack have to treat the
2743 * pty with respect.
2744 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002745 final = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002746 break;
2747 case SOCKFS_MAGIC:
2748 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002749 * Socket access is controlled by the socket
2750 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002751 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002752 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002753 break;
2754 case PROC_SUPER_MAGIC:
2755 /*
2756 * Casey says procfs appears not to care.
2757 * The superblock default suffices.
2758 */
2759 break;
2760 case TMPFS_MAGIC:
2761 /*
2762 * Device labels should come from the filesystem,
2763 * but watch out, because they're volitile,
2764 * getting recreated on every reboot.
2765 */
2766 final = smack_known_star.smk_known;
2767 /*
2768 * No break.
2769 *
2770 * If a smack value has been set we want to use it,
2771 * but since tmpfs isn't giving us the opportunity
2772 * to set mount options simulate setting the
2773 * superblock default.
2774 */
2775 default:
2776 /*
2777 * This isn't an understood special case.
2778 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002779 */
2780
2781 /*
2782 * UNIX domain sockets use lower level socket data.
2783 */
2784 if (S_ISSOCK(inode->i_mode)) {
2785 final = smack_known_star.smk_known;
2786 break;
2787 }
2788 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002789 * No xattr support means, alas, no SMACK label.
2790 * Use the aforeapplied default.
2791 * It would be curious if the label of the task
2792 * does not match that assigned.
2793 */
2794 if (inode->i_op->getxattr == NULL)
2795 break;
2796 /*
2797 * Get the dentry for xattr.
2798 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002799 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002800 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2801 if (skp != NULL)
2802 final = skp->smk_known;
Casey Schaufler2267b132012-03-13 19:14:19 -07002803
2804 /*
2805 * Transmuting directory
2806 */
2807 if (S_ISDIR(inode->i_mode)) {
2808 /*
2809 * If this is a new directory and the label was
2810 * transmuted when the inode was initialized
2811 * set the transmute attribute on the directory
2812 * and mark the inode.
2813 *
2814 * If there is a transmute attribute on the
2815 * directory mark the inode.
2816 */
2817 if (isp->smk_flags & SMK_INODE_CHANGED) {
2818 isp->smk_flags &= ~SMK_INODE_CHANGED;
2819 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002820 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002821 TRANS_TRUE, TRANS_TRUE_SIZE,
2822 0);
2823 } else {
2824 rc = inode->i_op->getxattr(dp,
2825 XATTR_NAME_SMACKTRANSMUTE, trattr,
2826 TRANS_TRUE_SIZE);
2827 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2828 TRANS_TRUE_SIZE) != 0)
2829 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002830 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002831 if (rc >= 0)
2832 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002833 }
2834 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002835 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002836
Casey Schauflere114e472008-02-04 22:29:50 -08002837 dput(dp);
2838 break;
2839 }
2840
2841 if (final == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002842 isp->smk_inode = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002843 else
2844 isp->smk_inode = final;
2845
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002846 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002847
2848unlockandout:
2849 mutex_unlock(&isp->smk_lock);
2850 return;
2851}
2852
2853/**
2854 * smack_getprocattr - Smack process attribute access
2855 * @p: the object task
2856 * @name: the name of the attribute in /proc/.../attr
2857 * @value: where to put the result
2858 *
2859 * Places a copy of the task Smack into value
2860 *
2861 * Returns the length of the smack label or an error code
2862 */
2863static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2864{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002865 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08002866 char *cp;
2867 int slen;
2868
2869 if (strcmp(name, "current") != 0)
2870 return -EINVAL;
2871
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002872 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002873 if (cp == NULL)
2874 return -ENOMEM;
2875
2876 slen = strlen(cp);
2877 *value = cp;
2878 return slen;
2879}
2880
2881/**
2882 * smack_setprocattr - Smack process attribute setting
2883 * @p: the object task
2884 * @name: the name of the attribute in /proc/.../attr
2885 * @value: the value to set
2886 * @size: the size of the value
2887 *
2888 * Sets the Smack value of the task. Only setting self
2889 * is permitted and only with privilege
2890 *
2891 * Returns the length of the smack label or an error code
2892 */
2893static int smack_setprocattr(struct task_struct *p, char *name,
2894 void *value, size_t size)
2895{
Casey Schaufler676dac42010-12-02 06:43:39 -08002896 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002897 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002898 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002899
Casey Schauflere114e472008-02-04 22:29:50 -08002900 /*
2901 * Changing another process' Smack value is too dangerous
2902 * and supports no sane use case.
2903 */
2904 if (p != current)
2905 return -EPERM;
2906
Casey Schaufler1880eff2012-06-05 15:28:30 -07002907 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01002908 return -EPERM;
2909
Casey Schauflerf7112e62012-05-06 15:22:02 -07002910 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08002911 return -EINVAL;
2912
2913 if (strcmp(name, "current") != 0)
2914 return -EINVAL;
2915
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002916 skp = smk_import_entry(value, size);
2917 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002918 return -EINVAL;
2919
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002920 /*
2921 * No process is ever allowed the web ("@") label.
2922 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002923 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002924 return -EPERM;
2925
David Howellsd84f4f92008-11-14 10:39:23 +11002926 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002927 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002928 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002929
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07002930 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002931 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002932
David Howellsd84f4f92008-11-14 10:39:23 +11002933 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002934 return size;
2935}
2936
2937/**
2938 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002939 * @sock: one sock
2940 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002941 * @newsk: unused
2942 *
2943 * Return 0 if a subject with the smack of sock could access
2944 * an object with the smack of other, otherwise an error code
2945 */
David S. Miller3610cda2011-01-05 15:38:53 -08002946static int smack_unix_stream_connect(struct sock *sock,
2947 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002948{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002949 struct smack_known *skp;
James Morrisd2e7ad12011-01-10 09:46:24 +11002950 struct socket_smack *ssp = sock->sk_security;
2951 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002952 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002953 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002954 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002955
Kees Cook923e9a12012-04-10 13:26:44 -07002956#ifdef CONFIG_AUDIT
2957 struct lsm_network_audit net;
2958
Eric Paris48c62af2012-04-02 13:15:44 -04002959 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
David S. Miller3610cda2011-01-05 15:38:53 -08002960 smk_ad_setfield_u_net_sk(&ad, other);
Kees Cook923e9a12012-04-10 13:26:44 -07002961#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002962
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002963 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2964 skp = ssp->smk_out;
2965 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2966 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002967
Casey Schaufler975d5e52011-09-26 14:43:39 -07002968 /*
2969 * Cross reference the peer labels for SO_PEERSEC.
2970 */
2971 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002972 nsp->smk_packet = ssp->smk_out->smk_known;
2973 ssp->smk_packet = osp->smk_out->smk_known;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002974 }
2975
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002976 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002977}
2978
2979/**
2980 * smack_unix_may_send - Smack access on UDS
2981 * @sock: one socket
2982 * @other: the other socket
2983 *
2984 * Return 0 if a subject with the smack of sock could access
2985 * an object with the smack of other, otherwise an error code
2986 */
2987static int smack_unix_may_send(struct socket *sock, struct socket *other)
2988{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002989 struct socket_smack *ssp = sock->sk->sk_security;
2990 struct socket_smack *osp = other->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002991 struct smack_known *skp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002992 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002993
Kees Cook923e9a12012-04-10 13:26:44 -07002994#ifdef CONFIG_AUDIT
2995 struct lsm_network_audit net;
2996
Eric Paris48c62af2012-04-02 13:15:44 -04002997 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002998 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07002999#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003000
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003001 if (smack_privileged(CAP_MAC_OVERRIDE))
3002 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003003
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003004 skp = ssp->smk_out;
3005 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003006}
3007
3008/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003009 * smack_socket_sendmsg - Smack check based on destination host
3010 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003011 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003012 * @size: the size of the message
3013 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003014 * Return 0 if the current subject can write to the destination host.
3015 * For IPv4 this is only a question if the destination is a single label host.
3016 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003017 */
3018static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3019 int size)
3020{
3021 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003022 struct sockaddr *sap = (struct sockaddr *) msg->msg_name;
3023 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003024
3025 /*
3026 * Perfectly reasonable for this to be NULL
3027 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003028 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003029 return 0;
3030
Casey Schauflerc6739442013-05-22 18:42:56 -07003031 switch (sip->sin_family) {
3032 case AF_INET:
3033 rc = smack_netlabel_send(sock->sk, sip);
3034 break;
3035 case AF_INET6:
3036 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3037 break;
3038 }
3039 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003040}
3041
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003042/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003043 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003044 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003045 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003046 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003047 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003048 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003049static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3050 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003051{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003052 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003053 int found = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08003054
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003055 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003056 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003057 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003058 * If there are flags but no level netlabel isn't
3059 * behaving the way we expect it to.
3060 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003061 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003062 * Without guidance regarding the smack value
3063 * for the packet fall back on the network
3064 * ambient value.
3065 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003066 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003067 list_for_each_entry(skp, &smack_known_list, list) {
3068 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003069 continue;
3070 if (memcmp(sap->attr.mls.cat,
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003071 skp->smk_netlabel.attr.mls.cat,
Casey Schauflerf7112e62012-05-06 15:22:02 -07003072 SMK_CIPSOLEN) != 0)
3073 continue;
3074 found = 1;
3075 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003076 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003077 rcu_read_unlock();
3078
3079 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003080 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003081
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003082 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003083 return &smack_known_web;
3084 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003085 }
3086 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3087 /*
3088 * Looks like a fallback, which gives us a secid.
3089 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003090 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003091 /*
3092 * This has got to be a bug because it is
3093 * impossible to specify a fallback without
3094 * specifying the label, which will ensure
3095 * it has a secid, and the only way to get a
3096 * secid is from a fallback.
3097 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003098 BUG_ON(skp == NULL);
3099 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003100 }
3101 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003102 * Without guidance regarding the smack value
3103 * for the packet fall back on the network
3104 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003105 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003106 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003107}
3108
Casey Schauflerc6739442013-05-22 18:42:56 -07003109static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr *sap)
3110{
3111 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
3112 u8 nexthdr;
3113 int offset;
3114 int proto = -EINVAL;
3115 struct ipv6hdr _ipv6h;
3116 struct ipv6hdr *ip6;
3117 __be16 frag_off;
3118 struct tcphdr _tcph, *th;
3119 struct udphdr _udph, *uh;
3120 struct dccp_hdr _dccph, *dh;
3121
3122 sip->sin6_port = 0;
3123
3124 offset = skb_network_offset(skb);
3125 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3126 if (ip6 == NULL)
3127 return -EINVAL;
3128 sip->sin6_addr = ip6->saddr;
3129
3130 nexthdr = ip6->nexthdr;
3131 offset += sizeof(_ipv6h);
3132 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3133 if (offset < 0)
3134 return -EINVAL;
3135
3136 proto = nexthdr;
3137 switch (proto) {
3138 case IPPROTO_TCP:
3139 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3140 if (th != NULL)
3141 sip->sin6_port = th->source;
3142 break;
3143 case IPPROTO_UDP:
3144 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3145 if (uh != NULL)
3146 sip->sin6_port = uh->source;
3147 break;
3148 case IPPROTO_DCCP:
3149 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3150 if (dh != NULL)
3151 sip->sin6_port = dh->dccph_sport;
3152 break;
3153 }
3154 return proto;
3155}
3156
Casey Schauflere114e472008-02-04 22:29:50 -08003157/**
3158 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3159 * @sk: socket
3160 * @skb: packet
3161 *
3162 * Returns 0 if the packet should be delivered, an error code otherwise
3163 */
3164static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3165{
3166 struct netlbl_lsm_secattr secattr;
3167 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003168 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07003169 struct sockaddr sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003170 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003171 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003172#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003173 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003174#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003175 switch (sk->sk_family) {
3176 case PF_INET:
3177 /*
3178 * Translate what netlabel gave us.
3179 */
3180 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003181
Casey Schauflerc6739442013-05-22 18:42:56 -07003182 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3183 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003184 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003185 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003186 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003187
Casey Schauflerc6739442013-05-22 18:42:56 -07003188 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003189
Etienne Bassetecfcc532009-04-08 20:40:06 +02003190#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003191 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3192 ad.a.u.net->family = sk->sk_family;
3193 ad.a.u.net->netif = skb->skb_iif;
3194 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003195#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003196 /*
3197 * Receiving a packet requires that the other end
3198 * be able to write here. Read access is not required.
3199 * This is the simplist possible security model
3200 * for networking.
3201 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003202 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07003203 if (rc != 0)
3204 netlbl_skbuff_err(skb, rc, 0);
3205 break;
3206 case PF_INET6:
3207 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3208 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3209 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3210 else
3211 rc = 0;
3212 break;
3213 }
Paul Moorea8134292008-10-10 10:16:31 -04003214 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003215}
3216
3217/**
3218 * smack_socket_getpeersec_stream - pull in packet label
3219 * @sock: the socket
3220 * @optval: user's destination
3221 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003222 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003223 *
3224 * returns zero on success, an error code otherwise
3225 */
3226static int smack_socket_getpeersec_stream(struct socket *sock,
3227 char __user *optval,
3228 int __user *optlen, unsigned len)
3229{
3230 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003231 char *rcp = "";
3232 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003233 int rc = 0;
3234
3235 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003236 if (ssp->smk_packet != NULL) {
3237 rcp = ssp->smk_packet;
3238 slen = strlen(rcp) + 1;
3239 }
Casey Schauflere114e472008-02-04 22:29:50 -08003240
3241 if (slen > len)
3242 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003243 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003244 rc = -EFAULT;
3245
3246 if (put_user(slen, optlen) != 0)
3247 rc = -EFAULT;
3248
3249 return rc;
3250}
3251
3252
3253/**
3254 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003255 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003256 * @skb: packet data
3257 * @secid: pointer to where to put the secid of the packet
3258 *
3259 * Sets the netlabel socket state on sk from parent
3260 */
3261static int smack_socket_getpeersec_dgram(struct socket *sock,
3262 struct sk_buff *skb, u32 *secid)
3263
3264{
3265 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003266 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003267 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003268 int family = PF_UNSPEC;
3269 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003270 int rc;
3271
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003272 if (skb != NULL) {
3273 if (skb->protocol == htons(ETH_P_IP))
3274 family = PF_INET;
3275 else if (skb->protocol == htons(ETH_P_IPV6))
3276 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003277 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003278 if (family == PF_UNSPEC && sock != NULL)
3279 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003280
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003281 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003282 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003283 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003284 } else if (family == PF_INET || family == PF_INET6) {
3285 /*
3286 * Translate what netlabel gave us.
3287 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003288 if (sock != NULL && sock->sk != NULL)
3289 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003290 netlbl_secattr_init(&secattr);
3291 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3292 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003293 skp = smack_from_secattr(&secattr, ssp);
3294 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003295 }
3296 netlbl_secattr_destroy(&secattr);
3297 }
3298 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003299 if (s == 0)
3300 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003301 return 0;
3302}
3303
3304/**
Paul Moore07feee82009-03-27 17:10:54 -04003305 * smack_sock_graft - Initialize a newly created socket with an existing sock
3306 * @sk: child sock
3307 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003308 *
Paul Moore07feee82009-03-27 17:10:54 -04003309 * Set the smk_{in,out} state of an existing sock based on the process that
3310 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003311 */
3312static void smack_sock_graft(struct sock *sk, struct socket *parent)
3313{
3314 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003315 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003316
Paul Moore07feee82009-03-27 17:10:54 -04003317 if (sk == NULL ||
3318 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003319 return;
3320
3321 ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003322 ssp->smk_in = skp->smk_known;
3323 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003324 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003325}
3326
3327/**
3328 * smack_inet_conn_request - Smack access check on connect
3329 * @sk: socket involved
3330 * @skb: packet
3331 * @req: unused
3332 *
3333 * Returns 0 if a task with the packet label could write to
3334 * the socket, otherwise an error code
3335 */
3336static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3337 struct request_sock *req)
3338{
Paul Moore07feee82009-03-27 17:10:54 -04003339 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003340 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003341 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003342 struct netlbl_lsm_secattr secattr;
3343 struct sockaddr_in addr;
3344 struct iphdr *hdr;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003345 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003346 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003347 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003348#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003349 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003350#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003351
Casey Schauflerc6739442013-05-22 18:42:56 -07003352 if (family == PF_INET6) {
3353 /*
3354 * Handle mapped IPv4 packets arriving
3355 * via IPv6 sockets. Don't set up netlabel
3356 * processing on IPv6.
3357 */
3358 if (skb->protocol == htons(ETH_P_IP))
3359 family = PF_INET;
3360 else
3361 return 0;
3362 }
Casey Schauflere114e472008-02-04 22:29:50 -08003363
Paul Moore07feee82009-03-27 17:10:54 -04003364 netlbl_secattr_init(&secattr);
3365 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003366 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003367 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003368 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003369 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003370 netlbl_secattr_destroy(&secattr);
3371
Etienne Bassetecfcc532009-04-08 20:40:06 +02003372#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003373 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3374 ad.a.u.net->family = family;
3375 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003376 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3377#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003378 /*
Paul Moore07feee82009-03-27 17:10:54 -04003379 * Receiving a packet requires that the other end be able to write
3380 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003381 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003382 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003383 if (rc != 0)
3384 return rc;
3385
3386 /*
3387 * Save the peer's label in the request_sock so we can later setup
3388 * smk_packet in the child socket so that SO_PEERCRED can report it.
3389 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003390 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003391
3392 /*
3393 * We need to decide if we want to label the incoming connection here
3394 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003395 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003396 */
3397 hdr = ip_hdr(skb);
3398 addr.sin_addr.s_addr = hdr->saddr;
3399 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003400 hsp = smack_host_label(&addr);
3401 rcu_read_unlock();
3402
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003403 if (hsp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003404 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003405 else
Paul Moore07feee82009-03-27 17:10:54 -04003406 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003407
3408 return rc;
3409}
3410
Paul Moore07feee82009-03-27 17:10:54 -04003411/**
3412 * smack_inet_csk_clone - Copy the connection information to the new socket
3413 * @sk: the new socket
3414 * @req: the connection's request_sock
3415 *
3416 * Transfer the connection's peer label to the newly created socket.
3417 */
3418static void smack_inet_csk_clone(struct sock *sk,
3419 const struct request_sock *req)
3420{
3421 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003422 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003423
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003424 if (req->peer_secid != 0) {
3425 skp = smack_from_secid(req->peer_secid);
3426 ssp->smk_packet = skp->smk_known;
3427 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003428 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003429}
3430
Casey Schauflere114e472008-02-04 22:29:50 -08003431/*
3432 * Key management security hooks
3433 *
3434 * Casey has not tested key support very heavily.
3435 * The permission check is most likely too restrictive.
3436 * If you care about keys please have a look.
3437 */
3438#ifdef CONFIG_KEYS
3439
3440/**
3441 * smack_key_alloc - Set the key security blob
3442 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003443 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003444 * @flags: unused
3445 *
3446 * No allocation required
3447 *
3448 * Returns 0
3449 */
David Howellsd84f4f92008-11-14 10:39:23 +11003450static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003451 unsigned long flags)
3452{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003453 struct smack_known *skp = smk_of_task(cred->security);
3454
3455 key->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003456 return 0;
3457}
3458
3459/**
3460 * smack_key_free - Clear the key security blob
3461 * @key: the object
3462 *
3463 * Clear the blob pointer
3464 */
3465static void smack_key_free(struct key *key)
3466{
3467 key->security = NULL;
3468}
3469
3470/*
3471 * smack_key_permission - Smack access on a key
3472 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003473 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003474 * @perm: unused
3475 *
3476 * Return 0 if the task has read and write to the object,
3477 * an error code otherwise
3478 */
3479static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003480 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003481{
3482 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003483 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003484 struct smack_known *tkp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003485
3486 keyp = key_ref_to_ptr(key_ref);
3487 if (keyp == NULL)
3488 return -EINVAL;
3489 /*
3490 * If the key hasn't been initialized give it access so that
3491 * it may do so.
3492 */
3493 if (keyp->security == NULL)
3494 return 0;
3495 /*
3496 * This should not occur
3497 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003498 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003499 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003500#ifdef CONFIG_AUDIT
3501 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3502 ad.a.u.key_struct.key = keyp->serial;
3503 ad.a.u.key_struct.key_desc = keyp->description;
3504#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003505 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003506}
3507#endif /* CONFIG_KEYS */
3508
3509/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003510 * Smack Audit hooks
3511 *
3512 * Audit requires a unique representation of each Smack specific
3513 * rule. This unique representation is used to distinguish the
3514 * object to be audited from remaining kernel objects and also
3515 * works as a glue between the audit hooks.
3516 *
3517 * Since repository entries are added but never deleted, we'll use
3518 * the smack_known label address related to the given audit rule as
3519 * the needed unique representation. This also better fits the smack
3520 * model where nearly everything is a label.
3521 */
3522#ifdef CONFIG_AUDIT
3523
3524/**
3525 * smack_audit_rule_init - Initialize a smack audit rule
3526 * @field: audit rule fields given from user-space (audit.h)
3527 * @op: required testing operator (=, !=, >, <, ...)
3528 * @rulestr: smack label to be audited
3529 * @vrule: pointer to save our own audit rule representation
3530 *
3531 * Prepare to audit cases where (@field @op @rulestr) is true.
3532 * The label to be audited is created if necessay.
3533 */
3534static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3535{
3536 char **rule = (char **)vrule;
3537 *rule = NULL;
3538
3539 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3540 return -EINVAL;
3541
Al Viro5af75d82008-12-16 05:59:26 -05003542 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003543 return -EINVAL;
3544
3545 *rule = smk_import(rulestr, 0);
3546
3547 return 0;
3548}
3549
3550/**
3551 * smack_audit_rule_known - Distinguish Smack audit rules
3552 * @krule: rule of interest, in Audit kernel representation format
3553 *
3554 * This is used to filter Smack rules from remaining Audit ones.
3555 * If it's proved that this rule belongs to us, the
3556 * audit_rule_match hook will be called to do the final judgement.
3557 */
3558static int smack_audit_rule_known(struct audit_krule *krule)
3559{
3560 struct audit_field *f;
3561 int i;
3562
3563 for (i = 0; i < krule->field_count; i++) {
3564 f = &krule->fields[i];
3565
3566 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3567 return 1;
3568 }
3569
3570 return 0;
3571}
3572
3573/**
3574 * smack_audit_rule_match - Audit given object ?
3575 * @secid: security id for identifying the object to test
3576 * @field: audit rule flags given from user-space
3577 * @op: required testing operator
3578 * @vrule: smack internal rule presentation
3579 * @actx: audit context associated with the check
3580 *
3581 * The core Audit hook. It's used to take the decision of
3582 * whether to audit or not to audit a given object.
3583 */
3584static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3585 struct audit_context *actx)
3586{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003587 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003588 char *rule = vrule;
3589
3590 if (!rule) {
Tetsuo Handaceffec552012-03-29 16:19:05 +09003591 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003592 "Smack: missing rule\n");
3593 return -ENOENT;
3594 }
3595
3596 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3597 return 0;
3598
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003599 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003600
3601 /*
3602 * No need to do string comparisons. If a match occurs,
3603 * both pointers will point to the same smack_known
3604 * label.
3605 */
Al Viro5af75d82008-12-16 05:59:26 -05003606 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003607 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003608 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003609 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003610
3611 return 0;
3612}
3613
3614/**
3615 * smack_audit_rule_free - free smack rule representation
3616 * @vrule: rule to be freed.
3617 *
3618 * No memory was allocated.
3619 */
3620static void smack_audit_rule_free(void *vrule)
3621{
3622 /* No-op */
3623}
3624
3625#endif /* CONFIG_AUDIT */
3626
Randy Dunlap251a2a92009-02-18 11:42:33 -08003627/**
Casey Schauflere114e472008-02-04 22:29:50 -08003628 * smack_secid_to_secctx - return the smack label for a secid
3629 * @secid: incoming integer
3630 * @secdata: destination
3631 * @seclen: how long it is
3632 *
3633 * Exists for networking code.
3634 */
3635static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3636{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003637 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003638
Eric Parisd5630b92010-10-13 16:24:48 -04003639 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003640 *secdata = skp->smk_known;
3641 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08003642 return 0;
3643}
3644
Randy Dunlap251a2a92009-02-18 11:42:33 -08003645/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003646 * smack_secctx_to_secid - return the secid for a smack label
3647 * @secdata: smack label
3648 * @seclen: how long result is
3649 * @secid: outgoing integer
3650 *
3651 * Exists for audit and networking code.
3652 */
David Howellse52c17642008-04-29 20:52:51 +01003653static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003654{
3655 *secid = smack_to_secid(secdata);
3656 return 0;
3657}
3658
Randy Dunlap251a2a92009-02-18 11:42:33 -08003659/**
Casey Schauflere114e472008-02-04 22:29:50 -08003660 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003661 * @secdata: unused
3662 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003663 *
3664 * Exists to make sure nothing gets done, and properly
3665 */
3666static void smack_release_secctx(char *secdata, u32 seclen)
3667{
3668}
3669
David P. Quigley1ee65e32009-09-03 14:25:57 -04003670static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3671{
3672 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3673}
3674
3675static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3676{
3677 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3678}
3679
3680static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3681{
3682 int len = 0;
3683 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3684
3685 if (len < 0)
3686 return len;
3687 *ctxlen = len;
3688 return 0;
3689}
3690
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003691struct security_operations smack_ops = {
3692 .name = "smack",
3693
Ingo Molnar9e488582009-05-07 19:26:19 +10003694 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003695 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003696 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003697
3698 .sb_alloc_security = smack_sb_alloc_security,
3699 .sb_free_security = smack_sb_free_security,
3700 .sb_copy_data = smack_sb_copy_data,
3701 .sb_kern_mount = smack_sb_kern_mount,
3702 .sb_statfs = smack_sb_statfs,
3703 .sb_mount = smack_sb_mount,
3704 .sb_umount = smack_sb_umount,
3705
Casey Schaufler676dac42010-12-02 06:43:39 -08003706 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003707 .bprm_committing_creds = smack_bprm_committing_creds,
3708 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003709
Casey Schauflere114e472008-02-04 22:29:50 -08003710 .inode_alloc_security = smack_inode_alloc_security,
3711 .inode_free_security = smack_inode_free_security,
3712 .inode_init_security = smack_inode_init_security,
3713 .inode_link = smack_inode_link,
3714 .inode_unlink = smack_inode_unlink,
3715 .inode_rmdir = smack_inode_rmdir,
3716 .inode_rename = smack_inode_rename,
3717 .inode_permission = smack_inode_permission,
3718 .inode_setattr = smack_inode_setattr,
3719 .inode_getattr = smack_inode_getattr,
3720 .inode_setxattr = smack_inode_setxattr,
3721 .inode_post_setxattr = smack_inode_post_setxattr,
3722 .inode_getxattr = smack_inode_getxattr,
3723 .inode_removexattr = smack_inode_removexattr,
3724 .inode_getsecurity = smack_inode_getsecurity,
3725 .inode_setsecurity = smack_inode_setsecurity,
3726 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003727 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003728
3729 .file_permission = smack_file_permission,
3730 .file_alloc_security = smack_file_alloc_security,
3731 .file_free_security = smack_file_free_security,
3732 .file_ioctl = smack_file_ioctl,
3733 .file_lock = smack_file_lock,
3734 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003735 .mmap_file = smack_mmap_file,
3736 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003737 .file_set_fowner = smack_file_set_fowner,
3738 .file_send_sigiotask = smack_file_send_sigiotask,
3739 .file_receive = smack_file_receive,
3740
Eric Paris83d49852012-04-04 13:45:40 -04003741 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003742
David Howellsee18d642009-09-02 09:14:21 +01003743 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003744 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003745 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003746 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003747 .kernel_act_as = smack_kernel_act_as,
3748 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003749 .task_setpgid = smack_task_setpgid,
3750 .task_getpgid = smack_task_getpgid,
3751 .task_getsid = smack_task_getsid,
3752 .task_getsecid = smack_task_getsecid,
3753 .task_setnice = smack_task_setnice,
3754 .task_setioprio = smack_task_setioprio,
3755 .task_getioprio = smack_task_getioprio,
3756 .task_setscheduler = smack_task_setscheduler,
3757 .task_getscheduler = smack_task_getscheduler,
3758 .task_movememory = smack_task_movememory,
3759 .task_kill = smack_task_kill,
3760 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003761 .task_to_inode = smack_task_to_inode,
3762
3763 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003764 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003765
3766 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3767 .msg_msg_free_security = smack_msg_msg_free_security,
3768
3769 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3770 .msg_queue_free_security = smack_msg_queue_free_security,
3771 .msg_queue_associate = smack_msg_queue_associate,
3772 .msg_queue_msgctl = smack_msg_queue_msgctl,
3773 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3774 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3775
3776 .shm_alloc_security = smack_shm_alloc_security,
3777 .shm_free_security = smack_shm_free_security,
3778 .shm_associate = smack_shm_associate,
3779 .shm_shmctl = smack_shm_shmctl,
3780 .shm_shmat = smack_shm_shmat,
3781
3782 .sem_alloc_security = smack_sem_alloc_security,
3783 .sem_free_security = smack_sem_free_security,
3784 .sem_associate = smack_sem_associate,
3785 .sem_semctl = smack_sem_semctl,
3786 .sem_semop = smack_sem_semop,
3787
Casey Schauflere114e472008-02-04 22:29:50 -08003788 .d_instantiate = smack_d_instantiate,
3789
3790 .getprocattr = smack_getprocattr,
3791 .setprocattr = smack_setprocattr,
3792
3793 .unix_stream_connect = smack_unix_stream_connect,
3794 .unix_may_send = smack_unix_may_send,
3795
3796 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07003797 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003798 .socket_connect = smack_socket_connect,
3799 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003800 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3801 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3802 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3803 .sk_alloc_security = smack_sk_alloc_security,
3804 .sk_free_security = smack_sk_free_security,
3805 .sock_graft = smack_sock_graft,
3806 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003807 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003808
Casey Schauflere114e472008-02-04 22:29:50 -08003809 /* key management security hooks */
3810#ifdef CONFIG_KEYS
3811 .key_alloc = smack_key_alloc,
3812 .key_free = smack_key_free,
3813 .key_permission = smack_key_permission,
3814#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003815
3816 /* Audit hooks */
3817#ifdef CONFIG_AUDIT
3818 .audit_rule_init = smack_audit_rule_init,
3819 .audit_rule_known = smack_audit_rule_known,
3820 .audit_rule_match = smack_audit_rule_match,
3821 .audit_rule_free = smack_audit_rule_free,
3822#endif /* CONFIG_AUDIT */
3823
Casey Schauflere114e472008-02-04 22:29:50 -08003824 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003825 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003826 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003827 .inode_notifysecctx = smack_inode_notifysecctx,
3828 .inode_setsecctx = smack_inode_setsecctx,
3829 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003830};
3831
Etienne Basset7198e2e2009-03-24 20:53:24 +01003832
Casey Schaufler86812bb2012-04-17 18:55:46 -07003833static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003834{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003835 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003836 * Initialize rule list locks
3837 */
3838 mutex_init(&smack_known_huh.smk_rules_lock);
3839 mutex_init(&smack_known_hat.smk_rules_lock);
3840 mutex_init(&smack_known_floor.smk_rules_lock);
3841 mutex_init(&smack_known_star.smk_rules_lock);
3842 mutex_init(&smack_known_invalid.smk_rules_lock);
3843 mutex_init(&smack_known_web.smk_rules_lock);
3844 /*
3845 * Initialize rule lists
3846 */
3847 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3848 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3849 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3850 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3851 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3852 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3853 /*
3854 * Create the known labels list
3855 */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003856 list_add(&smack_known_huh.list, &smack_known_list);
3857 list_add(&smack_known_hat.list, &smack_known_list);
3858 list_add(&smack_known_star.list, &smack_known_list);
3859 list_add(&smack_known_floor.list, &smack_known_list);
3860 list_add(&smack_known_invalid.list, &smack_known_list);
3861 list_add(&smack_known_web.list, &smack_known_list);
3862}
3863
Casey Schauflere114e472008-02-04 22:29:50 -08003864/**
3865 * smack_init - initialize the smack system
3866 *
3867 * Returns 0
3868 */
3869static __init int smack_init(void)
3870{
David Howellsd84f4f92008-11-14 10:39:23 +11003871 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003872 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003873
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003874 if (!security_module_enable(&smack_ops))
3875 return 0;
3876
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003877 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3878 GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003879 if (tsp == NULL)
3880 return -ENOMEM;
3881
Casey Schauflere114e472008-02-04 22:29:50 -08003882 printk(KERN_INFO "Smack: Initializing.\n");
3883
3884 /*
3885 * Set the security state for the initial task.
3886 */
David Howellsd84f4f92008-11-14 10:39:23 +11003887 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003888 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003889
Casey Schaufler86812bb2012-04-17 18:55:46 -07003890 /* initialize the smack_known_list */
3891 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003892
3893 /*
3894 * Register with LSM
3895 */
3896 if (register_security(&smack_ops))
3897 panic("smack: Unable to register with kernel.\n");
3898
3899 return 0;
3900}
3901
3902/*
3903 * Smack requires early initialization in order to label
3904 * all processes and objects when they are created.
3905 */
3906security_initcall(smack_init);