blob: b0be893ad44d52bd0f062a1747199330c4d6940d [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 Schauflerbcdca222008-02-23 15:24:04 -0800840 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800841
Casey Schauflerbcdca222008-02-23 15:24:04 -0800842 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
843 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800844 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800845 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
846 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700847 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800848 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200849 /*
850 * check label validity here so import wont fail on
851 * post_setxattr
852 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700853 if (size == 0 || size >= SMK_LONGLABEL ||
Etienne Bassetdefc4332009-04-16 23:58:42 +0200854 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400855 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200856 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700857 if (!smack_privileged(CAP_MAC_ADMIN))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200858 rc = -EPERM;
859 if (size != TRANS_TRUE_SIZE ||
860 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
861 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800862 } else
863 rc = cap_inode_setxattr(dentry, name, value, size, flags);
864
Eric Parisa2694342011-04-25 13:10:27 -0400865 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200866 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
867
Casey Schauflerbcdca222008-02-23 15:24:04 -0800868 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200869 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800870
871 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800872}
873
874/**
875 * smack_inode_post_setxattr - Apply the Smack update approved above
876 * @dentry: object
877 * @name: attribute name
878 * @value: attribute value
879 * @size: attribute size
880 * @flags: unused
881 *
882 * Set the pointer in the inode blob to the entry found
883 * in the master label list.
884 */
David Howells8f0cfa52008-04-29 00:59:41 -0700885static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
886 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800887{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700888 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200889 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800890
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700891 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
892 isp->smk_flags |= SMK_INODE_TRANSMUTE;
893 return;
894 }
895
896 skp = smk_import_entry(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800897 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700898 if (skp != NULL)
899 isp->smk_inode = skp->smk_known;
Casey Schaufler676dac42010-12-02 06:43:39 -0800900 else
901 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200902 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700903 if (skp != NULL)
904 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800905 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700906 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800907 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700908 if (skp != NULL)
909 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800910 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700911 isp->smk_mmap = &smack_known_invalid;
912 }
Casey Schauflere114e472008-02-04 22:29:50 -0800913
914 return;
915}
916
Casey Schauflerce8a4322011-09-29 18:21:01 -0700917/**
Casey Schauflere114e472008-02-04 22:29:50 -0800918 * smack_inode_getxattr - Smack check on getxattr
919 * @dentry: the object
920 * @name: unused
921 *
922 * Returns 0 if access is permitted, an error code otherwise
923 */
David Howells8f0cfa52008-04-29 00:59:41 -0700924static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800925{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200926 struct smk_audit_info ad;
927
Eric Parisa2694342011-04-25 13:10:27 -0400928 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200929 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
930
931 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800932}
933
Casey Schauflerce8a4322011-09-29 18:21:01 -0700934/**
Casey Schauflere114e472008-02-04 22:29:50 -0800935 * smack_inode_removexattr - Smack check on removexattr
936 * @dentry: the object
937 * @name: name of the attribute
938 *
939 * Removing the Smack attribute requires CAP_MAC_ADMIN
940 *
941 * Returns 0 if access is permitted, an error code otherwise
942 */
David Howells8f0cfa52008-04-29 00:59:41 -0700943static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800944{
Casey Schaufler676dac42010-12-02 06:43:39 -0800945 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200946 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800947 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800948
Casey Schauflerbcdca222008-02-23 15:24:04 -0800949 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
950 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800951 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200952 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800953 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
954 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700955 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800956 rc = -EPERM;
957 } else
958 rc = cap_inode_removexattr(dentry, name);
959
Eric Parisa2694342011-04-25 13:10:27 -0400960 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200961 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800962 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200963 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800964
Casey Schaufler676dac42010-12-02 06:43:39 -0800965 if (rc == 0) {
966 isp = dentry->d_inode->i_security;
967 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800968 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800969 }
970
Casey Schauflerbcdca222008-02-23 15:24:04 -0800971 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800972}
973
974/**
975 * smack_inode_getsecurity - get smack xattrs
976 * @inode: the object
977 * @name: attribute name
978 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800979 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800980 *
981 * Returns the size of the attribute or an error code
982 */
983static int smack_inode_getsecurity(const struct inode *inode,
984 const char *name, void **buffer,
985 bool alloc)
986{
987 struct socket_smack *ssp;
988 struct socket *sock;
989 struct super_block *sbp;
990 struct inode *ip = (struct inode *)inode;
991 char *isp;
992 int ilen;
993 int rc = 0;
994
995 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
996 isp = smk_of_inode(inode);
997 ilen = strlen(isp) + 1;
998 *buffer = isp;
999 return ilen;
1000 }
1001
1002 /*
1003 * The rest of the Smack xattrs are only on sockets.
1004 */
1005 sbp = ip->i_sb;
1006 if (sbp->s_magic != SOCKFS_MAGIC)
1007 return -EOPNOTSUPP;
1008
1009 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001010 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001011 return -EOPNOTSUPP;
1012
1013 ssp = sock->sk->sk_security;
1014
1015 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1016 isp = ssp->smk_in;
1017 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001018 isp = ssp->smk_out->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001019 else
1020 return -EOPNOTSUPP;
1021
1022 ilen = strlen(isp) + 1;
1023 if (rc == 0) {
1024 *buffer = isp;
1025 rc = ilen;
1026 }
1027
1028 return rc;
1029}
1030
1031
1032/**
1033 * smack_inode_listsecurity - list the Smack attributes
1034 * @inode: the object
1035 * @buffer: where they go
1036 * @buffer_size: size of buffer
1037 *
1038 * Returns 0 on success, -EINVAL otherwise
1039 */
1040static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1041 size_t buffer_size)
1042{
1043 int len = strlen(XATTR_NAME_SMACK);
1044
1045 if (buffer != NULL && len <= buffer_size) {
1046 memcpy(buffer, XATTR_NAME_SMACK, len);
1047 return len;
1048 }
1049 return -EINVAL;
1050}
1051
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001052/**
1053 * smack_inode_getsecid - Extract inode's security id
1054 * @inode: inode to extract the info from
1055 * @secid: where result will be saved
1056 */
1057static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1058{
1059 struct inode_smack *isp = inode->i_security;
1060
1061 *secid = smack_to_secid(isp->smk_inode);
1062}
1063
Casey Schauflere114e472008-02-04 22:29:50 -08001064/*
1065 * File Hooks
1066 */
1067
1068/**
1069 * smack_file_permission - Smack check on file operations
1070 * @file: unused
1071 * @mask: unused
1072 *
1073 * Returns 0
1074 *
1075 * Should access checks be done on each read or write?
1076 * UNICOS and SELinux say yes.
1077 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1078 *
1079 * I'll say no for now. Smack does not do the frequent
1080 * label changing that SELinux does.
1081 */
1082static int smack_file_permission(struct file *file, int mask)
1083{
1084 return 0;
1085}
1086
1087/**
1088 * smack_file_alloc_security - assign a file security blob
1089 * @file: the object
1090 *
1091 * The security blob for a file is a pointer to the master
1092 * label list, so no allocation is done.
1093 *
1094 * Returns 0
1095 */
1096static int smack_file_alloc_security(struct file *file)
1097{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001098 struct smack_known *skp = smk_of_current();
1099
1100 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001101 return 0;
1102}
1103
1104/**
1105 * smack_file_free_security - clear a file security blob
1106 * @file: the object
1107 *
1108 * The security blob for a file is a pointer to the master
1109 * label list, so no memory is freed.
1110 */
1111static void smack_file_free_security(struct file *file)
1112{
1113 file->f_security = NULL;
1114}
1115
1116/**
1117 * smack_file_ioctl - Smack check on ioctls
1118 * @file: the object
1119 * @cmd: what to do
1120 * @arg: unused
1121 *
1122 * Relies heavily on the correct use of the ioctl command conventions.
1123 *
1124 * Returns 0 if allowed, error code otherwise
1125 */
1126static int smack_file_ioctl(struct file *file, unsigned int cmd,
1127 unsigned long arg)
1128{
1129 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001130 struct smk_audit_info ad;
1131
Eric Parisf48b7392011-04-25 12:54:27 -04001132 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001133 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001134
1135 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001136 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001137
1138 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001139 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001140
1141 return rc;
1142}
1143
1144/**
1145 * smack_file_lock - Smack check on file locking
1146 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001147 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001148 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001149 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001150 */
1151static int smack_file_lock(struct file *file, unsigned int cmd)
1152{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001153 struct smk_audit_info ad;
1154
Eric Paris92f42502011-04-25 13:15:55 -04001155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1156 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001157 return smk_curacc(file->f_security, MAY_LOCK, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001158}
1159
1160/**
1161 * smack_file_fcntl - Smack check on fcntl
1162 * @file: the object
1163 * @cmd: what action to check
1164 * @arg: unused
1165 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001166 * Generally these operations are harmless.
1167 * File locking operations present an obvious mechanism
1168 * for passing information, so they require write access.
1169 *
Casey Schauflere114e472008-02-04 22:29:50 -08001170 * Returns 0 if current has access, error code otherwise
1171 */
1172static int smack_file_fcntl(struct file *file, unsigned int cmd,
1173 unsigned long arg)
1174{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001175 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001176 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001177
Etienne Bassetecfcc532009-04-08 20:40:06 +02001178
Casey Schauflere114e472008-02-04 22:29:50 -08001179 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001180 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001181 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001182 case F_SETLK:
1183 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001184 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1185 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1186 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1187 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001188 case F_SETOWN:
1189 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1191 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001192 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001193 break;
1194 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001195 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001196 }
1197
1198 return rc;
1199}
1200
1201/**
Al Viroe5467852012-05-30 13:30:51 -04001202 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001203 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1204 * if mapping anonymous memory.
1205 * @file contains the file structure for file to map (may be NULL).
1206 * @reqprot contains the protection requested by the application.
1207 * @prot contains the protection that will be applied by the kernel.
1208 * @flags contains the operational flags.
1209 * Return 0 if permission is granted.
1210 */
Al Viroe5467852012-05-30 13:30:51 -04001211static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001212 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001213 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001214{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001215 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001216 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001217 struct smack_rule *srp;
1218 struct task_smack *tsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001219 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001220 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001221 int may;
1222 int mmay;
1223 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001224 int rc;
1225
Al Viro496ad9a2013-01-23 17:07:38 -05001226 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001227 return 0;
1228
Al Viro496ad9a2013-01-23 17:07:38 -05001229 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001230 if (isp->smk_mmap == NULL)
1231 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001232 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001233
1234 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001235 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001236 rc = 0;
1237
1238 rcu_read_lock();
1239 /*
1240 * For each Smack rule associated with the subject
1241 * label verify that the SMACK64MMAP also has access
1242 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001243 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001244 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001245 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001246 /*
1247 * Matching labels always allows access.
1248 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001249 if (mkp->smk_known == osmack)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001250 continue;
1251 /*
1252 * If there is a matching local rule take
1253 * that into account as well.
1254 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001255 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001256 &tsp->smk_rules);
1257 if (may == -ENOENT)
1258 may = srp->smk_access;
1259 else
1260 may &= srp->smk_access;
1261 /*
1262 * If may is zero the SMACK64MMAP subject can't
1263 * possibly have less access.
1264 */
1265 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001266 continue;
1267
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001268 /*
1269 * Fetch the global list entry.
1270 * If there isn't one a SMACK64MMAP subject
1271 * can't have as much access as current.
1272 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001273 mmay = smk_access_entry(mkp->smk_known, osmack,
1274 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001275 if (mmay == -ENOENT) {
1276 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001277 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001278 }
1279 /*
1280 * If there is a local entry it modifies the
1281 * potential access, too.
1282 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001283 tmay = smk_access_entry(mkp->smk_known, osmack,
1284 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001285 if (tmay != -ENOENT)
1286 mmay &= tmay;
1287
1288 /*
1289 * If there is any access available to current that is
1290 * not available to a SMACK64MMAP subject
1291 * deny access.
1292 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001293 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001294 rc = -EACCES;
1295 break;
1296 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001297 }
1298
1299 rcu_read_unlock();
1300
1301 return rc;
1302}
1303
1304/**
Casey Schauflere114e472008-02-04 22:29:50 -08001305 * smack_file_set_fowner - set the file security blob value
1306 * @file: object in question
1307 *
1308 * Returns 0
1309 * Further research may be required on this one.
1310 */
1311static int smack_file_set_fowner(struct file *file)
1312{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001313 struct smack_known *skp = smk_of_current();
1314
1315 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001316 return 0;
1317}
1318
1319/**
1320 * smack_file_send_sigiotask - Smack on sigio
1321 * @tsk: The target task
1322 * @fown: the object the signal come from
1323 * @signum: unused
1324 *
1325 * Allow a privileged task to get signals even if it shouldn't
1326 *
1327 * Returns 0 if a subject with the object's smack could
1328 * write to the task, an error code otherwise.
1329 */
1330static int smack_file_send_sigiotask(struct task_struct *tsk,
1331 struct fown_struct *fown, int signum)
1332{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001333 struct smack_known *skp;
1334 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001335 struct file *file;
1336 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001337 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001338
1339 /*
1340 * struct fown_struct is never outside the context of a struct file
1341 */
1342 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001343
Etienne Bassetecfcc532009-04-08 20:40:06 +02001344 /* we don't log here as rc can be overriden */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001345 skp = smk_find_entry(file->f_security);
1346 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001347 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001348 rc = 0;
1349
1350 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1351 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001352 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001353 return rc;
1354}
1355
1356/**
1357 * smack_file_receive - Smack file receive check
1358 * @file: the object
1359 *
1360 * Returns 0 if current has access, error code otherwise
1361 */
1362static int smack_file_receive(struct file *file)
1363{
1364 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001365 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001366
Etienne Bassetecfcc532009-04-08 20:40:06 +02001367 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1368 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001369 /*
1370 * This code relies on bitmasks.
1371 */
1372 if (file->f_mode & FMODE_READ)
1373 may = MAY_READ;
1374 if (file->f_mode & FMODE_WRITE)
1375 may |= MAY_WRITE;
1376
Etienne Bassetecfcc532009-04-08 20:40:06 +02001377 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001378}
1379
Casey Schaufler531f1d42011-09-19 12:41:42 -07001380/**
Eric Paris83d49852012-04-04 13:45:40 -04001381 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001382 * @file: the object
1383 * @cred: unused
1384 *
1385 * Set the security blob in the file structure.
1386 *
1387 * Returns 0
1388 */
Eric Paris83d49852012-04-04 13:45:40 -04001389static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001390{
Al Viro496ad9a2013-01-23 17:07:38 -05001391 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001392
1393 file->f_security = isp->smk_inode;
1394
1395 return 0;
1396}
1397
Casey Schauflere114e472008-02-04 22:29:50 -08001398/*
1399 * Task hooks
1400 */
1401
1402/**
David Howellsee18d642009-09-02 09:14:21 +01001403 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1404 * @new: the new credentials
1405 * @gfp: the atomicity of any memory allocations
1406 *
1407 * Prepare a blank set of credentials for modification. This must allocate all
1408 * the memory the LSM module might require such that cred_transfer() can
1409 * complete without error.
1410 */
1411static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1412{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001413 struct task_smack *tsp;
1414
1415 tsp = new_task_smack(NULL, NULL, gfp);
1416 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001417 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001418
1419 cred->security = tsp;
1420
David Howellsee18d642009-09-02 09:14:21 +01001421 return 0;
1422}
1423
1424
1425/**
David Howellsf1752ee2008-11-14 10:39:17 +11001426 * smack_cred_free - "free" task-level security credentials
1427 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001428 *
Casey Schauflere114e472008-02-04 22:29:50 -08001429 */
David Howellsf1752ee2008-11-14 10:39:17 +11001430static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001431{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001432 struct task_smack *tsp = cred->security;
1433 struct smack_rule *rp;
1434 struct list_head *l;
1435 struct list_head *n;
1436
1437 if (tsp == NULL)
1438 return;
1439 cred->security = NULL;
1440
1441 list_for_each_safe(l, n, &tsp->smk_rules) {
1442 rp = list_entry(l, struct smack_rule, list);
1443 list_del(&rp->list);
1444 kfree(rp);
1445 }
1446 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001447}
1448
1449/**
David Howellsd84f4f92008-11-14 10:39:23 +11001450 * smack_cred_prepare - prepare new set of credentials for modification
1451 * @new: the new credentials
1452 * @old: the original credentials
1453 * @gfp: the atomicity of any memory allocations
1454 *
1455 * Prepare a new set of credentials for modification.
1456 */
1457static int smack_cred_prepare(struct cred *new, const struct cred *old,
1458 gfp_t gfp)
1459{
Casey Schaufler676dac42010-12-02 06:43:39 -08001460 struct task_smack *old_tsp = old->security;
1461 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001462 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001463
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001464 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001465 if (new_tsp == NULL)
1466 return -ENOMEM;
1467
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001468 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1469 if (rc != 0)
1470 return rc;
1471
Casey Schaufler676dac42010-12-02 06:43:39 -08001472 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001473 return 0;
1474}
1475
Randy Dunlap251a2a92009-02-18 11:42:33 -08001476/**
David Howellsee18d642009-09-02 09:14:21 +01001477 * smack_cred_transfer - Transfer the old credentials to the new credentials
1478 * @new: the new credentials
1479 * @old: the original credentials
1480 *
1481 * Fill in a set of blank credentials from another set of credentials.
1482 */
1483static void smack_cred_transfer(struct cred *new, const struct cred *old)
1484{
Casey Schaufler676dac42010-12-02 06:43:39 -08001485 struct task_smack *old_tsp = old->security;
1486 struct task_smack *new_tsp = new->security;
1487
1488 new_tsp->smk_task = old_tsp->smk_task;
1489 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001490 mutex_init(&new_tsp->smk_rules_lock);
1491 INIT_LIST_HEAD(&new_tsp->smk_rules);
1492
1493
1494 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001495}
1496
1497/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001498 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001499 * @new: points to the set of credentials to be modified.
1500 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001501 *
1502 * Set the security data for a kernel service.
1503 */
1504static int smack_kernel_act_as(struct cred *new, u32 secid)
1505{
Casey Schaufler676dac42010-12-02 06:43:39 -08001506 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001507 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001508
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001509 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001510 return -EINVAL;
1511
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001512 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001513 return 0;
1514}
1515
1516/**
1517 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001518 * @new: points to the set of credentials to be modified
1519 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001520 *
1521 * Set the file creation context in a set of credentials to the same
1522 * as the objective context of the specified inode
1523 */
1524static int smack_kernel_create_files_as(struct cred *new,
1525 struct inode *inode)
1526{
1527 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001528 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001529
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001530 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1531 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001532 return 0;
1533}
1534
1535/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001536 * smk_curacc_on_task - helper to log task related access
1537 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001538 * @access: the access requested
1539 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001540 *
1541 * Return 0 if access is permitted
1542 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001543static int smk_curacc_on_task(struct task_struct *p, int access,
1544 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001545{
1546 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001547 struct smack_known *skp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001548
Casey Schaufler531f1d42011-09-19 12:41:42 -07001549 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001550 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001551 return smk_curacc(skp->smk_known, access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001552}
1553
1554/**
Casey Schauflere114e472008-02-04 22:29:50 -08001555 * smack_task_setpgid - Smack check on setting pgid
1556 * @p: the task object
1557 * @pgid: unused
1558 *
1559 * Return 0 if write access is permitted
1560 */
1561static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1562{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001563 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001564}
1565
1566/**
1567 * smack_task_getpgid - Smack access check for getpgid
1568 * @p: the object task
1569 *
1570 * Returns 0 if current can read the object task, error code otherwise
1571 */
1572static int smack_task_getpgid(struct task_struct *p)
1573{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001574 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001575}
1576
1577/**
1578 * smack_task_getsid - Smack access check for getsid
1579 * @p: the object task
1580 *
1581 * Returns 0 if current can read the object task, error code otherwise
1582 */
1583static int smack_task_getsid(struct task_struct *p)
1584{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001585 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001586}
1587
1588/**
1589 * smack_task_getsecid - get the secid of the task
1590 * @p: the object task
1591 * @secid: where to put the result
1592 *
1593 * Sets the secid to contain a u32 version of the smack label.
1594 */
1595static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1596{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001597 struct smack_known *skp = smk_of_task(task_security(p));
1598
1599 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001600}
1601
1602/**
1603 * smack_task_setnice - Smack check on setting nice
1604 * @p: the task object
1605 * @nice: unused
1606 *
1607 * Return 0 if write access is permitted
1608 */
1609static int smack_task_setnice(struct task_struct *p, int nice)
1610{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001611 int rc;
1612
1613 rc = cap_task_setnice(p, nice);
1614 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001615 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001616 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001617}
1618
1619/**
1620 * smack_task_setioprio - Smack check on setting ioprio
1621 * @p: the task object
1622 * @ioprio: unused
1623 *
1624 * Return 0 if write access is permitted
1625 */
1626static int smack_task_setioprio(struct task_struct *p, int ioprio)
1627{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001628 int rc;
1629
1630 rc = cap_task_setioprio(p, ioprio);
1631 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001632 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001633 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001634}
1635
1636/**
1637 * smack_task_getioprio - Smack check on reading ioprio
1638 * @p: the task object
1639 *
1640 * Return 0 if read access is permitted
1641 */
1642static int smack_task_getioprio(struct task_struct *p)
1643{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001644 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001645}
1646
1647/**
1648 * smack_task_setscheduler - Smack check on setting scheduler
1649 * @p: the task object
1650 * @policy: unused
1651 * @lp: unused
1652 *
1653 * Return 0 if read access is permitted
1654 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001655static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001656{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001657 int rc;
1658
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001659 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001660 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001661 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001662 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001663}
1664
1665/**
1666 * smack_task_getscheduler - Smack check on reading scheduler
1667 * @p: the task object
1668 *
1669 * Return 0 if read access is permitted
1670 */
1671static int smack_task_getscheduler(struct task_struct *p)
1672{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001673 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001674}
1675
1676/**
1677 * smack_task_movememory - Smack check on moving memory
1678 * @p: the task object
1679 *
1680 * Return 0 if write access is permitted
1681 */
1682static int smack_task_movememory(struct task_struct *p)
1683{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001684 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001685}
1686
1687/**
1688 * smack_task_kill - Smack check on signal delivery
1689 * @p: the task object
1690 * @info: unused
1691 * @sig: unused
1692 * @secid: identifies the smack to use in lieu of current's
1693 *
1694 * Return 0 if write access is permitted
1695 *
1696 * The secid behavior is an artifact of an SELinux hack
1697 * in the USB code. Someday it may go away.
1698 */
1699static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1700 int sig, u32 secid)
1701{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001702 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001703 struct smack_known *skp;
1704 struct smack_known *tkp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001705
1706 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1707 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001708 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001709 * Sending a signal requires that the sender
1710 * can write the receiver.
1711 */
1712 if (secid == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001713 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001714 /*
1715 * If the secid isn't 0 we're dealing with some USB IO
1716 * specific behavior. This is not clean. For one thing
1717 * we can't take privilege into account.
1718 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001719 skp = smack_from_secid(secid);
1720 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001721}
1722
1723/**
1724 * smack_task_wait - Smack access check for waiting
1725 * @p: task to wait for
1726 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001727 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08001728 */
1729static int smack_task_wait(struct task_struct *p)
1730{
Casey Schauflere114e472008-02-04 22:29:50 -08001731 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001732 * Allow the operation to succeed.
1733 * Zombies are bad.
1734 * In userless environments (e.g. phones) programs
1735 * get marked with SMACK64EXEC and even if the parent
1736 * and child shouldn't be talking the parent still
1737 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08001738 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001739 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001740}
1741
1742/**
1743 * smack_task_to_inode - copy task smack into the inode blob
1744 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001745 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001746 *
1747 * Sets the smack pointer in the inode security blob
1748 */
1749static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1750{
1751 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001752 struct smack_known *skp = smk_of_task(task_security(p));
1753
1754 isp->smk_inode = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001755}
1756
1757/*
1758 * Socket hooks.
1759 */
1760
1761/**
1762 * smack_sk_alloc_security - Allocate a socket blob
1763 * @sk: the socket
1764 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001765 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001766 *
1767 * Assign Smack pointers to current
1768 *
1769 * Returns 0 on success, -ENOMEM is there's no memory
1770 */
1771static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1772{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001773 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001774 struct socket_smack *ssp;
1775
1776 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1777 if (ssp == NULL)
1778 return -ENOMEM;
1779
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001780 ssp->smk_in = skp->smk_known;
1781 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001782 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001783
1784 sk->sk_security = ssp;
1785
1786 return 0;
1787}
1788
1789/**
1790 * smack_sk_free_security - Free a socket blob
1791 * @sk: the socket
1792 *
1793 * Clears the blob pointer
1794 */
1795static void smack_sk_free_security(struct sock *sk)
1796{
1797 kfree(sk->sk_security);
1798}
1799
1800/**
Paul Moore07feee82009-03-27 17:10:54 -04001801* smack_host_label - check host based restrictions
1802* @sip: the object end
1803*
1804* looks for host based access restrictions
1805*
1806* This version will only be appropriate for really small sets of single label
1807* hosts. The caller is responsible for ensuring that the RCU read lock is
1808* taken before calling this function.
1809*
1810* Returns the label of the far end or NULL if it's not special.
1811*/
1812static char *smack_host_label(struct sockaddr_in *sip)
1813{
1814 struct smk_netlbladdr *snp;
1815 struct in_addr *siap = &sip->sin_addr;
1816
1817 if (siap->s_addr == 0)
1818 return NULL;
1819
1820 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1821 /*
1822 * we break after finding the first match because
1823 * the list is sorted from longest to shortest mask
1824 * so we have found the most specific match
1825 */
1826 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001827 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1828 /* we have found the special CIPSO option */
1829 if (snp->smk_label == smack_cipso_option)
1830 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001831 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001832 }
Paul Moore07feee82009-03-27 17:10:54 -04001833
1834 return NULL;
1835}
1836
1837/**
Casey Schauflere114e472008-02-04 22:29:50 -08001838 * smack_netlabel - Set the secattr on a socket
1839 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001840 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001841 *
1842 * Convert the outbound smack value (smk_out) to a
1843 * secattr and attach it to the socket.
1844 *
1845 * Returns 0 on success or an error code
1846 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001847static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001848{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001849 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001850 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001851 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001852
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001853 /*
1854 * Usually the netlabel code will handle changing the
1855 * packet labeling based on the label.
1856 * The case of a single label host is different, because
1857 * a single label host should never get a labeled packet
1858 * even though the label is usually associated with a packet
1859 * label.
1860 */
1861 local_bh_disable();
1862 bh_lock_sock_nested(sk);
1863
1864 if (ssp->smk_out == smack_net_ambient ||
1865 labeled == SMACK_UNLABELED_SOCKET)
1866 netlbl_sock_delattr(sk);
1867 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001868 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001869 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001870 }
1871
1872 bh_unlock_sock(sk);
1873 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001874
Casey Schauflere114e472008-02-04 22:29:50 -08001875 return rc;
1876}
1877
1878/**
Paul Moore07feee82009-03-27 17:10:54 -04001879 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1880 * @sk: the socket
1881 * @sap: the destination address
1882 *
1883 * Set the correct secattr for the given socket based on the destination
1884 * address and perform any outbound access checks needed.
1885 *
1886 * Returns 0 on success or an error code.
1887 *
1888 */
1889static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1890{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001891 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001892 int rc;
1893 int sk_lbl;
1894 char *hostsp;
1895 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001896 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001897
1898 rcu_read_lock();
1899 hostsp = smack_host_label(sap);
1900 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001901#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001902 struct lsm_network_audit net;
1903
Eric Paris48c62af2012-04-02 13:15:44 -04001904 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1905 ad.a.u.net->family = sap->sin_family;
1906 ad.a.u.net->dport = sap->sin_port;
1907 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001908#endif
Kees Cook923e9a12012-04-10 13:26:44 -07001909 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001910 skp = ssp->smk_out;
1911 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001912 } else {
1913 sk_lbl = SMACK_CIPSO_SOCKET;
1914 rc = 0;
1915 }
1916 rcu_read_unlock();
1917 if (rc != 0)
1918 return rc;
1919
1920 return smack_netlabel(sk, sk_lbl);
1921}
1922
1923/**
Casey Schauflerc6739442013-05-22 18:42:56 -07001924 * smk_ipv6_port_label - Smack port access table management
1925 * @sock: socket
1926 * @address: address
1927 *
1928 * Create or update the port list entry
1929 */
1930static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1931{
1932 struct sock *sk = sock->sk;
1933 struct sockaddr_in6 *addr6;
1934 struct socket_smack *ssp = sock->sk->sk_security;
1935 struct smk_port_label *spp;
1936 unsigned short port = 0;
1937
1938 if (address == NULL) {
1939 /*
1940 * This operation is changing the Smack information
1941 * on the bound socket. Take the changes to the port
1942 * as well.
1943 */
1944 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1945 if (sk != spp->smk_sock)
1946 continue;
1947 spp->smk_in = ssp->smk_in;
1948 spp->smk_out = ssp->smk_out;
1949 return;
1950 }
1951 /*
1952 * A NULL address is only used for updating existing
1953 * bound entries. If there isn't one, it's OK.
1954 */
1955 return;
1956 }
1957
1958 addr6 = (struct sockaddr_in6 *)address;
1959 port = ntohs(addr6->sin6_port);
1960 /*
1961 * This is a special case that is safely ignored.
1962 */
1963 if (port == 0)
1964 return;
1965
1966 /*
1967 * Look for an existing port list entry.
1968 * This is an indication that a port is getting reused.
1969 */
1970 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1971 if (spp->smk_port != port)
1972 continue;
1973 spp->smk_port = port;
1974 spp->smk_sock = sk;
1975 spp->smk_in = ssp->smk_in;
1976 spp->smk_out = ssp->smk_out;
1977 return;
1978 }
1979
1980 /*
1981 * A new port entry is required.
1982 */
1983 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1984 if (spp == NULL)
1985 return;
1986
1987 spp->smk_port = port;
1988 spp->smk_sock = sk;
1989 spp->smk_in = ssp->smk_in;
1990 spp->smk_out = ssp->smk_out;
1991
1992 list_add(&spp->list, &smk_ipv6_port_list);
1993 return;
1994}
1995
1996/**
1997 * smk_ipv6_port_check - check Smack port access
1998 * @sock: socket
1999 * @address: address
2000 *
2001 * Create or update the port list entry
2002 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002003static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002004 int act)
2005{
2006 __be16 *bep;
2007 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002008 struct smk_port_label *spp;
2009 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002010 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002011 unsigned short port = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002012 char *object;
2013 struct smk_audit_info ad;
2014#ifdef CONFIG_AUDIT
2015 struct lsm_network_audit net;
2016#endif
2017
2018 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002019 skp = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002020 object = ssp->smk_in;
2021 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002022 skp = ssp->smk_out;
2023 object = smack_net_ambient->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002024 }
2025
2026 /*
2027 * Get the IP address and port from the address.
2028 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002029 port = ntohs(address->sin6_port);
2030 bep = (__be16 *)(&address->sin6_addr);
2031 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002032
2033 /*
2034 * It's remote, so port lookup does no good.
2035 */
2036 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2037 goto auditout;
2038
2039 /*
2040 * It's local so the send check has to have passed.
2041 */
2042 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002043 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002044 goto auditout;
2045 }
2046
2047 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2048 if (spp->smk_port != port)
2049 continue;
2050 object = spp->smk_in;
2051 if (act == SMK_CONNECTING)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002052 ssp->smk_packet = spp->smk_out->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002053 break;
2054 }
2055
2056auditout:
2057
2058#ifdef CONFIG_AUDIT
2059 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2060 ad.a.u.net->family = sk->sk_family;
2061 ad.a.u.net->dport = port;
2062 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002063 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002064 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002065 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002066#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002067 return smk_access(skp, object, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07002068}
2069
2070/**
Casey Schauflere114e472008-02-04 22:29:50 -08002071 * smack_inode_setsecurity - set smack xattrs
2072 * @inode: the object
2073 * @name: attribute name
2074 * @value: attribute value
2075 * @size: size of the attribute
2076 * @flags: unused
2077 *
2078 * Sets the named attribute in the appropriate blob
2079 *
2080 * Returns 0 on success, or an error code
2081 */
2082static int smack_inode_setsecurity(struct inode *inode, const char *name,
2083 const void *value, size_t size, int flags)
2084{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002085 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002086 struct inode_smack *nsp = inode->i_security;
2087 struct socket_smack *ssp;
2088 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002089 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002090
Casey Schauflerf7112e62012-05-06 15:22:02 -07002091 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08002092 return -EACCES;
2093
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002094 skp = smk_import_entry(value, size);
2095 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002096 return -EINVAL;
2097
2098 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002099 nsp->smk_inode = skp->smk_known;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002100 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002101 return 0;
2102 }
2103 /*
2104 * The rest of the Smack xattrs are only on sockets.
2105 */
2106 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2107 return -EOPNOTSUPP;
2108
2109 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002110 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002111 return -EOPNOTSUPP;
2112
2113 ssp = sock->sk->sk_security;
2114
2115 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002116 ssp->smk_in = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002117 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002118 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002119 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002120 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2121 if (rc != 0)
2122 printk(KERN_WARNING
2123 "Smack: \"%s\" netlbl error %d.\n",
2124 __func__, -rc);
2125 }
Casey Schauflere114e472008-02-04 22:29:50 -08002126 } else
2127 return -EOPNOTSUPP;
2128
Casey Schauflerc6739442013-05-22 18:42:56 -07002129 if (sock->sk->sk_family == PF_INET6)
2130 smk_ipv6_port_label(sock, NULL);
2131
Casey Schauflere114e472008-02-04 22:29:50 -08002132 return 0;
2133}
2134
2135/**
2136 * smack_socket_post_create - finish socket setup
2137 * @sock: the socket
2138 * @family: protocol family
2139 * @type: unused
2140 * @protocol: unused
2141 * @kern: unused
2142 *
2143 * Sets the netlabel information on the socket
2144 *
2145 * Returns 0 on success, and error code otherwise
2146 */
2147static int smack_socket_post_create(struct socket *sock, int family,
2148 int type, int protocol, int kern)
2149{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002150 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002151 return 0;
2152 /*
2153 * Set the outbound netlbl.
2154 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002155 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2156}
2157
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002158/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002159 * smack_socket_bind - record port binding information.
2160 * @sock: the socket
2161 * @address: the port address
2162 * @addrlen: size of the address
2163 *
2164 * Records the label bound to a port.
2165 *
2166 * Returns 0
2167 */
2168static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2169 int addrlen)
2170{
2171 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2172 smk_ipv6_port_label(sock, address);
2173
2174 return 0;
2175}
2176
2177/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002178 * smack_socket_connect - connect access check
2179 * @sock: the socket
2180 * @sap: the other end
2181 * @addrlen: size of sap
2182 *
2183 * Verifies that a connection may be possible
2184 *
2185 * Returns 0 on success, and error code otherwise
2186 */
2187static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2188 int addrlen)
2189{
Casey Schauflerc6739442013-05-22 18:42:56 -07002190 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002191
Casey Schauflerc6739442013-05-22 18:42:56 -07002192 if (sock->sk == NULL)
2193 return 0;
2194
2195 switch (sock->sk->sk_family) {
2196 case PF_INET:
2197 if (addrlen < sizeof(struct sockaddr_in))
2198 return -EINVAL;
2199 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2200 break;
2201 case PF_INET6:
2202 if (addrlen < sizeof(struct sockaddr_in6))
2203 return -EINVAL;
Casey Schaufler6ea06242013-08-05 13:21:22 -07002204 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2205 SMK_CONNECTING);
Casey Schauflerc6739442013-05-22 18:42:56 -07002206 break;
2207 }
2208 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002209}
2210
2211/**
2212 * smack_flags_to_may - convert S_ to MAY_ values
2213 * @flags: the S_ value
2214 *
2215 * Returns the equivalent MAY_ value
2216 */
2217static int smack_flags_to_may(int flags)
2218{
2219 int may = 0;
2220
2221 if (flags & S_IRUGO)
2222 may |= MAY_READ;
2223 if (flags & S_IWUGO)
2224 may |= MAY_WRITE;
2225 if (flags & S_IXUGO)
2226 may |= MAY_EXEC;
2227
2228 return may;
2229}
2230
2231/**
2232 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2233 * @msg: the object
2234 *
2235 * Returns 0
2236 */
2237static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2238{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002239 struct smack_known *skp = smk_of_current();
2240
2241 msg->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002242 return 0;
2243}
2244
2245/**
2246 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2247 * @msg: the object
2248 *
2249 * Clears the blob pointer
2250 */
2251static void smack_msg_msg_free_security(struct msg_msg *msg)
2252{
2253 msg->security = NULL;
2254}
2255
2256/**
2257 * smack_of_shm - the smack pointer for the shm
2258 * @shp: the object
2259 *
2260 * Returns a pointer to the smack value
2261 */
2262static char *smack_of_shm(struct shmid_kernel *shp)
2263{
2264 return (char *)shp->shm_perm.security;
2265}
2266
2267/**
2268 * smack_shm_alloc_security - Set the security blob for shm
2269 * @shp: the object
2270 *
2271 * Returns 0
2272 */
2273static int smack_shm_alloc_security(struct shmid_kernel *shp)
2274{
2275 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002276 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002277
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002278 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002279 return 0;
2280}
2281
2282/**
2283 * smack_shm_free_security - Clear the security blob for shm
2284 * @shp: the object
2285 *
2286 * Clears the blob pointer
2287 */
2288static void smack_shm_free_security(struct shmid_kernel *shp)
2289{
2290 struct kern_ipc_perm *isp = &shp->shm_perm;
2291
2292 isp->security = NULL;
2293}
2294
2295/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002296 * smk_curacc_shm : check if current has access on shm
2297 * @shp : the object
2298 * @access : access requested
2299 *
2300 * Returns 0 if current has the requested access, error code otherwise
2301 */
2302static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2303{
2304 char *ssp = smack_of_shm(shp);
2305 struct smk_audit_info ad;
2306
2307#ifdef CONFIG_AUDIT
2308 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2309 ad.a.u.ipc_id = shp->shm_perm.id;
2310#endif
2311 return smk_curacc(ssp, access, &ad);
2312}
2313
2314/**
Casey Schauflere114e472008-02-04 22:29:50 -08002315 * smack_shm_associate - Smack access check for shm
2316 * @shp: the object
2317 * @shmflg: access requested
2318 *
2319 * Returns 0 if current has the requested access, error code otherwise
2320 */
2321static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2322{
Casey Schauflere114e472008-02-04 22:29:50 -08002323 int may;
2324
2325 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002326 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002327}
2328
2329/**
2330 * smack_shm_shmctl - Smack access check for shm
2331 * @shp: the object
2332 * @cmd: what it wants to do
2333 *
2334 * Returns 0 if current has the requested access, error code otherwise
2335 */
2336static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2337{
Casey Schauflere114e472008-02-04 22:29:50 -08002338 int may;
2339
2340 switch (cmd) {
2341 case IPC_STAT:
2342 case SHM_STAT:
2343 may = MAY_READ;
2344 break;
2345 case IPC_SET:
2346 case SHM_LOCK:
2347 case SHM_UNLOCK:
2348 case IPC_RMID:
2349 may = MAY_READWRITE;
2350 break;
2351 case IPC_INFO:
2352 case SHM_INFO:
2353 /*
2354 * System level information.
2355 */
2356 return 0;
2357 default:
2358 return -EINVAL;
2359 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002360 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002361}
2362
2363/**
2364 * smack_shm_shmat - Smack access for shmat
2365 * @shp: the object
2366 * @shmaddr: unused
2367 * @shmflg: access requested
2368 *
2369 * Returns 0 if current has the requested access, error code otherwise
2370 */
2371static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2372 int shmflg)
2373{
Casey Schauflere114e472008-02-04 22:29:50 -08002374 int may;
2375
2376 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002377 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002378}
2379
2380/**
2381 * smack_of_sem - the smack pointer for the sem
2382 * @sma: the object
2383 *
2384 * Returns a pointer to the smack value
2385 */
2386static char *smack_of_sem(struct sem_array *sma)
2387{
2388 return (char *)sma->sem_perm.security;
2389}
2390
2391/**
2392 * smack_sem_alloc_security - Set the security blob for sem
2393 * @sma: the object
2394 *
2395 * Returns 0
2396 */
2397static int smack_sem_alloc_security(struct sem_array *sma)
2398{
2399 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002400 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002401
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002402 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002403 return 0;
2404}
2405
2406/**
2407 * smack_sem_free_security - Clear the security blob for sem
2408 * @sma: the object
2409 *
2410 * Clears the blob pointer
2411 */
2412static void smack_sem_free_security(struct sem_array *sma)
2413{
2414 struct kern_ipc_perm *isp = &sma->sem_perm;
2415
2416 isp->security = NULL;
2417}
2418
2419/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002420 * smk_curacc_sem : check if current has access on sem
2421 * @sma : the object
2422 * @access : access requested
2423 *
2424 * Returns 0 if current has the requested access, error code otherwise
2425 */
2426static int smk_curacc_sem(struct sem_array *sma, int access)
2427{
2428 char *ssp = smack_of_sem(sma);
2429 struct smk_audit_info ad;
2430
2431#ifdef CONFIG_AUDIT
2432 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2433 ad.a.u.ipc_id = sma->sem_perm.id;
2434#endif
2435 return smk_curacc(ssp, access, &ad);
2436}
2437
2438/**
Casey Schauflere114e472008-02-04 22:29:50 -08002439 * smack_sem_associate - Smack access check for sem
2440 * @sma: the object
2441 * @semflg: access requested
2442 *
2443 * Returns 0 if current has the requested access, error code otherwise
2444 */
2445static int smack_sem_associate(struct sem_array *sma, int semflg)
2446{
Casey Schauflere114e472008-02-04 22:29:50 -08002447 int may;
2448
2449 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002450 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002451}
2452
2453/**
2454 * smack_sem_shmctl - Smack access check for sem
2455 * @sma: the object
2456 * @cmd: what it wants to do
2457 *
2458 * Returns 0 if current has the requested access, error code otherwise
2459 */
2460static int smack_sem_semctl(struct sem_array *sma, int cmd)
2461{
Casey Schauflere114e472008-02-04 22:29:50 -08002462 int may;
2463
2464 switch (cmd) {
2465 case GETPID:
2466 case GETNCNT:
2467 case GETZCNT:
2468 case GETVAL:
2469 case GETALL:
2470 case IPC_STAT:
2471 case SEM_STAT:
2472 may = MAY_READ;
2473 break;
2474 case SETVAL:
2475 case SETALL:
2476 case IPC_RMID:
2477 case IPC_SET:
2478 may = MAY_READWRITE;
2479 break;
2480 case IPC_INFO:
2481 case SEM_INFO:
2482 /*
2483 * System level information
2484 */
2485 return 0;
2486 default:
2487 return -EINVAL;
2488 }
2489
Etienne Bassetecfcc532009-04-08 20:40:06 +02002490 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002491}
2492
2493/**
2494 * smack_sem_semop - Smack checks of semaphore operations
2495 * @sma: the object
2496 * @sops: unused
2497 * @nsops: unused
2498 * @alter: unused
2499 *
2500 * Treated as read and write in all cases.
2501 *
2502 * Returns 0 if access is allowed, error code otherwise
2503 */
2504static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2505 unsigned nsops, int alter)
2506{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002507 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002508}
2509
2510/**
2511 * smack_msg_alloc_security - Set the security blob for msg
2512 * @msq: the object
2513 *
2514 * Returns 0
2515 */
2516static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2517{
2518 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002519 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002520
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002521 kisp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002522 return 0;
2523}
2524
2525/**
2526 * smack_msg_free_security - Clear the security blob for msg
2527 * @msq: the object
2528 *
2529 * Clears the blob pointer
2530 */
2531static void smack_msg_queue_free_security(struct msg_queue *msq)
2532{
2533 struct kern_ipc_perm *kisp = &msq->q_perm;
2534
2535 kisp->security = NULL;
2536}
2537
2538/**
2539 * smack_of_msq - the smack pointer for the msq
2540 * @msq: the object
2541 *
2542 * Returns a pointer to the smack value
2543 */
2544static char *smack_of_msq(struct msg_queue *msq)
2545{
2546 return (char *)msq->q_perm.security;
2547}
2548
2549/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002550 * smk_curacc_msq : helper to check if current has access on msq
2551 * @msq : the msq
2552 * @access : access requested
2553 *
2554 * return 0 if current has access, error otherwise
2555 */
2556static int smk_curacc_msq(struct msg_queue *msq, int access)
2557{
2558 char *msp = smack_of_msq(msq);
2559 struct smk_audit_info ad;
2560
2561#ifdef CONFIG_AUDIT
2562 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2563 ad.a.u.ipc_id = msq->q_perm.id;
2564#endif
2565 return smk_curacc(msp, access, &ad);
2566}
2567
2568/**
Casey Schauflere114e472008-02-04 22:29:50 -08002569 * smack_msg_queue_associate - Smack access check for msg_queue
2570 * @msq: the object
2571 * @msqflg: access requested
2572 *
2573 * Returns 0 if current has the requested access, error code otherwise
2574 */
2575static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2576{
Casey Schauflere114e472008-02-04 22:29:50 -08002577 int may;
2578
2579 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002580 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002581}
2582
2583/**
2584 * smack_msg_queue_msgctl - Smack access check for msg_queue
2585 * @msq: the object
2586 * @cmd: what it wants to do
2587 *
2588 * Returns 0 if current has the requested access, error code otherwise
2589 */
2590static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2591{
Casey Schauflere114e472008-02-04 22:29:50 -08002592 int may;
2593
2594 switch (cmd) {
2595 case IPC_STAT:
2596 case MSG_STAT:
2597 may = MAY_READ;
2598 break;
2599 case IPC_SET:
2600 case IPC_RMID:
2601 may = MAY_READWRITE;
2602 break;
2603 case IPC_INFO:
2604 case MSG_INFO:
2605 /*
2606 * System level information
2607 */
2608 return 0;
2609 default:
2610 return -EINVAL;
2611 }
2612
Etienne Bassetecfcc532009-04-08 20:40:06 +02002613 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002614}
2615
2616/**
2617 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2618 * @msq: the object
2619 * @msg: unused
2620 * @msqflg: access requested
2621 *
2622 * Returns 0 if current has the requested access, error code otherwise
2623 */
2624static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2625 int msqflg)
2626{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002627 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002628
Etienne Bassetecfcc532009-04-08 20:40:06 +02002629 may = smack_flags_to_may(msqflg);
2630 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002631}
2632
2633/**
2634 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2635 * @msq: the object
2636 * @msg: unused
2637 * @target: unused
2638 * @type: unused
2639 * @mode: unused
2640 *
2641 * Returns 0 if current has read and write access, error code otherwise
2642 */
2643static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2644 struct task_struct *target, long type, int mode)
2645{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002646 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002647}
2648
2649/**
2650 * smack_ipc_permission - Smack access for ipc_permission()
2651 * @ipp: the object permissions
2652 * @flag: access requested
2653 *
2654 * Returns 0 if current has read and write access, error code otherwise
2655 */
2656static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2657{
2658 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002659 int may = smack_flags_to_may(flag);
2660 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002661
Etienne Bassetecfcc532009-04-08 20:40:06 +02002662#ifdef CONFIG_AUDIT
2663 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2664 ad.a.u.ipc_id = ipp->id;
2665#endif
2666 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002667}
2668
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002669/**
2670 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002671 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002672 * @secid: where result will be saved
2673 */
2674static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2675{
2676 char *smack = ipp->security;
2677
2678 *secid = smack_to_secid(smack);
2679}
2680
Casey Schauflere114e472008-02-04 22:29:50 -08002681/**
2682 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002683 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002684 * @inode: the object
2685 *
2686 * Set the inode's security blob if it hasn't been done already.
2687 */
2688static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2689{
2690 struct super_block *sbp;
2691 struct superblock_smack *sbsp;
2692 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002693 struct smack_known *skp;
2694 struct smack_known *ckp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002695 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002696 char trattr[TRANS_TRUE_SIZE];
2697 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002698 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002699 struct dentry *dp;
2700
2701 if (inode == NULL)
2702 return;
2703
2704 isp = inode->i_security;
2705
2706 mutex_lock(&isp->smk_lock);
2707 /*
2708 * If the inode is already instantiated
2709 * take the quick way out
2710 */
2711 if (isp->smk_flags & SMK_INODE_INSTANT)
2712 goto unlockandout;
2713
2714 sbp = inode->i_sb;
2715 sbsp = sbp->s_security;
2716 /*
2717 * We're going to use the superblock default label
2718 * if there's no label on the file.
2719 */
2720 final = sbsp->smk_default;
2721
2722 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002723 * If this is the root inode the superblock
2724 * may be in the process of initialization.
2725 * If that is the case use the root value out
2726 * of the superblock.
2727 */
2728 if (opt_dentry->d_parent == opt_dentry) {
2729 isp->smk_inode = sbsp->smk_root;
2730 isp->smk_flags |= SMK_INODE_INSTANT;
2731 goto unlockandout;
2732 }
2733
2734 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002735 * This is pretty hackish.
2736 * Casey says that we shouldn't have to do
2737 * file system specific code, but it does help
2738 * with keeping it simple.
2739 */
2740 switch (sbp->s_magic) {
2741 case SMACK_MAGIC:
2742 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002743 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002744 * that the smack file system doesn't do
2745 * extended attributes.
2746 */
2747 final = smack_known_star.smk_known;
2748 break;
2749 case PIPEFS_MAGIC:
2750 /*
2751 * Casey says pipes are easy (?)
2752 */
2753 final = smack_known_star.smk_known;
2754 break;
2755 case DEVPTS_SUPER_MAGIC:
2756 /*
2757 * devpts seems content with the label of the task.
2758 * Programs that change smack have to treat the
2759 * pty with respect.
2760 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002761 final = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002762 break;
2763 case SOCKFS_MAGIC:
2764 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002765 * Socket access is controlled by the socket
2766 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002767 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002768 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002769 break;
2770 case PROC_SUPER_MAGIC:
2771 /*
2772 * Casey says procfs appears not to care.
2773 * The superblock default suffices.
2774 */
2775 break;
2776 case TMPFS_MAGIC:
2777 /*
2778 * Device labels should come from the filesystem,
2779 * but watch out, because they're volitile,
2780 * getting recreated on every reboot.
2781 */
2782 final = smack_known_star.smk_known;
2783 /*
2784 * No break.
2785 *
2786 * If a smack value has been set we want to use it,
2787 * but since tmpfs isn't giving us the opportunity
2788 * to set mount options simulate setting the
2789 * superblock default.
2790 */
2791 default:
2792 /*
2793 * This isn't an understood special case.
2794 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002795 */
2796
2797 /*
2798 * UNIX domain sockets use lower level socket data.
2799 */
2800 if (S_ISSOCK(inode->i_mode)) {
2801 final = smack_known_star.smk_known;
2802 break;
2803 }
2804 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002805 * No xattr support means, alas, no SMACK label.
2806 * Use the aforeapplied default.
2807 * It would be curious if the label of the task
2808 * does not match that assigned.
2809 */
2810 if (inode->i_op->getxattr == NULL)
2811 break;
2812 /*
2813 * Get the dentry for xattr.
2814 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002815 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002816 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2817 if (skp != NULL)
2818 final = skp->smk_known;
Casey Schaufler2267b132012-03-13 19:14:19 -07002819
2820 /*
2821 * Transmuting directory
2822 */
2823 if (S_ISDIR(inode->i_mode)) {
2824 /*
2825 * If this is a new directory and the label was
2826 * transmuted when the inode was initialized
2827 * set the transmute attribute on the directory
2828 * and mark the inode.
2829 *
2830 * If there is a transmute attribute on the
2831 * directory mark the inode.
2832 */
2833 if (isp->smk_flags & SMK_INODE_CHANGED) {
2834 isp->smk_flags &= ~SMK_INODE_CHANGED;
2835 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002836 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002837 TRANS_TRUE, TRANS_TRUE_SIZE,
2838 0);
2839 } else {
2840 rc = inode->i_op->getxattr(dp,
2841 XATTR_NAME_SMACKTRANSMUTE, trattr,
2842 TRANS_TRUE_SIZE);
2843 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2844 TRANS_TRUE_SIZE) != 0)
2845 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002846 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002847 if (rc >= 0)
2848 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002849 }
2850 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002851 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002852
Casey Schauflere114e472008-02-04 22:29:50 -08002853 dput(dp);
2854 break;
2855 }
2856
2857 if (final == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002858 isp->smk_inode = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002859 else
2860 isp->smk_inode = final;
2861
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002862 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002863
2864unlockandout:
2865 mutex_unlock(&isp->smk_lock);
2866 return;
2867}
2868
2869/**
2870 * smack_getprocattr - Smack process attribute access
2871 * @p: the object task
2872 * @name: the name of the attribute in /proc/.../attr
2873 * @value: where to put the result
2874 *
2875 * Places a copy of the task Smack into value
2876 *
2877 * Returns the length of the smack label or an error code
2878 */
2879static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2880{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002881 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08002882 char *cp;
2883 int slen;
2884
2885 if (strcmp(name, "current") != 0)
2886 return -EINVAL;
2887
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002888 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002889 if (cp == NULL)
2890 return -ENOMEM;
2891
2892 slen = strlen(cp);
2893 *value = cp;
2894 return slen;
2895}
2896
2897/**
2898 * smack_setprocattr - Smack process attribute setting
2899 * @p: the object task
2900 * @name: the name of the attribute in /proc/.../attr
2901 * @value: the value to set
2902 * @size: the size of the value
2903 *
2904 * Sets the Smack value of the task. Only setting self
2905 * is permitted and only with privilege
2906 *
2907 * Returns the length of the smack label or an error code
2908 */
2909static int smack_setprocattr(struct task_struct *p, char *name,
2910 void *value, size_t size)
2911{
Casey Schaufler676dac42010-12-02 06:43:39 -08002912 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002913 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002914 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002915
Casey Schauflere114e472008-02-04 22:29:50 -08002916 /*
2917 * Changing another process' Smack value is too dangerous
2918 * and supports no sane use case.
2919 */
2920 if (p != current)
2921 return -EPERM;
2922
Casey Schaufler1880eff2012-06-05 15:28:30 -07002923 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01002924 return -EPERM;
2925
Casey Schauflerf7112e62012-05-06 15:22:02 -07002926 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08002927 return -EINVAL;
2928
2929 if (strcmp(name, "current") != 0)
2930 return -EINVAL;
2931
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002932 skp = smk_import_entry(value, size);
2933 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002934 return -EINVAL;
2935
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002936 /*
2937 * No process is ever allowed the web ("@") label.
2938 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002939 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002940 return -EPERM;
2941
David Howellsd84f4f92008-11-14 10:39:23 +11002942 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002943 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002944 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002945
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07002946 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002947 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002948
David Howellsd84f4f92008-11-14 10:39:23 +11002949 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002950 return size;
2951}
2952
2953/**
2954 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002955 * @sock: one sock
2956 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002957 * @newsk: unused
2958 *
2959 * Return 0 if a subject with the smack of sock could access
2960 * an object with the smack of other, otherwise an error code
2961 */
David S. Miller3610cda2011-01-05 15:38:53 -08002962static int smack_unix_stream_connect(struct sock *sock,
2963 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002964{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002965 struct smack_known *skp;
James Morrisd2e7ad12011-01-10 09:46:24 +11002966 struct socket_smack *ssp = sock->sk_security;
2967 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002968 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002969 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002970 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002971
Kees Cook923e9a12012-04-10 13:26:44 -07002972#ifdef CONFIG_AUDIT
2973 struct lsm_network_audit net;
2974
Eric Paris48c62af2012-04-02 13:15:44 -04002975 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
David S. Miller3610cda2011-01-05 15:38:53 -08002976 smk_ad_setfield_u_net_sk(&ad, other);
Kees Cook923e9a12012-04-10 13:26:44 -07002977#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002978
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002979 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
2980 skp = ssp->smk_out;
2981 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
2982 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002983
Casey Schaufler975d5e52011-09-26 14:43:39 -07002984 /*
2985 * Cross reference the peer labels for SO_PEERSEC.
2986 */
2987 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002988 nsp->smk_packet = ssp->smk_out->smk_known;
2989 ssp->smk_packet = osp->smk_out->smk_known;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002990 }
2991
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002992 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002993}
2994
2995/**
2996 * smack_unix_may_send - Smack access on UDS
2997 * @sock: one socket
2998 * @other: the other socket
2999 *
3000 * Return 0 if a subject with the smack of sock could access
3001 * an object with the smack of other, otherwise an error code
3002 */
3003static int smack_unix_may_send(struct socket *sock, struct socket *other)
3004{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003005 struct socket_smack *ssp = sock->sk->sk_security;
3006 struct socket_smack *osp = other->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003007 struct smack_known *skp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003008 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003009
Kees Cook923e9a12012-04-10 13:26:44 -07003010#ifdef CONFIG_AUDIT
3011 struct lsm_network_audit net;
3012
Eric Paris48c62af2012-04-02 13:15:44 -04003013 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003014 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003015#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003016
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003017 if (smack_privileged(CAP_MAC_OVERRIDE))
3018 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003019
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003020 skp = ssp->smk_out;
3021 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003022}
3023
3024/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003025 * smack_socket_sendmsg - Smack check based on destination host
3026 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003027 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003028 * @size: the size of the message
3029 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003030 * Return 0 if the current subject can write to the destination host.
3031 * For IPv4 this is only a question if the destination is a single label host.
3032 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003033 */
3034static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3035 int size)
3036{
3037 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003038 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003039 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003040
3041 /*
3042 * Perfectly reasonable for this to be NULL
3043 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003044 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003045 return 0;
3046
Casey Schauflerc6739442013-05-22 18:42:56 -07003047 switch (sip->sin_family) {
3048 case AF_INET:
3049 rc = smack_netlabel_send(sock->sk, sip);
3050 break;
3051 case AF_INET6:
3052 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3053 break;
3054 }
3055 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003056}
3057
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003058/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003059 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003060 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003061 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003062 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003063 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003064 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003065static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3066 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003067{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003068 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003069 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003070 int acat;
3071 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003072
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003073 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003074 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003075 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003076 * If there are flags but no level netlabel isn't
3077 * behaving the way we expect it to.
3078 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003079 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003080 * Without guidance regarding the smack value
3081 * for the packet fall back on the network
3082 * ambient value.
3083 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003084 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003085 list_for_each_entry(skp, &smack_known_list, list) {
3086 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003087 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003088 /*
3089 * Compare the catsets. Use the netlbl APIs.
3090 */
3091 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3092 if ((skp->smk_netlabel.flags &
3093 NETLBL_SECATTR_MLS_CAT) == 0)
3094 found = 1;
3095 break;
3096 }
3097 for (acat = -1, kcat = -1; acat == kcat; ) {
3098 acat = netlbl_secattr_catmap_walk(
3099 sap->attr.mls.cat, acat + 1);
3100 kcat = netlbl_secattr_catmap_walk(
3101 skp->smk_netlabel.attr.mls.cat,
3102 kcat + 1);
3103 if (acat < 0 || kcat < 0)
3104 break;
3105 }
3106 if (acat == kcat) {
3107 found = 1;
3108 break;
3109 }
Casey Schauflere114e472008-02-04 22:29:50 -08003110 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003111 rcu_read_unlock();
3112
3113 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003114 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003115
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003116 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003117 return &smack_known_web;
3118 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003119 }
3120 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3121 /*
3122 * Looks like a fallback, which gives us a secid.
3123 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003124 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003125 /*
3126 * This has got to be a bug because it is
3127 * impossible to specify a fallback without
3128 * specifying the label, which will ensure
3129 * it has a secid, and the only way to get a
3130 * secid is from a fallback.
3131 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003132 BUG_ON(skp == NULL);
3133 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003134 }
3135 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003136 * Without guidance regarding the smack value
3137 * for the packet fall back on the network
3138 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003139 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003140 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003141}
3142
Casey Schaufler6ea06242013-08-05 13:21:22 -07003143static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003144{
Casey Schauflerc6739442013-05-22 18:42:56 -07003145 u8 nexthdr;
3146 int offset;
3147 int proto = -EINVAL;
3148 struct ipv6hdr _ipv6h;
3149 struct ipv6hdr *ip6;
3150 __be16 frag_off;
3151 struct tcphdr _tcph, *th;
3152 struct udphdr _udph, *uh;
3153 struct dccp_hdr _dccph, *dh;
3154
3155 sip->sin6_port = 0;
3156
3157 offset = skb_network_offset(skb);
3158 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3159 if (ip6 == NULL)
3160 return -EINVAL;
3161 sip->sin6_addr = ip6->saddr;
3162
3163 nexthdr = ip6->nexthdr;
3164 offset += sizeof(_ipv6h);
3165 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3166 if (offset < 0)
3167 return -EINVAL;
3168
3169 proto = nexthdr;
3170 switch (proto) {
3171 case IPPROTO_TCP:
3172 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3173 if (th != NULL)
3174 sip->sin6_port = th->source;
3175 break;
3176 case IPPROTO_UDP:
3177 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3178 if (uh != NULL)
3179 sip->sin6_port = uh->source;
3180 break;
3181 case IPPROTO_DCCP:
3182 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3183 if (dh != NULL)
3184 sip->sin6_port = dh->dccph_sport;
3185 break;
3186 }
3187 return proto;
3188}
3189
Casey Schauflere114e472008-02-04 22:29:50 -08003190/**
3191 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3192 * @sk: socket
3193 * @skb: packet
3194 *
3195 * Returns 0 if the packet should be delivered, an error code otherwise
3196 */
3197static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3198{
3199 struct netlbl_lsm_secattr secattr;
3200 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003201 struct smack_known *skp;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003202 struct sockaddr_in6 sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003203 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003204 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003205#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003206 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003207#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003208 switch (sk->sk_family) {
3209 case PF_INET:
3210 /*
3211 * Translate what netlabel gave us.
3212 */
3213 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003214
Casey Schauflerc6739442013-05-22 18:42:56 -07003215 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3216 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003217 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003218 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003219 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003220
Casey Schauflerc6739442013-05-22 18:42:56 -07003221 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003222
Etienne Bassetecfcc532009-04-08 20:40:06 +02003223#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003224 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3225 ad.a.u.net->family = sk->sk_family;
3226 ad.a.u.net->netif = skb->skb_iif;
3227 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003228#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003229 /*
3230 * Receiving a packet requires that the other end
3231 * be able to write here. Read access is not required.
3232 * This is the simplist possible security model
3233 * for networking.
3234 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003235 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07003236 if (rc != 0)
3237 netlbl_skbuff_err(skb, rc, 0);
3238 break;
3239 case PF_INET6:
3240 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3241 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3242 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3243 else
3244 rc = 0;
3245 break;
3246 }
Paul Moorea8134292008-10-10 10:16:31 -04003247 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003248}
3249
3250/**
3251 * smack_socket_getpeersec_stream - pull in packet label
3252 * @sock: the socket
3253 * @optval: user's destination
3254 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003255 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003256 *
3257 * returns zero on success, an error code otherwise
3258 */
3259static int smack_socket_getpeersec_stream(struct socket *sock,
3260 char __user *optval,
3261 int __user *optlen, unsigned len)
3262{
3263 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003264 char *rcp = "";
3265 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003266 int rc = 0;
3267
3268 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003269 if (ssp->smk_packet != NULL) {
3270 rcp = ssp->smk_packet;
3271 slen = strlen(rcp) + 1;
3272 }
Casey Schauflere114e472008-02-04 22:29:50 -08003273
3274 if (slen > len)
3275 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003276 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003277 rc = -EFAULT;
3278
3279 if (put_user(slen, optlen) != 0)
3280 rc = -EFAULT;
3281
3282 return rc;
3283}
3284
3285
3286/**
3287 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003288 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003289 * @skb: packet data
3290 * @secid: pointer to where to put the secid of the packet
3291 *
3292 * Sets the netlabel socket state on sk from parent
3293 */
3294static int smack_socket_getpeersec_dgram(struct socket *sock,
3295 struct sk_buff *skb, u32 *secid)
3296
3297{
3298 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003299 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003300 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003301 int family = PF_UNSPEC;
3302 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003303 int rc;
3304
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003305 if (skb != NULL) {
3306 if (skb->protocol == htons(ETH_P_IP))
3307 family = PF_INET;
3308 else if (skb->protocol == htons(ETH_P_IPV6))
3309 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003310 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003311 if (family == PF_UNSPEC && sock != NULL)
3312 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003313
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003314 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003315 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003316 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003317 } else if (family == PF_INET || family == PF_INET6) {
3318 /*
3319 * Translate what netlabel gave us.
3320 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003321 if (sock != NULL && sock->sk != NULL)
3322 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003323 netlbl_secattr_init(&secattr);
3324 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3325 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003326 skp = smack_from_secattr(&secattr, ssp);
3327 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003328 }
3329 netlbl_secattr_destroy(&secattr);
3330 }
3331 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003332 if (s == 0)
3333 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003334 return 0;
3335}
3336
3337/**
Paul Moore07feee82009-03-27 17:10:54 -04003338 * smack_sock_graft - Initialize a newly created socket with an existing sock
3339 * @sk: child sock
3340 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003341 *
Paul Moore07feee82009-03-27 17:10:54 -04003342 * Set the smk_{in,out} state of an existing sock based on the process that
3343 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003344 */
3345static void smack_sock_graft(struct sock *sk, struct socket *parent)
3346{
3347 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003348 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003349
Paul Moore07feee82009-03-27 17:10:54 -04003350 if (sk == NULL ||
3351 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003352 return;
3353
3354 ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003355 ssp->smk_in = skp->smk_known;
3356 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003357 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003358}
3359
3360/**
3361 * smack_inet_conn_request - Smack access check on connect
3362 * @sk: socket involved
3363 * @skb: packet
3364 * @req: unused
3365 *
3366 * Returns 0 if a task with the packet label could write to
3367 * the socket, otherwise an error code
3368 */
3369static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3370 struct request_sock *req)
3371{
Paul Moore07feee82009-03-27 17:10:54 -04003372 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003373 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003374 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003375 struct netlbl_lsm_secattr secattr;
3376 struct sockaddr_in addr;
3377 struct iphdr *hdr;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003378 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003379 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003380 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003381#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003382 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003383#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003384
Casey Schauflerc6739442013-05-22 18:42:56 -07003385 if (family == PF_INET6) {
3386 /*
3387 * Handle mapped IPv4 packets arriving
3388 * via IPv6 sockets. Don't set up netlabel
3389 * processing on IPv6.
3390 */
3391 if (skb->protocol == htons(ETH_P_IP))
3392 family = PF_INET;
3393 else
3394 return 0;
3395 }
Casey Schauflere114e472008-02-04 22:29:50 -08003396
Paul Moore07feee82009-03-27 17:10:54 -04003397 netlbl_secattr_init(&secattr);
3398 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003399 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003400 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003401 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003402 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003403 netlbl_secattr_destroy(&secattr);
3404
Etienne Bassetecfcc532009-04-08 20:40:06 +02003405#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003406 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3407 ad.a.u.net->family = family;
3408 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003409 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3410#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003411 /*
Paul Moore07feee82009-03-27 17:10:54 -04003412 * Receiving a packet requires that the other end be able to write
3413 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003414 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003415 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003416 if (rc != 0)
3417 return rc;
3418
3419 /*
3420 * Save the peer's label in the request_sock so we can later setup
3421 * smk_packet in the child socket so that SO_PEERCRED can report it.
3422 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003423 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003424
3425 /*
3426 * We need to decide if we want to label the incoming connection here
3427 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003428 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003429 */
3430 hdr = ip_hdr(skb);
3431 addr.sin_addr.s_addr = hdr->saddr;
3432 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003433 hsp = smack_host_label(&addr);
3434 rcu_read_unlock();
3435
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003436 if (hsp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003437 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003438 else
Paul Moore07feee82009-03-27 17:10:54 -04003439 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003440
3441 return rc;
3442}
3443
Paul Moore07feee82009-03-27 17:10:54 -04003444/**
3445 * smack_inet_csk_clone - Copy the connection information to the new socket
3446 * @sk: the new socket
3447 * @req: the connection's request_sock
3448 *
3449 * Transfer the connection's peer label to the newly created socket.
3450 */
3451static void smack_inet_csk_clone(struct sock *sk,
3452 const struct request_sock *req)
3453{
3454 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003455 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003456
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003457 if (req->peer_secid != 0) {
3458 skp = smack_from_secid(req->peer_secid);
3459 ssp->smk_packet = skp->smk_known;
3460 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003461 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003462}
3463
Casey Schauflere114e472008-02-04 22:29:50 -08003464/*
3465 * Key management security hooks
3466 *
3467 * Casey has not tested key support very heavily.
3468 * The permission check is most likely too restrictive.
3469 * If you care about keys please have a look.
3470 */
3471#ifdef CONFIG_KEYS
3472
3473/**
3474 * smack_key_alloc - Set the key security blob
3475 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003476 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003477 * @flags: unused
3478 *
3479 * No allocation required
3480 *
3481 * Returns 0
3482 */
David Howellsd84f4f92008-11-14 10:39:23 +11003483static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003484 unsigned long flags)
3485{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003486 struct smack_known *skp = smk_of_task(cred->security);
3487
3488 key->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003489 return 0;
3490}
3491
3492/**
3493 * smack_key_free - Clear the key security blob
3494 * @key: the object
3495 *
3496 * Clear the blob pointer
3497 */
3498static void smack_key_free(struct key *key)
3499{
3500 key->security = NULL;
3501}
3502
3503/*
3504 * smack_key_permission - Smack access on a key
3505 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003506 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003507 * @perm: unused
3508 *
3509 * Return 0 if the task has read and write to the object,
3510 * an error code otherwise
3511 */
3512static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003513 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003514{
3515 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003516 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003517 struct smack_known *tkp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003518
3519 keyp = key_ref_to_ptr(key_ref);
3520 if (keyp == NULL)
3521 return -EINVAL;
3522 /*
3523 * If the key hasn't been initialized give it access so that
3524 * it may do so.
3525 */
3526 if (keyp->security == NULL)
3527 return 0;
3528 /*
3529 * This should not occur
3530 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003531 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003532 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003533#ifdef CONFIG_AUDIT
3534 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3535 ad.a.u.key_struct.key = keyp->serial;
3536 ad.a.u.key_struct.key_desc = keyp->description;
3537#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003538 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003539}
3540#endif /* CONFIG_KEYS */
3541
3542/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003543 * Smack Audit hooks
3544 *
3545 * Audit requires a unique representation of each Smack specific
3546 * rule. This unique representation is used to distinguish the
3547 * object to be audited from remaining kernel objects and also
3548 * works as a glue between the audit hooks.
3549 *
3550 * Since repository entries are added but never deleted, we'll use
3551 * the smack_known label address related to the given audit rule as
3552 * the needed unique representation. This also better fits the smack
3553 * model where nearly everything is a label.
3554 */
3555#ifdef CONFIG_AUDIT
3556
3557/**
3558 * smack_audit_rule_init - Initialize a smack audit rule
3559 * @field: audit rule fields given from user-space (audit.h)
3560 * @op: required testing operator (=, !=, >, <, ...)
3561 * @rulestr: smack label to be audited
3562 * @vrule: pointer to save our own audit rule representation
3563 *
3564 * Prepare to audit cases where (@field @op @rulestr) is true.
3565 * The label to be audited is created if necessay.
3566 */
3567static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3568{
3569 char **rule = (char **)vrule;
3570 *rule = NULL;
3571
3572 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3573 return -EINVAL;
3574
Al Viro5af75d82008-12-16 05:59:26 -05003575 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003576 return -EINVAL;
3577
3578 *rule = smk_import(rulestr, 0);
3579
3580 return 0;
3581}
3582
3583/**
3584 * smack_audit_rule_known - Distinguish Smack audit rules
3585 * @krule: rule of interest, in Audit kernel representation format
3586 *
3587 * This is used to filter Smack rules from remaining Audit ones.
3588 * If it's proved that this rule belongs to us, the
3589 * audit_rule_match hook will be called to do the final judgement.
3590 */
3591static int smack_audit_rule_known(struct audit_krule *krule)
3592{
3593 struct audit_field *f;
3594 int i;
3595
3596 for (i = 0; i < krule->field_count; i++) {
3597 f = &krule->fields[i];
3598
3599 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3600 return 1;
3601 }
3602
3603 return 0;
3604}
3605
3606/**
3607 * smack_audit_rule_match - Audit given object ?
3608 * @secid: security id for identifying the object to test
3609 * @field: audit rule flags given from user-space
3610 * @op: required testing operator
3611 * @vrule: smack internal rule presentation
3612 * @actx: audit context associated with the check
3613 *
3614 * The core Audit hook. It's used to take the decision of
3615 * whether to audit or not to audit a given object.
3616 */
3617static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3618 struct audit_context *actx)
3619{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003620 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003621 char *rule = vrule;
3622
3623 if (!rule) {
Tetsuo Handaceffec552012-03-29 16:19:05 +09003624 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003625 "Smack: missing rule\n");
3626 return -ENOENT;
3627 }
3628
3629 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3630 return 0;
3631
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003632 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003633
3634 /*
3635 * No need to do string comparisons. If a match occurs,
3636 * both pointers will point to the same smack_known
3637 * label.
3638 */
Al Viro5af75d82008-12-16 05:59:26 -05003639 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003640 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003641 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003642 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003643
3644 return 0;
3645}
3646
3647/**
3648 * smack_audit_rule_free - free smack rule representation
3649 * @vrule: rule to be freed.
3650 *
3651 * No memory was allocated.
3652 */
3653static void smack_audit_rule_free(void *vrule)
3654{
3655 /* No-op */
3656}
3657
3658#endif /* CONFIG_AUDIT */
3659
Randy Dunlap251a2a92009-02-18 11:42:33 -08003660/**
David Quigley746df9b2013-05-22 12:50:35 -04003661 * smack_ismaclabel - check if xattr @name references a smack MAC label
3662 * @name: Full xattr name to check.
3663 */
3664static int smack_ismaclabel(const char *name)
3665{
3666 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3667}
3668
3669
3670/**
Casey Schauflere114e472008-02-04 22:29:50 -08003671 * smack_secid_to_secctx - return the smack label for a secid
3672 * @secid: incoming integer
3673 * @secdata: destination
3674 * @seclen: how long it is
3675 *
3676 * Exists for networking code.
3677 */
3678static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3679{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003680 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003681
Eric Parisd5630b92010-10-13 16:24:48 -04003682 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003683 *secdata = skp->smk_known;
3684 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08003685 return 0;
3686}
3687
Randy Dunlap251a2a92009-02-18 11:42:33 -08003688/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003689 * smack_secctx_to_secid - return the secid for a smack label
3690 * @secdata: smack label
3691 * @seclen: how long result is
3692 * @secid: outgoing integer
3693 *
3694 * Exists for audit and networking code.
3695 */
David Howellse52c17642008-04-29 20:52:51 +01003696static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003697{
3698 *secid = smack_to_secid(secdata);
3699 return 0;
3700}
3701
Randy Dunlap251a2a92009-02-18 11:42:33 -08003702/**
Casey Schauflere114e472008-02-04 22:29:50 -08003703 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003704 * @secdata: unused
3705 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003706 *
3707 * Exists to make sure nothing gets done, and properly
3708 */
3709static void smack_release_secctx(char *secdata, u32 seclen)
3710{
3711}
3712
David P. Quigley1ee65e32009-09-03 14:25:57 -04003713static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3714{
3715 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3716}
3717
3718static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3719{
3720 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3721}
3722
3723static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3724{
3725 int len = 0;
3726 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3727
3728 if (len < 0)
3729 return len;
3730 *ctxlen = len;
3731 return 0;
3732}
3733
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003734struct security_operations smack_ops = {
3735 .name = "smack",
3736
Ingo Molnar9e488582009-05-07 19:26:19 +10003737 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003738 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003739 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003740
3741 .sb_alloc_security = smack_sb_alloc_security,
3742 .sb_free_security = smack_sb_free_security,
3743 .sb_copy_data = smack_sb_copy_data,
3744 .sb_kern_mount = smack_sb_kern_mount,
3745 .sb_statfs = smack_sb_statfs,
3746 .sb_mount = smack_sb_mount,
3747 .sb_umount = smack_sb_umount,
3748
Casey Schaufler676dac42010-12-02 06:43:39 -08003749 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003750 .bprm_committing_creds = smack_bprm_committing_creds,
3751 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003752
Casey Schauflere114e472008-02-04 22:29:50 -08003753 .inode_alloc_security = smack_inode_alloc_security,
3754 .inode_free_security = smack_inode_free_security,
3755 .inode_init_security = smack_inode_init_security,
3756 .inode_link = smack_inode_link,
3757 .inode_unlink = smack_inode_unlink,
3758 .inode_rmdir = smack_inode_rmdir,
3759 .inode_rename = smack_inode_rename,
3760 .inode_permission = smack_inode_permission,
3761 .inode_setattr = smack_inode_setattr,
3762 .inode_getattr = smack_inode_getattr,
3763 .inode_setxattr = smack_inode_setxattr,
3764 .inode_post_setxattr = smack_inode_post_setxattr,
3765 .inode_getxattr = smack_inode_getxattr,
3766 .inode_removexattr = smack_inode_removexattr,
3767 .inode_getsecurity = smack_inode_getsecurity,
3768 .inode_setsecurity = smack_inode_setsecurity,
3769 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003770 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003771
3772 .file_permission = smack_file_permission,
3773 .file_alloc_security = smack_file_alloc_security,
3774 .file_free_security = smack_file_free_security,
3775 .file_ioctl = smack_file_ioctl,
3776 .file_lock = smack_file_lock,
3777 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003778 .mmap_file = smack_mmap_file,
3779 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003780 .file_set_fowner = smack_file_set_fowner,
3781 .file_send_sigiotask = smack_file_send_sigiotask,
3782 .file_receive = smack_file_receive,
3783
Eric Paris83d49852012-04-04 13:45:40 -04003784 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003785
David Howellsee18d642009-09-02 09:14:21 +01003786 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003787 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003788 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003789 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003790 .kernel_act_as = smack_kernel_act_as,
3791 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003792 .task_setpgid = smack_task_setpgid,
3793 .task_getpgid = smack_task_getpgid,
3794 .task_getsid = smack_task_getsid,
3795 .task_getsecid = smack_task_getsecid,
3796 .task_setnice = smack_task_setnice,
3797 .task_setioprio = smack_task_setioprio,
3798 .task_getioprio = smack_task_getioprio,
3799 .task_setscheduler = smack_task_setscheduler,
3800 .task_getscheduler = smack_task_getscheduler,
3801 .task_movememory = smack_task_movememory,
3802 .task_kill = smack_task_kill,
3803 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003804 .task_to_inode = smack_task_to_inode,
3805
3806 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003807 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003808
3809 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3810 .msg_msg_free_security = smack_msg_msg_free_security,
3811
3812 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3813 .msg_queue_free_security = smack_msg_queue_free_security,
3814 .msg_queue_associate = smack_msg_queue_associate,
3815 .msg_queue_msgctl = smack_msg_queue_msgctl,
3816 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3817 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3818
3819 .shm_alloc_security = smack_shm_alloc_security,
3820 .shm_free_security = smack_shm_free_security,
3821 .shm_associate = smack_shm_associate,
3822 .shm_shmctl = smack_shm_shmctl,
3823 .shm_shmat = smack_shm_shmat,
3824
3825 .sem_alloc_security = smack_sem_alloc_security,
3826 .sem_free_security = smack_sem_free_security,
3827 .sem_associate = smack_sem_associate,
3828 .sem_semctl = smack_sem_semctl,
3829 .sem_semop = smack_sem_semop,
3830
Casey Schauflere114e472008-02-04 22:29:50 -08003831 .d_instantiate = smack_d_instantiate,
3832
3833 .getprocattr = smack_getprocattr,
3834 .setprocattr = smack_setprocattr,
3835
3836 .unix_stream_connect = smack_unix_stream_connect,
3837 .unix_may_send = smack_unix_may_send,
3838
3839 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07003840 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003841 .socket_connect = smack_socket_connect,
3842 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003843 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3844 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3845 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3846 .sk_alloc_security = smack_sk_alloc_security,
3847 .sk_free_security = smack_sk_free_security,
3848 .sock_graft = smack_sock_graft,
3849 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003850 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003851
Casey Schauflere114e472008-02-04 22:29:50 -08003852 /* key management security hooks */
3853#ifdef CONFIG_KEYS
3854 .key_alloc = smack_key_alloc,
3855 .key_free = smack_key_free,
3856 .key_permission = smack_key_permission,
3857#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003858
3859 /* Audit hooks */
3860#ifdef CONFIG_AUDIT
3861 .audit_rule_init = smack_audit_rule_init,
3862 .audit_rule_known = smack_audit_rule_known,
3863 .audit_rule_match = smack_audit_rule_match,
3864 .audit_rule_free = smack_audit_rule_free,
3865#endif /* CONFIG_AUDIT */
3866
David Quigley746df9b2013-05-22 12:50:35 -04003867 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08003868 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003869 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003870 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003871 .inode_notifysecctx = smack_inode_notifysecctx,
3872 .inode_setsecctx = smack_inode_setsecctx,
3873 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003874};
3875
Etienne Basset7198e2e2009-03-24 20:53:24 +01003876
Casey Schaufler86812bb2012-04-17 18:55:46 -07003877static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003878{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003879 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003880 * Initialize rule list locks
3881 */
3882 mutex_init(&smack_known_huh.smk_rules_lock);
3883 mutex_init(&smack_known_hat.smk_rules_lock);
3884 mutex_init(&smack_known_floor.smk_rules_lock);
3885 mutex_init(&smack_known_star.smk_rules_lock);
3886 mutex_init(&smack_known_invalid.smk_rules_lock);
3887 mutex_init(&smack_known_web.smk_rules_lock);
3888 /*
3889 * Initialize rule lists
3890 */
3891 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3892 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3893 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3894 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3895 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3896 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3897 /*
3898 * Create the known labels list
3899 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02003900 smk_insert_entry(&smack_known_huh);
3901 smk_insert_entry(&smack_known_hat);
3902 smk_insert_entry(&smack_known_star);
3903 smk_insert_entry(&smack_known_floor);
3904 smk_insert_entry(&smack_known_invalid);
3905 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01003906}
3907
Casey Schauflere114e472008-02-04 22:29:50 -08003908/**
3909 * smack_init - initialize the smack system
3910 *
3911 * Returns 0
3912 */
3913static __init int smack_init(void)
3914{
David Howellsd84f4f92008-11-14 10:39:23 +11003915 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003916 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003917
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003918 if (!security_module_enable(&smack_ops))
3919 return 0;
3920
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003921 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3922 GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003923 if (tsp == NULL)
3924 return -ENOMEM;
3925
Casey Schauflere114e472008-02-04 22:29:50 -08003926 printk(KERN_INFO "Smack: Initializing.\n");
3927
3928 /*
3929 * Set the security state for the initial task.
3930 */
David Howellsd84f4f92008-11-14 10:39:23 +11003931 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003932 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003933
Casey Schaufler86812bb2012-04-17 18:55:46 -07003934 /* initialize the smack_known_list */
3935 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003936
3937 /*
3938 * Register with LSM
3939 */
3940 if (register_security(&smack_ops))
3941 panic("smack: Unable to register with kernel.\n");
3942
3943 return 0;
3944}
3945
3946/*
3947 * Smack requires early initialization in order to label
3948 * all processes and objects when they are created.
3949 */
3950security_initcall(smack_init);