blob: a5d86ffbf9a0690f822acc1f3a5fe0e6bdfd4a55 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
19 */
20
21#include <linux/xattr.h>
22#include <linux/pagemap.h>
23#include <linux/mount.h>
24#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070030#include <linux/dccp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080032#include <linux/mutex.h>
33#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080034#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070035#include <net/ip.h>
36#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100037#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070038#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050039#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030040#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000041#include <linux/msg.h>
42#include <linux/shm.h>
43#include <linux/binfmts.h>
Casey Schauflere114e472008-02-04 22:29:50 -080044#include "smack.h"
45
David Howellsc69e8d92008-11-14 10:39:19 +110046#define task_security(task) (task_cred_xxx((task), security))
47
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020048#define TRANS_TRUE "TRUE"
49#define TRANS_TRUE_SIZE 4
50
Casey Schauflerc6739442013-05-22 18:42:56 -070051#define SMK_CONNECTING 0
52#define SMK_RECEIVING 1
53#define SMK_SENDING 2
54
55LIST_HEAD(smk_ipv6_port_list);
56
Casey Schauflere114e472008-02-04 22:29:50 -080057/**
58 * smk_fetch - Fetch the smack label from a file.
59 * @ip: a pointer to the inode
60 * @dp: a pointer to the dentry
61 *
62 * Returns a pointer to the master list entry for the Smack label
63 * or NULL if there was no label to fetch.
64 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -070065static struct smack_known *smk_fetch(const char *name, struct inode *ip,
66 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -080067{
68 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -070069 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -070070 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -080071
72 if (ip->i_op->getxattr == NULL)
73 return NULL;
74
Casey Schauflerf7112e62012-05-06 15:22:02 -070075 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
76 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -080077 return NULL;
78
Casey Schauflerf7112e62012-05-06 15:22:02 -070079 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
80 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -070081 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -070082
83 kfree(buffer);
84
Casey Schaufler2f823ff2013-05-22 18:43:03 -070085 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -080086}
87
88/**
89 * new_inode_smack - allocate an inode security blob
90 * @smack: a pointer to the Smack label to use in the blob
91 *
92 * Returns the new blob or NULL if there's no memory available
93 */
94struct inode_smack *new_inode_smack(char *smack)
95{
96 struct inode_smack *isp;
97
Tetsuo Handaceffec552012-03-29 16:19:05 +090098 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -080099 if (isp == NULL)
100 return NULL;
101
102 isp->smk_inode = smack;
103 isp->smk_flags = 0;
104 mutex_init(&isp->smk_lock);
105
106 return isp;
107}
108
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800109/**
110 * new_task_smack - allocate a task security blob
111 * @smack: a pointer to the Smack label to use in the blob
112 *
113 * Returns the new blob or NULL if there's no memory available
114 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700115static struct task_smack *new_task_smack(struct smack_known *task,
116 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800117{
118 struct task_smack *tsp;
119
120 tsp = kzalloc(sizeof(struct task_smack), gfp);
121 if (tsp == NULL)
122 return NULL;
123
124 tsp->smk_task = task;
125 tsp->smk_forked = forked;
126 INIT_LIST_HEAD(&tsp->smk_rules);
127 mutex_init(&tsp->smk_rules_lock);
128
129 return tsp;
130}
131
132/**
133 * smk_copy_rules - copy a rule set
134 * @nhead - new rules header pointer
135 * @ohead - old rules header pointer
136 *
137 * Returns 0 on success, -ENOMEM on error
138 */
139static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
140 gfp_t gfp)
141{
142 struct smack_rule *nrp;
143 struct smack_rule *orp;
144 int rc = 0;
145
146 INIT_LIST_HEAD(nhead);
147
148 list_for_each_entry_rcu(orp, ohead, list) {
149 nrp = kzalloc(sizeof(struct smack_rule), gfp);
150 if (nrp == NULL) {
151 rc = -ENOMEM;
152 break;
153 }
154 *nrp = *orp;
155 list_add_rcu(&nrp->list, nhead);
156 }
157 return rc;
158}
159
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100160/**
161 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
162 * @mode - input mode in form of PTRACE_MODE_*
163 *
164 * Returns a converted MAY_* mode usable by smack rules
165 */
166static inline unsigned int smk_ptrace_mode(unsigned int mode)
167{
168 switch (mode) {
169 case PTRACE_MODE_READ:
170 return MAY_READ;
171 case PTRACE_MODE_ATTACH:
172 return MAY_READWRITE;
173 }
174
175 return 0;
176}
177
178/**
179 * smk_ptrace_rule_check - helper for ptrace access
180 * @tracer: tracer process
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100181 * @tracee_label: label of the process that's about to be traced,
182 * the pointer must originate from smack structures
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100183 * @mode: ptrace attachment mode (PTRACE_MODE_*)
184 * @func: name of the function that called us, used for audit
185 *
186 * Returns 0 on access granted, -error on error
187 */
188static int smk_ptrace_rule_check(struct task_struct *tracer, char *tracee_label,
189 unsigned int mode, const char *func)
190{
191 int rc;
192 struct smk_audit_info ad, *saip = NULL;
193 struct task_smack *tsp;
194 struct smack_known *skp;
195
196 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
197 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
198 smk_ad_setfield_u_tsk(&ad, tracer);
199 saip = &ad;
200 }
201
202 tsp = task_security(tracer);
203 skp = smk_of_task(tsp);
204
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100205 if ((mode & PTRACE_MODE_ATTACH) &&
206 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
207 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
208 if (skp->smk_known == tracee_label)
209 rc = 0;
210 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
211 rc = -EACCES;
212 else if (capable(CAP_SYS_PTRACE))
213 rc = 0;
214 else
215 rc = -EACCES;
216
217 if (saip)
218 smack_log(skp->smk_known, tracee_label, 0, rc, saip);
219
220 return rc;
221 }
222
223 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100224 rc = smk_tskacc(tsp, tracee_label, smk_ptrace_mode(mode), saip);
225 return rc;
226}
227
Casey Schauflere114e472008-02-04 22:29:50 -0800228/*
229 * LSM hooks.
230 * We he, that is fun!
231 */
232
233/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000234 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800235 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100236 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800237 *
238 * Returns 0 if access is OK, an error code otherwise
239 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100240 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800241 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000242static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800243{
244 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700245 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800246
Ingo Molnar9e488582009-05-07 19:26:19 +1000247 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800248 if (rc != 0)
249 return rc;
250
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700251 skp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200252
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100253 rc = smk_ptrace_rule_check(current, skp->smk_known, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100254 return rc;
255}
Casey Schauflere114e472008-02-04 22:29:50 -0800256
David Howells5cd9c582008-08-14 11:37:28 +0100257/**
258 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
259 * @ptp: parent task pointer
260 *
261 * Returns 0 if access is OK, an error code otherwise
262 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100263 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100264 */
265static int smack_ptrace_traceme(struct task_struct *ptp)
266{
267 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700268 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100269
270 rc = cap_ptrace_traceme(ptp);
271 if (rc != 0)
272 return rc;
273
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100274 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200275
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100276 rc = smk_ptrace_rule_check(ptp, skp->smk_known,
277 PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800278 return rc;
279}
280
281/**
282 * smack_syslog - Smack approval on syslog
283 * @type: message type
284 *
Casey Schauflere114e472008-02-04 22:29:50 -0800285 * Returns 0 on success, error code otherwise.
286 */
Eric Paris12b30522010-11-15 18:36:29 -0500287static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800288{
Eric Paris12b30522010-11-15 18:36:29 -0500289 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700290 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800291
Casey Schaufler1880eff2012-06-05 15:28:30 -0700292 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800293 return 0;
294
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800295 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800296 rc = -EACCES;
297
298 return rc;
299}
300
301
302/*
303 * Superblock Hooks.
304 */
305
306/**
307 * smack_sb_alloc_security - allocate a superblock blob
308 * @sb: the superblock getting the blob
309 *
310 * Returns 0 on success or -ENOMEM on error.
311 */
312static int smack_sb_alloc_security(struct super_block *sb)
313{
314 struct superblock_smack *sbsp;
315
316 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
317
318 if (sbsp == NULL)
319 return -ENOMEM;
320
321 sbsp->smk_root = smack_known_floor.smk_known;
322 sbsp->smk_default = smack_known_floor.smk_known;
323 sbsp->smk_floor = smack_known_floor.smk_known;
324 sbsp->smk_hat = smack_known_hat.smk_known;
Casey Schauflere830b392013-05-22 18:43:07 -0700325 /*
326 * smk_initialized will be zero from kzalloc.
327 */
Casey Schauflere114e472008-02-04 22:29:50 -0800328 sb->s_security = sbsp;
329
330 return 0;
331}
332
333/**
334 * smack_sb_free_security - free a superblock blob
335 * @sb: the superblock getting the blob
336 *
337 */
338static void smack_sb_free_security(struct super_block *sb)
339{
340 kfree(sb->s_security);
341 sb->s_security = NULL;
342}
343
344/**
345 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800346 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800347 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800348 *
349 * Returns 0 on success or -ENOMEM on error.
350 *
351 * Copy the Smack specific mount options out of the mount
352 * options list.
353 */
Eric Parise0007522008-03-05 10:31:54 -0500354static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800355{
356 char *cp, *commap, *otheropts, *dp;
357
Casey Schauflere114e472008-02-04 22:29:50 -0800358 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
359 if (otheropts == NULL)
360 return -ENOMEM;
361
362 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
363 if (strstr(cp, SMK_FSDEFAULT) == cp)
364 dp = smackopts;
365 else if (strstr(cp, SMK_FSFLOOR) == cp)
366 dp = smackopts;
367 else if (strstr(cp, SMK_FSHAT) == cp)
368 dp = smackopts;
369 else if (strstr(cp, SMK_FSROOT) == cp)
370 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700371 else if (strstr(cp, SMK_FSTRANS) == cp)
372 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800373 else
374 dp = otheropts;
375
376 commap = strchr(cp, ',');
377 if (commap != NULL)
378 *commap = '\0';
379
380 if (*dp != '\0')
381 strcat(dp, ",");
382 strcat(dp, cp);
383 }
384
385 strcpy(orig, otheropts);
386 free_page((unsigned long)otheropts);
387
388 return 0;
389}
390
391/**
392 * smack_sb_kern_mount - Smack specific mount processing
393 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100394 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800395 * @data: the smack mount options
396 *
397 * Returns 0 on success, an error code on failure
398 */
James Morris12204e22008-12-19 10:44:42 +1100399static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800400{
401 struct dentry *root = sb->s_root;
402 struct inode *inode = root->d_inode;
403 struct superblock_smack *sp = sb->s_security;
404 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800405 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800406 char *op;
407 char *commap;
408 char *nsp;
Casey Schauflere830b392013-05-22 18:43:07 -0700409 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800410 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800411
Casey Schauflere830b392013-05-22 18:43:07 -0700412 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800413 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700414
Casey Schauflere114e472008-02-04 22:29:50 -0800415 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800416
417 for (op = data; op != NULL; op = commap) {
418 commap = strchr(op, ',');
419 if (commap != NULL)
420 *commap++ = '\0';
421
422 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
423 op += strlen(SMK_FSHAT);
424 nsp = smk_import(op, 0);
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800425 if (nsp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800426 sp->smk_hat = nsp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800427 specified = 1;
428 }
Casey Schauflere114e472008-02-04 22:29:50 -0800429 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
430 op += strlen(SMK_FSFLOOR);
431 nsp = smk_import(op, 0);
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800432 if (nsp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800433 sp->smk_floor = nsp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800434 specified = 1;
435 }
Casey Schauflere114e472008-02-04 22:29:50 -0800436 } else if (strncmp(op, SMK_FSDEFAULT,
437 strlen(SMK_FSDEFAULT)) == 0) {
438 op += strlen(SMK_FSDEFAULT);
439 nsp = smk_import(op, 0);
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800440 if (nsp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800441 sp->smk_default = nsp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800442 specified = 1;
443 }
Casey Schauflere114e472008-02-04 22:29:50 -0800444 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
445 op += strlen(SMK_FSROOT);
446 nsp = smk_import(op, 0);
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800447 if (nsp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800448 sp->smk_root = nsp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800449 specified = 1;
450 }
Casey Schauflere830b392013-05-22 18:43:07 -0700451 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
452 op += strlen(SMK_FSTRANS);
453 nsp = smk_import(op, 0);
454 if (nsp != NULL) {
455 sp->smk_root = nsp;
456 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800457 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700458 }
Casey Schauflere114e472008-02-04 22:29:50 -0800459 }
460 }
461
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800462 if (!smack_privileged(CAP_MAC_ADMIN)) {
463 /*
464 * Unprivileged mounts don't get to specify Smack values.
465 */
466 if (specified)
467 return -EPERM;
468 /*
469 * Unprivileged mounts get root and default from the caller.
470 */
471 skp = smk_of_current();
472 sp->smk_root = skp->smk_known;
473 sp->smk_default = skp->smk_known;
474 }
Casey Schauflere114e472008-02-04 22:29:50 -0800475 /*
476 * Initialize the root inode.
477 */
478 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100479 if (isp == NULL) {
480 isp = new_inode_smack(sp->smk_root);
481 if (isp == NULL)
482 return -ENOMEM;
483 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700484 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800485 isp->smk_inode = sp->smk_root;
486
Casey Schauflere830b392013-05-22 18:43:07 -0700487 if (transmute)
488 isp->smk_flags |= SMK_INODE_TRANSMUTE;
489
Casey Schauflere114e472008-02-04 22:29:50 -0800490 return 0;
491}
492
493/**
494 * smack_sb_statfs - Smack check on statfs
495 * @dentry: identifies the file system in question
496 *
497 * Returns 0 if current can read the floor of the filesystem,
498 * and error code otherwise
499 */
500static int smack_sb_statfs(struct dentry *dentry)
501{
502 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200503 int rc;
504 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800505
Eric Parisa2694342011-04-25 13:10:27 -0400506 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200507 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
508
509 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
510 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800511}
512
Casey Schauflere114e472008-02-04 22:29:50 -0800513/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800514 * BPRM hooks
515 */
516
Casey Schauflerce8a4322011-09-29 18:21:01 -0700517/**
518 * smack_bprm_set_creds - set creds for exec
519 * @bprm: the exec information
520 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100521 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700522 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800523static int smack_bprm_set_creds(struct linux_binprm *bprm)
524{
Al Viro496ad9a2013-01-23 17:07:38 -0500525 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300526 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800527 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800528 int rc;
529
530 rc = cap_bprm_set_creds(bprm);
531 if (rc != 0)
532 return rc;
533
534 if (bprm->cred_prepared)
535 return 0;
536
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300537 isp = inode->i_security;
538 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800539 return 0;
540
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100541 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
542 struct task_struct *tracer;
543 rc = 0;
544
545 rcu_read_lock();
546 tracer = ptrace_parent(current);
547 if (likely(tracer != NULL))
548 rc = smk_ptrace_rule_check(tracer,
549 isp->smk_task->smk_known,
550 PTRACE_MODE_ATTACH,
551 __func__);
552 rcu_read_unlock();
553
554 if (rc != 0)
555 return rc;
556 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300557 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800558
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300559 bsp->smk_task = isp->smk_task;
560 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800561
562 return 0;
563}
564
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300565/**
566 * smack_bprm_committing_creds - Prepare to install the new credentials
567 * from bprm.
568 *
569 * @bprm: binprm for exec
570 */
571static void smack_bprm_committing_creds(struct linux_binprm *bprm)
572{
573 struct task_smack *bsp = bprm->cred->security;
574
575 if (bsp->smk_task != bsp->smk_forked)
576 current->pdeath_signal = 0;
577}
578
579/**
580 * smack_bprm_secureexec - Return the decision to use secureexec.
581 * @bprm: binprm for exec
582 *
583 * Returns 0 on success.
584 */
585static int smack_bprm_secureexec(struct linux_binprm *bprm)
586{
587 struct task_smack *tsp = current_security();
588 int ret = cap_bprm_secureexec(bprm);
589
590 if (!ret && (tsp->smk_task != tsp->smk_forked))
591 ret = 1;
592
593 return ret;
594}
595
Casey Schaufler676dac42010-12-02 06:43:39 -0800596/*
Casey Schauflere114e472008-02-04 22:29:50 -0800597 * Inode hooks
598 */
599
600/**
601 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800602 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800603 *
604 * Returns 0 if it gets a blob, -ENOMEM otherwise
605 */
606static int smack_inode_alloc_security(struct inode *inode)
607{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700608 struct smack_known *skp = smk_of_current();
609
610 inode->i_security = new_inode_smack(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -0800611 if (inode->i_security == NULL)
612 return -ENOMEM;
613 return 0;
614}
615
616/**
617 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800618 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800619 *
620 * Clears the blob pointer in inode
621 */
622static void smack_inode_free_security(struct inode *inode)
623{
624 kfree(inode->i_security);
625 inode->i_security = NULL;
626}
627
628/**
629 * smack_inode_init_security - copy out the smack from an inode
630 * @inode: the inode
631 * @dir: unused
Eric Paris2a7dba32011-02-01 11:05:39 -0500632 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800633 * @name: where to put the attribute name
634 * @value: where to put the attribute value
635 * @len: where to put the length of the attribute
636 *
637 * Returns 0 if it all works out, -ENOMEM if there's no memory
638 */
639static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900640 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500641 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800642{
Casey Schaufler2267b132012-03-13 19:14:19 -0700643 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700644 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800645 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200646 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800647 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800648
Tetsuo Handa95489062013-07-25 05:44:02 +0900649 if (name)
650 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800651
652 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800653 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700654 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800655 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200656
657 /*
658 * If the access rule allows transmutation and
659 * the directory requests transmutation then
660 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700661 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200662 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800663 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700664 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200665 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700666 issp->smk_flags |= SMK_INODE_CHANGED;
667 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200668
Tetsuo Handaceffec552012-03-29 16:19:05 +0900669 *value = kstrdup(isp, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800670 if (*value == NULL)
671 return -ENOMEM;
672 }
673
674 if (len)
675 *len = strlen(isp) + 1;
676
677 return 0;
678}
679
680/**
681 * smack_inode_link - Smack check on link
682 * @old_dentry: the existing object
683 * @dir: unused
684 * @new_dentry: the new object
685 *
686 * Returns 0 if access is permitted, an error code otherwise
687 */
688static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
689 struct dentry *new_dentry)
690{
Casey Schauflere114e472008-02-04 22:29:50 -0800691 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200692 struct smk_audit_info ad;
693 int rc;
694
Eric Parisa2694342011-04-25 13:10:27 -0400695 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200696 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800697
698 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200699 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800700
701 if (rc == 0 && new_dentry->d_inode != NULL) {
702 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200703 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
704 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800705 }
706
707 return rc;
708}
709
710/**
711 * smack_inode_unlink - Smack check on inode deletion
712 * @dir: containing directory object
713 * @dentry: file to unlink
714 *
715 * Returns 0 if current can write the containing directory
716 * and the object, error code otherwise
717 */
718static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
719{
720 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200721 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800722 int rc;
723
Eric Parisa2694342011-04-25 13:10:27 -0400724 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200725 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
726
Casey Schauflere114e472008-02-04 22:29:50 -0800727 /*
728 * You need write access to the thing you're unlinking
729 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200730 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
731 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800732 /*
733 * You also need write access to the containing directory
734 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400735 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200736 smk_ad_setfield_u_fs_inode(&ad, dir);
737 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
738 }
Casey Schauflere114e472008-02-04 22:29:50 -0800739 return rc;
740}
741
742/**
743 * smack_inode_rmdir - Smack check on directory deletion
744 * @dir: containing directory object
745 * @dentry: directory to unlink
746 *
747 * Returns 0 if current can write the containing directory
748 * and the directory, error code otherwise
749 */
750static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
751{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200752 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800753 int rc;
754
Eric Parisa2694342011-04-25 13:10:27 -0400755 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200756 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
757
Casey Schauflere114e472008-02-04 22:29:50 -0800758 /*
759 * You need write access to the thing you're removing
760 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200761 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
762 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800763 /*
764 * You also need write access to the containing directory
765 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400766 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200767 smk_ad_setfield_u_fs_inode(&ad, dir);
768 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
769 }
Casey Schauflere114e472008-02-04 22:29:50 -0800770
771 return rc;
772}
773
774/**
775 * smack_inode_rename - Smack check on rename
776 * @old_inode: the old directory
777 * @old_dentry: unused
778 * @new_inode: the new directory
779 * @new_dentry: unused
780 *
781 * Read and write access is required on both the old and
782 * new directories.
783 *
784 * Returns 0 if access is permitted, an error code otherwise
785 */
786static int smack_inode_rename(struct inode *old_inode,
787 struct dentry *old_dentry,
788 struct inode *new_inode,
789 struct dentry *new_dentry)
790{
791 int rc;
792 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200793 struct smk_audit_info ad;
794
Eric Parisa2694342011-04-25 13:10:27 -0400795 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200796 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800797
798 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200799 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800800
801 if (rc == 0 && new_dentry->d_inode != NULL) {
802 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200803 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
804 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800805 }
Casey Schauflere114e472008-02-04 22:29:50 -0800806 return rc;
807}
808
809/**
810 * smack_inode_permission - Smack version of permission()
811 * @inode: the inode in question
812 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800813 *
814 * This is the important Smack hook.
815 *
816 * Returns 0 if access is permitted, -EACCES otherwise
817 */
Al Viroe74f71e2011-06-20 19:38:15 -0400818static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800819{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200820 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400821 int no_block = mask & MAY_NOT_BLOCK;
Eric Parisd09ca732010-07-23 11:43:57 -0400822
823 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800824 /*
825 * No permission to check. Existence test. Yup, it's there.
826 */
827 if (mask == 0)
828 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700829
830 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400831 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700832 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400833 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200834 smk_ad_setfield_u_fs_inode(&ad, inode);
835 return smk_curacc(smk_of_inode(inode), mask, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800836}
837
838/**
839 * smack_inode_setattr - Smack check for setting attributes
840 * @dentry: the object
841 * @iattr: for the force flag
842 *
843 * Returns 0 if access is permitted, an error code otherwise
844 */
845static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
846{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200847 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800848 /*
849 * Need to allow for clearing the setuid bit.
850 */
851 if (iattr->ia_valid & ATTR_FORCE)
852 return 0;
Eric Parisa2694342011-04-25 13:10:27 -0400853 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200854 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800855
Etienne Bassetecfcc532009-04-08 20:40:06 +0200856 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800857}
858
859/**
860 * smack_inode_getattr - Smack check for getting attributes
861 * @mnt: unused
862 * @dentry: the object
863 *
864 * Returns 0 if access is permitted, an error code otherwise
865 */
866static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
867{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200868 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -0400869 struct path path;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200870
Eric Parisa2694342011-04-25 13:10:27 -0400871 path.dentry = dentry;
872 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200873
Eric Parisf48b7392011-04-25 12:54:27 -0400874 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -0400875 smk_ad_setfield_u_fs_path(&ad, path);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200876 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -0800877}
878
879/**
880 * smack_inode_setxattr - Smack check for setting xattrs
881 * @dentry: the object
882 * @name: name of the attribute
883 * @value: unused
884 * @size: unused
885 * @flags: unused
886 *
887 * This protects the Smack attribute explicitly.
888 *
889 * Returns 0 if access is permitted, an error code otherwise
890 */
David Howells8f0cfa52008-04-29 00:59:41 -0700891static int smack_inode_setxattr(struct dentry *dentry, const char *name,
892 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800893{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200894 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -0800895 struct smack_known *skp;
896 int check_priv = 0;
897 int check_import = 0;
898 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800899 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800900
Casey Schaufler19760ad2013-12-16 16:27:26 -0800901 /*
902 * Check label validity here so import won't fail in post_setxattr
903 */
Casey Schauflerbcdca222008-02-23 15:24:04 -0800904 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
905 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -0800906 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
907 check_priv = 1;
908 check_import = 1;
909 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
910 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
911 check_priv = 1;
912 check_import = 1;
913 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200914 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -0800915 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200916 if (size != TRANS_TRUE_SIZE ||
917 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
918 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -0800919 } else
920 rc = cap_inode_setxattr(dentry, name, value, size, flags);
921
Casey Schaufler19760ad2013-12-16 16:27:26 -0800922 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
923 rc = -EPERM;
924
925 if (rc == 0 && check_import) {
926 skp = smk_import_entry(value, size);
927 if (skp == NULL || (check_star &&
928 (skp == &smack_known_star || skp == &smack_known_web)))
929 rc = -EINVAL;
930 }
931
Eric Parisa2694342011-04-25 13:10:27 -0400932 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200933 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
934
Casey Schauflerbcdca222008-02-23 15:24:04 -0800935 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200936 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -0800937
938 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800939}
940
941/**
942 * smack_inode_post_setxattr - Apply the Smack update approved above
943 * @dentry: object
944 * @name: attribute name
945 * @value: attribute value
946 * @size: attribute size
947 * @flags: unused
948 *
949 * Set the pointer in the inode blob to the entry found
950 * in the master label list.
951 */
David Howells8f0cfa52008-04-29 00:59:41 -0700952static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
953 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800954{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700955 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200956 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800957
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700958 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
959 isp->smk_flags |= SMK_INODE_TRANSMUTE;
960 return;
961 }
962
Casey Schaufler676dac42010-12-02 06:43:39 -0800963 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +0200964 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700965 if (skp != NULL)
966 isp->smk_inode = skp->smk_known;
Casey Schaufler676dac42010-12-02 06:43:39 -0800967 else
968 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200969 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +0200970 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700971 if (skp != NULL)
972 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800973 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700974 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800975 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +0200976 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700977 if (skp != NULL)
978 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800979 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700980 isp->smk_mmap = &smack_known_invalid;
981 }
Casey Schauflere114e472008-02-04 22:29:50 -0800982
983 return;
984}
985
Casey Schauflerce8a4322011-09-29 18:21:01 -0700986/**
Casey Schauflere114e472008-02-04 22:29:50 -0800987 * smack_inode_getxattr - Smack check on getxattr
988 * @dentry: the object
989 * @name: unused
990 *
991 * Returns 0 if access is permitted, an error code otherwise
992 */
David Howells8f0cfa52008-04-29 00:59:41 -0700993static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -0800994{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200995 struct smk_audit_info ad;
996
Eric Parisa2694342011-04-25 13:10:27 -0400997 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200998 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
999
1000 return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001001}
1002
Casey Schauflerce8a4322011-09-29 18:21:01 -07001003/**
Casey Schauflere114e472008-02-04 22:29:50 -08001004 * smack_inode_removexattr - Smack check on removexattr
1005 * @dentry: the object
1006 * @name: name of the attribute
1007 *
1008 * Removing the Smack attribute requires CAP_MAC_ADMIN
1009 *
1010 * Returns 0 if access is permitted, an error code otherwise
1011 */
David Howells8f0cfa52008-04-29 00:59:41 -07001012static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001013{
Casey Schaufler676dac42010-12-02 06:43:39 -08001014 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001015 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001016 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001017
Casey Schauflerbcdca222008-02-23 15:24:04 -08001018 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1019 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001020 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001021 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001022 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301023 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001024 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001025 rc = -EPERM;
1026 } else
1027 rc = cap_inode_removexattr(dentry, name);
1028
Eric Parisa2694342011-04-25 13:10:27 -04001029 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001030 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001031 if (rc == 0)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001032 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001033
Casey Schaufler676dac42010-12-02 06:43:39 -08001034 if (rc == 0) {
1035 isp = dentry->d_inode->i_security;
1036 isp->smk_task = NULL;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001037 isp->smk_mmap = NULL;
Casey Schaufler676dac42010-12-02 06:43:39 -08001038 }
1039
Casey Schauflerbcdca222008-02-23 15:24:04 -08001040 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001041}
1042
1043/**
1044 * smack_inode_getsecurity - get smack xattrs
1045 * @inode: the object
1046 * @name: attribute name
1047 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001048 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001049 *
1050 * Returns the size of the attribute or an error code
1051 */
1052static int smack_inode_getsecurity(const struct inode *inode,
1053 const char *name, void **buffer,
1054 bool alloc)
1055{
1056 struct socket_smack *ssp;
1057 struct socket *sock;
1058 struct super_block *sbp;
1059 struct inode *ip = (struct inode *)inode;
1060 char *isp;
1061 int ilen;
1062 int rc = 0;
1063
1064 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1065 isp = smk_of_inode(inode);
1066 ilen = strlen(isp) + 1;
1067 *buffer = isp;
1068 return ilen;
1069 }
1070
1071 /*
1072 * The rest of the Smack xattrs are only on sockets.
1073 */
1074 sbp = ip->i_sb;
1075 if (sbp->s_magic != SOCKFS_MAGIC)
1076 return -EOPNOTSUPP;
1077
1078 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001079 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001080 return -EOPNOTSUPP;
1081
1082 ssp = sock->sk->sk_security;
1083
1084 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1085 isp = ssp->smk_in;
1086 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001087 isp = ssp->smk_out->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001088 else
1089 return -EOPNOTSUPP;
1090
1091 ilen = strlen(isp) + 1;
1092 if (rc == 0) {
1093 *buffer = isp;
1094 rc = ilen;
1095 }
1096
1097 return rc;
1098}
1099
1100
1101/**
1102 * smack_inode_listsecurity - list the Smack attributes
1103 * @inode: the object
1104 * @buffer: where they go
1105 * @buffer_size: size of buffer
1106 *
1107 * Returns 0 on success, -EINVAL otherwise
1108 */
1109static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1110 size_t buffer_size)
1111{
1112 int len = strlen(XATTR_NAME_SMACK);
1113
1114 if (buffer != NULL && len <= buffer_size) {
1115 memcpy(buffer, XATTR_NAME_SMACK, len);
1116 return len;
1117 }
1118 return -EINVAL;
1119}
1120
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001121/**
1122 * smack_inode_getsecid - Extract inode's security id
1123 * @inode: inode to extract the info from
1124 * @secid: where result will be saved
1125 */
1126static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1127{
1128 struct inode_smack *isp = inode->i_security;
1129
1130 *secid = smack_to_secid(isp->smk_inode);
1131}
1132
Casey Schauflere114e472008-02-04 22:29:50 -08001133/*
1134 * File Hooks
1135 */
1136
1137/**
1138 * smack_file_permission - Smack check on file operations
1139 * @file: unused
1140 * @mask: unused
1141 *
1142 * Returns 0
1143 *
1144 * Should access checks be done on each read or write?
1145 * UNICOS and SELinux say yes.
1146 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1147 *
1148 * I'll say no for now. Smack does not do the frequent
1149 * label changing that SELinux does.
1150 */
1151static int smack_file_permission(struct file *file, int mask)
1152{
1153 return 0;
1154}
1155
1156/**
1157 * smack_file_alloc_security - assign a file security blob
1158 * @file: the object
1159 *
1160 * The security blob for a file is a pointer to the master
1161 * label list, so no allocation is done.
1162 *
1163 * Returns 0
1164 */
1165static int smack_file_alloc_security(struct file *file)
1166{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001167 struct smack_known *skp = smk_of_current();
1168
1169 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001170 return 0;
1171}
1172
1173/**
1174 * smack_file_free_security - clear a file security blob
1175 * @file: the object
1176 *
1177 * The security blob for a file is a pointer to the master
1178 * label list, so no memory is freed.
1179 */
1180static void smack_file_free_security(struct file *file)
1181{
1182 file->f_security = NULL;
1183}
1184
1185/**
1186 * smack_file_ioctl - Smack check on ioctls
1187 * @file: the object
1188 * @cmd: what to do
1189 * @arg: unused
1190 *
1191 * Relies heavily on the correct use of the ioctl command conventions.
1192 *
1193 * Returns 0 if allowed, error code otherwise
1194 */
1195static int smack_file_ioctl(struct file *file, unsigned int cmd,
1196 unsigned long arg)
1197{
1198 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001199 struct smk_audit_info ad;
1200
Eric Parisf48b7392011-04-25 12:54:27 -04001201 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001202 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001203
1204 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001205 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001206
1207 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001208 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001209
1210 return rc;
1211}
1212
1213/**
1214 * smack_file_lock - Smack check on file locking
1215 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001216 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001217 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001218 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001219 */
1220static int smack_file_lock(struct file *file, unsigned int cmd)
1221{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001222 struct smk_audit_info ad;
1223
Eric Paris92f42502011-04-25 13:15:55 -04001224 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1225 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001226 return smk_curacc(file->f_security, MAY_LOCK, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001227}
1228
1229/**
1230 * smack_file_fcntl - Smack check on fcntl
1231 * @file: the object
1232 * @cmd: what action to check
1233 * @arg: unused
1234 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001235 * Generally these operations are harmless.
1236 * File locking operations present an obvious mechanism
1237 * for passing information, so they require write access.
1238 *
Casey Schauflere114e472008-02-04 22:29:50 -08001239 * Returns 0 if current has access, error code otherwise
1240 */
1241static int smack_file_fcntl(struct file *file, unsigned int cmd,
1242 unsigned long arg)
1243{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001244 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001245 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001246
Etienne Bassetecfcc532009-04-08 20:40:06 +02001247
Casey Schauflere114e472008-02-04 22:29:50 -08001248 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001249 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001250 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001251 case F_SETLK:
1252 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001253 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1254 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1255 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1256 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001257 case F_SETOWN:
1258 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001259 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1260 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001261 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001262 break;
1263 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001264 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001265 }
1266
1267 return rc;
1268}
1269
1270/**
Al Viroe5467852012-05-30 13:30:51 -04001271 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001272 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1273 * if mapping anonymous memory.
1274 * @file contains the file structure for file to map (may be NULL).
1275 * @reqprot contains the protection requested by the application.
1276 * @prot contains the protection that will be applied by the kernel.
1277 * @flags contains the operational flags.
1278 * Return 0 if permission is granted.
1279 */
Al Viroe5467852012-05-30 13:30:51 -04001280static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001281 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001282 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001283{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001284 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001285 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001286 struct smack_rule *srp;
1287 struct task_smack *tsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001288 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001289 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001290 int may;
1291 int mmay;
1292 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001293 int rc;
1294
Al Viro496ad9a2013-01-23 17:07:38 -05001295 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001296 return 0;
1297
Al Viro496ad9a2013-01-23 17:07:38 -05001298 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001299 if (isp->smk_mmap == NULL)
1300 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001301 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001302
1303 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001304 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001305 rc = 0;
1306
1307 rcu_read_lock();
1308 /*
1309 * For each Smack rule associated with the subject
1310 * label verify that the SMACK64MMAP also has access
1311 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001312 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001313 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001314 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001315 /*
1316 * Matching labels always allows access.
1317 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001318 if (mkp->smk_known == osmack)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001319 continue;
1320 /*
1321 * If there is a matching local rule take
1322 * that into account as well.
1323 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001324 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001325 &tsp->smk_rules);
1326 if (may == -ENOENT)
1327 may = srp->smk_access;
1328 else
1329 may &= srp->smk_access;
1330 /*
1331 * If may is zero the SMACK64MMAP subject can't
1332 * possibly have less access.
1333 */
1334 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001335 continue;
1336
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001337 /*
1338 * Fetch the global list entry.
1339 * If there isn't one a SMACK64MMAP subject
1340 * can't have as much access as current.
1341 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001342 mmay = smk_access_entry(mkp->smk_known, osmack,
1343 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001344 if (mmay == -ENOENT) {
1345 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001346 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001347 }
1348 /*
1349 * If there is a local entry it modifies the
1350 * potential access, too.
1351 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001352 tmay = smk_access_entry(mkp->smk_known, osmack,
1353 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001354 if (tmay != -ENOENT)
1355 mmay &= tmay;
1356
1357 /*
1358 * If there is any access available to current that is
1359 * not available to a SMACK64MMAP subject
1360 * deny access.
1361 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001362 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001363 rc = -EACCES;
1364 break;
1365 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001366 }
1367
1368 rcu_read_unlock();
1369
1370 return rc;
1371}
1372
1373/**
Casey Schauflere114e472008-02-04 22:29:50 -08001374 * smack_file_set_fowner - set the file security blob value
1375 * @file: object in question
1376 *
1377 * Returns 0
1378 * Further research may be required on this one.
1379 */
1380static int smack_file_set_fowner(struct file *file)
1381{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001382 struct smack_known *skp = smk_of_current();
1383
1384 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001385 return 0;
1386}
1387
1388/**
1389 * smack_file_send_sigiotask - Smack on sigio
1390 * @tsk: The target task
1391 * @fown: the object the signal come from
1392 * @signum: unused
1393 *
1394 * Allow a privileged task to get signals even if it shouldn't
1395 *
1396 * Returns 0 if a subject with the object's smack could
1397 * write to the task, an error code otherwise.
1398 */
1399static int smack_file_send_sigiotask(struct task_struct *tsk,
1400 struct fown_struct *fown, int signum)
1401{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001402 struct smack_known *skp;
1403 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001404 struct file *file;
1405 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001406 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001407
1408 /*
1409 * struct fown_struct is never outside the context of a struct file
1410 */
1411 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001412
Etienne Bassetecfcc532009-04-08 20:40:06 +02001413 /* we don't log here as rc can be overriden */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001414 skp = smk_find_entry(file->f_security);
1415 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001416 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001417 rc = 0;
1418
1419 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1420 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001421 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001422 return rc;
1423}
1424
1425/**
1426 * smack_file_receive - Smack file receive check
1427 * @file: the object
1428 *
1429 * Returns 0 if current has access, error code otherwise
1430 */
1431static int smack_file_receive(struct file *file)
1432{
1433 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001434 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001435
Casey Schaufler4482a442013-12-30 17:37:45 -08001436 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001437 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001438 /*
1439 * This code relies on bitmasks.
1440 */
1441 if (file->f_mode & FMODE_READ)
1442 may = MAY_READ;
1443 if (file->f_mode & FMODE_WRITE)
1444 may |= MAY_WRITE;
1445
Etienne Bassetecfcc532009-04-08 20:40:06 +02001446 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001447}
1448
Casey Schaufler531f1d42011-09-19 12:41:42 -07001449/**
Eric Paris83d49852012-04-04 13:45:40 -04001450 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001451 * @file: the object
1452 * @cred: unused
1453 *
1454 * Set the security blob in the file structure.
1455 *
1456 * Returns 0
1457 */
Eric Paris83d49852012-04-04 13:45:40 -04001458static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001459{
Al Viro496ad9a2013-01-23 17:07:38 -05001460 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001461
1462 file->f_security = isp->smk_inode;
1463
1464 return 0;
1465}
1466
Casey Schauflere114e472008-02-04 22:29:50 -08001467/*
1468 * Task hooks
1469 */
1470
1471/**
David Howellsee18d642009-09-02 09:14:21 +01001472 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1473 * @new: the new credentials
1474 * @gfp: the atomicity of any memory allocations
1475 *
1476 * Prepare a blank set of credentials for modification. This must allocate all
1477 * the memory the LSM module might require such that cred_transfer() can
1478 * complete without error.
1479 */
1480static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1481{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001482 struct task_smack *tsp;
1483
1484 tsp = new_task_smack(NULL, NULL, gfp);
1485 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001486 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001487
1488 cred->security = tsp;
1489
David Howellsee18d642009-09-02 09:14:21 +01001490 return 0;
1491}
1492
1493
1494/**
David Howellsf1752ee2008-11-14 10:39:17 +11001495 * smack_cred_free - "free" task-level security credentials
1496 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001497 *
Casey Schauflere114e472008-02-04 22:29:50 -08001498 */
David Howellsf1752ee2008-11-14 10:39:17 +11001499static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001500{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001501 struct task_smack *tsp = cred->security;
1502 struct smack_rule *rp;
1503 struct list_head *l;
1504 struct list_head *n;
1505
1506 if (tsp == NULL)
1507 return;
1508 cred->security = NULL;
1509
1510 list_for_each_safe(l, n, &tsp->smk_rules) {
1511 rp = list_entry(l, struct smack_rule, list);
1512 list_del(&rp->list);
1513 kfree(rp);
1514 }
1515 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001516}
1517
1518/**
David Howellsd84f4f92008-11-14 10:39:23 +11001519 * smack_cred_prepare - prepare new set of credentials for modification
1520 * @new: the new credentials
1521 * @old: the original credentials
1522 * @gfp: the atomicity of any memory allocations
1523 *
1524 * Prepare a new set of credentials for modification.
1525 */
1526static int smack_cred_prepare(struct cred *new, const struct cred *old,
1527 gfp_t gfp)
1528{
Casey Schaufler676dac42010-12-02 06:43:39 -08001529 struct task_smack *old_tsp = old->security;
1530 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001531 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001532
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001533 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001534 if (new_tsp == NULL)
1535 return -ENOMEM;
1536
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001537 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1538 if (rc != 0)
1539 return rc;
1540
Casey Schaufler676dac42010-12-02 06:43:39 -08001541 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001542 return 0;
1543}
1544
Randy Dunlap251a2a92009-02-18 11:42:33 -08001545/**
David Howellsee18d642009-09-02 09:14:21 +01001546 * smack_cred_transfer - Transfer the old credentials to the new credentials
1547 * @new: the new credentials
1548 * @old: the original credentials
1549 *
1550 * Fill in a set of blank credentials from another set of credentials.
1551 */
1552static void smack_cred_transfer(struct cred *new, const struct cred *old)
1553{
Casey Schaufler676dac42010-12-02 06:43:39 -08001554 struct task_smack *old_tsp = old->security;
1555 struct task_smack *new_tsp = new->security;
1556
1557 new_tsp->smk_task = old_tsp->smk_task;
1558 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001559 mutex_init(&new_tsp->smk_rules_lock);
1560 INIT_LIST_HEAD(&new_tsp->smk_rules);
1561
1562
1563 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001564}
1565
1566/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001567 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001568 * @new: points to the set of credentials to be modified.
1569 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001570 *
1571 * Set the security data for a kernel service.
1572 */
1573static int smack_kernel_act_as(struct cred *new, u32 secid)
1574{
Casey Schaufler676dac42010-12-02 06:43:39 -08001575 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001576 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001577
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001578 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001579 return -EINVAL;
1580
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001581 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001582 return 0;
1583}
1584
1585/**
1586 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001587 * @new: points to the set of credentials to be modified
1588 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001589 *
1590 * Set the file creation context in a set of credentials to the same
1591 * as the objective context of the specified inode
1592 */
1593static int smack_kernel_create_files_as(struct cred *new,
1594 struct inode *inode)
1595{
1596 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001597 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001598
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001599 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1600 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001601 return 0;
1602}
1603
1604/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001605 * smk_curacc_on_task - helper to log task related access
1606 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001607 * @access: the access requested
1608 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001609 *
1610 * Return 0 if access is permitted
1611 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001612static int smk_curacc_on_task(struct task_struct *p, int access,
1613 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001614{
1615 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001616 struct smack_known *skp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001617
Casey Schaufler531f1d42011-09-19 12:41:42 -07001618 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001619 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001620 return smk_curacc(skp->smk_known, access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001621}
1622
1623/**
Casey Schauflere114e472008-02-04 22:29:50 -08001624 * smack_task_setpgid - Smack check on setting pgid
1625 * @p: the task object
1626 * @pgid: unused
1627 *
1628 * Return 0 if write access is permitted
1629 */
1630static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1631{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001632 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001633}
1634
1635/**
1636 * smack_task_getpgid - Smack access check for getpgid
1637 * @p: the object task
1638 *
1639 * Returns 0 if current can read the object task, error code otherwise
1640 */
1641static int smack_task_getpgid(struct task_struct *p)
1642{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001643 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001644}
1645
1646/**
1647 * smack_task_getsid - Smack access check for getsid
1648 * @p: the object task
1649 *
1650 * Returns 0 if current can read the object task, error code otherwise
1651 */
1652static int smack_task_getsid(struct task_struct *p)
1653{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001654 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001655}
1656
1657/**
1658 * smack_task_getsecid - get the secid of the task
1659 * @p: the object task
1660 * @secid: where to put the result
1661 *
1662 * Sets the secid to contain a u32 version of the smack label.
1663 */
1664static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1665{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001666 struct smack_known *skp = smk_of_task(task_security(p));
1667
1668 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001669}
1670
1671/**
1672 * smack_task_setnice - Smack check on setting nice
1673 * @p: the task object
1674 * @nice: unused
1675 *
1676 * Return 0 if write access is permitted
1677 */
1678static int smack_task_setnice(struct task_struct *p, int nice)
1679{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001680 int rc;
1681
1682 rc = cap_task_setnice(p, nice);
1683 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001684 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001685 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001686}
1687
1688/**
1689 * smack_task_setioprio - Smack check on setting ioprio
1690 * @p: the task object
1691 * @ioprio: unused
1692 *
1693 * Return 0 if write access is permitted
1694 */
1695static int smack_task_setioprio(struct task_struct *p, int ioprio)
1696{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001697 int rc;
1698
1699 rc = cap_task_setioprio(p, ioprio);
1700 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001701 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001702 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001703}
1704
1705/**
1706 * smack_task_getioprio - Smack check on reading ioprio
1707 * @p: the task object
1708 *
1709 * Return 0 if read access is permitted
1710 */
1711static int smack_task_getioprio(struct task_struct *p)
1712{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001713 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001714}
1715
1716/**
1717 * smack_task_setscheduler - Smack check on setting scheduler
1718 * @p: the task object
1719 * @policy: unused
1720 * @lp: unused
1721 *
1722 * Return 0 if read access is permitted
1723 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001724static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001725{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001726 int rc;
1727
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001728 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001729 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001730 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001731 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001732}
1733
1734/**
1735 * smack_task_getscheduler - Smack check on reading scheduler
1736 * @p: the task object
1737 *
1738 * Return 0 if read access is permitted
1739 */
1740static int smack_task_getscheduler(struct task_struct *p)
1741{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001742 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001743}
1744
1745/**
1746 * smack_task_movememory - Smack check on moving memory
1747 * @p: the task object
1748 *
1749 * Return 0 if write access is permitted
1750 */
1751static int smack_task_movememory(struct task_struct *p)
1752{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001753 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001754}
1755
1756/**
1757 * smack_task_kill - Smack check on signal delivery
1758 * @p: the task object
1759 * @info: unused
1760 * @sig: unused
1761 * @secid: identifies the smack to use in lieu of current's
1762 *
1763 * Return 0 if write access is permitted
1764 *
1765 * The secid behavior is an artifact of an SELinux hack
1766 * in the USB code. Someday it may go away.
1767 */
1768static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1769 int sig, u32 secid)
1770{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001771 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001772 struct smack_known *skp;
1773 struct smack_known *tkp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001774
1775 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1776 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001777 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001778 * Sending a signal requires that the sender
1779 * can write the receiver.
1780 */
1781 if (secid == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001782 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001783 /*
1784 * If the secid isn't 0 we're dealing with some USB IO
1785 * specific behavior. This is not clean. For one thing
1786 * we can't take privilege into account.
1787 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001788 skp = smack_from_secid(secid);
1789 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001790}
1791
1792/**
1793 * smack_task_wait - Smack access check for waiting
1794 * @p: task to wait for
1795 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001796 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08001797 */
1798static int smack_task_wait(struct task_struct *p)
1799{
Casey Schauflere114e472008-02-04 22:29:50 -08001800 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001801 * Allow the operation to succeed.
1802 * Zombies are bad.
1803 * In userless environments (e.g. phones) programs
1804 * get marked with SMACK64EXEC and even if the parent
1805 * and child shouldn't be talking the parent still
1806 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08001807 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001808 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001809}
1810
1811/**
1812 * smack_task_to_inode - copy task smack into the inode blob
1813 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001814 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001815 *
1816 * Sets the smack pointer in the inode security blob
1817 */
1818static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1819{
1820 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001821 struct smack_known *skp = smk_of_task(task_security(p));
1822
1823 isp->smk_inode = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001824}
1825
1826/*
1827 * Socket hooks.
1828 */
1829
1830/**
1831 * smack_sk_alloc_security - Allocate a socket blob
1832 * @sk: the socket
1833 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001834 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001835 *
1836 * Assign Smack pointers to current
1837 *
1838 * Returns 0 on success, -ENOMEM is there's no memory
1839 */
1840static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1841{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001842 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001843 struct socket_smack *ssp;
1844
1845 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1846 if (ssp == NULL)
1847 return -ENOMEM;
1848
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001849 ssp->smk_in = skp->smk_known;
1850 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001851 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001852
1853 sk->sk_security = ssp;
1854
1855 return 0;
1856}
1857
1858/**
1859 * smack_sk_free_security - Free a socket blob
1860 * @sk: the socket
1861 *
1862 * Clears the blob pointer
1863 */
1864static void smack_sk_free_security(struct sock *sk)
1865{
1866 kfree(sk->sk_security);
1867}
1868
1869/**
Paul Moore07feee82009-03-27 17:10:54 -04001870* smack_host_label - check host based restrictions
1871* @sip: the object end
1872*
1873* looks for host based access restrictions
1874*
1875* This version will only be appropriate for really small sets of single label
1876* hosts. The caller is responsible for ensuring that the RCU read lock is
1877* taken before calling this function.
1878*
1879* Returns the label of the far end or NULL if it's not special.
1880*/
1881static char *smack_host_label(struct sockaddr_in *sip)
1882{
1883 struct smk_netlbladdr *snp;
1884 struct in_addr *siap = &sip->sin_addr;
1885
1886 if (siap->s_addr == 0)
1887 return NULL;
1888
1889 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1890 /*
1891 * we break after finding the first match because
1892 * the list is sorted from longest to shortest mask
1893 * so we have found the most specific match
1894 */
1895 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001896 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1897 /* we have found the special CIPSO option */
1898 if (snp->smk_label == smack_cipso_option)
1899 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001900 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001901 }
Paul Moore07feee82009-03-27 17:10:54 -04001902
1903 return NULL;
1904}
1905
1906/**
Casey Schauflere114e472008-02-04 22:29:50 -08001907 * smack_netlabel - Set the secattr on a socket
1908 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001909 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001910 *
1911 * Convert the outbound smack value (smk_out) to a
1912 * secattr and attach it to the socket.
1913 *
1914 * Returns 0 on success or an error code
1915 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001916static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001917{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001918 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001919 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001920 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001921
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001922 /*
1923 * Usually the netlabel code will handle changing the
1924 * packet labeling based on the label.
1925 * The case of a single label host is different, because
1926 * a single label host should never get a labeled packet
1927 * even though the label is usually associated with a packet
1928 * label.
1929 */
1930 local_bh_disable();
1931 bh_lock_sock_nested(sk);
1932
1933 if (ssp->smk_out == smack_net_ambient ||
1934 labeled == SMACK_UNLABELED_SOCKET)
1935 netlbl_sock_delattr(sk);
1936 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001937 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001938 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001939 }
1940
1941 bh_unlock_sock(sk);
1942 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001943
Casey Schauflere114e472008-02-04 22:29:50 -08001944 return rc;
1945}
1946
1947/**
Paul Moore07feee82009-03-27 17:10:54 -04001948 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1949 * @sk: the socket
1950 * @sap: the destination address
1951 *
1952 * Set the correct secattr for the given socket based on the destination
1953 * address and perform any outbound access checks needed.
1954 *
1955 * Returns 0 on success or an error code.
1956 *
1957 */
1958static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1959{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001960 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001961 int rc;
1962 int sk_lbl;
1963 char *hostsp;
1964 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001965 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001966
1967 rcu_read_lock();
1968 hostsp = smack_host_label(sap);
1969 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001970#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001971 struct lsm_network_audit net;
1972
Eric Paris48c62af2012-04-02 13:15:44 -04001973 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1974 ad.a.u.net->family = sap->sin_family;
1975 ad.a.u.net->dport = sap->sin_port;
1976 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001977#endif
Kees Cook923e9a12012-04-10 13:26:44 -07001978 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001979 skp = ssp->smk_out;
1980 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04001981 } else {
1982 sk_lbl = SMACK_CIPSO_SOCKET;
1983 rc = 0;
1984 }
1985 rcu_read_unlock();
1986 if (rc != 0)
1987 return rc;
1988
1989 return smack_netlabel(sk, sk_lbl);
1990}
1991
1992/**
Casey Schauflerc6739442013-05-22 18:42:56 -07001993 * smk_ipv6_port_label - Smack port access table management
1994 * @sock: socket
1995 * @address: address
1996 *
1997 * Create or update the port list entry
1998 */
1999static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2000{
2001 struct sock *sk = sock->sk;
2002 struct sockaddr_in6 *addr6;
2003 struct socket_smack *ssp = sock->sk->sk_security;
2004 struct smk_port_label *spp;
2005 unsigned short port = 0;
2006
2007 if (address == NULL) {
2008 /*
2009 * This operation is changing the Smack information
2010 * on the bound socket. Take the changes to the port
2011 * as well.
2012 */
2013 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2014 if (sk != spp->smk_sock)
2015 continue;
2016 spp->smk_in = ssp->smk_in;
2017 spp->smk_out = ssp->smk_out;
2018 return;
2019 }
2020 /*
2021 * A NULL address is only used for updating existing
2022 * bound entries. If there isn't one, it's OK.
2023 */
2024 return;
2025 }
2026
2027 addr6 = (struct sockaddr_in6 *)address;
2028 port = ntohs(addr6->sin6_port);
2029 /*
2030 * This is a special case that is safely ignored.
2031 */
2032 if (port == 0)
2033 return;
2034
2035 /*
2036 * Look for an existing port list entry.
2037 * This is an indication that a port is getting reused.
2038 */
2039 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2040 if (spp->smk_port != port)
2041 continue;
2042 spp->smk_port = port;
2043 spp->smk_sock = sk;
2044 spp->smk_in = ssp->smk_in;
2045 spp->smk_out = ssp->smk_out;
2046 return;
2047 }
2048
2049 /*
2050 * A new port entry is required.
2051 */
2052 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2053 if (spp == NULL)
2054 return;
2055
2056 spp->smk_port = port;
2057 spp->smk_sock = sk;
2058 spp->smk_in = ssp->smk_in;
2059 spp->smk_out = ssp->smk_out;
2060
2061 list_add(&spp->list, &smk_ipv6_port_list);
2062 return;
2063}
2064
2065/**
2066 * smk_ipv6_port_check - check Smack port access
2067 * @sock: socket
2068 * @address: address
2069 *
2070 * Create or update the port list entry
2071 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002072static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002073 int act)
2074{
2075 __be16 *bep;
2076 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002077 struct smk_port_label *spp;
2078 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002079 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002080 unsigned short port = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002081 char *object;
2082 struct smk_audit_info ad;
2083#ifdef CONFIG_AUDIT
2084 struct lsm_network_audit net;
2085#endif
2086
2087 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002088 skp = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002089 object = ssp->smk_in;
2090 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002091 skp = ssp->smk_out;
2092 object = smack_net_ambient->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002093 }
2094
2095 /*
2096 * Get the IP address and port from the address.
2097 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002098 port = ntohs(address->sin6_port);
2099 bep = (__be16 *)(&address->sin6_addr);
2100 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002101
2102 /*
2103 * It's remote, so port lookup does no good.
2104 */
2105 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2106 goto auditout;
2107
2108 /*
2109 * It's local so the send check has to have passed.
2110 */
2111 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002112 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002113 goto auditout;
2114 }
2115
2116 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2117 if (spp->smk_port != port)
2118 continue;
2119 object = spp->smk_in;
2120 if (act == SMK_CONNECTING)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002121 ssp->smk_packet = spp->smk_out->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002122 break;
2123 }
2124
2125auditout:
2126
2127#ifdef CONFIG_AUDIT
2128 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2129 ad.a.u.net->family = sk->sk_family;
2130 ad.a.u.net->dport = port;
2131 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002132 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002133 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002134 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002135#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002136 return smk_access(skp, object, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07002137}
2138
2139/**
Casey Schauflere114e472008-02-04 22:29:50 -08002140 * smack_inode_setsecurity - set smack xattrs
2141 * @inode: the object
2142 * @name: attribute name
2143 * @value: attribute value
2144 * @size: size of the attribute
2145 * @flags: unused
2146 *
2147 * Sets the named attribute in the appropriate blob
2148 *
2149 * Returns 0 on success, or an error code
2150 */
2151static int smack_inode_setsecurity(struct inode *inode, const char *name,
2152 const void *value, size_t size, int flags)
2153{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002154 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002155 struct inode_smack *nsp = inode->i_security;
2156 struct socket_smack *ssp;
2157 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002158 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002159
Casey Schauflerf7112e62012-05-06 15:22:02 -07002160 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302161 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002162
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002163 skp = smk_import_entry(value, size);
2164 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002165 return -EINVAL;
2166
2167 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002168 nsp->smk_inode = skp->smk_known;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002169 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002170 return 0;
2171 }
2172 /*
2173 * The rest of the Smack xattrs are only on sockets.
2174 */
2175 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2176 return -EOPNOTSUPP;
2177
2178 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002179 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002180 return -EOPNOTSUPP;
2181
2182 ssp = sock->sk->sk_security;
2183
2184 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002185 ssp->smk_in = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002186 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002187 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002188 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002189 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2190 if (rc != 0)
2191 printk(KERN_WARNING
2192 "Smack: \"%s\" netlbl error %d.\n",
2193 __func__, -rc);
2194 }
Casey Schauflere114e472008-02-04 22:29:50 -08002195 } else
2196 return -EOPNOTSUPP;
2197
Casey Schauflerc6739442013-05-22 18:42:56 -07002198 if (sock->sk->sk_family == PF_INET6)
2199 smk_ipv6_port_label(sock, NULL);
2200
Casey Schauflere114e472008-02-04 22:29:50 -08002201 return 0;
2202}
2203
2204/**
2205 * smack_socket_post_create - finish socket setup
2206 * @sock: the socket
2207 * @family: protocol family
2208 * @type: unused
2209 * @protocol: unused
2210 * @kern: unused
2211 *
2212 * Sets the netlabel information on the socket
2213 *
2214 * Returns 0 on success, and error code otherwise
2215 */
2216static int smack_socket_post_create(struct socket *sock, int family,
2217 int type, int protocol, int kern)
2218{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002219 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002220 return 0;
2221 /*
2222 * Set the outbound netlbl.
2223 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002224 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2225}
2226
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002227/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002228 * smack_socket_bind - record port binding information.
2229 * @sock: the socket
2230 * @address: the port address
2231 * @addrlen: size of the address
2232 *
2233 * Records the label bound to a port.
2234 *
2235 * Returns 0
2236 */
2237static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2238 int addrlen)
2239{
2240 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2241 smk_ipv6_port_label(sock, address);
2242
2243 return 0;
2244}
2245
2246/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002247 * smack_socket_connect - connect access check
2248 * @sock: the socket
2249 * @sap: the other end
2250 * @addrlen: size of sap
2251 *
2252 * Verifies that a connection may be possible
2253 *
2254 * Returns 0 on success, and error code otherwise
2255 */
2256static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2257 int addrlen)
2258{
Casey Schauflerc6739442013-05-22 18:42:56 -07002259 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002260
Casey Schauflerc6739442013-05-22 18:42:56 -07002261 if (sock->sk == NULL)
2262 return 0;
2263
2264 switch (sock->sk->sk_family) {
2265 case PF_INET:
2266 if (addrlen < sizeof(struct sockaddr_in))
2267 return -EINVAL;
2268 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2269 break;
2270 case PF_INET6:
2271 if (addrlen < sizeof(struct sockaddr_in6))
2272 return -EINVAL;
Casey Schaufler6ea06242013-08-05 13:21:22 -07002273 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2274 SMK_CONNECTING);
Casey Schauflerc6739442013-05-22 18:42:56 -07002275 break;
2276 }
2277 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002278}
2279
2280/**
2281 * smack_flags_to_may - convert S_ to MAY_ values
2282 * @flags: the S_ value
2283 *
2284 * Returns the equivalent MAY_ value
2285 */
2286static int smack_flags_to_may(int flags)
2287{
2288 int may = 0;
2289
2290 if (flags & S_IRUGO)
2291 may |= MAY_READ;
2292 if (flags & S_IWUGO)
2293 may |= MAY_WRITE;
2294 if (flags & S_IXUGO)
2295 may |= MAY_EXEC;
2296
2297 return may;
2298}
2299
2300/**
2301 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2302 * @msg: the object
2303 *
2304 * Returns 0
2305 */
2306static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2307{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002308 struct smack_known *skp = smk_of_current();
2309
2310 msg->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002311 return 0;
2312}
2313
2314/**
2315 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2316 * @msg: the object
2317 *
2318 * Clears the blob pointer
2319 */
2320static void smack_msg_msg_free_security(struct msg_msg *msg)
2321{
2322 msg->security = NULL;
2323}
2324
2325/**
2326 * smack_of_shm - the smack pointer for the shm
2327 * @shp: the object
2328 *
2329 * Returns a pointer to the smack value
2330 */
2331static char *smack_of_shm(struct shmid_kernel *shp)
2332{
2333 return (char *)shp->shm_perm.security;
2334}
2335
2336/**
2337 * smack_shm_alloc_security - Set the security blob for shm
2338 * @shp: the object
2339 *
2340 * Returns 0
2341 */
2342static int smack_shm_alloc_security(struct shmid_kernel *shp)
2343{
2344 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002345 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002346
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002347 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002348 return 0;
2349}
2350
2351/**
2352 * smack_shm_free_security - Clear the security blob for shm
2353 * @shp: the object
2354 *
2355 * Clears the blob pointer
2356 */
2357static void smack_shm_free_security(struct shmid_kernel *shp)
2358{
2359 struct kern_ipc_perm *isp = &shp->shm_perm;
2360
2361 isp->security = NULL;
2362}
2363
2364/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002365 * smk_curacc_shm : check if current has access on shm
2366 * @shp : the object
2367 * @access : access requested
2368 *
2369 * Returns 0 if current has the requested access, error code otherwise
2370 */
2371static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2372{
2373 char *ssp = smack_of_shm(shp);
2374 struct smk_audit_info ad;
2375
2376#ifdef CONFIG_AUDIT
2377 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2378 ad.a.u.ipc_id = shp->shm_perm.id;
2379#endif
2380 return smk_curacc(ssp, access, &ad);
2381}
2382
2383/**
Casey Schauflere114e472008-02-04 22:29:50 -08002384 * smack_shm_associate - Smack access check for shm
2385 * @shp: the object
2386 * @shmflg: access requested
2387 *
2388 * Returns 0 if current has the requested access, error code otherwise
2389 */
2390static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2391{
Casey Schauflere114e472008-02-04 22:29:50 -08002392 int may;
2393
2394 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002395 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002396}
2397
2398/**
2399 * smack_shm_shmctl - Smack access check for shm
2400 * @shp: the object
2401 * @cmd: what it wants to do
2402 *
2403 * Returns 0 if current has the requested access, error code otherwise
2404 */
2405static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2406{
Casey Schauflere114e472008-02-04 22:29:50 -08002407 int may;
2408
2409 switch (cmd) {
2410 case IPC_STAT:
2411 case SHM_STAT:
2412 may = MAY_READ;
2413 break;
2414 case IPC_SET:
2415 case SHM_LOCK:
2416 case SHM_UNLOCK:
2417 case IPC_RMID:
2418 may = MAY_READWRITE;
2419 break;
2420 case IPC_INFO:
2421 case SHM_INFO:
2422 /*
2423 * System level information.
2424 */
2425 return 0;
2426 default:
2427 return -EINVAL;
2428 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002429 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002430}
2431
2432/**
2433 * smack_shm_shmat - Smack access for shmat
2434 * @shp: the object
2435 * @shmaddr: unused
2436 * @shmflg: access requested
2437 *
2438 * Returns 0 if current has the requested access, error code otherwise
2439 */
2440static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2441 int shmflg)
2442{
Casey Schauflere114e472008-02-04 22:29:50 -08002443 int may;
2444
2445 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002446 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002447}
2448
2449/**
2450 * smack_of_sem - the smack pointer for the sem
2451 * @sma: the object
2452 *
2453 * Returns a pointer to the smack value
2454 */
2455static char *smack_of_sem(struct sem_array *sma)
2456{
2457 return (char *)sma->sem_perm.security;
2458}
2459
2460/**
2461 * smack_sem_alloc_security - Set the security blob for sem
2462 * @sma: the object
2463 *
2464 * Returns 0
2465 */
2466static int smack_sem_alloc_security(struct sem_array *sma)
2467{
2468 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002469 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002470
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002471 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002472 return 0;
2473}
2474
2475/**
2476 * smack_sem_free_security - Clear the security blob for sem
2477 * @sma: the object
2478 *
2479 * Clears the blob pointer
2480 */
2481static void smack_sem_free_security(struct sem_array *sma)
2482{
2483 struct kern_ipc_perm *isp = &sma->sem_perm;
2484
2485 isp->security = NULL;
2486}
2487
2488/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002489 * smk_curacc_sem : check if current has access on sem
2490 * @sma : the object
2491 * @access : access requested
2492 *
2493 * Returns 0 if current has the requested access, error code otherwise
2494 */
2495static int smk_curacc_sem(struct sem_array *sma, int access)
2496{
2497 char *ssp = smack_of_sem(sma);
2498 struct smk_audit_info ad;
2499
2500#ifdef CONFIG_AUDIT
2501 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2502 ad.a.u.ipc_id = sma->sem_perm.id;
2503#endif
2504 return smk_curacc(ssp, access, &ad);
2505}
2506
2507/**
Casey Schauflere114e472008-02-04 22:29:50 -08002508 * smack_sem_associate - Smack access check for sem
2509 * @sma: the object
2510 * @semflg: access requested
2511 *
2512 * Returns 0 if current has the requested access, error code otherwise
2513 */
2514static int smack_sem_associate(struct sem_array *sma, int semflg)
2515{
Casey Schauflere114e472008-02-04 22:29:50 -08002516 int may;
2517
2518 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002519 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002520}
2521
2522/**
2523 * smack_sem_shmctl - Smack access check for sem
2524 * @sma: the object
2525 * @cmd: what it wants to do
2526 *
2527 * Returns 0 if current has the requested access, error code otherwise
2528 */
2529static int smack_sem_semctl(struct sem_array *sma, int cmd)
2530{
Casey Schauflere114e472008-02-04 22:29:50 -08002531 int may;
2532
2533 switch (cmd) {
2534 case GETPID:
2535 case GETNCNT:
2536 case GETZCNT:
2537 case GETVAL:
2538 case GETALL:
2539 case IPC_STAT:
2540 case SEM_STAT:
2541 may = MAY_READ;
2542 break;
2543 case SETVAL:
2544 case SETALL:
2545 case IPC_RMID:
2546 case IPC_SET:
2547 may = MAY_READWRITE;
2548 break;
2549 case IPC_INFO:
2550 case SEM_INFO:
2551 /*
2552 * System level information
2553 */
2554 return 0;
2555 default:
2556 return -EINVAL;
2557 }
2558
Etienne Bassetecfcc532009-04-08 20:40:06 +02002559 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002560}
2561
2562/**
2563 * smack_sem_semop - Smack checks of semaphore operations
2564 * @sma: the object
2565 * @sops: unused
2566 * @nsops: unused
2567 * @alter: unused
2568 *
2569 * Treated as read and write in all cases.
2570 *
2571 * Returns 0 if access is allowed, error code otherwise
2572 */
2573static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2574 unsigned nsops, int alter)
2575{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002576 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002577}
2578
2579/**
2580 * smack_msg_alloc_security - Set the security blob for msg
2581 * @msq: the object
2582 *
2583 * Returns 0
2584 */
2585static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2586{
2587 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002588 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002589
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002590 kisp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002591 return 0;
2592}
2593
2594/**
2595 * smack_msg_free_security - Clear the security blob for msg
2596 * @msq: the object
2597 *
2598 * Clears the blob pointer
2599 */
2600static void smack_msg_queue_free_security(struct msg_queue *msq)
2601{
2602 struct kern_ipc_perm *kisp = &msq->q_perm;
2603
2604 kisp->security = NULL;
2605}
2606
2607/**
2608 * smack_of_msq - the smack pointer for the msq
2609 * @msq: the object
2610 *
2611 * Returns a pointer to the smack value
2612 */
2613static char *smack_of_msq(struct msg_queue *msq)
2614{
2615 return (char *)msq->q_perm.security;
2616}
2617
2618/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002619 * smk_curacc_msq : helper to check if current has access on msq
2620 * @msq : the msq
2621 * @access : access requested
2622 *
2623 * return 0 if current has access, error otherwise
2624 */
2625static int smk_curacc_msq(struct msg_queue *msq, int access)
2626{
2627 char *msp = smack_of_msq(msq);
2628 struct smk_audit_info ad;
2629
2630#ifdef CONFIG_AUDIT
2631 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2632 ad.a.u.ipc_id = msq->q_perm.id;
2633#endif
2634 return smk_curacc(msp, access, &ad);
2635}
2636
2637/**
Casey Schauflere114e472008-02-04 22:29:50 -08002638 * smack_msg_queue_associate - Smack access check for msg_queue
2639 * @msq: the object
2640 * @msqflg: access requested
2641 *
2642 * Returns 0 if current has the requested access, error code otherwise
2643 */
2644static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2645{
Casey Schauflere114e472008-02-04 22:29:50 -08002646 int may;
2647
2648 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002649 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002650}
2651
2652/**
2653 * smack_msg_queue_msgctl - Smack access check for msg_queue
2654 * @msq: the object
2655 * @cmd: what it wants to do
2656 *
2657 * Returns 0 if current has the requested access, error code otherwise
2658 */
2659static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2660{
Casey Schauflere114e472008-02-04 22:29:50 -08002661 int may;
2662
2663 switch (cmd) {
2664 case IPC_STAT:
2665 case MSG_STAT:
2666 may = MAY_READ;
2667 break;
2668 case IPC_SET:
2669 case IPC_RMID:
2670 may = MAY_READWRITE;
2671 break;
2672 case IPC_INFO:
2673 case MSG_INFO:
2674 /*
2675 * System level information
2676 */
2677 return 0;
2678 default:
2679 return -EINVAL;
2680 }
2681
Etienne Bassetecfcc532009-04-08 20:40:06 +02002682 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002683}
2684
2685/**
2686 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2687 * @msq: the object
2688 * @msg: unused
2689 * @msqflg: access requested
2690 *
2691 * Returns 0 if current has the requested access, error code otherwise
2692 */
2693static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2694 int msqflg)
2695{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002696 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002697
Etienne Bassetecfcc532009-04-08 20:40:06 +02002698 may = smack_flags_to_may(msqflg);
2699 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002700}
2701
2702/**
2703 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2704 * @msq: the object
2705 * @msg: unused
2706 * @target: unused
2707 * @type: unused
2708 * @mode: unused
2709 *
2710 * Returns 0 if current has read and write access, error code otherwise
2711 */
2712static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2713 struct task_struct *target, long type, int mode)
2714{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002715 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002716}
2717
2718/**
2719 * smack_ipc_permission - Smack access for ipc_permission()
2720 * @ipp: the object permissions
2721 * @flag: access requested
2722 *
2723 * Returns 0 if current has read and write access, error code otherwise
2724 */
2725static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2726{
2727 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002728 int may = smack_flags_to_may(flag);
2729 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002730
Etienne Bassetecfcc532009-04-08 20:40:06 +02002731#ifdef CONFIG_AUDIT
2732 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2733 ad.a.u.ipc_id = ipp->id;
2734#endif
2735 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002736}
2737
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002738/**
2739 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002740 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002741 * @secid: where result will be saved
2742 */
2743static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2744{
2745 char *smack = ipp->security;
2746
2747 *secid = smack_to_secid(smack);
2748}
2749
Casey Schauflere114e472008-02-04 22:29:50 -08002750/**
2751 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002752 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002753 * @inode: the object
2754 *
2755 * Set the inode's security blob if it hasn't been done already.
2756 */
2757static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2758{
2759 struct super_block *sbp;
2760 struct superblock_smack *sbsp;
2761 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002762 struct smack_known *skp;
2763 struct smack_known *ckp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002764 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002765 char trattr[TRANS_TRUE_SIZE];
2766 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002767 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002768 struct dentry *dp;
2769
2770 if (inode == NULL)
2771 return;
2772
2773 isp = inode->i_security;
2774
2775 mutex_lock(&isp->smk_lock);
2776 /*
2777 * If the inode is already instantiated
2778 * take the quick way out
2779 */
2780 if (isp->smk_flags & SMK_INODE_INSTANT)
2781 goto unlockandout;
2782
2783 sbp = inode->i_sb;
2784 sbsp = sbp->s_security;
2785 /*
2786 * We're going to use the superblock default label
2787 * if there's no label on the file.
2788 */
2789 final = sbsp->smk_default;
2790
2791 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002792 * If this is the root inode the superblock
2793 * may be in the process of initialization.
2794 * If that is the case use the root value out
2795 * of the superblock.
2796 */
2797 if (opt_dentry->d_parent == opt_dentry) {
2798 isp->smk_inode = sbsp->smk_root;
2799 isp->smk_flags |= SMK_INODE_INSTANT;
2800 goto unlockandout;
2801 }
2802
2803 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002804 * This is pretty hackish.
2805 * Casey says that we shouldn't have to do
2806 * file system specific code, but it does help
2807 * with keeping it simple.
2808 */
2809 switch (sbp->s_magic) {
2810 case SMACK_MAGIC:
2811 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002812 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002813 * that the smack file system doesn't do
2814 * extended attributes.
2815 */
2816 final = smack_known_star.smk_known;
2817 break;
2818 case PIPEFS_MAGIC:
2819 /*
2820 * Casey says pipes are easy (?)
2821 */
2822 final = smack_known_star.smk_known;
2823 break;
2824 case DEVPTS_SUPER_MAGIC:
2825 /*
2826 * devpts seems content with the label of the task.
2827 * Programs that change smack have to treat the
2828 * pty with respect.
2829 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002830 final = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002831 break;
2832 case SOCKFS_MAGIC:
2833 /*
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002834 * Socket access is controlled by the socket
2835 * structures associated with the task involved.
Casey Schauflere114e472008-02-04 22:29:50 -08002836 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002837 final = smack_known_star.smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002838 break;
2839 case PROC_SUPER_MAGIC:
2840 /*
2841 * Casey says procfs appears not to care.
2842 * The superblock default suffices.
2843 */
2844 break;
2845 case TMPFS_MAGIC:
2846 /*
2847 * Device labels should come from the filesystem,
2848 * but watch out, because they're volitile,
2849 * getting recreated on every reboot.
2850 */
2851 final = smack_known_star.smk_known;
2852 /*
2853 * No break.
2854 *
2855 * If a smack value has been set we want to use it,
2856 * but since tmpfs isn't giving us the opportunity
2857 * to set mount options simulate setting the
2858 * superblock default.
2859 */
2860 default:
2861 /*
2862 * This isn't an understood special case.
2863 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002864 */
2865
2866 /*
2867 * UNIX domain sockets use lower level socket data.
2868 */
2869 if (S_ISSOCK(inode->i_mode)) {
2870 final = smack_known_star.smk_known;
2871 break;
2872 }
2873 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002874 * No xattr support means, alas, no SMACK label.
2875 * Use the aforeapplied default.
2876 * It would be curious if the label of the task
2877 * does not match that assigned.
2878 */
2879 if (inode->i_op->getxattr == NULL)
2880 break;
2881 /*
2882 * Get the dentry for xattr.
2883 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002884 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002885 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2886 if (skp != NULL)
2887 final = skp->smk_known;
Casey Schaufler2267b132012-03-13 19:14:19 -07002888
2889 /*
2890 * Transmuting directory
2891 */
2892 if (S_ISDIR(inode->i_mode)) {
2893 /*
2894 * If this is a new directory and the label was
2895 * transmuted when the inode was initialized
2896 * set the transmute attribute on the directory
2897 * and mark the inode.
2898 *
2899 * If there is a transmute attribute on the
2900 * directory mark the inode.
2901 */
2902 if (isp->smk_flags & SMK_INODE_CHANGED) {
2903 isp->smk_flags &= ~SMK_INODE_CHANGED;
2904 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002905 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002906 TRANS_TRUE, TRANS_TRUE_SIZE,
2907 0);
2908 } else {
2909 rc = inode->i_op->getxattr(dp,
2910 XATTR_NAME_SMACKTRANSMUTE, trattr,
2911 TRANS_TRUE_SIZE);
2912 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2913 TRANS_TRUE_SIZE) != 0)
2914 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002915 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002916 if (rc >= 0)
2917 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002918 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08002919 /*
2920 * Don't let the exec or mmap label be "*" or "@".
2921 */
2922 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2923 if (skp == &smack_known_star || skp == &smack_known_web)
2924 skp = NULL;
2925 isp->smk_task = skp;
2926 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2927 if (skp == &smack_known_star || skp == &smack_known_web)
2928 skp = NULL;
2929 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002930
Casey Schauflere114e472008-02-04 22:29:50 -08002931 dput(dp);
2932 break;
2933 }
2934
2935 if (final == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002936 isp->smk_inode = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002937 else
2938 isp->smk_inode = final;
2939
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002940 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002941
2942unlockandout:
2943 mutex_unlock(&isp->smk_lock);
2944 return;
2945}
2946
2947/**
2948 * smack_getprocattr - Smack process attribute access
2949 * @p: the object task
2950 * @name: the name of the attribute in /proc/.../attr
2951 * @value: where to put the result
2952 *
2953 * Places a copy of the task Smack into value
2954 *
2955 * Returns the length of the smack label or an error code
2956 */
2957static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2958{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002959 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08002960 char *cp;
2961 int slen;
2962
2963 if (strcmp(name, "current") != 0)
2964 return -EINVAL;
2965
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002966 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002967 if (cp == NULL)
2968 return -ENOMEM;
2969
2970 slen = strlen(cp);
2971 *value = cp;
2972 return slen;
2973}
2974
2975/**
2976 * smack_setprocattr - Smack process attribute setting
2977 * @p: the object task
2978 * @name: the name of the attribute in /proc/.../attr
2979 * @value: the value to set
2980 * @size: the size of the value
2981 *
2982 * Sets the Smack value of the task. Only setting self
2983 * is permitted and only with privilege
2984 *
2985 * Returns the length of the smack label or an error code
2986 */
2987static int smack_setprocattr(struct task_struct *p, char *name,
2988 void *value, size_t size)
2989{
Casey Schaufler676dac42010-12-02 06:43:39 -08002990 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002991 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002992 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002993
Casey Schauflere114e472008-02-04 22:29:50 -08002994 /*
2995 * Changing another process' Smack value is too dangerous
2996 * and supports no sane use case.
2997 */
2998 if (p != current)
2999 return -EPERM;
3000
Casey Schaufler1880eff2012-06-05 15:28:30 -07003001 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003002 return -EPERM;
3003
Casey Schauflerf7112e62012-05-06 15:22:02 -07003004 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003005 return -EINVAL;
3006
3007 if (strcmp(name, "current") != 0)
3008 return -EINVAL;
3009
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003010 skp = smk_import_entry(value, size);
3011 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003012 return -EINVAL;
3013
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003014 /*
3015 * No process is ever allowed the web ("@") label.
3016 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003017 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003018 return -EPERM;
3019
David Howellsd84f4f92008-11-14 10:39:23 +11003020 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003021 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003022 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003023
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003024 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003025 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003026
David Howellsd84f4f92008-11-14 10:39:23 +11003027 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003028 return size;
3029}
3030
3031/**
3032 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003033 * @sock: one sock
3034 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003035 * @newsk: unused
3036 *
3037 * Return 0 if a subject with the smack of sock could access
3038 * an object with the smack of other, otherwise an error code
3039 */
David S. Miller3610cda2011-01-05 15:38:53 -08003040static int smack_unix_stream_connect(struct sock *sock,
3041 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003042{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003043 struct smack_known *skp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003044 struct socket_smack *ssp = sock->sk_security;
3045 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003046 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003047 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003048 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08003049
Kees Cook923e9a12012-04-10 13:26:44 -07003050#ifdef CONFIG_AUDIT
3051 struct lsm_network_audit net;
3052
Eric Paris48c62af2012-04-02 13:15:44 -04003053 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
David S. Miller3610cda2011-01-05 15:38:53 -08003054 smk_ad_setfield_u_net_sk(&ad, other);
Kees Cook923e9a12012-04-10 13:26:44 -07003055#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003056
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003057 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3058 skp = ssp->smk_out;
3059 rc = smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
3060 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003061
Casey Schaufler975d5e52011-09-26 14:43:39 -07003062 /*
3063 * Cross reference the peer labels for SO_PEERSEC.
3064 */
3065 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003066 nsp->smk_packet = ssp->smk_out->smk_known;
3067 ssp->smk_packet = osp->smk_out->smk_known;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003068 }
3069
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003070 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003071}
3072
3073/**
3074 * smack_unix_may_send - Smack access on UDS
3075 * @sock: one socket
3076 * @other: the other socket
3077 *
3078 * Return 0 if a subject with the smack of sock could access
3079 * an object with the smack of other, otherwise an error code
3080 */
3081static int smack_unix_may_send(struct socket *sock, struct socket *other)
3082{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003083 struct socket_smack *ssp = sock->sk->sk_security;
3084 struct socket_smack *osp = other->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003085 struct smack_known *skp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003086 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003087
Kees Cook923e9a12012-04-10 13:26:44 -07003088#ifdef CONFIG_AUDIT
3089 struct lsm_network_audit net;
3090
Eric Paris48c62af2012-04-02 13:15:44 -04003091 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003092 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003093#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003094
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003095 if (smack_privileged(CAP_MAC_OVERRIDE))
3096 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003097
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003098 skp = ssp->smk_out;
3099 return smk_access(skp, osp->smk_in, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003100}
3101
3102/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003103 * smack_socket_sendmsg - Smack check based on destination host
3104 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003105 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003106 * @size: the size of the message
3107 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003108 * Return 0 if the current subject can write to the destination host.
3109 * For IPv4 this is only a question if the destination is a single label host.
3110 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003111 */
3112static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3113 int size)
3114{
3115 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003116 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003117 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003118
3119 /*
3120 * Perfectly reasonable for this to be NULL
3121 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003122 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003123 return 0;
3124
Casey Schauflerc6739442013-05-22 18:42:56 -07003125 switch (sip->sin_family) {
3126 case AF_INET:
3127 rc = smack_netlabel_send(sock->sk, sip);
3128 break;
3129 case AF_INET6:
3130 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3131 break;
3132 }
3133 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003134}
3135
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003136/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003137 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003138 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003139 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003140 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003141 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003142 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003143static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3144 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003145{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003146 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003147 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003148 int acat;
3149 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003150
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003151 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003152 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003153 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003154 * If there are flags but no level netlabel isn't
3155 * behaving the way we expect it to.
3156 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003157 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003158 * Without guidance regarding the smack value
3159 * for the packet fall back on the network
3160 * ambient value.
3161 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003162 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003163 list_for_each_entry(skp, &smack_known_list, list) {
3164 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003165 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003166 /*
3167 * Compare the catsets. Use the netlbl APIs.
3168 */
3169 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3170 if ((skp->smk_netlabel.flags &
3171 NETLBL_SECATTR_MLS_CAT) == 0)
3172 found = 1;
3173 break;
3174 }
3175 for (acat = -1, kcat = -1; acat == kcat; ) {
3176 acat = netlbl_secattr_catmap_walk(
3177 sap->attr.mls.cat, acat + 1);
3178 kcat = netlbl_secattr_catmap_walk(
3179 skp->smk_netlabel.attr.mls.cat,
3180 kcat + 1);
3181 if (acat < 0 || kcat < 0)
3182 break;
3183 }
3184 if (acat == kcat) {
3185 found = 1;
3186 break;
3187 }
Casey Schauflere114e472008-02-04 22:29:50 -08003188 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003189 rcu_read_unlock();
3190
3191 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003192 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003193
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003194 if (ssp != NULL && ssp->smk_in == smack_known_star.smk_known)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003195 return &smack_known_web;
3196 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003197 }
3198 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3199 /*
3200 * Looks like a fallback, which gives us a secid.
3201 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003202 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003203 /*
3204 * This has got to be a bug because it is
3205 * impossible to specify a fallback without
3206 * specifying the label, which will ensure
3207 * it has a secid, and the only way to get a
3208 * secid is from a fallback.
3209 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003210 BUG_ON(skp == NULL);
3211 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003212 }
3213 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003214 * Without guidance regarding the smack value
3215 * for the packet fall back on the network
3216 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003217 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003218 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003219}
3220
Casey Schaufler6ea06242013-08-05 13:21:22 -07003221static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003222{
Casey Schauflerc6739442013-05-22 18:42:56 -07003223 u8 nexthdr;
3224 int offset;
3225 int proto = -EINVAL;
3226 struct ipv6hdr _ipv6h;
3227 struct ipv6hdr *ip6;
3228 __be16 frag_off;
3229 struct tcphdr _tcph, *th;
3230 struct udphdr _udph, *uh;
3231 struct dccp_hdr _dccph, *dh;
3232
3233 sip->sin6_port = 0;
3234
3235 offset = skb_network_offset(skb);
3236 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3237 if (ip6 == NULL)
3238 return -EINVAL;
3239 sip->sin6_addr = ip6->saddr;
3240
3241 nexthdr = ip6->nexthdr;
3242 offset += sizeof(_ipv6h);
3243 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3244 if (offset < 0)
3245 return -EINVAL;
3246
3247 proto = nexthdr;
3248 switch (proto) {
3249 case IPPROTO_TCP:
3250 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3251 if (th != NULL)
3252 sip->sin6_port = th->source;
3253 break;
3254 case IPPROTO_UDP:
3255 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3256 if (uh != NULL)
3257 sip->sin6_port = uh->source;
3258 break;
3259 case IPPROTO_DCCP:
3260 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3261 if (dh != NULL)
3262 sip->sin6_port = dh->dccph_sport;
3263 break;
3264 }
3265 return proto;
3266}
3267
Casey Schauflere114e472008-02-04 22:29:50 -08003268/**
3269 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3270 * @sk: socket
3271 * @skb: packet
3272 *
3273 * Returns 0 if the packet should be delivered, an error code otherwise
3274 */
3275static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3276{
3277 struct netlbl_lsm_secattr secattr;
3278 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003279 struct smack_known *skp;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003280 struct sockaddr_in6 sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003281 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003282 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003283#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003284 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003285#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003286 switch (sk->sk_family) {
3287 case PF_INET:
3288 /*
3289 * Translate what netlabel gave us.
3290 */
3291 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003292
Casey Schauflerc6739442013-05-22 18:42:56 -07003293 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3294 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003295 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003296 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003297 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003298
Casey Schauflerc6739442013-05-22 18:42:56 -07003299 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003300
Etienne Bassetecfcc532009-04-08 20:40:06 +02003301#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003302 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3303 ad.a.u.net->family = sk->sk_family;
3304 ad.a.u.net->netif = skb->skb_iif;
3305 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003306#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003307 /*
3308 * Receiving a packet requires that the other end
3309 * be able to write here. Read access is not required.
3310 * This is the simplist possible security model
3311 * for networking.
3312 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003313 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07003314 if (rc != 0)
3315 netlbl_skbuff_err(skb, rc, 0);
3316 break;
3317 case PF_INET6:
3318 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3319 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3320 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3321 else
3322 rc = 0;
3323 break;
3324 }
Paul Moorea8134292008-10-10 10:16:31 -04003325 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003326}
3327
3328/**
3329 * smack_socket_getpeersec_stream - pull in packet label
3330 * @sock: the socket
3331 * @optval: user's destination
3332 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003333 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003334 *
3335 * returns zero on success, an error code otherwise
3336 */
3337static int smack_socket_getpeersec_stream(struct socket *sock,
3338 char __user *optval,
3339 int __user *optlen, unsigned len)
3340{
3341 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003342 char *rcp = "";
3343 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003344 int rc = 0;
3345
3346 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003347 if (ssp->smk_packet != NULL) {
3348 rcp = ssp->smk_packet;
3349 slen = strlen(rcp) + 1;
3350 }
Casey Schauflere114e472008-02-04 22:29:50 -08003351
3352 if (slen > len)
3353 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003354 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003355 rc = -EFAULT;
3356
3357 if (put_user(slen, optlen) != 0)
3358 rc = -EFAULT;
3359
3360 return rc;
3361}
3362
3363
3364/**
3365 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003366 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003367 * @skb: packet data
3368 * @secid: pointer to where to put the secid of the packet
3369 *
3370 * Sets the netlabel socket state on sk from parent
3371 */
3372static int smack_socket_getpeersec_dgram(struct socket *sock,
3373 struct sk_buff *skb, u32 *secid)
3374
3375{
3376 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003377 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003378 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003379 int family = PF_UNSPEC;
3380 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003381 int rc;
3382
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003383 if (skb != NULL) {
3384 if (skb->protocol == htons(ETH_P_IP))
3385 family = PF_INET;
3386 else if (skb->protocol == htons(ETH_P_IPV6))
3387 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003388 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003389 if (family == PF_UNSPEC && sock != NULL)
3390 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003391
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003392 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003393 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003394 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003395 } else if (family == PF_INET || family == PF_INET6) {
3396 /*
3397 * Translate what netlabel gave us.
3398 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003399 if (sock != NULL && sock->sk != NULL)
3400 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003401 netlbl_secattr_init(&secattr);
3402 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3403 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003404 skp = smack_from_secattr(&secattr, ssp);
3405 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003406 }
3407 netlbl_secattr_destroy(&secattr);
3408 }
3409 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003410 if (s == 0)
3411 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003412 return 0;
3413}
3414
3415/**
Paul Moore07feee82009-03-27 17:10:54 -04003416 * smack_sock_graft - Initialize a newly created socket with an existing sock
3417 * @sk: child sock
3418 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003419 *
Paul Moore07feee82009-03-27 17:10:54 -04003420 * Set the smk_{in,out} state of an existing sock based on the process that
3421 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003422 */
3423static void smack_sock_graft(struct sock *sk, struct socket *parent)
3424{
3425 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003426 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003427
Paul Moore07feee82009-03-27 17:10:54 -04003428 if (sk == NULL ||
3429 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003430 return;
3431
3432 ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003433 ssp->smk_in = skp->smk_known;
3434 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003435 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003436}
3437
3438/**
3439 * smack_inet_conn_request - Smack access check on connect
3440 * @sk: socket involved
3441 * @skb: packet
3442 * @req: unused
3443 *
3444 * Returns 0 if a task with the packet label could write to
3445 * the socket, otherwise an error code
3446 */
3447static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3448 struct request_sock *req)
3449{
Paul Moore07feee82009-03-27 17:10:54 -04003450 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003451 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003452 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003453 struct netlbl_lsm_secattr secattr;
3454 struct sockaddr_in addr;
3455 struct iphdr *hdr;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003456 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003457 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003458 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003459#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003460 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003461#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003462
Casey Schauflerc6739442013-05-22 18:42:56 -07003463 if (family == PF_INET6) {
3464 /*
3465 * Handle mapped IPv4 packets arriving
3466 * via IPv6 sockets. Don't set up netlabel
3467 * processing on IPv6.
3468 */
3469 if (skb->protocol == htons(ETH_P_IP))
3470 family = PF_INET;
3471 else
3472 return 0;
3473 }
Casey Schauflere114e472008-02-04 22:29:50 -08003474
Paul Moore07feee82009-03-27 17:10:54 -04003475 netlbl_secattr_init(&secattr);
3476 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003477 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003478 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003479 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003480 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003481 netlbl_secattr_destroy(&secattr);
3482
Etienne Bassetecfcc532009-04-08 20:40:06 +02003483#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003484 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3485 ad.a.u.net->family = family;
3486 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003487 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3488#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003489 /*
Paul Moore07feee82009-03-27 17:10:54 -04003490 * Receiving a packet requires that the other end be able to write
3491 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003492 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003493 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003494 if (rc != 0)
3495 return rc;
3496
3497 /*
3498 * Save the peer's label in the request_sock so we can later setup
3499 * smk_packet in the child socket so that SO_PEERCRED can report it.
3500 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003501 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003502
3503 /*
3504 * We need to decide if we want to label the incoming connection here
3505 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003506 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003507 */
3508 hdr = ip_hdr(skb);
3509 addr.sin_addr.s_addr = hdr->saddr;
3510 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003511 hsp = smack_host_label(&addr);
3512 rcu_read_unlock();
3513
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003514 if (hsp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003515 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003516 else
Paul Moore07feee82009-03-27 17:10:54 -04003517 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003518
3519 return rc;
3520}
3521
Paul Moore07feee82009-03-27 17:10:54 -04003522/**
3523 * smack_inet_csk_clone - Copy the connection information to the new socket
3524 * @sk: the new socket
3525 * @req: the connection's request_sock
3526 *
3527 * Transfer the connection's peer label to the newly created socket.
3528 */
3529static void smack_inet_csk_clone(struct sock *sk,
3530 const struct request_sock *req)
3531{
3532 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003533 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003534
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003535 if (req->peer_secid != 0) {
3536 skp = smack_from_secid(req->peer_secid);
3537 ssp->smk_packet = skp->smk_known;
3538 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003539 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003540}
3541
Casey Schauflere114e472008-02-04 22:29:50 -08003542/*
3543 * Key management security hooks
3544 *
3545 * Casey has not tested key support very heavily.
3546 * The permission check is most likely too restrictive.
3547 * If you care about keys please have a look.
3548 */
3549#ifdef CONFIG_KEYS
3550
3551/**
3552 * smack_key_alloc - Set the key security blob
3553 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003554 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003555 * @flags: unused
3556 *
3557 * No allocation required
3558 *
3559 * Returns 0
3560 */
David Howellsd84f4f92008-11-14 10:39:23 +11003561static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003562 unsigned long flags)
3563{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003564 struct smack_known *skp = smk_of_task(cred->security);
3565
3566 key->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003567 return 0;
3568}
3569
3570/**
3571 * smack_key_free - Clear the key security blob
3572 * @key: the object
3573 *
3574 * Clear the blob pointer
3575 */
3576static void smack_key_free(struct key *key)
3577{
3578 key->security = NULL;
3579}
3580
3581/*
3582 * smack_key_permission - Smack access on a key
3583 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003584 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003585 * @perm: unused
3586 *
3587 * Return 0 if the task has read and write to the object,
3588 * an error code otherwise
3589 */
3590static int smack_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11003591 const struct cred *cred, key_perm_t perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003592{
3593 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003594 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003595 struct smack_known *tkp = smk_of_task(cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08003596
3597 keyp = key_ref_to_ptr(key_ref);
3598 if (keyp == NULL)
3599 return -EINVAL;
3600 /*
3601 * If the key hasn't been initialized give it access so that
3602 * it may do so.
3603 */
3604 if (keyp->security == NULL)
3605 return 0;
3606 /*
3607 * This should not occur
3608 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003609 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003610 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003611#ifdef CONFIG_AUDIT
3612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3613 ad.a.u.key_struct.key = keyp->serial;
3614 ad.a.u.key_struct.key_desc = keyp->description;
3615#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003616 return smk_access(tkp, keyp->security, MAY_READWRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003617}
3618#endif /* CONFIG_KEYS */
3619
3620/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003621 * Smack Audit hooks
3622 *
3623 * Audit requires a unique representation of each Smack specific
3624 * rule. This unique representation is used to distinguish the
3625 * object to be audited from remaining kernel objects and also
3626 * works as a glue between the audit hooks.
3627 *
3628 * Since repository entries are added but never deleted, we'll use
3629 * the smack_known label address related to the given audit rule as
3630 * the needed unique representation. This also better fits the smack
3631 * model where nearly everything is a label.
3632 */
3633#ifdef CONFIG_AUDIT
3634
3635/**
3636 * smack_audit_rule_init - Initialize a smack audit rule
3637 * @field: audit rule fields given from user-space (audit.h)
3638 * @op: required testing operator (=, !=, >, <, ...)
3639 * @rulestr: smack label to be audited
3640 * @vrule: pointer to save our own audit rule representation
3641 *
3642 * Prepare to audit cases where (@field @op @rulestr) is true.
3643 * The label to be audited is created if necessay.
3644 */
3645static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3646{
3647 char **rule = (char **)vrule;
3648 *rule = NULL;
3649
3650 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3651 return -EINVAL;
3652
Al Viro5af75d82008-12-16 05:59:26 -05003653 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003654 return -EINVAL;
3655
3656 *rule = smk_import(rulestr, 0);
3657
3658 return 0;
3659}
3660
3661/**
3662 * smack_audit_rule_known - Distinguish Smack audit rules
3663 * @krule: rule of interest, in Audit kernel representation format
3664 *
3665 * This is used to filter Smack rules from remaining Audit ones.
3666 * If it's proved that this rule belongs to us, the
3667 * audit_rule_match hook will be called to do the final judgement.
3668 */
3669static int smack_audit_rule_known(struct audit_krule *krule)
3670{
3671 struct audit_field *f;
3672 int i;
3673
3674 for (i = 0; i < krule->field_count; i++) {
3675 f = &krule->fields[i];
3676
3677 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3678 return 1;
3679 }
3680
3681 return 0;
3682}
3683
3684/**
3685 * smack_audit_rule_match - Audit given object ?
3686 * @secid: security id for identifying the object to test
3687 * @field: audit rule flags given from user-space
3688 * @op: required testing operator
3689 * @vrule: smack internal rule presentation
3690 * @actx: audit context associated with the check
3691 *
3692 * The core Audit hook. It's used to take the decision of
3693 * whether to audit or not to audit a given object.
3694 */
3695static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3696 struct audit_context *actx)
3697{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003698 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003699 char *rule = vrule;
3700
3701 if (!rule) {
Tetsuo Handaceffec552012-03-29 16:19:05 +09003702 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003703 "Smack: missing rule\n");
3704 return -ENOENT;
3705 }
3706
3707 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3708 return 0;
3709
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003710 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003711
3712 /*
3713 * No need to do string comparisons. If a match occurs,
3714 * both pointers will point to the same smack_known
3715 * label.
3716 */
Al Viro5af75d82008-12-16 05:59:26 -05003717 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003718 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003719 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003720 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003721
3722 return 0;
3723}
3724
3725/**
3726 * smack_audit_rule_free - free smack rule representation
3727 * @vrule: rule to be freed.
3728 *
3729 * No memory was allocated.
3730 */
3731static void smack_audit_rule_free(void *vrule)
3732{
3733 /* No-op */
3734}
3735
3736#endif /* CONFIG_AUDIT */
3737
Randy Dunlap251a2a92009-02-18 11:42:33 -08003738/**
David Quigley746df9b2013-05-22 12:50:35 -04003739 * smack_ismaclabel - check if xattr @name references a smack MAC label
3740 * @name: Full xattr name to check.
3741 */
3742static int smack_ismaclabel(const char *name)
3743{
3744 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3745}
3746
3747
3748/**
Casey Schauflere114e472008-02-04 22:29:50 -08003749 * smack_secid_to_secctx - return the smack label for a secid
3750 * @secid: incoming integer
3751 * @secdata: destination
3752 * @seclen: how long it is
3753 *
3754 * Exists for networking code.
3755 */
3756static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3757{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003758 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003759
Eric Parisd5630b92010-10-13 16:24:48 -04003760 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003761 *secdata = skp->smk_known;
3762 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08003763 return 0;
3764}
3765
Randy Dunlap251a2a92009-02-18 11:42:33 -08003766/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003767 * smack_secctx_to_secid - return the secid for a smack label
3768 * @secdata: smack label
3769 * @seclen: how long result is
3770 * @secid: outgoing integer
3771 *
3772 * Exists for audit and networking code.
3773 */
David Howellse52c17642008-04-29 20:52:51 +01003774static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003775{
3776 *secid = smack_to_secid(secdata);
3777 return 0;
3778}
3779
Randy Dunlap251a2a92009-02-18 11:42:33 -08003780/**
Casey Schauflere114e472008-02-04 22:29:50 -08003781 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003782 * @secdata: unused
3783 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003784 *
3785 * Exists to make sure nothing gets done, and properly
3786 */
3787static void smack_release_secctx(char *secdata, u32 seclen)
3788{
3789}
3790
David P. Quigley1ee65e32009-09-03 14:25:57 -04003791static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3792{
3793 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3794}
3795
3796static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3797{
3798 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3799}
3800
3801static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3802{
3803 int len = 0;
3804 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3805
3806 if (len < 0)
3807 return len;
3808 *ctxlen = len;
3809 return 0;
3810}
3811
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003812struct security_operations smack_ops = {
3813 .name = "smack",
3814
Ingo Molnar9e488582009-05-07 19:26:19 +10003815 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003816 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003817 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003818
3819 .sb_alloc_security = smack_sb_alloc_security,
3820 .sb_free_security = smack_sb_free_security,
3821 .sb_copy_data = smack_sb_copy_data,
3822 .sb_kern_mount = smack_sb_kern_mount,
3823 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08003824
Casey Schaufler676dac42010-12-02 06:43:39 -08003825 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003826 .bprm_committing_creds = smack_bprm_committing_creds,
3827 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003828
Casey Schauflere114e472008-02-04 22:29:50 -08003829 .inode_alloc_security = smack_inode_alloc_security,
3830 .inode_free_security = smack_inode_free_security,
3831 .inode_init_security = smack_inode_init_security,
3832 .inode_link = smack_inode_link,
3833 .inode_unlink = smack_inode_unlink,
3834 .inode_rmdir = smack_inode_rmdir,
3835 .inode_rename = smack_inode_rename,
3836 .inode_permission = smack_inode_permission,
3837 .inode_setattr = smack_inode_setattr,
3838 .inode_getattr = smack_inode_getattr,
3839 .inode_setxattr = smack_inode_setxattr,
3840 .inode_post_setxattr = smack_inode_post_setxattr,
3841 .inode_getxattr = smack_inode_getxattr,
3842 .inode_removexattr = smack_inode_removexattr,
3843 .inode_getsecurity = smack_inode_getsecurity,
3844 .inode_setsecurity = smack_inode_setsecurity,
3845 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003846 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003847
3848 .file_permission = smack_file_permission,
3849 .file_alloc_security = smack_file_alloc_security,
3850 .file_free_security = smack_file_free_security,
3851 .file_ioctl = smack_file_ioctl,
3852 .file_lock = smack_file_lock,
3853 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003854 .mmap_file = smack_mmap_file,
3855 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003856 .file_set_fowner = smack_file_set_fowner,
3857 .file_send_sigiotask = smack_file_send_sigiotask,
3858 .file_receive = smack_file_receive,
3859
Eric Paris83d49852012-04-04 13:45:40 -04003860 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003861
David Howellsee18d642009-09-02 09:14:21 +01003862 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003863 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003864 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003865 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003866 .kernel_act_as = smack_kernel_act_as,
3867 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003868 .task_setpgid = smack_task_setpgid,
3869 .task_getpgid = smack_task_getpgid,
3870 .task_getsid = smack_task_getsid,
3871 .task_getsecid = smack_task_getsecid,
3872 .task_setnice = smack_task_setnice,
3873 .task_setioprio = smack_task_setioprio,
3874 .task_getioprio = smack_task_getioprio,
3875 .task_setscheduler = smack_task_setscheduler,
3876 .task_getscheduler = smack_task_getscheduler,
3877 .task_movememory = smack_task_movememory,
3878 .task_kill = smack_task_kill,
3879 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003880 .task_to_inode = smack_task_to_inode,
3881
3882 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003883 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003884
3885 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3886 .msg_msg_free_security = smack_msg_msg_free_security,
3887
3888 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3889 .msg_queue_free_security = smack_msg_queue_free_security,
3890 .msg_queue_associate = smack_msg_queue_associate,
3891 .msg_queue_msgctl = smack_msg_queue_msgctl,
3892 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3893 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3894
3895 .shm_alloc_security = smack_shm_alloc_security,
3896 .shm_free_security = smack_shm_free_security,
3897 .shm_associate = smack_shm_associate,
3898 .shm_shmctl = smack_shm_shmctl,
3899 .shm_shmat = smack_shm_shmat,
3900
3901 .sem_alloc_security = smack_sem_alloc_security,
3902 .sem_free_security = smack_sem_free_security,
3903 .sem_associate = smack_sem_associate,
3904 .sem_semctl = smack_sem_semctl,
3905 .sem_semop = smack_sem_semop,
3906
Casey Schauflere114e472008-02-04 22:29:50 -08003907 .d_instantiate = smack_d_instantiate,
3908
3909 .getprocattr = smack_getprocattr,
3910 .setprocattr = smack_setprocattr,
3911
3912 .unix_stream_connect = smack_unix_stream_connect,
3913 .unix_may_send = smack_unix_may_send,
3914
3915 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07003916 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003917 .socket_connect = smack_socket_connect,
3918 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003919 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3920 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3921 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3922 .sk_alloc_security = smack_sk_alloc_security,
3923 .sk_free_security = smack_sk_free_security,
3924 .sock_graft = smack_sock_graft,
3925 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003926 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003927
Casey Schauflere114e472008-02-04 22:29:50 -08003928 /* key management security hooks */
3929#ifdef CONFIG_KEYS
3930 .key_alloc = smack_key_alloc,
3931 .key_free = smack_key_free,
3932 .key_permission = smack_key_permission,
3933#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003934
3935 /* Audit hooks */
3936#ifdef CONFIG_AUDIT
3937 .audit_rule_init = smack_audit_rule_init,
3938 .audit_rule_known = smack_audit_rule_known,
3939 .audit_rule_match = smack_audit_rule_match,
3940 .audit_rule_free = smack_audit_rule_free,
3941#endif /* CONFIG_AUDIT */
3942
David Quigley746df9b2013-05-22 12:50:35 -04003943 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08003944 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003945 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003946 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003947 .inode_notifysecctx = smack_inode_notifysecctx,
3948 .inode_setsecctx = smack_inode_setsecctx,
3949 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003950};
3951
Etienne Basset7198e2e2009-03-24 20:53:24 +01003952
Casey Schaufler86812bb2012-04-17 18:55:46 -07003953static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003954{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003955 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003956 * Initialize rule list locks
3957 */
3958 mutex_init(&smack_known_huh.smk_rules_lock);
3959 mutex_init(&smack_known_hat.smk_rules_lock);
3960 mutex_init(&smack_known_floor.smk_rules_lock);
3961 mutex_init(&smack_known_star.smk_rules_lock);
3962 mutex_init(&smack_known_invalid.smk_rules_lock);
3963 mutex_init(&smack_known_web.smk_rules_lock);
3964 /*
3965 * Initialize rule lists
3966 */
3967 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
3968 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
3969 INIT_LIST_HEAD(&smack_known_star.smk_rules);
3970 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
3971 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
3972 INIT_LIST_HEAD(&smack_known_web.smk_rules);
3973 /*
3974 * Create the known labels list
3975 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02003976 smk_insert_entry(&smack_known_huh);
3977 smk_insert_entry(&smack_known_hat);
3978 smk_insert_entry(&smack_known_star);
3979 smk_insert_entry(&smack_known_floor);
3980 smk_insert_entry(&smack_known_invalid);
3981 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01003982}
3983
Casey Schauflere114e472008-02-04 22:29:50 -08003984/**
3985 * smack_init - initialize the smack system
3986 *
3987 * Returns 0
3988 */
3989static __init int smack_init(void)
3990{
David Howellsd84f4f92008-11-14 10:39:23 +11003991 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08003992 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003993
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003994 if (!security_module_enable(&smack_ops))
3995 return 0;
3996
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003997 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
3998 GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08003999 if (tsp == NULL)
4000 return -ENOMEM;
4001
Casey Schauflere114e472008-02-04 22:29:50 -08004002 printk(KERN_INFO "Smack: Initializing.\n");
4003
4004 /*
4005 * Set the security state for the initial task.
4006 */
David Howellsd84f4f92008-11-14 10:39:23 +11004007 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004008 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004009
Casey Schaufler86812bb2012-04-17 18:55:46 -07004010 /* initialize the smack_known_list */
4011 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004012
4013 /*
4014 * Register with LSM
4015 */
4016 if (register_security(&smack_ops))
4017 panic("smack: Unable to register with kernel.\n");
4018
4019 return 0;
4020}
4021
4022/*
4023 * Smack requires early initialization in order to label
4024 * all processes and objects when they are created.
4025 */
4026security_initcall(smack_init);