blob: 8825375cc031709b3918cd073cd574708c3f0405 [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,
Tetsuo Handa95489062013-07-25 05:44:02 +0900585 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500586 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
Tetsuo Handa95489062013-07-25 05:44:02 +0900594 if (name)
595 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800596
597 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800598 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700599 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800600 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200601
602 /*
603 * If the access rule allows transmutation and
604 * the directory requests transmutation then
605 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700606 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200607 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800608 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700609 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200610 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700611 issp->smk_flags |= SMK_INODE_CHANGED;
612 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200613
Tetsuo Handaceffec552012-03-29 16:19:05 +0900614 *value = kstrdup(isp, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800615 if (*value == NULL)
616 return -ENOMEM;
617 }
618
619 if (len)
620 *len = strlen(isp) + 1;
621
622 return 0;
623}
624
625/**
626 * smack_inode_link - Smack check on link
627 * @old_dentry: the existing object
628 * @dir: unused
629 * @new_dentry: the new object
630 *
631 * Returns 0 if access is permitted, an error code otherwise
632 */
633static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
634 struct dentry *new_dentry)
635{
Casey Schauflere114e472008-02-04 22:29:50 -0800636 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200637 struct smk_audit_info ad;
638 int rc;
639
Eric Parisa2694342011-04-25 13:10:27 -0400640 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200641 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800642
643 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200644 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800645
646 if (rc == 0 && new_dentry->d_inode != NULL) {
647 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200648 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
649 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800650 }
651
652 return rc;
653}
654
655/**
656 * smack_inode_unlink - Smack check on inode deletion
657 * @dir: containing directory object
658 * @dentry: file to unlink
659 *
660 * Returns 0 if current can write the containing directory
661 * and the object, error code otherwise
662 */
663static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
664{
665 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200666 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800667 int rc;
668
Eric Parisa2694342011-04-25 13:10:27 -0400669 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200670 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
671
Casey Schauflere114e472008-02-04 22:29:50 -0800672 /*
673 * You need write access to the thing you're unlinking
674 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200675 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
676 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800677 /*
678 * You also need write access to the containing directory
679 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400680 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200681 smk_ad_setfield_u_fs_inode(&ad, dir);
682 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
683 }
Casey Schauflere114e472008-02-04 22:29:50 -0800684 return rc;
685}
686
687/**
688 * smack_inode_rmdir - Smack check on directory deletion
689 * @dir: containing directory object
690 * @dentry: directory to unlink
691 *
692 * Returns 0 if current can write the containing directory
693 * and the directory, error code otherwise
694 */
695static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
696{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200697 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800698 int rc;
699
Eric Parisa2694342011-04-25 13:10:27 -0400700 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200701 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
702
Casey Schauflere114e472008-02-04 22:29:50 -0800703 /*
704 * You need write access to the thing you're removing
705 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200706 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
707 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800708 /*
709 * You also need write access to the containing directory
710 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400711 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200712 smk_ad_setfield_u_fs_inode(&ad, dir);
713 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
714 }
Casey Schauflere114e472008-02-04 22:29:50 -0800715
716 return rc;
717}
718
719/**
720 * smack_inode_rename - Smack check on rename
721 * @old_inode: the old directory
722 * @old_dentry: unused
723 * @new_inode: the new directory
724 * @new_dentry: unused
725 *
726 * Read and write access is required on both the old and
727 * new directories.
728 *
729 * Returns 0 if access is permitted, an error code otherwise
730 */
731static int smack_inode_rename(struct inode *old_inode,
732 struct dentry *old_dentry,
733 struct inode *new_inode,
734 struct dentry *new_dentry)
735{
736 int rc;
737 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200738 struct smk_audit_info ad;
739
Eric Parisa2694342011-04-25 13:10:27 -0400740 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200741 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800742
743 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200744 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800745
746 if (rc == 0 && new_dentry->d_inode != NULL) {
747 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200748 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
749 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800750 }
Casey Schauflere114e472008-02-04 22:29:50 -0800751 return rc;
752}
753
754/**
755 * smack_inode_permission - Smack version of permission()
756 * @inode: the inode in question
757 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800758 *
759 * This is the important Smack hook.
760 *
761 * Returns 0 if access is permitted, -EACCES otherwise
762 */
Al Viroe74f71e2011-06-20 19:38:15 -0400763static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800764{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200765 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400766 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400767
768 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800769 /*
770 * No permission to check. Existence test. Yup, it's there.
771 */
772 if (mask == 0)
773 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700774
775 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400776 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700777 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400778 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200779 smk_ad_setfield_u_fs_inode(&ad, inode);
780 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800781}
782
783/**
784 * smack_inode_setattr - Smack check for setting attributes
785 * @dentry: the object
786 * @iattr: for the force flag
787 *
788 * Returns 0 if access is permitted, an error code otherwise
789 */
790static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
791{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200792 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800793 /*
794 * Need to allow for clearing the setuid bit.
795 */
796 if (iattr->ia_valid & ATTR_FORCE)
797 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400798 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200799 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800800
Etienne Bassetecfcc532009-04-08 20:40:06 +0200801 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800802}
803
804/**
805 * smack_inode_getattr - Smack check for getting attributes
806 * @mnt: unused
807 * @dentry: the object
808 *
809 * Returns 0 if access is permitted, an error code otherwise
810 */
811static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
812{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200813 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400814 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200815
Eric Parisa2694342011-04-25 13:10:27 -0400816 path.dentry = dentry;
817 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200818
Eric Parisf48b7392011-04-25 12:54:27 -0400819 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400820 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200821 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800822}
823
824/**
825 * smack_inode_setxattr - Smack check for setting xattrs
826 * @dentry: the object
827 * @name: name of the attribute
828 * @value: unused
829 * @size: unused
830 * @flags: unused
831 *
832 * This protects the Smack attribute explicitly.
833 *
834 * Returns 0 if access is permitted, an error code otherwise
835 */
David Howells8f0cfa52008-04-29 00:59:41 -0700836static int smack_inode_setxattr(struct dentry *dentry, const char *name,
837 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800838{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200839 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800840 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800841
Casey Schauflerbcdca222008-02-23 15:24:04 -0800842 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
843 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800844 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800845 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
846 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700847 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800848 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200849 /*
850 * check label validity here so import wont fail on
851 * post_setxattr
852 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700853 if (size == 0 || size >= SMK_LONGLABEL ||
Etienne Bassetdefc4332009-04-16 23:58:42 +0200854 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400855 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200856 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700857 if (!smack_privileged(CAP_MAC_ADMIN))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200858 rc = -EPERM;
859 if (size != TRANS_TRUE_SIZE ||
860 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
861 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800862 } else
863 rc = cap_inode_setxattr(dentry, name, value, size, flags);
864
Eric Parisa2694342011-04-25 13:10:27 -0400865 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200866 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
867
Casey Schauflerbcdca222008-02-23 15:24:04 -0800868 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200869 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800870
871 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800872}
873
874/**
875 * smack_inode_post_setxattr - Apply the Smack update approved above
876 * @dentry: object
877 * @name: attribute name
878 * @value: attribute value
879 * @size: attribute size
880 * @flags: unused
881 *
882 * Set the pointer in the inode blob to the entry found
883 * in the master label list.
884 */
David Howells8f0cfa52008-04-29 00:59:41 -0700885static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
886 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800887{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700888 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200889 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800890
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700891 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
892 isp->smk_flags |= SMK_INODE_TRANSMUTE;
893 return;
894 }
895
896 skp = smk_import_entry(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800897 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700898 if (skp != NULL)
899 isp->smk_inode = skp->smk_known;
Casey Schaufler676dac42010-12-02 06:43:39 -0800900 else
901 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200902 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700903 if (skp != NULL)
904 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800905 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700906 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800907 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700908 if (skp != NULL)
909 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800910 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700911 isp->smk_mmap = &smack_known_invalid;
912 }
Casey Schauflere114e472008-02-04 22:29:50 -0800913
914 return;
915}
916
Casey Schauflerce8a4322011-09-29 18:21:01 -0700917/**
Casey Schauflere114e472008-02-04 22:29:50 -0800918 * smack_inode_getxattr - Smack check on getxattr
919 * @dentry: the object
920 * @name: unused
921 *
922 * Returns 0 if access is permitted, an error code otherwise
923 */
David Howells8f0cfa52008-04-29 00:59:41 -0700924static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800925{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200926 struct smk_audit_info ad;
927
Eric Parisa2694342011-04-25 13:10:27 -0400928 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200929 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
930
931 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800932}
933
Casey Schauflerce8a4322011-09-29 18:21:01 -0700934/**
Casey Schauflere114e472008-02-04 22:29:50 -0800935 * smack_inode_removexattr - Smack check on removexattr
936 * @dentry: the object
937 * @name: name of the attribute
938 *
939 * Removing the Smack attribute requires CAP_MAC_ADMIN
940 *
941 * Returns 0 if access is permitted, an error code otherwise
942 */
David Howells8f0cfa52008-04-29 00:59:41 -0700943static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800944{
Casey Schaufler676dac42010-12-02 06:43:39 -0800945 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200946 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800947 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800948
Casey Schauflerbcdca222008-02-23 15:24:04 -0800949 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
950 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800951 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200952 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800953 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
954 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700955 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800956 rc = -EPERM;
957 } else
958 rc = cap_inode_removexattr(dentry, name);
959
Eric Parisa2694342011-04-25 13:10:27 -0400960 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200961 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800962 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200963 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800964
Casey Schaufler676dac42010-12-02 06:43:39 -0800965 if (rc == 0) {
966 isp = dentry->d_inode->i_security;
967 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800968 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800969 }
970
Casey Schauflerbcdca222008-02-23 15:24:04 -0800971 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800972}
973
974/**
975 * smack_inode_getsecurity - get smack xattrs
976 * @inode: the object
977 * @name: attribute name
978 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800979 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800980 *
981 * Returns the size of the attribute or an error code
982 */
983static int smack_inode_getsecurity(const struct inode *inode,
984 const char *name, void **buffer,
985 bool alloc)
986{
987 struct socket_smack *ssp;
988 struct socket *sock;
989 struct super_block *sbp;
990 struct inode *ip = (struct inode *)inode;
991 char *isp;
992 int ilen;
993 int rc = 0;
994
995 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
996 isp = smk_of_inode(inode);
997 ilen = strlen(isp) + 1;
998 *buffer = isp;
999 return ilen;
1000 }
1001
1002 /*
1003 * The rest of the Smack xattrs are only on sockets.
1004 */
1005 sbp = ip->i_sb;
1006 if (sbp->s_magic != SOCKFS_MAGIC)
1007 return -EOPNOTSUPP;
1008
1009 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001010 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001011 return -EOPNOTSUPP;
1012
1013 ssp = sock->sk->sk_security;
1014
1015 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1016 isp = ssp->smk_in;
1017 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001018 isp = ssp->smk_out->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001019 else
1020 return -EOPNOTSUPP;
1021
1022 ilen = strlen(isp) + 1;
1023 if (rc == 0) {
1024 *buffer = isp;
1025 rc = ilen;
1026 }
1027
1028 return rc;
1029}
1030
1031
1032/**
1033 * smack_inode_listsecurity - list the Smack attributes
1034 * @inode: the object
1035 * @buffer: where they go
1036 * @buffer_size: size of buffer
1037 *
1038 * Returns 0 on success, -EINVAL otherwise
1039 */
1040static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1041 size_t buffer_size)
1042{
1043 int len = strlen(XATTR_NAME_SMACK);
1044
1045 if (buffer != NULL && len <= buffer_size) {
1046 memcpy(buffer, XATTR_NAME_SMACK, len);
1047 return len;
1048 }
1049 return -EINVAL;
1050}
1051
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001052/**
1053 * smack_inode_getsecid - Extract inode's security id
1054 * @inode: inode to extract the info from
1055 * @secid: where result will be saved
1056 */
1057static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1058{
1059 struct inode_smack *isp = inode->i_security;
1060
1061 *secid = smack_to_secid(isp->smk_inode);
1062}
1063
Casey Schauflere114e472008-02-04 22:29:50 -08001064/*
1065 * File Hooks
1066 */
1067
1068/**
1069 * smack_file_permission - Smack check on file operations
1070 * @file: unused
1071 * @mask: unused
1072 *
1073 * Returns 0
1074 *
1075 * Should access checks be done on each read or write?
1076 * UNICOS and SELinux say yes.
1077 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1078 *
1079 * I'll say no for now. Smack does not do the frequent
1080 * label changing that SELinux does.
1081 */
1082static int smack_file_permission(struct file *file, int mask)
1083{
1084 return 0;
1085}
1086
1087/**
1088 * smack_file_alloc_security - assign a file security blob
1089 * @file: the object
1090 *
1091 * The security blob for a file is a pointer to the master
1092 * label list, so no allocation is done.
1093 *
1094 * Returns 0
1095 */
1096static int smack_file_alloc_security(struct file *file)
1097{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001098 struct smack_known *skp = smk_of_current();
1099
1100 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001101 return 0;
1102}
1103
1104/**
1105 * smack_file_free_security - clear a file security blob
1106 * @file: the object
1107 *
1108 * The security blob for a file is a pointer to the master
1109 * label list, so no memory is freed.
1110 */
1111static void smack_file_free_security(struct file *file)
1112{
1113 file->f_security = NULL;
1114}
1115
1116/**
1117 * smack_file_ioctl - Smack check on ioctls
1118 * @file: the object
1119 * @cmd: what to do
1120 * @arg: unused
1121 *
1122 * Relies heavily on the correct use of the ioctl command conventions.
1123 *
1124 * Returns 0 if allowed, error code otherwise
1125 */
1126static int smack_file_ioctl(struct file *file, unsigned int cmd,
1127 unsigned long arg)
1128{
1129 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001130 struct smk_audit_info ad;
1131
Eric Parisf48b7392011-04-25 12:54:27 -04001132 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001133 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001134
1135 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001136 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001137
1138 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001139 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001140
1141 return rc;
1142}
1143
1144/**
1145 * smack_file_lock - Smack check on file locking
1146 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001147 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001148 *
1149 * Returns 0 if current has write access, error code otherwise
1150 */
1151static int smack_file_lock(struct file *file, unsigned int cmd)
1152{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001153 struct smk_audit_info ad;
1154
Eric Paris92f42502011-04-25 13:15:55 -04001155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1156 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001157 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001158}
1159
1160/**
1161 * smack_file_fcntl - Smack check on fcntl
1162 * @file: the object
1163 * @cmd: what action to check
1164 * @arg: unused
1165 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001166 * Generally these operations are harmless.
1167 * File locking operations present an obvious mechanism
1168 * for passing information, so they require write access.
1169 *
Casey Schauflere114e472008-02-04 22:29:50 -08001170 * Returns 0 if current has access, error code otherwise
1171 */
1172static int smack_file_fcntl(struct file *file, unsigned int cmd,
1173 unsigned long arg)
1174{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001175 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001176 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001177
Etienne Bassetecfcc532009-04-08 20:40:06 +02001178
Casey Schauflere114e472008-02-04 22:29:50 -08001179 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001180 case F_GETLK:
Casey Schauflere114e472008-02-04 22:29:50 -08001181 case F_SETLK:
1182 case F_SETLKW:
1183 case F_SETOWN:
1184 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001185 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1186 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001187 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001188 break;
1189 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001190 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001191 }
1192
1193 return rc;
1194}
1195
1196/**
Al Viroe5467852012-05-30 13:30:51 -04001197 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001198 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1199 * if mapping anonymous memory.
1200 * @file contains the file structure for file to map (may be NULL).
1201 * @reqprot contains the protection requested by the application.
1202 * @prot contains the protection that will be applied by the kernel.
1203 * @flags contains the operational flags.
1204 * Return 0 if permission is granted.
1205 */
Al Viroe5467852012-05-30 13:30:51 -04001206static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001207 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001208 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001209{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001210 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001211 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001212 struct smack_rule *srp;
1213 struct task_smack *tsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001214 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001215 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001216 int may;
1217 int mmay;
1218 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001219 int rc;
1220
Al Viro496ad9a2013-01-23 17:07:38 -05001221 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001222 return 0;
1223
Al Viro496ad9a2013-01-23 17:07:38 -05001224 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001225 if (isp->smk_mmap == NULL)
1226 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001227 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001228
1229 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001230 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001231 rc = 0;
1232
1233 rcu_read_lock();
1234 /*
1235 * For each Smack rule associated with the subject
1236 * label verify that the SMACK64MMAP also has access
1237 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001238 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001239 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001240 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001241 /*
1242 * Matching labels always allows access.
1243 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001244 if (mkp->smk_known == osmack)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001245 continue;
1246 /*
1247 * If there is a matching local rule take
1248 * that into account as well.
1249 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001250 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001251 &tsp->smk_rules);
1252 if (may == -ENOENT)
1253 may = srp->smk_access;
1254 else
1255 may &= srp->smk_access;
1256 /*
1257 * If may is zero the SMACK64MMAP subject can't
1258 * possibly have less access.
1259 */
1260 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001261 continue;
1262
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001263 /*
1264 * Fetch the global list entry.
1265 * If there isn't one a SMACK64MMAP subject
1266 * can't have as much access as current.
1267 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001268 mmay = smk_access_entry(mkp->smk_known, osmack,
1269 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001270 if (mmay == -ENOENT) {
1271 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001272 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001273 }
1274 /*
1275 * If there is a local entry it modifies the
1276 * potential access, too.
1277 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001278 tmay = smk_access_entry(mkp->smk_known, osmack,
1279 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001280 if (tmay != -ENOENT)
1281 mmay &= tmay;
1282
1283 /*
1284 * If there is any access available to current that is
1285 * not available to a SMACK64MMAP subject
1286 * deny access.
1287 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001288 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001289 rc = -EACCES;
1290 break;
1291 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001292 }
1293
1294 rcu_read_unlock();
1295
1296 return rc;
1297}
1298
1299/**
Casey Schauflere114e472008-02-04 22:29:50 -08001300 * smack_file_set_fowner - set the file security blob value
1301 * @file: object in question
1302 *
1303 * Returns 0
1304 * Further research may be required on this one.
1305 */
1306static int smack_file_set_fowner(struct file *file)
1307{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001308 struct smack_known *skp = smk_of_current();
1309
1310 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001311 return 0;
1312}
1313
1314/**
1315 * smack_file_send_sigiotask - Smack on sigio
1316 * @tsk: The target task
1317 * @fown: the object the signal come from
1318 * @signum: unused
1319 *
1320 * Allow a privileged task to get signals even if it shouldn't
1321 *
1322 * Returns 0 if a subject with the object's smack could
1323 * write to the task, an error code otherwise.
1324 */
1325static int smack_file_send_sigiotask(struct task_struct *tsk,
1326 struct fown_struct *fown, int signum)
1327{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001328 struct smack_known *skp;
1329 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001330 struct file *file;
1331 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001332 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001333
1334 /*
1335 * struct fown_struct is never outside the context of a struct file
1336 */
1337 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001338
Etienne Bassetecfcc532009-04-08 20:40:06 +02001339 /* we don't log here as rc can be overriden */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001340 skp = smk_find_entry(file->f_security);
1341 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001342 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001343 rc = 0;
1344
1345 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1346 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001347 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001348 return rc;
1349}
1350
1351/**
1352 * smack_file_receive - Smack file receive check
1353 * @file: the object
1354 *
1355 * Returns 0 if current has access, error code otherwise
1356 */
1357static int smack_file_receive(struct file *file)
1358{
1359 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001361
Etienne Bassetecfcc532009-04-08 20:40:06 +02001362 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1363 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001364 /*
1365 * This code relies on bitmasks.
1366 */
1367 if (file->f_mode & FMODE_READ)
1368 may = MAY_READ;
1369 if (file->f_mode & FMODE_WRITE)
1370 may |= MAY_WRITE;
1371
Etienne Bassetecfcc532009-04-08 20:40:06 +02001372 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001373}
1374
Casey Schaufler531f1d42011-09-19 12:41:42 -07001375/**
Eric Paris83d49852012-04-04 13:45:40 -04001376 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001377 * @file: the object
1378 * @cred: unused
1379 *
1380 * Set the security blob in the file structure.
1381 *
1382 * Returns 0
1383 */
Eric Paris83d49852012-04-04 13:45:40 -04001384static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001385{
Al Viro496ad9a2013-01-23 17:07:38 -05001386 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001387
1388 file->f_security = isp->smk_inode;
1389
1390 return 0;
1391}
1392
Casey Schauflere114e472008-02-04 22:29:50 -08001393/*
1394 * Task hooks
1395 */
1396
1397/**
David Howellsee18d642009-09-02 09:14:21 +01001398 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1399 * @new: the new credentials
1400 * @gfp: the atomicity of any memory allocations
1401 *
1402 * Prepare a blank set of credentials for modification. This must allocate all
1403 * the memory the LSM module might require such that cred_transfer() can
1404 * complete without error.
1405 */
1406static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1407{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001408 struct task_smack *tsp;
1409
1410 tsp = new_task_smack(NULL, NULL, gfp);
1411 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001412 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001413
1414 cred->security = tsp;
1415
David Howellsee18d642009-09-02 09:14:21 +01001416 return 0;
1417}
1418
1419
1420/**
David Howellsf1752ee2008-11-14 10:39:17 +11001421 * smack_cred_free - "free" task-level security credentials
1422 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001423 *
Casey Schauflere114e472008-02-04 22:29:50 -08001424 */
David Howellsf1752ee2008-11-14 10:39:17 +11001425static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001426{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001427 struct task_smack *tsp = cred->security;
1428 struct smack_rule *rp;
1429 struct list_head *l;
1430 struct list_head *n;
1431
1432 if (tsp == NULL)
1433 return;
1434 cred->security = NULL;
1435
1436 list_for_each_safe(l, n, &tsp->smk_rules) {
1437 rp = list_entry(l, struct smack_rule, list);
1438 list_del(&rp->list);
1439 kfree(rp);
1440 }
1441 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001442}
1443
1444/**
David Howellsd84f4f92008-11-14 10:39:23 +11001445 * smack_cred_prepare - prepare new set of credentials for modification
1446 * @new: the new credentials
1447 * @old: the original credentials
1448 * @gfp: the atomicity of any memory allocations
1449 *
1450 * Prepare a new set of credentials for modification.
1451 */
1452static int smack_cred_prepare(struct cred *new, const struct cred *old,
1453 gfp_t gfp)
1454{
Casey Schaufler676dac42010-12-02 06:43:39 -08001455 struct task_smack *old_tsp = old->security;
1456 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001457 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001458
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001459 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001460 if (new_tsp == NULL)
1461 return -ENOMEM;
1462
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001463 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1464 if (rc != 0)
1465 return rc;
1466
Casey Schaufler676dac42010-12-02 06:43:39 -08001467 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001468 return 0;
1469}
1470
Randy Dunlap251a2a92009-02-18 11:42:33 -08001471/**
David Howellsee18d642009-09-02 09:14:21 +01001472 * smack_cred_transfer - Transfer the old credentials to the new credentials
1473 * @new: the new credentials
1474 * @old: the original credentials
1475 *
1476 * Fill in a set of blank credentials from another set of credentials.
1477 */
1478static void smack_cred_transfer(struct cred *new, const struct cred *old)
1479{
Casey Schaufler676dac42010-12-02 06:43:39 -08001480 struct task_smack *old_tsp = old->security;
1481 struct task_smack *new_tsp = new->security;
1482
1483 new_tsp->smk_task = old_tsp->smk_task;
1484 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001485 mutex_init(&new_tsp->smk_rules_lock);
1486 INIT_LIST_HEAD(&new_tsp->smk_rules);
1487
1488
1489 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001490}
1491
1492/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001493 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001494 * @new: points to the set of credentials to be modified.
1495 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001496 *
1497 * Set the security data for a kernel service.
1498 */
1499static int smack_kernel_act_as(struct cred *new, u32 secid)
1500{
Casey Schaufler676dac42010-12-02 06:43:39 -08001501 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001502 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001503
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001504 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001505 return -EINVAL;
1506
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001507 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001508 return 0;
1509}
1510
1511/**
1512 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001513 * @new: points to the set of credentials to be modified
1514 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001515 *
1516 * Set the file creation context in a set of credentials to the same
1517 * as the objective context of the specified inode
1518 */
1519static int smack_kernel_create_files_as(struct cred *new,
1520 struct inode *inode)
1521{
1522 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001523 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001524
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001525 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1526 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001527 return 0;
1528}
1529
1530/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001531 * smk_curacc_on_task - helper to log task related access
1532 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001533 * @access: the access requested
1534 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001535 *
1536 * Return 0 if access is permitted
1537 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001538static int smk_curacc_on_task(struct task_struct *p, int access,
1539 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001540{
1541 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001542 struct smack_known *skp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001543
Casey Schaufler531f1d42011-09-19 12:41:42 -07001544 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001545 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001546 return smk_curacc(skp->smk_known, access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001547}
1548
1549/**
Casey Schauflere114e472008-02-04 22:29:50 -08001550 * smack_task_setpgid - Smack check on setting pgid
1551 * @p: the task object
1552 * @pgid: unused
1553 *
1554 * Return 0 if write access is permitted
1555 */
1556static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1557{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001558 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001559}
1560
1561/**
1562 * smack_task_getpgid - Smack access check for getpgid
1563 * @p: the object task
1564 *
1565 * Returns 0 if current can read the object task, error code otherwise
1566 */
1567static int smack_task_getpgid(struct task_struct *p)
1568{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001569 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001570}
1571
1572/**
1573 * smack_task_getsid - Smack access check for getsid
1574 * @p: the object task
1575 *
1576 * Returns 0 if current can read the object task, error code otherwise
1577 */
1578static int smack_task_getsid(struct task_struct *p)
1579{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001580 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001581}
1582
1583/**
1584 * smack_task_getsecid - get the secid of the task
1585 * @p: the object task
1586 * @secid: where to put the result
1587 *
1588 * Sets the secid to contain a u32 version of the smack label.
1589 */
1590static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1591{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001592 struct smack_known *skp = smk_of_task(task_security(p));
1593
1594 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001595}
1596
1597/**
1598 * smack_task_setnice - Smack check on setting nice
1599 * @p: the task object
1600 * @nice: unused
1601 *
1602 * Return 0 if write access is permitted
1603 */
1604static int smack_task_setnice(struct task_struct *p, int nice)
1605{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001606 int rc;
1607
1608 rc = cap_task_setnice(p, nice);
1609 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001610 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001611 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001612}
1613
1614/**
1615 * smack_task_setioprio - Smack check on setting ioprio
1616 * @p: the task object
1617 * @ioprio: unused
1618 *
1619 * Return 0 if write access is permitted
1620 */
1621static int smack_task_setioprio(struct task_struct *p, int ioprio)
1622{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001623 int rc;
1624
1625 rc = cap_task_setioprio(p, ioprio);
1626 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001627 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001628 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001629}
1630
1631/**
1632 * smack_task_getioprio - Smack check on reading ioprio
1633 * @p: the task object
1634 *
1635 * Return 0 if read access is permitted
1636 */
1637static int smack_task_getioprio(struct task_struct *p)
1638{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001639 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001640}
1641
1642/**
1643 * smack_task_setscheduler - Smack check on setting scheduler
1644 * @p: the task object
1645 * @policy: unused
1646 * @lp: unused
1647 *
1648 * Return 0 if read access is permitted
1649 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001650static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001651{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001652 int rc;
1653
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001654 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001655 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001656 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001657 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001658}
1659
1660/**
1661 * smack_task_getscheduler - Smack check on reading scheduler
1662 * @p: the task object
1663 *
1664 * Return 0 if read access is permitted
1665 */
1666static int smack_task_getscheduler(struct task_struct *p)
1667{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001668 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001669}
1670
1671/**
1672 * smack_task_movememory - Smack check on moving memory
1673 * @p: the task object
1674 *
1675 * Return 0 if write access is permitted
1676 */
1677static int smack_task_movememory(struct task_struct *p)
1678{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001679 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001680}
1681
1682/**
1683 * smack_task_kill - Smack check on signal delivery
1684 * @p: the task object
1685 * @info: unused
1686 * @sig: unused
1687 * @secid: identifies the smack to use in lieu of current's
1688 *
1689 * Return 0 if write access is permitted
1690 *
1691 * The secid behavior is an artifact of an SELinux hack
1692 * in the USB code. Someday it may go away.
1693 */
1694static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1695 int sig, u32 secid)
1696{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001697 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001698 struct smack_known *skp;
1699 struct smack_known *tkp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001700
1701 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1702 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001703 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001704 * Sending a signal requires that the sender
1705 * can write the receiver.
1706 */
1707 if (secid == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001708 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001709 /*
1710 * If the secid isn't 0 we're dealing with some USB IO
1711 * specific behavior. This is not clean. For one thing
1712 * we can't take privilege into account.
1713 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001714 skp = smack_from_secid(secid);
1715 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001716}
1717
1718/**
1719 * smack_task_wait - Smack access check for waiting
1720 * @p: task to wait for
1721 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001722 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08001723 */
1724static int smack_task_wait(struct task_struct *p)
1725{
Casey Schauflere114e472008-02-04 22:29:50 -08001726 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001727 * Allow the operation to succeed.
1728 * Zombies are bad.
1729 * In userless environments (e.g. phones) programs
1730 * get marked with SMACK64EXEC and even if the parent
1731 * and child shouldn't be talking the parent still
1732 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08001733 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001734 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001735}
1736
1737/**
1738 * smack_task_to_inode - copy task smack into the inode blob
1739 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001740 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001741 *
1742 * Sets the smack pointer in the inode security blob
1743 */
1744static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1745{
1746 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001747 struct smack_known *skp = smk_of_task(task_security(p));
1748
1749 isp->smk_inode = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001750}
1751
1752/*
1753 * Socket hooks.
1754 */
1755
1756/**
1757 * smack_sk_alloc_security - Allocate a socket blob
1758 * @sk: the socket
1759 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001760 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001761 *
1762 * Assign Smack pointers to current
1763 *
1764 * Returns 0 on success, -ENOMEM is there's no memory
1765 */
1766static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1767{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001768 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001769 struct socket_smack *ssp;
1770
1771 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1772 if (ssp == NULL)
1773 return -ENOMEM;
1774
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001775 ssp->smk_in = skp->smk_known;
1776 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001777 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001778
1779 sk->sk_security = ssp;
1780
1781 return 0;
1782}
1783
1784/**
1785 * smack_sk_free_security - Free a socket blob
1786 * @sk: the socket
1787 *
1788 * Clears the blob pointer
1789 */
1790static void smack_sk_free_security(struct sock *sk)
1791{
1792 kfree(sk->sk_security);
1793}
1794
1795/**
Paul Moore07feee82009-03-27 17:10:54 -04001796* smack_host_label - check host based restrictions
1797* @sip: the object end
1798*
1799* looks for host based access restrictions
1800*
1801* This version will only be appropriate for really small sets of single label
1802* hosts. The caller is responsible for ensuring that the RCU read lock is
1803* taken before calling this function.
1804*
1805* Returns the label of the far end or NULL if it's not special.
1806*/
1807static char *smack_host_label(struct sockaddr_in *sip)
1808{
1809 struct smk_netlbladdr *snp;
1810 struct in_addr *siap = &sip->sin_addr;
1811
1812 if (siap->s_addr == 0)
1813 return NULL;
1814
1815 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1816 /*
1817 * we break after finding the first match because
1818 * the list is sorted from longest to shortest mask
1819 * so we have found the most specific match
1820 */
1821 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001822 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1823 /* we have found the special CIPSO option */
1824 if (snp->smk_label == smack_cipso_option)
1825 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001826 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001827 }
Paul Moore07feee82009-03-27 17:10:54 -04001828
1829 return NULL;
1830}
1831
1832/**
Casey Schauflere114e472008-02-04 22:29:50 -08001833 * smack_netlabel - Set the secattr on a socket
1834 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001835 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001836 *
1837 * Convert the outbound smack value (smk_out) to a
1838 * secattr and attach it to the socket.
1839 *
1840 * Returns 0 on success or an error code
1841 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001842static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001843{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001844 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001845 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001846 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001847
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001848 /*
1849 * Usually the netlabel code will handle changing the
1850 * packet labeling based on the label.
1851 * The case of a single label host is different, because
1852 * a single label host should never get a labeled packet
1853 * even though the label is usually associated with a packet
1854 * label.
1855 */
1856 local_bh_disable();
1857 bh_lock_sock_nested(sk);
1858
1859 if (ssp->smk_out == smack_net_ambient ||
1860 labeled == SMACK_UNLABELED_SOCKET)
1861 netlbl_sock_delattr(sk);
1862 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001863 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001864 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001865 }
1866
1867 bh_unlock_sock(sk);
1868 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001869
Casey Schauflere114e472008-02-04 22:29:50 -08001870 return rc;
1871}
1872
1873/**
Paul Moore07feee82009-03-27 17:10:54 -04001874 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1875 * @sk: the socket
1876 * @sap: the destination address
1877 *
1878 * Set the correct secattr for the given socket based on the destination
1879 * address and perform any outbound access checks needed.
1880 *
1881 * Returns 0 on success or an error code.
1882 *
1883 */
1884static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1885{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001886 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001887 int rc;
1888 int sk_lbl;
1889 char *hostsp;
1890 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001891 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001892
1893 rcu_read_lock();
1894 hostsp = smack_host_label(sap);
1895 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001896#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001897 struct lsm_network_audit net;
1898
Eric Paris48c62af2012-04-02 13:15:44 -04001899 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1900 ad.a.u.net->family = sap->sin_family;
1901 ad.a.u.net->dport = sap->sin_port;
1902 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001903#endif
Kees Cook923e9a12012-04-10 13:26:44 -07001904 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001905 skp = ssp->smk_out;
1906 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001907 } else {
1908 sk_lbl = SMACK_CIPSO_SOCKET;
1909 rc = 0;
1910 }
1911 rcu_read_unlock();
1912 if (rc != 0)
1913 return rc;
1914
1915 return smack_netlabel(sk, sk_lbl);
1916}
1917
1918/**
Casey Schauflerc6739442013-05-22 18:42:56 -07001919 * smk_ipv6_port_label - Smack port access table management
1920 * @sock: socket
1921 * @address: address
1922 *
1923 * Create or update the port list entry
1924 */
1925static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1926{
1927 struct sock *sk = sock->sk;
1928 struct sockaddr_in6 *addr6;
1929 struct socket_smack *ssp = sock->sk->sk_security;
1930 struct smk_port_label *spp;
1931 unsigned short port = 0;
1932
1933 if (address == NULL) {
1934 /*
1935 * This operation is changing the Smack information
1936 * on the bound socket. Take the changes to the port
1937 * as well.
1938 */
1939 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1940 if (sk != spp->smk_sock)
1941 continue;
1942 spp->smk_in = ssp->smk_in;
1943 spp->smk_out = ssp->smk_out;
1944 return;
1945 }
1946 /*
1947 * A NULL address is only used for updating existing
1948 * bound entries. If there isn't one, it's OK.
1949 */
1950 return;
1951 }
1952
1953 addr6 = (struct sockaddr_in6 *)address;
1954 port = ntohs(addr6->sin6_port);
1955 /*
1956 * This is a special case that is safely ignored.
1957 */
1958 if (port == 0)
1959 return;
1960
1961 /*
1962 * Look for an existing port list entry.
1963 * This is an indication that a port is getting reused.
1964 */
1965 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1966 if (spp->smk_port != port)
1967 continue;
1968 spp->smk_port = port;
1969 spp->smk_sock = sk;
1970 spp->smk_in = ssp->smk_in;
1971 spp->smk_out = ssp->smk_out;
1972 return;
1973 }
1974
1975 /*
1976 * A new port entry is required.
1977 */
1978 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1979 if (spp == NULL)
1980 return;
1981
1982 spp->smk_port = port;
1983 spp->smk_sock = sk;
1984 spp->smk_in = ssp->smk_in;
1985 spp->smk_out = ssp->smk_out;
1986
1987 list_add(&spp->list, &smk_ipv6_port_list);
1988 return;
1989}
1990
1991/**
1992 * smk_ipv6_port_check - check Smack port access
1993 * @sock: socket
1994 * @address: address
1995 *
1996 * Create or update the port list entry
1997 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07001998static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07001999 int act)
2000{
2001 __be16 *bep;
2002 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002003 struct smk_port_label *spp;
2004 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002005 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002006 unsigned short port = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002007 char *object;
2008 struct smk_audit_info ad;
2009#ifdef CONFIG_AUDIT
2010 struct lsm_network_audit net;
2011#endif
2012
2013 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002014 skp = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002015 object = ssp->smk_in;
2016 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002017 skp = ssp->smk_out;
2018 object = smack_net_ambient->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002019 }
2020
2021 /*
2022 * Get the IP address and port from the address.
2023 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002024 port = ntohs(address->sin6_port);
2025 bep = (__be16 *)(&address->sin6_addr);
2026 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002027
2028 /*
2029 * It's remote, so port lookup does no good.
2030 */
2031 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2032 goto auditout;
2033
2034 /*
2035 * It's local so the send check has to have passed.
2036 */
2037 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002038 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002039 goto auditout;
2040 }
2041
2042 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2043 if (spp->smk_port != port)
2044 continue;
2045 object = spp->smk_in;
2046 if (act == SMK_CONNECTING)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002047 ssp->smk_packet = spp->smk_out->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002048 break;
2049 }
2050
2051auditout:
2052
2053#ifdef CONFIG_AUDIT
2054 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2055 ad.a.u.net->family = sk->sk_family;
2056 ad.a.u.net->dport = port;
2057 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002058 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002059 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002060 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002061#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002062 return smk_access(skp, object, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07002063}
2064
2065/**
Casey Schauflere114e472008-02-04 22:29:50 -08002066 * smack_inode_setsecurity - set smack xattrs
2067 * @inode: the object
2068 * @name: attribute name
2069 * @value: attribute value
2070 * @size: size of the attribute
2071 * @flags: unused
2072 *
2073 * Sets the named attribute in the appropriate blob
2074 *
2075 * Returns 0 on success, or an error code
2076 */
2077static int smack_inode_setsecurity(struct inode *inode, const char *name,
2078 const void *value, size_t size, int flags)
2079{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002080 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002081 struct inode_smack *nsp = inode->i_security;
2082 struct socket_smack *ssp;
2083 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002084 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002085
Casey Schauflerf7112e62012-05-06 15:22:02 -07002086 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08002087 return -EACCES;
2088
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002089 skp = smk_import_entry(value, size);
2090 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002091 return -EINVAL;
2092
2093 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002094 nsp->smk_inode = skp->smk_known;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002095 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002096 return 0;
2097 }
2098 /*
2099 * The rest of the Smack xattrs are only on sockets.
2100 */
2101 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2102 return -EOPNOTSUPP;
2103
2104 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002105 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002106 return -EOPNOTSUPP;
2107
2108 ssp = sock->sk->sk_security;
2109
2110 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002111 ssp->smk_in = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002112 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002113 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002114 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002115 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2116 if (rc != 0)
2117 printk(KERN_WARNING
2118 "Smack: \"%s\" netlbl error %d.\n",
2119 __func__, -rc);
2120 }
Casey Schauflere114e472008-02-04 22:29:50 -08002121 } else
2122 return -EOPNOTSUPP;
2123
Casey Schauflerc6739442013-05-22 18:42:56 -07002124 if (sock->sk->sk_family == PF_INET6)
2125 smk_ipv6_port_label(sock, NULL);
2126
Casey Schauflere114e472008-02-04 22:29:50 -08002127 return 0;
2128}
2129
2130/**
2131 * smack_socket_post_create - finish socket setup
2132 * @sock: the socket
2133 * @family: protocol family
2134 * @type: unused
2135 * @protocol: unused
2136 * @kern: unused
2137 *
2138 * Sets the netlabel information on the socket
2139 *
2140 * Returns 0 on success, and error code otherwise
2141 */
2142static int smack_socket_post_create(struct socket *sock, int family,
2143 int type, int protocol, int kern)
2144{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002145 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002146 return 0;
2147 /*
2148 * Set the outbound netlbl.
2149 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002150 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2151}
2152
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002153/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002154 * smack_socket_bind - record port binding information.
2155 * @sock: the socket
2156 * @address: the port address
2157 * @addrlen: size of the address
2158 *
2159 * Records the label bound to a port.
2160 *
2161 * Returns 0
2162 */
2163static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2164 int addrlen)
2165{
2166 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2167 smk_ipv6_port_label(sock, address);
2168
2169 return 0;
2170}
2171
2172/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002173 * smack_socket_connect - connect access check
2174 * @sock: the socket
2175 * @sap: the other end
2176 * @addrlen: size of sap
2177 *
2178 * Verifies that a connection may be possible
2179 *
2180 * Returns 0 on success, and error code otherwise
2181 */
2182static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2183 int addrlen)
2184{
Casey Schauflerc6739442013-05-22 18:42:56 -07002185 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002186
Casey Schauflerc6739442013-05-22 18:42:56 -07002187 if (sock->sk == NULL)
2188 return 0;
2189
2190 switch (sock->sk->sk_family) {
2191 case PF_INET:
2192 if (addrlen < sizeof(struct sockaddr_in))
2193 return -EINVAL;
2194 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2195 break;
2196 case PF_INET6:
2197 if (addrlen < sizeof(struct sockaddr_in6))
2198 return -EINVAL;
Casey Schaufler6ea06242013-08-05 13:21:22 -07002199 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2200 SMK_CONNECTING);
Casey Schauflerc6739442013-05-22 18:42:56 -07002201 break;
2202 }
2203 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002204}
2205
2206/**
2207 * smack_flags_to_may - convert S_ to MAY_ values
2208 * @flags: the S_ value
2209 *
2210 * Returns the equivalent MAY_ value
2211 */
2212static int smack_flags_to_may(int flags)
2213{
2214 int may = 0;
2215
2216 if (flags & S_IRUGO)
2217 may |= MAY_READ;
2218 if (flags & S_IWUGO)
2219 may |= MAY_WRITE;
2220 if (flags & S_IXUGO)
2221 may |= MAY_EXEC;
2222
2223 return may;
2224}
2225
2226/**
2227 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2228 * @msg: the object
2229 *
2230 * Returns 0
2231 */
2232static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2233{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002234 struct smack_known *skp = smk_of_current();
2235
2236 msg->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002237 return 0;
2238}
2239
2240/**
2241 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2242 * @msg: the object
2243 *
2244 * Clears the blob pointer
2245 */
2246static void smack_msg_msg_free_security(struct msg_msg *msg)
2247{
2248 msg->security = NULL;
2249}
2250
2251/**
2252 * smack_of_shm - the smack pointer for the shm
2253 * @shp: the object
2254 *
2255 * Returns a pointer to the smack value
2256 */
2257static char *smack_of_shm(struct shmid_kernel *shp)
2258{
2259 return (char *)shp->shm_perm.security;
2260}
2261
2262/**
2263 * smack_shm_alloc_security - Set the security blob for shm
2264 * @shp: the object
2265 *
2266 * Returns 0
2267 */
2268static int smack_shm_alloc_security(struct shmid_kernel *shp)
2269{
2270 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002271 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002272
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002273 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002274 return 0;
2275}
2276
2277/**
2278 * smack_shm_free_security - Clear the security blob for shm
2279 * @shp: the object
2280 *
2281 * Clears the blob pointer
2282 */
2283static void smack_shm_free_security(struct shmid_kernel *shp)
2284{
2285 struct kern_ipc_perm *isp = &shp->shm_perm;
2286
2287 isp->security = NULL;
2288}
2289
2290/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002291 * smk_curacc_shm : check if current has access on shm
2292 * @shp : the object
2293 * @access : access requested
2294 *
2295 * Returns 0 if current has the requested access, error code otherwise
2296 */
2297static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2298{
2299 char *ssp = smack_of_shm(shp);
2300 struct smk_audit_info ad;
2301
2302#ifdef CONFIG_AUDIT
2303 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2304 ad.a.u.ipc_id = shp->shm_perm.id;
2305#endif
2306 return smk_curacc(ssp, access, &ad);
2307}
2308
2309/**
Casey Schauflere114e472008-02-04 22:29:50 -08002310 * smack_shm_associate - Smack access check for shm
2311 * @shp: the object
2312 * @shmflg: access requested
2313 *
2314 * Returns 0 if current has the requested access, error code otherwise
2315 */
2316static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2317{
Casey Schauflere114e472008-02-04 22:29:50 -08002318 int may;
2319
2320 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002321 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002322}
2323
2324/**
2325 * smack_shm_shmctl - Smack access check for shm
2326 * @shp: the object
2327 * @cmd: what it wants to do
2328 *
2329 * Returns 0 if current has the requested access, error code otherwise
2330 */
2331static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2332{
Casey Schauflere114e472008-02-04 22:29:50 -08002333 int may;
2334
2335 switch (cmd) {
2336 case IPC_STAT:
2337 case SHM_STAT:
2338 may = MAY_READ;
2339 break;
2340 case IPC_SET:
2341 case SHM_LOCK:
2342 case SHM_UNLOCK:
2343 case IPC_RMID:
2344 may = MAY_READWRITE;
2345 break;
2346 case IPC_INFO:
2347 case SHM_INFO:
2348 /*
2349 * System level information.
2350 */
2351 return 0;
2352 default:
2353 return -EINVAL;
2354 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002355 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002356}
2357
2358/**
2359 * smack_shm_shmat - Smack access for shmat
2360 * @shp: the object
2361 * @shmaddr: unused
2362 * @shmflg: access requested
2363 *
2364 * Returns 0 if current has the requested access, error code otherwise
2365 */
2366static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2367 int shmflg)
2368{
Casey Schauflere114e472008-02-04 22:29:50 -08002369 int may;
2370
2371 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002372 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002373}
2374
2375/**
2376 * smack_of_sem - the smack pointer for the sem
2377 * @sma: the object
2378 *
2379 * Returns a pointer to the smack value
2380 */
2381static char *smack_of_sem(struct sem_array *sma)
2382{
2383 return (char *)sma->sem_perm.security;
2384}
2385
2386/**
2387 * smack_sem_alloc_security - Set the security blob for sem
2388 * @sma: the object
2389 *
2390 * Returns 0
2391 */
2392static int smack_sem_alloc_security(struct sem_array *sma)
2393{
2394 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002395 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002396
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002397 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002398 return 0;
2399}
2400
2401/**
2402 * smack_sem_free_security - Clear the security blob for sem
2403 * @sma: the object
2404 *
2405 * Clears the blob pointer
2406 */
2407static void smack_sem_free_security(struct sem_array *sma)
2408{
2409 struct kern_ipc_perm *isp = &sma->sem_perm;
2410
2411 isp->security = NULL;
2412}
2413
2414/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002415 * smk_curacc_sem : check if current has access on sem
2416 * @sma : the object
2417 * @access : access requested
2418 *
2419 * Returns 0 if current has the requested access, error code otherwise
2420 */
2421static int smk_curacc_sem(struct sem_array *sma, int access)
2422{
2423 char *ssp = smack_of_sem(sma);
2424 struct smk_audit_info ad;
2425
2426#ifdef CONFIG_AUDIT
2427 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2428 ad.a.u.ipc_id = sma->sem_perm.id;
2429#endif
2430 return smk_curacc(ssp, access, &ad);
2431}
2432
2433/**
Casey Schauflere114e472008-02-04 22:29:50 -08002434 * smack_sem_associate - Smack access check for sem
2435 * @sma: the object
2436 * @semflg: access requested
2437 *
2438 * Returns 0 if current has the requested access, error code otherwise
2439 */
2440static int smack_sem_associate(struct sem_array *sma, int semflg)
2441{
Casey Schauflere114e472008-02-04 22:29:50 -08002442 int may;
2443
2444 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002445 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002446}
2447
2448/**
2449 * smack_sem_shmctl - Smack access check for sem
2450 * @sma: the object
2451 * @cmd: what it wants to do
2452 *
2453 * Returns 0 if current has the requested access, error code otherwise
2454 */
2455static int smack_sem_semctl(struct sem_array *sma, int cmd)
2456{
Casey Schauflere114e472008-02-04 22:29:50 -08002457 int may;
2458
2459 switch (cmd) {
2460 case GETPID:
2461 case GETNCNT:
2462 case GETZCNT:
2463 case GETVAL:
2464 case GETALL:
2465 case IPC_STAT:
2466 case SEM_STAT:
2467 may = MAY_READ;
2468 break;
2469 case SETVAL:
2470 case SETALL:
2471 case IPC_RMID:
2472 case IPC_SET:
2473 may = MAY_READWRITE;
2474 break;
2475 case IPC_INFO:
2476 case SEM_INFO:
2477 /*
2478 * System level information
2479 */
2480 return 0;
2481 default:
2482 return -EINVAL;
2483 }
2484
Etienne Bassetecfcc532009-04-08 20:40:06 +02002485 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002486}
2487
2488/**
2489 * smack_sem_semop - Smack checks of semaphore operations
2490 * @sma: the object
2491 * @sops: unused
2492 * @nsops: unused
2493 * @alter: unused
2494 *
2495 * Treated as read and write in all cases.
2496 *
2497 * Returns 0 if access is allowed, error code otherwise
2498 */
2499static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2500 unsigned nsops, int alter)
2501{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002502 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002503}
2504
2505/**
2506 * smack_msg_alloc_security - Set the security blob for msg
2507 * @msq: the object
2508 *
2509 * Returns 0
2510 */
2511static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2512{
2513 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002514 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002515
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002516 kisp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002517 return 0;
2518}
2519
2520/**
2521 * smack_msg_free_security - Clear the security blob for msg
2522 * @msq: the object
2523 *
2524 * Clears the blob pointer
2525 */
2526static void smack_msg_queue_free_security(struct msg_queue *msq)
2527{
2528 struct kern_ipc_perm *kisp = &msq->q_perm;
2529
2530 kisp->security = NULL;
2531}
2532
2533/**
2534 * smack_of_msq - the smack pointer for the msq
2535 * @msq: the object
2536 *
2537 * Returns a pointer to the smack value
2538 */
2539static char *smack_of_msq(struct msg_queue *msq)
2540{
2541 return (char *)msq->q_perm.security;
2542}
2543
2544/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002545 * smk_curacc_msq : helper to check if current has access on msq
2546 * @msq : the msq
2547 * @access : access requested
2548 *
2549 * return 0 if current has access, error otherwise
2550 */
2551static int smk_curacc_msq(struct msg_queue *msq, int access)
2552{
2553 char *msp = smack_of_msq(msq);
2554 struct smk_audit_info ad;
2555
2556#ifdef CONFIG_AUDIT
2557 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2558 ad.a.u.ipc_id = msq->q_perm.id;
2559#endif
2560 return smk_curacc(msp, access, &ad);
2561}
2562
2563/**
Casey Schauflere114e472008-02-04 22:29:50 -08002564 * smack_msg_queue_associate - Smack access check for msg_queue
2565 * @msq: the object
2566 * @msqflg: access requested
2567 *
2568 * Returns 0 if current has the requested access, error code otherwise
2569 */
2570static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2571{
Casey Schauflere114e472008-02-04 22:29:50 -08002572 int may;
2573
2574 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002575 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002576}
2577
2578/**
2579 * smack_msg_queue_msgctl - Smack access check for msg_queue
2580 * @msq: the object
2581 * @cmd: what it wants to do
2582 *
2583 * Returns 0 if current has the requested access, error code otherwise
2584 */
2585static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2586{
Casey Schauflere114e472008-02-04 22:29:50 -08002587 int may;
2588
2589 switch (cmd) {
2590 case IPC_STAT:
2591 case MSG_STAT:
2592 may = MAY_READ;
2593 break;
2594 case IPC_SET:
2595 case IPC_RMID:
2596 may = MAY_READWRITE;
2597 break;
2598 case IPC_INFO:
2599 case MSG_INFO:
2600 /*
2601 * System level information
2602 */
2603 return 0;
2604 default:
2605 return -EINVAL;
2606 }
2607
Etienne Bassetecfcc532009-04-08 20:40:06 +02002608 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002609}
2610
2611/**
2612 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2613 * @msq: the object
2614 * @msg: unused
2615 * @msqflg: access requested
2616 *
2617 * Returns 0 if current has the requested access, error code otherwise
2618 */
2619static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2620 int msqflg)
2621{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002622 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002623
Etienne Bassetecfcc532009-04-08 20:40:06 +02002624 may = smack_flags_to_may(msqflg);
2625 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002626}
2627
2628/**
2629 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2630 * @msq: the object
2631 * @msg: unused
2632 * @target: unused
2633 * @type: unused
2634 * @mode: unused
2635 *
2636 * Returns 0 if current has read and write access, error code otherwise
2637 */
2638static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2639 struct task_struct *target, long type, int mode)
2640{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002641 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002642}
2643
2644/**
2645 * smack_ipc_permission - Smack access for ipc_permission()
2646 * @ipp: the object permissions
2647 * @flag: access requested
2648 *
2649 * Returns 0 if current has read and write access, error code otherwise
2650 */
2651static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2652{
2653 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002654 int may = smack_flags_to_may(flag);
2655 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002656
Etienne Bassetecfcc532009-04-08 20:40:06 +02002657#ifdef CONFIG_AUDIT
2658 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2659 ad.a.u.ipc_id = ipp->id;
2660#endif
2661 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002662}
2663
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002664/**
2665 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002666 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002667 * @secid: where result will be saved
2668 */
2669static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2670{
2671 char *smack = ipp->security;
2672
2673 *secid = smack_to_secid(smack);
2674}
2675
Casey Schauflere114e472008-02-04 22:29:50 -08002676/**
2677 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002678 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002679 * @inode: the object
2680 *
2681 * Set the inode's security blob if it hasn't been done already.
2682 */
2683static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2684{
2685 struct super_block *sbp;
2686 struct superblock_smack *sbsp;
2687 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002688 struct smack_known *skp;
2689 struct smack_known *ckp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002690 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002691 char trattr[TRANS_TRUE_SIZE];
2692 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002693 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002694 struct dentry *dp;
2695
2696 if (inode == NULL)
2697 return;
2698
2699 isp = inode->i_security;
2700
2701 mutex_lock(&isp->smk_lock);
2702 /*
2703 * If the inode is already instantiated
2704 * take the quick way out
2705 */
2706 if (isp->smk_flags & SMK_INODE_INSTANT)
2707 goto unlockandout;
2708
2709 sbp = inode->i_sb;
2710 sbsp = sbp->s_security;
2711 /*
2712 * We're going to use the superblock default label
2713 * if there's no label on the file.
2714 */
2715 final = sbsp->smk_default;
2716
2717 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002718 * If this is the root inode the superblock
2719 * may be in the process of initialization.
2720 * If that is the case use the root value out
2721 * of the superblock.
2722 */
2723 if (opt_dentry->d_parent == opt_dentry) {
2724 isp->smk_inode = sbsp->smk_root;
2725 isp->smk_flags |= SMK_INODE_INSTANT;
2726 goto unlockandout;
2727 }
2728
2729 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002730 * This is pretty hackish.
2731 * Casey says that we shouldn't have to do
2732 * file system specific code, but it does help
2733 * with keeping it simple.
2734 */
2735 switch (sbp->s_magic) {
2736 case SMACK_MAGIC:
2737 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002738 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002739 * that the smack file system doesn't do
2740 * extended attributes.
2741 */
2742 final = smack_known_star.smk_known;
2743 break;
2744 case PIPEFS_MAGIC:
2745 /*
2746 * Casey says pipes are easy (?)
2747 */
2748 final = smack_known_star.smk_known;
2749 break;
2750 case DEVPTS_SUPER_MAGIC:
2751 /*
2752 * devpts seems content with the label of the task.
2753 * Programs that change smack have to treat the
2754 * pty with respect.
2755 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002756 final = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002757 break;
2758 case SOCKFS_MAGIC:
2759 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002760 * Socket access is controlled by the socket
2761 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002762 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002763 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002764 break;
2765 case PROC_SUPER_MAGIC:
2766 /*
2767 * Casey says procfs appears not to care.
2768 * The superblock default suffices.
2769 */
2770 break;
2771 case TMPFS_MAGIC:
2772 /*
2773 * Device labels should come from the filesystem,
2774 * but watch out, because they're volitile,
2775 * getting recreated on every reboot.
2776 */
2777 final = smack_known_star.smk_known;
2778 /*
2779 * No break.
2780 *
2781 * If a smack value has been set we want to use it,
2782 * but since tmpfs isn't giving us the opportunity
2783 * to set mount options simulate setting the
2784 * superblock default.
2785 */
2786 default:
2787 /*
2788 * This isn't an understood special case.
2789 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002790 */
2791
2792 /*
2793 * UNIX domain sockets use lower level socket data.
2794 */
2795 if (S_ISSOCK(inode->i_mode)) {
2796 final = smack_known_star.smk_known;
2797 break;
2798 }
2799 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002800 * No xattr support means, alas, no SMACK label.
2801 * Use the aforeapplied default.
2802 * It would be curious if the label of the task
2803 * does not match that assigned.
2804 */
2805 if (inode->i_op->getxattr == NULL)
2806 break;
2807 /*
2808 * Get the dentry for xattr.
2809 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002810 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002811 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2812 if (skp != NULL)
2813 final = skp->smk_known;
Casey Schaufler2267b132012-03-13 19:14:19 -07002814
2815 /*
2816 * Transmuting directory
2817 */
2818 if (S_ISDIR(inode->i_mode)) {
2819 /*
2820 * If this is a new directory and the label was
2821 * transmuted when the inode was initialized
2822 * set the transmute attribute on the directory
2823 * and mark the inode.
2824 *
2825 * If there is a transmute attribute on the
2826 * directory mark the inode.
2827 */
2828 if (isp->smk_flags & SMK_INODE_CHANGED) {
2829 isp->smk_flags &= ~SMK_INODE_CHANGED;
2830 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002831 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002832 TRANS_TRUE, TRANS_TRUE_SIZE,
2833 0);
2834 } else {
2835 rc = inode->i_op->getxattr(dp,
2836 XATTR_NAME_SMACKTRANSMUTE, trattr,
2837 TRANS_TRUE_SIZE);
2838 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2839 TRANS_TRUE_SIZE) != 0)
2840 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002841 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002842 if (rc >= 0)
2843 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002844 }
2845 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002846 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002847
Casey Schauflere114e472008-02-04 22:29:50 -08002848 dput(dp);
2849 break;
2850 }
2851
2852 if (final == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002853 isp->smk_inode = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002854 else
2855 isp->smk_inode = final;
2856
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002857 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002858
2859unlockandout:
2860 mutex_unlock(&isp->smk_lock);
2861 return;
2862}
2863
2864/**
2865 * smack_getprocattr - Smack process attribute access
2866 * @p: the object task
2867 * @name: the name of the attribute in /proc/.../attr
2868 * @value: where to put the result
2869 *
2870 * Places a copy of the task Smack into value
2871 *
2872 * Returns the length of the smack label or an error code
2873 */
2874static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2875{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002876 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08002877 char *cp;
2878 int slen;
2879
2880 if (strcmp(name, "current") != 0)
2881 return -EINVAL;
2882
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002883 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002884 if (cp == NULL)
2885 return -ENOMEM;
2886
2887 slen = strlen(cp);
2888 *value = cp;
2889 return slen;
2890}
2891
2892/**
2893 * smack_setprocattr - Smack process attribute setting
2894 * @p: the object task
2895 * @name: the name of the attribute in /proc/.../attr
2896 * @value: the value to set
2897 * @size: the size of the value
2898 *
2899 * Sets the Smack value of the task. Only setting self
2900 * is permitted and only with privilege
2901 *
2902 * Returns the length of the smack label or an error code
2903 */
2904static int smack_setprocattr(struct task_struct *p, char *name,
2905 void *value, size_t size)
2906{
Casey Schaufler676dac42010-12-02 06:43:39 -08002907 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002908 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002909 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002910
Casey Schauflere114e472008-02-04 22:29:50 -08002911 /*
2912 * Changing another process' Smack value is too dangerous
2913 * and supports no sane use case.
2914 */
2915 if (p != current)
2916 return -EPERM;
2917
Casey Schaufler1880eff2012-06-05 15:28:30 -07002918 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01002919 return -EPERM;
2920
Casey Schauflerf7112e62012-05-06 15:22:02 -07002921 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08002922 return -EINVAL;
2923
2924 if (strcmp(name, "current") != 0)
2925 return -EINVAL;
2926
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002927 skp = smk_import_entry(value, size);
2928 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002929 return -EINVAL;
2930
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002931 /*
2932 * No process is ever allowed the web ("@") label.
2933 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002934 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002935 return -EPERM;
2936
David Howellsd84f4f92008-11-14 10:39:23 +11002937 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002938 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002939 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002940
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07002941 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002942 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002943
David Howellsd84f4f92008-11-14 10:39:23 +11002944 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002945 return size;
2946}
2947
2948/**
2949 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002950 * @sock: one sock
2951 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002952 * @newsk: unused
2953 *
2954 * Return 0 if a subject with the smack of sock could access
2955 * an object with the smack of other, otherwise an error code
2956 */
David S. Miller3610cda2011-01-05 15:38:53 -08002957static int smack_unix_stream_connect(struct sock *sock,
2958 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002959{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002960 struct smack_known *skp;
James Morrisd2e7ad12011-01-10 09:46:24 +11002961 struct socket_smack *ssp = sock->sk_security;
2962 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002963 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002964 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002965 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002966
Kees Cook923e9a12012-04-10 13:26:44 -07002967#ifdef CONFIG_AUDIT
2968 struct lsm_network_audit net;
2969
Eric Paris48c62af2012-04-02 13:15:44 -04002970 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
David S. Miller3610cda2011-01-05 15:38:53 -08002971 smk_ad_setfield_u_net_sk(&ad, other);
Kees Cook923e9a12012-04-10 13:26:44 -07002972#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002973
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002974 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2975 skp = ssp->smk_out;
2976 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2977 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002978
Casey Schaufler975d5e52011-09-26 14:43:39 -07002979 /*
2980 * Cross reference the peer labels for SO_PEERSEC.
2981 */
2982 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002983 nsp->smk_packet = ssp->smk_out->smk_known;
2984 ssp->smk_packet = osp->smk_out->smk_known;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002985 }
2986
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002987 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002988}
2989
2990/**
2991 * smack_unix_may_send - Smack access on UDS
2992 * @sock: one socket
2993 * @other: the other socket
2994 *
2995 * Return 0 if a subject with the smack of sock could access
2996 * an object with the smack of other, otherwise an error code
2997 */
2998static int smack_unix_may_send(struct socket *sock, struct socket *other)
2999{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003000 struct socket_smack *ssp = sock->sk->sk_security;
3001 struct socket_smack *osp = other->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003002 struct smack_known *skp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003003 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003004
Kees Cook923e9a12012-04-10 13:26:44 -07003005#ifdef CONFIG_AUDIT
3006 struct lsm_network_audit net;
3007
Eric Paris48c62af2012-04-02 13:15:44 -04003008 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003009 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003010#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003011
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003012 if (smack_privileged(CAP_MAC_OVERRIDE))
3013 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003014
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003015 skp = ssp->smk_out;
3016 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003017}
3018
3019/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003020 * smack_socket_sendmsg - Smack check based on destination host
3021 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003022 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003023 * @size: the size of the message
3024 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003025 * Return 0 if the current subject can write to the destination host.
3026 * For IPv4 this is only a question if the destination is a single label host.
3027 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003028 */
3029static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3030 int size)
3031{
3032 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003033 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003034 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003035
3036 /*
3037 * Perfectly reasonable for this to be NULL
3038 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003039 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003040 return 0;
3041
Casey Schauflerc6739442013-05-22 18:42:56 -07003042 switch (sip->sin_family) {
3043 case AF_INET:
3044 rc = smack_netlabel_send(sock->sk, sip);
3045 break;
3046 case AF_INET6:
3047 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3048 break;
3049 }
3050 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003051}
3052
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003053/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003054 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003055 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003056 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003057 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003058 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003059 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003060static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3061 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003062{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003063 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003064 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003065 int acat;
3066 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003067
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003068 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003069 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003070 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003071 * If there are flags but no level netlabel isn't
3072 * behaving the way we expect it to.
3073 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003074 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003075 * Without guidance regarding the smack value
3076 * for the packet fall back on the network
3077 * ambient value.
3078 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003079 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003080 list_for_each_entry(skp, &smack_known_list, list) {
3081 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003082 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003083 /*
3084 * Compare the catsets. Use the netlbl APIs.
3085 */
3086 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3087 if ((skp->smk_netlabel.flags &
3088 NETLBL_SECATTR_MLS_CAT) == 0)
3089 found = 1;
3090 break;
3091 }
3092 for (acat = -1, kcat = -1; acat == kcat; ) {
3093 acat = netlbl_secattr_catmap_walk(
3094 sap->attr.mls.cat, acat + 1);
3095 kcat = netlbl_secattr_catmap_walk(
3096 skp->smk_netlabel.attr.mls.cat,
3097 kcat + 1);
3098 if (acat < 0 || kcat < 0)
3099 break;
3100 }
3101 if (acat == kcat) {
3102 found = 1;
3103 break;
3104 }
Casey Schauflere114e472008-02-04 22:29:50 -08003105 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003106 rcu_read_unlock();
3107
3108 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003109 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003110
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003111 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003112 return &smack_known_web;
3113 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003114 }
3115 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3116 /*
3117 * Looks like a fallback, which gives us a secid.
3118 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003119 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003120 /*
3121 * This has got to be a bug because it is
3122 * impossible to specify a fallback without
3123 * specifying the label, which will ensure
3124 * it has a secid, and the only way to get a
3125 * secid is from a fallback.
3126 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003127 BUG_ON(skp == NULL);
3128 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003129 }
3130 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003131 * Without guidance regarding the smack value
3132 * for the packet fall back on the network
3133 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003134 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003135 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003136}
3137
Casey Schaufler6ea06242013-08-05 13:21:22 -07003138static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003139{
Casey Schauflerc6739442013-05-22 18:42:56 -07003140 u8 nexthdr;
3141 int offset;
3142 int proto = -EINVAL;
3143 struct ipv6hdr _ipv6h;
3144 struct ipv6hdr *ip6;
3145 __be16 frag_off;
3146 struct tcphdr _tcph, *th;
3147 struct udphdr _udph, *uh;
3148 struct dccp_hdr _dccph, *dh;
3149
3150 sip->sin6_port = 0;
3151
3152 offset = skb_network_offset(skb);
3153 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3154 if (ip6 == NULL)
3155 return -EINVAL;
3156 sip->sin6_addr = ip6->saddr;
3157
3158 nexthdr = ip6->nexthdr;
3159 offset += sizeof(_ipv6h);
3160 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3161 if (offset < 0)
3162 return -EINVAL;
3163
3164 proto = nexthdr;
3165 switch (proto) {
3166 case IPPROTO_TCP:
3167 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3168 if (th != NULL)
3169 sip->sin6_port = th->source;
3170 break;
3171 case IPPROTO_UDP:
3172 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3173 if (uh != NULL)
3174 sip->sin6_port = uh->source;
3175 break;
3176 case IPPROTO_DCCP:
3177 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3178 if (dh != NULL)
3179 sip->sin6_port = dh->dccph_sport;
3180 break;
3181 }
3182 return proto;
3183}
3184
Casey Schauflere114e472008-02-04 22:29:50 -08003185/**
3186 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3187 * @sk: socket
3188 * @skb: packet
3189 *
3190 * Returns 0 if the packet should be delivered, an error code otherwise
3191 */
3192static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3193{
3194 struct netlbl_lsm_secattr secattr;
3195 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003196 struct smack_known *skp;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003197 struct sockaddr_in6 sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003198 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003199 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003200#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003201 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003202#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003203 switch (sk->sk_family) {
3204 case PF_INET:
3205 /*
3206 * Translate what netlabel gave us.
3207 */
3208 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003209
Casey Schauflerc6739442013-05-22 18:42:56 -07003210 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3211 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003212 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003213 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003214 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003215
Casey Schauflerc6739442013-05-22 18:42:56 -07003216 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003217
Etienne Bassetecfcc532009-04-08 20:40:06 +02003218#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003219 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3220 ad.a.u.net->family = sk->sk_family;
3221 ad.a.u.net->netif = skb->skb_iif;
3222 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003223#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003224 /*
3225 * Receiving a packet requires that the other end
3226 * be able to write here. Read access is not required.
3227 * This is the simplist possible security model
3228 * for networking.
3229 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003230 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07003231 if (rc != 0)
3232 netlbl_skbuff_err(skb, rc, 0);
3233 break;
3234 case PF_INET6:
3235 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3236 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3237 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3238 else
3239 rc = 0;
3240 break;
3241 }
Paul Moorea8134292008-10-10 10:16:31 -04003242 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003243}
3244
3245/**
3246 * smack_socket_getpeersec_stream - pull in packet label
3247 * @sock: the socket
3248 * @optval: user's destination
3249 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003250 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003251 *
3252 * returns zero on success, an error code otherwise
3253 */
3254static int smack_socket_getpeersec_stream(struct socket *sock,
3255 char __user *optval,
3256 int __user *optlen, unsigned len)
3257{
3258 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003259 char *rcp = "";
3260 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003261 int rc = 0;
3262
3263 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003264 if (ssp->smk_packet != NULL) {
3265 rcp = ssp->smk_packet;
3266 slen = strlen(rcp) + 1;
3267 }
Casey Schauflere114e472008-02-04 22:29:50 -08003268
3269 if (slen > len)
3270 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003271 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003272 rc = -EFAULT;
3273
3274 if (put_user(slen, optlen) != 0)
3275 rc = -EFAULT;
3276
3277 return rc;
3278}
3279
3280
3281/**
3282 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003283 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003284 * @skb: packet data
3285 * @secid: pointer to where to put the secid of the packet
3286 *
3287 * Sets the netlabel socket state on sk from parent
3288 */
3289static int smack_socket_getpeersec_dgram(struct socket *sock,
3290 struct sk_buff *skb, u32 *secid)
3291
3292{
3293 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003294 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003295 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003296 int family = PF_UNSPEC;
3297 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003298 int rc;
3299
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003300 if (skb != NULL) {
3301 if (skb->protocol == htons(ETH_P_IP))
3302 family = PF_INET;
3303 else if (skb->protocol == htons(ETH_P_IPV6))
3304 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003305 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003306 if (family == PF_UNSPEC && sock != NULL)
3307 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003308
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003309 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003310 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003311 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003312 } else if (family == PF_INET || family == PF_INET6) {
3313 /*
3314 * Translate what netlabel gave us.
3315 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003316 if (sock != NULL && sock->sk != NULL)
3317 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003318 netlbl_secattr_init(&secattr);
3319 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3320 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003321 skp = smack_from_secattr(&secattr, ssp);
3322 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003323 }
3324 netlbl_secattr_destroy(&secattr);
3325 }
3326 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003327 if (s == 0)
3328 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003329 return 0;
3330}
3331
3332/**
Paul Moore07feee82009-03-27 17:10:54 -04003333 * smack_sock_graft - Initialize a newly created socket with an existing sock
3334 * @sk: child sock
3335 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003336 *
Paul Moore07feee82009-03-27 17:10:54 -04003337 * Set the smk_{in,out} state of an existing sock based on the process that
3338 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003339 */
3340static void smack_sock_graft(struct sock *sk, struct socket *parent)
3341{
3342 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003343 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003344
Paul Moore07feee82009-03-27 17:10:54 -04003345 if (sk == NULL ||
3346 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003347 return;
3348
3349 ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003350 ssp->smk_in = skp->smk_known;
3351 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003352 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003353}
3354
3355/**
3356 * smack_inet_conn_request - Smack access check on connect
3357 * @sk: socket involved
3358 * @skb: packet
3359 * @req: unused
3360 *
3361 * Returns 0 if a task with the packet label could write to
3362 * the socket, otherwise an error code
3363 */
3364static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3365 struct request_sock *req)
3366{
Paul Moore07feee82009-03-27 17:10:54 -04003367 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003368 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003369 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003370 struct netlbl_lsm_secattr secattr;
3371 struct sockaddr_in addr;
3372 struct iphdr *hdr;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003373 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003374 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003375 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003376#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003377 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003378#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003379
Casey Schauflerc6739442013-05-22 18:42:56 -07003380 if (family == PF_INET6) {
3381 /*
3382 * Handle mapped IPv4 packets arriving
3383 * via IPv6 sockets. Don't set up netlabel
3384 * processing on IPv6.
3385 */
3386 if (skb->protocol == htons(ETH_P_IP))
3387 family = PF_INET;
3388 else
3389 return 0;
3390 }
Casey Schauflere114e472008-02-04 22:29:50 -08003391
Paul Moore07feee82009-03-27 17:10:54 -04003392 netlbl_secattr_init(&secattr);
3393 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003394 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003395 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003396 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003397 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003398 netlbl_secattr_destroy(&secattr);
3399
Etienne Bassetecfcc532009-04-08 20:40:06 +02003400#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003401 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3402 ad.a.u.net->family = family;
3403 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003404 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3405#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003406 /*
Paul Moore07feee82009-03-27 17:10:54 -04003407 * Receiving a packet requires that the other end be able to write
3408 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003409 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003410 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003411 if (rc != 0)
3412 return rc;
3413
3414 /*
3415 * Save the peer's label in the request_sock so we can later setup
3416 * smk_packet in the child socket so that SO_PEERCRED can report it.
3417 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003418 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003419
3420 /*
3421 * We need to decide if we want to label the incoming connection here
3422 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003423 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003424 */
3425 hdr = ip_hdr(skb);
3426 addr.sin_addr.s_addr = hdr->saddr;
3427 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003428 hsp = smack_host_label(&addr);
3429 rcu_read_unlock();
3430
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003431 if (hsp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003432 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003433 else
Paul Moore07feee82009-03-27 17:10:54 -04003434 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003435
3436 return rc;
3437}
3438
Paul Moore07feee82009-03-27 17:10:54 -04003439/**
3440 * smack_inet_csk_clone - Copy the connection information to the new socket
3441 * @sk: the new socket
3442 * @req: the connection's request_sock
3443 *
3444 * Transfer the connection's peer label to the newly created socket.
3445 */
3446static void smack_inet_csk_clone(struct sock *sk,
3447 const struct request_sock *req)
3448{
3449 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003450 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003451
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003452 if (req->peer_secid != 0) {
3453 skp = smack_from_secid(req->peer_secid);
3454 ssp->smk_packet = skp->smk_known;
3455 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003456 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003457}
3458
Casey Schauflere114e472008-02-04 22:29:50 -08003459/*
3460 * Key management security hooks
3461 *
3462 * Casey has not tested key support very heavily.
3463 * The permission check is most likely too restrictive.
3464 * If you care about keys please have a look.
3465 */
3466#ifdef CONFIG_KEYS
3467
3468/**
3469 * smack_key_alloc - Set the key security blob
3470 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003471 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003472 * @flags: unused
3473 *
3474 * No allocation required
3475 *
3476 * Returns 0
3477 */
David Howellsd84f4f92008-11-14 10:39:23 +11003478static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003479 unsigned long flags)
3480{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003481 struct smack_known *skp = smk_of_task(cred->security);
3482
3483 key->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003484 return 0;
3485}
3486
3487/**
3488 * smack_key_free - Clear the key security blob
3489 * @key: the object
3490 *
3491 * Clear the blob pointer
3492 */
3493static void smack_key_free(struct key *key)
3494{
3495 key->security = NULL;
3496}
3497
3498/*
3499 * smack_key_permission - Smack access on a key
3500 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003501 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003502 * @perm: unused
3503 *
3504 * Return 0 if the task has read and write to the object,
3505 * an error code otherwise
3506 */
3507static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003508 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003509{
3510 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003511 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003512 struct smack_known *tkp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003513
3514 keyp = key_ref_to_ptr(key_ref);
3515 if (keyp == NULL)
3516 return -EINVAL;
3517 /*
3518 * If the key hasn't been initialized give it access so that
3519 * it may do so.
3520 */
3521 if (keyp->security == NULL)
3522 return 0;
3523 /*
3524 * This should not occur
3525 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003526 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003527 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003528#ifdef CONFIG_AUDIT
3529 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3530 ad.a.u.key_struct.key = keyp->serial;
3531 ad.a.u.key_struct.key_desc = keyp->description;
3532#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003533 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003534}
3535#endif /* CONFIG_KEYS */
3536
3537/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003538 * Smack Audit hooks
3539 *
3540 * Audit requires a unique representation of each Smack specific
3541 * rule. This unique representation is used to distinguish the
3542 * object to be audited from remaining kernel objects and also
3543 * works as a glue between the audit hooks.
3544 *
3545 * Since repository entries are added but never deleted, we'll use
3546 * the smack_known label address related to the given audit rule as
3547 * the needed unique representation. This also better fits the smack
3548 * model where nearly everything is a label.
3549 */
3550#ifdef CONFIG_AUDIT
3551
3552/**
3553 * smack_audit_rule_init - Initialize a smack audit rule
3554 * @field: audit rule fields given from user-space (audit.h)
3555 * @op: required testing operator (=, !=, >, <, ...)
3556 * @rulestr: smack label to be audited
3557 * @vrule: pointer to save our own audit rule representation
3558 *
3559 * Prepare to audit cases where (@field @op @rulestr) is true.
3560 * The label to be audited is created if necessay.
3561 */
3562static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3563{
3564 char **rule = (char **)vrule;
3565 *rule = NULL;
3566
3567 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3568 return -EINVAL;
3569
Al Viro5af75d82008-12-16 05:59:26 -05003570 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003571 return -EINVAL;
3572
3573 *rule = smk_import(rulestr, 0);
3574
3575 return 0;
3576}
3577
3578/**
3579 * smack_audit_rule_known - Distinguish Smack audit rules
3580 * @krule: rule of interest, in Audit kernel representation format
3581 *
3582 * This is used to filter Smack rules from remaining Audit ones.
3583 * If it's proved that this rule belongs to us, the
3584 * audit_rule_match hook will be called to do the final judgement.
3585 */
3586static int smack_audit_rule_known(struct audit_krule *krule)
3587{
3588 struct audit_field *f;
3589 int i;
3590
3591 for (i = 0; i < krule->field_count; i++) {
3592 f = &krule->fields[i];
3593
3594 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3595 return 1;
3596 }
3597
3598 return 0;
3599}
3600
3601/**
3602 * smack_audit_rule_match - Audit given object ?
3603 * @secid: security id for identifying the object to test
3604 * @field: audit rule flags given from user-space
3605 * @op: required testing operator
3606 * @vrule: smack internal rule presentation
3607 * @actx: audit context associated with the check
3608 *
3609 * The core Audit hook. It's used to take the decision of
3610 * whether to audit or not to audit a given object.
3611 */
3612static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3613 struct audit_context *actx)
3614{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003615 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003616 char *rule = vrule;
3617
3618 if (!rule) {
Tetsuo Handaceffec552012-03-29 16:19:05 +09003619 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003620 "Smack: missing rule\n");
3621 return -ENOENT;
3622 }
3623
3624 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3625 return 0;
3626
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003627 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003628
3629 /*
3630 * No need to do string comparisons. If a match occurs,
3631 * both pointers will point to the same smack_known
3632 * label.
3633 */
Al Viro5af75d82008-12-16 05:59:26 -05003634 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003635 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003636 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003637 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003638
3639 return 0;
3640}
3641
3642/**
3643 * smack_audit_rule_free - free smack rule representation
3644 * @vrule: rule to be freed.
3645 *
3646 * No memory was allocated.
3647 */
3648static void smack_audit_rule_free(void *vrule)
3649{
3650 /* No-op */
3651}
3652
3653#endif /* CONFIG_AUDIT */
3654
Randy Dunlap251a2a92009-02-18 11:42:33 -08003655/**
David Quigley746df9b2013-05-22 12:50:35 -04003656 * smack_ismaclabel - check if xattr @name references a smack MAC label
3657 * @name: Full xattr name to check.
3658 */
3659static int smack_ismaclabel(const char *name)
3660{
3661 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3662}
3663
3664
3665/**
Casey Schauflere114e472008-02-04 22:29:50 -08003666 * smack_secid_to_secctx - return the smack label for a secid
3667 * @secid: incoming integer
3668 * @secdata: destination
3669 * @seclen: how long it is
3670 *
3671 * Exists for networking code.
3672 */
3673static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3674{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003675 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003676
Eric Parisd5630b92010-10-13 16:24:48 -04003677 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003678 *secdata = skp->smk_known;
3679 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08003680 return 0;
3681}
3682
Randy Dunlap251a2a92009-02-18 11:42:33 -08003683/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003684 * smack_secctx_to_secid - return the secid for a smack label
3685 * @secdata: smack label
3686 * @seclen: how long result is
3687 * @secid: outgoing integer
3688 *
3689 * Exists for audit and networking code.
3690 */
David Howellse52c17642008-04-29 20:52:51 +01003691static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003692{
3693 *secid = smack_to_secid(secdata);
3694 return 0;
3695}
3696
Randy Dunlap251a2a92009-02-18 11:42:33 -08003697/**
Casey Schauflere114e472008-02-04 22:29:50 -08003698 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003699 * @secdata: unused
3700 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003701 *
3702 * Exists to make sure nothing gets done, and properly
3703 */
3704static void smack_release_secctx(char *secdata, u32 seclen)
3705{
3706}
3707
David P. Quigley1ee65e32009-09-03 14:25:57 -04003708static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3709{
3710 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3711}
3712
3713static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3714{
3715 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3716}
3717
3718static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3719{
3720 int len = 0;
3721 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3722
3723 if (len < 0)
3724 return len;
3725 *ctxlen = len;
3726 return 0;
3727}
3728
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003729struct security_operations smack_ops = {
3730 .name = "smack",
3731
Ingo Molnar9e488582009-05-07 19:26:19 +10003732 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003733 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003734 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003735
3736 .sb_alloc_security = smack_sb_alloc_security,
3737 .sb_free_security = smack_sb_free_security,
3738 .sb_copy_data = smack_sb_copy_data,
3739 .sb_kern_mount = smack_sb_kern_mount,
3740 .sb_statfs = smack_sb_statfs,
3741 .sb_mount = smack_sb_mount,
3742 .sb_umount = smack_sb_umount,
3743
Casey Schaufler676dac42010-12-02 06:43:39 -08003744 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003745 .bprm_committing_creds = smack_bprm_committing_creds,
3746 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003747
Casey Schauflere114e472008-02-04 22:29:50 -08003748 .inode_alloc_security = smack_inode_alloc_security,
3749 .inode_free_security = smack_inode_free_security,
3750 .inode_init_security = smack_inode_init_security,
3751 .inode_link = smack_inode_link,
3752 .inode_unlink = smack_inode_unlink,
3753 .inode_rmdir = smack_inode_rmdir,
3754 .inode_rename = smack_inode_rename,
3755 .inode_permission = smack_inode_permission,
3756 .inode_setattr = smack_inode_setattr,
3757 .inode_getattr = smack_inode_getattr,
3758 .inode_setxattr = smack_inode_setxattr,
3759 .inode_post_setxattr = smack_inode_post_setxattr,
3760 .inode_getxattr = smack_inode_getxattr,
3761 .inode_removexattr = smack_inode_removexattr,
3762 .inode_getsecurity = smack_inode_getsecurity,
3763 .inode_setsecurity = smack_inode_setsecurity,
3764 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003765 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003766
3767 .file_permission = smack_file_permission,
3768 .file_alloc_security = smack_file_alloc_security,
3769 .file_free_security = smack_file_free_security,
3770 .file_ioctl = smack_file_ioctl,
3771 .file_lock = smack_file_lock,
3772 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003773 .mmap_file = smack_mmap_file,
3774 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003775 .file_set_fowner = smack_file_set_fowner,
3776 .file_send_sigiotask = smack_file_send_sigiotask,
3777 .file_receive = smack_file_receive,
3778
Eric Paris83d49852012-04-04 13:45:40 -04003779 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003780
David Howellsee18d642009-09-02 09:14:21 +01003781 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003782 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003783 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003784 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003785 .kernel_act_as = smack_kernel_act_as,
3786 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003787 .task_setpgid = smack_task_setpgid,
3788 .task_getpgid = smack_task_getpgid,
3789 .task_getsid = smack_task_getsid,
3790 .task_getsecid = smack_task_getsecid,
3791 .task_setnice = smack_task_setnice,
3792 .task_setioprio = smack_task_setioprio,
3793 .task_getioprio = smack_task_getioprio,
3794 .task_setscheduler = smack_task_setscheduler,
3795 .task_getscheduler = smack_task_getscheduler,
3796 .task_movememory = smack_task_movememory,
3797 .task_kill = smack_task_kill,
3798 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003799 .task_to_inode = smack_task_to_inode,
3800
3801 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003802 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003803
3804 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3805 .msg_msg_free_security = smack_msg_msg_free_security,
3806
3807 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3808 .msg_queue_free_security = smack_msg_queue_free_security,
3809 .msg_queue_associate = smack_msg_queue_associate,
3810 .msg_queue_msgctl = smack_msg_queue_msgctl,
3811 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3812 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3813
3814 .shm_alloc_security = smack_shm_alloc_security,
3815 .shm_free_security = smack_shm_free_security,
3816 .shm_associate = smack_shm_associate,
3817 .shm_shmctl = smack_shm_shmctl,
3818 .shm_shmat = smack_shm_shmat,
3819
3820 .sem_alloc_security = smack_sem_alloc_security,
3821 .sem_free_security = smack_sem_free_security,
3822 .sem_associate = smack_sem_associate,
3823 .sem_semctl = smack_sem_semctl,
3824 .sem_semop = smack_sem_semop,
3825
Casey Schauflere114e472008-02-04 22:29:50 -08003826 .d_instantiate = smack_d_instantiate,
3827
3828 .getprocattr = smack_getprocattr,
3829 .setprocattr = smack_setprocattr,
3830
3831 .unix_stream_connect = smack_unix_stream_connect,
3832 .unix_may_send = smack_unix_may_send,
3833
3834 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07003835 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003836 .socket_connect = smack_socket_connect,
3837 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003838 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3839 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3840 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3841 .sk_alloc_security = smack_sk_alloc_security,
3842 .sk_free_security = smack_sk_free_security,
3843 .sock_graft = smack_sock_graft,
3844 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003845 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003846
Casey Schauflere114e472008-02-04 22:29:50 -08003847 /* key management security hooks */
3848#ifdef CONFIG_KEYS
3849 .key_alloc = smack_key_alloc,
3850 .key_free = smack_key_free,
3851 .key_permission = smack_key_permission,
3852#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003853
3854 /* Audit hooks */
3855#ifdef CONFIG_AUDIT
3856 .audit_rule_init = smack_audit_rule_init,
3857 .audit_rule_known = smack_audit_rule_known,
3858 .audit_rule_match = smack_audit_rule_match,
3859 .audit_rule_free = smack_audit_rule_free,
3860#endif /* CONFIG_AUDIT */
3861
David Quigley746df9b2013-05-22 12:50:35 -04003862 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08003863 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003864 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003865 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003866 .inode_notifysecctx = smack_inode_notifysecctx,
3867 .inode_setsecctx = smack_inode_setsecctx,
3868 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003869};
3870
Etienne Basset7198e2e2009-03-24 20:53:24 +01003871
Casey Schaufler86812bb2012-04-17 18:55:46 -07003872static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003873{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003874 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003875 * Initialize rule list locks
3876 */
3877 mutex_init(&smack_known_huh.smk_rules_lock);
3878 mutex_init(&smack_known_hat.smk_rules_lock);
3879 mutex_init(&smack_known_floor.smk_rules_lock);
3880 mutex_init(&smack_known_star.smk_rules_lock);
3881 mutex_init(&smack_known_invalid.smk_rules_lock);
3882 mutex_init(&smack_known_web.smk_rules_lock);
3883 /*
3884 * Initialize rule lists
3885 */
3886 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3887 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3888 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3889 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3890 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3891 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3892 /*
3893 * Create the known labels list
3894 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02003895 smk_insert_entry(&smack_known_huh);
3896 smk_insert_entry(&smack_known_hat);
3897 smk_insert_entry(&smack_known_star);
3898 smk_insert_entry(&smack_known_floor);
3899 smk_insert_entry(&smack_known_invalid);
3900 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01003901}
3902
Casey Schauflere114e472008-02-04 22:29:50 -08003903/**
3904 * smack_init - initialize the smack system
3905 *
3906 * Returns 0
3907 */
3908static __init int smack_init(void)
3909{
David Howellsd84f4f92008-11-14 10:39:23 +11003910 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003911 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003912
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003913 if (!security_module_enable(&smack_ops))
3914 return 0;
3915
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003916 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3917 GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003918 if (tsp == NULL)
3919 return -ENOMEM;
3920
Casey Schauflere114e472008-02-04 22:29:50 -08003921 printk(KERN_INFO "Smack: Initializing.\n");
3922
3923 /*
3924 * Set the security state for the initial task.
3925 */
David Howellsd84f4f92008-11-14 10:39:23 +11003926 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003927 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003928
Casey Schaufler86812bb2012-04-17 18:55:46 -07003929 /* initialize the smack_known_list */
3930 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003931
3932 /*
3933 * Register with LSM
3934 */
3935 if (register_security(&smack_ops))
3936 panic("smack: Unable to register with kernel.\n");
3937
3938 return 0;
3939}
3940
3941/*
3942 * Smack requires early initialization in order to label
3943 * all processes and objects when they are created.
3944 */
3945security_initcall(smack_init);