blob: ce000a81caf7fca373af4ef16724b34e9c2869b5 [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;
Casey Schauflere830b392013-05-22 18:43:07 -0700264 /*
265 * smk_initialized will be zero from kzalloc.
266 */
Casey Schauflere114e472008-02-04 22:29:50 -0800267 sb->s_security = sbsp;
268
269 return 0;
270}
271
272/**
273 * smack_sb_free_security - free a superblock blob
274 * @sb: the superblock getting the blob
275 *
276 */
277static void smack_sb_free_security(struct super_block *sb)
278{
279 kfree(sb->s_security);
280 sb->s_security = NULL;
281}
282
283/**
284 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800285 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800286 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800287 *
288 * Returns 0 on success or -ENOMEM on error.
289 *
290 * Copy the Smack specific mount options out of the mount
291 * options list.
292 */
Eric Parise0007522008-03-05 10:31:54 -0500293static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800294{
295 char *cp, *commap, *otheropts, *dp;
296
Casey Schauflere114e472008-02-04 22:29:50 -0800297 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
298 if (otheropts == NULL)
299 return -ENOMEM;
300
301 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
302 if (strstr(cp, SMK_FSDEFAULT) == cp)
303 dp = smackopts;
304 else if (strstr(cp, SMK_FSFLOOR) == cp)
305 dp = smackopts;
306 else if (strstr(cp, SMK_FSHAT) == cp)
307 dp = smackopts;
308 else if (strstr(cp, SMK_FSROOT) == cp)
309 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700310 else if (strstr(cp, SMK_FSTRANS) == cp)
311 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800312 else
313 dp = otheropts;
314
315 commap = strchr(cp, ',');
316 if (commap != NULL)
317 *commap = '\0';
318
319 if (*dp != '\0')
320 strcat(dp, ",");
321 strcat(dp, cp);
322 }
323
324 strcpy(orig, otheropts);
325 free_page((unsigned long)otheropts);
326
327 return 0;
328}
329
330/**
331 * smack_sb_kern_mount - Smack specific mount processing
332 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100333 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800334 * @data: the smack mount options
335 *
336 * Returns 0 on success, an error code on failure
337 */
James Morris12204e22008-12-19 10:44:42 +1100338static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800339{
340 struct dentry *root = sb->s_root;
341 struct inode *inode = root->d_inode;
342 struct superblock_smack *sp = sb->s_security;
343 struct inode_smack *isp;
344 char *op;
345 char *commap;
346 char *nsp;
Casey Schauflere830b392013-05-22 18:43:07 -0700347 int transmute = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800348
Casey Schauflere830b392013-05-22 18:43:07 -0700349 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800350 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700351
Casey Schauflere114e472008-02-04 22:29:50 -0800352 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800353
354 for (op = data; op != NULL; op = commap) {
355 commap = strchr(op, ',');
356 if (commap != NULL)
357 *commap++ = '\0';
358
359 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
360 op += strlen(SMK_FSHAT);
361 nsp = smk_import(op, 0);
362 if (nsp != NULL)
363 sp->smk_hat = nsp;
364 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
365 op += strlen(SMK_FSFLOOR);
366 nsp = smk_import(op, 0);
367 if (nsp != NULL)
368 sp->smk_floor = nsp;
369 } else if (strncmp(op, SMK_FSDEFAULT,
370 strlen(SMK_FSDEFAULT)) == 0) {
371 op += strlen(SMK_FSDEFAULT);
372 nsp = smk_import(op, 0);
373 if (nsp != NULL)
374 sp->smk_default = nsp;
375 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
376 op += strlen(SMK_FSROOT);
377 nsp = smk_import(op, 0);
378 if (nsp != NULL)
379 sp->smk_root = nsp;
Casey Schauflere830b392013-05-22 18:43:07 -0700380 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
381 op += strlen(SMK_FSTRANS);
382 nsp = smk_import(op, 0);
383 if (nsp != NULL) {
384 sp->smk_root = nsp;
385 transmute = 1;
386 }
Casey Schauflere114e472008-02-04 22:29:50 -0800387 }
388 }
389
390 /*
391 * Initialize the root inode.
392 */
393 isp = inode->i_security;
Casey Schauflere830b392013-05-22 18:43:07 -0700394 if (inode->i_security == NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800395 inode->i_security = new_inode_smack(sp->smk_root);
Casey Schauflere830b392013-05-22 18:43:07 -0700396 isp = inode->i_security;
397 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800398 isp->smk_inode = sp->smk_root;
399
Casey Schauflere830b392013-05-22 18:43:07 -0700400 if (transmute)
401 isp->smk_flags |= SMK_INODE_TRANSMUTE;
402
Casey Schauflere114e472008-02-04 22:29:50 -0800403 return 0;
404}
405
406/**
407 * smack_sb_statfs - Smack check on statfs
408 * @dentry: identifies the file system in question
409 *
410 * Returns 0 if current can read the floor of the filesystem,
411 * and error code otherwise
412 */
413static int smack_sb_statfs(struct dentry *dentry)
414{
415 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200416 int rc;
417 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800418
Eric Parisa2694342011-04-25 13:10:27 -0400419 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200420 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
421
422 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
423 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800424}
425
426/**
427 * smack_sb_mount - Smack check for mounting
428 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800429 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800430 * @type: unused
431 * @flags: unused
432 * @data: unused
433 *
434 * Returns 0 if current can write the floor of the filesystem
435 * being mounted on, an error code otherwise.
436 */
Al Viro808d4e32012-10-11 11:42:01 -0400437static int smack_sb_mount(const char *dev_name, struct path *path,
438 const char *type, unsigned long flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800439{
Al Virod8c95842011-12-07 18:16:57 -0500440 struct superblock_smack *sbp = path->dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200441 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800442
Eric Parisf48b7392011-04-25 12:54:27 -0400443 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200444 smk_ad_setfield_u_fs_path(&ad, *path);
445
446 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800447}
448
449/**
450 * smack_sb_umount - Smack check for unmounting
451 * @mnt: file system to unmount
452 * @flags: unused
453 *
454 * Returns 0 if current can write the floor of the filesystem
455 * being unmounted, an error code otherwise.
456 */
457static int smack_sb_umount(struct vfsmount *mnt, int flags)
458{
459 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200460 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400461 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200462
Eric Parisa2694342011-04-25 13:10:27 -0400463 path.dentry = mnt->mnt_root;
464 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200465
Eric Parisf48b7392011-04-25 12:54:27 -0400466 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400467 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflere114e472008-02-04 22:29:50 -0800468
Al Virod8c95842011-12-07 18:16:57 -0500469 sbp = path.dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200470 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800471}
472
473/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800474 * BPRM hooks
475 */
476
Casey Schauflerce8a4322011-09-29 18:21:01 -0700477/**
478 * smack_bprm_set_creds - set creds for exec
479 * @bprm: the exec information
480 *
481 * Returns 0 if it gets a blob, -ENOMEM otherwise
482 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800483static int smack_bprm_set_creds(struct linux_binprm *bprm)
484{
Al Viro496ad9a2013-01-23 17:07:38 -0500485 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300486 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800487 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800488 int rc;
489
490 rc = cap_bprm_set_creds(bprm);
491 if (rc != 0)
492 return rc;
493
494 if (bprm->cred_prepared)
495 return 0;
496
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300497 isp = inode->i_security;
498 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800499 return 0;
500
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300501 if (bprm->unsafe)
502 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800503
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300504 bsp->smk_task = isp->smk_task;
505 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800506
507 return 0;
508}
509
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300510/**
511 * smack_bprm_committing_creds - Prepare to install the new credentials
512 * from bprm.
513 *
514 * @bprm: binprm for exec
515 */
516static void smack_bprm_committing_creds(struct linux_binprm *bprm)
517{
518 struct task_smack *bsp = bprm->cred->security;
519
520 if (bsp->smk_task != bsp->smk_forked)
521 current->pdeath_signal = 0;
522}
523
524/**
525 * smack_bprm_secureexec - Return the decision to use secureexec.
526 * @bprm: binprm for exec
527 *
528 * Returns 0 on success.
529 */
530static int smack_bprm_secureexec(struct linux_binprm *bprm)
531{
532 struct task_smack *tsp = current_security();
533 int ret = cap_bprm_secureexec(bprm);
534
535 if (!ret && (tsp->smk_task != tsp->smk_forked))
536 ret = 1;
537
538 return ret;
539}
540
Casey Schaufler676dac42010-12-02 06:43:39 -0800541/*
Casey Schauflere114e472008-02-04 22:29:50 -0800542 * Inode hooks
543 */
544
545/**
546 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800547 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800548 *
549 * Returns 0 if it gets a blob, -ENOMEM otherwise
550 */
551static int smack_inode_alloc_security(struct inode *inode)
552{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700553 struct smack_known *skp = smk_of_current();
554
555 inode->i_security = new_inode_smack(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -0800556 if (inode->i_security == NULL)
557 return -ENOMEM;
558 return 0;
559}
560
561/**
562 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800563 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800564 *
565 * Clears the blob pointer in inode
566 */
567static void smack_inode_free_security(struct inode *inode)
568{
569 kfree(inode->i_security);
570 inode->i_security = NULL;
571}
572
573/**
574 * smack_inode_init_security - copy out the smack from an inode
575 * @inode: the inode
576 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500577 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800578 * @name: where to put the attribute name
579 * @value: where to put the attribute value
580 * @len: where to put the length of the attribute
581 *
582 * Returns 0 if it all works out, -ENOMEM if there's no memory
583 */
584static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -0500585 const struct qstr *qstr, char **name,
586 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800587{
Casey Schaufler2267b132012-03-13 19:14:19 -0700588 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700589 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800590 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200591 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800592 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800593
594 if (name) {
Tetsuo Handaceffec552012-03-29 16:19:05 +0900595 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800596 if (*name == NULL)
597 return -ENOMEM;
598 }
599
600 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800601 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700602 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800603 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200604
605 /*
606 * If the access rule allows transmutation and
607 * the directory requests transmutation then
608 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700609 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200610 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800611 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700612 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200613 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700614 issp->smk_flags |= SMK_INODE_CHANGED;
615 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200616
Tetsuo Handaceffec552012-03-29 16:19:05 +0900617 *value = kstrdup(isp, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800618 if (*value == NULL)
619 return -ENOMEM;
620 }
621
622 if (len)
623 *len = strlen(isp) + 1;
624
625 return 0;
626}
627
628/**
629 * smack_inode_link - Smack check on link
630 * @old_dentry: the existing object
631 * @dir: unused
632 * @new_dentry: the new object
633 *
634 * Returns 0 if access is permitted, an error code otherwise
635 */
636static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
637 struct dentry *new_dentry)
638{
Casey Schauflere114e472008-02-04 22:29:50 -0800639 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200640 struct smk_audit_info ad;
641 int rc;
642
Eric Parisa2694342011-04-25 13:10:27 -0400643 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200644 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800645
646 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200647 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800648
649 if (rc == 0 && new_dentry->d_inode != NULL) {
650 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200651 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
652 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800653 }
654
655 return rc;
656}
657
658/**
659 * smack_inode_unlink - Smack check on inode deletion
660 * @dir: containing directory object
661 * @dentry: file to unlink
662 *
663 * Returns 0 if current can write the containing directory
664 * and the object, error code otherwise
665 */
666static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
667{
668 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200669 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800670 int rc;
671
Eric Parisa2694342011-04-25 13:10:27 -0400672 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200673 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
674
Casey Schauflere114e472008-02-04 22:29:50 -0800675 /*
676 * You need write access to the thing you're unlinking
677 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200678 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
679 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800680 /*
681 * You also need write access to the containing directory
682 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400683 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200684 smk_ad_setfield_u_fs_inode(&ad, dir);
685 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
686 }
Casey Schauflere114e472008-02-04 22:29:50 -0800687 return rc;
688}
689
690/**
691 * smack_inode_rmdir - Smack check on directory deletion
692 * @dir: containing directory object
693 * @dentry: directory to unlink
694 *
695 * Returns 0 if current can write the containing directory
696 * and the directory, error code otherwise
697 */
698static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
699{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200700 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800701 int rc;
702
Eric Parisa2694342011-04-25 13:10:27 -0400703 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200704 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
705
Casey Schauflere114e472008-02-04 22:29:50 -0800706 /*
707 * You need write access to the thing you're removing
708 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200709 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
710 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800711 /*
712 * You also need write access to the containing directory
713 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400714 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200715 smk_ad_setfield_u_fs_inode(&ad, dir);
716 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
717 }
Casey Schauflere114e472008-02-04 22:29:50 -0800718
719 return rc;
720}
721
722/**
723 * smack_inode_rename - Smack check on rename
724 * @old_inode: the old directory
725 * @old_dentry: unused
726 * @new_inode: the new directory
727 * @new_dentry: unused
728 *
729 * Read and write access is required on both the old and
730 * new directories.
731 *
732 * Returns 0 if access is permitted, an error code otherwise
733 */
734static int smack_inode_rename(struct inode *old_inode,
735 struct dentry *old_dentry,
736 struct inode *new_inode,
737 struct dentry *new_dentry)
738{
739 int rc;
740 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200741 struct smk_audit_info ad;
742
Eric Parisa2694342011-04-25 13:10:27 -0400743 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200744 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800745
746 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200747 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800748
749 if (rc == 0 && new_dentry->d_inode != NULL) {
750 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200751 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
752 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800753 }
Casey Schauflere114e472008-02-04 22:29:50 -0800754 return rc;
755}
756
757/**
758 * smack_inode_permission - Smack version of permission()
759 * @inode: the inode in question
760 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800761 *
762 * This is the important Smack hook.
763 *
764 * Returns 0 if access is permitted, -EACCES otherwise
765 */
Al Viroe74f71e2011-06-20 19:38:15 -0400766static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800767{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200768 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400769 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400770
771 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800772 /*
773 * No permission to check. Existence test. Yup, it's there.
774 */
775 if (mask == 0)
776 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700777
778 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400779 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700780 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400781 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200782 smk_ad_setfield_u_fs_inode(&ad, inode);
783 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800784}
785
786/**
787 * smack_inode_setattr - Smack check for setting attributes
788 * @dentry: the object
789 * @iattr: for the force flag
790 *
791 * Returns 0 if access is permitted, an error code otherwise
792 */
793static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
794{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200795 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800796 /*
797 * Need to allow for clearing the setuid bit.
798 */
799 if (iattr->ia_valid & ATTR_FORCE)
800 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400801 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200802 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800803
Etienne Bassetecfcc532009-04-08 20:40:06 +0200804 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800805}
806
807/**
808 * smack_inode_getattr - Smack check for getting attributes
809 * @mnt: unused
810 * @dentry: the object
811 *
812 * Returns 0 if access is permitted, an error code otherwise
813 */
814static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
815{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200816 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400817 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200818
Eric Parisa2694342011-04-25 13:10:27 -0400819 path.dentry = dentry;
820 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200821
Eric Parisf48b7392011-04-25 12:54:27 -0400822 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400823 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200824 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800825}
826
827/**
828 * smack_inode_setxattr - Smack check for setting xattrs
829 * @dentry: the object
830 * @name: name of the attribute
831 * @value: unused
832 * @size: unused
833 * @flags: unused
834 *
835 * This protects the Smack attribute explicitly.
836 *
837 * Returns 0 if access is permitted, an error code otherwise
838 */
David Howells8f0cfa52008-04-29 00:59:41 -0700839static int smack_inode_setxattr(struct dentry *dentry, const char *name,
840 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800841{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200842 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800843 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800844
Casey Schauflerbcdca222008-02-23 15:24:04 -0800845 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
846 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800847 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800848 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
849 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700850 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800851 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200852 /*
853 * check label validity here so import wont fail on
854 * post_setxattr
855 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700856 if (size == 0 || size >= SMK_LONGLABEL ||
Etienne Bassetdefc4332009-04-16 23:58:42 +0200857 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400858 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200859 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700860 if (!smack_privileged(CAP_MAC_ADMIN))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200861 rc = -EPERM;
862 if (size != TRANS_TRUE_SIZE ||
863 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
864 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800865 } else
866 rc = cap_inode_setxattr(dentry, name, value, size, flags);
867
Eric Parisa2694342011-04-25 13:10:27 -0400868 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200869 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
870
Casey Schauflerbcdca222008-02-23 15:24:04 -0800871 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200872 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800873
874 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800875}
876
877/**
878 * smack_inode_post_setxattr - Apply the Smack update approved above
879 * @dentry: object
880 * @name: attribute name
881 * @value: attribute value
882 * @size: attribute size
883 * @flags: unused
884 *
885 * Set the pointer in the inode blob to the entry found
886 * in the master label list.
887 */
David Howells8f0cfa52008-04-29 00:59:41 -0700888static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
889 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800890{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700891 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200892 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800893
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700894 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
895 isp->smk_flags |= SMK_INODE_TRANSMUTE;
896 return;
897 }
898
899 skp = smk_import_entry(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800900 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700901 if (skp != NULL)
902 isp->smk_inode = skp->smk_known;
Casey Schaufler676dac42010-12-02 06:43:39 -0800903 else
904 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200905 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700906 if (skp != NULL)
907 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800908 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700909 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800910 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700911 if (skp != NULL)
912 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800913 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700914 isp->smk_mmap = &smack_known_invalid;
915 }
Casey Schauflere114e472008-02-04 22:29:50 -0800916
917 return;
918}
919
Casey Schauflerce8a4322011-09-29 18:21:01 -0700920/**
Casey Schauflere114e472008-02-04 22:29:50 -0800921 * smack_inode_getxattr - Smack check on getxattr
922 * @dentry: the object
923 * @name: unused
924 *
925 * Returns 0 if access is permitted, an error code otherwise
926 */
David Howells8f0cfa52008-04-29 00:59:41 -0700927static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800928{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200929 struct smk_audit_info ad;
930
Eric Parisa2694342011-04-25 13:10:27 -0400931 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200932 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
933
934 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800935}
936
Casey Schauflerce8a4322011-09-29 18:21:01 -0700937/**
Casey Schauflere114e472008-02-04 22:29:50 -0800938 * smack_inode_removexattr - Smack check on removexattr
939 * @dentry: the object
940 * @name: name of the attribute
941 *
942 * Removing the Smack attribute requires CAP_MAC_ADMIN
943 *
944 * Returns 0 if access is permitted, an error code otherwise
945 */
David Howells8f0cfa52008-04-29 00:59:41 -0700946static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800947{
Casey Schaufler676dac42010-12-02 06:43:39 -0800948 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200949 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800950 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800951
Casey Schauflerbcdca222008-02-23 15:24:04 -0800952 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
953 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800954 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200955 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800956 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
957 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700958 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800959 rc = -EPERM;
960 } else
961 rc = cap_inode_removexattr(dentry, name);
962
Eric Parisa2694342011-04-25 13:10:27 -0400963 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200964 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800965 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200966 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800967
Casey Schaufler676dac42010-12-02 06:43:39 -0800968 if (rc == 0) {
969 isp = dentry->d_inode->i_security;
970 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800971 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800972 }
973
Casey Schauflerbcdca222008-02-23 15:24:04 -0800974 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800975}
976
977/**
978 * smack_inode_getsecurity - get smack xattrs
979 * @inode: the object
980 * @name: attribute name
981 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800982 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800983 *
984 * Returns the size of the attribute or an error code
985 */
986static int smack_inode_getsecurity(const struct inode *inode,
987 const char *name, void **buffer,
988 bool alloc)
989{
990 struct socket_smack *ssp;
991 struct socket *sock;
992 struct super_block *sbp;
993 struct inode *ip = (struct inode *)inode;
994 char *isp;
995 int ilen;
996 int rc = 0;
997
998 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
999 isp = smk_of_inode(inode);
1000 ilen = strlen(isp) + 1;
1001 *buffer = isp;
1002 return ilen;
1003 }
1004
1005 /*
1006 * The rest of the Smack xattrs are only on sockets.
1007 */
1008 sbp = ip->i_sb;
1009 if (sbp->s_magic != SOCKFS_MAGIC)
1010 return -EOPNOTSUPP;
1011
1012 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001013 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001014 return -EOPNOTSUPP;
1015
1016 ssp = sock->sk->sk_security;
1017
1018 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1019 isp = ssp->smk_in;
1020 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001021 isp = ssp->smk_out->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001022 else
1023 return -EOPNOTSUPP;
1024
1025 ilen = strlen(isp) + 1;
1026 if (rc == 0) {
1027 *buffer = isp;
1028 rc = ilen;
1029 }
1030
1031 return rc;
1032}
1033
1034
1035/**
1036 * smack_inode_listsecurity - list the Smack attributes
1037 * @inode: the object
1038 * @buffer: where they go
1039 * @buffer_size: size of buffer
1040 *
1041 * Returns 0 on success, -EINVAL otherwise
1042 */
1043static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1044 size_t buffer_size)
1045{
1046 int len = strlen(XATTR_NAME_SMACK);
1047
1048 if (buffer != NULL && len <= buffer_size) {
1049 memcpy(buffer, XATTR_NAME_SMACK, len);
1050 return len;
1051 }
1052 return -EINVAL;
1053}
1054
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001055/**
1056 * smack_inode_getsecid - Extract inode's security id
1057 * @inode: inode to extract the info from
1058 * @secid: where result will be saved
1059 */
1060static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1061{
1062 struct inode_smack *isp = inode->i_security;
1063
1064 *secid = smack_to_secid(isp->smk_inode);
1065}
1066
Casey Schauflere114e472008-02-04 22:29:50 -08001067/*
1068 * File Hooks
1069 */
1070
1071/**
1072 * smack_file_permission - Smack check on file operations
1073 * @file: unused
1074 * @mask: unused
1075 *
1076 * Returns 0
1077 *
1078 * Should access checks be done on each read or write?
1079 * UNICOS and SELinux say yes.
1080 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1081 *
1082 * I'll say no for now. Smack does not do the frequent
1083 * label changing that SELinux does.
1084 */
1085static int smack_file_permission(struct file *file, int mask)
1086{
1087 return 0;
1088}
1089
1090/**
1091 * smack_file_alloc_security - assign a file security blob
1092 * @file: the object
1093 *
1094 * The security blob for a file is a pointer to the master
1095 * label list, so no allocation is done.
1096 *
1097 * Returns 0
1098 */
1099static int smack_file_alloc_security(struct file *file)
1100{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001101 struct smack_known *skp = smk_of_current();
1102
1103 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001104 return 0;
1105}
1106
1107/**
1108 * smack_file_free_security - clear a file security blob
1109 * @file: the object
1110 *
1111 * The security blob for a file is a pointer to the master
1112 * label list, so no memory is freed.
1113 */
1114static void smack_file_free_security(struct file *file)
1115{
1116 file->f_security = NULL;
1117}
1118
1119/**
1120 * smack_file_ioctl - Smack check on ioctls
1121 * @file: the object
1122 * @cmd: what to do
1123 * @arg: unused
1124 *
1125 * Relies heavily on the correct use of the ioctl command conventions.
1126 *
1127 * Returns 0 if allowed, error code otherwise
1128 */
1129static int smack_file_ioctl(struct file *file, unsigned int cmd,
1130 unsigned long arg)
1131{
1132 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001133 struct smk_audit_info ad;
1134
Eric Parisf48b7392011-04-25 12:54:27 -04001135 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001136 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001137
1138 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001139 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001140
1141 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001142 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001143
1144 return rc;
1145}
1146
1147/**
1148 * smack_file_lock - Smack check on file locking
1149 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001150 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001151 *
1152 * Returns 0 if current has write access, error code otherwise
1153 */
1154static int smack_file_lock(struct file *file, unsigned int cmd)
1155{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001156 struct smk_audit_info ad;
1157
Eric Paris92f42502011-04-25 13:15:55 -04001158 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1159 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001160 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001161}
1162
1163/**
1164 * smack_file_fcntl - Smack check on fcntl
1165 * @file: the object
1166 * @cmd: what action to check
1167 * @arg: unused
1168 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001169 * Generally these operations are harmless.
1170 * File locking operations present an obvious mechanism
1171 * for passing information, so they require write access.
1172 *
Casey Schauflere114e472008-02-04 22:29:50 -08001173 * Returns 0 if current has access, error code otherwise
1174 */
1175static int smack_file_fcntl(struct file *file, unsigned int cmd,
1176 unsigned long arg)
1177{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001178 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001179 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001180
Etienne Bassetecfcc532009-04-08 20:40:06 +02001181
Casey Schauflere114e472008-02-04 22:29:50 -08001182 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001183 case F_GETLK:
Casey Schauflere114e472008-02-04 22:29:50 -08001184 case F_SETLK:
1185 case F_SETLKW:
1186 case F_SETOWN:
1187 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1189 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001190 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001191 break;
1192 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001193 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001194 }
1195
1196 return rc;
1197}
1198
1199/**
Al Viroe5467852012-05-30 13:30:51 -04001200 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001201 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1202 * if mapping anonymous memory.
1203 * @file contains the file structure for file to map (may be NULL).
1204 * @reqprot contains the protection requested by the application.
1205 * @prot contains the protection that will be applied by the kernel.
1206 * @flags contains the operational flags.
1207 * Return 0 if permission is granted.
1208 */
Al Viroe5467852012-05-30 13:30:51 -04001209static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001210 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001211 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001212{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001213 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001214 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001215 struct smack_rule *srp;
1216 struct task_smack *tsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001217 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001218 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001219 int may;
1220 int mmay;
1221 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001222 int rc;
1223
Al Viro496ad9a2013-01-23 17:07:38 -05001224 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001225 return 0;
1226
Al Viro496ad9a2013-01-23 17:07:38 -05001227 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001228 if (isp->smk_mmap == NULL)
1229 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001230 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001231
1232 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001233 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001234 rc = 0;
1235
1236 rcu_read_lock();
1237 /*
1238 * For each Smack rule associated with the subject
1239 * label verify that the SMACK64MMAP also has access
1240 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001241 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001242 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001243 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001244 /*
1245 * Matching labels always allows access.
1246 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001247 if (mkp->smk_known == osmack)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001248 continue;
1249 /*
1250 * If there is a matching local rule take
1251 * that into account as well.
1252 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001253 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001254 &tsp->smk_rules);
1255 if (may == -ENOENT)
1256 may = srp->smk_access;
1257 else
1258 may &= srp->smk_access;
1259 /*
1260 * If may is zero the SMACK64MMAP subject can't
1261 * possibly have less access.
1262 */
1263 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001264 continue;
1265
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001266 /*
1267 * Fetch the global list entry.
1268 * If there isn't one a SMACK64MMAP subject
1269 * can't have as much access as current.
1270 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001271 mmay = smk_access_entry(mkp->smk_known, osmack,
1272 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001273 if (mmay == -ENOENT) {
1274 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001275 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001276 }
1277 /*
1278 * If there is a local entry it modifies the
1279 * potential access, too.
1280 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001281 tmay = smk_access_entry(mkp->smk_known, osmack,
1282 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001283 if (tmay != -ENOENT)
1284 mmay &= tmay;
1285
1286 /*
1287 * If there is any access available to current that is
1288 * not available to a SMACK64MMAP subject
1289 * deny access.
1290 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001291 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001292 rc = -EACCES;
1293 break;
1294 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001295 }
1296
1297 rcu_read_unlock();
1298
1299 return rc;
1300}
1301
1302/**
Casey Schauflere114e472008-02-04 22:29:50 -08001303 * smack_file_set_fowner - set the file security blob value
1304 * @file: object in question
1305 *
1306 * Returns 0
1307 * Further research may be required on this one.
1308 */
1309static int smack_file_set_fowner(struct file *file)
1310{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001311 struct smack_known *skp = smk_of_current();
1312
1313 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001314 return 0;
1315}
1316
1317/**
1318 * smack_file_send_sigiotask - Smack on sigio
1319 * @tsk: The target task
1320 * @fown: the object the signal come from
1321 * @signum: unused
1322 *
1323 * Allow a privileged task to get signals even if it shouldn't
1324 *
1325 * Returns 0 if a subject with the object's smack could
1326 * write to the task, an error code otherwise.
1327 */
1328static int smack_file_send_sigiotask(struct task_struct *tsk,
1329 struct fown_struct *fown, int signum)
1330{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001331 struct smack_known *skp;
1332 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001333 struct file *file;
1334 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001335 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001336
1337 /*
1338 * struct fown_struct is never outside the context of a struct file
1339 */
1340 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001341
Etienne Bassetecfcc532009-04-08 20:40:06 +02001342 /* we don't log here as rc can be overriden */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001343 skp = smk_find_entry(file->f_security);
1344 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001345 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001346 rc = 0;
1347
1348 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1349 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001350 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001351 return rc;
1352}
1353
1354/**
1355 * smack_file_receive - Smack file receive check
1356 * @file: the object
1357 *
1358 * Returns 0 if current has access, error code otherwise
1359 */
1360static int smack_file_receive(struct file *file)
1361{
1362 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001363 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001364
Etienne Bassetecfcc532009-04-08 20:40:06 +02001365 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1366 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001367 /*
1368 * This code relies on bitmasks.
1369 */
1370 if (file->f_mode & FMODE_READ)
1371 may = MAY_READ;
1372 if (file->f_mode & FMODE_WRITE)
1373 may |= MAY_WRITE;
1374
Etienne Bassetecfcc532009-04-08 20:40:06 +02001375 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001376}
1377
Casey Schaufler531f1d42011-09-19 12:41:42 -07001378/**
Eric Paris83d49852012-04-04 13:45:40 -04001379 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001380 * @file: the object
1381 * @cred: unused
1382 *
1383 * Set the security blob in the file structure.
1384 *
1385 * Returns 0
1386 */
Eric Paris83d49852012-04-04 13:45:40 -04001387static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001388{
Al Viro496ad9a2013-01-23 17:07:38 -05001389 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001390
1391 file->f_security = isp->smk_inode;
1392
1393 return 0;
1394}
1395
Casey Schauflere114e472008-02-04 22:29:50 -08001396/*
1397 * Task hooks
1398 */
1399
1400/**
David Howellsee18d642009-09-02 09:14:21 +01001401 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1402 * @new: the new credentials
1403 * @gfp: the atomicity of any memory allocations
1404 *
1405 * Prepare a blank set of credentials for modification. This must allocate all
1406 * the memory the LSM module might require such that cred_transfer() can
1407 * complete without error.
1408 */
1409static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1410{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001411 struct task_smack *tsp;
1412
1413 tsp = new_task_smack(NULL, NULL, gfp);
1414 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001415 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001416
1417 cred->security = tsp;
1418
David Howellsee18d642009-09-02 09:14:21 +01001419 return 0;
1420}
1421
1422
1423/**
David Howellsf1752ee2008-11-14 10:39:17 +11001424 * smack_cred_free - "free" task-level security credentials
1425 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001426 *
Casey Schauflere114e472008-02-04 22:29:50 -08001427 */
David Howellsf1752ee2008-11-14 10:39:17 +11001428static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001429{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001430 struct task_smack *tsp = cred->security;
1431 struct smack_rule *rp;
1432 struct list_head *l;
1433 struct list_head *n;
1434
1435 if (tsp == NULL)
1436 return;
1437 cred->security = NULL;
1438
1439 list_for_each_safe(l, n, &tsp->smk_rules) {
1440 rp = list_entry(l, struct smack_rule, list);
1441 list_del(&rp->list);
1442 kfree(rp);
1443 }
1444 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001445}
1446
1447/**
David Howellsd84f4f92008-11-14 10:39:23 +11001448 * smack_cred_prepare - prepare new set of credentials for modification
1449 * @new: the new credentials
1450 * @old: the original credentials
1451 * @gfp: the atomicity of any memory allocations
1452 *
1453 * Prepare a new set of credentials for modification.
1454 */
1455static int smack_cred_prepare(struct cred *new, const struct cred *old,
1456 gfp_t gfp)
1457{
Casey Schaufler676dac42010-12-02 06:43:39 -08001458 struct task_smack *old_tsp = old->security;
1459 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001460 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001461
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001462 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001463 if (new_tsp == NULL)
1464 return -ENOMEM;
1465
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001466 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1467 if (rc != 0)
1468 return rc;
1469
Casey Schaufler676dac42010-12-02 06:43:39 -08001470 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001471 return 0;
1472}
1473
Randy Dunlap251a2a92009-02-18 11:42:33 -08001474/**
David Howellsee18d642009-09-02 09:14:21 +01001475 * smack_cred_transfer - Transfer the old credentials to the new credentials
1476 * @new: the new credentials
1477 * @old: the original credentials
1478 *
1479 * Fill in a set of blank credentials from another set of credentials.
1480 */
1481static void smack_cred_transfer(struct cred *new, const struct cred *old)
1482{
Casey Schaufler676dac42010-12-02 06:43:39 -08001483 struct task_smack *old_tsp = old->security;
1484 struct task_smack *new_tsp = new->security;
1485
1486 new_tsp->smk_task = old_tsp->smk_task;
1487 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001488 mutex_init(&new_tsp->smk_rules_lock);
1489 INIT_LIST_HEAD(&new_tsp->smk_rules);
1490
1491
1492 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001493}
1494
1495/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001496 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001497 * @new: points to the set of credentials to be modified.
1498 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001499 *
1500 * Set the security data for a kernel service.
1501 */
1502static int smack_kernel_act_as(struct cred *new, u32 secid)
1503{
Casey Schaufler676dac42010-12-02 06:43:39 -08001504 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001505 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001506
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001507 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001508 return -EINVAL;
1509
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001510 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001511 return 0;
1512}
1513
1514/**
1515 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001516 * @new: points to the set of credentials to be modified
1517 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001518 *
1519 * Set the file creation context in a set of credentials to the same
1520 * as the objective context of the specified inode
1521 */
1522static int smack_kernel_create_files_as(struct cred *new,
1523 struct inode *inode)
1524{
1525 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001526 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001527
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001528 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1529 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001530 return 0;
1531}
1532
1533/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001534 * smk_curacc_on_task - helper to log task related access
1535 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001536 * @access: the access requested
1537 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001538 *
1539 * Return 0 if access is permitted
1540 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001541static int smk_curacc_on_task(struct task_struct *p, int access,
1542 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001543{
1544 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001545 struct smack_known *skp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001546
Casey Schaufler531f1d42011-09-19 12:41:42 -07001547 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001548 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001549 return smk_curacc(skp->smk_known, access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001550}
1551
1552/**
Casey Schauflere114e472008-02-04 22:29:50 -08001553 * smack_task_setpgid - Smack check on setting pgid
1554 * @p: the task object
1555 * @pgid: unused
1556 *
1557 * Return 0 if write access is permitted
1558 */
1559static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1560{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001561 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001562}
1563
1564/**
1565 * smack_task_getpgid - Smack access check for getpgid
1566 * @p: the object task
1567 *
1568 * Returns 0 if current can read the object task, error code otherwise
1569 */
1570static int smack_task_getpgid(struct task_struct *p)
1571{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001572 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001573}
1574
1575/**
1576 * smack_task_getsid - Smack access check for getsid
1577 * @p: the object task
1578 *
1579 * Returns 0 if current can read the object task, error code otherwise
1580 */
1581static int smack_task_getsid(struct task_struct *p)
1582{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001583 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001584}
1585
1586/**
1587 * smack_task_getsecid - get the secid of the task
1588 * @p: the object task
1589 * @secid: where to put the result
1590 *
1591 * Sets the secid to contain a u32 version of the smack label.
1592 */
1593static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1594{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001595 struct smack_known *skp = smk_of_task(task_security(p));
1596
1597 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001598}
1599
1600/**
1601 * smack_task_setnice - Smack check on setting nice
1602 * @p: the task object
1603 * @nice: unused
1604 *
1605 * Return 0 if write access is permitted
1606 */
1607static int smack_task_setnice(struct task_struct *p, int nice)
1608{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001609 int rc;
1610
1611 rc = cap_task_setnice(p, nice);
1612 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001613 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001614 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001615}
1616
1617/**
1618 * smack_task_setioprio - Smack check on setting ioprio
1619 * @p: the task object
1620 * @ioprio: unused
1621 *
1622 * Return 0 if write access is permitted
1623 */
1624static int smack_task_setioprio(struct task_struct *p, int ioprio)
1625{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001626 int rc;
1627
1628 rc = cap_task_setioprio(p, ioprio);
1629 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001630 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001631 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001632}
1633
1634/**
1635 * smack_task_getioprio - Smack check on reading ioprio
1636 * @p: the task object
1637 *
1638 * Return 0 if read access is permitted
1639 */
1640static int smack_task_getioprio(struct task_struct *p)
1641{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001642 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001643}
1644
1645/**
1646 * smack_task_setscheduler - Smack check on setting scheduler
1647 * @p: the task object
1648 * @policy: unused
1649 * @lp: unused
1650 *
1651 * Return 0 if read access is permitted
1652 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001653static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001654{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001655 int rc;
1656
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001657 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001658 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001659 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001660 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001661}
1662
1663/**
1664 * smack_task_getscheduler - Smack check on reading scheduler
1665 * @p: the task object
1666 *
1667 * Return 0 if read access is permitted
1668 */
1669static int smack_task_getscheduler(struct task_struct *p)
1670{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001671 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001672}
1673
1674/**
1675 * smack_task_movememory - Smack check on moving memory
1676 * @p: the task object
1677 *
1678 * Return 0 if write access is permitted
1679 */
1680static int smack_task_movememory(struct task_struct *p)
1681{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001682 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001683}
1684
1685/**
1686 * smack_task_kill - Smack check on signal delivery
1687 * @p: the task object
1688 * @info: unused
1689 * @sig: unused
1690 * @secid: identifies the smack to use in lieu of current's
1691 *
1692 * Return 0 if write access is permitted
1693 *
1694 * The secid behavior is an artifact of an SELinux hack
1695 * in the USB code. Someday it may go away.
1696 */
1697static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1698 int sig, u32 secid)
1699{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001700 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001701 struct smack_known *skp;
1702 struct smack_known *tkp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001703
1704 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1705 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001706 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001707 * Sending a signal requires that the sender
1708 * can write the receiver.
1709 */
1710 if (secid == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001711 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001712 /*
1713 * If the secid isn't 0 we're dealing with some USB IO
1714 * specific behavior. This is not clean. For one thing
1715 * we can't take privilege into account.
1716 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001717 skp = smack_from_secid(secid);
1718 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001719}
1720
1721/**
1722 * smack_task_wait - Smack access check for waiting
1723 * @p: task to wait for
1724 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001725 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08001726 */
1727static int smack_task_wait(struct task_struct *p)
1728{
Casey Schauflere114e472008-02-04 22:29:50 -08001729 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001730 * Allow the operation to succeed.
1731 * Zombies are bad.
1732 * In userless environments (e.g. phones) programs
1733 * get marked with SMACK64EXEC and even if the parent
1734 * and child shouldn't be talking the parent still
1735 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08001736 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001737 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001738}
1739
1740/**
1741 * smack_task_to_inode - copy task smack into the inode blob
1742 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001743 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001744 *
1745 * Sets the smack pointer in the inode security blob
1746 */
1747static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1748{
1749 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001750 struct smack_known *skp = smk_of_task(task_security(p));
1751
1752 isp->smk_inode = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001753}
1754
1755/*
1756 * Socket hooks.
1757 */
1758
1759/**
1760 * smack_sk_alloc_security - Allocate a socket blob
1761 * @sk: the socket
1762 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001763 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001764 *
1765 * Assign Smack pointers to current
1766 *
1767 * Returns 0 on success, -ENOMEM is there's no memory
1768 */
1769static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1770{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001771 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001772 struct socket_smack *ssp;
1773
1774 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1775 if (ssp == NULL)
1776 return -ENOMEM;
1777
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001778 ssp->smk_in = skp->smk_known;
1779 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001780 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001781
1782 sk->sk_security = ssp;
1783
1784 return 0;
1785}
1786
1787/**
1788 * smack_sk_free_security - Free a socket blob
1789 * @sk: the socket
1790 *
1791 * Clears the blob pointer
1792 */
1793static void smack_sk_free_security(struct sock *sk)
1794{
1795 kfree(sk->sk_security);
1796}
1797
1798/**
Paul Moore07feee82009-03-27 17:10:54 -04001799* smack_host_label - check host based restrictions
1800* @sip: the object end
1801*
1802* looks for host based access restrictions
1803*
1804* This version will only be appropriate for really small sets of single label
1805* hosts. The caller is responsible for ensuring that the RCU read lock is
1806* taken before calling this function.
1807*
1808* Returns the label of the far end or NULL if it's not special.
1809*/
1810static char *smack_host_label(struct sockaddr_in *sip)
1811{
1812 struct smk_netlbladdr *snp;
1813 struct in_addr *siap = &sip->sin_addr;
1814
1815 if (siap->s_addr == 0)
1816 return NULL;
1817
1818 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1819 /*
1820 * we break after finding the first match because
1821 * the list is sorted from longest to shortest mask
1822 * so we have found the most specific match
1823 */
1824 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001825 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1826 /* we have found the special CIPSO option */
1827 if (snp->smk_label == smack_cipso_option)
1828 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001829 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001830 }
Paul Moore07feee82009-03-27 17:10:54 -04001831
1832 return NULL;
1833}
1834
1835/**
Casey Schauflere114e472008-02-04 22:29:50 -08001836 * smack_netlabel - Set the secattr on a socket
1837 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001838 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001839 *
1840 * Convert the outbound smack value (smk_out) to a
1841 * secattr and attach it to the socket.
1842 *
1843 * Returns 0 on success or an error code
1844 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001845static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001846{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001847 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001848 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001849 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001850
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001851 /*
1852 * Usually the netlabel code will handle changing the
1853 * packet labeling based on the label.
1854 * The case of a single label host is different, because
1855 * a single label host should never get a labeled packet
1856 * even though the label is usually associated with a packet
1857 * label.
1858 */
1859 local_bh_disable();
1860 bh_lock_sock_nested(sk);
1861
1862 if (ssp->smk_out == smack_net_ambient ||
1863 labeled == SMACK_UNLABELED_SOCKET)
1864 netlbl_sock_delattr(sk);
1865 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001866 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001867 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001868 }
1869
1870 bh_unlock_sock(sk);
1871 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001872
Casey Schauflere114e472008-02-04 22:29:50 -08001873 return rc;
1874}
1875
1876/**
Paul Moore07feee82009-03-27 17:10:54 -04001877 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1878 * @sk: the socket
1879 * @sap: the destination address
1880 *
1881 * Set the correct secattr for the given socket based on the destination
1882 * address and perform any outbound access checks needed.
1883 *
1884 * Returns 0 on success or an error code.
1885 *
1886 */
1887static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1888{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001889 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001890 int rc;
1891 int sk_lbl;
1892 char *hostsp;
1893 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001894 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001895
1896 rcu_read_lock();
1897 hostsp = smack_host_label(sap);
1898 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001899#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001900 struct lsm_network_audit net;
1901
Eric Paris48c62af2012-04-02 13:15:44 -04001902 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1903 ad.a.u.net->family = sap->sin_family;
1904 ad.a.u.net->dport = sap->sin_port;
1905 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001906#endif
Kees Cook923e9a12012-04-10 13:26:44 -07001907 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001908 skp = ssp->smk_out;
1909 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001910 } else {
1911 sk_lbl = SMACK_CIPSO_SOCKET;
1912 rc = 0;
1913 }
1914 rcu_read_unlock();
1915 if (rc != 0)
1916 return rc;
1917
1918 return smack_netlabel(sk, sk_lbl);
1919}
1920
1921/**
Casey Schauflerc6739442013-05-22 18:42:56 -07001922 * smk_ipv6_port_label - Smack port access table management
1923 * @sock: socket
1924 * @address: address
1925 *
1926 * Create or update the port list entry
1927 */
1928static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1929{
1930 struct sock *sk = sock->sk;
1931 struct sockaddr_in6 *addr6;
1932 struct socket_smack *ssp = sock->sk->sk_security;
1933 struct smk_port_label *spp;
1934 unsigned short port = 0;
1935
1936 if (address == NULL) {
1937 /*
1938 * This operation is changing the Smack information
1939 * on the bound socket. Take the changes to the port
1940 * as well.
1941 */
1942 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1943 if (sk != spp->smk_sock)
1944 continue;
1945 spp->smk_in = ssp->smk_in;
1946 spp->smk_out = ssp->smk_out;
1947 return;
1948 }
1949 /*
1950 * A NULL address is only used for updating existing
1951 * bound entries. If there isn't one, it's OK.
1952 */
1953 return;
1954 }
1955
1956 addr6 = (struct sockaddr_in6 *)address;
1957 port = ntohs(addr6->sin6_port);
1958 /*
1959 * This is a special case that is safely ignored.
1960 */
1961 if (port == 0)
1962 return;
1963
1964 /*
1965 * Look for an existing port list entry.
1966 * This is an indication that a port is getting reused.
1967 */
1968 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1969 if (spp->smk_port != port)
1970 continue;
1971 spp->smk_port = port;
1972 spp->smk_sock = sk;
1973 spp->smk_in = ssp->smk_in;
1974 spp->smk_out = ssp->smk_out;
1975 return;
1976 }
1977
1978 /*
1979 * A new port entry is required.
1980 */
1981 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1982 if (spp == NULL)
1983 return;
1984
1985 spp->smk_port = port;
1986 spp->smk_sock = sk;
1987 spp->smk_in = ssp->smk_in;
1988 spp->smk_out = ssp->smk_out;
1989
1990 list_add(&spp->list, &smk_ipv6_port_list);
1991 return;
1992}
1993
1994/**
1995 * smk_ipv6_port_check - check Smack port access
1996 * @sock: socket
1997 * @address: address
1998 *
1999 * Create or update the port list entry
2000 */
2001static int smk_ipv6_port_check(struct sock *sk, struct sockaddr *address,
2002 int act)
2003{
2004 __be16 *bep;
2005 __be32 *be32p;
2006 struct sockaddr_in6 *addr6;
2007 struct smk_port_label *spp;
2008 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002009 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002010 unsigned short port = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002011 char *object;
2012 struct smk_audit_info ad;
2013#ifdef CONFIG_AUDIT
2014 struct lsm_network_audit net;
2015#endif
2016
2017 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002018 skp = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002019 object = ssp->smk_in;
2020 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002021 skp = ssp->smk_out;
2022 object = smack_net_ambient->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002023 }
2024
2025 /*
2026 * Get the IP address and port from the address.
2027 */
2028 addr6 = (struct sockaddr_in6 *)address;
2029 port = ntohs(addr6->sin6_port);
2030 bep = (__be16 *)(&addr6->sin6_addr);
2031 be32p = (__be32 *)(&addr6->sin6_addr);
2032
2033 /*
2034 * It's remote, so port lookup does no good.
2035 */
2036 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2037 goto auditout;
2038
2039 /*
2040 * It's local so the send check has to have passed.
2041 */
2042 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002043 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002044 goto auditout;
2045 }
2046
2047 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2048 if (spp->smk_port != port)
2049 continue;
2050 object = spp->smk_in;
2051 if (act == SMK_CONNECTING)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002052 ssp->smk_packet = spp->smk_out->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002053 break;
2054 }
2055
2056auditout:
2057
2058#ifdef CONFIG_AUDIT
2059 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2060 ad.a.u.net->family = sk->sk_family;
2061 ad.a.u.net->dport = port;
2062 if (act == SMK_RECEIVING)
2063 ad.a.u.net->v6info.saddr = addr6->sin6_addr;
2064 else
2065 ad.a.u.net->v6info.daddr = addr6->sin6_addr;
2066#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002067 return smk_access(skp, object, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07002068}
2069
2070/**
Casey Schauflere114e472008-02-04 22:29:50 -08002071 * smack_inode_setsecurity - set smack xattrs
2072 * @inode: the object
2073 * @name: attribute name
2074 * @value: attribute value
2075 * @size: size of the attribute
2076 * @flags: unused
2077 *
2078 * Sets the named attribute in the appropriate blob
2079 *
2080 * Returns 0 on success, or an error code
2081 */
2082static int smack_inode_setsecurity(struct inode *inode, const char *name,
2083 const void *value, size_t size, int flags)
2084{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002085 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002086 struct inode_smack *nsp = inode->i_security;
2087 struct socket_smack *ssp;
2088 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002089 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002090
Casey Schauflerf7112e62012-05-06 15:22:02 -07002091 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08002092 return -EACCES;
2093
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002094 skp = smk_import_entry(value, size);
2095 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002096 return -EINVAL;
2097
2098 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002099 nsp->smk_inode = skp->smk_known;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002100 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002101 return 0;
2102 }
2103 /*
2104 * The rest of the Smack xattrs are only on sockets.
2105 */
2106 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2107 return -EOPNOTSUPP;
2108
2109 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002110 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002111 return -EOPNOTSUPP;
2112
2113 ssp = sock->sk->sk_security;
2114
2115 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002116 ssp->smk_in = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002117 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002118 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002119 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002120 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2121 if (rc != 0)
2122 printk(KERN_WARNING
2123 "Smack: \"%s\" netlbl error %d.\n",
2124 __func__, -rc);
2125 }
Casey Schauflere114e472008-02-04 22:29:50 -08002126 } else
2127 return -EOPNOTSUPP;
2128
Casey Schauflerc6739442013-05-22 18:42:56 -07002129 if (sock->sk->sk_family == PF_INET6)
2130 smk_ipv6_port_label(sock, NULL);
2131
Casey Schauflere114e472008-02-04 22:29:50 -08002132 return 0;
2133}
2134
2135/**
2136 * smack_socket_post_create - finish socket setup
2137 * @sock: the socket
2138 * @family: protocol family
2139 * @type: unused
2140 * @protocol: unused
2141 * @kern: unused
2142 *
2143 * Sets the netlabel information on the socket
2144 *
2145 * Returns 0 on success, and error code otherwise
2146 */
2147static int smack_socket_post_create(struct socket *sock, int family,
2148 int type, int protocol, int kern)
2149{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002150 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002151 return 0;
2152 /*
2153 * Set the outbound netlbl.
2154 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002155 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2156}
2157
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002158/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002159 * smack_socket_bind - record port binding information.
2160 * @sock: the socket
2161 * @address: the port address
2162 * @addrlen: size of the address
2163 *
2164 * Records the label bound to a port.
2165 *
2166 * Returns 0
2167 */
2168static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2169 int addrlen)
2170{
2171 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2172 smk_ipv6_port_label(sock, address);
2173
2174 return 0;
2175}
2176
2177/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002178 * smack_socket_connect - connect access check
2179 * @sock: the socket
2180 * @sap: the other end
2181 * @addrlen: size of sap
2182 *
2183 * Verifies that a connection may be possible
2184 *
2185 * Returns 0 on success, and error code otherwise
2186 */
2187static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2188 int addrlen)
2189{
Casey Schauflerc6739442013-05-22 18:42:56 -07002190 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002191
Casey Schauflerc6739442013-05-22 18:42:56 -07002192 if (sock->sk == NULL)
2193 return 0;
2194
2195 switch (sock->sk->sk_family) {
2196 case PF_INET:
2197 if (addrlen < sizeof(struct sockaddr_in))
2198 return -EINVAL;
2199 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2200 break;
2201 case PF_INET6:
2202 if (addrlen < sizeof(struct sockaddr_in6))
2203 return -EINVAL;
2204 rc = smk_ipv6_port_check(sock->sk, sap, SMK_CONNECTING);
2205 break;
2206 }
2207 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002208}
2209
2210/**
2211 * smack_flags_to_may - convert S_ to MAY_ values
2212 * @flags: the S_ value
2213 *
2214 * Returns the equivalent MAY_ value
2215 */
2216static int smack_flags_to_may(int flags)
2217{
2218 int may = 0;
2219
2220 if (flags & S_IRUGO)
2221 may |= MAY_READ;
2222 if (flags & S_IWUGO)
2223 may |= MAY_WRITE;
2224 if (flags & S_IXUGO)
2225 may |= MAY_EXEC;
2226
2227 return may;
2228}
2229
2230/**
2231 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2232 * @msg: the object
2233 *
2234 * Returns 0
2235 */
2236static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2237{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002238 struct smack_known *skp = smk_of_current();
2239
2240 msg->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002241 return 0;
2242}
2243
2244/**
2245 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2246 * @msg: the object
2247 *
2248 * Clears the blob pointer
2249 */
2250static void smack_msg_msg_free_security(struct msg_msg *msg)
2251{
2252 msg->security = NULL;
2253}
2254
2255/**
2256 * smack_of_shm - the smack pointer for the shm
2257 * @shp: the object
2258 *
2259 * Returns a pointer to the smack value
2260 */
2261static char *smack_of_shm(struct shmid_kernel *shp)
2262{
2263 return (char *)shp->shm_perm.security;
2264}
2265
2266/**
2267 * smack_shm_alloc_security - Set the security blob for shm
2268 * @shp: the object
2269 *
2270 * Returns 0
2271 */
2272static int smack_shm_alloc_security(struct shmid_kernel *shp)
2273{
2274 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002275 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002276
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002277 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002278 return 0;
2279}
2280
2281/**
2282 * smack_shm_free_security - Clear the security blob for shm
2283 * @shp: the object
2284 *
2285 * Clears the blob pointer
2286 */
2287static void smack_shm_free_security(struct shmid_kernel *shp)
2288{
2289 struct kern_ipc_perm *isp = &shp->shm_perm;
2290
2291 isp->security = NULL;
2292}
2293
2294/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002295 * smk_curacc_shm : check if current has access on shm
2296 * @shp : the object
2297 * @access : access requested
2298 *
2299 * Returns 0 if current has the requested access, error code otherwise
2300 */
2301static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2302{
2303 char *ssp = smack_of_shm(shp);
2304 struct smk_audit_info ad;
2305
2306#ifdef CONFIG_AUDIT
2307 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2308 ad.a.u.ipc_id = shp->shm_perm.id;
2309#endif
2310 return smk_curacc(ssp, access, &ad);
2311}
2312
2313/**
Casey Schauflere114e472008-02-04 22:29:50 -08002314 * smack_shm_associate - Smack access check for shm
2315 * @shp: the object
2316 * @shmflg: access requested
2317 *
2318 * Returns 0 if current has the requested access, error code otherwise
2319 */
2320static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2321{
Casey Schauflere114e472008-02-04 22:29:50 -08002322 int may;
2323
2324 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002325 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002326}
2327
2328/**
2329 * smack_shm_shmctl - Smack access check for shm
2330 * @shp: the object
2331 * @cmd: what it wants to do
2332 *
2333 * Returns 0 if current has the requested access, error code otherwise
2334 */
2335static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2336{
Casey Schauflere114e472008-02-04 22:29:50 -08002337 int may;
2338
2339 switch (cmd) {
2340 case IPC_STAT:
2341 case SHM_STAT:
2342 may = MAY_READ;
2343 break;
2344 case IPC_SET:
2345 case SHM_LOCK:
2346 case SHM_UNLOCK:
2347 case IPC_RMID:
2348 may = MAY_READWRITE;
2349 break;
2350 case IPC_INFO:
2351 case SHM_INFO:
2352 /*
2353 * System level information.
2354 */
2355 return 0;
2356 default:
2357 return -EINVAL;
2358 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002359 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002360}
2361
2362/**
2363 * smack_shm_shmat - Smack access for shmat
2364 * @shp: the object
2365 * @shmaddr: unused
2366 * @shmflg: access requested
2367 *
2368 * Returns 0 if current has the requested access, error code otherwise
2369 */
2370static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2371 int shmflg)
2372{
Casey Schauflere114e472008-02-04 22:29:50 -08002373 int may;
2374
2375 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002376 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002377}
2378
2379/**
2380 * smack_of_sem - the smack pointer for the sem
2381 * @sma: the object
2382 *
2383 * Returns a pointer to the smack value
2384 */
2385static char *smack_of_sem(struct sem_array *sma)
2386{
2387 return (char *)sma->sem_perm.security;
2388}
2389
2390/**
2391 * smack_sem_alloc_security - Set the security blob for sem
2392 * @sma: the object
2393 *
2394 * Returns 0
2395 */
2396static int smack_sem_alloc_security(struct sem_array *sma)
2397{
2398 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002399 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002400
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002401 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002402 return 0;
2403}
2404
2405/**
2406 * smack_sem_free_security - Clear the security blob for sem
2407 * @sma: the object
2408 *
2409 * Clears the blob pointer
2410 */
2411static void smack_sem_free_security(struct sem_array *sma)
2412{
2413 struct kern_ipc_perm *isp = &sma->sem_perm;
2414
2415 isp->security = NULL;
2416}
2417
2418/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002419 * smk_curacc_sem : check if current has access on sem
2420 * @sma : the object
2421 * @access : access requested
2422 *
2423 * Returns 0 if current has the requested access, error code otherwise
2424 */
2425static int smk_curacc_sem(struct sem_array *sma, int access)
2426{
2427 char *ssp = smack_of_sem(sma);
2428 struct smk_audit_info ad;
2429
2430#ifdef CONFIG_AUDIT
2431 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2432 ad.a.u.ipc_id = sma->sem_perm.id;
2433#endif
2434 return smk_curacc(ssp, access, &ad);
2435}
2436
2437/**
Casey Schauflere114e472008-02-04 22:29:50 -08002438 * smack_sem_associate - Smack access check for sem
2439 * @sma: the object
2440 * @semflg: access requested
2441 *
2442 * Returns 0 if current has the requested access, error code otherwise
2443 */
2444static int smack_sem_associate(struct sem_array *sma, int semflg)
2445{
Casey Schauflere114e472008-02-04 22:29:50 -08002446 int may;
2447
2448 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002449 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002450}
2451
2452/**
2453 * smack_sem_shmctl - Smack access check for sem
2454 * @sma: the object
2455 * @cmd: what it wants to do
2456 *
2457 * Returns 0 if current has the requested access, error code otherwise
2458 */
2459static int smack_sem_semctl(struct sem_array *sma, int cmd)
2460{
Casey Schauflere114e472008-02-04 22:29:50 -08002461 int may;
2462
2463 switch (cmd) {
2464 case GETPID:
2465 case GETNCNT:
2466 case GETZCNT:
2467 case GETVAL:
2468 case GETALL:
2469 case IPC_STAT:
2470 case SEM_STAT:
2471 may = MAY_READ;
2472 break;
2473 case SETVAL:
2474 case SETALL:
2475 case IPC_RMID:
2476 case IPC_SET:
2477 may = MAY_READWRITE;
2478 break;
2479 case IPC_INFO:
2480 case SEM_INFO:
2481 /*
2482 * System level information
2483 */
2484 return 0;
2485 default:
2486 return -EINVAL;
2487 }
2488
Etienne Bassetecfcc532009-04-08 20:40:06 +02002489 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002490}
2491
2492/**
2493 * smack_sem_semop - Smack checks of semaphore operations
2494 * @sma: the object
2495 * @sops: unused
2496 * @nsops: unused
2497 * @alter: unused
2498 *
2499 * Treated as read and write in all cases.
2500 *
2501 * Returns 0 if access is allowed, error code otherwise
2502 */
2503static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2504 unsigned nsops, int alter)
2505{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002506 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002507}
2508
2509/**
2510 * smack_msg_alloc_security - Set the security blob for msg
2511 * @msq: the object
2512 *
2513 * Returns 0
2514 */
2515static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2516{
2517 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002518 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002519
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002520 kisp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002521 return 0;
2522}
2523
2524/**
2525 * smack_msg_free_security - Clear the security blob for msg
2526 * @msq: the object
2527 *
2528 * Clears the blob pointer
2529 */
2530static void smack_msg_queue_free_security(struct msg_queue *msq)
2531{
2532 struct kern_ipc_perm *kisp = &msq->q_perm;
2533
2534 kisp->security = NULL;
2535}
2536
2537/**
2538 * smack_of_msq - the smack pointer for the msq
2539 * @msq: the object
2540 *
2541 * Returns a pointer to the smack value
2542 */
2543static char *smack_of_msq(struct msg_queue *msq)
2544{
2545 return (char *)msq->q_perm.security;
2546}
2547
2548/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002549 * smk_curacc_msq : helper to check if current has access on msq
2550 * @msq : the msq
2551 * @access : access requested
2552 *
2553 * return 0 if current has access, error otherwise
2554 */
2555static int smk_curacc_msq(struct msg_queue *msq, int access)
2556{
2557 char *msp = smack_of_msq(msq);
2558 struct smk_audit_info ad;
2559
2560#ifdef CONFIG_AUDIT
2561 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2562 ad.a.u.ipc_id = msq->q_perm.id;
2563#endif
2564 return smk_curacc(msp, access, &ad);
2565}
2566
2567/**
Casey Schauflere114e472008-02-04 22:29:50 -08002568 * smack_msg_queue_associate - Smack access check for msg_queue
2569 * @msq: the object
2570 * @msqflg: access requested
2571 *
2572 * Returns 0 if current has the requested access, error code otherwise
2573 */
2574static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2575{
Casey Schauflere114e472008-02-04 22:29:50 -08002576 int may;
2577
2578 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002579 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002580}
2581
2582/**
2583 * smack_msg_queue_msgctl - Smack access check for msg_queue
2584 * @msq: the object
2585 * @cmd: what it wants to do
2586 *
2587 * Returns 0 if current has the requested access, error code otherwise
2588 */
2589static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2590{
Casey Schauflere114e472008-02-04 22:29:50 -08002591 int may;
2592
2593 switch (cmd) {
2594 case IPC_STAT:
2595 case MSG_STAT:
2596 may = MAY_READ;
2597 break;
2598 case IPC_SET:
2599 case IPC_RMID:
2600 may = MAY_READWRITE;
2601 break;
2602 case IPC_INFO:
2603 case MSG_INFO:
2604 /*
2605 * System level information
2606 */
2607 return 0;
2608 default:
2609 return -EINVAL;
2610 }
2611
Etienne Bassetecfcc532009-04-08 20:40:06 +02002612 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002613}
2614
2615/**
2616 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2617 * @msq: the object
2618 * @msg: unused
2619 * @msqflg: access requested
2620 *
2621 * Returns 0 if current has the requested access, error code otherwise
2622 */
2623static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2624 int msqflg)
2625{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002626 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002627
Etienne Bassetecfcc532009-04-08 20:40:06 +02002628 may = smack_flags_to_may(msqflg);
2629 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002630}
2631
2632/**
2633 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2634 * @msq: the object
2635 * @msg: unused
2636 * @target: unused
2637 * @type: unused
2638 * @mode: unused
2639 *
2640 * Returns 0 if current has read and write access, error code otherwise
2641 */
2642static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2643 struct task_struct *target, long type, int mode)
2644{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002645 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002646}
2647
2648/**
2649 * smack_ipc_permission - Smack access for ipc_permission()
2650 * @ipp: the object permissions
2651 * @flag: access requested
2652 *
2653 * Returns 0 if current has read and write access, error code otherwise
2654 */
2655static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2656{
2657 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002658 int may = smack_flags_to_may(flag);
2659 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002660
Etienne Bassetecfcc532009-04-08 20:40:06 +02002661#ifdef CONFIG_AUDIT
2662 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2663 ad.a.u.ipc_id = ipp->id;
2664#endif
2665 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002666}
2667
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002668/**
2669 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002670 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002671 * @secid: where result will be saved
2672 */
2673static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2674{
2675 char *smack = ipp->security;
2676
2677 *secid = smack_to_secid(smack);
2678}
2679
Casey Schauflere114e472008-02-04 22:29:50 -08002680/**
2681 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002682 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002683 * @inode: the object
2684 *
2685 * Set the inode's security blob if it hasn't been done already.
2686 */
2687static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2688{
2689 struct super_block *sbp;
2690 struct superblock_smack *sbsp;
2691 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002692 struct smack_known *skp;
2693 struct smack_known *ckp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002694 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002695 char trattr[TRANS_TRUE_SIZE];
2696 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002697 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002698 struct dentry *dp;
2699
2700 if (inode == NULL)
2701 return;
2702
2703 isp = inode->i_security;
2704
2705 mutex_lock(&isp->smk_lock);
2706 /*
2707 * If the inode is already instantiated
2708 * take the quick way out
2709 */
2710 if (isp->smk_flags & SMK_INODE_INSTANT)
2711 goto unlockandout;
2712
2713 sbp = inode->i_sb;
2714 sbsp = sbp->s_security;
2715 /*
2716 * We're going to use the superblock default label
2717 * if there's no label on the file.
2718 */
2719 final = sbsp->smk_default;
2720
2721 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002722 * If this is the root inode the superblock
2723 * may be in the process of initialization.
2724 * If that is the case use the root value out
2725 * of the superblock.
2726 */
2727 if (opt_dentry->d_parent == opt_dentry) {
2728 isp->smk_inode = sbsp->smk_root;
2729 isp->smk_flags |= SMK_INODE_INSTANT;
2730 goto unlockandout;
2731 }
2732
2733 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002734 * This is pretty hackish.
2735 * Casey says that we shouldn't have to do
2736 * file system specific code, but it does help
2737 * with keeping it simple.
2738 */
2739 switch (sbp->s_magic) {
2740 case SMACK_MAGIC:
2741 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002742 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002743 * that the smack file system doesn't do
2744 * extended attributes.
2745 */
2746 final = smack_known_star.smk_known;
2747 break;
2748 case PIPEFS_MAGIC:
2749 /*
2750 * Casey says pipes are easy (?)
2751 */
2752 final = smack_known_star.smk_known;
2753 break;
2754 case DEVPTS_SUPER_MAGIC:
2755 /*
2756 * devpts seems content with the label of the task.
2757 * Programs that change smack have to treat the
2758 * pty with respect.
2759 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002760 final = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002761 break;
2762 case SOCKFS_MAGIC:
2763 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002764 * Socket access is controlled by the socket
2765 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002766 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002767 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002768 break;
2769 case PROC_SUPER_MAGIC:
2770 /*
2771 * Casey says procfs appears not to care.
2772 * The superblock default suffices.
2773 */
2774 break;
2775 case TMPFS_MAGIC:
2776 /*
2777 * Device labels should come from the filesystem,
2778 * but watch out, because they're volitile,
2779 * getting recreated on every reboot.
2780 */
2781 final = smack_known_star.smk_known;
2782 /*
2783 * No break.
2784 *
2785 * If a smack value has been set we want to use it,
2786 * but since tmpfs isn't giving us the opportunity
2787 * to set mount options simulate setting the
2788 * superblock default.
2789 */
2790 default:
2791 /*
2792 * This isn't an understood special case.
2793 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002794 */
2795
2796 /*
2797 * UNIX domain sockets use lower level socket data.
2798 */
2799 if (S_ISSOCK(inode->i_mode)) {
2800 final = smack_known_star.smk_known;
2801 break;
2802 }
2803 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002804 * No xattr support means, alas, no SMACK label.
2805 * Use the aforeapplied default.
2806 * It would be curious if the label of the task
2807 * does not match that assigned.
2808 */
2809 if (inode->i_op->getxattr == NULL)
2810 break;
2811 /*
2812 * Get the dentry for xattr.
2813 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002814 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002815 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2816 if (skp != NULL)
2817 final = skp->smk_known;
Casey Schaufler2267b132012-03-13 19:14:19 -07002818
2819 /*
2820 * Transmuting directory
2821 */
2822 if (S_ISDIR(inode->i_mode)) {
2823 /*
2824 * If this is a new directory and the label was
2825 * transmuted when the inode was initialized
2826 * set the transmute attribute on the directory
2827 * and mark the inode.
2828 *
2829 * If there is a transmute attribute on the
2830 * directory mark the inode.
2831 */
2832 if (isp->smk_flags & SMK_INODE_CHANGED) {
2833 isp->smk_flags &= ~SMK_INODE_CHANGED;
2834 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002835 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002836 TRANS_TRUE, TRANS_TRUE_SIZE,
2837 0);
2838 } else {
2839 rc = inode->i_op->getxattr(dp,
2840 XATTR_NAME_SMACKTRANSMUTE, trattr,
2841 TRANS_TRUE_SIZE);
2842 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2843 TRANS_TRUE_SIZE) != 0)
2844 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002845 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002846 if (rc >= 0)
2847 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002848 }
2849 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002850 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002851
Casey Schauflere114e472008-02-04 22:29:50 -08002852 dput(dp);
2853 break;
2854 }
2855
2856 if (final == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002857 isp->smk_inode = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002858 else
2859 isp->smk_inode = final;
2860
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002861 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002862
2863unlockandout:
2864 mutex_unlock(&isp->smk_lock);
2865 return;
2866}
2867
2868/**
2869 * smack_getprocattr - Smack process attribute access
2870 * @p: the object task
2871 * @name: the name of the attribute in /proc/.../attr
2872 * @value: where to put the result
2873 *
2874 * Places a copy of the task Smack into value
2875 *
2876 * Returns the length of the smack label or an error code
2877 */
2878static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2879{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002880 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08002881 char *cp;
2882 int slen;
2883
2884 if (strcmp(name, "current") != 0)
2885 return -EINVAL;
2886
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002887 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002888 if (cp == NULL)
2889 return -ENOMEM;
2890
2891 slen = strlen(cp);
2892 *value = cp;
2893 return slen;
2894}
2895
2896/**
2897 * smack_setprocattr - Smack process attribute setting
2898 * @p: the object task
2899 * @name: the name of the attribute in /proc/.../attr
2900 * @value: the value to set
2901 * @size: the size of the value
2902 *
2903 * Sets the Smack value of the task. Only setting self
2904 * is permitted and only with privilege
2905 *
2906 * Returns the length of the smack label or an error code
2907 */
2908static int smack_setprocattr(struct task_struct *p, char *name,
2909 void *value, size_t size)
2910{
Casey Schaufler676dac42010-12-02 06:43:39 -08002911 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002912 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002913 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002914
Casey Schauflere114e472008-02-04 22:29:50 -08002915 /*
2916 * Changing another process' Smack value is too dangerous
2917 * and supports no sane use case.
2918 */
2919 if (p != current)
2920 return -EPERM;
2921
Casey Schaufler1880eff2012-06-05 15:28:30 -07002922 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01002923 return -EPERM;
2924
Casey Schauflerf7112e62012-05-06 15:22:02 -07002925 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08002926 return -EINVAL;
2927
2928 if (strcmp(name, "current") != 0)
2929 return -EINVAL;
2930
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002931 skp = smk_import_entry(value, size);
2932 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002933 return -EINVAL;
2934
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002935 /*
2936 * No process is ever allowed the web ("@") label.
2937 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002938 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002939 return -EPERM;
2940
David Howellsd84f4f92008-11-14 10:39:23 +11002941 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002942 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002943 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002944
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07002945 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002946 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002947
David Howellsd84f4f92008-11-14 10:39:23 +11002948 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002949 return size;
2950}
2951
2952/**
2953 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002954 * @sock: one sock
2955 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002956 * @newsk: unused
2957 *
2958 * Return 0 if a subject with the smack of sock could access
2959 * an object with the smack of other, otherwise an error code
2960 */
David S. Miller3610cda2011-01-05 15:38:53 -08002961static int smack_unix_stream_connect(struct sock *sock,
2962 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002963{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002964 struct smack_known *skp;
James Morrisd2e7ad12011-01-10 09:46:24 +11002965 struct socket_smack *ssp = sock->sk_security;
2966 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002967 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002968 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002969 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002970
Kees Cook923e9a12012-04-10 13:26:44 -07002971#ifdef CONFIG_AUDIT
2972 struct lsm_network_audit net;
2973
Eric Paris48c62af2012-04-02 13:15:44 -04002974 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
David S. Miller3610cda2011-01-05 15:38:53 -08002975 smk_ad_setfield_u_net_sk(&ad, other);
Kees Cook923e9a12012-04-10 13:26:44 -07002976#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002977
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002978 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2979 skp = ssp->smk_out;
2980 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2981 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002982
Casey Schaufler975d5e52011-09-26 14:43:39 -07002983 /*
2984 * Cross reference the peer labels for SO_PEERSEC.
2985 */
2986 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002987 nsp->smk_packet = ssp->smk_out->smk_known;
2988 ssp->smk_packet = osp->smk_out->smk_known;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002989 }
2990
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002991 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002992}
2993
2994/**
2995 * smack_unix_may_send - Smack access on UDS
2996 * @sock: one socket
2997 * @other: the other socket
2998 *
2999 * Return 0 if a subject with the smack of sock could access
3000 * an object with the smack of other, otherwise an error code
3001 */
3002static int smack_unix_may_send(struct socket *sock, struct socket *other)
3003{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003004 struct socket_smack *ssp = sock->sk->sk_security;
3005 struct socket_smack *osp = other->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003006 struct smack_known *skp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003007 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003008
Kees Cook923e9a12012-04-10 13:26:44 -07003009#ifdef CONFIG_AUDIT
3010 struct lsm_network_audit net;
3011
Eric Paris48c62af2012-04-02 13:15:44 -04003012 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003013 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003014#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003015
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003016 if (smack_privileged(CAP_MAC_OVERRIDE))
3017 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003018
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003019 skp = ssp->smk_out;
3020 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003021}
3022
3023/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003024 * smack_socket_sendmsg - Smack check based on destination host
3025 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003026 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003027 * @size: the size of the message
3028 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003029 * Return 0 if the current subject can write to the destination host.
3030 * For IPv4 this is only a question if the destination is a single label host.
3031 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003032 */
3033static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3034 int size)
3035{
3036 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003037 struct sockaddr *sap = (struct sockaddr *) msg->msg_name;
3038 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003039
3040 /*
3041 * Perfectly reasonable for this to be NULL
3042 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003043 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003044 return 0;
3045
Casey Schauflerc6739442013-05-22 18:42:56 -07003046 switch (sip->sin_family) {
3047 case AF_INET:
3048 rc = smack_netlabel_send(sock->sk, sip);
3049 break;
3050 case AF_INET6:
3051 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3052 break;
3053 }
3054 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003055}
3056
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003057/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003058 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003059 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003060 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003061 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003062 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003063 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003064static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3065 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003066{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003067 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003068 int found = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08003069
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003070 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003071 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003072 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003073 * If there are flags but no level netlabel isn't
3074 * behaving the way we expect it to.
3075 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003076 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003077 * Without guidance regarding the smack value
3078 * for the packet fall back on the network
3079 * ambient value.
3080 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003081 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003082 list_for_each_entry(skp, &smack_known_list, list) {
3083 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003084 continue;
3085 if (memcmp(sap->attr.mls.cat,
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003086 skp->smk_netlabel.attr.mls.cat,
Casey Schauflerf7112e62012-05-06 15:22:02 -07003087 SMK_CIPSOLEN) != 0)
3088 continue;
3089 found = 1;
3090 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003091 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003092 rcu_read_unlock();
3093
3094 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003095 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003096
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003097 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003098 return &smack_known_web;
3099 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003100 }
3101 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3102 /*
3103 * Looks like a fallback, which gives us a secid.
3104 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003105 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003106 /*
3107 * This has got to be a bug because it is
3108 * impossible to specify a fallback without
3109 * specifying the label, which will ensure
3110 * it has a secid, and the only way to get a
3111 * secid is from a fallback.
3112 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003113 BUG_ON(skp == NULL);
3114 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003115 }
3116 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003117 * Without guidance regarding the smack value
3118 * for the packet fall back on the network
3119 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003120 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003121 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003122}
3123
Casey Schauflerc6739442013-05-22 18:42:56 -07003124static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr *sap)
3125{
3126 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
3127 u8 nexthdr;
3128 int offset;
3129 int proto = -EINVAL;
3130 struct ipv6hdr _ipv6h;
3131 struct ipv6hdr *ip6;
3132 __be16 frag_off;
3133 struct tcphdr _tcph, *th;
3134 struct udphdr _udph, *uh;
3135 struct dccp_hdr _dccph, *dh;
3136
3137 sip->sin6_port = 0;
3138
3139 offset = skb_network_offset(skb);
3140 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3141 if (ip6 == NULL)
3142 return -EINVAL;
3143 sip->sin6_addr = ip6->saddr;
3144
3145 nexthdr = ip6->nexthdr;
3146 offset += sizeof(_ipv6h);
3147 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3148 if (offset < 0)
3149 return -EINVAL;
3150
3151 proto = nexthdr;
3152 switch (proto) {
3153 case IPPROTO_TCP:
3154 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3155 if (th != NULL)
3156 sip->sin6_port = th->source;
3157 break;
3158 case IPPROTO_UDP:
3159 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3160 if (uh != NULL)
3161 sip->sin6_port = uh->source;
3162 break;
3163 case IPPROTO_DCCP:
3164 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3165 if (dh != NULL)
3166 sip->sin6_port = dh->dccph_sport;
3167 break;
3168 }
3169 return proto;
3170}
3171
Casey Schauflere114e472008-02-04 22:29:50 -08003172/**
3173 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3174 * @sk: socket
3175 * @skb: packet
3176 *
3177 * Returns 0 if the packet should be delivered, an error code otherwise
3178 */
3179static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3180{
3181 struct netlbl_lsm_secattr secattr;
3182 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003183 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07003184 struct sockaddr sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003185 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003186 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003187#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003188 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003189#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003190 switch (sk->sk_family) {
3191 case PF_INET:
3192 /*
3193 * Translate what netlabel gave us.
3194 */
3195 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003196
Casey Schauflerc6739442013-05-22 18:42:56 -07003197 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3198 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003199 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003200 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003201 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003202
Casey Schauflerc6739442013-05-22 18:42:56 -07003203 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003204
Etienne Bassetecfcc532009-04-08 20:40:06 +02003205#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003206 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3207 ad.a.u.net->family = sk->sk_family;
3208 ad.a.u.net->netif = skb->skb_iif;
3209 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003210#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003211 /*
3212 * Receiving a packet requires that the other end
3213 * be able to write here. Read access is not required.
3214 * This is the simplist possible security model
3215 * for networking.
3216 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003217 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07003218 if (rc != 0)
3219 netlbl_skbuff_err(skb, rc, 0);
3220 break;
3221 case PF_INET6:
3222 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3223 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3224 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3225 else
3226 rc = 0;
3227 break;
3228 }
Paul Moorea8134292008-10-10 10:16:31 -04003229 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003230}
3231
3232/**
3233 * smack_socket_getpeersec_stream - pull in packet label
3234 * @sock: the socket
3235 * @optval: user's destination
3236 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003237 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003238 *
3239 * returns zero on success, an error code otherwise
3240 */
3241static int smack_socket_getpeersec_stream(struct socket *sock,
3242 char __user *optval,
3243 int __user *optlen, unsigned len)
3244{
3245 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003246 char *rcp = "";
3247 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003248 int rc = 0;
3249
3250 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003251 if (ssp->smk_packet != NULL) {
3252 rcp = ssp->smk_packet;
3253 slen = strlen(rcp) + 1;
3254 }
Casey Schauflere114e472008-02-04 22:29:50 -08003255
3256 if (slen > len)
3257 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003258 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003259 rc = -EFAULT;
3260
3261 if (put_user(slen, optlen) != 0)
3262 rc = -EFAULT;
3263
3264 return rc;
3265}
3266
3267
3268/**
3269 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003270 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003271 * @skb: packet data
3272 * @secid: pointer to where to put the secid of the packet
3273 *
3274 * Sets the netlabel socket state on sk from parent
3275 */
3276static int smack_socket_getpeersec_dgram(struct socket *sock,
3277 struct sk_buff *skb, u32 *secid)
3278
3279{
3280 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003281 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003282 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003283 int family = PF_UNSPEC;
3284 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003285 int rc;
3286
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003287 if (skb != NULL) {
3288 if (skb->protocol == htons(ETH_P_IP))
3289 family = PF_INET;
3290 else if (skb->protocol == htons(ETH_P_IPV6))
3291 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003292 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003293 if (family == PF_UNSPEC && sock != NULL)
3294 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003295
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003296 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003297 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003298 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003299 } else if (family == PF_INET || family == PF_INET6) {
3300 /*
3301 * Translate what netlabel gave us.
3302 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003303 if (sock != NULL && sock->sk != NULL)
3304 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003305 netlbl_secattr_init(&secattr);
3306 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3307 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003308 skp = smack_from_secattr(&secattr, ssp);
3309 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003310 }
3311 netlbl_secattr_destroy(&secattr);
3312 }
3313 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003314 if (s == 0)
3315 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003316 return 0;
3317}
3318
3319/**
Paul Moore07feee82009-03-27 17:10:54 -04003320 * smack_sock_graft - Initialize a newly created socket with an existing sock
3321 * @sk: child sock
3322 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003323 *
Paul Moore07feee82009-03-27 17:10:54 -04003324 * Set the smk_{in,out} state of an existing sock based on the process that
3325 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003326 */
3327static void smack_sock_graft(struct sock *sk, struct socket *parent)
3328{
3329 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003330 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003331
Paul Moore07feee82009-03-27 17:10:54 -04003332 if (sk == NULL ||
3333 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003334 return;
3335
3336 ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003337 ssp->smk_in = skp->smk_known;
3338 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003339 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003340}
3341
3342/**
3343 * smack_inet_conn_request - Smack access check on connect
3344 * @sk: socket involved
3345 * @skb: packet
3346 * @req: unused
3347 *
3348 * Returns 0 if a task with the packet label could write to
3349 * the socket, otherwise an error code
3350 */
3351static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3352 struct request_sock *req)
3353{
Paul Moore07feee82009-03-27 17:10:54 -04003354 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003355 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003356 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003357 struct netlbl_lsm_secattr secattr;
3358 struct sockaddr_in addr;
3359 struct iphdr *hdr;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003360 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003361 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003362 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003363#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003364 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003365#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003366
Casey Schauflerc6739442013-05-22 18:42:56 -07003367 if (family == PF_INET6) {
3368 /*
3369 * Handle mapped IPv4 packets arriving
3370 * via IPv6 sockets. Don't set up netlabel
3371 * processing on IPv6.
3372 */
3373 if (skb->protocol == htons(ETH_P_IP))
3374 family = PF_INET;
3375 else
3376 return 0;
3377 }
Casey Schauflere114e472008-02-04 22:29:50 -08003378
Paul Moore07feee82009-03-27 17:10:54 -04003379 netlbl_secattr_init(&secattr);
3380 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003381 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003382 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003383 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003384 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003385 netlbl_secattr_destroy(&secattr);
3386
Etienne Bassetecfcc532009-04-08 20:40:06 +02003387#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003388 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3389 ad.a.u.net->family = family;
3390 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003391 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3392#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003393 /*
Paul Moore07feee82009-03-27 17:10:54 -04003394 * Receiving a packet requires that the other end be able to write
3395 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003396 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003397 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003398 if (rc != 0)
3399 return rc;
3400
3401 /*
3402 * Save the peer's label in the request_sock so we can later setup
3403 * smk_packet in the child socket so that SO_PEERCRED can report it.
3404 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003405 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003406
3407 /*
3408 * We need to decide if we want to label the incoming connection here
3409 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003410 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003411 */
3412 hdr = ip_hdr(skb);
3413 addr.sin_addr.s_addr = hdr->saddr;
3414 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003415 hsp = smack_host_label(&addr);
3416 rcu_read_unlock();
3417
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003418 if (hsp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003419 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003420 else
Paul Moore07feee82009-03-27 17:10:54 -04003421 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003422
3423 return rc;
3424}
3425
Paul Moore07feee82009-03-27 17:10:54 -04003426/**
3427 * smack_inet_csk_clone - Copy the connection information to the new socket
3428 * @sk: the new socket
3429 * @req: the connection's request_sock
3430 *
3431 * Transfer the connection's peer label to the newly created socket.
3432 */
3433static void smack_inet_csk_clone(struct sock *sk,
3434 const struct request_sock *req)
3435{
3436 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003437 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003438
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003439 if (req->peer_secid != 0) {
3440 skp = smack_from_secid(req->peer_secid);
3441 ssp->smk_packet = skp->smk_known;
3442 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003443 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003444}
3445
Casey Schauflere114e472008-02-04 22:29:50 -08003446/*
3447 * Key management security hooks
3448 *
3449 * Casey has not tested key support very heavily.
3450 * The permission check is most likely too restrictive.
3451 * If you care about keys please have a look.
3452 */
3453#ifdef CONFIG_KEYS
3454
3455/**
3456 * smack_key_alloc - Set the key security blob
3457 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003458 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003459 * @flags: unused
3460 *
3461 * No allocation required
3462 *
3463 * Returns 0
3464 */
David Howellsd84f4f92008-11-14 10:39:23 +11003465static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003466 unsigned long flags)
3467{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003468 struct smack_known *skp = smk_of_task(cred->security);
3469
3470 key->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003471 return 0;
3472}
3473
3474/**
3475 * smack_key_free - Clear the key security blob
3476 * @key: the object
3477 *
3478 * Clear the blob pointer
3479 */
3480static void smack_key_free(struct key *key)
3481{
3482 key->security = NULL;
3483}
3484
3485/*
3486 * smack_key_permission - Smack access on a key
3487 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003488 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003489 * @perm: unused
3490 *
3491 * Return 0 if the task has read and write to the object,
3492 * an error code otherwise
3493 */
3494static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003495 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003496{
3497 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003498 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003499 struct smack_known *tkp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003500
3501 keyp = key_ref_to_ptr(key_ref);
3502 if (keyp == NULL)
3503 return -EINVAL;
3504 /*
3505 * If the key hasn't been initialized give it access so that
3506 * it may do so.
3507 */
3508 if (keyp->security == NULL)
3509 return 0;
3510 /*
3511 * This should not occur
3512 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003513 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003514 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003515#ifdef CONFIG_AUDIT
3516 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3517 ad.a.u.key_struct.key = keyp->serial;
3518 ad.a.u.key_struct.key_desc = keyp->description;
3519#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003520 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003521}
3522#endif /* CONFIG_KEYS */
3523
3524/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003525 * Smack Audit hooks
3526 *
3527 * Audit requires a unique representation of each Smack specific
3528 * rule. This unique representation is used to distinguish the
3529 * object to be audited from remaining kernel objects and also
3530 * works as a glue between the audit hooks.
3531 *
3532 * Since repository entries are added but never deleted, we'll use
3533 * the smack_known label address related to the given audit rule as
3534 * the needed unique representation. This also better fits the smack
3535 * model where nearly everything is a label.
3536 */
3537#ifdef CONFIG_AUDIT
3538
3539/**
3540 * smack_audit_rule_init - Initialize a smack audit rule
3541 * @field: audit rule fields given from user-space (audit.h)
3542 * @op: required testing operator (=, !=, >, <, ...)
3543 * @rulestr: smack label to be audited
3544 * @vrule: pointer to save our own audit rule representation
3545 *
3546 * Prepare to audit cases where (@field @op @rulestr) is true.
3547 * The label to be audited is created if necessay.
3548 */
3549static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3550{
3551 char **rule = (char **)vrule;
3552 *rule = NULL;
3553
3554 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3555 return -EINVAL;
3556
Al Viro5af75d82008-12-16 05:59:26 -05003557 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003558 return -EINVAL;
3559
3560 *rule = smk_import(rulestr, 0);
3561
3562 return 0;
3563}
3564
3565/**
3566 * smack_audit_rule_known - Distinguish Smack audit rules
3567 * @krule: rule of interest, in Audit kernel representation format
3568 *
3569 * This is used to filter Smack rules from remaining Audit ones.
3570 * If it's proved that this rule belongs to us, the
3571 * audit_rule_match hook will be called to do the final judgement.
3572 */
3573static int smack_audit_rule_known(struct audit_krule *krule)
3574{
3575 struct audit_field *f;
3576 int i;
3577
3578 for (i = 0; i < krule->field_count; i++) {
3579 f = &krule->fields[i];
3580
3581 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3582 return 1;
3583 }
3584
3585 return 0;
3586}
3587
3588/**
3589 * smack_audit_rule_match - Audit given object ?
3590 * @secid: security id for identifying the object to test
3591 * @field: audit rule flags given from user-space
3592 * @op: required testing operator
3593 * @vrule: smack internal rule presentation
3594 * @actx: audit context associated with the check
3595 *
3596 * The core Audit hook. It's used to take the decision of
3597 * whether to audit or not to audit a given object.
3598 */
3599static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3600 struct audit_context *actx)
3601{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003602 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003603 char *rule = vrule;
3604
3605 if (!rule) {
Tetsuo Handaceffec552012-03-29 16:19:05 +09003606 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003607 "Smack: missing rule\n");
3608 return -ENOENT;
3609 }
3610
3611 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3612 return 0;
3613
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003614 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003615
3616 /*
3617 * No need to do string comparisons. If a match occurs,
3618 * both pointers will point to the same smack_known
3619 * label.
3620 */
Al Viro5af75d82008-12-16 05:59:26 -05003621 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003622 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003623 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003624 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003625
3626 return 0;
3627}
3628
3629/**
3630 * smack_audit_rule_free - free smack rule representation
3631 * @vrule: rule to be freed.
3632 *
3633 * No memory was allocated.
3634 */
3635static void smack_audit_rule_free(void *vrule)
3636{
3637 /* No-op */
3638}
3639
3640#endif /* CONFIG_AUDIT */
3641
Randy Dunlap251a2a92009-02-18 11:42:33 -08003642/**
David Quigley746df9b2013-05-22 12:50:35 -04003643 * smack_ismaclabel - check if xattr @name references a smack MAC label
3644 * @name: Full xattr name to check.
3645 */
3646static int smack_ismaclabel(const char *name)
3647{
3648 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3649}
3650
3651
3652/**
Casey Schauflere114e472008-02-04 22:29:50 -08003653 * smack_secid_to_secctx - return the smack label for a secid
3654 * @secid: incoming integer
3655 * @secdata: destination
3656 * @seclen: how long it is
3657 *
3658 * Exists for networking code.
3659 */
3660static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3661{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003662 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003663
Eric Parisd5630b92010-10-13 16:24:48 -04003664 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003665 *secdata = skp->smk_known;
3666 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08003667 return 0;
3668}
3669
Randy Dunlap251a2a92009-02-18 11:42:33 -08003670/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003671 * smack_secctx_to_secid - return the secid for a smack label
3672 * @secdata: smack label
3673 * @seclen: how long result is
3674 * @secid: outgoing integer
3675 *
3676 * Exists for audit and networking code.
3677 */
David Howellse52c17642008-04-29 20:52:51 +01003678static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003679{
3680 *secid = smack_to_secid(secdata);
3681 return 0;
3682}
3683
Randy Dunlap251a2a92009-02-18 11:42:33 -08003684/**
Casey Schauflere114e472008-02-04 22:29:50 -08003685 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003686 * @secdata: unused
3687 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003688 *
3689 * Exists to make sure nothing gets done, and properly
3690 */
3691static void smack_release_secctx(char *secdata, u32 seclen)
3692{
3693}
3694
David P. Quigley1ee65e32009-09-03 14:25:57 -04003695static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3696{
3697 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3698}
3699
3700static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3701{
3702 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3703}
3704
3705static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3706{
3707 int len = 0;
3708 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3709
3710 if (len < 0)
3711 return len;
3712 *ctxlen = len;
3713 return 0;
3714}
3715
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003716struct security_operations smack_ops = {
3717 .name = "smack",
3718
Ingo Molnar9e488582009-05-07 19:26:19 +10003719 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003720 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003721 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003722
3723 .sb_alloc_security = smack_sb_alloc_security,
3724 .sb_free_security = smack_sb_free_security,
3725 .sb_copy_data = smack_sb_copy_data,
3726 .sb_kern_mount = smack_sb_kern_mount,
3727 .sb_statfs = smack_sb_statfs,
3728 .sb_mount = smack_sb_mount,
3729 .sb_umount = smack_sb_umount,
3730
Casey Schaufler676dac42010-12-02 06:43:39 -08003731 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003732 .bprm_committing_creds = smack_bprm_committing_creds,
3733 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003734
Casey Schauflere114e472008-02-04 22:29:50 -08003735 .inode_alloc_security = smack_inode_alloc_security,
3736 .inode_free_security = smack_inode_free_security,
3737 .inode_init_security = smack_inode_init_security,
3738 .inode_link = smack_inode_link,
3739 .inode_unlink = smack_inode_unlink,
3740 .inode_rmdir = smack_inode_rmdir,
3741 .inode_rename = smack_inode_rename,
3742 .inode_permission = smack_inode_permission,
3743 .inode_setattr = smack_inode_setattr,
3744 .inode_getattr = smack_inode_getattr,
3745 .inode_setxattr = smack_inode_setxattr,
3746 .inode_post_setxattr = smack_inode_post_setxattr,
3747 .inode_getxattr = smack_inode_getxattr,
3748 .inode_removexattr = smack_inode_removexattr,
3749 .inode_getsecurity = smack_inode_getsecurity,
3750 .inode_setsecurity = smack_inode_setsecurity,
3751 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003752 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003753
3754 .file_permission = smack_file_permission,
3755 .file_alloc_security = smack_file_alloc_security,
3756 .file_free_security = smack_file_free_security,
3757 .file_ioctl = smack_file_ioctl,
3758 .file_lock = smack_file_lock,
3759 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003760 .mmap_file = smack_mmap_file,
3761 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003762 .file_set_fowner = smack_file_set_fowner,
3763 .file_send_sigiotask = smack_file_send_sigiotask,
3764 .file_receive = smack_file_receive,
3765
Eric Paris83d49852012-04-04 13:45:40 -04003766 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003767
David Howellsee18d642009-09-02 09:14:21 +01003768 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003769 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003770 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003771 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003772 .kernel_act_as = smack_kernel_act_as,
3773 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003774 .task_setpgid = smack_task_setpgid,
3775 .task_getpgid = smack_task_getpgid,
3776 .task_getsid = smack_task_getsid,
3777 .task_getsecid = smack_task_getsecid,
3778 .task_setnice = smack_task_setnice,
3779 .task_setioprio = smack_task_setioprio,
3780 .task_getioprio = smack_task_getioprio,
3781 .task_setscheduler = smack_task_setscheduler,
3782 .task_getscheduler = smack_task_getscheduler,
3783 .task_movememory = smack_task_movememory,
3784 .task_kill = smack_task_kill,
3785 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003786 .task_to_inode = smack_task_to_inode,
3787
3788 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003789 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003790
3791 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3792 .msg_msg_free_security = smack_msg_msg_free_security,
3793
3794 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3795 .msg_queue_free_security = smack_msg_queue_free_security,
3796 .msg_queue_associate = smack_msg_queue_associate,
3797 .msg_queue_msgctl = smack_msg_queue_msgctl,
3798 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3799 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3800
3801 .shm_alloc_security = smack_shm_alloc_security,
3802 .shm_free_security = smack_shm_free_security,
3803 .shm_associate = smack_shm_associate,
3804 .shm_shmctl = smack_shm_shmctl,
3805 .shm_shmat = smack_shm_shmat,
3806
3807 .sem_alloc_security = smack_sem_alloc_security,
3808 .sem_free_security = smack_sem_free_security,
3809 .sem_associate = smack_sem_associate,
3810 .sem_semctl = smack_sem_semctl,
3811 .sem_semop = smack_sem_semop,
3812
Casey Schauflere114e472008-02-04 22:29:50 -08003813 .d_instantiate = smack_d_instantiate,
3814
3815 .getprocattr = smack_getprocattr,
3816 .setprocattr = smack_setprocattr,
3817
3818 .unix_stream_connect = smack_unix_stream_connect,
3819 .unix_may_send = smack_unix_may_send,
3820
3821 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07003822 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003823 .socket_connect = smack_socket_connect,
3824 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003825 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3826 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3827 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3828 .sk_alloc_security = smack_sk_alloc_security,
3829 .sk_free_security = smack_sk_free_security,
3830 .sock_graft = smack_sock_graft,
3831 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003832 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003833
Casey Schauflere114e472008-02-04 22:29:50 -08003834 /* key management security hooks */
3835#ifdef CONFIG_KEYS
3836 .key_alloc = smack_key_alloc,
3837 .key_free = smack_key_free,
3838 .key_permission = smack_key_permission,
3839#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003840
3841 /* Audit hooks */
3842#ifdef CONFIG_AUDIT
3843 .audit_rule_init = smack_audit_rule_init,
3844 .audit_rule_known = smack_audit_rule_known,
3845 .audit_rule_match = smack_audit_rule_match,
3846 .audit_rule_free = smack_audit_rule_free,
3847#endif /* CONFIG_AUDIT */
3848
David Quigley746df9b2013-05-22 12:50:35 -04003849 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08003850 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003851 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003852 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003853 .inode_notifysecctx = smack_inode_notifysecctx,
3854 .inode_setsecctx = smack_inode_setsecctx,
3855 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003856};
3857
Etienne Basset7198e2e2009-03-24 20:53:24 +01003858
Casey Schaufler86812bb2012-04-17 18:55:46 -07003859static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003860{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003861 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003862 * Initialize rule list locks
3863 */
3864 mutex_init(&smack_known_huh.smk_rules_lock);
3865 mutex_init(&smack_known_hat.smk_rules_lock);
3866 mutex_init(&smack_known_floor.smk_rules_lock);
3867 mutex_init(&smack_known_star.smk_rules_lock);
3868 mutex_init(&smack_known_invalid.smk_rules_lock);
3869 mutex_init(&smack_known_web.smk_rules_lock);
3870 /*
3871 * Initialize rule lists
3872 */
3873 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3874 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3875 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3876 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3877 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3878 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3879 /*
3880 * Create the known labels list
3881 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02003882 smk_insert_entry(&smack_known_huh);
3883 smk_insert_entry(&smack_known_hat);
3884 smk_insert_entry(&smack_known_star);
3885 smk_insert_entry(&smack_known_floor);
3886 smk_insert_entry(&smack_known_invalid);
3887 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01003888}
3889
Casey Schauflere114e472008-02-04 22:29:50 -08003890/**
3891 * smack_init - initialize the smack system
3892 *
3893 * Returns 0
3894 */
3895static __init int smack_init(void)
3896{
David Howellsd84f4f92008-11-14 10:39:23 +11003897 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003898 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003899
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003900 if (!security_module_enable(&smack_ops))
3901 return 0;
3902
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003903 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3904 GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003905 if (tsp == NULL)
3906 return -ENOMEM;
3907
Casey Schauflere114e472008-02-04 22:29:50 -08003908 printk(KERN_INFO "Smack: Initializing.\n");
3909
3910 /*
3911 * Set the security state for the initial task.
3912 */
David Howellsd84f4f92008-11-14 10:39:23 +11003913 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003914 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003915
Casey Schaufler86812bb2012-04-17 18:55:46 -07003916 /* initialize the smack_known_list */
3917 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003918
3919 /*
3920 * Register with LSM
3921 */
3922 if (register_security(&smack_ops))
3923 panic("smack: Unable to register with kernel.\n");
3924
3925 return 0;
3926}
3927
3928/*
3929 * Smack requires early initialization in order to label
3930 * all processes and objects when they are created.
3931 */
3932security_initcall(smack_init);