blob: 62ebf4f8a6c76e491e192ead7568dd8c5cad5c87 [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 Schauflerb5dfd802013-10-22 11:47:45 -0700188 rc = smk_curacc(skp->smk_known, mode, &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 Schaufler19760ad2013-12-16 16:27:26 -0800840 struct smack_known *skp;
841 int check_priv = 0;
842 int check_import = 0;
843 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800844 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800845
Casey Schaufler19760ad2013-12-16 16:27:26 -0800846 /*
847 * Check label validity here so import won't fail in post_setxattr
848 */
Casey Schauflerbcdca222008-02-23 15:24:04 -0800849 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
850 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -0800851 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
852 check_priv = 1;
853 check_import = 1;
854 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
855 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
856 check_priv = 1;
857 check_import = 1;
858 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200859 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -0800860 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200861 if (size != TRANS_TRUE_SIZE ||
862 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
863 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800864 } else
865 rc = cap_inode_setxattr(dentry, name, value, size, flags);
866
Casey Schaufler19760ad2013-12-16 16:27:26 -0800867 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
868 rc = -EPERM;
869
870 if (rc == 0 && check_import) {
871 skp = smk_import_entry(value, size);
872 if (skp == NULL || (check_star &&
873 (skp == &smack_known_star || skp == &smack_known_web)))
874 rc = -EINVAL;
875 }
876
Eric Parisa2694342011-04-25 13:10:27 -0400877 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200878 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
879
Casey Schauflerbcdca222008-02-23 15:24:04 -0800880 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200881 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800882
883 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800884}
885
886/**
887 * smack_inode_post_setxattr - Apply the Smack update approved above
888 * @dentry: object
889 * @name: attribute name
890 * @value: attribute value
891 * @size: attribute size
892 * @flags: unused
893 *
894 * Set the pointer in the inode blob to the entry found
895 * in the master label list.
896 */
David Howells8f0cfa52008-04-29 00:59:41 -0700897static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
898 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800899{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700900 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200901 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800902
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700903 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
904 isp->smk_flags |= SMK_INODE_TRANSMUTE;
905 return;
906 }
907
908 skp = smk_import_entry(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800909 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700910 if (skp != NULL)
911 isp->smk_inode = skp->smk_known;
Casey Schaufler676dac42010-12-02 06:43:39 -0800912 else
913 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200914 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700915 if (skp != NULL)
916 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800917 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700918 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800919 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700920 if (skp != NULL)
921 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800922 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700923 isp->smk_mmap = &smack_known_invalid;
924 }
Casey Schauflere114e472008-02-04 22:29:50 -0800925
926 return;
927}
928
Casey Schauflerce8a4322011-09-29 18:21:01 -0700929/**
Casey Schauflere114e472008-02-04 22:29:50 -0800930 * smack_inode_getxattr - Smack check on getxattr
931 * @dentry: the object
932 * @name: unused
933 *
934 * Returns 0 if access is permitted, an error code otherwise
935 */
David Howells8f0cfa52008-04-29 00:59:41 -0700936static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800937{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200938 struct smk_audit_info ad;
939
Eric Parisa2694342011-04-25 13:10:27 -0400940 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200941 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
942
943 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800944}
945
Casey Schauflerce8a4322011-09-29 18:21:01 -0700946/**
Casey Schauflere114e472008-02-04 22:29:50 -0800947 * smack_inode_removexattr - Smack check on removexattr
948 * @dentry: the object
949 * @name: name of the attribute
950 *
951 * Removing the Smack attribute requires CAP_MAC_ADMIN
952 *
953 * Returns 0 if access is permitted, an error code otherwise
954 */
David Howells8f0cfa52008-04-29 00:59:41 -0700955static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800956{
Casey Schaufler676dac42010-12-02 06:43:39 -0800957 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200958 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800959 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800960
Casey Schauflerbcdca222008-02-23 15:24:04 -0800961 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
962 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800963 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200964 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800965 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
966 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700967 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800968 rc = -EPERM;
969 } else
970 rc = cap_inode_removexattr(dentry, name);
971
Eric Parisa2694342011-04-25 13:10:27 -0400972 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200973 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800974 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200975 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800976
Casey Schaufler676dac42010-12-02 06:43:39 -0800977 if (rc == 0) {
978 isp = dentry->d_inode->i_security;
979 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800980 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800981 }
982
Casey Schauflerbcdca222008-02-23 15:24:04 -0800983 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800984}
985
986/**
987 * smack_inode_getsecurity - get smack xattrs
988 * @inode: the object
989 * @name: attribute name
990 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800991 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800992 *
993 * Returns the size of the attribute or an error code
994 */
995static int smack_inode_getsecurity(const struct inode *inode,
996 const char *name, void **buffer,
997 bool alloc)
998{
999 struct socket_smack *ssp;
1000 struct socket *sock;
1001 struct super_block *sbp;
1002 struct inode *ip = (struct inode *)inode;
1003 char *isp;
1004 int ilen;
1005 int rc = 0;
1006
1007 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1008 isp = smk_of_inode(inode);
1009 ilen = strlen(isp) + 1;
1010 *buffer = isp;
1011 return ilen;
1012 }
1013
1014 /*
1015 * The rest of the Smack xattrs are only on sockets.
1016 */
1017 sbp = ip->i_sb;
1018 if (sbp->s_magic != SOCKFS_MAGIC)
1019 return -EOPNOTSUPP;
1020
1021 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001022 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001023 return -EOPNOTSUPP;
1024
1025 ssp = sock->sk->sk_security;
1026
1027 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1028 isp = ssp->smk_in;
1029 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001030 isp = ssp->smk_out->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001031 else
1032 return -EOPNOTSUPP;
1033
1034 ilen = strlen(isp) + 1;
1035 if (rc == 0) {
1036 *buffer = isp;
1037 rc = ilen;
1038 }
1039
1040 return rc;
1041}
1042
1043
1044/**
1045 * smack_inode_listsecurity - list the Smack attributes
1046 * @inode: the object
1047 * @buffer: where they go
1048 * @buffer_size: size of buffer
1049 *
1050 * Returns 0 on success, -EINVAL otherwise
1051 */
1052static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1053 size_t buffer_size)
1054{
1055 int len = strlen(XATTR_NAME_SMACK);
1056
1057 if (buffer != NULL && len <= buffer_size) {
1058 memcpy(buffer, XATTR_NAME_SMACK, len);
1059 return len;
1060 }
1061 return -EINVAL;
1062}
1063
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001064/**
1065 * smack_inode_getsecid - Extract inode's security id
1066 * @inode: inode to extract the info from
1067 * @secid: where result will be saved
1068 */
1069static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1070{
1071 struct inode_smack *isp = inode->i_security;
1072
1073 *secid = smack_to_secid(isp->smk_inode);
1074}
1075
Casey Schauflere114e472008-02-04 22:29:50 -08001076/*
1077 * File Hooks
1078 */
1079
1080/**
1081 * smack_file_permission - Smack check on file operations
1082 * @file: unused
1083 * @mask: unused
1084 *
1085 * Returns 0
1086 *
1087 * Should access checks be done on each read or write?
1088 * UNICOS and SELinux say yes.
1089 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1090 *
1091 * I'll say no for now. Smack does not do the frequent
1092 * label changing that SELinux does.
1093 */
1094static int smack_file_permission(struct file *file, int mask)
1095{
1096 return 0;
1097}
1098
1099/**
1100 * smack_file_alloc_security - assign a file security blob
1101 * @file: the object
1102 *
1103 * The security blob for a file is a pointer to the master
1104 * label list, so no allocation is done.
1105 *
1106 * Returns 0
1107 */
1108static int smack_file_alloc_security(struct file *file)
1109{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001110 struct smack_known *skp = smk_of_current();
1111
1112 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001113 return 0;
1114}
1115
1116/**
1117 * smack_file_free_security - clear a file security blob
1118 * @file: the object
1119 *
1120 * The security blob for a file is a pointer to the master
1121 * label list, so no memory is freed.
1122 */
1123static void smack_file_free_security(struct file *file)
1124{
1125 file->f_security = NULL;
1126}
1127
1128/**
1129 * smack_file_ioctl - Smack check on ioctls
1130 * @file: the object
1131 * @cmd: what to do
1132 * @arg: unused
1133 *
1134 * Relies heavily on the correct use of the ioctl command conventions.
1135 *
1136 * Returns 0 if allowed, error code otherwise
1137 */
1138static int smack_file_ioctl(struct file *file, unsigned int cmd,
1139 unsigned long arg)
1140{
1141 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001142 struct smk_audit_info ad;
1143
Eric Parisf48b7392011-04-25 12:54:27 -04001144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001146
1147 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001149
1150 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001151 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001152
1153 return rc;
1154}
1155
1156/**
1157 * smack_file_lock - Smack check on file locking
1158 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001159 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001160 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001161 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001162 */
1163static int smack_file_lock(struct file *file, unsigned int cmd)
1164{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001165 struct smk_audit_info ad;
1166
Eric Paris92f42502011-04-25 13:15:55 -04001167 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1168 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001169 return smk_curacc(file->f_security, MAY_LOCK, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001170}
1171
1172/**
1173 * smack_file_fcntl - Smack check on fcntl
1174 * @file: the object
1175 * @cmd: what action to check
1176 * @arg: unused
1177 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001178 * Generally these operations are harmless.
1179 * File locking operations present an obvious mechanism
1180 * for passing information, so they require write access.
1181 *
Casey Schauflere114e472008-02-04 22:29:50 -08001182 * Returns 0 if current has access, error code otherwise
1183 */
1184static int smack_file_fcntl(struct file *file, unsigned int cmd,
1185 unsigned long arg)
1186{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001187 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001188 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001189
Etienne Bassetecfcc532009-04-08 20:40:06 +02001190
Casey Schauflere114e472008-02-04 22:29:50 -08001191 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001192 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001193 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001194 case F_SETLK:
1195 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001196 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1197 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1198 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1199 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001200 case F_SETOWN:
1201 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001202 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1203 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001204 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001205 break;
1206 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001207 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001208 }
1209
1210 return rc;
1211}
1212
1213/**
Al Viroe5467852012-05-30 13:30:51 -04001214 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001215 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1216 * if mapping anonymous memory.
1217 * @file contains the file structure for file to map (may be NULL).
1218 * @reqprot contains the protection requested by the application.
1219 * @prot contains the protection that will be applied by the kernel.
1220 * @flags contains the operational flags.
1221 * Return 0 if permission is granted.
1222 */
Al Viroe5467852012-05-30 13:30:51 -04001223static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001224 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001225 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001226{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001227 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001228 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001229 struct smack_rule *srp;
1230 struct task_smack *tsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001231 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001232 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001233 int may;
1234 int mmay;
1235 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001236 int rc;
1237
Al Viro496ad9a2013-01-23 17:07:38 -05001238 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001239 return 0;
1240
Al Viro496ad9a2013-01-23 17:07:38 -05001241 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001242 if (isp->smk_mmap == NULL)
1243 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001244 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001245
1246 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001247 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001248 rc = 0;
1249
1250 rcu_read_lock();
1251 /*
1252 * For each Smack rule associated with the subject
1253 * label verify that the SMACK64MMAP also has access
1254 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001255 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001256 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001257 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001258 /*
1259 * Matching labels always allows access.
1260 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001261 if (mkp->smk_known == osmack)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001262 continue;
1263 /*
1264 * If there is a matching local rule take
1265 * that into account as well.
1266 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001267 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001268 &tsp->smk_rules);
1269 if (may == -ENOENT)
1270 may = srp->smk_access;
1271 else
1272 may &= srp->smk_access;
1273 /*
1274 * If may is zero the SMACK64MMAP subject can't
1275 * possibly have less access.
1276 */
1277 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001278 continue;
1279
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001280 /*
1281 * Fetch the global list entry.
1282 * If there isn't one a SMACK64MMAP subject
1283 * can't have as much access as current.
1284 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001285 mmay = smk_access_entry(mkp->smk_known, osmack,
1286 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001287 if (mmay == -ENOENT) {
1288 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001289 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001290 }
1291 /*
1292 * If there is a local entry it modifies the
1293 * potential access, too.
1294 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001295 tmay = smk_access_entry(mkp->smk_known, osmack,
1296 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001297 if (tmay != -ENOENT)
1298 mmay &= tmay;
1299
1300 /*
1301 * If there is any access available to current that is
1302 * not available to a SMACK64MMAP subject
1303 * deny access.
1304 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001305 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001306 rc = -EACCES;
1307 break;
1308 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001309 }
1310
1311 rcu_read_unlock();
1312
1313 return rc;
1314}
1315
1316/**
Casey Schauflere114e472008-02-04 22:29:50 -08001317 * smack_file_set_fowner - set the file security blob value
1318 * @file: object in question
1319 *
1320 * Returns 0
1321 * Further research may be required on this one.
1322 */
1323static int smack_file_set_fowner(struct file *file)
1324{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001325 struct smack_known *skp = smk_of_current();
1326
1327 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001328 return 0;
1329}
1330
1331/**
1332 * smack_file_send_sigiotask - Smack on sigio
1333 * @tsk: The target task
1334 * @fown: the object the signal come from
1335 * @signum: unused
1336 *
1337 * Allow a privileged task to get signals even if it shouldn't
1338 *
1339 * Returns 0 if a subject with the object's smack could
1340 * write to the task, an error code otherwise.
1341 */
1342static int smack_file_send_sigiotask(struct task_struct *tsk,
1343 struct fown_struct *fown, int signum)
1344{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001345 struct smack_known *skp;
1346 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001347 struct file *file;
1348 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001349 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001350
1351 /*
1352 * struct fown_struct is never outside the context of a struct file
1353 */
1354 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001355
Etienne Bassetecfcc532009-04-08 20:40:06 +02001356 /* we don't log here as rc can be overriden */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001357 skp = smk_find_entry(file->f_security);
1358 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001359 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360 rc = 0;
1361
1362 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1363 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001364 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001365 return rc;
1366}
1367
1368/**
1369 * smack_file_receive - Smack file receive check
1370 * @file: the object
1371 *
1372 * Returns 0 if current has access, error code otherwise
1373 */
1374static int smack_file_receive(struct file *file)
1375{
1376 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001377 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001378
Etienne Bassetecfcc532009-04-08 20:40:06 +02001379 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1380 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001381 /*
1382 * This code relies on bitmasks.
1383 */
1384 if (file->f_mode & FMODE_READ)
1385 may = MAY_READ;
1386 if (file->f_mode & FMODE_WRITE)
1387 may |= MAY_WRITE;
1388
Etienne Bassetecfcc532009-04-08 20:40:06 +02001389 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001390}
1391
Casey Schaufler531f1d42011-09-19 12:41:42 -07001392/**
Eric Paris83d49852012-04-04 13:45:40 -04001393 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001394 * @file: the object
1395 * @cred: unused
1396 *
1397 * Set the security blob in the file structure.
1398 *
1399 * Returns 0
1400 */
Eric Paris83d49852012-04-04 13:45:40 -04001401static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001402{
Al Viro496ad9a2013-01-23 17:07:38 -05001403 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001404
1405 file->f_security = isp->smk_inode;
1406
1407 return 0;
1408}
1409
Casey Schauflere114e472008-02-04 22:29:50 -08001410/*
1411 * Task hooks
1412 */
1413
1414/**
David Howellsee18d642009-09-02 09:14:21 +01001415 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1416 * @new: the new credentials
1417 * @gfp: the atomicity of any memory allocations
1418 *
1419 * Prepare a blank set of credentials for modification. This must allocate all
1420 * the memory the LSM module might require such that cred_transfer() can
1421 * complete without error.
1422 */
1423static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1424{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001425 struct task_smack *tsp;
1426
1427 tsp = new_task_smack(NULL, NULL, gfp);
1428 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001429 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001430
1431 cred->security = tsp;
1432
David Howellsee18d642009-09-02 09:14:21 +01001433 return 0;
1434}
1435
1436
1437/**
David Howellsf1752ee2008-11-14 10:39:17 +11001438 * smack_cred_free - "free" task-level security credentials
1439 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001440 *
Casey Schauflere114e472008-02-04 22:29:50 -08001441 */
David Howellsf1752ee2008-11-14 10:39:17 +11001442static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001443{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001444 struct task_smack *tsp = cred->security;
1445 struct smack_rule *rp;
1446 struct list_head *l;
1447 struct list_head *n;
1448
1449 if (tsp == NULL)
1450 return;
1451 cred->security = NULL;
1452
1453 list_for_each_safe(l, n, &tsp->smk_rules) {
1454 rp = list_entry(l, struct smack_rule, list);
1455 list_del(&rp->list);
1456 kfree(rp);
1457 }
1458 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001459}
1460
1461/**
David Howellsd84f4f92008-11-14 10:39:23 +11001462 * smack_cred_prepare - prepare new set of credentials for modification
1463 * @new: the new credentials
1464 * @old: the original credentials
1465 * @gfp: the atomicity of any memory allocations
1466 *
1467 * Prepare a new set of credentials for modification.
1468 */
1469static int smack_cred_prepare(struct cred *new, const struct cred *old,
1470 gfp_t gfp)
1471{
Casey Schaufler676dac42010-12-02 06:43:39 -08001472 struct task_smack *old_tsp = old->security;
1473 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001474 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001475
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001476 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001477 if (new_tsp == NULL)
1478 return -ENOMEM;
1479
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001480 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1481 if (rc != 0)
1482 return rc;
1483
Casey Schaufler676dac42010-12-02 06:43:39 -08001484 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001485 return 0;
1486}
1487
Randy Dunlap251a2a92009-02-18 11:42:33 -08001488/**
David Howellsee18d642009-09-02 09:14:21 +01001489 * smack_cred_transfer - Transfer the old credentials to the new credentials
1490 * @new: the new credentials
1491 * @old: the original credentials
1492 *
1493 * Fill in a set of blank credentials from another set of credentials.
1494 */
1495static void smack_cred_transfer(struct cred *new, const struct cred *old)
1496{
Casey Schaufler676dac42010-12-02 06:43:39 -08001497 struct task_smack *old_tsp = old->security;
1498 struct task_smack *new_tsp = new->security;
1499
1500 new_tsp->smk_task = old_tsp->smk_task;
1501 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001502 mutex_init(&new_tsp->smk_rules_lock);
1503 INIT_LIST_HEAD(&new_tsp->smk_rules);
1504
1505
1506 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001507}
1508
1509/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001510 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001511 * @new: points to the set of credentials to be modified.
1512 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001513 *
1514 * Set the security data for a kernel service.
1515 */
1516static int smack_kernel_act_as(struct cred *new, u32 secid)
1517{
Casey Schaufler676dac42010-12-02 06:43:39 -08001518 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001519 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001520
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001521 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001522 return -EINVAL;
1523
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001524 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001525 return 0;
1526}
1527
1528/**
1529 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001530 * @new: points to the set of credentials to be modified
1531 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001532 *
1533 * Set the file creation context in a set of credentials to the same
1534 * as the objective context of the specified inode
1535 */
1536static int smack_kernel_create_files_as(struct cred *new,
1537 struct inode *inode)
1538{
1539 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001540 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001541
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001542 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1543 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001544 return 0;
1545}
1546
1547/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001548 * smk_curacc_on_task - helper to log task related access
1549 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001550 * @access: the access requested
1551 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001552 *
1553 * Return 0 if access is permitted
1554 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001555static int smk_curacc_on_task(struct task_struct *p, int access,
1556 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001557{
1558 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001559 struct smack_known *skp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001560
Casey Schaufler531f1d42011-09-19 12:41:42 -07001561 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001562 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001563 return smk_curacc(skp->smk_known, access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001564}
1565
1566/**
Casey Schauflere114e472008-02-04 22:29:50 -08001567 * smack_task_setpgid - Smack check on setting pgid
1568 * @p: the task object
1569 * @pgid: unused
1570 *
1571 * Return 0 if write access is permitted
1572 */
1573static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1574{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001575 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001576}
1577
1578/**
1579 * smack_task_getpgid - Smack access check for getpgid
1580 * @p: the object task
1581 *
1582 * Returns 0 if current can read the object task, error code otherwise
1583 */
1584static int smack_task_getpgid(struct task_struct *p)
1585{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001586 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001587}
1588
1589/**
1590 * smack_task_getsid - Smack access check for getsid
1591 * @p: the object task
1592 *
1593 * Returns 0 if current can read the object task, error code otherwise
1594 */
1595static int smack_task_getsid(struct task_struct *p)
1596{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001597 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001598}
1599
1600/**
1601 * smack_task_getsecid - get the secid of the task
1602 * @p: the object task
1603 * @secid: where to put the result
1604 *
1605 * Sets the secid to contain a u32 version of the smack label.
1606 */
1607static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1608{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001609 struct smack_known *skp = smk_of_task(task_security(p));
1610
1611 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001612}
1613
1614/**
1615 * smack_task_setnice - Smack check on setting nice
1616 * @p: the task object
1617 * @nice: unused
1618 *
1619 * Return 0 if write access is permitted
1620 */
1621static int smack_task_setnice(struct task_struct *p, int nice)
1622{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001623 int rc;
1624
1625 rc = cap_task_setnice(p, nice);
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_setioprio - Smack check on setting ioprio
1633 * @p: the task object
1634 * @ioprio: unused
1635 *
1636 * Return 0 if write access is permitted
1637 */
1638static int smack_task_setioprio(struct task_struct *p, int ioprio)
1639{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001640 int rc;
1641
1642 rc = cap_task_setioprio(p, ioprio);
1643 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001644 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001645 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001646}
1647
1648/**
1649 * smack_task_getioprio - Smack check on reading ioprio
1650 * @p: the task object
1651 *
1652 * Return 0 if read access is permitted
1653 */
1654static int smack_task_getioprio(struct task_struct *p)
1655{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001656 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001657}
1658
1659/**
1660 * smack_task_setscheduler - Smack check on setting scheduler
1661 * @p: the task object
1662 * @policy: unused
1663 * @lp: unused
1664 *
1665 * Return 0 if read access is permitted
1666 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001667static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001668{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001669 int rc;
1670
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001671 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001672 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001673 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001674 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001675}
1676
1677/**
1678 * smack_task_getscheduler - Smack check on reading scheduler
1679 * @p: the task object
1680 *
1681 * Return 0 if read access is permitted
1682 */
1683static int smack_task_getscheduler(struct task_struct *p)
1684{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001685 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001686}
1687
1688/**
1689 * smack_task_movememory - Smack check on moving memory
1690 * @p: the task object
1691 *
1692 * Return 0 if write access is permitted
1693 */
1694static int smack_task_movememory(struct task_struct *p)
1695{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001696 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001697}
1698
1699/**
1700 * smack_task_kill - Smack check on signal delivery
1701 * @p: the task object
1702 * @info: unused
1703 * @sig: unused
1704 * @secid: identifies the smack to use in lieu of current's
1705 *
1706 * Return 0 if write access is permitted
1707 *
1708 * The secid behavior is an artifact of an SELinux hack
1709 * in the USB code. Someday it may go away.
1710 */
1711static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1712 int sig, u32 secid)
1713{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001714 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001715 struct smack_known *skp;
1716 struct smack_known *tkp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001717
1718 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1719 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001720 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001721 * Sending a signal requires that the sender
1722 * can write the receiver.
1723 */
1724 if (secid == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001725 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001726 /*
1727 * If the secid isn't 0 we're dealing with some USB IO
1728 * specific behavior. This is not clean. For one thing
1729 * we can't take privilege into account.
1730 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001731 skp = smack_from_secid(secid);
1732 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001733}
1734
1735/**
1736 * smack_task_wait - Smack access check for waiting
1737 * @p: task to wait for
1738 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001739 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08001740 */
1741static int smack_task_wait(struct task_struct *p)
1742{
Casey Schauflere114e472008-02-04 22:29:50 -08001743 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001744 * Allow the operation to succeed.
1745 * Zombies are bad.
1746 * In userless environments (e.g. phones) programs
1747 * get marked with SMACK64EXEC and even if the parent
1748 * and child shouldn't be talking the parent still
1749 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08001750 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001751 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001752}
1753
1754/**
1755 * smack_task_to_inode - copy task smack into the inode blob
1756 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001757 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001758 *
1759 * Sets the smack pointer in the inode security blob
1760 */
1761static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1762{
1763 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001764 struct smack_known *skp = smk_of_task(task_security(p));
1765
1766 isp->smk_inode = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001767}
1768
1769/*
1770 * Socket hooks.
1771 */
1772
1773/**
1774 * smack_sk_alloc_security - Allocate a socket blob
1775 * @sk: the socket
1776 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001777 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001778 *
1779 * Assign Smack pointers to current
1780 *
1781 * Returns 0 on success, -ENOMEM is there's no memory
1782 */
1783static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1784{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001785 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001786 struct socket_smack *ssp;
1787
1788 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1789 if (ssp == NULL)
1790 return -ENOMEM;
1791
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001792 ssp->smk_in = skp->smk_known;
1793 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001794 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001795
1796 sk->sk_security = ssp;
1797
1798 return 0;
1799}
1800
1801/**
1802 * smack_sk_free_security - Free a socket blob
1803 * @sk: the socket
1804 *
1805 * Clears the blob pointer
1806 */
1807static void smack_sk_free_security(struct sock *sk)
1808{
1809 kfree(sk->sk_security);
1810}
1811
1812/**
Paul Moore07feee82009-03-27 17:10:54 -04001813* smack_host_label - check host based restrictions
1814* @sip: the object end
1815*
1816* looks for host based access restrictions
1817*
1818* This version will only be appropriate for really small sets of single label
1819* hosts. The caller is responsible for ensuring that the RCU read lock is
1820* taken before calling this function.
1821*
1822* Returns the label of the far end or NULL if it's not special.
1823*/
1824static char *smack_host_label(struct sockaddr_in *sip)
1825{
1826 struct smk_netlbladdr *snp;
1827 struct in_addr *siap = &sip->sin_addr;
1828
1829 if (siap->s_addr == 0)
1830 return NULL;
1831
1832 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1833 /*
1834 * we break after finding the first match because
1835 * the list is sorted from longest to shortest mask
1836 * so we have found the most specific match
1837 */
1838 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001839 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1840 /* we have found the special CIPSO option */
1841 if (snp->smk_label == smack_cipso_option)
1842 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001843 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001844 }
Paul Moore07feee82009-03-27 17:10:54 -04001845
1846 return NULL;
1847}
1848
1849/**
Casey Schauflere114e472008-02-04 22:29:50 -08001850 * smack_netlabel - Set the secattr on a socket
1851 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001852 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001853 *
1854 * Convert the outbound smack value (smk_out) to a
1855 * secattr and attach it to the socket.
1856 *
1857 * Returns 0 on success or an error code
1858 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001859static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001860{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001861 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001862 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001863 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001864
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001865 /*
1866 * Usually the netlabel code will handle changing the
1867 * packet labeling based on the label.
1868 * The case of a single label host is different, because
1869 * a single label host should never get a labeled packet
1870 * even though the label is usually associated with a packet
1871 * label.
1872 */
1873 local_bh_disable();
1874 bh_lock_sock_nested(sk);
1875
1876 if (ssp->smk_out == smack_net_ambient ||
1877 labeled == SMACK_UNLABELED_SOCKET)
1878 netlbl_sock_delattr(sk);
1879 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001880 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001881 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001882 }
1883
1884 bh_unlock_sock(sk);
1885 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001886
Casey Schauflere114e472008-02-04 22:29:50 -08001887 return rc;
1888}
1889
1890/**
Paul Moore07feee82009-03-27 17:10:54 -04001891 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1892 * @sk: the socket
1893 * @sap: the destination address
1894 *
1895 * Set the correct secattr for the given socket based on the destination
1896 * address and perform any outbound access checks needed.
1897 *
1898 * Returns 0 on success or an error code.
1899 *
1900 */
1901static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1902{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001903 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001904 int rc;
1905 int sk_lbl;
1906 char *hostsp;
1907 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001908 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001909
1910 rcu_read_lock();
1911 hostsp = smack_host_label(sap);
1912 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001913#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001914 struct lsm_network_audit net;
1915
Eric Paris48c62af2012-04-02 13:15:44 -04001916 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1917 ad.a.u.net->family = sap->sin_family;
1918 ad.a.u.net->dport = sap->sin_port;
1919 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001920#endif
Kees Cook923e9a12012-04-10 13:26:44 -07001921 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001922 skp = ssp->smk_out;
1923 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001924 } else {
1925 sk_lbl = SMACK_CIPSO_SOCKET;
1926 rc = 0;
1927 }
1928 rcu_read_unlock();
1929 if (rc != 0)
1930 return rc;
1931
1932 return smack_netlabel(sk, sk_lbl);
1933}
1934
1935/**
Casey Schauflerc6739442013-05-22 18:42:56 -07001936 * smk_ipv6_port_label - Smack port access table management
1937 * @sock: socket
1938 * @address: address
1939 *
1940 * Create or update the port list entry
1941 */
1942static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1943{
1944 struct sock *sk = sock->sk;
1945 struct sockaddr_in6 *addr6;
1946 struct socket_smack *ssp = sock->sk->sk_security;
1947 struct smk_port_label *spp;
1948 unsigned short port = 0;
1949
1950 if (address == NULL) {
1951 /*
1952 * This operation is changing the Smack information
1953 * on the bound socket. Take the changes to the port
1954 * as well.
1955 */
1956 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1957 if (sk != spp->smk_sock)
1958 continue;
1959 spp->smk_in = ssp->smk_in;
1960 spp->smk_out = ssp->smk_out;
1961 return;
1962 }
1963 /*
1964 * A NULL address is only used for updating existing
1965 * bound entries. If there isn't one, it's OK.
1966 */
1967 return;
1968 }
1969
1970 addr6 = (struct sockaddr_in6 *)address;
1971 port = ntohs(addr6->sin6_port);
1972 /*
1973 * This is a special case that is safely ignored.
1974 */
1975 if (port == 0)
1976 return;
1977
1978 /*
1979 * Look for an existing port list entry.
1980 * This is an indication that a port is getting reused.
1981 */
1982 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1983 if (spp->smk_port != port)
1984 continue;
1985 spp->smk_port = port;
1986 spp->smk_sock = sk;
1987 spp->smk_in = ssp->smk_in;
1988 spp->smk_out = ssp->smk_out;
1989 return;
1990 }
1991
1992 /*
1993 * A new port entry is required.
1994 */
1995 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1996 if (spp == NULL)
1997 return;
1998
1999 spp->smk_port = port;
2000 spp->smk_sock = sk;
2001 spp->smk_in = ssp->smk_in;
2002 spp->smk_out = ssp->smk_out;
2003
2004 list_add(&spp->list, &smk_ipv6_port_list);
2005 return;
2006}
2007
2008/**
2009 * smk_ipv6_port_check - check Smack port access
2010 * @sock: socket
2011 * @address: address
2012 *
2013 * Create or update the port list entry
2014 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002015static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002016 int act)
2017{
2018 __be16 *bep;
2019 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002020 struct smk_port_label *spp;
2021 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002022 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002023 unsigned short port = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002024 char *object;
2025 struct smk_audit_info ad;
2026#ifdef CONFIG_AUDIT
2027 struct lsm_network_audit net;
2028#endif
2029
2030 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002031 skp = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002032 object = ssp->smk_in;
2033 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002034 skp = ssp->smk_out;
2035 object = smack_net_ambient->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002036 }
2037
2038 /*
2039 * Get the IP address and port from the address.
2040 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002041 port = ntohs(address->sin6_port);
2042 bep = (__be16 *)(&address->sin6_addr);
2043 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002044
2045 /*
2046 * It's remote, so port lookup does no good.
2047 */
2048 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2049 goto auditout;
2050
2051 /*
2052 * It's local so the send check has to have passed.
2053 */
2054 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002055 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002056 goto auditout;
2057 }
2058
2059 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2060 if (spp->smk_port != port)
2061 continue;
2062 object = spp->smk_in;
2063 if (act == SMK_CONNECTING)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002064 ssp->smk_packet = spp->smk_out->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002065 break;
2066 }
2067
2068auditout:
2069
2070#ifdef CONFIG_AUDIT
2071 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2072 ad.a.u.net->family = sk->sk_family;
2073 ad.a.u.net->dport = port;
2074 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002075 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002076 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002077 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002078#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002079 return smk_access(skp, object, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07002080}
2081
2082/**
Casey Schauflere114e472008-02-04 22:29:50 -08002083 * smack_inode_setsecurity - set smack xattrs
2084 * @inode: the object
2085 * @name: attribute name
2086 * @value: attribute value
2087 * @size: size of the attribute
2088 * @flags: unused
2089 *
2090 * Sets the named attribute in the appropriate blob
2091 *
2092 * Returns 0 on success, or an error code
2093 */
2094static int smack_inode_setsecurity(struct inode *inode, const char *name,
2095 const void *value, size_t size, int flags)
2096{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002097 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002098 struct inode_smack *nsp = inode->i_security;
2099 struct socket_smack *ssp;
2100 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002101 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002102
Casey Schauflerf7112e62012-05-06 15:22:02 -07002103 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08002104 return -EACCES;
2105
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002106 skp = smk_import_entry(value, size);
2107 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002108 return -EINVAL;
2109
2110 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002111 nsp->smk_inode = skp->smk_known;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002112 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002113 return 0;
2114 }
2115 /*
2116 * The rest of the Smack xattrs are only on sockets.
2117 */
2118 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2119 return -EOPNOTSUPP;
2120
2121 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002122 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002123 return -EOPNOTSUPP;
2124
2125 ssp = sock->sk->sk_security;
2126
2127 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002128 ssp->smk_in = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002129 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002130 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002131 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002132 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2133 if (rc != 0)
2134 printk(KERN_WARNING
2135 "Smack: \"%s\" netlbl error %d.\n",
2136 __func__, -rc);
2137 }
Casey Schauflere114e472008-02-04 22:29:50 -08002138 } else
2139 return -EOPNOTSUPP;
2140
Casey Schauflerc6739442013-05-22 18:42:56 -07002141 if (sock->sk->sk_family == PF_INET6)
2142 smk_ipv6_port_label(sock, NULL);
2143
Casey Schauflere114e472008-02-04 22:29:50 -08002144 return 0;
2145}
2146
2147/**
2148 * smack_socket_post_create - finish socket setup
2149 * @sock: the socket
2150 * @family: protocol family
2151 * @type: unused
2152 * @protocol: unused
2153 * @kern: unused
2154 *
2155 * Sets the netlabel information on the socket
2156 *
2157 * Returns 0 on success, and error code otherwise
2158 */
2159static int smack_socket_post_create(struct socket *sock, int family,
2160 int type, int protocol, int kern)
2161{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002162 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002163 return 0;
2164 /*
2165 * Set the outbound netlbl.
2166 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002167 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2168}
2169
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002170/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002171 * smack_socket_bind - record port binding information.
2172 * @sock: the socket
2173 * @address: the port address
2174 * @addrlen: size of the address
2175 *
2176 * Records the label bound to a port.
2177 *
2178 * Returns 0
2179 */
2180static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2181 int addrlen)
2182{
2183 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2184 smk_ipv6_port_label(sock, address);
2185
2186 return 0;
2187}
2188
2189/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002190 * smack_socket_connect - connect access check
2191 * @sock: the socket
2192 * @sap: the other end
2193 * @addrlen: size of sap
2194 *
2195 * Verifies that a connection may be possible
2196 *
2197 * Returns 0 on success, and error code otherwise
2198 */
2199static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2200 int addrlen)
2201{
Casey Schauflerc6739442013-05-22 18:42:56 -07002202 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002203
Casey Schauflerc6739442013-05-22 18:42:56 -07002204 if (sock->sk == NULL)
2205 return 0;
2206
2207 switch (sock->sk->sk_family) {
2208 case PF_INET:
2209 if (addrlen < sizeof(struct sockaddr_in))
2210 return -EINVAL;
2211 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2212 break;
2213 case PF_INET6:
2214 if (addrlen < sizeof(struct sockaddr_in6))
2215 return -EINVAL;
Casey Schaufler6ea06242013-08-05 13:21:22 -07002216 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2217 SMK_CONNECTING);
Casey Schauflerc6739442013-05-22 18:42:56 -07002218 break;
2219 }
2220 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002221}
2222
2223/**
2224 * smack_flags_to_may - convert S_ to MAY_ values
2225 * @flags: the S_ value
2226 *
2227 * Returns the equivalent MAY_ value
2228 */
2229static int smack_flags_to_may(int flags)
2230{
2231 int may = 0;
2232
2233 if (flags & S_IRUGO)
2234 may |= MAY_READ;
2235 if (flags & S_IWUGO)
2236 may |= MAY_WRITE;
2237 if (flags & S_IXUGO)
2238 may |= MAY_EXEC;
2239
2240 return may;
2241}
2242
2243/**
2244 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2245 * @msg: the object
2246 *
2247 * Returns 0
2248 */
2249static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2250{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002251 struct smack_known *skp = smk_of_current();
2252
2253 msg->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002254 return 0;
2255}
2256
2257/**
2258 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2259 * @msg: the object
2260 *
2261 * Clears the blob pointer
2262 */
2263static void smack_msg_msg_free_security(struct msg_msg *msg)
2264{
2265 msg->security = NULL;
2266}
2267
2268/**
2269 * smack_of_shm - the smack pointer for the shm
2270 * @shp: the object
2271 *
2272 * Returns a pointer to the smack value
2273 */
2274static char *smack_of_shm(struct shmid_kernel *shp)
2275{
2276 return (char *)shp->shm_perm.security;
2277}
2278
2279/**
2280 * smack_shm_alloc_security - Set the security blob for shm
2281 * @shp: the object
2282 *
2283 * Returns 0
2284 */
2285static int smack_shm_alloc_security(struct shmid_kernel *shp)
2286{
2287 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002288 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002289
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002290 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002291 return 0;
2292}
2293
2294/**
2295 * smack_shm_free_security - Clear the security blob for shm
2296 * @shp: the object
2297 *
2298 * Clears the blob pointer
2299 */
2300static void smack_shm_free_security(struct shmid_kernel *shp)
2301{
2302 struct kern_ipc_perm *isp = &shp->shm_perm;
2303
2304 isp->security = NULL;
2305}
2306
2307/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002308 * smk_curacc_shm : check if current has access on shm
2309 * @shp : the object
2310 * @access : access requested
2311 *
2312 * Returns 0 if current has the requested access, error code otherwise
2313 */
2314static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2315{
2316 char *ssp = smack_of_shm(shp);
2317 struct smk_audit_info ad;
2318
2319#ifdef CONFIG_AUDIT
2320 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2321 ad.a.u.ipc_id = shp->shm_perm.id;
2322#endif
2323 return smk_curacc(ssp, access, &ad);
2324}
2325
2326/**
Casey Schauflere114e472008-02-04 22:29:50 -08002327 * smack_shm_associate - Smack access check for shm
2328 * @shp: the object
2329 * @shmflg: access requested
2330 *
2331 * Returns 0 if current has the requested access, error code otherwise
2332 */
2333static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2334{
Casey Schauflere114e472008-02-04 22:29:50 -08002335 int may;
2336
2337 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002338 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002339}
2340
2341/**
2342 * smack_shm_shmctl - Smack access check for shm
2343 * @shp: the object
2344 * @cmd: what it wants to do
2345 *
2346 * Returns 0 if current has the requested access, error code otherwise
2347 */
2348static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2349{
Casey Schauflere114e472008-02-04 22:29:50 -08002350 int may;
2351
2352 switch (cmd) {
2353 case IPC_STAT:
2354 case SHM_STAT:
2355 may = MAY_READ;
2356 break;
2357 case IPC_SET:
2358 case SHM_LOCK:
2359 case SHM_UNLOCK:
2360 case IPC_RMID:
2361 may = MAY_READWRITE;
2362 break;
2363 case IPC_INFO:
2364 case SHM_INFO:
2365 /*
2366 * System level information.
2367 */
2368 return 0;
2369 default:
2370 return -EINVAL;
2371 }
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_shm_shmat - Smack access for shmat
2377 * @shp: the object
2378 * @shmaddr: unused
2379 * @shmflg: access requested
2380 *
2381 * Returns 0 if current has the requested access, error code otherwise
2382 */
2383static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2384 int shmflg)
2385{
Casey Schauflere114e472008-02-04 22:29:50 -08002386 int may;
2387
2388 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002389 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002390}
2391
2392/**
2393 * smack_of_sem - the smack pointer for the sem
2394 * @sma: the object
2395 *
2396 * Returns a pointer to the smack value
2397 */
2398static char *smack_of_sem(struct sem_array *sma)
2399{
2400 return (char *)sma->sem_perm.security;
2401}
2402
2403/**
2404 * smack_sem_alloc_security - Set the security blob for sem
2405 * @sma: the object
2406 *
2407 * Returns 0
2408 */
2409static int smack_sem_alloc_security(struct sem_array *sma)
2410{
2411 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002412 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002413
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002414 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002415 return 0;
2416}
2417
2418/**
2419 * smack_sem_free_security - Clear the security blob for sem
2420 * @sma: the object
2421 *
2422 * Clears the blob pointer
2423 */
2424static void smack_sem_free_security(struct sem_array *sma)
2425{
2426 struct kern_ipc_perm *isp = &sma->sem_perm;
2427
2428 isp->security = NULL;
2429}
2430
2431/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002432 * smk_curacc_sem : check if current has access on sem
2433 * @sma : the object
2434 * @access : access requested
2435 *
2436 * Returns 0 if current has the requested access, error code otherwise
2437 */
2438static int smk_curacc_sem(struct sem_array *sma, int access)
2439{
2440 char *ssp = smack_of_sem(sma);
2441 struct smk_audit_info ad;
2442
2443#ifdef CONFIG_AUDIT
2444 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2445 ad.a.u.ipc_id = sma->sem_perm.id;
2446#endif
2447 return smk_curacc(ssp, access, &ad);
2448}
2449
2450/**
Casey Schauflere114e472008-02-04 22:29:50 -08002451 * smack_sem_associate - Smack access check for sem
2452 * @sma: the object
2453 * @semflg: access requested
2454 *
2455 * Returns 0 if current has the requested access, error code otherwise
2456 */
2457static int smack_sem_associate(struct sem_array *sma, int semflg)
2458{
Casey Schauflere114e472008-02-04 22:29:50 -08002459 int may;
2460
2461 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002462 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002463}
2464
2465/**
2466 * smack_sem_shmctl - Smack access check for sem
2467 * @sma: the object
2468 * @cmd: what it wants to do
2469 *
2470 * Returns 0 if current has the requested access, error code otherwise
2471 */
2472static int smack_sem_semctl(struct sem_array *sma, int cmd)
2473{
Casey Schauflere114e472008-02-04 22:29:50 -08002474 int may;
2475
2476 switch (cmd) {
2477 case GETPID:
2478 case GETNCNT:
2479 case GETZCNT:
2480 case GETVAL:
2481 case GETALL:
2482 case IPC_STAT:
2483 case SEM_STAT:
2484 may = MAY_READ;
2485 break;
2486 case SETVAL:
2487 case SETALL:
2488 case IPC_RMID:
2489 case IPC_SET:
2490 may = MAY_READWRITE;
2491 break;
2492 case IPC_INFO:
2493 case SEM_INFO:
2494 /*
2495 * System level information
2496 */
2497 return 0;
2498 default:
2499 return -EINVAL;
2500 }
2501
Etienne Bassetecfcc532009-04-08 20:40:06 +02002502 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002503}
2504
2505/**
2506 * smack_sem_semop - Smack checks of semaphore operations
2507 * @sma: the object
2508 * @sops: unused
2509 * @nsops: unused
2510 * @alter: unused
2511 *
2512 * Treated as read and write in all cases.
2513 *
2514 * Returns 0 if access is allowed, error code otherwise
2515 */
2516static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2517 unsigned nsops, int alter)
2518{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002519 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002520}
2521
2522/**
2523 * smack_msg_alloc_security - Set the security blob for msg
2524 * @msq: the object
2525 *
2526 * Returns 0
2527 */
2528static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2529{
2530 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002531 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002532
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002533 kisp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002534 return 0;
2535}
2536
2537/**
2538 * smack_msg_free_security - Clear the security blob for msg
2539 * @msq: the object
2540 *
2541 * Clears the blob pointer
2542 */
2543static void smack_msg_queue_free_security(struct msg_queue *msq)
2544{
2545 struct kern_ipc_perm *kisp = &msq->q_perm;
2546
2547 kisp->security = NULL;
2548}
2549
2550/**
2551 * smack_of_msq - the smack pointer for the msq
2552 * @msq: the object
2553 *
2554 * Returns a pointer to the smack value
2555 */
2556static char *smack_of_msq(struct msg_queue *msq)
2557{
2558 return (char *)msq->q_perm.security;
2559}
2560
2561/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002562 * smk_curacc_msq : helper to check if current has access on msq
2563 * @msq : the msq
2564 * @access : access requested
2565 *
2566 * return 0 if current has access, error otherwise
2567 */
2568static int smk_curacc_msq(struct msg_queue *msq, int access)
2569{
2570 char *msp = smack_of_msq(msq);
2571 struct smk_audit_info ad;
2572
2573#ifdef CONFIG_AUDIT
2574 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2575 ad.a.u.ipc_id = msq->q_perm.id;
2576#endif
2577 return smk_curacc(msp, access, &ad);
2578}
2579
2580/**
Casey Schauflere114e472008-02-04 22:29:50 -08002581 * smack_msg_queue_associate - Smack access check for msg_queue
2582 * @msq: the object
2583 * @msqflg: access requested
2584 *
2585 * Returns 0 if current has the requested access, error code otherwise
2586 */
2587static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2588{
Casey Schauflere114e472008-02-04 22:29:50 -08002589 int may;
2590
2591 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002592 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002593}
2594
2595/**
2596 * smack_msg_queue_msgctl - Smack access check for msg_queue
2597 * @msq: the object
2598 * @cmd: what it wants to do
2599 *
2600 * Returns 0 if current has the requested access, error code otherwise
2601 */
2602static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2603{
Casey Schauflere114e472008-02-04 22:29:50 -08002604 int may;
2605
2606 switch (cmd) {
2607 case IPC_STAT:
2608 case MSG_STAT:
2609 may = MAY_READ;
2610 break;
2611 case IPC_SET:
2612 case IPC_RMID:
2613 may = MAY_READWRITE;
2614 break;
2615 case IPC_INFO:
2616 case MSG_INFO:
2617 /*
2618 * System level information
2619 */
2620 return 0;
2621 default:
2622 return -EINVAL;
2623 }
2624
Etienne Bassetecfcc532009-04-08 20:40:06 +02002625 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 * @msqflg: access requested
2633 *
2634 * Returns 0 if current has the requested access, error code otherwise
2635 */
2636static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2637 int msqflg)
2638{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002639 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002640
Etienne Bassetecfcc532009-04-08 20:40:06 +02002641 may = smack_flags_to_may(msqflg);
2642 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002643}
2644
2645/**
2646 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2647 * @msq: the object
2648 * @msg: unused
2649 * @target: unused
2650 * @type: unused
2651 * @mode: unused
2652 *
2653 * Returns 0 if current has read and write access, error code otherwise
2654 */
2655static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2656 struct task_struct *target, long type, int mode)
2657{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002658 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002659}
2660
2661/**
2662 * smack_ipc_permission - Smack access for ipc_permission()
2663 * @ipp: the object permissions
2664 * @flag: access requested
2665 *
2666 * Returns 0 if current has read and write access, error code otherwise
2667 */
2668static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2669{
2670 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002671 int may = smack_flags_to_may(flag);
2672 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002673
Etienne Bassetecfcc532009-04-08 20:40:06 +02002674#ifdef CONFIG_AUDIT
2675 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2676 ad.a.u.ipc_id = ipp->id;
2677#endif
2678 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002679}
2680
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002681/**
2682 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002683 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002684 * @secid: where result will be saved
2685 */
2686static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2687{
2688 char *smack = ipp->security;
2689
2690 *secid = smack_to_secid(smack);
2691}
2692
Casey Schauflere114e472008-02-04 22:29:50 -08002693/**
2694 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002695 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002696 * @inode: the object
2697 *
2698 * Set the inode's security blob if it hasn't been done already.
2699 */
2700static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2701{
2702 struct super_block *sbp;
2703 struct superblock_smack *sbsp;
2704 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002705 struct smack_known *skp;
2706 struct smack_known *ckp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002707 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002708 char trattr[TRANS_TRUE_SIZE];
2709 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002710 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002711 struct dentry *dp;
2712
2713 if (inode == NULL)
2714 return;
2715
2716 isp = inode->i_security;
2717
2718 mutex_lock(&isp->smk_lock);
2719 /*
2720 * If the inode is already instantiated
2721 * take the quick way out
2722 */
2723 if (isp->smk_flags & SMK_INODE_INSTANT)
2724 goto unlockandout;
2725
2726 sbp = inode->i_sb;
2727 sbsp = sbp->s_security;
2728 /*
2729 * We're going to use the superblock default label
2730 * if there's no label on the file.
2731 */
2732 final = sbsp->smk_default;
2733
2734 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002735 * If this is the root inode the superblock
2736 * may be in the process of initialization.
2737 * If that is the case use the root value out
2738 * of the superblock.
2739 */
2740 if (opt_dentry->d_parent == opt_dentry) {
2741 isp->smk_inode = sbsp->smk_root;
2742 isp->smk_flags |= SMK_INODE_INSTANT;
2743 goto unlockandout;
2744 }
2745
2746 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002747 * This is pretty hackish.
2748 * Casey says that we shouldn't have to do
2749 * file system specific code, but it does help
2750 * with keeping it simple.
2751 */
2752 switch (sbp->s_magic) {
2753 case SMACK_MAGIC:
2754 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002755 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002756 * that the smack file system doesn't do
2757 * extended attributes.
2758 */
2759 final = smack_known_star.smk_known;
2760 break;
2761 case PIPEFS_MAGIC:
2762 /*
2763 * Casey says pipes are easy (?)
2764 */
2765 final = smack_known_star.smk_known;
2766 break;
2767 case DEVPTS_SUPER_MAGIC:
2768 /*
2769 * devpts seems content with the label of the task.
2770 * Programs that change smack have to treat the
2771 * pty with respect.
2772 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002773 final = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002774 break;
2775 case SOCKFS_MAGIC:
2776 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002777 * Socket access is controlled by the socket
2778 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002779 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002780 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002781 break;
2782 case PROC_SUPER_MAGIC:
2783 /*
2784 * Casey says procfs appears not to care.
2785 * The superblock default suffices.
2786 */
2787 break;
2788 case TMPFS_MAGIC:
2789 /*
2790 * Device labels should come from the filesystem,
2791 * but watch out, because they're volitile,
2792 * getting recreated on every reboot.
2793 */
2794 final = smack_known_star.smk_known;
2795 /*
2796 * No break.
2797 *
2798 * If a smack value has been set we want to use it,
2799 * but since tmpfs isn't giving us the opportunity
2800 * to set mount options simulate setting the
2801 * superblock default.
2802 */
2803 default:
2804 /*
2805 * This isn't an understood special case.
2806 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002807 */
2808
2809 /*
2810 * UNIX domain sockets use lower level socket data.
2811 */
2812 if (S_ISSOCK(inode->i_mode)) {
2813 final = smack_known_star.smk_known;
2814 break;
2815 }
2816 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002817 * No xattr support means, alas, no SMACK label.
2818 * Use the aforeapplied default.
2819 * It would be curious if the label of the task
2820 * does not match that assigned.
2821 */
2822 if (inode->i_op->getxattr == NULL)
2823 break;
2824 /*
2825 * Get the dentry for xattr.
2826 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002827 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002828 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2829 if (skp != NULL)
2830 final = skp->smk_known;
Casey Schaufler2267b132012-03-13 19:14:19 -07002831
2832 /*
2833 * Transmuting directory
2834 */
2835 if (S_ISDIR(inode->i_mode)) {
2836 /*
2837 * If this is a new directory and the label was
2838 * transmuted when the inode was initialized
2839 * set the transmute attribute on the directory
2840 * and mark the inode.
2841 *
2842 * If there is a transmute attribute on the
2843 * directory mark the inode.
2844 */
2845 if (isp->smk_flags & SMK_INODE_CHANGED) {
2846 isp->smk_flags &= ~SMK_INODE_CHANGED;
2847 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002848 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002849 TRANS_TRUE, TRANS_TRUE_SIZE,
2850 0);
2851 } else {
2852 rc = inode->i_op->getxattr(dp,
2853 XATTR_NAME_SMACKTRANSMUTE, trattr,
2854 TRANS_TRUE_SIZE);
2855 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2856 TRANS_TRUE_SIZE) != 0)
2857 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002858 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002859 if (rc >= 0)
2860 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002861 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08002862 /*
2863 * Don't let the exec or mmap label be "*" or "@".
2864 */
2865 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2866 if (skp == &smack_known_star || skp == &smack_known_web)
2867 skp = NULL;
2868 isp->smk_task = skp;
2869 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2870 if (skp == &smack_known_star || skp == &smack_known_web)
2871 skp = NULL;
2872 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002873
Casey Schauflere114e472008-02-04 22:29:50 -08002874 dput(dp);
2875 break;
2876 }
2877
2878 if (final == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002879 isp->smk_inode = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002880 else
2881 isp->smk_inode = final;
2882
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002883 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002884
2885unlockandout:
2886 mutex_unlock(&isp->smk_lock);
2887 return;
2888}
2889
2890/**
2891 * smack_getprocattr - Smack process attribute access
2892 * @p: the object task
2893 * @name: the name of the attribute in /proc/.../attr
2894 * @value: where to put the result
2895 *
2896 * Places a copy of the task Smack into value
2897 *
2898 * Returns the length of the smack label or an error code
2899 */
2900static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2901{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002902 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08002903 char *cp;
2904 int slen;
2905
2906 if (strcmp(name, "current") != 0)
2907 return -EINVAL;
2908
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002909 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002910 if (cp == NULL)
2911 return -ENOMEM;
2912
2913 slen = strlen(cp);
2914 *value = cp;
2915 return slen;
2916}
2917
2918/**
2919 * smack_setprocattr - Smack process attribute setting
2920 * @p: the object task
2921 * @name: the name of the attribute in /proc/.../attr
2922 * @value: the value to set
2923 * @size: the size of the value
2924 *
2925 * Sets the Smack value of the task. Only setting self
2926 * is permitted and only with privilege
2927 *
2928 * Returns the length of the smack label or an error code
2929 */
2930static int smack_setprocattr(struct task_struct *p, char *name,
2931 void *value, size_t size)
2932{
Casey Schaufler676dac42010-12-02 06:43:39 -08002933 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002934 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002935 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002936
Casey Schauflere114e472008-02-04 22:29:50 -08002937 /*
2938 * Changing another process' Smack value is too dangerous
2939 * and supports no sane use case.
2940 */
2941 if (p != current)
2942 return -EPERM;
2943
Casey Schaufler1880eff2012-06-05 15:28:30 -07002944 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01002945 return -EPERM;
2946
Casey Schauflerf7112e62012-05-06 15:22:02 -07002947 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08002948 return -EINVAL;
2949
2950 if (strcmp(name, "current") != 0)
2951 return -EINVAL;
2952
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002953 skp = smk_import_entry(value, size);
2954 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002955 return -EINVAL;
2956
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002957 /*
2958 * No process is ever allowed the web ("@") label.
2959 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002960 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002961 return -EPERM;
2962
David Howellsd84f4f92008-11-14 10:39:23 +11002963 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002964 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002965 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002966
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07002967 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002968 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002969
David Howellsd84f4f92008-11-14 10:39:23 +11002970 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002971 return size;
2972}
2973
2974/**
2975 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002976 * @sock: one sock
2977 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002978 * @newsk: unused
2979 *
2980 * Return 0 if a subject with the smack of sock could access
2981 * an object with the smack of other, otherwise an error code
2982 */
David S. Miller3610cda2011-01-05 15:38:53 -08002983static int smack_unix_stream_connect(struct sock *sock,
2984 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002985{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002986 struct smack_known *skp;
James Morrisd2e7ad12011-01-10 09:46:24 +11002987 struct socket_smack *ssp = sock->sk_security;
2988 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002989 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002990 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002991 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002992
Kees Cook923e9a12012-04-10 13:26:44 -07002993#ifdef CONFIG_AUDIT
2994 struct lsm_network_audit net;
2995
Eric Paris48c62af2012-04-02 13:15:44 -04002996 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
David S. Miller3610cda2011-01-05 15:38:53 -08002997 smk_ad_setfield_u_net_sk(&ad, other);
Kees Cook923e9a12012-04-10 13:26:44 -07002998#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002999
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003000 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3001 skp = ssp->smk_out;
3002 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3003 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003004
Casey Schaufler975d5e52011-09-26 14:43:39 -07003005 /*
3006 * Cross reference the peer labels for SO_PEERSEC.
3007 */
3008 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003009 nsp->smk_packet = ssp->smk_out->smk_known;
3010 ssp->smk_packet = osp->smk_out->smk_known;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003011 }
3012
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003013 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003014}
3015
3016/**
3017 * smack_unix_may_send - Smack access on UDS
3018 * @sock: one socket
3019 * @other: the other socket
3020 *
3021 * Return 0 if a subject with the smack of sock could access
3022 * an object with the smack of other, otherwise an error code
3023 */
3024static int smack_unix_may_send(struct socket *sock, struct socket *other)
3025{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003026 struct socket_smack *ssp = sock->sk->sk_security;
3027 struct socket_smack *osp = other->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003028 struct smack_known *skp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003029 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003030
Kees Cook923e9a12012-04-10 13:26:44 -07003031#ifdef CONFIG_AUDIT
3032 struct lsm_network_audit net;
3033
Eric Paris48c62af2012-04-02 13:15:44 -04003034 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003035 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003036#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003037
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003038 if (smack_privileged(CAP_MAC_OVERRIDE))
3039 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003040
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003041 skp = ssp->smk_out;
3042 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003043}
3044
3045/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003046 * smack_socket_sendmsg - Smack check based on destination host
3047 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003048 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003049 * @size: the size of the message
3050 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003051 * Return 0 if the current subject can write to the destination host.
3052 * For IPv4 this is only a question if the destination is a single label host.
3053 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003054 */
3055static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3056 int size)
3057{
3058 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003059 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003060 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003061
3062 /*
3063 * Perfectly reasonable for this to be NULL
3064 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003065 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003066 return 0;
3067
Casey Schauflerc6739442013-05-22 18:42:56 -07003068 switch (sip->sin_family) {
3069 case AF_INET:
3070 rc = smack_netlabel_send(sock->sk, sip);
3071 break;
3072 case AF_INET6:
3073 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3074 break;
3075 }
3076 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003077}
3078
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003079/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003080 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003081 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003082 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003083 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003084 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003085 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003086static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3087 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003088{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003089 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003090 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003091 int acat;
3092 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003093
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003094 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003095 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003096 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003097 * If there are flags but no level netlabel isn't
3098 * behaving the way we expect it to.
3099 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003100 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003101 * Without guidance regarding the smack value
3102 * for the packet fall back on the network
3103 * ambient value.
3104 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003105 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003106 list_for_each_entry(skp, &smack_known_list, list) {
3107 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003108 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003109 /*
3110 * Compare the catsets. Use the netlbl APIs.
3111 */
3112 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3113 if ((skp->smk_netlabel.flags &
3114 NETLBL_SECATTR_MLS_CAT) == 0)
3115 found = 1;
3116 break;
3117 }
3118 for (acat = -1, kcat = -1; acat == kcat; ) {
3119 acat = netlbl_secattr_catmap_walk(
3120 sap->attr.mls.cat, acat + 1);
3121 kcat = netlbl_secattr_catmap_walk(
3122 skp->smk_netlabel.attr.mls.cat,
3123 kcat + 1);
3124 if (acat < 0 || kcat < 0)
3125 break;
3126 }
3127 if (acat == kcat) {
3128 found = 1;
3129 break;
3130 }
Casey Schauflere114e472008-02-04 22:29:50 -08003131 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003132 rcu_read_unlock();
3133
3134 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003135 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003136
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003137 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003138 return &smack_known_web;
3139 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003140 }
3141 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3142 /*
3143 * Looks like a fallback, which gives us a secid.
3144 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003145 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003146 /*
3147 * This has got to be a bug because it is
3148 * impossible to specify a fallback without
3149 * specifying the label, which will ensure
3150 * it has a secid, and the only way to get a
3151 * secid is from a fallback.
3152 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003153 BUG_ON(skp == NULL);
3154 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003155 }
3156 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003157 * Without guidance regarding the smack value
3158 * for the packet fall back on the network
3159 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003160 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003161 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003162}
3163
Casey Schaufler6ea06242013-08-05 13:21:22 -07003164static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003165{
Casey Schauflerc6739442013-05-22 18:42:56 -07003166 u8 nexthdr;
3167 int offset;
3168 int proto = -EINVAL;
3169 struct ipv6hdr _ipv6h;
3170 struct ipv6hdr *ip6;
3171 __be16 frag_off;
3172 struct tcphdr _tcph, *th;
3173 struct udphdr _udph, *uh;
3174 struct dccp_hdr _dccph, *dh;
3175
3176 sip->sin6_port = 0;
3177
3178 offset = skb_network_offset(skb);
3179 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3180 if (ip6 == NULL)
3181 return -EINVAL;
3182 sip->sin6_addr = ip6->saddr;
3183
3184 nexthdr = ip6->nexthdr;
3185 offset += sizeof(_ipv6h);
3186 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3187 if (offset < 0)
3188 return -EINVAL;
3189
3190 proto = nexthdr;
3191 switch (proto) {
3192 case IPPROTO_TCP:
3193 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3194 if (th != NULL)
3195 sip->sin6_port = th->source;
3196 break;
3197 case IPPROTO_UDP:
3198 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3199 if (uh != NULL)
3200 sip->sin6_port = uh->source;
3201 break;
3202 case IPPROTO_DCCP:
3203 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3204 if (dh != NULL)
3205 sip->sin6_port = dh->dccph_sport;
3206 break;
3207 }
3208 return proto;
3209}
3210
Casey Schauflere114e472008-02-04 22:29:50 -08003211/**
3212 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3213 * @sk: socket
3214 * @skb: packet
3215 *
3216 * Returns 0 if the packet should be delivered, an error code otherwise
3217 */
3218static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3219{
3220 struct netlbl_lsm_secattr secattr;
3221 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003222 struct smack_known *skp;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003223 struct sockaddr_in6 sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003224 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003225 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003226#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003227 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003228#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003229 switch (sk->sk_family) {
3230 case PF_INET:
3231 /*
3232 * Translate what netlabel gave us.
3233 */
3234 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003235
Casey Schauflerc6739442013-05-22 18:42:56 -07003236 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3237 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003238 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003239 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003240 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003241
Casey Schauflerc6739442013-05-22 18:42:56 -07003242 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003243
Etienne Bassetecfcc532009-04-08 20:40:06 +02003244#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003245 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3246 ad.a.u.net->family = sk->sk_family;
3247 ad.a.u.net->netif = skb->skb_iif;
3248 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003249#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003250 /*
3251 * Receiving a packet requires that the other end
3252 * be able to write here. Read access is not required.
3253 * This is the simplist possible security model
3254 * for networking.
3255 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003256 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07003257 if (rc != 0)
3258 netlbl_skbuff_err(skb, rc, 0);
3259 break;
3260 case PF_INET6:
3261 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3262 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3263 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3264 else
3265 rc = 0;
3266 break;
3267 }
Paul Moorea8134292008-10-10 10:16:31 -04003268 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003269}
3270
3271/**
3272 * smack_socket_getpeersec_stream - pull in packet label
3273 * @sock: the socket
3274 * @optval: user's destination
3275 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003276 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003277 *
3278 * returns zero on success, an error code otherwise
3279 */
3280static int smack_socket_getpeersec_stream(struct socket *sock,
3281 char __user *optval,
3282 int __user *optlen, unsigned len)
3283{
3284 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003285 char *rcp = "";
3286 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003287 int rc = 0;
3288
3289 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003290 if (ssp->smk_packet != NULL) {
3291 rcp = ssp->smk_packet;
3292 slen = strlen(rcp) + 1;
3293 }
Casey Schauflere114e472008-02-04 22:29:50 -08003294
3295 if (slen > len)
3296 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003297 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003298 rc = -EFAULT;
3299
3300 if (put_user(slen, optlen) != 0)
3301 rc = -EFAULT;
3302
3303 return rc;
3304}
3305
3306
3307/**
3308 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003309 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003310 * @skb: packet data
3311 * @secid: pointer to where to put the secid of the packet
3312 *
3313 * Sets the netlabel socket state on sk from parent
3314 */
3315static int smack_socket_getpeersec_dgram(struct socket *sock,
3316 struct sk_buff *skb, u32 *secid)
3317
3318{
3319 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003320 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003321 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003322 int family = PF_UNSPEC;
3323 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003324 int rc;
3325
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003326 if (skb != NULL) {
3327 if (skb->protocol == htons(ETH_P_IP))
3328 family = PF_INET;
3329 else if (skb->protocol == htons(ETH_P_IPV6))
3330 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003331 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003332 if (family == PF_UNSPEC && sock != NULL)
3333 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003334
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003335 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003336 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003337 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003338 } else if (family == PF_INET || family == PF_INET6) {
3339 /*
3340 * Translate what netlabel gave us.
3341 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003342 if (sock != NULL && sock->sk != NULL)
3343 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003344 netlbl_secattr_init(&secattr);
3345 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3346 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003347 skp = smack_from_secattr(&secattr, ssp);
3348 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003349 }
3350 netlbl_secattr_destroy(&secattr);
3351 }
3352 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003353 if (s == 0)
3354 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003355 return 0;
3356}
3357
3358/**
Paul Moore07feee82009-03-27 17:10:54 -04003359 * smack_sock_graft - Initialize a newly created socket with an existing sock
3360 * @sk: child sock
3361 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003362 *
Paul Moore07feee82009-03-27 17:10:54 -04003363 * Set the smk_{in,out} state of an existing sock based on the process that
3364 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003365 */
3366static void smack_sock_graft(struct sock *sk, struct socket *parent)
3367{
3368 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003369 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003370
Paul Moore07feee82009-03-27 17:10:54 -04003371 if (sk == NULL ||
3372 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003373 return;
3374
3375 ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003376 ssp->smk_in = skp->smk_known;
3377 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003378 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003379}
3380
3381/**
3382 * smack_inet_conn_request - Smack access check on connect
3383 * @sk: socket involved
3384 * @skb: packet
3385 * @req: unused
3386 *
3387 * Returns 0 if a task with the packet label could write to
3388 * the socket, otherwise an error code
3389 */
3390static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3391 struct request_sock *req)
3392{
Paul Moore07feee82009-03-27 17:10:54 -04003393 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003394 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003395 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003396 struct netlbl_lsm_secattr secattr;
3397 struct sockaddr_in addr;
3398 struct iphdr *hdr;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003399 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003400 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003401 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003402#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003403 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003404#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003405
Casey Schauflerc6739442013-05-22 18:42:56 -07003406 if (family == PF_INET6) {
3407 /*
3408 * Handle mapped IPv4 packets arriving
3409 * via IPv6 sockets. Don't set up netlabel
3410 * processing on IPv6.
3411 */
3412 if (skb->protocol == htons(ETH_P_IP))
3413 family = PF_INET;
3414 else
3415 return 0;
3416 }
Casey Schauflere114e472008-02-04 22:29:50 -08003417
Paul Moore07feee82009-03-27 17:10:54 -04003418 netlbl_secattr_init(&secattr);
3419 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003420 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003421 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003422 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003423 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003424 netlbl_secattr_destroy(&secattr);
3425
Etienne Bassetecfcc532009-04-08 20:40:06 +02003426#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003427 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3428 ad.a.u.net->family = family;
3429 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003430 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3431#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003432 /*
Paul Moore07feee82009-03-27 17:10:54 -04003433 * Receiving a packet requires that the other end be able to write
3434 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003435 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003436 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003437 if (rc != 0)
3438 return rc;
3439
3440 /*
3441 * Save the peer's label in the request_sock so we can later setup
3442 * smk_packet in the child socket so that SO_PEERCRED can report it.
3443 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003444 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003445
3446 /*
3447 * We need to decide if we want to label the incoming connection here
3448 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003449 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003450 */
3451 hdr = ip_hdr(skb);
3452 addr.sin_addr.s_addr = hdr->saddr;
3453 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003454 hsp = smack_host_label(&addr);
3455 rcu_read_unlock();
3456
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003457 if (hsp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003458 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003459 else
Paul Moore07feee82009-03-27 17:10:54 -04003460 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003461
3462 return rc;
3463}
3464
Paul Moore07feee82009-03-27 17:10:54 -04003465/**
3466 * smack_inet_csk_clone - Copy the connection information to the new socket
3467 * @sk: the new socket
3468 * @req: the connection's request_sock
3469 *
3470 * Transfer the connection's peer label to the newly created socket.
3471 */
3472static void smack_inet_csk_clone(struct sock *sk,
3473 const struct request_sock *req)
3474{
3475 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003476 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003477
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003478 if (req->peer_secid != 0) {
3479 skp = smack_from_secid(req->peer_secid);
3480 ssp->smk_packet = skp->smk_known;
3481 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003482 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003483}
3484
Casey Schauflere114e472008-02-04 22:29:50 -08003485/*
3486 * Key management security hooks
3487 *
3488 * Casey has not tested key support very heavily.
3489 * The permission check is most likely too restrictive.
3490 * If you care about keys please have a look.
3491 */
3492#ifdef CONFIG_KEYS
3493
3494/**
3495 * smack_key_alloc - Set the key security blob
3496 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003497 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003498 * @flags: unused
3499 *
3500 * No allocation required
3501 *
3502 * Returns 0
3503 */
David Howellsd84f4f92008-11-14 10:39:23 +11003504static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003505 unsigned long flags)
3506{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003507 struct smack_known *skp = smk_of_task(cred->security);
3508
3509 key->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003510 return 0;
3511}
3512
3513/**
3514 * smack_key_free - Clear the key security blob
3515 * @key: the object
3516 *
3517 * Clear the blob pointer
3518 */
3519static void smack_key_free(struct key *key)
3520{
3521 key->security = NULL;
3522}
3523
3524/*
3525 * smack_key_permission - Smack access on a key
3526 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003527 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003528 * @perm: unused
3529 *
3530 * Return 0 if the task has read and write to the object,
3531 * an error code otherwise
3532 */
3533static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003534 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003535{
3536 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003537 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003538 struct smack_known *tkp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003539
3540 keyp = key_ref_to_ptr(key_ref);
3541 if (keyp == NULL)
3542 return -EINVAL;
3543 /*
3544 * If the key hasn't been initialized give it access so that
3545 * it may do so.
3546 */
3547 if (keyp->security == NULL)
3548 return 0;
3549 /*
3550 * This should not occur
3551 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003552 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003553 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003554#ifdef CONFIG_AUDIT
3555 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3556 ad.a.u.key_struct.key = keyp->serial;
3557 ad.a.u.key_struct.key_desc = keyp->description;
3558#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003559 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003560}
3561#endif /* CONFIG_KEYS */
3562
3563/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003564 * Smack Audit hooks
3565 *
3566 * Audit requires a unique representation of each Smack specific
3567 * rule. This unique representation is used to distinguish the
3568 * object to be audited from remaining kernel objects and also
3569 * works as a glue between the audit hooks.
3570 *
3571 * Since repository entries are added but never deleted, we'll use
3572 * the smack_known label address related to the given audit rule as
3573 * the needed unique representation. This also better fits the smack
3574 * model where nearly everything is a label.
3575 */
3576#ifdef CONFIG_AUDIT
3577
3578/**
3579 * smack_audit_rule_init - Initialize a smack audit rule
3580 * @field: audit rule fields given from user-space (audit.h)
3581 * @op: required testing operator (=, !=, >, <, ...)
3582 * @rulestr: smack label to be audited
3583 * @vrule: pointer to save our own audit rule representation
3584 *
3585 * Prepare to audit cases where (@field @op @rulestr) is true.
3586 * The label to be audited is created if necessay.
3587 */
3588static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3589{
3590 char **rule = (char **)vrule;
3591 *rule = NULL;
3592
3593 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3594 return -EINVAL;
3595
Al Viro5af75d82008-12-16 05:59:26 -05003596 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003597 return -EINVAL;
3598
3599 *rule = smk_import(rulestr, 0);
3600
3601 return 0;
3602}
3603
3604/**
3605 * smack_audit_rule_known - Distinguish Smack audit rules
3606 * @krule: rule of interest, in Audit kernel representation format
3607 *
3608 * This is used to filter Smack rules from remaining Audit ones.
3609 * If it's proved that this rule belongs to us, the
3610 * audit_rule_match hook will be called to do the final judgement.
3611 */
3612static int smack_audit_rule_known(struct audit_krule *krule)
3613{
3614 struct audit_field *f;
3615 int i;
3616
3617 for (i = 0; i < krule->field_count; i++) {
3618 f = &krule->fields[i];
3619
3620 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3621 return 1;
3622 }
3623
3624 return 0;
3625}
3626
3627/**
3628 * smack_audit_rule_match - Audit given object ?
3629 * @secid: security id for identifying the object to test
3630 * @field: audit rule flags given from user-space
3631 * @op: required testing operator
3632 * @vrule: smack internal rule presentation
3633 * @actx: audit context associated with the check
3634 *
3635 * The core Audit hook. It's used to take the decision of
3636 * whether to audit or not to audit a given object.
3637 */
3638static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3639 struct audit_context *actx)
3640{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003641 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003642 char *rule = vrule;
3643
3644 if (!rule) {
Tetsuo Handaceffec552012-03-29 16:19:05 +09003645 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003646 "Smack: missing rule\n");
3647 return -ENOENT;
3648 }
3649
3650 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3651 return 0;
3652
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003653 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003654
3655 /*
3656 * No need to do string comparisons. If a match occurs,
3657 * both pointers will point to the same smack_known
3658 * label.
3659 */
Al Viro5af75d82008-12-16 05:59:26 -05003660 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003661 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003662 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003663 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003664
3665 return 0;
3666}
3667
3668/**
3669 * smack_audit_rule_free - free smack rule representation
3670 * @vrule: rule to be freed.
3671 *
3672 * No memory was allocated.
3673 */
3674static void smack_audit_rule_free(void *vrule)
3675{
3676 /* No-op */
3677}
3678
3679#endif /* CONFIG_AUDIT */
3680
Randy Dunlap251a2a92009-02-18 11:42:33 -08003681/**
David Quigley746df9b2013-05-22 12:50:35 -04003682 * smack_ismaclabel - check if xattr @name references a smack MAC label
3683 * @name: Full xattr name to check.
3684 */
3685static int smack_ismaclabel(const char *name)
3686{
3687 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3688}
3689
3690
3691/**
Casey Schauflere114e472008-02-04 22:29:50 -08003692 * smack_secid_to_secctx - return the smack label for a secid
3693 * @secid: incoming integer
3694 * @secdata: destination
3695 * @seclen: how long it is
3696 *
3697 * Exists for networking code.
3698 */
3699static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3700{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003701 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003702
Eric Parisd5630b92010-10-13 16:24:48 -04003703 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003704 *secdata = skp->smk_known;
3705 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08003706 return 0;
3707}
3708
Randy Dunlap251a2a92009-02-18 11:42:33 -08003709/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003710 * smack_secctx_to_secid - return the secid for a smack label
3711 * @secdata: smack label
3712 * @seclen: how long result is
3713 * @secid: outgoing integer
3714 *
3715 * Exists for audit and networking code.
3716 */
David Howellse52c17642008-04-29 20:52:51 +01003717static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003718{
3719 *secid = smack_to_secid(secdata);
3720 return 0;
3721}
3722
Randy Dunlap251a2a92009-02-18 11:42:33 -08003723/**
Casey Schauflere114e472008-02-04 22:29:50 -08003724 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003725 * @secdata: unused
3726 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003727 *
3728 * Exists to make sure nothing gets done, and properly
3729 */
3730static void smack_release_secctx(char *secdata, u32 seclen)
3731{
3732}
3733
David P. Quigley1ee65e32009-09-03 14:25:57 -04003734static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3735{
3736 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3737}
3738
3739static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3740{
3741 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3742}
3743
3744static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3745{
3746 int len = 0;
3747 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3748
3749 if (len < 0)
3750 return len;
3751 *ctxlen = len;
3752 return 0;
3753}
3754
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003755struct security_operations smack_ops = {
3756 .name = "smack",
3757
Ingo Molnar9e488582009-05-07 19:26:19 +10003758 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003759 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003760 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003761
3762 .sb_alloc_security = smack_sb_alloc_security,
3763 .sb_free_security = smack_sb_free_security,
3764 .sb_copy_data = smack_sb_copy_data,
3765 .sb_kern_mount = smack_sb_kern_mount,
3766 .sb_statfs = smack_sb_statfs,
3767 .sb_mount = smack_sb_mount,
3768 .sb_umount = smack_sb_umount,
3769
Casey Schaufler676dac42010-12-02 06:43:39 -08003770 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003771 .bprm_committing_creds = smack_bprm_committing_creds,
3772 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003773
Casey Schauflere114e472008-02-04 22:29:50 -08003774 .inode_alloc_security = smack_inode_alloc_security,
3775 .inode_free_security = smack_inode_free_security,
3776 .inode_init_security = smack_inode_init_security,
3777 .inode_link = smack_inode_link,
3778 .inode_unlink = smack_inode_unlink,
3779 .inode_rmdir = smack_inode_rmdir,
3780 .inode_rename = smack_inode_rename,
3781 .inode_permission = smack_inode_permission,
3782 .inode_setattr = smack_inode_setattr,
3783 .inode_getattr = smack_inode_getattr,
3784 .inode_setxattr = smack_inode_setxattr,
3785 .inode_post_setxattr = smack_inode_post_setxattr,
3786 .inode_getxattr = smack_inode_getxattr,
3787 .inode_removexattr = smack_inode_removexattr,
3788 .inode_getsecurity = smack_inode_getsecurity,
3789 .inode_setsecurity = smack_inode_setsecurity,
3790 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003791 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003792
3793 .file_permission = smack_file_permission,
3794 .file_alloc_security = smack_file_alloc_security,
3795 .file_free_security = smack_file_free_security,
3796 .file_ioctl = smack_file_ioctl,
3797 .file_lock = smack_file_lock,
3798 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003799 .mmap_file = smack_mmap_file,
3800 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003801 .file_set_fowner = smack_file_set_fowner,
3802 .file_send_sigiotask = smack_file_send_sigiotask,
3803 .file_receive = smack_file_receive,
3804
Eric Paris83d49852012-04-04 13:45:40 -04003805 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003806
David Howellsee18d642009-09-02 09:14:21 +01003807 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003808 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003809 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003810 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003811 .kernel_act_as = smack_kernel_act_as,
3812 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003813 .task_setpgid = smack_task_setpgid,
3814 .task_getpgid = smack_task_getpgid,
3815 .task_getsid = smack_task_getsid,
3816 .task_getsecid = smack_task_getsecid,
3817 .task_setnice = smack_task_setnice,
3818 .task_setioprio = smack_task_setioprio,
3819 .task_getioprio = smack_task_getioprio,
3820 .task_setscheduler = smack_task_setscheduler,
3821 .task_getscheduler = smack_task_getscheduler,
3822 .task_movememory = smack_task_movememory,
3823 .task_kill = smack_task_kill,
3824 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003825 .task_to_inode = smack_task_to_inode,
3826
3827 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003828 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003829
3830 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3831 .msg_msg_free_security = smack_msg_msg_free_security,
3832
3833 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3834 .msg_queue_free_security = smack_msg_queue_free_security,
3835 .msg_queue_associate = smack_msg_queue_associate,
3836 .msg_queue_msgctl = smack_msg_queue_msgctl,
3837 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3838 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3839
3840 .shm_alloc_security = smack_shm_alloc_security,
3841 .shm_free_security = smack_shm_free_security,
3842 .shm_associate = smack_shm_associate,
3843 .shm_shmctl = smack_shm_shmctl,
3844 .shm_shmat = smack_shm_shmat,
3845
3846 .sem_alloc_security = smack_sem_alloc_security,
3847 .sem_free_security = smack_sem_free_security,
3848 .sem_associate = smack_sem_associate,
3849 .sem_semctl = smack_sem_semctl,
3850 .sem_semop = smack_sem_semop,
3851
Casey Schauflere114e472008-02-04 22:29:50 -08003852 .d_instantiate = smack_d_instantiate,
3853
3854 .getprocattr = smack_getprocattr,
3855 .setprocattr = smack_setprocattr,
3856
3857 .unix_stream_connect = smack_unix_stream_connect,
3858 .unix_may_send = smack_unix_may_send,
3859
3860 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07003861 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003862 .socket_connect = smack_socket_connect,
3863 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003864 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3865 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3866 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3867 .sk_alloc_security = smack_sk_alloc_security,
3868 .sk_free_security = smack_sk_free_security,
3869 .sock_graft = smack_sock_graft,
3870 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003871 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003872
Casey Schauflere114e472008-02-04 22:29:50 -08003873 /* key management security hooks */
3874#ifdef CONFIG_KEYS
3875 .key_alloc = smack_key_alloc,
3876 .key_free = smack_key_free,
3877 .key_permission = smack_key_permission,
3878#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003879
3880 /* Audit hooks */
3881#ifdef CONFIG_AUDIT
3882 .audit_rule_init = smack_audit_rule_init,
3883 .audit_rule_known = smack_audit_rule_known,
3884 .audit_rule_match = smack_audit_rule_match,
3885 .audit_rule_free = smack_audit_rule_free,
3886#endif /* CONFIG_AUDIT */
3887
David Quigley746df9b2013-05-22 12:50:35 -04003888 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08003889 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003890 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003891 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003892 .inode_notifysecctx = smack_inode_notifysecctx,
3893 .inode_setsecctx = smack_inode_setsecctx,
3894 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003895};
3896
Etienne Basset7198e2e2009-03-24 20:53:24 +01003897
Casey Schaufler86812bb2012-04-17 18:55:46 -07003898static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003899{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003900 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003901 * Initialize rule list locks
3902 */
3903 mutex_init(&smack_known_huh.smk_rules_lock);
3904 mutex_init(&smack_known_hat.smk_rules_lock);
3905 mutex_init(&smack_known_floor.smk_rules_lock);
3906 mutex_init(&smack_known_star.smk_rules_lock);
3907 mutex_init(&smack_known_invalid.smk_rules_lock);
3908 mutex_init(&smack_known_web.smk_rules_lock);
3909 /*
3910 * Initialize rule lists
3911 */
3912 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3913 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3914 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3915 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3916 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3917 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3918 /*
3919 * Create the known labels list
3920 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02003921 smk_insert_entry(&smack_known_huh);
3922 smk_insert_entry(&smack_known_hat);
3923 smk_insert_entry(&smack_known_star);
3924 smk_insert_entry(&smack_known_floor);
3925 smk_insert_entry(&smack_known_invalid);
3926 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01003927}
3928
Casey Schauflere114e472008-02-04 22:29:50 -08003929/**
3930 * smack_init - initialize the smack system
3931 *
3932 * Returns 0
3933 */
3934static __init int smack_init(void)
3935{
David Howellsd84f4f92008-11-14 10:39:23 +11003936 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003937 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003938
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003939 if (!security_module_enable(&smack_ops))
3940 return 0;
3941
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003942 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3943 GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003944 if (tsp == NULL)
3945 return -ENOMEM;
3946
Casey Schauflere114e472008-02-04 22:29:50 -08003947 printk(KERN_INFO "Smack: Initializing.\n");
3948
3949 /*
3950 * Set the security state for the initial task.
3951 */
David Howellsd84f4f92008-11-14 10:39:23 +11003952 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003953 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003954
Casey Schaufler86812bb2012-04-17 18:55:46 -07003955 /* initialize the smack_known_list */
3956 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003957
3958 /*
3959 * Register with LSM
3960 */
3961 if (register_security(&smack_ops))
3962 panic("smack: Unable to register with kernel.\n");
3963
3964 return 0;
3965}
3966
3967/*
3968 * Smack requires early initialization in order to label
3969 * all processes and objects when they are created.
3970 */
3971security_initcall(smack_init);