blob: b11ab23b328b17dabccf26ea035dbb4e3e526075 [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
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001029 if (rc != 0)
1030 return rc;
1031
Eric Parisa2694342011-04-25 13:10:27 -04001032 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001033 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001034
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001035 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1036 if (rc != 0)
1037 return rc;
1038
1039 isp = dentry->d_inode->i_security;
1040 /*
1041 * Don't do anything special for these.
1042 * XATTR_NAME_SMACKIPIN
1043 * XATTR_NAME_SMACKIPOUT
1044 * XATTR_NAME_SMACKEXEC
1045 */
1046 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001047 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001048 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001049 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001050 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1051 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001052
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001053 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001054}
1055
1056/**
1057 * smack_inode_getsecurity - get smack xattrs
1058 * @inode: the object
1059 * @name: attribute name
1060 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001061 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001062 *
1063 * Returns the size of the attribute or an error code
1064 */
1065static int smack_inode_getsecurity(const struct inode *inode,
1066 const char *name, void **buffer,
1067 bool alloc)
1068{
1069 struct socket_smack *ssp;
1070 struct socket *sock;
1071 struct super_block *sbp;
1072 struct inode *ip = (struct inode *)inode;
1073 char *isp;
1074 int ilen;
1075 int rc = 0;
1076
1077 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1078 isp = smk_of_inode(inode);
1079 ilen = strlen(isp) + 1;
1080 *buffer = isp;
1081 return ilen;
1082 }
1083
1084 /*
1085 * The rest of the Smack xattrs are only on sockets.
1086 */
1087 sbp = ip->i_sb;
1088 if (sbp->s_magic != SOCKFS_MAGIC)
1089 return -EOPNOTSUPP;
1090
1091 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001092 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001093 return -EOPNOTSUPP;
1094
1095 ssp = sock->sk->sk_security;
1096
1097 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07001098 isp = ssp->smk_in->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001099 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001100 isp = ssp->smk_out->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001101 else
1102 return -EOPNOTSUPP;
1103
1104 ilen = strlen(isp) + 1;
1105 if (rc == 0) {
1106 *buffer = isp;
1107 rc = ilen;
1108 }
1109
1110 return rc;
1111}
1112
1113
1114/**
1115 * smack_inode_listsecurity - list the Smack attributes
1116 * @inode: the object
1117 * @buffer: where they go
1118 * @buffer_size: size of buffer
1119 *
1120 * Returns 0 on success, -EINVAL otherwise
1121 */
1122static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1123 size_t buffer_size)
1124{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001125 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001126
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001127 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001128 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001129
1130 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001131}
1132
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001133/**
1134 * smack_inode_getsecid - Extract inode's security id
1135 * @inode: inode to extract the info from
1136 * @secid: where result will be saved
1137 */
1138static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1139{
1140 struct inode_smack *isp = inode->i_security;
1141
1142 *secid = smack_to_secid(isp->smk_inode);
1143}
1144
Casey Schauflere114e472008-02-04 22:29:50 -08001145/*
1146 * File Hooks
1147 */
1148
1149/**
1150 * smack_file_permission - Smack check on file operations
1151 * @file: unused
1152 * @mask: unused
1153 *
1154 * Returns 0
1155 *
1156 * Should access checks be done on each read or write?
1157 * UNICOS and SELinux say yes.
1158 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1159 *
1160 * I'll say no for now. Smack does not do the frequent
1161 * label changing that SELinux does.
1162 */
1163static int smack_file_permission(struct file *file, int mask)
1164{
1165 return 0;
1166}
1167
1168/**
1169 * smack_file_alloc_security - assign a file security blob
1170 * @file: the object
1171 *
1172 * The security blob for a file is a pointer to the master
1173 * label list, so no allocation is done.
1174 *
1175 * Returns 0
1176 */
1177static int smack_file_alloc_security(struct file *file)
1178{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001179 struct smack_known *skp = smk_of_current();
1180
1181 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001182 return 0;
1183}
1184
1185/**
1186 * smack_file_free_security - clear a file security blob
1187 * @file: the object
1188 *
1189 * The security blob for a file is a pointer to the master
1190 * label list, so no memory is freed.
1191 */
1192static void smack_file_free_security(struct file *file)
1193{
1194 file->f_security = NULL;
1195}
1196
1197/**
1198 * smack_file_ioctl - Smack check on ioctls
1199 * @file: the object
1200 * @cmd: what to do
1201 * @arg: unused
1202 *
1203 * Relies heavily on the correct use of the ioctl command conventions.
1204 *
1205 * Returns 0 if allowed, error code otherwise
1206 */
1207static int smack_file_ioctl(struct file *file, unsigned int cmd,
1208 unsigned long arg)
1209{
1210 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001211 struct smk_audit_info ad;
1212
Eric Parisf48b7392011-04-25 12:54:27 -04001213 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001214 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001215
1216 if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001217 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001218
1219 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001220 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001221
1222 return rc;
1223}
1224
1225/**
1226 * smack_file_lock - Smack check on file locking
1227 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001228 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001229 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001230 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001231 */
1232static int smack_file_lock(struct file *file, unsigned int cmd)
1233{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001234 struct smk_audit_info ad;
1235
Eric Paris92f42502011-04-25 13:15:55 -04001236 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1237 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001238 return smk_curacc(file->f_security, MAY_LOCK, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001239}
1240
1241/**
1242 * smack_file_fcntl - Smack check on fcntl
1243 * @file: the object
1244 * @cmd: what action to check
1245 * @arg: unused
1246 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001247 * Generally these operations are harmless.
1248 * File locking operations present an obvious mechanism
1249 * for passing information, so they require write access.
1250 *
Casey Schauflere114e472008-02-04 22:29:50 -08001251 * Returns 0 if current has access, error code otherwise
1252 */
1253static int smack_file_fcntl(struct file *file, unsigned int cmd,
1254 unsigned long arg)
1255{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001256 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001257 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001258
Etienne Bassetecfcc532009-04-08 20:40:06 +02001259
Casey Schauflere114e472008-02-04 22:29:50 -08001260 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001261 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001262 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001263 case F_SETLK:
1264 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001265 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1266 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1267 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1268 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001269 case F_SETOWN:
1270 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001271 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1272 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001273 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001274 break;
1275 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001276 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001277 }
1278
1279 return rc;
1280}
1281
1282/**
Al Viroe5467852012-05-30 13:30:51 -04001283 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001284 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1285 * if mapping anonymous memory.
1286 * @file contains the file structure for file to map (may be NULL).
1287 * @reqprot contains the protection requested by the application.
1288 * @prot contains the protection that will be applied by the kernel.
1289 * @flags contains the operational flags.
1290 * Return 0 if permission is granted.
1291 */
Al Viroe5467852012-05-30 13:30:51 -04001292static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001293 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001294 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001295{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001296 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001297 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001298 struct smack_rule *srp;
1299 struct task_smack *tsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001300 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001301 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001302 int may;
1303 int mmay;
1304 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001305 int rc;
1306
Al Viro496ad9a2013-01-23 17:07:38 -05001307 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001308 return 0;
1309
Al Viro496ad9a2013-01-23 17:07:38 -05001310 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001311 if (isp->smk_mmap == NULL)
1312 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001313 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001314
1315 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001316 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001317 rc = 0;
1318
1319 rcu_read_lock();
1320 /*
1321 * For each Smack rule associated with the subject
1322 * label verify that the SMACK64MMAP also has access
1323 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001324 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001325 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001326 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001327 /*
1328 * Matching labels always allows access.
1329 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001330 if (mkp->smk_known == osmack)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001331 continue;
1332 /*
1333 * If there is a matching local rule take
1334 * that into account as well.
1335 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001336 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001337 &tsp->smk_rules);
1338 if (may == -ENOENT)
1339 may = srp->smk_access;
1340 else
1341 may &= srp->smk_access;
1342 /*
1343 * If may is zero the SMACK64MMAP subject can't
1344 * possibly have less access.
1345 */
1346 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001347 continue;
1348
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001349 /*
1350 * Fetch the global list entry.
1351 * If there isn't one a SMACK64MMAP subject
1352 * can't have as much access as current.
1353 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001354 mmay = smk_access_entry(mkp->smk_known, osmack,
1355 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001356 if (mmay == -ENOENT) {
1357 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001358 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001359 }
1360 /*
1361 * If there is a local entry it modifies the
1362 * potential access, too.
1363 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001364 tmay = smk_access_entry(mkp->smk_known, osmack,
1365 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001366 if (tmay != -ENOENT)
1367 mmay &= tmay;
1368
1369 /*
1370 * If there is any access available to current that is
1371 * not available to a SMACK64MMAP subject
1372 * deny access.
1373 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001374 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001375 rc = -EACCES;
1376 break;
1377 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001378 }
1379
1380 rcu_read_unlock();
1381
1382 return rc;
1383}
1384
1385/**
Casey Schauflere114e472008-02-04 22:29:50 -08001386 * smack_file_set_fowner - set the file security blob value
1387 * @file: object in question
1388 *
1389 * Returns 0
1390 * Further research may be required on this one.
1391 */
1392static int smack_file_set_fowner(struct file *file)
1393{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001394 struct smack_known *skp = smk_of_current();
1395
1396 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001397 return 0;
1398}
1399
1400/**
1401 * smack_file_send_sigiotask - Smack on sigio
1402 * @tsk: The target task
1403 * @fown: the object the signal come from
1404 * @signum: unused
1405 *
1406 * Allow a privileged task to get signals even if it shouldn't
1407 *
1408 * Returns 0 if a subject with the object's smack could
1409 * write to the task, an error code otherwise.
1410 */
1411static int smack_file_send_sigiotask(struct task_struct *tsk,
1412 struct fown_struct *fown, int signum)
1413{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001414 struct smack_known *skp;
1415 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001416 struct file *file;
1417 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001418 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001419
1420 /*
1421 * struct fown_struct is never outside the context of a struct file
1422 */
1423 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001424
Etienne Bassetecfcc532009-04-08 20:40:06 +02001425 /* we don't log here as rc can be overriden */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001426 skp = smk_find_entry(file->f_security);
1427 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01001428 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001429 rc = 0;
1430
1431 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1432 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001433 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001434 return rc;
1435}
1436
1437/**
1438 * smack_file_receive - Smack file receive check
1439 * @file: the object
1440 *
1441 * Returns 0 if current has access, error code otherwise
1442 */
1443static int smack_file_receive(struct file *file)
1444{
1445 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001446 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001447
Casey Schaufler4482a442013-12-30 17:37:45 -08001448 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001449 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001450 /*
1451 * This code relies on bitmasks.
1452 */
1453 if (file->f_mode & FMODE_READ)
1454 may = MAY_READ;
1455 if (file->f_mode & FMODE_WRITE)
1456 may |= MAY_WRITE;
1457
Etienne Bassetecfcc532009-04-08 20:40:06 +02001458 return smk_curacc(file->f_security, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001459}
1460
Casey Schaufler531f1d42011-09-19 12:41:42 -07001461/**
Eric Paris83d49852012-04-04 13:45:40 -04001462 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001463 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001464 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001465 *
1466 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001467 * Allow the open only if the task has read access. There are
1468 * many read operations (e.g. fstat) that you can do with an
1469 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001470 *
1471 * Returns 0
1472 */
Eric Paris83d49852012-04-04 13:45:40 -04001473static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001474{
Casey Schauflera6834c02014-04-21 11:10:26 -07001475 struct task_smack *tsp = cred->security;
Al Viro496ad9a2013-01-23 17:07:38 -05001476 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schauflera6834c02014-04-21 11:10:26 -07001477 struct smk_audit_info ad;
1478 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001479
Casey Schauflera6834c02014-04-21 11:10:26 -07001480 if (smack_privileged(CAP_MAC_OVERRIDE))
1481 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001482
Casey Schauflera6834c02014-04-21 11:10:26 -07001483 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1484 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1485 rc = smk_access(tsp->smk_task, isp->smk_inode, MAY_READ, &ad);
1486 if (rc == 0)
1487 file->f_security = isp->smk_inode;
1488
1489 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001490}
1491
Casey Schauflere114e472008-02-04 22:29:50 -08001492/*
1493 * Task hooks
1494 */
1495
1496/**
David Howellsee18d642009-09-02 09:14:21 +01001497 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1498 * @new: the new credentials
1499 * @gfp: the atomicity of any memory allocations
1500 *
1501 * Prepare a blank set of credentials for modification. This must allocate all
1502 * the memory the LSM module might require such that cred_transfer() can
1503 * complete without error.
1504 */
1505static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1506{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001507 struct task_smack *tsp;
1508
1509 tsp = new_task_smack(NULL, NULL, gfp);
1510 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001511 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001512
1513 cred->security = tsp;
1514
David Howellsee18d642009-09-02 09:14:21 +01001515 return 0;
1516}
1517
1518
1519/**
David Howellsf1752ee2008-11-14 10:39:17 +11001520 * smack_cred_free - "free" task-level security credentials
1521 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001522 *
Casey Schauflere114e472008-02-04 22:29:50 -08001523 */
David Howellsf1752ee2008-11-14 10:39:17 +11001524static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001525{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001526 struct task_smack *tsp = cred->security;
1527 struct smack_rule *rp;
1528 struct list_head *l;
1529 struct list_head *n;
1530
1531 if (tsp == NULL)
1532 return;
1533 cred->security = NULL;
1534
1535 list_for_each_safe(l, n, &tsp->smk_rules) {
1536 rp = list_entry(l, struct smack_rule, list);
1537 list_del(&rp->list);
1538 kfree(rp);
1539 }
1540 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001541}
1542
1543/**
David Howellsd84f4f92008-11-14 10:39:23 +11001544 * smack_cred_prepare - prepare new set of credentials for modification
1545 * @new: the new credentials
1546 * @old: the original credentials
1547 * @gfp: the atomicity of any memory allocations
1548 *
1549 * Prepare a new set of credentials for modification.
1550 */
1551static int smack_cred_prepare(struct cred *new, const struct cred *old,
1552 gfp_t gfp)
1553{
Casey Schaufler676dac42010-12-02 06:43:39 -08001554 struct task_smack *old_tsp = old->security;
1555 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001556 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001557
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001558 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001559 if (new_tsp == NULL)
1560 return -ENOMEM;
1561
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001562 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1563 if (rc != 0)
1564 return rc;
1565
Casey Schaufler676dac42010-12-02 06:43:39 -08001566 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001567 return 0;
1568}
1569
Randy Dunlap251a2a92009-02-18 11:42:33 -08001570/**
David Howellsee18d642009-09-02 09:14:21 +01001571 * smack_cred_transfer - Transfer the old credentials to the new credentials
1572 * @new: the new credentials
1573 * @old: the original credentials
1574 *
1575 * Fill in a set of blank credentials from another set of credentials.
1576 */
1577static void smack_cred_transfer(struct cred *new, const struct cred *old)
1578{
Casey Schaufler676dac42010-12-02 06:43:39 -08001579 struct task_smack *old_tsp = old->security;
1580 struct task_smack *new_tsp = new->security;
1581
1582 new_tsp->smk_task = old_tsp->smk_task;
1583 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001584 mutex_init(&new_tsp->smk_rules_lock);
1585 INIT_LIST_HEAD(&new_tsp->smk_rules);
1586
1587
1588 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001589}
1590
1591/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001592 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001593 * @new: points to the set of credentials to be modified.
1594 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001595 *
1596 * Set the security data for a kernel service.
1597 */
1598static int smack_kernel_act_as(struct cred *new, u32 secid)
1599{
Casey Schaufler676dac42010-12-02 06:43:39 -08001600 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001601 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001602
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001603 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001604 return -EINVAL;
1605
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001606 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001607 return 0;
1608}
1609
1610/**
1611 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001612 * @new: points to the set of credentials to be modified
1613 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001614 *
1615 * Set the file creation context in a set of credentials to the same
1616 * as the objective context of the specified inode
1617 */
1618static int smack_kernel_create_files_as(struct cred *new,
1619 struct inode *inode)
1620{
1621 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001622 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001623
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001624 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1625 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001626 return 0;
1627}
1628
1629/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001630 * smk_curacc_on_task - helper to log task related access
1631 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001632 * @access: the access requested
1633 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001634 *
1635 * Return 0 if access is permitted
1636 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001637static int smk_curacc_on_task(struct task_struct *p, int access,
1638 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001639{
1640 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001641 struct smack_known *skp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001642
Casey Schaufler531f1d42011-09-19 12:41:42 -07001643 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001644 smk_ad_setfield_u_tsk(&ad, p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001645 return smk_curacc(skp->smk_known, access, &ad);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001646}
1647
1648/**
Casey Schauflere114e472008-02-04 22:29:50 -08001649 * smack_task_setpgid - Smack check on setting pgid
1650 * @p: the task object
1651 * @pgid: unused
1652 *
1653 * Return 0 if write access is permitted
1654 */
1655static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1656{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001657 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001658}
1659
1660/**
1661 * smack_task_getpgid - Smack access check for getpgid
1662 * @p: the object task
1663 *
1664 * Returns 0 if current can read the object task, error code otherwise
1665 */
1666static int smack_task_getpgid(struct task_struct *p)
1667{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001668 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001669}
1670
1671/**
1672 * smack_task_getsid - Smack access check for getsid
1673 * @p: the object task
1674 *
1675 * Returns 0 if current can read the object task, error code otherwise
1676 */
1677static int smack_task_getsid(struct task_struct *p)
1678{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001679 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001680}
1681
1682/**
1683 * smack_task_getsecid - get the secid of the task
1684 * @p: the object task
1685 * @secid: where to put the result
1686 *
1687 * Sets the secid to contain a u32 version of the smack label.
1688 */
1689static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1690{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001691 struct smack_known *skp = smk_of_task(task_security(p));
1692
1693 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001694}
1695
1696/**
1697 * smack_task_setnice - Smack check on setting nice
1698 * @p: the task object
1699 * @nice: unused
1700 *
1701 * Return 0 if write access is permitted
1702 */
1703static int smack_task_setnice(struct task_struct *p, int nice)
1704{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001705 int rc;
1706
1707 rc = cap_task_setnice(p, nice);
1708 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001709 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001710 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001711}
1712
1713/**
1714 * smack_task_setioprio - Smack check on setting ioprio
1715 * @p: the task object
1716 * @ioprio: unused
1717 *
1718 * Return 0 if write access is permitted
1719 */
1720static int smack_task_setioprio(struct task_struct *p, int ioprio)
1721{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001722 int rc;
1723
1724 rc = cap_task_setioprio(p, ioprio);
1725 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001726 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001727 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001728}
1729
1730/**
1731 * smack_task_getioprio - Smack check on reading ioprio
1732 * @p: the task object
1733 *
1734 * Return 0 if read access is permitted
1735 */
1736static int smack_task_getioprio(struct task_struct *p)
1737{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001738 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001739}
1740
1741/**
1742 * smack_task_setscheduler - Smack check on setting scheduler
1743 * @p: the task object
1744 * @policy: unused
1745 * @lp: unused
1746 *
1747 * Return 0 if read access is permitted
1748 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001749static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001750{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001751 int rc;
1752
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001753 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001754 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001755 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001756 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001757}
1758
1759/**
1760 * smack_task_getscheduler - Smack check on reading scheduler
1761 * @p: the task object
1762 *
1763 * Return 0 if read access is permitted
1764 */
1765static int smack_task_getscheduler(struct task_struct *p)
1766{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001767 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001768}
1769
1770/**
1771 * smack_task_movememory - Smack check on moving memory
1772 * @p: the task object
1773 *
1774 * Return 0 if write access is permitted
1775 */
1776static int smack_task_movememory(struct task_struct *p)
1777{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001778 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001779}
1780
1781/**
1782 * smack_task_kill - Smack check on signal delivery
1783 * @p: the task object
1784 * @info: unused
1785 * @sig: unused
1786 * @secid: identifies the smack to use in lieu of current's
1787 *
1788 * Return 0 if write access is permitted
1789 *
1790 * The secid behavior is an artifact of an SELinux hack
1791 * in the USB code. Someday it may go away.
1792 */
1793static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1794 int sig, u32 secid)
1795{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001796 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001797 struct smack_known *skp;
1798 struct smack_known *tkp = smk_of_task(task_security(p));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001799
1800 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1801 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001802 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001803 * Sending a signal requires that the sender
1804 * can write the receiver.
1805 */
1806 if (secid == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001807 return smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001808 /*
1809 * If the secid isn't 0 we're dealing with some USB IO
1810 * specific behavior. This is not clean. For one thing
1811 * we can't take privilege into account.
1812 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001813 skp = smack_from_secid(secid);
1814 return smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001815}
1816
1817/**
1818 * smack_task_wait - Smack access check for waiting
1819 * @p: task to wait for
1820 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001821 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08001822 */
1823static int smack_task_wait(struct task_struct *p)
1824{
Casey Schauflere114e472008-02-04 22:29:50 -08001825 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001826 * Allow the operation to succeed.
1827 * Zombies are bad.
1828 * In userless environments (e.g. phones) programs
1829 * get marked with SMACK64EXEC and even if the parent
1830 * and child shouldn't be talking the parent still
1831 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08001832 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07001833 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001834}
1835
1836/**
1837 * smack_task_to_inode - copy task smack into the inode blob
1838 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08001839 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08001840 *
1841 * Sets the smack pointer in the inode security blob
1842 */
1843static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
1844{
1845 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001846 struct smack_known *skp = smk_of_task(task_security(p));
1847
1848 isp->smk_inode = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001849}
1850
1851/*
1852 * Socket hooks.
1853 */
1854
1855/**
1856 * smack_sk_alloc_security - Allocate a socket blob
1857 * @sk: the socket
1858 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08001859 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08001860 *
1861 * Assign Smack pointers to current
1862 *
1863 * Returns 0 on success, -ENOMEM is there's no memory
1864 */
1865static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
1866{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001867 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001868 struct socket_smack *ssp;
1869
1870 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
1871 if (ssp == NULL)
1872 return -ENOMEM;
1873
Casey Schaufler54e70ec2014-04-10 16:37:08 -07001874 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001875 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001876 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001877
1878 sk->sk_security = ssp;
1879
1880 return 0;
1881}
1882
1883/**
1884 * smack_sk_free_security - Free a socket blob
1885 * @sk: the socket
1886 *
1887 * Clears the blob pointer
1888 */
1889static void smack_sk_free_security(struct sock *sk)
1890{
1891 kfree(sk->sk_security);
1892}
1893
1894/**
Paul Moore07feee82009-03-27 17:10:54 -04001895* smack_host_label - check host based restrictions
1896* @sip: the object end
1897*
1898* looks for host based access restrictions
1899*
1900* This version will only be appropriate for really small sets of single label
1901* hosts. The caller is responsible for ensuring that the RCU read lock is
1902* taken before calling this function.
1903*
1904* Returns the label of the far end or NULL if it's not special.
1905*/
1906static char *smack_host_label(struct sockaddr_in *sip)
1907{
1908 struct smk_netlbladdr *snp;
1909 struct in_addr *siap = &sip->sin_addr;
1910
1911 if (siap->s_addr == 0)
1912 return NULL;
1913
1914 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
1915 /*
1916 * we break after finding the first match because
1917 * the list is sorted from longest to shortest mask
1918 * so we have found the most specific match
1919 */
1920 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04001921 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
1922 /* we have found the special CIPSO option */
1923 if (snp->smk_label == smack_cipso_option)
1924 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04001925 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04001926 }
Paul Moore07feee82009-03-27 17:10:54 -04001927
1928 return NULL;
1929}
1930
1931/**
Casey Schauflere114e472008-02-04 22:29:50 -08001932 * smack_netlabel - Set the secattr on a socket
1933 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001934 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08001935 *
1936 * Convert the outbound smack value (smk_out) to a
1937 * secattr and attach it to the socket.
1938 *
1939 * Returns 0 on success or an error code
1940 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001941static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08001942{
Casey Schauflerf7112e62012-05-06 15:22:02 -07001943 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001944 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001945 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001946
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001947 /*
1948 * Usually the netlabel code will handle changing the
1949 * packet labeling based on the label.
1950 * The case of a single label host is different, because
1951 * a single label host should never get a labeled packet
1952 * even though the label is usually associated with a packet
1953 * label.
1954 */
1955 local_bh_disable();
1956 bh_lock_sock_nested(sk);
1957
1958 if (ssp->smk_out == smack_net_ambient ||
1959 labeled == SMACK_UNLABELED_SOCKET)
1960 netlbl_sock_delattr(sk);
1961 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001962 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07001963 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05001964 }
1965
1966 bh_unlock_sock(sk);
1967 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08001968
Casey Schauflere114e472008-02-04 22:29:50 -08001969 return rc;
1970}
1971
1972/**
Paul Moore07feee82009-03-27 17:10:54 -04001973 * smack_netlbel_send - Set the secattr on a socket and perform access checks
1974 * @sk: the socket
1975 * @sap: the destination address
1976 *
1977 * Set the correct secattr for the given socket based on the destination
1978 * address and perform any outbound access checks needed.
1979 *
1980 * Returns 0 on success or an error code.
1981 *
1982 */
1983static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
1984{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001985 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04001986 int rc;
1987 int sk_lbl;
1988 char *hostsp;
1989 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001990 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04001991
1992 rcu_read_lock();
1993 hostsp = smack_host_label(sap);
1994 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001995#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07001996 struct lsm_network_audit net;
1997
Eric Paris48c62af2012-04-02 13:15:44 -04001998 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
1999 ad.a.u.net->family = sap->sin_family;
2000 ad.a.u.net->dport = sap->sin_port;
2001 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002002#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002003 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002004 skp = ssp->smk_out;
2005 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04002006 } else {
2007 sk_lbl = SMACK_CIPSO_SOCKET;
2008 rc = 0;
2009 }
2010 rcu_read_unlock();
2011 if (rc != 0)
2012 return rc;
2013
2014 return smack_netlabel(sk, sk_lbl);
2015}
2016
2017/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002018 * smk_ipv6_port_label - Smack port access table management
2019 * @sock: socket
2020 * @address: address
2021 *
2022 * Create or update the port list entry
2023 */
2024static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2025{
2026 struct sock *sk = sock->sk;
2027 struct sockaddr_in6 *addr6;
2028 struct socket_smack *ssp = sock->sk->sk_security;
2029 struct smk_port_label *spp;
2030 unsigned short port = 0;
2031
2032 if (address == NULL) {
2033 /*
2034 * This operation is changing the Smack information
2035 * on the bound socket. Take the changes to the port
2036 * as well.
2037 */
2038 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2039 if (sk != spp->smk_sock)
2040 continue;
2041 spp->smk_in = ssp->smk_in;
2042 spp->smk_out = ssp->smk_out;
2043 return;
2044 }
2045 /*
2046 * A NULL address is only used for updating existing
2047 * bound entries. If there isn't one, it's OK.
2048 */
2049 return;
2050 }
2051
2052 addr6 = (struct sockaddr_in6 *)address;
2053 port = ntohs(addr6->sin6_port);
2054 /*
2055 * This is a special case that is safely ignored.
2056 */
2057 if (port == 0)
2058 return;
2059
2060 /*
2061 * Look for an existing port list entry.
2062 * This is an indication that a port is getting reused.
2063 */
2064 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2065 if (spp->smk_port != port)
2066 continue;
2067 spp->smk_port = port;
2068 spp->smk_sock = sk;
2069 spp->smk_in = ssp->smk_in;
2070 spp->smk_out = ssp->smk_out;
2071 return;
2072 }
2073
2074 /*
2075 * A new port entry is required.
2076 */
2077 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2078 if (spp == NULL)
2079 return;
2080
2081 spp->smk_port = port;
2082 spp->smk_sock = sk;
2083 spp->smk_in = ssp->smk_in;
2084 spp->smk_out = ssp->smk_out;
2085
2086 list_add(&spp->list, &smk_ipv6_port_list);
2087 return;
2088}
2089
2090/**
2091 * smk_ipv6_port_check - check Smack port access
2092 * @sock: socket
2093 * @address: address
2094 *
2095 * Create or update the port list entry
2096 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002097static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002098 int act)
2099{
2100 __be16 *bep;
2101 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002102 struct smk_port_label *spp;
2103 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002104 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002105 unsigned short port = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002106 char *object;
2107 struct smk_audit_info ad;
2108#ifdef CONFIG_AUDIT
2109 struct lsm_network_audit net;
2110#endif
2111
2112 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002113 skp = smack_net_ambient;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002114 object = ssp->smk_in->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002115 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002116 skp = ssp->smk_out;
2117 object = smack_net_ambient->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002118 }
2119
2120 /*
2121 * Get the IP address and port from the address.
2122 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002123 port = ntohs(address->sin6_port);
2124 bep = (__be16 *)(&address->sin6_addr);
2125 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002126
2127 /*
2128 * It's remote, so port lookup does no good.
2129 */
2130 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2131 goto auditout;
2132
2133 /*
2134 * It's local so the send check has to have passed.
2135 */
2136 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002137 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002138 goto auditout;
2139 }
2140
2141 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2142 if (spp->smk_port != port)
2143 continue;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002144 object = spp->smk_in->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002145 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002146 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002147 break;
2148 }
2149
2150auditout:
2151
2152#ifdef CONFIG_AUDIT
2153 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2154 ad.a.u.net->family = sk->sk_family;
2155 ad.a.u.net->dport = port;
2156 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002157 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002158 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002159 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002160#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002161 return smk_access(skp, object, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07002162}
2163
2164/**
Casey Schauflere114e472008-02-04 22:29:50 -08002165 * smack_inode_setsecurity - set smack xattrs
2166 * @inode: the object
2167 * @name: attribute name
2168 * @value: attribute value
2169 * @size: size of the attribute
2170 * @flags: unused
2171 *
2172 * Sets the named attribute in the appropriate blob
2173 *
2174 * Returns 0 on success, or an error code
2175 */
2176static int smack_inode_setsecurity(struct inode *inode, const char *name,
2177 const void *value, size_t size, int flags)
2178{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002179 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002180 struct inode_smack *nsp = inode->i_security;
2181 struct socket_smack *ssp;
2182 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002183 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002184
Casey Schauflerf7112e62012-05-06 15:22:02 -07002185 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302186 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002187
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002188 skp = smk_import_entry(value, size);
2189 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002190 return -EINVAL;
2191
2192 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002193 nsp->smk_inode = skp->smk_known;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002194 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002195 return 0;
2196 }
2197 /*
2198 * The rest of the Smack xattrs are only on sockets.
2199 */
2200 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2201 return -EOPNOTSUPP;
2202
2203 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002204 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002205 return -EOPNOTSUPP;
2206
2207 ssp = sock->sk->sk_security;
2208
2209 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002210 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002211 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002212 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002213 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002214 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2215 if (rc != 0)
2216 printk(KERN_WARNING
2217 "Smack: \"%s\" netlbl error %d.\n",
2218 __func__, -rc);
2219 }
Casey Schauflere114e472008-02-04 22:29:50 -08002220 } else
2221 return -EOPNOTSUPP;
2222
Casey Schauflerc6739442013-05-22 18:42:56 -07002223 if (sock->sk->sk_family == PF_INET6)
2224 smk_ipv6_port_label(sock, NULL);
2225
Casey Schauflere114e472008-02-04 22:29:50 -08002226 return 0;
2227}
2228
2229/**
2230 * smack_socket_post_create - finish socket setup
2231 * @sock: the socket
2232 * @family: protocol family
2233 * @type: unused
2234 * @protocol: unused
2235 * @kern: unused
2236 *
2237 * Sets the netlabel information on the socket
2238 *
2239 * Returns 0 on success, and error code otherwise
2240 */
2241static int smack_socket_post_create(struct socket *sock, int family,
2242 int type, int protocol, int kern)
2243{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002244 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002245 return 0;
2246 /*
2247 * Set the outbound netlbl.
2248 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002249 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2250}
2251
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002252/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002253 * smack_socket_bind - record port binding information.
2254 * @sock: the socket
2255 * @address: the port address
2256 * @addrlen: size of the address
2257 *
2258 * Records the label bound to a port.
2259 *
2260 * Returns 0
2261 */
2262static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2263 int addrlen)
2264{
2265 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2266 smk_ipv6_port_label(sock, address);
2267
2268 return 0;
2269}
2270
2271/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002272 * smack_socket_connect - connect access check
2273 * @sock: the socket
2274 * @sap: the other end
2275 * @addrlen: size of sap
2276 *
2277 * Verifies that a connection may be possible
2278 *
2279 * Returns 0 on success, and error code otherwise
2280 */
2281static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2282 int addrlen)
2283{
Casey Schauflerc6739442013-05-22 18:42:56 -07002284 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002285
Casey Schauflerc6739442013-05-22 18:42:56 -07002286 if (sock->sk == NULL)
2287 return 0;
2288
2289 switch (sock->sk->sk_family) {
2290 case PF_INET:
2291 if (addrlen < sizeof(struct sockaddr_in))
2292 return -EINVAL;
2293 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2294 break;
2295 case PF_INET6:
2296 if (addrlen < sizeof(struct sockaddr_in6))
2297 return -EINVAL;
Casey Schaufler6ea06242013-08-05 13:21:22 -07002298 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2299 SMK_CONNECTING);
Casey Schauflerc6739442013-05-22 18:42:56 -07002300 break;
2301 }
2302 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002303}
2304
2305/**
2306 * smack_flags_to_may - convert S_ to MAY_ values
2307 * @flags: the S_ value
2308 *
2309 * Returns the equivalent MAY_ value
2310 */
2311static int smack_flags_to_may(int flags)
2312{
2313 int may = 0;
2314
2315 if (flags & S_IRUGO)
2316 may |= MAY_READ;
2317 if (flags & S_IWUGO)
2318 may |= MAY_WRITE;
2319 if (flags & S_IXUGO)
2320 may |= MAY_EXEC;
2321
2322 return may;
2323}
2324
2325/**
2326 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2327 * @msg: the object
2328 *
2329 * Returns 0
2330 */
2331static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2332{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002333 struct smack_known *skp = smk_of_current();
2334
2335 msg->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002336 return 0;
2337}
2338
2339/**
2340 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2341 * @msg: the object
2342 *
2343 * Clears the blob pointer
2344 */
2345static void smack_msg_msg_free_security(struct msg_msg *msg)
2346{
2347 msg->security = NULL;
2348}
2349
2350/**
2351 * smack_of_shm - the smack pointer for the shm
2352 * @shp: the object
2353 *
2354 * Returns a pointer to the smack value
2355 */
2356static char *smack_of_shm(struct shmid_kernel *shp)
2357{
2358 return (char *)shp->shm_perm.security;
2359}
2360
2361/**
2362 * smack_shm_alloc_security - Set the security blob for shm
2363 * @shp: the object
2364 *
2365 * Returns 0
2366 */
2367static int smack_shm_alloc_security(struct shmid_kernel *shp)
2368{
2369 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002370 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002371
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002372 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002373 return 0;
2374}
2375
2376/**
2377 * smack_shm_free_security - Clear the security blob for shm
2378 * @shp: the object
2379 *
2380 * Clears the blob pointer
2381 */
2382static void smack_shm_free_security(struct shmid_kernel *shp)
2383{
2384 struct kern_ipc_perm *isp = &shp->shm_perm;
2385
2386 isp->security = NULL;
2387}
2388
2389/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002390 * smk_curacc_shm : check if current has access on shm
2391 * @shp : the object
2392 * @access : access requested
2393 *
2394 * Returns 0 if current has the requested access, error code otherwise
2395 */
2396static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2397{
2398 char *ssp = smack_of_shm(shp);
2399 struct smk_audit_info ad;
2400
2401#ifdef CONFIG_AUDIT
2402 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2403 ad.a.u.ipc_id = shp->shm_perm.id;
2404#endif
2405 return smk_curacc(ssp, access, &ad);
2406}
2407
2408/**
Casey Schauflere114e472008-02-04 22:29:50 -08002409 * smack_shm_associate - Smack access check for shm
2410 * @shp: the object
2411 * @shmflg: access requested
2412 *
2413 * Returns 0 if current has the requested access, error code otherwise
2414 */
2415static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2416{
Casey Schauflere114e472008-02-04 22:29:50 -08002417 int may;
2418
2419 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002420 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002421}
2422
2423/**
2424 * smack_shm_shmctl - Smack access check for shm
2425 * @shp: the object
2426 * @cmd: what it wants to do
2427 *
2428 * Returns 0 if current has the requested access, error code otherwise
2429 */
2430static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2431{
Casey Schauflere114e472008-02-04 22:29:50 -08002432 int may;
2433
2434 switch (cmd) {
2435 case IPC_STAT:
2436 case SHM_STAT:
2437 may = MAY_READ;
2438 break;
2439 case IPC_SET:
2440 case SHM_LOCK:
2441 case SHM_UNLOCK:
2442 case IPC_RMID:
2443 may = MAY_READWRITE;
2444 break;
2445 case IPC_INFO:
2446 case SHM_INFO:
2447 /*
2448 * System level information.
2449 */
2450 return 0;
2451 default:
2452 return -EINVAL;
2453 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002454 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002455}
2456
2457/**
2458 * smack_shm_shmat - Smack access for shmat
2459 * @shp: the object
2460 * @shmaddr: unused
2461 * @shmflg: access requested
2462 *
2463 * Returns 0 if current has the requested access, error code otherwise
2464 */
2465static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2466 int shmflg)
2467{
Casey Schauflere114e472008-02-04 22:29:50 -08002468 int may;
2469
2470 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002471 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002472}
2473
2474/**
2475 * smack_of_sem - the smack pointer for the sem
2476 * @sma: the object
2477 *
2478 * Returns a pointer to the smack value
2479 */
2480static char *smack_of_sem(struct sem_array *sma)
2481{
2482 return (char *)sma->sem_perm.security;
2483}
2484
2485/**
2486 * smack_sem_alloc_security - Set the security blob for sem
2487 * @sma: the object
2488 *
2489 * Returns 0
2490 */
2491static int smack_sem_alloc_security(struct sem_array *sma)
2492{
2493 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002494 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002495
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002496 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002497 return 0;
2498}
2499
2500/**
2501 * smack_sem_free_security - Clear the security blob for sem
2502 * @sma: the object
2503 *
2504 * Clears the blob pointer
2505 */
2506static void smack_sem_free_security(struct sem_array *sma)
2507{
2508 struct kern_ipc_perm *isp = &sma->sem_perm;
2509
2510 isp->security = NULL;
2511}
2512
2513/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002514 * smk_curacc_sem : check if current has access on sem
2515 * @sma : the object
2516 * @access : access requested
2517 *
2518 * Returns 0 if current has the requested access, error code otherwise
2519 */
2520static int smk_curacc_sem(struct sem_array *sma, int access)
2521{
2522 char *ssp = smack_of_sem(sma);
2523 struct smk_audit_info ad;
2524
2525#ifdef CONFIG_AUDIT
2526 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2527 ad.a.u.ipc_id = sma->sem_perm.id;
2528#endif
2529 return smk_curacc(ssp, access, &ad);
2530}
2531
2532/**
Casey Schauflere114e472008-02-04 22:29:50 -08002533 * smack_sem_associate - Smack access check for sem
2534 * @sma: the object
2535 * @semflg: access requested
2536 *
2537 * Returns 0 if current has the requested access, error code otherwise
2538 */
2539static int smack_sem_associate(struct sem_array *sma, int semflg)
2540{
Casey Schauflere114e472008-02-04 22:29:50 -08002541 int may;
2542
2543 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002544 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002545}
2546
2547/**
2548 * smack_sem_shmctl - Smack access check for sem
2549 * @sma: the object
2550 * @cmd: what it wants to do
2551 *
2552 * Returns 0 if current has the requested access, error code otherwise
2553 */
2554static int smack_sem_semctl(struct sem_array *sma, int cmd)
2555{
Casey Schauflere114e472008-02-04 22:29:50 -08002556 int may;
2557
2558 switch (cmd) {
2559 case GETPID:
2560 case GETNCNT:
2561 case GETZCNT:
2562 case GETVAL:
2563 case GETALL:
2564 case IPC_STAT:
2565 case SEM_STAT:
2566 may = MAY_READ;
2567 break;
2568 case SETVAL:
2569 case SETALL:
2570 case IPC_RMID:
2571 case IPC_SET:
2572 may = MAY_READWRITE;
2573 break;
2574 case IPC_INFO:
2575 case SEM_INFO:
2576 /*
2577 * System level information
2578 */
2579 return 0;
2580 default:
2581 return -EINVAL;
2582 }
2583
Etienne Bassetecfcc532009-04-08 20:40:06 +02002584 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002585}
2586
2587/**
2588 * smack_sem_semop - Smack checks of semaphore operations
2589 * @sma: the object
2590 * @sops: unused
2591 * @nsops: unused
2592 * @alter: unused
2593 *
2594 * Treated as read and write in all cases.
2595 *
2596 * Returns 0 if access is allowed, error code otherwise
2597 */
2598static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2599 unsigned nsops, int alter)
2600{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002601 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002602}
2603
2604/**
2605 * smack_msg_alloc_security - Set the security blob for msg
2606 * @msq: the object
2607 *
2608 * Returns 0
2609 */
2610static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2611{
2612 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002613 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002614
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002615 kisp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002616 return 0;
2617}
2618
2619/**
2620 * smack_msg_free_security - Clear the security blob for msg
2621 * @msq: the object
2622 *
2623 * Clears the blob pointer
2624 */
2625static void smack_msg_queue_free_security(struct msg_queue *msq)
2626{
2627 struct kern_ipc_perm *kisp = &msq->q_perm;
2628
2629 kisp->security = NULL;
2630}
2631
2632/**
2633 * smack_of_msq - the smack pointer for the msq
2634 * @msq: the object
2635 *
2636 * Returns a pointer to the smack value
2637 */
2638static char *smack_of_msq(struct msg_queue *msq)
2639{
2640 return (char *)msq->q_perm.security;
2641}
2642
2643/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002644 * smk_curacc_msq : helper to check if current has access on msq
2645 * @msq : the msq
2646 * @access : access requested
2647 *
2648 * return 0 if current has access, error otherwise
2649 */
2650static int smk_curacc_msq(struct msg_queue *msq, int access)
2651{
2652 char *msp = smack_of_msq(msq);
2653 struct smk_audit_info ad;
2654
2655#ifdef CONFIG_AUDIT
2656 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2657 ad.a.u.ipc_id = msq->q_perm.id;
2658#endif
2659 return smk_curacc(msp, access, &ad);
2660}
2661
2662/**
Casey Schauflere114e472008-02-04 22:29:50 -08002663 * smack_msg_queue_associate - Smack access check for msg_queue
2664 * @msq: the object
2665 * @msqflg: access requested
2666 *
2667 * Returns 0 if current has the requested access, error code otherwise
2668 */
2669static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2670{
Casey Schauflere114e472008-02-04 22:29:50 -08002671 int may;
2672
2673 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002674 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002675}
2676
2677/**
2678 * smack_msg_queue_msgctl - Smack access check for msg_queue
2679 * @msq: the object
2680 * @cmd: what it wants to do
2681 *
2682 * Returns 0 if current has the requested access, error code otherwise
2683 */
2684static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2685{
Casey Schauflere114e472008-02-04 22:29:50 -08002686 int may;
2687
2688 switch (cmd) {
2689 case IPC_STAT:
2690 case MSG_STAT:
2691 may = MAY_READ;
2692 break;
2693 case IPC_SET:
2694 case IPC_RMID:
2695 may = MAY_READWRITE;
2696 break;
2697 case IPC_INFO:
2698 case MSG_INFO:
2699 /*
2700 * System level information
2701 */
2702 return 0;
2703 default:
2704 return -EINVAL;
2705 }
2706
Etienne Bassetecfcc532009-04-08 20:40:06 +02002707 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002708}
2709
2710/**
2711 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2712 * @msq: the object
2713 * @msg: unused
2714 * @msqflg: access requested
2715 *
2716 * Returns 0 if current has the requested access, error code otherwise
2717 */
2718static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2719 int msqflg)
2720{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002721 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002722
Etienne Bassetecfcc532009-04-08 20:40:06 +02002723 may = smack_flags_to_may(msqflg);
2724 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002725}
2726
2727/**
2728 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2729 * @msq: the object
2730 * @msg: unused
2731 * @target: unused
2732 * @type: unused
2733 * @mode: unused
2734 *
2735 * Returns 0 if current has read and write access, error code otherwise
2736 */
2737static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2738 struct task_struct *target, long type, int mode)
2739{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002740 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002741}
2742
2743/**
2744 * smack_ipc_permission - Smack access for ipc_permission()
2745 * @ipp: the object permissions
2746 * @flag: access requested
2747 *
2748 * Returns 0 if current has read and write access, error code otherwise
2749 */
2750static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2751{
2752 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002753 int may = smack_flags_to_may(flag);
2754 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08002755
Etienne Bassetecfcc532009-04-08 20:40:06 +02002756#ifdef CONFIG_AUDIT
2757 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2758 ad.a.u.ipc_id = ipp->id;
2759#endif
2760 return smk_curacc(isp, may, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08002761}
2762
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002763/**
2764 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002765 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002766 * @secid: where result will be saved
2767 */
2768static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2769{
2770 char *smack = ipp->security;
2771
2772 *secid = smack_to_secid(smack);
2773}
2774
Casey Schauflere114e472008-02-04 22:29:50 -08002775/**
2776 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002777 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002778 * @inode: the object
2779 *
2780 * Set the inode's security blob if it hasn't been done already.
2781 */
2782static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2783{
2784 struct super_block *sbp;
2785 struct superblock_smack *sbsp;
2786 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002787 struct smack_known *skp;
2788 struct smack_known *ckp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002789 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002790 char trattr[TRANS_TRUE_SIZE];
2791 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07002792 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002793 struct dentry *dp;
2794
2795 if (inode == NULL)
2796 return;
2797
2798 isp = inode->i_security;
2799
2800 mutex_lock(&isp->smk_lock);
2801 /*
2802 * If the inode is already instantiated
2803 * take the quick way out
2804 */
2805 if (isp->smk_flags & SMK_INODE_INSTANT)
2806 goto unlockandout;
2807
2808 sbp = inode->i_sb;
2809 sbsp = sbp->s_security;
2810 /*
2811 * We're going to use the superblock default label
2812 * if there's no label on the file.
2813 */
2814 final = sbsp->smk_default;
2815
2816 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07002817 * If this is the root inode the superblock
2818 * may be in the process of initialization.
2819 * If that is the case use the root value out
2820 * of the superblock.
2821 */
2822 if (opt_dentry->d_parent == opt_dentry) {
Casey Schaufler36ea7352014-04-28 15:23:01 -07002823 if (sbp->s_magic == CGROUP_SUPER_MAGIC) {
2824 /*
2825 * The cgroup filesystem is never mounted,
2826 * so there's no opportunity to set the mount
2827 * options.
2828 */
2829 sbsp->smk_root = smack_known_star.smk_known;
2830 sbsp->smk_default = smack_known_star.smk_known;
2831 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07002832 isp->smk_inode = sbsp->smk_root;
2833 isp->smk_flags |= SMK_INODE_INSTANT;
2834 goto unlockandout;
2835 }
2836
2837 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002838 * This is pretty hackish.
2839 * Casey says that we shouldn't have to do
2840 * file system specific code, but it does help
2841 * with keeping it simple.
2842 */
2843 switch (sbp->s_magic) {
2844 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07002845 case PIPEFS_MAGIC:
2846 case SOCKFS_MAGIC:
2847 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08002848 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002849 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08002850 * that the smack file system doesn't do
2851 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07002852 *
Casey Schauflere114e472008-02-04 22:29:50 -08002853 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07002854 *
2855 * Socket access is controlled by the socket
2856 * structures associated with the task involved.
2857 *
2858 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08002859 */
2860 final = smack_known_star.smk_known;
2861 break;
2862 case DEVPTS_SUPER_MAGIC:
2863 /*
2864 * devpts seems content with the label of the task.
2865 * Programs that change smack have to treat the
2866 * pty with respect.
2867 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002868 final = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002869 break;
Casey Schauflere114e472008-02-04 22:29:50 -08002870 case PROC_SUPER_MAGIC:
2871 /*
2872 * Casey says procfs appears not to care.
2873 * The superblock default suffices.
2874 */
2875 break;
2876 case TMPFS_MAGIC:
2877 /*
2878 * Device labels should come from the filesystem,
2879 * but watch out, because they're volitile,
2880 * getting recreated on every reboot.
2881 */
2882 final = smack_known_star.smk_known;
2883 /*
2884 * No break.
2885 *
2886 * If a smack value has been set we want to use it,
2887 * but since tmpfs isn't giving us the opportunity
2888 * to set mount options simulate setting the
2889 * superblock default.
2890 */
2891 default:
2892 /*
2893 * This isn't an understood special case.
2894 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002895 */
2896
2897 /*
2898 * UNIX domain sockets use lower level socket data.
2899 */
2900 if (S_ISSOCK(inode->i_mode)) {
2901 final = smack_known_star.smk_known;
2902 break;
2903 }
2904 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002905 * No xattr support means, alas, no SMACK label.
2906 * Use the aforeapplied default.
2907 * It would be curious if the label of the task
2908 * does not match that assigned.
2909 */
2910 if (inode->i_op->getxattr == NULL)
2911 break;
2912 /*
2913 * Get the dentry for xattr.
2914 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002915 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002916 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
2917 if (skp != NULL)
2918 final = skp->smk_known;
Casey Schaufler2267b132012-03-13 19:14:19 -07002919
2920 /*
2921 * Transmuting directory
2922 */
2923 if (S_ISDIR(inode->i_mode)) {
2924 /*
2925 * If this is a new directory and the label was
2926 * transmuted when the inode was initialized
2927 * set the transmute attribute on the directory
2928 * and mark the inode.
2929 *
2930 * If there is a transmute attribute on the
2931 * directory mark the inode.
2932 */
2933 if (isp->smk_flags & SMK_INODE_CHANGED) {
2934 isp->smk_flags &= ~SMK_INODE_CHANGED;
2935 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002936 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07002937 TRANS_TRUE, TRANS_TRUE_SIZE,
2938 0);
2939 } else {
2940 rc = inode->i_op->getxattr(dp,
2941 XATTR_NAME_SMACKTRANSMUTE, trattr,
2942 TRANS_TRUE_SIZE);
2943 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
2944 TRANS_TRUE_SIZE) != 0)
2945 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002946 }
Casey Schaufler2267b132012-03-13 19:14:19 -07002947 if (rc >= 0)
2948 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002949 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08002950 /*
2951 * Don't let the exec or mmap label be "*" or "@".
2952 */
2953 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
2954 if (skp == &smack_known_star || skp == &smack_known_web)
2955 skp = NULL;
2956 isp->smk_task = skp;
2957 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
2958 if (skp == &smack_known_star || skp == &smack_known_web)
2959 skp = NULL;
2960 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08002961
Casey Schauflere114e472008-02-04 22:29:50 -08002962 dput(dp);
2963 break;
2964 }
2965
2966 if (final == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002967 isp->smk_inode = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002968 else
2969 isp->smk_inode = final;
2970
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002971 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08002972
2973unlockandout:
2974 mutex_unlock(&isp->smk_lock);
2975 return;
2976}
2977
2978/**
2979 * smack_getprocattr - Smack process attribute access
2980 * @p: the object task
2981 * @name: the name of the attribute in /proc/.../attr
2982 * @value: where to put the result
2983 *
2984 * Places a copy of the task Smack into value
2985 *
2986 * Returns the length of the smack label or an error code
2987 */
2988static int smack_getprocattr(struct task_struct *p, char *name, char **value)
2989{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002990 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08002991 char *cp;
2992 int slen;
2993
2994 if (strcmp(name, "current") != 0)
2995 return -EINVAL;
2996
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002997 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08002998 if (cp == NULL)
2999 return -ENOMEM;
3000
3001 slen = strlen(cp);
3002 *value = cp;
3003 return slen;
3004}
3005
3006/**
3007 * smack_setprocattr - Smack process attribute setting
3008 * @p: the object task
3009 * @name: the name of the attribute in /proc/.../attr
3010 * @value: the value to set
3011 * @size: the size of the value
3012 *
3013 * Sets the Smack value of the task. Only setting self
3014 * is permitted and only with privilege
3015 *
3016 * Returns the length of the smack label or an error code
3017 */
3018static int smack_setprocattr(struct task_struct *p, char *name,
3019 void *value, size_t size)
3020{
Casey Schaufler676dac42010-12-02 06:43:39 -08003021 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003022 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003023 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003024
Casey Schauflere114e472008-02-04 22:29:50 -08003025 /*
3026 * Changing another process' Smack value is too dangerous
3027 * and supports no sane use case.
3028 */
3029 if (p != current)
3030 return -EPERM;
3031
Casey Schaufler1880eff2012-06-05 15:28:30 -07003032 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003033 return -EPERM;
3034
Casey Schauflerf7112e62012-05-06 15:22:02 -07003035 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003036 return -EINVAL;
3037
3038 if (strcmp(name, "current") != 0)
3039 return -EINVAL;
3040
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003041 skp = smk_import_entry(value, size);
3042 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003043 return -EINVAL;
3044
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003045 /*
3046 * No process is ever allowed the web ("@") label.
3047 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003048 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003049 return -EPERM;
3050
David Howellsd84f4f92008-11-14 10:39:23 +11003051 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003052 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003053 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003054
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003055 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003056 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003057
David Howellsd84f4f92008-11-14 10:39:23 +11003058 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003059 return size;
3060}
3061
3062/**
3063 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003064 * @sock: one sock
3065 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003066 * @newsk: unused
3067 *
3068 * Return 0 if a subject with the smack of sock could access
3069 * an object with the smack of other, otherwise an error code
3070 */
David S. Miller3610cda2011-01-05 15:38:53 -08003071static int smack_unix_stream_connect(struct sock *sock,
3072 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003073{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003074 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003075 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003076 struct socket_smack *ssp = sock->sk_security;
3077 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003078 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003079 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003080 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003081#ifdef CONFIG_AUDIT
3082 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003083#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003084
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003085 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3086 skp = ssp->smk_out;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003087 okp = osp->smk_out;
3088#ifdef CONFIG_AUDIT
3089 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3090 smk_ad_setfield_u_net_sk(&ad, other);
3091#endif
3092 rc = smk_access(skp, okp->smk_known, MAY_WRITE, &ad);
3093 if (rc == 0)
3094 rc = smk_access(okp, okp->smk_known, MAY_WRITE, NULL);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003095 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003096
Casey Schaufler975d5e52011-09-26 14:43:39 -07003097 /*
3098 * Cross reference the peer labels for SO_PEERSEC.
3099 */
3100 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003101 nsp->smk_packet = ssp->smk_out;
3102 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003103 }
3104
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003105 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003106}
3107
3108/**
3109 * smack_unix_may_send - Smack access on UDS
3110 * @sock: one socket
3111 * @other: the other socket
3112 *
3113 * Return 0 if a subject with the smack of sock could access
3114 * an object with the smack of other, otherwise an error code
3115 */
3116static int smack_unix_may_send(struct socket *sock, struct socket *other)
3117{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003118 struct socket_smack *ssp = sock->sk->sk_security;
3119 struct socket_smack *osp = other->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003120 struct smack_known *skp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003121 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08003122
Kees Cook923e9a12012-04-10 13:26:44 -07003123#ifdef CONFIG_AUDIT
3124 struct lsm_network_audit net;
3125
Eric Paris48c62af2012-04-02 13:15:44 -04003126 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003127 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003128#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003129
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003130 if (smack_privileged(CAP_MAC_OVERRIDE))
3131 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003132
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003133 skp = ssp->smk_out;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003134 return smk_access(skp, osp->smk_in->smk_known, MAY_WRITE, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003135}
3136
3137/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003138 * smack_socket_sendmsg - Smack check based on destination host
3139 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003140 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003141 * @size: the size of the message
3142 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003143 * Return 0 if the current subject can write to the destination host.
3144 * For IPv4 this is only a question if the destination is a single label host.
3145 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003146 */
3147static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3148 int size)
3149{
3150 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003151 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003152 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003153
3154 /*
3155 * Perfectly reasonable for this to be NULL
3156 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003157 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003158 return 0;
3159
Casey Schauflerc6739442013-05-22 18:42:56 -07003160 switch (sip->sin_family) {
3161 case AF_INET:
3162 rc = smack_netlabel_send(sock->sk, sip);
3163 break;
3164 case AF_INET6:
3165 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3166 break;
3167 }
3168 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003169}
3170
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003171/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003172 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003173 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003174 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003175 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003176 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003177 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003178static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3179 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003180{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003181 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003182 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003183 int acat;
3184 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003185
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003186 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003187 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003188 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003189 * If there are flags but no level netlabel isn't
3190 * behaving the way we expect it to.
3191 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003192 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003193 * Without guidance regarding the smack value
3194 * for the packet fall back on the network
3195 * ambient value.
3196 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003197 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003198 list_for_each_entry(skp, &smack_known_list, list) {
3199 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003200 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003201 /*
3202 * Compare the catsets. Use the netlbl APIs.
3203 */
3204 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3205 if ((skp->smk_netlabel.flags &
3206 NETLBL_SECATTR_MLS_CAT) == 0)
3207 found = 1;
3208 break;
3209 }
3210 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003211 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3212 acat + 1);
3213 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003214 skp->smk_netlabel.attr.mls.cat,
3215 kcat + 1);
3216 if (acat < 0 || kcat < 0)
3217 break;
3218 }
3219 if (acat == kcat) {
3220 found = 1;
3221 break;
3222 }
Casey Schauflere114e472008-02-04 22:29:50 -08003223 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003224 rcu_read_unlock();
3225
3226 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003227 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003228
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003229 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003230 return &smack_known_web;
3231 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003232 }
3233 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3234 /*
3235 * Looks like a fallback, which gives us a secid.
3236 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003237 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003238 /*
3239 * This has got to be a bug because it is
3240 * impossible to specify a fallback without
3241 * specifying the label, which will ensure
3242 * it has a secid, and the only way to get a
3243 * secid is from a fallback.
3244 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003245 BUG_ON(skp == NULL);
3246 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003247 }
3248 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003249 * Without guidance regarding the smack value
3250 * for the packet fall back on the network
3251 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003252 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003253 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003254}
3255
Casey Schaufler6ea06242013-08-05 13:21:22 -07003256static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003257{
Casey Schauflerc6739442013-05-22 18:42:56 -07003258 u8 nexthdr;
3259 int offset;
3260 int proto = -EINVAL;
3261 struct ipv6hdr _ipv6h;
3262 struct ipv6hdr *ip6;
3263 __be16 frag_off;
3264 struct tcphdr _tcph, *th;
3265 struct udphdr _udph, *uh;
3266 struct dccp_hdr _dccph, *dh;
3267
3268 sip->sin6_port = 0;
3269
3270 offset = skb_network_offset(skb);
3271 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3272 if (ip6 == NULL)
3273 return -EINVAL;
3274 sip->sin6_addr = ip6->saddr;
3275
3276 nexthdr = ip6->nexthdr;
3277 offset += sizeof(_ipv6h);
3278 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3279 if (offset < 0)
3280 return -EINVAL;
3281
3282 proto = nexthdr;
3283 switch (proto) {
3284 case IPPROTO_TCP:
3285 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3286 if (th != NULL)
3287 sip->sin6_port = th->source;
3288 break;
3289 case IPPROTO_UDP:
3290 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3291 if (uh != NULL)
3292 sip->sin6_port = uh->source;
3293 break;
3294 case IPPROTO_DCCP:
3295 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3296 if (dh != NULL)
3297 sip->sin6_port = dh->dccph_sport;
3298 break;
3299 }
3300 return proto;
3301}
3302
Casey Schauflere114e472008-02-04 22:29:50 -08003303/**
3304 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3305 * @sk: socket
3306 * @skb: packet
3307 *
3308 * Returns 0 if the packet should be delivered, an error code otherwise
3309 */
3310static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3311{
3312 struct netlbl_lsm_secattr secattr;
3313 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003314 struct smack_known *skp;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003315 struct sockaddr_in6 sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003316 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003317 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003318#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003319 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003320#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003321 switch (sk->sk_family) {
3322 case PF_INET:
3323 /*
3324 * Translate what netlabel gave us.
3325 */
3326 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003327
Casey Schauflerc6739442013-05-22 18:42:56 -07003328 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3329 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003330 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003331 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003332 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003333
Casey Schauflerc6739442013-05-22 18:42:56 -07003334 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003335
Etienne Bassetecfcc532009-04-08 20:40:06 +02003336#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003337 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3338 ad.a.u.net->family = sk->sk_family;
3339 ad.a.u.net->netif = skb->skb_iif;
3340 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003341#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003342 /*
3343 * Receiving a packet requires that the other end
3344 * be able to write here. Read access is not required.
3345 * This is the simplist possible security model
3346 * for networking.
3347 */
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003348 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
Casey Schauflerc6739442013-05-22 18:42:56 -07003349 if (rc != 0)
3350 netlbl_skbuff_err(skb, rc, 0);
3351 break;
3352 case PF_INET6:
3353 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3354 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3355 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3356 else
3357 rc = 0;
3358 break;
3359 }
Paul Moorea8134292008-10-10 10:16:31 -04003360 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003361}
3362
3363/**
3364 * smack_socket_getpeersec_stream - pull in packet label
3365 * @sock: the socket
3366 * @optval: user's destination
3367 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003368 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003369 *
3370 * returns zero on success, an error code otherwise
3371 */
3372static int smack_socket_getpeersec_stream(struct socket *sock,
3373 char __user *optval,
3374 int __user *optlen, unsigned len)
3375{
3376 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003377 char *rcp = "";
3378 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003379 int rc = 0;
3380
3381 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003382 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003383 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003384 slen = strlen(rcp) + 1;
3385 }
Casey Schauflere114e472008-02-04 22:29:50 -08003386
3387 if (slen > len)
3388 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003389 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003390 rc = -EFAULT;
3391
3392 if (put_user(slen, optlen) != 0)
3393 rc = -EFAULT;
3394
3395 return rc;
3396}
3397
3398
3399/**
3400 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003401 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003402 * @skb: packet data
3403 * @secid: pointer to where to put the secid of the packet
3404 *
3405 * Sets the netlabel socket state on sk from parent
3406 */
3407static int smack_socket_getpeersec_dgram(struct socket *sock,
3408 struct sk_buff *skb, u32 *secid)
3409
3410{
3411 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003412 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003413 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003414 int family = PF_UNSPEC;
3415 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003416 int rc;
3417
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003418 if (skb != NULL) {
3419 if (skb->protocol == htons(ETH_P_IP))
3420 family = PF_INET;
3421 else if (skb->protocol == htons(ETH_P_IPV6))
3422 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003423 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003424 if (family == PF_UNSPEC && sock != NULL)
3425 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003426
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003427 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003428 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003429 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003430 } else if (family == PF_INET || family == PF_INET6) {
3431 /*
3432 * Translate what netlabel gave us.
3433 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003434 if (sock != NULL && sock->sk != NULL)
3435 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003436 netlbl_secattr_init(&secattr);
3437 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3438 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003439 skp = smack_from_secattr(&secattr, ssp);
3440 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003441 }
3442 netlbl_secattr_destroy(&secattr);
3443 }
3444 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003445 if (s == 0)
3446 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003447 return 0;
3448}
3449
3450/**
Paul Moore07feee82009-03-27 17:10:54 -04003451 * smack_sock_graft - Initialize a newly created socket with an existing sock
3452 * @sk: child sock
3453 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003454 *
Paul Moore07feee82009-03-27 17:10:54 -04003455 * Set the smk_{in,out} state of an existing sock based on the process that
3456 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003457 */
3458static void smack_sock_graft(struct sock *sk, struct socket *parent)
3459{
3460 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003461 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003462
Paul Moore07feee82009-03-27 17:10:54 -04003463 if (sk == NULL ||
3464 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003465 return;
3466
3467 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003468 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003469 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003470 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003471}
3472
3473/**
3474 * smack_inet_conn_request - Smack access check on connect
3475 * @sk: socket involved
3476 * @skb: packet
3477 * @req: unused
3478 *
3479 * Returns 0 if a task with the packet label could write to
3480 * the socket, otherwise an error code
3481 */
3482static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3483 struct request_sock *req)
3484{
Paul Moore07feee82009-03-27 17:10:54 -04003485 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003486 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003487 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003488 struct netlbl_lsm_secattr secattr;
3489 struct sockaddr_in addr;
3490 struct iphdr *hdr;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003491 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003492 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003493 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003494#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003495 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003496#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003497
Casey Schauflerc6739442013-05-22 18:42:56 -07003498 if (family == PF_INET6) {
3499 /*
3500 * Handle mapped IPv4 packets arriving
3501 * via IPv6 sockets. Don't set up netlabel
3502 * processing on IPv6.
3503 */
3504 if (skb->protocol == htons(ETH_P_IP))
3505 family = PF_INET;
3506 else
3507 return 0;
3508 }
Casey Schauflere114e472008-02-04 22:29:50 -08003509
Paul Moore07feee82009-03-27 17:10:54 -04003510 netlbl_secattr_init(&secattr);
3511 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003512 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003513 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003514 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003515 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003516 netlbl_secattr_destroy(&secattr);
3517
Etienne Bassetecfcc532009-04-08 20:40:06 +02003518#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003519 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3520 ad.a.u.net->family = family;
3521 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003522 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3523#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003524 /*
Paul Moore07feee82009-03-27 17:10:54 -04003525 * Receiving a packet requires that the other end be able to write
3526 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003527 */
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003528 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
Paul Moore07feee82009-03-27 17:10:54 -04003529 if (rc != 0)
3530 return rc;
3531
3532 /*
3533 * Save the peer's label in the request_sock so we can later setup
3534 * smk_packet in the child socket so that SO_PEERCRED can report it.
3535 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003536 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003537
3538 /*
3539 * We need to decide if we want to label the incoming connection here
3540 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003541 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003542 */
3543 hdr = ip_hdr(skb);
3544 addr.sin_addr.s_addr = hdr->saddr;
3545 rcu_read_lock();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003546 hsp = smack_host_label(&addr);
3547 rcu_read_unlock();
3548
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003549 if (hsp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003550 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003551 else
Paul Moore07feee82009-03-27 17:10:54 -04003552 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003553
3554 return rc;
3555}
3556
Paul Moore07feee82009-03-27 17:10:54 -04003557/**
3558 * smack_inet_csk_clone - Copy the connection information to the new socket
3559 * @sk: the new socket
3560 * @req: the connection's request_sock
3561 *
3562 * Transfer the connection's peer label to the newly created socket.
3563 */
3564static void smack_inet_csk_clone(struct sock *sk,
3565 const struct request_sock *req)
3566{
3567 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003568 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003569
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003570 if (req->peer_secid != 0) {
3571 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003572 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003573 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003574 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003575}
3576
Casey Schauflere114e472008-02-04 22:29:50 -08003577/*
3578 * Key management security hooks
3579 *
3580 * Casey has not tested key support very heavily.
3581 * The permission check is most likely too restrictive.
3582 * If you care about keys please have a look.
3583 */
3584#ifdef CONFIG_KEYS
3585
3586/**
3587 * smack_key_alloc - Set the key security blob
3588 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003589 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003590 * @flags: unused
3591 *
3592 * No allocation required
3593 *
3594 * Returns 0
3595 */
David Howellsd84f4f92008-11-14 10:39:23 +11003596static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003597 unsigned long flags)
3598{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003599 struct smack_known *skp = smk_of_task(cred->security);
3600
3601 key->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003602 return 0;
3603}
3604
3605/**
3606 * smack_key_free - Clear the key security blob
3607 * @key: the object
3608 *
3609 * Clear the blob pointer
3610 */
3611static void smack_key_free(struct key *key)
3612{
3613 key->security = NULL;
3614}
3615
3616/*
3617 * smack_key_permission - Smack access on a key
3618 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003619 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003620 * @perm: unused
3621 *
3622 * Return 0 if the task has read and write to the object,
3623 * an error code otherwise
3624 */
3625static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00003626 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003627{
3628 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003629 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003630 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003631 int request = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08003632
3633 keyp = key_ref_to_ptr(key_ref);
3634 if (keyp == NULL)
3635 return -EINVAL;
3636 /*
3637 * If the key hasn't been initialized give it access so that
3638 * it may do so.
3639 */
3640 if (keyp->security == NULL)
3641 return 0;
3642 /*
3643 * This should not occur
3644 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003645 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003646 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003647#ifdef CONFIG_AUDIT
3648 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3649 ad.a.u.key_struct.key = keyp->serial;
3650 ad.a.u.key_struct.key_desc = keyp->description;
3651#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003652 if (perm & KEY_NEED_READ)
3653 request = MAY_READ;
3654 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3655 request = MAY_WRITE;
3656 return smk_access(tkp, keyp->security, request, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08003657}
3658#endif /* CONFIG_KEYS */
3659
3660/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003661 * Smack Audit hooks
3662 *
3663 * Audit requires a unique representation of each Smack specific
3664 * rule. This unique representation is used to distinguish the
3665 * object to be audited from remaining kernel objects and also
3666 * works as a glue between the audit hooks.
3667 *
3668 * Since repository entries are added but never deleted, we'll use
3669 * the smack_known label address related to the given audit rule as
3670 * the needed unique representation. This also better fits the smack
3671 * model where nearly everything is a label.
3672 */
3673#ifdef CONFIG_AUDIT
3674
3675/**
3676 * smack_audit_rule_init - Initialize a smack audit rule
3677 * @field: audit rule fields given from user-space (audit.h)
3678 * @op: required testing operator (=, !=, >, <, ...)
3679 * @rulestr: smack label to be audited
3680 * @vrule: pointer to save our own audit rule representation
3681 *
3682 * Prepare to audit cases where (@field @op @rulestr) is true.
3683 * The label to be audited is created if necessay.
3684 */
3685static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3686{
3687 char **rule = (char **)vrule;
3688 *rule = NULL;
3689
3690 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3691 return -EINVAL;
3692
Al Viro5af75d82008-12-16 05:59:26 -05003693 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003694 return -EINVAL;
3695
3696 *rule = smk_import(rulestr, 0);
3697
3698 return 0;
3699}
3700
3701/**
3702 * smack_audit_rule_known - Distinguish Smack audit rules
3703 * @krule: rule of interest, in Audit kernel representation format
3704 *
3705 * This is used to filter Smack rules from remaining Audit ones.
3706 * If it's proved that this rule belongs to us, the
3707 * audit_rule_match hook will be called to do the final judgement.
3708 */
3709static int smack_audit_rule_known(struct audit_krule *krule)
3710{
3711 struct audit_field *f;
3712 int i;
3713
3714 for (i = 0; i < krule->field_count; i++) {
3715 f = &krule->fields[i];
3716
3717 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3718 return 1;
3719 }
3720
3721 return 0;
3722}
3723
3724/**
3725 * smack_audit_rule_match - Audit given object ?
3726 * @secid: security id for identifying the object to test
3727 * @field: audit rule flags given from user-space
3728 * @op: required testing operator
3729 * @vrule: smack internal rule presentation
3730 * @actx: audit context associated with the check
3731 *
3732 * The core Audit hook. It's used to take the decision of
3733 * whether to audit or not to audit a given object.
3734 */
3735static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3736 struct audit_context *actx)
3737{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003738 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003739 char *rule = vrule;
3740
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05003741 if (unlikely(!rule)) {
3742 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003743 return -ENOENT;
3744 }
3745
3746 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3747 return 0;
3748
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003749 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003750
3751 /*
3752 * No need to do string comparisons. If a match occurs,
3753 * both pointers will point to the same smack_known
3754 * label.
3755 */
Al Viro5af75d82008-12-16 05:59:26 -05003756 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003757 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003758 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003759 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003760
3761 return 0;
3762}
3763
3764/**
3765 * smack_audit_rule_free - free smack rule representation
3766 * @vrule: rule to be freed.
3767 *
3768 * No memory was allocated.
3769 */
3770static void smack_audit_rule_free(void *vrule)
3771{
3772 /* No-op */
3773}
3774
3775#endif /* CONFIG_AUDIT */
3776
Randy Dunlap251a2a92009-02-18 11:42:33 -08003777/**
David Quigley746df9b2013-05-22 12:50:35 -04003778 * smack_ismaclabel - check if xattr @name references a smack MAC label
3779 * @name: Full xattr name to check.
3780 */
3781static int smack_ismaclabel(const char *name)
3782{
3783 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
3784}
3785
3786
3787/**
Casey Schauflere114e472008-02-04 22:29:50 -08003788 * smack_secid_to_secctx - return the smack label for a secid
3789 * @secid: incoming integer
3790 * @secdata: destination
3791 * @seclen: how long it is
3792 *
3793 * Exists for networking code.
3794 */
3795static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
3796{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003797 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003798
Eric Parisd5630b92010-10-13 16:24:48 -04003799 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003800 *secdata = skp->smk_known;
3801 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08003802 return 0;
3803}
3804
Randy Dunlap251a2a92009-02-18 11:42:33 -08003805/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003806 * smack_secctx_to_secid - return the secid for a smack label
3807 * @secdata: smack label
3808 * @seclen: how long result is
3809 * @secid: outgoing integer
3810 *
3811 * Exists for audit and networking code.
3812 */
David Howellse52c17642008-04-29 20:52:51 +01003813static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003814{
3815 *secid = smack_to_secid(secdata);
3816 return 0;
3817}
3818
Randy Dunlap251a2a92009-02-18 11:42:33 -08003819/**
Casey Schauflere114e472008-02-04 22:29:50 -08003820 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08003821 * @secdata: unused
3822 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08003823 *
3824 * Exists to make sure nothing gets done, and properly
3825 */
3826static void smack_release_secctx(char *secdata, u32 seclen)
3827{
3828}
3829
David P. Quigley1ee65e32009-09-03 14:25:57 -04003830static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
3831{
3832 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
3833}
3834
3835static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
3836{
3837 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
3838}
3839
3840static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
3841{
3842 int len = 0;
3843 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
3844
3845 if (len < 0)
3846 return len;
3847 *ctxlen = len;
3848 return 0;
3849}
3850
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02003851struct security_operations smack_ops = {
3852 .name = "smack",
3853
Ingo Molnar9e488582009-05-07 19:26:19 +10003854 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01003855 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08003856 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08003857
3858 .sb_alloc_security = smack_sb_alloc_security,
3859 .sb_free_security = smack_sb_free_security,
3860 .sb_copy_data = smack_sb_copy_data,
3861 .sb_kern_mount = smack_sb_kern_mount,
3862 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08003863
Casey Schaufler676dac42010-12-02 06:43:39 -08003864 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03003865 .bprm_committing_creds = smack_bprm_committing_creds,
3866 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08003867
Casey Schauflere114e472008-02-04 22:29:50 -08003868 .inode_alloc_security = smack_inode_alloc_security,
3869 .inode_free_security = smack_inode_free_security,
3870 .inode_init_security = smack_inode_init_security,
3871 .inode_link = smack_inode_link,
3872 .inode_unlink = smack_inode_unlink,
3873 .inode_rmdir = smack_inode_rmdir,
3874 .inode_rename = smack_inode_rename,
3875 .inode_permission = smack_inode_permission,
3876 .inode_setattr = smack_inode_setattr,
3877 .inode_getattr = smack_inode_getattr,
3878 .inode_setxattr = smack_inode_setxattr,
3879 .inode_post_setxattr = smack_inode_post_setxattr,
3880 .inode_getxattr = smack_inode_getxattr,
3881 .inode_removexattr = smack_inode_removexattr,
3882 .inode_getsecurity = smack_inode_getsecurity,
3883 .inode_setsecurity = smack_inode_setsecurity,
3884 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003885 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003886
3887 .file_permission = smack_file_permission,
3888 .file_alloc_security = smack_file_alloc_security,
3889 .file_free_security = smack_file_free_security,
3890 .file_ioctl = smack_file_ioctl,
3891 .file_lock = smack_file_lock,
3892 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04003893 .mmap_file = smack_mmap_file,
3894 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08003895 .file_set_fowner = smack_file_set_fowner,
3896 .file_send_sigiotask = smack_file_send_sigiotask,
3897 .file_receive = smack_file_receive,
3898
Eric Paris83d49852012-04-04 13:45:40 -04003899 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07003900
David Howellsee18d642009-09-02 09:14:21 +01003901 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11003902 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11003903 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01003904 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11003905 .kernel_act_as = smack_kernel_act_as,
3906 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08003907 .task_setpgid = smack_task_setpgid,
3908 .task_getpgid = smack_task_getpgid,
3909 .task_getsid = smack_task_getsid,
3910 .task_getsecid = smack_task_getsecid,
3911 .task_setnice = smack_task_setnice,
3912 .task_setioprio = smack_task_setioprio,
3913 .task_getioprio = smack_task_getioprio,
3914 .task_setscheduler = smack_task_setscheduler,
3915 .task_getscheduler = smack_task_getscheduler,
3916 .task_movememory = smack_task_movememory,
3917 .task_kill = smack_task_kill,
3918 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08003919 .task_to_inode = smack_task_to_inode,
3920
3921 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003922 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08003923
3924 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
3925 .msg_msg_free_security = smack_msg_msg_free_security,
3926
3927 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
3928 .msg_queue_free_security = smack_msg_queue_free_security,
3929 .msg_queue_associate = smack_msg_queue_associate,
3930 .msg_queue_msgctl = smack_msg_queue_msgctl,
3931 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
3932 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
3933
3934 .shm_alloc_security = smack_shm_alloc_security,
3935 .shm_free_security = smack_shm_free_security,
3936 .shm_associate = smack_shm_associate,
3937 .shm_shmctl = smack_shm_shmctl,
3938 .shm_shmat = smack_shm_shmat,
3939
3940 .sem_alloc_security = smack_sem_alloc_security,
3941 .sem_free_security = smack_sem_free_security,
3942 .sem_associate = smack_sem_associate,
3943 .sem_semctl = smack_sem_semctl,
3944 .sem_semop = smack_sem_semop,
3945
Casey Schauflere114e472008-02-04 22:29:50 -08003946 .d_instantiate = smack_d_instantiate,
3947
3948 .getprocattr = smack_getprocattr,
3949 .setprocattr = smack_setprocattr,
3950
3951 .unix_stream_connect = smack_unix_stream_connect,
3952 .unix_may_send = smack_unix_may_send,
3953
3954 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07003955 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003956 .socket_connect = smack_socket_connect,
3957 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08003958 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
3959 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
3960 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
3961 .sk_alloc_security = smack_sk_alloc_security,
3962 .sk_free_security = smack_sk_free_security,
3963 .sock_graft = smack_sock_graft,
3964 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04003965 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003966
Casey Schauflere114e472008-02-04 22:29:50 -08003967 /* key management security hooks */
3968#ifdef CONFIG_KEYS
3969 .key_alloc = smack_key_alloc,
3970 .key_free = smack_key_free,
3971 .key_permission = smack_key_permission,
3972#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003973
3974 /* Audit hooks */
3975#ifdef CONFIG_AUDIT
3976 .audit_rule_init = smack_audit_rule_init,
3977 .audit_rule_known = smack_audit_rule_known,
3978 .audit_rule_match = smack_audit_rule_match,
3979 .audit_rule_free = smack_audit_rule_free,
3980#endif /* CONFIG_AUDIT */
3981
David Quigley746df9b2013-05-22 12:50:35 -04003982 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08003983 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08003984 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08003985 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04003986 .inode_notifysecctx = smack_inode_notifysecctx,
3987 .inode_setsecctx = smack_inode_setsecctx,
3988 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08003989};
3990
Etienne Basset7198e2e2009-03-24 20:53:24 +01003991
Casey Schaufler86812bb2012-04-17 18:55:46 -07003992static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01003993{
Casey Schaufler86812bb2012-04-17 18:55:46 -07003994 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07003995 * Initialize rule list locks
3996 */
3997 mutex_init(&smack_known_huh.smk_rules_lock);
3998 mutex_init(&smack_known_hat.smk_rules_lock);
3999 mutex_init(&smack_known_floor.smk_rules_lock);
4000 mutex_init(&smack_known_star.smk_rules_lock);
4001 mutex_init(&smack_known_invalid.smk_rules_lock);
4002 mutex_init(&smack_known_web.smk_rules_lock);
4003 /*
4004 * Initialize rule lists
4005 */
4006 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4007 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4008 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4009 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4010 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4011 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4012 /*
4013 * Create the known labels list
4014 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004015 smk_insert_entry(&smack_known_huh);
4016 smk_insert_entry(&smack_known_hat);
4017 smk_insert_entry(&smack_known_star);
4018 smk_insert_entry(&smack_known_floor);
4019 smk_insert_entry(&smack_known_invalid);
4020 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004021}
4022
Casey Schauflere114e472008-02-04 22:29:50 -08004023/**
4024 * smack_init - initialize the smack system
4025 *
4026 * Returns 0
4027 */
4028static __init int smack_init(void)
4029{
David Howellsd84f4f92008-11-14 10:39:23 +11004030 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004031 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004032
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004033 if (!security_module_enable(&smack_ops))
4034 return 0;
4035
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004036 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4037 GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08004038 if (tsp == NULL)
4039 return -ENOMEM;
4040
Casey Schauflere114e472008-02-04 22:29:50 -08004041 printk(KERN_INFO "Smack: Initializing.\n");
4042
4043 /*
4044 * Set the security state for the initial task.
4045 */
David Howellsd84f4f92008-11-14 10:39:23 +11004046 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004047 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004048
Casey Schaufler86812bb2012-04-17 18:55:46 -07004049 /* initialize the smack_known_list */
4050 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004051
4052 /*
4053 * Register with LSM
4054 */
4055 if (register_security(&smack_ops))
4056 panic("smack: Unable to register with kernel.\n");
4057
4058 return 0;
4059}
4060
4061/*
4062 * Smack requires early initialization in order to label
4063 * all processes and objects when they are created.
4064 */
4065security_initcall(smack_init);