blob: 609e89de3c24be029551f7321673d95ada99c5c2 [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 Schaufler676dac42010-12-02 06:43:39 -080065static char *smk_fetch(const char *name, struct inode *ip, struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080066{
67 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -070068 char *buffer;
69 char *result = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -080070
71 if (ip->i_op->getxattr == NULL)
72 return NULL;
73
Casey Schauflerf7112e62012-05-06 15:22:02 -070074 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
75 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -080076 return NULL;
77
Casey Schauflerf7112e62012-05-06 15:22:02 -070078 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
79 if (rc > 0)
80 result = smk_import(buffer, rc);
81
82 kfree(buffer);
83
84 return result;
Casey Schauflere114e472008-02-04 22:29:50 -080085}
86
87/**
88 * new_inode_smack - allocate an inode security blob
89 * @smack: a pointer to the Smack label to use in the blob
90 *
91 * Returns the new blob or NULL if there's no memory available
92 */
93struct inode_smack *new_inode_smack(char *smack)
94{
95 struct inode_smack *isp;
96
Tetsuo Handaceffec552012-03-29 16:19:05 +090097 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -080098 if (isp == NULL)
99 return NULL;
100
101 isp->smk_inode = smack;
102 isp->smk_flags = 0;
103 mutex_init(&isp->smk_lock);
104
105 return isp;
106}
107
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800108/**
109 * new_task_smack - allocate a task security blob
110 * @smack: a pointer to the Smack label to use in the blob
111 *
112 * Returns the new blob or NULL if there's no memory available
113 */
114static struct task_smack *new_task_smack(char *task, char *forked, gfp_t gfp)
115{
116 struct task_smack *tsp;
117
118 tsp = kzalloc(sizeof(struct task_smack), gfp);
119 if (tsp == NULL)
120 return NULL;
121
122 tsp->smk_task = task;
123 tsp->smk_forked = forked;
124 INIT_LIST_HEAD(&tsp->smk_rules);
125 mutex_init(&tsp->smk_rules_lock);
126
127 return tsp;
128}
129
130/**
131 * smk_copy_rules - copy a rule set
132 * @nhead - new rules header pointer
133 * @ohead - old rules header pointer
134 *
135 * Returns 0 on success, -ENOMEM on error
136 */
137static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
138 gfp_t gfp)
139{
140 struct smack_rule *nrp;
141 struct smack_rule *orp;
142 int rc = 0;
143
144 INIT_LIST_HEAD(nhead);
145
146 list_for_each_entry_rcu(orp, ohead, list) {
147 nrp = kzalloc(sizeof(struct smack_rule), gfp);
148 if (nrp == NULL) {
149 rc = -ENOMEM;
150 break;
151 }
152 *nrp = *orp;
153 list_add_rcu(&nrp->list, nhead);
154 }
155 return rc;
156}
157
Casey Schauflere114e472008-02-04 22:29:50 -0800158/*
159 * LSM hooks.
160 * We he, that is fun!
161 */
162
163/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000164 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800165 * @ctp: child task pointer
Randy Dunlap251a2a92009-02-18 11:42:33 -0800166 * @mode: ptrace attachment mode
Casey Schauflere114e472008-02-04 22:29:50 -0800167 *
168 * Returns 0 if access is OK, an error code otherwise
169 *
170 * Do the capability checks, and require read and write.
171 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000172static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800173{
174 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200175 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800176 char *tsp;
Casey Schauflere114e472008-02-04 22:29:50 -0800177
Ingo Molnar9e488582009-05-07 19:26:19 +1000178 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800179 if (rc != 0)
180 return rc;
181
Casey Schaufler676dac42010-12-02 06:43:39 -0800182 tsp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200183 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
184 smk_ad_setfield_u_tsk(&ad, ctp);
185
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800186 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
David Howells5cd9c582008-08-14 11:37:28 +0100187 return rc;
188}
Casey Schauflere114e472008-02-04 22:29:50 -0800189
David Howells5cd9c582008-08-14 11:37:28 +0100190/**
191 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
192 * @ptp: parent task pointer
193 *
194 * Returns 0 if access is OK, an error code otherwise
195 *
196 * Do the capability checks, and require read and write.
197 */
198static int smack_ptrace_traceme(struct task_struct *ptp)
199{
200 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200201 struct smk_audit_info ad;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800202 char *tsp;
David Howells5cd9c582008-08-14 11:37:28 +0100203
204 rc = cap_ptrace_traceme(ptp);
205 if (rc != 0)
206 return rc;
207
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800208 tsp = smk_of_task(task_security(ptp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200209 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
210 smk_ad_setfield_u_tsk(&ad, ptp);
211
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800212 rc = smk_curacc(tsp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800213 return rc;
214}
215
216/**
217 * smack_syslog - Smack approval on syslog
218 * @type: message type
219 *
220 * Require that the task has the floor label
221 *
222 * Returns 0 on success, error code otherwise.
223 */
Eric Paris12b30522010-11-15 18:36:29 -0500224static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800225{
Eric Paris12b30522010-11-15 18:36:29 -0500226 int rc = 0;
Casey Schaufler676dac42010-12-02 06:43:39 -0800227 char *sp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800228
Casey Schaufler1880eff2012-06-05 15:28:30 -0700229 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800230 return 0;
231
232 if (sp != smack_known_floor.smk_known)
233 rc = -EACCES;
234
235 return rc;
236}
237
238
239/*
240 * Superblock Hooks.
241 */
242
243/**
244 * smack_sb_alloc_security - allocate a superblock blob
245 * @sb: the superblock getting the blob
246 *
247 * Returns 0 on success or -ENOMEM on error.
248 */
249static int smack_sb_alloc_security(struct super_block *sb)
250{
251 struct superblock_smack *sbsp;
252
253 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
254
255 if (sbsp == NULL)
256 return -ENOMEM;
257
258 sbsp->smk_root = smack_known_floor.smk_known;
259 sbsp->smk_default = smack_known_floor.smk_known;
260 sbsp->smk_floor = smack_known_floor.smk_known;
261 sbsp->smk_hat = smack_known_hat.smk_known;
262 sbsp->smk_initialized = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800263
264 sb->s_security = sbsp;
265
266 return 0;
267}
268
269/**
270 * smack_sb_free_security - free a superblock blob
271 * @sb: the superblock getting the blob
272 *
273 */
274static void smack_sb_free_security(struct super_block *sb)
275{
276 kfree(sb->s_security);
277 sb->s_security = NULL;
278}
279
280/**
281 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800282 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800283 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800284 *
285 * Returns 0 on success or -ENOMEM on error.
286 *
287 * Copy the Smack specific mount options out of the mount
288 * options list.
289 */
Eric Parise0007522008-03-05 10:31:54 -0500290static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800291{
292 char *cp, *commap, *otheropts, *dp;
293
Casey Schauflere114e472008-02-04 22:29:50 -0800294 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
295 if (otheropts == NULL)
296 return -ENOMEM;
297
298 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
299 if (strstr(cp, SMK_FSDEFAULT) == cp)
300 dp = smackopts;
301 else if (strstr(cp, SMK_FSFLOOR) == cp)
302 dp = smackopts;
303 else if (strstr(cp, SMK_FSHAT) == cp)
304 dp = smackopts;
305 else if (strstr(cp, SMK_FSROOT) == cp)
306 dp = smackopts;
307 else
308 dp = otheropts;
309
310 commap = strchr(cp, ',');
311 if (commap != NULL)
312 *commap = '\0';
313
314 if (*dp != '\0')
315 strcat(dp, ",");
316 strcat(dp, cp);
317 }
318
319 strcpy(orig, otheropts);
320 free_page((unsigned long)otheropts);
321
322 return 0;
323}
324
325/**
326 * smack_sb_kern_mount - Smack specific mount processing
327 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100328 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800329 * @data: the smack mount options
330 *
331 * Returns 0 on success, an error code on failure
332 */
James Morris12204e22008-12-19 10:44:42 +1100333static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800334{
335 struct dentry *root = sb->s_root;
336 struct inode *inode = root->d_inode;
337 struct superblock_smack *sp = sb->s_security;
338 struct inode_smack *isp;
339 char *op;
340 char *commap;
341 char *nsp;
342
Casey Schauflereb982cb2012-05-23 17:46:58 -0700343 if (sp->smk_initialized != 0)
Casey Schauflere114e472008-02-04 22:29:50 -0800344 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700345
Casey Schauflere114e472008-02-04 22:29:50 -0800346 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800347
348 for (op = data; op != NULL; op = commap) {
349 commap = strchr(op, ',');
350 if (commap != NULL)
351 *commap++ = '\0';
352
353 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
354 op += strlen(SMK_FSHAT);
355 nsp = smk_import(op, 0);
356 if (nsp != NULL)
357 sp->smk_hat = nsp;
358 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
359 op += strlen(SMK_FSFLOOR);
360 nsp = smk_import(op, 0);
361 if (nsp != NULL)
362 sp->smk_floor = nsp;
363 } else if (strncmp(op, SMK_FSDEFAULT,
364 strlen(SMK_FSDEFAULT)) == 0) {
365 op += strlen(SMK_FSDEFAULT);
366 nsp = smk_import(op, 0);
367 if (nsp != NULL)
368 sp->smk_default = nsp;
369 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
370 op += strlen(SMK_FSROOT);
371 nsp = smk_import(op, 0);
372 if (nsp != NULL)
373 sp->smk_root = nsp;
374 }
375 }
376
377 /*
378 * Initialize the root inode.
379 */
380 isp = inode->i_security;
381 if (isp == NULL)
382 inode->i_security = new_inode_smack(sp->smk_root);
383 else
384 isp->smk_inode = sp->smk_root;
385
386 return 0;
387}
388
389/**
390 * smack_sb_statfs - Smack check on statfs
391 * @dentry: identifies the file system in question
392 *
393 * Returns 0 if current can read the floor of the filesystem,
394 * and error code otherwise
395 */
396static int smack_sb_statfs(struct dentry *dentry)
397{
398 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200399 int rc;
400 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800401
Eric Parisa2694342011-04-25 13:10:27 -0400402 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200403 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
404
405 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
406 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800407}
408
409/**
410 * smack_sb_mount - Smack check for mounting
411 * @dev_name: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -0800412 * @path: mount point
Casey Schauflere114e472008-02-04 22:29:50 -0800413 * @type: unused
414 * @flags: unused
415 * @data: unused
416 *
417 * Returns 0 if current can write the floor of the filesystem
418 * being mounted on, an error code otherwise.
419 */
Al Viro808d4e32012-10-11 11:42:01 -0400420static int smack_sb_mount(const char *dev_name, struct path *path,
421 const char *type, unsigned long flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800422{
Al Virod8c95842011-12-07 18:16:57 -0500423 struct superblock_smack *sbp = path->dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200424 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800425
Eric Parisf48b7392011-04-25 12:54:27 -0400426 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200427 smk_ad_setfield_u_fs_path(&ad, *path);
428
429 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800430}
431
432/**
433 * smack_sb_umount - Smack check for unmounting
434 * @mnt: file system to unmount
435 * @flags: unused
436 *
437 * Returns 0 if current can write the floor of the filesystem
438 * being unmounted, an error code otherwise.
439 */
440static int smack_sb_umount(struct vfsmount *mnt, int flags)
441{
442 struct superblock_smack *sbp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200443 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400444 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200445
Eric Parisa2694342011-04-25 13:10:27 -0400446 path.dentry = mnt->mnt_root;
447 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200448
Eric Parisf48b7392011-04-25 12:54:27 -0400449 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400450 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflere114e472008-02-04 22:29:50 -0800451
Al Virod8c95842011-12-07 18:16:57 -0500452 sbp = path.dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200453 return smk_curacc(sbp->smk_floor, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800454}
455
456/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800457 * BPRM hooks
458 */
459
Casey Schauflerce8a4322011-09-29 18:21:01 -0700460/**
461 * smack_bprm_set_creds - set creds for exec
462 * @bprm: the exec information
463 *
464 * Returns 0 if it gets a blob, -ENOMEM otherwise
465 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800466static int smack_bprm_set_creds(struct linux_binprm *bprm)
467{
Al Viro496ad9a2013-01-23 17:07:38 -0500468 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300469 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800470 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800471 int rc;
472
473 rc = cap_bprm_set_creds(bprm);
474 if (rc != 0)
475 return rc;
476
477 if (bprm->cred_prepared)
478 return 0;
479
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300480 isp = inode->i_security;
481 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800482 return 0;
483
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300484 if (bprm->unsafe)
485 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800486
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300487 bsp->smk_task = isp->smk_task;
488 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800489
490 return 0;
491}
492
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300493/**
494 * smack_bprm_committing_creds - Prepare to install the new credentials
495 * from bprm.
496 *
497 * @bprm: binprm for exec
498 */
499static void smack_bprm_committing_creds(struct linux_binprm *bprm)
500{
501 struct task_smack *bsp = bprm->cred->security;
502
503 if (bsp->smk_task != bsp->smk_forked)
504 current->pdeath_signal = 0;
505}
506
507/**
508 * smack_bprm_secureexec - Return the decision to use secureexec.
509 * @bprm: binprm for exec
510 *
511 * Returns 0 on success.
512 */
513static int smack_bprm_secureexec(struct linux_binprm *bprm)
514{
515 struct task_smack *tsp = current_security();
516 int ret = cap_bprm_secureexec(bprm);
517
518 if (!ret && (tsp->smk_task != tsp->smk_forked))
519 ret = 1;
520
521 return ret;
522}
523
Casey Schaufler676dac42010-12-02 06:43:39 -0800524/*
Casey Schauflere114e472008-02-04 22:29:50 -0800525 * Inode hooks
526 */
527
528/**
529 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800530 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800531 *
532 * Returns 0 if it gets a blob, -ENOMEM otherwise
533 */
534static int smack_inode_alloc_security(struct inode *inode)
535{
Casey Schaufler676dac42010-12-02 06:43:39 -0800536 inode->i_security = new_inode_smack(smk_of_current());
Casey Schauflere114e472008-02-04 22:29:50 -0800537 if (inode->i_security == NULL)
538 return -ENOMEM;
539 return 0;
540}
541
542/**
543 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800544 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800545 *
546 * Clears the blob pointer in inode
547 */
548static void smack_inode_free_security(struct inode *inode)
549{
550 kfree(inode->i_security);
551 inode->i_security = NULL;
552}
553
554/**
555 * smack_inode_init_security - copy out the smack from an inode
556 * @inode: the inode
557 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500558 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800559 * @name: where to put the attribute name
560 * @value: where to put the attribute value
561 * @len: where to put the length of the attribute
562 *
563 * Returns 0 if it all works out, -ENOMEM if there's no memory
564 */
565static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -0500566 const struct qstr *qstr, char **name,
567 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800568{
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700569 struct smack_known *skp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700570 struct inode_smack *issp = inode->i_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700571 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800572 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200573 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800574 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800575
576 if (name) {
Tetsuo Handaceffec552012-03-29 16:19:05 +0900577 *name = kstrdup(XATTR_SMACK_SUFFIX, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800578 if (*name == NULL)
579 return -ENOMEM;
580 }
581
582 if (value) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700583 skp = smk_find_entry(csp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800584 rcu_read_lock();
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700585 may = smk_access_entry(csp, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800586 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200587
588 /*
589 * If the access rule allows transmutation and
590 * the directory requests transmutation then
591 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700592 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200593 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800594 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700595 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200596 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700597 issp->smk_flags |= SMK_INODE_CHANGED;
598 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200599
Tetsuo Handaceffec552012-03-29 16:19:05 +0900600 *value = kstrdup(isp, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800601 if (*value == NULL)
602 return -ENOMEM;
603 }
604
605 if (len)
606 *len = strlen(isp) + 1;
607
608 return 0;
609}
610
611/**
612 * smack_inode_link - Smack check on link
613 * @old_dentry: the existing object
614 * @dir: unused
615 * @new_dentry: the new object
616 *
617 * Returns 0 if access is permitted, an error code otherwise
618 */
619static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
620 struct dentry *new_dentry)
621{
Casey Schauflere114e472008-02-04 22:29:50 -0800622 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200623 struct smk_audit_info ad;
624 int rc;
625
Eric Parisa2694342011-04-25 13:10:27 -0400626 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200627 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800628
629 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200630 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800631
632 if (rc == 0 && new_dentry->d_inode != NULL) {
633 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200634 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
635 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800636 }
637
638 return rc;
639}
640
641/**
642 * smack_inode_unlink - Smack check on inode deletion
643 * @dir: containing directory object
644 * @dentry: file to unlink
645 *
646 * Returns 0 if current can write the containing directory
647 * and the object, error code otherwise
648 */
649static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
650{
651 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200652 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800653 int rc;
654
Eric Parisa2694342011-04-25 13:10:27 -0400655 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200656 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
657
Casey Schauflere114e472008-02-04 22:29:50 -0800658 /*
659 * You need write access to the thing you're unlinking
660 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200661 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
662 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800663 /*
664 * You also need write access to the containing directory
665 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400666 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200667 smk_ad_setfield_u_fs_inode(&ad, dir);
668 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
669 }
Casey Schauflere114e472008-02-04 22:29:50 -0800670 return rc;
671}
672
673/**
674 * smack_inode_rmdir - Smack check on directory deletion
675 * @dir: containing directory object
676 * @dentry: directory to unlink
677 *
678 * Returns 0 if current can write the containing directory
679 * and the directory, error code otherwise
680 */
681static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
682{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200683 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800684 int rc;
685
Eric Parisa2694342011-04-25 13:10:27 -0400686 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200687 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
688
Casey Schauflere114e472008-02-04 22:29:50 -0800689 /*
690 * You need write access to the thing you're removing
691 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200692 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
693 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800694 /*
695 * You also need write access to the containing directory
696 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400697 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200698 smk_ad_setfield_u_fs_inode(&ad, dir);
699 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
700 }
Casey Schauflere114e472008-02-04 22:29:50 -0800701
702 return rc;
703}
704
705/**
706 * smack_inode_rename - Smack check on rename
707 * @old_inode: the old directory
708 * @old_dentry: unused
709 * @new_inode: the new directory
710 * @new_dentry: unused
711 *
712 * Read and write access is required on both the old and
713 * new directories.
714 *
715 * Returns 0 if access is permitted, an error code otherwise
716 */
717static int smack_inode_rename(struct inode *old_inode,
718 struct dentry *old_dentry,
719 struct inode *new_inode,
720 struct dentry *new_dentry)
721{
722 int rc;
723 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200724 struct smk_audit_info ad;
725
Eric Parisa2694342011-04-25 13:10:27 -0400726 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200727 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800728
729 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200730 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800731
732 if (rc == 0 && new_dentry->d_inode != NULL) {
733 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200734 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
735 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800736 }
Casey Schauflere114e472008-02-04 22:29:50 -0800737 return rc;
738}
739
740/**
741 * smack_inode_permission - Smack version of permission()
742 * @inode: the inode in question
743 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800744 *
745 * This is the important Smack hook.
746 *
747 * Returns 0 if access is permitted, -EACCES otherwise
748 */
Al Viroe74f71e2011-06-20 19:38:15 -0400749static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800750{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200751 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400752 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400753
754 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800755 /*
756 * No permission to check. Existence test. Yup, it's there.
757 */
758 if (mask == 0)
759 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700760
761 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400762 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700763 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400764 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200765 smk_ad_setfield_u_fs_inode(&ad, inode);
766 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800767}
768
769/**
770 * smack_inode_setattr - Smack check for setting attributes
771 * @dentry: the object
772 * @iattr: for the force flag
773 *
774 * Returns 0 if access is permitted, an error code otherwise
775 */
776static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
777{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200778 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800779 /*
780 * Need to allow for clearing the setuid bit.
781 */
782 if (iattr->ia_valid & ATTR_FORCE)
783 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400784 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200785 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800786
Etienne Bassetecfcc532009-04-08 20:40:06 +0200787 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800788}
789
790/**
791 * smack_inode_getattr - Smack check for getting attributes
792 * @mnt: unused
793 * @dentry: the object
794 *
795 * Returns 0 if access is permitted, an error code otherwise
796 */
797static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
798{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200799 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400800 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200801
Eric Parisa2694342011-04-25 13:10:27 -0400802 path.dentry = dentry;
803 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200804
Eric Parisf48b7392011-04-25 12:54:27 -0400805 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400806 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200807 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800808}
809
810/**
811 * smack_inode_setxattr - Smack check for setting xattrs
812 * @dentry: the object
813 * @name: name of the attribute
814 * @value: unused
815 * @size: unused
816 * @flags: unused
817 *
818 * This protects the Smack attribute explicitly.
819 *
820 * Returns 0 if access is permitted, an error code otherwise
821 */
David Howells8f0cfa52008-04-29 00:59:41 -0700822static int smack_inode_setxattr(struct dentry *dentry, const char *name,
823 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800824{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200825 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800826 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800827
Casey Schauflerbcdca222008-02-23 15:24:04 -0800828 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
829 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800830 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800831 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
832 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700833 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800834 rc = -EPERM;
Etienne Bassetdefc4332009-04-16 23:58:42 +0200835 /*
836 * check label validity here so import wont fail on
837 * post_setxattr
838 */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700839 if (size == 0 || size >= SMK_LONGLABEL ||
Etienne Bassetdefc4332009-04-16 23:58:42 +0200840 smk_import(value, size) == NULL)
Etienne Basset43031542009-03-27 17:11:01 -0400841 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200842 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700843 if (!smack_privileged(CAP_MAC_ADMIN))
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200844 rc = -EPERM;
845 if (size != TRANS_TRUE_SIZE ||
846 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
847 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800848 } else
849 rc = cap_inode_setxattr(dentry, name, value, size, flags);
850
Eric Parisa2694342011-04-25 13:10:27 -0400851 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200852 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
853
Casey Schauflerbcdca222008-02-23 15:24:04 -0800854 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200855 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800856
857 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800858}
859
860/**
861 * smack_inode_post_setxattr - Apply the Smack update approved above
862 * @dentry: object
863 * @name: attribute name
864 * @value: attribute value
865 * @size: attribute size
866 * @flags: unused
867 *
868 * Set the pointer in the inode blob to the entry found
869 * in the master label list.
870 */
David Howells8f0cfa52008-04-29 00:59:41 -0700871static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
872 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800873{
Casey Schauflere114e472008-02-04 22:29:50 -0800874 char *nsp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200875 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800876
877 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200878 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800879 if (nsp != NULL)
880 isp->smk_inode = nsp;
881 else
882 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200883 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
884 nsp = smk_import(value, size);
Casey Schaufler676dac42010-12-02 06:43:39 -0800885 if (nsp != NULL)
886 isp->smk_task = nsp;
887 else
888 isp->smk_task = smack_known_invalid.smk_known;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800889 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
890 nsp = smk_import(value, size);
891 if (nsp != NULL)
892 isp->smk_mmap = nsp;
893 else
894 isp->smk_mmap = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200895 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
896 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflere114e472008-02-04 22:29:50 -0800897
898 return;
899}
900
Casey Schauflerce8a4322011-09-29 18:21:01 -0700901/**
Casey Schauflere114e472008-02-04 22:29:50 -0800902 * smack_inode_getxattr - Smack check on getxattr
903 * @dentry: the object
904 * @name: unused
905 *
906 * Returns 0 if access is permitted, an error code otherwise
907 */
David Howells8f0cfa52008-04-29 00:59:41 -0700908static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800909{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200910 struct smk_audit_info ad;
911
Eric Parisa2694342011-04-25 13:10:27 -0400912 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200913 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
914
915 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800916}
917
Casey Schauflerce8a4322011-09-29 18:21:01 -0700918/**
Casey Schauflere114e472008-02-04 22:29:50 -0800919 * smack_inode_removexattr - Smack check on removexattr
920 * @dentry: the object
921 * @name: name of the attribute
922 *
923 * Removing the Smack attribute requires CAP_MAC_ADMIN
924 *
925 * Returns 0 if access is permitted, an error code otherwise
926 */
David Howells8f0cfa52008-04-29 00:59:41 -0700927static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800928{
Casey Schaufler676dac42010-12-02 06:43:39 -0800929 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200930 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800931 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800932
Casey Schauflerbcdca222008-02-23 15:24:04 -0800933 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
934 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -0800935 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200936 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800937 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
938 strcmp(name, XATTR_NAME_SMACKMMAP)) {
Casey Schaufler1880eff2012-06-05 15:28:30 -0700939 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -0800940 rc = -EPERM;
941 } else
942 rc = cap_inode_removexattr(dentry, name);
943
Eric Parisa2694342011-04-25 13:10:27 -0400944 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200945 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800946 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200947 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800948
Casey Schaufler676dac42010-12-02 06:43:39 -0800949 if (rc == 0) {
950 isp = dentry->d_inode->i_security;
951 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800952 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -0800953 }
954
Casey Schauflerbcdca222008-02-23 15:24:04 -0800955 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800956}
957
958/**
959 * smack_inode_getsecurity - get smack xattrs
960 * @inode: the object
961 * @name: attribute name
962 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -0800963 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800964 *
965 * Returns the size of the attribute or an error code
966 */
967static int smack_inode_getsecurity(const struct inode *inode,
968 const char *name, void **buffer,
969 bool alloc)
970{
971 struct socket_smack *ssp;
972 struct socket *sock;
973 struct super_block *sbp;
974 struct inode *ip = (struct inode *)inode;
975 char *isp;
976 int ilen;
977 int rc = 0;
978
979 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
980 isp = smk_of_inode(inode);
981 ilen = strlen(isp) + 1;
982 *buffer = isp;
983 return ilen;
984 }
985
986 /*
987 * The rest of the Smack xattrs are only on sockets.
988 */
989 sbp = ip->i_sb;
990 if (sbp->s_magic != SOCKFS_MAGIC)
991 return -EOPNOTSUPP;
992
993 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -0800994 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800995 return -EOPNOTSUPP;
996
997 ssp = sock->sk->sk_security;
998
999 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1000 isp = ssp->smk_in;
1001 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1002 isp = ssp->smk_out;
1003 else
1004 return -EOPNOTSUPP;
1005
1006 ilen = strlen(isp) + 1;
1007 if (rc == 0) {
1008 *buffer = isp;
1009 rc = ilen;
1010 }
1011
1012 return rc;
1013}
1014
1015
1016/**
1017 * smack_inode_listsecurity - list the Smack attributes
1018 * @inode: the object
1019 * @buffer: where they go
1020 * @buffer_size: size of buffer
1021 *
1022 * Returns 0 on success, -EINVAL otherwise
1023 */
1024static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1025 size_t buffer_size)
1026{
1027 int len = strlen(XATTR_NAME_SMACK);
1028
1029 if (buffer != NULL && len <= buffer_size) {
1030 memcpy(buffer, XATTR_NAME_SMACK, len);
1031 return len;
1032 }
1033 return -EINVAL;
1034}
1035
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001036/**
1037 * smack_inode_getsecid - Extract inode's security id
1038 * @inode: inode to extract the info from
1039 * @secid: where result will be saved
1040 */
1041static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1042{
1043 struct inode_smack *isp = inode->i_security;
1044
1045 *secid = smack_to_secid(isp->smk_inode);
1046}
1047
Casey Schauflere114e472008-02-04 22:29:50 -08001048/*
1049 * File Hooks
1050 */
1051
1052/**
1053 * smack_file_permission - Smack check on file operations
1054 * @file: unused
1055 * @mask: unused
1056 *
1057 * Returns 0
1058 *
1059 * Should access checks be done on each read or write?
1060 * UNICOS and SELinux say yes.
1061 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1062 *
1063 * I'll say no for now. Smack does not do the frequent
1064 * label changing that SELinux does.
1065 */
1066static int smack_file_permission(struct file *file, int mask)
1067{
1068 return 0;
1069}
1070
1071/**
1072 * smack_file_alloc_security - assign a file security blob
1073 * @file: the object
1074 *
1075 * The security blob for a file is a pointer to the master
1076 * label list, so no allocation is done.
1077 *
1078 * Returns 0
1079 */
1080static int smack_file_alloc_security(struct file *file)
1081{
Casey Schaufler676dac42010-12-02 06:43:39 -08001082 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001083 return 0;
1084}
1085
1086/**
1087 * smack_file_free_security - clear a file security blob
1088 * @file: the object
1089 *
1090 * The security blob for a file is a pointer to the master
1091 * label list, so no memory is freed.
1092 */
1093static void smack_file_free_security(struct file *file)
1094{
1095 file->f_security = NULL;
1096}
1097
1098/**
1099 * smack_file_ioctl - Smack check on ioctls
1100 * @file: the object
1101 * @cmd: what to do
1102 * @arg: unused
1103 *
1104 * Relies heavily on the correct use of the ioctl command conventions.
1105 *
1106 * Returns 0 if allowed, error code otherwise
1107 */
1108static int smack_file_ioctl(struct file *file, unsigned int cmd,
1109 unsigned long arg)
1110{
1111 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001112 struct smk_audit_info ad;
1113
Eric Parisf48b7392011-04-25 12:54:27 -04001114 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001115 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001116
1117 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001119
1120 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001121 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001122
1123 return rc;
1124}
1125
1126/**
1127 * smack_file_lock - Smack check on file locking
1128 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001129 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001130 *
1131 * Returns 0 if current has write access, error code otherwise
1132 */
1133static int smack_file_lock(struct file *file, unsigned int cmd)
1134{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001135 struct smk_audit_info ad;
1136
Eric Paris92f42502011-04-25 13:15:55 -04001137 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1138 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001139 return smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001140}
1141
1142/**
1143 * smack_file_fcntl - Smack check on fcntl
1144 * @file: the object
1145 * @cmd: what action to check
1146 * @arg: unused
1147 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001148 * Generally these operations are harmless.
1149 * File locking operations present an obvious mechanism
1150 * for passing information, so they require write access.
1151 *
Casey Schauflere114e472008-02-04 22:29:50 -08001152 * Returns 0 if current has access, error code otherwise
1153 */
1154static int smack_file_fcntl(struct file *file, unsigned int cmd,
1155 unsigned long arg)
1156{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001157 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001158 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001159
Etienne Bassetecfcc532009-04-08 20:40:06 +02001160
Casey Schauflere114e472008-02-04 22:29:50 -08001161 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001162 case F_GETLK:
Casey Schauflere114e472008-02-04 22:29:50 -08001163 case F_SETLK:
1164 case F_SETLKW:
1165 case F_SETOWN:
1166 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001167 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1168 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001169 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001170 break;
1171 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001172 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001173 }
1174
1175 return rc;
1176}
1177
1178/**
Al Viroe5467852012-05-30 13:30:51 -04001179 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001180 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1181 * if mapping anonymous memory.
1182 * @file contains the file structure for file to map (may be NULL).
1183 * @reqprot contains the protection requested by the application.
1184 * @prot contains the protection that will be applied by the kernel.
1185 * @flags contains the operational flags.
1186 * Return 0 if permission is granted.
1187 */
Al Viroe5467852012-05-30 13:30:51 -04001188static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001189 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001190 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001191{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001192 struct smack_known *skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001193 struct smack_rule *srp;
1194 struct task_smack *tsp;
1195 char *sp;
1196 char *msmack;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001197 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001198 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001199 int may;
1200 int mmay;
1201 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001202 int rc;
1203
Al Viro496ad9a2013-01-23 17:07:38 -05001204 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001205 return 0;
1206
Al Viro496ad9a2013-01-23 17:07:38 -05001207 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001208 if (isp->smk_mmap == NULL)
1209 return 0;
1210 msmack = isp->smk_mmap;
1211
1212 tsp = current_security();
1213 sp = smk_of_current();
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001214 skp = smk_find_entry(sp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001215 rc = 0;
1216
1217 rcu_read_lock();
1218 /*
1219 * For each Smack rule associated with the subject
1220 * label verify that the SMACK64MMAP also has access
1221 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001222 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001223 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001224 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001225 /*
1226 * Matching labels always allows access.
1227 */
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001228 if (msmack == osmack)
1229 continue;
1230 /*
1231 * If there is a matching local rule take
1232 * that into account as well.
1233 */
1234 may = smk_access_entry(srp->smk_subject, osmack,
1235 &tsp->smk_rules);
1236 if (may == -ENOENT)
1237 may = srp->smk_access;
1238 else
1239 may &= srp->smk_access;
1240 /*
1241 * If may is zero the SMACK64MMAP subject can't
1242 * possibly have less access.
1243 */
1244 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001245 continue;
1246
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001247 /*
1248 * Fetch the global list entry.
1249 * If there isn't one a SMACK64MMAP subject
1250 * can't have as much access as current.
1251 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001252 skp = smk_find_entry(msmack);
1253 mmay = smk_access_entry(msmack, osmack, &skp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001254 if (mmay == -ENOENT) {
1255 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001256 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001257 }
1258 /*
1259 * If there is a local entry it modifies the
1260 * potential access, too.
1261 */
1262 tmay = smk_access_entry(msmack, osmack, &tsp->smk_rules);
1263 if (tmay != -ENOENT)
1264 mmay &= tmay;
1265
1266 /*
1267 * If there is any access available to current that is
1268 * not available to a SMACK64MMAP subject
1269 * deny access.
1270 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001271 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001272 rc = -EACCES;
1273 break;
1274 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001275 }
1276
1277 rcu_read_unlock();
1278
1279 return rc;
1280}
1281
1282/**
Casey Schauflere114e472008-02-04 22:29:50 -08001283 * smack_file_set_fowner - set the file security blob value
1284 * @file: object in question
1285 *
1286 * Returns 0
1287 * Further research may be required on this one.
1288 */
1289static int smack_file_set_fowner(struct file *file)
1290{
Casey Schaufler676dac42010-12-02 06:43:39 -08001291 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001292 return 0;
1293}
1294
1295/**
1296 * smack_file_send_sigiotask - Smack on sigio
1297 * @tsk: The target task
1298 * @fown: the object the signal come from
1299 * @signum: unused
1300 *
1301 * Allow a privileged task to get signals even if it shouldn't
1302 *
1303 * Returns 0 if a subject with the object's smack could
1304 * write to the task, an error code otherwise.
1305 */
1306static int smack_file_send_sigiotask(struct task_struct *tsk,
1307 struct fown_struct *fown, int signum)
1308{
1309 struct file *file;
1310 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001311 char *tsp = smk_of_task(tsk->cred->security);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001312 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001313
1314 /*
1315 * struct fown_struct is never outside the context of a struct file
1316 */
1317 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001318
Etienne Bassetecfcc532009-04-08 20:40:06 +02001319 /* we don't log here as rc can be overriden */
1320 rc = smk_access(file->f_security, tsp, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001321 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001322 rc = 0;
1323
1324 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1325 smk_ad_setfield_u_tsk(&ad, tsk);
1326 smack_log(file->f_security, tsp, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001327 return rc;
1328}
1329
1330/**
1331 * smack_file_receive - Smack file receive check
1332 * @file: the object
1333 *
1334 * Returns 0 if current has access, error code otherwise
1335 */
1336static int smack_file_receive(struct file *file)
1337{
1338 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001339 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001340
Etienne Bassetecfcc532009-04-08 20:40:06 +02001341 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1342 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001343 /*
1344 * This code relies on bitmasks.
1345 */
1346 if (file->f_mode & FMODE_READ)
1347 may = MAY_READ;
1348 if (file->f_mode & FMODE_WRITE)
1349 may |= MAY_WRITE;
1350
Etienne Bassetecfcc532009-04-08 20:40:06 +02001351 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001352}
1353
Casey Schaufler531f1d42011-09-19 12:41:42 -07001354/**
Eric Paris83d49852012-04-04 13:45:40 -04001355 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001356 * @file: the object
1357 * @cred: unused
1358 *
1359 * Set the security blob in the file structure.
1360 *
1361 * Returns 0
1362 */
Eric Paris83d49852012-04-04 13:45:40 -04001363static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001364{
Al Viro496ad9a2013-01-23 17:07:38 -05001365 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001366
1367 file->f_security = isp->smk_inode;
1368
1369 return 0;
1370}
1371
Casey Schauflere114e472008-02-04 22:29:50 -08001372/*
1373 * Task hooks
1374 */
1375
1376/**
David Howellsee18d642009-09-02 09:14:21 +01001377 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1378 * @new: the new credentials
1379 * @gfp: the atomicity of any memory allocations
1380 *
1381 * Prepare a blank set of credentials for modification. This must allocate all
1382 * the memory the LSM module might require such that cred_transfer() can
1383 * complete without error.
1384 */
1385static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1386{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001387 struct task_smack *tsp;
1388
1389 tsp = new_task_smack(NULL, NULL, gfp);
1390 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001391 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001392
1393 cred->security = tsp;
1394
David Howellsee18d642009-09-02 09:14:21 +01001395 return 0;
1396}
1397
1398
1399/**
David Howellsf1752ee2008-11-14 10:39:17 +11001400 * smack_cred_free - "free" task-level security credentials
1401 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001402 *
Casey Schauflere114e472008-02-04 22:29:50 -08001403 */
David Howellsf1752ee2008-11-14 10:39:17 +11001404static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001405{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001406 struct task_smack *tsp = cred->security;
1407 struct smack_rule *rp;
1408 struct list_head *l;
1409 struct list_head *n;
1410
1411 if (tsp == NULL)
1412 return;
1413 cred->security = NULL;
1414
1415 list_for_each_safe(l, n, &tsp->smk_rules) {
1416 rp = list_entry(l, struct smack_rule, list);
1417 list_del(&rp->list);
1418 kfree(rp);
1419 }
1420 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001421}
1422
1423/**
David Howellsd84f4f92008-11-14 10:39:23 +11001424 * smack_cred_prepare - prepare new set of credentials for modification
1425 * @new: the new credentials
1426 * @old: the original credentials
1427 * @gfp: the atomicity of any memory allocations
1428 *
1429 * Prepare a new set of credentials for modification.
1430 */
1431static int smack_cred_prepare(struct cred *new, const struct cred *old,
1432 gfp_t gfp)
1433{
Casey Schaufler676dac42010-12-02 06:43:39 -08001434 struct task_smack *old_tsp = old->security;
1435 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001436 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001437
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001438 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001439 if (new_tsp == NULL)
1440 return -ENOMEM;
1441
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001442 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1443 if (rc != 0)
1444 return rc;
1445
Casey Schaufler676dac42010-12-02 06:43:39 -08001446 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001447 return 0;
1448}
1449
Randy Dunlap251a2a92009-02-18 11:42:33 -08001450/**
David Howellsee18d642009-09-02 09:14:21 +01001451 * smack_cred_transfer - Transfer the old credentials to the new credentials
1452 * @new: the new credentials
1453 * @old: the original credentials
1454 *
1455 * Fill in a set of blank credentials from another set of credentials.
1456 */
1457static void smack_cred_transfer(struct cred *new, const struct cred *old)
1458{
Casey Schaufler676dac42010-12-02 06:43:39 -08001459 struct task_smack *old_tsp = old->security;
1460 struct task_smack *new_tsp = new->security;
1461
1462 new_tsp->smk_task = old_tsp->smk_task;
1463 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001464 mutex_init(&new_tsp->smk_rules_lock);
1465 INIT_LIST_HEAD(&new_tsp->smk_rules);
1466
1467
1468 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001469}
1470
1471/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001472 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001473 * @new: points to the set of credentials to be modified.
1474 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001475 *
1476 * Set the security data for a kernel service.
1477 */
1478static int smack_kernel_act_as(struct cred *new, u32 secid)
1479{
Casey Schaufler676dac42010-12-02 06:43:39 -08001480 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001481 char *smack = smack_from_secid(secid);
1482
1483 if (smack == NULL)
1484 return -EINVAL;
1485
Casey Schaufler676dac42010-12-02 06:43:39 -08001486 new_tsp->smk_task = smack;
David Howells3a3b7ce2008-11-14 10:39:28 +11001487 return 0;
1488}
1489
1490/**
1491 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001492 * @new: points to the set of credentials to be modified
1493 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001494 *
1495 * Set the file creation context in a set of credentials to the same
1496 * as the objective context of the specified inode
1497 */
1498static int smack_kernel_create_files_as(struct cred *new,
1499 struct inode *inode)
1500{
1501 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001502 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001503
Casey Schaufler676dac42010-12-02 06:43:39 -08001504 tsp->smk_forked = isp->smk_inode;
1505 tsp->smk_task = isp->smk_inode;
David Howells3a3b7ce2008-11-14 10:39:28 +11001506 return 0;
1507}
1508
1509/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001510 * smk_curacc_on_task - helper to log task related access
1511 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001512 * @access: the access requested
1513 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001514 *
1515 * Return 0 if access is permitted
1516 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001517static int smk_curacc_on_task(struct task_struct *p, int access,
1518 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001519{
1520 struct smk_audit_info ad;
1521
Casey Schaufler531f1d42011-09-19 12:41:42 -07001522 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001523 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler676dac42010-12-02 06:43:39 -08001524 return smk_curacc(smk_of_task(task_security(p)), access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001525}
1526
1527/**
Casey Schauflere114e472008-02-04 22:29:50 -08001528 * smack_task_setpgid - Smack check on setting pgid
1529 * @p: the task object
1530 * @pgid: unused
1531 *
1532 * Return 0 if write access is permitted
1533 */
1534static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1535{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001536 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001537}
1538
1539/**
1540 * smack_task_getpgid - Smack access check for getpgid
1541 * @p: the object task
1542 *
1543 * Returns 0 if current can read the object task, error code otherwise
1544 */
1545static int smack_task_getpgid(struct task_struct *p)
1546{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001547 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001548}
1549
1550/**
1551 * smack_task_getsid - Smack access check for getsid
1552 * @p: the object task
1553 *
1554 * Returns 0 if current can read the object task, error code otherwise
1555 */
1556static int smack_task_getsid(struct task_struct *p)
1557{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001558 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001559}
1560
1561/**
1562 * smack_task_getsecid - get the secid of the task
1563 * @p: the object task
1564 * @secid: where to put the result
1565 *
1566 * Sets the secid to contain a u32 version of the smack label.
1567 */
1568static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1569{
Casey Schaufler676dac42010-12-02 06:43:39 -08001570 *secid = smack_to_secid(smk_of_task(task_security(p)));
Casey Schauflere114e472008-02-04 22:29:50 -08001571}
1572
1573/**
1574 * smack_task_setnice - Smack check on setting nice
1575 * @p: the task object
1576 * @nice: unused
1577 *
1578 * Return 0 if write access is permitted
1579 */
1580static int smack_task_setnice(struct task_struct *p, int nice)
1581{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001582 int rc;
1583
1584 rc = cap_task_setnice(p, nice);
1585 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001586 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001587 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001588}
1589
1590/**
1591 * smack_task_setioprio - Smack check on setting ioprio
1592 * @p: the task object
1593 * @ioprio: unused
1594 *
1595 * Return 0 if write access is permitted
1596 */
1597static int smack_task_setioprio(struct task_struct *p, int ioprio)
1598{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001599 int rc;
1600
1601 rc = cap_task_setioprio(p, ioprio);
1602 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001603 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001604 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001605}
1606
1607/**
1608 * smack_task_getioprio - Smack check on reading ioprio
1609 * @p: the task object
1610 *
1611 * Return 0 if read access is permitted
1612 */
1613static int smack_task_getioprio(struct task_struct *p)
1614{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001615 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001616}
1617
1618/**
1619 * smack_task_setscheduler - Smack check on setting scheduler
1620 * @p: the task object
1621 * @policy: unused
1622 * @lp: unused
1623 *
1624 * Return 0 if read access is permitted
1625 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001626static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001627{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001628 int rc;
1629
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001630 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001631 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_getscheduler - Smack check on reading scheduler
1638 * @p: the task object
1639 *
1640 * Return 0 if read access is permitted
1641 */
1642static int smack_task_getscheduler(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_movememory - Smack check on moving memory
1649 * @p: the task object
1650 *
1651 * Return 0 if write access is permitted
1652 */
1653static int smack_task_movememory(struct task_struct *p)
1654{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001655 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001656}
1657
1658/**
1659 * smack_task_kill - Smack check on signal delivery
1660 * @p: the task object
1661 * @info: unused
1662 * @sig: unused
1663 * @secid: identifies the smack to use in lieu of current's
1664 *
1665 * Return 0 if write access is permitted
1666 *
1667 * The secid behavior is an artifact of an SELinux hack
1668 * in the USB code. Someday it may go away.
1669 */
1670static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1671 int sig, u32 secid)
1672{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001673 struct smk_audit_info ad;
1674
1675 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1676 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001677 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001678 * Sending a signal requires that the sender
1679 * can write the receiver.
1680 */
1681 if (secid == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001682 return smk_curacc(smk_of_task(task_security(p)), MAY_WRITE,
1683 &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001684 /*
1685 * If the secid isn't 0 we're dealing with some USB IO
1686 * specific behavior. This is not clean. For one thing
1687 * we can't take privilege into account.
1688 */
Casey Schaufler676dac42010-12-02 06:43:39 -08001689 return smk_access(smack_from_secid(secid),
1690 smk_of_task(task_security(p)), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001691}
1692
1693/**
1694 * smack_task_wait - Smack access check for waiting
1695 * @p: task to wait for
1696 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001697 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08001698 */
1699static int smack_task_wait(struct task_struct *p)
1700{
Casey Schauflere114e472008-02-04 22:29:50 -08001701 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001702 * Allow the operation to succeed.
1703 * Zombies are bad.
1704 * In userless environments (e.g. phones) programs
1705 * get marked with SMACK64EXEC and even if the parent
1706 * and child shouldn't be talking the parent still
1707 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08001708 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001709 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001710}
1711
1712/**
1713 * smack_task_to_inode - copy task smack into the inode blob
1714 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001715 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001716 *
1717 * Sets the smack pointer in the inode security blob
1718 */
1719static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1720{
1721 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001722 isp->smk_inode = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08001723}
1724
1725/*
1726 * Socket hooks.
1727 */
1728
1729/**
1730 * smack_sk_alloc_security - Allocate a socket blob
1731 * @sk: the socket
1732 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001733 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001734 *
1735 * Assign Smack pointers to current
1736 *
1737 * Returns 0 on success, -ENOMEM is there's no memory
1738 */
1739static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1740{
Casey Schaufler676dac42010-12-02 06:43:39 -08001741 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001742 struct socket_smack *ssp;
1743
1744 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1745 if (ssp == NULL)
1746 return -ENOMEM;
1747
1748 ssp->smk_in = csp;
1749 ssp->smk_out = csp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001750 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001751
1752 sk->sk_security = ssp;
1753
1754 return 0;
1755}
1756
1757/**
1758 * smack_sk_free_security - Free a socket blob
1759 * @sk: the socket
1760 *
1761 * Clears the blob pointer
1762 */
1763static void smack_sk_free_security(struct sock *sk)
1764{
1765 kfree(sk->sk_security);
1766}
1767
1768/**
Paul Moore07feee82009-03-27 17:10:54 -04001769* smack_host_label - check host based restrictions
1770* @sip: the object end
1771*
1772* looks for host based access restrictions
1773*
1774* This version will only be appropriate for really small sets of single label
1775* hosts. The caller is responsible for ensuring that the RCU read lock is
1776* taken before calling this function.
1777*
1778* Returns the label of the far end or NULL if it's not special.
1779*/
1780static char *smack_host_label(struct sockaddr_in *sip)
1781{
1782 struct smk_netlbladdr *snp;
1783 struct in_addr *siap = &sip->sin_addr;
1784
1785 if (siap->s_addr == 0)
1786 return NULL;
1787
1788 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1789 /*
1790 * we break after finding the first match because
1791 * the list is sorted from longest to shortest mask
1792 * so we have found the most specific match
1793 */
1794 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001795 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1796 /* we have found the special CIPSO option */
1797 if (snp->smk_label == smack_cipso_option)
1798 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001799 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001800 }
Paul Moore07feee82009-03-27 17:10:54 -04001801
1802 return NULL;
1803}
1804
1805/**
Casey Schauflere114e472008-02-04 22:29:50 -08001806 * smack_netlabel - Set the secattr on a socket
1807 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001808 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001809 *
1810 * Convert the outbound smack value (smk_out) to a
1811 * secattr and attach it to the socket.
1812 *
1813 * Returns 0 on success or an error code
1814 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001815static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001816{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001817 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001818 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001819 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001820
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001821 /*
1822 * Usually the netlabel code will handle changing the
1823 * packet labeling based on the label.
1824 * The case of a single label host is different, because
1825 * a single label host should never get a labeled packet
1826 * even though the label is usually associated with a packet
1827 * label.
1828 */
1829 local_bh_disable();
1830 bh_lock_sock_nested(sk);
1831
1832 if (ssp->smk_out == smack_net_ambient ||
1833 labeled == SMACK_UNLABELED_SOCKET)
1834 netlbl_sock_delattr(sk);
1835 else {
Casey Schauflerf7112e62012-05-06 15:22:02 -07001836 skp = smk_find_entry(ssp->smk_out);
1837 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001838 }
1839
1840 bh_unlock_sock(sk);
1841 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001842
Casey Schauflere114e472008-02-04 22:29:50 -08001843 return rc;
1844}
1845
1846/**
Paul Moore07feee82009-03-27 17:10:54 -04001847 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1848 * @sk: the socket
1849 * @sap: the destination address
1850 *
1851 * Set the correct secattr for the given socket based on the destination
1852 * address and perform any outbound access checks needed.
1853 *
1854 * Returns 0 on success or an error code.
1855 *
1856 */
1857static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1858{
1859 int rc;
1860 int sk_lbl;
1861 char *hostsp;
1862 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001863 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001864
1865 rcu_read_lock();
1866 hostsp = smack_host_label(sap);
1867 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001868#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001869 struct lsm_network_audit net;
1870
Eric Paris48c62af2012-04-02 13:15:44 -04001871 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1872 ad.a.u.net->family = sap->sin_family;
1873 ad.a.u.net->dport = sap->sin_port;
1874 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001875#endif
Kees Cook923e9a12012-04-10 13:26:44 -07001876 sk_lbl = SMACK_UNLABELED_SOCKET;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001877 rc = smk_access(ssp->smk_out, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001878 } else {
1879 sk_lbl = SMACK_CIPSO_SOCKET;
1880 rc = 0;
1881 }
1882 rcu_read_unlock();
1883 if (rc != 0)
1884 return rc;
1885
1886 return smack_netlabel(sk, sk_lbl);
1887}
1888
1889/**
Casey Schauflerc6739442013-05-22 18:42:56 -07001890 * smk_ipv6_port_label - Smack port access table management
1891 * @sock: socket
1892 * @address: address
1893 *
1894 * Create or update the port list entry
1895 */
1896static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
1897{
1898 struct sock *sk = sock->sk;
1899 struct sockaddr_in6 *addr6;
1900 struct socket_smack *ssp = sock->sk->sk_security;
1901 struct smk_port_label *spp;
1902 unsigned short port = 0;
1903
1904 if (address == NULL) {
1905 /*
1906 * This operation is changing the Smack information
1907 * on the bound socket. Take the changes to the port
1908 * as well.
1909 */
1910 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1911 if (sk != spp->smk_sock)
1912 continue;
1913 spp->smk_in = ssp->smk_in;
1914 spp->smk_out = ssp->smk_out;
1915 return;
1916 }
1917 /*
1918 * A NULL address is only used for updating existing
1919 * bound entries. If there isn't one, it's OK.
1920 */
1921 return;
1922 }
1923
1924 addr6 = (struct sockaddr_in6 *)address;
1925 port = ntohs(addr6->sin6_port);
1926 /*
1927 * This is a special case that is safely ignored.
1928 */
1929 if (port == 0)
1930 return;
1931
1932 /*
1933 * Look for an existing port list entry.
1934 * This is an indication that a port is getting reused.
1935 */
1936 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
1937 if (spp->smk_port != port)
1938 continue;
1939 spp->smk_port = port;
1940 spp->smk_sock = sk;
1941 spp->smk_in = ssp->smk_in;
1942 spp->smk_out = ssp->smk_out;
1943 return;
1944 }
1945
1946 /*
1947 * A new port entry is required.
1948 */
1949 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
1950 if (spp == NULL)
1951 return;
1952
1953 spp->smk_port = port;
1954 spp->smk_sock = sk;
1955 spp->smk_in = ssp->smk_in;
1956 spp->smk_out = ssp->smk_out;
1957
1958 list_add(&spp->list, &smk_ipv6_port_list);
1959 return;
1960}
1961
1962/**
1963 * smk_ipv6_port_check - check Smack port access
1964 * @sock: socket
1965 * @address: address
1966 *
1967 * Create or update the port list entry
1968 */
1969static int smk_ipv6_port_check(struct sock *sk, struct sockaddr *address,
1970 int act)
1971{
1972 __be16 *bep;
1973 __be32 *be32p;
1974 struct sockaddr_in6 *addr6;
1975 struct smk_port_label *spp;
1976 struct socket_smack *ssp = sk->sk_security;
1977 unsigned short port = 0;
1978 char *subject;
1979 char *object;
1980 struct smk_audit_info ad;
1981#ifdef CONFIG_AUDIT
1982 struct lsm_network_audit net;
1983#endif
1984
1985 if (act == SMK_RECEIVING) {
1986 subject = smack_net_ambient;
1987 object = ssp->smk_in;
1988 } else {
1989 subject = ssp->smk_out;
1990 object = smack_net_ambient;
1991 }
1992
1993 /*
1994 * Get the IP address and port from the address.
1995 */
1996 addr6 = (struct sockaddr_in6 *)address;
1997 port = ntohs(addr6->sin6_port);
1998 bep = (__be16 *)(&addr6->sin6_addr);
1999 be32p = (__be32 *)(&addr6->sin6_addr);
2000
2001 /*
2002 * It's remote, so port lookup does no good.
2003 */
2004 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2005 goto auditout;
2006
2007 /*
2008 * It's local so the send check has to have passed.
2009 */
2010 if (act == SMK_RECEIVING) {
2011 subject = smack_known_web.smk_known;
2012 goto auditout;
2013 }
2014
2015 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2016 if (spp->smk_port != port)
2017 continue;
2018 object = spp->smk_in;
2019 if (act == SMK_CONNECTING)
2020 ssp->smk_packet = spp->smk_out;
2021 break;
2022 }
2023
2024auditout:
2025
2026#ifdef CONFIG_AUDIT
2027 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2028 ad.a.u.net->family = sk->sk_family;
2029 ad.a.u.net->dport = port;
2030 if (act == SMK_RECEIVING)
2031 ad.a.u.net->v6info.saddr = addr6->sin6_addr;
2032 else
2033 ad.a.u.net->v6info.daddr = addr6->sin6_addr;
2034#endif
2035 return smk_access(subject, object, MAY_WRITE, &ad);
2036}
2037
2038/**
Casey Schauflere114e472008-02-04 22:29:50 -08002039 * smack_inode_setsecurity - set smack xattrs
2040 * @inode: the object
2041 * @name: attribute name
2042 * @value: attribute value
2043 * @size: size of the attribute
2044 * @flags: unused
2045 *
2046 * Sets the named attribute in the appropriate blob
2047 *
2048 * Returns 0 on success, or an error code
2049 */
2050static int smack_inode_setsecurity(struct inode *inode, const char *name,
2051 const void *value, size_t size, int flags)
2052{
2053 char *sp;
2054 struct inode_smack *nsp = inode->i_security;
2055 struct socket_smack *ssp;
2056 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002057 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002058
Casey Schauflerf7112e62012-05-06 15:22:02 -07002059 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08002060 return -EACCES;
2061
2062 sp = smk_import(value, size);
2063 if (sp == NULL)
2064 return -EINVAL;
2065
2066 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2067 nsp->smk_inode = sp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002068 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002069 return 0;
2070 }
2071 /*
2072 * The rest of the Smack xattrs are only on sockets.
2073 */
2074 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2075 return -EOPNOTSUPP;
2076
2077 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002078 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002079 return -EOPNOTSUPP;
2080
2081 ssp = sock->sk->sk_security;
2082
2083 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2084 ssp->smk_in = sp;
2085 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2086 ssp->smk_out = sp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002087 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002088 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2089 if (rc != 0)
2090 printk(KERN_WARNING
2091 "Smack: \"%s\" netlbl error %d.\n",
2092 __func__, -rc);
2093 }
Casey Schauflere114e472008-02-04 22:29:50 -08002094 } else
2095 return -EOPNOTSUPP;
2096
Casey Schauflerc6739442013-05-22 18:42:56 -07002097 if (sock->sk->sk_family == PF_INET6)
2098 smk_ipv6_port_label(sock, NULL);
2099
Casey Schauflere114e472008-02-04 22:29:50 -08002100 return 0;
2101}
2102
2103/**
2104 * smack_socket_post_create - finish socket setup
2105 * @sock: the socket
2106 * @family: protocol family
2107 * @type: unused
2108 * @protocol: unused
2109 * @kern: unused
2110 *
2111 * Sets the netlabel information on the socket
2112 *
2113 * Returns 0 on success, and error code otherwise
2114 */
2115static int smack_socket_post_create(struct socket *sock, int family,
2116 int type, int protocol, int kern)
2117{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002118 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002119 return 0;
2120 /*
2121 * Set the outbound netlbl.
2122 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002123 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2124}
2125
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002126/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002127 * smack_socket_bind - record port binding information.
2128 * @sock: the socket
2129 * @address: the port address
2130 * @addrlen: size of the address
2131 *
2132 * Records the label bound to a port.
2133 *
2134 * Returns 0
2135 */
2136static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2137 int addrlen)
2138{
2139 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2140 smk_ipv6_port_label(sock, address);
2141
2142 return 0;
2143}
2144
2145/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002146 * smack_socket_connect - connect access check
2147 * @sock: the socket
2148 * @sap: the other end
2149 * @addrlen: size of sap
2150 *
2151 * Verifies that a connection may be possible
2152 *
2153 * Returns 0 on success, and error code otherwise
2154 */
2155static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2156 int addrlen)
2157{
Casey Schauflerc6739442013-05-22 18:42:56 -07002158 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002159
Casey Schauflerc6739442013-05-22 18:42:56 -07002160 if (sock->sk == NULL)
2161 return 0;
2162
2163 switch (sock->sk->sk_family) {
2164 case PF_INET:
2165 if (addrlen < sizeof(struct sockaddr_in))
2166 return -EINVAL;
2167 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2168 break;
2169 case PF_INET6:
2170 if (addrlen < sizeof(struct sockaddr_in6))
2171 return -EINVAL;
2172 rc = smk_ipv6_port_check(sock->sk, sap, SMK_CONNECTING);
2173 break;
2174 }
2175 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002176}
2177
2178/**
2179 * smack_flags_to_may - convert S_ to MAY_ values
2180 * @flags: the S_ value
2181 *
2182 * Returns the equivalent MAY_ value
2183 */
2184static int smack_flags_to_may(int flags)
2185{
2186 int may = 0;
2187
2188 if (flags & S_IRUGO)
2189 may |= MAY_READ;
2190 if (flags & S_IWUGO)
2191 may |= MAY_WRITE;
2192 if (flags & S_IXUGO)
2193 may |= MAY_EXEC;
2194
2195 return may;
2196}
2197
2198/**
2199 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2200 * @msg: the object
2201 *
2202 * Returns 0
2203 */
2204static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2205{
Casey Schaufler676dac42010-12-02 06:43:39 -08002206 msg->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002207 return 0;
2208}
2209
2210/**
2211 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2212 * @msg: the object
2213 *
2214 * Clears the blob pointer
2215 */
2216static void smack_msg_msg_free_security(struct msg_msg *msg)
2217{
2218 msg->security = NULL;
2219}
2220
2221/**
2222 * smack_of_shm - the smack pointer for the shm
2223 * @shp: the object
2224 *
2225 * Returns a pointer to the smack value
2226 */
2227static char *smack_of_shm(struct shmid_kernel *shp)
2228{
2229 return (char *)shp->shm_perm.security;
2230}
2231
2232/**
2233 * smack_shm_alloc_security - Set the security blob for shm
2234 * @shp: the object
2235 *
2236 * Returns 0
2237 */
2238static int smack_shm_alloc_security(struct shmid_kernel *shp)
2239{
2240 struct kern_ipc_perm *isp = &shp->shm_perm;
2241
Casey Schaufler676dac42010-12-02 06:43:39 -08002242 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002243 return 0;
2244}
2245
2246/**
2247 * smack_shm_free_security - Clear the security blob for shm
2248 * @shp: the object
2249 *
2250 * Clears the blob pointer
2251 */
2252static void smack_shm_free_security(struct shmid_kernel *shp)
2253{
2254 struct kern_ipc_perm *isp = &shp->shm_perm;
2255
2256 isp->security = NULL;
2257}
2258
2259/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002260 * smk_curacc_shm : check if current has access on shm
2261 * @shp : the object
2262 * @access : access requested
2263 *
2264 * Returns 0 if current has the requested access, error code otherwise
2265 */
2266static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2267{
2268 char *ssp = smack_of_shm(shp);
2269 struct smk_audit_info ad;
2270
2271#ifdef CONFIG_AUDIT
2272 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2273 ad.a.u.ipc_id = shp->shm_perm.id;
2274#endif
2275 return smk_curacc(ssp, access, &ad);
2276}
2277
2278/**
Casey Schauflere114e472008-02-04 22:29:50 -08002279 * smack_shm_associate - Smack access check for shm
2280 * @shp: the object
2281 * @shmflg: access requested
2282 *
2283 * Returns 0 if current has the requested access, error code otherwise
2284 */
2285static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2286{
Casey Schauflere114e472008-02-04 22:29:50 -08002287 int may;
2288
2289 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002290 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002291}
2292
2293/**
2294 * smack_shm_shmctl - Smack access check for shm
2295 * @shp: the object
2296 * @cmd: what it wants to do
2297 *
2298 * Returns 0 if current has the requested access, error code otherwise
2299 */
2300static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2301{
Casey Schauflere114e472008-02-04 22:29:50 -08002302 int may;
2303
2304 switch (cmd) {
2305 case IPC_STAT:
2306 case SHM_STAT:
2307 may = MAY_READ;
2308 break;
2309 case IPC_SET:
2310 case SHM_LOCK:
2311 case SHM_UNLOCK:
2312 case IPC_RMID:
2313 may = MAY_READWRITE;
2314 break;
2315 case IPC_INFO:
2316 case SHM_INFO:
2317 /*
2318 * System level information.
2319 */
2320 return 0;
2321 default:
2322 return -EINVAL;
2323 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002324 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002325}
2326
2327/**
2328 * smack_shm_shmat - Smack access for shmat
2329 * @shp: the object
2330 * @shmaddr: unused
2331 * @shmflg: access requested
2332 *
2333 * Returns 0 if current has the requested access, error code otherwise
2334 */
2335static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2336 int shmflg)
2337{
Casey Schauflere114e472008-02-04 22:29:50 -08002338 int may;
2339
2340 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002341 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002342}
2343
2344/**
2345 * smack_of_sem - the smack pointer for the sem
2346 * @sma: the object
2347 *
2348 * Returns a pointer to the smack value
2349 */
2350static char *smack_of_sem(struct sem_array *sma)
2351{
2352 return (char *)sma->sem_perm.security;
2353}
2354
2355/**
2356 * smack_sem_alloc_security - Set the security blob for sem
2357 * @sma: the object
2358 *
2359 * Returns 0
2360 */
2361static int smack_sem_alloc_security(struct sem_array *sma)
2362{
2363 struct kern_ipc_perm *isp = &sma->sem_perm;
2364
Casey Schaufler676dac42010-12-02 06:43:39 -08002365 isp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002366 return 0;
2367}
2368
2369/**
2370 * smack_sem_free_security - Clear the security blob for sem
2371 * @sma: the object
2372 *
2373 * Clears the blob pointer
2374 */
2375static void smack_sem_free_security(struct sem_array *sma)
2376{
2377 struct kern_ipc_perm *isp = &sma->sem_perm;
2378
2379 isp->security = NULL;
2380}
2381
2382/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002383 * smk_curacc_sem : check if current has access on sem
2384 * @sma : the object
2385 * @access : access requested
2386 *
2387 * Returns 0 if current has the requested access, error code otherwise
2388 */
2389static int smk_curacc_sem(struct sem_array *sma, int access)
2390{
2391 char *ssp = smack_of_sem(sma);
2392 struct smk_audit_info ad;
2393
2394#ifdef CONFIG_AUDIT
2395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2396 ad.a.u.ipc_id = sma->sem_perm.id;
2397#endif
2398 return smk_curacc(ssp, access, &ad);
2399}
2400
2401/**
Casey Schauflere114e472008-02-04 22:29:50 -08002402 * smack_sem_associate - Smack access check for sem
2403 * @sma: the object
2404 * @semflg: access requested
2405 *
2406 * Returns 0 if current has the requested access, error code otherwise
2407 */
2408static int smack_sem_associate(struct sem_array *sma, int semflg)
2409{
Casey Schauflere114e472008-02-04 22:29:50 -08002410 int may;
2411
2412 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002413 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002414}
2415
2416/**
2417 * smack_sem_shmctl - Smack access check for sem
2418 * @sma: the object
2419 * @cmd: what it wants to do
2420 *
2421 * Returns 0 if current has the requested access, error code otherwise
2422 */
2423static int smack_sem_semctl(struct sem_array *sma, int cmd)
2424{
Casey Schauflere114e472008-02-04 22:29:50 -08002425 int may;
2426
2427 switch (cmd) {
2428 case GETPID:
2429 case GETNCNT:
2430 case GETZCNT:
2431 case GETVAL:
2432 case GETALL:
2433 case IPC_STAT:
2434 case SEM_STAT:
2435 may = MAY_READ;
2436 break;
2437 case SETVAL:
2438 case SETALL:
2439 case IPC_RMID:
2440 case IPC_SET:
2441 may = MAY_READWRITE;
2442 break;
2443 case IPC_INFO:
2444 case SEM_INFO:
2445 /*
2446 * System level information
2447 */
2448 return 0;
2449 default:
2450 return -EINVAL;
2451 }
2452
Etienne Bassetecfcc532009-04-08 20:40:06 +02002453 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002454}
2455
2456/**
2457 * smack_sem_semop - Smack checks of semaphore operations
2458 * @sma: the object
2459 * @sops: unused
2460 * @nsops: unused
2461 * @alter: unused
2462 *
2463 * Treated as read and write in all cases.
2464 *
2465 * Returns 0 if access is allowed, error code otherwise
2466 */
2467static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2468 unsigned nsops, int alter)
2469{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002470 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002471}
2472
2473/**
2474 * smack_msg_alloc_security - Set the security blob for msg
2475 * @msq: the object
2476 *
2477 * Returns 0
2478 */
2479static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2480{
2481 struct kern_ipc_perm *kisp = &msq->q_perm;
2482
Casey Schaufler676dac42010-12-02 06:43:39 -08002483 kisp->security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002484 return 0;
2485}
2486
2487/**
2488 * smack_msg_free_security - Clear the security blob for msg
2489 * @msq: the object
2490 *
2491 * Clears the blob pointer
2492 */
2493static void smack_msg_queue_free_security(struct msg_queue *msq)
2494{
2495 struct kern_ipc_perm *kisp = &msq->q_perm;
2496
2497 kisp->security = NULL;
2498}
2499
2500/**
2501 * smack_of_msq - the smack pointer for the msq
2502 * @msq: the object
2503 *
2504 * Returns a pointer to the smack value
2505 */
2506static char *smack_of_msq(struct msg_queue *msq)
2507{
2508 return (char *)msq->q_perm.security;
2509}
2510
2511/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002512 * smk_curacc_msq : helper to check if current has access on msq
2513 * @msq : the msq
2514 * @access : access requested
2515 *
2516 * return 0 if current has access, error otherwise
2517 */
2518static int smk_curacc_msq(struct msg_queue *msq, int access)
2519{
2520 char *msp = smack_of_msq(msq);
2521 struct smk_audit_info ad;
2522
2523#ifdef CONFIG_AUDIT
2524 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2525 ad.a.u.ipc_id = msq->q_perm.id;
2526#endif
2527 return smk_curacc(msp, access, &ad);
2528}
2529
2530/**
Casey Schauflere114e472008-02-04 22:29:50 -08002531 * smack_msg_queue_associate - Smack access check for msg_queue
2532 * @msq: the object
2533 * @msqflg: access requested
2534 *
2535 * Returns 0 if current has the requested access, error code otherwise
2536 */
2537static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2538{
Casey Schauflere114e472008-02-04 22:29:50 -08002539 int may;
2540
2541 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002542 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002543}
2544
2545/**
2546 * smack_msg_queue_msgctl - Smack access check for msg_queue
2547 * @msq: the object
2548 * @cmd: what it wants to do
2549 *
2550 * Returns 0 if current has the requested access, error code otherwise
2551 */
2552static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2553{
Casey Schauflere114e472008-02-04 22:29:50 -08002554 int may;
2555
2556 switch (cmd) {
2557 case IPC_STAT:
2558 case MSG_STAT:
2559 may = MAY_READ;
2560 break;
2561 case IPC_SET:
2562 case IPC_RMID:
2563 may = MAY_READWRITE;
2564 break;
2565 case IPC_INFO:
2566 case MSG_INFO:
2567 /*
2568 * System level information
2569 */
2570 return 0;
2571 default:
2572 return -EINVAL;
2573 }
2574
Etienne Bassetecfcc532009-04-08 20:40:06 +02002575 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002576}
2577
2578/**
2579 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2580 * @msq: the object
2581 * @msg: unused
2582 * @msqflg: access requested
2583 *
2584 * Returns 0 if current has the requested access, error code otherwise
2585 */
2586static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2587 int msqflg)
2588{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002589 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002590
Etienne Bassetecfcc532009-04-08 20:40:06 +02002591 may = smack_flags_to_may(msqflg);
2592 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002593}
2594
2595/**
2596 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2597 * @msq: the object
2598 * @msg: unused
2599 * @target: unused
2600 * @type: unused
2601 * @mode: unused
2602 *
2603 * Returns 0 if current has read and write access, error code otherwise
2604 */
2605static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2606 struct task_struct *target, long type, int mode)
2607{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002608 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002609}
2610
2611/**
2612 * smack_ipc_permission - Smack access for ipc_permission()
2613 * @ipp: the object permissions
2614 * @flag: access requested
2615 *
2616 * Returns 0 if current has read and write access, error code otherwise
2617 */
2618static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2619{
2620 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002621 int may = smack_flags_to_may(flag);
2622 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002623
Etienne Bassetecfcc532009-04-08 20:40:06 +02002624#ifdef CONFIG_AUDIT
2625 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2626 ad.a.u.ipc_id = ipp->id;
2627#endif
2628 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002629}
2630
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002631/**
2632 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002633 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002634 * @secid: where result will be saved
2635 */
2636static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2637{
2638 char *smack = ipp->security;
2639
2640 *secid = smack_to_secid(smack);
2641}
2642
Casey Schauflere114e472008-02-04 22:29:50 -08002643/**
2644 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002645 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002646 * @inode: the object
2647 *
2648 * Set the inode's security blob if it hasn't been done already.
2649 */
2650static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2651{
2652 struct super_block *sbp;
2653 struct superblock_smack *sbsp;
2654 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002655 char *csp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002656 char *fetched;
2657 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002658 char trattr[TRANS_TRUE_SIZE];
2659 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002660 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002661 struct dentry *dp;
2662
2663 if (inode == NULL)
2664 return;
2665
2666 isp = inode->i_security;
2667
2668 mutex_lock(&isp->smk_lock);
2669 /*
2670 * If the inode is already instantiated
2671 * take the quick way out
2672 */
2673 if (isp->smk_flags & SMK_INODE_INSTANT)
2674 goto unlockandout;
2675
2676 sbp = inode->i_sb;
2677 sbsp = sbp->s_security;
2678 /*
2679 * We're going to use the superblock default label
2680 * if there's no label on the file.
2681 */
2682 final = sbsp->smk_default;
2683
2684 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002685 * If this is the root inode the superblock
2686 * may be in the process of initialization.
2687 * If that is the case use the root value out
2688 * of the superblock.
2689 */
2690 if (opt_dentry->d_parent == opt_dentry) {
2691 isp->smk_inode = sbsp->smk_root;
2692 isp->smk_flags |= SMK_INODE_INSTANT;
2693 goto unlockandout;
2694 }
2695
2696 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002697 * This is pretty hackish.
2698 * Casey says that we shouldn't have to do
2699 * file system specific code, but it does help
2700 * with keeping it simple.
2701 */
2702 switch (sbp->s_magic) {
2703 case SMACK_MAGIC:
2704 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002705 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002706 * that the smack file system doesn't do
2707 * extended attributes.
2708 */
2709 final = smack_known_star.smk_known;
2710 break;
2711 case PIPEFS_MAGIC:
2712 /*
2713 * Casey says pipes are easy (?)
2714 */
2715 final = smack_known_star.smk_known;
2716 break;
2717 case DEVPTS_SUPER_MAGIC:
2718 /*
2719 * devpts seems content with the label of the task.
2720 * Programs that change smack have to treat the
2721 * pty with respect.
2722 */
2723 final = csp;
2724 break;
2725 case SOCKFS_MAGIC:
2726 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002727 * Socket access is controlled by the socket
2728 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002729 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002730 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002731 break;
2732 case PROC_SUPER_MAGIC:
2733 /*
2734 * Casey says procfs appears not to care.
2735 * The superblock default suffices.
2736 */
2737 break;
2738 case TMPFS_MAGIC:
2739 /*
2740 * Device labels should come from the filesystem,
2741 * but watch out, because they're volitile,
2742 * getting recreated on every reboot.
2743 */
2744 final = smack_known_star.smk_known;
2745 /*
2746 * No break.
2747 *
2748 * If a smack value has been set we want to use it,
2749 * but since tmpfs isn't giving us the opportunity
2750 * to set mount options simulate setting the
2751 * superblock default.
2752 */
2753 default:
2754 /*
2755 * This isn't an understood special case.
2756 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002757 */
2758
2759 /*
2760 * UNIX domain sockets use lower level socket data.
2761 */
2762 if (S_ISSOCK(inode->i_mode)) {
2763 final = smack_known_star.smk_known;
2764 break;
2765 }
2766 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002767 * No xattr support means, alas, no SMACK label.
2768 * Use the aforeapplied default.
2769 * It would be curious if the label of the task
2770 * does not match that assigned.
2771 */
2772 if (inode->i_op->getxattr == NULL)
2773 break;
2774 /*
2775 * Get the dentry for xattr.
2776 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002777 dp = dget(opt_dentry);
Casey Schaufler676dac42010-12-02 06:43:39 -08002778 fetched = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Casey Schaufler2267b132012-03-13 19:14:19 -07002779 if (fetched != NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002780 final = fetched;
Casey Schaufler2267b132012-03-13 19:14:19 -07002781
2782 /*
2783 * Transmuting directory
2784 */
2785 if (S_ISDIR(inode->i_mode)) {
2786 /*
2787 * If this is a new directory and the label was
2788 * transmuted when the inode was initialized
2789 * set the transmute attribute on the directory
2790 * and mark the inode.
2791 *
2792 * If there is a transmute attribute on the
2793 * directory mark the inode.
2794 */
2795 if (isp->smk_flags & SMK_INODE_CHANGED) {
2796 isp->smk_flags &= ~SMK_INODE_CHANGED;
2797 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002798 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002799 TRANS_TRUE, TRANS_TRUE_SIZE,
2800 0);
2801 } else {
2802 rc = inode->i_op->getxattr(dp,
2803 XATTR_NAME_SMACKTRANSMUTE, trattr,
2804 TRANS_TRUE_SIZE);
2805 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2806 TRANS_TRUE_SIZE) != 0)
2807 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002808 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002809 if (rc >= 0)
2810 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002811 }
2812 isp->smk_task = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002813 isp->smk_mmap = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002814
Casey Schauflere114e472008-02-04 22:29:50 -08002815 dput(dp);
2816 break;
2817 }
2818
2819 if (final == NULL)
2820 isp->smk_inode = csp;
2821 else
2822 isp->smk_inode = final;
2823
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002824 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002825
2826unlockandout:
2827 mutex_unlock(&isp->smk_lock);
2828 return;
2829}
2830
2831/**
2832 * smack_getprocattr - Smack process attribute access
2833 * @p: the object task
2834 * @name: the name of the attribute in /proc/.../attr
2835 * @value: where to put the result
2836 *
2837 * Places a copy of the task Smack into value
2838 *
2839 * Returns the length of the smack label or an error code
2840 */
2841static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2842{
2843 char *cp;
2844 int slen;
2845
2846 if (strcmp(name, "current") != 0)
2847 return -EINVAL;
2848
Casey Schaufler676dac42010-12-02 06:43:39 -08002849 cp = kstrdup(smk_of_task(task_security(p)), GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002850 if (cp == NULL)
2851 return -ENOMEM;
2852
2853 slen = strlen(cp);
2854 *value = cp;
2855 return slen;
2856}
2857
2858/**
2859 * smack_setprocattr - Smack process attribute setting
2860 * @p: the object task
2861 * @name: the name of the attribute in /proc/.../attr
2862 * @value: the value to set
2863 * @size: the size of the value
2864 *
2865 * Sets the Smack value of the task. Only setting self
2866 * is permitted and only with privilege
2867 *
2868 * Returns the length of the smack label or an error code
2869 */
2870static int smack_setprocattr(struct task_struct *p, char *name,
2871 void *value, size_t size)
2872{
Casey Schaufler676dac42010-12-02 06:43:39 -08002873 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002874 struct cred *new;
Casey Schauflere114e472008-02-04 22:29:50 -08002875 char *newsmack;
2876
Casey Schauflere114e472008-02-04 22:29:50 -08002877 /*
2878 * Changing another process' Smack value is too dangerous
2879 * and supports no sane use case.
2880 */
2881 if (p != current)
2882 return -EPERM;
2883
Casey Schaufler1880eff2012-06-05 15:28:30 -07002884 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01002885 return -EPERM;
2886
Casey Schauflerf7112e62012-05-06 15:22:02 -07002887 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08002888 return -EINVAL;
2889
2890 if (strcmp(name, "current") != 0)
2891 return -EINVAL;
2892
2893 newsmack = smk_import(value, size);
2894 if (newsmack == NULL)
2895 return -EINVAL;
2896
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002897 /*
2898 * No process is ever allowed the web ("@") label.
2899 */
2900 if (newsmack == smack_known_web.smk_known)
2901 return -EPERM;
2902
David Howellsd84f4f92008-11-14 10:39:23 +11002903 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002904 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11002905 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002906
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07002907 tsp = new->security;
2908 tsp->smk_task = newsmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002909
David Howellsd84f4f92008-11-14 10:39:23 +11002910 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08002911 return size;
2912}
2913
2914/**
2915 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08002916 * @sock: one sock
2917 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08002918 * @newsk: unused
2919 *
2920 * Return 0 if a subject with the smack of sock could access
2921 * an object with the smack of other, otherwise an error code
2922 */
David S. Miller3610cda2011-01-05 15:38:53 -08002923static int smack_unix_stream_connect(struct sock *sock,
2924 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08002925{
James Morrisd2e7ad12011-01-10 09:46:24 +11002926 struct socket_smack *ssp = sock->sk_security;
2927 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07002928 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002929 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002930 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002931
Kees Cook923e9a12012-04-10 13:26:44 -07002932#ifdef CONFIG_AUDIT
2933 struct lsm_network_audit net;
2934
Eric Paris48c62af2012-04-02 13:15:44 -04002935 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
David S. Miller3610cda2011-01-05 15:38:53 -08002936 smk_ad_setfield_u_net_sk(&ad, other);
Kees Cook923e9a12012-04-10 13:26:44 -07002937#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002938
Casey Schaufler1880eff2012-06-05 15:28:30 -07002939 if (!smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002940 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2941
Casey Schaufler975d5e52011-09-26 14:43:39 -07002942 /*
2943 * Cross reference the peer labels for SO_PEERSEC.
2944 */
2945 if (rc == 0) {
2946 nsp->smk_packet = ssp->smk_out;
2947 ssp->smk_packet = osp->smk_out;
2948 }
2949
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002950 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002951}
2952
2953/**
2954 * smack_unix_may_send - Smack access on UDS
2955 * @sock: one socket
2956 * @other: the other socket
2957 *
2958 * Return 0 if a subject with the smack of sock could access
2959 * an object with the smack of other, otherwise an error code
2960 */
2961static int smack_unix_may_send(struct socket *sock, struct socket *other)
2962{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002963 struct socket_smack *ssp = sock->sk->sk_security;
2964 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002965 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002966 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002967
Kees Cook923e9a12012-04-10 13:26:44 -07002968#ifdef CONFIG_AUDIT
2969 struct lsm_network_audit net;
2970
Eric Paris48c62af2012-04-02 13:15:44 -04002971 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002972 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07002973#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002974
Casey Schaufler1880eff2012-06-05 15:28:30 -07002975 if (!smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002976 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
2977
2978 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002979}
2980
2981/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002982 * smack_socket_sendmsg - Smack check based on destination host
2983 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08002984 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002985 * @size: the size of the message
2986 *
Casey Schauflerc6739442013-05-22 18:42:56 -07002987 * Return 0 if the current subject can write to the destination host.
2988 * For IPv4 this is only a question if the destination is a single label host.
2989 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002990 */
2991static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
2992 int size)
2993{
2994 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07002995 struct sockaddr *sap = (struct sockaddr *) msg->msg_name;
2996 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002997
2998 /*
2999 * Perfectly reasonable for this to be NULL
3000 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003001 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003002 return 0;
3003
Casey Schauflerc6739442013-05-22 18:42:56 -07003004 switch (sip->sin_family) {
3005 case AF_INET:
3006 rc = smack_netlabel_send(sock->sk, sip);
3007 break;
3008 case AF_INET6:
3009 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3010 break;
3011 }
3012 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003013}
3014
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003015/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003016 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003017 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003018 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003019 *
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003020 * Returns a pointer to a Smack label found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003021 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003022static char *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3023 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003024{
Casey Schauflerf7112e62012-05-06 15:22:02 -07003025 struct smack_known *kp;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003026 char *sp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003027 int found = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08003028
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003029 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003030 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003031 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003032 * If there are flags but no level netlabel isn't
3033 * behaving the way we expect it to.
3034 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003035 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003036 * Without guidance regarding the smack value
3037 * for the packet fall back on the network
3038 * ambient value.
3039 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003040 rcu_read_lock();
3041 list_for_each_entry(kp, &smack_known_list, list) {
3042 if (sap->attr.mls.lvl != kp->smk_netlabel.attr.mls.lvl)
3043 continue;
3044 if (memcmp(sap->attr.mls.cat,
3045 kp->smk_netlabel.attr.mls.cat,
3046 SMK_CIPSOLEN) != 0)
3047 continue;
3048 found = 1;
3049 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003050 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003051 rcu_read_unlock();
3052
3053 if (found)
3054 return kp->smk_known;
3055
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003056 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
3057 return smack_known_web.smk_known;
3058 return smack_known_star.smk_known;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003059 }
3060 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3061 /*
3062 * Looks like a fallback, which gives us a secid.
3063 */
3064 sp = smack_from_secid(sap->attr.secid);
3065 /*
3066 * This has got to be a bug because it is
3067 * impossible to specify a fallback without
3068 * specifying the label, which will ensure
3069 * it has a secid, and the only way to get a
3070 * secid is from a fallback.
3071 */
3072 BUG_ON(sp == NULL);
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003073 return sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003074 }
3075 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003076 * Without guidance regarding the smack value
3077 * for the packet fall back on the network
3078 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003079 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003080 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003081}
3082
Casey Schauflerc6739442013-05-22 18:42:56 -07003083static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr *sap)
3084{
3085 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
3086 u8 nexthdr;
3087 int offset;
3088 int proto = -EINVAL;
3089 struct ipv6hdr _ipv6h;
3090 struct ipv6hdr *ip6;
3091 __be16 frag_off;
3092 struct tcphdr _tcph, *th;
3093 struct udphdr _udph, *uh;
3094 struct dccp_hdr _dccph, *dh;
3095
3096 sip->sin6_port = 0;
3097
3098 offset = skb_network_offset(skb);
3099 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3100 if (ip6 == NULL)
3101 return -EINVAL;
3102 sip->sin6_addr = ip6->saddr;
3103
3104 nexthdr = ip6->nexthdr;
3105 offset += sizeof(_ipv6h);
3106 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3107 if (offset < 0)
3108 return -EINVAL;
3109
3110 proto = nexthdr;
3111 switch (proto) {
3112 case IPPROTO_TCP:
3113 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3114 if (th != NULL)
3115 sip->sin6_port = th->source;
3116 break;
3117 case IPPROTO_UDP:
3118 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3119 if (uh != NULL)
3120 sip->sin6_port = uh->source;
3121 break;
3122 case IPPROTO_DCCP:
3123 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3124 if (dh != NULL)
3125 sip->sin6_port = dh->dccph_sport;
3126 break;
3127 }
3128 return proto;
3129}
3130
Casey Schauflere114e472008-02-04 22:29:50 -08003131/**
3132 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3133 * @sk: socket
3134 * @skb: packet
3135 *
3136 * Returns 0 if the packet should be delivered, an error code otherwise
3137 */
3138static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3139{
3140 struct netlbl_lsm_secattr secattr;
3141 struct socket_smack *ssp = sk->sk_security;
Casey Schauflerc6739442013-05-22 18:42:56 -07003142 struct sockaddr sadd;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003143 char *csp;
Casey Schauflerc6739442013-05-22 18:42:56 -07003144 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003145 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003146#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003147 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003148#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003149 switch (sk->sk_family) {
3150 case PF_INET:
3151 /*
3152 * Translate what netlabel gave us.
3153 */
3154 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003155
Casey Schauflerc6739442013-05-22 18:42:56 -07003156 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3157 if (rc == 0)
3158 csp = smack_from_secattr(&secattr, ssp);
3159 else
3160 csp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003161
Casey Schauflerc6739442013-05-22 18:42:56 -07003162 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003163
Etienne Bassetecfcc532009-04-08 20:40:06 +02003164#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003165 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3166 ad.a.u.net->family = sk->sk_family;
3167 ad.a.u.net->netif = skb->skb_iif;
3168 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003169#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003170 /*
3171 * Receiving a packet requires that the other end
3172 * be able to write here. Read access is not required.
3173 * This is the simplist possible security model
3174 * for networking.
3175 */
3176 rc = smk_access(csp, ssp->smk_in, MAY_WRITE, &ad);
3177 if (rc != 0)
3178 netlbl_skbuff_err(skb, rc, 0);
3179 break;
3180 case PF_INET6:
3181 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3182 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3183 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3184 else
3185 rc = 0;
3186 break;
3187 }
Paul Moorea8134292008-10-10 10:16:31 -04003188 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003189}
3190
3191/**
3192 * smack_socket_getpeersec_stream - pull in packet label
3193 * @sock: the socket
3194 * @optval: user's destination
3195 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003196 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003197 *
3198 * returns zero on success, an error code otherwise
3199 */
3200static int smack_socket_getpeersec_stream(struct socket *sock,
3201 char __user *optval,
3202 int __user *optlen, unsigned len)
3203{
3204 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003205 char *rcp = "";
3206 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003207 int rc = 0;
3208
3209 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003210 if (ssp->smk_packet != NULL) {
3211 rcp = ssp->smk_packet;
3212 slen = strlen(rcp) + 1;
3213 }
Casey Schauflere114e472008-02-04 22:29:50 -08003214
3215 if (slen > len)
3216 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003217 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003218 rc = -EFAULT;
3219
3220 if (put_user(slen, optlen) != 0)
3221 rc = -EFAULT;
3222
3223 return rc;
3224}
3225
3226
3227/**
3228 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003229 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003230 * @skb: packet data
3231 * @secid: pointer to where to put the secid of the packet
3232 *
3233 * Sets the netlabel socket state on sk from parent
3234 */
3235static int smack_socket_getpeersec_dgram(struct socket *sock,
3236 struct sk_buff *skb, u32 *secid)
3237
3238{
3239 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003240 struct socket_smack *ssp = NULL;
3241 char *sp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003242 int family = PF_UNSPEC;
3243 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003244 int rc;
3245
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003246 if (skb != NULL) {
3247 if (skb->protocol == htons(ETH_P_IP))
3248 family = PF_INET;
3249 else if (skb->protocol == htons(ETH_P_IPV6))
3250 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003251 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003252 if (family == PF_UNSPEC && sock != NULL)
3253 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003254
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003255 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003256 ssp = sock->sk->sk_security;
3257 s = smack_to_secid(ssp->smk_out);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003258 } else if (family == PF_INET || family == PF_INET6) {
3259 /*
3260 * Translate what netlabel gave us.
3261 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003262 if (sock != NULL && sock->sk != NULL)
3263 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003264 netlbl_secattr_init(&secattr);
3265 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3266 if (rc == 0) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003267 sp = smack_from_secattr(&secattr, ssp);
3268 s = smack_to_secid(sp);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003269 }
3270 netlbl_secattr_destroy(&secattr);
3271 }
3272 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003273 if (s == 0)
3274 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003275 return 0;
3276}
3277
3278/**
Paul Moore07feee82009-03-27 17:10:54 -04003279 * smack_sock_graft - Initialize a newly created socket with an existing sock
3280 * @sk: child sock
3281 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003282 *
Paul Moore07feee82009-03-27 17:10:54 -04003283 * Set the smk_{in,out} state of an existing sock based on the process that
3284 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003285 */
3286static void smack_sock_graft(struct sock *sk, struct socket *parent)
3287{
3288 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08003289
Paul Moore07feee82009-03-27 17:10:54 -04003290 if (sk == NULL ||
3291 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003292 return;
3293
3294 ssp = sk->sk_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08003295 ssp->smk_in = ssp->smk_out = smk_of_current();
Paul Moore07feee82009-03-27 17:10:54 -04003296 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003297}
3298
3299/**
3300 * smack_inet_conn_request - Smack access check on connect
3301 * @sk: socket involved
3302 * @skb: packet
3303 * @req: unused
3304 *
3305 * Returns 0 if a task with the packet label could write to
3306 * the socket, otherwise an error code
3307 */
3308static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3309 struct request_sock *req)
3310{
Paul Moore07feee82009-03-27 17:10:54 -04003311 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003312 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003313 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003314 struct netlbl_lsm_secattr secattr;
3315 struct sockaddr_in addr;
3316 struct iphdr *hdr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003317 char *sp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003318 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003319 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003320 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003321#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003322 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003323#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003324
Casey Schauflerc6739442013-05-22 18:42:56 -07003325 if (family == PF_INET6) {
3326 /*
3327 * Handle mapped IPv4 packets arriving
3328 * via IPv6 sockets. Don't set up netlabel
3329 * processing on IPv6.
3330 */
3331 if (skb->protocol == htons(ETH_P_IP))
3332 family = PF_INET;
3333 else
3334 return 0;
3335 }
Casey Schauflere114e472008-02-04 22:29:50 -08003336
Paul Moore07feee82009-03-27 17:10:54 -04003337 netlbl_secattr_init(&secattr);
3338 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003339 if (rc == 0)
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003340 sp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003341 else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003342 sp = smack_known_huh.smk_known;
Paul Moore07feee82009-03-27 17:10:54 -04003343 netlbl_secattr_destroy(&secattr);
3344
Etienne Bassetecfcc532009-04-08 20:40:06 +02003345#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003346 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3347 ad.a.u.net->family = family;
3348 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003349 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3350#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003351 /*
Paul Moore07feee82009-03-27 17:10:54 -04003352 * Receiving a packet requires that the other end be able to write
3353 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003354 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003355 rc = smk_access(sp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003356 if (rc != 0)
3357 return rc;
3358
3359 /*
3360 * Save the peer's label in the request_sock so we can later setup
3361 * smk_packet in the child socket so that SO_PEERCRED can report it.
3362 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003363 req->peer_secid = smack_to_secid(sp);
Paul Moore07feee82009-03-27 17:10:54 -04003364
3365 /*
3366 * We need to decide if we want to label the incoming connection here
3367 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003368 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003369 */
3370 hdr = ip_hdr(skb);
3371 addr.sin_addr.s_addr = hdr->saddr;
3372 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003373 hsp = smack_host_label(&addr);
3374 rcu_read_unlock();
3375
3376 if (hsp == NULL) {
3377 skp = smk_find_entry(sp);
3378 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
3379 } else
Paul Moore07feee82009-03-27 17:10:54 -04003380 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003381
3382 return rc;
3383}
3384
Paul Moore07feee82009-03-27 17:10:54 -04003385/**
3386 * smack_inet_csk_clone - Copy the connection information to the new socket
3387 * @sk: the new socket
3388 * @req: the connection's request_sock
3389 *
3390 * Transfer the connection's peer label to the newly created socket.
3391 */
3392static void smack_inet_csk_clone(struct sock *sk,
3393 const struct request_sock *req)
3394{
3395 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003396
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003397 if (req->peer_secid != 0)
3398 ssp->smk_packet = smack_from_secid(req->peer_secid);
3399 else
3400 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003401}
3402
Casey Schauflere114e472008-02-04 22:29:50 -08003403/*
3404 * Key management security hooks
3405 *
3406 * Casey has not tested key support very heavily.
3407 * The permission check is most likely too restrictive.
3408 * If you care about keys please have a look.
3409 */
3410#ifdef CONFIG_KEYS
3411
3412/**
3413 * smack_key_alloc - Set the key security blob
3414 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003415 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003416 * @flags: unused
3417 *
3418 * No allocation required
3419 *
3420 * Returns 0
3421 */
David Howellsd84f4f92008-11-14 10:39:23 +11003422static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003423 unsigned long flags)
3424{
Casey Schaufler676dac42010-12-02 06:43:39 -08003425 key->security = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003426 return 0;
3427}
3428
3429/**
3430 * smack_key_free - Clear the key security blob
3431 * @key: the object
3432 *
3433 * Clear the blob pointer
3434 */
3435static void smack_key_free(struct key *key)
3436{
3437 key->security = NULL;
3438}
3439
3440/*
3441 * smack_key_permission - Smack access on a key
3442 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003443 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003444 * @perm: unused
3445 *
3446 * Return 0 if the task has read and write to the object,
3447 * an error code otherwise
3448 */
3449static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003450 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003451{
3452 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003453 struct smk_audit_info ad;
Casey Schaufler676dac42010-12-02 06:43:39 -08003454 char *tsp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003455
3456 keyp = key_ref_to_ptr(key_ref);
3457 if (keyp == NULL)
3458 return -EINVAL;
3459 /*
3460 * If the key hasn't been initialized give it access so that
3461 * it may do so.
3462 */
3463 if (keyp->security == NULL)
3464 return 0;
3465 /*
3466 * This should not occur
3467 */
Casey Schaufler676dac42010-12-02 06:43:39 -08003468 if (tsp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003469 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003470#ifdef CONFIG_AUDIT
3471 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3472 ad.a.u.key_struct.key = keyp->serial;
3473 ad.a.u.key_struct.key_desc = keyp->description;
3474#endif
Casey Schaufler676dac42010-12-02 06:43:39 -08003475 return smk_access(tsp, keyp->security,
Etienne Bassetecfcc532009-04-08 20:40:06 +02003476 MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003477}
3478#endif /* CONFIG_KEYS */
3479
3480/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003481 * Smack Audit hooks
3482 *
3483 * Audit requires a unique representation of each Smack specific
3484 * rule. This unique representation is used to distinguish the
3485 * object to be audited from remaining kernel objects and also
3486 * works as a glue between the audit hooks.
3487 *
3488 * Since repository entries are added but never deleted, we'll use
3489 * the smack_known label address related to the given audit rule as
3490 * the needed unique representation. This also better fits the smack
3491 * model where nearly everything is a label.
3492 */
3493#ifdef CONFIG_AUDIT
3494
3495/**
3496 * smack_audit_rule_init - Initialize a smack audit rule
3497 * @field: audit rule fields given from user-space (audit.h)
3498 * @op: required testing operator (=, !=, >, <, ...)
3499 * @rulestr: smack label to be audited
3500 * @vrule: pointer to save our own audit rule representation
3501 *
3502 * Prepare to audit cases where (@field @op @rulestr) is true.
3503 * The label to be audited is created if necessay.
3504 */
3505static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3506{
3507 char **rule = (char **)vrule;
3508 *rule = NULL;
3509
3510 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3511 return -EINVAL;
3512
Al Viro5af75d82008-12-16 05:59:26 -05003513 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003514 return -EINVAL;
3515
3516 *rule = smk_import(rulestr, 0);
3517
3518 return 0;
3519}
3520
3521/**
3522 * smack_audit_rule_known - Distinguish Smack audit rules
3523 * @krule: rule of interest, in Audit kernel representation format
3524 *
3525 * This is used to filter Smack rules from remaining Audit ones.
3526 * If it's proved that this rule belongs to us, the
3527 * audit_rule_match hook will be called to do the final judgement.
3528 */
3529static int smack_audit_rule_known(struct audit_krule *krule)
3530{
3531 struct audit_field *f;
3532 int i;
3533
3534 for (i = 0; i < krule->field_count; i++) {
3535 f = &krule->fields[i];
3536
3537 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3538 return 1;
3539 }
3540
3541 return 0;
3542}
3543
3544/**
3545 * smack_audit_rule_match - Audit given object ?
3546 * @secid: security id for identifying the object to test
3547 * @field: audit rule flags given from user-space
3548 * @op: required testing operator
3549 * @vrule: smack internal rule presentation
3550 * @actx: audit context associated with the check
3551 *
3552 * The core Audit hook. It's used to take the decision of
3553 * whether to audit or not to audit a given object.
3554 */
3555static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3556 struct audit_context *actx)
3557{
3558 char *smack;
3559 char *rule = vrule;
3560
3561 if (!rule) {
Tetsuo Handaceffec552012-03-29 16:19:05 +09003562 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003563 "Smack: missing rule\n");
3564 return -ENOENT;
3565 }
3566
3567 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3568 return 0;
3569
3570 smack = smack_from_secid(secid);
3571
3572 /*
3573 * No need to do string comparisons. If a match occurs,
3574 * both pointers will point to the same smack_known
3575 * label.
3576 */
Al Viro5af75d82008-12-16 05:59:26 -05003577 if (op == Audit_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003578 return (rule == smack);
Al Viro5af75d82008-12-16 05:59:26 -05003579 if (op == Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003580 return (rule != smack);
3581
3582 return 0;
3583}
3584
3585/**
3586 * smack_audit_rule_free - free smack rule representation
3587 * @vrule: rule to be freed.
3588 *
3589 * No memory was allocated.
3590 */
3591static void smack_audit_rule_free(void *vrule)
3592{
3593 /* No-op */
3594}
3595
3596#endif /* CONFIG_AUDIT */
3597
Randy Dunlap251a2a92009-02-18 11:42:33 -08003598/**
Casey Schauflere114e472008-02-04 22:29:50 -08003599 * smack_secid_to_secctx - return the smack label for a secid
3600 * @secid: incoming integer
3601 * @secdata: destination
3602 * @seclen: how long it is
3603 *
3604 * Exists for networking code.
3605 */
3606static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3607{
3608 char *sp = smack_from_secid(secid);
3609
Eric Parisd5630b92010-10-13 16:24:48 -04003610 if (secdata)
3611 *secdata = sp;
Casey Schauflere114e472008-02-04 22:29:50 -08003612 *seclen = strlen(sp);
3613 return 0;
3614}
3615
Randy Dunlap251a2a92009-02-18 11:42:33 -08003616/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003617 * smack_secctx_to_secid - return the secid for a smack label
3618 * @secdata: smack label
3619 * @seclen: how long result is
3620 * @secid: outgoing integer
3621 *
3622 * Exists for audit and networking code.
3623 */
David Howellse52c17642008-04-29 20:52:51 +01003624static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003625{
3626 *secid = smack_to_secid(secdata);
3627 return 0;
3628}
3629
Randy Dunlap251a2a92009-02-18 11:42:33 -08003630/**
Casey Schauflere114e472008-02-04 22:29:50 -08003631 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003632 * @secdata: unused
3633 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003634 *
3635 * Exists to make sure nothing gets done, and properly
3636 */
3637static void smack_release_secctx(char *secdata, u32 seclen)
3638{
3639}
3640
David P. Quigley1ee65e32009-09-03 14:25:57 -04003641static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3642{
3643 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3644}
3645
3646static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3647{
3648 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3649}
3650
3651static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3652{
3653 int len = 0;
3654 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3655
3656 if (len < 0)
3657 return len;
3658 *ctxlen = len;
3659 return 0;
3660}
3661
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003662struct security_operations smack_ops = {
3663 .name = "smack",
3664
Ingo Molnar9e488582009-05-07 19:26:19 +10003665 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003666 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003667 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003668
3669 .sb_alloc_security = smack_sb_alloc_security,
3670 .sb_free_security = smack_sb_free_security,
3671 .sb_copy_data = smack_sb_copy_data,
3672 .sb_kern_mount = smack_sb_kern_mount,
3673 .sb_statfs = smack_sb_statfs,
3674 .sb_mount = smack_sb_mount,
3675 .sb_umount = smack_sb_umount,
3676
Casey Schaufler676dac42010-12-02 06:43:39 -08003677 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003678 .bprm_committing_creds = smack_bprm_committing_creds,
3679 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003680
Casey Schauflere114e472008-02-04 22:29:50 -08003681 .inode_alloc_security = smack_inode_alloc_security,
3682 .inode_free_security = smack_inode_free_security,
3683 .inode_init_security = smack_inode_init_security,
3684 .inode_link = smack_inode_link,
3685 .inode_unlink = smack_inode_unlink,
3686 .inode_rmdir = smack_inode_rmdir,
3687 .inode_rename = smack_inode_rename,
3688 .inode_permission = smack_inode_permission,
3689 .inode_setattr = smack_inode_setattr,
3690 .inode_getattr = smack_inode_getattr,
3691 .inode_setxattr = smack_inode_setxattr,
3692 .inode_post_setxattr = smack_inode_post_setxattr,
3693 .inode_getxattr = smack_inode_getxattr,
3694 .inode_removexattr = smack_inode_removexattr,
3695 .inode_getsecurity = smack_inode_getsecurity,
3696 .inode_setsecurity = smack_inode_setsecurity,
3697 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003698 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003699
3700 .file_permission = smack_file_permission,
3701 .file_alloc_security = smack_file_alloc_security,
3702 .file_free_security = smack_file_free_security,
3703 .file_ioctl = smack_file_ioctl,
3704 .file_lock = smack_file_lock,
3705 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003706 .mmap_file = smack_mmap_file,
3707 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003708 .file_set_fowner = smack_file_set_fowner,
3709 .file_send_sigiotask = smack_file_send_sigiotask,
3710 .file_receive = smack_file_receive,
3711
Eric Paris83d49852012-04-04 13:45:40 -04003712 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003713
David Howellsee18d642009-09-02 09:14:21 +01003714 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003715 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003716 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003717 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003718 .kernel_act_as = smack_kernel_act_as,
3719 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003720 .task_setpgid = smack_task_setpgid,
3721 .task_getpgid = smack_task_getpgid,
3722 .task_getsid = smack_task_getsid,
3723 .task_getsecid = smack_task_getsecid,
3724 .task_setnice = smack_task_setnice,
3725 .task_setioprio = smack_task_setioprio,
3726 .task_getioprio = smack_task_getioprio,
3727 .task_setscheduler = smack_task_setscheduler,
3728 .task_getscheduler = smack_task_getscheduler,
3729 .task_movememory = smack_task_movememory,
3730 .task_kill = smack_task_kill,
3731 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003732 .task_to_inode = smack_task_to_inode,
3733
3734 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003735 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003736
3737 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3738 .msg_msg_free_security = smack_msg_msg_free_security,
3739
3740 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3741 .msg_queue_free_security = smack_msg_queue_free_security,
3742 .msg_queue_associate = smack_msg_queue_associate,
3743 .msg_queue_msgctl = smack_msg_queue_msgctl,
3744 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3745 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3746
3747 .shm_alloc_security = smack_shm_alloc_security,
3748 .shm_free_security = smack_shm_free_security,
3749 .shm_associate = smack_shm_associate,
3750 .shm_shmctl = smack_shm_shmctl,
3751 .shm_shmat = smack_shm_shmat,
3752
3753 .sem_alloc_security = smack_sem_alloc_security,
3754 .sem_free_security = smack_sem_free_security,
3755 .sem_associate = smack_sem_associate,
3756 .sem_semctl = smack_sem_semctl,
3757 .sem_semop = smack_sem_semop,
3758
Casey Schauflere114e472008-02-04 22:29:50 -08003759 .d_instantiate = smack_d_instantiate,
3760
3761 .getprocattr = smack_getprocattr,
3762 .setprocattr = smack_setprocattr,
3763
3764 .unix_stream_connect = smack_unix_stream_connect,
3765 .unix_may_send = smack_unix_may_send,
3766
3767 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07003768 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003769 .socket_connect = smack_socket_connect,
3770 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003771 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3772 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3773 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3774 .sk_alloc_security = smack_sk_alloc_security,
3775 .sk_free_security = smack_sk_free_security,
3776 .sock_graft = smack_sock_graft,
3777 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003778 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003779
Casey Schauflere114e472008-02-04 22:29:50 -08003780 /* key management security hooks */
3781#ifdef CONFIG_KEYS
3782 .key_alloc = smack_key_alloc,
3783 .key_free = smack_key_free,
3784 .key_permission = smack_key_permission,
3785#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003786
3787 /* Audit hooks */
3788#ifdef CONFIG_AUDIT
3789 .audit_rule_init = smack_audit_rule_init,
3790 .audit_rule_known = smack_audit_rule_known,
3791 .audit_rule_match = smack_audit_rule_match,
3792 .audit_rule_free = smack_audit_rule_free,
3793#endif /* CONFIG_AUDIT */
3794
Casey Schauflere114e472008-02-04 22:29:50 -08003795 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003796 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003797 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003798 .inode_notifysecctx = smack_inode_notifysecctx,
3799 .inode_setsecctx = smack_inode_setsecctx,
3800 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003801};
3802
Etienne Basset7198e2e2009-03-24 20:53:24 +01003803
Casey Schaufler86812bb2012-04-17 18:55:46 -07003804static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003805{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003806 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003807 * Initialize rule list locks
3808 */
3809 mutex_init(&smack_known_huh.smk_rules_lock);
3810 mutex_init(&smack_known_hat.smk_rules_lock);
3811 mutex_init(&smack_known_floor.smk_rules_lock);
3812 mutex_init(&smack_known_star.smk_rules_lock);
3813 mutex_init(&smack_known_invalid.smk_rules_lock);
3814 mutex_init(&smack_known_web.smk_rules_lock);
3815 /*
3816 * Initialize rule lists
3817 */
3818 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3819 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3820 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3821 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3822 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3823 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3824 /*
3825 * Create the known labels list
3826 */
Etienne Basset7198e2e2009-03-24 20:53:24 +01003827 list_add(&smack_known_huh.list, &smack_known_list);
3828 list_add(&smack_known_hat.list, &smack_known_list);
3829 list_add(&smack_known_star.list, &smack_known_list);
3830 list_add(&smack_known_floor.list, &smack_known_list);
3831 list_add(&smack_known_invalid.list, &smack_known_list);
3832 list_add(&smack_known_web.list, &smack_known_list);
3833}
3834
Casey Schauflere114e472008-02-04 22:29:50 -08003835/**
3836 * smack_init - initialize the smack system
3837 *
3838 * Returns 0
3839 */
3840static __init int smack_init(void)
3841{
David Howellsd84f4f92008-11-14 10:39:23 +11003842 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003843 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003844
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003845 if (!security_module_enable(&smack_ops))
3846 return 0;
3847
3848 tsp = new_task_smack(smack_known_floor.smk_known,
3849 smack_known_floor.smk_known, GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003850 if (tsp == NULL)
3851 return -ENOMEM;
3852
Casey Schauflere114e472008-02-04 22:29:50 -08003853 printk(KERN_INFO "Smack: Initializing.\n");
3854
3855 /*
3856 * Set the security state for the initial task.
3857 */
David Howellsd84f4f92008-11-14 10:39:23 +11003858 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003859 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003860
Casey Schaufler86812bb2012-04-17 18:55:46 -07003861 /* initialize the smack_known_list */
3862 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08003863
3864 /*
3865 * Register with LSM
3866 */
3867 if (register_security(&smack_ops))
3868 panic("smack: Unable to register with kernel.\n");
3869
3870 return 0;
3871}
3872
3873/*
3874 * Smack requires early initialization in order to label
3875 * all processes and objects when they are created.
3876 */
3877security_initcall(smack_init);